16#include "llvm/ADT/SmallPtrSet.h"
18#define DEBUG_TYPE "format-token-annotator"
39static bool startsWithInitStatement(
const AnnotatedLine &
Line) {
40 return Line.startsWith(tok::kw_for) ||
Line.startsWith(tok::kw_if) ||
41 Line.startsWith(tok::kw_switch);
55static bool canBeObjCSelectorComponent(
const FormatToken &Tok) {
56 return Tok.Tok.getIdentifierInfo();
61static bool isLambdaParameterList(
const FormatToken *Left) {
63 if (Left->Previous && Left->Previous->is(tok::greater) &&
64 Left->Previous->MatchingParen &&
65 Left->Previous->MatchingParen->is(TT_TemplateOpener)) {
66 Left = Left->Previous->MatchingParen;
70 return Left->Previous && Left->Previous->is(tok::r_square) &&
71 Left->Previous->MatchingParen &&
72 Left->Previous->MatchingParen->is(TT_LambdaLSquare);
77static bool isKeywordWithCondition(
const FormatToken &Tok) {
78 return Tok.isOneOf(tok::kw_if, tok::kw_for, tok::kw_while, tok::kw_switch,
79 tok::kw_constexpr, tok::kw_catch);
83static bool isCppAttribute(
const FormatToken &Tok) {
84 if (!
IsCpp || !Tok.startsSequence(tok::l_square, tok::l_square))
87 if (Tok.Previous && Tok.Previous->is(tok::at))
89 const FormatToken *AttrTok = Tok.Next->Next;
94 if (AttrTok->startsSequence(tok::kw_using, tok::identifier, tok::colon))
96 if (AttrTok->isNot(tok::identifier))
98 while (AttrTok && !AttrTok->startsSequence(tok::r_square, tok::r_square)) {
102 if (AttrTok->is(tok::colon) ||
103 AttrTok->startsSequence(tok::identifier, tok::identifier) ||
104 AttrTok->startsSequence(tok::r_paren, tok::identifier)) {
107 if (AttrTok->is(tok::ellipsis))
109 AttrTok = AttrTok->Next;
111 return AttrTok && AttrTok->startsSequence(tok::r_square, tok::r_square);
119class AnnotatingParser {
121 AnnotatingParser(
const FormatStyle &Style, AnnotatedLine &Line,
122 const AdditionalKeywords &Keywords,
123 SmallVector<ScopeType> &Scopes)
124 : Style(Style), Line(Line), CurrentToken(Line.
First), AutoFound(
false),
125 Keywords(Keywords), Scopes(Scopes) {
126 assert(
IsCpp == Style.isCpp());
127 Contexts.push_back(Context(tok::unknown, 1,
false));
128 resetTokenMetadata();
132 ScopeType getScopeType(
const FormatToken &Token)
const {
133 switch (Token.getType()) {
134 case TT_FunctionLBrace:
135 case TT_LambdaLBrace:
138 case TT_StructLBrace:
147 if (!CurrentToken || !CurrentToken->Previous)
149 if (NonTemplateLess.count(CurrentToken->Previous) > 0)
152 const FormatToken &
Previous = *CurrentToken->Previous;
154 if (
Previous.Previous->Tok.isLiteral())
156 if (
Previous.Previous->is(tok::r_brace))
158 if (
Previous.Previous->is(tok::r_paren) && Contexts.size() > 1 &&
159 (!
Previous.Previous->MatchingParen ||
160 Previous.Previous->MatchingParen->isNot(
161 TT_OverloadedOperatorLParen))) {
164 if (
Previous.Previous->is(tok::kw_operator) &&
165 CurrentToken->is(tok::l_paren)) {
170 FormatToken *Left = CurrentToken->Previous;
171 Left->ParentBracket = Contexts.back().ContextKind;
172 ScopedContextCreator ContextCreator(*
this, tok::less, 12);
176 bool InExprContext = Contexts.back().IsExpression;
178 Contexts.back().IsExpression =
false;
181 if (Left->Previous && Left->Previous->isNot(tok::kw_template))
182 Contexts.back().ContextType = Context::TemplateArgument;
185 CurrentToken->is(tok::question)) {
189 while (CurrentToken) {
190 if (CurrentToken->is(tok::greater)) {
197 if (CurrentToken->Next && CurrentToken->Next->is(tok::greater) &&
198 Left->ParentBracket != tok::less &&
199 CurrentToken->getStartOfNonWhitespace() ==
200 CurrentToken->Next->getStartOfNonWhitespace().getLocWithOffset(
204 Left->MatchingParen = CurrentToken;
205 CurrentToken->MatchingParen = Left;
213 Left->Previous->isOneOf(TT_SelectorName, TT_DictLiteral))) {
214 CurrentToken->setType(TT_DictLiteral);
216 CurrentToken->setType(TT_TemplateCloser);
217 CurrentToken->Tok.setLength(1);
219 if (CurrentToken->Next && CurrentToken->Next->Tok.isLiteral())
224 if (CurrentToken->is(tok::question) &&
229 if (CurrentToken->isOneOf(tok::r_paren, tok::r_square, tok::r_brace) ||
230 (CurrentToken->isOneOf(tok::colon, tok::question) && InExprContext &&
231 !Style.isCSharp() && !Style.isProto())) {
240 if (CurrentToken->Previous->isOneOf(tok::pipepipe, tok::ampamp) &&
241 CurrentToken->Previous->is(TT_BinaryOperator) &&
242 Contexts[Contexts.size() - 2].IsExpression &&
243 !Line.startsWith(tok::kw_template)) {
246 updateParameterCount(Left, CurrentToken);
248 if (FormatToken *
Previous = CurrentToken->getPreviousNonComment()) {
249 if (CurrentToken->is(tok::colon) ||
250 (CurrentToken->isOneOf(tok::l_brace, tok::less) &&
256 if (Style.isTableGen()) {
257 if (CurrentToken->isOneOf(tok::comma, tok::equal)) {
264 if (!parseTableGenValue())
274 bool parseUntouchableParens() {
275 while (CurrentToken) {
276 CurrentToken->Finalized =
true;
277 switch (CurrentToken->Tok.getKind()) {
280 if (!parseUntouchableParens())
295 bool parseParens(
bool LookForDecls =
false) {
298 assert(CurrentToken->Previous &&
"Unknown previous token");
299 FormatToken &OpeningParen = *CurrentToken->Previous;
300 assert(OpeningParen.is(tok::l_paren));
301 FormatToken *PrevNonComment = OpeningParen.getPreviousNonComment();
302 OpeningParen.ParentBracket = Contexts.back().ContextKind;
303 ScopedContextCreator ContextCreator(*
this, tok::l_paren, 1);
306 Contexts.back().ColonIsForRangeExpr =
307 Contexts.size() == 2 && Contexts[0].ColonIsForRangeExpr;
309 if (OpeningParen.Previous &&
310 OpeningParen.Previous->is(TT_UntouchableMacroFunc)) {
311 OpeningParen.Finalized =
true;
312 return parseUntouchableParens();
315 bool StartsObjCMethodExpr =
false;
316 if (!Style.isVerilog()) {
317 if (FormatToken *MaybeSel = OpeningParen.Previous) {
319 if (MaybeSel->isObjCAtKeyword(tok::objc_selector) &&
320 MaybeSel->Previous && MaybeSel->Previous->is(tok::at)) {
321 StartsObjCMethodExpr =
true;
326 if (OpeningParen.is(TT_OverloadedOperatorLParen)) {
328 FormatToken *Prev = &OpeningParen;
329 while (Prev->isNot(tok::kw_operator)) {
330 Prev = Prev->Previous;
331 assert(Prev &&
"Expect a kw_operator prior to the OperatorLParen!");
337 bool OperatorCalledAsMemberFunction =
338 Prev->Previous && Prev->Previous->isOneOf(tok::period, tok::arrow);
339 Contexts.back().IsExpression = OperatorCalledAsMemberFunction;
340 }
else if (OpeningParen.is(TT_VerilogInstancePortLParen)) {
341 Contexts.back().IsExpression =
true;
342 Contexts.back().ContextType = Context::VerilogInstancePortList;
343 }
else if (Style.isJavaScript() &&
344 (Line.startsWith(Keywords.kw_type, tok::identifier) ||
345 Line.startsWith(tok::kw_export, Keywords.kw_type,
349 Contexts.back().IsExpression =
false;
350 }
else if (OpeningParen.Previous &&
351 (OpeningParen.Previous->isOneOf(
352 tok::kw_static_assert, tok::kw_noexcept, tok::kw_explicit,
353 tok::kw_while, tok::l_paren, tok::comma,
354 TT_BinaryOperator) ||
355 OpeningParen.Previous->isIf())) {
357 Contexts.back().IsExpression =
true;
358 }
else if (Style.isJavaScript() && OpeningParen.Previous &&
359 (OpeningParen.Previous->is(Keywords.kw_function) ||
360 (OpeningParen.Previous->endsSequence(tok::identifier,
361 Keywords.kw_function)))) {
363 Contexts.back().IsExpression =
false;
364 }
else if (Style.isJavaScript() && OpeningParen.Previous &&
365 OpeningParen.Previous->is(TT_JsTypeColon)) {
367 Contexts.back().IsExpression =
false;
368 }
else if (isLambdaParameterList(&OpeningParen)) {
370 Contexts.back().IsExpression =
false;
371 }
else if (OpeningParen.is(TT_RequiresExpressionLParen)) {
372 Contexts.back().IsExpression =
false;
373 }
else if (OpeningParen.Previous &&
374 OpeningParen.Previous->is(tok::kw__Generic)) {
375 Contexts.back().ContextType = Context::C11GenericSelection;
376 Contexts.back().IsExpression =
true;
377 }
else if (Line.InPPDirective &&
378 (!OpeningParen.Previous ||
379 OpeningParen.Previous->isNot(tok::identifier))) {
380 Contexts.back().IsExpression =
true;
381 }
else if (Contexts[Contexts.size() - 2].CaretFound) {
383 Contexts.back().IsExpression =
false;
384 }
else if (OpeningParen.Previous &&
385 OpeningParen.Previous->is(TT_ForEachMacro)) {
387 Contexts.back().ContextType = Context::ForEachMacro;
388 Contexts.back().IsExpression =
false;
389 }
else if (OpeningParen.Previous && OpeningParen.Previous->MatchingParen &&
390 OpeningParen.Previous->MatchingParen->isOneOf(
391 TT_ObjCBlockLParen, TT_FunctionTypeLParen)) {
392 Contexts.back().IsExpression =
false;
393 }
else if (!Line.MustBeDeclaration && !Line.InPPDirective) {
395 OpeningParen.Previous &&
396 OpeningParen.Previous->isOneOf(tok::kw_for, tok::kw_catch);
397 Contexts.back().IsExpression = !IsForOrCatch;
400 if (Style.isTableGen()) {
401 if (FormatToken *Prev = OpeningParen.Previous) {
402 if (Prev->is(TT_TableGenCondOperator)) {
403 Contexts.back().IsTableGenCondOpe =
true;
404 Contexts.back().IsExpression =
true;
405 }
else if (Contexts.size() > 1 &&
406 Contexts[Contexts.size() - 2].IsTableGenBangOpe) {
411 Contexts.back().IsTableGenBangOpe =
true;
412 Contexts.back().IsExpression =
true;
415 if (!parseTableGenDAGArg())
417 return parseTableGenDAGArgAndList(&OpeningParen);
424 if (PrevNonComment && OpeningParen.is(TT_Unknown)) {
425 if (PrevNonComment->isAttribute()) {
426 OpeningParen.setType(TT_AttributeLParen);
427 }
else if (PrevNonComment->isOneOf(TT_TypenameMacro, tok::kw_decltype,
430#include
"clang/Basic/TransformTypeTraits.def"
432 OpeningParen.setType(TT_TypeDeclarationParen);
434 if (PrevNonComment->isOneOf(tok::kw_decltype, tok::kw_typeof))
435 Contexts.back().IsExpression =
true;
439 if (StartsObjCMethodExpr) {
440 Contexts.back().ColonIsObjCMethodExpr =
true;
441 OpeningParen.setType(TT_ObjCMethodExpr);
452 bool MightBeFunctionType = !Contexts[Contexts.size() - 2].IsExpression;
453 bool ProbablyFunctionType =
454 CurrentToken->isPointerOrReference() || CurrentToken->is(tok::caret);
455 bool HasMultipleLines =
false;
456 bool HasMultipleParametersOnALine =
false;
457 bool MightBeObjCForRangeLoop =
458 OpeningParen.Previous && OpeningParen.Previous->is(tok::kw_for);
459 FormatToken *PossibleObjCForInToken =
nullptr;
460 while (CurrentToken) {
465 if (LookForDecls && CurrentToken->Next) {
466 FormatToken *Prev = CurrentToken->getPreviousNonComment();
468 FormatToken *PrevPrev = Prev->getPreviousNonComment();
469 FormatToken *Next = CurrentToken->Next;
470 if (PrevPrev && PrevPrev->is(tok::identifier) &&
471 PrevPrev->isNot(TT_TypeName) && Prev->isPointerOrReference() &&
472 CurrentToken->is(tok::identifier) && Next->isNot(tok::equal)) {
473 Prev->setType(TT_BinaryOperator);
474 LookForDecls =
false;
479 if (CurrentToken->Previous->is(TT_PointerOrReference) &&
480 CurrentToken->Previous->Previous->isOneOf(tok::l_paren,
482 ProbablyFunctionType =
true;
484 if (CurrentToken->is(tok::comma))
485 MightBeFunctionType =
false;
486 if (CurrentToken->Previous->is(TT_BinaryOperator))
487 Contexts.back().IsExpression =
true;
488 if (CurrentToken->is(tok::r_paren)) {
489 if (OpeningParen.isNot(TT_CppCastLParen) && MightBeFunctionType &&
490 ProbablyFunctionType && CurrentToken->Next &&
491 (CurrentToken->Next->is(tok::l_paren) ||
492 (CurrentToken->Next->is(tok::l_square) &&
493 Line.MustBeDeclaration))) {
494 OpeningParen.setType(OpeningParen.Next->is(tok::caret)
496 : TT_FunctionTypeLParen);
498 OpeningParen.MatchingParen = CurrentToken;
499 CurrentToken->MatchingParen = &OpeningParen;
501 if (CurrentToken->Next && CurrentToken->Next->is(tok::l_brace) &&
502 OpeningParen.Previous && OpeningParen.Previous->is(tok::l_paren)) {
506 for (FormatToken *Tok = &OpeningParen; Tok != CurrentToken;
508 if (Tok->is(TT_BinaryOperator) && Tok->isPointerOrReference())
509 Tok->setType(TT_PointerOrReference);
513 if (StartsObjCMethodExpr) {
514 CurrentToken->setType(TT_ObjCMethodExpr);
515 if (Contexts.back().FirstObjCSelectorName) {
516 Contexts.back().FirstObjCSelectorName->LongestObjCSelectorName =
517 Contexts.back().LongestObjCSelectorName;
521 if (OpeningParen.is(TT_AttributeLParen))
522 CurrentToken->setType(TT_AttributeRParen);
523 if (OpeningParen.is(TT_TypeDeclarationParen))
524 CurrentToken->setType(TT_TypeDeclarationParen);
525 if (OpeningParen.Previous &&
526 OpeningParen.Previous->is(TT_JavaAnnotation)) {
527 CurrentToken->setType(TT_JavaAnnotation);
529 if (OpeningParen.Previous &&
530 OpeningParen.Previous->is(TT_LeadingJavaAnnotation)) {
531 CurrentToken->setType(TT_LeadingJavaAnnotation);
533 if (OpeningParen.Previous &&
534 OpeningParen.Previous->is(TT_AttributeSquare)) {
535 CurrentToken->setType(TT_AttributeSquare);
538 if (!HasMultipleLines)
540 else if (HasMultipleParametersOnALine)
548 if (CurrentToken->isOneOf(tok::r_square, tok::r_brace))
551 if (CurrentToken->is(tok::l_brace) && OpeningParen.is(TT_ObjCBlockLParen))
552 OpeningParen.setType(TT_Unknown);
553 if (CurrentToken->is(tok::comma) && CurrentToken->Next &&
554 !CurrentToken->Next->HasUnescapedNewline &&
555 !CurrentToken->Next->isTrailingComment()) {
556 HasMultipleParametersOnALine =
true;
558 bool ProbablyFunctionTypeLParen =
559 (CurrentToken->is(tok::l_paren) && CurrentToken->Next &&
560 CurrentToken->Next->isOneOf(tok::star, tok::amp, tok::caret));
561 if ((CurrentToken->Previous->isOneOf(tok::kw_const, tok::kw_auto) ||
562 CurrentToken->Previous->isTypeName()) &&
563 !(CurrentToken->is(tok::l_brace) ||
564 (CurrentToken->is(tok::l_paren) && !ProbablyFunctionTypeLParen))) {
565 Contexts.back().IsExpression =
false;
567 if (CurrentToken->isOneOf(tok::semi, tok::colon)) {
568 MightBeObjCForRangeLoop =
false;
569 if (PossibleObjCForInToken) {
570 PossibleObjCForInToken->setType(TT_Unknown);
571 PossibleObjCForInToken =
nullptr;
574 if (MightBeObjCForRangeLoop && CurrentToken->is(Keywords.kw_in)) {
575 PossibleObjCForInToken = CurrentToken;
576 PossibleObjCForInToken->setType(TT_ObjCForIn);
580 if (CurrentToken->is(tok::comma))
581 Contexts.back().CanBeExpression =
true;
583 if (Style.isTableGen()) {
584 if (CurrentToken->is(tok::comma)) {
585 if (Contexts.back().IsTableGenCondOpe)
586 CurrentToken->setType(TT_TableGenCondOperatorComma);
588 }
else if (CurrentToken->is(tok::colon)) {
589 if (Contexts.back().IsTableGenCondOpe)
590 CurrentToken->setType(TT_TableGenCondOperatorColon);
594 if (!parseTableGenValue())
599 FormatToken *Tok = CurrentToken;
602 updateParameterCount(&OpeningParen, Tok);
603 if (CurrentToken && CurrentToken->HasUnescapedNewline)
604 HasMultipleLines =
true;
609 bool isCSharpAttributeSpecifier(
const FormatToken &Tok) {
610 if (!Style.isCSharp())
614 if (Tok.Previous && Tok.Previous->is(tok::identifier))
618 if (Tok.Previous && Tok.Previous->is(tok::r_square)) {
619 auto *MatchingParen = Tok.Previous->MatchingParen;
620 if (!MatchingParen || MatchingParen->is(TT_ArraySubscriptLSquare))
624 const FormatToken *AttrTok = Tok.Next;
629 if (AttrTok->is(tok::r_square))
633 while (AttrTok && AttrTok->isNot(tok::r_square))
634 AttrTok = AttrTok->Next;
640 AttrTok = AttrTok->Next;
645 if (AttrTok->isOneOf(tok::kw_public, tok::kw_private, tok::kw_protected,
646 tok::comment, tok::kw_class, tok::kw_static,
647 tok::l_square, Keywords.kw_internal)) {
653 AttrTok->Next->startsSequence(tok::identifier, tok::l_paren)) {
668 FormatToken *Left = CurrentToken->Previous;
669 Left->ParentBracket = Contexts.back().ContextKind;
670 FormatToken *
Parent = Left->getPreviousNonComment();
675 bool CppArrayTemplates =
677 (Contexts.back().CanBeExpression || Contexts.back().IsExpression ||
678 Contexts.back().ContextType == Context::TemplateArgument);
680 const bool IsInnerSquare = Contexts.back().InCpp11AttributeSpecifier;
681 const bool IsCpp11AttributeSpecifier =
682 isCppAttribute(*Left) || IsInnerSquare;
685 bool IsCSharpAttributeSpecifier =
686 isCSharpAttributeSpecifier(*Left) ||
687 Contexts.back().InCSharpAttributeSpecifier;
689 bool InsideInlineASM = Line.startsWith(tok::kw_asm);
690 bool IsCppStructuredBinding = Left->isCppStructuredBinding();
691 bool StartsObjCMethodExpr =
692 !IsCppStructuredBinding && !InsideInlineASM && !CppArrayTemplates &&
693 IsCpp && !IsCpp11AttributeSpecifier && !IsCSharpAttributeSpecifier &&
694 Contexts.back().CanBeExpression && Left->isNot(TT_LambdaLSquare) &&
695 !CurrentToken->isOneOf(tok::l_brace, tok::r_square) &&
697 Parent->isOneOf(tok::colon, tok::l_square, tok::l_paren,
698 tok::kw_return, tok::kw_throw) ||
699 Parent->isUnaryOperator() ||
701 Parent->isOneOf(TT_ObjCForIn, TT_CastRParen) ||
704 bool ColonFound =
false;
706 unsigned BindingIncrease = 1;
707 if (IsCppStructuredBinding) {
708 Left->setType(TT_StructuredBindingLSquare);
709 }
else if (Left->is(TT_Unknown)) {
710 if (StartsObjCMethodExpr) {
711 Left->setType(TT_ObjCMethodExpr);
712 }
else if (InsideInlineASM) {
713 Left->setType(TT_InlineASMSymbolicNameLSquare);
714 }
else if (IsCpp11AttributeSpecifier) {
715 Left->setType(TT_AttributeSquare);
716 if (!IsInnerSquare && Left->Previous)
717 Left->Previous->EndsCppAttributeGroup =
false;
718 }
else if (Style.isJavaScript() &&
Parent &&
719 Contexts.back().ContextKind == tok::l_brace &&
720 Parent->isOneOf(tok::l_brace, tok::comma)) {
721 Left->setType(TT_JsComputedPropertyName);
722 }
else if (
IsCpp && Contexts.back().ContextKind == tok::l_brace &&
724 Left->setType(TT_DesignatedInitializerLSquare);
725 }
else if (IsCSharpAttributeSpecifier) {
726 Left->setType(TT_AttributeSquare);
727 }
else if (CurrentToken->is(tok::r_square) &&
Parent &&
728 Parent->is(TT_TemplateCloser)) {
729 Left->setType(TT_ArraySubscriptLSquare);
730 }
else if (Style.isProto()) {
757 Left->setType(TT_ArrayInitializerLSquare);
758 if (!Left->endsSequence(tok::l_square, tok::numeric_constant,
760 !Left->endsSequence(tok::l_square, tok::numeric_constant,
762 !Left->endsSequence(tok::l_square, tok::colon, TT_SelectorName)) {
763 Left->setType(TT_ProtoExtensionLSquare);
764 BindingIncrease = 10;
766 }
else if (!CppArrayTemplates &&
Parent &&
767 Parent->isOneOf(TT_BinaryOperator, TT_TemplateCloser, tok::at,
768 tok::comma, tok::l_paren, tok::l_square,
769 tok::question, tok::colon, tok::kw_return,
772 Left->setType(TT_ArrayInitializerLSquare);
774 BindingIncrease = 10;
775 Left->setType(TT_ArraySubscriptLSquare);
779 ScopedContextCreator ContextCreator(*
this, tok::l_square, BindingIncrease);
780 Contexts.back().IsExpression =
true;
781 if (Style.isJavaScript() &&
Parent &&
Parent->is(TT_JsTypeColon))
782 Contexts.back().IsExpression =
false;
784 Contexts.back().ColonIsObjCMethodExpr = StartsObjCMethodExpr;
785 Contexts.back().InCpp11AttributeSpecifier = IsCpp11AttributeSpecifier;
786 Contexts.back().InCSharpAttributeSpecifier = IsCSharpAttributeSpecifier;
788 while (CurrentToken) {
789 if (CurrentToken->is(tok::r_square)) {
790 if (IsCpp11AttributeSpecifier) {
791 CurrentToken->setType(TT_AttributeSquare);
793 CurrentToken->EndsCppAttributeGroup =
true;
795 if (IsCSharpAttributeSpecifier) {
796 CurrentToken->setType(TT_AttributeSquare);
797 }
else if (((CurrentToken->Next &&
798 CurrentToken->Next->is(tok::l_paren)) ||
799 (CurrentToken->Previous &&
800 CurrentToken->Previous->Previous == Left)) &&
801 Left->is(TT_ObjCMethodExpr)) {
806 StartsObjCMethodExpr =
false;
807 Left->setType(TT_Unknown);
809 if (StartsObjCMethodExpr && CurrentToken->Previous != Left) {
810 CurrentToken->setType(TT_ObjCMethodExpr);
813 if (!ColonFound && CurrentToken->Previous &&
814 CurrentToken->Previous->is(TT_Unknown) &&
815 canBeObjCSelectorComponent(*CurrentToken->Previous)) {
816 CurrentToken->Previous->setType(TT_SelectorName);
822 Parent->overwriteFixedType(TT_BinaryOperator);
825 if (CurrentToken->getType() == TT_ObjCMethodExpr &&
826 CurrentToken->Next &&
827 CurrentToken->Next->is(TT_TrailingReturnArrow)) {
828 CurrentToken->Next->overwriteFixedType(TT_Unknown);
830 Left->MatchingParen = CurrentToken;
831 CurrentToken->MatchingParen = Left;
836 if (!Contexts.back().FirstObjCSelectorName) {
837 FormatToken *
Previous = CurrentToken->getPreviousNonComment();
839 Previous->ObjCSelectorNameParts = 1;
840 Contexts.back().FirstObjCSelectorName =
Previous;
843 Left->ParameterCount =
844 Contexts.back().FirstObjCSelectorName->ObjCSelectorNameParts;
846 if (Contexts.back().FirstObjCSelectorName) {
847 Contexts.back().FirstObjCSelectorName->LongestObjCSelectorName =
848 Contexts.back().LongestObjCSelectorName;
849 if (Left->BlockParameterCount > 1)
850 Contexts.back().FirstObjCSelectorName->LongestObjCSelectorName = 0;
852 if (Style.isTableGen() && Left->is(TT_TableGenListOpener))
853 CurrentToken->setType(TT_TableGenListCloser);
857 if (CurrentToken->isOneOf(tok::r_paren, tok::r_brace))
859 if (CurrentToken->is(tok::colon)) {
860 if (IsCpp11AttributeSpecifier &&
861 CurrentToken->endsSequence(tok::colon, tok::identifier,
865 CurrentToken->setType(TT_AttributeColon);
866 }
else if (!Style.isVerilog() && !Line.InPragmaDirective &&
867 Left->isOneOf(TT_ArraySubscriptLSquare,
868 TT_DesignatedInitializerLSquare)) {
869 Left->setType(TT_ObjCMethodExpr);
870 StartsObjCMethodExpr =
true;
871 Contexts.back().ColonIsObjCMethodExpr =
true;
874 Parent->setType(TT_CastRParen);
879 if (CurrentToken->is(tok::comma) && Left->is(TT_ObjCMethodExpr) &&
881 Left->setType(TT_ArrayInitializerLSquare);
883 FormatToken *Tok = CurrentToken;
884 if (Style.isTableGen()) {
885 if (CurrentToken->isOneOf(tok::comma, tok::minus, tok::ellipsis)) {
891 if (!parseTableGenValue())
894 updateParameterCount(Left, Tok);
899 updateParameterCount(Left, Tok);
904 void skipToNextNonComment() {
906 while (CurrentToken && CurrentToken->is(tok::comment))
915 bool parseTableGenValue(
bool ParseNameMode =
false) {
918 while (CurrentToken->is(tok::comment))
920 if (!parseTableGenSimpleValue())
925 if (CurrentToken->is(tok::hash)) {
926 if (CurrentToken->Next &&
927 CurrentToken->Next->isOneOf(tok::colon, tok::semi, tok::l_brace)) {
930 CurrentToken->setType(TT_TableGenTrailingPasteOperator);
934 FormatToken *HashTok = CurrentToken;
935 skipToNextNonComment();
936 HashTok->setType(TT_Unknown);
937 if (!parseTableGenValue(ParseNameMode))
942 if (ParseNameMode && CurrentToken->is(tok::l_brace))
945 if (CurrentToken->isOneOf(tok::l_brace, tok::l_square, tok::period)) {
946 CurrentToken->setType(TT_TableGenValueSuffix);
947 FormatToken *Suffix = CurrentToken;
948 skipToNextNonComment();
949 if (Suffix->is(tok::l_square))
950 return parseSquare();
951 if (Suffix->is(tok::l_brace)) {
952 Scopes.push_back(getScopeType(*Suffix));
962 bool tryToParseTableGenTokVar() {
965 if (CurrentToken->is(tok::identifier) &&
966 CurrentToken->TokenText.front() ==
'$') {
967 skipToNextNonComment();
975 bool parseTableGenDAGArg() {
976 if (tryToParseTableGenTokVar())
978 if (parseTableGenValue()) {
979 if (CurrentToken && CurrentToken->is(tok::colon)) {
980 CurrentToken->setType(TT_TableGenDAGArgListColon);
981 skipToNextNonComment();
982 return tryToParseTableGenTokVar();
991 bool parseTableGenDAGArgAndList(FormatToken *Opener) {
992 if (!parseTableGenDAGArg())
995 bool FirstDAGArgListElm =
true;
996 while (CurrentToken) {
997 if (!FirstDAGArgListElm && CurrentToken->is(tok::comma)) {
998 CurrentToken->setType(TT_TableGenDAGArgListComma);
999 skipToNextNonComment();
1001 if (CurrentToken && CurrentToken->is(tok::r_paren)) {
1002 CurrentToken->setType(TT_TableGenDAGArgCloser);
1003 Opener->MatchingParen = CurrentToken;
1004 CurrentToken->MatchingParen = Opener;
1005 skipToNextNonComment();
1008 if (!parseTableGenDAGArg())
1010 FirstDAGArgListElm =
false;
1015 bool parseTableGenSimpleValue() {
1016 assert(Style.isTableGen());
1019 FormatToken *Tok = CurrentToken;
1020 skipToNextNonComment();
1022 if (Tok->isOneOf(tok::numeric_constant, tok::string_literal,
1023 TT_TableGenMultiLineString, tok::kw_true, tok::kw_false,
1024 tok::question, tok::kw_int)) {
1028 if (Tok->is(tok::l_brace)) {
1029 Scopes.push_back(getScopeType(*Tok));
1030 return parseBrace();
1033 if (Tok->is(tok::l_square)) {
1034 Tok->setType(TT_TableGenListOpener);
1037 if (Tok->is(tok::less)) {
1038 CurrentToken->setType(TT_TemplateOpener);
1039 return parseAngle();
1045 if (Tok->is(tok::l_paren)) {
1046 Tok->setType(TT_TableGenDAGArgOpener);
1047 return parseTableGenDAGArgAndList(Tok);
1050 if (Tok->is(TT_TableGenBangOperator)) {
1051 if (CurrentToken && CurrentToken->is(tok::less)) {
1052 CurrentToken->setType(TT_TemplateOpener);
1053 skipToNextNonComment();
1057 if (!CurrentToken || CurrentToken->isNot(tok::l_paren))
1059 skipToNextNonComment();
1061 Contexts.back().IsTableGenBangOpe =
true;
1062 bool Result = parseParens();
1063 Contexts.back().IsTableGenBangOpe =
false;
1067 if (Tok->is(TT_TableGenCondOperator)) {
1069 skipToNextNonComment();
1070 if (!Tok || Tok->isNot(tok::l_paren))
1072 bool Result = parseParens();
1078 if (Tok->is(tok::identifier)) {
1080 if (CurrentToken && CurrentToken->is(tok::less)) {
1081 CurrentToken->setType(TT_TemplateOpener);
1082 skipToNextNonComment();
1083 return parseAngle();
1091 bool couldBeInStructArrayInitializer()
const {
1092 if (Contexts.size() < 2)
1096 const auto End = std::next(Contexts.rbegin(), 2);
1097 auto Last = Contexts.rbegin();
1100 if (
Last->ContextKind == tok::l_brace)
1102 return Depth == 2 &&
Last->ContextKind != tok::l_brace;
1109 assert(CurrentToken->Previous);
1110 FormatToken &OpeningBrace = *CurrentToken->Previous;
1111 assert(OpeningBrace.is(tok::l_brace));
1112 OpeningBrace.ParentBracket = Contexts.back().ContextKind;
1114 if (Contexts.back().CaretFound)
1115 OpeningBrace.overwriteFixedType(TT_ObjCBlockLBrace);
1116 Contexts.back().CaretFound =
false;
1118 ScopedContextCreator ContextCreator(*
this, tok::l_brace, 1);
1119 Contexts.back().ColonIsDictLiteral =
true;
1121 Contexts.back().IsExpression =
true;
1122 if (Style.isJavaScript() && OpeningBrace.Previous &&
1123 OpeningBrace.Previous->is(TT_JsTypeColon)) {
1124 Contexts.back().IsExpression =
false;
1126 if (Style.isVerilog() &&
1127 (!OpeningBrace.getPreviousNonComment() ||
1128 OpeningBrace.getPreviousNonComment()->isNot(Keywords.kw_apostrophe))) {
1129 Contexts.back().VerilogMayBeConcatenation =
true;
1131 if (Style.isTableGen())
1132 Contexts.back().ColonIsDictLiteral =
false;
1134 unsigned CommaCount = 0;
1135 while (CurrentToken) {
1136 if (CurrentToken->is(tok::r_brace)) {
1137 assert(!Scopes.empty());
1138 assert(Scopes.back() == getScopeType(OpeningBrace));
1140 assert(OpeningBrace.Optional == CurrentToken->Optional);
1141 OpeningBrace.MatchingParen = CurrentToken;
1142 CurrentToken->MatchingParen = &OpeningBrace;
1144 if (OpeningBrace.ParentBracket == tok::l_brace &&
1145 couldBeInStructArrayInitializer() && CommaCount > 0) {
1146 Contexts.back().ContextType = Context::StructArrayInitializer;
1152 if (CurrentToken->isOneOf(tok::r_paren, tok::r_square))
1154 updateParameterCount(&OpeningBrace, CurrentToken);
1155 if (CurrentToken->isOneOf(tok::colon, tok::l_brace, tok::less)) {
1156 FormatToken *
Previous = CurrentToken->getPreviousNonComment();
1157 if (
Previous->is(TT_JsTypeOptionalQuestion))
1159 if ((CurrentToken->is(tok::colon) && !Style.isTableGen() &&
1160 (!Contexts.back().ColonIsDictLiteral || !
IsCpp)) ||
1162 OpeningBrace.setType(TT_DictLiteral);
1163 if (
Previous->Tok.getIdentifierInfo() ||
1164 Previous->is(tok::string_literal)) {
1165 Previous->setType(TT_SelectorName);
1168 if (CurrentToken->is(tok::colon) && OpeningBrace.is(TT_Unknown) &&
1169 !Style.isTableGen()) {
1170 OpeningBrace.setType(TT_DictLiteral);
1171 }
else if (Style.isJavaScript()) {
1172 OpeningBrace.overwriteFixedType(TT_DictLiteral);
1175 if (CurrentToken->is(tok::comma)) {
1176 if (Style.isJavaScript())
1177 OpeningBrace.overwriteFixedType(TT_DictLiteral);
1180 if (!consumeToken())
1186 void updateParameterCount(FormatToken *Left, FormatToken *Current) {
1190 if (Current->is(tok::l_brace) && Current->is(
BK_Block))
1191 ++Left->BlockParameterCount;
1192 if (Current->is(tok::comma)) {
1193 ++Left->ParameterCount;
1195 Left->Role.reset(
new CommaSeparatedList(Style));
1196 Left->Role->CommaFound(Current);
1197 }
else if (Left->ParameterCount == 0 && Current->isNot(tok::comment)) {
1198 Left->ParameterCount = 1;
1202 bool parseConditional() {
1203 while (CurrentToken) {
1204 if (CurrentToken->is(tok::colon)) {
1205 CurrentToken->setType(TT_ConditionalExpr);
1209 if (!consumeToken())
1215 bool parseTemplateDeclaration() {
1216 if (CurrentToken && CurrentToken->is(tok::less)) {
1217 CurrentToken->setType(TT_TemplateOpener);
1222 CurrentToken->Previous->ClosesTemplateDeclaration =
true;
1228 bool consumeToken() {
1230 const auto *Prev = CurrentToken->getPreviousNonComment();
1231 if (Prev && Prev->is(tok::r_square) && Prev->is(TT_AttributeSquare) &&
1232 CurrentToken->isOneOf(tok::kw_if, tok::kw_switch, tok::kw_case,
1233 tok::kw_default, tok::kw_for, tok::kw_while) &&
1235 CurrentToken->MustBreakBefore =
true;
1238 FormatToken *Tok = CurrentToken;
1242 if (Tok->is(TT_VerilogTableItem))
1245 if (Tok->is(TT_TableGenMultiLineString))
1247 switch (Tok->Tok.getKind()) {
1250 if (!Tok->Previous && Line.MustBeDeclaration)
1251 Tok->setType(TT_ObjCMethodSpecifier);
1258 if (Tok->isTypeFinalized())
1261 if (Style.isJavaScript()) {
1262 if (Contexts.back().ColonIsForRangeExpr ||
1263 (Contexts.size() == 1 &&
1264 !Line.First->isOneOf(tok::kw_enum, tok::kw_case)) ||
1265 Contexts.back().ContextKind == tok::l_paren ||
1266 Contexts.back().ContextKind == tok::l_square ||
1267 (!Contexts.back().IsExpression &&
1268 Contexts.back().ContextKind == tok::l_brace) ||
1269 (Contexts.size() == 1 &&
1270 Line.MustBeDeclaration)) {
1271 Contexts.back().IsExpression =
false;
1272 Tok->setType(TT_JsTypeColon);
1275 }
else if (Style.isCSharp()) {
1276 if (Contexts.back().InCSharpAttributeSpecifier) {
1277 Tok->setType(TT_AttributeColon);
1280 if (Contexts.back().ContextKind == tok::l_paren) {
1281 Tok->setType(TT_CSharpNamedArgumentColon);
1284 }
else if (Style.isVerilog() && Tok->isNot(TT_BinaryOperator)) {
1287 if (Keywords.isVerilogEnd(*Tok->Previous) ||
1288 Keywords.isVerilogBegin(*Tok->Previous)) {
1289 Tok->setType(TT_VerilogBlockLabelColon);
1290 }
else if (Contexts.back().ContextKind == tok::l_square) {
1291 Tok->setType(TT_BitFieldColon);
1292 }
else if (Contexts.back().ColonIsDictLiteral) {
1293 Tok->setType(TT_DictLiteral);
1294 }
else if (Contexts.size() == 1) {
1298 Tok->setType(TT_CaseLabelColon);
1299 if (Line.Level > 1 || (!Line.InPPDirective && Line.Level > 0))
1304 if (Line.First->isOneOf(Keywords.kw_module, Keywords.kw_import) ||
1305 Line.First->startsSequence(tok::kw_export, Keywords.kw_module) ||
1306 Line.First->startsSequence(tok::kw_export, Keywords.kw_import)) {
1307 Tok->setType(TT_ModulePartitionColon);
1308 }
else if (Contexts.back().ColonIsDictLiteral || Style.isProto()) {
1309 Tok->setType(TT_DictLiteral);
1311 if (FormatToken *
Previous = Tok->getPreviousNonComment())
1312 Previous->setType(TT_SelectorName);
1314 }
else if (Contexts.back().ColonIsObjCMethodExpr ||
1315 Line.startsWith(TT_ObjCMethodSpecifier)) {
1316 Tok->setType(TT_ObjCMethodExpr);
1317 const FormatToken *BeforePrevious = Tok->Previous->Previous;
1320 bool UnknownIdentifierInMethodDeclaration =
1321 Line.startsWith(TT_ObjCMethodSpecifier) &&
1322 Tok->Previous->is(tok::identifier) && Tok->Previous->is(TT_Unknown);
1323 if (!BeforePrevious ||
1325 !(BeforePrevious->is(TT_CastRParen) ||
1326 (BeforePrevious->is(TT_ObjCMethodExpr) &&
1327 BeforePrevious->is(tok::colon))) ||
1328 BeforePrevious->is(tok::r_square) ||
1329 Contexts.back().LongestObjCSelectorName == 0 ||
1330 UnknownIdentifierInMethodDeclaration) {
1331 Tok->Previous->setType(TT_SelectorName);
1332 if (!Contexts.back().FirstObjCSelectorName) {
1333 Contexts.back().FirstObjCSelectorName = Tok->Previous;
1334 }
else if (Tok->Previous->ColumnWidth >
1335 Contexts.back().LongestObjCSelectorName) {
1336 Contexts.back().LongestObjCSelectorName =
1337 Tok->Previous->ColumnWidth;
1339 Tok->Previous->ParameterIndex =
1340 Contexts.back().FirstObjCSelectorName->ObjCSelectorNameParts;
1341 ++Contexts.back().FirstObjCSelectorName->ObjCSelectorNameParts;
1343 }
else if (Contexts.back().ColonIsForRangeExpr) {
1344 Tok->setType(TT_RangeBasedForLoopColon);
1345 }
else if (Contexts.back().ContextType == Context::C11GenericSelection) {
1346 Tok->setType(TT_GenericSelectionColon);
1347 }
else if (CurrentToken && CurrentToken->is(tok::numeric_constant)) {
1348 Tok->setType(TT_BitFieldColon);
1349 }
else if (Contexts.size() == 1 &&
1350 !Line.First->isOneOf(tok::kw_enum, tok::kw_case,
1352 FormatToken *Prev = Tok->getPreviousNonComment();
1355 if (Prev->isOneOf(tok::r_paren, tok::kw_noexcept) ||
1356 Prev->ClosesRequiresClause) {
1357 Tok->setType(TT_CtorInitializerColon);
1358 }
else if (Prev->is(tok::kw_try)) {
1360 FormatToken *PrevPrev = Prev->getPreviousNonComment();
1363 if (PrevPrev && PrevPrev->isOneOf(tok::r_paren, tok::kw_noexcept))
1364 Tok->setType(TT_CtorInitializerColon);
1366 Tok->setType(TT_InheritanceColon);
1368 }
else if (canBeObjCSelectorComponent(*Tok->Previous) && Tok->Next &&
1369 (Tok->Next->isOneOf(tok::r_paren, tok::comma) ||
1370 (canBeObjCSelectorComponent(*Tok->Next) && Tok->Next->Next &&
1371 Tok->Next->Next->is(tok::colon)))) {
1374 Tok->setType(TT_ObjCMethodExpr);
1375 }
else if (Contexts.back().ContextKind == tok::l_paren &&
1376 !Line.InPragmaDirective) {
1377 if (Style.isTableGen() && Contexts.back().IsTableGenDAGArg) {
1378 Tok->setType(TT_TableGenDAGArgListColon);
1381 Tok->setType(TT_InlineASMColon);
1388 if (Style.isJavaScript() && !Contexts.back().IsExpression)
1389 Tok->setType(TT_JsTypeOperator);
1392 if (Style.isTableGen()) {
1394 if (!parseTableGenValue())
1396 if (CurrentToken && CurrentToken->is(Keywords.kw_then))
1401 CurrentToken->isOneOf(tok::kw_constexpr, tok::identifier)) {
1406 if (CurrentToken && CurrentToken->is(tok::l_paren)) {
1408 if (!parseParens(
true))
1413 if (Style.isJavaScript()) {
1415 if ((Tok->Previous && Tok->Previous->is(tok::period)) ||
1416 (Tok->Next && Tok->Next->is(tok::colon))) {
1420 if (CurrentToken && CurrentToken->is(Keywords.kw_await))
1423 if (
IsCpp && CurrentToken && CurrentToken->is(tok::kw_co_await))
1425 Contexts.back().ColonIsForRangeExpr =
true;
1426 if (!CurrentToken || CurrentToken->isNot(tok::l_paren))
1437 if (Tok->Previous && Tok->Previous->is(tok::r_paren) &&
1438 Tok->Previous->MatchingParen &&
1439 Tok->Previous->MatchingParen->is(TT_OverloadedOperatorLParen)) {
1440 Tok->Previous->setType(TT_OverloadedOperator);
1441 Tok->Previous->MatchingParen->setType(TT_OverloadedOperator);
1442 Tok->setType(TT_OverloadedOperatorLParen);
1445 if (Style.isVerilog()) {
1451 auto IsInstancePort = [&]() {
1452 const FormatToken *Prev = Tok->getPreviousNonComment();
1453 const FormatToken *PrevPrev;
1461 if (!Prev || !(PrevPrev = Prev->getPreviousNonComment()))
1464 if (Keywords.isVerilogIdentifier(*Prev) &&
1465 Keywords.isVerilogIdentifier(*PrevPrev)) {
1469 if (Prev->is(Keywords.kw_verilogHash) &&
1470 Keywords.isVerilogIdentifier(*PrevPrev)) {
1474 if (Keywords.isVerilogIdentifier(*Prev) && PrevPrev->is(tok::r_paren))
1477 if (Keywords.isVerilogIdentifier(*Prev) && PrevPrev->is(tok::comma)) {
1478 const FormatToken *PrevParen = PrevPrev->getPreviousNonComment();
1479 if (PrevParen->is(tok::r_paren) && PrevParen->MatchingParen &&
1480 PrevParen->MatchingParen->is(TT_VerilogInstancePortLParen)) {
1487 if (IsInstancePort())
1488 Tok->setFinalizedType(TT_VerilogInstancePortLParen);
1493 if (Line.MustBeDeclaration && Contexts.size() == 1 &&
1494 !Contexts.back().IsExpression && !Line.startsWith(TT_ObjCProperty) &&
1495 !Tok->isOneOf(TT_TypeDeclarationParen, TT_RequiresExpressionLParen)) {
1496 if (
const auto *
Previous = Tok->Previous;
1499 !
Previous->isOneOf(TT_RequiresClause, TT_LeadingJavaAnnotation))) {
1500 Line.MightBeFunctionDecl =
true;
1505 if (Style.isTableGen())
1506 Tok->setType(TT_TableGenListOpener);
1512 FormatToken *
Previous = Tok->getPreviousNonComment();
1514 Previous->setType(TT_SelectorName);
1516 Scopes.push_back(getScopeType(*Tok));
1522 Tok->setType(TT_TemplateOpener);
1530 Tok->Previous->isOneOf(TT_SelectorName, TT_DictLiteral))) {
1531 Tok->setType(TT_DictLiteral);
1532 FormatToken *
Previous = Tok->getPreviousNonComment();
1534 Previous->setType(TT_SelectorName);
1536 if (Style.isTableGen())
1537 Tok->setType(TT_TemplateOpener);
1539 Tok->setType(TT_BinaryOperator);
1540 NonTemplateLess.insert(Tok);
1550 if (!Scopes.empty())
1558 Tok->setType(TT_BinaryOperator);
1559 if (Tok->Previous && Tok->Previous->is(TT_TemplateCloser))
1560 Tok->SpacesRequiredBefore = 1;
1562 case tok::kw_operator:
1563 if (Style.isProto())
1565 while (CurrentToken &&
1566 !CurrentToken->isOneOf(tok::l_paren, tok::semi, tok::r_paren)) {
1567 if (CurrentToken->isOneOf(tok::star, tok::amp))
1568 CurrentToken->setType(TT_PointerOrReference);
1569 auto Next = CurrentToken->getNextNonComment();
1572 if (Next->is(tok::less))
1578 auto Previous = CurrentToken->getPreviousNonComment();
1580 if (CurrentToken->is(tok::comma) &&
Previous->isNot(tok::kw_operator))
1582 if (
Previous->isOneOf(TT_BinaryOperator, TT_UnaryOperator, tok::comma,
1583 tok::star, tok::arrow, tok::amp, tok::ampamp) ||
1585 Previous->TokenText.starts_with(
"\"\"")) {
1586 Previous->setType(TT_OverloadedOperator);
1587 if (CurrentToken->isOneOf(tok::less, tok::greater))
1591 if (CurrentToken && CurrentToken->is(tok::l_paren))
1592 CurrentToken->setType(TT_OverloadedOperatorLParen);
1593 if (CurrentToken && CurrentToken->Previous->is(TT_BinaryOperator))
1594 CurrentToken->Previous->setType(TT_OverloadedOperator);
1597 if (Style.isJavaScript() && Tok->Next &&
1598 Tok->Next->isOneOf(tok::semi, tok::comma, tok::colon, tok::r_paren,
1599 tok::r_brace, tok::r_square)) {
1604 Tok->setType(TT_JsTypeOptionalQuestion);
1609 if (Line.MustBeDeclaration && !Contexts.back().IsExpression &&
1610 Style.isJavaScript()) {
1613 if (Style.isCSharp()) {
1619 (Tok->Next->startsSequence(tok::question, tok::r_paren) ||
1620 Tok->Next->startsSequence(tok::question, tok::greater) ||
1621 Tok->Next->startsSequence(tok::question, tok::identifier,
1623 Tok->setType(TT_CSharpNullable);
1628 if (Tok->Next && Tok->Next->is(tok::identifier) && Tok->Next->Next &&
1629 Tok->Next->Next->is(tok::equal)) {
1630 Tok->setType(TT_CSharpNullable);
1639 if (!Contexts.back().IsExpression && Line.MustBeDeclaration &&
1641 !Tok->Next->isOneOf(tok::identifier, tok::string_literal) ||
1643 !Tok->Next->Next->isOneOf(tok::colon, tok::question))) {
1644 Tok->setType(TT_CSharpNullable);
1650 case tok::kw_template:
1651 parseTemplateDeclaration();
1654 switch (Contexts.back().ContextType) {
1655 case Context::CtorInitializer:
1656 Tok->setType(TT_CtorInitializerComma);
1658 case Context::InheritanceList:
1659 Tok->setType(TT_InheritanceComma);
1661 case Context::VerilogInstancePortList:
1662 Tok->setFinalizedType(TT_VerilogInstancePortComma);
1665 if (Style.isVerilog() && Contexts.size() == 1 &&
1666 Line.startsWith(Keywords.kw_assign)) {
1667 Tok->setFinalizedType(TT_VerilogAssignComma);
1668 }
else if (Contexts.back().FirstStartOfName &&
1669 (Contexts.size() == 1 || startsWithInitStatement(Line))) {
1670 Contexts.back().FirstStartOfName->PartOfMultiVariableDeclStmt =
true;
1671 Line.IsMultiVariableDeclStmt =
true;
1675 if (Contexts.back().ContextType == Context::ForEachMacro)
1676 Contexts.back().IsExpression =
true;
1678 case tok::kw_default:
1680 if (Style.isVerilog() && Keywords.isVerilogEndOfLabel(*Tok) &&
1681 (Line.Level > 1 || (!Line.InPPDirective && Line.Level > 0))) {
1685 case tok::identifier:
1686 if (Tok->isOneOf(Keywords.kw___has_include,
1687 Keywords.kw___has_include_next)) {
1690 if (Style.isCSharp() && Tok->is(Keywords.kw_where) && Tok->Next &&
1691 Tok->Next->isNot(tok::l_paren)) {
1692 Tok->setType(TT_CSharpGenericTypeConstraint);
1693 parseCSharpGenericTypeConstraint();
1694 if (!Tok->getPreviousNonComment())
1695 Line.IsContinuation =
true;
1697 if (Style.isTableGen()) {
1698 if (Tok->is(Keywords.kw_assert)) {
1699 if (!parseTableGenValue())
1701 }
else if (Tok->isOneOf(Keywords.kw_def, Keywords.kw_defm) &&
1703 !Tok->Next->isOneOf(tok::colon, tok::l_brace))) {
1705 if (!parseTableGenValue(
true))
1711 if (Tok->Previous && Tok->Previous->is(tok::kw_noexcept))
1712 Tok->setType(TT_TrailingReturnArrow);
1716 if (Style.isTableGen() && !parseTableGenValue())
1725 void parseCSharpGenericTypeConstraint() {
1726 int OpenAngleBracketsCount = 0;
1727 while (CurrentToken) {
1728 if (CurrentToken->is(tok::less)) {
1730 CurrentToken->setType(TT_TemplateOpener);
1731 ++OpenAngleBracketsCount;
1733 }
else if (CurrentToken->is(tok::greater)) {
1734 CurrentToken->setType(TT_TemplateCloser);
1735 --OpenAngleBracketsCount;
1737 }
else if (CurrentToken->is(tok::comma) && OpenAngleBracketsCount == 0) {
1740 CurrentToken->setType(TT_CSharpGenericTypeConstraintComma);
1742 }
else if (CurrentToken->is(Keywords.kw_where)) {
1743 CurrentToken->setType(TT_CSharpGenericTypeConstraint);
1745 }
else if (CurrentToken->is(tok::colon)) {
1746 CurrentToken->setType(TT_CSharpGenericTypeConstraintColon);
1754 void parseIncludeDirective() {
1755 if (CurrentToken && CurrentToken->is(tok::less)) {
1757 while (CurrentToken) {
1760 if (CurrentToken->isNot(tok::comment) &&
1761 !CurrentToken->TokenText.starts_with(
"//")) {
1762 CurrentToken->setType(TT_ImplicitStringLiteral);
1769 void parseWarningOrError() {
1774 while (CurrentToken) {
1775 CurrentToken->setType(TT_ImplicitStringLiteral);
1780 void parsePragma() {
1783 CurrentToken->isOneOf(Keywords.kw_mark, Keywords.kw_option,
1784 Keywords.kw_region)) {
1785 bool IsMarkOrRegion =
1786 CurrentToken->isOneOf(Keywords.kw_mark, Keywords.kw_region);
1789 while (CurrentToken) {
1790 if (IsMarkOrRegion || CurrentToken->Previous->is(TT_BinaryOperator))
1791 CurrentToken->setType(TT_ImplicitStringLiteral);
1797 void parseHasInclude() {
1798 if (!CurrentToken || CurrentToken->isNot(tok::l_paren))
1801 parseIncludeDirective();
1805 LineType parsePreprocessorDirective() {
1806 bool IsFirstToken = CurrentToken->IsFirst;
1812 if (Style.isJavaScript() && IsFirstToken) {
1816 while (CurrentToken) {
1818 CurrentToken->setType(TT_ImplicitStringLiteral);
1824 if (CurrentToken->is(tok::numeric_constant)) {
1825 CurrentToken->SpacesRequiredBefore = 1;
1830 if (!CurrentToken->Tok.getIdentifierInfo())
1834 if (Style.isVerilog() && !Keywords.isVerilogPPDirective(*CurrentToken))
1836 switch (CurrentToken->Tok.getIdentifierInfo()->getPPKeywordID()) {
1837 case tok::pp_include:
1838 case tok::pp_include_next:
1839 case tok::pp_import:
1841 parseIncludeDirective();
1845 case tok::pp_warning:
1846 parseWarningOrError();
1848 case tok::pp_pragma:
1853 Contexts.back().IsExpression =
true;
1856 CurrentToken->SpacesRequiredBefore =
true;
1862 while (CurrentToken) {
1863 FormatToken *Tok = CurrentToken;
1865 if (Tok->is(tok::l_paren)) {
1867 }
else if (Tok->isOneOf(Keywords.kw___has_include,
1868 Keywords.kw___has_include_next)) {
1879 NonTemplateLess.clear();
1880 if (!Line.InMacroBody && CurrentToken->is(tok::hash)) {
1884 auto Type = parsePreprocessorDirective();
1892 IdentifierInfo *Info = CurrentToken->Tok.getIdentifierInfo();
1894 CurrentToken->is(Keywords.kw_package)) ||
1895 (!Style.isVerilog() && Info &&
1896 Info->getPPKeywordID() == tok::pp_import && CurrentToken->Next &&
1897 CurrentToken->Next->isOneOf(tok::string_literal, tok::identifier,
1900 parseIncludeDirective();
1906 if (CurrentToken->is(tok::less) && Line.Last->is(tok::greater)) {
1907 parseIncludeDirective();
1914 CurrentToken->isOneOf(Keywords.kw_option, Keywords.kw_package)) {
1916 if (CurrentToken && CurrentToken->is(tok::identifier)) {
1917 while (CurrentToken)
1923 bool KeywordVirtualFound =
false;
1924 bool ImportStatement =
false;
1927 if (Style.isJavaScript() && CurrentToken->is(Keywords.kw_import))
1928 ImportStatement =
true;
1930 while (CurrentToken) {
1931 if (CurrentToken->is(tok::kw_virtual))
1932 KeywordVirtualFound =
true;
1933 if (Style.isJavaScript()) {
1940 if (Line.First->is(tok::kw_export) &&
1941 CurrentToken->is(Keywords.kw_from) && CurrentToken->Next &&
1942 CurrentToken->Next->isStringLiteral()) {
1943 ImportStatement =
true;
1945 if (isClosureImportStatement(*CurrentToken))
1946 ImportStatement =
true;
1948 if (!consumeToken())
1951 if (KeywordVirtualFound)
1953 if (ImportStatement)
1956 if (Line.startsWith(TT_ObjCMethodSpecifier)) {
1957 if (Contexts.back().FirstObjCSelectorName) {
1958 Contexts.back().FirstObjCSelectorName->LongestObjCSelectorName =
1959 Contexts.back().LongestObjCSelectorName;
1964 for (
const auto &ctx : Contexts)
1965 if (ctx.ContextType == Context::StructArrayInitializer)
1972 bool isClosureImportStatement(
const FormatToken &Tok) {
1975 return Tok.TokenText ==
"goog" && Tok.Next && Tok.Next->is(tok::period) &&
1977 (Tok.Next->Next->TokenText ==
"module" ||
1978 Tok.Next->Next->TokenText ==
"provide" ||
1979 Tok.Next->Next->TokenText ==
"require" ||
1980 Tok.Next->Next->TokenText ==
"requireType" ||
1981 Tok.Next->Next->TokenText ==
"forwardDeclare") &&
1982 Tok.Next->Next->Next && Tok.Next->Next->Next->is(tok::l_paren);
1985 void resetTokenMetadata() {
1991 if (!CurrentToken->isTypeFinalized() &&
1992 !CurrentToken->isOneOf(
1993 TT_LambdaLSquare, TT_LambdaLBrace, TT_AttributeMacro, TT_IfMacro,
1994 TT_ForEachMacro, TT_TypenameMacro, TT_FunctionLBrace,
1995 TT_ImplicitStringLiteral, TT_InlineASMBrace, TT_FatArrow,
1996 TT_NamespaceMacro, TT_OverloadedOperator, TT_RegexLiteral,
1997 TT_TemplateString, TT_ObjCStringLiteral, TT_UntouchableMacroFunc,
1998 TT_StatementAttributeLikeMacro, TT_FunctionLikeOrFreestandingMacro,
1999 TT_ClassLBrace, TT_EnumLBrace, TT_RecordLBrace, TT_StructLBrace,
2000 TT_UnionLBrace, TT_RequiresClause,
2001 TT_RequiresClauseInARequiresExpression, TT_RequiresExpression,
2002 TT_RequiresExpressionLParen, TT_RequiresExpressionLBrace,
2003 TT_BracedListLBrace)) {
2004 CurrentToken->setType(TT_Unknown);
2006 CurrentToken->Role.reset();
2007 CurrentToken->MatchingParen =
nullptr;
2008 CurrentToken->FakeLParens.clear();
2009 CurrentToken->FakeRParens = 0;
2016 CurrentToken->NestingLevel = Contexts.size() - 1;
2017 CurrentToken->BindingStrength = Contexts.back().BindingStrength;
2018 modifyContext(*CurrentToken);
2019 determineTokenType(*CurrentToken);
2020 CurrentToken = CurrentToken->Next;
2022 resetTokenMetadata();
2065 StructArrayInitializer,
2069 C11GenericSelection,
2071 VerilogInstancePortList,
2077 struct ScopedContextCreator {
2078 AnnotatingParser &
P;
2084 P.Contexts.back().BindingStrength + Increase,
2085 P.Contexts.back().IsExpression));
2088 ~ScopedContextCreator() {
2090 if (
P.Contexts.back().ContextType == Context::StructArrayInitializer) {
2091 P.Contexts.pop_back();
2092 P.Contexts.back().ContextType = Context::StructArrayInitializer;
2096 P.Contexts.pop_back();
2100 void modifyContext(
const FormatToken &Current) {
2101 auto AssignmentStartsExpression = [&]() {
2105 if (Line.First->isOneOf(tok::kw_using, tok::kw_return))
2107 if (Line.First->is(tok::kw_template)) {
2108 assert(Current.Previous);
2109 if (Current.Previous->is(tok::kw_operator)) {
2115 const FormatToken *Tok = Line.First->getNextNonComment();
2117 if (Tok->isNot(TT_TemplateOpener)) {
2124 if (Contexts.back().ContextKind == tok::less) {
2125 assert(Current.Previous->Previous);
2126 return !Current.Previous->Previous->isOneOf(tok::kw_typename,
2130 Tok = Tok->MatchingParen;
2133 Tok = Tok->getNextNonComment();
2137 if (Tok->isOneOf(tok::kw_class, tok::kw_enum, tok::kw_struct,
2147 if (Style.isJavaScript() &&
2148 (Line.startsWith(Keywords.kw_type, tok::identifier) ||
2149 Line.startsWith(tok::kw_export, Keywords.kw_type,
2150 tok::identifier))) {
2154 return !Current.Previous || Current.Previous->isNot(tok::kw_operator);
2157 if (AssignmentStartsExpression()) {
2158 Contexts.back().IsExpression =
true;
2159 if (!Line.startsWith(TT_UnaryOperator)) {
2160 for (FormatToken *
Previous = Current.Previous;
2162 !
Previous->Previous->isOneOf(tok::comma, tok::semi);
2164 if (
Previous->isOneOf(tok::r_square, tok::r_paren, tok::greater)) {
2171 if (
Previous->isOneOf(TT_BinaryOperator, TT_UnaryOperator) &&
2173 Previous->Previous->isNot(tok::equal)) {
2174 Previous->setType(TT_PointerOrReference);
2178 }
else if (Current.is(tok::lessless) &&
2179 (!Current.Previous ||
2180 Current.Previous->isNot(tok::kw_operator))) {
2181 Contexts.back().IsExpression =
true;
2182 }
else if (Current.isOneOf(tok::kw_return, tok::kw_throw)) {
2183 Contexts.back().IsExpression =
true;
2184 }
else if (Current.is(TT_TrailingReturnArrow)) {
2185 Contexts.back().IsExpression =
false;
2186 }
else if (Current.is(Keywords.kw_assert)) {
2188 }
else if (Current.Previous &&
2189 Current.Previous->is(TT_CtorInitializerColon)) {
2190 Contexts.back().IsExpression =
true;
2191 Contexts.back().ContextType = Context::CtorInitializer;
2192 }
else if (Current.Previous && Current.Previous->is(TT_InheritanceColon)) {
2193 Contexts.back().ContextType = Context::InheritanceList;
2194 }
else if (Current.isOneOf(tok::r_paren, tok::greater, tok::comma)) {
2195 for (FormatToken *
Previous = Current.Previous;
2198 Previous->setType(TT_PointerOrReference);
2200 if (Line.MustBeDeclaration &&
2201 Contexts.front().ContextType != Context::CtorInitializer) {
2202 Contexts.back().IsExpression =
false;
2204 }
else if (Current.is(tok::kw_new)) {
2205 Contexts.back().CanBeExpression =
false;
2206 }
else if (Current.is(tok::semi) ||
2207 (Current.is(tok::exclaim) && Current.Previous &&
2208 Current.Previous->isNot(tok::kw_operator))) {
2212 Contexts.back().IsExpression =
true;
2216 static FormatToken *untilMatchingParen(FormatToken *Current) {
2220 if (Current->is(tok::l_paren))
2222 if (Current->is(tok::r_paren))
2226 Current = Current->Next;
2231 static bool isDeductionGuide(FormatToken &Current) {
2233 if (Current.Previous && Current.Previous->is(tok::r_paren) &&
2234 Current.startsSequence(tok::arrow, tok::identifier, tok::less)) {
2236 FormatToken *TemplateCloser = Current.Next->Next;
2237 int NestingLevel = 0;
2238 while (TemplateCloser) {
2240 if (TemplateCloser->is(tok::l_paren)) {
2242 TemplateCloser = untilMatchingParen(TemplateCloser);
2243 if (!TemplateCloser)
2246 if (TemplateCloser->is(tok::less))
2248 if (TemplateCloser->is(tok::greater))
2250 if (NestingLevel < 1)
2252 TemplateCloser = TemplateCloser->Next;
2256 if (TemplateCloser && TemplateCloser->Next &&
2257 TemplateCloser->Next->is(tok::semi) &&
2258 Current.Previous->MatchingParen) {
2261 FormatToken *LeadingIdentifier =
2262 Current.Previous->MatchingParen->Previous;
2264 return LeadingIdentifier &&
2265 LeadingIdentifier->TokenText == Current.Next->TokenText;
2271 void determineTokenType(FormatToken &Current) {
2272 if (Current.isNot(TT_Unknown)) {
2277 if ((Style.isJavaScript() || Style.isCSharp()) &&
2278 Current.is(tok::exclaim)) {
2279 if (Current.Previous) {
2281 Style.isJavaScript()
2282 ? Keywords.IsJavaScriptIdentifier(
2283 *Current.Previous,
true)
2284 : Current.Previous->is(tok::identifier);
2286 Current.Previous->isOneOf(
2287 tok::kw_default, tok::kw_namespace, tok::r_paren, tok::r_square,
2288 tok::r_brace, tok::kw_false, tok::kw_true, Keywords.kw_type,
2289 Keywords.kw_get, Keywords.kw_init, Keywords.kw_set) ||
2290 Current.Previous->Tok.isLiteral()) {
2291 Current.setType(TT_NonNullAssertion);
2296 Current.Next->isOneOf(TT_BinaryOperator, Keywords.kw_as)) {
2297 Current.setType(TT_NonNullAssertion);
2305 if (Current.is(Keywords.kw_instanceof)) {
2306 Current.setType(TT_BinaryOperator);
2307 }
else if (isStartOfName(Current) &&
2308 (!Line.MightBeFunctionDecl || Current.NestingLevel != 0)) {
2309 Contexts.back().FirstStartOfName = &Current;
2310 Current.setType(TT_StartOfName);
2311 }
else if (Current.is(tok::semi)) {
2315 Contexts.back().FirstStartOfName =
nullptr;
2316 }
else if (Current.isOneOf(tok::kw_auto, tok::kw___auto_type)) {
2318 }
else if (Current.is(tok::arrow) &&
2320 Current.setType(TT_TrailingReturnArrow);
2321 }
else if (Current.is(tok::arrow) && Style.isVerilog()) {
2323 Current.setType(TT_BinaryOperator);
2324 }
else if (Current.is(tok::arrow) && AutoFound &&
2325 Line.MightBeFunctionDecl && Current.NestingLevel == 0 &&
2326 !Current.Previous->isOneOf(tok::kw_operator, tok::identifier)) {
2328 Current.setType(TT_TrailingReturnArrow);
2329 }
else if (Current.is(tok::arrow) && Current.Previous &&
2330 Current.Previous->is(tok::r_brace)) {
2333 Current.setType(TT_TrailingReturnArrow);
2334 }
else if (isDeductionGuide(Current)) {
2336 Current.setType(TT_TrailingReturnArrow);
2337 }
else if (Current.isPointerOrReference()) {
2338 Current.setType(determineStarAmpUsage(
2340 Contexts.back().CanBeExpression && Contexts.back().IsExpression,
2341 Contexts.back().ContextType == Context::TemplateArgument));
2342 }
else if (Current.isOneOf(tok::minus, tok::plus, tok::caret) ||
2343 (Style.isVerilog() && Current.is(tok::pipe))) {
2344 Current.setType(determinePlusMinusCaretUsage(Current));
2345 if (Current.is(TT_UnaryOperator) && Current.is(tok::caret))
2346 Contexts.back().CaretFound =
true;
2347 }
else if (Current.isOneOf(tok::minusminus, tok::plusplus)) {
2348 Current.setType(determineIncrementUsage(Current));
2349 }
else if (Current.isOneOf(tok::exclaim, tok::tilde)) {
2350 Current.setType(TT_UnaryOperator);
2351 }
else if (Current.is(tok::question)) {
2352 if (Style.isJavaScript() && Line.MustBeDeclaration &&
2353 !Contexts.back().IsExpression) {
2356 Current.setType(TT_JsTypeOptionalQuestion);
2357 }
else if (Style.isTableGen()) {
2359 Current.setType(TT_Unknown);
2361 Current.setType(TT_ConditionalExpr);
2363 }
else if (Current.isBinaryOperator() &&
2364 (!Current.Previous || Current.Previous->isNot(tok::l_square)) &&
2365 (Current.isNot(tok::greater) &&
2367 if (Style.isVerilog()) {
2368 if (Current.is(tok::lessequal) && Contexts.size() == 1 &&
2369 !Contexts.back().VerilogAssignmentFound) {
2373 Current.setFinalizedType(TT_BinaryOperator);
2376 Contexts.back().VerilogAssignmentFound =
true;
2378 Current.setType(TT_BinaryOperator);
2379 }
else if (Current.is(tok::comment)) {
2380 if (Current.TokenText.starts_with(
"/*")) {
2381 if (Current.TokenText.ends_with(
"*/")) {
2382 Current.setType(TT_BlockComment);
2386 Current.Tok.setKind(tok::unknown);
2389 Current.setType(TT_LineComment);
2391 }
else if (Current.is(tok::string_literal)) {
2392 if (Style.isVerilog() && Contexts.back().VerilogMayBeConcatenation &&
2393 Current.getPreviousNonComment() &&
2394 Current.getPreviousNonComment()->isOneOf(tok::comma, tok::l_brace) &&
2395 Current.getNextNonComment() &&
2396 Current.getNextNonComment()->isOneOf(tok::comma, tok::r_brace)) {
2397 Current.setType(TT_StringInConcatenation);
2399 }
else if (Current.is(tok::l_paren)) {
2400 if (lParenStartsCppCast(Current))
2401 Current.setType(TT_CppCastLParen);
2402 }
else if (Current.is(tok::r_paren)) {
2403 if (rParenEndsCast(Current))
2404 Current.setType(TT_CastRParen);
2405 if (Current.MatchingParen && Current.Next &&
2406 !Current.Next->isBinaryOperator() &&
2407 !Current.Next->isOneOf(tok::semi, tok::colon, tok::l_brace,
2408 tok::comma, tok::period, tok::arrow,
2409 tok::coloncolon, tok::kw_noexcept)) {
2410 if (FormatToken *AfterParen = Current.MatchingParen->Next;
2411 AfterParen && AfterParen->isNot(tok::caret)) {
2413 if (FormatToken *BeforeParen = Current.MatchingParen->Previous;
2414 BeforeParen && BeforeParen->is(tok::identifier) &&
2415 BeforeParen->isNot(TT_TypenameMacro) &&
2416 BeforeParen->TokenText == BeforeParen->TokenText.upper() &&
2417 (!BeforeParen->Previous ||
2418 BeforeParen->Previous->ClosesTemplateDeclaration ||
2419 BeforeParen->Previous->ClosesRequiresClause)) {
2420 Current.setType(TT_FunctionAnnotationRParen);
2424 }
else if (Current.is(tok::at) && Current.Next && !Style.isJavaScript() &&
2428 switch (Current.Next->Tok.getObjCKeywordID()) {
2429 case tok::objc_interface:
2430 case tok::objc_implementation:
2431 case tok::objc_protocol:
2432 Current.setType(TT_ObjCDecl);
2434 case tok::objc_property:
2435 Current.setType(TT_ObjCProperty);
2440 }
else if (Current.is(tok::period)) {
2441 FormatToken *PreviousNoComment = Current.getPreviousNonComment();
2442 if (PreviousNoComment &&
2443 PreviousNoComment->isOneOf(tok::comma, tok::l_brace)) {
2444 Current.setType(TT_DesignatedInitializerPeriod);
2446 Current.Previous->isOneOf(TT_JavaAnnotation,
2447 TT_LeadingJavaAnnotation)) {
2448 Current.setType(Current.Previous->getType());
2450 }
else if (canBeObjCSelectorComponent(Current) &&
2453 Current.Previous && Current.Previous->is(TT_CastRParen) &&
2454 Current.Previous->MatchingParen &&
2455 Current.Previous->MatchingParen->Previous &&
2456 Current.Previous->MatchingParen->Previous->is(
2457 TT_ObjCMethodSpecifier)) {
2461 Current.setType(TT_SelectorName);
2462 }
else if (Current.isOneOf(tok::identifier, tok::kw_const, tok::kw_noexcept,
2463 tok::kw_requires) &&
2465 !Current.Previous->isOneOf(tok::equal, tok::at,
2466 TT_CtorInitializerComma,
2467 TT_CtorInitializerColon) &&
2468 Line.MightBeFunctionDecl && Contexts.size() == 1) {
2471 Current.setType(TT_TrailingAnnotation);
2473 Style.isJavaScript()) &&
2475 if (Current.Previous->is(tok::at) &&
2476 Current.isNot(Keywords.kw_interface)) {
2477 const FormatToken &AtToken = *Current.Previous;
2478 const FormatToken *
Previous = AtToken.getPreviousNonComment();
2480 Current.setType(TT_LeadingJavaAnnotation);
2482 Current.setType(TT_JavaAnnotation);
2483 }
else if (Current.Previous->is(tok::period) &&
2484 Current.Previous->isOneOf(TT_JavaAnnotation,
2485 TT_LeadingJavaAnnotation)) {
2486 Current.setType(Current.Previous->getType());
2496 bool isStartOfName(
const FormatToken &Tok) {
2498 if (Style.isVerilog())
2501 if (Tok.isNot(tok::identifier) || !Tok.Previous)
2504 if (
const auto *NextNonComment = Tok.getNextNonComment();
2505 (!NextNonComment && !Line.InMacroBody) ||
2507 (NextNonComment->isPointerOrReference() ||
2508 NextNonComment->is(tok::string_literal) ||
2509 (Line.InPragmaDirective && NextNonComment->is(tok::identifier))))) {
2513 if (Tok.Previous->isOneOf(TT_LeadingJavaAnnotation, Keywords.kw_instanceof,
2517 if (Style.isJavaScript() && Tok.Previous->is(Keywords.kw_in))
2521 FormatToken *PreviousNotConst = Tok.getPreviousNonComment();
2524 if (!Style.isJavaScript())
2525 while (PreviousNotConst && PreviousNotConst->is(tok::kw_const))
2526 PreviousNotConst = PreviousNotConst->getPreviousNonComment();
2528 if (!PreviousNotConst)
2531 if (PreviousNotConst->ClosesRequiresClause)
2534 if (Style.isTableGen()) {
2536 if (Keywords.isTableGenDefinition(*PreviousNotConst))
2539 if (Contexts.back().ContextKind != tok::l_brace)
2543 bool IsPPKeyword = PreviousNotConst->is(tok::identifier) &&
2544 PreviousNotConst->Previous &&
2545 PreviousNotConst->Previous->is(tok::hash);
2547 if (PreviousNotConst->is(TT_TemplateCloser)) {
2548 return PreviousNotConst && PreviousNotConst->MatchingParen &&
2549 PreviousNotConst->MatchingParen->Previous &&
2550 PreviousNotConst->MatchingParen->Previous->isNot(tok::period) &&
2551 PreviousNotConst->MatchingParen->Previous->isNot(tok::kw_template);
2554 if ((PreviousNotConst->is(tok::r_paren) &&
2555 PreviousNotConst->is(TT_TypeDeclarationParen)) ||
2556 PreviousNotConst->is(TT_AttributeRParen)) {
2565 if (PreviousNotConst->isOneOf(tok::identifier, tok::kw_auto))
2569 if (PreviousNotConst->is(TT_PointerOrReference))
2573 if (PreviousNotConst->isTypeName())
2578 PreviousNotConst->is(tok::r_square)) {
2583 return Style.isJavaScript() && PreviousNotConst->is(tok::kw_const);
2587 bool lParenStartsCppCast(
const FormatToken &Tok) {
2592 FormatToken *LeftOfParens = Tok.getPreviousNonComment();
2593 if (LeftOfParens && LeftOfParens->is(TT_TemplateCloser) &&
2594 LeftOfParens->MatchingParen) {
2595 auto *Prev = LeftOfParens->MatchingParen->getPreviousNonComment();
2597 Prev->isOneOf(tok::kw_const_cast, tok::kw_dynamic_cast,
2598 tok::kw_reinterpret_cast, tok::kw_static_cast)) {
2608 bool rParenEndsCast(
const FormatToken &Tok) {
2614 if (Tok.Previous == Tok.MatchingParen || !Tok.Next || !Tok.MatchingParen)
2617 if (Tok.MatchingParen->is(TT_OverloadedOperatorLParen))
2620 FormatToken *LeftOfParens = Tok.MatchingParen->getPreviousNonComment();
2624 if (LeftOfParens->is(tok::r_paren) &&
2625 LeftOfParens->isNot(TT_CastRParen)) {
2626 if (!LeftOfParens->MatchingParen ||
2627 !LeftOfParens->MatchingParen->Previous) {
2630 LeftOfParens = LeftOfParens->MatchingParen->Previous;
2633 if (LeftOfParens->is(tok::r_square)) {
2635 auto MayBeArrayDelete = [](FormatToken *Tok) -> FormatToken * {
2636 if (Tok->isNot(tok::r_square))
2639 Tok = Tok->getPreviousNonComment();
2640 if (!Tok || Tok->isNot(tok::l_square))
2643 Tok = Tok->getPreviousNonComment();
2644 if (!Tok || Tok->isNot(tok::kw_delete))
2648 if (FormatToken *MaybeDelete = MayBeArrayDelete(LeftOfParens))
2649 LeftOfParens = MaybeDelete;
2655 if (LeftOfParens->Tok.getIdentifierInfo() && LeftOfParens->Previous &&
2656 LeftOfParens->Previous->is(tok::kw_operator)) {
2662 if (LeftOfParens->Tok.getIdentifierInfo() &&
2663 !LeftOfParens->isOneOf(Keywords.kw_in, tok::kw_return, tok::kw_case,
2664 tok::kw_delete, tok::kw_throw)) {
2670 if (LeftOfParens->isOneOf(tok::at, tok::r_square, TT_OverloadedOperator,
2671 TT_TemplateCloser, tok::ellipsis)) {
2676 if (Tok.Next->isOneOf(tok::question, tok::ampamp))
2680 if (Tok.Next->is(Keywords.kw_in) && Style.isCSharp())
2685 if (Tok.Next->isOneOf(tok::kw_noexcept, tok::kw_volatile, tok::kw_const,
2686 tok::kw_requires, tok::kw_throw, tok::arrow,
2687 Keywords.kw_override, Keywords.kw_final) ||
2688 isCppAttribute(*Tok.Next)) {
2698 if (Tok.Next->isOneOf(tok::kw_sizeof, tok::kw_alignof) ||
2699 (Tok.Next->Tok.isLiteral() && Tok.Next->isNot(tok::string_literal))) {
2704 auto IsQualifiedPointerOrReference = [](FormatToken *T) {
2706 assert(!T->isTypeName() &&
"Should have already been checked");
2710 if (T->is(TT_AttributeRParen)) {
2712 assert(T->is(tok::r_paren));
2713 assert(T->MatchingParen);
2714 assert(T->MatchingParen->is(tok::l_paren));
2715 assert(T->MatchingParen->is(TT_AttributeLParen));
2716 if (
const auto *Tok = T->MatchingParen->Previous;
2717 Tok && Tok->isAttribute()) {
2721 }
else if (T->is(TT_AttributeSquare)) {
2723 if (T->MatchingParen && T->MatchingParen->Previous) {
2724 T = T->MatchingParen->Previous;
2727 }
else if (T->canBePointerOrReferenceQualifier()) {
2733 return T && T->is(TT_PointerOrReference);
2735 bool ParensAreType =
2737 Tok.Previous->isOneOf(TT_TemplateCloser, TT_TypeDeclarationParen) ||
2738 Tok.Previous->isTypeName() ||
2739 IsQualifiedPointerOrReference(Tok.Previous);
2740 bool ParensCouldEndDecl =
2741 Tok.Next->isOneOf(tok::equal, tok::semi, tok::l_brace, tok::greater);
2742 if (ParensAreType && !ParensCouldEndDecl)
2753 for (
const FormatToken *Token = Tok.MatchingParen->Next; Token != &Tok;
2754 Token = Token->Next) {
2755 if (Token->is(TT_BinaryOperator))
2761 if (Tok.Next->isOneOf(tok::identifier, tok::kw_this))
2765 if (Tok.Next->is(tok::l_paren) && Tok.Previous && Tok.Previous->Previous) {
2766 if (Tok.Previous->is(tok::identifier) &&
2767 Tok.Previous->Previous->is(tok::l_paren)) {
2772 if (!Tok.Next->Next)
2778 const bool NextIsAmpOrStar = Tok.Next->isOneOf(tok::amp, tok::star);
2779 if (!(Tok.Next->isUnaryOperator() || NextIsAmpOrStar) ||
2780 Tok.Next->is(tok::plus) ||
2781 !Tok.Next->Next->isOneOf(tok::identifier, tok::numeric_constant)) {
2784 if (NextIsAmpOrStar &&
2785 (Tok.Next->Next->is(tok::numeric_constant) || Line.InPPDirective)) {
2788 if (Line.InPPDirective && Tok.Next->is(tok::minus))
2791 for (FormatToken *Prev = Tok.Previous; Prev != Tok.MatchingParen;
2792 Prev = Prev->Previous) {
2793 if (!Prev->isOneOf(tok::kw_const, tok::identifier, tok::coloncolon))
2800 bool determineUnaryOperatorByUsage(
const FormatToken &Tok) {
2801 const FormatToken *PrevToken = Tok.getPreviousNonComment();
2811 if (PrevToken->isOneOf(
2812 TT_ConditionalExpr, tok::l_paren, tok::comma, tok::colon, tok::semi,
2813 tok::equal, tok::question, tok::l_square, tok::l_brace,
2814 tok::kw_case, tok::kw_co_await, tok::kw_co_return, tok::kw_co_yield,
2815 tok::kw_delete, tok::kw_return, tok::kw_throw)) {
2822 if (PrevToken->is(tok::kw_sizeof))
2826 if (PrevToken->isOneOf(TT_CastRParen, TT_UnaryOperator))
2830 if (PrevToken->is(TT_BinaryOperator))
2838 bool InTemplateArgument) {
2839 if (Style.isJavaScript())
2840 return TT_BinaryOperator;
2843 if (Style.isCSharp() && Tok.is(tok::ampamp))
2844 return TT_BinaryOperator;
2846 if (Style.isVerilog()) {
2851 if (Tok.is(tok::star))
2852 return TT_BinaryOperator;
2853 return determineUnaryOperatorByUsage(Tok) ? TT_UnaryOperator
2854 : TT_BinaryOperator;
2857 const FormatToken *PrevToken = Tok.getPreviousNonComment();
2859 return TT_UnaryOperator;
2860 if (PrevToken->is(TT_TypeName))
2861 return TT_PointerOrReference;
2863 const FormatToken *NextToken = Tok.getNextNonComment();
2865 if (InTemplateArgument && NextToken && NextToken->is(tok::kw_noexcept))
2866 return TT_BinaryOperator;
2869 NextToken->isOneOf(tok::arrow, tok::equal, tok::comma, tok::r_paren,
2870 TT_RequiresClause) ||
2872 NextToken->canBePointerOrReferenceQualifier() ||
2873 (NextToken->is(tok::l_brace) && !NextToken->getNextNonComment())) {
2874 return TT_PointerOrReference;
2877 if (PrevToken->is(tok::coloncolon))
2878 return TT_PointerOrReference;
2880 if (PrevToken->is(tok::r_paren) && PrevToken->is(TT_TypeDeclarationParen))
2881 return TT_PointerOrReference;
2883 if (determineUnaryOperatorByUsage(Tok))
2884 return TT_UnaryOperator;
2886 if (NextToken->is(tok::l_square) && NextToken->isNot(TT_LambdaLSquare))
2887 return TT_PointerOrReference;
2889 return TT_PointerOrReference;
2890 if (NextToken->isOneOf(tok::comma, tok::semi))
2891 return TT_PointerOrReference;
2903 if (PrevToken->is(tok::r_brace) && Tok.is(tok::star) &&
2904 !PrevToken->MatchingParen) {
2905 return TT_PointerOrReference;
2908 if (PrevToken->endsSequence(tok::r_square, tok::l_square, tok::kw_delete))
2909 return TT_UnaryOperator;
2911 if (PrevToken->Tok.isLiteral() ||
2912 PrevToken->isOneOf(tok::r_paren, tok::r_square, tok::kw_true,
2913 tok::kw_false, tok::r_brace)) {
2914 return TT_BinaryOperator;
2917 const FormatToken *NextNonParen = NextToken;
2918 while (NextNonParen && NextNonParen->is(tok::l_paren))
2919 NextNonParen = NextNonParen->getNextNonComment();
2920 if (NextNonParen && (NextNonParen->Tok.isLiteral() ||
2921 NextNonParen->isOneOf(tok::kw_true, tok::kw_false) ||
2922 NextNonParen->isUnaryOperator())) {
2923 return TT_BinaryOperator;
2929 if (InTemplateArgument && NextToken->Tok.isAnyIdentifier())
2930 return TT_BinaryOperator;
2934 if (Tok.is(tok::ampamp) &&
2935 NextToken->isOneOf(tok::l_paren, tok::star, tok::amp)) {
2936 return TT_BinaryOperator;
2941 if (NextToken->Tok.isAnyIdentifier()) {
2942 const FormatToken *NextNextToken = NextToken->getNextNonComment();
2943 if (NextNextToken && NextNextToken->is(tok::arrow))
2944 return TT_BinaryOperator;
2950 return TT_BinaryOperator;
2953 if (!Scopes.empty() && Scopes.back() ==
ST_Class)
2954 return TT_PointerOrReference;
2957 auto IsChainedOperatorAmpOrMember = [](
const FormatToken *token) {
2958 return !token || token->isOneOf(tok::amp, tok::period, tok::arrow,
2959 tok::arrowstar, tok::periodstar);
2964 if (Tok.is(tok::amp) && PrevToken && PrevToken->Tok.isAnyIdentifier() &&
2965 IsChainedOperatorAmpOrMember(PrevToken->getPreviousNonComment()) &&
2966 NextToken && NextToken->Tok.isAnyIdentifier()) {
2967 if (
auto NextNext = NextToken->getNextNonComment();
2969 (IsChainedOperatorAmpOrMember(NextNext) || NextNext->is(tok::semi))) {
2970 return TT_BinaryOperator;
2974 return TT_PointerOrReference;
2977 TokenType determinePlusMinusCaretUsage(
const FormatToken &Tok) {
2978 if (determineUnaryOperatorByUsage(Tok))
2979 return TT_UnaryOperator;
2981 const FormatToken *PrevToken = Tok.getPreviousNonComment();
2983 return TT_UnaryOperator;
2985 if (PrevToken->is(tok::at))
2986 return TT_UnaryOperator;
2989 return TT_BinaryOperator;
2993 TokenType determineIncrementUsage(
const FormatToken &Tok) {
2994 const FormatToken *PrevToken = Tok.getPreviousNonComment();
2995 if (!PrevToken || PrevToken->is(TT_CastRParen))
2996 return TT_UnaryOperator;
2997 if (PrevToken->isOneOf(tok::r_paren, tok::r_square, tok::identifier))
2998 return TT_TrailingUnaryOperator;
3000 return TT_UnaryOperator;
3003 SmallVector<Context, 8> Contexts;
3005 const FormatStyle &Style;
3006 AnnotatedLine &Line;
3007 FormatToken *CurrentToken;
3009 const AdditionalKeywords &Keywords;
3011 SmallVector<ScopeType> &Scopes;
3025class ExpressionParser {
3027 ExpressionParser(
const FormatStyle &Style,
const AdditionalKeywords &Keywords,
3028 AnnotatedLine &Line)
3029 : Style(Style), Keywords(Keywords), Line(Line), Current(Line.
First) {}
3032 void parse(
int Precedence = 0) {
3035 while (Current && (Current->is(tok::kw_return) ||
3036 (Current->is(tok::colon) &&
3037 Current->isOneOf(TT_ObjCMethodExpr, TT_DictLiteral)))) {
3041 if (!Current || Precedence > PrecedenceArrowAndPeriod)
3046 parseConditionalExpr();
3052 if (Precedence == PrecedenceUnaryOperator) {
3053 parseUnaryOperator();
3057 FormatToken *Start = Current;
3058 FormatToken *LatestOperator =
nullptr;
3059 unsigned OperatorIndex = 0;
3061 FormatToken *VerilogFirstOfType =
nullptr;
3070 if (Style.isVerilog() && Precedence ==
prec::Comma) {
3071 VerilogFirstOfType =
3072 verilogGroupDecl(VerilogFirstOfType, LatestOperator);
3076 parse(Precedence + 1);
3078 int CurrentPrecedence = getCurrentPrecedence();
3080 if (Precedence == CurrentPrecedence && Current &&
3081 Current->is(TT_SelectorName)) {
3083 addFakeParenthesis(Start,
prec::Level(Precedence));
3087 if ((Style.isCSharp() || Style.isJavaScript() ||
3092 FormatToken *Prev = Current->getPreviousNonComment();
3093 if (Prev && Prev->is(tok::string_literal) &&
3094 (Prev == Start || Prev->endsSequence(tok::string_literal, tok::plus,
3095 TT_StringInConcatenation))) {
3096 Prev->setType(TT_StringInConcatenation);
3103 (Current->closesScope() &&
3104 (Current->MatchingParen || Current->is(TT_TemplateString))) ||
3105 (CurrentPrecedence != -1 && CurrentPrecedence < Precedence) ||
3114 if (Current->opensScope() ||
3115 Current->isOneOf(TT_RequiresClause,
3116 TT_RequiresClauseInARequiresExpression)) {
3119 while (Current && (!Current->closesScope() || Current->opensScope())) {
3126 if (CurrentPrecedence == Precedence) {
3128 LatestOperator->NextOperator = Current;
3129 LatestOperator = Current;
3130 Current->OperatorIndex = OperatorIndex;
3133 next(Precedence > 0);
3138 if (Style.isVerilog() && Precedence ==
prec::Comma && VerilogFirstOfType)
3139 addFakeParenthesis(VerilogFirstOfType,
prec::Comma);
3141 if (LatestOperator && (Current || Precedence > 0)) {
3147 Start->Previous->isOneOf(TT_RequiresClause,
3148 TT_RequiresClauseInARequiresExpression))
3150 auto Ret = Current ? Current : Line.Last;
3151 while (!
Ret->ClosesRequiresClause &&
Ret->Previous)
3157 if (Precedence == PrecedenceArrowAndPeriod) {
3161 addFakeParenthesis(Start,
prec::Level(Precedence), End);
3169 int getCurrentPrecedence() {
3171 const FormatToken *NextNonComment = Current->getNextNonComment();
3172 if (Current->is(TT_ConditionalExpr))
3174 if (NextNonComment && Current->is(TT_SelectorName) &&
3175 (NextNonComment->isOneOf(TT_DictLiteral, TT_JsTypeColon) ||
3176 (Style.isProto() && NextNonComment->is(tok::less)))) {
3179 if (Current->is(TT_JsComputedPropertyName))
3181 if (Current->is(TT_TrailingReturnArrow))
3183 if (Current->is(TT_FatArrow))
3185 if (Current->isOneOf(tok::semi, TT_InlineASMColon, TT_SelectorName) ||
3186 (Current->is(tok::comment) && NextNonComment &&
3187 NextNonComment->is(TT_SelectorName))) {
3190 if (Current->is(TT_RangeBasedForLoopColon))
3193 Current->is(Keywords.kw_instanceof)) {
3196 if (Style.isJavaScript() &&
3197 Current->isOneOf(Keywords.kw_in, Keywords.kw_as)) {
3200 if (Current->is(TT_BinaryOperator) || Current->is(tok::comma))
3201 return Current->getPrecedence();
3202 if (Current->isOneOf(tok::period, tok::arrow) &&
3203 Current->isNot(TT_TrailingReturnArrow)) {
3204 return PrecedenceArrowAndPeriod;
3207 Current->isOneOf(Keywords.kw_extends, Keywords.kw_implements,
3208 Keywords.kw_throws)) {
3213 if (Style.isVerilog() && Current->is(tok::colon))
3219 void addFakeParenthesis(FormatToken *Start,
prec::Level Precedence,
3220 FormatToken *End =
nullptr) {
3225 if (Start->MacroParent)
3228 Start->FakeLParens.push_back(Precedence);
3230 Start->StartsBinaryExpression =
true;
3231 if (!End && Current)
3232 End = Current->getPreviousNonComment();
3236 End->EndsBinaryExpression =
true;
3242 void parseUnaryOperator() {
3244 while (Current && Current->is(TT_UnaryOperator)) {
3245 Tokens.push_back(Current);
3248 parse(PrecedenceArrowAndPeriod);
3249 for (FormatToken *Token : llvm::reverse(Tokens)) {
3255 void parseConditionalExpr() {
3256 while (Current && Current->isTrailingComment())
3258 FormatToken *Start = Current;
3260 if (!Current || Current->isNot(tok::question))
3264 if (!Current || Current->isNot(TT_ConditionalExpr))
3271 void next(
bool SkipPastLeadingComments =
true) {
3273 Current = Current->Next;
3275 (Current->NewlinesBefore == 0 || SkipPastLeadingComments) &&
3276 Current->isTrailingComment()) {
3277 Current = Current->Next;
3283 FormatToken *verilogGroupDecl(FormatToken *FirstOfType,
3284 FormatToken *PreviousComma) {
3288 FormatToken *Start = Current;
3291 while (Start->startsSequence(tok::l_paren, tok::star)) {
3292 if (!(Start = Start->MatchingParen) ||
3293 !(Start = Start->getNextNonComment())) {
3298 FormatToken *Tok = Start;
3300 if (Tok->is(Keywords.kw_assign))
3301 Tok = Tok->getNextNonComment();
3309 FormatToken *
First =
nullptr;
3311 FormatToken *Next = Tok->getNextNonComment();
3313 if (Tok->is(tok::hash)) {
3318 Tok = Tok->getNextNonComment();
3319 }
else if (Tok->is(tok::hashhash)) {
3323 Tok = Tok->getNextNonComment();
3324 }
else if (Keywords.isVerilogQualifier(*Tok) ||
3325 Keywords.isVerilogIdentifier(*Tok)) {
3329 while (Tok && Tok->isOneOf(tok::period, tok::coloncolon) &&
3330 (Tok = Tok->getNextNonComment())) {
3331 if (Keywords.isVerilogIdentifier(*Tok))
3332 Tok = Tok->getNextNonComment();
3336 }
else if (Tok->is(tok::l_paren)) {
3341 Keywords.kw_highz0, Keywords.kw_highz1, Keywords.kw_large,
3342 Keywords.kw_medium, Keywords.kw_pull0, Keywords.kw_pull1,
3343 Keywords.kw_small, Keywords.kw_strong0, Keywords.kw_strong1,
3344 Keywords.kw_supply0, Keywords.kw_supply1, Keywords.kw_weak0,
3345 Keywords.kw_weak1)) {
3346 Tok->setType(TT_VerilogStrength);
3347 Tok = Tok->MatchingParen;
3349 Tok->setType(TT_VerilogStrength);
3350 Tok = Tok->getNextNonComment();
3355 }
else if (Tok->is(tok::hash)) {
3356 if (Next->is(tok::l_paren))
3357 Next = Next->MatchingParen;
3359 Tok = Next->getNextNonComment();
3366 FormatToken *Second =
nullptr;
3368 while (Tok && Tok->is(tok::l_square) && (Tok = Tok->MatchingParen))
3369 Tok = Tok->getNextNonComment();
3370 if (Tok && (Tok->is(tok::hash) || Keywords.isVerilogIdentifier(*Tok)))
3375 FormatToken *TypedName =
nullptr;
3379 First->setType(TT_VerilogDimensionedTypeName);
3380 }
else if (
First != Start) {
3388 if (TypedName->is(TT_Unknown))
3389 TypedName->setType(TT_StartOfName);
3391 if (FirstOfType && PreviousComma) {
3392 PreviousComma->setType(TT_VerilogTypeComma);
3393 addFakeParenthesis(FirstOfType,
prec::Comma, PreviousComma->Previous);
3396 FirstOfType = TypedName;
3403 while (Current && Current != FirstOfType) {
3404 if (Current->opensScope()) {
3415 const FormatStyle &Style;
3416 const AdditionalKeywords &Keywords;
3417 const AnnotatedLine &Line;
3418 FormatToken *Current;
3427 assert(
Line->First);
3434 Line->First->OriginalColumn) {
3435 const bool PPDirectiveOrImportStmt =
3438 if (PPDirectiveOrImportStmt)
3444 PPDirectiveOrImportStmt
3446 : NextNonCommentLine->
Level;
3448 NextNonCommentLine =
Line->First->isNot(tok::r_brace) ?
Line :
nullptr;
3457 for (
const auto *Tok =
Line.First; Tok; Tok = Tok->Next)
3465 for (
FormatToken *Tok =
Line.getFirstNonComment(), *Name =
nullptr; Tok;
3466 Tok = Tok->getNextNonComment()) {
3468 if (Tok->is(tok::l_square) && Tok->is(TT_AttributeSquare)) {
3477 return Tok->
is(tok::l_paren) && Tok->isNot(TT_FunctionTypeLParen) &&
3484 if (Tok->isOneOf(tok::kw_friend, tok::kw_inline, tok::kw_virtual,
3485 tok::kw_constexpr, tok::kw_consteval, tok::kw_explicit)) {
3490 if (Tok->is(tok::coloncolon)) {
3497 while (Tok->startsSequence(tok::identifier, tok::coloncolon)) {
3499 Tok = Tok->Next->Next;
3505 if (Tok->is(tok::tilde)) {
3512 if (Tok->isNot(tok::identifier) || Tok->isNot(TT_Unknown))
3523 assert(Tok && Tok->
is(tok::identifier));
3526 if (Prev && Prev->is(tok::tilde))
3529 if (!Prev || !Prev->endsSequence(tok::coloncolon, tok::identifier))
3532 assert(Prev->Previous);
3533 return Prev->Previous->TokenText == Tok->
TokenText;
3537 AnnotatingParser
Parser(Style,
Line, Keywords, Scopes);
3540 for (
auto &Child :
Line.Children)
3553 ExpressionParser ExprParser(Style, Keywords,
Line);
3558 if (Tok && ((!Scopes.empty() && Scopes.back() ==
ST_Class) ||
3560 Tok->setFinalizedType(TT_CtorDtorDeclName);
3564 if (
Line.startsWith(TT_ObjCMethodSpecifier))
3566 else if (
Line.startsWith(TT_ObjCDecl))
3568 else if (
Line.startsWith(TT_ObjCProperty))
3572 First->SpacesRequiredBefore = 1;
3573 First->CanBreakBefore =
First->MustBreakBefore;
3575 if (
First->is(tok::eof) &&
First->NewlinesBefore == 0 &&
3576 Style.InsertNewlineAtEOF) {
3577 First->NewlinesBefore = 1;
3588 if (Current.
is(TT_FunctionDeclarationName))
3596 if (
const auto *PrevPrev =
Previous.Previous;
3597 PrevPrev && PrevPrev->is(TT_ObjCDecl)) {
3602 for (; Next; Next = Next->Next) {
3603 if (Next->is(TT_OverloadedOperatorLParen))
3605 if (Next->is(TT_OverloadedOperator))
3607 if (Next->isOneOf(tok::kw_new, tok::kw_delete)) {
3610 Next->Next->startsSequence(tok::l_square, tok::r_square)) {
3611 Next = Next->Next->Next;
3615 if (Next->startsSequence(tok::l_square, tok::r_square)) {
3620 if ((Next->isTypeName() || Next->is(tok::identifier)) && Next->Next &&
3621 Next->Next->isPointerOrReference()) {
3626 if (Next->is(TT_TemplateOpener) && Next->MatchingParen) {
3627 Next = Next->MatchingParen;
3638 if (Current.
is(tok::kw_operator)) {
3639 if (
Previous.Tok.getIdentifierInfo() &&
3640 !
Previous.isOneOf(tok::kw_return, tok::kw_co_return)) {
3645 assert(
Previous.MatchingParen->is(tok::l_paren));
3646 assert(
Previous.MatchingParen->is(TT_TypeDeclarationParen));
3651 Next = skipOperatorName(Next);
3655 for (; Next; Next = Next->Next) {
3656 if (Next->is(TT_TemplateOpener) && Next->MatchingParen) {
3657 Next = Next->MatchingParen;
3658 }
else if (Next->is(tok::coloncolon)) {
3662 if (Next->is(tok::kw_operator)) {
3663 Next = skipOperatorName(Next->Next);
3666 if (Next->isNot(tok::identifier))
3668 }
else if (isCppAttribute(*Next)) {
3669 Next = Next->MatchingParen;
3672 }
else if (Next->is(tok::l_paren)) {
3681 if (!Next || Next->isNot(tok::l_paren) || !Next->MatchingParen)
3683 ClosingParen = Next->MatchingParen;
3684 assert(ClosingParen->
is(tok::r_paren));
3686 if (
Line.Last->is(tok::l_brace))
3688 if (Next->Next == ClosingParen)
3691 if (ClosingParen->
Next && ClosingParen->
Next->
is(TT_PointerOrReference))
3704 if (
IsCpp && Next->Next && Next->Next->is(tok::identifier) &&
3705 !
Line.endsWith(tok::semi)) {
3709 for (
const FormatToken *Tok = Next->Next; Tok && Tok != ClosingParen;
3711 if (Tok->is(TT_TypeDeclarationParen))
3713 if (Tok->isOneOf(tok::l_paren, TT_TemplateOpener) && Tok->MatchingParen) {
3714 Tok = Tok->MatchingParen;
3717 if (Tok->is(tok::kw_const) || Tok->isTypeName() ||
3718 Tok->isOneOf(TT_PointerOrReference, TT_StartOfName, tok::ellipsis)) {
3721 if (Tok->isOneOf(tok::l_brace, TT_ObjCMethodExpr) || Tok->Tok.isLiteral())
3727bool TokenAnnotator::mustBreakForReturnType(
const AnnotatedLine &
Line)
const {
3728 assert(
Line.MightBeFunctionDecl);
3736 switch (Style.BreakAfterReturnType) {
3746 return Line.mightBeFunctionDefinition();
3759 :
Line.FirstStartColumn +
First->ColumnWidth;
3761 bool InFunctionDecl =
Line.MightBeFunctionDecl;
3762 bool AlignArrayOfStructures =
3765 if (AlignArrayOfStructures)
3766 calculateArrayInitializerColumnList(
Line);
3768 bool SeenName =
false;
3769 bool LineIsFunctionDeclaration =
false;
3773 for (
auto *Tok = Current; Tok; Tok = Tok->Next) {
3774 if (Tok->is(TT_StartOfName))
3776 if (Tok->Previous->EndsCppAttributeGroup)
3777 AfterLastAttribute = Tok;
3778 if (
const bool IsCtorOrDtor = Tok->is(TT_CtorDtorDeclName);
3782 LineIsFunctionDeclaration =
true;
3788 if (
IsCpp && (LineIsFunctionDeclaration ||
First->is(TT_CtorDtorDeclName)) &&
3789 Line.endsWith(tok::semi, tok::r_brace)) {
3790 auto *Tok =
Line.Last->Previous;
3791 while (Tok->isNot(tok::r_brace))
3792 Tok = Tok->Previous;
3793 if (
auto *LBrace = Tok->MatchingParen; LBrace) {
3794 assert(LBrace->is(tok::l_brace));
3797 LBrace->setFinalizedType(TT_FunctionLBrace);
3801 if (
IsCpp && SeenName && AfterLastAttribute &&
3804 if (LineIsFunctionDeclaration)
3805 Line.ReturnTypeWrapped =
true;
3809 if (!LineIsFunctionDeclaration) {
3811 for (
const auto *Tok =
First; Tok; Tok = Tok->Next) {
3812 if (Tok->isNot(tok::kw_operator))
3816 }
while (Tok && Tok->isNot(TT_OverloadedOperatorLParen));
3817 if (!Tok || !Tok->MatchingParen)
3819 const auto *LeftParen = Tok;
3820 for (Tok = Tok->Next; Tok && Tok != LeftParen->MatchingParen;
3822 if (Tok->isNot(tok::identifier))
3824 auto *Next = Tok->Next;
3825 const bool NextIsBinaryOperator =
3826 Next && Next->isPointerOrReference() && Next->Next &&
3827 Next->Next->is(tok::identifier);
3828 if (!NextIsBinaryOperator)
3830 Next->setType(TT_BinaryOperator);
3834 }
else if (ClosingParen) {
3835 for (
auto *Tok = ClosingParen->
Next; Tok; Tok = Tok->
Next) {
3836 if (Tok->is(tok::arrow)) {
3837 Tok->setType(TT_TrailingReturnArrow);
3840 if (Tok->isNot(TT_TrailingAnnotation))
3842 const auto *Next = Tok->Next;
3843 if (!Next || Next->isNot(tok::l_paren))
3845 Tok = Next->MatchingParen;
3854 if (Current->is(TT_LineComment)) {
3856 Current->SpacesRequiredBefore =
3857 (Style.Cpp11BracedListStyle && !Style.SpacesInParensOptions.Other)
3860 }
else if (Prev->
is(TT_VerilogMultiLineListLParen)) {
3861 Current->SpacesRequiredBefore = 0;
3863 Current->SpacesRequiredBefore = Style.SpacesBeforeTrailingComments;
3873 if (!Current->HasUnescapedNewline) {
3876 if (
Parameter->isOneOf(tok::comment, tok::r_brace))
3879 if (
Parameter->Previous->isNot(TT_CtorInitializerComma) &&
3887 }
else if (!Current->Finalized && Current->SpacesRequiredBefore == 0 &&
3888 spaceRequiredBefore(
Line, *Current)) {
3889 Current->SpacesRequiredBefore = 1;
3892 const auto &Children = Prev->
Children;
3893 if (!Children.empty() && Children.back()->Last->is(TT_LineComment)) {
3894 Current->MustBreakBefore =
true;
3896 Current->MustBreakBefore =
3897 Current->MustBreakBefore || mustBreakBefore(
Line, *Current);
3898 if (!Current->MustBreakBefore && InFunctionDecl &&
3899 Current->is(TT_FunctionDeclarationName)) {
3900 Current->MustBreakBefore = mustBreakForReturnType(
Line);
3904 Current->CanBreakBefore =
3905 Current->MustBreakBefore || canBreakBefore(
Line, *Current);
3906 unsigned ChildSize = 0;
3912 if (Current->MustBreakBefore || Prev->
Children.size() > 1 ||
3914 Prev->
Children[0]->First->MustBreakBefore) ||
3915 Current->IsMultiline) {
3916 Current->TotalLength = Prev->
TotalLength + Style.ColumnLimit;
3918 Current->TotalLength = Prev->
TotalLength + Current->ColumnWidth +
3919 ChildSize + Current->SpacesRequiredBefore;
3922 if (Current->is(TT_CtorInitializerColon))
3923 InFunctionDecl =
false;
3934 Current->SplitPenalty = splitPenalty(
Line, *Current, InFunctionDecl);
3936 Current->is(TT_SelectorName) && Current->ParameterIndex > 0) {
3937 if (Current->ParameterIndex == 1)
3938 Current->SplitPenalty += 5 * Current->BindingStrength;
3940 Current->SplitPenalty += 20 * Current->BindingStrength;
3943 Current = Current->Next;
3946 calculateUnbreakableTailLengths(
Line);
3947 unsigned IndentLevel =
Line.Level;
3948 for (Current =
First; Current; Current = Current->Next) {
3950 Current->Role->precomputeFormattingInfos(Current);
3951 if (Current->MatchingParen &&
3952 Current->MatchingParen->opensBlockOrBlockTypeList(Style) &&
3956 Current->IndentLevel = IndentLevel;
3957 if (Current->opensBlockOrBlockTypeList(Style))
3961 LLVM_DEBUG({ printDebugInfo(
Line); });
3964void TokenAnnotator::calculateUnbreakableTailLengths(
3966 unsigned UnbreakableTailLength = 0;
3969 Current->UnbreakableTailLength = UnbreakableTailLength;
3970 if (Current->CanBreakBefore ||
3971 Current->isOneOf(tok::comment, tok::string_literal)) {
3972 UnbreakableTailLength = 0;
3974 UnbreakableTailLength +=
3975 Current->ColumnWidth + Current->SpacesRequiredBefore;
3977 Current = Current->Previous;
3981void TokenAnnotator::calculateArrayInitializerColumnList(
3982 AnnotatedLine &
Line)
const {
3985 auto *CurrentToken =
Line.First;
3986 CurrentToken->ArrayInitializerLineStart =
true;
3988 while (CurrentToken && CurrentToken !=
Line.Last) {
3989 if (CurrentToken->is(tok::l_brace)) {
3990 CurrentToken->IsArrayInitializer =
true;
3991 if (CurrentToken->Next)
3992 CurrentToken->Next->MustBreakBefore =
true;
3994 calculateInitializerColumnList(
Line, CurrentToken->Next, Depth + 1);
3996 CurrentToken = CurrentToken->Next;
4001FormatToken *TokenAnnotator::calculateInitializerColumnList(
4002 AnnotatedLine &
Line, FormatToken *CurrentToken,
unsigned Depth)
const {
4003 while (CurrentToken && CurrentToken !=
Line.Last) {
4004 if (CurrentToken->is(tok::l_brace))
4006 else if (CurrentToken->is(tok::r_brace))
4008 if (Depth == 2 && CurrentToken->isOneOf(tok::l_brace, tok::comma)) {
4009 CurrentToken = CurrentToken->Next;
4012 CurrentToken->StartsColumn =
true;
4013 CurrentToken = CurrentToken->Previous;
4015 CurrentToken = CurrentToken->Next;
4017 return CurrentToken;
4020unsigned TokenAnnotator::splitPenalty(
const AnnotatedLine &
Line,
4021 const FormatToken &Tok,
4022 bool InFunctionDecl)
const {
4023 const FormatToken &Left = *Tok.Previous;
4024 const FormatToken &Right = Tok;
4026 if (Left.is(tok::semi))
4031 if (Right.isOneOf(Keywords.kw_extends, Keywords.kw_throws))
4033 if (Right.is(Keywords.kw_implements))
4035 if (Left.is(tok::comma) && Left.NestingLevel == 0)
4037 }
else if (Style.isJavaScript()) {
4038 if (Right.is(Keywords.kw_function) && Left.isNot(tok::comma))
4040 if (Left.is(TT_JsTypeColon))
4042 if ((Left.is(TT_TemplateString) && Left.TokenText.ends_with(
"${")) ||
4043 (Right.is(TT_TemplateString) && Right.TokenText.starts_with(
"}"))) {
4047 if (Left.opensScope() && Right.closesScope())
4050 if (Right.is(tok::l_square))
4052 if (Right.is(tok::period))
4056 if (Right.is(tok::identifier) && Right.Next && Right.Next->is(TT_DictLiteral))
4058 if (Right.is(tok::l_square)) {
4059 if (Left.is(tok::r_square))
4062 if (Right.is(TT_LambdaLSquare) && Left.is(tok::equal))
4064 if (!Right.isOneOf(TT_ObjCMethodExpr, TT_LambdaLSquare,
4065 TT_ArrayInitializerLSquare,
4066 TT_DesignatedInitializerLSquare, TT_AttributeSquare)) {
4071 if (Left.is(tok::coloncolon))
4072 return Style.PenaltyBreakScopeResolution;
4073 if (Right.isOneOf(TT_StartOfName, TT_FunctionDeclarationName) ||
4074 Right.is(tok::kw_operator)) {
4075 if (
Line.startsWith(tok::kw_for) && Right.PartOfMultiVariableDeclStmt)
4077 if (Left.is(TT_StartOfName))
4079 if (InFunctionDecl && Right.NestingLevel == 0)
4080 return Style.PenaltyReturnTypeOnItsOwnLine;
4083 if (Right.is(TT_PointerOrReference))
4085 if (Right.is(TT_TrailingReturnArrow))
4087 if (Left.is(tok::equal) && Right.is(tok::l_brace))
4089 if (Left.is(TT_CastRParen))
4091 if (Left.isOneOf(tok::kw_class, tok::kw_struct, tok::kw_union))
4093 if (Left.is(tok::comment))
4096 if (Left.isOneOf(TT_RangeBasedForLoopColon, TT_InheritanceColon,
4097 TT_CtorInitializerColon)) {
4101 if (Right.isMemberAccess()) {
4121 return !Right.NextOperator || !Right.NextOperator->Previous->closesScope()
4126 if (Right.is(TT_TrailingAnnotation) &&
4127 (!Right.Next || Right.Next->isNot(tok::l_paren))) {
4130 if (
Line.startsWith(TT_ObjCMethodSpecifier))
4137 bool is_short_annotation = Right.TokenText.size() < 10;
4138 return (Left.is(tok::r_paren) ? 100 : 120) + (is_short_annotation ? 50 : 0);
4142 if (
Line.startsWith(tok::kw_for) && Left.is(tok::equal))
4147 if (Right.is(TT_SelectorName))
4149 if (Left.is(tok::colon) && Left.is(TT_ObjCMethodExpr))
4150 return Line.MightBeFunctionDecl ? 50 : 500;
4156 Left.Previous->isOneOf(tok::identifier, tok::greater)) {
4160 if (Left.is(tok::l_paren) && Style.PenaltyBreakOpenParenthesis != 0)
4161 return Style.PenaltyBreakOpenParenthesis;
4162 if (Left.is(tok::l_paren) && InFunctionDecl &&
4166 if (Left.is(tok::l_paren) && Left.Previous &&
4167 (Left.Previous->isOneOf(tok::kw_for, tok::kw__Generic) ||
4168 Left.Previous->isIf())) {
4171 if (Left.is(tok::equal) && InFunctionDecl)
4173 if (Right.is(tok::r_brace))
4175 if (Left.is(TT_TemplateOpener))
4177 if (Left.opensScope()) {
4182 (Left.ParameterCount <= 1 || Style.AllowAllArgumentsOnNextLine)) {
4185 if (Left.is(tok::l_brace) && !Style.Cpp11BracedListStyle)
4187 return Left.ParameterCount > 1 ? Style.PenaltyBreakBeforeFirstCallParameter
4190 if (Left.is(TT_JavaAnnotation))
4193 if (Left.is(TT_UnaryOperator))
4195 if (Left.isOneOf(tok::plus, tok::comma) && Left.Previous &&
4196 Left.Previous->isLabelString() &&
4197 (Left.NextOperator || Left.OperatorIndex != 0)) {
4200 if (Right.is(tok::plus) && Left.isLabelString() &&
4201 (Right.NextOperator || Right.OperatorIndex != 0)) {
4204 if (Left.is(tok::comma))
4206 if (Right.is(tok::lessless) && Left.isLabelString() &&
4207 (Right.NextOperator || Right.OperatorIndex != 1)) {
4210 if (Right.is(tok::lessless)) {
4212 if (Left.isNot(tok::r_paren) || Right.OperatorIndex > 0) {
4218 if (Left.ClosesTemplateDeclaration)
4219 return Style.PenaltyBreakTemplateDeclaration;
4220 if (Left.ClosesRequiresClause)
4222 if (Left.is(TT_ConditionalExpr))
4226 Level = Right.getPrecedence();
4228 return Style.PenaltyBreakAssignment;
4235bool TokenAnnotator::spaceRequiredBeforeParens(
const FormatToken &Right)
const {
4238 if (Right.is(TT_OverloadedOperatorLParen) &&
4239 Style.SpaceBeforeParensOptions.AfterOverloadedOperator) {
4242 if (Style.SpaceBeforeParensOptions.BeforeNonEmptyParentheses &&
4243 Right.ParameterCount > 0) {
4249bool TokenAnnotator::spaceRequiredBetween(
const AnnotatedLine &
Line,
4250 const FormatToken &Left,
4251 const FormatToken &Right)
const {
4252 if (Left.is(tok::kw_return) &&
4253 !Right.isOneOf(tok::semi, tok::r_paren, tok::hashhash)) {
4256 if (Left.is(tok::kw_throw) && Right.is(tok::l_paren) && Right.MatchingParen &&
4257 Right.MatchingParen->is(TT_CastRParen)) {
4263 Left.Tok.getObjCKeywordID() == tok::objc_property) {
4266 if (Right.is(tok::hashhash))
4267 return Left.is(tok::hash);
4268 if (Left.isOneOf(tok::hashhash, tok::hash))
4269 return Right.is(tok::hash);
4270 if (Left.is(
BK_Block) && Right.is(tok::r_brace) &&
4271 Right.MatchingParen == &Left &&
Line.Children.empty()) {
4272 return Style.SpaceInEmptyBlock;
4274 if ((Left.is(tok::l_paren) && Right.is(tok::r_paren)) ||
4275 (Left.is(tok::l_brace) && Left.isNot(
BK_Block) &&
4276 Right.is(tok::r_brace) && Right.isNot(
BK_Block))) {
4277 return Style.SpacesInParensOptions.InEmptyParentheses;
4279 if (Style.SpacesInParensOptions.InConditionalStatements) {
4280 const FormatToken *LeftParen =
nullptr;
4281 if (Left.is(tok::l_paren))
4283 else if (Right.is(tok::r_paren) && Right.MatchingParen)
4284 LeftParen = Right.MatchingParen;
4286 if (LeftParen->is(TT_ConditionLParen))
4288 if (LeftParen->Previous && isKeywordWithCondition(*LeftParen->Previous))
4294 if (Left.is(tok::kw_auto) && Right.isOneOf(TT_LambdaLBrace, TT_FunctionLBrace,
4296 TT_FunctionTypeLParen)) {
4301 if (Left.is(tok::kw_auto) && Right.isOneOf(tok::l_paren, tok::l_brace))
4305 if (Right.is(tok::l_paren) && Left.is(tok::kw_co_await) && Left.Previous &&
4306 Left.Previous->is(tok::kw_operator)) {
4310 if (Left.isOneOf(tok::kw_co_await, tok::kw_co_yield, tok::kw_co_return) &&
4311 !Right.isOneOf(tok::semi, tok::r_paren)) {
4315 if (Left.is(tok::l_paren) || Right.is(tok::r_paren)) {
4316 return (Right.is(TT_CastRParen) ||
4317 (Left.MatchingParen && Left.MatchingParen->is(TT_CastRParen)))
4318 ? Style.SpacesInParensOptions.InCStyleCasts
4319 : Style.SpacesInParensOptions.Other;
4321 if (Right.isOneOf(tok::semi, tok::comma))
4324 bool IsLightweightGeneric = Right.MatchingParen &&
4325 Right.MatchingParen->Next &&
4326 Right.MatchingParen->Next->is(tok::colon);
4327 return !IsLightweightGeneric && Style.ObjCSpaceBeforeProtocolList;
4329 if (Right.is(tok::less) && Left.is(tok::kw_template))
4330 return Style.SpaceAfterTemplateKeyword;
4331 if (Left.isOneOf(tok::exclaim, tok::tilde))
4333 if (Left.is(tok::at) &&
4334 Right.isOneOf(tok::identifier, tok::string_literal, tok::char_constant,
4335 tok::numeric_constant, tok::l_paren, tok::l_brace,
4336 tok::kw_true, tok::kw_false)) {
4339 if (Left.is(tok::colon))
4340 return Left.isNot(TT_ObjCMethodExpr);
4341 if (Left.is(tok::coloncolon))
4343 if (Left.is(tok::less) || Right.isOneOf(tok::greater, tok::less)) {
4346 (Left.is(TT_DictLiteral) || Right.is(TT_DictLiteral)))) {
4348 if (Left.is(tok::less) && Right.is(tok::greater))
4350 return !Style.Cpp11BracedListStyle;
4353 if (Right.isNot(TT_OverloadedOperatorLParen))
4356 if (Right.is(tok::ellipsis)) {
4357 return Left.Tok.isLiteral() || (Left.is(tok::identifier) && Left.Previous &&
4358 Left.Previous->is(tok::kw_case));
4360 if (Left.is(tok::l_square) && Right.is(tok::amp))
4361 return Style.SpacesInSquareBrackets;
4362 if (Right.is(TT_PointerOrReference)) {
4363 if (Left.is(tok::r_paren) &&
Line.MightBeFunctionDecl) {
4364 if (!Left.MatchingParen)
4366 FormatToken *TokenBeforeMatchingParen =
4367 Left.MatchingParen->getPreviousNonComment();
4368 if (!TokenBeforeMatchingParen || Left.isNot(TT_TypeDeclarationParen))
4376 (Left.is(TT_AttributeRParen) ||
4377 Left.canBePointerOrReferenceQualifier())) {
4380 if (Left.Tok.isLiteral())
4383 if (Left.isTypeOrIdentifier() && Right.Next && Right.Next->Next &&
4384 Right.Next->Next->is(TT_RangeBasedForLoopColon)) {
4385 return getTokenPointerOrReferenceAlignment(Right) !=
4388 return !Left.isOneOf(TT_PointerOrReference, tok::l_paren) &&
4389 (getTokenPointerOrReferenceAlignment(Right) !=
4391 (
Line.IsMultiVariableDeclStmt &&
4392 (Left.NestingLevel == 0 ||
4393 (Left.NestingLevel == 1 && startsWithInitStatement(
Line)))));
4395 if (Right.is(TT_FunctionTypeLParen) && Left.isNot(tok::l_paren) &&
4396 (Left.isNot(TT_PointerOrReference) ||
4398 !
Line.IsMultiVariableDeclStmt))) {
4401 if (Left.is(TT_PointerOrReference)) {
4406 Right.canBePointerOrReferenceQualifier()) {
4410 if (Right.Tok.isLiteral())
4413 if (Right.is(TT_BlockComment))
4417 if (Right.isOneOf(Keywords.kw_override, Keywords.kw_final, tok::kw_noexcept,
4418 TT_RequiresClause) &&
4419 Right.isNot(TT_StartOfName)) {
4423 if (Right.is(tok::l_brace) && Right.is(
BK_Block))
4426 if (Left.Previous && Left.Previous->isTypeOrIdentifier() && Right.Next &&
4427 Right.Next->is(TT_RangeBasedForLoopColon)) {
4428 return getTokenPointerOrReferenceAlignment(Left) !=
4431 if (Right.isOneOf(TT_PointerOrReference, TT_ArraySubscriptLSquare,
4443 if (
Line.IsMultiVariableDeclStmt &&
4444 (Left.NestingLevel ==
Line.First->NestingLevel ||
4445 ((Left.NestingLevel ==
Line.First->NestingLevel + 1) &&
4446 startsWithInitStatement(
Line)))) {
4449 return Left.Previous && !Left.Previous->isOneOf(
4450 tok::l_paren, tok::coloncolon, tok::l_square);
4453 if (Left.is(tok::ellipsis) && Left.Previous &&
4454 Left.Previous->isPointerOrReference()) {
4458 if (Right.is(tok::star) && Left.is(tok::l_paren))
4460 if (Left.is(tok::star) && Right.isPointerOrReference())
4462 if (Right.isPointerOrReference()) {
4463 const FormatToken *
Previous = &Left;
4473 if (
Previous->is(tok::coloncolon)) {
4492 if (
Previous->endsSequence(tok::kw_operator))
4496 (Style.SpaceAroundPointerQualifiers ==
4502 if (Style.isCSharp() && Left.is(Keywords.kw_is) && Right.is(tok::l_square))
4504 const auto SpaceRequiredForArrayInitializerLSquare =
4505 [](
const FormatToken &LSquareTok,
const FormatStyle &Style) {
4506 return Style.SpacesInContainerLiterals ||
4507 (Style.isProto() && !Style.Cpp11BracedListStyle &&
4508 LSquareTok.endsSequence(tok::l_square, tok::colon,
4511 if (Left.is(tok::l_square)) {
4512 return (Left.is(TT_ArrayInitializerLSquare) && Right.isNot(tok::r_square) &&
4513 SpaceRequiredForArrayInitializerLSquare(Left, Style)) ||
4514 (Left.isOneOf(TT_ArraySubscriptLSquare, TT_StructuredBindingLSquare,
4515 TT_LambdaLSquare) &&
4516 Style.SpacesInSquareBrackets && Right.isNot(tok::r_square));
4518 if (Right.is(tok::r_square)) {
4519 return Right.MatchingParen &&
4520 ((Right.MatchingParen->is(TT_ArrayInitializerLSquare) &&
4521 SpaceRequiredForArrayInitializerLSquare(*Right.MatchingParen,
4523 (Style.SpacesInSquareBrackets &&
4524 Right.MatchingParen->isOneOf(TT_ArraySubscriptLSquare,
4525 TT_StructuredBindingLSquare,
4526 TT_LambdaLSquare)));
4528 if (Right.is(tok::l_square) &&
4529 !Right.isOneOf(TT_ObjCMethodExpr, TT_LambdaLSquare,
4530 TT_DesignatedInitializerLSquare,
4531 TT_StructuredBindingLSquare, TT_AttributeSquare) &&
4532 !Left.isOneOf(tok::numeric_constant, TT_DictLiteral) &&
4533 !(Left.isNot(tok::r_square) && Style.SpaceBeforeSquareBrackets &&
4534 Right.is(TT_ArraySubscriptLSquare))) {
4537 if (Left.is(tok::l_brace) && Right.is(tok::r_brace))
4538 return !Left.Children.empty();
4539 if ((Left.is(tok::l_brace) && Left.isNot(
BK_Block)) ||
4540 (Right.is(tok::r_brace) && Right.MatchingParen &&
4541 Right.MatchingParen->isNot(
BK_Block))) {
4542 return !Style.Cpp11BracedListStyle || Style.SpacesInParensOptions.Other;
4544 if (Left.is(TT_BlockComment)) {
4546 return Style.isJavaScript() || !Left.TokenText.ends_with(
"=*/");
4551 if (Left.is(TT_TemplateCloser) && Right.is(TT_AttributeSquare))
4554 if (Right.is(tok::l_paren)) {
4555 if (Left.is(TT_TemplateCloser) && Right.isNot(TT_FunctionTypeLParen))
4556 return spaceRequiredBeforeParens(Right);
4557 if (Left.isOneOf(TT_RequiresClause,
4558 TT_RequiresClauseInARequiresExpression)) {
4559 return Style.SpaceBeforeParensOptions.AfterRequiresInClause ||
4560 spaceRequiredBeforeParens(Right);
4562 if (Left.is(TT_RequiresExpression)) {
4563 return Style.SpaceBeforeParensOptions.AfterRequiresInExpression ||
4564 spaceRequiredBeforeParens(Right);
4566 if (Left.is(TT_AttributeRParen) ||
4567 (Left.is(tok::r_square) && Left.is(TT_AttributeSquare))) {
4570 if (Left.is(TT_ForEachMacro)) {
4571 return Style.SpaceBeforeParensOptions.AfterForeachMacros ||
4572 spaceRequiredBeforeParens(Right);
4574 if (Left.is(TT_IfMacro)) {
4575 return Style.SpaceBeforeParensOptions.AfterIfMacros ||
4576 spaceRequiredBeforeParens(Right);
4579 Left.isOneOf(tok::kw_new, tok::kw_delete) &&
4580 Right.isNot(TT_OverloadedOperatorLParen) &&
4581 !(
Line.MightBeFunctionDecl && Left.is(TT_FunctionDeclarationName))) {
4582 return Style.SpaceBeforeParensOptions.AfterPlacementOperator;
4586 if (Left.is(tok::semi))
4588 if (Left.isOneOf(tok::pp_elif, tok::kw_for, tok::kw_while, tok::kw_switch,
4589 tok::kw_case, TT_ForEachMacro, TT_ObjCForIn) ||
4591 Right.is(TT_ConditionLParen)) {
4592 return Style.SpaceBeforeParensOptions.AfterControlStatements ||
4593 spaceRequiredBeforeParens(Right);
4598 if (Right.is(TT_OverloadedOperatorLParen))
4599 return spaceRequiredBeforeParens(Right);
4601 if (
Line.MightBeFunctionDecl && (Left.is(TT_FunctionDeclarationName))) {
4602 if (
Line.mightBeFunctionDefinition()) {
4603 return Style.SpaceBeforeParensOptions.AfterFunctionDefinitionName ||
4604 spaceRequiredBeforeParens(Right);
4606 return Style.SpaceBeforeParensOptions.AfterFunctionDeclarationName ||
4607 spaceRequiredBeforeParens(Right);
4612 Left.MatchingParen && Left.MatchingParen->is(TT_LambdaLSquare)) {
4613 return Style.SpaceBeforeParensOptions.AfterFunctionDefinitionName ||
4614 spaceRequiredBeforeParens(Right);
4616 if (!Left.Previous || Left.Previous->isNot(tok::period)) {
4617 if (Left.isOneOf(tok::kw_try, Keywords.kw___except, tok::kw_catch)) {
4618 return Style.SpaceBeforeParensOptions.AfterControlStatements ||
4619 spaceRequiredBeforeParens(Right);
4621 if (Left.isOneOf(tok::kw_new, tok::kw_delete)) {
4622 return ((!
Line.MightBeFunctionDecl || !Left.Previous) &&
4624 spaceRequiredBeforeParens(Right);
4627 if (Left.is(tok::r_square) && Left.MatchingParen &&
4628 Left.MatchingParen->Previous &&
4629 Left.MatchingParen->Previous->is(tok::kw_delete)) {
4631 spaceRequiredBeforeParens(Right);
4636 (Left.Tok.getIdentifierInfo() || Left.is(tok::r_paren))) {
4637 return spaceRequiredBeforeParens(Right);
4641 if (Left.is(tok::at) && Right.Tok.getObjCKeywordID() != tok::objc_not_keyword)
4643 if (Right.is(TT_UnaryOperator)) {
4644 return !Left.isOneOf(tok::l_paren, tok::l_square, tok::at) &&
4645 (Left.isNot(tok::colon) || Left.isNot(TT_ObjCMethodExpr));
4651 if (!Style.isVerilog() &&
4652 (Left.isOneOf(tok::identifier, tok::greater, tok::r_square,
4654 Left.isTypeName()) &&
4655 Right.is(tok::l_brace) && Right.getNextNonComment() &&
4659 if (Left.is(tok::period) || Right.is(tok::period))
4663 if (Right.is(tok::hash) && Left.is(tok::identifier) &&
4664 (Left.TokenText ==
"L" || Left.TokenText ==
"u" ||
4665 Left.TokenText ==
"U" || Left.TokenText ==
"u8" ||
4666 Left.TokenText ==
"LR" || Left.TokenText ==
"uR" ||
4667 Left.TokenText ==
"UR" || Left.TokenText ==
"u8R")) {
4670 if (Left.is(TT_TemplateCloser) && Left.MatchingParen &&
4671 Left.MatchingParen->Previous &&
4672 (Left.MatchingParen->Previous->is(tok::period) ||
4673 Left.MatchingParen->Previous->is(tok::coloncolon))) {
4679 if (Left.is(TT_TemplateCloser) && Right.is(tok::l_square))
4681 if (Left.is(tok::l_brace) && Left.endsSequence(TT_DictLiteral, tok::at)) {
4685 if (Right.is(tok::r_brace) && Right.MatchingParen &&
4686 Right.MatchingParen->endsSequence(TT_DictLiteral, tok::at)) {
4690 if (Right.getType() == TT_TrailingAnnotation &&
4691 Right.isOneOf(tok::amp, tok::ampamp) &&
4692 Left.isOneOf(tok::kw_const, tok::kw_volatile) &&
4693 (!Right.Next || Right.Next->is(tok::semi))) {
4703bool TokenAnnotator::spaceRequiredBefore(
const AnnotatedLine &
Line,
4704 const FormatToken &Right)
const {
4705 const FormatToken &Left = *Right.Previous;
4710 return Right.hasWhitespaceBefore();
4713 if (Keywords.isWordLike(Right) && Keywords.isWordLike(Left))
4718 if (Left.is(tok::star) && Right.is(tok::comment))
4722 if (Left.is(TT_OverloadedOperator) &&
4723 Right.isOneOf(TT_TemplateOpener, TT_TemplateCloser)) {
4727 if (Right.is(tok::period) && Left.is(tok::numeric_constant))
4731 if (Left.is(Keywords.kw_import) && Right.isOneOf(tok::less, tok::ellipsis))
4734 if (Left.isOneOf(Keywords.kw_module, Keywords.kw_import) &&
4735 Right.is(TT_ModulePartitionColon)) {
4739 if (Left.is(tok::identifier) && Right.is(TT_ModulePartitionColon))
4742 if (Left.is(TT_ModulePartitionColon) &&
4743 Right.isOneOf(tok::identifier, tok::kw_private)) {
4746 if (Left.is(tok::ellipsis) && Right.is(tok::identifier) &&
4747 Line.First->is(Keywords.kw_import)) {
4751 if (Left.isOneOf(TT_AttributeRParen, TT_AttributeMacro) &&
4752 Right.is(tok::coloncolon)) {
4756 if (Left.is(tok::kw_operator))
4757 return Right.is(tok::coloncolon);
4759 !Left.opensScope() && Style.SpaceBeforeCpp11BracedList) {
4762 if (Left.is(tok::less) && Left.is(TT_OverloadedOperator) &&
4763 Right.is(TT_TemplateOpener)) {
4766 }
else if (Style.isProto()) {
4767 if (Right.is(tok::period) &&
4768 Left.isOneOf(Keywords.kw_optional, Keywords.kw_required,
4769 Keywords.kw_repeated, Keywords.kw_extend)) {
4772 if (Right.is(tok::l_paren) &&
4773 Left.isOneOf(Keywords.kw_returns, Keywords.kw_option)) {
4776 if (Right.isOneOf(tok::l_brace, tok::less) && Left.is(TT_SelectorName))
4779 if (Left.is(tok::slash) || Right.is(tok::slash))
4781 if (Left.MatchingParen &&
4782 Left.MatchingParen->is(TT_ProtoExtensionLSquare) &&
4783 Right.isOneOf(tok::l_brace, tok::less)) {
4784 return !Style.Cpp11BracedListStyle;
4787 if (Left.is(tok::percent))
4791 if (Left.is(tok::numeric_constant) && Right.is(tok::percent))
4792 return Right.hasWhitespaceBefore();
4793 }
else if (Style.isJson()) {
4794 if (Right.is(tok::colon) && Left.is(tok::string_literal))
4795 return Style.SpaceBeforeJsonColon;
4796 }
else if (Style.isCSharp()) {
4802 if (Left.is(tok::kw_this) && Right.is(tok::l_square))
4806 if (Left.is(tok::kw_new) && Right.is(tok::l_paren))
4810 if (Right.is(tok::l_brace))
4814 if (Left.is(tok::l_brace) && Right.isNot(tok::r_brace))
4817 if (Left.isNot(tok::l_brace) && Right.is(tok::r_brace))
4821 if (Left.is(TT_FatArrow) || Right.is(TT_FatArrow))
4825 if (Left.is(TT_AttributeColon) || Right.is(TT_AttributeColon))
4829 if (Left.is(TT_TemplateCloser) && Right.is(TT_StartOfName))
4833 if (Left.is(tok::l_square) || Right.is(tok::r_square))
4834 return Style.SpacesInSquareBrackets;
4837 if (Right.is(TT_CSharpNullable))
4841 if (Right.is(TT_NonNullAssertion))
4845 if (Left.is(tok::comma) && Right.is(tok::comma))
4849 if (Left.is(Keywords.kw_var) && Right.is(tok::l_paren))
4853 if (Right.is(tok::l_paren)) {
4854 if (Left.isOneOf(tok::kw_using, Keywords.kw_async, Keywords.kw_when,
4855 Keywords.kw_lock)) {
4856 return Style.SpaceBeforeParensOptions.AfterControlStatements ||
4857 spaceRequiredBeforeParens(Right);
4863 if (Left.isOneOf(tok::kw_public, tok::kw_private, tok::kw_protected,
4864 tok::kw_virtual, tok::kw_extern, tok::kw_static,
4865 Keywords.kw_internal, Keywords.kw_abstract,
4866 Keywords.kw_sealed, Keywords.kw_override,
4867 Keywords.kw_async, Keywords.kw_unsafe) &&
4868 Right.is(tok::l_paren)) {
4871 }
else if (Style.isJavaScript()) {
4872 if (Left.is(TT_FatArrow))
4875 if (Right.is(tok::l_paren) && Left.is(Keywords.kw_await) && Left.Previous &&
4876 Left.Previous->is(tok::kw_for)) {
4879 if (Left.is(Keywords.kw_async) && Right.is(tok::l_paren) &&
4880 Right.MatchingParen) {
4881 const FormatToken *Next = Right.MatchingParen->getNextNonComment();
4884 if (Next && Next->is(TT_FatArrow))
4887 if ((Left.is(TT_TemplateString) && Left.TokenText.ends_with(
"${")) ||
4888 (Right.is(TT_TemplateString) && Right.TokenText.starts_with(
"}"))) {
4893 if (Keywords.IsJavaScriptIdentifier(Left,
4895 Right.is(TT_TemplateString)) {
4898 if (Right.is(tok::star) &&
4899 Left.isOneOf(Keywords.kw_function, Keywords.kw_yield)) {
4902 if (Right.isOneOf(tok::l_brace, tok::l_square) &&
4903 Left.isOneOf(Keywords.kw_function, Keywords.kw_yield,
4904 Keywords.kw_extends, Keywords.kw_implements)) {
4907 if (Right.is(tok::l_paren)) {
4909 if (
Line.MustBeDeclaration && Left.Tok.getIdentifierInfo())
4913 if (Left.Previous && Left.Previous->is(tok::period) &&
4914 Left.Tok.getIdentifierInfo()) {
4918 if (Left.isOneOf(tok::kw_throw, Keywords.kw_await, Keywords.kw_typeof,
4924 if (Left.endsSequence(tok::kw_const, Keywords.kw_as))
4926 if ((Left.isOneOf(Keywords.kw_let, Keywords.kw_var, Keywords.kw_in,
4931 (Left.is(Keywords.kw_of) && Left.Previous &&
4932 (Left.Previous->is(tok::identifier) ||
4933 Left.Previous->isOneOf(tok::r_square, tok::r_brace)))) &&
4934 (!Left.Previous || Left.Previous->isNot(tok::period))) {
4937 if (Left.isOneOf(tok::kw_for, Keywords.kw_as) && Left.Previous &&
4938 Left.Previous->is(tok::period) && Right.is(tok::l_paren)) {
4941 if (Left.is(Keywords.kw_as) &&
4942 Right.isOneOf(tok::l_square, tok::l_brace, tok::l_paren)) {
4945 if (Left.is(tok::kw_default) && Left.Previous &&
4946 Left.Previous->is(tok::kw_export)) {
4949 if (Left.is(Keywords.kw_is) && Right.is(tok::l_brace))
4951 if (Right.isOneOf(TT_JsTypeColon, TT_JsTypeOptionalQuestion))
4953 if (Left.is(TT_JsTypeOperator) || Right.is(TT_JsTypeOperator))
4955 if ((Left.is(tok::l_brace) || Right.is(tok::r_brace)) &&
4956 Line.First->isOneOf(Keywords.kw_import, tok::kw_export)) {
4959 if (Left.is(tok::ellipsis))
4961 if (Left.is(TT_TemplateCloser) &&
4962 !Right.isOneOf(tok::equal, tok::l_brace, tok::comma, tok::l_square,
4963 Keywords.kw_implements, Keywords.kw_extends)) {
4969 if (Right.is(TT_NonNullAssertion))
4971 if (Left.is(TT_NonNullAssertion) &&
4972 Right.isOneOf(Keywords.kw_as, Keywords.kw_in)) {
4976 if (Left.is(tok::r_square) && Right.is(tok::l_brace))
4979 if (Left.is(tok::l_square) || Right.is(tok::r_square))
4980 return Style.SpacesInSquareBrackets;
4982 if (Left.is(Keywords.kw_synchronized) && Right.is(tok::l_paren)) {
4983 return Style.SpaceBeforeParensOptions.AfterControlStatements ||
4984 spaceRequiredBeforeParens(Right);
4986 if ((Left.isOneOf(tok::kw_static, tok::kw_public, tok::kw_private,
4987 tok::kw_protected) ||
4988 Left.isOneOf(Keywords.kw_final, Keywords.kw_abstract,
4989 Keywords.kw_native)) &&
4990 Right.is(TT_TemplateOpener)) {
4993 }
else if (Style.isVerilog()) {
4995 if (Style.isVerilog() && Left.is(tok::identifier) &&
4996 Left.TokenText[0] ==
'\\') {
5001 if ((Left.is(TT_VerilogTableItem) &&
5002 !Right.isOneOf(tok::r_paren, tok::semi)) ||
5003 (Right.is(TT_VerilogTableItem) && Left.isNot(tok::l_paren))) {
5004 const FormatToken *Next = Right.getNextNonComment();
5005 return !(Next && Next->is(tok::r_paren));
5008 if (Left.isNot(TT_BinaryOperator) &&
5009 Left.isOneOf(Keywords.kw_verilogHash, Keywords.kw_verilogHashHash)) {
5013 if (Right.isNot(tok::semi) &&
5014 (Left.endsSequence(tok::numeric_constant, Keywords.kw_verilogHash) ||
5015 Left.endsSequence(tok::numeric_constant,
5016 Keywords.kw_verilogHashHash) ||
5017 (Left.is(tok::r_paren) && Left.MatchingParen &&
5018 Left.MatchingParen->endsSequence(tok::l_paren, tok::at)))) {
5023 if (Left.is(Keywords.kw_apostrophe) ||
5024 (Left.is(TT_VerilogNumberBase) && Right.is(tok::numeric_constant))) {
5028 if (Left.is(tok::arrow) || Right.is(tok::arrow))
5033 if (Left.is(tok::at) && Right.isOneOf(tok::l_paren, tok::star, tok::at))
5036 if (Right.is(tok::l_square) &&
5037 Left.isOneOf(TT_VerilogDimensionedTypeName, Keywords.kw_function)) {
5041 if (Right.isOneOf(tok::period, Keywords.kw_apostrophe) &&
5042 Keywords.isVerilogIdentifier(Left) && Left.getPreviousNonComment() &&
5043 Left.getPreviousNonComment()->is(Keywords.kw_tagged)) {
5049 if ((Right.is(Keywords.kw_apostrophe) ||
5051 !(Left.isOneOf(Keywords.kw_assign, Keywords.kw_unique) ||
5052 Keywords.isVerilogWordOperator(Left)) &&
5053 (Left.isOneOf(tok::r_square, tok::r_paren, tok::r_brace,
5054 tok::numeric_constant) ||
5055 Keywords.isWordLike(Left))) {
5059 if ((Right.is(tok::star) && Left.is(tok::coloncolon)) ||
5060 (Left.is(tok::star) && Right.is(tok::semi))) {
5064 if (Left.endsSequence(tok::star, tok::l_paren) && Right.is(tok::identifier))
5067 if (Right.is(tok::l_paren) && Right.is(TT_VerilogStrength))
5070 if ((Left.is(tok::l_brace) &&
5071 Right.isOneOf(tok::lessless, tok::greatergreater)) ||
5072 (Left.endsSequence(tok::lessless, tok::l_brace) ||
5073 Left.endsSequence(tok::greatergreater, tok::l_brace))) {
5076 }
else if (Style.isTableGen()) {
5078 if (Left.is(tok::l_square) && Right.is(tok::l_brace))
5080 if (Left.is(tok::r_brace) && Right.is(tok::r_square))
5083 if (Right.is(TT_TableGenDAGArgListColon) ||
5084 Left.is(TT_TableGenDAGArgListColon)) {
5087 if (Right.is(TT_TableGenCondOperatorColon))
5090 if (Right.isOneOf(tok::l_paren, tok::less) &&
5091 Left.isOneOf(TT_TableGenBangOperator, TT_TableGenCondOperator)) {
5096 if (Left.is(TT_TableGenTrailingPasteOperator) &&
5097 Right.isOneOf(tok::l_brace, tok::colon)) {
5101 if (Left.is(tok::hash) || Right.is(tok::hash))
5104 if (Keywords.isTableGenDefinition(Left))
5108 if (Left.is(TT_ImplicitStringLiteral))
5109 return Right.hasWhitespaceBefore();
5111 if (Left.is(TT_ObjCMethodSpecifier))
5113 if (Left.is(tok::r_paren) && Left.isNot(TT_AttributeRParen) &&
5114 canBeObjCSelectorComponent(Right)) {
5122 (Right.is(tok::equal) || Left.is(tok::equal))) {
5126 if (Right.is(TT_TrailingReturnArrow) || Left.is(TT_TrailingReturnArrow))
5129 if (Left.is(tok::comma) && Right.isNot(TT_OverloadedOperatorLParen) &&
5132 (Left.Children.empty() || !Left.MacroParent)) {
5135 if (Right.is(tok::comma))
5137 if (Right.is(TT_ObjCBlockLParen))
5139 if (Right.is(TT_CtorInitializerColon))
5140 return Style.SpaceBeforeCtorInitializerColon;
5141 if (Right.is(TT_InheritanceColon) && !Style.SpaceBeforeInheritanceColon)
5143 if (Right.is(TT_RangeBasedForLoopColon) &&
5144 !Style.SpaceBeforeRangeBasedForLoopColon) {
5147 if (Left.is(TT_BitFieldColon)) {
5151 if (Right.is(tok::colon)) {
5152 if (Right.is(TT_CaseLabelColon))
5153 return Style.SpaceBeforeCaseColon;
5154 if (Right.is(TT_GotoLabelColon))
5157 if (!Right.getNextNonComment())
5159 if (Right.is(TT_ObjCMethodExpr))
5161 if (Left.is(tok::question))
5163 if (Right.is(TT_InlineASMColon) && Left.is(tok::coloncolon))
5165 if (Right.is(TT_DictLiteral))
5166 return Style.SpacesInContainerLiterals;
5167 if (Right.is(TT_AttributeColon))
5169 if (Right.is(TT_CSharpNamedArgumentColon))
5171 if (Right.is(TT_GenericSelectionColon))
5173 if (Right.is(TT_BitFieldColon)) {
5180 if ((Left.isOneOf(tok::minus, tok::minusminus) &&
5181 Right.isOneOf(tok::minus, tok::minusminus)) ||
5182 (Left.isOneOf(tok::plus, tok::plusplus) &&
5183 Right.isOneOf(tok::plus, tok::plusplus))) {
5186 if (Left.is(TT_UnaryOperator)) {
5187 if (Right.isNot(tok::l_paren)) {
5191 if (Left.is(tok::exclaim) && Left.TokenText ==
"not")
5193 if (Left.is(tok::tilde) && Left.TokenText ==
"compl")
5197 if (Left.is(tok::amp) && Right.is(tok::r_square))
5198 return Style.SpacesInSquareBrackets;
5200 return (Style.SpaceAfterLogicalNot && Left.is(tok::exclaim)) ||
5201 Right.is(TT_BinaryOperator);
5206 if (Left.is(TT_CastRParen)) {
5207 return Style.SpaceAfterCStyleCast ||
5208 Right.isOneOf(TT_BinaryOperator, TT_SelectorName);
5211 auto ShouldAddSpacesInAngles = [
this, &Right]() {
5215 return Right.hasWhitespaceBefore();
5219 if (Left.is(tok::greater) && Right.is(tok::greater)) {
5222 return !Style.Cpp11BracedListStyle;
5224 return Right.is(TT_TemplateCloser) && Left.is(TT_TemplateCloser) &&
5226 ShouldAddSpacesInAngles());
5228 if (Right.isOneOf(tok::arrow, tok::arrowstar, tok::periodstar) ||
5229 Left.isOneOf(tok::arrow, tok::period, tok::arrowstar, tok::periodstar) ||
5230 (Right.is(tok::period) && Right.isNot(TT_DesignatedInitializerPeriod))) {
5233 if (!Style.SpaceBeforeAssignmentOperators && Left.isNot(TT_TemplateCloser) &&
5238 (Left.is(tok::identifier) || Left.is(tok::kw_this))) {
5241 if (Right.is(tok::coloncolon) && Left.is(tok::identifier)) {
5245 return Right.hasWhitespaceBefore();
5247 if (Right.is(tok::coloncolon) &&
5248 !Left.isOneOf(tok::l_brace, tok::comment, tok::l_paren)) {
5250 return (Left.is(TT_TemplateOpener) &&
5252 ShouldAddSpacesInAngles())) ||
5253 !(Left.isOneOf(tok::l_paren, tok::r_paren, tok::l_square,
5254 tok::kw___super, TT_TemplateOpener,
5255 TT_TemplateCloser)) ||
5256 (Left.is(tok::l_paren) && Style.SpacesInParensOptions.Other);
5258 if ((Left.is(TT_TemplateOpener)) != (Right.is(TT_TemplateCloser)))
5259 return ShouldAddSpacesInAngles();
5261 if (Right.is(TT_StructuredBindingLSquare)) {
5262 return !Left.isOneOf(tok::amp, tok::ampamp) ||
5266 if (Right.Next && Right.Next->is(TT_StructuredBindingLSquare) &&
5267 Right.isOneOf(tok::amp, tok::ampamp)) {
5270 if ((Right.is(TT_BinaryOperator) && Left.isNot(tok::l_paren)) ||
5271 (Left.isOneOf(TT_BinaryOperator, TT_ConditionalExpr) &&
5272 Right.isNot(tok::r_paren))) {
5275 if (Right.is(TT_TemplateOpener) && Left.is(tok::r_paren) &&
5276 Left.MatchingParen &&
5277 Left.MatchingParen->is(TT_OverloadedOperatorLParen)) {
5280 if (Right.is(tok::less) && Left.isNot(tok::l_paren) &&
5284 if (Right.is(TT_TrailingUnaryOperator))
5286 if (Left.is(TT_RegexLiteral))
5288 return spaceRequiredBetween(
Line, Left, Right);
5294 !Tok.
isOneOf(TT_ObjCBlockLBrace, TT_LambdaLBrace, TT_DictLiteral);
5311 !Tok.
isOneOf(TT_ObjCBlockLBrace, TT_DictLiteral);
5314bool TokenAnnotator::mustBreakBefore(
const AnnotatedLine &
Line,
5315 const FormatToken &Right)
const {
5316 const FormatToken &Left = *Right.Previous;
5317 if (Right.NewlinesBefore > 1 && Style.MaxEmptyLinesToKeep > 0)
5320 if (Style.isCSharp()) {
5321 if (Left.is(TT_FatArrow) && Right.is(tok::l_brace) &&
5322 Style.BraceWrapping.AfterFunction) {
5325 if (Right.is(TT_CSharpNamedArgumentColon) ||
5326 Left.is(TT_CSharpNamedArgumentColon)) {
5329 if (Right.is(TT_CSharpGenericTypeConstraint))
5331 if (Right.Next && Right.Next->is(TT_FatArrow) &&
5332 (Right.is(tok::numeric_constant) ||
5333 (Right.is(tok::identifier) && Right.TokenText ==
"_"))) {
5338 if (Left.is(TT_AttributeSquare) && Left.is(tok::r_square) &&
5339 (Right.isAccessSpecifier(
false) ||
5340 Right.is(Keywords.kw_internal))) {
5344 if (Left.is(TT_AttributeSquare) && Right.is(TT_AttributeSquare) &&
5345 Left.is(tok::r_square) && Right.is(tok::l_square)) {
5349 }
else if (Style.isJavaScript()) {
5351 if (Right.is(tok::string_literal) && Left.is(tok::plus) && Left.Previous &&
5352 Left.Previous->is(tok::string_literal)) {
5355 if (Left.is(TT_DictLiteral) && Left.is(tok::l_brace) &&
Line.Level == 0 &&
5356 Left.Previous && Left.Previous->is(tok::equal) &&
5357 Line.First->isOneOf(tok::identifier, Keywords.kw_import, tok::kw_export,
5361 !
Line.First->isOneOf(Keywords.kw_var, Keywords.kw_let)) {
5366 if (Left.is(tok::l_brace) &&
Line.Level == 0 &&
5367 (
Line.startsWith(tok::kw_enum) ||
5368 Line.startsWith(tok::kw_const, tok::kw_enum) ||
5369 Line.startsWith(tok::kw_export, tok::kw_enum) ||
5370 Line.startsWith(tok::kw_export, tok::kw_const, tok::kw_enum))) {
5375 if (Right.is(tok::r_brace) && Left.is(tok::l_brace) && Left.Previous &&
5376 Left.Previous->is(TT_FatArrow)) {
5378 switch (Style.AllowShortLambdasOnASingleLine) {
5384 return !Left.Children.empty();
5388 return (Left.NestingLevel == 0 &&
Line.Level == 0) &&
5389 !Left.Children.empty();
5391 llvm_unreachable(
"Unknown FormatStyle::ShortLambdaStyle enum");
5394 if (Right.is(tok::r_brace) && Left.is(tok::l_brace) &&
5395 !Left.Children.empty()) {
5399 (Left.NestingLevel == 0 &&
Line.Level == 0 &&
5400 Style.AllowShortFunctionsOnASingleLine &
5404 if (Right.is(tok::plus) && Left.is(tok::string_literal) && Right.Next &&
5405 Right.Next->is(tok::string_literal)) {
5408 }
else if (Style.isVerilog()) {
5410 if (Left.is(TT_VerilogAssignComma))
5413 if (Left.is(TT_VerilogTypeComma))
5417 if (Style.VerilogBreakBetweenInstancePorts &&
5418 (Left.is(TT_VerilogInstancePortComma) ||
5419 (Left.is(tok::r_paren) && Keywords.isVerilogIdentifier(Right) &&
5420 Left.MatchingParen &&
5421 Left.MatchingParen->is(TT_VerilogInstancePortLParen)))) {
5426 if (!Keywords.isVerilogBegin(Right) && Keywords.isVerilogEndOfLabel(Left))
5428 }
else if (Style.BreakAdjacentStringLiterals &&
5429 (
IsCpp || Style.isProto() ||
5431 if (Left.isStringLiteral() && Right.isStringLiteral())
5436 if (Style.isJson()) {
5440 if (Left.is(TT_DictLiteral) && Left.is(tok::l_brace))
5443 if ((Left.is(TT_ArrayInitializerLSquare) && Left.is(tok::l_square) &&
5444 Right.isNot(tok::r_square)) ||
5445 Left.is(tok::comma)) {
5446 if (Right.is(tok::l_brace))
5450 for (
const auto *Tok = &Right; Tok; Tok = Tok->Next) {
5451 if (Tok->isOneOf(tok::l_brace, tok::l_square))
5453 if (Tok->isOneOf(tok::r_brace, tok::r_square))
5456 return Style.BreakArrays;
5459 if (Style.isTableGen()) {
5463 if (Left.is(TT_TableGenCondOperatorComma))
5467 if (
Line.startsWith(tok::kw_asm) && Right.is(TT_InlineASMColon) &&
5477 const FormatToken *BeforeClosingBrace =
nullptr;
5478 if ((Left.isOneOf(tok::l_brace, TT_ArrayInitializerLSquare) ||
5479 (Style.isJavaScript() && Left.is(tok::l_paren))) &&
5480 Left.isNot(
BK_Block) && Left.MatchingParen) {
5481 BeforeClosingBrace = Left.MatchingParen->Previous;
5482 }
else if (Right.MatchingParen &&
5483 (Right.MatchingParen->isOneOf(tok::l_brace,
5484 TT_ArrayInitializerLSquare) ||
5485 (Style.isJavaScript() &&
5486 Right.MatchingParen->is(tok::l_paren)))) {
5487 BeforeClosingBrace = &Left;
5489 if (BeforeClosingBrace && (BeforeClosingBrace->is(tok::comma) ||
5490 BeforeClosingBrace->isTrailingComment())) {
5495 if (Right.is(tok::comment)) {
5496 return Left.isNot(
BK_BracedInit) && Left.isNot(TT_CtorInitializerColon) &&
5497 (Right.NewlinesBefore > 0 && Right.HasUnescapedNewline);
5499 if (Left.isTrailingComment())
5501 if (Left.IsUnterminatedLiteral)
5506 if (Right.is(tok::lessless) && Right.Next &&
5507 Right.Next->is(tok::string_literal) && Left.is(tok::string_literal) &&
5508 Left.TokenText.ends_with(
"\\n\"")) {
5511 if (Right.is(TT_RequiresClause)) {
5512 switch (Style.RequiresClausePosition) {
5521 if (Left.ClosesTemplateDeclaration && Left.MatchingParen &&
5522 Left.MatchingParen->NestingLevel == 0) {
5526 if (Right.is(tok::kw_concept))
5530 Right.NewlinesBefore > 0);
5532 if (Left.ClosesRequiresClause && Right.isNot(tok::semi)) {
5533 switch (Style.RequiresClausePosition) {
5543 (Left.is(TT_CtorInitializerComma) ||
5544 Right.is(TT_CtorInitializerColon))) {
5549 Left.isOneOf(TT_CtorInitializerColon, TT_CtorInitializerComma)) {
5555 Right.isOneOf(TT_CtorInitializerComma, TT_CtorInitializerColon)) {
5561 Right.is(TT_CtorInitializerColon)) {
5566 Left.is(TT_CtorInitializerColon)) {
5572 Right.is(TT_InheritanceComma)) {
5576 Left.is(TT_InheritanceComma)) {
5579 if (Right.is(tok::string_literal) && Right.TokenText.starts_with(
"R\"")) {
5583 return Right.IsMultiline && Right.NewlinesBefore > 0;
5585 if ((Left.is(tok::l_brace) || (Left.is(tok::less) && Left.Previous &&
5586 Left.Previous->is(tok::equal))) &&
5592 if (Right.is(TT_InlineASMBrace))
5593 return Right.HasUnescapedNewline;
5596 auto *FirstNonComment =
Line.getFirstNonComment();
5599 FirstNonComment->isOneOf(Keywords.kw_internal, tok::kw_public,
5600 tok::kw_private, tok::kw_protected);
5602 if (Style.BraceWrapping.AfterEnum) {
5603 if (
Line.startsWith(tok::kw_enum) ||
5604 Line.startsWith(tok::kw_typedef, tok::kw_enum)) {
5609 FirstNonComment->Next->is(tok::kw_enum)) {
5615 if (Style.BraceWrapping.AfterClass &&
5617 FirstNonComment->Next->is(Keywords.kw_interface)) ||
5618 Line.startsWith(Keywords.kw_interface))) {
5623 if (Right.isNot(TT_FunctionLBrace)) {
5624 return (
Line.startsWith(tok::kw_class) &&
5625 Style.BraceWrapping.AfterClass) ||
5626 (
Line.startsWith(tok::kw_struct) &&
5627 Style.BraceWrapping.AfterStruct);
5631 if (Left.is(TT_ObjCBlockLBrace) &&
5637 if (Left.isOneOf(TT_AttributeRParen, TT_AttributeMacro) &&
5638 Right.is(TT_ObjCDecl)) {
5642 if (Left.is(TT_LambdaLBrace)) {
5650 (!Left.Children.empty() &&
5656 if (Style.BraceWrapping.BeforeLambdaBody && Right.is(TT_LambdaLBrace) &&
5657 (Left.isPointerOrReference() || Left.is(TT_TemplateCloser))) {
5663 Left.is(TT_LeadingJavaAnnotation) &&
5664 Right.isNot(TT_LeadingJavaAnnotation) && Right.isNot(tok::l_paren) &&
5665 (
Line.Last->is(tok::l_brace) || Style.BreakAfterJavaFieldAnnotations)) {
5669 if (Right.is(TT_ProtoExtensionLSquare))
5699 if (Style.isProto() && Right.is(TT_SelectorName) &&
5700 Right.isNot(tok::r_square) && Right.Next) {
5703 if (Left.is(tok::at))
5709 FormatToken *LBrace = Right.Next;
5710 if (LBrace && LBrace->is(tok::colon)) {
5711 LBrace = LBrace->Next;
5712 if (LBrace && LBrace->is(tok::at)) {
5713 LBrace = LBrace->Next;
5715 LBrace = LBrace->Next;
5727 ((LBrace->is(tok::l_brace) &&
5728 (LBrace->is(TT_DictLiteral) ||
5729 (LBrace->Next && LBrace->Next->is(tok::r_brace)))) ||
5730 LBrace->is(TT_ArrayInitializerLSquare) || LBrace->is(tok::less))) {
5737 if (Left.ParameterCount == 0)
5752 if (Left.isOneOf(tok::r_brace, tok::greater, tok::r_square))
5759bool TokenAnnotator::canBreakBefore(
const AnnotatedLine &
Line,
5760 const FormatToken &Right)
const {
5761 const FormatToken &Left = *Right.Previous;
5763 if (Style.isCSharp()) {
5764 if (Left.isOneOf(TT_CSharpNamedArgumentColon, TT_AttributeColon) ||
5765 Right.isOneOf(TT_CSharpNamedArgumentColon, TT_AttributeColon)) {
5769 if (
Line.First->is(TT_CSharpGenericTypeConstraint))
5770 return Left.is(TT_CSharpGenericTypeConstraintComma);
5772 if (Right.is(TT_CSharpNullable))
5775 if (Left.isOneOf(Keywords.kw_throws, Keywords.kw_extends,
5776 Keywords.kw_implements)) {
5779 if (Right.isOneOf(Keywords.kw_throws, Keywords.kw_extends,
5780 Keywords.kw_implements)) {
5783 }
else if (Style.isJavaScript()) {
5784 const FormatToken *NonComment = Right.getPreviousNonComment();
5786 NonComment->isOneOf(
5787 tok::kw_return, Keywords.kw_yield, tok::kw_continue, tok::kw_break,
5788 tok::kw_throw, Keywords.kw_interface, Keywords.kw_type,
5789 tok::kw_static, tok::kw_public, tok::kw_private, tok::kw_protected,
5790 Keywords.kw_readonly, Keywords.kw_override, Keywords.kw_abstract,
5791 Keywords.kw_get, Keywords.kw_set, Keywords.kw_async,
5792 Keywords.kw_await)) {
5795 if (Right.NestingLevel == 0 &&
5796 (Left.Tok.getIdentifierInfo() ||
5797 Left.isOneOf(tok::r_square, tok::r_paren)) &&
5798 Right.isOneOf(tok::l_square, tok::l_paren)) {
5801 if (NonComment && NonComment->is(tok::identifier) &&
5802 NonComment->TokenText ==
"asserts") {
5805 if (Left.is(TT_FatArrow) && Right.is(tok::l_brace))
5807 if (Left.is(TT_JsTypeColon))
5810 if (Left.is(tok::exclaim) && Right.is(tok::colon))
5815 if (Right.is(Keywords.kw_is)) {
5816 const FormatToken *Next = Right.getNextNonComment();
5824 if (!Next || Next->isNot(tok::colon))
5827 if (Left.is(Keywords.kw_in))
5829 if (Right.is(Keywords.kw_in))
5831 if (Right.is(Keywords.kw_as))
5833 if (Right.isOneOf(Keywords.kw_extends, Keywords.kw_infer)) {
5839 if (Left.is(Keywords.kw_as))
5841 if (Left.is(TT_NonNullAssertion))
5843 if (Left.is(Keywords.kw_declare) &&
5844 Right.isOneOf(Keywords.kw_module, tok::kw_namespace,
5845 Keywords.kw_function, tok::kw_class, tok::kw_enum,
5846 Keywords.kw_interface, Keywords.kw_type, Keywords.kw_var,
5847 Keywords.kw_let, tok::kw_const)) {
5852 if (Left.isOneOf(Keywords.kw_module, tok::kw_namespace) &&
5853 Right.isOneOf(tok::identifier, tok::string_literal)) {
5856 if (Right.is(TT_TemplateString) && Right.closesScope())
5860 if (Left.is(tok::identifier) && Right.is(TT_TemplateString))
5862 if (Left.is(TT_TemplateString) && Left.opensScope())
5864 }
else if (Style.isTableGen()) {
5866 if (Keywords.isTableGenDefinition(Left))
5869 if (Right.is(tok::l_paren)) {
5870 return !Left.isOneOf(TT_TableGenBangOperator, TT_TableGenCondOperator,
5874 if (Left.is(TT_TableGenValueSuffix))
5877 if (Left.is(tok::hash) || Right.is(tok::hash))
5881 if (Left.is(tok::at))
5883 if (Left.Tok.getObjCKeywordID() == tok::objc_interface)
5885 if (Left.isOneOf(TT_JavaAnnotation, TT_LeadingJavaAnnotation))
5886 return Right.isNot(tok::l_paren);
5887 if (Right.is(TT_PointerOrReference)) {
5888 return Line.IsMultiVariableDeclStmt ||
5889 (getTokenPointerOrReferenceAlignment(Right) ==
5891 (!Right.Next || Right.Next->isNot(TT_FunctionDeclarationName)));
5893 if (Right.isOneOf(TT_StartOfName, TT_FunctionDeclarationName) ||
5894 Right.is(tok::kw_operator)) {
5897 if (Left.is(TT_PointerOrReference))
5899 if (Right.isTrailingComment()) {
5906 (Left.is(TT_CtorInitializerColon) && Right.NewlinesBefore > 0 &&
5909 if (Left.is(tok::question) && Right.is(tok::colon))
5911 if (Right.is(TT_ConditionalExpr) || Right.is(tok::question))
5912 return Style.BreakBeforeTernaryOperators;
5913 if (Left.is(TT_ConditionalExpr) || Left.is(tok::question))
5914 return !Style.BreakBeforeTernaryOperators;
5915 if (Left.is(TT_InheritanceColon))
5917 if (Right.is(TT_InheritanceColon))
5919 if (Right.is(TT_ObjCMethodExpr) && Right.isNot(tok::r_square) &&
5920 Left.isNot(TT_SelectorName)) {
5924 if (Right.is(tok::colon) &&
5925 !Right.isOneOf(TT_CtorInitializerColon, TT_InlineASMColon)) {
5928 if (Left.is(tok::colon) && Left.isOneOf(TT_DictLiteral, TT_ObjCMethodExpr)) {
5929 if (Style.isProto()) {
5930 if (!Style.AlwaysBreakBeforeMultilineStrings && Right.isStringLiteral())
5956 if (((Right.is(tok::l_brace) || Right.is(tok::less)) &&
5957 Right.is(TT_DictLiteral)) ||
5958 Right.is(TT_ArrayInitializerLSquare)) {
5964 if (Right.is(tok::r_square) && Right.MatchingParen &&
5965 Right.MatchingParen->is(TT_ProtoExtensionLSquare)) {
5968 if (Right.is(TT_SelectorName) || (Right.is(tok::identifier) && Right.Next &&
5969 Right.Next->is(TT_ObjCMethodExpr))) {
5970 return Left.isNot(tok::period);
5974 if (Right.is(tok::kw_concept))
5976 if (Right.is(TT_RequiresClause))
5978 if (Left.ClosesTemplateDeclaration) {
5980 Right.NewlinesBefore > 0;
5982 if (Left.is(TT_FunctionAnnotationRParen))
5984 if (Left.ClosesRequiresClause)
5986 if (Right.isOneOf(TT_RangeBasedForLoopColon, TT_OverloadedOperatorLParen,
5987 TT_OverloadedOperator)) {
5990 if (Left.is(TT_RangeBasedForLoopColon))
5992 if (Right.is(TT_RangeBasedForLoopColon))
5994 if (Left.is(TT_TemplateCloser) && Right.is(TT_TemplateOpener))
5996 if ((Left.is(tok::greater) && Right.is(tok::greater)) ||
5997 (Left.is(tok::less) && Right.is(tok::less))) {
6000 if (Right.is(TT_BinaryOperator) &&
6006 if (Left.isOneOf(TT_TemplateCloser, TT_UnaryOperator) ||
6007 Left.is(tok::kw_operator)) {
6010 if (Left.is(tok::equal) && !Right.isOneOf(tok::kw_default, tok::kw_delete) &&
6014 if (Left.is(tok::equal) && Right.is(tok::l_brace) &&
6015 !Style.Cpp11BracedListStyle) {
6018 if (Left.is(TT_AttributeLParen) ||
6019 (Left.is(tok::l_paren) && Left.is(TT_TypeDeclarationParen))) {
6022 if (Left.is(tok::l_paren) && Left.Previous &&
6023 (Left.Previous->isOneOf(TT_BinaryOperator, TT_CastRParen))) {
6026 if (Right.is(TT_ImplicitStringLiteral))
6029 if (Right.is(TT_TemplateCloser))
6031 if (Right.is(tok::r_square) && Right.MatchingParen &&
6032 Right.MatchingParen->is(TT_LambdaLSquare)) {
6038 if (Right.is(tok::r_brace)) {
6039 return Right.MatchingParen && (Right.MatchingParen->is(
BK_Block) ||
6040 (Right.isBlockIndentedInitRBrace(Style)));
6044 if (Right.is(tok::r_paren)) {
6046 !Right.MatchingParen) {
6049 auto Next = Right.Next;
6050 if (Next && Next->is(tok::r_paren))
6052 if (Next && Next->is(tok::l_paren))
6054 const FormatToken *
Previous = Right.MatchingParen->Previous;
6060 if (Left.is(TT_TrailingAnnotation)) {
6061 return !Right.isOneOf(tok::l_brace, tok::semi, tok::equal, tok::l_paren,
6062 tok::less, tok::coloncolon);
6065 if (Right.isAttribute())
6068 if (Right.is(tok::l_square) && Right.is(TT_AttributeSquare))
6069 return Left.isNot(TT_AttributeSquare);
6071 if (Left.is(tok::identifier) && Right.is(tok::string_literal))
6074 if (Right.is(tok::identifier) && Right.Next && Right.Next->is(TT_DictLiteral))
6077 if (Left.is(TT_CtorInitializerColon)) {
6079 (!Right.isTrailingComment() || Right.NewlinesBefore > 0);
6081 if (Right.is(TT_CtorInitializerColon))
6083 if (Left.is(TT_CtorInitializerComma) &&
6087 if (Right.is(TT_CtorInitializerComma) &&
6091 if (Left.is(TT_InheritanceComma) &&
6095 if (Right.is(TT_InheritanceComma) &&
6099 if (Left.is(TT_ArrayInitializerLSquare))
6101 if (Right.is(tok::kw_typename) && Left.isNot(tok::kw_const))
6103 if ((Left.isBinaryOperator() || Left.is(TT_BinaryOperator)) &&
6104 !Left.isOneOf(tok::arrowstar, tok::lessless) &&
6110 if ((Left.is(TT_AttributeSquare) && Right.is(tok::l_square)) ||
6111 (Left.is(tok::r_square) && Right.is(TT_AttributeSquare))) {
6115 auto ShortLambdaOption = Style.AllowShortLambdasOnASingleLine;
6116 if (Style.BraceWrapping.BeforeLambdaBody && Right.is(TT_LambdaLBrace)) {
6123 if (Right.is(tok::kw_noexcept) && Right.is(TT_TrailingAnnotation)) {
6124 switch (Style.AllowBreakBeforeNoexceptSpecifier) {
6130 return Right.Next && Right.Next->is(tok::l_paren);
6134 return Left.isOneOf(tok::comma, tok::coloncolon, tok::semi, tok::l_brace,
6135 tok::kw_class, tok::kw_struct, tok::comment) ||
6136 Right.isMemberAccess() ||
6137 Right.isOneOf(TT_TrailingReturnArrow, tok::lessless, tok::colon,
6138 tok::l_square, tok::at) ||
6139 (Left.is(tok::r_paren) &&
6140 Right.isOneOf(tok::identifier, tok::kw_const)) ||
6141 (Left.is(tok::l_paren) && Right.isNot(tok::r_paren)) ||
6142 (Left.is(TT_TemplateOpener) && Right.isNot(TT_TemplateCloser));
6145void TokenAnnotator::printDebugInfo(
const AnnotatedLine &
Line)
const {
6146 llvm::errs() <<
"AnnotatedTokens(L=" <<
Line.Level <<
", P=" <<
Line.PPLevel
6147 <<
", T=" <<
Line.Type <<
", C=" <<
Line.IsContinuation
6149 const FormatToken *Tok =
Line.First;
6151 llvm::errs() <<
" M=" << Tok->MustBreakBefore
6152 <<
" C=" << Tok->CanBreakBefore
6154 <<
" S=" << Tok->SpacesRequiredBefore
6155 <<
" F=" << Tok->Finalized <<
" B=" << Tok->BlockParameterCount
6156 <<
" BK=" << Tok->getBlockKind() <<
" P=" << Tok->SplitPenalty
6157 <<
" Name=" << Tok->Tok.getName() <<
" L=" << Tok->TotalLength
6158 <<
" PPK=" << Tok->getPackingKind() <<
" FakeLParens=";
6160 llvm::errs() << LParen <<
"/";
6161 llvm::errs() <<
" FakeRParens=" << Tok->FakeRParens;
6162 llvm::errs() <<
" II=" << Tok->Tok.getIdentifierInfo();
6163 llvm::errs() <<
" Text='" << Tok->TokenText <<
"'\n";
6165 assert(Tok ==
Line.Last);
6168 llvm::errs() <<
"----\n";
6172TokenAnnotator::getTokenReferenceAlignment(
const FormatToken &Reference)
const {
6173 assert(
Reference.isOneOf(tok::amp, tok::ampamp));
6174 switch (Style.ReferenceAlignment) {
6176 return Style.PointerAlignment;
6185 return Style.PointerAlignment;
6189TokenAnnotator::getTokenPointerOrReferenceAlignment(
6190 const FormatToken &PointerOrReference)
const {
6191 if (PointerOrReference.isOneOf(tok::amp, tok::ampamp)) {
6192 switch (Style.ReferenceAlignment) {
6194 return Style.PointerAlignment;
6203 assert(PointerOrReference.is(tok::star));
6204 return Style.PointerAlignment;
bool ColonIsObjCMethodExpr
enum clang::format::@1245::AnnotatingParser::Context::@328 ContextType
FormatToken * FirstStartOfName
bool InCpp11AttributeSpecifier
unsigned LongestObjCSelectorName
bool VerilogAssignmentFound
bool InCSharpAttributeSpecifier
tok::TokenKind ContextKind
FormatToken * FirstObjCSelectorName
bool VerilogMayBeConcatenation
This file implements a token annotator, i.e.
#define TRANSFORM_TYPE_TRAIT_DEF(Enum, _)
Parser - This implements a parser for the C family of languages.
IdentifierInfo * getIdentifierInfo() const
bool Ret(InterpState &S, CodePtr &PC, APValue &Result)
TokenKind
Provides a simple uniform namespace for tokens from all C languages.
The JSON file list parser is used to communicate input to InstallAPI.
@ 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...