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()) {
140 case TT_LambdaLBrace:
143 case TT_StructLBrace:
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 FormatToken *
Previous = Tok->getPreviousNonComment();
1586 Previous->setType(TT_SelectorName);
1588 Scopes.push_back(getScopeType(*Tok));
1594 Tok->setType(TT_TemplateOpener);
1602 Tok->Previous->isOneOf(TT_SelectorName, TT_DictLiteral))) {
1603 Tok->setType(TT_DictLiteral);
1604 FormatToken *
Previous = Tok->getPreviousNonComment();
1606 Previous->setType(TT_SelectorName);
1608 if (Style.isTableGen())
1609 Tok->setType(TT_TemplateOpener);
1611 Tok->setType(TT_BinaryOperator);
1612 NonTemplateLess.insert(Tok);
1622 if (!Scopes.empty())
1630 Tok->setType(TT_BinaryOperator);
1631 if (Tok->Previous && Tok->Previous->is(TT_TemplateCloser))
1632 Tok->SpacesRequiredBefore = 1;
1634 case tok::kw_operator:
1635 if (Style.isProto())
1637 while (CurrentToken &&
1638 !CurrentToken->isOneOf(tok::l_paren, tok::semi, tok::r_paren)) {
1639 if (CurrentToken->isOneOf(tok::star, tok::amp))
1640 CurrentToken->setType(TT_PointerOrReference);
1641 auto Next = CurrentToken->getNextNonComment();
1644 if (Next->is(tok::less))
1650 auto Previous = CurrentToken->getPreviousNonComment();
1652 if (CurrentToken->is(tok::comma) &&
Previous->isNot(tok::kw_operator))
1654 if (
Previous->isOneOf(TT_BinaryOperator, TT_UnaryOperator, tok::comma,
1655 tok::star, tok::arrow, tok::amp, tok::ampamp) ||
1657 Previous->TokenText.starts_with(
"\"\"")) {
1658 Previous->setType(TT_OverloadedOperator);
1659 if (CurrentToken->isOneOf(tok::less, tok::greater))
1663 if (CurrentToken && CurrentToken->is(tok::l_paren))
1664 CurrentToken->setType(TT_OverloadedOperatorLParen);
1665 if (CurrentToken && CurrentToken->Previous->is(TT_BinaryOperator))
1666 CurrentToken->Previous->setType(TT_OverloadedOperator);
1669 if (Style.isJavaScript() && Tok->Next &&
1670 Tok->Next->isOneOf(tok::semi, tok::comma, tok::colon, tok::r_paren,
1671 tok::r_brace, tok::r_square)) {
1676 Tok->setType(TT_JsTypeOptionalQuestion);
1681 if (Line.MustBeDeclaration && !Contexts.back().IsExpression &&
1682 Style.isJavaScript()) {
1685 if (Style.isCSharp()) {
1691 (Tok->Next->startsSequence(tok::question, tok::r_paren) ||
1692 Tok->Next->startsSequence(tok::question, tok::greater) ||
1693 Tok->Next->startsSequence(tok::question, tok::identifier,
1695 Tok->setType(TT_CSharpNullable);
1700 if (Tok->Next && Tok->Next->is(tok::identifier) && Tok->Next->Next &&
1701 Tok->Next->Next->is(tok::equal)) {
1702 Tok->setType(TT_CSharpNullable);
1711 if (!Contexts.back().IsExpression && Line.MustBeDeclaration &&
1713 !Tok->Next->isOneOf(tok::identifier, tok::string_literal) ||
1715 !Tok->Next->Next->isOneOf(tok::colon, tok::question))) {
1716 Tok->setType(TT_CSharpNullable);
1722 case tok::kw_template:
1723 parseTemplateDeclaration();
1726 switch (Contexts.back().ContextType) {
1727 case Context::CtorInitializer:
1728 Tok->setType(TT_CtorInitializerComma);
1730 case Context::InheritanceList:
1731 Tok->setType(TT_InheritanceComma);
1733 case Context::VerilogInstancePortList:
1734 Tok->setType(TT_VerilogInstancePortComma);
1737 if (Style.isVerilog() && Contexts.size() == 1 &&
1738 Line.startsWith(Keywords.kw_assign)) {
1739 Tok->setFinalizedType(TT_VerilogAssignComma);
1740 }
else if (Contexts.back().FirstStartOfName &&
1741 (Contexts.size() == 1 || startsWithInitStatement(Line))) {
1742 Contexts.back().FirstStartOfName->PartOfMultiVariableDeclStmt =
true;
1743 Line.IsMultiVariableDeclStmt =
true;
1747 if (Contexts.back().ContextType == Context::ForEachMacro)
1748 Contexts.back().IsExpression =
true;
1750 case tok::kw_default:
1752 if (Style.isVerilog() && Keywords.isVerilogEndOfLabel(*Tok) &&
1753 (Line.Level > 1 || (!Line.InPPDirective && Line.Level > 0))) {
1757 case tok::identifier:
1758 if (Tok->isOneOf(Keywords.kw___has_include,
1759 Keywords.kw___has_include_next)) {
1762 if (Style.isCSharp() && Tok->is(Keywords.kw_where) && Tok->Next &&
1763 Tok->Next->isNot(tok::l_paren)) {
1764 Tok->setType(TT_CSharpGenericTypeConstraint);
1765 parseCSharpGenericTypeConstraint();
1766 if (!Tok->getPreviousNonComment())
1767 Line.IsContinuation =
true;
1769 if (Style.isTableGen()) {
1770 if (Tok->is(Keywords.kw_assert)) {
1771 if (!parseTableGenValue())
1773 }
else if (Tok->isOneOf(Keywords.kw_def, Keywords.kw_defm) &&
1775 !Tok->Next->isOneOf(tok::colon, tok::l_brace))) {
1777 if (!parseTableGenValue(
true))
1783 if (Tok->isNot(TT_LambdaArrow) && Tok->Previous &&
1784 Tok->Previous->is(tok::kw_noexcept)) {
1785 Tok->setType(TT_TrailingReturnArrow);
1790 if (Style.isTableGen() && !parseTableGenValue())
1799 void parseCSharpGenericTypeConstraint() {
1800 int OpenAngleBracketsCount = 0;
1801 while (CurrentToken) {
1802 if (CurrentToken->is(tok::less)) {
1804 CurrentToken->setType(TT_TemplateOpener);
1805 ++OpenAngleBracketsCount;
1807 }
else if (CurrentToken->is(tok::greater)) {
1808 CurrentToken->setType(TT_TemplateCloser);
1809 --OpenAngleBracketsCount;
1811 }
else if (CurrentToken->is(tok::comma) && OpenAngleBracketsCount == 0) {
1814 CurrentToken->setType(TT_CSharpGenericTypeConstraintComma);
1816 }
else if (CurrentToken->is(Keywords.kw_where)) {
1817 CurrentToken->setType(TT_CSharpGenericTypeConstraint);
1819 }
else if (CurrentToken->is(tok::colon)) {
1820 CurrentToken->setType(TT_CSharpGenericTypeConstraintColon);
1828 void parseIncludeDirective() {
1829 if (CurrentToken && CurrentToken->is(tok::less)) {
1831 while (CurrentToken) {
1834 if (CurrentToken->isNot(tok::comment) &&
1835 !CurrentToken->TokenText.starts_with(
"//")) {
1836 CurrentToken->setType(TT_ImplicitStringLiteral);
1843 void parseWarningOrError() {
1848 while (CurrentToken) {
1849 CurrentToken->setType(TT_ImplicitStringLiteral);
1854 void parsePragma() {
1857 CurrentToken->isOneOf(Keywords.kw_mark, Keywords.kw_option,
1858 Keywords.kw_region)) {
1859 bool IsMarkOrRegion =
1860 CurrentToken->isOneOf(Keywords.kw_mark, Keywords.kw_region);
1863 while (CurrentToken) {
1864 if (IsMarkOrRegion || CurrentToken->Previous->is(TT_BinaryOperator))
1865 CurrentToken->setType(TT_ImplicitStringLiteral);
1871 void parseHasInclude() {
1872 if (!CurrentToken || CurrentToken->isNot(tok::l_paren))
1875 parseIncludeDirective();
1879 LineType parsePreprocessorDirective() {
1880 bool IsFirstToken = CurrentToken->IsFirst;
1886 if (Style.isJavaScript() && IsFirstToken) {
1890 while (CurrentToken) {
1892 CurrentToken->setType(TT_ImplicitStringLiteral);
1898 if (CurrentToken->is(tok::numeric_constant)) {
1899 CurrentToken->SpacesRequiredBefore = 1;
1904 if (!CurrentToken->Tok.getIdentifierInfo())
1908 if (Style.isVerilog() && !Keywords.isVerilogPPDirective(*CurrentToken))
1910 switch (CurrentToken->Tok.getIdentifierInfo()->getPPKeywordID()) {
1911 case tok::pp_include:
1912 case tok::pp_include_next:
1913 case tok::pp_import:
1915 parseIncludeDirective();
1919 case tok::pp_warning:
1920 parseWarningOrError();
1922 case tok::pp_pragma:
1927 Contexts.back().IsExpression =
true;
1930 CurrentToken->SpacesRequiredBefore =
true;
1936 while (CurrentToken) {
1937 FormatToken *Tok = CurrentToken;
1939 if (Tok->is(tok::l_paren)) {
1941 }
else if (Tok->isOneOf(Keywords.kw___has_include,
1942 Keywords.kw___has_include_next)) {
1953 NonTemplateLess.clear();
1954 if (!Line.InMacroBody && CurrentToken->is(tok::hash)) {
1958 auto Type = parsePreprocessorDirective();
1966 IdentifierInfo *Info = CurrentToken->Tok.getIdentifierInfo();
1968 CurrentToken->is(Keywords.kw_package)) ||
1969 (!Style.isVerilog() && Info &&
1970 Info->getPPKeywordID() == tok::pp_import && CurrentToken->Next &&
1971 CurrentToken->Next->isOneOf(tok::string_literal, tok::identifier,
1974 parseIncludeDirective();
1980 if (CurrentToken->is(tok::less) && Line.Last->is(tok::greater)) {
1981 parseIncludeDirective();
1988 CurrentToken->isOneOf(Keywords.kw_option, Keywords.kw_package)) {
1990 if (CurrentToken && CurrentToken->is(tok::identifier)) {
1991 while (CurrentToken)
1997 bool KeywordVirtualFound =
false;
1998 bool ImportStatement =
false;
2001 if (Style.isJavaScript() && CurrentToken->is(Keywords.kw_import))
2002 ImportStatement =
true;
2004 while (CurrentToken) {
2005 if (CurrentToken->is(tok::kw_virtual))
2006 KeywordVirtualFound =
true;
2007 if (Style.isJavaScript()) {
2014 if (Line.First->is(tok::kw_export) &&
2015 CurrentToken->is(Keywords.kw_from) && CurrentToken->Next &&
2016 CurrentToken->Next->isStringLiteral()) {
2017 ImportStatement =
true;
2019 if (isClosureImportStatement(*CurrentToken))
2020 ImportStatement =
true;
2022 if (!consumeToken())
2027 if (KeywordVirtualFound)
2029 if (ImportStatement)
2032 if (Line.startsWith(TT_ObjCMethodSpecifier)) {
2033 if (Contexts.back().FirstObjCSelectorName) {
2034 Contexts.back().FirstObjCSelectorName->LongestObjCSelectorName =
2035 Contexts.back().LongestObjCSelectorName;
2040 for (
const auto &ctx : Contexts)
2041 if (ctx.ContextType == Context::StructArrayInitializer)
2048 bool isClosureImportStatement(
const FormatToken &Tok) {
2051 return Tok.TokenText ==
"goog" && Tok.Next && Tok.Next->is(tok::period) &&
2053 (Tok.Next->Next->TokenText ==
"module" ||
2054 Tok.Next->Next->TokenText ==
"provide" ||
2055 Tok.Next->Next->TokenText ==
"require" ||
2056 Tok.Next->Next->TokenText ==
"requireType" ||
2057 Tok.Next->Next->TokenText ==
"forwardDeclare") &&
2058 Tok.Next->Next->Next && Tok.Next->Next->Next->is(tok::l_paren);
2061 void resetTokenMetadata() {
2067 if (!CurrentToken->isTypeFinalized() &&
2068 !CurrentToken->isOneOf(
2069 TT_LambdaLSquare, TT_LambdaLBrace, TT_AttributeMacro, TT_IfMacro,
2070 TT_ForEachMacro, TT_TypenameMacro, TT_FunctionLBrace,
2071 TT_ImplicitStringLiteral, TT_InlineASMBrace, TT_FatArrow,
2072 TT_LambdaArrow, TT_NamespaceMacro, TT_OverloadedOperator,
2073 TT_RegexLiteral, TT_TemplateString, TT_ObjCStringLiteral,
2074 TT_UntouchableMacroFunc, TT_StatementAttributeLikeMacro,
2075 TT_FunctionLikeOrFreestandingMacro, TT_ClassLBrace, TT_EnumLBrace,
2076 TT_RecordLBrace, TT_StructLBrace, TT_UnionLBrace, TT_RequiresClause,
2077 TT_RequiresClauseInARequiresExpression, TT_RequiresExpression,
2078 TT_RequiresExpressionLParen, TT_RequiresExpressionLBrace,
2079 TT_BracedListLBrace)) {
2080 CurrentToken->setType(TT_Unknown);
2082 CurrentToken->Role.reset();
2083 CurrentToken->MatchingParen =
nullptr;
2084 CurrentToken->FakeLParens.clear();
2085 CurrentToken->FakeRParens = 0;
2092 CurrentToken->NestingLevel = Contexts.size() - 1;
2093 CurrentToken->BindingStrength = Contexts.back().BindingStrength;
2094 modifyContext(*CurrentToken);
2095 determineTokenType(*CurrentToken);
2096 CurrentToken = CurrentToken->Next;
2098 resetTokenMetadata();
2141 StructArrayInitializer,
2145 C11GenericSelection,
2147 VerilogInstancePortList,
2153 struct ScopedContextCreator {
2154 AnnotatingParser &
P;
2160 P.Contexts.back().BindingStrength + Increase,
2161 P.Contexts.back().IsExpression));
2164 ~ScopedContextCreator() {
2166 if (
P.Contexts.back().ContextType == Context::StructArrayInitializer) {
2167 P.Contexts.pop_back();
2168 P.Contexts.back().ContextType = Context::StructArrayInitializer;
2172 P.Contexts.pop_back();
2176 void modifyContext(
const FormatToken &Current) {
2177 auto AssignmentStartsExpression = [&]() {
2181 if (Line.First->isOneOf(tok::kw_using, tok::kw_return))
2183 if (Line.First->is(tok::kw_template)) {
2184 assert(Current.Previous);
2185 if (Current.Previous->is(tok::kw_operator)) {
2191 const FormatToken *Tok = Line.First->getNextNonComment();
2193 if (Tok->isNot(TT_TemplateOpener)) {
2200 if (Contexts.back().ContextKind == tok::less) {
2201 assert(Current.Previous->Previous);
2202 return !Current.Previous->Previous->isOneOf(tok::kw_typename,
2206 Tok = Tok->MatchingParen;
2209 Tok = Tok->getNextNonComment();
2213 if (Tok->isOneOf(tok::kw_class, tok::kw_enum, tok::kw_struct,
2223 if (Style.isJavaScript() &&
2224 (Line.startsWith(Keywords.kw_type, tok::identifier) ||
2225 Line.startsWith(tok::kw_export, Keywords.kw_type,
2226 tok::identifier))) {
2230 return !Current.Previous || Current.Previous->isNot(tok::kw_operator);
2233 if (AssignmentStartsExpression()) {
2234 Contexts.back().IsExpression =
true;
2235 if (!Line.startsWith(TT_UnaryOperator)) {
2236 for (FormatToken *
Previous = Current.Previous;
2238 !
Previous->Previous->isOneOf(tok::comma, tok::semi);
2240 if (
Previous->isOneOf(tok::r_square, tok::r_paren, tok::greater)) {
2247 if (
Previous->isOneOf(TT_BinaryOperator, TT_UnaryOperator) &&
2249 Previous->Previous->isNot(tok::equal)) {
2250 Previous->setType(TT_PointerOrReference);
2254 }
else if (Current.is(tok::lessless) &&
2255 (!Current.Previous ||
2256 Current.Previous->isNot(tok::kw_operator))) {
2257 Contexts.back().IsExpression =
true;
2258 }
else if (Current.isOneOf(tok::kw_return, tok::kw_throw)) {
2259 Contexts.back().IsExpression =
true;
2260 }
else if (Current.is(TT_TrailingReturnArrow)) {
2261 Contexts.back().IsExpression =
false;
2262 }
else if (Current.isOneOf(TT_LambdaArrow, Keywords.kw_assert)) {
2264 }
else if (Current.Previous &&
2265 Current.Previous->is(TT_CtorInitializerColon)) {
2266 Contexts.back().IsExpression =
true;
2267 Contexts.back().ContextType = Context::CtorInitializer;
2268 }
else if (Current.Previous && Current.Previous->is(TT_InheritanceColon)) {
2269 Contexts.back().ContextType = Context::InheritanceList;
2270 }
else if (Current.isOneOf(tok::r_paren, tok::greater, tok::comma)) {
2271 for (FormatToken *
Previous = Current.Previous;
2274 Previous->setType(TT_PointerOrReference);
2276 if (Line.MustBeDeclaration &&
2277 Contexts.front().ContextType != Context::CtorInitializer) {
2278 Contexts.back().IsExpression =
false;
2280 }
else if (Current.is(tok::kw_new)) {
2281 Contexts.back().CanBeExpression =
false;
2282 }
else if (Current.is(tok::semi) ||
2283 (Current.is(tok::exclaim) && Current.Previous &&
2284 Current.Previous->isNot(tok::kw_operator))) {
2288 Contexts.back().IsExpression =
true;
2292 static FormatToken *untilMatchingParen(FormatToken *Current) {
2296 if (Current->is(tok::l_paren))
2298 if (Current->is(tok::r_paren))
2302 Current = Current->Next;
2307 static bool isDeductionGuide(FormatToken &Current) {
2309 if (Current.Previous && Current.Previous->is(tok::r_paren) &&
2310 Current.startsSequence(tok::arrow, tok::identifier, tok::less)) {
2312 FormatToken *TemplateCloser = Current.Next->Next;
2313 int NestingLevel = 0;
2314 while (TemplateCloser) {
2316 if (TemplateCloser->is(tok::l_paren)) {
2318 TemplateCloser = untilMatchingParen(TemplateCloser);
2319 if (!TemplateCloser)
2322 if (TemplateCloser->is(tok::less))
2324 if (TemplateCloser->is(tok::greater))
2326 if (NestingLevel < 1)
2328 TemplateCloser = TemplateCloser->Next;
2332 if (TemplateCloser && TemplateCloser->Next &&
2333 TemplateCloser->Next->is(tok::semi) &&
2334 Current.Previous->MatchingParen) {
2337 FormatToken *LeadingIdentifier =
2338 Current.Previous->MatchingParen->Previous;
2340 return LeadingIdentifier &&
2341 LeadingIdentifier->TokenText == Current.Next->TokenText;
2347 void determineTokenType(FormatToken &Current) {
2348 if (Current.isNot(TT_Unknown)) {
2353 if ((Style.isJavaScript() || Style.isCSharp()) &&
2354 Current.is(tok::exclaim)) {
2355 if (Current.Previous) {
2357 Style.isJavaScript()
2358 ? Keywords.isJavaScriptIdentifier(
2359 *Current.Previous,
true)
2360 : Current.Previous->is(tok::identifier);
2362 Current.Previous->isOneOf(
2363 tok::kw_default, tok::kw_namespace, tok::r_paren, tok::r_square,
2364 tok::r_brace, tok::kw_false, tok::kw_true, Keywords.kw_type,
2365 Keywords.kw_get, Keywords.kw_init, Keywords.kw_set) ||
2366 Current.Previous->Tok.isLiteral()) {
2367 Current.setType(TT_NonNullAssertion);
2372 Current.Next->isOneOf(TT_BinaryOperator, Keywords.kw_as)) {
2373 Current.setType(TT_NonNullAssertion);
2382 Current.is(Keywords.kw_instanceof)) {
2383 Current.setType(TT_BinaryOperator);
2384 }
else if (isStartOfName(Current) &&
2385 (!Line.MightBeFunctionDecl || Current.NestingLevel != 0)) {
2386 Contexts.back().FirstStartOfName = &Current;
2387 Current.setType(TT_StartOfName);
2388 }
else if (Current.is(tok::semi)) {
2392 Contexts.back().FirstStartOfName =
nullptr;
2393 }
else if (Current.isOneOf(tok::kw_auto, tok::kw___auto_type)) {
2395 }
else if (Current.is(tok::arrow) &&
2397 Current.setType(TT_LambdaArrow);
2398 }
else if (Current.is(tok::arrow) && Style.isVerilog()) {
2400 Current.setType(TT_BinaryOperator);
2401 }
else if (Current.is(tok::arrow) && AutoFound &&
2402 Line.MightBeFunctionDecl && Current.NestingLevel == 0 &&
2403 !Current.Previous->isOneOf(tok::kw_operator, tok::identifier)) {
2405 Current.setType(TT_TrailingReturnArrow);
2406 }
else if (Current.is(tok::arrow) && Current.Previous &&
2407 Current.Previous->is(tok::r_brace) &&
2411 Current.setType(TT_TrailingReturnArrow);
2412 }
else if (isDeductionGuide(Current)) {
2414 Current.setType(TT_TrailingReturnArrow);
2415 }
else if (Current.isPointerOrReference()) {
2416 Current.setType(determineStarAmpUsage(
2418 Contexts.back().CanBeExpression && Contexts.back().IsExpression,
2419 Contexts.back().ContextType == Context::TemplateArgument));
2420 }
else if (Current.isOneOf(tok::minus, tok::plus, tok::caret) ||
2421 (Style.isVerilog() && Current.is(tok::pipe))) {
2422 Current.setType(determinePlusMinusCaretUsage(Current));
2423 if (Current.is(TT_UnaryOperator) && Current.is(tok::caret))
2424 Contexts.back().CaretFound =
true;
2425 }
else if (Current.isOneOf(tok::minusminus, tok::plusplus)) {
2426 Current.setType(determineIncrementUsage(Current));
2427 }
else if (Current.isOneOf(tok::exclaim, tok::tilde)) {
2428 Current.setType(TT_UnaryOperator);
2429 }
else if (Current.is(tok::question)) {
2430 if (Style.isJavaScript() && Line.MustBeDeclaration &&
2431 !Contexts.back().IsExpression) {
2434 Current.setType(TT_JsTypeOptionalQuestion);
2435 }
else if (Style.isTableGen()) {
2437 Current.setType(TT_Unknown);
2439 Current.setType(TT_ConditionalExpr);
2441 }
else if (Current.isBinaryOperator() &&
2442 (!Current.Previous || Current.Previous->isNot(tok::l_square)) &&
2443 (Current.isNot(tok::greater) &&
2445 if (Style.isVerilog()) {
2446 if (Current.is(tok::lessequal) && Contexts.size() == 1 &&
2447 !Contexts.back().VerilogAssignmentFound) {
2451 Current.setFinalizedType(TT_BinaryOperator);
2454 Contexts.back().VerilogAssignmentFound =
true;
2456 Current.setType(TT_BinaryOperator);
2457 }
else if (Current.is(tok::comment)) {
2458 if (Current.TokenText.starts_with(
"/*")) {
2459 if (Current.TokenText.ends_with(
"*/")) {
2460 Current.setType(TT_BlockComment);
2464 Current.Tok.setKind(tok::unknown);
2467 Current.setType(TT_LineComment);
2469 }
else if (Current.is(tok::string_literal)) {
2470 if (Style.isVerilog() && Contexts.back().VerilogMayBeConcatenation &&
2471 Current.getPreviousNonComment() &&
2472 Current.getPreviousNonComment()->isOneOf(tok::comma, tok::l_brace) &&
2473 Current.getNextNonComment() &&
2474 Current.getNextNonComment()->isOneOf(tok::comma, tok::r_brace)) {
2475 Current.setType(TT_StringInConcatenation);
2477 }
else if (Current.is(tok::l_paren)) {
2478 if (lParenStartsCppCast(Current))
2479 Current.setType(TT_CppCastLParen);
2480 }
else if (Current.is(tok::r_paren)) {
2481 if (rParenEndsCast(Current))
2482 Current.setType(TT_CastRParen);
2483 if (Current.MatchingParen && Current.Next &&
2484 !Current.Next->isBinaryOperator() &&
2485 !Current.Next->isOneOf(
2486 tok::semi, tok::colon, tok::l_brace, tok::l_paren, tok::comma,
2487 tok::period, tok::arrow, tok::coloncolon, tok::kw_noexcept)) {
2488 if (FormatToken *AfterParen = Current.MatchingParen->Next;
2489 AfterParen && AfterParen->isNot(tok::caret)) {
2491 if (FormatToken *BeforeParen = Current.MatchingParen->Previous;
2492 BeforeParen && BeforeParen->is(tok::identifier) &&
2493 BeforeParen->isNot(TT_TypenameMacro) &&
2494 BeforeParen->TokenText == BeforeParen->TokenText.upper() &&
2495 (!BeforeParen->Previous ||
2496 BeforeParen->Previous->ClosesTemplateDeclaration ||
2497 BeforeParen->Previous->ClosesRequiresClause)) {
2498 Current.setType(TT_FunctionAnnotationRParen);
2502 }
else if (Current.is(tok::at) && Current.Next && !Style.isJavaScript() &&
2506 switch (Current.Next->Tok.getObjCKeywordID()) {
2507 case tok::objc_interface:
2508 case tok::objc_implementation:
2509 case tok::objc_protocol:
2510 Current.setType(TT_ObjCDecl);
2512 case tok::objc_property:
2513 Current.setType(TT_ObjCProperty);
2518 }
else if (Current.is(tok::period)) {
2519 FormatToken *PreviousNoComment = Current.getPreviousNonComment();
2520 if (PreviousNoComment &&
2521 PreviousNoComment->isOneOf(tok::comma, tok::l_brace)) {
2522 Current.setType(TT_DesignatedInitializerPeriod);
2524 Current.Previous->isOneOf(TT_JavaAnnotation,
2525 TT_LeadingJavaAnnotation)) {
2526 Current.setType(Current.Previous->getType());
2528 }
else if (canBeObjCSelectorComponent(Current) &&
2531 Current.Previous && Current.Previous->is(TT_CastRParen) &&
2532 Current.Previous->MatchingParen &&
2533 Current.Previous->MatchingParen->Previous &&
2534 Current.Previous->MatchingParen->Previous->is(
2535 TT_ObjCMethodSpecifier)) {
2539 Current.setType(TT_SelectorName);
2540 }
else if (Current.isOneOf(tok::identifier, tok::kw_const, tok::kw_noexcept,
2541 tok::kw_requires) &&
2543 !Current.Previous->isOneOf(tok::equal, tok::at,
2544 TT_CtorInitializerComma,
2545 TT_CtorInitializerColon) &&
2546 Line.MightBeFunctionDecl && Contexts.size() == 1) {
2549 Current.setType(TT_TrailingAnnotation);
2551 Style.isJavaScript()) &&
2553 if (Current.Previous->is(tok::at) &&
2554 Current.isNot(Keywords.kw_interface)) {
2555 const FormatToken &AtToken = *Current.Previous;
2556 const FormatToken *
Previous = AtToken.getPreviousNonComment();
2558 Current.setType(TT_LeadingJavaAnnotation);
2560 Current.setType(TT_JavaAnnotation);
2561 }
else if (Current.Previous->is(tok::period) &&
2562 Current.Previous->isOneOf(TT_JavaAnnotation,
2563 TT_LeadingJavaAnnotation)) {
2564 Current.setType(Current.Previous->getType());
2574 bool isStartOfName(
const FormatToken &Tok) {
2576 if (Style.isVerilog())
2579 if (Tok.isNot(tok::identifier) || !Tok.Previous)
2582 if (
const auto *NextNonComment = Tok.getNextNonComment();
2583 (!NextNonComment && !Line.InMacroBody) ||
2585 (NextNonComment->isPointerOrReference() ||
2586 NextNonComment->is(tok::string_literal) ||
2587 (Line.InPragmaDirective && NextNonComment->is(tok::identifier))))) {
2591 if (Tok.Previous->isOneOf(TT_LeadingJavaAnnotation, Keywords.kw_instanceof,
2595 if (Style.isJavaScript() && Tok.Previous->is(Keywords.kw_in))
2599 FormatToken *PreviousNotConst = Tok.getPreviousNonComment();
2602 if (!Style.isJavaScript())
2603 while (PreviousNotConst && PreviousNotConst->is(tok::kw_const))
2604 PreviousNotConst = PreviousNotConst->getPreviousNonComment();
2606 if (!PreviousNotConst)
2609 if (PreviousNotConst->ClosesRequiresClause)
2612 if (Style.isTableGen()) {
2614 if (Keywords.isTableGenDefinition(*PreviousNotConst))
2617 if (Contexts.back().ContextKind != tok::l_brace)
2621 bool IsPPKeyword = PreviousNotConst->is(tok::identifier) &&
2622 PreviousNotConst->Previous &&
2623 PreviousNotConst->Previous->is(tok::hash);
2625 if (PreviousNotConst->is(TT_TemplateCloser)) {
2626 return PreviousNotConst && PreviousNotConst->MatchingParen &&
2627 PreviousNotConst->MatchingParen->Previous &&
2628 PreviousNotConst->MatchingParen->Previous->isNot(tok::period) &&
2629 PreviousNotConst->MatchingParen->Previous->isNot(tok::kw_template);
2632 if ((PreviousNotConst->is(tok::r_paren) &&
2633 PreviousNotConst->is(TT_TypeDeclarationParen)) ||
2634 PreviousNotConst->is(TT_AttributeRParen)) {
2643 if (PreviousNotConst->isOneOf(tok::identifier, tok::kw_auto) &&
2644 PreviousNotConst->isNot(TT_StatementAttributeLikeMacro)) {
2649 if (PreviousNotConst->is(TT_PointerOrReference))
2653 if (PreviousNotConst->isTypeName(LangOpts))
2658 PreviousNotConst->is(tok::r_square)) {
2663 return Style.isJavaScript() && PreviousNotConst->is(tok::kw_const);
2667 bool lParenStartsCppCast(
const FormatToken &Tok) {
2672 FormatToken *LeftOfParens = Tok.getPreviousNonComment();
2673 if (LeftOfParens && LeftOfParens->is(TT_TemplateCloser) &&
2674 LeftOfParens->MatchingParen) {
2675 auto *Prev = LeftOfParens->MatchingParen->getPreviousNonComment();
2677 Prev->isOneOf(tok::kw_const_cast, tok::kw_dynamic_cast,
2678 tok::kw_reinterpret_cast, tok::kw_static_cast)) {
2688 bool rParenEndsCast(
const FormatToken &Tok) {
2689 assert(Tok.is(tok::r_paren));
2691 if (!Tok.MatchingParen || !Tok.Previous)
2698 const auto *LParen = Tok.MatchingParen;
2699 const auto *BeforeRParen = Tok.Previous;
2700 const auto *AfterRParen = Tok.Next;
2703 if (BeforeRParen == LParen || !AfterRParen)
2706 if (LParen->is(TT_OverloadedOperatorLParen))
2709 auto *LeftOfParens = LParen->getPreviousNonComment();
2713 if (LeftOfParens->is(tok::r_paren) &&
2714 LeftOfParens->isNot(TT_CastRParen)) {
2715 if (!LeftOfParens->MatchingParen ||
2716 !LeftOfParens->MatchingParen->Previous) {
2719 LeftOfParens = LeftOfParens->MatchingParen->Previous;
2722 if (LeftOfParens->is(tok::r_square)) {
2724 auto MayBeArrayDelete = [](FormatToken *Tok) -> FormatToken * {
2725 if (Tok->isNot(tok::r_square))
2728 Tok = Tok->getPreviousNonComment();
2729 if (!Tok || Tok->isNot(tok::l_square))
2732 Tok = Tok->getPreviousNonComment();
2733 if (!Tok || Tok->isNot(tok::kw_delete))
2737 if (FormatToken *MaybeDelete = MayBeArrayDelete(LeftOfParens))
2738 LeftOfParens = MaybeDelete;
2744 if (LeftOfParens->Tok.getIdentifierInfo() && LeftOfParens->Previous &&
2745 LeftOfParens->Previous->is(tok::kw_operator)) {
2751 if (LeftOfParens->Tok.getIdentifierInfo() &&
2752 !LeftOfParens->isOneOf(Keywords.kw_in, tok::kw_return, tok::kw_case,
2753 tok::kw_delete, tok::kw_throw)) {
2759 if (LeftOfParens->isOneOf(tok::at, tok::r_square, TT_OverloadedOperator,
2760 TT_TemplateCloser, tok::ellipsis)) {
2765 if (AfterRParen->is(tok::question) ||
2766 (AfterRParen->is(tok::ampamp) && !BeforeRParen->isTypeName(LangOpts))) {
2771 if (AfterRParen->is(Keywords.kw_in) && Style.isCSharp())
2776 if (AfterRParen->isOneOf(tok::kw_noexcept, tok::kw_volatile, tok::kw_const,
2777 tok::kw_requires, tok::kw_throw, tok::arrow,
2778 Keywords.kw_override, Keywords.kw_final) ||
2779 isCppAttribute(IsCpp, *AfterRParen)) {
2789 if (AfterRParen->isOneOf(tok::kw_sizeof, tok::kw_alignof) ||
2790 (AfterRParen->Tok.isLiteral() &&
2791 AfterRParen->isNot(tok::string_literal))) {
2796 auto IsQualifiedPointerOrReference = [](
const FormatToken *
T,
2797 const LangOptions &LangOpts) {
2799 assert(!
T->isTypeName(LangOpts) &&
"Should have already been checked");
2803 if (
T->is(TT_AttributeRParen)) {
2805 assert(
T->is(tok::r_paren));
2806 assert(
T->MatchingParen);
2807 assert(
T->MatchingParen->is(tok::l_paren));
2808 assert(
T->MatchingParen->is(TT_AttributeLParen));
2809 if (
const auto *Tok =
T->MatchingParen->Previous;
2810 Tok && Tok->isAttribute()) {
2814 }
else if (
T->is(TT_AttributeSquare)) {
2816 if (
T->MatchingParen &&
T->MatchingParen->Previous) {
2817 T =
T->MatchingParen->Previous;
2820 }
else if (
T->canBePointerOrReferenceQualifier()) {
2826 return T &&
T->is(TT_PointerOrReference);
2828 bool ParensAreType =
2829 BeforeRParen->isOneOf(TT_TemplateCloser, TT_TypeDeclarationParen) ||
2830 BeforeRParen->isTypeName(LangOpts) ||
2831 IsQualifiedPointerOrReference(BeforeRParen, LangOpts);
2832 bool ParensCouldEndDecl =
2833 AfterRParen->isOneOf(tok::equal, tok::semi, tok::l_brace, tok::greater);
2834 if (ParensAreType && !ParensCouldEndDecl)
2845 for (
const auto *Token = LParen->Next; Token != &Tok; Token = Token->Next)
2846 if (Token->is(TT_BinaryOperator))
2851 if (AfterRParen->isOneOf(tok::identifier, tok::kw_this))
2855 if (AfterRParen->is(tok::l_paren)) {
2856 for (
const auto *Prev = BeforeRParen; Prev->is(tok::identifier);) {
2857 Prev = Prev->Previous;
2858 if (Prev->is(tok::coloncolon))
2859 Prev = Prev->Previous;
2865 if (!AfterRParen->Next)
2868 if (AfterRParen->is(tok::l_brace) &&
2876 const bool NextIsAmpOrStar = AfterRParen->isOneOf(tok::amp, tok::star);
2877 if (!(AfterRParen->isUnaryOperator() || NextIsAmpOrStar) ||
2878 AfterRParen->is(tok::plus) ||
2879 !AfterRParen->Next->isOneOf(tok::identifier, tok::numeric_constant)) {
2883 if (NextIsAmpOrStar &&
2884 (AfterRParen->Next->is(tok::numeric_constant) || Line.InPPDirective)) {
2888 if (Line.InPPDirective && AfterRParen->is(tok::minus))
2891 const auto *Prev = BeforeRParen;
2894 if (Prev->is(tok::r_paren)) {
2895 if (Prev->is(TT_CastRParen))
2897 Prev = Prev->MatchingParen;
2900 Prev = Prev->Previous;
2901 if (!Prev || Prev->isNot(tok::r_paren))
2903 Prev = Prev->MatchingParen;
2904 return Prev && Prev->is(TT_FunctionTypeLParen);
2908 for (Prev = BeforeRParen; Prev != LParen; Prev = Prev->Previous)
2909 if (!Prev->isOneOf(tok::kw_const, tok::identifier, tok::coloncolon))
2916 bool determineUnaryOperatorByUsage(
const FormatToken &Tok) {
2917 const FormatToken *PrevToken = Tok.getPreviousNonComment();
2927 if (PrevToken->isOneOf(
2928 TT_ConditionalExpr, tok::l_paren, tok::comma, tok::colon, tok::semi,
2929 tok::equal, tok::question, tok::l_square, tok::l_brace,
2930 tok::kw_case, tok::kw_co_await, tok::kw_co_return, tok::kw_co_yield,
2931 tok::kw_delete, tok::kw_return, tok::kw_throw)) {
2938 if (PrevToken->is(tok::kw_sizeof))
2942 if (PrevToken->isOneOf(TT_CastRParen, TT_UnaryOperator))
2946 if (PrevToken->is(TT_BinaryOperator))
2954 bool InTemplateArgument) {
2955 if (Style.isJavaScript())
2956 return TT_BinaryOperator;
2959 if (Style.isCSharp() && Tok.is(tok::ampamp))
2960 return TT_BinaryOperator;
2962 if (Style.isVerilog()) {
2967 if (Tok.is(tok::star))
2968 return TT_BinaryOperator;
2969 return determineUnaryOperatorByUsage(Tok) ? TT_UnaryOperator
2970 : TT_BinaryOperator;
2973 const FormatToken *PrevToken = Tok.getPreviousNonComment();
2975 return TT_UnaryOperator;
2976 if (PrevToken->is(TT_TypeName))
2977 return TT_PointerOrReference;
2978 if (PrevToken->isOneOf(tok::kw_new, tok::kw_delete) && Tok.is(tok::ampamp))
2979 return TT_BinaryOperator;
2981 const FormatToken *NextToken = Tok.getNextNonComment();
2983 if (InTemplateArgument && NextToken && NextToken->is(tok::kw_noexcept))
2984 return TT_BinaryOperator;
2987 NextToken->isOneOf(tok::arrow, tok::equal, tok::comma, tok::r_paren,
2988 TT_RequiresClause) ||
2990 NextToken->canBePointerOrReferenceQualifier() ||
2991 (NextToken->is(tok::l_brace) && !NextToken->getNextNonComment())) {
2992 return TT_PointerOrReference;
2995 if (PrevToken->is(tok::coloncolon))
2996 return TT_PointerOrReference;
2998 if (PrevToken->is(tok::r_paren) && PrevToken->is(TT_TypeDeclarationParen))
2999 return TT_PointerOrReference;
3001 if (determineUnaryOperatorByUsage(Tok))
3002 return TT_UnaryOperator;
3004 if (NextToken->is(tok::l_square) && NextToken->isNot(TT_LambdaLSquare))
3005 return TT_PointerOrReference;
3007 return TT_PointerOrReference;
3008 if (NextToken->isOneOf(tok::comma, tok::semi))
3009 return TT_PointerOrReference;
3021 if (PrevToken->is(tok::r_brace) && Tok.is(tok::star) &&
3022 !PrevToken->MatchingParen) {
3023 return TT_PointerOrReference;
3026 if (PrevToken->endsSequence(tok::r_square, tok::l_square, tok::kw_delete))
3027 return TT_UnaryOperator;
3029 if (PrevToken->Tok.isLiteral() ||
3030 PrevToken->isOneOf(tok::r_paren, tok::r_square, tok::kw_true,
3031 tok::kw_false, tok::r_brace)) {
3032 return TT_BinaryOperator;
3035 const FormatToken *NextNonParen = NextToken;
3036 while (NextNonParen && NextNonParen->is(tok::l_paren))
3037 NextNonParen = NextNonParen->getNextNonComment();
3038 if (NextNonParen && (NextNonParen->Tok.isLiteral() ||
3039 NextNonParen->isOneOf(tok::kw_true, tok::kw_false) ||
3040 NextNonParen->isUnaryOperator())) {
3041 return TT_BinaryOperator;
3047 if (InTemplateArgument && NextToken->Tok.isAnyIdentifier())
3048 return TT_BinaryOperator;
3052 if (Tok.is(tok::ampamp) &&
3053 NextToken->isOneOf(tok::l_paren, tok::star, tok::amp)) {
3054 return TT_BinaryOperator;
3059 if (NextToken->Tok.isAnyIdentifier()) {
3060 const FormatToken *NextNextToken = NextToken->getNextNonComment();
3061 if (NextNextToken && NextNextToken->is(tok::arrow))
3062 return TT_BinaryOperator;
3068 return TT_BinaryOperator;
3071 if (!Scopes.empty() && Scopes.back() ==
ST_Class)
3072 return TT_PointerOrReference;
3075 auto IsChainedOperatorAmpOrMember = [](
const FormatToken *token) {
3076 return !token || token->isOneOf(tok::amp, tok::period, tok::arrow,
3077 tok::arrowstar, tok::periodstar);
3082 if (Tok.is(tok::amp) && PrevToken && PrevToken->Tok.isAnyIdentifier() &&
3083 IsChainedOperatorAmpOrMember(PrevToken->getPreviousNonComment()) &&
3084 NextToken && NextToken->Tok.isAnyIdentifier()) {
3085 if (
auto NextNext = NextToken->getNextNonComment();
3087 (IsChainedOperatorAmpOrMember(NextNext) || NextNext->is(tok::semi))) {
3088 return TT_BinaryOperator;
3092 return TT_PointerOrReference;
3095 TokenType determinePlusMinusCaretUsage(
const FormatToken &Tok) {
3096 if (determineUnaryOperatorByUsage(Tok))
3097 return TT_UnaryOperator;
3099 const FormatToken *PrevToken = Tok.getPreviousNonComment();
3101 return TT_UnaryOperator;
3103 if (PrevToken->is(tok::at))
3104 return TT_UnaryOperator;
3107 return TT_BinaryOperator;
3111 TokenType determineIncrementUsage(
const FormatToken &Tok) {
3112 const FormatToken *PrevToken = Tok.getPreviousNonComment();
3113 if (!PrevToken || PrevToken->is(TT_CastRParen))
3114 return TT_UnaryOperator;
3115 if (PrevToken->isOneOf(tok::r_paren, tok::r_square, tok::identifier))
3116 return TT_TrailingUnaryOperator;
3118 return TT_UnaryOperator;
3121 SmallVector<Context, 8> Contexts;
3123 const FormatStyle &Style;
3124 AnnotatedLine &Line;
3125 FormatToken *CurrentToken;
3128 LangOptions LangOpts;
3129 const AdditionalKeywords &Keywords;
3131 SmallVector<ScopeType> &Scopes;
3139 int TemplateDeclarationDepth;
3147class ExpressionParser {
3149 ExpressionParser(
const FormatStyle &Style,
const AdditionalKeywords &Keywords,
3150 AnnotatedLine &Line)
3151 : Style(Style), Keywords(Keywords), Line(Line), Current(Line.
First) {}
3154 void parse(
int Precedence = 0) {
3157 while (Current && (Current->is(tok::kw_return) ||
3158 (Current->is(tok::colon) &&
3159 Current->isOneOf(TT_ObjCMethodExpr, TT_DictLiteral)))) {
3163 if (!Current || Precedence > PrecedenceArrowAndPeriod)
3168 parseConditionalExpr();
3174 if (Precedence == PrecedenceUnaryOperator) {
3175 parseUnaryOperator();
3179 FormatToken *Start = Current;
3180 FormatToken *LatestOperator =
nullptr;
3181 unsigned OperatorIndex = 0;
3183 FormatToken *VerilogFirstOfType =
nullptr;
3192 if (Style.isVerilog() && Precedence ==
prec::Comma) {
3193 VerilogFirstOfType =
3194 verilogGroupDecl(VerilogFirstOfType, LatestOperator);
3198 parse(Precedence + 1);
3200 int CurrentPrecedence = getCurrentPrecedence();
3211 if (Precedence == CurrentPrecedence && Current &&
3212 Current->is(TT_SelectorName)) {
3214 addFakeParenthesis(Start,
prec::Level(Precedence));
3218 if ((Style.isCSharp() || Style.isJavaScript() ||
3223 FormatToken *Prev = Current->getPreviousNonComment();
3224 if (Prev && Prev->is(tok::string_literal) &&
3225 (Prev == Start || Prev->endsSequence(tok::string_literal, tok::plus,
3226 TT_StringInConcatenation))) {
3227 Prev->setType(TT_StringInConcatenation);
3234 (Current->closesScope() &&
3235 (Current->MatchingParen || Current->is(TT_TemplateString))) ||
3236 (CurrentPrecedence != -1 && CurrentPrecedence < Precedence) ||
3245 if (Current->opensScope() ||
3246 Current->isOneOf(TT_RequiresClause,
3247 TT_RequiresClauseInARequiresExpression)) {
3250 while (Current && (!Current->closesScope() || Current->opensScope())) {
3257 if (CurrentPrecedence == Precedence) {
3259 LatestOperator->NextOperator = Current;
3260 LatestOperator = Current;
3261 Current->OperatorIndex = OperatorIndex;
3264 next(Precedence > 0);
3269 if (Style.isVerilog() && Precedence ==
prec::Comma && VerilogFirstOfType)
3270 addFakeParenthesis(VerilogFirstOfType,
prec::Comma);
3272 if (LatestOperator && (Current || Precedence > 0)) {
3278 Start->Previous->isOneOf(TT_RequiresClause,
3279 TT_RequiresClauseInARequiresExpression))
3281 auto Ret = Current ? Current : Line.Last;
3282 while (!
Ret->ClosesRequiresClause &&
Ret->Previous)
3288 if (Precedence == PrecedenceArrowAndPeriod) {
3292 addFakeParenthesis(Start,
prec::Level(Precedence), End);
3300 int getCurrentPrecedence() {
3302 const FormatToken *NextNonComment = Current->getNextNonComment();
3303 if (Current->is(TT_ConditionalExpr))
3305 if (NextNonComment && Current->is(TT_SelectorName) &&
3306 (NextNonComment->isOneOf(TT_DictLiteral, TT_JsTypeColon) ||
3307 (Style.isProto() && NextNonComment->is(tok::less)))) {
3310 if (Current->is(TT_JsComputedPropertyName))
3312 if (Current->is(TT_LambdaArrow))
3314 if (Current->is(TT_FatArrow))
3316 if (Current->isOneOf(tok::semi, TT_InlineASMColon, TT_SelectorName) ||
3317 (Current->is(tok::comment) && NextNonComment &&
3318 NextNonComment->is(TT_SelectorName))) {
3321 if (Current->is(TT_RangeBasedForLoopColon))
3324 Current->is(Keywords.kw_instanceof)) {
3327 if (Style.isJavaScript() &&
3328 Current->isOneOf(Keywords.kw_in, Keywords.kw_as)) {
3331 if (Current->is(TT_BinaryOperator) || Current->is(tok::comma))
3332 return Current->getPrecedence();
3333 if (Current->isOneOf(tok::period, tok::arrow) &&
3334 Current->isNot(TT_TrailingReturnArrow)) {
3335 return PrecedenceArrowAndPeriod;
3338 Current->isOneOf(Keywords.kw_extends, Keywords.kw_implements,
3339 Keywords.kw_throws)) {
3344 if (Style.isVerilog() && Current->is(tok::colon))
3350 void addFakeParenthesis(FormatToken *Start,
prec::Level Precedence,
3351 FormatToken *End =
nullptr) {
3356 if (Start->MacroParent)
3359 Start->FakeLParens.push_back(Precedence);
3361 Start->StartsBinaryExpression =
true;
3362 if (!End && Current)
3363 End = Current->getPreviousNonComment();
3367 End->EndsBinaryExpression =
true;
3373 void parseUnaryOperator() {
3375 while (Current && Current->is(TT_UnaryOperator)) {
3376 Tokens.push_back(Current);
3379 parse(PrecedenceArrowAndPeriod);
3380 for (FormatToken *Token : llvm::reverse(Tokens)) {
3386 void parseConditionalExpr() {
3387 while (Current && Current->isTrailingComment())
3389 FormatToken *Start = Current;
3391 if (!Current || Current->isNot(tok::question))
3395 if (!Current || Current->isNot(TT_ConditionalExpr))
3402 void next(
bool SkipPastLeadingComments =
true) {
3404 Current = Current->Next;
3406 (Current->NewlinesBefore == 0 || SkipPastLeadingComments) &&
3407 Current->isTrailingComment()) {
3408 Current = Current->Next;
3414 FormatToken *verilogGroupDecl(FormatToken *FirstOfType,
3415 FormatToken *PreviousComma) {
3419 FormatToken *Start = Current;
3422 while (Start->startsSequence(tok::l_paren, tok::star)) {
3423 if (!(Start = Start->MatchingParen) ||
3424 !(Start = Start->getNextNonComment())) {
3429 FormatToken *Tok = Start;
3431 if (Tok->is(Keywords.kw_assign))
3432 Tok = Tok->getNextNonComment();
3440 FormatToken *
First =
nullptr;
3442 FormatToken *Next = Tok->getNextNonComment();
3444 if (Tok->is(tok::hash)) {
3449 Tok = Tok->getNextNonComment();
3450 }
else if (Tok->is(tok::hashhash)) {
3454 Tok = Tok->getNextNonComment();
3455 }
else if (Keywords.isVerilogQualifier(*Tok) ||
3456 Keywords.isVerilogIdentifier(*Tok)) {
3460 while (Tok && Tok->isOneOf(tok::period, tok::coloncolon) &&
3461 (Tok = Tok->getNextNonComment())) {
3462 if (Keywords.isVerilogIdentifier(*Tok))
3463 Tok = Tok->getNextNonComment();
3467 }
else if (Tok->is(tok::l_paren)) {
3472 Keywords.kw_highz0, Keywords.kw_highz1, Keywords.kw_large,
3473 Keywords.kw_medium, Keywords.kw_pull0, Keywords.kw_pull1,
3474 Keywords.kw_small, Keywords.kw_strong0, Keywords.kw_strong1,
3475 Keywords.kw_supply0, Keywords.kw_supply1, Keywords.kw_weak0,
3476 Keywords.kw_weak1)) {
3477 Tok->setType(TT_VerilogStrength);
3478 Tok = Tok->MatchingParen;
3480 Tok->setType(TT_VerilogStrength);
3481 Tok = Tok->getNextNonComment();
3486 }
else if (Tok->is(Keywords.kw_verilogHash)) {
3488 if (Next->is(tok::l_paren))
3489 Next = Next->MatchingParen;
3491 Tok = Next->getNextNonComment();
3498 FormatToken *Second =
nullptr;
3500 while (Tok && Tok->is(tok::l_square) && (Tok = Tok->MatchingParen))
3501 Tok = Tok->getNextNonComment();
3502 if (Tok && (Tok->is(tok::hash) || Keywords.isVerilogIdentifier(*Tok)))
3507 FormatToken *TypedName =
nullptr;
3511 First->setType(TT_VerilogDimensionedTypeName);
3512 }
else if (
First != Start) {
3520 if (TypedName->is(TT_Unknown))
3521 TypedName->setType(TT_StartOfName);
3523 if (FirstOfType && PreviousComma) {
3524 PreviousComma->setType(TT_VerilogTypeComma);
3525 addFakeParenthesis(FirstOfType,
prec::Comma, PreviousComma->Previous);
3528 FirstOfType = TypedName;
3535 while (Current && Current != FirstOfType) {
3536 if (Current->opensScope()) {
3547 const FormatStyle &Style;
3548 const AdditionalKeywords &Keywords;
3549 const AnnotatedLine &Line;
3550 FormatToken *Current;
3559 assert(
Line->First);
3566 Line->First->OriginalColumn) {
3567 const bool PPDirectiveOrImportStmt =
3570 if (PPDirectiveOrImportStmt)
3576 PPDirectiveOrImportStmt
3578 : NextNonCommentLine->
Level;
3580 NextNonCommentLine =
Line->First->isNot(tok::r_brace) ?
Line :
nullptr;
3589 for (
const auto *Tok =
Line.First; Tok; Tok = Tok->Next)
3598 for (
FormatToken *Tok =
Line.getFirstNonComment(), *Name =
nullptr; Tok;
3599 Tok = Tok->getNextNonComment()) {
3601 if (Tok->is(tok::l_square) && Tok->is(TT_AttributeSquare)) {
3610 if (Tok->is(tok::l_paren) && Tok->is(TT_Unknown) && Tok->MatchingParen) {
3618 if (Tok->isOneOf(tok::kw_friend, tok::kw_inline, tok::kw_virtual,
3619 tok::kw_constexpr, tok::kw_consteval, tok::kw_explicit)) {
3624 if (Tok->is(tok::coloncolon)) {
3631 while (Tok->startsSequence(tok::identifier, tok::coloncolon)) {
3633 Tok = Tok->Next->Next;
3639 if (Tok->is(tok::tilde)) {
3646 if (Tok->isNot(tok::identifier) || Tok->isNot(TT_Unknown))
3657 assert(Tok && Tok->
is(tok::identifier));
3660 if (Prev && Prev->is(tok::tilde))
3663 if (!Prev || !Prev->endsSequence(tok::coloncolon, tok::identifier))
3666 assert(Prev->Previous);
3667 return Prev->Previous->TokenText == Tok->
TokenText;
3671 if (!
Line.InMacroBody)
3672 MacroBodyScopes.clear();
3674 auto &ScopeStack =
Line.InMacroBody ? MacroBodyScopes : Scopes;
3675 AnnotatingParser
Parser(Style,
Line, Keywords, ScopeStack);
3678 if (!
Line.Children.empty()) {
3680 for (
auto &Child :
Line.Children)
3683 if (!ScopeStack.empty())
3684 ScopeStack.pop_back();
3697 ExpressionParser ExprParser(Style, Keywords,
Line);
3703 if (Tok && ((!ScopeStack.empty() && ScopeStack.back() ==
ST_Class) ||
3705 Tok->setFinalizedType(TT_CtorDtorDeclName);
3706 assert(OpeningParen);
3711 if (
Line.startsWith(TT_ObjCMethodSpecifier))
3713 else if (
Line.startsWith(TT_ObjCDecl))
3715 else if (
Line.startsWith(TT_ObjCProperty))
3719 First->SpacesRequiredBefore = 1;
3720 First->CanBreakBefore =
First->MustBreakBefore;
3729 if (Current.
is(TT_FunctionDeclarationName))
3738 if (Prev->is(tok::coloncolon))
3739 Prev = Prev->Previous;
3746 if (
const auto *PrevPrev =
Previous.getPreviousNonComment();
3747 PrevPrev && PrevPrev->is(TT_ObjCDecl)) {
3751 auto skipOperatorName =
3753 for (; Next; Next = Next->Next) {
3754 if (Next->is(TT_OverloadedOperatorLParen))
3756 if (Next->is(TT_OverloadedOperator))
3758 if (Next->isOneOf(tok::kw_new, tok::kw_delete)) {
3761 Next->Next->startsSequence(tok::l_square, tok::r_square)) {
3762 Next = Next->Next->Next;
3766 if (Next->startsSequence(tok::l_square, tok::r_square)) {
3771 if ((Next->isTypeName(LangOpts) || Next->is(tok::identifier)) &&
3772 Next->Next && Next->Next->isPointerOrReference()) {
3777 if (Next->is(TT_TemplateOpener) && Next->MatchingParen) {
3778 Next = Next->MatchingParen;
3787 const auto *Next = Current.
Next;
3788 const bool IsCpp = LangOpts.CXXOperatorNames;
3791 if (Current.
is(tok::kw_operator)) {
3792 if (
Previous.Tok.getIdentifierInfo() &&
3793 !
Previous.isOneOf(tok::kw_return, tok::kw_co_return)) {
3798 assert(
Previous.MatchingParen->is(tok::l_paren));
3799 assert(
Previous.MatchingParen->is(TT_TypeDeclarationParen));
3804 Next = skipOperatorName(Next);
3808 for (; Next; Next = Next->Next) {
3809 if (Next->is(TT_TemplateOpener) && Next->MatchingParen) {
3810 Next = Next->MatchingParen;
3811 }
else if (Next->is(tok::coloncolon)) {
3815 if (Next->is(tok::kw_operator)) {
3816 Next = skipOperatorName(Next->Next);
3819 if (Next->isNot(tok::identifier))
3821 }
else if (isCppAttribute(IsCpp, *Next)) {
3822 Next = Next->MatchingParen;
3825 }
else if (Next->is(tok::l_paren)) {
3834 if (!Next || Next->isNot(tok::l_paren) || !Next->MatchingParen)
3836 ClosingParen = Next->MatchingParen;
3837 assert(ClosingParen->
is(tok::r_paren));
3839 if (
Line.Last->is(tok::l_brace))
3841 if (Next->Next == ClosingParen)
3844 if (ClosingParen->
Next && ClosingParen->
Next->
is(TT_PointerOrReference))
3857 if (IsCpp && Next->Next && Next->Next->is(tok::identifier) &&
3858 !
Line.endsWith(tok::semi)) {
3862 for (
const FormatToken *Tok = Next->Next; Tok && Tok != ClosingParen;
3864 if (Tok->is(TT_TypeDeclarationParen))
3866 if (Tok->isOneOf(tok::l_paren, TT_TemplateOpener) && Tok->MatchingParen) {
3867 Tok = Tok->MatchingParen;
3870 if (Tok->is(tok::kw_const) || Tok->isTypeName(LangOpts) ||
3871 Tok->isOneOf(TT_PointerOrReference, TT_StartOfName, tok::ellipsis)) {
3874 if (Tok->isOneOf(tok::l_brace, TT_ObjCMethodExpr) || Tok->Tok.isLiteral())
3880bool TokenAnnotator::mustBreakForReturnType(
const AnnotatedLine &
Line)
const {
3881 assert(
Line.MightBeFunctionDecl);
3889 switch (Style.BreakAfterReturnType) {
3899 return Line.mightBeFunctionDefinition();
3909 Line.Computed =
true;
3917 :
Line.FirstStartColumn +
First->ColumnWidth;
3918 bool AlignArrayOfStructures =
3921 if (AlignArrayOfStructures)
3922 calculateArrayInitializerColumnList(
Line);
3924 const auto *FirstNonComment =
Line.getFirstNonComment();
3925 bool SeenName =
false;
3926 bool LineIsFunctionDeclaration =
false;
3930 for (
auto *Tok = FirstNonComment ? FirstNonComment->Next :
nullptr; Tok;
3932 if (Tok->is(TT_StartOfName))
3934 if (Tok->Previous->EndsCppAttributeGroup)
3935 AfterLastAttribute = Tok;
3936 if (
const bool IsCtorOrDtor = Tok->is(TT_CtorDtorDeclName);
3941 LineIsFunctionDeclaration =
true;
3945 assert(OpeningParen);
3946 if (OpeningParen->is(TT_Unknown))
3947 OpeningParen->setType(TT_FunctionDeclarationLParen);
3954 (LineIsFunctionDeclaration ||
3955 (FirstNonComment && FirstNonComment->is(TT_CtorDtorDeclName))) &&
3956 Line.endsWith(tok::semi, tok::r_brace)) {
3957 auto *Tok =
Line.Last->Previous;
3958 while (Tok->isNot(tok::r_brace))
3959 Tok = Tok->Previous;
3960 if (
auto *LBrace = Tok->MatchingParen; LBrace) {
3961 assert(LBrace->is(tok::l_brace));
3964 LBrace->setFinalizedType(TT_FunctionLBrace);
3968 if (IsCpp && SeenName && AfterLastAttribute &&
3971 if (LineIsFunctionDeclaration)
3972 Line.ReturnTypeWrapped =
true;
3976 if (!LineIsFunctionDeclaration) {
3978 for (
const auto *Tok = FirstNonComment; Tok; Tok = Tok->Next) {
3979 if (Tok->isNot(tok::kw_operator))
3983 }
while (Tok && Tok->isNot(TT_OverloadedOperatorLParen));
3984 if (!Tok || !Tok->MatchingParen)
3986 const auto *LeftParen = Tok;
3987 for (Tok = Tok->Next; Tok && Tok != LeftParen->MatchingParen;
3989 if (Tok->isNot(tok::identifier))
3991 auto *Next = Tok->Next;
3992 const bool NextIsBinaryOperator =
3993 Next && Next->isPointerOrReference() && Next->Next &&
3994 Next->Next->is(tok::identifier);
3995 if (!NextIsBinaryOperator)
3997 Next->setType(TT_BinaryOperator);
4001 }
else if (ClosingParen) {
4002 for (
auto *Tok = ClosingParen->
Next; Tok; Tok = Tok->
Next) {
4003 if (Tok->is(TT_CtorInitializerColon))
4005 if (Tok->is(tok::arrow)) {
4006 Tok->setType(TT_TrailingReturnArrow);
4009 if (Tok->isNot(TT_TrailingAnnotation))
4011 const auto *Next = Tok->Next;
4012 if (!Next || Next->isNot(tok::l_paren))
4014 Tok = Next->MatchingParen;
4021 bool InFunctionDecl =
Line.MightBeFunctionDecl;
4022 for (
auto *Current =
First->Next; Current; Current = Current->Next) {
4024 if (Current->is(TT_LineComment)) {
4026 Current->SpacesRequiredBefore =
4027 (Style.Cpp11BracedListStyle && !Style.SpacesInParensOptions.Other)
4030 }
else if (Prev->
is(TT_VerilogMultiLineListLParen)) {
4031 Current->SpacesRequiredBefore = 0;
4033 Current->SpacesRequiredBefore = Style.SpacesBeforeTrailingComments;
4043 if (!Current->HasUnescapedNewline) {
4046 if (
Parameter->isOneOf(tok::comment, tok::r_brace))
4049 if (
Parameter->Previous->isNot(TT_CtorInitializerComma) &&
4057 }
else if (!Current->Finalized && Current->SpacesRequiredBefore == 0 &&
4058 spaceRequiredBefore(
Line, *Current)) {
4059 Current->SpacesRequiredBefore = 1;
4062 const auto &Children = Prev->
Children;
4063 if (!Children.empty() && Children.back()->Last->is(TT_LineComment)) {
4064 Current->MustBreakBefore =
true;
4066 Current->MustBreakBefore =
4067 Current->MustBreakBefore || mustBreakBefore(
Line, *Current);
4068 if (!Current->MustBreakBefore && InFunctionDecl &&
4069 Current->is(TT_FunctionDeclarationName)) {
4070 Current->MustBreakBefore = mustBreakForReturnType(
Line);
4074 Current->CanBreakBefore =
4075 Current->MustBreakBefore || canBreakBefore(
Line, *Current);
4076 unsigned ChildSize = 0;
4082 if (Current->MustBreakBefore || Prev->
Children.size() > 1 ||
4084 Prev->
Children[0]->First->MustBreakBefore) ||
4085 Current->IsMultiline) {
4086 Current->TotalLength = Prev->
TotalLength + Style.ColumnLimit;
4088 Current->TotalLength = Prev->
TotalLength + Current->ColumnWidth +
4089 ChildSize + Current->SpacesRequiredBefore;
4092 if (Current->is(TT_CtorInitializerColon))
4093 InFunctionDecl =
false;
4104 Current->SplitPenalty = splitPenalty(
Line, *Current, InFunctionDecl);
4106 Current->is(TT_SelectorName) && Current->ParameterIndex > 0) {
4107 if (Current->ParameterIndex == 1)
4108 Current->SplitPenalty += 5 * Current->BindingStrength;
4110 Current->SplitPenalty += 20 * Current->BindingStrength;
4114 calculateUnbreakableTailLengths(
Line);
4115 unsigned IndentLevel =
Line.Level;
4116 for (
auto *Current =
First; Current; Current = Current->Next) {
4118 Current->Role->precomputeFormattingInfos(Current);
4119 if (Current->MatchingParen &&
4120 Current->MatchingParen->opensBlockOrBlockTypeList(Style) &&
4124 Current->IndentLevel = IndentLevel;
4125 if (Current->opensBlockOrBlockTypeList(Style))
4129 LLVM_DEBUG({ printDebugInfo(
Line); });
4132void TokenAnnotator::calculateUnbreakableTailLengths(
4134 unsigned UnbreakableTailLength = 0;
4137 Current->UnbreakableTailLength = UnbreakableTailLength;
4138 if (Current->CanBreakBefore ||
4139 Current->isOneOf(tok::comment, tok::string_literal)) {
4140 UnbreakableTailLength = 0;
4142 UnbreakableTailLength +=
4143 Current->ColumnWidth + Current->SpacesRequiredBefore;
4145 Current = Current->Previous;
4149void TokenAnnotator::calculateArrayInitializerColumnList(
4150 AnnotatedLine &
Line)
const {
4153 auto *CurrentToken =
Line.First;
4154 CurrentToken->ArrayInitializerLineStart =
true;
4156 while (CurrentToken && CurrentToken !=
Line.Last) {
4157 if (CurrentToken->is(tok::l_brace)) {
4158 CurrentToken->IsArrayInitializer =
true;
4159 if (CurrentToken->Next)
4160 CurrentToken->Next->MustBreakBefore =
true;
4162 calculateInitializerColumnList(
Line, CurrentToken->Next, Depth + 1);
4164 CurrentToken = CurrentToken->Next;
4169FormatToken *TokenAnnotator::calculateInitializerColumnList(
4170 AnnotatedLine &
Line, FormatToken *CurrentToken,
unsigned Depth)
const {
4171 while (CurrentToken && CurrentToken !=
Line.Last) {
4172 if (CurrentToken->is(tok::l_brace))
4174 else if (CurrentToken->is(tok::r_brace))
4176 if (Depth == 2 && CurrentToken->isOneOf(tok::l_brace, tok::comma)) {
4177 CurrentToken = CurrentToken->Next;
4180 CurrentToken->StartsColumn =
true;
4181 CurrentToken = CurrentToken->Previous;
4183 CurrentToken = CurrentToken->Next;
4185 return CurrentToken;
4188unsigned TokenAnnotator::splitPenalty(
const AnnotatedLine &
Line,
4189 const FormatToken &Tok,
4190 bool InFunctionDecl)
const {
4191 const FormatToken &
Left = *Tok.Previous;
4192 const FormatToken &
Right = Tok;
4194 if (
Left.is(tok::semi))
4199 if (
Right.isOneOf(Keywords.kw_extends, Keywords.kw_throws))
4201 if (
Right.is(Keywords.kw_implements))
4203 if (
Left.is(tok::comma) &&
Left.NestingLevel == 0)
4205 }
else if (Style.isJavaScript()) {
4206 if (
Right.is(Keywords.kw_function) &&
Left.isNot(tok::comma))
4208 if (
Left.is(TT_JsTypeColon))
4210 if ((
Left.is(TT_TemplateString) &&
Left.TokenText.ends_with(
"${")) ||
4211 (
Right.is(TT_TemplateString) &&
Right.TokenText.starts_with(
"}"))) {
4215 if (
Left.opensScope() &&
Right.closesScope())
4218 if (
Right.is(tok::l_square))
4220 if (
Right.is(tok::period))
4224 if (
Right.is(tok::identifier) &&
Right.Next &&
Right.Next->is(TT_DictLiteral))
4226 if (
Right.is(tok::l_square)) {
4227 if (
Left.is(tok::r_square))
4230 if (
Right.is(TT_LambdaLSquare) &&
Left.is(tok::equal))
4232 if (!
Right.isOneOf(TT_ObjCMethodExpr, TT_LambdaLSquare,
4233 TT_ArrayInitializerLSquare,
4234 TT_DesignatedInitializerLSquare, TT_AttributeSquare)) {
4239 if (
Left.is(tok::coloncolon))
4240 return Style.PenaltyBreakScopeResolution;
4241 if (
Right.isOneOf(TT_StartOfName, TT_FunctionDeclarationName) ||
4242 Right.is(tok::kw_operator)) {
4243 if (
Line.startsWith(tok::kw_for) &&
Right.PartOfMultiVariableDeclStmt)
4245 if (
Left.is(TT_StartOfName))
4247 if (InFunctionDecl &&
Right.NestingLevel == 0)
4248 return Style.PenaltyReturnTypeOnItsOwnLine;
4251 if (
Right.is(TT_PointerOrReference))
4253 if (
Right.is(TT_LambdaArrow))
4255 if (
Left.is(tok::equal) &&
Right.is(tok::l_brace))
4257 if (
Left.is(TT_CastRParen))
4259 if (
Left.isOneOf(tok::kw_class, tok::kw_struct, tok::kw_union))
4261 if (
Left.is(tok::comment))
4264 if (
Left.isOneOf(TT_RangeBasedForLoopColon, TT_InheritanceColon,
4265 TT_CtorInitializerColon)) {
4269 if (
Right.isMemberAccess()) {
4289 return !
Right.NextOperator || !
Right.NextOperator->Previous->closesScope()
4294 if (
Right.is(TT_TrailingAnnotation) &&
4295 (!
Right.Next ||
Right.Next->isNot(tok::l_paren))) {
4298 if (
Line.startsWith(TT_ObjCMethodSpecifier))
4305 bool is_short_annotation =
Right.TokenText.size() < 10;
4306 return (
Left.is(tok::r_paren) ? 100 : 120) + (is_short_annotation ? 50 : 0);
4310 if (
Line.startsWith(tok::kw_for) &&
Left.is(tok::equal))
4315 if (
Right.is(TT_SelectorName))
4317 if (
Left.is(tok::colon) &&
Left.is(TT_ObjCMethodExpr))
4318 return Line.MightBeFunctionDecl ? 50 : 500;
4324 Left.Previous->isOneOf(tok::identifier, tok::greater)) {
4328 if (
Left.is(tok::l_paren) && Style.PenaltyBreakOpenParenthesis != 0)
4329 return Style.PenaltyBreakOpenParenthesis;
4330 if (
Left.is(tok::l_paren) && InFunctionDecl &&
4334 if (
Left.is(tok::l_paren) &&
Left.Previous &&
4335 (
Left.Previous->isOneOf(tok::kw_for, tok::kw__Generic) ||
4336 Left.Previous->isIf())) {
4339 if (
Left.is(tok::equal) && InFunctionDecl)
4341 if (
Right.is(tok::r_brace))
4343 if (
Left.is(TT_TemplateOpener))
4345 if (
Left.opensScope()) {
4350 (
Left.ParameterCount <= 1 || Style.AllowAllArgumentsOnNextLine)) {
4353 if (
Left.is(tok::l_brace) && !Style.Cpp11BracedListStyle)
4355 return Left.ParameterCount > 1 ? Style.PenaltyBreakBeforeFirstCallParameter
4358 if (
Left.is(TT_JavaAnnotation))
4361 if (
Left.is(TT_UnaryOperator))
4363 if (
Left.isOneOf(tok::plus, tok::comma) &&
Left.Previous &&
4364 Left.Previous->isLabelString() &&
4365 (
Left.NextOperator ||
Left.OperatorIndex != 0)) {
4368 if (
Right.is(tok::plus) &&
Left.isLabelString() &&
4369 (
Right.NextOperator ||
Right.OperatorIndex != 0)) {
4372 if (
Left.is(tok::comma))
4374 if (
Right.is(tok::lessless) &&
Left.isLabelString() &&
4375 (
Right.NextOperator ||
Right.OperatorIndex != 1)) {
4378 if (
Right.is(tok::lessless)) {
4380 if (
Left.isNot(tok::r_paren) ||
Right.OperatorIndex > 0) {
4386 if (
Left.ClosesTemplateDeclaration)
4387 return Style.PenaltyBreakTemplateDeclaration;
4388 if (
Left.ClosesRequiresClause)
4390 if (
Left.is(TT_ConditionalExpr))
4396 return Style.PenaltyBreakAssignment;
4403bool TokenAnnotator::spaceRequiredBeforeParens(
const FormatToken &Right)
const {
4406 if (
Right.is(TT_OverloadedOperatorLParen) &&
4407 Style.SpaceBeforeParensOptions.AfterOverloadedOperator) {
4410 if (Style.SpaceBeforeParensOptions.BeforeNonEmptyParentheses &&
4411 Right.ParameterCount > 0) {
4417bool TokenAnnotator::spaceRequiredBetween(
const AnnotatedLine &
Line,
4418 const FormatToken &Left,
4419 const FormatToken &Right)
const {
4420 if (
Left.is(tok::kw_return) &&
4421 !
Right.isOneOf(tok::semi, tok::r_paren, tok::hashhash)) {
4424 if (
Left.is(tok::kw_throw) &&
Right.is(tok::l_paren) &&
Right.MatchingParen &&
4425 Right.MatchingParen->is(TT_CastRParen)) {
4431 Left.Tok.getObjCKeywordID() == tok::objc_property) {
4434 if (
Right.is(tok::hashhash))
4435 return Left.is(tok::hash);
4436 if (
Left.isOneOf(tok::hashhash, tok::hash))
4437 return Right.is(tok::hash);
4439 Right.MatchingParen == &Left &&
Line.Children.empty()) {
4440 return Style.SpaceInEmptyBlock;
4443 if ((
Left.is(tok::l_paren) &&
Right.is(tok::r_paren)) ||
4446 return Style.SpacesInParensOptions.InEmptyParentheses;
4448 if (Style.SpacesInParensOptions.ExceptDoubleParentheses &&
4449 Left.is(tok::r_paren) &&
Right.is(tok::r_paren)) {
4450 auto *InnerLParen =
Left.MatchingParen;
4451 if (InnerLParen && InnerLParen->Previous ==
Right.MatchingParen) {
4452 InnerLParen->SpacesRequiredBefore = 0;
4456 const FormatToken *LeftParen =
nullptr;
4457 if (
Left.is(tok::l_paren))
4459 else if (
Right.is(tok::r_paren) &&
Right.MatchingParen)
4460 LeftParen =
Right.MatchingParen;
4461 if (LeftParen && (LeftParen->is(TT_ConditionLParen) ||
4462 (LeftParen->Previous &&
4463 isKeywordWithCondition(*LeftParen->Previous)))) {
4464 return Style.SpacesInParensOptions.InConditionalStatements;
4469 if (
Left.is(tok::kw_auto) &&
Right.isOneOf(TT_LambdaLBrace, TT_FunctionLBrace,
4471 TT_FunctionTypeLParen)) {
4476 if (
Left.is(tok::kw_auto) &&
Right.isOneOf(tok::l_paren, tok::l_brace))
4479 const auto *BeforeLeft =
Left.Previous;
4482 if (
Right.is(tok::l_paren) &&
Left.is(tok::kw_co_await) && BeforeLeft &&
4483 BeforeLeft->is(tok::kw_operator)) {
4487 if (
Left.isOneOf(tok::kw_co_await, tok::kw_co_yield, tok::kw_co_return) &&
4488 !
Right.isOneOf(tok::semi, tok::r_paren)) {
4492 if (
Left.is(tok::l_paren) ||
Right.is(tok::r_paren)) {
4493 return (
Right.is(TT_CastRParen) ||
4494 (
Left.MatchingParen &&
Left.MatchingParen->is(TT_CastRParen)))
4495 ? Style.SpacesInParensOptions.InCStyleCasts
4496 : Style.SpacesInParensOptions.Other;
4498 if (
Right.isOneOf(tok::semi, tok::comma))
4501 bool IsLightweightGeneric =
Right.MatchingParen &&
4502 Right.MatchingParen->Next &&
4503 Right.MatchingParen->Next->is(tok::colon);
4504 return !IsLightweightGeneric && Style.ObjCSpaceBeforeProtocolList;
4506 if (
Right.is(tok::less) &&
Left.is(tok::kw_template))
4507 return Style.SpaceAfterTemplateKeyword;
4508 if (
Left.isOneOf(tok::exclaim, tok::tilde))
4510 if (
Left.is(tok::at) &&
4511 Right.isOneOf(tok::identifier, tok::string_literal, tok::char_constant,
4512 tok::numeric_constant, tok::l_paren, tok::l_brace,
4513 tok::kw_true, tok::kw_false)) {
4516 if (
Left.is(tok::colon))
4517 return Left.isNot(TT_ObjCMethodExpr);
4518 if (
Left.is(tok::coloncolon))
4520 if (
Left.is(tok::less) ||
Right.isOneOf(tok::greater, tok::less)) {
4523 (
Left.is(TT_DictLiteral) ||
Right.is(TT_DictLiteral)))) {
4525 if (
Left.is(tok::less) &&
Right.is(tok::greater))
4527 return !Style.Cpp11BracedListStyle;
4530 if (
Right.isNot(TT_OverloadedOperatorLParen))
4533 if (
Right.is(tok::ellipsis)) {
4534 return Left.Tok.isLiteral() || (
Left.is(tok::identifier) && BeforeLeft &&
4535 BeforeLeft->is(tok::kw_case));
4537 if (
Left.is(tok::l_square) &&
Right.is(tok::amp))
4538 return Style.SpacesInSquareBrackets;
4539 if (
Right.is(TT_PointerOrReference)) {
4540 if (
Left.is(tok::r_paren) &&
Line.MightBeFunctionDecl) {
4541 if (!
Left.MatchingParen)
4543 FormatToken *TokenBeforeMatchingParen =
4544 Left.MatchingParen->getPreviousNonComment();
4545 if (!TokenBeforeMatchingParen ||
Left.isNot(TT_TypeDeclarationParen))
4553 (
Left.is(TT_AttributeRParen) ||
4554 Left.canBePointerOrReferenceQualifier())) {
4557 if (
Left.Tok.isLiteral())
4560 if (
Left.isTypeOrIdentifier(LangOpts) &&
Right.Next &&
Right.Next->Next &&
4561 Right.Next->Next->is(TT_RangeBasedForLoopColon)) {
4562 return getTokenPointerOrReferenceAlignment(Right) !=
4565 return !
Left.isOneOf(TT_PointerOrReference, tok::l_paren) &&
4566 (getTokenPointerOrReferenceAlignment(Right) !=
4568 (
Line.IsMultiVariableDeclStmt &&
4569 (
Left.NestingLevel == 0 ||
4570 (
Left.NestingLevel == 1 && startsWithInitStatement(
Line)))));
4572 if (
Right.is(TT_FunctionTypeLParen) &&
Left.isNot(tok::l_paren) &&
4573 (
Left.isNot(TT_PointerOrReference) ||
4575 !
Line.IsMultiVariableDeclStmt))) {
4578 if (
Left.is(TT_PointerOrReference)) {
4583 Right.canBePointerOrReferenceQualifier()) {
4587 if (
Right.Tok.isLiteral())
4590 if (
Right.is(TT_BlockComment))
4594 if (
Right.isOneOf(Keywords.kw_override, Keywords.kw_final, tok::kw_noexcept,
4595 TT_RequiresClause) &&
4596 Right.isNot(TT_StartOfName)) {
4603 if (BeforeLeft && BeforeLeft->isTypeOrIdentifier(LangOpts) &&
Right.Next &&
4604 Right.Next->is(TT_RangeBasedForLoopColon)) {
4605 return getTokenPointerOrReferenceAlignment(Left) !=
4608 if (
Right.isOneOf(TT_PointerOrReference, TT_ArraySubscriptLSquare,
4620 if (
Line.IsMultiVariableDeclStmt &&
4621 (
Left.NestingLevel ==
Line.First->NestingLevel ||
4622 ((
Left.NestingLevel ==
Line.First->NestingLevel + 1) &&
4623 startsWithInitStatement(
Line)))) {
4628 if (BeforeLeft->is(tok::coloncolon)) {
4629 if (
Left.isNot(tok::star))
4632 if (!
Right.startsSequence(tok::identifier, tok::r_paren))
4635 const auto *LParen =
Right.Next->MatchingParen;
4636 return !LParen || LParen->isNot(TT_FunctionTypeLParen);
4638 return !BeforeLeft->isOneOf(tok::l_paren, tok::l_square);
4641 if (
Left.is(tok::ellipsis) && BeforeLeft &&
4642 BeforeLeft->isPointerOrReference()) {
4646 if (
Right.is(tok::star) &&
Left.is(tok::l_paren))
4648 if (
Left.is(tok::star) &&
Right.isPointerOrReference())
4650 if (
Right.isPointerOrReference()) {
4661 if (
Previous->is(tok::coloncolon)) {
4680 if (
Previous->endsSequence(tok::kw_operator))
4684 (Style.SpaceAroundPointerQualifiers ==
4690 if (Style.isCSharp() &&
Left.is(Keywords.kw_is) &&
Right.is(tok::l_square))
4692 const auto SpaceRequiredForArrayInitializerLSquare =
4693 [](
const FormatToken &LSquareTok,
const FormatStyle &Style) {
4694 return Style.SpacesInContainerLiterals ||
4695 (Style.isProto() && !Style.Cpp11BracedListStyle &&
4696 LSquareTok.endsSequence(tok::l_square, tok::colon,
4699 if (
Left.is(tok::l_square)) {
4700 return (
Left.is(TT_ArrayInitializerLSquare) &&
Right.isNot(tok::r_square) &&
4701 SpaceRequiredForArrayInitializerLSquare(Left, Style)) ||
4702 (
Left.isOneOf(TT_ArraySubscriptLSquare, TT_StructuredBindingLSquare,
4703 TT_LambdaLSquare) &&
4704 Style.SpacesInSquareBrackets &&
Right.isNot(tok::r_square));
4706 if (
Right.is(tok::r_square)) {
4707 return Right.MatchingParen &&
4708 ((
Right.MatchingParen->is(TT_ArrayInitializerLSquare) &&
4709 SpaceRequiredForArrayInitializerLSquare(*
Right.MatchingParen,
4711 (Style.SpacesInSquareBrackets &&
4712 Right.MatchingParen->isOneOf(TT_ArraySubscriptLSquare,
4713 TT_StructuredBindingLSquare,
4714 TT_LambdaLSquare)));
4716 if (
Right.is(tok::l_square) &&
4717 !
Right.isOneOf(TT_ObjCMethodExpr, TT_LambdaLSquare,
4718 TT_DesignatedInitializerLSquare,
4719 TT_StructuredBindingLSquare, TT_AttributeSquare) &&
4720 !
Left.isOneOf(tok::numeric_constant, TT_DictLiteral) &&
4721 !(
Left.isNot(tok::r_square) && Style.SpaceBeforeSquareBrackets &&
4722 Right.is(TT_ArraySubscriptLSquare))) {
4725 if (
Left.is(tok::l_brace) &&
Right.is(tok::r_brace))
4726 return !
Left.Children.empty();
4728 (
Right.is(tok::r_brace) &&
Right.MatchingParen &&
4730 return !Style.Cpp11BracedListStyle || Style.SpacesInParensOptions.Other;
4732 if (
Left.is(TT_BlockComment)) {
4734 return Style.isJavaScript() || !
Left.TokenText.ends_with(
"=*/");
4739 if (
Left.is(TT_TemplateCloser) &&
Right.is(TT_AttributeSquare))
4742 if (
Right.is(tok::l_paren)) {
4743 if (
Left.is(TT_TemplateCloser) &&
Right.isNot(TT_FunctionTypeLParen))
4744 return spaceRequiredBeforeParens(Right);
4745 if (
Left.isOneOf(TT_RequiresClause,
4746 TT_RequiresClauseInARequiresExpression)) {
4747 return Style.SpaceBeforeParensOptions.AfterRequiresInClause ||
4748 spaceRequiredBeforeParens(Right);
4750 if (
Left.is(TT_RequiresExpression)) {
4751 return Style.SpaceBeforeParensOptions.AfterRequiresInExpression ||
4752 spaceRequiredBeforeParens(Right);
4754 if (
Left.is(TT_AttributeRParen) ||
4755 (
Left.is(tok::r_square) &&
Left.is(TT_AttributeSquare))) {
4758 if (
Left.is(TT_ForEachMacro)) {
4759 return Style.SpaceBeforeParensOptions.AfterForeachMacros ||
4760 spaceRequiredBeforeParens(Right);
4762 if (
Left.is(TT_IfMacro)) {
4763 return Style.SpaceBeforeParensOptions.AfterIfMacros ||
4764 spaceRequiredBeforeParens(Right);
4767 Left.isOneOf(tok::kw_new, tok::kw_delete) &&
4768 Right.isNot(TT_OverloadedOperatorLParen) &&
4769 !(
Line.MightBeFunctionDecl &&
Left.is(TT_FunctionDeclarationName))) {
4770 const auto *RParen =
Right.MatchingParen;
4771 return Style.SpaceBeforeParensOptions.AfterPlacementOperator ||
4772 (RParen && RParen->is(TT_CastRParen));
4776 if (
Left.is(tok::semi))
4778 if (
Left.isOneOf(tok::pp_elif, tok::kw_for, tok::kw_while, tok::kw_switch,
4779 tok::kw_case, TT_ForEachMacro, TT_ObjCForIn) ||
4781 Right.is(TT_ConditionLParen)) {
4782 return Style.SpaceBeforeParensOptions.AfterControlStatements ||
4783 spaceRequiredBeforeParens(Right);
4788 if (
Right.is(TT_OverloadedOperatorLParen))
4789 return spaceRequiredBeforeParens(Right);
4791 if (
Line.MightBeFunctionDecl &&
Right.is(TT_FunctionDeclarationLParen)) {
4792 if (spaceRequiredBeforeParens(Right))
4794 const auto &Options = Style.SpaceBeforeParensOptions;
4795 return Line.mightBeFunctionDefinition()
4796 ? Options.AfterFunctionDefinitionName
4797 : Options.AfterFunctionDeclarationName;
4801 Left.MatchingParen &&
Left.MatchingParen->is(TT_LambdaLSquare)) {
4802 return Style.SpaceBeforeParensOptions.AfterFunctionDefinitionName ||
4803 spaceRequiredBeforeParens(Right);
4805 if (!BeforeLeft || !BeforeLeft->isOneOf(tok::period, tok::arrow)) {
4806 if (
Left.isOneOf(tok::kw_try, Keywords.kw___except, tok::kw_catch)) {
4807 return Style.SpaceBeforeParensOptions.AfterControlStatements ||
4808 spaceRequiredBeforeParens(Right);
4810 if (
Left.isOneOf(tok::kw_new, tok::kw_delete)) {
4811 return ((!
Line.MightBeFunctionDecl || !BeforeLeft) &&
4813 spaceRequiredBeforeParens(Right);
4816 if (
Left.is(tok::r_square) &&
Left.MatchingParen &&
4817 Left.MatchingParen->Previous &&
4818 Left.MatchingParen->Previous->is(tok::kw_delete)) {
4820 spaceRequiredBeforeParens(Right);
4825 (
Left.Tok.getIdentifierInfo() ||
Left.is(tok::r_paren))) {
4826 return spaceRequiredBeforeParens(Right);
4830 if (
Left.is(tok::at) &&
Right.Tok.getObjCKeywordID() != tok::objc_not_keyword)
4832 if (
Right.is(TT_UnaryOperator)) {
4833 return !
Left.isOneOf(tok::l_paren, tok::l_square, tok::at) &&
4834 (
Left.isNot(tok::colon) ||
Left.isNot(TT_ObjCMethodExpr));
4840 if (!Style.isVerilog() &&
4841 (
Left.isOneOf(tok::identifier, tok::greater, tok::r_square,
4843 Left.isTypeName(LangOpts)) &&
4844 Right.is(tok::l_brace) &&
Right.getNextNonComment() &&
4848 if (
Left.is(tok::period) ||
Right.is(tok::period))
4852 if (
Right.is(tok::hash) &&
Left.is(tok::identifier) &&
4853 (
Left.TokenText ==
"L" ||
Left.TokenText ==
"u" ||
4854 Left.TokenText ==
"U" ||
Left.TokenText ==
"u8" ||
4855 Left.TokenText ==
"LR" ||
Left.TokenText ==
"uR" ||
4856 Left.TokenText ==
"UR" ||
Left.TokenText ==
"u8R")) {
4859 if (
Left.is(TT_TemplateCloser) &&
Left.MatchingParen &&
4860 Left.MatchingParen->Previous &&
4861 (
Left.MatchingParen->Previous->is(tok::period) ||
4862 Left.MatchingParen->Previous->is(tok::coloncolon))) {
4868 if (
Left.is(TT_TemplateCloser) &&
Right.is(tok::l_square))
4870 if (
Left.is(tok::l_brace) &&
Left.endsSequence(TT_DictLiteral, tok::at)) {
4874 if (
Right.is(tok::r_brace) &&
Right.MatchingParen &&
4875 Right.MatchingParen->endsSequence(TT_DictLiteral, tok::at)) {
4879 if (
Right.is(TT_TrailingAnnotation) &&
Right.isOneOf(tok::amp, tok::ampamp) &&
4880 Left.isOneOf(tok::kw_const, tok::kw_volatile) &&
4881 (!
Right.Next ||
Right.Next->is(tok::semi))) {
4891bool TokenAnnotator::spaceRequiredBefore(
const AnnotatedLine &
Line,
4892 const FormatToken &Right)
const {
4893 const FormatToken &
Left = *
Right.Previous;
4898 return Right.hasWhitespaceBefore();
4900 const bool IsVerilog = Style.isVerilog();
4901 assert(!IsVerilog || !IsCpp);
4904 if (Keywords.isWordLike(Right, IsVerilog) &&
4905 Keywords.isWordLike(Left, IsVerilog)) {
4911 if (
Left.is(tok::star) &&
Right.is(tok::comment))
4914 const auto *BeforeLeft =
Left.Previous;
4917 if (
Left.is(TT_OverloadedOperator) &&
4918 Right.isOneOf(TT_TemplateOpener, TT_TemplateCloser)) {
4922 if (
Right.is(tok::period) &&
Left.is(tok::numeric_constant))
4926 if (
Left.is(Keywords.kw_import) &&
Right.isOneOf(tok::less, tok::ellipsis))
4929 if (
Left.isOneOf(Keywords.kw_module, Keywords.kw_import) &&
4930 Right.is(TT_ModulePartitionColon)) {
4934 if (
Left.is(tok::identifier) &&
Right.is(TT_ModulePartitionColon))
4937 if (
Left.is(TT_ModulePartitionColon) &&
4938 Right.isOneOf(tok::identifier, tok::kw_private)) {
4941 if (
Left.is(tok::ellipsis) &&
Right.is(tok::identifier) &&
4942 Line.First->is(Keywords.kw_import)) {
4946 if (
Left.isOneOf(TT_AttributeRParen, TT_AttributeMacro) &&
4947 Right.is(tok::coloncolon)) {
4951 if (
Left.is(tok::kw_operator))
4952 return Right.is(tok::coloncolon);
4954 !
Left.opensScope() && Style.SpaceBeforeCpp11BracedList) {
4957 if (
Left.is(tok::less) &&
Left.is(TT_OverloadedOperator) &&
4958 Right.is(TT_TemplateOpener)) {
4962 if (
Left.is(tok::identifier) &&
Right.is(tok::numeric_constant))
4963 return Right.TokenText[0] !=
'.';
4965 if (
Left.Tok.getIdentifierInfo() &&
Right.Tok.isLiteral())
4967 }
else if (Style.isProto()) {
4968 if (
Right.is(tok::period) && !(BeforeLeft && BeforeLeft->is(tok::period)) &&
4969 Left.isOneOf(Keywords.kw_optional, Keywords.kw_required,
4970 Keywords.kw_repeated, Keywords.kw_extend)) {
4973 if (
Right.is(tok::l_paren) &&
4974 Left.isOneOf(Keywords.kw_returns, Keywords.kw_option)) {
4977 if (
Right.isOneOf(tok::l_brace, tok::less) &&
Left.is(TT_SelectorName))
4980 if (
Left.is(tok::slash) ||
Right.is(tok::slash))
4982 if (
Left.MatchingParen &&
4983 Left.MatchingParen->is(TT_ProtoExtensionLSquare) &&
4984 Right.isOneOf(tok::l_brace, tok::less)) {
4985 return !Style.Cpp11BracedListStyle;
4988 if (
Left.is(tok::percent))
4992 if (
Left.is(tok::numeric_constant) &&
Right.is(tok::percent))
4993 return Right.hasWhitespaceBefore();
4994 }
else if (Style.isJson()) {
4995 if (
Right.is(tok::colon) &&
Left.is(tok::string_literal))
4996 return Style.SpaceBeforeJsonColon;
4997 }
else if (Style.isCSharp()) {
5003 if (
Left.is(tok::kw_this) &&
Right.is(tok::l_square))
5007 if (
Left.is(tok::kw_new) &&
Right.is(tok::l_paren))
5011 if (
Right.is(tok::l_brace))
5015 if (
Left.is(tok::l_brace) &&
Right.isNot(tok::r_brace))
5018 if (
Left.isNot(tok::l_brace) &&
Right.is(tok::r_brace))
5022 if (
Left.is(TT_FatArrow) ||
Right.is(TT_FatArrow))
5026 if (
Left.is(TT_AttributeColon) ||
Right.is(TT_AttributeColon))
5030 if (
Left.is(TT_TemplateCloser) &&
Right.is(TT_StartOfName))
5034 if (
Left.is(tok::l_square) ||
Right.is(tok::r_square))
5035 return Style.SpacesInSquareBrackets;
5038 if (
Right.is(TT_CSharpNullable))
5042 if (
Right.is(TT_NonNullAssertion))
5046 if (
Left.is(tok::comma) &&
Right.is(tok::comma))
5050 if (
Left.is(Keywords.kw_var) &&
Right.is(tok::l_paren))
5054 if (
Right.is(tok::l_paren)) {
5055 if (
Left.isOneOf(tok::kw_using, Keywords.kw_async, Keywords.kw_when,
5056 Keywords.kw_lock)) {
5057 return Style.SpaceBeforeParensOptions.AfterControlStatements ||
5058 spaceRequiredBeforeParens(Right);
5064 if ((
Left.isAccessSpecifierKeyword() ||
5065 Left.isOneOf(tok::kw_virtual, tok::kw_extern, tok::kw_static,
5066 Keywords.kw_internal, Keywords.kw_abstract,
5067 Keywords.kw_sealed, Keywords.kw_override,
5068 Keywords.kw_async, Keywords.kw_unsafe)) &&
5069 Right.is(tok::l_paren)) {
5072 }
else if (Style.isJavaScript()) {
5073 if (
Left.is(TT_FatArrow))
5076 if (
Right.is(tok::l_paren) &&
Left.is(Keywords.kw_await) && BeforeLeft &&
5077 BeforeLeft->is(tok::kw_for)) {
5080 if (
Left.is(Keywords.kw_async) &&
Right.is(tok::l_paren) &&
5081 Right.MatchingParen) {
5082 const FormatToken *Next =
Right.MatchingParen->getNextNonComment();
5085 if (Next && Next->is(TT_FatArrow))
5088 if ((
Left.is(TT_TemplateString) &&
Left.TokenText.ends_with(
"${")) ||
5089 (
Right.is(TT_TemplateString) &&
Right.TokenText.starts_with(
"}"))) {
5094 if (Keywords.isJavaScriptIdentifier(Left,
5096 Right.is(TT_TemplateString)) {
5099 if (
Right.is(tok::star) &&
5100 Left.isOneOf(Keywords.kw_function, Keywords.kw_yield)) {
5103 if (
Right.isOneOf(tok::l_brace, tok::l_square) &&
5104 Left.isOneOf(Keywords.kw_function, Keywords.kw_yield,
5105 Keywords.kw_extends, Keywords.kw_implements)) {
5108 if (
Right.is(tok::l_paren)) {
5110 if (
Line.MustBeDeclaration &&
Left.Tok.getIdentifierInfo())
5114 if (BeforeLeft && BeforeLeft->is(tok::period) &&
5115 Left.Tok.getIdentifierInfo()) {
5119 if (
Left.isOneOf(tok::kw_throw, Keywords.kw_await, Keywords.kw_typeof,
5125 if (
Left.endsSequence(tok::kw_const, Keywords.kw_as))
5127 if ((
Left.isOneOf(Keywords.kw_let, Keywords.kw_var, Keywords.kw_in,
5132 (
Left.is(Keywords.kw_of) && BeforeLeft &&
5133 (BeforeLeft->is(tok::identifier) ||
5134 BeforeLeft->isOneOf(tok::r_square, tok::r_brace)))) &&
5135 (!BeforeLeft || BeforeLeft->isNot(tok::period))) {
5138 if (
Left.isOneOf(tok::kw_for, Keywords.kw_as) && BeforeLeft &&
5139 BeforeLeft->is(tok::period) &&
Right.is(tok::l_paren)) {
5142 if (
Left.is(Keywords.kw_as) &&
5143 Right.isOneOf(tok::l_square, tok::l_brace, tok::l_paren)) {
5146 if (
Left.is(tok::kw_default) && BeforeLeft &&
5147 BeforeLeft->is(tok::kw_export)) {
5150 if (
Left.is(Keywords.kw_is) &&
Right.is(tok::l_brace))
5152 if (
Right.isOneOf(TT_JsTypeColon, TT_JsTypeOptionalQuestion))
5154 if (
Left.is(TT_JsTypeOperator) ||
Right.is(TT_JsTypeOperator))
5156 if ((
Left.is(tok::l_brace) ||
Right.is(tok::r_brace)) &&
5157 Line.First->isOneOf(Keywords.kw_import, tok::kw_export)) {
5160 if (
Left.is(tok::ellipsis))
5162 if (
Left.is(TT_TemplateCloser) &&
5163 !
Right.isOneOf(tok::equal, tok::l_brace, tok::comma, tok::l_square,
5164 Keywords.kw_implements, Keywords.kw_extends)) {
5170 if (
Right.is(TT_NonNullAssertion))
5172 if (
Left.is(TT_NonNullAssertion) &&
5173 Right.isOneOf(Keywords.kw_as, Keywords.kw_in)) {
5177 if (
Left.is(TT_CaseLabelArrow) ||
Right.is(TT_CaseLabelArrow))
5179 if (
Left.is(tok::r_square) &&
Right.is(tok::l_brace))
5182 if (
Left.is(tok::l_square) ||
Right.is(tok::r_square))
5183 return Style.SpacesInSquareBrackets;
5185 if (
Left.is(Keywords.kw_synchronized) &&
Right.is(tok::l_paren)) {
5186 return Style.SpaceBeforeParensOptions.AfterControlStatements ||
5187 spaceRequiredBeforeParens(Right);
5189 if ((
Left.isAccessSpecifierKeyword() ||
5190 Left.isOneOf(tok::kw_static, Keywords.kw_final, Keywords.kw_abstract,
5191 Keywords.kw_native)) &&
5192 Right.is(TT_TemplateOpener)) {
5195 }
else if (IsVerilog) {
5197 if (
Left.is(tok::identifier) &&
Left.TokenText[0] ==
'\\')
5201 if ((
Left.is(TT_VerilogTableItem) &&
5202 !
Right.isOneOf(tok::r_paren, tok::semi)) ||
5203 (
Right.is(TT_VerilogTableItem) &&
Left.isNot(tok::l_paren))) {
5204 const FormatToken *Next =
Right.getNextNonComment();
5205 return !(Next && Next->is(tok::r_paren));
5208 if (
Left.isNot(TT_BinaryOperator) &&
5209 Left.isOneOf(Keywords.kw_verilogHash, Keywords.kw_verilogHashHash)) {
5213 if (
Right.isNot(tok::semi) &&
5214 (
Left.endsSequence(tok::numeric_constant, Keywords.kw_verilogHash) ||
5215 Left.endsSequence(tok::numeric_constant,
5216 Keywords.kw_verilogHashHash) ||
5217 (
Left.is(tok::r_paren) &&
Left.MatchingParen &&
5218 Left.MatchingParen->endsSequence(tok::l_paren, tok::at)))) {
5223 if (
Left.is(Keywords.kw_apostrophe) ||
5224 (
Left.is(TT_VerilogNumberBase) &&
Right.is(tok::numeric_constant))) {
5228 if (
Left.is(tok::arrow) ||
Right.is(tok::arrow))
5233 if (
Left.is(tok::at) &&
Right.isOneOf(tok::l_paren, tok::star, tok::at))
5236 if (
Right.is(tok::l_square) &&
5237 Left.isOneOf(TT_VerilogDimensionedTypeName, Keywords.kw_function)) {
5241 if (
Right.isOneOf(tok::period, Keywords.kw_apostrophe) &&
5242 Keywords.isVerilogIdentifier(Left) &&
Left.getPreviousNonComment() &&
5243 Left.getPreviousNonComment()->is(Keywords.kw_tagged)) {
5249 if ((
Right.is(Keywords.kw_apostrophe) ||
5251 !(
Left.isOneOf(Keywords.kw_assign, Keywords.kw_unique) ||
5252 Keywords.isVerilogWordOperator(Left)) &&
5253 (
Left.isOneOf(tok::r_square, tok::r_paren, tok::r_brace,
5254 tok::numeric_constant) ||
5255 Keywords.isWordLike(Left))) {
5259 if ((
Right.is(tok::star) &&
Left.is(tok::coloncolon)) ||
5260 (
Left.is(tok::star) &&
Right.is(tok::semi))) {
5264 if (
Left.endsSequence(tok::star, tok::l_paren) &&
Right.is(tok::identifier))
5267 if (
Right.is(tok::l_paren) &&
Right.is(TT_VerilogStrength))
5270 if ((
Left.is(tok::l_brace) &&
5271 Right.isOneOf(tok::lessless, tok::greatergreater)) ||
5272 (
Left.endsSequence(tok::lessless, tok::l_brace) ||
5273 Left.endsSequence(tok::greatergreater, tok::l_brace))) {
5276 }
else if (Style.isTableGen()) {
5278 if (
Left.is(tok::l_square) &&
Right.is(tok::l_brace))
5280 if (
Left.is(tok::r_brace) &&
Right.is(tok::r_square))
5283 if (
Right.isOneOf(TT_TableGenDAGArgListColon,
5284 TT_TableGenDAGArgListColonToAlign) ||
5285 Left.isOneOf(TT_TableGenDAGArgListColon,
5286 TT_TableGenDAGArgListColonToAlign)) {
5289 if (
Right.is(TT_TableGenCondOperatorColon))
5291 if (
Left.isOneOf(TT_TableGenDAGArgOperatorID,
5292 TT_TableGenDAGArgOperatorToBreak) &&
5293 Right.isNot(TT_TableGenDAGArgCloser)) {
5297 if (
Right.isOneOf(tok::l_paren, tok::less) &&
5298 Left.isOneOf(TT_TableGenBangOperator, TT_TableGenCondOperator)) {
5303 if (
Left.is(TT_TableGenTrailingPasteOperator) &&
5304 Right.isOneOf(tok::l_brace, tok::colon)) {
5308 if (
Left.is(tok::hash) ||
Right.is(tok::hash))
5311 if (Keywords.isTableGenDefinition(Left))
5315 if (
Left.is(TT_ImplicitStringLiteral))
5316 return Right.hasWhitespaceBefore();
5318 if (
Left.is(TT_ObjCMethodSpecifier))
5320 if (
Left.is(tok::r_paren) &&
Left.isNot(TT_AttributeRParen) &&
5321 canBeObjCSelectorComponent(Right)) {
5329 (
Right.is(tok::equal) ||
Left.is(tok::equal))) {
5333 if (
Right.isOneOf(TT_TrailingReturnArrow, TT_LambdaArrow) ||
5334 Left.isOneOf(TT_TrailingReturnArrow, TT_LambdaArrow)) {
5337 if (
Left.is(tok::comma) &&
Right.isNot(TT_OverloadedOperatorLParen) &&
5340 (
Left.Children.empty() || !
Left.MacroParent)) {
5343 if (
Right.is(tok::comma))
5345 if (
Right.is(TT_ObjCBlockLParen))
5347 if (
Right.is(TT_CtorInitializerColon))
5348 return Style.SpaceBeforeCtorInitializerColon;
5349 if (
Right.is(TT_InheritanceColon) && !Style.SpaceBeforeInheritanceColon)
5351 if (
Right.is(TT_RangeBasedForLoopColon) &&
5352 !Style.SpaceBeforeRangeBasedForLoopColon) {
5355 if (
Left.is(TT_BitFieldColon)) {
5359 if (
Right.is(tok::colon)) {
5360 if (
Right.is(TT_CaseLabelColon))
5361 return Style.SpaceBeforeCaseColon;
5362 if (
Right.is(TT_GotoLabelColon))
5365 if (!
Right.getNextNonComment())
5367 if (
Right.is(TT_ObjCMethodExpr))
5369 if (
Left.is(tok::question))
5371 if (
Right.is(TT_InlineASMColon) &&
Left.is(tok::coloncolon))
5373 if (
Right.is(TT_DictLiteral))
5374 return Style.SpacesInContainerLiterals;
5375 if (
Right.is(TT_AttributeColon))
5377 if (
Right.is(TT_CSharpNamedArgumentColon))
5379 if (
Right.is(TT_GenericSelectionColon))
5381 if (
Right.is(TT_BitFieldColon)) {
5388 if ((
Left.isOneOf(tok::minus, tok::minusminus) &&
5389 Right.isOneOf(tok::minus, tok::minusminus)) ||
5390 (
Left.isOneOf(tok::plus, tok::plusplus) &&
5391 Right.isOneOf(tok::plus, tok::plusplus))) {
5394 if (
Left.is(TT_UnaryOperator)) {
5397 if (
Left.is(tok::amp) &&
Right.is(tok::r_square))
5398 return Style.SpacesInSquareBrackets;
5399 return Style.SpaceAfterLogicalNot &&
Left.is(tok::exclaim);
5404 if (
Left.is(TT_CastRParen)) {
5405 return Style.SpaceAfterCStyleCast ||
5406 Right.isOneOf(TT_BinaryOperator, TT_SelectorName);
5409 auto ShouldAddSpacesInAngles = [
this, &
Right]() {
5413 return Right.hasWhitespaceBefore();
5417 if (
Left.is(tok::greater) &&
Right.is(tok::greater)) {
5420 return !Style.Cpp11BracedListStyle;
5422 return Right.is(TT_TemplateCloser) &&
Left.is(TT_TemplateCloser) &&
5424 ShouldAddSpacesInAngles());
5426 if (
Right.isOneOf(tok::arrow, tok::arrowstar, tok::periodstar) ||
5427 Left.isOneOf(tok::arrow, tok::period, tok::arrowstar, tok::periodstar) ||
5428 (
Right.is(tok::period) &&
Right.isNot(TT_DesignatedInitializerPeriod))) {
5431 if (!Style.SpaceBeforeAssignmentOperators &&
Left.isNot(TT_TemplateCloser) &&
5436 (
Left.is(tok::identifier) ||
Left.is(tok::kw_this))) {
5439 if (
Right.is(tok::coloncolon) &&
Left.is(tok::identifier)) {
5443 return Right.hasWhitespaceBefore();
5445 if (
Right.is(tok::coloncolon) &&
5446 !
Left.isOneOf(tok::l_brace, tok::comment, tok::l_paren)) {
5448 return (
Left.is(TT_TemplateOpener) &&
5450 ShouldAddSpacesInAngles())) ||
5451 !(
Left.isOneOf(tok::l_paren, tok::r_paren, tok::l_square,
5452 tok::kw___super, TT_TemplateOpener,
5453 TT_TemplateCloser)) ||
5454 (
Left.is(tok::l_paren) && Style.SpacesInParensOptions.Other);
5456 if ((
Left.is(TT_TemplateOpener)) != (
Right.is(TT_TemplateCloser)))
5457 return ShouldAddSpacesInAngles();
5458 if (
Left.is(tok::r_paren) &&
Right.is(TT_PointerOrReference) &&
5459 Right.isOneOf(tok::amp, tok::ampamp)) {
5463 if (
Right.is(TT_StructuredBindingLSquare)) {
5464 return !
Left.isOneOf(tok::amp, tok::ampamp) ||
5468 if (
Right.Next &&
Right.Next->is(TT_StructuredBindingLSquare) &&
5469 Right.isOneOf(tok::amp, tok::ampamp)) {
5472 if ((
Right.is(TT_BinaryOperator) &&
Left.isNot(tok::l_paren)) ||
5473 (
Left.isOneOf(TT_BinaryOperator, TT_ConditionalExpr) &&
5474 Right.isNot(tok::r_paren))) {
5477 if (
Right.is(TT_TemplateOpener) &&
Left.is(tok::r_paren) &&
5478 Left.MatchingParen &&
5479 Left.MatchingParen->is(TT_OverloadedOperatorLParen)) {
5482 if (
Right.is(tok::less) &&
Left.isNot(tok::l_paren) &&
5486 if (
Right.is(TT_TrailingUnaryOperator))
5488 if (
Left.is(TT_RegexLiteral))
5490 return spaceRequiredBetween(
Line, Left, Right);
5496 !Tok.
isOneOf(TT_ObjCBlockLBrace, TT_LambdaLBrace, TT_DictLiteral);
5513 !Tok.
isOneOf(TT_ObjCBlockLBrace, TT_DictLiteral);
5516bool TokenAnnotator::mustBreakBefore(
const AnnotatedLine &
Line,
5517 const FormatToken &Right)
const {
5518 const FormatToken &Left = *Right.Previous;
5519 if (Right.NewlinesBefore > 1 && Style.MaxEmptyLinesToKeep > 0 &&
5520 (!Style.RemoveEmptyLinesInUnwrappedLines || &Right ==
Line.First)) {
5524 if (Style.BreakFunctionDefinitionParameters &&
Line.MightBeFunctionDecl &&
5525 Line.mightBeFunctionDefinition() && Left.MightBeFunctionDeclParen &&
5526 Left.ParameterCount > 0) {
5533 Line.MightBeFunctionDecl && !
Left.opensScope() &&
5538 const auto *BeforeLeft =
Left.Previous;
5539 const auto *AfterRight =
Right.Next;
5541 if (Style.isCSharp()) {
5542 if (
Left.is(TT_FatArrow) &&
Right.is(tok::l_brace) &&
5543 Style.BraceWrapping.AfterFunction) {
5546 if (
Right.is(TT_CSharpNamedArgumentColon) ||
5547 Left.is(TT_CSharpNamedArgumentColon)) {
5550 if (
Right.is(TT_CSharpGenericTypeConstraint))
5552 if (AfterRight && AfterRight->is(TT_FatArrow) &&
5553 (
Right.is(tok::numeric_constant) ||
5554 (
Right.is(tok::identifier) &&
Right.TokenText ==
"_"))) {
5559 if (
Left.is(TT_AttributeSquare) &&
Left.is(tok::r_square) &&
5560 (
Right.isAccessSpecifier(
false) ||
5561 Right.is(Keywords.kw_internal))) {
5565 if (
Left.is(TT_AttributeSquare) &&
Right.is(TT_AttributeSquare) &&
5566 Left.is(tok::r_square) &&
Right.is(tok::l_square)) {
5569 }
else if (Style.isJavaScript()) {
5571 if (
Right.is(tok::string_literal) &&
Left.is(tok::plus) && BeforeLeft &&
5572 BeforeLeft->is(tok::string_literal)) {
5575 if (
Left.is(TT_DictLiteral) &&
Left.is(tok::l_brace) &&
Line.Level == 0 &&
5576 BeforeLeft && BeforeLeft->is(tok::equal) &&
5577 Line.First->isOneOf(tok::identifier, Keywords.kw_import, tok::kw_export,
5581 !
Line.First->isOneOf(Keywords.kw_var, Keywords.kw_let)) {
5586 if (
Left.is(tok::l_brace) &&
Line.Level == 0 &&
5587 (
Line.startsWith(tok::kw_enum) ||
5588 Line.startsWith(tok::kw_const, tok::kw_enum) ||
5589 Line.startsWith(tok::kw_export, tok::kw_enum) ||
5590 Line.startsWith(tok::kw_export, tok::kw_const, tok::kw_enum))) {
5595 if (
Right.is(tok::r_brace) &&
Left.is(tok::l_brace) && BeforeLeft &&
5596 BeforeLeft->is(TT_FatArrow)) {
5598 switch (Style.AllowShortLambdasOnASingleLine) {
5604 return !
Left.Children.empty();
5608 return (
Left.NestingLevel == 0 &&
Line.Level == 0) &&
5609 !
Left.Children.empty();
5611 llvm_unreachable(
"Unknown FormatStyle::ShortLambdaStyle enum");
5614 if (
Right.is(tok::r_brace) &&
Left.is(tok::l_brace) &&
5615 !
Left.Children.empty()) {
5619 (
Left.NestingLevel == 0 &&
Line.Level == 0 &&
5620 Style.AllowShortFunctionsOnASingleLine &
5624 if (
Right.is(tok::plus) &&
Left.is(tok::string_literal) && AfterRight &&
5625 AfterRight->is(tok::string_literal)) {
5628 }
else if (Style.isVerilog()) {
5630 if (
Left.is(TT_VerilogAssignComma))
5633 if (
Left.is(TT_VerilogTypeComma))
5637 if (Style.VerilogBreakBetweenInstancePorts &&
5638 (
Left.is(TT_VerilogInstancePortComma) ||
5639 (
Left.is(tok::r_paren) && Keywords.isVerilogIdentifier(Right) &&
5640 Left.MatchingParen &&
5641 Left.MatchingParen->is(TT_VerilogInstancePortLParen)))) {
5646 if (!Keywords.isVerilogBegin(Right) && Keywords.isVerilogEndOfLabel(Left))
5648 }
else if (Style.BreakAdjacentStringLiterals &&
5649 (IsCpp || Style.isProto() ||
5651 if (
Left.isStringLiteral() &&
Right.isStringLiteral())
5656 if (Style.isJson()) {
5660 if (
Left.is(TT_DictLiteral) &&
Left.is(tok::l_brace))
5663 if ((
Left.is(TT_ArrayInitializerLSquare) &&
Left.is(tok::l_square) &&
5664 Right.isNot(tok::r_square)) ||
5665 Left.is(tok::comma)) {
5666 if (
Right.is(tok::l_brace))
5670 for (
const auto *Tok = &Right; Tok; Tok = Tok->Next) {
5671 if (Tok->isOneOf(tok::l_brace, tok::l_square))
5673 if (Tok->isOneOf(tok::r_brace, tok::r_square))
5676 return Style.BreakArrays;
5678 }
else if (Style.isTableGen()) {
5682 if (
Left.is(TT_TableGenCondOperatorComma))
5684 if (
Left.is(TT_TableGenDAGArgOperatorToBreak) &&
5685 Right.isNot(TT_TableGenDAGArgCloser)) {
5688 if (
Left.is(TT_TableGenDAGArgListCommaToBreak))
5690 if (
Right.is(TT_TableGenDAGArgCloser) &&
Right.MatchingParen &&
5691 Right.MatchingParen->is(TT_TableGenDAGArgOpenerToBreak) &&
5698 if (
Line.startsWith(tok::kw_asm) &&
Right.is(TT_InlineASMColon) &&
5708 const FormatToken *BeforeClosingBrace =
nullptr;
5709 if ((
Left.isOneOf(tok::l_brace, TT_ArrayInitializerLSquare) ||
5710 (Style.isJavaScript() &&
Left.is(tok::l_paren))) &&
5712 BeforeClosingBrace =
Left.MatchingParen->Previous;
5713 }
else if (
Right.MatchingParen &&
5714 (
Right.MatchingParen->isOneOf(tok::l_brace,
5715 TT_ArrayInitializerLSquare) ||
5716 (Style.isJavaScript() &&
5717 Right.MatchingParen->is(tok::l_paren)))) {
5718 BeforeClosingBrace = &
Left;
5720 if (BeforeClosingBrace && (BeforeClosingBrace->is(tok::comma) ||
5721 BeforeClosingBrace->isTrailingComment())) {
5726 if (
Right.is(tok::comment)) {
5728 (
Right.NewlinesBefore > 0 &&
Right.HasUnescapedNewline);
5730 if (
Left.isTrailingComment())
5732 if (
Left.IsUnterminatedLiteral)
5735 if (BeforeLeft && BeforeLeft->is(tok::lessless) &&
5736 Left.is(tok::string_literal) &&
Right.is(tok::lessless) && AfterRight &&
5737 AfterRight->is(tok::string_literal)) {
5738 return Right.NewlinesBefore > 0;
5741 if (
Right.is(TT_RequiresClause)) {
5742 switch (Style.RequiresClausePosition) {
5752 if (
Left.ClosesTemplateDeclaration &&
Left.MatchingParen &&
5753 Left.MatchingParen->NestingLevel == 0) {
5757 if (
Right.is(tok::kw_concept))
5761 Right.NewlinesBefore > 0);
5763 if (
Left.ClosesRequiresClause) {
5764 switch (Style.RequiresClausePosition) {
5767 return Right.isNot(tok::semi);
5769 return !
Right.isOneOf(tok::semi, tok::l_brace);
5776 (
Left.is(TT_CtorInitializerComma) ||
5777 Right.is(TT_CtorInitializerColon))) {
5782 Left.isOneOf(TT_CtorInitializerColon, TT_CtorInitializerComma)) {
5788 Right.isOneOf(TT_CtorInitializerComma, TT_CtorInitializerColon)) {
5794 Right.is(TT_CtorInitializerColon)) {
5799 Left.is(TT_CtorInitializerColon)) {
5805 Right.is(TT_InheritanceComma)) {
5809 Left.is(TT_InheritanceComma)) {
5812 if (
Right.is(tok::string_literal) &&
Right.TokenText.starts_with(
"R\"")) {
5816 return Right.IsMultiline &&
Right.NewlinesBefore > 0;
5818 if ((
Left.is(tok::l_brace) ||
5819 (
Left.is(tok::less) && BeforeLeft && BeforeLeft->is(tok::equal))) &&
5825 if (
Right.is(TT_InlineASMBrace))
5826 return Right.HasUnescapedNewline;
5829 auto *FirstNonComment =
Line.getFirstNonComment();
5831 FirstNonComment && (FirstNonComment->is(Keywords.kw_internal) ||
5832 FirstNonComment->isAccessSpecifierKeyword());
5834 if (Style.BraceWrapping.AfterEnum) {
5835 if (
Line.startsWith(tok::kw_enum) ||
5836 Line.startsWith(tok::kw_typedef, tok::kw_enum)) {
5841 FirstNonComment->Next->is(tok::kw_enum)) {
5847 if (Style.BraceWrapping.AfterClass &&
5849 FirstNonComment->Next->is(Keywords.kw_interface)) ||
5850 Line.startsWith(Keywords.kw_interface))) {
5855 if (
Right.isNot(TT_FunctionLBrace)) {
5856 return (
Line.startsWith(tok::kw_class) &&
5857 Style.BraceWrapping.AfterClass) ||
5858 (
Line.startsWith(tok::kw_struct) &&
5859 Style.BraceWrapping.AfterStruct);
5863 if (
Left.is(TT_ObjCBlockLBrace) &&
5869 if (
Left.isOneOf(TT_AttributeRParen, TT_AttributeMacro) &&
5870 Right.is(TT_ObjCDecl)) {
5874 if (
Left.is(TT_LambdaLBrace)) {
5882 (!
Left.Children.empty() &&
5888 if (Style.BraceWrapping.BeforeLambdaBody &&
Right.is(TT_LambdaLBrace) &&
5889 (
Left.isPointerOrReference() ||
Left.is(TT_TemplateCloser))) {
5895 Left.is(TT_LeadingJavaAnnotation) &&
5896 Right.isNot(TT_LeadingJavaAnnotation) &&
Right.isNot(tok::l_paren) &&
5897 (
Line.Last->is(tok::l_brace) || Style.BreakAfterJavaFieldAnnotations)) {
5901 if (
Right.is(TT_ProtoExtensionLSquare))
5931 if (Style.isProto() &&
Right.is(TT_SelectorName) &&
5932 Right.isNot(tok::r_square) && AfterRight) {
5935 if (
Left.is(tok::at))
5941 const auto *LBrace = AfterRight;
5942 if (LBrace && LBrace->is(tok::colon)) {
5943 LBrace = LBrace->Next;
5944 if (LBrace && LBrace->is(tok::at)) {
5945 LBrace = LBrace->Next;
5947 LBrace = LBrace->Next;
5959 ((LBrace->is(tok::l_brace) &&
5960 (LBrace->is(TT_DictLiteral) ||
5961 (LBrace->Next && LBrace->Next->is(tok::r_brace)))) ||
5962 LBrace->is(TT_ArrayInitializerLSquare) || LBrace->is(tok::less))) {
5969 if (
Left.ParameterCount == 0)
5984 if (
Left.isOneOf(tok::r_brace, tok::greater, tok::r_square))
5991bool TokenAnnotator::canBreakBefore(
const AnnotatedLine &
Line,
5992 const FormatToken &Right)
const {
5993 const FormatToken &
Left = *
Right.Previous;
5995 if (Style.isCSharp()) {
5996 if (
Left.isOneOf(TT_CSharpNamedArgumentColon, TT_AttributeColon) ||
5997 Right.isOneOf(TT_CSharpNamedArgumentColon, TT_AttributeColon)) {
6001 if (
Line.First->is(TT_CSharpGenericTypeConstraint))
6002 return Left.is(TT_CSharpGenericTypeConstraintComma);
6004 if (
Right.is(TT_CSharpNullable))
6007 if (
Left.isOneOf(Keywords.kw_throws, Keywords.kw_extends,
6008 Keywords.kw_implements)) {
6011 if (
Right.isOneOf(Keywords.kw_throws, Keywords.kw_extends,
6012 Keywords.kw_implements)) {
6015 }
else if (Style.isJavaScript()) {
6016 const FormatToken *NonComment =
Right.getPreviousNonComment();
6018 (NonComment->isAccessSpecifierKeyword() ||
6019 NonComment->isOneOf(
6020 tok::kw_return, Keywords.kw_yield, tok::kw_continue, tok::kw_break,
6021 tok::kw_throw, Keywords.kw_interface, Keywords.kw_type,
6022 tok::kw_static, Keywords.kw_readonly, Keywords.kw_override,
6023 Keywords.kw_abstract, Keywords.kw_get, Keywords.kw_set,
6024 Keywords.kw_async, Keywords.kw_await))) {
6027 if (
Right.NestingLevel == 0 &&
6028 (
Left.Tok.getIdentifierInfo() ||
6029 Left.isOneOf(tok::r_square, tok::r_paren)) &&
6030 Right.isOneOf(tok::l_square, tok::l_paren)) {
6033 if (NonComment && NonComment->is(tok::identifier) &&
6034 NonComment->TokenText ==
"asserts") {
6037 if (
Left.is(TT_FatArrow) &&
Right.is(tok::l_brace))
6039 if (
Left.is(TT_JsTypeColon))
6042 if (
Left.is(tok::exclaim) &&
Right.is(tok::colon))
6047 if (
Right.is(Keywords.kw_is)) {
6048 const FormatToken *Next =
Right.getNextNonComment();
6056 if (!Next || Next->isNot(tok::colon))
6059 if (
Left.is(Keywords.kw_in))
6061 if (
Right.is(Keywords.kw_in))
6063 if (
Right.is(Keywords.kw_as))
6065 if (
Right.isOneOf(Keywords.kw_extends, Keywords.kw_infer)) {
6071 if (
Left.is(Keywords.kw_as))
6073 if (
Left.is(TT_NonNullAssertion))
6075 if (
Left.is(Keywords.kw_declare) &&
6076 Right.isOneOf(Keywords.kw_module, tok::kw_namespace,
6077 Keywords.kw_function, tok::kw_class, tok::kw_enum,
6078 Keywords.kw_interface, Keywords.kw_type, Keywords.kw_var,
6079 Keywords.kw_let, tok::kw_const)) {
6084 if (
Left.isOneOf(Keywords.kw_module, tok::kw_namespace) &&
6085 Right.isOneOf(tok::identifier, tok::string_literal)) {
6088 if (
Right.is(TT_TemplateString) &&
Right.closesScope())
6092 if (
Left.is(tok::identifier) &&
Right.is(TT_TemplateString))
6094 if (
Left.is(TT_TemplateString) &&
Left.opensScope())
6096 }
else if (Style.isTableGen()) {
6098 if (Keywords.isTableGenDefinition(Left))
6101 if (
Right.is(tok::l_paren)) {
6102 return !
Left.isOneOf(TT_TableGenBangOperator, TT_TableGenCondOperator,
6106 if (
Left.is(TT_TableGenValueSuffix))
6109 if (
Left.is(tok::hash) ||
Right.is(tok::hash))
6111 if (
Left.isOneOf(TT_TableGenBangOperator, TT_TableGenCondOperator))
6118 if (
Right.is(tok::r_brace)) {
6120 (
Right.isBlockIndentedInitRBrace(Style)));
6124 if (
Right.is(tok::r_paren)) {
6126 !
Right.MatchingParen) {
6129 auto Next =
Right.Next;
6130 if (Next && Next->is(tok::r_paren))
6132 if (Next && Next->is(tok::l_paren))
6134 const FormatToken *
Previous =
Right.MatchingParen->Previous;
6138 if (
Left.isOneOf(tok::r_paren, TT_TrailingAnnotation) &&
6139 Right.is(TT_TrailingAnnotation) &&
6144 if (
Left.is(tok::at))
6146 if (
Left.Tok.getObjCKeywordID() == tok::objc_interface)
6148 if (
Left.isOneOf(TT_JavaAnnotation, TT_LeadingJavaAnnotation))
6149 return Right.isNot(tok::l_paren);
6150 if (
Right.is(TT_PointerOrReference)) {
6151 return Line.IsMultiVariableDeclStmt ||
6152 (getTokenPointerOrReferenceAlignment(Right) ==
6154 (!
Right.Next ||
Right.Next->isNot(TT_FunctionDeclarationName)));
6156 if (
Right.isOneOf(TT_StartOfName, TT_FunctionDeclarationName) ||
6157 Right.is(tok::kw_operator)) {
6160 if (
Left.is(TT_PointerOrReference))
6162 if (
Right.isTrailingComment()) {
6169 (
Left.is(TT_CtorInitializerColon) &&
Right.NewlinesBefore > 0 &&
6172 if (
Left.is(tok::question) &&
Right.is(tok::colon))
6174 if (
Right.is(TT_ConditionalExpr) ||
Right.is(tok::question))
6175 return Style.BreakBeforeTernaryOperators;
6176 if (
Left.is(TT_ConditionalExpr) ||
Left.is(tok::question))
6177 return !Style.BreakBeforeTernaryOperators;
6178 if (
Left.is(TT_InheritanceColon))
6180 if (
Right.is(TT_InheritanceColon))
6182 if (
Right.is(TT_ObjCMethodExpr) &&
Right.isNot(tok::r_square) &&
6183 Left.isNot(TT_SelectorName)) {
6187 if (
Right.is(tok::colon) &&
6188 !
Right.isOneOf(TT_CtorInitializerColon, TT_InlineASMColon)) {
6191 if (
Left.is(tok::colon) &&
Left.isOneOf(TT_DictLiteral, TT_ObjCMethodExpr)) {
6192 if (Style.isProto()) {
6193 if (!Style.AlwaysBreakBeforeMultilineStrings &&
Right.isStringLiteral())
6219 if (((
Right.is(tok::l_brace) ||
Right.is(tok::less)) &&
6220 Right.is(TT_DictLiteral)) ||
6221 Right.is(TT_ArrayInitializerLSquare)) {
6227 if (
Right.is(tok::r_square) &&
Right.MatchingParen &&
6228 Right.MatchingParen->is(TT_ProtoExtensionLSquare)) {
6231 if (
Right.is(TT_SelectorName) || (
Right.is(tok::identifier) &&
Right.Next &&
6232 Right.Next->is(TT_ObjCMethodExpr))) {
6233 return Left.isNot(tok::period);
6237 if (
Right.is(tok::kw_concept))
6239 if (
Right.is(TT_RequiresClause))
6241 if (
Left.ClosesTemplateDeclaration) {
6243 Right.NewlinesBefore > 0;
6245 if (
Left.is(TT_FunctionAnnotationRParen))
6247 if (
Left.ClosesRequiresClause)
6249 if (
Right.isOneOf(TT_RangeBasedForLoopColon, TT_OverloadedOperatorLParen,
6250 TT_OverloadedOperator)) {
6253 if (
Left.is(TT_RangeBasedForLoopColon))
6255 if (
Right.is(TT_RangeBasedForLoopColon))
6257 if (
Left.is(TT_TemplateCloser) &&
Right.is(TT_TemplateOpener))
6259 if ((
Left.is(tok::greater) &&
Right.is(tok::greater)) ||
6260 (
Left.is(tok::less) &&
Right.is(tok::less))) {
6263 if (
Right.is(TT_BinaryOperator) &&
6269 if (
Left.isOneOf(TT_TemplateCloser, TT_UnaryOperator) ||
6270 Left.is(tok::kw_operator)) {
6273 if (
Left.is(tok::equal) && !
Right.isOneOf(tok::kw_default, tok::kw_delete) &&
6277 if (
Left.is(tok::equal) &&
Right.is(tok::l_brace) &&
6278 !Style.Cpp11BracedListStyle) {
6281 if (
Left.is(TT_AttributeLParen) ||
6282 (
Left.is(tok::l_paren) &&
Left.is(TT_TypeDeclarationParen))) {
6285 if (
Left.is(tok::l_paren) &&
Left.Previous &&
6286 (
Left.Previous->isOneOf(TT_BinaryOperator, TT_CastRParen))) {
6289 if (
Right.is(TT_ImplicitStringLiteral))
6292 if (
Right.is(TT_TemplateCloser))
6294 if (
Right.is(tok::r_square) &&
Right.MatchingParen &&
6295 Right.MatchingParen->is(TT_LambdaLSquare)) {
6301 if (
Left.is(TT_TrailingAnnotation)) {
6302 return !
Right.isOneOf(tok::l_brace, tok::semi, tok::equal, tok::l_paren,
6303 tok::less, tok::coloncolon);
6306 if (
Right.isAttribute())
6309 if (
Right.is(tok::l_square) &&
Right.is(TT_AttributeSquare))
6310 return Left.isNot(TT_AttributeSquare);
6312 if (
Left.is(tok::identifier) &&
Right.is(tok::string_literal))
6315 if (
Right.is(tok::identifier) &&
Right.Next &&
Right.Next->is(TT_DictLiteral))
6318 if (
Left.is(TT_CtorInitializerColon)) {
6320 (!
Right.isTrailingComment() ||
Right.NewlinesBefore > 0);
6322 if (
Right.is(TT_CtorInitializerColon))
6324 if (
Left.is(TT_CtorInitializerComma) &&
6328 if (
Right.is(TT_CtorInitializerComma) &&
6332 if (
Left.is(TT_InheritanceComma) &&
6336 if (
Right.is(TT_InheritanceComma) &&
6340 if (
Left.is(TT_ArrayInitializerLSquare))
6342 if (
Right.is(tok::kw_typename) &&
Left.isNot(tok::kw_const))
6344 if ((
Left.isBinaryOperator() ||
Left.is(TT_BinaryOperator)) &&
6345 !
Left.isOneOf(tok::arrowstar, tok::lessless) &&
6351 if ((
Left.is(TT_AttributeSquare) &&
Right.is(tok::l_square)) ||
6352 (
Left.is(tok::r_square) &&
Right.is(TT_AttributeSquare))) {
6356 auto ShortLambdaOption = Style.AllowShortLambdasOnASingleLine;
6357 if (Style.BraceWrapping.BeforeLambdaBody &&
Right.is(TT_LambdaLBrace)) {
6364 if (
Right.is(tok::kw_noexcept) &&
Right.is(TT_TrailingAnnotation)) {
6365 switch (Style.AllowBreakBeforeNoexceptSpecifier) {
6371 return Right.Next &&
Right.Next->is(tok::l_paren);
6375 return Left.isOneOf(tok::comma, tok::coloncolon, tok::semi, tok::l_brace,
6376 tok::kw_class, tok::kw_struct, tok::comment) ||
6377 Right.isMemberAccess() ||
6378 Right.isOneOf(TT_TrailingReturnArrow, TT_LambdaArrow, tok::lessless,
6379 tok::colon, tok::l_square, tok::at) ||
6380 (
Left.is(tok::r_paren) &&
6381 Right.isOneOf(tok::identifier, tok::kw_const)) ||
6382 (
Left.is(tok::l_paren) &&
Right.isNot(tok::r_paren)) ||
6383 (
Left.is(TT_TemplateOpener) &&
Right.isNot(TT_TemplateCloser));
6386void TokenAnnotator::printDebugInfo(
const AnnotatedLine &
Line)
const {
6387 llvm::errs() <<
"AnnotatedTokens(L=" <<
Line.Level <<
", P=" <<
Line.PPLevel
6388 <<
", T=" <<
Line.Type <<
", C=" <<
Line.IsContinuation
6390 const FormatToken *Tok =
Line.First;
6392 llvm::errs() <<
" M=" << Tok->MustBreakBefore
6393 <<
" C=" << Tok->CanBreakBefore
6395 <<
" S=" << Tok->SpacesRequiredBefore
6396 <<
" F=" << Tok->Finalized <<
" B=" << Tok->BlockParameterCount
6397 <<
" BK=" << Tok->getBlockKind() <<
" P=" << Tok->SplitPenalty
6398 <<
" Name=" << Tok->Tok.getName() <<
" L=" << Tok->TotalLength
6399 <<
" PPK=" << Tok->getPackingKind() <<
" FakeLParens=";
6401 llvm::errs() << LParen <<
"/";
6402 llvm::errs() <<
" FakeRParens=" << Tok->FakeRParens;
6403 llvm::errs() <<
" II=" << Tok->Tok.getIdentifierInfo();
6404 llvm::errs() <<
" Text='" << Tok->TokenText <<
"'\n";
6406 assert(Tok ==
Line.Last);
6409 llvm::errs() <<
"----\n";
6413TokenAnnotator::getTokenReferenceAlignment(
const FormatToken &
Reference)
const {
6414 assert(
Reference.isOneOf(tok::amp, tok::ampamp));
6415 switch (Style.ReferenceAlignment) {
6417 return Style.PointerAlignment;
6426 return Style.PointerAlignment;
6430TokenAnnotator::getTokenPointerOrReferenceAlignment(
6431 const FormatToken &PointerOrReference)
const {
6432 if (PointerOrReference.isOneOf(tok::amp, tok::ampamp)) {
6433 switch (Style.ReferenceAlignment) {
6435 return Style.PointerAlignment;
6444 assert(PointerOrReference.is(tok::star));
6445 return Style.PointerAlignment;
Defines the SourceManager interface.
bool ColonIsObjCMethodExpr
FormatToken * FirstStartOfName
bool InCpp11AttributeSpecifier
unsigned LongestObjCSelectorName
bool VerilogAssignmentFound
bool InCSharpAttributeSpecifier
tok::TokenKind ContextKind
FormatToken * FirstObjCSelectorName
bool VerilogMayBeConcatenation
enum clang::format::@1329::AnnotatingParser::Context::@350 ContextType
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...