19#include "llvm/ADT/SmallPtrSet.h"
20#include "llvm/Support/Debug.h"
22#define DEBUG_TYPE "format-token-annotator"
43static bool startsWithInitStatement(
const AnnotatedLine &
Line) {
44 return Line.startsWith(tok::kw_for) ||
Line.startsWith(tok::kw_if) ||
45 Line.startsWith(tok::kw_switch);
59static bool canBeObjCSelectorComponent(
const FormatToken &Tok) {
60 return Tok.Tok.getIdentifierInfo();
66static bool isLambdaParameterList(
const FormatToken *Left) {
68 if (
Left->Previous &&
Left->Previous->is(tok::greater) &&
69 Left->Previous->MatchingParen &&
70 Left->Previous->MatchingParen->is(TT_TemplateOpener)) {
71 Left =
Left->Previous->MatchingParen;
75 return Left->Previous &&
Left->Previous->is(tok::r_square) &&
76 Left->Previous->MatchingParen &&
77 Left->Previous->MatchingParen->is(TT_LambdaLSquare);
82static bool isKeywordWithCondition(
const FormatToken &Tok) {
83 return Tok.isOneOf(tok::kw_if, tok::kw_for, tok::kw_while, tok::kw_switch,
84 tok::kw_constexpr, tok::kw_catch);
88static bool isCppAttribute(
bool IsCpp,
const FormatToken &Tok) {
89 if (!IsCpp || !Tok.startsSequence(tok::l_square, tok::l_square))
92 if (Tok.Previous && Tok.Previous->is(tok::at))
94 const FormatToken *AttrTok = Tok.Next->Next;
99 if (AttrTok->startsSequence(tok::kw_using, tok::identifier, tok::colon))
101 if (AttrTok->isNot(tok::identifier))
103 while (AttrTok && !AttrTok->startsSequence(tok::r_square, tok::r_square)) {
107 if (AttrTok->is(tok::colon) ||
108 AttrTok->startsSequence(tok::identifier, tok::identifier) ||
109 AttrTok->startsSequence(tok::r_paren, tok::identifier)) {
112 if (AttrTok->is(tok::ellipsis))
114 AttrTok = AttrTok->Next;
116 return AttrTok && AttrTok->startsSequence(tok::r_square, tok::r_square);
124class AnnotatingParser {
126 AnnotatingParser(
const FormatStyle &Style, AnnotatedLine &Line,
127 const AdditionalKeywords &Keywords,
128 SmallVector<ScopeType> &Scopes)
129 : Style(Style), Line(Line), CurrentToken(Line.
First), AutoFound(
false),
131 Keywords(Keywords), Scopes(Scopes), TemplateDeclarationDepth(0) {
132 assert(IsCpp == LangOpts.CXXOperatorNames);
133 Contexts.push_back(Context(tok::unknown, 1,
false));
134 resetTokenMetadata();
138 ScopeType getScopeType(
const FormatToken &Token)
const {
139 switch (Token.getType()) {
141 case TT_StructLBrace:
144 case TT_CompoundRequirementLBrace:
155 auto *
Left = CurrentToken->Previous;
159 if (NonTemplateLess.count(Left) > 0)
162 const auto *BeforeLess =
Left->Previous;
165 if (BeforeLess->Tok.isLiteral())
167 if (BeforeLess->is(tok::r_brace))
169 if (BeforeLess->is(tok::r_paren) && Contexts.size() > 1 &&
170 !(BeforeLess->MatchingParen &&
171 BeforeLess->MatchingParen->is(TT_OverloadedOperatorLParen))) {
174 if (BeforeLess->is(tok::kw_operator) && CurrentToken->is(tok::l_paren))
178 Left->ParentBracket = Contexts.back().ContextKind;
179 ScopedContextCreator ContextCreator(*
this, tok::less, 12);
180 Contexts.back().IsExpression =
false;
184 if (BeforeLess && BeforeLess->isNot(tok::kw_template))
185 Contexts.back().ContextType = Context::TemplateArgument;
188 CurrentToken->is(tok::question)) {
192 for (
bool SeenTernaryOperator =
false, MaybeAngles =
true; CurrentToken;) {
193 const bool InExpr = Contexts[Contexts.size() - 2].IsExpression;
194 if (CurrentToken->is(tok::greater)) {
195 const auto *Next = CurrentToken->Next;
196 if (CurrentToken->isNot(TT_TemplateCloser)) {
203 if (Next && Next->is(tok::greater) &&
204 Left->ParentBracket != tok::less &&
205 CurrentToken->getStartOfNonWhitespace() ==
206 Next->getStartOfNonWhitespace().getLocWithOffset(-1)) {
209 if (InExpr && SeenTernaryOperator &&
210 (!Next || !Next->isOneOf(tok::l_paren, tok::l_brace))) {
216 Left->MatchingParen = CurrentToken;
217 CurrentToken->MatchingParen =
Left;
225 BeforeLess->isOneOf(TT_SelectorName, TT_DictLiteral))) {
226 CurrentToken->setType(TT_DictLiteral);
228 CurrentToken->setType(TT_TemplateCloser);
229 CurrentToken->Tok.setLength(1);
231 if (Next && Next->Tok.isLiteral())
236 if (BeforeLess && BeforeLess->is(TT_TemplateName)) {
240 if (CurrentToken->is(tok::question) &&
245 if (CurrentToken->isOneOf(tok::r_paren, tok::r_square, tok::r_brace))
247 const auto &Prev = *CurrentToken->Previous;
254 if (MaybeAngles && InExpr && !Line.startsWith(tok::kw_template) &&
255 Prev.is(TT_BinaryOperator)) {
256 const auto Precedence = Prev.getPrecedence();
260 if (Prev.isOneOf(tok::question, tok::colon) && !Style.isProto())
261 SeenTernaryOperator =
true;
262 updateParameterCount(Left, CurrentToken);
264 if (FormatToken *
Previous = CurrentToken->getPreviousNonComment()) {
265 if (CurrentToken->is(tok::colon) ||
266 (CurrentToken->isOneOf(tok::l_brace, tok::less) &&
272 if (Style.isTableGen()) {
273 if (CurrentToken->isOneOf(tok::comma, tok::equal)) {
280 if (!parseTableGenValue())
290 bool parseUntouchableParens() {
291 while (CurrentToken) {
292 CurrentToken->Finalized =
true;
293 switch (CurrentToken->Tok.getKind()) {
296 if (!parseUntouchableParens())
311 bool parseParens(
bool IsIf =
false) {
314 assert(CurrentToken->Previous &&
"Unknown previous token");
315 FormatToken &OpeningParen = *CurrentToken->Previous;
316 assert(OpeningParen.is(tok::l_paren));
317 FormatToken *PrevNonComment = OpeningParen.getPreviousNonComment();
318 OpeningParen.ParentBracket = Contexts.back().ContextKind;
319 ScopedContextCreator ContextCreator(*
this, tok::l_paren, 1);
322 Contexts.back().ColonIsForRangeExpr =
323 Contexts.size() == 2 && Contexts[0].ColonIsForRangeExpr;
325 if (OpeningParen.Previous &&
326 OpeningParen.Previous->is(TT_UntouchableMacroFunc)) {
327 OpeningParen.Finalized =
true;
328 return parseUntouchableParens();
331 bool StartsObjCMethodExpr =
false;
332 if (!Style.isVerilog()) {
333 if (FormatToken *MaybeSel = OpeningParen.Previous) {
335 if (MaybeSel->isObjCAtKeyword(tok::objc_selector) &&
336 MaybeSel->Previous && MaybeSel->Previous->is(tok::at)) {
337 StartsObjCMethodExpr =
true;
342 if (OpeningParen.is(TT_OverloadedOperatorLParen)) {
344 FormatToken *Prev = &OpeningParen;
345 while (Prev->isNot(tok::kw_operator)) {
346 Prev = Prev->Previous;
347 assert(Prev &&
"Expect a kw_operator prior to the OperatorLParen!");
353 bool OperatorCalledAsMemberFunction =
354 Prev->Previous && Prev->Previous->isOneOf(tok::period, tok::arrow);
355 Contexts.back().IsExpression = OperatorCalledAsMemberFunction;
356 }
else if (OpeningParen.is(TT_VerilogInstancePortLParen)) {
357 Contexts.back().IsExpression =
true;
358 Contexts.back().ContextType = Context::VerilogInstancePortList;
359 }
else if (Style.isJavaScript() &&
360 (Line.startsWith(Keywords.kw_type, tok::identifier) ||
361 Line.startsWith(tok::kw_export, Keywords.kw_type,
365 Contexts.back().IsExpression =
false;
366 }
else if (OpeningParen.Previous &&
367 (OpeningParen.Previous->isOneOf(
368 tok::kw_static_assert, tok::kw_noexcept, tok::kw_explicit,
369 tok::kw_while, tok::l_paren, tok::comma, TT_CastRParen,
370 TT_BinaryOperator) ||
371 OpeningParen.Previous->isIf())) {
373 Contexts.back().IsExpression =
true;
374 }
else if (Style.isJavaScript() && OpeningParen.Previous &&
375 (OpeningParen.Previous->is(Keywords.kw_function) ||
376 (OpeningParen.Previous->endsSequence(tok::identifier,
377 Keywords.kw_function)))) {
379 Contexts.back().IsExpression =
false;
380 }
else if (Style.isJavaScript() && OpeningParen.Previous &&
381 OpeningParen.Previous->is(TT_JsTypeColon)) {
383 Contexts.back().IsExpression =
false;
384 }
else if (isLambdaParameterList(&OpeningParen)) {
386 OpeningParen.setType(TT_LambdaDefinitionLParen);
387 Contexts.back().IsExpression =
false;
388 }
else if (OpeningParen.is(TT_RequiresExpressionLParen)) {
389 Contexts.back().IsExpression =
false;
390 }
else if (OpeningParen.Previous &&
391 OpeningParen.Previous->is(tok::kw__Generic)) {
392 Contexts.back().ContextType = Context::C11GenericSelection;
393 Contexts.back().IsExpression =
true;
394 }
else if (Line.InPPDirective &&
395 (!OpeningParen.Previous ||
396 OpeningParen.Previous->isNot(tok::identifier))) {
397 Contexts.back().IsExpression =
true;
398 }
else if (Contexts[Contexts.size() - 2].CaretFound) {
400 Contexts.back().IsExpression =
false;
401 }
else if (OpeningParen.Previous &&
402 OpeningParen.Previous->is(TT_ForEachMacro)) {
404 Contexts.back().ContextType = Context::ForEachMacro;
405 Contexts.back().IsExpression =
false;
406 }
else if (OpeningParen.Previous && OpeningParen.Previous->MatchingParen &&
407 OpeningParen.Previous->MatchingParen->isOneOf(
408 TT_ObjCBlockLParen, TT_FunctionTypeLParen)) {
409 Contexts.back().IsExpression =
false;
410 }
else if (!Line.MustBeDeclaration &&
411 (!Line.InPPDirective || (Line.InMacroBody && !Scopes.empty()))) {
413 OpeningParen.Previous &&
414 OpeningParen.Previous->isOneOf(tok::kw_for, tok::kw_catch);
415 Contexts.back().IsExpression = !IsForOrCatch;
418 if (Style.isTableGen()) {
419 if (FormatToken *Prev = OpeningParen.Previous) {
420 if (Prev->is(TT_TableGenCondOperator)) {
421 Contexts.back().IsTableGenCondOpe =
true;
422 Contexts.back().IsExpression =
true;
423 }
else if (Contexts.size() > 1 &&
424 Contexts[Contexts.size() - 2].IsTableGenBangOpe) {
429 Contexts.back().IsTableGenBangOpe =
true;
430 Contexts.back().IsExpression =
true;
433 if (!parseTableGenDAGArg())
435 return parseTableGenDAGArgAndList(&OpeningParen);
442 if (PrevNonComment && OpeningParen.is(TT_Unknown)) {
443 if (PrevNonComment->isAttribute()) {
444 OpeningParen.setType(TT_AttributeLParen);
445 }
else if (PrevNonComment->isOneOf(TT_TypenameMacro, tok::kw_decltype,
448#include
"clang/Basic/TransformTypeTraits.def"
450 OpeningParen.setType(TT_TypeDeclarationParen);
452 if (PrevNonComment->isOneOf(tok::kw_decltype, tok::kw_typeof))
453 Contexts.back().IsExpression =
true;
457 if (StartsObjCMethodExpr) {
458 Contexts.back().ColonIsObjCMethodExpr =
true;
459 OpeningParen.setType(TT_ObjCMethodExpr);
470 bool MightBeFunctionType = !Contexts[Contexts.size() - 2].IsExpression;
471 bool ProbablyFunctionType =
472 CurrentToken->isPointerOrReference() || CurrentToken->is(tok::caret);
473 bool HasMultipleLines =
false;
474 bool HasMultipleParametersOnALine =
false;
475 bool MightBeObjCForRangeLoop =
476 OpeningParen.Previous && OpeningParen.Previous->is(tok::kw_for);
477 FormatToken *PossibleObjCForInToken =
nullptr;
478 while (CurrentToken) {
479 const auto &Prev = *CurrentToken->Previous;
480 if (Prev.is(TT_PointerOrReference) &&
481 Prev.Previous->isOneOf(tok::l_paren, tok::coloncolon)) {
482 ProbablyFunctionType =
true;
484 if (CurrentToken->is(tok::comma))
485 MightBeFunctionType =
false;
486 if (Prev.is(TT_BinaryOperator))
487 Contexts.back().IsExpression =
true;
488 if (CurrentToken->is(tok::r_paren)) {
489 if (Prev.is(TT_PointerOrReference) && Prev.Previous == &OpeningParen)
490 MightBeFunctionType =
true;
491 if (OpeningParen.isNot(TT_CppCastLParen) && MightBeFunctionType &&
492 ProbablyFunctionType && CurrentToken->Next &&
493 (CurrentToken->Next->is(tok::l_paren) ||
494 (CurrentToken->Next->is(tok::l_square) &&
495 (Line.MustBeDeclaration ||
496 (PrevNonComment && PrevNonComment->isTypeName(LangOpts)))))) {
497 OpeningParen.setType(OpeningParen.Next->is(tok::caret)
499 : TT_FunctionTypeLParen);
501 OpeningParen.MatchingParen = CurrentToken;
502 CurrentToken->MatchingParen = &OpeningParen;
504 if (CurrentToken->Next && CurrentToken->Next->is(tok::l_brace) &&
505 OpeningParen.Previous && OpeningParen.Previous->is(tok::l_paren)) {
509 for (FormatToken *Tok = &OpeningParen; Tok != CurrentToken;
511 if (Tok->is(TT_BinaryOperator) && Tok->isPointerOrReference())
512 Tok->setType(TT_PointerOrReference);
516 if (StartsObjCMethodExpr) {
517 CurrentToken->setType(TT_ObjCMethodExpr);
518 if (Contexts.back().FirstObjCSelectorName) {
519 Contexts.back().FirstObjCSelectorName->LongestObjCSelectorName =
520 Contexts.back().LongestObjCSelectorName;
524 if (OpeningParen.is(TT_AttributeLParen))
525 CurrentToken->setType(TT_AttributeRParen);
526 if (OpeningParen.is(TT_TypeDeclarationParen))
527 CurrentToken->setType(TT_TypeDeclarationParen);
528 if (OpeningParen.Previous &&
529 OpeningParen.Previous->is(TT_JavaAnnotation)) {
530 CurrentToken->setType(TT_JavaAnnotation);
532 if (OpeningParen.Previous &&
533 OpeningParen.Previous->is(TT_LeadingJavaAnnotation)) {
534 CurrentToken->setType(TT_LeadingJavaAnnotation);
536 if (OpeningParen.Previous &&
537 OpeningParen.Previous->is(TT_AttributeSquare)) {
538 CurrentToken->setType(TT_AttributeSquare);
541 if (!HasMultipleLines)
543 else if (HasMultipleParametersOnALine)
551 if (CurrentToken->isOneOf(tok::r_square, tok::r_brace))
554 if (CurrentToken->is(tok::l_brace) && OpeningParen.is(TT_ObjCBlockLParen))
555 OpeningParen.setType(TT_Unknown);
556 if (CurrentToken->is(tok::comma) && CurrentToken->Next &&
557 !CurrentToken->Next->HasUnescapedNewline &&
558 !CurrentToken->Next->isTrailingComment()) {
559 HasMultipleParametersOnALine =
true;
561 bool ProbablyFunctionTypeLParen =
562 (CurrentToken->is(tok::l_paren) && CurrentToken->Next &&
563 CurrentToken->Next->isOneOf(tok::star, tok::amp, tok::caret));
564 if ((Prev.isOneOf(tok::kw_const, tok::kw_auto) ||
565 Prev.isTypeName(LangOpts)) &&
566 !(CurrentToken->is(tok::l_brace) ||
567 (CurrentToken->is(tok::l_paren) && !ProbablyFunctionTypeLParen))) {
568 Contexts.back().IsExpression =
false;
570 if (CurrentToken->isOneOf(tok::semi, tok::colon)) {
571 MightBeObjCForRangeLoop =
false;
572 if (PossibleObjCForInToken) {
573 PossibleObjCForInToken->setType(TT_Unknown);
574 PossibleObjCForInToken =
nullptr;
577 if (IsIf && CurrentToken->is(tok::semi)) {
578 for (
auto *Tok = OpeningParen.Next;
579 Tok != CurrentToken &&
580 !Tok->isOneOf(tok::equal, tok::l_paren, tok::l_brace);
582 if (Tok->isPointerOrReference())
583 Tok->setFinalizedType(TT_PointerOrReference);
586 if (MightBeObjCForRangeLoop && CurrentToken->is(Keywords.kw_in)) {
587 PossibleObjCForInToken = CurrentToken;
588 PossibleObjCForInToken->setType(TT_ObjCForIn);
592 if (CurrentToken->is(tok::comma))
593 Contexts.back().CanBeExpression =
true;
595 if (Style.isTableGen()) {
596 if (CurrentToken->is(tok::comma)) {
597 if (Contexts.back().IsTableGenCondOpe)
598 CurrentToken->setType(TT_TableGenCondOperatorComma);
600 }
else if (CurrentToken->is(tok::colon)) {
601 if (Contexts.back().IsTableGenCondOpe)
602 CurrentToken->setType(TT_TableGenCondOperatorColon);
606 if (!parseTableGenValue())
611 FormatToken *Tok = CurrentToken;
614 updateParameterCount(&OpeningParen, Tok);
615 if (CurrentToken && CurrentToken->HasUnescapedNewline)
616 HasMultipleLines =
true;
621 bool isCSharpAttributeSpecifier(
const FormatToken &Tok) {
622 if (!Style.isCSharp())
626 if (Tok.Previous && Tok.Previous->is(tok::identifier))
630 if (Tok.Previous && Tok.Previous->is(tok::r_square)) {
631 auto *MatchingParen = Tok.Previous->MatchingParen;
632 if (!MatchingParen || MatchingParen->is(TT_ArraySubscriptLSquare))
636 const FormatToken *AttrTok = Tok.Next;
641 if (AttrTok->is(tok::r_square))
645 while (AttrTok && AttrTok->isNot(tok::r_square))
646 AttrTok = AttrTok->Next;
652 AttrTok = AttrTok->Next;
657 if (AttrTok->isAccessSpecifierKeyword() ||
658 AttrTok->isOneOf(tok::comment, tok::kw_class, tok::kw_static,
659 tok::l_square, Keywords.kw_internal)) {
665 AttrTok->Next->startsSequence(tok::identifier, tok::l_paren)) {
680 FormatToken *
Left = CurrentToken->Previous;
681 Left->ParentBracket = Contexts.back().ContextKind;
682 FormatToken *
Parent =
Left->getPreviousNonComment();
687 bool CppArrayTemplates =
689 (Contexts.back().CanBeExpression || Contexts.back().IsExpression ||
690 Contexts.back().ContextType == Context::TemplateArgument);
692 const bool IsInnerSquare = Contexts.back().InCpp11AttributeSpecifier;
693 const bool IsCpp11AttributeSpecifier =
694 isCppAttribute(IsCpp, *Left) || IsInnerSquare;
697 bool IsCSharpAttributeSpecifier =
698 isCSharpAttributeSpecifier(*Left) ||
699 Contexts.back().InCSharpAttributeSpecifier;
701 bool InsideInlineASM = Line.startsWith(tok::kw_asm);
702 bool IsCppStructuredBinding =
Left->isCppStructuredBinding(IsCpp);
703 bool StartsObjCMethodExpr =
704 !IsCppStructuredBinding && !InsideInlineASM && !CppArrayTemplates &&
705 IsCpp && !IsCpp11AttributeSpecifier && !IsCSharpAttributeSpecifier &&
706 Contexts.back().CanBeExpression &&
Left->isNot(TT_LambdaLSquare) &&
707 !CurrentToken->isOneOf(tok::l_brace, tok::r_square) &&
709 Parent->isOneOf(tok::colon, tok::l_square, tok::l_paren,
710 tok::kw_return, tok::kw_throw) ||
711 Parent->isUnaryOperator() ||
713 Parent->isOneOf(TT_ObjCForIn, TT_CastRParen) ||
716 bool ColonFound =
false;
718 unsigned BindingIncrease = 1;
719 if (IsCppStructuredBinding) {
720 Left->setType(TT_StructuredBindingLSquare);
721 }
else if (
Left->is(TT_Unknown)) {
722 if (StartsObjCMethodExpr) {
723 Left->setType(TT_ObjCMethodExpr);
724 }
else if (InsideInlineASM) {
725 Left->setType(TT_InlineASMSymbolicNameLSquare);
726 }
else if (IsCpp11AttributeSpecifier) {
727 Left->setType(TT_AttributeSquare);
728 if (!IsInnerSquare &&
Left->Previous)
729 Left->Previous->EndsCppAttributeGroup =
false;
730 }
else if (Style.isJavaScript() &&
Parent &&
731 Contexts.back().ContextKind == tok::l_brace &&
732 Parent->isOneOf(tok::l_brace, tok::comma)) {
733 Left->setType(TT_JsComputedPropertyName);
734 }
else if (IsCpp && Contexts.back().ContextKind == tok::l_brace &&
736 Left->setType(TT_DesignatedInitializerLSquare);
737 }
else if (IsCSharpAttributeSpecifier) {
738 Left->setType(TT_AttributeSquare);
739 }
else if (CurrentToken->is(tok::r_square) &&
Parent &&
740 Parent->is(TT_TemplateCloser)) {
741 Left->setType(TT_ArraySubscriptLSquare);
742 }
else if (Style.isProto()) {
769 Left->setType(TT_ArrayInitializerLSquare);
770 if (!
Left->endsSequence(tok::l_square, tok::numeric_constant,
772 !
Left->endsSequence(tok::l_square, tok::numeric_constant,
774 !
Left->endsSequence(tok::l_square, tok::colon, TT_SelectorName)) {
775 Left->setType(TT_ProtoExtensionLSquare);
776 BindingIncrease = 10;
778 }
else if (!CppArrayTemplates &&
Parent &&
779 Parent->isOneOf(TT_BinaryOperator, TT_TemplateCloser, tok::at,
780 tok::comma, tok::l_paren, tok::l_square,
781 tok::question, tok::colon, tok::kw_return,
784 Left->setType(TT_ArrayInitializerLSquare);
786 BindingIncrease = 10;
787 Left->setType(TT_ArraySubscriptLSquare);
791 ScopedContextCreator ContextCreator(*
this, tok::l_square, BindingIncrease);
792 Contexts.back().IsExpression =
true;
793 if (Style.isJavaScript() &&
Parent &&
Parent->is(TT_JsTypeColon))
794 Contexts.back().IsExpression =
false;
796 Contexts.back().ColonIsObjCMethodExpr = StartsObjCMethodExpr;
797 Contexts.back().InCpp11AttributeSpecifier = IsCpp11AttributeSpecifier;
798 Contexts.back().InCSharpAttributeSpecifier = IsCSharpAttributeSpecifier;
800 while (CurrentToken) {
801 if (CurrentToken->is(tok::r_square)) {
802 if (IsCpp11AttributeSpecifier) {
803 CurrentToken->setType(TT_AttributeSquare);
805 CurrentToken->EndsCppAttributeGroup =
true;
807 if (IsCSharpAttributeSpecifier) {
808 CurrentToken->setType(TT_AttributeSquare);
809 }
else if (((CurrentToken->Next &&
810 CurrentToken->Next->is(tok::l_paren)) ||
811 (CurrentToken->Previous &&
812 CurrentToken->Previous->Previous == Left)) &&
813 Left->is(TT_ObjCMethodExpr)) {
818 StartsObjCMethodExpr =
false;
819 Left->setType(TT_Unknown);
821 if (StartsObjCMethodExpr && CurrentToken->Previous != Left) {
822 CurrentToken->setType(TT_ObjCMethodExpr);
825 if (!ColonFound && CurrentToken->Previous &&
826 CurrentToken->Previous->is(TT_Unknown) &&
827 canBeObjCSelectorComponent(*CurrentToken->Previous)) {
828 CurrentToken->Previous->setType(TT_SelectorName);
834 Parent->overwriteFixedType(TT_BinaryOperator);
837 if (CurrentToken->is(TT_ObjCMethodExpr) && CurrentToken->Next &&
838 CurrentToken->Next->is(TT_LambdaArrow)) {
839 CurrentToken->Next->overwriteFixedType(TT_Unknown);
841 Left->MatchingParen = CurrentToken;
842 CurrentToken->MatchingParen =
Left;
847 if (!Contexts.back().FirstObjCSelectorName) {
848 FormatToken *
Previous = CurrentToken->getPreviousNonComment();
850 Previous->ObjCSelectorNameParts = 1;
851 Contexts.back().FirstObjCSelectorName =
Previous;
854 Left->ParameterCount =
855 Contexts.back().FirstObjCSelectorName->ObjCSelectorNameParts;
857 if (Contexts.back().FirstObjCSelectorName) {
858 Contexts.back().FirstObjCSelectorName->LongestObjCSelectorName =
859 Contexts.back().LongestObjCSelectorName;
860 if (
Left->BlockParameterCount > 1)
861 Contexts.back().FirstObjCSelectorName->LongestObjCSelectorName = 0;
863 if (Style.isTableGen() &&
Left->is(TT_TableGenListOpener))
864 CurrentToken->setType(TT_TableGenListCloser);
868 if (CurrentToken->isOneOf(tok::r_paren, tok::r_brace))
870 if (CurrentToken->is(tok::colon)) {
871 if (IsCpp11AttributeSpecifier &&
872 CurrentToken->endsSequence(tok::colon, tok::identifier,
876 CurrentToken->setType(TT_AttributeColon);
877 }
else if (!Style.isVerilog() && !Line.InPragmaDirective &&
878 Left->isOneOf(TT_ArraySubscriptLSquare,
879 TT_DesignatedInitializerLSquare)) {
880 Left->setType(TT_ObjCMethodExpr);
881 StartsObjCMethodExpr =
true;
882 Contexts.back().ColonIsObjCMethodExpr =
true;
885 Parent->setType(TT_CastRParen);
890 if (CurrentToken->is(tok::comma) &&
Left->is(TT_ObjCMethodExpr) &&
892 Left->setType(TT_ArrayInitializerLSquare);
894 FormatToken *Tok = CurrentToken;
895 if (Style.isTableGen()) {
896 if (CurrentToken->isOneOf(tok::comma, tok::minus, tok::ellipsis)) {
902 if (!parseTableGenValue())
905 updateParameterCount(Left, Tok);
910 updateParameterCount(Left, Tok);
915 void skipToNextNonComment() {
917 while (CurrentToken && CurrentToken->is(tok::comment))
926 bool parseTableGenValue(
bool ParseNameMode =
false) {
929 while (CurrentToken->is(tok::comment))
931 if (!parseTableGenSimpleValue())
936 if (CurrentToken->is(tok::hash)) {
937 if (CurrentToken->Next &&
938 CurrentToken->Next->isOneOf(tok::colon, tok::semi, tok::l_brace)) {
941 CurrentToken->setType(TT_TableGenTrailingPasteOperator);
945 FormatToken *HashTok = CurrentToken;
946 skipToNextNonComment();
947 HashTok->setType(TT_Unknown);
948 if (!parseTableGenValue(ParseNameMode))
953 if (ParseNameMode && CurrentToken->is(tok::l_brace))
956 if (CurrentToken->isOneOf(tok::l_brace, tok::l_square, tok::period)) {
957 CurrentToken->setType(TT_TableGenValueSuffix);
958 FormatToken *Suffix = CurrentToken;
959 skipToNextNonComment();
960 if (Suffix->is(tok::l_square))
961 return parseSquare();
962 if (Suffix->is(tok::l_brace)) {
963 Scopes.push_back(getScopeType(*Suffix));
973 bool tryToParseTableGenTokVar() {
976 if (CurrentToken->is(tok::identifier) &&
977 CurrentToken->TokenText.front() ==
'$') {
978 skipToNextNonComment();
986 bool parseTableGenDAGArg(
bool AlignColon =
false) {
987 if (tryToParseTableGenTokVar())
989 if (parseTableGenValue()) {
990 if (CurrentToken && CurrentToken->is(tok::colon)) {
992 CurrentToken->setType(TT_TableGenDAGArgListColonToAlign);
994 CurrentToken->setType(TT_TableGenDAGArgListColon);
995 skipToNextNonComment();
996 return tryToParseTableGenTokVar();
1006 bool isTableGenDAGArgBreakingOperator(
const FormatToken &Tok) {
1007 auto &Opes = Style.TableGenBreakingDAGArgOperators;
1012 if (Tok.isNot(tok::identifier) ||
1013 Tok.isOneOf(TT_TableGenBangOperator, TT_TableGenCondOperator)) {
1017 if (!Tok.Next || Tok.Next->is(tok::colon))
1019 return llvm::is_contained(Opes, Tok.TokenText.str());
1024 bool parseTableGenDAGArgAndList(FormatToken *Opener) {
1025 FormatToken *FirstTok = CurrentToken;
1026 if (!parseTableGenDAGArg())
1028 bool BreakInside =
false;
1032 if (isTableGenDAGArgBreakingOperator(*FirstTok)) {
1035 Opener->setType(TT_TableGenDAGArgOpenerToBreak);
1036 if (FirstTok->isOneOf(TT_TableGenBangOperator,
1037 TT_TableGenCondOperator)) {
1040 CurrentToken->Previous->setType(TT_TableGenDAGArgOperatorToBreak);
1041 }
else if (FirstTok->is(tok::identifier)) {
1043 FirstTok->setType(TT_TableGenDAGArgOperatorToBreak);
1045 FirstTok->setType(TT_TableGenDAGArgOperatorID);
1050 bool FirstDAGArgListElm =
true;
1051 while (CurrentToken) {
1052 if (!FirstDAGArgListElm && CurrentToken->is(tok::comma)) {
1053 CurrentToken->setType(BreakInside ? TT_TableGenDAGArgListCommaToBreak
1054 : TT_TableGenDAGArgListComma);
1055 skipToNextNonComment();
1057 if (CurrentToken && CurrentToken->is(tok::r_paren)) {
1058 CurrentToken->setType(TT_TableGenDAGArgCloser);
1059 Opener->MatchingParen = CurrentToken;
1060 CurrentToken->MatchingParen = Opener;
1061 skipToNextNonComment();
1064 if (!parseTableGenDAGArg(
1066 Style.AlignConsecutiveTableGenBreakingDAGArgColons.Enabled)) {
1069 FirstDAGArgListElm =
false;
1074 bool parseTableGenSimpleValue() {
1075 assert(Style.isTableGen());
1078 FormatToken *Tok = CurrentToken;
1079 skipToNextNonComment();
1081 if (Tok->isOneOf(tok::numeric_constant, tok::string_literal,
1082 TT_TableGenMultiLineString, tok::kw_true, tok::kw_false,
1083 tok::question, tok::kw_int)) {
1087 if (Tok->is(tok::l_brace)) {
1088 Scopes.push_back(getScopeType(*Tok));
1089 return parseBrace();
1092 if (Tok->is(tok::l_square)) {
1093 Tok->setType(TT_TableGenListOpener);
1096 if (Tok->is(tok::less)) {
1097 CurrentToken->setType(TT_TemplateOpener);
1098 return parseAngle();
1104 if (Tok->is(tok::l_paren)) {
1105 Tok->setType(TT_TableGenDAGArgOpener);
1106 return parseTableGenDAGArgAndList(Tok);
1109 if (Tok->is(TT_TableGenBangOperator)) {
1110 if (CurrentToken && CurrentToken->is(tok::less)) {
1111 CurrentToken->setType(TT_TemplateOpener);
1112 skipToNextNonComment();
1116 if (!CurrentToken || CurrentToken->isNot(tok::l_paren))
1118 skipToNextNonComment();
1120 Contexts.back().IsTableGenBangOpe =
true;
1121 bool Result = parseParens();
1122 Contexts.back().IsTableGenBangOpe =
false;
1126 if (Tok->is(TT_TableGenCondOperator)) {
1128 skipToNextNonComment();
1129 if (!Tok || Tok->isNot(tok::l_paren))
1131 bool Result = parseParens();
1137 if (Tok->is(tok::identifier)) {
1139 if (CurrentToken && CurrentToken->is(tok::less)) {
1140 CurrentToken->setType(TT_TemplateOpener);
1141 skipToNextNonComment();
1142 return parseAngle();
1150 bool couldBeInStructArrayInitializer()
const {
1151 if (Contexts.size() < 2)
1155 const auto End = std::next(Contexts.rbegin(), 2);
1156 auto Last = Contexts.rbegin();
1159 if (
Last->ContextKind == tok::l_brace)
1161 return Depth == 2 &&
Last->ContextKind != tok::l_brace;
1168 assert(CurrentToken->Previous);
1169 FormatToken &OpeningBrace = *CurrentToken->Previous;
1170 assert(OpeningBrace.is(tok::l_brace));
1171 OpeningBrace.ParentBracket = Contexts.back().ContextKind;
1173 if (Contexts.back().CaretFound)
1174 OpeningBrace.overwriteFixedType(TT_ObjCBlockLBrace);
1175 Contexts.back().CaretFound =
false;
1177 ScopedContextCreator ContextCreator(*
this, tok::l_brace, 1);
1178 Contexts.back().ColonIsDictLiteral =
true;
1180 Contexts.back().IsExpression =
true;
1181 if (Style.isJavaScript() && OpeningBrace.Previous &&
1182 OpeningBrace.Previous->is(TT_JsTypeColon)) {
1183 Contexts.back().IsExpression =
false;
1185 if (Style.isVerilog() &&
1186 (!OpeningBrace.getPreviousNonComment() ||
1187 OpeningBrace.getPreviousNonComment()->isNot(Keywords.kw_apostrophe))) {
1188 Contexts.back().VerilogMayBeConcatenation =
true;
1190 if (Style.isTableGen())
1191 Contexts.back().ColonIsDictLiteral =
false;
1193 unsigned CommaCount = 0;
1194 while (CurrentToken) {
1195 if (CurrentToken->is(tok::r_brace)) {
1196 assert(!Scopes.empty());
1197 assert(Scopes.back() == getScopeType(OpeningBrace));
1199 assert(OpeningBrace.Optional == CurrentToken->Optional);
1200 OpeningBrace.MatchingParen = CurrentToken;
1201 CurrentToken->MatchingParen = &OpeningBrace;
1203 if (OpeningBrace.ParentBracket == tok::l_brace &&
1204 couldBeInStructArrayInitializer() && CommaCount > 0) {
1205 Contexts.back().ContextType = Context::StructArrayInitializer;
1211 if (CurrentToken->isOneOf(tok::r_paren, tok::r_square))
1213 updateParameterCount(&OpeningBrace, CurrentToken);
1214 if (CurrentToken->isOneOf(tok::colon, tok::l_brace, tok::less)) {
1215 FormatToken *
Previous = CurrentToken->getPreviousNonComment();
1216 if (
Previous->is(TT_JsTypeOptionalQuestion))
1218 if ((CurrentToken->is(tok::colon) && !Style.isTableGen() &&
1219 (!Contexts.back().ColonIsDictLiteral || !IsCpp)) ||
1221 OpeningBrace.setType(TT_DictLiteral);
1222 if (
Previous->Tok.getIdentifierInfo() ||
1223 Previous->is(tok::string_literal)) {
1224 Previous->setType(TT_SelectorName);
1227 if (CurrentToken->is(tok::colon) && OpeningBrace.is(TT_Unknown) &&
1228 !Style.isTableGen()) {
1229 OpeningBrace.setType(TT_DictLiteral);
1230 }
else if (Style.isJavaScript()) {
1231 OpeningBrace.overwriteFixedType(TT_DictLiteral);
1234 if (CurrentToken->is(tok::comma)) {
1235 if (Style.isJavaScript())
1236 OpeningBrace.overwriteFixedType(TT_DictLiteral);
1239 if (!consumeToken())
1245 void updateParameterCount(FormatToken *Left, FormatToken *Current) {
1249 if (Current->is(tok::l_brace) && Current->is(
BK_Block))
1250 ++
Left->BlockParameterCount;
1251 if (Current->is(tok::comma)) {
1252 ++
Left->ParameterCount;
1254 Left->Role.reset(
new CommaSeparatedList(Style));
1255 Left->Role->CommaFound(Current);
1256 }
else if (
Left->ParameterCount == 0 && Current->isNot(tok::comment)) {
1257 Left->ParameterCount = 1;
1261 bool parseConditional() {
1262 while (CurrentToken) {
1263 if (CurrentToken->is(tok::colon) && CurrentToken->is(TT_Unknown)) {
1264 CurrentToken->setType(TT_ConditionalExpr);
1268 if (!consumeToken())
1274 bool parseTemplateDeclaration() {
1275 if (!CurrentToken || CurrentToken->isNot(tok::less))
1278 CurrentToken->setType(TT_TemplateOpener);
1281 TemplateDeclarationDepth++;
1282 const bool WellFormed = parseAngle();
1283 TemplateDeclarationDepth--;
1287 if (CurrentToken && TemplateDeclarationDepth == 0)
1288 CurrentToken->Previous->ClosesTemplateDeclaration =
true;
1293 bool consumeToken() {
1295 const auto *Prev = CurrentToken->getPreviousNonComment();
1296 if (Prev && Prev->is(tok::r_square) && Prev->is(TT_AttributeSquare) &&
1297 CurrentToken->isOneOf(tok::kw_if, tok::kw_switch, tok::kw_case,
1298 tok::kw_default, tok::kw_for, tok::kw_while) &&
1300 CurrentToken->MustBreakBefore =
true;
1303 FormatToken *Tok = CurrentToken;
1307 if (Tok->is(TT_VerilogTableItem))
1310 if (Tok->is(TT_TableGenMultiLineString))
1312 switch (
bool IsIf =
false; Tok->Tok.getKind()) {
1315 if (!Tok->Previous && Line.MustBeDeclaration)
1316 Tok->setType(TT_ObjCMethodSpecifier);
1323 if (Tok->isTypeFinalized())
1326 if (Style.isJavaScript()) {
1327 if (Contexts.back().ColonIsForRangeExpr ||
1328 (Contexts.size() == 1 &&
1329 !Line.First->isOneOf(tok::kw_enum, tok::kw_case)) ||
1330 Contexts.back().ContextKind == tok::l_paren ||
1331 Contexts.back().ContextKind == tok::l_square ||
1332 (!Contexts.back().IsExpression &&
1333 Contexts.back().ContextKind == tok::l_brace) ||
1334 (Contexts.size() == 1 &&
1335 Line.MustBeDeclaration)) {
1336 Contexts.back().IsExpression =
false;
1337 Tok->setType(TT_JsTypeColon);
1340 }
else if (Style.isCSharp()) {
1341 if (Contexts.back().InCSharpAttributeSpecifier) {
1342 Tok->setType(TT_AttributeColon);
1345 if (Contexts.back().ContextKind == tok::l_paren) {
1346 Tok->setType(TT_CSharpNamedArgumentColon);
1349 }
else if (Style.isVerilog() && Tok->isNot(TT_BinaryOperator)) {
1352 if (Keywords.isVerilogEnd(*Tok->Previous) ||
1353 Keywords.isVerilogBegin(*Tok->Previous)) {
1354 Tok->setType(TT_VerilogBlockLabelColon);
1355 }
else if (Contexts.back().ContextKind == tok::l_square) {
1356 Tok->setType(TT_BitFieldColon);
1357 }
else if (Contexts.back().ColonIsDictLiteral) {
1358 Tok->setType(TT_DictLiteral);
1359 }
else if (Contexts.size() == 1) {
1363 Tok->setType(TT_CaseLabelColon);
1364 if (Line.Level > 1 || (!Line.InPPDirective && Line.Level > 0))
1369 if (Line.First->isOneOf(Keywords.kw_module, Keywords.kw_import) ||
1370 Line.First->startsSequence(tok::kw_export, Keywords.kw_module) ||
1371 Line.First->startsSequence(tok::kw_export, Keywords.kw_import)) {
1372 Tok->setType(TT_ModulePartitionColon);
1373 }
else if (Line.First->is(tok::kw_asm)) {
1374 Tok->setType(TT_InlineASMColon);
1375 }
else if (Contexts.back().ColonIsDictLiteral || Style.isProto()) {
1376 Tok->setType(TT_DictLiteral);
1378 if (FormatToken *
Previous = Tok->getPreviousNonComment())
1379 Previous->setType(TT_SelectorName);
1381 }
else if (Contexts.back().ColonIsObjCMethodExpr ||
1382 Line.startsWith(TT_ObjCMethodSpecifier)) {
1383 Tok->setType(TT_ObjCMethodExpr);
1384 const FormatToken *BeforePrevious = Tok->Previous->Previous;
1387 bool UnknownIdentifierInMethodDeclaration =
1388 Line.startsWith(TT_ObjCMethodSpecifier) &&
1389 Tok->Previous->is(tok::identifier) && Tok->Previous->is(TT_Unknown);
1390 if (!BeforePrevious ||
1392 !(BeforePrevious->is(TT_CastRParen) ||
1393 (BeforePrevious->is(TT_ObjCMethodExpr) &&
1394 BeforePrevious->is(tok::colon))) ||
1395 BeforePrevious->is(tok::r_square) ||
1396 Contexts.back().LongestObjCSelectorName == 0 ||
1397 UnknownIdentifierInMethodDeclaration) {
1398 Tok->Previous->setType(TT_SelectorName);
1399 if (!Contexts.back().FirstObjCSelectorName) {
1400 Contexts.back().FirstObjCSelectorName = Tok->Previous;
1401 }
else if (Tok->Previous->ColumnWidth >
1402 Contexts.back().LongestObjCSelectorName) {
1403 Contexts.back().LongestObjCSelectorName =
1404 Tok->Previous->ColumnWidth;
1406 Tok->Previous->ParameterIndex =
1407 Contexts.back().FirstObjCSelectorName->ObjCSelectorNameParts;
1408 ++Contexts.back().FirstObjCSelectorName->ObjCSelectorNameParts;
1410 }
else if (Contexts.back().ColonIsForRangeExpr) {
1411 Tok->setType(TT_RangeBasedForLoopColon);
1412 for (
auto *Prev = Tok->Previous;
1413 Prev && !Prev->isOneOf(tok::semi, tok::l_paren);
1414 Prev = Prev->Previous) {
1415 if (Prev->isPointerOrReference())
1416 Prev->setFinalizedType(TT_PointerOrReference);
1418 }
else if (Contexts.back().ContextType == Context::C11GenericSelection) {
1419 Tok->setType(TT_GenericSelectionColon);
1420 }
else if (CurrentToken && CurrentToken->is(tok::numeric_constant)) {
1421 Tok->setType(TT_BitFieldColon);
1422 }
else if (Contexts.size() == 1 &&
1423 !Line.First->isOneOf(tok::kw_enum, tok::kw_case,
1425 FormatToken *Prev = Tok->getPreviousNonComment();
1428 if (Prev->isOneOf(tok::r_paren, tok::kw_noexcept) ||
1429 Prev->ClosesRequiresClause) {
1430 Tok->setType(TT_CtorInitializerColon);
1431 }
else if (Prev->is(tok::kw_try)) {
1433 FormatToken *PrevPrev = Prev->getPreviousNonComment();
1436 if (PrevPrev && PrevPrev->isOneOf(tok::r_paren, tok::kw_noexcept))
1437 Tok->setType(TT_CtorInitializerColon);
1439 Tok->setType(TT_InheritanceColon);
1440 if (Prev->isAccessSpecifierKeyword())
1443 }
else if (canBeObjCSelectorComponent(*Tok->Previous) && Tok->Next &&
1444 (Tok->Next->isOneOf(tok::r_paren, tok::comma) ||
1445 (canBeObjCSelectorComponent(*Tok->Next) && Tok->Next->Next &&
1446 Tok->Next->Next->is(tok::colon)))) {
1449 Tok->setType(TT_ObjCMethodExpr);
1456 if (Style.isJavaScript() && !Contexts.back().IsExpression)
1457 Tok->setType(TT_JsTypeOperator);
1460 if (Style.isTableGen()) {
1462 if (!parseTableGenValue())
1464 if (CurrentToken && CurrentToken->is(Keywords.kw_then))
1469 CurrentToken->isOneOf(tok::kw_constexpr, tok::identifier)) {
1475 if (CurrentToken && CurrentToken->is(tok::l_paren)) {
1477 if (!parseParens(IsIf))
1482 if (Style.isJavaScript()) {
1484 if ((Tok->Previous && Tok->Previous->is(tok::period)) ||
1485 (Tok->Next && Tok->Next->is(tok::colon))) {
1489 if (CurrentToken && CurrentToken->is(Keywords.kw_await))
1492 if (IsCpp && CurrentToken && CurrentToken->is(tok::kw_co_await))
1494 Contexts.back().ColonIsForRangeExpr =
true;
1495 if (!CurrentToken || CurrentToken->isNot(tok::l_paren))
1506 if (Tok->Previous && Tok->Previous->is(tok::r_paren) &&
1507 Tok->Previous->MatchingParen &&
1508 Tok->Previous->MatchingParen->is(TT_OverloadedOperatorLParen)) {
1509 Tok->Previous->setType(TT_OverloadedOperator);
1510 Tok->Previous->MatchingParen->setType(TT_OverloadedOperator);
1511 Tok->setType(TT_OverloadedOperatorLParen);
1514 if (Style.isVerilog()) {
1520 auto IsInstancePort = [&]() {
1521 const FormatToken *Prev = Tok->getPreviousNonComment();
1522 const FormatToken *PrevPrev;
1530 if (!Prev || !(PrevPrev = Prev->getPreviousNonComment()))
1533 if (Keywords.isVerilogIdentifier(*Prev) &&
1534 Keywords.isVerilogIdentifier(*PrevPrev)) {
1538 if (Prev->is(Keywords.kw_verilogHash) &&
1539 Keywords.isVerilogIdentifier(*PrevPrev)) {
1543 if (Keywords.isVerilogIdentifier(*Prev) && PrevPrev->is(tok::r_paren))
1546 if (Keywords.isVerilogIdentifier(*Prev) && PrevPrev->is(tok::comma)) {
1547 const FormatToken *PrevParen = PrevPrev->getPreviousNonComment();
1548 if (PrevParen && PrevParen->is(tok::r_paren) &&
1549 PrevParen->MatchingParen &&
1550 PrevParen->MatchingParen->is(TT_VerilogInstancePortLParen)) {
1557 if (IsInstancePort())
1558 Tok->setType(TT_VerilogInstancePortLParen);
1563 if (Line.MustBeDeclaration && Contexts.size() == 1 &&
1564 !Contexts.back().IsExpression && !Line.startsWith(TT_ObjCProperty) &&
1565 !Line.startsWith(tok::l_paren) &&
1566 !Tok->isOneOf(TT_TypeDeclarationParen, TT_RequiresExpressionLParen)) {
1567 if (
const auto *
Previous = Tok->Previous;
1570 !
Previous->isOneOf(TT_RequiresClause, TT_LeadingJavaAnnotation))) {
1571 Line.MightBeFunctionDecl =
true;
1572 Tok->MightBeFunctionDeclParen =
true;
1577 if (Style.isTableGen())
1578 Tok->setType(TT_TableGenListOpener);
1584 if (Tok->is(TT_RequiresExpressionLBrace))
1587 FormatToken *
Previous = Tok->getPreviousNonComment();
1589 Previous->setType(TT_SelectorName);
1591 Scopes.push_back(getScopeType(*Tok));
1597 Tok->setType(TT_TemplateOpener);
1605 Tok->Previous->isOneOf(TT_SelectorName, TT_DictLiteral))) {
1606 Tok->setType(TT_DictLiteral);
1607 FormatToken *
Previous = Tok->getPreviousNonComment();
1609 Previous->setType(TT_SelectorName);
1611 if (Style.isTableGen())
1612 Tok->setType(TT_TemplateOpener);
1614 Tok->setType(TT_BinaryOperator);
1615 NonTemplateLess.insert(Tok);
1625 if (!Scopes.empty())
1633 Tok->setType(TT_BinaryOperator);
1634 if (Tok->Previous && Tok->Previous->is(TT_TemplateCloser))
1635 Tok->SpacesRequiredBefore = 1;
1637 case tok::kw_operator:
1638 if (Style.isProto())
1640 while (CurrentToken &&
1641 !CurrentToken->isOneOf(tok::l_paren, tok::semi, tok::r_paren)) {
1642 if (CurrentToken->isOneOf(tok::star, tok::amp))
1643 CurrentToken->setType(TT_PointerOrReference);
1644 auto Next = CurrentToken->getNextNonComment();
1647 if (Next->is(tok::less))
1653 auto Previous = CurrentToken->getPreviousNonComment();
1655 if (CurrentToken->is(tok::comma) &&
Previous->isNot(tok::kw_operator))
1657 if (
Previous->isOneOf(TT_BinaryOperator, TT_UnaryOperator, tok::comma,
1658 tok::star, tok::arrow, tok::amp, tok::ampamp) ||
1660 Previous->TokenText.starts_with(
"\"\"")) {
1661 Previous->setType(TT_OverloadedOperator);
1662 if (CurrentToken->isOneOf(tok::less, tok::greater))
1666 if (CurrentToken && CurrentToken->is(tok::l_paren))
1667 CurrentToken->setType(TT_OverloadedOperatorLParen);
1668 if (CurrentToken && CurrentToken->Previous->is(TT_BinaryOperator))
1669 CurrentToken->Previous->setType(TT_OverloadedOperator);
1672 if (Style.isJavaScript() && Tok->Next &&
1673 Tok->Next->isOneOf(tok::semi, tok::comma, tok::colon, tok::r_paren,
1674 tok::r_brace, tok::r_square)) {
1679 Tok->setType(TT_JsTypeOptionalQuestion);
1684 if (Line.MustBeDeclaration && !Contexts.back().IsExpression &&
1685 Style.isJavaScript()) {
1688 if (Style.isCSharp()) {
1694 (Tok->Next->startsSequence(tok::question, tok::r_paren) ||
1695 Tok->Next->startsSequence(tok::question, tok::greater) ||
1696 Tok->Next->startsSequence(tok::question, tok::identifier,
1698 Tok->setType(TT_CSharpNullable);
1703 if (Tok->Next && Tok->Next->is(tok::identifier) && Tok->Next->Next &&
1704 Tok->Next->Next->is(tok::equal)) {
1705 Tok->setType(TT_CSharpNullable);
1714 if (!Contexts.back().IsExpression && Line.MustBeDeclaration &&
1716 !Tok->Next->isOneOf(tok::identifier, tok::string_literal) ||
1718 !Tok->Next->Next->isOneOf(tok::colon, tok::question))) {
1719 Tok->setType(TT_CSharpNullable);
1725 case tok::kw_template:
1726 parseTemplateDeclaration();
1729 switch (Contexts.back().ContextType) {
1730 case Context::CtorInitializer:
1731 Tok->setType(TT_CtorInitializerComma);
1733 case Context::InheritanceList:
1734 Tok->setType(TT_InheritanceComma);
1736 case Context::VerilogInstancePortList:
1737 Tok->setType(TT_VerilogInstancePortComma);
1740 if (Style.isVerilog() && Contexts.size() == 1 &&
1741 Line.startsWith(Keywords.kw_assign)) {
1742 Tok->setFinalizedType(TT_VerilogAssignComma);
1743 }
else if (Contexts.back().FirstStartOfName &&
1744 (Contexts.size() == 1 || startsWithInitStatement(Line))) {
1745 Contexts.back().FirstStartOfName->PartOfMultiVariableDeclStmt =
true;
1746 Line.IsMultiVariableDeclStmt =
true;
1750 if (Contexts.back().ContextType == Context::ForEachMacro)
1751 Contexts.back().IsExpression =
true;
1753 case tok::kw_default:
1755 if (Style.isVerilog() && Keywords.isVerilogEndOfLabel(*Tok) &&
1756 (Line.Level > 1 || (!Line.InPPDirective && Line.Level > 0))) {
1760 case tok::identifier:
1761 if (Tok->isOneOf(Keywords.kw___has_include,
1762 Keywords.kw___has_include_next)) {
1765 if (Style.isCSharp() && Tok->is(Keywords.kw_where) && Tok->Next &&
1766 Tok->Next->isNot(tok::l_paren)) {
1767 Tok->setType(TT_CSharpGenericTypeConstraint);
1768 parseCSharpGenericTypeConstraint();
1769 if (!Tok->getPreviousNonComment())
1770 Line.IsContinuation =
true;
1772 if (Style.isTableGen()) {
1773 if (Tok->is(Keywords.kw_assert)) {
1774 if (!parseTableGenValue())
1776 }
else if (Tok->isOneOf(Keywords.kw_def, Keywords.kw_defm) &&
1778 !Tok->Next->isOneOf(tok::colon, tok::l_brace))) {
1780 if (!parseTableGenValue(
true))
1786 if (Tok->isNot(TT_LambdaArrow) && Tok->Previous &&
1787 Tok->Previous->is(tok::kw_noexcept)) {
1788 Tok->setType(TT_TrailingReturnArrow);
1793 if (Style.isTableGen() && !parseTableGenValue())
1802 void parseCSharpGenericTypeConstraint() {
1803 int OpenAngleBracketsCount = 0;
1804 while (CurrentToken) {
1805 if (CurrentToken->is(tok::less)) {
1807 CurrentToken->setType(TT_TemplateOpener);
1808 ++OpenAngleBracketsCount;
1810 }
else if (CurrentToken->is(tok::greater)) {
1811 CurrentToken->setType(TT_TemplateCloser);
1812 --OpenAngleBracketsCount;
1814 }
else if (CurrentToken->is(tok::comma) && OpenAngleBracketsCount == 0) {
1817 CurrentToken->setType(TT_CSharpGenericTypeConstraintComma);
1819 }
else if (CurrentToken->is(Keywords.kw_where)) {
1820 CurrentToken->setType(TT_CSharpGenericTypeConstraint);
1822 }
else if (CurrentToken->is(tok::colon)) {
1823 CurrentToken->setType(TT_CSharpGenericTypeConstraintColon);
1831 void parseIncludeDirective() {
1832 if (CurrentToken && CurrentToken->is(tok::less)) {
1834 while (CurrentToken) {
1837 if (CurrentToken->isNot(tok::comment) &&
1838 !CurrentToken->TokenText.starts_with(
"//")) {
1839 CurrentToken->setType(TT_ImplicitStringLiteral);
1846 void parseWarningOrError() {
1851 while (CurrentToken) {
1852 CurrentToken->setType(TT_ImplicitStringLiteral);
1857 void parsePragma() {
1860 CurrentToken->isOneOf(Keywords.kw_mark, Keywords.kw_option,
1861 Keywords.kw_region)) {
1862 bool IsMarkOrRegion =
1863 CurrentToken->isOneOf(Keywords.kw_mark, Keywords.kw_region);
1866 while (CurrentToken) {
1867 if (IsMarkOrRegion || CurrentToken->Previous->is(TT_BinaryOperator))
1868 CurrentToken->setType(TT_ImplicitStringLiteral);
1874 void parseHasInclude() {
1875 if (!CurrentToken || CurrentToken->isNot(tok::l_paren))
1878 parseIncludeDirective();
1882 LineType parsePreprocessorDirective() {
1883 bool IsFirstToken = CurrentToken->IsFirst;
1889 if (Style.isJavaScript() && IsFirstToken) {
1893 while (CurrentToken) {
1895 CurrentToken->setType(TT_ImplicitStringLiteral);
1901 if (CurrentToken->is(tok::numeric_constant)) {
1902 CurrentToken->SpacesRequiredBefore = 1;
1907 if (!CurrentToken->Tok.getIdentifierInfo())
1911 if (Style.isVerilog() && !Keywords.isVerilogPPDirective(*CurrentToken))
1913 switch (CurrentToken->Tok.getIdentifierInfo()->getPPKeywordID()) {
1914 case tok::pp_include:
1915 case tok::pp_include_next:
1916 case tok::pp_import:
1918 parseIncludeDirective();
1922 case tok::pp_warning:
1923 parseWarningOrError();
1925 case tok::pp_pragma:
1930 Contexts.back().IsExpression =
true;
1933 CurrentToken->SpacesRequiredBefore =
true;
1939 while (CurrentToken) {
1940 FormatToken *Tok = CurrentToken;
1942 if (Tok->is(tok::l_paren)) {
1944 }
else if (Tok->isOneOf(Keywords.kw___has_include,
1945 Keywords.kw___has_include_next)) {
1956 NonTemplateLess.clear();
1957 if (!Line.InMacroBody && CurrentToken->is(tok::hash)) {
1961 auto Type = parsePreprocessorDirective();
1969 IdentifierInfo *Info = CurrentToken->Tok.getIdentifierInfo();
1971 CurrentToken->is(Keywords.kw_package)) ||
1972 (!Style.isVerilog() && Info &&
1973 Info->getPPKeywordID() == tok::pp_import && CurrentToken->Next &&
1974 CurrentToken->Next->isOneOf(tok::string_literal, tok::identifier,
1977 parseIncludeDirective();
1983 if (CurrentToken->is(tok::less) && Line.Last->is(tok::greater)) {
1984 parseIncludeDirective();
1991 CurrentToken->isOneOf(Keywords.kw_option, Keywords.kw_package)) {
1993 if (CurrentToken && CurrentToken->is(tok::identifier)) {
1994 while (CurrentToken)
2000 bool KeywordVirtualFound =
false;
2001 bool ImportStatement =
false;
2004 if (Style.isJavaScript() && CurrentToken->is(Keywords.kw_import))
2005 ImportStatement =
true;
2007 while (CurrentToken) {
2008 if (CurrentToken->is(tok::kw_virtual))
2009 KeywordVirtualFound =
true;
2010 if (Style.isJavaScript()) {
2017 if (Line.First->is(tok::kw_export) &&
2018 CurrentToken->is(Keywords.kw_from) && CurrentToken->Next &&
2019 CurrentToken->Next->isStringLiteral()) {
2020 ImportStatement =
true;
2022 if (isClosureImportStatement(*CurrentToken))
2023 ImportStatement =
true;
2025 if (!consumeToken())
2033 if (KeywordVirtualFound)
2035 if (ImportStatement)
2038 if (Line.startsWith(TT_ObjCMethodSpecifier)) {
2039 if (Contexts.back().FirstObjCSelectorName) {
2040 Contexts.back().FirstObjCSelectorName->LongestObjCSelectorName =
2041 Contexts.back().LongestObjCSelectorName;
2046 for (
const auto &ctx : Contexts)
2047 if (ctx.ContextType == Context::StructArrayInitializer)
2054 bool isClosureImportStatement(
const FormatToken &Tok) {
2057 return Tok.TokenText ==
"goog" && Tok.Next && Tok.Next->is(tok::period) &&
2059 (Tok.Next->Next->TokenText ==
"module" ||
2060 Tok.Next->Next->TokenText ==
"provide" ||
2061 Tok.Next->Next->TokenText ==
"require" ||
2062 Tok.Next->Next->TokenText ==
"requireType" ||
2063 Tok.Next->Next->TokenText ==
"forwardDeclare") &&
2064 Tok.Next->Next->Next && Tok.Next->Next->Next->is(tok::l_paren);
2067 void resetTokenMetadata() {
2073 if (!CurrentToken->isTypeFinalized() &&
2074 !CurrentToken->isOneOf(
2075 TT_LambdaLSquare, TT_LambdaLBrace, TT_AttributeMacro, TT_IfMacro,
2076 TT_ForEachMacro, TT_TypenameMacro, TT_FunctionLBrace,
2077 TT_ImplicitStringLiteral, TT_InlineASMBrace, TT_FatArrow,
2078 TT_LambdaArrow, TT_NamespaceMacro, TT_OverloadedOperator,
2079 TT_RegexLiteral, TT_TemplateString, TT_ObjCStringLiteral,
2080 TT_UntouchableMacroFunc, TT_StatementAttributeLikeMacro,
2081 TT_FunctionLikeOrFreestandingMacro, TT_ClassLBrace, TT_EnumLBrace,
2082 TT_RecordLBrace, TT_StructLBrace, TT_UnionLBrace, TT_RequiresClause,
2083 TT_RequiresClauseInARequiresExpression, TT_RequiresExpression,
2084 TT_RequiresExpressionLParen, TT_RequiresExpressionLBrace,
2085 TT_CompoundRequirementLBrace, TT_BracedListLBrace)) {
2086 CurrentToken->setType(TT_Unknown);
2088 CurrentToken->Role.reset();
2089 CurrentToken->MatchingParen =
nullptr;
2090 CurrentToken->FakeLParens.clear();
2091 CurrentToken->FakeRParens = 0;
2098 CurrentToken->NestingLevel = Contexts.size() - 1;
2099 CurrentToken->BindingStrength = Contexts.back().BindingStrength;
2100 modifyContext(*CurrentToken);
2101 determineTokenType(*CurrentToken);
2102 CurrentToken = CurrentToken->Next;
2104 resetTokenMetadata();
2147 StructArrayInitializer,
2151 C11GenericSelection,
2153 VerilogInstancePortList,
2159 struct ScopedContextCreator {
2160 AnnotatingParser &
P;
2166 P.Contexts.back().BindingStrength + Increase,
2167 P.Contexts.back().IsExpression));
2170 ~ScopedContextCreator() {
2172 if (
P.Contexts.back().ContextType == Context::StructArrayInitializer) {
2173 P.Contexts.pop_back();
2174 P.Contexts.back().ContextType = Context::StructArrayInitializer;
2178 P.Contexts.pop_back();
2182 void modifyContext(
const FormatToken &Current) {
2183 auto AssignmentStartsExpression = [&]() {
2187 if (Line.First->isOneOf(tok::kw_using, tok::kw_return))
2189 if (Line.First->is(tok::kw_template)) {
2190 assert(Current.Previous);
2191 if (Current.Previous->is(tok::kw_operator)) {
2197 const FormatToken *Tok = Line.First->getNextNonComment();
2199 if (Tok->isNot(TT_TemplateOpener)) {
2206 if (Contexts.back().ContextKind == tok::less) {
2207 assert(Current.Previous->Previous);
2208 return !Current.Previous->Previous->isOneOf(tok::kw_typename,
2212 Tok = Tok->MatchingParen;
2215 Tok = Tok->getNextNonComment();
2219 if (Tok->isOneOf(tok::kw_class, tok::kw_enum, tok::kw_struct,
2229 if (Style.isJavaScript() &&
2230 (Line.startsWith(Keywords.kw_type, tok::identifier) ||
2231 Line.startsWith(tok::kw_export, Keywords.kw_type,
2232 tok::identifier))) {
2236 return !Current.Previous || Current.Previous->isNot(tok::kw_operator);
2239 if (AssignmentStartsExpression()) {
2240 Contexts.back().IsExpression =
true;
2241 if (!Line.startsWith(TT_UnaryOperator)) {
2242 for (FormatToken *
Previous = Current.Previous;
2244 !
Previous->Previous->isOneOf(tok::comma, tok::semi);
2246 if (
Previous->isOneOf(tok::r_square, tok::r_paren, tok::greater)) {
2253 if (
Previous->isOneOf(TT_BinaryOperator, TT_UnaryOperator) &&
2255 Previous->Previous->isNot(tok::equal)) {
2256 Previous->setType(TT_PointerOrReference);
2260 }
else if (Current.is(tok::lessless) &&
2261 (!Current.Previous ||
2262 Current.Previous->isNot(tok::kw_operator))) {
2263 Contexts.back().IsExpression =
true;
2264 }
else if (Current.isOneOf(tok::kw_return, tok::kw_throw)) {
2265 Contexts.back().IsExpression =
true;
2266 }
else if (Current.is(TT_TrailingReturnArrow)) {
2267 Contexts.back().IsExpression =
false;
2268 }
else if (Current.isOneOf(TT_LambdaArrow, Keywords.kw_assert)) {
2270 }
else if (Current.Previous &&
2271 Current.Previous->is(TT_CtorInitializerColon)) {
2272 Contexts.back().IsExpression =
true;
2273 Contexts.back().ContextType = Context::CtorInitializer;
2274 }
else if (Current.Previous && Current.Previous->is(TT_InheritanceColon)) {
2275 Contexts.back().ContextType = Context::InheritanceList;
2276 }
else if (Current.isOneOf(tok::r_paren, tok::greater, tok::comma)) {
2277 for (FormatToken *
Previous = Current.Previous;
2280 Previous->setType(TT_PointerOrReference);
2282 if (Line.MustBeDeclaration &&
2283 Contexts.front().ContextType != Context::CtorInitializer) {
2284 Contexts.back().IsExpression =
false;
2286 }
else if (Current.is(tok::kw_new)) {
2287 Contexts.back().CanBeExpression =
false;
2288 }
else if (Current.is(tok::semi) ||
2289 (Current.is(tok::exclaim) && Current.Previous &&
2290 Current.Previous->isNot(tok::kw_operator))) {
2294 Contexts.back().IsExpression =
true;
2298 static FormatToken *untilMatchingParen(FormatToken *Current) {
2302 if (Current->is(tok::l_paren))
2304 if (Current->is(tok::r_paren))
2308 Current = Current->Next;
2313 static bool isDeductionGuide(FormatToken &Current) {
2315 if (Current.Previous && Current.Previous->is(tok::r_paren) &&
2316 Current.startsSequence(tok::arrow, tok::identifier, tok::less)) {
2318 FormatToken *TemplateCloser = Current.Next->Next;
2319 int NestingLevel = 0;
2320 while (TemplateCloser) {
2322 if (TemplateCloser->is(tok::l_paren)) {
2324 TemplateCloser = untilMatchingParen(TemplateCloser);
2325 if (!TemplateCloser)
2328 if (TemplateCloser->is(tok::less))
2330 if (TemplateCloser->is(tok::greater))
2332 if (NestingLevel < 1)
2334 TemplateCloser = TemplateCloser->Next;
2338 if (TemplateCloser && TemplateCloser->Next &&
2339 TemplateCloser->Next->is(tok::semi) &&
2340 Current.Previous->MatchingParen) {
2343 FormatToken *LeadingIdentifier =
2344 Current.Previous->MatchingParen->Previous;
2346 return LeadingIdentifier &&
2347 LeadingIdentifier->TokenText == Current.Next->TokenText;
2353 void determineTokenType(FormatToken &Current) {
2354 if (Current.isNot(TT_Unknown)) {
2359 if ((Style.isJavaScript() || Style.isCSharp()) &&
2360 Current.is(tok::exclaim)) {
2361 if (Current.Previous) {
2363 Style.isJavaScript()
2364 ? Keywords.isJavaScriptIdentifier(
2365 *Current.Previous,
true)
2366 : Current.Previous->is(tok::identifier);
2368 Current.Previous->isOneOf(
2369 tok::kw_default, tok::kw_namespace, tok::r_paren, tok::r_square,
2370 tok::r_brace, tok::kw_false, tok::kw_true, Keywords.kw_type,
2371 Keywords.kw_get, Keywords.kw_init, Keywords.kw_set) ||
2372 Current.Previous->Tok.isLiteral()) {
2373 Current.setType(TT_NonNullAssertion);
2378 Current.Next->isOneOf(TT_BinaryOperator, Keywords.kw_as)) {
2379 Current.setType(TT_NonNullAssertion);
2388 Current.is(Keywords.kw_instanceof)) {
2389 Current.setType(TT_BinaryOperator);
2390 }
else if (isStartOfName(Current) &&
2391 (!Line.MightBeFunctionDecl || Current.NestingLevel != 0)) {
2392 Contexts.back().FirstStartOfName = &Current;
2393 Current.setType(TT_StartOfName);
2394 }
else if (Current.is(tok::semi)) {
2398 Contexts.back().FirstStartOfName =
nullptr;
2399 }
else if (Current.isOneOf(tok::kw_auto, tok::kw___auto_type)) {
2401 }
else if (Current.is(tok::arrow) &&
2403 Current.setType(TT_LambdaArrow);
2404 }
else if (Current.is(tok::arrow) && Style.isVerilog()) {
2406 Current.setType(TT_BinaryOperator);
2407 }
else if (Current.is(tok::arrow) && AutoFound &&
2408 Line.MightBeFunctionDecl && Current.NestingLevel == 0 &&
2409 !Current.Previous->isOneOf(tok::kw_operator, tok::identifier)) {
2411 Current.setType(TT_TrailingReturnArrow);
2412 }
else if (Current.is(tok::arrow) && Current.Previous &&
2413 Current.Previous->is(tok::r_brace) &&
2417 Current.setType(TT_TrailingReturnArrow);
2418 }
else if (isDeductionGuide(Current)) {
2420 Current.setType(TT_TrailingReturnArrow);
2421 }
else if (Current.isPointerOrReference()) {
2422 Current.setType(determineStarAmpUsage(
2424 Contexts.back().CanBeExpression && Contexts.back().IsExpression,
2425 Contexts.back().ContextType == Context::TemplateArgument));
2426 }
else if (Current.isOneOf(tok::minus, tok::plus, tok::caret) ||
2427 (Style.isVerilog() && Current.is(tok::pipe))) {
2428 Current.setType(determinePlusMinusCaretUsage(Current));
2429 if (Current.is(TT_UnaryOperator) && Current.is(tok::caret))
2430 Contexts.back().CaretFound =
true;
2431 }
else if (Current.isOneOf(tok::minusminus, tok::plusplus)) {
2432 Current.setType(determineIncrementUsage(Current));
2433 }
else if (Current.isOneOf(tok::exclaim, tok::tilde)) {
2434 Current.setType(TT_UnaryOperator);
2435 }
else if (Current.is(tok::question)) {
2436 if (Style.isJavaScript() && Line.MustBeDeclaration &&
2437 !Contexts.back().IsExpression) {
2440 Current.setType(TT_JsTypeOptionalQuestion);
2441 }
else if (Style.isTableGen()) {
2443 Current.setType(TT_Unknown);
2445 Current.setType(TT_ConditionalExpr);
2447 }
else if (Current.isBinaryOperator() &&
2448 (!Current.Previous || Current.Previous->isNot(tok::l_square)) &&
2449 (Current.isNot(tok::greater) &&
2451 if (Style.isVerilog()) {
2452 if (Current.is(tok::lessequal) && Contexts.size() == 1 &&
2453 !Contexts.back().VerilogAssignmentFound) {
2457 Current.setFinalizedType(TT_BinaryOperator);
2460 Contexts.back().VerilogAssignmentFound =
true;
2462 Current.setType(TT_BinaryOperator);
2463 }
else if (Current.is(tok::comment)) {
2464 if (Current.TokenText.starts_with(
"/*")) {
2465 if (Current.TokenText.ends_with(
"*/")) {
2466 Current.setType(TT_BlockComment);
2470 Current.Tok.setKind(tok::unknown);
2473 Current.setType(TT_LineComment);
2475 }
else if (Current.is(tok::string_literal)) {
2476 if (Style.isVerilog() && Contexts.back().VerilogMayBeConcatenation &&
2477 Current.getPreviousNonComment() &&
2478 Current.getPreviousNonComment()->isOneOf(tok::comma, tok::l_brace) &&
2479 Current.getNextNonComment() &&
2480 Current.getNextNonComment()->isOneOf(tok::comma, tok::r_brace)) {
2481 Current.setType(TT_StringInConcatenation);
2483 }
else if (Current.is(tok::l_paren)) {
2484 if (lParenStartsCppCast(Current))
2485 Current.setType(TT_CppCastLParen);
2486 }
else if (Current.is(tok::r_paren)) {
2487 if (rParenEndsCast(Current))
2488 Current.setType(TT_CastRParen);
2489 if (Current.MatchingParen && Current.Next &&
2490 !Current.Next->isBinaryOperator() &&
2491 !Current.Next->isOneOf(
2492 tok::semi, tok::colon, tok::l_brace, tok::l_paren, tok::comma,
2493 tok::period, tok::arrow, tok::coloncolon, tok::kw_noexcept)) {
2494 if (FormatToken *AfterParen = Current.MatchingParen->Next;
2495 AfterParen && AfterParen->isNot(tok::caret)) {
2497 if (FormatToken *BeforeParen = Current.MatchingParen->Previous;
2498 BeforeParen && BeforeParen->is(tok::identifier) &&
2499 BeforeParen->isNot(TT_TypenameMacro) &&
2500 BeforeParen->TokenText == BeforeParen->TokenText.upper() &&
2501 (!BeforeParen->Previous ||
2502 BeforeParen->Previous->ClosesTemplateDeclaration ||
2503 BeforeParen->Previous->ClosesRequiresClause)) {
2504 Current.setType(TT_FunctionAnnotationRParen);
2508 }
else if (Current.is(tok::at) && Current.Next && !Style.isJavaScript() &&
2512 switch (Current.Next->Tok.getObjCKeywordID()) {
2513 case tok::objc_interface:
2514 case tok::objc_implementation:
2515 case tok::objc_protocol:
2516 Current.setType(TT_ObjCDecl);
2518 case tok::objc_property:
2519 Current.setType(TT_ObjCProperty);
2524 }
else if (Current.is(tok::period)) {
2525 FormatToken *PreviousNoComment = Current.getPreviousNonComment();
2526 if (PreviousNoComment &&
2527 PreviousNoComment->isOneOf(tok::comma, tok::l_brace)) {
2528 Current.setType(TT_DesignatedInitializerPeriod);
2530 Current.Previous->isOneOf(TT_JavaAnnotation,
2531 TT_LeadingJavaAnnotation)) {
2532 Current.setType(Current.Previous->getType());
2534 }
else if (canBeObjCSelectorComponent(Current) &&
2537 Current.Previous && Current.Previous->is(TT_CastRParen) &&
2538 Current.Previous->MatchingParen &&
2539 Current.Previous->MatchingParen->Previous &&
2540 Current.Previous->MatchingParen->Previous->is(
2541 TT_ObjCMethodSpecifier)) {
2545 Current.setType(TT_SelectorName);
2546 }
else if (Current.isOneOf(tok::identifier, tok::kw_const, tok::kw_noexcept,
2547 tok::kw_requires) &&
2549 !Current.Previous->isOneOf(tok::equal, tok::at,
2550 TT_CtorInitializerComma,
2551 TT_CtorInitializerColon) &&
2552 Line.MightBeFunctionDecl && Contexts.size() == 1) {
2555 Current.setType(TT_TrailingAnnotation);
2557 Style.isJavaScript()) &&
2559 if (Current.Previous->is(tok::at) &&
2560 Current.isNot(Keywords.kw_interface)) {
2561 const FormatToken &AtToken = *Current.Previous;
2562 const FormatToken *
Previous = AtToken.getPreviousNonComment();
2564 Current.setType(TT_LeadingJavaAnnotation);
2566 Current.setType(TT_JavaAnnotation);
2567 }
else if (Current.Previous->is(tok::period) &&
2568 Current.Previous->isOneOf(TT_JavaAnnotation,
2569 TT_LeadingJavaAnnotation)) {
2570 Current.setType(Current.Previous->getType());
2580 bool isStartOfName(
const FormatToken &Tok) {
2582 if (Style.isVerilog())
2585 if (Tok.isNot(tok::identifier) || !Tok.Previous)
2588 if (
const auto *NextNonComment = Tok.getNextNonComment();
2589 (!NextNonComment && !Line.InMacroBody) ||
2591 (NextNonComment->isPointerOrReference() ||
2592 NextNonComment->is(tok::string_literal) ||
2593 (Line.InPragmaDirective && NextNonComment->is(tok::identifier))))) {
2597 if (Tok.Previous->isOneOf(TT_LeadingJavaAnnotation, Keywords.kw_instanceof,
2601 if (Style.isJavaScript() && Tok.Previous->is(Keywords.kw_in))
2605 FormatToken *PreviousNotConst = Tok.getPreviousNonComment();
2608 if (!Style.isJavaScript())
2609 while (PreviousNotConst && PreviousNotConst->is(tok::kw_const))
2610 PreviousNotConst = PreviousNotConst->getPreviousNonComment();
2612 if (!PreviousNotConst)
2615 if (PreviousNotConst->ClosesRequiresClause)
2618 if (Style.isTableGen()) {
2620 if (Keywords.isTableGenDefinition(*PreviousNotConst))
2623 if (Contexts.back().ContextKind != tok::l_brace)
2627 bool IsPPKeyword = PreviousNotConst->is(tok::identifier) &&
2628 PreviousNotConst->Previous &&
2629 PreviousNotConst->Previous->is(tok::hash);
2631 if (PreviousNotConst->is(TT_TemplateCloser)) {
2632 return PreviousNotConst && PreviousNotConst->MatchingParen &&
2633 PreviousNotConst->MatchingParen->Previous &&
2634 PreviousNotConst->MatchingParen->Previous->isNot(tok::period) &&
2635 PreviousNotConst->MatchingParen->Previous->isNot(tok::kw_template);
2638 if ((PreviousNotConst->is(tok::r_paren) &&
2639 PreviousNotConst->is(TT_TypeDeclarationParen)) ||
2640 PreviousNotConst->is(TT_AttributeRParen)) {
2649 if (PreviousNotConst->isOneOf(tok::identifier, tok::kw_auto) &&
2650 PreviousNotConst->isNot(TT_StatementAttributeLikeMacro)) {
2655 if (PreviousNotConst->is(TT_PointerOrReference))
2659 if (PreviousNotConst->isTypeName(LangOpts))
2664 PreviousNotConst->is(tok::r_square)) {
2669 return Style.isJavaScript() && PreviousNotConst->is(tok::kw_const);
2673 bool lParenStartsCppCast(
const FormatToken &Tok) {
2678 FormatToken *LeftOfParens = Tok.getPreviousNonComment();
2679 if (LeftOfParens && LeftOfParens->is(TT_TemplateCloser) &&
2680 LeftOfParens->MatchingParen) {
2681 auto *Prev = LeftOfParens->MatchingParen->getPreviousNonComment();
2683 Prev->isOneOf(tok::kw_const_cast, tok::kw_dynamic_cast,
2684 tok::kw_reinterpret_cast, tok::kw_static_cast)) {
2694 bool rParenEndsCast(
const FormatToken &Tok) {
2695 assert(Tok.is(tok::r_paren));
2697 if (!Tok.MatchingParen || !Tok.Previous)
2704 const auto *LParen = Tok.MatchingParen;
2705 const auto *BeforeRParen = Tok.Previous;
2706 const auto *AfterRParen = Tok.Next;
2709 if (BeforeRParen == LParen || !AfterRParen)
2712 if (LParen->is(TT_OverloadedOperatorLParen))
2715 auto *LeftOfParens = LParen->getPreviousNonComment();
2719 if (LeftOfParens->is(tok::r_paren) &&
2720 LeftOfParens->isNot(TT_CastRParen)) {
2721 if (!LeftOfParens->MatchingParen ||
2722 !LeftOfParens->MatchingParen->Previous) {
2725 LeftOfParens = LeftOfParens->MatchingParen->Previous;
2728 if (LeftOfParens->is(tok::r_square)) {
2730 auto MayBeArrayDelete = [](FormatToken *Tok) -> FormatToken * {
2731 if (Tok->isNot(tok::r_square))
2734 Tok = Tok->getPreviousNonComment();
2735 if (!Tok || Tok->isNot(tok::l_square))
2738 Tok = Tok->getPreviousNonComment();
2739 if (!Tok || Tok->isNot(tok::kw_delete))
2743 if (FormatToken *MaybeDelete = MayBeArrayDelete(LeftOfParens))
2744 LeftOfParens = MaybeDelete;
2750 if (LeftOfParens->Tok.getIdentifierInfo() && LeftOfParens->Previous &&
2751 LeftOfParens->Previous->is(tok::kw_operator)) {
2757 if (LeftOfParens->Tok.getIdentifierInfo() &&
2758 !LeftOfParens->isOneOf(Keywords.kw_in, tok::kw_return, tok::kw_case,
2759 tok::kw_delete, tok::kw_throw)) {
2765 if (LeftOfParens->isOneOf(tok::at, tok::r_square, TT_OverloadedOperator,
2766 TT_TemplateCloser, tok::ellipsis)) {
2771 if (AfterRParen->is(tok::question) ||
2772 (AfterRParen->is(tok::ampamp) && !BeforeRParen->isTypeName(LangOpts))) {
2777 if (AfterRParen->is(Keywords.kw_in) && Style.isCSharp())
2782 if (AfterRParen->isOneOf(tok::kw_noexcept, tok::kw_volatile, tok::kw_const,
2783 tok::kw_requires, tok::kw_throw, tok::arrow,
2784 Keywords.kw_override, Keywords.kw_final) ||
2785 isCppAttribute(IsCpp, *AfterRParen)) {
2795 if (AfterRParen->isOneOf(tok::kw_sizeof, tok::kw_alignof) ||
2796 (AfterRParen->Tok.isLiteral() &&
2797 AfterRParen->isNot(tok::string_literal))) {
2801 auto IsNonVariableTemplate = [](
const FormatToken &Tok) {
2802 if (Tok.isNot(TT_TemplateCloser))
2804 const auto *
Less = Tok.MatchingParen;
2807 const auto *BeforeLess =
Less->getPreviousNonComment();
2808 return BeforeLess && BeforeLess->isNot(TT_VariableTemplate);
2812 auto IsQualifiedPointerOrReference = [](
const FormatToken *
T,
2813 const LangOptions &LangOpts) {
2815 assert(!
T->isTypeName(LangOpts) &&
"Should have already been checked");
2819 if (
T->is(TT_AttributeRParen)) {
2821 assert(
T->is(tok::r_paren));
2822 assert(
T->MatchingParen);
2823 assert(
T->MatchingParen->is(tok::l_paren));
2824 assert(
T->MatchingParen->is(TT_AttributeLParen));
2825 if (
const auto *Tok =
T->MatchingParen->Previous;
2826 Tok && Tok->isAttribute()) {
2830 }
else if (
T->is(TT_AttributeSquare)) {
2832 if (
T->MatchingParen &&
T->MatchingParen->Previous) {
2833 T =
T->MatchingParen->Previous;
2836 }
else if (
T->canBePointerOrReferenceQualifier()) {
2842 return T &&
T->is(TT_PointerOrReference);
2845 bool ParensAreType = IsNonVariableTemplate(*BeforeRParen) ||
2846 BeforeRParen->is(TT_TypeDeclarationParen) ||
2847 BeforeRParen->isTypeName(LangOpts) ||
2848 IsQualifiedPointerOrReference(BeforeRParen, LangOpts);
2849 bool ParensCouldEndDecl =
2850 AfterRParen->isOneOf(tok::equal, tok::semi, tok::l_brace, tok::greater);
2851 if (ParensAreType && !ParensCouldEndDecl)
2862 for (
const auto *Token = LParen->Next; Token != &Tok; Token = Token->Next)
2863 if (Token->is(TT_BinaryOperator))
2868 if (AfterRParen->isOneOf(tok::identifier, tok::kw_this))
2872 if (AfterRParen->is(tok::l_paren)) {
2873 for (
const auto *Prev = BeforeRParen; Prev->is(tok::identifier);) {
2874 Prev = Prev->Previous;
2875 if (Prev->is(tok::coloncolon))
2876 Prev = Prev->Previous;
2882 if (!AfterRParen->Next)
2885 if (AfterRParen->is(tok::l_brace) &&
2893 const bool NextIsAmpOrStar = AfterRParen->isOneOf(tok::amp, tok::star);
2894 if (!(AfterRParen->isUnaryOperator() || NextIsAmpOrStar) ||
2895 AfterRParen->is(tok::plus) ||
2896 !AfterRParen->Next->isOneOf(tok::identifier, tok::numeric_constant)) {
2900 if (NextIsAmpOrStar &&
2901 (AfterRParen->Next->is(tok::numeric_constant) || Line.InPPDirective)) {
2905 if (Line.InPPDirective && AfterRParen->is(tok::minus))
2908 const auto *Prev = BeforeRParen;
2911 if (Prev->is(tok::r_paren)) {
2912 if (Prev->is(TT_CastRParen))
2914 Prev = Prev->MatchingParen;
2917 Prev = Prev->Previous;
2918 if (!Prev || Prev->isNot(tok::r_paren))
2920 Prev = Prev->MatchingParen;
2921 return Prev && Prev->is(TT_FunctionTypeLParen);
2925 for (Prev = BeforeRParen; Prev != LParen; Prev = Prev->Previous)
2926 if (!Prev->isOneOf(tok::kw_const, tok::identifier, tok::coloncolon))
2933 bool determineUnaryOperatorByUsage(
const FormatToken &Tok) {
2934 const FormatToken *PrevToken = Tok.getPreviousNonComment();
2944 if (PrevToken->isOneOf(
2945 TT_ConditionalExpr, tok::l_paren, tok::comma, tok::colon, tok::semi,
2946 tok::equal, tok::question, tok::l_square, tok::l_brace,
2947 tok::kw_case, tok::kw_co_await, tok::kw_co_return, tok::kw_co_yield,
2948 tok::kw_delete, tok::kw_return, tok::kw_throw)) {
2955 if (PrevToken->is(tok::kw_sizeof))
2959 if (PrevToken->isOneOf(TT_CastRParen, TT_UnaryOperator))
2963 if (PrevToken->is(TT_BinaryOperator))
2971 bool InTemplateArgument) {
2972 if (Style.isJavaScript())
2973 return TT_BinaryOperator;
2976 if (Style.isCSharp() && Tok.is(tok::ampamp))
2977 return TT_BinaryOperator;
2979 if (Style.isVerilog()) {
2984 if (Tok.is(tok::star))
2985 return TT_BinaryOperator;
2986 return determineUnaryOperatorByUsage(Tok) ? TT_UnaryOperator
2987 : TT_BinaryOperator;
2990 const FormatToken *PrevToken = Tok.getPreviousNonComment();
2992 return TT_UnaryOperator;
2993 if (PrevToken->is(TT_TypeName))
2994 return TT_PointerOrReference;
2995 if (PrevToken->isOneOf(tok::kw_new, tok::kw_delete) && Tok.is(tok::ampamp))
2996 return TT_BinaryOperator;
2998 const FormatToken *NextToken = Tok.getNextNonComment();
3000 if (InTemplateArgument && NextToken && NextToken->is(tok::kw_noexcept))
3001 return TT_BinaryOperator;
3004 NextToken->isOneOf(tok::arrow, tok::equal, tok::comma, tok::r_paren,
3005 TT_RequiresClause) ||
3007 NextToken->canBePointerOrReferenceQualifier() ||
3008 (NextToken->is(tok::l_brace) && !NextToken->getNextNonComment())) {
3009 return TT_PointerOrReference;
3012 if (PrevToken->is(tok::coloncolon))
3013 return TT_PointerOrReference;
3015 if (PrevToken->is(tok::r_paren) && PrevToken->is(TT_TypeDeclarationParen))
3016 return TT_PointerOrReference;
3018 if (determineUnaryOperatorByUsage(Tok))
3019 return TT_UnaryOperator;
3021 if (NextToken->is(tok::l_square) && NextToken->isNot(TT_LambdaLSquare))
3022 return TT_PointerOrReference;
3024 return TT_PointerOrReference;
3025 if (NextToken->isOneOf(tok::comma, tok::semi))
3026 return TT_PointerOrReference;
3038 if (PrevToken->is(tok::r_brace) && Tok.is(tok::star) &&
3039 !PrevToken->MatchingParen) {
3040 return TT_PointerOrReference;
3043 if (PrevToken->endsSequence(tok::r_square, tok::l_square, tok::kw_delete))
3044 return TT_UnaryOperator;
3046 if (PrevToken->Tok.isLiteral() ||
3047 PrevToken->isOneOf(tok::r_paren, tok::r_square, tok::kw_true,
3048 tok::kw_false, tok::r_brace)) {
3049 return TT_BinaryOperator;
3052 const FormatToken *NextNonParen = NextToken;
3053 while (NextNonParen && NextNonParen->is(tok::l_paren))
3054 NextNonParen = NextNonParen->getNextNonComment();
3055 if (NextNonParen && (NextNonParen->Tok.isLiteral() ||
3056 NextNonParen->isOneOf(tok::kw_true, tok::kw_false) ||
3057 NextNonParen->isUnaryOperator())) {
3058 return TT_BinaryOperator;
3064 if (InTemplateArgument && NextToken->Tok.isAnyIdentifier())
3065 return TT_BinaryOperator;
3069 if (Tok.is(tok::ampamp) &&
3070 NextToken->isOneOf(tok::l_paren, tok::star, tok::amp)) {
3071 return TT_BinaryOperator;
3076 if (NextToken->Tok.isAnyIdentifier()) {
3077 const FormatToken *NextNextToken = NextToken->getNextNonComment();
3078 if (NextNextToken && NextNextToken->is(tok::arrow))
3079 return TT_BinaryOperator;
3085 return TT_BinaryOperator;
3088 if (!Scopes.empty() && Scopes.back() ==
ST_Class)
3089 return TT_PointerOrReference;
3092 auto IsChainedOperatorAmpOrMember = [](
const FormatToken *token) {
3093 return !token || token->isOneOf(tok::amp, tok::period, tok::arrow,
3094 tok::arrowstar, tok::periodstar);
3099 if (Tok.is(tok::amp) && PrevToken && PrevToken->Tok.isAnyIdentifier() &&
3100 IsChainedOperatorAmpOrMember(PrevToken->getPreviousNonComment()) &&
3101 NextToken && NextToken->Tok.isAnyIdentifier()) {
3102 if (
auto NextNext = NextToken->getNextNonComment();
3104 (IsChainedOperatorAmpOrMember(NextNext) || NextNext->is(tok::semi))) {
3105 return TT_BinaryOperator;
3111 return TT_BinaryOperator;
3114 return TT_PointerOrReference;
3117 TokenType determinePlusMinusCaretUsage(
const FormatToken &Tok) {
3118 if (determineUnaryOperatorByUsage(Tok))
3119 return TT_UnaryOperator;
3121 const FormatToken *PrevToken = Tok.getPreviousNonComment();
3123 return TT_UnaryOperator;
3125 if (PrevToken->is(tok::at))
3126 return TT_UnaryOperator;
3129 return TT_BinaryOperator;
3133 TokenType determineIncrementUsage(
const FormatToken &Tok) {
3134 const FormatToken *PrevToken = Tok.getPreviousNonComment();
3135 if (!PrevToken || PrevToken->is(TT_CastRParen))
3136 return TT_UnaryOperator;
3137 if (PrevToken->isOneOf(tok::r_paren, tok::r_square, tok::identifier))
3138 return TT_TrailingUnaryOperator;
3140 return TT_UnaryOperator;
3143 SmallVector<Context, 8> Contexts;
3145 const FormatStyle &Style;
3146 AnnotatedLine &Line;
3147 FormatToken *CurrentToken;
3150 LangOptions LangOpts;
3151 const AdditionalKeywords &Keywords;
3153 SmallVector<ScopeType> &Scopes;
3161 int TemplateDeclarationDepth;
3169class ExpressionParser {
3171 ExpressionParser(
const FormatStyle &Style,
const AdditionalKeywords &Keywords,
3172 AnnotatedLine &Line)
3173 : Style(Style), Keywords(Keywords), Line(Line), Current(Line.
First) {}
3176 void parse(
int Precedence = 0) {
3179 while (Current && (Current->is(tok::kw_return) ||
3180 (Current->is(tok::colon) &&
3181 Current->isOneOf(TT_ObjCMethodExpr, TT_DictLiteral)))) {
3185 if (!Current || Precedence > PrecedenceArrowAndPeriod)
3190 parseConditionalExpr();
3196 if (Precedence == PrecedenceUnaryOperator) {
3197 parseUnaryOperator();
3201 FormatToken *Start = Current;
3202 FormatToken *LatestOperator =
nullptr;
3203 unsigned OperatorIndex = 0;
3205 FormatToken *VerilogFirstOfType =
nullptr;
3214 if (Style.isVerilog() && Precedence ==
prec::Comma) {
3215 VerilogFirstOfType =
3216 verilogGroupDecl(VerilogFirstOfType, LatestOperator);
3220 parse(Precedence + 1);
3222 int CurrentPrecedence = getCurrentPrecedence();
3233 if (Precedence == CurrentPrecedence && Current &&
3234 Current->is(TT_SelectorName)) {
3236 addFakeParenthesis(Start,
prec::Level(Precedence));
3240 if ((Style.isCSharp() || Style.isJavaScript() ||
3245 FormatToken *Prev = Current->getPreviousNonComment();
3246 if (Prev && Prev->is(tok::string_literal) &&
3247 (Prev == Start || Prev->endsSequence(tok::string_literal, tok::plus,
3248 TT_StringInConcatenation))) {
3249 Prev->setType(TT_StringInConcatenation);
3256 (Current->closesScope() &&
3257 (Current->MatchingParen || Current->is(TT_TemplateString))) ||
3258 (CurrentPrecedence != -1 && CurrentPrecedence < Precedence) ||
3267 if (Current->opensScope() ||
3268 Current->isOneOf(TT_RequiresClause,
3269 TT_RequiresClauseInARequiresExpression)) {
3272 while (Current && (!Current->closesScope() || Current->opensScope())) {
3279 if (CurrentPrecedence == Precedence) {
3281 LatestOperator->NextOperator = Current;
3282 LatestOperator = Current;
3283 Current->OperatorIndex = OperatorIndex;
3286 next(Precedence > 0);
3291 if (Style.isVerilog() && Precedence ==
prec::Comma && VerilogFirstOfType)
3292 addFakeParenthesis(VerilogFirstOfType,
prec::Comma);
3294 if (LatestOperator && (Current || Precedence > 0)) {
3300 Start->Previous->isOneOf(TT_RequiresClause,
3301 TT_RequiresClauseInARequiresExpression))
3303 auto Ret = Current ? Current : Line.Last;
3304 while (!
Ret->ClosesRequiresClause &&
Ret->Previous)
3310 if (Precedence == PrecedenceArrowAndPeriod) {
3314 addFakeParenthesis(Start,
prec::Level(Precedence), End);
3322 int getCurrentPrecedence() {
3324 const FormatToken *NextNonComment = Current->getNextNonComment();
3325 if (Current->is(TT_ConditionalExpr))
3327 if (NextNonComment && Current->is(TT_SelectorName) &&
3328 (NextNonComment->isOneOf(TT_DictLiteral, TT_JsTypeColon) ||
3329 (Style.isProto() && NextNonComment->is(tok::less)))) {
3332 if (Current->is(TT_JsComputedPropertyName))
3334 if (Current->is(TT_LambdaArrow))
3336 if (Current->is(TT_FatArrow))
3338 if (Current->isOneOf(tok::semi, TT_InlineASMColon, TT_SelectorName) ||
3339 (Current->is(tok::comment) && NextNonComment &&
3340 NextNonComment->is(TT_SelectorName))) {
3343 if (Current->is(TT_RangeBasedForLoopColon))
3346 Current->is(Keywords.kw_instanceof)) {
3349 if (Style.isJavaScript() &&
3350 Current->isOneOf(Keywords.kw_in, Keywords.kw_as)) {
3353 if (Current->is(TT_BinaryOperator) || Current->is(tok::comma))
3354 return Current->getPrecedence();
3355 if (Current->isOneOf(tok::period, tok::arrow) &&
3356 Current->isNot(TT_TrailingReturnArrow)) {
3357 return PrecedenceArrowAndPeriod;
3360 Current->isOneOf(Keywords.kw_extends, Keywords.kw_implements,
3361 Keywords.kw_throws)) {
3366 if (Style.isVerilog() && Current->is(tok::colon))
3372 void addFakeParenthesis(FormatToken *Start,
prec::Level Precedence,
3373 FormatToken *End =
nullptr) {
3378 if (Start->MacroParent)
3381 Start->FakeLParens.push_back(Precedence);
3383 Start->StartsBinaryExpression =
true;
3384 if (!End && Current)
3385 End = Current->getPreviousNonComment();
3389 End->EndsBinaryExpression =
true;
3395 void parseUnaryOperator() {
3396 SmallVector<FormatToken *, 2> Tokens;
3397 while (Current && Current->is(TT_UnaryOperator)) {
3398 Tokens.push_back(Current);
3401 parse(PrecedenceArrowAndPeriod);
3402 for (FormatToken *Token : reverse(Tokens)) {
3408 void parseConditionalExpr() {
3409 while (Current && Current->isTrailingComment())
3411 FormatToken *Start = Current;
3413 if (!Current || Current->isNot(tok::question))
3417 if (!Current || Current->isNot(TT_ConditionalExpr))
3424 void next(
bool SkipPastLeadingComments =
true) {
3426 Current = Current->Next;
3428 (Current->NewlinesBefore == 0 || SkipPastLeadingComments) &&
3429 Current->isTrailingComment()) {
3430 Current = Current->Next;
3436 FormatToken *verilogGroupDecl(FormatToken *FirstOfType,
3437 FormatToken *PreviousComma) {
3441 FormatToken *Start = Current;
3444 while (Start->startsSequence(tok::l_paren, tok::star)) {
3445 if (!(Start = Start->MatchingParen) ||
3446 !(Start = Start->getNextNonComment())) {
3451 FormatToken *Tok = Start;
3453 if (Tok->is(Keywords.kw_assign))
3454 Tok = Tok->getNextNonComment();
3462 FormatToken *
First =
nullptr;
3464 FormatToken *Next = Tok->getNextNonComment();
3466 if (Tok->is(tok::hash)) {
3471 Tok = Tok->getNextNonComment();
3472 }
else if (Tok->is(tok::hashhash)) {
3476 Tok = Tok->getNextNonComment();
3477 }
else if (Keywords.isVerilogQualifier(*Tok) ||
3478 Keywords.isVerilogIdentifier(*Tok)) {
3482 while (Tok && Tok->isOneOf(tok::period, tok::coloncolon) &&
3483 (Tok = Tok->getNextNonComment())) {
3484 if (Keywords.isVerilogIdentifier(*Tok))
3485 Tok = Tok->getNextNonComment();
3489 }
else if (Tok->is(tok::l_paren)) {
3494 Keywords.kw_highz0, Keywords.kw_highz1, Keywords.kw_large,
3495 Keywords.kw_medium, Keywords.kw_pull0, Keywords.kw_pull1,
3496 Keywords.kw_small, Keywords.kw_strong0, Keywords.kw_strong1,
3497 Keywords.kw_supply0, Keywords.kw_supply1, Keywords.kw_weak0,
3498 Keywords.kw_weak1)) {
3499 Tok->setType(TT_VerilogStrength);
3500 Tok = Tok->MatchingParen;
3502 Tok->setType(TT_VerilogStrength);
3503 Tok = Tok->getNextNonComment();
3508 }
else if (Tok->is(Keywords.kw_verilogHash)) {
3510 if (Next->is(tok::l_paren))
3511 Next = Next->MatchingParen;
3513 Tok = Next->getNextNonComment();
3520 FormatToken *Second =
nullptr;
3522 while (Tok && Tok->is(tok::l_square) && (Tok = Tok->MatchingParen))
3523 Tok = Tok->getNextNonComment();
3524 if (Tok && (Tok->is(tok::hash) || Keywords.isVerilogIdentifier(*Tok)))
3529 FormatToken *TypedName =
nullptr;
3533 First->setType(TT_VerilogDimensionedTypeName);
3534 }
else if (
First != Start) {
3542 if (TypedName->is(TT_Unknown))
3543 TypedName->setType(TT_StartOfName);
3545 if (FirstOfType && PreviousComma) {
3546 PreviousComma->setType(TT_VerilogTypeComma);
3547 addFakeParenthesis(FirstOfType,
prec::Comma, PreviousComma->Previous);
3550 FirstOfType = TypedName;
3557 while (Current && Current != FirstOfType) {
3558 if (Current->opensScope()) {
3569 const FormatStyle &Style;
3570 const AdditionalKeywords &Keywords;
3571 const AnnotatedLine &Line;
3572 FormatToken *Current;
3581 assert(
Line->First);
3588 Line->First->OriginalColumn) {
3589 const bool PPDirectiveOrImportStmt =
3592 if (PPDirectiveOrImportStmt)
3598 PPDirectiveOrImportStmt
3600 : NextNonCommentLine->
Level;
3602 NextNonCommentLine =
Line->First->isNot(tok::r_brace) ?
Line :
nullptr;
3611 for (
const auto *Tok =
Line.First; Tok; Tok = Tok->Next)
3620 for (
FormatToken *Tok =
Line.getFirstNonComment(), *Name =
nullptr; Tok;
3621 Tok = Tok->getNextNonComment()) {
3623 if (Tok->is(tok::l_square) && Tok->is(TT_AttributeSquare)) {
3632 if (Tok->is(tok::l_paren) && Tok->is(TT_Unknown) && Tok->MatchingParen) {
3640 if (Tok->isOneOf(tok::kw_friend, tok::kw_inline, tok::kw_virtual,
3641 tok::kw_constexpr, tok::kw_consteval, tok::kw_explicit)) {
3646 if (Tok->is(tok::coloncolon)) {
3653 while (Tok->startsSequence(tok::identifier, tok::coloncolon)) {
3655 Tok = Tok->Next->Next;
3661 if (Tok->is(tok::tilde)) {
3668 if (Tok->isNot(tok::identifier) || Tok->isNot(TT_Unknown))
3679 assert(Tok && Tok->
is(tok::identifier));
3682 if (Prev && Prev->is(tok::tilde))
3685 if (!Prev || !Prev->endsSequence(tok::coloncolon, tok::identifier))
3688 assert(Prev->Previous);
3689 return Prev->Previous->TokenText == Tok->
TokenText;
3693 if (!
Line.InMacroBody)
3694 MacroBodyScopes.clear();
3696 auto &ScopeStack =
Line.InMacroBody ? MacroBodyScopes : Scopes;
3697 AnnotatingParser
Parser(Style,
Line, Keywords, ScopeStack);
3700 if (!
Line.Children.empty()) {
3703 for (
auto &Child :
Line.Children) {
3704 if (InRequiresExpression &&
3705 !Child->First->isOneOf(tok::kw_typename, tok::kw_requires,
3706 TT_CompoundRequirementLBrace)) {
3712 if (!ScopeStack.empty())
3713 ScopeStack.pop_back();
3726 ExpressionParser ExprParser(Style, Keywords,
Line);
3732 if (Tok && ((!ScopeStack.empty() && ScopeStack.back() ==
ST_Class) ||
3734 Tok->setFinalizedType(TT_CtorDtorDeclName);
3735 assert(OpeningParen);
3740 if (
Line.startsWith(TT_ObjCMethodSpecifier))
3742 else if (
Line.startsWith(TT_ObjCDecl))
3744 else if (
Line.startsWith(TT_ObjCProperty))
3748 First->SpacesRequiredBefore = 1;
3749 First->CanBreakBefore =
First->MustBreakBefore;
3758 if (Current.
is(TT_FunctionDeclarationName))
3767 if (Prev->is(tok::coloncolon))
3768 Prev = Prev->Previous;
3775 if (
const auto *PrevPrev =
Previous.getPreviousNonComment();
3776 PrevPrev && PrevPrev->is(TT_ObjCDecl)) {
3780 auto skipOperatorName =
3782 for (; Next; Next = Next->Next) {
3783 if (Next->is(TT_OverloadedOperatorLParen))
3785 if (Next->is(TT_OverloadedOperator))
3787 if (Next->isOneOf(tok::kw_new, tok::kw_delete)) {
3790 Next->Next->startsSequence(tok::l_square, tok::r_square)) {
3791 Next = Next->Next->Next;
3795 if (Next->startsSequence(tok::l_square, tok::r_square)) {
3800 if ((Next->isTypeName(LangOpts) || Next->is(tok::identifier)) &&
3801 Next->Next && Next->Next->isPointerOrReference()) {
3806 if (Next->is(TT_TemplateOpener) && Next->MatchingParen) {
3807 Next = Next->MatchingParen;
3816 const auto *Next = Current.
Next;
3817 const bool IsCpp = LangOpts.CXXOperatorNames;
3820 if (Current.
is(tok::kw_operator)) {
3821 if (
Previous.Tok.getIdentifierInfo() &&
3822 !
Previous.isOneOf(tok::kw_return, tok::kw_co_return)) {
3827 assert(
Previous.MatchingParen->is(tok::l_paren));
3828 assert(
Previous.MatchingParen->is(TT_TypeDeclarationParen));
3833 Next = skipOperatorName(Next);
3837 for (; Next; Next = Next->Next) {
3838 if (Next->is(TT_TemplateOpener) && Next->MatchingParen) {
3839 Next = Next->MatchingParen;
3840 }
else if (Next->is(tok::coloncolon)) {
3844 if (Next->is(tok::kw_operator)) {
3845 Next = skipOperatorName(Next->Next);
3848 if (Next->isNot(tok::identifier))
3850 }
else if (isCppAttribute(IsCpp, *Next)) {
3851 Next = Next->MatchingParen;
3854 }
else if (Next->is(tok::l_paren)) {
3863 if (!Next || Next->isNot(tok::l_paren) || !Next->MatchingParen)
3865 ClosingParen = Next->MatchingParen;
3866 assert(ClosingParen->
is(tok::r_paren));
3868 if (
Line.Last->is(tok::l_brace))
3870 if (Next->Next == ClosingParen)
3873 if (ClosingParen->
Next && ClosingParen->
Next->
is(TT_PointerOrReference))
3886 if (IsCpp && Next->Next && Next->Next->is(tok::identifier) &&
3887 !
Line.endsWith(tok::semi)) {
3891 for (
const FormatToken *Tok = Next->Next; Tok && Tok != ClosingParen;
3893 if (Tok->is(TT_TypeDeclarationParen))
3895 if (Tok->isOneOf(tok::l_paren, TT_TemplateOpener) && Tok->MatchingParen) {
3896 Tok = Tok->MatchingParen;
3899 if (Tok->is(tok::kw_const) || Tok->isTypeName(LangOpts) ||
3900 Tok->isOneOf(TT_PointerOrReference, TT_StartOfName, tok::ellipsis)) {
3903 if (Tok->isOneOf(tok::l_brace, TT_ObjCMethodExpr) || Tok->Tok.isLiteral())
3909bool TokenAnnotator::mustBreakForReturnType(
const AnnotatedLine &
Line)
const {
3910 assert(
Line.MightBeFunctionDecl);
3918 switch (Style.BreakAfterReturnType) {
3928 return Line.mightBeFunctionDefinition();
3938 Line.Computed =
true;
3946 :
Line.FirstStartColumn +
First->ColumnWidth;
3947 bool AlignArrayOfStructures =
3950 if (AlignArrayOfStructures)
3951 calculateArrayInitializerColumnList(
Line);
3953 const auto *FirstNonComment =
Line.getFirstNonComment();
3954 bool SeenName =
false;
3955 bool LineIsFunctionDeclaration =
false;
3959 for (
auto *Tok = FirstNonComment ? FirstNonComment->Next :
nullptr; Tok;
3961 if (Tok->is(TT_StartOfName))
3963 if (Tok->Previous->EndsCppAttributeGroup)
3964 AfterLastAttribute = Tok;
3965 if (
const bool IsCtorOrDtor = Tok->is(TT_CtorDtorDeclName);
3970 LineIsFunctionDeclaration =
true;
3974 assert(OpeningParen);
3975 if (OpeningParen->is(TT_Unknown))
3976 OpeningParen->setType(TT_FunctionDeclarationLParen);
3983 (LineIsFunctionDeclaration ||
3984 (FirstNonComment && FirstNonComment->is(TT_CtorDtorDeclName))) &&
3985 Line.endsWith(tok::semi, tok::r_brace)) {
3986 auto *Tok =
Line.Last->Previous;
3987 while (Tok->isNot(tok::r_brace))
3988 Tok = Tok->Previous;
3989 if (
auto *LBrace = Tok->MatchingParen; LBrace) {
3990 assert(LBrace->is(tok::l_brace));
3993 LBrace->setFinalizedType(TT_FunctionLBrace);
3997 if (IsCpp && SeenName && AfterLastAttribute &&
4000 if (LineIsFunctionDeclaration)
4001 Line.ReturnTypeWrapped =
true;
4005 if (!LineIsFunctionDeclaration) {
4007 for (
const auto *Tok = FirstNonComment; Tok; Tok = Tok->Next) {
4008 if (Tok->isNot(tok::kw_operator))
4012 }
while (Tok && Tok->isNot(TT_OverloadedOperatorLParen));
4013 if (!Tok || !Tok->MatchingParen)
4015 const auto *LeftParen = Tok;
4016 for (Tok = Tok->Next; Tok && Tok != LeftParen->MatchingParen;
4018 if (Tok->isNot(tok::identifier))
4020 auto *Next = Tok->Next;
4021 const bool NextIsBinaryOperator =
4022 Next && Next->isPointerOrReference() && Next->Next &&
4023 Next->Next->is(tok::identifier);
4024 if (!NextIsBinaryOperator)
4026 Next->setType(TT_BinaryOperator);
4030 }
else if (ClosingParen) {
4031 for (
auto *Tok = ClosingParen->
Next; Tok; Tok = Tok->
Next) {
4032 if (Tok->is(TT_CtorInitializerColon))
4034 if (Tok->is(tok::arrow)) {
4035 Tok->setType(TT_TrailingReturnArrow);
4038 if (Tok->isNot(TT_TrailingAnnotation))
4040 const auto *Next = Tok->Next;
4041 if (!Next || Next->isNot(tok::l_paren))
4043 Tok = Next->MatchingParen;
4050 bool InFunctionDecl =
Line.MightBeFunctionDecl;
4051 for (
auto *Current =
First->Next; Current; Current = Current->Next) {
4053 if (Current->is(TT_LineComment)) {
4055 Current->SpacesRequiredBefore =
4056 (Style.Cpp11BracedListStyle && !Style.SpacesInParensOptions.Other)
4059 }
else if (Prev->
is(TT_VerilogMultiLineListLParen)) {
4060 Current->SpacesRequiredBefore = 0;
4062 Current->SpacesRequiredBefore = Style.SpacesBeforeTrailingComments;
4072 if (!Current->HasUnescapedNewline) {
4075 if (
Parameter->isOneOf(tok::comment, tok::r_brace))
4078 if (
Parameter->Previous->isNot(TT_CtorInitializerComma) &&
4086 }
else if (!Current->Finalized && Current->SpacesRequiredBefore == 0 &&
4087 spaceRequiredBefore(
Line, *Current)) {
4088 Current->SpacesRequiredBefore = 1;
4091 const auto &Children = Prev->
Children;
4092 if (!Children.empty() && Children.back()->Last->is(TT_LineComment)) {
4093 Current->MustBreakBefore =
true;
4095 Current->MustBreakBefore =
4096 Current->MustBreakBefore || mustBreakBefore(
Line, *Current);
4097 if (!Current->MustBreakBefore && InFunctionDecl &&
4098 Current->is(TT_FunctionDeclarationName)) {
4099 Current->MustBreakBefore = mustBreakForReturnType(
Line);
4103 Current->CanBreakBefore =
4104 Current->MustBreakBefore || canBreakBefore(
Line, *Current);
4105 unsigned ChildSize = 0;
4111 if (Current->MustBreakBefore || Prev->
Children.size() > 1 ||
4113 Prev->
Children[0]->First->MustBreakBefore) ||
4114 Current->IsMultiline) {
4115 Current->TotalLength = Prev->
TotalLength + Style.ColumnLimit;
4117 Current->TotalLength = Prev->
TotalLength + Current->ColumnWidth +
4118 ChildSize + Current->SpacesRequiredBefore;
4121 if (Current->is(TT_CtorInitializerColon))
4122 InFunctionDecl =
false;
4133 Current->SplitPenalty = splitPenalty(
Line, *Current, InFunctionDecl);
4135 Current->is(TT_SelectorName) && Current->ParameterIndex > 0) {
4136 if (Current->ParameterIndex == 1)
4137 Current->SplitPenalty += 5 * Current->BindingStrength;
4139 Current->SplitPenalty += 20 * Current->BindingStrength;
4143 calculateUnbreakableTailLengths(
Line);
4144 unsigned IndentLevel =
Line.Level;
4145 for (
auto *Current =
First; Current; Current = Current->Next) {
4147 Current->Role->precomputeFormattingInfos(Current);
4148 if (Current->MatchingParen &&
4149 Current->MatchingParen->opensBlockOrBlockTypeList(Style) &&
4153 Current->IndentLevel = IndentLevel;
4154 if (Current->opensBlockOrBlockTypeList(Style))
4158 LLVM_DEBUG({ printDebugInfo(
Line); });
4161void TokenAnnotator::calculateUnbreakableTailLengths(
4163 unsigned UnbreakableTailLength = 0;
4166 Current->UnbreakableTailLength = UnbreakableTailLength;
4167 if (Current->CanBreakBefore ||
4168 Current->isOneOf(tok::comment, tok::string_literal)) {
4169 UnbreakableTailLength = 0;
4171 UnbreakableTailLength +=
4172 Current->ColumnWidth + Current->SpacesRequiredBefore;
4174 Current = Current->Previous;
4178void TokenAnnotator::calculateArrayInitializerColumnList(
4179 AnnotatedLine &
Line)
const {
4182 auto *CurrentToken =
Line.First;
4183 CurrentToken->ArrayInitializerLineStart =
true;
4185 while (CurrentToken && CurrentToken !=
Line.Last) {
4186 if (CurrentToken->is(tok::l_brace)) {
4187 CurrentToken->IsArrayInitializer =
true;
4188 if (CurrentToken->Next)
4189 CurrentToken->Next->MustBreakBefore =
true;
4191 calculateInitializerColumnList(
Line, CurrentToken->Next, Depth + 1);
4193 CurrentToken = CurrentToken->Next;
4198FormatToken *TokenAnnotator::calculateInitializerColumnList(
4199 AnnotatedLine &
Line, FormatToken *CurrentToken,
unsigned Depth)
const {
4200 while (CurrentToken && CurrentToken !=
Line.Last) {
4201 if (CurrentToken->is(tok::l_brace))
4203 else if (CurrentToken->is(tok::r_brace))
4205 if (Depth == 2 && CurrentToken->isOneOf(tok::l_brace, tok::comma)) {
4206 CurrentToken = CurrentToken->Next;
4209 CurrentToken->StartsColumn =
true;
4210 CurrentToken = CurrentToken->Previous;
4212 CurrentToken = CurrentToken->Next;
4214 return CurrentToken;
4217unsigned TokenAnnotator::splitPenalty(
const AnnotatedLine &
Line,
4218 const FormatToken &Tok,
4219 bool InFunctionDecl)
const {
4220 const FormatToken &
Left = *Tok.Previous;
4221 const FormatToken &
Right = Tok;
4223 if (
Left.is(tok::semi))
4228 if (
Right.isOneOf(Keywords.kw_extends, Keywords.kw_throws))
4230 if (
Right.is(Keywords.kw_implements))
4232 if (
Left.is(tok::comma) &&
Left.NestingLevel == 0)
4234 }
else if (Style.isJavaScript()) {
4235 if (
Right.is(Keywords.kw_function) &&
Left.isNot(tok::comma))
4237 if (
Left.is(TT_JsTypeColon))
4239 if ((
Left.is(TT_TemplateString) &&
Left.TokenText.ends_with(
"${")) ||
4240 (
Right.is(TT_TemplateString) &&
Right.TokenText.starts_with(
"}"))) {
4244 if (
Left.opensScope() &&
Right.closesScope())
4247 if (
Right.is(tok::l_square))
4249 if (
Right.is(tok::period))
4253 if (
Right.is(tok::identifier) &&
Right.Next &&
Right.Next->is(TT_DictLiteral))
4255 if (
Right.is(tok::l_square)) {
4256 if (
Left.is(tok::r_square))
4259 if (
Right.is(TT_LambdaLSquare) &&
Left.is(tok::equal))
4261 if (!
Right.isOneOf(TT_ObjCMethodExpr, TT_LambdaLSquare,
4262 TT_ArrayInitializerLSquare,
4263 TT_DesignatedInitializerLSquare, TT_AttributeSquare)) {
4268 if (
Left.is(tok::coloncolon))
4269 return Style.PenaltyBreakScopeResolution;
4270 if (
Right.isOneOf(TT_StartOfName, TT_FunctionDeclarationName) ||
4271 Right.is(tok::kw_operator)) {
4272 if (
Line.startsWith(tok::kw_for) &&
Right.PartOfMultiVariableDeclStmt)
4274 if (
Left.is(TT_StartOfName))
4276 if (InFunctionDecl &&
Right.NestingLevel == 0)
4277 return Style.PenaltyReturnTypeOnItsOwnLine;
4280 if (
Right.is(TT_PointerOrReference))
4282 if (
Right.is(TT_LambdaArrow))
4284 if (
Left.is(tok::equal) &&
Right.is(tok::l_brace))
4286 if (
Left.is(TT_CastRParen))
4288 if (
Left.isOneOf(tok::kw_class, tok::kw_struct, tok::kw_union))
4290 if (
Left.is(tok::comment))
4293 if (
Left.isOneOf(TT_RangeBasedForLoopColon, TT_InheritanceColon,
4294 TT_CtorInitializerColon)) {
4298 if (
Right.isMemberAccess()) {
4318 return !
Right.NextOperator || !
Right.NextOperator->Previous->closesScope()
4323 if (
Right.is(TT_TrailingAnnotation) &&
4324 (!
Right.Next ||
Right.Next->isNot(tok::l_paren))) {
4327 if (
Line.startsWith(TT_ObjCMethodSpecifier))
4334 bool is_short_annotation =
Right.TokenText.size() < 10;
4335 return (
Left.is(tok::r_paren) ? 100 : 120) + (is_short_annotation ? 50 : 0);
4339 if (
Line.startsWith(tok::kw_for) &&
Left.is(tok::equal))
4344 if (
Right.is(TT_SelectorName))
4346 if (
Left.is(tok::colon) &&
Left.is(TT_ObjCMethodExpr))
4347 return Line.MightBeFunctionDecl ? 50 : 500;
4353 Left.Previous->isOneOf(tok::identifier, tok::greater)) {
4357 if (
Left.is(tok::l_paren) && Style.PenaltyBreakOpenParenthesis != 0)
4358 return Style.PenaltyBreakOpenParenthesis;
4359 if (
Left.is(tok::l_paren) && InFunctionDecl &&
4363 if (
Left.is(tok::l_paren) &&
Left.Previous &&
4364 (
Left.Previous->isOneOf(tok::kw_for, tok::kw__Generic) ||
4365 Left.Previous->isIf())) {
4368 if (
Left.is(tok::equal) && InFunctionDecl)
4370 if (
Right.is(tok::r_brace))
4372 if (
Left.is(TT_TemplateOpener))
4374 if (
Left.opensScope()) {
4379 (
Left.ParameterCount <= 1 || Style.AllowAllArgumentsOnNextLine)) {
4382 if (
Left.is(tok::l_brace) && !Style.Cpp11BracedListStyle)
4384 return Left.ParameterCount > 1 ? Style.PenaltyBreakBeforeFirstCallParameter
4387 if (
Left.is(TT_JavaAnnotation))
4390 if (
Left.is(TT_UnaryOperator))
4392 if (
Left.isOneOf(tok::plus, tok::comma) &&
Left.Previous &&
4393 Left.Previous->isLabelString() &&
4394 (
Left.NextOperator ||
Left.OperatorIndex != 0)) {
4397 if (
Right.is(tok::plus) &&
Left.isLabelString() &&
4398 (
Right.NextOperator ||
Right.OperatorIndex != 0)) {
4401 if (
Left.is(tok::comma))
4403 if (
Right.is(tok::lessless) &&
Left.isLabelString() &&
4404 (
Right.NextOperator ||
Right.OperatorIndex != 1)) {
4407 if (
Right.is(tok::lessless)) {
4409 if (
Left.isNot(tok::r_paren) ||
Right.OperatorIndex > 0) {
4415 if (
Left.ClosesTemplateDeclaration)
4416 return Style.PenaltyBreakTemplateDeclaration;
4417 if (
Left.ClosesRequiresClause)
4419 if (
Left.is(TT_ConditionalExpr))
4425 return Style.PenaltyBreakAssignment;
4432bool TokenAnnotator::spaceRequiredBeforeParens(
const FormatToken &Right)
const {
4435 if (
Right.is(TT_OverloadedOperatorLParen) &&
4436 Style.SpaceBeforeParensOptions.AfterOverloadedOperator) {
4439 if (Style.SpaceBeforeParensOptions.BeforeNonEmptyParentheses &&
4440 Right.ParameterCount > 0) {
4446bool TokenAnnotator::spaceRequiredBetween(
const AnnotatedLine &
Line,
4447 const FormatToken &Left,
4448 const FormatToken &Right)
const {
4449 if (
Left.is(tok::kw_return) &&
4450 !
Right.isOneOf(tok::semi, tok::r_paren, tok::hashhash)) {
4453 if (
Left.is(tok::kw_throw) &&
Right.is(tok::l_paren) &&
Right.MatchingParen &&
4454 Right.MatchingParen->is(TT_CastRParen)) {
4460 Left.Tok.getObjCKeywordID() == tok::objc_property) {
4463 if (
Right.is(tok::hashhash))
4464 return Left.is(tok::hash);
4465 if (
Left.isOneOf(tok::hashhash, tok::hash))
4466 return Right.is(tok::hash);
4468 Right.MatchingParen == &Left &&
Line.Children.empty()) {
4469 return Style.SpaceInEmptyBlock;
4472 if ((
Left.is(tok::l_paren) &&
Right.is(tok::r_paren)) ||
4475 return Style.SpacesInParensOptions.InEmptyParentheses;
4477 if (Style.SpacesInParensOptions.ExceptDoubleParentheses &&
4478 Left.is(tok::r_paren) &&
Right.is(tok::r_paren)) {
4479 auto *InnerLParen =
Left.MatchingParen;
4480 if (InnerLParen && InnerLParen->Previous ==
Right.MatchingParen) {
4481 InnerLParen->SpacesRequiredBefore = 0;
4485 const FormatToken *LeftParen =
nullptr;
4486 if (
Left.is(tok::l_paren))
4488 else if (
Right.is(tok::r_paren) &&
Right.MatchingParen)
4489 LeftParen =
Right.MatchingParen;
4490 if (LeftParen && (LeftParen->is(TT_ConditionLParen) ||
4491 (LeftParen->Previous &&
4492 isKeywordWithCondition(*LeftParen->Previous)))) {
4493 return Style.SpacesInParensOptions.InConditionalStatements;
4498 if (
Left.is(tok::kw_auto) &&
Right.isOneOf(TT_LambdaLBrace, TT_FunctionLBrace,
4500 TT_FunctionTypeLParen)) {
4505 if (
Left.is(tok::kw_auto) &&
Right.isOneOf(tok::l_paren, tok::l_brace))
4508 const auto *BeforeLeft =
Left.Previous;
4511 if (
Right.is(tok::l_paren) &&
Left.is(tok::kw_co_await) && BeforeLeft &&
4512 BeforeLeft->is(tok::kw_operator)) {
4516 if (
Left.isOneOf(tok::kw_co_await, tok::kw_co_yield, tok::kw_co_return) &&
4517 !
Right.isOneOf(tok::semi, tok::r_paren)) {
4521 if (
Left.is(tok::l_paren) ||
Right.is(tok::r_paren)) {
4522 return (
Right.is(TT_CastRParen) ||
4523 (
Left.MatchingParen &&
Left.MatchingParen->is(TT_CastRParen)))
4524 ? Style.SpacesInParensOptions.InCStyleCasts
4525 : Style.SpacesInParensOptions.Other;
4527 if (
Right.isOneOf(tok::semi, tok::comma))
4530 bool IsLightweightGeneric =
Right.MatchingParen &&
4531 Right.MatchingParen->Next &&
4532 Right.MatchingParen->Next->is(tok::colon);
4533 return !IsLightweightGeneric && Style.ObjCSpaceBeforeProtocolList;
4535 if (
Right.is(tok::less) &&
Left.is(tok::kw_template))
4536 return Style.SpaceAfterTemplateKeyword;
4537 if (
Left.isOneOf(tok::exclaim, tok::tilde))
4539 if (
Left.is(tok::at) &&
4540 Right.isOneOf(tok::identifier, tok::string_literal, tok::char_constant,
4541 tok::numeric_constant, tok::l_paren, tok::l_brace,
4542 tok::kw_true, tok::kw_false)) {
4545 if (
Left.is(tok::colon))
4546 return Left.isNot(TT_ObjCMethodExpr);
4547 if (
Left.is(tok::coloncolon))
4549 if (
Left.is(tok::less) ||
Right.isOneOf(tok::greater, tok::less)) {
4552 (
Left.is(TT_DictLiteral) ||
Right.is(TT_DictLiteral)))) {
4554 if (
Left.is(tok::less) &&
Right.is(tok::greater))
4556 return !Style.Cpp11BracedListStyle;
4559 if (
Right.isNot(TT_OverloadedOperatorLParen))
4562 if (
Right.is(tok::ellipsis)) {
4563 return Left.Tok.isLiteral() || (
Left.is(tok::identifier) && BeforeLeft &&
4564 BeforeLeft->is(tok::kw_case));
4566 if (
Left.is(tok::l_square) &&
Right.is(tok::amp))
4567 return Style.SpacesInSquareBrackets;
4568 if (
Right.is(TT_PointerOrReference)) {
4569 if (
Left.is(tok::r_paren) &&
Line.MightBeFunctionDecl) {
4570 if (!
Left.MatchingParen)
4572 FormatToken *TokenBeforeMatchingParen =
4573 Left.MatchingParen->getPreviousNonComment();
4574 if (!TokenBeforeMatchingParen ||
Left.isNot(TT_TypeDeclarationParen))
4582 (
Left.is(TT_AttributeRParen) ||
4583 Left.canBePointerOrReferenceQualifier())) {
4586 if (
Left.Tok.isLiteral())
4589 if (
Left.isTypeOrIdentifier(LangOpts) &&
Right.Next &&
Right.Next->Next &&
4590 Right.Next->Next->is(TT_RangeBasedForLoopColon)) {
4591 return getTokenPointerOrReferenceAlignment(Right) !=
4594 return !
Left.isOneOf(TT_PointerOrReference, tok::l_paren) &&
4595 (getTokenPointerOrReferenceAlignment(Right) !=
4597 (
Line.IsMultiVariableDeclStmt &&
4598 (
Left.NestingLevel == 0 ||
4599 (
Left.NestingLevel == 1 && startsWithInitStatement(
Line)))));
4601 if (
Right.is(TT_FunctionTypeLParen) &&
Left.isNot(tok::l_paren) &&
4602 (
Left.isNot(TT_PointerOrReference) ||
4604 !
Line.IsMultiVariableDeclStmt))) {
4607 if (
Left.is(TT_PointerOrReference)) {
4612 Right.canBePointerOrReferenceQualifier()) {
4616 if (
Right.Tok.isLiteral())
4619 if (
Right.is(TT_BlockComment))
4623 if (
Right.isOneOf(Keywords.kw_override, Keywords.kw_final, tok::kw_noexcept,
4624 TT_RequiresClause) &&
4625 Right.isNot(TT_StartOfName)) {
4632 if (BeforeLeft && BeforeLeft->isTypeOrIdentifier(LangOpts) &&
Right.Next &&
4633 Right.Next->is(TT_RangeBasedForLoopColon)) {
4634 return getTokenPointerOrReferenceAlignment(Left) !=
4637 if (
Right.isOneOf(TT_PointerOrReference, TT_ArraySubscriptLSquare,
4649 if (
Line.IsMultiVariableDeclStmt &&
4650 (
Left.NestingLevel ==
Line.First->NestingLevel ||
4651 ((
Left.NestingLevel ==
Line.First->NestingLevel + 1) &&
4652 startsWithInitStatement(
Line)))) {
4657 if (BeforeLeft->is(tok::coloncolon)) {
4658 if (
Left.isNot(tok::star))
4661 if (!
Right.startsSequence(tok::identifier, tok::r_paren))
4664 const auto *LParen =
Right.Next->MatchingParen;
4665 return !LParen || LParen->isNot(TT_FunctionTypeLParen);
4667 return !BeforeLeft->isOneOf(tok::l_paren, tok::l_square);
4670 if (
Left.is(tok::ellipsis) && BeforeLeft &&
4671 BeforeLeft->isPointerOrReference()) {
4675 if (
Right.is(tok::star) &&
Left.is(tok::l_paren))
4677 if (
Left.is(tok::star) &&
Right.isPointerOrReference())
4679 if (
Right.isPointerOrReference()) {
4690 if (
Previous->is(tok::coloncolon)) {
4709 if (
Previous->endsSequence(tok::kw_operator))
4713 (Style.SpaceAroundPointerQualifiers ==
4719 if (Style.isCSharp() &&
Left.is(Keywords.kw_is) &&
Right.is(tok::l_square))
4721 const auto SpaceRequiredForArrayInitializerLSquare =
4722 [](
const FormatToken &LSquareTok,
const FormatStyle &Style) {
4723 return Style.SpacesInContainerLiterals ||
4724 (Style.isProto() && !Style.Cpp11BracedListStyle &&
4725 LSquareTok.endsSequence(tok::l_square, tok::colon,
4728 if (
Left.is(tok::l_square)) {
4729 return (
Left.is(TT_ArrayInitializerLSquare) &&
Right.isNot(tok::r_square) &&
4730 SpaceRequiredForArrayInitializerLSquare(Left, Style)) ||
4731 (
Left.isOneOf(TT_ArraySubscriptLSquare, TT_StructuredBindingLSquare,
4732 TT_LambdaLSquare) &&
4733 Style.SpacesInSquareBrackets &&
Right.isNot(tok::r_square));
4735 if (
Right.is(tok::r_square)) {
4736 return Right.MatchingParen &&
4737 ((
Right.MatchingParen->is(TT_ArrayInitializerLSquare) &&
4738 SpaceRequiredForArrayInitializerLSquare(*
Right.MatchingParen,
4740 (Style.SpacesInSquareBrackets &&
4741 Right.MatchingParen->isOneOf(TT_ArraySubscriptLSquare,
4742 TT_StructuredBindingLSquare,
4743 TT_LambdaLSquare)));
4745 if (
Right.is(tok::l_square) &&
4746 !
Right.isOneOf(TT_ObjCMethodExpr, TT_LambdaLSquare,
4747 TT_DesignatedInitializerLSquare,
4748 TT_StructuredBindingLSquare, TT_AttributeSquare) &&
4749 !
Left.isOneOf(tok::numeric_constant, TT_DictLiteral) &&
4750 !(
Left.isNot(tok::r_square) && Style.SpaceBeforeSquareBrackets &&
4751 Right.is(TT_ArraySubscriptLSquare))) {
4754 if (
Left.is(tok::l_brace) &&
Right.is(tok::r_brace))
4755 return !
Left.Children.empty();
4757 (
Right.is(tok::r_brace) &&
Right.MatchingParen &&
4759 return !Style.Cpp11BracedListStyle || Style.SpacesInParensOptions.Other;
4761 if (
Left.is(TT_BlockComment)) {
4763 return Style.isJavaScript() || !
Left.TokenText.ends_with(
"=*/");
4768 if (
Left.is(TT_TemplateCloser) &&
Right.is(TT_AttributeSquare))
4771 if (
Right.is(tok::l_paren)) {
4772 if (
Left.is(TT_TemplateCloser) &&
Right.isNot(TT_FunctionTypeLParen))
4773 return spaceRequiredBeforeParens(Right);
4774 if (
Left.isOneOf(TT_RequiresClause,
4775 TT_RequiresClauseInARequiresExpression)) {
4776 return Style.SpaceBeforeParensOptions.AfterRequiresInClause ||
4777 spaceRequiredBeforeParens(Right);
4779 if (
Left.is(TT_RequiresExpression)) {
4780 return Style.SpaceBeforeParensOptions.AfterRequiresInExpression ||
4781 spaceRequiredBeforeParens(Right);
4783 if (
Left.is(TT_AttributeRParen) ||
4784 (
Left.is(tok::r_square) &&
Left.is(TT_AttributeSquare))) {
4787 if (
Left.is(TT_ForEachMacro)) {
4788 return Style.SpaceBeforeParensOptions.AfterForeachMacros ||
4789 spaceRequiredBeforeParens(Right);
4791 if (
Left.is(TT_IfMacro)) {
4792 return Style.SpaceBeforeParensOptions.AfterIfMacros ||
4793 spaceRequiredBeforeParens(Right);
4796 Left.isOneOf(tok::kw_new, tok::kw_delete) &&
4797 Right.isNot(TT_OverloadedOperatorLParen) &&
4798 !(
Line.MightBeFunctionDecl &&
Left.is(TT_FunctionDeclarationName))) {
4799 const auto *RParen =
Right.MatchingParen;
4800 return Style.SpaceBeforeParensOptions.AfterPlacementOperator ||
4801 (RParen && RParen->is(TT_CastRParen));
4805 if (
Left.is(tok::semi))
4807 if (
Left.isOneOf(tok::pp_elif, tok::kw_for, tok::kw_while, tok::kw_switch,
4808 tok::kw_case, TT_ForEachMacro, TT_ObjCForIn) ||
4810 Right.is(TT_ConditionLParen)) {
4811 return Style.SpaceBeforeParensOptions.AfterControlStatements ||
4812 spaceRequiredBeforeParens(Right);
4817 if (
Right.is(TT_OverloadedOperatorLParen))
4818 return spaceRequiredBeforeParens(Right);
4820 if (
Line.MightBeFunctionDecl &&
Right.is(TT_FunctionDeclarationLParen)) {
4821 if (spaceRequiredBeforeParens(Right))
4823 const auto &Options = Style.SpaceBeforeParensOptions;
4824 return Line.mightBeFunctionDefinition()
4825 ? Options.AfterFunctionDefinitionName
4826 : Options.AfterFunctionDeclarationName;
4830 Left.MatchingParen &&
Left.MatchingParen->is(TT_LambdaLSquare)) {
4831 return Style.SpaceBeforeParensOptions.AfterFunctionDefinitionName ||
4832 spaceRequiredBeforeParens(Right);
4834 if (!BeforeLeft || !BeforeLeft->isOneOf(tok::period, tok::arrow)) {
4835 if (
Left.isOneOf(tok::kw_try, Keywords.kw___except, tok::kw_catch)) {
4836 return Style.SpaceBeforeParensOptions.AfterControlStatements ||
4837 spaceRequiredBeforeParens(Right);
4839 if (
Left.isOneOf(tok::kw_new, tok::kw_delete)) {
4840 return ((!
Line.MightBeFunctionDecl || !BeforeLeft) &&
4842 spaceRequiredBeforeParens(Right);
4845 if (
Left.is(tok::r_square) &&
Left.MatchingParen &&
4846 Left.MatchingParen->Previous &&
4847 Left.MatchingParen->Previous->is(tok::kw_delete)) {
4849 spaceRequiredBeforeParens(Right);
4854 (
Left.Tok.getIdentifierInfo() ||
Left.is(tok::r_paren))) {
4855 return spaceRequiredBeforeParens(Right);
4859 if (
Left.is(tok::at) &&
Right.Tok.getObjCKeywordID() != tok::objc_not_keyword)
4861 if (
Right.is(TT_UnaryOperator)) {
4862 return !
Left.isOneOf(tok::l_paren, tok::l_square, tok::at) &&
4863 (
Left.isNot(tok::colon) ||
Left.isNot(TT_ObjCMethodExpr));
4869 if (!Style.isVerilog() &&
4870 (
Left.isOneOf(tok::identifier, tok::greater, tok::r_square,
4872 Left.isTypeName(LangOpts)) &&
4873 Right.is(tok::l_brace) &&
Right.getNextNonComment() &&
4877 if (
Left.is(tok::period) ||
Right.is(tok::period))
4881 if (
Right.is(tok::hash) &&
Left.is(tok::identifier) &&
4882 (
Left.TokenText ==
"L" ||
Left.TokenText ==
"u" ||
4883 Left.TokenText ==
"U" ||
Left.TokenText ==
"u8" ||
4884 Left.TokenText ==
"LR" ||
Left.TokenText ==
"uR" ||
4885 Left.TokenText ==
"UR" ||
Left.TokenText ==
"u8R")) {
4888 if (
Left.is(TT_TemplateCloser) &&
Left.MatchingParen &&
4889 Left.MatchingParen->Previous &&
4890 (
Left.MatchingParen->Previous->is(tok::period) ||
4891 Left.MatchingParen->Previous->is(tok::coloncolon))) {
4897 if (
Left.is(TT_TemplateCloser) &&
Right.is(tok::l_square))
4899 if (
Left.is(tok::l_brace) &&
Left.endsSequence(TT_DictLiteral, tok::at)) {
4903 if (
Right.is(tok::r_brace) &&
Right.MatchingParen &&
4904 Right.MatchingParen->endsSequence(TT_DictLiteral, tok::at)) {
4908 if (
Right.is(TT_TrailingAnnotation) &&
Right.isOneOf(tok::amp, tok::ampamp) &&
4909 Left.isOneOf(tok::kw_const, tok::kw_volatile) &&
4910 (!
Right.Next ||
Right.Next->is(tok::semi))) {
4920bool TokenAnnotator::spaceRequiredBefore(
const AnnotatedLine &
Line,
4921 const FormatToken &Right)
const {
4922 const FormatToken &
Left = *
Right.Previous;
4927 return Right.hasWhitespaceBefore();
4929 const bool IsVerilog = Style.isVerilog();
4930 assert(!IsVerilog || !IsCpp);
4933 if (Keywords.isWordLike(Right, IsVerilog) &&
4934 Keywords.isWordLike(Left, IsVerilog)) {
4940 if (
Left.is(tok::star) &&
Right.is(tok::comment))
4943 const auto *BeforeLeft =
Left.Previous;
4946 if (
Left.is(TT_OverloadedOperator) &&
4947 Right.isOneOf(TT_TemplateOpener, TT_TemplateCloser)) {
4951 if (
Right.is(tok::period) &&
Left.is(tok::numeric_constant))
4955 if (
Left.is(Keywords.kw_import) &&
Right.isOneOf(tok::less, tok::ellipsis))
4958 if (
Left.isOneOf(Keywords.kw_module, Keywords.kw_import) &&
4959 Right.is(TT_ModulePartitionColon)) {
4963 if (
Right.is(TT_AfterPPDirective))
4967 if (
Left.is(tok::identifier) &&
Right.is(TT_ModulePartitionColon))
4970 if (
Left.is(TT_ModulePartitionColon) &&
4971 Right.isOneOf(tok::identifier, tok::kw_private)) {
4974 if (
Left.is(tok::ellipsis) &&
Right.is(tok::identifier) &&
4975 Line.First->is(Keywords.kw_import)) {
4979 if (
Left.isOneOf(TT_AttributeRParen, TT_AttributeMacro) &&
4980 Right.is(tok::coloncolon)) {
4984 if (
Left.is(tok::kw_operator))
4985 return Right.is(tok::coloncolon);
4987 !
Left.opensScope() && Style.SpaceBeforeCpp11BracedList) {
4990 if (
Left.is(tok::less) &&
Left.is(TT_OverloadedOperator) &&
4991 Right.is(TT_TemplateOpener)) {
4995 if (
Left.is(tok::identifier) &&
Right.is(tok::numeric_constant))
4996 return Right.TokenText[0] !=
'.';
4998 if (
Left.Tok.getIdentifierInfo() &&
Right.Tok.isLiteral())
5000 }
else if (Style.isProto()) {
5001 if (
Right.is(tok::period) && !(BeforeLeft && BeforeLeft->is(tok::period)) &&
5002 Left.isOneOf(Keywords.kw_optional, Keywords.kw_required,
5003 Keywords.kw_repeated, Keywords.kw_extend)) {
5006 if (
Right.is(tok::l_paren) &&
5007 Left.isOneOf(Keywords.kw_returns, Keywords.kw_option)) {
5010 if (
Right.isOneOf(tok::l_brace, tok::less) &&
Left.is(TT_SelectorName))
5013 if (
Left.is(tok::slash) ||
Right.is(tok::slash))
5015 if (
Left.MatchingParen &&
5016 Left.MatchingParen->is(TT_ProtoExtensionLSquare) &&
5017 Right.isOneOf(tok::l_brace, tok::less)) {
5018 return !Style.Cpp11BracedListStyle;
5021 if (
Left.is(tok::percent))
5025 if (
Left.is(tok::numeric_constant) &&
Right.is(tok::percent))
5026 return Right.hasWhitespaceBefore();
5027 }
else if (Style.isJson()) {
5028 if (
Right.is(tok::colon) &&
Left.is(tok::string_literal))
5029 return Style.SpaceBeforeJsonColon;
5030 }
else if (Style.isCSharp()) {
5036 if (
Left.is(tok::kw_this) &&
Right.is(tok::l_square))
5040 if (
Left.is(tok::kw_new) &&
Right.is(tok::l_paren))
5044 if (
Right.is(tok::l_brace))
5048 if (
Left.is(tok::l_brace) &&
Right.isNot(tok::r_brace))
5051 if (
Left.isNot(tok::l_brace) &&
Right.is(tok::r_brace))
5055 if (
Left.is(TT_FatArrow) ||
Right.is(TT_FatArrow))
5059 if (
Left.is(TT_AttributeColon) ||
Right.is(TT_AttributeColon))
5063 if (
Left.is(TT_TemplateCloser) &&
Right.is(TT_StartOfName))
5067 if (
Left.is(tok::l_square) ||
Right.is(tok::r_square))
5068 return Style.SpacesInSquareBrackets;
5071 if (
Right.is(TT_CSharpNullable))
5075 if (
Right.is(TT_NonNullAssertion))
5079 if (
Left.is(tok::comma) &&
Right.is(tok::comma))
5083 if (
Left.is(Keywords.kw_var) &&
Right.is(tok::l_paren))
5087 if (
Right.is(tok::l_paren)) {
5088 if (
Left.isOneOf(tok::kw_using, Keywords.kw_async, Keywords.kw_when,
5089 Keywords.kw_lock)) {
5090 return Style.SpaceBeforeParensOptions.AfterControlStatements ||
5091 spaceRequiredBeforeParens(Right);
5097 if ((
Left.isAccessSpecifierKeyword() ||
5098 Left.isOneOf(tok::kw_virtual, tok::kw_extern, tok::kw_static,
5099 Keywords.kw_internal, Keywords.kw_abstract,
5100 Keywords.kw_sealed, Keywords.kw_override,
5101 Keywords.kw_async, Keywords.kw_unsafe)) &&
5102 Right.is(tok::l_paren)) {
5105 }
else if (Style.isJavaScript()) {
5106 if (
Left.is(TT_FatArrow))
5109 if (
Right.is(tok::l_paren) &&
Left.is(Keywords.kw_await) && BeforeLeft &&
5110 BeforeLeft->is(tok::kw_for)) {
5113 if (
Left.is(Keywords.kw_async) &&
Right.is(tok::l_paren) &&
5114 Right.MatchingParen) {
5115 const FormatToken *Next =
Right.MatchingParen->getNextNonComment();
5118 if (Next && Next->is(TT_FatArrow))
5121 if ((
Left.is(TT_TemplateString) &&
Left.TokenText.ends_with(
"${")) ||
5122 (
Right.is(TT_TemplateString) &&
Right.TokenText.starts_with(
"}"))) {
5127 if (Keywords.isJavaScriptIdentifier(Left,
5129 Right.is(TT_TemplateString)) {
5132 if (
Right.is(tok::star) &&
5133 Left.isOneOf(Keywords.kw_function, Keywords.kw_yield)) {
5136 if (
Right.isOneOf(tok::l_brace, tok::l_square) &&
5137 Left.isOneOf(Keywords.kw_function, Keywords.kw_yield,
5138 Keywords.kw_extends, Keywords.kw_implements)) {
5141 if (
Right.is(tok::l_paren)) {
5143 if (
Line.MustBeDeclaration &&
Left.Tok.getIdentifierInfo())
5147 if (BeforeLeft && BeforeLeft->is(tok::period) &&
5148 Left.Tok.getIdentifierInfo()) {
5152 if (
Left.isOneOf(tok::kw_throw, Keywords.kw_await, Keywords.kw_typeof,
5158 if (
Left.endsSequence(tok::kw_const, Keywords.kw_as))
5160 if ((
Left.isOneOf(Keywords.kw_let, Keywords.kw_var, Keywords.kw_in,
5165 (
Left.is(Keywords.kw_of) && BeforeLeft &&
5166 (BeforeLeft->is(tok::identifier) ||
5167 BeforeLeft->isOneOf(tok::r_square, tok::r_brace)))) &&
5168 (!BeforeLeft || BeforeLeft->isNot(tok::period))) {
5171 if (
Left.isOneOf(tok::kw_for, Keywords.kw_as) && BeforeLeft &&
5172 BeforeLeft->is(tok::period) &&
Right.is(tok::l_paren)) {
5175 if (
Left.is(Keywords.kw_as) &&
5176 Right.isOneOf(tok::l_square, tok::l_brace, tok::l_paren)) {
5179 if (
Left.is(tok::kw_default) && BeforeLeft &&
5180 BeforeLeft->is(tok::kw_export)) {
5183 if (
Left.is(Keywords.kw_is) &&
Right.is(tok::l_brace))
5185 if (
Right.isOneOf(TT_JsTypeColon, TT_JsTypeOptionalQuestion))
5187 if (
Left.is(TT_JsTypeOperator) ||
Right.is(TT_JsTypeOperator))
5189 if ((
Left.is(tok::l_brace) ||
Right.is(tok::r_brace)) &&
5190 Line.First->isOneOf(Keywords.kw_import, tok::kw_export)) {
5193 if (
Left.is(tok::ellipsis))
5195 if (
Left.is(TT_TemplateCloser) &&
5196 !
Right.isOneOf(tok::equal, tok::l_brace, tok::comma, tok::l_square,
5197 Keywords.kw_implements, Keywords.kw_extends)) {
5203 if (
Right.is(TT_NonNullAssertion))
5205 if (
Left.is(TT_NonNullAssertion) &&
5206 Right.isOneOf(Keywords.kw_as, Keywords.kw_in)) {
5210 if (
Left.is(TT_CaseLabelArrow) ||
Right.is(TT_CaseLabelArrow))
5212 if (
Left.is(tok::r_square) &&
Right.is(tok::l_brace))
5215 if (
Left.is(tok::l_square) ||
Right.is(tok::r_square))
5216 return Style.SpacesInSquareBrackets;
5218 if (
Left.is(Keywords.kw_synchronized) &&
Right.is(tok::l_paren)) {
5219 return Style.SpaceBeforeParensOptions.AfterControlStatements ||
5220 spaceRequiredBeforeParens(Right);
5222 if ((
Left.isAccessSpecifierKeyword() ||
5223 Left.isOneOf(tok::kw_static, Keywords.kw_final, Keywords.kw_abstract,
5224 Keywords.kw_native)) &&
5225 Right.is(TT_TemplateOpener)) {
5228 }
else if (IsVerilog) {
5230 if (
Left.is(tok::identifier) &&
Left.TokenText[0] ==
'\\')
5234 if ((
Left.is(TT_VerilogTableItem) &&
5235 !
Right.isOneOf(tok::r_paren, tok::semi)) ||
5236 (
Right.is(TT_VerilogTableItem) &&
Left.isNot(tok::l_paren))) {
5237 const FormatToken *Next =
Right.getNextNonComment();
5238 return !(Next && Next->is(tok::r_paren));
5241 if (
Left.isNot(TT_BinaryOperator) &&
5242 Left.isOneOf(Keywords.kw_verilogHash, Keywords.kw_verilogHashHash)) {
5246 if (
Right.isNot(tok::semi) &&
5247 (
Left.endsSequence(tok::numeric_constant, Keywords.kw_verilogHash) ||
5248 Left.endsSequence(tok::numeric_constant,
5249 Keywords.kw_verilogHashHash) ||
5250 (
Left.is(tok::r_paren) &&
Left.MatchingParen &&
5251 Left.MatchingParen->endsSequence(tok::l_paren, tok::at)))) {
5256 if (
Left.is(Keywords.kw_apostrophe) ||
5257 (
Left.is(TT_VerilogNumberBase) &&
Right.is(tok::numeric_constant))) {
5261 if (
Left.is(tok::arrow) ||
Right.is(tok::arrow))
5266 if (
Left.is(tok::at) &&
Right.isOneOf(tok::l_paren, tok::star, tok::at))
5269 if (
Right.is(tok::l_square) &&
5270 Left.isOneOf(TT_VerilogDimensionedTypeName, Keywords.kw_function)) {
5274 if (
Right.isOneOf(tok::period, Keywords.kw_apostrophe) &&
5275 Keywords.isVerilogIdentifier(Left) &&
Left.getPreviousNonComment() &&
5276 Left.getPreviousNonComment()->is(Keywords.kw_tagged)) {
5282 if ((
Right.is(Keywords.kw_apostrophe) ||
5284 !(
Left.isOneOf(Keywords.kw_assign, Keywords.kw_unique) ||
5285 Keywords.isVerilogWordOperator(Left)) &&
5286 (
Left.isOneOf(tok::r_square, tok::r_paren, tok::r_brace,
5287 tok::numeric_constant) ||
5288 Keywords.isWordLike(Left))) {
5292 if ((
Right.is(tok::star) &&
Left.is(tok::coloncolon)) ||
5293 (
Left.is(tok::star) &&
Right.is(tok::semi))) {
5297 if (
Left.endsSequence(tok::star, tok::l_paren) &&
Right.is(tok::identifier))
5300 if (
Right.is(tok::l_paren) &&
Right.is(TT_VerilogStrength))
5303 if ((
Left.is(tok::l_brace) &&
5304 Right.isOneOf(tok::lessless, tok::greatergreater)) ||
5305 (
Left.endsSequence(tok::lessless, tok::l_brace) ||
5306 Left.endsSequence(tok::greatergreater, tok::l_brace))) {
5309 }
else if (Style.isTableGen()) {
5311 if (
Left.is(tok::l_square) &&
Right.is(tok::l_brace))
5313 if (
Left.is(tok::r_brace) &&
Right.is(tok::r_square))
5316 if (
Right.isOneOf(TT_TableGenDAGArgListColon,
5317 TT_TableGenDAGArgListColonToAlign) ||
5318 Left.isOneOf(TT_TableGenDAGArgListColon,
5319 TT_TableGenDAGArgListColonToAlign)) {
5322 if (
Right.is(TT_TableGenCondOperatorColon))
5324 if (
Left.isOneOf(TT_TableGenDAGArgOperatorID,
5325 TT_TableGenDAGArgOperatorToBreak) &&
5326 Right.isNot(TT_TableGenDAGArgCloser)) {
5330 if (
Right.isOneOf(tok::l_paren, tok::less) &&
5331 Left.isOneOf(TT_TableGenBangOperator, TT_TableGenCondOperator)) {
5336 if (
Left.is(TT_TableGenTrailingPasteOperator) &&
5337 Right.isOneOf(tok::l_brace, tok::colon)) {
5341 if (
Left.is(tok::hash) ||
Right.is(tok::hash))
5344 if (Keywords.isTableGenDefinition(Left))
5348 if (
Left.is(TT_ImplicitStringLiteral))
5349 return Right.hasWhitespaceBefore();
5351 if (
Left.is(TT_ObjCMethodSpecifier))
5353 if (
Left.is(tok::r_paren) &&
Left.isNot(TT_AttributeRParen) &&
5354 canBeObjCSelectorComponent(Right)) {
5362 (
Right.is(tok::equal) ||
Left.is(tok::equal))) {
5366 if (
Right.isOneOf(TT_TrailingReturnArrow, TT_LambdaArrow) ||
5367 Left.isOneOf(TT_TrailingReturnArrow, TT_LambdaArrow)) {
5370 if (
Left.is(tok::comma) &&
Right.isNot(TT_OverloadedOperatorLParen) &&
5373 (
Left.Children.empty() || !
Left.MacroParent)) {
5376 if (
Right.is(tok::comma))
5378 if (
Right.is(TT_ObjCBlockLParen))
5380 if (
Right.is(TT_CtorInitializerColon))
5381 return Style.SpaceBeforeCtorInitializerColon;
5382 if (
Right.is(TT_InheritanceColon) && !Style.SpaceBeforeInheritanceColon)
5384 if (
Right.is(TT_RangeBasedForLoopColon) &&
5385 !Style.SpaceBeforeRangeBasedForLoopColon) {
5388 if (
Left.is(TT_BitFieldColon)) {
5392 if (
Right.is(tok::colon)) {
5393 if (
Right.is(TT_CaseLabelColon))
5394 return Style.SpaceBeforeCaseColon;
5395 if (
Right.is(TT_GotoLabelColon))
5398 if (!
Right.getNextNonComment())
5400 if (
Right.is(TT_ObjCMethodExpr))
5402 if (
Left.is(tok::question))
5404 if (
Right.is(TT_InlineASMColon) &&
Left.is(tok::coloncolon))
5406 if (
Right.is(TT_DictLiteral))
5407 return Style.SpacesInContainerLiterals;
5408 if (
Right.is(TT_AttributeColon))
5410 if (
Right.is(TT_CSharpNamedArgumentColon))
5412 if (
Right.is(TT_GenericSelectionColon))
5414 if (
Right.is(TT_BitFieldColon)) {
5421 if ((
Left.isOneOf(tok::minus, tok::minusminus) &&
5422 Right.isOneOf(tok::minus, tok::minusminus)) ||
5423 (
Left.isOneOf(tok::plus, tok::plusplus) &&
5424 Right.isOneOf(tok::plus, tok::plusplus))) {
5427 if (
Left.is(TT_UnaryOperator)) {
5430 if (
Left.is(tok::amp) &&
Right.is(tok::r_square))
5431 return Style.SpacesInSquareBrackets;
5432 return Style.SpaceAfterLogicalNot &&
Left.is(tok::exclaim);
5437 if (
Left.is(TT_CastRParen)) {
5438 return Style.SpaceAfterCStyleCast ||
5439 Right.isOneOf(TT_BinaryOperator, TT_SelectorName);
5442 auto ShouldAddSpacesInAngles = [
this, &
Right]() {
5446 return Right.hasWhitespaceBefore();
5450 if (
Left.is(tok::greater) &&
Right.is(tok::greater)) {
5453 return !Style.Cpp11BracedListStyle;
5455 return Right.is(TT_TemplateCloser) &&
Left.is(TT_TemplateCloser) &&
5457 ShouldAddSpacesInAngles());
5459 if (
Right.isOneOf(tok::arrow, tok::arrowstar, tok::periodstar) ||
5460 Left.isOneOf(tok::arrow, tok::period, tok::arrowstar, tok::periodstar) ||
5461 (
Right.is(tok::period) &&
Right.isNot(TT_DesignatedInitializerPeriod))) {
5464 if (!Style.SpaceBeforeAssignmentOperators &&
Left.isNot(TT_TemplateCloser) &&
5469 (
Left.is(tok::identifier) ||
Left.is(tok::kw_this))) {
5472 if (
Right.is(tok::coloncolon) &&
Left.is(tok::identifier)) {
5476 return Right.hasWhitespaceBefore();
5478 if (
Right.is(tok::coloncolon) &&
5479 !
Left.isOneOf(tok::l_brace, tok::comment, tok::l_paren)) {
5481 return (
Left.is(TT_TemplateOpener) &&
5483 ShouldAddSpacesInAngles())) ||
5484 !(
Left.isOneOf(tok::l_paren, tok::r_paren, tok::l_square,
5485 tok::kw___super, TT_TemplateOpener,
5486 TT_TemplateCloser)) ||
5487 (
Left.is(tok::l_paren) && Style.SpacesInParensOptions.Other);
5489 if ((
Left.is(TT_TemplateOpener)) != (
Right.is(TT_TemplateCloser)))
5490 return ShouldAddSpacesInAngles();
5491 if (
Left.is(tok::r_paren) &&
Right.is(TT_PointerOrReference) &&
5492 Right.isOneOf(tok::amp, tok::ampamp)) {
5496 if (
Right.is(TT_StructuredBindingLSquare)) {
5497 return !
Left.isOneOf(tok::amp, tok::ampamp) ||
5501 if (
Right.Next &&
Right.Next->is(TT_StructuredBindingLSquare) &&
5502 Right.isOneOf(tok::amp, tok::ampamp)) {
5505 if ((
Right.is(TT_BinaryOperator) &&
Left.isNot(tok::l_paren)) ||
5506 (
Left.isOneOf(TT_BinaryOperator, TT_ConditionalExpr) &&
5507 Right.isNot(tok::r_paren))) {
5510 if (
Right.is(TT_TemplateOpener) &&
Left.is(tok::r_paren) &&
5511 Left.MatchingParen &&
5512 Left.MatchingParen->is(TT_OverloadedOperatorLParen)) {
5515 if (
Right.is(tok::less) &&
Left.isNot(tok::l_paren) &&
5519 if (
Right.is(TT_TrailingUnaryOperator))
5521 if (
Left.is(TT_RegexLiteral))
5523 return spaceRequiredBetween(
Line, Left, Right);
5529 !Tok.
isOneOf(TT_ObjCBlockLBrace, TT_LambdaLBrace, TT_DictLiteral);
5546 !Tok.
isOneOf(TT_ObjCBlockLBrace, TT_DictLiteral);
5549bool TokenAnnotator::mustBreakBefore(
const AnnotatedLine &
Line,
5550 const FormatToken &Right)
const {
5551 const FormatToken &Left = *Right.Previous;
5552 if (Right.NewlinesBefore > 1 && Style.MaxEmptyLinesToKeep > 0 &&
5553 (!Style.RemoveEmptyLinesInUnwrappedLines || &Right ==
Line.First)) {
5557 if (Style.BreakFunctionDefinitionParameters &&
Line.MightBeFunctionDecl &&
5558 Line.mightBeFunctionDefinition() && Left.MightBeFunctionDeclParen &&
5559 Left.ParameterCount > 0) {
5566 Line.MightBeFunctionDecl && !
Left.opensScope() &&
5571 const auto *BeforeLeft =
Left.Previous;
5572 const auto *AfterRight =
Right.Next;
5574 if (Style.isCSharp()) {
5575 if (
Left.is(TT_FatArrow) &&
Right.is(tok::l_brace) &&
5576 Style.BraceWrapping.AfterFunction) {
5579 if (
Right.is(TT_CSharpNamedArgumentColon) ||
5580 Left.is(TT_CSharpNamedArgumentColon)) {
5583 if (
Right.is(TT_CSharpGenericTypeConstraint))
5585 if (AfterRight && AfterRight->is(TT_FatArrow) &&
5586 (
Right.is(tok::numeric_constant) ||
5587 (
Right.is(tok::identifier) &&
Right.TokenText ==
"_"))) {
5592 if (
Left.is(TT_AttributeSquare) &&
Left.is(tok::r_square) &&
5593 (
Right.isAccessSpecifier(
false) ||
5594 Right.is(Keywords.kw_internal))) {
5598 if (
Left.is(TT_AttributeSquare) &&
Right.is(TT_AttributeSquare) &&
5599 Left.is(tok::r_square) &&
Right.is(tok::l_square)) {
5602 }
else if (Style.isJavaScript()) {
5604 if (
Right.is(tok::string_literal) &&
Left.is(tok::plus) && BeforeLeft &&
5605 BeforeLeft->is(tok::string_literal)) {
5608 if (
Left.is(TT_DictLiteral) &&
Left.is(tok::l_brace) &&
Line.Level == 0 &&
5609 BeforeLeft && BeforeLeft->is(tok::equal) &&
5610 Line.First->isOneOf(tok::identifier, Keywords.kw_import, tok::kw_export,
5614 !
Line.First->isOneOf(Keywords.kw_var, Keywords.kw_let)) {
5619 if (
Left.is(tok::l_brace) &&
Line.Level == 0 &&
5620 (
Line.startsWith(tok::kw_enum) ||
5621 Line.startsWith(tok::kw_const, tok::kw_enum) ||
5622 Line.startsWith(tok::kw_export, tok::kw_enum) ||
5623 Line.startsWith(tok::kw_export, tok::kw_const, tok::kw_enum))) {
5628 if (
Right.is(tok::r_brace) &&
Left.is(tok::l_brace) && BeforeLeft &&
5629 BeforeLeft->is(TT_FatArrow)) {
5631 switch (Style.AllowShortLambdasOnASingleLine) {
5637 return !
Left.Children.empty();
5641 return (
Left.NestingLevel == 0 &&
Line.Level == 0) &&
5642 !
Left.Children.empty();
5644 llvm_unreachable(
"Unknown FormatStyle::ShortLambdaStyle enum");
5647 if (
Right.is(tok::r_brace) &&
Left.is(tok::l_brace) &&
5648 !
Left.Children.empty()) {
5652 (
Left.NestingLevel == 0 &&
Line.Level == 0 &&
5653 Style.AllowShortFunctionsOnASingleLine &
5657 if (
Right.is(tok::plus) &&
Left.is(tok::string_literal) && AfterRight &&
5658 AfterRight->is(tok::string_literal)) {
5661 }
else if (Style.isVerilog()) {
5663 if (
Left.is(TT_VerilogAssignComma))
5666 if (
Left.is(TT_VerilogTypeComma))
5670 if (Style.VerilogBreakBetweenInstancePorts &&
5671 (
Left.is(TT_VerilogInstancePortComma) ||
5672 (
Left.is(tok::r_paren) && Keywords.isVerilogIdentifier(Right) &&
5673 Left.MatchingParen &&
5674 Left.MatchingParen->is(TT_VerilogInstancePortLParen)))) {
5679 if (!Keywords.isVerilogBegin(Right) && Keywords.isVerilogEndOfLabel(Left))
5681 }
else if (Style.BreakAdjacentStringLiterals &&
5682 (IsCpp || Style.isProto() ||
5684 if (
Left.isStringLiteral() &&
Right.isStringLiteral())
5689 if (Style.isJson()) {
5693 if (
Left.is(TT_DictLiteral) &&
Left.is(tok::l_brace))
5696 if ((
Left.is(TT_ArrayInitializerLSquare) &&
Left.is(tok::l_square) &&
5697 Right.isNot(tok::r_square)) ||
5698 Left.is(tok::comma)) {
5699 if (
Right.is(tok::l_brace))
5703 for (
const auto *Tok = &Right; Tok; Tok = Tok->Next) {
5704 if (Tok->isOneOf(tok::l_brace, tok::l_square))
5706 if (Tok->isOneOf(tok::r_brace, tok::r_square))
5709 return Style.BreakArrays;
5711 }
else if (Style.isTableGen()) {
5715 if (
Left.is(TT_TableGenCondOperatorComma))
5717 if (
Left.is(TT_TableGenDAGArgOperatorToBreak) &&
5718 Right.isNot(TT_TableGenDAGArgCloser)) {
5721 if (
Left.is(TT_TableGenDAGArgListCommaToBreak))
5723 if (
Right.is(TT_TableGenDAGArgCloser) &&
Right.MatchingParen &&
5724 Right.MatchingParen->is(TT_TableGenDAGArgOpenerToBreak) &&
5731 if (
Line.startsWith(tok::kw_asm) &&
Right.is(TT_InlineASMColon) &&
5741 const FormatToken *BeforeClosingBrace =
nullptr;
5742 if ((
Left.isOneOf(tok::l_brace, TT_ArrayInitializerLSquare) ||
5743 (Style.isJavaScript() &&
Left.is(tok::l_paren))) &&
5745 BeforeClosingBrace =
Left.MatchingParen->Previous;
5746 }
else if (
Right.MatchingParen &&
5747 (
Right.MatchingParen->isOneOf(tok::l_brace,
5748 TT_ArrayInitializerLSquare) ||
5749 (Style.isJavaScript() &&
5750 Right.MatchingParen->is(tok::l_paren)))) {
5751 BeforeClosingBrace = &
Left;
5753 if (BeforeClosingBrace && (BeforeClosingBrace->is(tok::comma) ||
5754 BeforeClosingBrace->isTrailingComment())) {
5759 if (
Right.is(tok::comment)) {
5761 (
Right.NewlinesBefore > 0 &&
Right.HasUnescapedNewline);
5763 if (
Left.isTrailingComment())
5765 if (
Left.IsUnterminatedLiteral)
5768 if (BeforeLeft && BeforeLeft->is(tok::lessless) &&
5769 Left.is(tok::string_literal) &&
Right.is(tok::lessless) && AfterRight &&
5770 AfterRight->is(tok::string_literal)) {
5771 return Right.NewlinesBefore > 0;
5774 if (
Right.is(TT_RequiresClause)) {
5775 switch (Style.RequiresClausePosition) {
5785 if (
Left.ClosesTemplateDeclaration &&
Left.MatchingParen &&
5786 Left.MatchingParen->NestingLevel == 0) {
5790 if (
Right.is(tok::kw_concept))
5794 Right.NewlinesBefore > 0);
5796 if (
Left.ClosesRequiresClause) {
5797 switch (Style.RequiresClausePosition) {
5800 return Right.isNot(tok::semi);
5802 return !
Right.isOneOf(tok::semi, tok::l_brace);
5809 (
Left.is(TT_CtorInitializerComma) ||
5810 Right.is(TT_CtorInitializerColon))) {
5815 Left.isOneOf(TT_CtorInitializerColon, TT_CtorInitializerComma)) {
5821 Right.isOneOf(TT_CtorInitializerComma, TT_CtorInitializerColon)) {
5827 Right.is(TT_CtorInitializerColon)) {
5832 Left.is(TT_CtorInitializerColon)) {
5838 Right.is(TT_InheritanceComma)) {
5842 Left.is(TT_InheritanceComma)) {
5845 if (
Right.is(tok::string_literal) &&
Right.TokenText.starts_with(
"R\"")) {
5849 return Right.IsMultiline &&
Right.NewlinesBefore > 0;
5851 if ((
Left.is(tok::l_brace) ||
5852 (
Left.is(tok::less) && BeforeLeft && BeforeLeft->is(tok::equal))) &&
5858 if (
Right.is(TT_InlineASMBrace))
5859 return Right.HasUnescapedNewline;
5862 auto *FirstNonComment =
Line.getFirstNonComment();
5864 FirstNonComment && (FirstNonComment->is(Keywords.kw_internal) ||
5865 FirstNonComment->isAccessSpecifierKeyword());
5867 if (Style.BraceWrapping.AfterEnum) {
5868 if (
Line.startsWith(tok::kw_enum) ||
5869 Line.startsWith(tok::kw_typedef, tok::kw_enum)) {
5874 FirstNonComment->Next->is(tok::kw_enum)) {
5880 if (Style.BraceWrapping.AfterClass &&
5882 FirstNonComment->Next->is(Keywords.kw_interface)) ||
5883 Line.startsWith(Keywords.kw_interface))) {
5888 if (
Right.isNot(TT_FunctionLBrace)) {
5889 return (
Line.startsWith(tok::kw_class) &&
5890 Style.BraceWrapping.AfterClass) ||
5891 (
Line.startsWith(tok::kw_struct) &&
5892 Style.BraceWrapping.AfterStruct);
5896 if (
Left.is(TT_ObjCBlockLBrace) &&
5902 if (
Left.isOneOf(TT_AttributeRParen, TT_AttributeMacro) &&
5903 Right.is(TT_ObjCDecl)) {
5907 if (
Left.is(TT_LambdaLBrace)) {
5915 (!
Left.Children.empty() &&
5921 if (Style.BraceWrapping.BeforeLambdaBody &&
Right.is(TT_LambdaLBrace) &&
5922 (
Left.isPointerOrReference() ||
Left.is(TT_TemplateCloser))) {
5928 Left.is(TT_LeadingJavaAnnotation) &&
5929 Right.isNot(TT_LeadingJavaAnnotation) &&
Right.isNot(tok::l_paren) &&
5930 (
Line.Last->is(tok::l_brace) || Style.BreakAfterJavaFieldAnnotations)) {
5934 if (
Right.is(TT_ProtoExtensionLSquare))
5964 if (Style.isProto() &&
Right.is(TT_SelectorName) &&
5965 Right.isNot(tok::r_square) && AfterRight) {
5968 if (
Left.is(tok::at))
5974 const auto *LBrace = AfterRight;
5975 if (LBrace && LBrace->is(tok::colon)) {
5976 LBrace = LBrace->Next;
5977 if (LBrace && LBrace->is(tok::at)) {
5978 LBrace = LBrace->Next;
5980 LBrace = LBrace->Next;
5992 ((LBrace->is(tok::l_brace) &&
5993 (LBrace->is(TT_DictLiteral) ||
5994 (LBrace->Next && LBrace->Next->is(tok::r_brace)))) ||
5995 LBrace->is(TT_ArrayInitializerLSquare) || LBrace->is(tok::less))) {
6002 if (
Left.ParameterCount == 0)
6017 if (
Left.isOneOf(tok::r_brace, tok::greater, tok::r_square))
6024bool TokenAnnotator::canBreakBefore(
const AnnotatedLine &
Line,
6025 const FormatToken &Right)
const {
6026 const FormatToken &
Left = *
Right.Previous;
6028 if (Style.isCSharp()) {
6029 if (
Left.isOneOf(TT_CSharpNamedArgumentColon, TT_AttributeColon) ||
6030 Right.isOneOf(TT_CSharpNamedArgumentColon, TT_AttributeColon)) {
6034 if (
Line.First->is(TT_CSharpGenericTypeConstraint))
6035 return Left.is(TT_CSharpGenericTypeConstraintComma);
6037 if (
Right.is(TT_CSharpNullable))
6040 if (
Left.isOneOf(Keywords.kw_throws, Keywords.kw_extends,
6041 Keywords.kw_implements)) {
6044 if (
Right.isOneOf(Keywords.kw_throws, Keywords.kw_extends,
6045 Keywords.kw_implements)) {
6048 }
else if (Style.isJavaScript()) {
6049 const FormatToken *NonComment =
Right.getPreviousNonComment();
6051 (NonComment->isAccessSpecifierKeyword() ||
6052 NonComment->isOneOf(
6053 tok::kw_return, Keywords.kw_yield, tok::kw_continue, tok::kw_break,
6054 tok::kw_throw, Keywords.kw_interface, Keywords.kw_type,
6055 tok::kw_static, Keywords.kw_readonly, Keywords.kw_override,
6056 Keywords.kw_abstract, Keywords.kw_get, Keywords.kw_set,
6057 Keywords.kw_async, Keywords.kw_await))) {
6060 if (
Right.NestingLevel == 0 &&
6061 (
Left.Tok.getIdentifierInfo() ||
6062 Left.isOneOf(tok::r_square, tok::r_paren)) &&
6063 Right.isOneOf(tok::l_square, tok::l_paren)) {
6066 if (NonComment && NonComment->is(tok::identifier) &&
6067 NonComment->TokenText ==
"asserts") {
6070 if (
Left.is(TT_FatArrow) &&
Right.is(tok::l_brace))
6072 if (
Left.is(TT_JsTypeColon))
6075 if (
Left.is(tok::exclaim) &&
Right.is(tok::colon))
6080 if (
Right.is(Keywords.kw_is)) {
6081 const FormatToken *Next =
Right.getNextNonComment();
6089 if (!Next || Next->isNot(tok::colon))
6092 if (
Left.is(Keywords.kw_in))
6094 if (
Right.is(Keywords.kw_in))
6096 if (
Right.is(Keywords.kw_as))
6098 if (
Right.isOneOf(Keywords.kw_extends, Keywords.kw_infer)) {
6104 if (
Left.is(Keywords.kw_as))
6106 if (
Left.is(TT_NonNullAssertion))
6108 if (
Left.is(Keywords.kw_declare) &&
6109 Right.isOneOf(Keywords.kw_module, tok::kw_namespace,
6110 Keywords.kw_function, tok::kw_class, tok::kw_enum,
6111 Keywords.kw_interface, Keywords.kw_type, Keywords.kw_var,
6112 Keywords.kw_let, tok::kw_const)) {
6117 if (
Left.isOneOf(Keywords.kw_module, tok::kw_namespace) &&
6118 Right.isOneOf(tok::identifier, tok::string_literal)) {
6121 if (
Right.is(TT_TemplateString) &&
Right.closesScope())
6125 if (
Left.is(tok::identifier) &&
Right.is(TT_TemplateString))
6127 if (
Left.is(TT_TemplateString) &&
Left.opensScope())
6129 }
else if (Style.isTableGen()) {
6131 if (Keywords.isTableGenDefinition(Left))
6134 if (
Right.is(tok::l_paren)) {
6135 return !
Left.isOneOf(TT_TableGenBangOperator, TT_TableGenCondOperator,
6139 if (
Left.is(TT_TableGenValueSuffix))
6142 if (
Left.is(tok::hash) ||
Right.is(tok::hash))
6144 if (
Left.isOneOf(TT_TableGenBangOperator, TT_TableGenCondOperator))
6151 if (
Right.is(tok::r_brace)) {
6153 (
Right.isBlockIndentedInitRBrace(Style)));
6157 if (
Right.is(tok::r_paren)) {
6159 !
Right.MatchingParen) {
6162 auto Next =
Right.Next;
6163 if (Next && Next->is(tok::r_paren))
6165 if (Next && Next->is(tok::l_paren))
6167 const FormatToken *
Previous =
Right.MatchingParen->Previous;
6171 if (
Left.isOneOf(tok::r_paren, TT_TrailingAnnotation) &&
6172 Right.is(TT_TrailingAnnotation) &&
6177 if (
Left.is(tok::at))
6179 if (
Left.Tok.getObjCKeywordID() == tok::objc_interface)
6181 if (
Left.isOneOf(TT_JavaAnnotation, TT_LeadingJavaAnnotation))
6182 return Right.isNot(tok::l_paren);
6183 if (
Right.is(TT_PointerOrReference)) {
6184 return Line.IsMultiVariableDeclStmt ||
6185 (getTokenPointerOrReferenceAlignment(Right) ==
6187 (!
Right.Next ||
Right.Next->isNot(TT_FunctionDeclarationName)));
6189 if (
Right.isOneOf(TT_StartOfName, TT_FunctionDeclarationName) ||
6190 Right.is(tok::kw_operator)) {
6193 if (
Left.is(TT_PointerOrReference))
6195 if (
Right.isTrailingComment()) {
6202 (
Left.is(TT_CtorInitializerColon) &&
Right.NewlinesBefore > 0 &&
6205 if (
Left.is(tok::question) &&
Right.is(tok::colon))
6207 if (
Right.is(TT_ConditionalExpr) ||
Right.is(tok::question))
6208 return Style.BreakBeforeTernaryOperators;
6209 if (
Left.is(TT_ConditionalExpr) ||
Left.is(tok::question))
6210 return !Style.BreakBeforeTernaryOperators;
6211 if (
Left.is(TT_InheritanceColon))
6213 if (
Right.is(TT_InheritanceColon))
6215 if (
Right.is(TT_ObjCMethodExpr) &&
Right.isNot(tok::r_square) &&
6216 Left.isNot(TT_SelectorName)) {
6220 if (
Right.is(tok::colon) &&
6221 !
Right.isOneOf(TT_CtorInitializerColon, TT_InlineASMColon)) {
6224 if (
Left.is(tok::colon) &&
Left.isOneOf(TT_DictLiteral, TT_ObjCMethodExpr)) {
6225 if (Style.isProto()) {
6226 if (!Style.AlwaysBreakBeforeMultilineStrings &&
Right.isStringLiteral())
6252 if (((
Right.is(tok::l_brace) ||
Right.is(tok::less)) &&
6253 Right.is(TT_DictLiteral)) ||
6254 Right.is(TT_ArrayInitializerLSquare)) {
6260 if (
Right.is(tok::r_square) &&
Right.MatchingParen &&
6261 Right.MatchingParen->is(TT_ProtoExtensionLSquare)) {
6264 if (
Right.is(TT_SelectorName) || (
Right.is(tok::identifier) &&
Right.Next &&
6265 Right.Next->is(TT_ObjCMethodExpr))) {
6266 return Left.isNot(tok::period);
6270 if (
Right.is(tok::kw_concept))
6272 if (
Right.is(TT_RequiresClause))
6274 if (
Left.ClosesTemplateDeclaration) {
6276 Right.NewlinesBefore > 0;
6278 if (
Left.is(TT_FunctionAnnotationRParen))
6280 if (
Left.ClosesRequiresClause)
6282 if (
Right.isOneOf(TT_RangeBasedForLoopColon, TT_OverloadedOperatorLParen,
6283 TT_OverloadedOperator)) {
6286 if (
Left.is(TT_RangeBasedForLoopColon))
6288 if (
Right.is(TT_RangeBasedForLoopColon))
6290 if (
Left.is(TT_TemplateCloser) &&
Right.is(TT_TemplateOpener))
6292 if ((
Left.is(tok::greater) &&
Right.is(tok::greater)) ||
6293 (
Left.is(tok::less) &&
Right.is(tok::less))) {
6296 if (
Right.is(TT_BinaryOperator) &&
6302 if (
Left.isOneOf(TT_TemplateCloser, TT_UnaryOperator) ||
6303 Left.is(tok::kw_operator)) {
6306 if (
Left.is(tok::equal) && !
Right.isOneOf(tok::kw_default, tok::kw_delete) &&
6310 if (
Left.is(tok::equal) &&
Right.is(tok::l_brace) &&
6311 !Style.Cpp11BracedListStyle) {
6314 if (
Left.is(TT_AttributeLParen) ||
6315 (
Left.is(tok::l_paren) &&
Left.is(TT_TypeDeclarationParen))) {
6318 if (
Left.is(tok::l_paren) &&
Left.Previous &&
6319 (
Left.Previous->isOneOf(TT_BinaryOperator, TT_CastRParen))) {
6322 if (
Right.is(TT_ImplicitStringLiteral))
6325 if (
Right.is(TT_TemplateCloser))
6327 if (
Right.is(tok::r_square) &&
Right.MatchingParen &&
6328 Right.MatchingParen->is(TT_LambdaLSquare)) {
6334 if (
Left.is(TT_TrailingAnnotation)) {
6335 return !
Right.isOneOf(tok::l_brace, tok::semi, tok::equal, tok::l_paren,
6336 tok::less, tok::coloncolon);
6339 if (
Right.isAttribute())
6342 if (
Right.is(tok::l_square) &&
Right.is(TT_AttributeSquare))
6343 return Left.isNot(TT_AttributeSquare);
6345 if (
Left.is(tok::identifier) &&
Right.is(tok::string_literal))
6348 if (
Right.is(tok::identifier) &&
Right.Next &&
Right.Next->is(TT_DictLiteral))
6351 if (
Left.is(TT_CtorInitializerColon)) {
6353 (!
Right.isTrailingComment() ||
Right.NewlinesBefore > 0);
6355 if (
Right.is(TT_CtorInitializerColon))
6357 if (
Left.is(TT_CtorInitializerComma) &&
6361 if (
Right.is(TT_CtorInitializerComma) &&
6365 if (
Left.is(TT_InheritanceComma) &&
6369 if (
Right.is(TT_InheritanceComma) &&
6373 if (
Left.is(TT_ArrayInitializerLSquare))
6375 if (
Right.is(tok::kw_typename) &&
Left.isNot(tok::kw_const))
6377 if ((
Left.isBinaryOperator() ||
Left.is(TT_BinaryOperator)) &&
6378 !
Left.isOneOf(tok::arrowstar, tok::lessless) &&
6384 if ((
Left.is(TT_AttributeSquare) &&
Right.is(tok::l_square)) ||
6385 (
Left.is(tok::r_square) &&
Right.is(TT_AttributeSquare))) {
6389 auto ShortLambdaOption = Style.AllowShortLambdasOnASingleLine;
6390 if (Style.BraceWrapping.BeforeLambdaBody &&
Right.is(TT_LambdaLBrace)) {
6397 if (
Right.is(tok::kw_noexcept) &&
Right.is(TT_TrailingAnnotation)) {
6398 switch (Style.AllowBreakBeforeNoexceptSpecifier) {
6404 return Right.Next &&
Right.Next->is(tok::l_paren);
6408 return Left.isOneOf(tok::comma, tok::coloncolon, tok::semi, tok::l_brace,
6409 tok::kw_class, tok::kw_struct, tok::comment) ||
6410 Right.isMemberAccess() ||
6411 Right.isOneOf(TT_TrailingReturnArrow, TT_LambdaArrow, tok::lessless,
6412 tok::colon, tok::l_square, tok::at) ||
6413 (
Left.is(tok::r_paren) &&
6414 Right.isOneOf(tok::identifier, tok::kw_const)) ||
6415 (
Left.is(tok::l_paren) &&
Right.isNot(tok::r_paren)) ||
6416 (
Left.is(TT_TemplateOpener) &&
Right.isNot(TT_TemplateCloser));
6419void TokenAnnotator::printDebugInfo(
const AnnotatedLine &
Line)
const {
6420 llvm::errs() <<
"AnnotatedTokens(L=" <<
Line.Level <<
", P=" <<
Line.PPLevel
6421 <<
", T=" <<
Line.Type <<
", C=" <<
Line.IsContinuation
6423 const FormatToken *Tok =
Line.First;
6425 llvm::errs() <<
" M=" << Tok->MustBreakBefore
6426 <<
" C=" << Tok->CanBreakBefore
6428 <<
" S=" << Tok->SpacesRequiredBefore
6429 <<
" F=" << Tok->Finalized <<
" B=" << Tok->BlockParameterCount
6430 <<
" BK=" << Tok->getBlockKind() <<
" P=" << Tok->SplitPenalty
6431 <<
" Name=" << Tok->Tok.getName() <<
" L=" << Tok->TotalLength
6432 <<
" PPK=" << Tok->getPackingKind() <<
" FakeLParens=";
6434 llvm::errs() << LParen <<
"/";
6435 llvm::errs() <<
" FakeRParens=" << Tok->FakeRParens;
6436 llvm::errs() <<
" II=" << Tok->Tok.getIdentifierInfo();
6437 llvm::errs() <<
" Text='" << Tok->TokenText <<
"'\n";
6439 assert(Tok ==
Line.Last);
6442 llvm::errs() <<
"----\n";
6446TokenAnnotator::getTokenReferenceAlignment(
const FormatToken &
Reference)
const {
6447 assert(
Reference.isOneOf(tok::amp, tok::ampamp));
6448 switch (Style.ReferenceAlignment) {
6450 return Style.PointerAlignment;
6459 return Style.PointerAlignment;
6463TokenAnnotator::getTokenPointerOrReferenceAlignment(
6464 const FormatToken &PointerOrReference)
const {
6465 if (PointerOrReference.isOneOf(tok::amp, tok::ampamp)) {
6466 switch (Style.ReferenceAlignment) {
6468 return Style.PointerAlignment;
6477 assert(PointerOrReference.is(tok::star));
6478 return Style.PointerAlignment;
Defines the SourceManager interface.
bool ColonIsObjCMethodExpr
FormatToken * FirstStartOfName
bool InCpp11AttributeSpecifier
unsigned LongestObjCSelectorName
bool VerilogAssignmentFound
enum clang::format::@1335::AnnotatingParser::Context::@351 ContextType
bool InCSharpAttributeSpecifier
tok::TokenKind ContextKind
FormatToken * FirstObjCSelectorName
bool VerilogMayBeConcatenation
This file implements a token annotator, i.e.
Defines the clang::TokenKind enum and support functions.
#define TRANSFORM_TYPE_TRAIT_DEF(Enum, _)
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
Parser - This implements a parser for the C family of languages.
IdentifierInfo * getIdentifierInfo() const
bool Ret(InterpState &S, CodePtr &PC)
TokenKind
Provides a simple uniform namespace for tokens from all C languages.
The JSON file list parser is used to communicate input to InstallAPI.
if(T->getSizeExpr()) TRY_TO(TraverseStmt(const_cast< Expr * >(T -> getSizeExpr())))
@ 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.
const FunctionProtoType * T
AccessSpecifier
A C++ access specifier (public, private, protected), plus the special value "none" which means differ...