19#include "llvm/ADT/SmallPtrSet.h"
20#include "llvm/Support/Debug.h"
22#define DEBUG_TYPE "format-token-annotator"
31static bool startsWithInitStatement(
const AnnotatedLine &Line) {
47static bool canBeObjCSelectorComponent(
const FormatToken &Tok) {
48 return Tok.Tok.getIdentifierInfo();
53static bool isLambdaParameterList(
const FormatToken *Left) {
55 if (Left->Previous && Left->Previous->is(tok::greater) &&
56 Left->Previous->MatchingParen &&
57 Left->Previous->MatchingParen->is(TT_TemplateOpener)) {
58 Left = Left->Previous->MatchingParen;
62 return Left->Previous && Left->Previous->is(tok::r_square) &&
63 Left->Previous->MatchingParen &&
64 Left->Previous->MatchingParen->is(TT_LambdaLSquare);
69static bool isKeywordWithCondition(
const FormatToken &Tok) {
70 return Tok.isOneOf(tok::kw_if, tok::kw_for, tok::kw_while, tok::kw_switch,
71 tok::kw_constexpr, tok::kw_catch);
75static bool isCppAttribute(
bool IsCpp,
const FormatToken &Tok) {
76 if (!IsCpp || !Tok.startsSequence(tok::l_square, tok::l_square))
79 if (Tok.Previous && Tok.Previous->is(tok::at))
81 const FormatToken *AttrTok = Tok.Next->Next;
86 if (AttrTok->startsSequence(tok::kw_using, tok::identifier, tok::colon))
88 if (AttrTok->isNot(tok::identifier))
90 while (AttrTok && !AttrTok->startsSequence(tok::r_square, tok::r_square)) {
94 if (AttrTok->is(tok::colon) ||
95 AttrTok->startsSequence(tok::identifier, tok::identifier) ||
96 AttrTok->startsSequence(tok::r_paren, tok::identifier)) {
99 if (AttrTok->is(tok::ellipsis))
101 AttrTok = AttrTok->Next;
103 return AttrTok && AttrTok->startsSequence(tok::r_square, tok::r_square);
111class AnnotatingParser {
113 AnnotatingParser(
const FormatStyle &Style, AnnotatedLine &Line,
114 const AdditionalKeywords &Keywords,
115 SmallVector<ScopeType> &Scopes)
116 : Style(Style), Line(Line), CurrentToken(Line.First), AutoFound(
false),
117 Keywords(Keywords), Scopes(Scopes) {
118 Contexts.push_back(Context(tok::unknown, 1,
false));
119 resetTokenMetadata();
123 ScopeType getScopeType(
const FormatToken &Token)
const {
124 switch (Token.getType()) {
125 case TT_FunctionLBrace:
126 case TT_LambdaLBrace:
129 case TT_StructLBrace:
138 if (!CurrentToken || !CurrentToken->Previous)
140 if (NonTemplateLess.count(CurrentToken->Previous))
143 const FormatToken &
Previous = *CurrentToken->Previous;
145 if (
Previous.Previous->Tok.isLiteral())
147 if (
Previous.Previous->is(tok::r_paren) && Contexts.size() > 1 &&
148 (!
Previous.Previous->MatchingParen ||
149 !
Previous.Previous->MatchingParen->is(
150 TT_OverloadedOperatorLParen))) {
155 FormatToken *Left = CurrentToken->Previous;
156 Left->ParentBracket = Contexts.back().ContextKind;
157 ScopedContextCreator ContextCreator(*
this, tok::less, 12);
161 bool InExprContext = Contexts.back().IsExpression;
163 Contexts.back().IsExpression =
false;
166 if (Left->Previous && Left->Previous->isNot(tok::kw_template))
167 Contexts.back().ContextType = Context::TemplateArgument;
170 CurrentToken->is(tok::question)) {
174 while (CurrentToken) {
175 if (CurrentToken->is(tok::greater)) {
182 if (CurrentToken->Next && CurrentToken->Next->is(tok::greater) &&
183 Left->ParentBracket != tok::less &&
184 CurrentToken->getStartOfNonWhitespace() ==
185 CurrentToken->Next->getStartOfNonWhitespace().getLocWithOffset(
189 Left->MatchingParen = CurrentToken;
190 CurrentToken->MatchingParen = Left;
198 Left->Previous->isOneOf(TT_SelectorName, TT_DictLiteral))) {
199 CurrentToken->setType(TT_DictLiteral);
201 CurrentToken->setType(TT_TemplateCloser);
203 if (CurrentToken->Next && CurrentToken->Next->Tok.isLiteral())
208 if (CurrentToken->is(tok::question) &&
213 if (CurrentToken->isOneOf(tok::r_paren, tok::r_square, tok::r_brace) ||
214 (CurrentToken->isOneOf(tok::colon, tok::question) && InExprContext &&
225 if (CurrentToken->Previous->isOneOf(tok::pipepipe, tok::ampamp) &&
226 CurrentToken->Previous->is(TT_BinaryOperator) &&
227 Contexts[Contexts.size() - 2].IsExpression &&
228 !Line.startsWith(tok::kw_template)) {
231 updateParameterCount(Left, CurrentToken);
233 if (FormatToken *
Previous = CurrentToken->getPreviousNonComment()) {
234 if (CurrentToken->is(tok::colon) ||
235 (CurrentToken->isOneOf(tok::l_brace, tok::less) &&
247 bool parseUntouchableParens() {
248 while (CurrentToken) {
249 CurrentToken->Finalized =
true;
250 switch (CurrentToken->Tok.getKind()) {
253 if (!parseUntouchableParens())
268 bool parseParens(
bool LookForDecls =
false) {
271 assert(CurrentToken->Previous &&
"Unknown previous token");
272 FormatToken &OpeningParen = *CurrentToken->Previous;
273 assert(OpeningParen.is(tok::l_paren));
274 FormatToken *PrevNonComment = OpeningParen.getPreviousNonComment();
275 OpeningParen.ParentBracket = Contexts.back().ContextKind;
276 ScopedContextCreator ContextCreator(*
this, tok::l_paren, 1);
279 Contexts.back().ColonIsForRangeExpr =
280 Contexts.size() == 2 && Contexts[0].ColonIsForRangeExpr;
282 if (OpeningParen.Previous &&
283 OpeningParen.Previous->is(TT_UntouchableMacroFunc)) {
284 OpeningParen.Finalized =
true;
285 return parseUntouchableParens();
288 bool StartsObjCMethodExpr =
false;
289 if (!Style.isVerilog()) {
290 if (FormatToken *MaybeSel = OpeningParen.Previous) {
292 if (MaybeSel->isObjCAtKeyword(tok::objc_selector) &&
293 MaybeSel->Previous && MaybeSel->Previous->is(tok::at)) {
294 StartsObjCMethodExpr =
true;
299 if (OpeningParen.is(TT_OverloadedOperatorLParen)) {
301 FormatToken *Prev = &OpeningParen;
302 while (!Prev->is(tok::kw_operator)) {
303 Prev = Prev->Previous;
304 assert(Prev &&
"Expect a kw_operator prior to the OperatorLParen!");
310 bool OperatorCalledAsMemberFunction =
311 Prev->Previous && Prev->Previous->isOneOf(tok::period, tok::arrow);
312 Contexts.back().IsExpression = OperatorCalledAsMemberFunction;
313 }
else if (Style.isJavaScript() &&
314 (Line.startsWith(Keywords.kw_type, tok::identifier) ||
315 Line.startsWith(tok::kw_export, Keywords.kw_type,
319 Contexts.back().IsExpression =
false;
320 }
else if (OpeningParen.Previous &&
321 (OpeningParen.Previous->isOneOf(
322 tok::kw_static_assert, tok::kw_noexcept, tok::kw_explicit,
323 tok::kw_while, tok::l_paren, tok::comma,
324 TT_BinaryOperator) ||
325 OpeningParen.Previous->isIf())) {
327 Contexts.back().IsExpression =
true;
328 }
else if (Style.isJavaScript() && OpeningParen.Previous &&
329 (OpeningParen.Previous->is(Keywords.kw_function) ||
330 (OpeningParen.Previous->endsSequence(tok::identifier,
331 Keywords.kw_function)))) {
333 Contexts.back().IsExpression =
false;
334 }
else if (Style.isJavaScript() && OpeningParen.Previous &&
335 OpeningParen.Previous->is(TT_JsTypeColon)) {
337 Contexts.back().IsExpression =
false;
338 }
else if (isLambdaParameterList(&OpeningParen)) {
340 Contexts.back().IsExpression =
false;
341 }
else if (OpeningParen.is(TT_RequiresExpressionLParen)) {
342 Contexts.back().IsExpression =
false;
343 }
else if (OpeningParen.Previous &&
344 OpeningParen.Previous->is(tok::kw__Generic)) {
345 Contexts.back().ContextType = Context::C11GenericSelection;
346 Contexts.back().IsExpression =
true;
347 }
else if (Line.InPPDirective &&
348 (!OpeningParen.Previous ||
349 !OpeningParen.Previous->is(tok::identifier))) {
350 Contexts.back().IsExpression =
true;
351 }
else if (Contexts[Contexts.size() - 2].CaretFound) {
353 Contexts.back().IsExpression =
false;
354 }
else if (OpeningParen.Previous &&
355 OpeningParen.Previous->is(TT_ForEachMacro)) {
357 Contexts.back().ContextType = Context::ForEachMacro;
358 Contexts.back().IsExpression =
false;
359 }
else if (OpeningParen.Previous && OpeningParen.Previous->MatchingParen &&
360 OpeningParen.Previous->MatchingParen->isOneOf(
361 TT_ObjCBlockLParen, TT_FunctionTypeLParen)) {
362 Contexts.back().IsExpression =
false;
363 }
else if (!Line.MustBeDeclaration && !Line.InPPDirective) {
365 OpeningParen.Previous &&
366 OpeningParen.Previous->isOneOf(tok::kw_for, tok::kw_catch);
367 Contexts.back().IsExpression = !IsForOrCatch;
372 if (PrevNonComment && OpeningParen.is(TT_Unknown)) {
373 if (PrevNonComment->is(tok::kw___attribute)) {
374 OpeningParen.setType(TT_AttributeParen);
375 }
else if (PrevNonComment->isOneOf(TT_TypenameMacro, tok::kw_decltype,
378#include
"clang/Basic/TransformTypeTraits.def"
380 OpeningParen.setType(TT_TypeDeclarationParen);
382 if (PrevNonComment->isOneOf(tok::kw_decltype, tok::kw_typeof))
383 Contexts.back().IsExpression =
true;
387 if (StartsObjCMethodExpr) {
388 Contexts.back().ColonIsObjCMethodExpr =
true;
389 OpeningParen.setType(TT_ObjCMethodExpr);
400 bool MightBeFunctionType = !Contexts[Contexts.size() - 2].IsExpression;
401 bool ProbablyFunctionType =
402 CurrentToken->isOneOf(tok::star, tok::amp, tok::ampamp, tok::caret);
403 bool HasMultipleLines =
false;
404 bool HasMultipleParametersOnALine =
false;
405 bool MightBeObjCForRangeLoop =
406 OpeningParen.Previous && OpeningParen.Previous->is(tok::kw_for);
407 FormatToken *PossibleObjCForInToken =
nullptr;
408 while (CurrentToken) {
413 if (LookForDecls && CurrentToken->Next) {
414 FormatToken *Prev = CurrentToken->getPreviousNonComment();
416 FormatToken *PrevPrev = Prev->getPreviousNonComment();
417 FormatToken *Next = CurrentToken->Next;
418 if (PrevPrev && PrevPrev->is(tok::identifier) &&
419 Prev->isOneOf(tok::star, tok::amp, tok::ampamp) &&
420 CurrentToken->is(tok::identifier) &&
421 !Next->isOneOf(tok::equal, tok::l_brace)) {
422 Prev->setType(TT_BinaryOperator);
423 LookForDecls =
false;
428 if (CurrentToken->Previous->is(TT_PointerOrReference) &&
429 CurrentToken->Previous->Previous->isOneOf(tok::l_paren,
431 ProbablyFunctionType =
true;
433 if (CurrentToken->is(tok::comma))
434 MightBeFunctionType =
false;
435 if (CurrentToken->Previous->is(TT_BinaryOperator))
436 Contexts.back().IsExpression =
true;
437 if (CurrentToken->is(tok::r_paren)) {
438 if (OpeningParen.isNot(TT_CppCastLParen) && MightBeFunctionType &&
439 ProbablyFunctionType && CurrentToken->Next &&
440 (CurrentToken->Next->is(tok::l_paren) ||
441 (CurrentToken->Next->is(tok::l_square) &&
442 Line.MustBeDeclaration))) {
443 OpeningParen.setType(OpeningParen.Next->is(tok::caret)
445 : TT_FunctionTypeLParen);
447 OpeningParen.MatchingParen = CurrentToken;
448 CurrentToken->MatchingParen = &OpeningParen;
450 if (CurrentToken->Next && CurrentToken->Next->is(tok::l_brace) &&
451 OpeningParen.Previous && OpeningParen.Previous->is(tok::l_paren)) {
455 for (FormatToken *Tok = &OpeningParen; Tok != CurrentToken;
457 if (Tok->is(TT_BinaryOperator) &&
458 Tok->isOneOf(tok::star, tok::amp, tok::ampamp)) {
459 Tok->setType(TT_PointerOrReference);
464 if (StartsObjCMethodExpr) {
465 CurrentToken->setType(TT_ObjCMethodExpr);
466 if (Contexts.back().FirstObjCSelectorName) {
467 Contexts.back().FirstObjCSelectorName->LongestObjCSelectorName =
468 Contexts.back().LongestObjCSelectorName;
472 if (OpeningParen.is(TT_AttributeParen))
473 CurrentToken->setType(TT_AttributeParen);
474 if (OpeningParen.is(TT_TypeDeclarationParen))
475 CurrentToken->setType(TT_TypeDeclarationParen);
476 if (OpeningParen.Previous &&
477 OpeningParen.Previous->is(TT_JavaAnnotation)) {
478 CurrentToken->setType(TT_JavaAnnotation);
480 if (OpeningParen.Previous &&
481 OpeningParen.Previous->is(TT_LeadingJavaAnnotation)) {
482 CurrentToken->setType(TT_LeadingJavaAnnotation);
484 if (OpeningParen.Previous &&
485 OpeningParen.Previous->is(TT_AttributeSquare)) {
486 CurrentToken->setType(TT_AttributeSquare);
489 if (!HasMultipleLines)
491 else if (HasMultipleParametersOnALine)
499 if (CurrentToken->isOneOf(tok::r_square, tok::r_brace))
502 if (CurrentToken->is(tok::l_brace) && OpeningParen.is(TT_ObjCBlockLParen))
503 OpeningParen.setType(TT_Unknown);
504 if (CurrentToken->is(tok::comma) && CurrentToken->Next &&
505 !CurrentToken->Next->HasUnescapedNewline &&
506 !CurrentToken->Next->isTrailingComment()) {
507 HasMultipleParametersOnALine =
true;
509 bool ProbablyFunctionTypeLParen =
510 (CurrentToken->is(tok::l_paren) && CurrentToken->Next &&
511 CurrentToken->Next->isOneOf(tok::star, tok::amp, tok::caret));
512 if ((CurrentToken->Previous->isOneOf(tok::kw_const, tok::kw_auto) ||
513 CurrentToken->Previous->isSimpleTypeSpecifier()) &&
514 !(CurrentToken->is(tok::l_brace) ||
515 (CurrentToken->is(tok::l_paren) && !ProbablyFunctionTypeLParen))) {
516 Contexts.back().IsExpression =
false;
518 if (CurrentToken->isOneOf(tok::semi, tok::colon)) {
519 MightBeObjCForRangeLoop =
false;
520 if (PossibleObjCForInToken) {
521 PossibleObjCForInToken->setType(TT_Unknown);
522 PossibleObjCForInToken =
nullptr;
525 if (MightBeObjCForRangeLoop && CurrentToken->is(Keywords.kw_in)) {
526 PossibleObjCForInToken = CurrentToken;
527 PossibleObjCForInToken->setType(TT_ObjCForIn);
531 if (CurrentToken->is(tok::comma))
532 Contexts.back().CanBeExpression =
true;
534 FormatToken *Tok = CurrentToken;
537 updateParameterCount(&OpeningParen, Tok);
538 if (CurrentToken && CurrentToken->HasUnescapedNewline)
539 HasMultipleLines =
true;
544 bool isCSharpAttributeSpecifier(
const FormatToken &Tok) {
545 if (!Style.isCSharp())
549 if (Tok.Previous && Tok.Previous->is(tok::identifier))
553 if (Tok.Previous && Tok.Previous->is(tok::r_square)) {
554 auto *MatchingParen = Tok.Previous->MatchingParen;
555 if (!MatchingParen || MatchingParen->is(TT_ArraySubscriptLSquare))
559 const FormatToken *AttrTok = Tok.Next;
564 if (AttrTok->is(tok::r_square))
568 while (AttrTok && AttrTok->isNot(tok::r_square))
569 AttrTok = AttrTok->Next;
575 AttrTok = AttrTok->Next;
580 if (AttrTok->isOneOf(tok::kw_public, tok::kw_private, tok::kw_protected,
581 tok::comment, tok::kw_class, tok::kw_static,
582 tok::l_square, Keywords.kw_internal)) {
588 AttrTok->Next->startsSequence(tok::identifier, tok::l_paren)) {
603 FormatToken *Left = CurrentToken->Previous;
604 Left->ParentBracket = Contexts.back().ContextKind;
605 FormatToken *
Parent = Left->getPreviousNonComment();
610 bool CppArrayTemplates =
611 Style.isCpp() &&
Parent &&
Parent->is(TT_TemplateCloser) &&
612 (Contexts.back().CanBeExpression || Contexts.back().IsExpression ||
613 Contexts.back().ContextType == Context::TemplateArgument);
615 const bool IsInnerSquare = Contexts.back().InCpp11AttributeSpecifier;
616 const bool IsCpp11AttributeSpecifier =
617 isCppAttribute(Style.isCpp(), *Left) || IsInnerSquare;
620 bool IsCSharpAttributeSpecifier =
621 isCSharpAttributeSpecifier(*Left) ||
622 Contexts.back().InCSharpAttributeSpecifier;
624 bool InsideInlineASM = Line.startsWith(tok::kw_asm);
625 bool IsCppStructuredBinding = Left->isCppStructuredBinding(Style);
626 bool StartsObjCMethodExpr =
627 !IsCppStructuredBinding && !InsideInlineASM && !CppArrayTemplates &&
628 Style.isCpp() && !IsCpp11AttributeSpecifier &&
629 !IsCSharpAttributeSpecifier && Contexts.back().CanBeExpression &&
630 Left->isNot(TT_LambdaLSquare) &&
631 !CurrentToken->isOneOf(tok::l_brace, tok::r_square) &&
633 Parent->isOneOf(tok::colon, tok::l_square, tok::l_paren,
634 tok::kw_return, tok::kw_throw) ||
635 Parent->isUnaryOperator() ||
637 Parent->isOneOf(TT_ObjCForIn, TT_CastRParen) ||
640 bool ColonFound =
false;
642 unsigned BindingIncrease = 1;
643 if (IsCppStructuredBinding) {
644 Left->setType(TT_StructuredBindingLSquare);
645 }
else if (Left->is(TT_Unknown)) {
646 if (StartsObjCMethodExpr) {
647 Left->setType(TT_ObjCMethodExpr);
648 }
else if (InsideInlineASM) {
649 Left->setType(TT_InlineASMSymbolicNameLSquare);
650 }
else if (IsCpp11AttributeSpecifier) {
651 Left->setType(TT_AttributeSquare);
652 if (!IsInnerSquare && Left->Previous)
653 Left->Previous->EndsCppAttributeGroup =
false;
654 }
else if (Style.isJavaScript() &&
Parent &&
655 Contexts.back().ContextKind == tok::l_brace &&
656 Parent->isOneOf(tok::l_brace, tok::comma)) {
657 Left->setType(TT_JsComputedPropertyName);
658 }
else if (Style.isCpp() && Contexts.back().ContextKind == tok::l_brace &&
660 Left->setType(TT_DesignatedInitializerLSquare);
661 }
else if (IsCSharpAttributeSpecifier) {
662 Left->setType(TT_AttributeSquare);
663 }
else if (CurrentToken->is(tok::r_square) &&
Parent &&
664 Parent->is(TT_TemplateCloser)) {
665 Left->setType(TT_ArraySubscriptLSquare);
694 Left->setType(TT_ArrayInitializerLSquare);
695 if (!Left->endsSequence(tok::l_square, tok::numeric_constant,
697 !Left->endsSequence(tok::l_square, tok::numeric_constant,
699 !Left->endsSequence(tok::l_square, tok::colon, TT_SelectorName)) {
700 Left->setType(TT_ProtoExtensionLSquare);
701 BindingIncrease = 10;
703 }
else if (!CppArrayTemplates &&
Parent &&
704 Parent->isOneOf(TT_BinaryOperator, TT_TemplateCloser, tok::at,
705 tok::comma, tok::l_paren, tok::l_square,
706 tok::question, tok::colon, tok::kw_return,
709 Left->setType(TT_ArrayInitializerLSquare);
711 BindingIncrease = 10;
712 Left->setType(TT_ArraySubscriptLSquare);
716 ScopedContextCreator ContextCreator(*
this, tok::l_square, BindingIncrease);
717 Contexts.back().IsExpression =
true;
718 if (Style.isJavaScript() &&
Parent &&
Parent->is(TT_JsTypeColon))
719 Contexts.back().IsExpression =
false;
721 Contexts.back().ColonIsObjCMethodExpr = StartsObjCMethodExpr;
722 Contexts.back().InCpp11AttributeSpecifier = IsCpp11AttributeSpecifier;
723 Contexts.back().InCSharpAttributeSpecifier = IsCSharpAttributeSpecifier;
725 while (CurrentToken) {
726 if (CurrentToken->is(tok::r_square)) {
727 if (IsCpp11AttributeSpecifier) {
728 CurrentToken->setType(TT_AttributeSquare);
730 CurrentToken->EndsCppAttributeGroup =
true;
732 if (IsCSharpAttributeSpecifier) {
733 CurrentToken->setType(TT_AttributeSquare);
734 }
else if (((CurrentToken->Next &&
735 CurrentToken->Next->is(tok::l_paren)) ||
736 (CurrentToken->Previous &&
737 CurrentToken->Previous->Previous == Left)) &&
738 Left->is(TT_ObjCMethodExpr)) {
743 StartsObjCMethodExpr =
false;
744 Left->setType(TT_Unknown);
746 if (StartsObjCMethodExpr && CurrentToken->Previous != Left) {
747 CurrentToken->setType(TT_ObjCMethodExpr);
750 if (!ColonFound && CurrentToken->Previous &&
751 CurrentToken->Previous->is(TT_Unknown) &&
752 canBeObjCSelectorComponent(*CurrentToken->Previous)) {
753 CurrentToken->Previous->setType(TT_SelectorName);
759 Parent->overwriteFixedType(TT_BinaryOperator);
762 if (CurrentToken->getType() == TT_ObjCMethodExpr &&
763 CurrentToken->Next && CurrentToken->Next->is(TT_LambdaArrow)) {
764 CurrentToken->Next->overwriteFixedType(TT_Unknown);
766 Left->MatchingParen = CurrentToken;
767 CurrentToken->MatchingParen = Left;
772 if (!Contexts.back().FirstObjCSelectorName) {
773 FormatToken *
Previous = CurrentToken->getPreviousNonComment();
775 Previous->ObjCSelectorNameParts = 1;
776 Contexts.back().FirstObjCSelectorName =
Previous;
779 Left->ParameterCount =
780 Contexts.back().FirstObjCSelectorName->ObjCSelectorNameParts;
782 if (Contexts.back().FirstObjCSelectorName) {
783 Contexts.back().FirstObjCSelectorName->LongestObjCSelectorName =
784 Contexts.back().LongestObjCSelectorName;
785 if (Left->BlockParameterCount > 1)
786 Contexts.back().FirstObjCSelectorName->LongestObjCSelectorName = 0;
791 if (CurrentToken->isOneOf(tok::r_paren, tok::r_brace))
793 if (CurrentToken->is(tok::colon)) {
794 if (IsCpp11AttributeSpecifier &&
795 CurrentToken->endsSequence(tok::colon, tok::identifier,
799 CurrentToken->setType(TT_AttributeColon);
800 }
else if (!Style.isVerilog() && !Line.InPragmaDirective &&
801 Left->isOneOf(TT_ArraySubscriptLSquare,
802 TT_DesignatedInitializerLSquare)) {
803 Left->setType(TT_ObjCMethodExpr);
804 StartsObjCMethodExpr =
true;
805 Contexts.back().ColonIsObjCMethodExpr =
true;
808 Parent->setType(TT_CastRParen);
813 if (CurrentToken->is(tok::comma) && Left->is(TT_ObjCMethodExpr) &&
815 Left->setType(TT_ArrayInitializerLSquare);
817 FormatToken *Tok = CurrentToken;
820 updateParameterCount(Left, Tok);
825 bool couldBeInStructArrayInitializer()
const {
826 if (Contexts.size() < 2)
830 const auto End = std::next(Contexts.rbegin(), 2);
831 auto Last = Contexts.rbegin();
834 if (
Last->ContextKind == tok::l_brace)
836 return Depth == 2 &&
Last->ContextKind != tok::l_brace;
843 assert(CurrentToken->Previous);
844 FormatToken &OpeningBrace = *CurrentToken->Previous;
845 assert(OpeningBrace.is(tok::l_brace));
846 OpeningBrace.ParentBracket = Contexts.back().ContextKind;
848 if (Contexts.back().CaretFound)
849 OpeningBrace.overwriteFixedType(TT_ObjCBlockLBrace);
850 Contexts.back().CaretFound =
false;
852 ScopedContextCreator ContextCreator(*
this, tok::l_brace, 1);
853 Contexts.back().ColonIsDictLiteral =
true;
855 Contexts.back().IsExpression =
true;
856 if (Style.isJavaScript() && OpeningBrace.Previous &&
857 OpeningBrace.Previous->is(TT_JsTypeColon)) {
858 Contexts.back().IsExpression =
false;
861 unsigned CommaCount = 0;
862 while (CurrentToken) {
863 if (CurrentToken->is(tok::r_brace)) {
864 assert(!Scopes.empty());
865 assert(Scopes.back() == getScopeType(OpeningBrace));
867 assert(OpeningBrace.Optional == CurrentToken->Optional);
868 OpeningBrace.MatchingParen = CurrentToken;
869 CurrentToken->MatchingParen = &OpeningBrace;
871 if (OpeningBrace.ParentBracket == tok::l_brace &&
872 couldBeInStructArrayInitializer() && CommaCount > 0) {
873 Contexts.back().ContextType = Context::StructArrayInitializer;
879 if (CurrentToken->isOneOf(tok::r_paren, tok::r_square))
881 updateParameterCount(&OpeningBrace, CurrentToken);
882 if (CurrentToken->isOneOf(tok::colon, tok::l_brace, tok::less)) {
883 FormatToken *
Previous = CurrentToken->getPreviousNonComment();
884 if (
Previous->is(TT_JsTypeOptionalQuestion))
886 if ((CurrentToken->is(tok::colon) &&
887 (!Contexts.back().ColonIsDictLiteral || !Style.isCpp())) ||
890 OpeningBrace.setType(TT_DictLiteral);
891 if (
Previous->Tok.getIdentifierInfo() ||
892 Previous->is(tok::string_literal)) {
896 if (CurrentToken->is(tok::colon) && OpeningBrace.is(TT_Unknown))
897 OpeningBrace.setType(TT_DictLiteral);
898 else if (Style.isJavaScript())
899 OpeningBrace.overwriteFixedType(TT_DictLiteral);
901 if (CurrentToken->is(tok::comma)) {
902 if (Style.isJavaScript())
903 OpeningBrace.overwriteFixedType(TT_DictLiteral);
912 void updateParameterCount(FormatToken *Left, FormatToken *Current) {
916 if (Current->is(tok::l_brace) && Current->is(
BK_Block))
917 ++Left->BlockParameterCount;
918 if (Current->is(tok::comma)) {
919 ++Left->ParameterCount;
921 Left->Role.reset(
new CommaSeparatedList(Style));
922 Left->Role->CommaFound(Current);
923 }
else if (Left->ParameterCount == 0 && Current->isNot(tok::comment)) {
924 Left->ParameterCount = 1;
928 bool parseConditional() {
929 while (CurrentToken) {
930 if (CurrentToken->is(tok::colon)) {
931 CurrentToken->setType(TT_ConditionalExpr);
941 bool parseTemplateDeclaration() {
942 if (CurrentToken && CurrentToken->is(tok::less)) {
943 CurrentToken->setType(TT_TemplateOpener);
948 CurrentToken->Previous->ClosesTemplateDeclaration =
true;
954 bool consumeToken() {
955 FormatToken *Tok = CurrentToken;
959 if (Tok->is(TT_VerilogTableItem))
961 switch (Tok->Tok.getKind()) {
964 if (!Tok->Previous && Line.MustBeDeclaration)
965 Tok->setType(TT_ObjCMethodSpecifier);
971 if (Style.isJavaScript()) {
972 if (Contexts.back().ColonIsForRangeExpr ||
973 (Contexts.size() == 1 &&
974 !Line.First->isOneOf(tok::kw_enum, tok::kw_case)) ||
975 Contexts.back().ContextKind == tok::l_paren ||
976 Contexts.back().ContextKind == tok::l_square ||
977 (!Contexts.back().IsExpression &&
978 Contexts.back().ContextKind == tok::l_brace) ||
979 (Contexts.size() == 1 &&
980 Line.MustBeDeclaration)) {
981 Contexts.back().IsExpression =
false;
982 Tok->setType(TT_JsTypeColon);
985 }
else if (Style.isCSharp()) {
986 if (Contexts.back().InCSharpAttributeSpecifier) {
987 Tok->setType(TT_AttributeColon);
990 if (Contexts.back().ContextKind == tok::l_paren) {
991 Tok->setType(TT_CSharpNamedArgumentColon);
994 }
else if (Style.isVerilog() && Tok->isNot(TT_BinaryOperator)) {
997 if (Keywords.isVerilogEnd(*Tok->Previous) ||
998 Keywords.isVerilogBegin(*Tok->Previous)) {
999 Tok->setType(TT_VerilogBlockLabelColon);
1000 }
else if (Contexts.back().ContextKind == tok::l_square) {
1001 Tok->setType(TT_BitFieldColon);
1002 }
else if (Contexts.back().ColonIsDictLiteral) {
1003 Tok->setType(TT_DictLiteral);
1004 }
else if (Contexts.size() == 1) {
1008 Tok->setType(TT_GotoLabelColon);
1009 if (Line.Level > 1 || (!Line.InPPDirective && Line.Level > 0))
1014 if (Line.First->isOneOf(Keywords.kw_module, Keywords.kw_import) ||
1015 Line.First->startsSequence(tok::kw_export, Keywords.kw_module) ||
1016 Line.First->startsSequence(tok::kw_export, Keywords.kw_import)) {
1017 Tok->setType(TT_ModulePartitionColon);
1018 }
else if (Contexts.back().ColonIsDictLiteral ||
1021 Tok->setType(TT_DictLiteral);
1023 if (FormatToken *
Previous = Tok->getPreviousNonComment())
1024 Previous->setType(TT_SelectorName);
1026 }
else if (Contexts.back().ColonIsObjCMethodExpr ||
1027 Line.startsWith(TT_ObjCMethodSpecifier)) {
1028 Tok->setType(TT_ObjCMethodExpr);
1029 const FormatToken *BeforePrevious = Tok->Previous->Previous;
1032 bool UnknownIdentifierInMethodDeclaration =
1033 Line.startsWith(TT_ObjCMethodSpecifier) &&
1034 Tok->Previous->is(tok::identifier) && Tok->Previous->is(TT_Unknown);
1035 if (!BeforePrevious ||
1037 !(BeforePrevious->is(TT_CastRParen) ||
1038 (BeforePrevious->is(TT_ObjCMethodExpr) &&
1039 BeforePrevious->is(tok::colon))) ||
1040 BeforePrevious->is(tok::r_square) ||
1041 Contexts.back().LongestObjCSelectorName == 0 ||
1042 UnknownIdentifierInMethodDeclaration) {
1043 Tok->Previous->setType(TT_SelectorName);
1044 if (!Contexts.back().FirstObjCSelectorName) {
1045 Contexts.back().FirstObjCSelectorName = Tok->Previous;
1046 }
else if (Tok->Previous->ColumnWidth >
1047 Contexts.back().LongestObjCSelectorName) {
1048 Contexts.back().LongestObjCSelectorName =
1049 Tok->Previous->ColumnWidth;
1051 Tok->Previous->ParameterIndex =
1052 Contexts.back().FirstObjCSelectorName->ObjCSelectorNameParts;
1053 ++Contexts.back().FirstObjCSelectorName->ObjCSelectorNameParts;
1055 }
else if (Contexts.back().ColonIsForRangeExpr) {
1056 Tok->setType(TT_RangeBasedForLoopColon);
1057 }
else if (Contexts.back().ContextType == Context::C11GenericSelection) {
1058 Tok->setType(TT_GenericSelectionColon);
1059 }
else if (CurrentToken && CurrentToken->is(tok::numeric_constant)) {
1060 Tok->setType(TT_BitFieldColon);
1061 }
else if (Contexts.size() == 1 &&
1062 !Line.First->isOneOf(tok::kw_enum, tok::kw_case,
1064 FormatToken *Prev = Tok->getPreviousNonComment();
1067 if (Prev->isOneOf(tok::r_paren, tok::kw_noexcept) ||
1068 Prev->ClosesRequiresClause) {
1069 Tok->setType(TT_CtorInitializerColon);
1070 }
else if (Prev->is(tok::kw_try)) {
1072 FormatToken *PrevPrev = Prev->getPreviousNonComment();
1075 if (PrevPrev && PrevPrev->isOneOf(tok::r_paren, tok::kw_noexcept))
1076 Tok->setType(TT_CtorInitializerColon);
1078 Tok->setType(TT_InheritanceColon);
1080 }
else if (canBeObjCSelectorComponent(*Tok->Previous) && Tok->Next &&
1081 (Tok->Next->isOneOf(tok::r_paren, tok::comma) ||
1082 (canBeObjCSelectorComponent(*Tok->Next) && Tok->Next->Next &&
1083 Tok->Next->Next->is(tok::colon)))) {
1086 Tok->setType(TT_ObjCMethodExpr);
1087 }
else if (Contexts.back().ContextKind == tok::l_paren &&
1088 !Line.InPragmaDirective) {
1089 Tok->setType(TT_InlineASMColon);
1096 if (Style.isJavaScript() && !Contexts.back().IsExpression)
1097 Tok->setType(TT_JsTypeOperator);
1101 CurrentToken->isOneOf(tok::kw_constexpr, tok::identifier)) {
1106 if (CurrentToken && CurrentToken->is(tok::l_paren)) {
1108 if (!parseParens(
true))
1113 if (Style.isJavaScript()) {
1115 if ((Tok->Previous && Tok->Previous->is(tok::period)) ||
1116 (Tok->Next && Tok->Next->is(tok::colon))) {
1120 if (CurrentToken && CurrentToken->is(Keywords.kw_await))
1123 if (Style.isCpp() && CurrentToken && CurrentToken->is(tok::kw_co_await))
1125 Contexts.back().ColonIsForRangeExpr =
true;
1126 if (!CurrentToken || CurrentToken->isNot(tok::l_paren))
1137 if (Tok->Previous && Tok->Previous->is(tok::r_paren) &&
1138 Tok->Previous->MatchingParen &&
1139 Tok->Previous->MatchingParen->is(TT_OverloadedOperatorLParen)) {
1140 Tok->Previous->setType(TT_OverloadedOperator);
1141 Tok->Previous->MatchingParen->setType(TT_OverloadedOperator);
1142 Tok->setType(TT_OverloadedOperatorLParen);
1147 if (Line.MustBeDeclaration && Contexts.size() == 1 &&
1148 !Contexts.back().IsExpression && !Line.startsWith(TT_ObjCProperty) &&
1149 !Tok->isOneOf(TT_TypeDeclarationParen, TT_RequiresExpressionLParen) &&
1151 !Tok->Previous->isOneOf(tok::kw___attribute, TT_RequiresClause,
1152 TT_LeadingJavaAnnotation))) {
1153 Line.MightBeFunctionDecl =
true;
1162 FormatToken *
Previous = Tok->getPreviousNonComment();
1164 Previous->setType(TT_SelectorName);
1166 Scopes.push_back(getScopeType(*Tok));
1172 Tok->setType(TT_TemplateOpener);
1180 Tok->Previous->isOneOf(TT_SelectorName, TT_DictLiteral))) {
1181 Tok->setType(TT_DictLiteral);
1182 FormatToken *
Previous = Tok->getPreviousNonComment();
1184 Previous->setType(TT_SelectorName);
1187 Tok->setType(TT_BinaryOperator);
1188 NonTemplateLess.insert(Tok);
1198 if (!Scopes.empty())
1206 Tok->setType(TT_BinaryOperator);
1207 if (Tok->Previous && Tok->Previous->is(TT_TemplateCloser))
1208 Tok->SpacesRequiredBefore = 1;
1210 case tok::kw_operator:
1215 while (CurrentToken &&
1216 !CurrentToken->isOneOf(tok::l_paren, tok::semi, tok::r_paren)) {
1217 if (CurrentToken->isOneOf(tok::star, tok::amp))
1218 CurrentToken->setType(TT_PointerOrReference);
1219 auto Next = CurrentToken->getNextNonComment();
1222 if (Next->is(tok::less))
1228 auto Previous = CurrentToken->getPreviousNonComment();
1230 if (CurrentToken->is(tok::comma) &&
Previous->isNot(tok::kw_operator))
1232 if (
Previous->isOneOf(TT_BinaryOperator, TT_UnaryOperator, tok::comma,
1233 tok::star, tok::arrow, tok::amp, tok::ampamp) ||
1235 Previous->TokenText.startswith(
"\"\"")) {
1236 Previous->setType(TT_OverloadedOperator);
1237 if (CurrentToken->isOneOf(tok::less, tok::greater))
1241 if (CurrentToken && CurrentToken->is(tok::l_paren))
1242 CurrentToken->setType(TT_OverloadedOperatorLParen);
1243 if (CurrentToken && CurrentToken->Previous->is(TT_BinaryOperator))
1244 CurrentToken->Previous->setType(TT_OverloadedOperator);
1247 if (Style.isJavaScript() && Tok->Next &&
1248 Tok->Next->isOneOf(tok::semi, tok::comma, tok::colon, tok::r_paren,
1249 tok::r_brace, tok::r_square)) {
1254 Tok->setType(TT_JsTypeOptionalQuestion);
1259 if (Line.MustBeDeclaration && !Contexts.back().IsExpression &&
1260 Style.isJavaScript()) {
1263 if (Style.isCSharp()) {
1267 if ((!Contexts.back().IsExpression && Line.MustBeDeclaration) ||
1268 (Tok->Next && Tok->Next->isOneOf(tok::r_paren, tok::greater)) ||
1269 (Tok->Next && Tok->Next->is(tok::identifier) && Tok->Next->Next &&
1270 Tok->Next->Next->is(tok::equal))) {
1271 Tok->setType(TT_CSharpNullable);
1277 case tok::kw_template:
1278 parseTemplateDeclaration();
1281 switch (Contexts.back().ContextType) {
1282 case Context::CtorInitializer:
1283 Tok->setType(TT_CtorInitializerComma);
1285 case Context::InheritanceList:
1286 Tok->setType(TT_InheritanceComma);
1289 if (Contexts.back().FirstStartOfName &&
1290 (Contexts.size() == 1 || startsWithInitStatement(Line))) {
1291 Contexts.back().FirstStartOfName->PartOfMultiVariableDeclStmt =
true;
1292 Line.IsMultiVariableDeclStmt =
true;
1296 if (Contexts.back().ContextType == Context::ForEachMacro)
1297 Contexts.back().IsExpression =
true;
1299 case tok::kw_default:
1301 if (Style.isVerilog() && Keywords.isVerilogEndOfLabel(*Tok) &&
1302 (Line.Level > 1 || (!Line.InPPDirective && Line.Level > 0))) {
1306 case tok::identifier:
1307 if (Tok->isOneOf(Keywords.kw___has_include,
1308 Keywords.kw___has_include_next)) {
1311 if (Style.isCSharp() && Tok->is(Keywords.kw_where) && Tok->Next &&
1312 Tok->Next->isNot(tok::l_paren)) {
1313 Tok->setType(TT_CSharpGenericTypeConstraint);
1314 parseCSharpGenericTypeConstraint();
1315 if (!Tok->getPreviousNonComment())
1316 Line.IsContinuation =
true;
1320 if (Tok->isNot(TT_LambdaArrow) && Tok->Previous &&
1321 Tok->Previous->is(tok::kw_noexcept)) {
1322 Tok->setType(TT_TrailingReturnArrow);
1326 if (Style.InsertNewlineAtEOF && Tok->NewlinesBefore == 0)
1327 Tok->NewlinesBefore = 1;
1335 void parseCSharpGenericTypeConstraint() {
1336 int OpenAngleBracketsCount = 0;
1337 while (CurrentToken) {
1338 if (CurrentToken->is(tok::less)) {
1340 CurrentToken->setType(TT_TemplateOpener);
1341 ++OpenAngleBracketsCount;
1343 }
else if (CurrentToken->is(tok::greater)) {
1344 CurrentToken->setType(TT_TemplateCloser);
1345 --OpenAngleBracketsCount;
1347 }
else if (CurrentToken->is(tok::comma) && OpenAngleBracketsCount == 0) {
1350 CurrentToken->setType(TT_CSharpGenericTypeConstraintComma);
1352 }
else if (CurrentToken->is(Keywords.kw_where)) {
1353 CurrentToken->setType(TT_CSharpGenericTypeConstraint);
1355 }
else if (CurrentToken->is(tok::colon)) {
1356 CurrentToken->setType(TT_CSharpGenericTypeConstraintColon);
1364 void parseIncludeDirective() {
1365 if (CurrentToken && CurrentToken->is(tok::less)) {
1367 while (CurrentToken) {
1370 if (CurrentToken->isNot(tok::comment) &&
1371 !CurrentToken->TokenText.startswith(
"//")) {
1372 CurrentToken->setType(TT_ImplicitStringLiteral);
1379 void parseWarningOrError() {
1384 while (CurrentToken) {
1385 CurrentToken->setType(TT_ImplicitStringLiteral);
1390 void parsePragma() {
1393 CurrentToken->isOneOf(Keywords.kw_mark, Keywords.kw_option,
1394 Keywords.kw_region)) {
1395 bool IsMarkOrRegion =
1396 CurrentToken->isOneOf(Keywords.kw_mark, Keywords.kw_region);
1399 while (CurrentToken) {
1400 if (IsMarkOrRegion || CurrentToken->Previous->is(TT_BinaryOperator))
1401 CurrentToken->setType(TT_ImplicitStringLiteral);
1407 void parseHasInclude() {
1408 if (!CurrentToken || !CurrentToken->is(tok::l_paren))
1411 parseIncludeDirective();
1415 LineType parsePreprocessorDirective() {
1416 bool IsFirstToken = CurrentToken->IsFirst;
1422 if (Style.isJavaScript() && IsFirstToken) {
1426 while (CurrentToken) {
1428 CurrentToken->setType(TT_ImplicitStringLiteral);
1434 if (CurrentToken->is(tok::numeric_constant)) {
1435 CurrentToken->SpacesRequiredBefore = 1;
1440 if (!CurrentToken->Tok.getIdentifierInfo())
1444 if (Style.isVerilog() && !Keywords.isVerilogPPDirective(*CurrentToken))
1446 switch (CurrentToken->Tok.getIdentifierInfo()->getPPKeywordID()) {
1447 case tok::pp_include:
1448 case tok::pp_include_next:
1449 case tok::pp_import:
1451 parseIncludeDirective();
1455 case tok::pp_warning:
1456 parseWarningOrError();
1458 case tok::pp_pragma:
1463 Contexts.back().IsExpression =
true;
1470 while (CurrentToken) {
1471 FormatToken *Tok = CurrentToken;
1473 if (Tok->is(tok::l_paren)) {
1475 }
else if (Tok->isOneOf(Keywords.kw___has_include,
1476 Keywords.kw___has_include_next)) {
1487 NonTemplateLess.clear();
1488 if (!Line.InMacroBody && CurrentToken->is(tok::hash)) {
1492 auto Type = parsePreprocessorDirective();
1500 IdentifierInfo *Info = CurrentToken->Tok.getIdentifierInfo();
1502 CurrentToken->is(Keywords.kw_package)) ||
1503 (!Style.isVerilog() && Info &&
1504 Info->getPPKeywordID() == tok::pp_import && CurrentToken->Next &&
1505 CurrentToken->Next->isOneOf(tok::string_literal, tok::identifier,
1508 parseIncludeDirective();
1514 if (CurrentToken->is(tok::less) && Line.Last->is(tok::greater)) {
1515 parseIncludeDirective();
1522 CurrentToken->isOneOf(Keywords.kw_option, Keywords.kw_package)) {
1524 if (CurrentToken && CurrentToken->is(tok::identifier)) {
1525 while (CurrentToken)
1531 bool KeywordVirtualFound =
false;
1532 bool ImportStatement =
false;
1535 if (Style.isJavaScript() && CurrentToken->is(Keywords.kw_import))
1536 ImportStatement =
true;
1538 while (CurrentToken) {
1539 if (CurrentToken->is(tok::kw_virtual))
1540 KeywordVirtualFound =
true;
1541 if (Style.isJavaScript()) {
1548 if (Line.First->is(tok::kw_export) &&
1549 CurrentToken->is(Keywords.kw_from) && CurrentToken->Next &&
1550 CurrentToken->Next->isStringLiteral()) {
1551 ImportStatement =
true;
1553 if (isClosureImportStatement(*CurrentToken))
1554 ImportStatement =
true;
1556 if (!consumeToken())
1559 if (KeywordVirtualFound)
1561 if (ImportStatement)
1564 if (Line.startsWith(TT_ObjCMethodSpecifier)) {
1565 if (Contexts.back().FirstObjCSelectorName) {
1566 Contexts.back().FirstObjCSelectorName->LongestObjCSelectorName =
1567 Contexts.back().LongestObjCSelectorName;
1572 for (
const auto &ctx : Contexts)
1573 if (ctx.ContextType == Context::StructArrayInitializer)
1580 bool isClosureImportStatement(
const FormatToken &Tok) {
1583 return Tok.TokenText ==
"goog" && Tok.Next && Tok.Next->is(tok::period) &&
1585 (Tok.Next->Next->TokenText ==
"module" ||
1586 Tok.Next->Next->TokenText ==
"provide" ||
1587 Tok.Next->Next->TokenText ==
"require" ||
1588 Tok.Next->Next->TokenText ==
"requireType" ||
1589 Tok.Next->Next->TokenText ==
"forwardDeclare") &&
1590 Tok.Next->Next->Next && Tok.Next->Next->Next->is(tok::l_paren);
1593 void resetTokenMetadata() {
1599 if (!CurrentToken->isTypeFinalized() &&
1600 !CurrentToken->isOneOf(
1601 TT_LambdaLSquare, TT_LambdaLBrace, TT_AttributeMacro, TT_IfMacro,
1602 TT_ForEachMacro, TT_TypenameMacro, TT_FunctionLBrace,
1603 TT_ImplicitStringLiteral, TT_InlineASMBrace, TT_FatArrow,
1604 TT_LambdaArrow, TT_NamespaceMacro, TT_OverloadedOperator,
1605 TT_RegexLiteral, TT_TemplateString, TT_ObjCStringLiteral,
1606 TT_UntouchableMacroFunc, TT_StatementAttributeLikeMacro,
1607 TT_FunctionLikeOrFreestandingMacro, TT_ClassLBrace, TT_EnumLBrace,
1608 TT_RecordLBrace, TT_StructLBrace, TT_UnionLBrace, TT_RequiresClause,
1609 TT_RequiresClauseInARequiresExpression, TT_RequiresExpression,
1610 TT_RequiresExpressionLParen, TT_RequiresExpressionLBrace,
1611 TT_CompoundRequirementLBrace, TT_BracedListLBrace)) {
1612 CurrentToken->setType(TT_Unknown);
1614 CurrentToken->Role.reset();
1615 CurrentToken->MatchingParen =
nullptr;
1616 CurrentToken->FakeLParens.clear();
1617 CurrentToken->FakeRParens = 0;
1624 CurrentToken->NestingLevel = Contexts.size() - 1;
1625 CurrentToken->BindingStrength = Contexts.back().BindingStrength;
1626 modifyContext(*CurrentToken);
1627 determineTokenType(*CurrentToken);
1628 CurrentToken = CurrentToken->Next;
1630 resetTokenMetadata();
1667 StructArrayInitializer,
1671 C11GenericSelection,
1677 struct ScopedContextCreator {
1678 AnnotatingParser &
P;
1684 P.Contexts.back().BindingStrength + Increase,
1685 P.Contexts.back().IsExpression));
1688 ~ScopedContextCreator() {
1690 if (
P.Contexts.back().ContextType == Context::StructArrayInitializer) {
1691 P.Contexts.pop_back();
1692 P.Contexts.back().ContextType = Context::StructArrayInitializer;
1696 P.Contexts.pop_back();
1700 void modifyContext(
const FormatToken &Current) {
1701 auto AssignmentStartsExpression = [&]() {
1705 if (Line.First->isOneOf(tok::kw_using, tok::kw_return))
1707 if (Line.First->is(tok::kw_template)) {
1708 assert(Current.Previous);
1709 if (Current.Previous->is(tok::kw_operator)) {
1715 const FormatToken *Tok = Line.First->getNextNonComment();
1717 if (Tok->isNot(TT_TemplateOpener)) {
1722 Tok = Tok->MatchingParen;
1725 Tok = Tok->getNextNonComment();
1729 if (Tok->isOneOf(tok::kw_class, tok::kw_enum, tok::kw_struct,
1739 if (Style.isJavaScript() &&
1740 (Line.startsWith(Keywords.kw_type, tok::identifier) ||
1741 Line.startsWith(tok::kw_export, Keywords.kw_type,
1742 tok::identifier))) {
1746 return !Current.Previous || Current.Previous->isNot(tok::kw_operator);
1749 if (AssignmentStartsExpression()) {
1750 Contexts.back().IsExpression =
true;
1751 if (!Line.startsWith(TT_UnaryOperator)) {
1752 for (FormatToken *
Previous = Current.Previous;
1754 !
Previous->Previous->isOneOf(tok::comma, tok::semi);
1756 if (
Previous->isOneOf(tok::r_square, tok::r_paren)) {
1763 if (
Previous->isOneOf(TT_BinaryOperator, TT_UnaryOperator) &&
1764 Previous->isOneOf(tok::star, tok::amp, tok::ampamp) &&
1766 Previous->setType(TT_PointerOrReference);
1770 }
else if (Current.is(tok::lessless) &&
1771 (!Current.Previous || !Current.Previous->is(tok::kw_operator))) {
1772 Contexts.back().IsExpression =
true;
1773 }
else if (Current.isOneOf(tok::kw_return, tok::kw_throw)) {
1774 Contexts.back().IsExpression =
true;
1775 }
else if (Current.is(TT_TrailingReturnArrow)) {
1776 Contexts.back().IsExpression =
false;
1777 }
else if (Current.is(TT_LambdaArrow) || Current.is(Keywords.kw_assert)) {
1779 }
else if (Current.Previous &&
1780 Current.Previous->is(TT_CtorInitializerColon)) {
1781 Contexts.back().IsExpression =
true;
1782 Contexts.back().ContextType = Context::CtorInitializer;
1783 }
else if (Current.Previous && Current.Previous->is(TT_InheritanceColon)) {
1784 Contexts.back().ContextType = Context::InheritanceList;
1785 }
else if (Current.isOneOf(tok::r_paren, tok::greater, tok::comma)) {
1786 for (FormatToken *
Previous = Current.Previous;
1789 Previous->setType(TT_PointerOrReference);
1791 if (Line.MustBeDeclaration &&
1792 Contexts.front().ContextType != Context::CtorInitializer) {
1793 Contexts.back().IsExpression =
false;
1795 }
else if (Current.is(tok::kw_new)) {
1796 Contexts.back().CanBeExpression =
false;
1797 }
else if (Current.is(tok::semi) ||
1798 (Current.is(tok::exclaim) && Current.Previous &&
1799 !Current.Previous->is(tok::kw_operator))) {
1803 Contexts.back().IsExpression =
true;
1807 static FormatToken *untilMatchingParen(FormatToken *Current) {
1811 if (Current->is(tok::l_paren))
1813 if (Current->is(tok::r_paren))
1817 Current = Current->Next;
1822 static bool isDeductionGuide(FormatToken &Current) {
1824 if (Current.Previous && Current.Previous->is(tok::r_paren) &&
1825 Current.startsSequence(tok::arrow, tok::identifier, tok::less)) {
1827 FormatToken *TemplateCloser = Current.Next->Next;
1828 int NestingLevel = 0;
1829 while (TemplateCloser) {
1831 if (TemplateCloser->is(tok::l_paren)) {
1833 TemplateCloser = untilMatchingParen(TemplateCloser);
1834 if (!TemplateCloser)
1837 if (TemplateCloser->is(tok::less))
1839 if (TemplateCloser->is(tok::greater))
1841 if (NestingLevel < 1)
1843 TemplateCloser = TemplateCloser->Next;
1847 if (TemplateCloser && TemplateCloser->Next &&
1848 TemplateCloser->Next->is(tok::semi) &&
1849 Current.Previous->MatchingParen) {
1852 FormatToken *LeadingIdentifier =
1853 Current.Previous->MatchingParen->Previous;
1855 return LeadingIdentifier &&
1856 LeadingIdentifier->TokenText == Current.Next->TokenText;
1862 void determineTokenType(FormatToken &Current) {
1863 if (!Current.is(TT_Unknown)) {
1868 if ((Style.isJavaScript() || Style.isCSharp()) &&
1869 Current.is(tok::exclaim)) {
1870 if (Current.Previous) {
1872 Style.isJavaScript()
1873 ? Keywords.IsJavaScriptIdentifier(
1874 *Current.Previous,
true)
1875 : Current.Previous->is(tok::identifier);
1877 Current.Previous->isOneOf(
1878 tok::kw_default, tok::kw_namespace, tok::r_paren, tok::r_square,
1879 tok::r_brace, tok::kw_false, tok::kw_true, Keywords.kw_type,
1880 Keywords.kw_get, Keywords.kw_init, Keywords.kw_set) ||
1881 Current.Previous->Tok.isLiteral()) {
1882 Current.setType(TT_NonNullAssertion);
1887 Current.Next->isOneOf(TT_BinaryOperator, Keywords.kw_as)) {
1888 Current.setType(TT_NonNullAssertion);
1896 if (Current.is(Keywords.kw_instanceof)) {
1897 Current.setType(TT_BinaryOperator);
1898 }
else if (isStartOfName(Current) &&
1899 (!Line.MightBeFunctionDecl || Current.NestingLevel != 0)) {
1900 Contexts.back().FirstStartOfName = &Current;
1901 Current.setType(TT_StartOfName);
1902 }
else if (Current.is(tok::semi)) {
1906 Contexts.back().FirstStartOfName =
nullptr;
1907 }
else if (Current.isOneOf(tok::kw_auto, tok::kw___auto_type)) {
1909 }
else if (Current.is(tok::arrow) &&
1911 Current.setType(TT_LambdaArrow);
1912 }
else if (Current.is(tok::arrow) && AutoFound &&
1913 (Line.MustBeDeclaration || Line.InPPDirective) &&
1914 Current.NestingLevel == 0 &&
1915 !Current.Previous->isOneOf(tok::kw_operator, tok::identifier)) {
1917 Current.setType(TT_TrailingReturnArrow);
1918 }
else if (Current.is(tok::arrow) && Current.Previous &&
1919 Current.Previous->is(tok::r_brace)) {
1922 Current.setType(TT_TrailingReturnArrow);
1923 }
else if (isDeductionGuide(Current)) {
1925 Current.setType(TT_TrailingReturnArrow);
1926 }
else if (Current.isOneOf(tok::star, tok::amp, tok::ampamp)) {
1927 Current.setType(determineStarAmpUsage(
1929 Contexts.back().CanBeExpression && Contexts.back().IsExpression,
1930 Contexts.back().ContextType == Context::TemplateArgument));
1931 }
else if (Current.isOneOf(tok::minus, tok::plus, tok::caret) ||
1932 (Style.isVerilog() && Current.is(tok::pipe))) {
1933 Current.setType(determinePlusMinusCaretUsage(Current));
1934 if (Current.is(TT_UnaryOperator) && Current.is(tok::caret))
1935 Contexts.back().CaretFound =
true;
1936 }
else if (Current.isOneOf(tok::minusminus, tok::plusplus)) {
1937 Current.setType(determineIncrementUsage(Current));
1938 }
else if (Current.isOneOf(tok::exclaim, tok::tilde)) {
1939 Current.setType(TT_UnaryOperator);
1940 }
else if (Current.is(tok::question)) {
1941 if (Style.isJavaScript() && Line.MustBeDeclaration &&
1942 !Contexts.back().IsExpression) {
1945 Current.setType(TT_JsTypeOptionalQuestion);
1947 Current.setType(TT_ConditionalExpr);
1949 }
else if (Current.isBinaryOperator() &&
1950 (!Current.Previous || Current.Previous->isNot(tok::l_square)) &&
1951 (!Current.is(tok::greater) &&
1953 if (Style.isVerilog()) {
1954 if (Current.is(tok::lessequal) && Contexts.size() == 1 &&
1955 !Contexts.back().VerilogAssignmentFound) {
1959 Current.setFinalizedType(TT_BinaryOperator);
1962 Contexts.back().VerilogAssignmentFound =
true;
1964 Current.setType(TT_BinaryOperator);
1965 }
else if (Current.is(tok::comment)) {
1966 if (Current.TokenText.startswith(
"/*")) {
1967 if (Current.TokenText.endswith(
"*/")) {
1968 Current.setType(TT_BlockComment);
1972 Current.Tok.setKind(tok::unknown);
1975 Current.setType(TT_LineComment);
1977 }
else if (Current.is(tok::l_paren)) {
1978 if (lParenStartsCppCast(Current))
1979 Current.setType(TT_CppCastLParen);
1980 }
else if (Current.is(tok::r_paren)) {
1981 if (rParenEndsCast(Current))
1982 Current.setType(TT_CastRParen);
1983 if (Current.MatchingParen && Current.Next &&
1984 !Current.Next->isBinaryOperator() &&
1985 !Current.Next->isOneOf(tok::semi, tok::colon, tok::l_brace,
1986 tok::comma, tok::period, tok::arrow,
1987 tok::coloncolon, tok::kw_noexcept)) {
1988 if (FormatToken *AfterParen = Current.MatchingParen->Next) {
1990 if (AfterParen->isNot(tok::caret)) {
1991 if (FormatToken *BeforeParen = Current.MatchingParen->Previous) {
1992 if (BeforeParen->is(tok::identifier) &&
1993 !BeforeParen->is(TT_TypenameMacro) &&
1994 BeforeParen->TokenText == BeforeParen->TokenText.upper() &&
1995 (!BeforeParen->Previous ||
1996 BeforeParen->Previous->ClosesTemplateDeclaration)) {
1997 Current.setType(TT_FunctionAnnotationRParen);
2003 }
else if (Current.is(tok::at) && Current.Next && !Style.isJavaScript() &&
2007 switch (Current.Next->Tok.getObjCKeywordID()) {
2008 case tok::objc_interface:
2009 case tok::objc_implementation:
2010 case tok::objc_protocol:
2011 Current.setType(TT_ObjCDecl);
2013 case tok::objc_property:
2014 Current.setType(TT_ObjCProperty);
2019 }
else if (Current.is(tok::period)) {
2020 FormatToken *PreviousNoComment = Current.getPreviousNonComment();
2021 if (PreviousNoComment &&
2022 PreviousNoComment->isOneOf(tok::comma, tok::l_brace)) {
2023 Current.setType(TT_DesignatedInitializerPeriod);
2025 Current.Previous->isOneOf(TT_JavaAnnotation,
2026 TT_LeadingJavaAnnotation)) {
2027 Current.setType(Current.Previous->getType());
2029 }
else if (canBeObjCSelectorComponent(Current) &&
2032 Current.Previous && Current.Previous->is(TT_CastRParen) &&
2033 Current.Previous->MatchingParen &&
2034 Current.Previous->MatchingParen->Previous &&
2035 Current.Previous->MatchingParen->Previous->is(
2036 TT_ObjCMethodSpecifier)) {
2040 Current.setType(TT_SelectorName);
2041 }
else if (Current.isOneOf(tok::identifier, tok::kw_const, tok::kw_noexcept,
2042 tok::kw_requires) &&
2044 !Current.Previous->isOneOf(tok::equal, tok::at,
2045 TT_CtorInitializerComma,
2046 TT_CtorInitializerColon) &&
2047 Line.MightBeFunctionDecl && Contexts.size() == 1) {
2050 Current.setType(TT_TrailingAnnotation);
2052 Style.isJavaScript()) &&
2054 if (Current.Previous->is(tok::at) &&
2055 Current.isNot(Keywords.kw_interface)) {
2056 const FormatToken &AtToken = *Current.Previous;
2057 const FormatToken *
Previous = AtToken.getPreviousNonComment();
2059 Current.setType(TT_LeadingJavaAnnotation);
2061 Current.setType(TT_JavaAnnotation);
2062 }
else if (Current.Previous->is(tok::period) &&
2063 Current.Previous->isOneOf(TT_JavaAnnotation,
2064 TT_LeadingJavaAnnotation)) {
2065 Current.setType(Current.Previous->getType());
2075 bool isStartOfName(
const FormatToken &Tok) {
2076 if (Tok.isNot(tok::identifier) || !Tok.Previous)
2079 if (Tok.Previous->isOneOf(TT_LeadingJavaAnnotation, Keywords.kw_instanceof,
2083 if (Style.isJavaScript() && Tok.Previous->is(Keywords.kw_in))
2087 FormatToken *PreviousNotConst = Tok.getPreviousNonComment();
2090 if (!Style.isJavaScript())
2091 while (PreviousNotConst && PreviousNotConst->is(tok::kw_const))
2092 PreviousNotConst = PreviousNotConst->getPreviousNonComment();
2094 if (!PreviousNotConst)
2097 if (PreviousNotConst->ClosesRequiresClause)
2100 bool IsPPKeyword = PreviousNotConst->is(tok::identifier) &&
2101 PreviousNotConst->Previous &&
2102 PreviousNotConst->Previous->is(tok::hash);
2104 if (PreviousNotConst->is(TT_TemplateCloser)) {
2105 return PreviousNotConst && PreviousNotConst->MatchingParen &&
2106 PreviousNotConst->MatchingParen->Previous &&
2107 PreviousNotConst->MatchingParen->Previous->isNot(tok::period) &&
2108 PreviousNotConst->MatchingParen->Previous->isNot(tok::kw_template);
2111 if (PreviousNotConst->is(tok::r_paren) &&
2112 PreviousNotConst->is(TT_TypeDeclarationParen)) {
2121 if (PreviousNotConst->isOneOf(tok::identifier, tok::kw_auto))
2125 if (PreviousNotConst->is(TT_PointerOrReference))
2129 if (PreviousNotConst->isSimpleTypeSpecifier())
2134 PreviousNotConst->is(tok::r_square)) {
2139 return Style.isJavaScript() && PreviousNotConst->is(tok::kw_const);
2143 bool lParenStartsCppCast(
const FormatToken &Tok) {
2148 FormatToken *LeftOfParens = Tok.getPreviousNonComment();
2149 if (LeftOfParens && LeftOfParens->is(TT_TemplateCloser) &&
2150 LeftOfParens->MatchingParen) {
2151 auto *Prev = LeftOfParens->MatchingParen->getPreviousNonComment();
2153 Prev->isOneOf(tok::kw_const_cast, tok::kw_dynamic_cast,
2154 tok::kw_reinterpret_cast, tok::kw_static_cast)) {
2164 bool rParenEndsCast(
const FormatToken &Tok) {
2166 if (!Style.isCSharp() && !Style.isCpp() &&
2172 if (Tok.Previous == Tok.MatchingParen || !Tok.Next || !Tok.MatchingParen)
2175 if (Tok.MatchingParen->is(TT_OverloadedOperatorLParen))
2178 FormatToken *LeftOfParens = Tok.MatchingParen->getPreviousNonComment();
2182 if (LeftOfParens->is(tok::r_paren) &&
2183 LeftOfParens->isNot(TT_CastRParen)) {
2184 if (!LeftOfParens->MatchingParen ||
2185 !LeftOfParens->MatchingParen->Previous) {
2188 LeftOfParens = LeftOfParens->MatchingParen->Previous;
2191 if (LeftOfParens->is(tok::r_square)) {
2193 auto MayBeArrayDelete = [](FormatToken *Tok) -> FormatToken * {
2194 if (Tok->isNot(tok::r_square))
2197 Tok = Tok->getPreviousNonComment();
2198 if (!Tok || Tok->isNot(tok::l_square))
2201 Tok = Tok->getPreviousNonComment();
2202 if (!Tok || Tok->isNot(tok::kw_delete))
2206 if (FormatToken *MaybeDelete = MayBeArrayDelete(LeftOfParens))
2207 LeftOfParens = MaybeDelete;
2213 if (LeftOfParens->Tok.getIdentifierInfo() && LeftOfParens->Previous &&
2214 LeftOfParens->Previous->is(tok::kw_operator)) {
2220 if (LeftOfParens->Tok.getIdentifierInfo() &&
2221 !LeftOfParens->isOneOf(Keywords.kw_in, tok::kw_return, tok::kw_case,
2222 tok::kw_delete, tok::kw_throw)) {
2228 if (LeftOfParens->isOneOf(tok::at, tok::r_square, TT_OverloadedOperator,
2229 TT_TemplateCloser, tok::ellipsis)) {
2234 if (Tok.Next->is(tok::question))
2238 if (Tok.Next->is(Keywords.kw_in) && Style.isCSharp())
2243 if (Tok.Next->isOneOf(tok::kw_noexcept, tok::kw_volatile, tok::kw_const,
2244 tok::kw_requires, tok::kw_throw, tok::arrow,
2245 Keywords.kw_override, Keywords.kw_final) ||
2246 isCppAttribute(Style.isCpp(), *Tok.Next)) {
2256 if (Tok.Next->isNot(tok::string_literal) &&
2257 (Tok.Next->Tok.isLiteral() ||
2258 Tok.Next->isOneOf(tok::kw_sizeof, tok::kw_alignof))) {
2263 auto IsQualifiedPointerOrReference = [](FormatToken *T) {
2265 assert(!T->isSimpleTypeSpecifier() &&
"Should have already been checked");
2269 if (T->is(TT_AttributeParen)) {
2271 if (T->MatchingParen && T->MatchingParen->Previous &&
2272 T->MatchingParen->Previous->is(tok::kw___attribute)) {
2273 T = T->MatchingParen->Previous->Previous;
2276 }
else if (T->is(TT_AttributeSquare)) {
2278 if (T->MatchingParen && T->MatchingParen->Previous) {
2279 T = T->MatchingParen->Previous;
2282 }
else if (T->canBePointerOrReferenceQualifier()) {
2288 return T && T->is(TT_PointerOrReference);
2290 bool ParensAreType =
2292 Tok.Previous->isOneOf(TT_TemplateCloser, TT_TypeDeclarationParen) ||
2293 Tok.Previous->isSimpleTypeSpecifier() ||
2294 IsQualifiedPointerOrReference(Tok.Previous);
2295 bool ParensCouldEndDecl =
2296 Tok.Next->isOneOf(tok::equal, tok::semi, tok::l_brace, tok::greater);
2297 if (ParensAreType && !ParensCouldEndDecl)
2308 for (
const FormatToken *Token = Tok.MatchingParen->Next; Token != &Tok;
2309 Token = Token->Next) {
2310 if (Token->is(TT_BinaryOperator))
2316 if (Tok.Next->isOneOf(tok::identifier, tok::kw_this))
2320 if (Tok.Next->is(tok::l_paren) && Tok.Previous && Tok.Previous->Previous) {
2321 if (Tok.Previous->is(tok::identifier) &&
2322 Tok.Previous->Previous->is(tok::l_paren)) {
2327 if (!Tok.Next->Next)
2334 Tok.Next->isUnaryOperator() || Tok.Next->isOneOf(tok::amp, tok::star);
2335 if (!NextIsUnary || Tok.Next->is(tok::plus) ||
2336 !Tok.Next->Next->isOneOf(tok::identifier, tok::numeric_constant)) {
2340 for (FormatToken *Prev = Tok.Previous; Prev != Tok.MatchingParen;
2341 Prev = Prev->Previous) {
2342 if (!Prev->isOneOf(tok::kw_const, tok::identifier, tok::coloncolon))
2349 bool determineUnaryOperatorByUsage(
const FormatToken &Tok) {
2350 const FormatToken *PrevToken = Tok.getPreviousNonComment();
2360 if (PrevToken->isOneOf(
2361 TT_ConditionalExpr, tok::l_paren, tok::comma, tok::colon, tok::semi,
2362 tok::equal, tok::question, tok::l_square, tok::l_brace,
2363 tok::kw_case, tok::kw_co_await, tok::kw_co_return, tok::kw_co_yield,
2364 tok::kw_delete, tok::kw_return, tok::kw_throw)) {
2371 if (PrevToken->is(tok::kw_sizeof))
2375 if (PrevToken->isOneOf(TT_CastRParen, TT_UnaryOperator))
2379 if (PrevToken->is(TT_BinaryOperator))
2387 bool InTemplateArgument) {
2388 if (Style.isJavaScript())
2389 return TT_BinaryOperator;
2392 if (Style.isCSharp() && Tok.is(tok::ampamp))
2393 return TT_BinaryOperator;
2395 const FormatToken *PrevToken = Tok.getPreviousNonComment();
2397 return TT_UnaryOperator;
2399 const FormatToken *NextToken = Tok.getNextNonComment();
2401 if (InTemplateArgument && NextToken && NextToken->is(tok::kw_noexcept))
2402 return TT_BinaryOperator;
2405 NextToken->isOneOf(tok::arrow, tok::equal, tok::kw_noexcept, tok::comma,
2406 tok::r_paren, TT_RequiresClause) ||
2407 NextToken->canBePointerOrReferenceQualifier() ||
2408 (NextToken->is(tok::l_brace) && !NextToken->getNextNonComment())) {
2409 return TT_PointerOrReference;
2412 if (PrevToken->is(tok::coloncolon))
2413 return TT_PointerOrReference;
2415 if (PrevToken->is(tok::r_paren) && PrevToken->is(TT_TypeDeclarationParen))
2416 return TT_PointerOrReference;
2418 if (determineUnaryOperatorByUsage(Tok))
2419 return TT_UnaryOperator;
2421 if (NextToken->is(tok::l_square) && NextToken->isNot(TT_LambdaLSquare))
2422 return TT_PointerOrReference;
2424 return TT_PointerOrReference;
2425 if (NextToken->isOneOf(tok::comma, tok::semi))
2426 return TT_PointerOrReference;
2438 if (PrevToken->is(tok::r_brace) && Tok.is(tok::star) &&
2439 !PrevToken->MatchingParen) {
2440 return TT_PointerOrReference;
2444 if (PrevToken->Tok.isAnyIdentifier() && NextToken->Tok.isAnyIdentifier() &&
2445 NextToken->Next && NextToken->Next->is(tok::l_brace)) {
2446 return TT_PointerOrReference;
2449 if (PrevToken->endsSequence(tok::r_square, tok::l_square, tok::kw_delete))
2450 return TT_UnaryOperator;
2452 if (PrevToken->Tok.isLiteral() ||
2453 PrevToken->isOneOf(tok::r_paren, tok::r_square, tok::kw_true,
2454 tok::kw_false, tok::r_brace)) {
2455 return TT_BinaryOperator;
2458 const FormatToken *NextNonParen = NextToken;
2459 while (NextNonParen && NextNonParen->is(tok::l_paren))
2460 NextNonParen = NextNonParen->getNextNonComment();
2461 if (NextNonParen && (NextNonParen->Tok.isLiteral() ||
2462 NextNonParen->isOneOf(tok::kw_true, tok::kw_false) ||
2463 NextNonParen->isUnaryOperator())) {
2464 return TT_BinaryOperator;
2470 if (InTemplateArgument && NextToken->Tok.isAnyIdentifier())
2471 return TT_BinaryOperator;
2474 if (Tok.is(tok::ampamp) && NextToken->is(tok::l_paren))
2475 return TT_BinaryOperator;
2479 if (NextToken->Tok.isAnyIdentifier()) {
2480 const FormatToken *NextNextToken = NextToken->getNextNonComment();
2481 if (NextNextToken && NextNextToken->is(tok::arrow))
2482 return TT_BinaryOperator;
2488 return TT_BinaryOperator;
2491 if (!Scopes.empty() && Scopes.back() ==
ST_Class)
2492 return TT_PointerOrReference;
2495 auto IsChainedOperatorAmpOrMember = [](
const FormatToken *token) {
2496 return !token || token->isOneOf(tok::amp, tok::period, tok::arrow,
2497 tok::arrowstar, tok::periodstar);
2502 if (Tok.is(tok::amp) && PrevToken && PrevToken->Tok.isAnyIdentifier() &&
2503 IsChainedOperatorAmpOrMember(PrevToken->getPreviousNonComment()) &&
2504 NextToken && NextToken->Tok.isAnyIdentifier()) {
2505 if (
auto NextNext = NextToken->getNextNonComment();
2507 (IsChainedOperatorAmpOrMember(NextNext) || NextNext->is(tok::semi))) {
2508 return TT_BinaryOperator;
2512 return TT_PointerOrReference;
2515 TokenType determinePlusMinusCaretUsage(
const FormatToken &Tok) {
2516 if (determineUnaryOperatorByUsage(Tok))
2517 return TT_UnaryOperator;
2519 const FormatToken *PrevToken = Tok.getPreviousNonComment();
2521 return TT_UnaryOperator;
2523 if (PrevToken->is(tok::at))
2524 return TT_UnaryOperator;
2527 return TT_BinaryOperator;
2531 TokenType determineIncrementUsage(
const FormatToken &Tok) {
2532 const FormatToken *PrevToken = Tok.getPreviousNonComment();
2533 if (!PrevToken || PrevToken->is(TT_CastRParen))
2534 return TT_UnaryOperator;
2535 if (PrevToken->isOneOf(tok::r_paren, tok::r_square, tok::identifier))
2536 return TT_TrailingUnaryOperator;
2538 return TT_UnaryOperator;
2541 SmallVector<Context, 8> Contexts;
2543 const FormatStyle &Style;
2544 AnnotatedLine &Line;
2545 FormatToken *CurrentToken;
2547 const AdditionalKeywords &Keywords;
2549 SmallVector<ScopeType> &Scopes;
2563class ExpressionParser {
2565 ExpressionParser(
const FormatStyle &Style,
const AdditionalKeywords &Keywords,
2566 AnnotatedLine &Line)
2567 : Style(Style), Keywords(Keywords), Line(Line), Current(Line.
First) {}
2570 void parse(
int Precedence = 0) {
2573 while (Current && (Current->is(tok::kw_return) ||
2574 (Current->is(tok::colon) &&
2575 Current->isOneOf(TT_ObjCMethodExpr, TT_DictLiteral)))) {
2579 if (!Current || Precedence > PrecedenceArrowAndPeriod)
2584 parseConditionalExpr();
2590 if (Precedence == PrecedenceUnaryOperator) {
2591 parseUnaryOperator();
2595 FormatToken *Start = Current;
2596 FormatToken *LatestOperator =
nullptr;
2597 unsigned OperatorIndex = 0;
2599 FormatToken *VerilogFirstOfType =
nullptr;
2608 if (Style.isVerilog() && Precedence ==
prec::Comma) {
2609 VerilogFirstOfType =
2610 verilogGroupDecl(VerilogFirstOfType, LatestOperator);
2614 parse(Precedence + 1);
2620 if (Current && Current->MacroParent)
2623 int CurrentPrecedence = getCurrentPrecedence();
2625 if (Precedence == CurrentPrecedence && Current &&
2626 Current->is(TT_SelectorName)) {
2628 addFakeParenthesis(Start,
prec::Level(Precedence));
2635 (Current->closesScope() &&
2636 (Current->MatchingParen || Current->is(TT_TemplateString))) ||
2637 (CurrentPrecedence != -1 && CurrentPrecedence < Precedence) ||
2646 if (Current->opensScope() ||
2647 Current->isOneOf(TT_RequiresClause,
2648 TT_RequiresClauseInARequiresExpression)) {
2651 while (Current && (!Current->closesScope() || Current->opensScope())) {
2658 if (CurrentPrecedence == Precedence) {
2660 LatestOperator->NextOperator = Current;
2661 LatestOperator = Current;
2662 Current->OperatorIndex = OperatorIndex;
2665 next(Precedence > 0);
2670 if (Style.isVerilog() && Precedence ==
prec::Comma && VerilogFirstOfType)
2671 addFakeParenthesis(VerilogFirstOfType,
prec::Comma);
2673 if (LatestOperator && (Current || Precedence > 0)) {
2679 Start->Previous->isOneOf(TT_RequiresClause,
2680 TT_RequiresClauseInARequiresExpression))
2682 auto Ret = Current ? Current : Line.Last;
2683 while (!
Ret->ClosesRequiresClause &&
Ret->Previous)
2689 if (Precedence == PrecedenceArrowAndPeriod) {
2693 addFakeParenthesis(Start,
prec::Level(Precedence), End);
2701 int getCurrentPrecedence() {
2703 const FormatToken *NextNonComment = Current->getNextNonComment();
2704 if (Current->is(TT_ConditionalExpr))
2706 if (NextNonComment && Current->is(TT_SelectorName) &&
2707 (NextNonComment->isOneOf(TT_DictLiteral, TT_JsTypeColon) ||
2710 NextNonComment->is(tok::less)))) {
2713 if (Current->is(TT_JsComputedPropertyName))
2715 if (Current->is(TT_LambdaArrow))
2717 if (Current->is(TT_FatArrow))
2719 if (Current->isOneOf(tok::semi, TT_InlineASMColon, TT_SelectorName) ||
2720 (Current->is(tok::comment) && NextNonComment &&
2721 NextNonComment->is(TT_SelectorName))) {
2724 if (Current->is(TT_RangeBasedForLoopColon))
2727 Current->is(Keywords.kw_instanceof)) {
2730 if (Style.isJavaScript() &&
2731 Current->isOneOf(Keywords.kw_in, Keywords.kw_as)) {
2734 if (Current->is(TT_BinaryOperator) || Current->is(tok::comma))
2735 return Current->getPrecedence();
2736 if (Current->isOneOf(tok::period, tok::arrow) &&
2737 Current->isNot(TT_TrailingReturnArrow)) {
2738 return PrecedenceArrowAndPeriod;
2741 Current->isOneOf(Keywords.kw_extends, Keywords.kw_implements,
2742 Keywords.kw_throws)) {
2747 if (Style.isVerilog() && Current->is(tok::colon))
2753 void addFakeParenthesis(FormatToken *Start,
prec::Level Precedence,
2754 FormatToken *End =
nullptr) {
2755 Start->FakeLParens.push_back(Precedence);
2757 Start->StartsBinaryExpression =
true;
2758 if (!End && Current)
2759 End = Current->getPreviousNonComment();
2763 End->EndsBinaryExpression =
true;
2769 void parseUnaryOperator() {
2771 while (Current && Current->is(TT_UnaryOperator)) {
2772 Tokens.push_back(Current);
2775 parse(PrecedenceArrowAndPeriod);
2776 for (FormatToken *Token : llvm::reverse(Tokens)) {
2782 void parseConditionalExpr() {
2783 while (Current && Current->isTrailingComment())
2785 FormatToken *Start = Current;
2787 if (!Current || !Current->is(tok::question))
2791 if (!Current || Current->isNot(TT_ConditionalExpr))
2798 void next(
bool SkipPastLeadingComments =
true) {
2800 Current = Current->Next;
2802 (Current->NewlinesBefore == 0 || SkipPastLeadingComments) &&
2803 Current->isTrailingComment()) {
2804 Current = Current->Next;
2810 FormatToken *verilogGroupDecl(FormatToken *FirstOfType,
2811 FormatToken *PreviousComma) {
2815 FormatToken *Start = Current;
2818 while (Start->startsSequence(tok::l_paren, tok::star)) {
2819 if (!(Start = Start->MatchingParen) ||
2820 !(Start = Start->getNextNonComment())) {
2825 FormatToken *Tok = Start;
2827 if (Tok->is(Keywords.kw_assign))
2828 Tok = Tok->getNextNonComment();
2836 FormatToken *
First =
nullptr;
2838 FormatToken *Next = Tok->getNextNonComment();
2840 if (Tok->is(tok::hash)) {
2845 Tok = Tok->getNextNonComment();
2846 }
else if (Tok->is(tok::hashhash)) {
2850 Tok = Tok->getNextNonComment();
2851 }
else if ((Keywords.isVerilogQualifier(*Tok) ||
2852 Keywords.isVerilogIdentifier(*Tok))) {
2856 while (Tok && Tok->isOneOf(tok::period, tok::coloncolon) &&
2857 (Tok = Tok->getNextNonComment())) {
2858 if (Keywords.isVerilogIdentifier(*Tok))
2859 Tok = Tok->getNextNonComment();
2863 }
else if (Tok->is(tok::l_paren)) {
2868 Keywords.kw_highz0, Keywords.kw_highz1, Keywords.kw_large,
2869 Keywords.kw_medium, Keywords.kw_pull0, Keywords.kw_pull1,
2870 Keywords.kw_small, Keywords.kw_strong0, Keywords.kw_strong1,
2871 Keywords.kw_supply0, Keywords.kw_supply1, Keywords.kw_weak0,
2872 Keywords.kw_weak1)) {
2873 Tok->setType(TT_VerilogStrength);
2874 Tok = Tok->MatchingParen;
2876 Tok->setType(TT_VerilogStrength);
2877 Tok = Tok->getNextNonComment();
2882 }
else if (Tok->is(tok::hash)) {
2883 if (Next->is(tok::l_paren))
2884 Next = Next->MatchingParen;
2886 Tok = Next->getNextNonComment();
2893 FormatToken *Second =
nullptr;
2895 while (Tok && Tok->is(tok::l_square) && (Tok = Tok->MatchingParen))
2896 Tok = Tok->getNextNonComment();
2897 if (Tok && (Tok->is(tok::hash) || Keywords.isVerilogIdentifier(*Tok)))
2902 FormatToken *TypedName =
nullptr;
2906 First->setType(TT_VerilogDimensionedTypeName);
2907 }
else if (
First != Start) {
2915 if (TypedName->is(TT_Unknown))
2916 TypedName->setType(TT_StartOfName);
2918 if (FirstOfType && PreviousComma) {
2919 PreviousComma->setType(TT_VerilogTypeComma);
2920 addFakeParenthesis(FirstOfType,
prec::Comma, PreviousComma->Previous);
2923 FirstOfType = TypedName;
2930 while (Current && Current != FirstOfType) {
2931 if (Current->opensScope()) {
2942 const FormatStyle &Style;
2943 const AdditionalKeywords &Keywords;
2944 const AnnotatedLine &Line;
2945 FormatToken *Current;
2954 assert(Line->First);
2958 if (NextNonCommentLine && Line->isComment() &&
2961 Line->First->OriginalColumn) {
2962 const bool PPDirectiveOrImportStmt =
2965 if (PPDirectiveOrImportStmt)
2971 PPDirectiveOrImportStmt
2973 : NextNonCommentLine->
Level;
2975 NextNonCommentLine = Line->First->isNot(tok::r_brace) ? Line :
nullptr;
2984 for (
const auto *Tok = Line.
First; Tok; Tok = Tok->
Next)
2990 for (
auto &Child : Line.Children)
2993 AnnotatingParser
Parser(Style, Line, Keywords, Scopes);
2994 Line.Type =
Parser.parseLine();
3006 ExpressionParser ExprParser(Style, Keywords, Line);
3009 if (Line.startsWith(TT_ObjCMethodSpecifier))
3011 else if (Line.startsWith(TT_ObjCDecl))
3013 else if (Line.startsWith(TT_ObjCProperty))
3016 Line.First->SpacesRequiredBefore = 1;
3017 Line.First->CanBreakBefore = Line.First->MustBreakBefore;
3025 for (; Next; Next = Next->Next) {
3026 if (Next->is(TT_OverloadedOperatorLParen))
3028 if (Next->is(TT_OverloadedOperator))
3030 if (Next->isOneOf(tok::kw_new, tok::kw_delete)) {
3033 Next->Next->startsSequence(tok::l_square, tok::r_square)) {
3034 Next = Next->Next->Next;
3038 if (Next->startsSequence(tok::l_square, tok::r_square)) {
3043 if ((Next->isSimpleTypeSpecifier() || Next->is(tok::identifier)) &&
3044 Next->Next && Next->Next->isOneOf(tok::star, tok::amp, tok::ampamp)) {
3049 if (Next->is(TT_TemplateOpener) && Next->MatchingParen) {
3050 Next = Next->MatchingParen;
3061 if (Current.
is(tok::kw_operator)) {
3064 Next = skipOperatorName(Next);
3068 for (; Next; Next = Next->Next) {
3069 if (Next->is(TT_TemplateOpener) && Next->MatchingParen) {
3070 Next = Next->MatchingParen;
3071 }
else if (Next->is(tok::coloncolon)) {
3075 if (Next->is(tok::kw_operator)) {
3076 Next = skipOperatorName(Next->Next);
3079 if (!Next->is(tok::identifier))
3081 }
else if (isCppAttribute(IsCpp, *Next)) {
3082 Next = Next->MatchingParen;
3085 }
else if (Next->is(tok::l_paren)) {
3094 if (!Next || !Next->is(tok::l_paren) || !Next->MatchingParen)
3097 if (Line.
Last->
is(tok::l_brace))
3099 if (Next->Next == Next->MatchingParen)
3102 if (Next->MatchingParen->Next &&
3103 Next->MatchingParen->Next->is(TT_PointerOrReference)) {
3117 if (IsCpp && Next->Next && Next->Next->is(tok::identifier) &&
3122 for (
const FormatToken *Tok = Next->Next; Tok && Tok != Next->MatchingParen;
3124 if (Tok->is(TT_TypeDeclarationParen))
3126 if (Tok->isOneOf(tok::l_paren, TT_TemplateOpener) && Tok->MatchingParen) {
3127 Tok = Tok->MatchingParen;
3130 if (Tok->is(tok::kw_const) || Tok->isSimpleTypeSpecifier() ||
3131 Tok->isOneOf(TT_PointerOrReference, TT_StartOfName, tok::ellipsis)) {
3134 if (Tok->isOneOf(tok::l_brace, tok::string_literal, TT_ObjCMethodExpr) ||
3135 Tok->Tok.isLiteral()) {
3142bool TokenAnnotator::mustBreakForReturnType(
const AnnotatedLine &Line)
const {
3143 assert(Line.MightBeFunctionDecl);
3146 Style.AlwaysBreakAfterReturnType ==
3152 switch (Style.AlwaysBreakAfterReturnType) {
3160 return Line.mightBeFunctionDefinition();
3182 Line.First->TotalLength =
3183 Line.First->IsMultiline ? Style.ColumnLimit
3184 : Line.FirstStartColumn + Line.First->ColumnWidth;
3186 bool InFunctionDecl = Line.MightBeFunctionDecl;
3187 bool AlignArrayOfStructures =
3190 if (AlignArrayOfStructures)
3191 calculateArrayInitializerColumnList(Line);
3193 for (
FormatToken *Tok = Current, *AfterLastAttribute =
nullptr; Tok;
3196 Tok->setType(TT_FunctionDeclarationName);
3197 if (AfterLastAttribute &&
3199 AfterLastAttribute->MustBreakBefore =
true;
3200 Line.ReturnTypeWrapped =
true;
3204 if (Tok->Previous->EndsCppAttributeGroup)
3205 AfterLastAttribute = Tok;
3210 if (Current->is(TT_LineComment)) {
3212 Current->SpacesRequiredBefore =
3213 (Style.Cpp11BracedListStyle && !Style.SpacesInParentheses) ? 0 : 1;
3215 Current->SpacesRequiredBefore = Style.SpacesBeforeTrailingComments;
3225 if (!Current->HasUnescapedNewline) {
3228 if (
Parameter->isOneOf(tok::comment, tok::r_brace))
3231 if (!
Parameter->Previous->is(TT_CtorInitializerComma) &&
3239 }
else if (Current->SpacesRequiredBefore == 0 &&
3240 spaceRequiredBefore(Line, *Current)) {
3241 Current->SpacesRequiredBefore = 1;
3244 const auto &Children = Prev->
Children;
3245 if (!Children.empty() && Children.back()->Last->is(TT_LineComment)) {
3246 Current->MustBreakBefore =
true;
3248 Current->MustBreakBefore =
3249 Current->MustBreakBefore || mustBreakBefore(Line, *Current);
3250 if (!Current->MustBreakBefore && InFunctionDecl &&
3251 Current->is(TT_FunctionDeclarationName)) {
3252 Current->MustBreakBefore = mustBreakForReturnType(Line);
3256 Current->CanBreakBefore =
3257 Current->MustBreakBefore || canBreakBefore(Line, *Current);
3258 unsigned ChildSize = 0;
3264 if (Current->MustBreakBefore || Prev->
Children.size() > 1 ||
3266 Prev->
Children[0]->First->MustBreakBefore) ||
3267 Current->IsMultiline) {
3268 Current->TotalLength = Prev->
TotalLength + Style.ColumnLimit;
3270 Current->TotalLength = Prev->
TotalLength + Current->ColumnWidth +
3271 ChildSize + Current->SpacesRequiredBefore;
3274 if (Current->is(TT_CtorInitializerColon))
3275 InFunctionDecl =
false;
3286 Current->SplitPenalty = splitPenalty(Line, *Current, InFunctionDecl);
3288 Current->is(TT_SelectorName) && Current->ParameterIndex > 0) {
3289 if (Current->ParameterIndex == 1)
3290 Current->SplitPenalty += 5 * Current->BindingStrength;
3292 Current->SplitPenalty += 20 * Current->BindingStrength;
3295 Current = Current->Next;
3298 calculateUnbreakableTailLengths(Line);
3299 unsigned IndentLevel = Line.Level;
3300 for (Current = Line.First; Current; Current = Current->Next) {
3302 Current->Role->precomputeFormattingInfos(Current);
3303 if (Current->MatchingParen &&
3304 Current->MatchingParen->opensBlockOrBlockTypeList(Style) &&
3308 Current->IndentLevel = IndentLevel;
3309 if (Current->opensBlockOrBlockTypeList(Style))
3313 LLVM_DEBUG({ printDebugInfo(Line); });
3316void TokenAnnotator::calculateUnbreakableTailLengths(
3318 unsigned UnbreakableTailLength = 0;
3321 Current->UnbreakableTailLength = UnbreakableTailLength;
3322 if (Current->CanBreakBefore ||
3323 Current->isOneOf(tok::comment, tok::string_literal)) {
3324 UnbreakableTailLength = 0;
3326 UnbreakableTailLength +=
3327 Current->ColumnWidth + Current->SpacesRequiredBefore;
3329 Current = Current->Previous;
3333void TokenAnnotator::calculateArrayInitializerColumnList(
3334 AnnotatedLine &Line)
const {
3335 if (Line.First == Line.Last)
3337 auto *CurrentToken = Line.First;
3338 CurrentToken->ArrayInitializerLineStart =
true;
3340 while (CurrentToken && CurrentToken != Line.Last) {
3341 if (CurrentToken->is(tok::l_brace)) {
3342 CurrentToken->IsArrayInitializer =
true;
3343 if (CurrentToken->Next)
3344 CurrentToken->Next->MustBreakBefore =
true;
3346 calculateInitializerColumnList(Line, CurrentToken->Next, Depth + 1);
3348 CurrentToken = CurrentToken->Next;
3353FormatToken *TokenAnnotator::calculateInitializerColumnList(
3354 AnnotatedLine &Line, FormatToken *CurrentToken,
unsigned Depth)
const {
3355 while (CurrentToken && CurrentToken != Line.Last) {
3356 if (CurrentToken->is(tok::l_brace))
3358 else if (CurrentToken->is(tok::r_brace))
3360 if (Depth == 2 && CurrentToken->isOneOf(tok::l_brace, tok::comma)) {
3361 CurrentToken = CurrentToken->Next;
3364 CurrentToken->StartsColumn =
true;
3365 CurrentToken = CurrentToken->Previous;
3367 CurrentToken = CurrentToken->Next;
3369 return CurrentToken;
3372unsigned TokenAnnotator::splitPenalty(
const AnnotatedLine &Line,
3373 const FormatToken &Tok,
3374 bool InFunctionDecl)
const {
3375 const FormatToken &Left = *Tok.Previous;
3376 const FormatToken &Right = Tok;
3378 if (Left.is(tok::semi))
3383 if (Right.isOneOf(Keywords.kw_extends, Keywords.kw_throws))
3385 if (Right.is(Keywords.kw_implements))
3387 if (Left.is(tok::comma) && Left.NestingLevel == 0)
3389 }
else if (Style.isJavaScript()) {
3390 if (Right.is(Keywords.kw_function) && Left.isNot(tok::comma))
3392 if (Left.is(TT_JsTypeColon))
3394 if ((Left.is(TT_TemplateString) && Left.TokenText.endswith(
"${")) ||
3395 (Right.is(TT_TemplateString) && Right.TokenText.startswith(
"}"))) {
3399 if (Left.opensScope() && Right.closesScope())
3401 }
else if (Style.isProto()) {
3402 if (Right.is(tok::l_square))
3404 if (Right.is(tok::period))
3408 if (Right.is(tok::identifier) && Right.Next && Right.Next->is(TT_DictLiteral))
3410 if (Right.is(tok::l_square)) {
3411 if (Left.is(tok::r_square))
3414 if (Right.is(TT_LambdaLSquare) && Left.is(tok::equal))
3416 if (!Right.isOneOf(TT_ObjCMethodExpr, TT_LambdaLSquare,
3417 TT_ArrayInitializerLSquare,
3418 TT_DesignatedInitializerLSquare, TT_AttributeSquare)) {
3423 if (Left.is(tok::coloncolon))
3425 if (Right.isOneOf(TT_StartOfName, TT_FunctionDeclarationName) ||
3426 Right.is(tok::kw_operator)) {
3427 if (Line.startsWith(tok::kw_for) && Right.PartOfMultiVariableDeclStmt)
3429 if (Left.is(TT_StartOfName))
3431 if (InFunctionDecl && Right.NestingLevel == 0)
3432 return Style.PenaltyReturnTypeOnItsOwnLine;
3435 if (Right.is(TT_PointerOrReference))
3437 if (Right.is(TT_LambdaArrow))
3439 if (Left.is(tok::equal) && Right.is(tok::l_brace))
3441 if (Left.is(TT_CastRParen))
3443 if (Left.isOneOf(tok::kw_class, tok::kw_struct, tok::kw_union))
3445 if (Left.is(tok::comment))
3448 if (Left.isOneOf(TT_RangeBasedForLoopColon, TT_InheritanceColon,
3449 TT_CtorInitializerColon)) {
3453 if (Right.isMemberAccess()) {
3473 return !Right.NextOperator || !Right.NextOperator->Previous->closesScope()
3478 if (Right.is(TT_TrailingAnnotation) &&
3479 (!Right.Next || Right.Next->isNot(tok::l_paren))) {
3482 if (Line.startsWith(TT_ObjCMethodSpecifier))
3489 bool is_short_annotation = Right.TokenText.size() < 10;
3490 return (Left.is(tok::r_paren) ? 100 : 120) + (is_short_annotation ? 50 : 0);
3494 if (Line.startsWith(tok::kw_for) && Left.is(tok::equal))
3499 if (Right.is(TT_SelectorName))
3501 if (Left.is(tok::colon) && Left.is(TT_ObjCMethodExpr))
3502 return Line.MightBeFunctionDecl ? 50 : 500;
3507 if (Line.Type ==
LT_ObjCDecl && Left.is(tok::l_paren) && Left.Previous &&
3508 Left.Previous->isOneOf(tok::identifier, tok::greater)) {
3512 if (Left.is(tok::l_paren) && Style.PenaltyBreakOpenParenthesis != 0)
3513 return Style.PenaltyBreakOpenParenthesis;
3514 if (Left.is(tok::l_paren) && InFunctionDecl &&
3518 if (Left.is(tok::l_paren) && Left.Previous &&
3519 (Left.Previous->isOneOf(tok::kw_for, tok::kw__Generic) ||
3520 Left.Previous->isIf())) {
3523 if (Left.is(tok::equal) && InFunctionDecl)
3525 if (Right.is(tok::r_brace))
3527 if (Left.is(TT_TemplateOpener))
3529 if (Left.opensScope()) {
3534 (Left.ParameterCount <= 1 || Style.AllowAllArgumentsOnNextLine)) {
3537 if (Left.is(tok::l_brace) && !Style.Cpp11BracedListStyle)
3539 return Left.ParameterCount > 1 ? Style.PenaltyBreakBeforeFirstCallParameter
3542 if (Left.is(TT_JavaAnnotation))
3545 if (Left.is(TT_UnaryOperator))
3547 if (Left.isOneOf(tok::plus, tok::comma) && Left.Previous &&
3548 Left.Previous->isLabelString() &&
3549 (Left.NextOperator || Left.OperatorIndex != 0)) {
3552 if (Right.is(tok::plus) && Left.isLabelString() &&
3553 (Right.NextOperator || Right.OperatorIndex != 0)) {
3556 if (Left.is(tok::comma))
3558 if (Right.is(tok::lessless) && Left.isLabelString() &&
3559 (Right.NextOperator || Right.OperatorIndex != 1)) {
3562 if (Right.is(tok::lessless)) {
3564 if (!Left.is(tok::r_paren) || Right.OperatorIndex > 0) {
3570 if (Left.ClosesTemplateDeclaration)
3571 return Style.PenaltyBreakTemplateDeclaration;
3572 if (Left.ClosesRequiresClause)
3574 if (Left.is(TT_ConditionalExpr))
3578 Level = Right.getPrecedence();
3580 return Style.PenaltyBreakAssignment;
3587bool TokenAnnotator::spaceRequiredBeforeParens(
const FormatToken &Right)
const {
3590 if (Right.is(TT_OverloadedOperatorLParen) &&
3591 Style.SpaceBeforeParensOptions.AfterOverloadedOperator) {
3594 if (Style.SpaceBeforeParensOptions.BeforeNonEmptyParentheses &&
3595 Right.ParameterCount > 0) {
3601bool TokenAnnotator::spaceRequiredBetween(
const AnnotatedLine &Line,
3602 const FormatToken &Left,
3603 const FormatToken &Right)
const {
3604 if (Left.is(tok::kw_return) &&
3605 !Right.isOneOf(tok::semi, tok::r_paren, tok::hashhash)) {
3608 if (Left.is(tok::kw_throw) && Right.is(tok::l_paren) && Right.MatchingParen &&
3609 Right.MatchingParen->is(TT_CastRParen)) {
3612 if (Style.isJson() && Left.is(tok::string_literal) && Right.is(tok::colon))
3617 Left.Tok.getObjCKeywordID() == tok::objc_property) {
3620 if (Right.is(tok::hashhash))
3621 return Left.is(tok::hash);
3622 if (Left.isOneOf(tok::hashhash, tok::hash))
3623 return Right.is(tok::hash);
3624 if ((Left.is(tok::l_paren) && Right.is(tok::r_paren)) ||
3625 (Left.is(tok::l_brace) && Left.isNot(
BK_Block) &&
3626 Right.is(tok::r_brace) && Right.isNot(
BK_Block))) {
3627 return Style.SpaceInEmptyParentheses;
3629 if (Style.SpacesInConditionalStatement) {
3630 const FormatToken *LeftParen =
nullptr;
3631 if (Left.is(tok::l_paren))
3633 else if (Right.is(tok::r_paren) && Right.MatchingParen)
3634 LeftParen = Right.MatchingParen;
3635 if (LeftParen && LeftParen->Previous &&
3636 isKeywordWithCondition(*LeftParen->Previous)) {
3642 if (Left.is(tok::kw_auto) && Right.isOneOf(TT_LambdaLBrace, TT_FunctionLBrace,
3644 TT_FunctionTypeLParen)) {
3649 if (Left.is(tok::kw_auto) && Right.isOneOf(tok::l_paren, tok::l_brace))
3653 if (Right.is(tok::l_paren) && Left.is(tok::kw_co_await) && Left.Previous &&
3654 Left.Previous->is(tok::kw_operator)) {
3658 if (Left.isOneOf(tok::kw_co_await, tok::kw_co_yield, tok::kw_co_return) &&
3659 !Right.isOneOf(tok::semi, tok::r_paren)) {
3663 if (Left.is(tok::l_paren) || Right.is(tok::r_paren)) {
3664 return (Right.is(TT_CastRParen) ||
3665 (Left.MatchingParen && Left.MatchingParen->is(TT_CastRParen)))
3666 ? Style.SpacesInCStyleCastParentheses
3667 : Style.SpacesInParentheses;
3669 if (Right.isOneOf(tok::semi, tok::comma))
3671 if (Right.is(tok::less) && Line.Type ==
LT_ObjCDecl) {
3672 bool IsLightweightGeneric = Right.MatchingParen &&
3673 Right.MatchingParen->Next &&
3674 Right.MatchingParen->Next->is(tok::colon);
3675 return !IsLightweightGeneric && Style.ObjCSpaceBeforeProtocolList;
3677 if (Right.is(tok::less) && Left.is(tok::kw_template))
3678 return Style.SpaceAfterTemplateKeyword;
3679 if (Left.isOneOf(tok::exclaim, tok::tilde))
3681 if (Left.is(tok::at) &&
3682 Right.isOneOf(tok::identifier, tok::string_literal, tok::char_constant,
3683 tok::numeric_constant, tok::l_paren, tok::l_brace,
3684 tok::kw_true, tok::kw_false)) {
3687 if (Left.is(tok::colon))
3688 return !Left.is(TT_ObjCMethodExpr);
3689 if (Left.is(tok::coloncolon))
3691 if (Left.is(tok::less) || Right.isOneOf(tok::greater, tok::less)) {
3694 (Left.is(TT_DictLiteral) || Right.is(TT_DictLiteral)))) {
3696 if (Left.is(tok::less) && Right.is(tok::greater))
3698 return !Style.Cpp11BracedListStyle;
3701 if (Right.isNot(TT_OverloadedOperatorLParen))
3704 if (Right.is(tok::ellipsis)) {
3705 return Left.Tok.isLiteral() || (Left.is(tok::identifier) && Left.Previous &&
3706 Left.Previous->is(tok::kw_case));
3708 if (Left.is(tok::l_square) && Right.is(tok::amp))
3709 return Style.SpacesInSquareBrackets;
3710 if (Right.is(TT_PointerOrReference)) {
3711 if (Left.is(tok::r_paren) && Line.MightBeFunctionDecl) {
3712 if (!Left.MatchingParen)
3714 FormatToken *TokenBeforeMatchingParen =
3715 Left.MatchingParen->getPreviousNonComment();
3716 if (!TokenBeforeMatchingParen || !Left.is(TT_TypeDeclarationParen))
3724 (Left.is(TT_AttributeParen) ||
3725 Left.canBePointerOrReferenceQualifier())) {
3728 if (Left.Tok.isLiteral())
3731 if (Left.isTypeOrIdentifier() && Right.Next && Right.Next->Next &&
3732 Right.Next->Next->is(TT_RangeBasedForLoopColon)) {
3733 return getTokenPointerOrReferenceAlignment(Right) !=
3736 return !Left.isOneOf(TT_PointerOrReference, tok::l_paren) &&
3737 (getTokenPointerOrReferenceAlignment(Right) !=
3739 (Line.IsMultiVariableDeclStmt &&
3740 (Left.NestingLevel == 0 ||
3741 (Left.NestingLevel == 1 && startsWithInitStatement(Line)))));
3743 if (Right.is(TT_FunctionTypeLParen) && Left.isNot(tok::l_paren) &&
3744 (!Left.is(TT_PointerOrReference) ||
3746 !Line.IsMultiVariableDeclStmt))) {
3749 if (Left.is(TT_PointerOrReference)) {
3754 Right.canBePointerOrReferenceQualifier()) {
3758 if (Right.Tok.isLiteral())
3761 if (Right.is(TT_BlockComment))
3765 if (Right.isOneOf(Keywords.kw_override, Keywords.kw_final, tok::kw_noexcept,
3766 TT_RequiresClause) &&
3767 !Right.is(TT_StartOfName)) {
3771 if (Right.is(tok::l_brace) && Right.is(
BK_Block))
3774 if (Left.Previous && Left.Previous->isTypeOrIdentifier() && Right.Next &&
3775 Right.Next->is(TT_RangeBasedForLoopColon)) {
3776 return getTokenPointerOrReferenceAlignment(Left) !=
3779 if (Right.isOneOf(TT_PointerOrReference, TT_ArraySubscriptLSquare,
3791 if (Line.IsMultiVariableDeclStmt &&
3792 (Left.NestingLevel == Line.First->NestingLevel ||
3793 ((Left.NestingLevel == Line.First->NestingLevel + 1) &&
3794 startsWithInitStatement(Line)))) {
3797 return Left.Previous && !Left.Previous->isOneOf(
3798 tok::l_paren, tok::coloncolon, tok::l_square);
3801 if (Left.is(tok::ellipsis) && Left.Previous &&
3802 Left.Previous->isOneOf(tok::star, tok::amp, tok::ampamp)) {
3806 if (Right.is(tok::star) && Left.is(tok::l_paren))
3808 if (Left.is(tok::star) && Right.isOneOf(tok::star, tok::amp, tok::ampamp))
3810 if (Right.isOneOf(tok::star, tok::amp, tok::ampamp)) {
3811 const FormatToken *
Previous = &Left;
3821 if (
Previous->is(tok::coloncolon)) {
3840 if (
Previous->endsSequence(tok::kw_operator))
3844 (Style.SpaceAroundPointerQualifiers ==
3850 const auto SpaceRequiredForArrayInitializerLSquare =
3851 [](
const FormatToken &LSquareTok,
const FormatStyle &Style) {
3852 return Style.SpacesInContainerLiterals ||
3855 !Style.Cpp11BracedListStyle &&
3856 LSquareTok.endsSequence(tok::l_square, tok::colon,
3859 if (Left.is(tok::l_square)) {
3860 return (Left.is(TT_ArrayInitializerLSquare) && Right.isNot(tok::r_square) &&
3861 SpaceRequiredForArrayInitializerLSquare(Left, Style)) ||
3862 (Left.isOneOf(TT_ArraySubscriptLSquare, TT_StructuredBindingLSquare,
3863 TT_LambdaLSquare) &&
3864 Style.SpacesInSquareBrackets && Right.isNot(tok::r_square));
3866 if (Right.is(tok::r_square)) {
3867 return Right.MatchingParen &&
3868 ((Right.MatchingParen->is(TT_ArrayInitializerLSquare) &&
3869 SpaceRequiredForArrayInitializerLSquare(*Right.MatchingParen,
3871 (Style.SpacesInSquareBrackets &&
3872 Right.MatchingParen->isOneOf(TT_ArraySubscriptLSquare,
3873 TT_StructuredBindingLSquare,
3874 TT_LambdaLSquare)) ||
3875 Right.MatchingParen->is(TT_AttributeParen));
3877 if (Right.is(tok::l_square) &&
3878 !Right.isOneOf(TT_ObjCMethodExpr, TT_LambdaLSquare,
3879 TT_DesignatedInitializerLSquare,
3880 TT_StructuredBindingLSquare, TT_AttributeSquare) &&
3881 !Left.isOneOf(tok::numeric_constant, TT_DictLiteral) &&
3882 !(!Left.is(tok::r_square) && Style.SpaceBeforeSquareBrackets &&
3883 Right.is(TT_ArraySubscriptLSquare))) {
3886 if (Left.is(tok::l_brace) && Right.is(tok::r_brace))
3887 return !Left.Children.empty();
3888 if ((Left.is(tok::l_brace) && Left.isNot(
BK_Block)) ||
3889 (Right.is(tok::r_brace) && Right.MatchingParen &&
3890 Right.MatchingParen->isNot(
BK_Block))) {
3891 return Style.Cpp11BracedListStyle ? Style.SpacesInParentheses :
true;
3893 if (Left.is(TT_BlockComment)) {
3895 return Style.isJavaScript() || !Left.TokenText.endswith(
"=*/");
3900 if (Left.is(TT_TemplateCloser) && Right.is(TT_AttributeSquare))
3903 if (Right.is(tok::l_paren)) {
3904 if (Left.is(TT_TemplateCloser) && Right.isNot(TT_FunctionTypeLParen))
3905 return spaceRequiredBeforeParens(Right);
3906 if (Left.isOneOf(TT_RequiresClause,
3907 TT_RequiresClauseInARequiresExpression)) {
3908 return Style.SpaceBeforeParensOptions.AfterRequiresInClause ||
3909 spaceRequiredBeforeParens(Right);
3911 if (Left.is(TT_RequiresExpression)) {
3912 return Style.SpaceBeforeParensOptions.AfterRequiresInExpression ||
3913 spaceRequiredBeforeParens(Right);
3915 if ((Left.is(tok::r_paren) && Left.is(TT_AttributeParen)) ||
3916 (Left.is(tok::r_square) && Left.is(TT_AttributeSquare))) {
3919 if (Left.is(TT_ForEachMacro)) {
3920 return Style.SpaceBeforeParensOptions.AfterForeachMacros ||
3921 spaceRequiredBeforeParens(Right);
3923 if (Left.is(TT_IfMacro)) {
3924 return Style.SpaceBeforeParensOptions.AfterIfMacros ||
3925 spaceRequiredBeforeParens(Right);
3929 if (Left.is(tok::semi))
3931 if (Left.isOneOf(tok::pp_elif, tok::kw_for, tok::kw_while, tok::kw_switch,
3932 tok::kw_case, TT_ForEachMacro, TT_ObjCForIn) ||
3934 Right.is(TT_ConditionLParen)) {
3935 return Style.SpaceBeforeParensOptions.AfterControlStatements ||
3936 spaceRequiredBeforeParens(Right);
3941 if (Right.is(TT_OverloadedOperatorLParen))
3942 return spaceRequiredBeforeParens(Right);
3944 if (Line.MightBeFunctionDecl && (Left.is(TT_FunctionDeclarationName))) {
3945 if (Line.mightBeFunctionDefinition()) {
3946 return Style.SpaceBeforeParensOptions.AfterFunctionDefinitionName ||
3947 spaceRequiredBeforeParens(Right);
3949 return Style.SpaceBeforeParensOptions.AfterFunctionDeclarationName ||
3950 spaceRequiredBeforeParens(Right);
3955 Left.MatchingParen && Left.MatchingParen->is(TT_LambdaLSquare)) {
3956 return Style.SpaceBeforeParensOptions.AfterFunctionDefinitionName ||
3957 spaceRequiredBeforeParens(Right);
3959 if (!Left.Previous || Left.Previous->isNot(tok::period)) {
3960 if (Left.isOneOf(tok::kw_try, Keywords.kw___except, tok::kw_catch)) {
3961 return Style.SpaceBeforeParensOptions.AfterControlStatements ||
3962 spaceRequiredBeforeParens(Right);
3964 if (Left.isOneOf(tok::kw_new, tok::kw_delete)) {
3965 return ((!Line.MightBeFunctionDecl || !Left.Previous) &&
3967 spaceRequiredBeforeParens(Right);
3970 if (Left.is(tok::r_square) && Left.MatchingParen &&
3971 Left.MatchingParen->Previous &&
3972 Left.MatchingParen->Previous->is(tok::kw_delete)) {
3974 spaceRequiredBeforeParens(Right);
3979 (Left.Tok.getIdentifierInfo() || Left.is(tok::r_paren))) {
3980 return spaceRequiredBeforeParens(Right);
3984 if (Left.is(tok::at) && Right.Tok.getObjCKeywordID() != tok::objc_not_keyword)
3986 if (Right.is(TT_UnaryOperator)) {
3987 return !Left.isOneOf(tok::l_paren, tok::l_square, tok::at) &&
3988 (Left.isNot(tok::colon) || Left.isNot(TT_ObjCMethodExpr));
3990 if ((Left.isOneOf(tok::identifier, tok::greater, tok::r_square,
3992 Left.isSimpleTypeSpecifier()) &&
3993 Right.is(tok::l_brace) && Right.getNextNonComment() &&
3997 if (Left.is(tok::period) || Right.is(tok::period))
4001 if (Right.is(tok::hash) && Left.is(tok::identifier) &&
4002 (Left.TokenText ==
"L" || Left.TokenText ==
"u" ||
4003 Left.TokenText ==
"U" || Left.TokenText ==
"u8" ||
4004 Left.TokenText ==
"LR" || Left.TokenText ==
"uR" ||
4005 Left.TokenText ==
"UR" || Left.TokenText ==
"u8R")) {
4008 if (Left.is(TT_TemplateCloser) && Left.MatchingParen &&
4009 Left.MatchingParen->Previous &&
4010 (Left.MatchingParen->Previous->is(tok::period) ||
4011 Left.MatchingParen->Previous->is(tok::coloncolon))) {
4017 if (Left.is(TT_TemplateCloser) && Right.is(tok::l_square))
4019 if (Left.is(tok::l_brace) && Left.endsSequence(TT_DictLiteral, tok::at)) {
4023 if (Right.is(tok::r_brace) && Right.MatchingParen &&
4024 Right.MatchingParen->endsSequence(TT_DictLiteral, tok::at)) {
4028 if (Right.getType() == TT_TrailingAnnotation &&
4029 Right.isOneOf(tok::amp, tok::ampamp) &&
4030 Left.isOneOf(tok::kw_const, tok::kw_volatile) &&
4031 (!Right.Next || Right.Next->is(tok::semi))) {
4041bool TokenAnnotator::spaceRequiredBefore(
const AnnotatedLine &Line,
4042 const FormatToken &Right)
const {
4043 const FormatToken &Left = *Right.Previous;
4048 return Right.hasWhitespaceBefore();
4051 if (Keywords.isWordLike(Right) && Keywords.isWordLike(Left))
4056 if (Left.is(tok::star) && Right.is(tok::comment))
4059 if (Style.isCpp()) {
4060 if (Left.is(TT_OverloadedOperator) &&
4061 Right.isOneOf(TT_TemplateOpener, TT_TemplateCloser)) {
4065 if (Right.is(tok::period) && Left.is(tok::numeric_constant))
4069 if (Left.is(Keywords.kw_import) && Right.isOneOf(tok::less, tok::ellipsis))
4072 if (Left.isOneOf(Keywords.kw_module, Keywords.kw_import) &&
4073 Right.is(TT_ModulePartitionColon)) {
4077 if (Left.is(tok::identifier) && Right.is(TT_ModulePartitionColon))
4080 if (Left.is(TT_ModulePartitionColon) &&
4081 Right.isOneOf(tok::identifier, tok::kw_private)) {
4084 if (Left.is(tok::ellipsis) && Right.is(tok::identifier) &&
4085 Line.First->is(Keywords.kw_import)) {
4089 if (Left.is(TT_AttributeParen) && Right.is(tok::coloncolon))
4092 if (Left.is(tok::kw_operator))
4093 return Right.is(tok::coloncolon);
4095 !Left.opensScope() && Style.SpaceBeforeCpp11BracedList) {
4098 if (Left.is(tok::less) && Left.is(TT_OverloadedOperator) &&
4099 Right.is(TT_TemplateOpener)) {
4104 if (Right.is(tok::period) &&
4105 Left.isOneOf(Keywords.kw_optional, Keywords.kw_required,
4106 Keywords.kw_repeated, Keywords.kw_extend)) {
4109 if (Right.is(tok::l_paren) &&
4110 Left.isOneOf(Keywords.kw_returns, Keywords.kw_option)) {
4113 if (Right.isOneOf(tok::l_brace, tok::less) && Left.is(TT_SelectorName))
4116 if (Left.is(tok::slash) || Right.is(tok::slash))
4118 if (Left.MatchingParen &&
4119 Left.MatchingParen->is(TT_ProtoExtensionLSquare) &&
4120 Right.isOneOf(tok::l_brace, tok::less)) {
4121 return !Style.Cpp11BracedListStyle;
4124 if (Left.is(tok::percent))
4128 if (Left.is(tok::numeric_constant) && Right.is(tok::percent))
4129 return Right.hasWhitespaceBefore();
4130 }
else if (Style.isJson()) {
4131 if (Right.is(tok::colon))
4133 }
else if (Style.isCSharp()) {
4139 if (Left.is(tok::kw_this) && Right.is(tok::l_square))
4143 if (Left.is(tok::kw_new) && Right.is(tok::l_paren))
4147 if (Right.is(tok::l_brace))
4151 if (Left.is(tok::l_brace) && Right.isNot(tok::r_brace))
4154 if (Left.isNot(tok::l_brace) && Right.is(tok::r_brace))
4158 if (Left.is(TT_FatArrow) || Right.is(TT_FatArrow))
4162 if (Left.is(TT_AttributeColon) || Right.is(TT_AttributeColon))
4166 if (Left.is(TT_TemplateCloser) && Right.is(TT_StartOfName))
4170 if (Left.is(tok::l_square) || Right.is(tok::r_square))
4171 return Style.SpacesInSquareBrackets;
4174 if (Right.is(TT_CSharpNullable))
4178 if (Right.is(TT_NonNullAssertion))
4182 if (Left.is(tok::comma) && Right.is(tok::comma))
4186 if (Left.is(Keywords.kw_var) && Right.is(tok::l_paren))
4190 if (Right.is(tok::l_paren)) {
4191 if (Left.isOneOf(tok::kw_using, Keywords.kw_async, Keywords.kw_when,
4192 Keywords.kw_lock)) {
4193 return Style.SpaceBeforeParensOptions.AfterControlStatements ||
4194 spaceRequiredBeforeParens(Right);
4200 if (Left.isOneOf(tok::kw_public, tok::kw_private, tok::kw_protected,
4201 tok::kw_virtual, tok::kw_extern, tok::kw_static,
4202 Keywords.kw_internal, Keywords.kw_abstract,
4203 Keywords.kw_sealed, Keywords.kw_override,
4204 Keywords.kw_async, Keywords.kw_unsafe) &&
4205 Right.is(tok::l_paren)) {
4208 }
else if (Style.isJavaScript()) {
4209 if (Left.is(TT_FatArrow))
4212 if (Right.is(tok::l_paren) && Left.is(Keywords.kw_await) && Left.Previous &&
4213 Left.Previous->is(tok::kw_for)) {
4216 if (Left.is(Keywords.kw_async) && Right.is(tok::l_paren) &&
4217 Right.MatchingParen) {
4218 const FormatToken *Next = Right.MatchingParen->getNextNonComment();
4221 if (Next && Next->is(TT_FatArrow))
4224 if ((Left.is(TT_TemplateString) && Left.TokenText.endswith(
"${")) ||
4225 (Right.is(TT_TemplateString) && Right.TokenText.startswith(
"}"))) {
4230 if (Keywords.IsJavaScriptIdentifier(Left,
4232 Right.is(TT_TemplateString)) {
4235 if (Right.is(tok::star) &&
4236 Left.isOneOf(Keywords.kw_function, Keywords.kw_yield)) {
4239 if (Right.isOneOf(tok::l_brace, tok::l_square) &&
4240 Left.isOneOf(Keywords.kw_function, Keywords.kw_yield,
4241 Keywords.kw_extends, Keywords.kw_implements)) {
4244 if (Right.is(tok::l_paren)) {
4246 if (Line.MustBeDeclaration && Left.Tok.getIdentifierInfo())
4250 if (Left.Previous && Left.Previous->is(tok::period) &&
4251 Left.Tok.getIdentifierInfo()) {
4255 if (Left.isOneOf(tok::kw_throw, Keywords.kw_await, Keywords.kw_typeof,
4261 if (Left.endsSequence(tok::kw_const, Keywords.kw_as))
4263 if ((Left.isOneOf(Keywords.kw_let, Keywords.kw_var, Keywords.kw_in,
4268 (Left.is(Keywords.kw_of) && Left.Previous &&
4269 (Left.Previous->is(tok::identifier) ||
4270 Left.Previous->isOneOf(tok::r_square, tok::r_brace)))) &&
4271 (!Left.Previous || !Left.Previous->is(tok::period))) {
4274 if (Left.isOneOf(tok::kw_for, Keywords.kw_as) && Left.Previous &&
4275 Left.Previous->is(tok::period) && Right.is(tok::l_paren)) {
4278 if (Left.is(Keywords.kw_as) &&
4279 Right.isOneOf(tok::l_square, tok::l_brace, tok::l_paren)) {
4282 if (Left.is(tok::kw_default) && Left.Previous &&
4283 Left.Previous->is(tok::kw_export)) {
4286 if (Left.is(Keywords.kw_is) && Right.is(tok::l_brace))
4288 if (Right.isOneOf(TT_JsTypeColon, TT_JsTypeOptionalQuestion))
4290 if (Left.is(TT_JsTypeOperator) || Right.is(TT_JsTypeOperator))
4292 if ((Left.is(tok::l_brace) || Right.is(tok::r_brace)) &&
4293 Line.First->isOneOf(Keywords.kw_import, tok::kw_export)) {
4296 if (Left.is(tok::ellipsis))
4298 if (Left.is(TT_TemplateCloser) &&
4299 !Right.isOneOf(tok::equal, tok::l_brace, tok::comma, tok::l_square,
4300 Keywords.kw_implements, Keywords.kw_extends)) {
4306 if (Right.is(TT_NonNullAssertion))
4308 if (Left.is(TT_NonNullAssertion) &&
4309 Right.isOneOf(Keywords.kw_as, Keywords.kw_in)) {
4313 if (Left.is(tok::r_square) && Right.is(tok::l_brace))
4315 if (Left.is(Keywords.kw_synchronized) && Right.is(tok::l_paren)) {
4316 return Style.SpaceBeforeParensOptions.AfterControlStatements ||
4317 spaceRequiredBeforeParens(Right);
4319 if ((Left.isOneOf(tok::kw_static, tok::kw_public, tok::kw_private,
4320 tok::kw_protected) ||
4321 Left.isOneOf(Keywords.kw_final, Keywords.kw_abstract,
4322 Keywords.kw_native)) &&
4323 Right.is(TT_TemplateOpener)) {
4326 }
else if (Style.isVerilog()) {
4329 if ((Left.is(TT_VerilogTableItem) &&
4330 !Right.isOneOf(tok::r_paren, tok::semi)) ||
4331 (Right.is(TT_VerilogTableItem) && Left.isNot(tok::l_paren))) {
4332 const FormatToken *Next = Right.getNextNonComment();
4333 return !(Next && Next->is(tok::r_paren));
4336 if (Left.isNot(TT_BinaryOperator) &&
4337 Left.isOneOf(Keywords.kw_verilogHash, Keywords.kw_verilogHashHash)) {
4341 if (!Right.is(tok::semi) &&
4342 (Left.endsSequence(tok::numeric_constant, Keywords.kw_verilogHash) ||
4343 Left.endsSequence(tok::numeric_constant,
4344 Keywords.kw_verilogHashHash) ||
4345 (Left.is(tok::r_paren) && Left.MatchingParen &&
4346 Left.MatchingParen->endsSequence(tok::l_paren, tok::at)))) {
4351 if (Left.is(Keywords.kw_apostrophe) ||
4352 (Left.is(TT_VerilogNumberBase) && Right.is(tok::numeric_constant))) {
4358 if (Left.is(tok::at) && Right.isOneOf(tok::l_paren, tok::star, tok::at))
4361 if (Right.is(tok::l_square) &&
4362 Left.isOneOf(TT_VerilogDimensionedTypeName, Keywords.kw_function)) {
4367 if ((Right.is(Keywords.kw_apostrophe) ||
4369 !(Left.isOneOf(Keywords.kw_assign, Keywords.kw_unique) ||
4370 Keywords.isVerilogWordOperator(Left)) &&
4371 (Left.isOneOf(tok::r_square, tok::r_paren, tok::r_brace,
4372 tok::numeric_constant) ||
4373 Keywords.isWordLike(Left))) {
4377 if (Left.endsSequence(tok::star, tok::l_paren) && Right.is(tok::identifier))
4380 if (Right.is(tok::l_paren) && Right.is(TT_VerilogStrength))
4383 if (Left.is(TT_ImplicitStringLiteral))
4384 return Right.hasWhitespaceBefore();
4386 if (Left.is(TT_ObjCMethodSpecifier))
4388 if (Left.is(tok::r_paren) && canBeObjCSelectorComponent(Right)) {
4396 (Right.is(tok::equal) || Left.is(tok::equal))) {
4400 if (Right.isOneOf(TT_TrailingReturnArrow, TT_LambdaArrow) ||
4401 Left.isOneOf(TT_TrailingReturnArrow, TT_LambdaArrow)) {
4404 if (Left.is(tok::comma) && !Right.is(TT_OverloadedOperatorLParen) &&
4407 (Left.Children.empty() || !Left.MacroParent)) {
4410 if (Right.is(tok::comma))
4412 if (Right.is(TT_ObjCBlockLParen))
4414 if (Right.is(TT_CtorInitializerColon))
4415 return Style.SpaceBeforeCtorInitializerColon;
4416 if (Right.is(TT_InheritanceColon) && !Style.SpaceBeforeInheritanceColon)
4418 if (Right.is(TT_RangeBasedForLoopColon) &&
4419 !Style.SpaceBeforeRangeBasedForLoopColon) {
4422 if (Left.is(TT_BitFieldColon)) {
4426 if (Right.is(tok::colon)) {
4427 if (Right.is(TT_GotoLabelColon) ||
4428 (!Style.isVerilog() &&
4429 Line.First->isOneOf(tok::kw_default, tok::kw_case))) {
4430 return Style.SpaceBeforeCaseColon;
4432 const FormatToken *Next = Right.getNextNonComment();
4433 if (!Next || Next->is(tok::semi))
4435 if (Right.is(TT_ObjCMethodExpr))
4437 if (Left.is(tok::question))
4439 if (Right.is(TT_InlineASMColon) && Left.is(tok::coloncolon))
4441 if (Right.is(TT_DictLiteral))
4442 return Style.SpacesInContainerLiterals;
4443 if (Right.is(TT_AttributeColon))
4445 if (Right.is(TT_CSharpNamedArgumentColon))
4447 if (Right.is(TT_GenericSelectionColon))
4449 if (Right.is(TT_BitFieldColon)) {
4456 if ((Left.isOneOf(tok::minus, tok::minusminus) &&
4457 Right.isOneOf(tok::minus, tok::minusminus)) ||
4458 (Left.isOneOf(tok::plus, tok::plusplus) &&
4459 Right.isOneOf(tok::plus, tok::plusplus))) {
4462 if (Left.is(TT_UnaryOperator)) {
4463 if (!Right.is(tok::l_paren)) {
4467 if (Left.is(tok::exclaim) && Left.TokenText ==
"not")
4469 if (Left.is(tok::tilde) && Left.TokenText ==
"compl")
4473 if (Left.is(tok::amp) && Right.is(tok::r_square))
4474 return Style.SpacesInSquareBrackets;
4476 return (Style.SpaceAfterLogicalNot && Left.is(tok::exclaim)) ||
4477 Right.is(TT_BinaryOperator);
4482 if (Left.is(TT_CastRParen)) {
4483 return Style.SpaceAfterCStyleCast ||
4484 Right.isOneOf(TT_BinaryOperator, TT_SelectorName);
4487 auto ShouldAddSpacesInAngles = [
this, &Right]() {
4491 return Right.hasWhitespaceBefore();
4495 if (Left.is(tok::greater) && Right.is(tok::greater)) {
4498 return !Style.Cpp11BracedListStyle;
4500 return Right.is(TT_TemplateCloser) && Left.is(TT_TemplateCloser) &&
4502 ShouldAddSpacesInAngles());
4504 if (Right.isOneOf(tok::arrow, tok::arrowstar, tok::periodstar) ||
4505 Left.isOneOf(tok::arrow, tok::period, tok::arrowstar, tok::periodstar) ||
4506 (Right.is(tok::period) && Right.isNot(TT_DesignatedInitializerPeriod))) {
4509 if (!Style.SpaceBeforeAssignmentOperators && Left.isNot(TT_TemplateCloser) &&
4514 (Left.is(tok::identifier) || Left.is(tok::kw_this))) {
4517 if (Right.is(tok::coloncolon) && Left.is(tok::identifier)) {
4521 return Right.hasWhitespaceBefore();
4523 if (Right.is(tok::coloncolon) &&
4524 !Left.isOneOf(tok::l_brace, tok::comment, tok::l_paren)) {
4526 return (Left.is(TT_TemplateOpener) &&
4528 ShouldAddSpacesInAngles())) ||
4529 !(Left.isOneOf(tok::l_paren, tok::r_paren, tok::l_square,
4530 tok::kw___super, TT_TemplateOpener,
4531 TT_TemplateCloser)) ||
4532 (Left.is(tok::l_paren) && Style.SpacesInParentheses);
4534 if ((Left.is(TT_TemplateOpener)) != (Right.is(TT_TemplateCloser)))
4535 return ShouldAddSpacesInAngles();
4537 if (Right.is(TT_StructuredBindingLSquare)) {
4538 return !Left.isOneOf(tok::amp, tok::ampamp) ||
4542 if (Right.Next && Right.Next->is(TT_StructuredBindingLSquare) &&
4543 Right.isOneOf(tok::amp, tok::ampamp)) {
4546 if ((Right.is(TT_BinaryOperator) && !Left.is(tok::l_paren)) ||
4547 (Left.isOneOf(TT_BinaryOperator, TT_ConditionalExpr) &&
4548 !Right.is(tok::r_paren))) {
4551 if (Right.is(TT_TemplateOpener) && Left.is(tok::r_paren) &&
4552 Left.MatchingParen &&
4553 Left.MatchingParen->is(TT_OverloadedOperatorLParen)) {
4556 if (Right.is(tok::less) && Left.isNot(tok::l_paren) &&
4560 if (Right.is(TT_TrailingUnaryOperator))
4562 if (Left.is(TT_RegexLiteral))
4564 return spaceRequiredBetween(Line, Left, Right);
4570 !Tok.
isOneOf(TT_ObjCBlockLBrace, TT_LambdaLBrace, TT_DictLiteral);
4587 !Tok.
isOneOf(TT_ObjCBlockLBrace, TT_DictLiteral);
4595 if (Next->is(tok::comment))
4596 Next = Next->getNextNonComment();
4600bool TokenAnnotator::mustBreakBefore(
const AnnotatedLine &Line,
4601 const FormatToken &Right)
const {
4602 const FormatToken &Left = *Right.Previous;
4603 if (Right.NewlinesBefore > 1 && Style.MaxEmptyLinesToKeep > 0)
4606 if (Style.isCSharp()) {
4607 if (Left.is(TT_FatArrow) && Right.is(tok::l_brace) &&
4608 Style.BraceWrapping.AfterFunction) {
4611 if (Right.is(TT_CSharpNamedArgumentColon) ||
4612 Left.is(TT_CSharpNamedArgumentColon)) {
4615 if (Right.is(TT_CSharpGenericTypeConstraint))
4617 if (Right.Next && Right.Next->is(TT_FatArrow) &&
4618 (Right.is(tok::numeric_constant) ||
4619 (Right.is(tok::identifier) && Right.TokenText ==
"_"))) {
4624 if (Left.is(TT_AttributeSquare) && Left.is(tok::r_square) &&
4625 (Right.isAccessSpecifier(
false) ||
4626 Right.is(Keywords.kw_internal))) {
4630 if (Left.is(TT_AttributeSquare) && Right.is(TT_AttributeSquare) &&
4631 Left.is(tok::r_square) && Right.is(tok::l_square)) {
4635 }
else if (Style.isJavaScript()) {
4637 if (Right.is(tok::string_literal) && Left.is(tok::plus) && Left.Previous &&
4638 Left.Previous->is(tok::string_literal)) {
4641 if (Left.is(TT_DictLiteral) && Left.is(tok::l_brace) && Line.Level == 0 &&
4642 Left.Previous && Left.Previous->is(tok::equal) &&
4643 Line.First->isOneOf(tok::identifier, Keywords.kw_import, tok::kw_export,
4647 !Line.First->isOneOf(Keywords.kw_var, Keywords.kw_let)) {
4652 if (Left.is(tok::l_brace) && Line.Level == 0 &&
4653 (Line.startsWith(tok::kw_enum) ||
4654 Line.startsWith(tok::kw_const, tok::kw_enum) ||
4655 Line.startsWith(tok::kw_export, tok::kw_enum) ||
4656 Line.startsWith(tok::kw_export, tok::kw_const, tok::kw_enum))) {
4661 if (Right.is(tok::r_brace) && Left.is(tok::l_brace) && Left.Previous &&
4662 Left.Previous->is(TT_FatArrow)) {
4664 switch (Style.AllowShortLambdasOnASingleLine) {
4670 return !Left.Children.empty();
4674 return (Left.NestingLevel == 0 && Line.Level == 0) &&
4675 !Left.Children.empty();
4677 llvm_unreachable(
"Unknown FormatStyle::ShortLambdaStyle enum");
4680 if (Right.is(tok::r_brace) && Left.is(tok::l_brace) &&
4681 !Left.Children.empty()) {
4685 (Left.NestingLevel == 0 && Line.Level == 0 &&
4686 Style.AllowShortFunctionsOnASingleLine &
4690 if (Right.is(tok::plus) && Left.is(tok::string_literal) && Right.Next &&
4691 Right.Next->is(tok::string_literal)) {
4694 }
else if (Style.isVerilog()) {
4696 if (Left.is(TT_VerilogTypeComma))
4700 if (!Keywords.isVerilogBegin(Right) && Keywords.isVerilogEndOfLabel(Left))
4707 if (Left.isStringLiteral() && Right.isStringLiteral())
4712 if (Style.isJson()) {
4716 if (Left.is(TT_DictLiteral) && Left.is(tok::l_brace))
4719 if ((Left.is(TT_ArrayInitializerLSquare) && Left.is(tok::l_square) &&
4720 Right.isNot(tok::r_square)) ||
4721 Left.is(tok::comma)) {
4722 if (Right.is(tok::l_brace))
4726 for (
const auto *Tok = &Right; Tok; Tok = Tok->Next) {
4727 if (Tok->isOneOf(tok::l_brace, tok::l_square))
4729 if (Tok->isOneOf(tok::r_brace, tok::r_square))
4732 return Style.BreakArrays;
4736 if (Line.startsWith(tok::kw_asm) && Right.is(TT_InlineASMColon) &&
4746 const FormatToken *BeforeClosingBrace =
nullptr;
4747 if ((Left.isOneOf(tok::l_brace, TT_ArrayInitializerLSquare) ||
4748 (Style.isJavaScript() && Left.is(tok::l_paren))) &&
4749 Left.isNot(
BK_Block) && Left.MatchingParen) {
4750 BeforeClosingBrace = Left.MatchingParen->Previous;
4751 }
else if (Right.MatchingParen &&
4752 (Right.MatchingParen->isOneOf(tok::l_brace,
4753 TT_ArrayInitializerLSquare) ||
4754 (Style.isJavaScript() &&
4755 Right.MatchingParen->is(tok::l_paren)))) {
4756 BeforeClosingBrace = &Left;
4758 if (BeforeClosingBrace && (BeforeClosingBrace->is(tok::comma) ||
4759 BeforeClosingBrace->isTrailingComment())) {
4764 if (Right.is(tok::comment)) {
4765 return Left.isNot(
BK_BracedInit) && Left.isNot(TT_CtorInitializerColon) &&
4766 (Right.NewlinesBefore > 0 && Right.HasUnescapedNewline);
4768 if (Left.isTrailingComment())
4770 if (Left.IsUnterminatedLiteral)
4772 if (Right.is(tok::lessless) && Right.Next && Left.is(tok::string_literal) &&
4773 Right.Next->is(tok::string_literal)) {
4776 if (Right.is(TT_RequiresClause)) {
4777 switch (Style.RequiresClausePosition) {
4786 if (Left.ClosesTemplateDeclaration && Left.MatchingParen &&
4787 Left.MatchingParen->NestingLevel == 0) {
4791 if (Right.is(tok::kw_concept))
4795 if (Left.ClosesRequiresClause && Right.isNot(tok::semi)) {
4796 switch (Style.RequiresClausePosition) {
4806 (Left.is(TT_CtorInitializerComma) ||
4807 Right.is(TT_CtorInitializerColon))) {
4812 Left.isOneOf(TT_CtorInitializerColon, TT_CtorInitializerComma)) {
4818 Right.isOneOf(TT_CtorInitializerComma, TT_CtorInitializerColon)) {
4824 Right.is(TT_CtorInitializerColon)) {
4829 Left.is(TT_CtorInitializerColon)) {
4835 Right.is(TT_InheritanceComma)) {
4839 Left.is(TT_InheritanceComma)) {
4842 if (Right.is(tok::string_literal) && Right.TokenText.startswith(
"R\"")) {
4846 return Right.IsMultiline && Right.NewlinesBefore > 0;
4848 if ((Left.is(tok::l_brace) || (Left.is(tok::less) && Left.Previous &&
4849 Left.Previous->is(tok::equal))) &&
4855 if (Right.is(TT_InlineASMBrace))
4856 return Right.HasUnescapedNewline;
4862 FirstNonComment->isOneOf(Keywords.kw_internal, tok::kw_public,
4863 tok::kw_private, tok::kw_protected);
4865 if (Style.BraceWrapping.AfterEnum) {
4866 if (Line.startsWith(tok::kw_enum) ||
4867 Line.startsWith(tok::kw_typedef, tok::kw_enum)) {
4872 FirstNonComment->Next->is(tok::kw_enum)) {
4878 if (Style.BraceWrapping.AfterClass &&
4880 FirstNonComment->Next->is(Keywords.kw_interface)) ||
4881 Line.startsWith(Keywords.kw_interface))) {
4885 return (Line.startsWith(tok::kw_class) && Style.BraceWrapping.AfterClass) ||
4886 (Line.startsWith(tok::kw_struct) && Style.BraceWrapping.AfterStruct);
4889 if (Left.is(TT_ObjCBlockLBrace) &&
4895 if (Left.is(TT_AttributeParen) && Right.is(TT_ObjCDecl))
4898 if (Left.is(TT_LambdaLBrace)) {
4906 (!Left.Children.empty() &&
4912 if (Style.BraceWrapping.BeforeLambdaBody && Right.is(TT_LambdaLBrace) &&
4913 Left.isOneOf(tok::star, tok::amp, tok::ampamp, TT_TemplateCloser)) {
4919 Left.is(TT_LeadingJavaAnnotation) &&
4920 Right.isNot(TT_LeadingJavaAnnotation) && Right.isNot(tok::l_paren) &&
4921 (Line.Last->is(tok::l_brace) || Style.BreakAfterJavaFieldAnnotations)) {
4925 if (Right.is(TT_ProtoExtensionLSquare))
4957 Right.is(TT_SelectorName) && !Right.is(tok::r_square) && Right.Next) {
4960 if (Left.is(tok::at))
4966 FormatToken *LBrace = Right.Next;
4967 if (LBrace && LBrace->is(tok::colon)) {
4968 LBrace = LBrace->Next;
4969 if (LBrace && LBrace->is(tok::at)) {
4970 LBrace = LBrace->Next;
4972 LBrace = LBrace->Next;
4984 ((LBrace->is(tok::l_brace) &&
4985 (LBrace->is(TT_DictLiteral) ||
4986 (LBrace->Next && LBrace->Next->is(tok::r_brace)))) ||
4987 LBrace->is(TT_ArrayInitializerLSquare) || LBrace->is(tok::less))) {
4994 if (Left.ParameterCount == 0)
5009 if (Left.isOneOf(tok::r_brace, tok::greater, tok::r_square))
5018 Left.is(tok::l_paren) && Left.BlockParameterCount > 0 &&
5019 !Right.isOneOf(tok::l_paren, TT_LambdaLSquare)) {
5021 if (Left.BlockParameterCount > 1)
5027 auto Comma = Left.Role->lastComma();
5030 auto Next =
Comma->getNextNonComment();
5033 if (!Next->isOneOf(TT_LambdaLSquare, tok::l_brace, tok::caret))
5040bool TokenAnnotator::canBreakBefore(
const AnnotatedLine &Line,
5041 const FormatToken &Right)
const {
5042 const FormatToken &Left = *Right.Previous;
5044 if (Style.isCSharp()) {
5045 if (Left.isOneOf(TT_CSharpNamedArgumentColon, TT_AttributeColon) ||
5046 Right.isOneOf(TT_CSharpNamedArgumentColon, TT_AttributeColon)) {
5050 if (Line.First->is(TT_CSharpGenericTypeConstraint))
5051 return Left.is(TT_CSharpGenericTypeConstraintComma);
5053 if (Right.is(TT_CSharpNullable))
5056 if (Left.isOneOf(Keywords.kw_throws, Keywords.kw_extends,
5057 Keywords.kw_implements)) {
5060 if (Right.isOneOf(Keywords.kw_throws, Keywords.kw_extends,
5061 Keywords.kw_implements)) {
5064 }
else if (Style.isJavaScript()) {
5065 const FormatToken *NonComment = Right.getPreviousNonComment();
5067 NonComment->isOneOf(
5068 tok::kw_return, Keywords.kw_yield, tok::kw_continue, tok::kw_break,
5069 tok::kw_throw, Keywords.kw_interface, Keywords.kw_type,
5070 tok::kw_static, tok::kw_public, tok::kw_private, tok::kw_protected,
5071 Keywords.kw_readonly, Keywords.kw_override, Keywords.kw_abstract,
5072 Keywords.kw_get, Keywords.kw_set, Keywords.kw_async,
5073 Keywords.kw_await)) {
5076 if (Right.NestingLevel == 0 &&
5077 (Left.Tok.getIdentifierInfo() ||
5078 Left.isOneOf(tok::r_square, tok::r_paren)) &&
5079 Right.isOneOf(tok::l_square, tok::l_paren)) {
5082 if (NonComment && NonComment->is(tok::identifier) &&
5083 NonComment->TokenText ==
"asserts") {
5086 if (Left.is(TT_FatArrow) && Right.is(tok::l_brace))
5088 if (Left.is(TT_JsTypeColon))
5091 if (Left.is(tok::exclaim) && Right.is(tok::colon))
5096 if (Right.is(Keywords.kw_is)) {
5097 const FormatToken *Next = Right.getNextNonComment();
5105 if (!Next || !Next->is(tok::colon))
5108 if (Left.is(Keywords.kw_in))
5110 if (Right.is(Keywords.kw_in))
5112 if (Right.is(Keywords.kw_as))
5114 if (Right.isOneOf(Keywords.kw_extends, Keywords.kw_infer)) {
5120 if (Left.is(Keywords.kw_as))
5122 if (Left.is(TT_NonNullAssertion))
5124 if (Left.is(Keywords.kw_declare) &&
5125 Right.isOneOf(Keywords.kw_module, tok::kw_namespace,
5126 Keywords.kw_function, tok::kw_class, tok::kw_enum,
5127 Keywords.kw_interface, Keywords.kw_type, Keywords.kw_var,
5128 Keywords.kw_let, tok::kw_const)) {
5133 if (Left.isOneOf(Keywords.kw_module, tok::kw_namespace) &&
5134 Right.isOneOf(tok::identifier, tok::string_literal)) {
5137 if (Right.is(TT_TemplateString) && Right.closesScope())
5141 if (Left.is(tok::identifier) && Right.is(TT_TemplateString))
5143 if (Left.is(TT_TemplateString) && Left.opensScope())
5147 if (Left.is(tok::at))
5149 if (Left.Tok.getObjCKeywordID() == tok::objc_interface)
5151 if (Left.isOneOf(TT_JavaAnnotation, TT_LeadingJavaAnnotation))
5152 return !Right.is(tok::l_paren);
5153 if (Right.is(TT_PointerOrReference)) {
5154 return Line.IsMultiVariableDeclStmt ||
5155 (getTokenPointerOrReferenceAlignment(Right) ==
5157 (!Right.Next || Right.Next->isNot(TT_FunctionDeclarationName)));
5159 if (Right.isOneOf(TT_StartOfName, TT_FunctionDeclarationName) ||
5160 Right.is(tok::kw_operator)) {
5163 if (Left.is(TT_PointerOrReference))
5165 if (Right.isTrailingComment()) {
5172 (Left.is(TT_CtorInitializerColon) && Right.NewlinesBefore > 0 &&
5175 if (Left.is(tok::question) && Right.is(tok::colon))
5177 if (Right.is(TT_ConditionalExpr) || Right.is(tok::question))
5178 return Style.BreakBeforeTernaryOperators;
5179 if (Left.is(TT_ConditionalExpr) || Left.is(tok::question))
5180 return !Style.BreakBeforeTernaryOperators;
5181 if (Left.is(TT_InheritanceColon))
5183 if (Right.is(TT_InheritanceColon))
5185 if (Right.is(TT_ObjCMethodExpr) && !Right.is(tok::r_square) &&
5186 Left.isNot(TT_SelectorName)) {
5190 if (Right.is(tok::colon) &&
5191 !Right.isOneOf(TT_CtorInitializerColon, TT_InlineASMColon)) {
5194 if (Left.is(tok::colon) && Left.isOneOf(TT_DictLiteral, TT_ObjCMethodExpr)) {
5197 if (!Style.AlwaysBreakBeforeMultilineStrings && Right.isStringLiteral())
5223 if (((Right.is(tok::l_brace) || Right.is(tok::less)) &&
5224 Right.is(TT_DictLiteral)) ||
5225 Right.is(TT_ArrayInitializerLSquare)) {
5231 if (Right.is(tok::r_square) && Right.MatchingParen &&
5232 Right.MatchingParen->is(TT_ProtoExtensionLSquare)) {
5235 if (Right.is(TT_SelectorName) || (Right.is(tok::identifier) && Right.Next &&
5236 Right.Next->is(TT_ObjCMethodExpr))) {
5237 return Left.isNot(tok::period);
5241 if (Right.is(tok::kw_concept))
5243 if (Right.is(TT_RequiresClause))
5245 if (Left.ClosesTemplateDeclaration || Left.is(TT_FunctionAnnotationRParen))
5247 if (Left.ClosesRequiresClause)
5249 if (Right.isOneOf(TT_RangeBasedForLoopColon, TT_OverloadedOperatorLParen,
5250 TT_OverloadedOperator)) {
5253 if (Left.is(TT_RangeBasedForLoopColon))
5255 if (Right.is(TT_RangeBasedForLoopColon))
5257 if (Left.is(TT_TemplateCloser) && Right.is(TT_TemplateOpener))
5259 if ((Left.is(tok::greater) && Right.is(tok::greater)) ||
5260 (Left.is(tok::less) && Right.is(tok::less))) {
5263 if (Right.is(TT_BinaryOperator) &&
5269 if (Left.isOneOf(TT_TemplateCloser, TT_UnaryOperator) ||
5270 Left.is(tok::kw_operator)) {
5273 if (Left.is(tok::equal) && !Right.isOneOf(tok::kw_default, tok::kw_delete) &&
5277 if (Left.is(tok::equal) && Right.is(tok::l_brace) &&
5278 !Style.Cpp11BracedListStyle) {
5281 if (Left.is(tok::l_paren) &&
5282 Left.isOneOf(TT_AttributeParen, TT_TypeDeclarationParen)) {
5285 if (Left.is(tok::l_paren) && Left.Previous &&
5286 (Left.Previous->isOneOf(TT_BinaryOperator, TT_CastRParen))) {
5289 if (Right.is(TT_ImplicitStringLiteral))
5292 if (Right.is(TT_TemplateCloser))
5294 if (Right.is(tok::r_square) && Right.MatchingParen &&
5295 Right.MatchingParen->is(TT_LambdaLSquare)) {
5301 if (Right.is(tok::r_brace))
5302 return Right.MatchingParen && Right.MatchingParen->is(
BK_Block);
5305 if (Right.is(tok::r_paren)) {
5307 !Right.MatchingParen) {
5310 auto Next = Right.Next;
5311 if (Next && Next->is(tok::r_paren))
5313 if (Next && Next->is(tok::l_paren))
5315 const FormatToken *
Previous = Right.MatchingParen->Previous;
5321 if (Left.is(TT_TrailingAnnotation)) {
5322 return !Right.isOneOf(tok::l_brace, tok::semi, tok::equal, tok::l_paren,
5323 tok::less, tok::coloncolon);
5326 if (Right.is(tok::kw___attribute) ||
5327 (Right.is(tok::l_square) && Right.is(TT_AttributeSquare))) {
5328 return !Left.is(TT_AttributeSquare);
5331 if (Left.is(tok::identifier) && Right.is(tok::string_literal))
5334 if (Right.is(tok::identifier) && Right.Next && Right.Next->is(TT_DictLiteral))
5337 if (Left.is(TT_CtorInitializerColon)) {
5339 (!Right.isTrailingComment() || Right.NewlinesBefore > 0);
5341 if (Right.is(TT_CtorInitializerColon))
5343 if (Left.is(TT_CtorInitializerComma) &&
5347 if (Right.is(TT_CtorInitializerComma) &&
5351 if (Left.is(TT_InheritanceComma) &&
5355 if (Right.is(TT_InheritanceComma) &&
5359 if (Left.is(TT_ArrayInitializerLSquare))
5361 if (Right.is(tok::kw_typename) && Left.isNot(tok::kw_const))
5363 if ((Left.isBinaryOperator() || Left.is(TT_BinaryOperator)) &&
5364 !Left.isOneOf(tok::arrowstar, tok::lessless) &&
5370 if ((Left.is(TT_AttributeSquare) && Right.is(tok::l_square)) ||
5371 (Left.is(tok::r_square) && Right.is(TT_AttributeSquare))) {
5375 auto ShortLambdaOption = Style.AllowShortLambdasOnASingleLine;
5376 if (Style.BraceWrapping.BeforeLambdaBody && Right.is(TT_LambdaLBrace)) {
5383 return Left.isOneOf(tok::comma, tok::coloncolon, tok::semi, tok::l_brace,
5384 tok::kw_class, tok::kw_struct, tok::comment) ||
5385 Right.isMemberAccess() ||
5386 Right.isOneOf(TT_TrailingReturnArrow, TT_LambdaArrow, tok::lessless,
5387 tok::colon, tok::l_square, tok::at) ||
5388 (Left.is(tok::r_paren) &&
5389 Right.isOneOf(tok::identifier, tok::kw_const)) ||
5390 (Left.is(tok::l_paren) && !Right.is(tok::r_paren)) ||
5391 (Left.is(TT_TemplateOpener) && !Right.is(TT_TemplateCloser));
5394void TokenAnnotator::printDebugInfo(
const AnnotatedLine &Line)
const {
5395 llvm::errs() <<
"AnnotatedTokens(L=" << Line.Level <<
", P=" << Line.PPLevel
5396 <<
", T=" << Line.Type <<
", C=" << Line.IsContinuation
5398 const FormatToken *Tok = Line.First;
5400 llvm::errs() <<
" M=" << Tok->MustBreakBefore
5401 <<
" C=" << Tok->CanBreakBefore
5403 <<
" S=" << Tok->SpacesRequiredBefore
5404 <<
" F=" << Tok->Finalized <<
" B=" << Tok->BlockParameterCount
5405 <<
" BK=" << Tok->getBlockKind() <<
" P=" << Tok->SplitPenalty
5406 <<
" Name=" << Tok->Tok.getName() <<
" L=" << Tok->TotalLength
5407 <<
" PPK=" << Tok->getPackingKind() <<
" FakeLParens=";
5409 llvm::errs() << LParen <<
"/";
5410 llvm::errs() <<
" FakeRParens=" << Tok->FakeRParens;
5411 llvm::errs() <<
" II=" << Tok->Tok.getIdentifierInfo();
5412 llvm::errs() <<
" Text='" << Tok->TokenText <<
"'\n";
5414 assert(Tok == Line.Last);
5417 llvm::errs() <<
"----\n";
5421TokenAnnotator::getTokenReferenceAlignment(
const FormatToken &Reference)
const {
5422 assert(
Reference.isOneOf(tok::amp, tok::ampamp));
5423 switch (Style.ReferenceAlignment) {
5425 return Style.PointerAlignment;
5434 return Style.PointerAlignment;
5438TokenAnnotator::getTokenPointerOrReferenceAlignment(
5439 const FormatToken &PointerOrReference)
const {
5440 if (PointerOrReference.isOneOf(tok::amp, tok::ampamp)) {
5441 switch (Style.ReferenceAlignment) {
5443 return Style.PointerAlignment;
5452 assert(PointerOrReference.is(tok::star));
5453 return Style.PointerAlignment;
Defines the SourceManager interface.
bool ColonIsObjCMethodExpr
FormatToken * FirstStartOfName
bool InCpp11AttributeSpecifier
unsigned LongestObjCSelectorName
enum clang::format::@1179::AnnotatingParser::Context::@327 ContextType
bool VerilogAssignmentFound
bool InCSharpAttributeSpecifier
tok::TokenKind ContextKind
FormatToken * FirstObjCSelectorName
This file implements a token annotator, i.e.
Defines the clang::TokenKind enum and support functions.
#define TRANSFORM_TYPE_TRAIT_DEF(Enum, _)
Parser - This implements a parser for the C family of languages.
@ Reference
An optional reference should be skipped past.
bool Ret(InterpState &S, CodePtr &PC, APValue &Result)
TokenKind
Provides a simple uniform namespace for tokens from all C languages.
@ Parameter
The parameter type of a method or function.
@ Result
The result type of a method or function.
prec::Level getBinOpPrecedence(tok::TokenKind Kind, bool GreaterThanIsOperator, bool CPlusPlus11)
Return the precedence of the specified binary operator token.
if(T->getSizeExpr()) TRY_TO(TraverseStmt(T -> getSizeExpr()))
AccessSpecifier
A C++ access specifier (public, private, protected), plus the special value "none" which means differ...