19#include "llvm/ADT/SmallPtrSet.h"
20#include "llvm/Support/Debug.h"
22#define DEBUG_TYPE "format-token-annotator"
43static bool startsWithInitStatement(
const AnnotatedLine &
Line) {
44 return Line.startsWith(tok::kw_for) ||
Line.startsWith(tok::kw_if) ||
45 Line.startsWith(tok::kw_switch);
59static bool canBeObjCSelectorComponent(
const FormatToken &Tok) {
60 return Tok.Tok.getIdentifierInfo();
66static bool isLambdaParameterList(
const FormatToken *Left) {
68 if (
Left->Previous &&
Left->Previous->is(tok::greater) &&
69 Left->Previous->MatchingParen &&
70 Left->Previous->MatchingParen->is(TT_TemplateOpener)) {
71 Left =
Left->Previous->MatchingParen;
75 return Left->Previous &&
Left->Previous->is(tok::r_square) &&
76 Left->Previous->MatchingParen &&
77 Left->Previous->MatchingParen->is(TT_LambdaLSquare);
82static bool isKeywordWithCondition(
const FormatToken &Tok) {
83 return Tok.isOneOf(tok::kw_if, tok::kw_for, tok::kw_while, tok::kw_switch,
84 tok::kw_constexpr, tok::kw_catch);
88static bool isCppAttribute(
bool IsCpp,
const FormatToken &Tok) {
89 if (!IsCpp || !Tok.startsSequence(tok::l_square, tok::l_square))
92 if (Tok.Previous && Tok.Previous->is(tok::at))
94 const FormatToken *AttrTok = Tok.Next->Next;
99 if (AttrTok->startsSequence(tok::kw_using, tok::identifier, tok::colon))
101 if (AttrTok->isNot(tok::identifier))
103 while (AttrTok && !AttrTok->startsSequence(tok::r_square, tok::r_square)) {
107 if (AttrTok->is(tok::colon) ||
108 AttrTok->startsSequence(tok::identifier, tok::identifier) ||
109 AttrTok->startsSequence(tok::r_paren, tok::identifier)) {
112 if (AttrTok->is(tok::ellipsis))
114 AttrTok = AttrTok->Next;
116 return AttrTok && AttrTok->startsSequence(tok::r_square, tok::r_square);
124class AnnotatingParser {
126 AnnotatingParser(
const FormatStyle &Style, AnnotatedLine &Line,
127 const AdditionalKeywords &Keywords,
128 SmallVector<ScopeType> &Scopes)
129 : Style(Style), Line(Line), CurrentToken(Line.
First), AutoFound(
false),
131 Keywords(Keywords), Scopes(Scopes), TemplateDeclarationDepth(0) {
132 assert(IsCpp == LangOpts.CXXOperatorNames);
133 Contexts.push_back(Context(tok::unknown, 1,
false));
134 resetTokenMetadata();
138 ScopeType getScopeType(
const FormatToken &Token)
const {
139 switch (Token.getType()) {
141 case TT_StructLBrace:
144 case TT_CompoundRequirementLBrace:
155 auto *
Left = CurrentToken->Previous;
159 if (NonTemplateLess.count(Left) > 0)
162 const auto *BeforeLess =
Left->Previous;
165 if (BeforeLess->Tok.isLiteral())
167 if (BeforeLess->is(tok::r_brace))
169 if (BeforeLess->is(tok::r_paren) && Contexts.size() > 1 &&
170 !(BeforeLess->MatchingParen &&
171 BeforeLess->MatchingParen->is(TT_OverloadedOperatorLParen))) {
174 if (BeforeLess->is(tok::kw_operator) && CurrentToken->is(tok::l_paren))
178 Left->ParentBracket = Contexts.back().ContextKind;
179 ScopedContextCreator ContextCreator(*
this, tok::less, 12);
180 Contexts.back().IsExpression =
false;
184 if (BeforeLess && BeforeLess->isNot(tok::kw_template))
185 Contexts.back().ContextType = Context::TemplateArgument;
188 CurrentToken->is(tok::question)) {
192 for (
bool SeenTernaryOperator =
false, MaybeAngles =
true; CurrentToken;) {
193 const bool InExpr = Contexts[Contexts.size() - 2].IsExpression;
194 if (CurrentToken->is(tok::greater)) {
195 const auto *Next = CurrentToken->Next;
196 if (CurrentToken->isNot(TT_TemplateCloser)) {
203 if (Next && Next->is(tok::greater) &&
204 Left->ParentBracket != tok::less &&
205 CurrentToken->getStartOfNonWhitespace() ==
206 Next->getStartOfNonWhitespace().getLocWithOffset(-1)) {
209 if (InExpr && SeenTernaryOperator &&
210 (!Next || !Next->isOneOf(tok::l_paren, tok::l_brace))) {
216 Left->MatchingParen = CurrentToken;
217 CurrentToken->MatchingParen =
Left;
225 BeforeLess->isOneOf(TT_SelectorName, TT_DictLiteral))) {
226 CurrentToken->setType(TT_DictLiteral);
228 CurrentToken->setType(TT_TemplateCloser);
229 CurrentToken->Tok.setLength(1);
231 if (Next && Next->Tok.isLiteral())
236 if (BeforeLess && BeforeLess->is(TT_TemplateName)) {
240 if (CurrentToken->is(tok::question) &&
245 if (CurrentToken->isOneOf(tok::r_paren, tok::r_square, tok::r_brace))
247 const auto &Prev = *CurrentToken->Previous;
254 if (MaybeAngles && InExpr && !Line.startsWith(tok::kw_template) &&
255 Prev.is(TT_BinaryOperator) &&
256 (Prev.isOneOf(tok::pipepipe, tok::ampamp) ||
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))
1120 Contexts.back().IsTableGenBangOpe =
true;
1121 bool Result = parseParens();
1122 Contexts.back().IsTableGenBangOpe =
false;
1126 if (Tok->is(TT_TableGenCondOperator)) {
1127 if (!CurrentToken || CurrentToken->isNot(tok::l_paren))
1130 return parseParens();
1135 if (Tok->is(tok::identifier)) {
1137 if (CurrentToken && CurrentToken->is(tok::less)) {
1138 CurrentToken->setType(TT_TemplateOpener);
1139 skipToNextNonComment();
1140 return parseAngle();
1148 bool couldBeInStructArrayInitializer()
const {
1149 if (Contexts.size() < 2)
1153 const auto End = std::next(Contexts.rbegin(), 2);
1154 auto Last = Contexts.rbegin();
1157 if (
Last->ContextKind == tok::l_brace)
1159 return Depth == 2 &&
Last->ContextKind != tok::l_brace;
1166 assert(CurrentToken->Previous);
1167 FormatToken &OpeningBrace = *CurrentToken->Previous;
1168 assert(OpeningBrace.is(tok::l_brace));
1169 OpeningBrace.ParentBracket = Contexts.back().ContextKind;
1171 if (Contexts.back().CaretFound)
1172 OpeningBrace.overwriteFixedType(TT_ObjCBlockLBrace);
1173 Contexts.back().CaretFound =
false;
1175 ScopedContextCreator ContextCreator(*
this, tok::l_brace, 1);
1176 Contexts.back().ColonIsDictLiteral =
true;
1178 Contexts.back().IsExpression =
true;
1179 if (Style.isJavaScript() && OpeningBrace.Previous &&
1180 OpeningBrace.Previous->is(TT_JsTypeColon)) {
1181 Contexts.back().IsExpression =
false;
1183 if (Style.isVerilog() &&
1184 (!OpeningBrace.getPreviousNonComment() ||
1185 OpeningBrace.getPreviousNonComment()->isNot(Keywords.kw_apostrophe))) {
1186 Contexts.back().VerilogMayBeConcatenation =
true;
1188 if (Style.isTableGen())
1189 Contexts.back().ColonIsDictLiteral =
false;
1191 unsigned CommaCount = 0;
1192 while (CurrentToken) {
1193 if (CurrentToken->is(tok::r_brace)) {
1194 assert(!Scopes.empty());
1195 assert(Scopes.back() == getScopeType(OpeningBrace));
1197 assert(OpeningBrace.Optional == CurrentToken->Optional);
1198 OpeningBrace.MatchingParen = CurrentToken;
1199 CurrentToken->MatchingParen = &OpeningBrace;
1201 if (OpeningBrace.ParentBracket == tok::l_brace &&
1202 couldBeInStructArrayInitializer() && CommaCount > 0) {
1203 Contexts.back().ContextType = Context::StructArrayInitializer;
1209 if (CurrentToken->isOneOf(tok::r_paren, tok::r_square))
1211 updateParameterCount(&OpeningBrace, CurrentToken);
1212 if (CurrentToken->isOneOf(tok::colon, tok::l_brace, tok::less)) {
1213 FormatToken *
Previous = CurrentToken->getPreviousNonComment();
1214 if (
Previous->is(TT_JsTypeOptionalQuestion))
1216 if ((CurrentToken->is(tok::colon) && !Style.isTableGen() &&
1217 (!Contexts.back().ColonIsDictLiteral || !IsCpp)) ||
1219 OpeningBrace.setType(TT_DictLiteral);
1220 if (
Previous->Tok.getIdentifierInfo() ||
1221 Previous->is(tok::string_literal)) {
1222 Previous->setType(TT_SelectorName);
1225 if (CurrentToken->is(tok::colon) && OpeningBrace.is(TT_Unknown) &&
1226 !Style.isTableGen()) {
1227 OpeningBrace.setType(TT_DictLiteral);
1228 }
else if (Style.isJavaScript()) {
1229 OpeningBrace.overwriteFixedType(TT_DictLiteral);
1232 if (CurrentToken->is(tok::comma)) {
1233 if (Style.isJavaScript())
1234 OpeningBrace.overwriteFixedType(TT_DictLiteral);
1237 if (!consumeToken())
1243 void updateParameterCount(FormatToken *Left, FormatToken *Current) {
1247 if (Current->is(tok::l_brace) && Current->is(
BK_Block))
1248 ++
Left->BlockParameterCount;
1249 if (Current->is(tok::comma)) {
1250 ++
Left->ParameterCount;
1252 Left->Role.reset(
new CommaSeparatedList(Style));
1253 Left->Role->CommaFound(Current);
1254 }
else if (
Left->ParameterCount == 0 && Current->isNot(tok::comment)) {
1255 Left->ParameterCount = 1;
1259 bool parseConditional() {
1260 while (CurrentToken) {
1261 if (CurrentToken->is(tok::colon) && CurrentToken->is(TT_Unknown)) {
1262 CurrentToken->setType(TT_ConditionalExpr);
1266 if (!consumeToken())
1272 bool parseTemplateDeclaration() {
1273 if (!CurrentToken || CurrentToken->isNot(tok::less))
1276 CurrentToken->setType(TT_TemplateOpener);
1279 TemplateDeclarationDepth++;
1280 const bool WellFormed = parseAngle();
1281 TemplateDeclarationDepth--;
1285 if (CurrentToken && TemplateDeclarationDepth == 0)
1286 CurrentToken->Previous->ClosesTemplateDeclaration =
true;
1291 bool consumeToken() {
1293 const auto *Prev = CurrentToken->getPreviousNonComment();
1294 if (Prev && Prev->is(tok::r_square) && Prev->is(TT_AttributeSquare) &&
1295 CurrentToken->isOneOf(tok::kw_if, tok::kw_switch, tok::kw_case,
1296 tok::kw_default, tok::kw_for, tok::kw_while) &&
1298 CurrentToken->MustBreakBefore =
true;
1301 FormatToken *Tok = CurrentToken;
1305 if (Tok->is(TT_VerilogTableItem))
1308 if (Tok->is(TT_TableGenMultiLineString))
1310 switch (
bool IsIf =
false; Tok->Tok.getKind()) {
1313 if (!Tok->Previous && Line.MustBeDeclaration)
1314 Tok->setType(TT_ObjCMethodSpecifier);
1321 if (Tok->isTypeFinalized())
1324 if (Style.isJavaScript()) {
1325 if (Contexts.back().ColonIsForRangeExpr ||
1326 (Contexts.size() == 1 &&
1327 !Line.First->isOneOf(tok::kw_enum, tok::kw_case)) ||
1328 Contexts.back().ContextKind == tok::l_paren ||
1329 Contexts.back().ContextKind == tok::l_square ||
1330 (!Contexts.back().IsExpression &&
1331 Contexts.back().ContextKind == tok::l_brace) ||
1332 (Contexts.size() == 1 &&
1333 Line.MustBeDeclaration)) {
1334 Contexts.back().IsExpression =
false;
1335 Tok->setType(TT_JsTypeColon);
1338 }
else if (Style.isCSharp()) {
1339 if (Contexts.back().InCSharpAttributeSpecifier) {
1340 Tok->setType(TT_AttributeColon);
1343 if (Contexts.back().ContextKind == tok::l_paren) {
1344 Tok->setType(TT_CSharpNamedArgumentColon);
1347 }
else if (Style.isVerilog() && Tok->isNot(TT_BinaryOperator)) {
1350 if (Keywords.isVerilogEnd(*Tok->Previous) ||
1351 Keywords.isVerilogBegin(*Tok->Previous)) {
1352 Tok->setType(TT_VerilogBlockLabelColon);
1353 }
else if (Contexts.back().ContextKind == tok::l_square) {
1354 Tok->setType(TT_BitFieldColon);
1355 }
else if (Contexts.back().ColonIsDictLiteral) {
1356 Tok->setType(TT_DictLiteral);
1357 }
else if (Contexts.size() == 1) {
1361 Tok->setType(TT_CaseLabelColon);
1362 if (Line.Level > 1 || (!Line.InPPDirective && Line.Level > 0))
1367 if (Line.First->isOneOf(Keywords.kw_module, Keywords.kw_import) ||
1368 Line.First->startsSequence(tok::kw_export, Keywords.kw_module) ||
1369 Line.First->startsSequence(tok::kw_export, Keywords.kw_import)) {
1370 Tok->setType(TT_ModulePartitionColon);
1371 }
else if (Line.First->is(tok::kw_asm)) {
1372 Tok->setType(TT_InlineASMColon);
1373 }
else if (Contexts.back().ColonIsDictLiteral || Style.isProto()) {
1374 Tok->setType(TT_DictLiteral);
1376 if (FormatToken *
Previous = Tok->getPreviousNonComment())
1377 Previous->setType(TT_SelectorName);
1379 }
else if (Contexts.back().ColonIsObjCMethodExpr ||
1380 Line.startsWith(TT_ObjCMethodSpecifier)) {
1381 Tok->setType(TT_ObjCMethodExpr);
1382 const FormatToken *BeforePrevious = Tok->Previous->Previous;
1385 bool UnknownIdentifierInMethodDeclaration =
1386 Line.startsWith(TT_ObjCMethodSpecifier) &&
1387 Tok->Previous->is(tok::identifier) && Tok->Previous->is(TT_Unknown);
1388 if (!BeforePrevious ||
1390 !(BeforePrevious->is(TT_CastRParen) ||
1391 (BeforePrevious->is(TT_ObjCMethodExpr) &&
1392 BeforePrevious->is(tok::colon))) ||
1393 BeforePrevious->is(tok::r_square) ||
1394 Contexts.back().LongestObjCSelectorName == 0 ||
1395 UnknownIdentifierInMethodDeclaration) {
1396 Tok->Previous->setType(TT_SelectorName);
1397 if (!Contexts.back().FirstObjCSelectorName) {
1398 Contexts.back().FirstObjCSelectorName = Tok->Previous;
1399 }
else if (Tok->Previous->ColumnWidth >
1400 Contexts.back().LongestObjCSelectorName) {
1401 Contexts.back().LongestObjCSelectorName =
1402 Tok->Previous->ColumnWidth;
1404 Tok->Previous->ParameterIndex =
1405 Contexts.back().FirstObjCSelectorName->ObjCSelectorNameParts;
1406 ++Contexts.back().FirstObjCSelectorName->ObjCSelectorNameParts;
1408 }
else if (Contexts.back().ColonIsForRangeExpr) {
1409 Tok->setType(TT_RangeBasedForLoopColon);
1410 for (
auto *Prev = Tok->Previous;
1411 Prev && !Prev->isOneOf(tok::semi, tok::l_paren);
1412 Prev = Prev->Previous) {
1413 if (Prev->isPointerOrReference())
1414 Prev->setFinalizedType(TT_PointerOrReference);
1416 }
else if (Contexts.back().ContextType == Context::C11GenericSelection) {
1417 Tok->setType(TT_GenericSelectionColon);
1418 }
else if (CurrentToken && CurrentToken->is(tok::numeric_constant)) {
1419 Tok->setType(TT_BitFieldColon);
1420 }
else if (Contexts.size() == 1 &&
1421 !Line.First->isOneOf(tok::kw_enum, tok::kw_case,
1423 FormatToken *Prev = Tok->getPreviousNonComment();
1426 if (Prev->isOneOf(tok::r_paren, tok::kw_noexcept) ||
1427 Prev->ClosesRequiresClause) {
1428 Tok->setType(TT_CtorInitializerColon);
1429 }
else if (Prev->is(tok::kw_try)) {
1431 FormatToken *PrevPrev = Prev->getPreviousNonComment();
1434 if (PrevPrev && PrevPrev->isOneOf(tok::r_paren, tok::kw_noexcept))
1435 Tok->setType(TT_CtorInitializerColon);
1437 Tok->setType(TT_InheritanceColon);
1438 if (Prev->isAccessSpecifierKeyword())
1441 }
else if (canBeObjCSelectorComponent(*Tok->Previous) && Tok->Next &&
1442 (Tok->Next->isOneOf(tok::r_paren, tok::comma) ||
1443 (canBeObjCSelectorComponent(*Tok->Next) && Tok->Next->Next &&
1444 Tok->Next->Next->is(tok::colon)))) {
1447 Tok->setType(TT_ObjCMethodExpr);
1454 if (Style.isJavaScript() && !Contexts.back().IsExpression)
1455 Tok->setType(TT_JsTypeOperator);
1458 if (Style.isTableGen()) {
1460 if (!parseTableGenValue())
1462 if (CurrentToken && CurrentToken->is(Keywords.kw_then))
1467 CurrentToken->isOneOf(tok::kw_constexpr, tok::identifier)) {
1473 if (CurrentToken && CurrentToken->is(tok::l_paren)) {
1475 if (!parseParens(IsIf))
1480 if (Style.isJavaScript()) {
1482 if ((Tok->Previous && Tok->Previous->is(tok::period)) ||
1483 (Tok->Next && Tok->Next->is(tok::colon))) {
1487 if (CurrentToken && CurrentToken->is(Keywords.kw_await))
1490 if (IsCpp && CurrentToken && CurrentToken->is(tok::kw_co_await))
1492 Contexts.back().ColonIsForRangeExpr =
true;
1493 if (!CurrentToken || CurrentToken->isNot(tok::l_paren))
1504 if (Tok->Previous && Tok->Previous->is(tok::r_paren) &&
1505 Tok->Previous->MatchingParen &&
1506 Tok->Previous->MatchingParen->is(TT_OverloadedOperatorLParen)) {
1507 Tok->Previous->setType(TT_OverloadedOperator);
1508 Tok->Previous->MatchingParen->setType(TT_OverloadedOperator);
1509 Tok->setType(TT_OverloadedOperatorLParen);
1512 if (Style.isVerilog()) {
1518 auto IsInstancePort = [&]() {
1519 const FormatToken *Prev = Tok->getPreviousNonComment();
1520 const FormatToken *PrevPrev;
1528 if (!Prev || !(PrevPrev = Prev->getPreviousNonComment()))
1531 if (Keywords.isVerilogIdentifier(*Prev) &&
1532 Keywords.isVerilogIdentifier(*PrevPrev)) {
1536 if (Prev->is(Keywords.kw_verilogHash) &&
1537 Keywords.isVerilogIdentifier(*PrevPrev)) {
1541 if (Keywords.isVerilogIdentifier(*Prev) && PrevPrev->is(tok::r_paren))
1544 if (Keywords.isVerilogIdentifier(*Prev) && PrevPrev->is(tok::comma)) {
1545 const FormatToken *PrevParen = PrevPrev->getPreviousNonComment();
1546 if (PrevParen && PrevParen->is(tok::r_paren) &&
1547 PrevParen->MatchingParen &&
1548 PrevParen->MatchingParen->is(TT_VerilogInstancePortLParen)) {
1555 if (IsInstancePort())
1556 Tok->setType(TT_VerilogInstancePortLParen);
1561 if (Line.MustBeDeclaration && Contexts.size() == 1 &&
1562 !Contexts.back().IsExpression && !Line.startsWith(TT_ObjCProperty) &&
1563 !Line.startsWith(tok::l_paren) &&
1564 !Tok->isOneOf(TT_TypeDeclarationParen, TT_RequiresExpressionLParen)) {
1565 if (
const auto *
Previous = Tok->Previous;
1568 !
Previous->isOneOf(TT_RequiresClause, TT_LeadingJavaAnnotation))) {
1569 Line.MightBeFunctionDecl =
true;
1570 Tok->MightBeFunctionDeclParen =
true;
1575 if (Style.isTableGen())
1576 Tok->setType(TT_TableGenListOpener);
1582 if (Tok->is(TT_RequiresExpressionLBrace))
1585 FormatToken *
Previous = Tok->getPreviousNonComment();
1587 Previous->setType(TT_SelectorName);
1589 Scopes.push_back(getScopeType(*Tok));
1595 Tok->setType(TT_TemplateOpener);
1603 Tok->Previous->isOneOf(TT_SelectorName, TT_DictLiteral))) {
1604 Tok->setType(TT_DictLiteral);
1605 FormatToken *
Previous = Tok->getPreviousNonComment();
1607 Previous->setType(TT_SelectorName);
1609 if (Style.isTableGen())
1610 Tok->setType(TT_TemplateOpener);
1612 Tok->setType(TT_BinaryOperator);
1613 NonTemplateLess.insert(Tok);
1623 if (!Scopes.empty())
1631 Tok->setType(TT_BinaryOperator);
1632 if (Tok->Previous && Tok->Previous->is(TT_TemplateCloser))
1633 Tok->SpacesRequiredBefore = 1;
1635 case tok::kw_operator:
1636 if (Style.isProto())
1638 while (CurrentToken &&
1639 !CurrentToken->isOneOf(tok::l_paren, tok::semi, tok::r_paren)) {
1640 if (CurrentToken->isOneOf(tok::star, tok::amp))
1641 CurrentToken->setType(TT_PointerOrReference);
1642 auto Next = CurrentToken->getNextNonComment();
1645 if (Next->is(tok::less))
1651 auto Previous = CurrentToken->getPreviousNonComment();
1653 if (CurrentToken->is(tok::comma) &&
Previous->isNot(tok::kw_operator))
1655 if (
Previous->isOneOf(TT_BinaryOperator, TT_UnaryOperator, tok::comma,
1656 tok::star, tok::arrow, tok::amp, tok::ampamp) ||
1658 Previous->TokenText.starts_with(
"\"\"")) {
1659 Previous->setType(TT_OverloadedOperator);
1660 if (CurrentToken->isOneOf(tok::less, tok::greater))
1664 if (CurrentToken && CurrentToken->is(tok::l_paren))
1665 CurrentToken->setType(TT_OverloadedOperatorLParen);
1666 if (CurrentToken && CurrentToken->Previous->is(TT_BinaryOperator))
1667 CurrentToken->Previous->setType(TT_OverloadedOperator);
1670 if (Style.isJavaScript() && Tok->Next &&
1671 Tok->Next->isOneOf(tok::semi, tok::comma, tok::colon, tok::r_paren,
1672 tok::r_brace, tok::r_square)) {
1677 Tok->setType(TT_JsTypeOptionalQuestion);
1682 if (Line.MustBeDeclaration && !Contexts.back().IsExpression &&
1683 Style.isJavaScript()) {
1686 if (Style.isCSharp()) {
1692 (Tok->Next->startsSequence(tok::question, tok::r_paren) ||
1693 Tok->Next->startsSequence(tok::question, tok::greater) ||
1694 Tok->Next->startsSequence(tok::question, tok::identifier,
1696 Tok->setType(TT_CSharpNullable);
1701 if (Tok->Next && Tok->Next->is(tok::identifier) && Tok->Next->Next &&
1702 Tok->Next->Next->is(tok::equal)) {
1703 Tok->setType(TT_CSharpNullable);
1712 if (!Contexts.back().IsExpression && Line.MustBeDeclaration &&
1714 !Tok->Next->isOneOf(tok::identifier, tok::string_literal) ||
1716 !Tok->Next->Next->isOneOf(tok::colon, tok::question))) {
1717 Tok->setType(TT_CSharpNullable);
1723 case tok::kw_template:
1724 parseTemplateDeclaration();
1727 switch (Contexts.back().ContextType) {
1728 case Context::CtorInitializer:
1729 Tok->setType(TT_CtorInitializerComma);
1731 case Context::InheritanceList:
1732 Tok->setType(TT_InheritanceComma);
1734 case Context::VerilogInstancePortList:
1735 Tok->setType(TT_VerilogInstancePortComma);
1738 if (Style.isVerilog() && Contexts.size() == 1 &&
1739 Line.startsWith(Keywords.kw_assign)) {
1740 Tok->setFinalizedType(TT_VerilogAssignComma);
1741 }
else if (Contexts.back().FirstStartOfName &&
1742 (Contexts.size() == 1 || startsWithInitStatement(Line))) {
1743 Contexts.back().FirstStartOfName->PartOfMultiVariableDeclStmt =
true;
1744 Line.IsMultiVariableDeclStmt =
true;
1748 if (Contexts.back().ContextType == Context::ForEachMacro)
1749 Contexts.back().IsExpression =
true;
1751 case tok::kw_default:
1753 if (Style.isVerilog() && Keywords.isVerilogEndOfLabel(*Tok) &&
1754 (Line.Level > 1 || (!Line.InPPDirective && Line.Level > 0))) {
1758 case tok::identifier:
1759 if (Tok->isOneOf(Keywords.kw___has_include,
1760 Keywords.kw___has_include_next)) {
1763 if (Style.isCSharp() && Tok->is(Keywords.kw_where) && Tok->Next &&
1764 Tok->Next->isNot(tok::l_paren)) {
1765 Tok->setType(TT_CSharpGenericTypeConstraint);
1766 parseCSharpGenericTypeConstraint();
1767 if (!Tok->getPreviousNonComment())
1768 Line.IsContinuation =
true;
1770 if (Style.isTableGen()) {
1771 if (Tok->is(Keywords.kw_assert)) {
1772 if (!parseTableGenValue())
1774 }
else if (Tok->isOneOf(Keywords.kw_def, Keywords.kw_defm) &&
1776 !Tok->Next->isOneOf(tok::colon, tok::l_brace))) {
1778 if (!parseTableGenValue(
true))
1784 if (Tok->isNot(TT_LambdaArrow) && Tok->Previous &&
1785 Tok->Previous->is(tok::kw_noexcept)) {
1786 Tok->setType(TT_TrailingReturnArrow);
1791 if (Style.isTableGen() && !parseTableGenValue())
1800 void parseCSharpGenericTypeConstraint() {
1801 int OpenAngleBracketsCount = 0;
1802 while (CurrentToken) {
1803 if (CurrentToken->is(tok::less)) {
1805 CurrentToken->setType(TT_TemplateOpener);
1806 ++OpenAngleBracketsCount;
1808 }
else if (CurrentToken->is(tok::greater)) {
1809 CurrentToken->setType(TT_TemplateCloser);
1810 --OpenAngleBracketsCount;
1812 }
else if (CurrentToken->is(tok::comma) && OpenAngleBracketsCount == 0) {
1815 CurrentToken->setType(TT_CSharpGenericTypeConstraintComma);
1817 }
else if (CurrentToken->is(Keywords.kw_where)) {
1818 CurrentToken->setType(TT_CSharpGenericTypeConstraint);
1820 }
else if (CurrentToken->is(tok::colon)) {
1821 CurrentToken->setType(TT_CSharpGenericTypeConstraintColon);
1829 void parseIncludeDirective() {
1830 if (CurrentToken && CurrentToken->is(tok::less)) {
1832 while (CurrentToken) {
1835 if (CurrentToken->isNot(tok::comment) &&
1836 !CurrentToken->TokenText.starts_with(
"//")) {
1837 CurrentToken->setType(TT_ImplicitStringLiteral);
1844 void parseWarningOrError() {
1849 while (CurrentToken) {
1850 CurrentToken->setType(TT_ImplicitStringLiteral);
1855 void parsePragma() {
1858 CurrentToken->isOneOf(Keywords.kw_mark, Keywords.kw_option,
1859 Keywords.kw_region)) {
1860 bool IsMarkOrRegion =
1861 CurrentToken->isOneOf(Keywords.kw_mark, Keywords.kw_region);
1864 while (CurrentToken) {
1865 if (IsMarkOrRegion || CurrentToken->Previous->is(TT_BinaryOperator))
1866 CurrentToken->setType(TT_ImplicitStringLiteral);
1872 void parseHasInclude() {
1873 if (!CurrentToken || CurrentToken->isNot(tok::l_paren))
1876 parseIncludeDirective();
1880 LineType parsePreprocessorDirective() {
1881 bool IsFirstToken = CurrentToken->IsFirst;
1887 if (Style.isJavaScript() && IsFirstToken) {
1891 while (CurrentToken) {
1893 CurrentToken->setType(TT_ImplicitStringLiteral);
1899 if (CurrentToken->is(tok::numeric_constant)) {
1900 CurrentToken->SpacesRequiredBefore = 1;
1905 if (!CurrentToken->Tok.getIdentifierInfo())
1909 if (Style.isVerilog() && !Keywords.isVerilogPPDirective(*CurrentToken))
1911 switch (CurrentToken->Tok.getIdentifierInfo()->getPPKeywordID()) {
1912 case tok::pp_include:
1913 case tok::pp_include_next:
1914 case tok::pp_import:
1916 parseIncludeDirective();
1920 case tok::pp_warning:
1921 parseWarningOrError();
1923 case tok::pp_pragma:
1928 Contexts.back().IsExpression =
true;
1931 CurrentToken->SpacesRequiredBefore =
true;
1937 while (CurrentToken) {
1938 FormatToken *Tok = CurrentToken;
1940 if (Tok->is(tok::l_paren)) {
1942 }
else if (Tok->isOneOf(Keywords.kw___has_include,
1943 Keywords.kw___has_include_next)) {
1954 NonTemplateLess.clear();
1955 if (!Line.InMacroBody && CurrentToken->is(tok::hash)) {
1959 auto Type = parsePreprocessorDirective();
1967 IdentifierInfo *Info = CurrentToken->Tok.getIdentifierInfo();
1969 CurrentToken->is(Keywords.kw_package)) ||
1970 (!Style.isVerilog() && Info &&
1971 Info->getPPKeywordID() == tok::pp_import && CurrentToken->Next &&
1972 CurrentToken->Next->isOneOf(tok::string_literal, tok::identifier,
1975 parseIncludeDirective();
1981 if (CurrentToken->is(tok::less) && Line.Last->is(tok::greater)) {
1982 parseIncludeDirective();
1989 CurrentToken->isOneOf(Keywords.kw_option, Keywords.kw_package)) {
1991 if (CurrentToken && CurrentToken->is(tok::identifier)) {
1992 while (CurrentToken)
1998 bool KeywordVirtualFound =
false;
1999 bool ImportStatement =
false;
2002 if (Style.isJavaScript() && CurrentToken->is(Keywords.kw_import))
2003 ImportStatement =
true;
2005 while (CurrentToken) {
2006 if (CurrentToken->is(tok::kw_virtual))
2007 KeywordVirtualFound =
true;
2008 if (Style.isJavaScript()) {
2015 if (Line.First->is(tok::kw_export) &&
2016 CurrentToken->is(Keywords.kw_from) && CurrentToken->Next &&
2017 CurrentToken->Next->isStringLiteral()) {
2018 ImportStatement =
true;
2020 if (isClosureImportStatement(*CurrentToken))
2021 ImportStatement =
true;
2023 if (!consumeToken())
2031 if (KeywordVirtualFound)
2033 if (ImportStatement)
2036 if (Line.startsWith(TT_ObjCMethodSpecifier)) {
2037 if (Contexts.back().FirstObjCSelectorName) {
2038 Contexts.back().FirstObjCSelectorName->LongestObjCSelectorName =
2039 Contexts.back().LongestObjCSelectorName;
2044 for (
const auto &ctx : Contexts)
2045 if (ctx.ContextType == Context::StructArrayInitializer)
2052 bool isClosureImportStatement(
const FormatToken &Tok) {
2055 return Tok.TokenText ==
"goog" && Tok.Next && Tok.Next->is(tok::period) &&
2057 (Tok.Next->Next->TokenText ==
"module" ||
2058 Tok.Next->Next->TokenText ==
"provide" ||
2059 Tok.Next->Next->TokenText ==
"require" ||
2060 Tok.Next->Next->TokenText ==
"requireType" ||
2061 Tok.Next->Next->TokenText ==
"forwardDeclare") &&
2062 Tok.Next->Next->Next && Tok.Next->Next->Next->is(tok::l_paren);
2065 void resetTokenMetadata() {
2071 if (!CurrentToken->isTypeFinalized() &&
2072 !CurrentToken->isOneOf(
2073 TT_LambdaLSquare, TT_LambdaLBrace, TT_AttributeMacro, TT_IfMacro,
2074 TT_ForEachMacro, TT_TypenameMacro, TT_FunctionLBrace,
2075 TT_ImplicitStringLiteral, TT_InlineASMBrace, TT_FatArrow,
2076 TT_LambdaArrow, TT_NamespaceMacro, TT_OverloadedOperator,
2077 TT_RegexLiteral, TT_TemplateString, TT_ObjCStringLiteral,
2078 TT_UntouchableMacroFunc, TT_StatementAttributeLikeMacro,
2079 TT_FunctionLikeOrFreestandingMacro, TT_ClassLBrace, TT_EnumLBrace,
2080 TT_RecordLBrace, TT_StructLBrace, TT_UnionLBrace, TT_RequiresClause,
2081 TT_RequiresClauseInARequiresExpression, TT_RequiresExpression,
2082 TT_RequiresExpressionLParen, TT_RequiresExpressionLBrace,
2083 TT_CompoundRequirementLBrace, TT_BracedListLBrace)) {
2084 CurrentToken->setType(TT_Unknown);
2086 CurrentToken->Role.reset();
2087 CurrentToken->MatchingParen =
nullptr;
2088 CurrentToken->FakeLParens.clear();
2089 CurrentToken->FakeRParens = 0;
2096 CurrentToken->NestingLevel = Contexts.size() - 1;
2097 CurrentToken->BindingStrength = Contexts.back().BindingStrength;
2098 modifyContext(*CurrentToken);
2099 determineTokenType(*CurrentToken);
2100 CurrentToken = CurrentToken->Next;
2102 resetTokenMetadata();
2145 StructArrayInitializer,
2149 C11GenericSelection,
2151 VerilogInstancePortList,
2157 struct ScopedContextCreator {
2158 AnnotatingParser &
P;
2164 P.Contexts.back().BindingStrength + Increase,
2165 P.Contexts.back().IsExpression));
2168 ~ScopedContextCreator() {
2170 if (
P.Contexts.back().ContextType == Context::StructArrayInitializer) {
2171 P.Contexts.pop_back();
2172 P.Contexts.back().ContextType = Context::StructArrayInitializer;
2176 P.Contexts.pop_back();
2180 void modifyContext(
const FormatToken &Current) {
2181 auto AssignmentStartsExpression = [&]() {
2185 if (Line.First->isOneOf(tok::kw_using, tok::kw_return))
2187 if (Line.First->is(tok::kw_template)) {
2188 assert(Current.Previous);
2189 if (Current.Previous->is(tok::kw_operator)) {
2195 const FormatToken *Tok = Line.First->getNextNonComment();
2197 if (Tok->isNot(TT_TemplateOpener)) {
2204 if (Contexts.back().ContextKind == tok::less) {
2205 assert(Current.Previous->Previous);
2206 return !Current.Previous->Previous->isOneOf(tok::kw_typename,
2210 Tok = Tok->MatchingParen;
2213 Tok = Tok->getNextNonComment();
2217 if (Tok->isOneOf(tok::kw_class, tok::kw_enum, tok::kw_struct,
2227 if (Style.isJavaScript() &&
2228 (Line.startsWith(Keywords.kw_type, tok::identifier) ||
2229 Line.startsWith(tok::kw_export, Keywords.kw_type,
2230 tok::identifier))) {
2234 return !Current.Previous || Current.Previous->isNot(tok::kw_operator);
2237 if (AssignmentStartsExpression()) {
2238 Contexts.back().IsExpression =
true;
2239 if (!Line.startsWith(TT_UnaryOperator)) {
2240 for (FormatToken *
Previous = Current.Previous;
2242 !
Previous->Previous->isOneOf(tok::comma, tok::semi);
2244 if (
Previous->isOneOf(tok::r_square, tok::r_paren, tok::greater)) {
2251 if (
Previous->isOneOf(TT_BinaryOperator, TT_UnaryOperator) &&
2253 Previous->Previous->isNot(tok::equal)) {
2254 Previous->setType(TT_PointerOrReference);
2258 }
else if (Current.is(tok::lessless) &&
2259 (!Current.Previous ||
2260 Current.Previous->isNot(tok::kw_operator))) {
2261 Contexts.back().IsExpression =
true;
2262 }
else if (Current.isOneOf(tok::kw_return, tok::kw_throw)) {
2263 Contexts.back().IsExpression =
true;
2264 }
else if (Current.is(TT_TrailingReturnArrow)) {
2265 Contexts.back().IsExpression =
false;
2266 }
else if (Current.isOneOf(TT_LambdaArrow, Keywords.kw_assert)) {
2268 }
else if (Current.Previous &&
2269 Current.Previous->is(TT_CtorInitializerColon)) {
2270 Contexts.back().IsExpression =
true;
2271 Contexts.back().ContextType = Context::CtorInitializer;
2272 }
else if (Current.Previous && Current.Previous->is(TT_InheritanceColon)) {
2273 Contexts.back().ContextType = Context::InheritanceList;
2274 }
else if (Current.isOneOf(tok::r_paren, tok::greater, tok::comma)) {
2275 for (FormatToken *
Previous = Current.Previous;
2278 Previous->setType(TT_PointerOrReference);
2280 if (Line.MustBeDeclaration &&
2281 Contexts.front().ContextType != Context::CtorInitializer) {
2282 Contexts.back().IsExpression =
false;
2284 }
else if (Current.is(tok::kw_new)) {
2285 Contexts.back().CanBeExpression =
false;
2286 }
else if (Current.is(tok::semi) ||
2287 (Current.is(tok::exclaim) && Current.Previous &&
2288 Current.Previous->isNot(tok::kw_operator))) {
2292 Contexts.back().IsExpression =
true;
2296 static FormatToken *untilMatchingParen(FormatToken *Current) {
2300 if (Current->is(tok::l_paren))
2302 if (Current->is(tok::r_paren))
2306 Current = Current->Next;
2311 static bool isDeductionGuide(FormatToken &Current) {
2313 if (Current.Previous && Current.Previous->is(tok::r_paren) &&
2314 Current.startsSequence(tok::arrow, tok::identifier, tok::less)) {
2316 FormatToken *TemplateCloser = Current.Next->Next;
2317 int NestingLevel = 0;
2318 while (TemplateCloser) {
2320 if (TemplateCloser->is(tok::l_paren)) {
2322 TemplateCloser = untilMatchingParen(TemplateCloser);
2323 if (!TemplateCloser)
2326 if (TemplateCloser->is(tok::less))
2328 if (TemplateCloser->is(tok::greater))
2330 if (NestingLevel < 1)
2332 TemplateCloser = TemplateCloser->Next;
2336 if (TemplateCloser && TemplateCloser->Next &&
2337 TemplateCloser->Next->is(tok::semi) &&
2338 Current.Previous->MatchingParen) {
2341 FormatToken *LeadingIdentifier =
2342 Current.Previous->MatchingParen->Previous;
2344 return LeadingIdentifier &&
2345 LeadingIdentifier->TokenText == Current.Next->TokenText;
2351 void determineTokenType(FormatToken &Current) {
2352 if (Current.isNot(TT_Unknown)) {
2357 if ((Style.isJavaScript() || Style.isCSharp()) &&
2358 Current.is(tok::exclaim)) {
2359 if (Current.Previous) {
2361 Style.isJavaScript()
2362 ? Keywords.isJavaScriptIdentifier(
2363 *Current.Previous,
true)
2364 : Current.Previous->is(tok::identifier);
2366 Current.Previous->isOneOf(
2367 tok::kw_default, tok::kw_namespace, tok::r_paren, tok::r_square,
2368 tok::r_brace, tok::kw_false, tok::kw_true, Keywords.kw_type,
2369 Keywords.kw_get, Keywords.kw_init, Keywords.kw_set) ||
2370 Current.Previous->Tok.isLiteral()) {
2371 Current.setType(TT_NonNullAssertion);
2376 Current.Next->isOneOf(TT_BinaryOperator, Keywords.kw_as)) {
2377 Current.setType(TT_NonNullAssertion);
2386 Current.is(Keywords.kw_instanceof)) {
2387 Current.setType(TT_BinaryOperator);
2388 }
else if (isStartOfName(Current) &&
2389 (!Line.MightBeFunctionDecl || Current.NestingLevel != 0)) {
2390 Contexts.back().FirstStartOfName = &Current;
2391 Current.setType(TT_StartOfName);
2392 }
else if (Current.is(tok::semi)) {
2396 Contexts.back().FirstStartOfName =
nullptr;
2397 }
else if (Current.isOneOf(tok::kw_auto, tok::kw___auto_type)) {
2399 }
else if (Current.is(tok::arrow) &&
2401 Current.setType(TT_LambdaArrow);
2402 }
else if (Current.is(tok::arrow) && Style.isVerilog()) {
2404 Current.setType(TT_BinaryOperator);
2405 }
else if (Current.is(tok::arrow) && AutoFound &&
2406 Line.MightBeFunctionDecl && Current.NestingLevel == 0 &&
2407 !Current.Previous->isOneOf(tok::kw_operator, tok::identifier)) {
2409 Current.setType(TT_TrailingReturnArrow);
2410 }
else if (Current.is(tok::arrow) && Current.Previous &&
2411 Current.Previous->is(tok::r_brace) &&
2415 Current.setType(TT_TrailingReturnArrow);
2416 }
else if (isDeductionGuide(Current)) {
2418 Current.setType(TT_TrailingReturnArrow);
2419 }
else if (Current.isPointerOrReference()) {
2420 Current.setType(determineStarAmpUsage(
2422 Contexts.back().CanBeExpression && Contexts.back().IsExpression,
2423 Contexts.back().ContextType == Context::TemplateArgument));
2424 }
else if (Current.isOneOf(tok::minus, tok::plus, tok::caret) ||
2425 (Style.isVerilog() && Current.is(tok::pipe))) {
2426 Current.setType(determinePlusMinusCaretUsage(Current));
2427 if (Current.is(TT_UnaryOperator) && Current.is(tok::caret))
2428 Contexts.back().CaretFound =
true;
2429 }
else if (Current.isOneOf(tok::minusminus, tok::plusplus)) {
2430 Current.setType(determineIncrementUsage(Current));
2431 }
else if (Current.isOneOf(tok::exclaim, tok::tilde)) {
2432 Current.setType(TT_UnaryOperator);
2433 }
else if (Current.is(tok::question)) {
2434 if (Style.isJavaScript() && Line.MustBeDeclaration &&
2435 !Contexts.back().IsExpression) {
2438 Current.setType(TT_JsTypeOptionalQuestion);
2439 }
else if (Style.isTableGen()) {
2441 Current.setType(TT_Unknown);
2443 Current.setType(TT_ConditionalExpr);
2445 }
else if (Current.isBinaryOperator() &&
2446 (!Current.Previous || Current.Previous->isNot(tok::l_square)) &&
2447 (Current.isNot(tok::greater) &&
2449 if (Style.isVerilog()) {
2450 if (Current.is(tok::lessequal) && Contexts.size() == 1 &&
2451 !Contexts.back().VerilogAssignmentFound) {
2455 Current.setFinalizedType(TT_BinaryOperator);
2458 Contexts.back().VerilogAssignmentFound =
true;
2460 Current.setType(TT_BinaryOperator);
2461 }
else if (Current.is(tok::comment)) {
2462 if (Current.TokenText.starts_with(
"/*")) {
2463 if (Current.TokenText.ends_with(
"*/")) {
2464 Current.setType(TT_BlockComment);
2468 Current.Tok.setKind(tok::unknown);
2471 Current.setType(TT_LineComment);
2473 }
else if (Current.is(tok::string_literal)) {
2474 if (Style.isVerilog() && Contexts.back().VerilogMayBeConcatenation &&
2475 Current.getPreviousNonComment() &&
2476 Current.getPreviousNonComment()->isOneOf(tok::comma, tok::l_brace) &&
2477 Current.getNextNonComment() &&
2478 Current.getNextNonComment()->isOneOf(tok::comma, tok::r_brace)) {
2479 Current.setType(TT_StringInConcatenation);
2481 }
else if (Current.is(tok::l_paren)) {
2482 if (lParenStartsCppCast(Current))
2483 Current.setType(TT_CppCastLParen);
2484 }
else if (Current.is(tok::r_paren)) {
2485 if (rParenEndsCast(Current))
2486 Current.setType(TT_CastRParen);
2487 if (Current.MatchingParen && Current.Next &&
2488 !Current.Next->isBinaryOperator() &&
2489 !Current.Next->isOneOf(
2490 tok::semi, tok::colon, tok::l_brace, tok::l_paren, tok::comma,
2491 tok::period, tok::arrow, tok::coloncolon, tok::kw_noexcept)) {
2492 if (FormatToken *AfterParen = Current.MatchingParen->Next;
2493 AfterParen && AfterParen->isNot(tok::caret)) {
2495 if (FormatToken *BeforeParen = Current.MatchingParen->Previous;
2496 BeforeParen && BeforeParen->is(tok::identifier) &&
2497 BeforeParen->isNot(TT_TypenameMacro) &&
2498 BeforeParen->TokenText == BeforeParen->TokenText.upper() &&
2499 (!BeforeParen->Previous ||
2500 BeforeParen->Previous->ClosesTemplateDeclaration ||
2501 BeforeParen->Previous->ClosesRequiresClause)) {
2502 Current.setType(TT_FunctionAnnotationRParen);
2506 }
else if (Current.is(tok::at) && Current.Next && !Style.isJavaScript() &&
2510 switch (Current.Next->Tok.getObjCKeywordID()) {
2511 case tok::objc_interface:
2512 case tok::objc_implementation:
2513 case tok::objc_protocol:
2514 Current.setType(TT_ObjCDecl);
2516 case tok::objc_property:
2517 Current.setType(TT_ObjCProperty);
2522 }
else if (Current.is(tok::period)) {
2523 FormatToken *PreviousNoComment = Current.getPreviousNonComment();
2524 if (PreviousNoComment &&
2525 PreviousNoComment->isOneOf(tok::comma, tok::l_brace)) {
2526 Current.setType(TT_DesignatedInitializerPeriod);
2528 Current.Previous->isOneOf(TT_JavaAnnotation,
2529 TT_LeadingJavaAnnotation)) {
2530 Current.setType(Current.Previous->getType());
2532 }
else if (canBeObjCSelectorComponent(Current) &&
2535 Current.Previous && Current.Previous->is(TT_CastRParen) &&
2536 Current.Previous->MatchingParen &&
2537 Current.Previous->MatchingParen->Previous &&
2538 Current.Previous->MatchingParen->Previous->is(
2539 TT_ObjCMethodSpecifier)) {
2543 Current.setType(TT_SelectorName);
2544 }
else if (Current.isOneOf(tok::identifier, tok::kw_const, tok::kw_noexcept,
2545 tok::kw_requires) &&
2547 !Current.Previous->isOneOf(tok::equal, tok::at,
2548 TT_CtorInitializerComma,
2549 TT_CtorInitializerColon) &&
2550 Line.MightBeFunctionDecl && Contexts.size() == 1) {
2553 Current.setType(TT_TrailingAnnotation);
2555 Style.isJavaScript()) &&
2557 if (Current.Previous->is(tok::at) &&
2558 Current.isNot(Keywords.kw_interface)) {
2559 const FormatToken &AtToken = *Current.Previous;
2560 const FormatToken *
Previous = AtToken.getPreviousNonComment();
2562 Current.setType(TT_LeadingJavaAnnotation);
2564 Current.setType(TT_JavaAnnotation);
2565 }
else if (Current.Previous->is(tok::period) &&
2566 Current.Previous->isOneOf(TT_JavaAnnotation,
2567 TT_LeadingJavaAnnotation)) {
2568 Current.setType(Current.Previous->getType());
2578 bool isStartOfName(
const FormatToken &Tok) {
2580 if (Style.isVerilog())
2583 if (Tok.isNot(tok::identifier) || !Tok.Previous)
2586 if (
const auto *NextNonComment = Tok.getNextNonComment();
2587 (!NextNonComment && !Line.InMacroBody) ||
2589 (NextNonComment->isPointerOrReference() ||
2590 NextNonComment->is(tok::string_literal) ||
2591 (Line.InPragmaDirective && NextNonComment->is(tok::identifier))))) {
2595 if (Tok.Previous->isOneOf(TT_LeadingJavaAnnotation, Keywords.kw_instanceof,
2599 if (Style.isJavaScript() && Tok.Previous->is(Keywords.kw_in))
2603 FormatToken *PreviousNotConst = Tok.getPreviousNonComment();
2606 if (!Style.isJavaScript())
2607 while (PreviousNotConst && PreviousNotConst->is(tok::kw_const))
2608 PreviousNotConst = PreviousNotConst->getPreviousNonComment();
2610 if (!PreviousNotConst)
2613 if (PreviousNotConst->ClosesRequiresClause)
2616 if (Style.isTableGen()) {
2618 if (Keywords.isTableGenDefinition(*PreviousNotConst))
2621 if (Contexts.back().ContextKind != tok::l_brace)
2625 bool IsPPKeyword = PreviousNotConst->is(tok::identifier) &&
2626 PreviousNotConst->Previous &&
2627 PreviousNotConst->Previous->is(tok::hash);
2629 if (PreviousNotConst->is(TT_TemplateCloser)) {
2630 return PreviousNotConst && PreviousNotConst->MatchingParen &&
2631 PreviousNotConst->MatchingParen->Previous &&
2632 PreviousNotConst->MatchingParen->Previous->isNot(tok::period) &&
2633 PreviousNotConst->MatchingParen->Previous->isNot(tok::kw_template);
2636 if ((PreviousNotConst->is(tok::r_paren) &&
2637 PreviousNotConst->is(TT_TypeDeclarationParen)) ||
2638 PreviousNotConst->is(TT_AttributeRParen)) {
2647 if (PreviousNotConst->isOneOf(tok::identifier, tok::kw_auto) &&
2648 PreviousNotConst->isNot(TT_StatementAttributeLikeMacro)) {
2653 if (PreviousNotConst->is(TT_PointerOrReference))
2657 if (PreviousNotConst->isTypeName(LangOpts))
2662 PreviousNotConst->is(tok::r_square)) {
2667 return Style.isJavaScript() && PreviousNotConst->is(tok::kw_const);
2671 bool lParenStartsCppCast(
const FormatToken &Tok) {
2676 FormatToken *LeftOfParens = Tok.getPreviousNonComment();
2677 if (LeftOfParens && LeftOfParens->is(TT_TemplateCloser) &&
2678 LeftOfParens->MatchingParen) {
2679 auto *Prev = LeftOfParens->MatchingParen->getPreviousNonComment();
2681 Prev->isOneOf(tok::kw_const_cast, tok::kw_dynamic_cast,
2682 tok::kw_reinterpret_cast, tok::kw_static_cast)) {
2692 bool rParenEndsCast(
const FormatToken &Tok) {
2693 assert(Tok.is(tok::r_paren));
2695 if (!Tok.MatchingParen || !Tok.Previous)
2702 const auto *LParen = Tok.MatchingParen;
2703 const auto *BeforeRParen = Tok.Previous;
2704 const auto *AfterRParen = Tok.Next;
2707 if (BeforeRParen == LParen || !AfterRParen)
2710 if (LParen->is(TT_OverloadedOperatorLParen))
2713 auto *LeftOfParens = LParen->getPreviousNonComment();
2717 if (LeftOfParens->is(tok::r_paren) &&
2718 LeftOfParens->isNot(TT_CastRParen)) {
2719 if (!LeftOfParens->MatchingParen ||
2720 !LeftOfParens->MatchingParen->Previous) {
2723 LeftOfParens = LeftOfParens->MatchingParen->Previous;
2726 if (LeftOfParens->is(tok::r_square)) {
2728 auto MayBeArrayDelete = [](FormatToken *Tok) -> FormatToken * {
2729 if (Tok->isNot(tok::r_square))
2732 Tok = Tok->getPreviousNonComment();
2733 if (!Tok || Tok->isNot(tok::l_square))
2736 Tok = Tok->getPreviousNonComment();
2737 if (!Tok || Tok->isNot(tok::kw_delete))
2741 if (FormatToken *MaybeDelete = MayBeArrayDelete(LeftOfParens))
2742 LeftOfParens = MaybeDelete;
2748 if (LeftOfParens->Tok.getIdentifierInfo() && LeftOfParens->Previous &&
2749 LeftOfParens->Previous->is(tok::kw_operator)) {
2755 if (LeftOfParens->Tok.getIdentifierInfo() &&
2756 !LeftOfParens->isOneOf(Keywords.kw_in, tok::kw_return, tok::kw_case,
2757 tok::kw_delete, tok::kw_throw)) {
2763 if (LeftOfParens->isOneOf(tok::at, tok::r_square, TT_OverloadedOperator,
2764 TT_TemplateCloser, tok::ellipsis)) {
2769 if (AfterRParen->is(tok::question) ||
2770 (AfterRParen->is(tok::ampamp) && !BeforeRParen->isTypeName(LangOpts))) {
2775 if (AfterRParen->is(Keywords.kw_in) && Style.isCSharp())
2780 if (AfterRParen->isOneOf(tok::kw_noexcept, tok::kw_volatile, tok::kw_const,
2781 tok::kw_requires, tok::kw_throw, tok::arrow,
2782 Keywords.kw_override, Keywords.kw_final) ||
2783 isCppAttribute(IsCpp, *AfterRParen)) {
2793 if (AfterRParen->isOneOf(tok::kw_sizeof, tok::kw_alignof) ||
2794 (AfterRParen->Tok.isLiteral() &&
2795 AfterRParen->isNot(tok::string_literal))) {
2799 auto IsNonVariableTemplate = [](
const FormatToken &Tok) {
2800 if (Tok.isNot(TT_TemplateCloser))
2802 const auto *
Less = Tok.MatchingParen;
2805 const auto *BeforeLess =
Less->getPreviousNonComment();
2806 return BeforeLess && BeforeLess->isNot(TT_VariableTemplate);
2810 auto IsQualifiedPointerOrReference = [](
const FormatToken *
T,
2811 const LangOptions &LangOpts) {
2813 assert(!
T->isTypeName(LangOpts) &&
"Should have already been checked");
2817 if (
T->is(TT_AttributeRParen)) {
2819 assert(
T->is(tok::r_paren));
2820 assert(
T->MatchingParen);
2821 assert(
T->MatchingParen->is(tok::l_paren));
2822 assert(
T->MatchingParen->is(TT_AttributeLParen));
2823 if (
const auto *Tok =
T->MatchingParen->Previous;
2824 Tok && Tok->isAttribute()) {
2828 }
else if (
T->is(TT_AttributeSquare)) {
2830 if (
T->MatchingParen &&
T->MatchingParen->Previous) {
2831 T =
T->MatchingParen->Previous;
2834 }
else if (
T->canBePointerOrReferenceQualifier()) {
2840 return T &&
T->is(TT_PointerOrReference);
2843 bool ParensAreType = IsNonVariableTemplate(*BeforeRParen) ||
2844 BeforeRParen->is(TT_TypeDeclarationParen) ||
2845 BeforeRParen->isTypeName(LangOpts) ||
2846 IsQualifiedPointerOrReference(BeforeRParen, LangOpts);
2847 bool ParensCouldEndDecl =
2848 AfterRParen->isOneOf(tok::equal, tok::semi, tok::l_brace, tok::greater);
2849 if (ParensAreType && !ParensCouldEndDecl)
2860 for (
const auto *Token = LParen->Next; Token != &Tok; Token = Token->Next)
2861 if (Token->is(TT_BinaryOperator))
2866 if (AfterRParen->isOneOf(tok::identifier, tok::kw_this))
2870 if (AfterRParen->is(tok::l_paren)) {
2871 for (
const auto *Prev = BeforeRParen; Prev->is(tok::identifier);) {
2872 Prev = Prev->Previous;
2873 if (Prev->is(tok::coloncolon))
2874 Prev = Prev->Previous;
2880 if (!AfterRParen->Next)
2883 if (AfterRParen->is(tok::l_brace) &&
2891 const bool NextIsAmpOrStar = AfterRParen->isOneOf(tok::amp, tok::star);
2892 if (!(AfterRParen->isUnaryOperator() || NextIsAmpOrStar) ||
2893 AfterRParen->is(tok::plus) ||
2894 !AfterRParen->Next->isOneOf(tok::identifier, tok::numeric_constant)) {
2898 if (NextIsAmpOrStar &&
2899 (AfterRParen->Next->is(tok::numeric_constant) || Line.InPPDirective)) {
2903 if (Line.InPPDirective && AfterRParen->is(tok::minus))
2906 const auto *Prev = BeforeRParen;
2909 if (Prev->is(tok::r_paren)) {
2910 if (Prev->is(TT_CastRParen))
2912 Prev = Prev->MatchingParen;
2915 Prev = Prev->Previous;
2916 if (!Prev || Prev->isNot(tok::r_paren))
2918 Prev = Prev->MatchingParen;
2919 return Prev && Prev->is(TT_FunctionTypeLParen);
2923 for (Prev = BeforeRParen; Prev != LParen; Prev = Prev->Previous)
2924 if (!Prev->isOneOf(tok::kw_const, tok::identifier, tok::coloncolon))
2931 bool determineUnaryOperatorByUsage(
const FormatToken &Tok) {
2932 const FormatToken *PrevToken = Tok.getPreviousNonComment();
2942 if (PrevToken->isOneOf(
2943 TT_ConditionalExpr, tok::l_paren, tok::comma, tok::colon, tok::semi,
2944 tok::equal, tok::question, tok::l_square, tok::l_brace,
2945 tok::kw_case, tok::kw_co_await, tok::kw_co_return, tok::kw_co_yield,
2946 tok::kw_delete, tok::kw_return, tok::kw_throw)) {
2953 if (PrevToken->is(tok::kw_sizeof))
2957 if (PrevToken->isOneOf(TT_CastRParen, TT_UnaryOperator))
2961 if (PrevToken->is(TT_BinaryOperator))
2969 bool InTemplateArgument) {
2970 if (Style.isJavaScript())
2971 return TT_BinaryOperator;
2974 if (Style.isCSharp() && Tok.is(tok::ampamp))
2975 return TT_BinaryOperator;
2977 if (Style.isVerilog()) {
2982 if (Tok.is(tok::star))
2983 return TT_BinaryOperator;
2984 return determineUnaryOperatorByUsage(Tok) ? TT_UnaryOperator
2985 : TT_BinaryOperator;
2988 const FormatToken *PrevToken = Tok.getPreviousNonComment();
2990 return TT_UnaryOperator;
2991 if (PrevToken->is(TT_TypeName))
2992 return TT_PointerOrReference;
2993 if (PrevToken->isOneOf(tok::kw_new, tok::kw_delete) && Tok.is(tok::ampamp))
2994 return TT_BinaryOperator;
2996 const FormatToken *NextToken = Tok.getNextNonComment();
2998 if (InTemplateArgument && NextToken && NextToken->is(tok::kw_noexcept))
2999 return TT_BinaryOperator;
3002 NextToken->isOneOf(tok::arrow, tok::equal, tok::comma, tok::r_paren,
3003 TT_RequiresClause) ||
3005 NextToken->canBePointerOrReferenceQualifier() ||
3006 (NextToken->is(tok::l_brace) && !NextToken->getNextNonComment())) {
3007 return TT_PointerOrReference;
3010 if (PrevToken->is(tok::coloncolon))
3011 return TT_PointerOrReference;
3013 if (PrevToken->is(tok::r_paren) && PrevToken->is(TT_TypeDeclarationParen))
3014 return TT_PointerOrReference;
3016 if (determineUnaryOperatorByUsage(Tok))
3017 return TT_UnaryOperator;
3019 if (NextToken->is(tok::l_square) && NextToken->isNot(TT_LambdaLSquare))
3020 return TT_PointerOrReference;
3022 return TT_PointerOrReference;
3023 if (NextToken->isOneOf(tok::comma, tok::semi))
3024 return TT_PointerOrReference;
3036 if (PrevToken->is(tok::r_brace) && Tok.is(tok::star) &&
3037 !PrevToken->MatchingParen) {
3038 return TT_PointerOrReference;
3041 if (PrevToken->endsSequence(tok::r_square, tok::l_square, tok::kw_delete))
3042 return TT_UnaryOperator;
3044 if (PrevToken->Tok.isLiteral() ||
3045 PrevToken->isOneOf(tok::r_paren, tok::r_square, tok::kw_true,
3046 tok::kw_false, tok::r_brace)) {
3047 return TT_BinaryOperator;
3050 const FormatToken *NextNonParen = NextToken;
3051 while (NextNonParen && NextNonParen->is(tok::l_paren))
3052 NextNonParen = NextNonParen->getNextNonComment();
3053 if (NextNonParen && (NextNonParen->Tok.isLiteral() ||
3054 NextNonParen->isOneOf(tok::kw_true, tok::kw_false) ||
3055 NextNonParen->isUnaryOperator())) {
3056 return TT_BinaryOperator;
3062 if (InTemplateArgument && NextToken->Tok.isAnyIdentifier())
3063 return TT_BinaryOperator;
3067 if (Tok.is(tok::ampamp) &&
3068 NextToken->isOneOf(tok::l_paren, tok::star, tok::amp)) {
3069 return TT_BinaryOperator;
3074 if (NextToken->Tok.isAnyIdentifier()) {
3075 const FormatToken *NextNextToken = NextToken->getNextNonComment();
3076 if (NextNextToken && NextNextToken->is(tok::arrow))
3077 return TT_BinaryOperator;
3083 return TT_BinaryOperator;
3086 if (!Scopes.empty() && Scopes.back() ==
ST_Class)
3087 return TT_PointerOrReference;
3090 auto IsChainedOperatorAmpOrMember = [](
const FormatToken *token) {
3091 return !token || token->isOneOf(tok::amp, tok::period, tok::arrow,
3092 tok::arrowstar, tok::periodstar);
3097 if (Tok.is(tok::amp) && PrevToken && PrevToken->Tok.isAnyIdentifier() &&
3098 IsChainedOperatorAmpOrMember(PrevToken->getPreviousNonComment()) &&
3099 NextToken && NextToken->Tok.isAnyIdentifier()) {
3100 if (
auto NextNext = NextToken->getNextNonComment();
3102 (IsChainedOperatorAmpOrMember(NextNext) || NextNext->is(tok::semi))) {
3103 return TT_BinaryOperator;
3109 return TT_BinaryOperator;
3112 return TT_PointerOrReference;
3115 TokenType determinePlusMinusCaretUsage(
const FormatToken &Tok) {
3116 if (determineUnaryOperatorByUsage(Tok))
3117 return TT_UnaryOperator;
3119 const FormatToken *PrevToken = Tok.getPreviousNonComment();
3121 return TT_UnaryOperator;
3123 if (PrevToken->is(tok::at))
3124 return TT_UnaryOperator;
3127 return TT_BinaryOperator;
3131 TokenType determineIncrementUsage(
const FormatToken &Tok) {
3132 const FormatToken *PrevToken = Tok.getPreviousNonComment();
3133 if (!PrevToken || PrevToken->is(TT_CastRParen))
3134 return TT_UnaryOperator;
3135 if (PrevToken->isOneOf(tok::r_paren, tok::r_square, tok::identifier))
3136 return TT_TrailingUnaryOperator;
3138 return TT_UnaryOperator;
3141 SmallVector<Context, 8> Contexts;
3143 const FormatStyle &Style;
3144 AnnotatedLine &Line;
3145 FormatToken *CurrentToken;
3148 LangOptions LangOpts;
3149 const AdditionalKeywords &Keywords;
3151 SmallVector<ScopeType> &Scopes;
3159 int TemplateDeclarationDepth;
3167class ExpressionParser {
3169 ExpressionParser(
const FormatStyle &Style,
const AdditionalKeywords &Keywords,
3170 AnnotatedLine &Line)
3171 : Style(Style), Keywords(Keywords), Line(Line), Current(Line.
First) {}
3174 void parse(
int Precedence = 0) {
3177 while (Current && (Current->is(tok::kw_return) ||
3178 (Current->is(tok::colon) &&
3179 Current->isOneOf(TT_ObjCMethodExpr, TT_DictLiteral)))) {
3183 if (!Current || Precedence > PrecedenceArrowAndPeriod)
3188 parseConditionalExpr();
3194 if (Precedence == PrecedenceUnaryOperator) {
3195 parseUnaryOperator();
3199 FormatToken *Start = Current;
3200 FormatToken *LatestOperator =
nullptr;
3201 unsigned OperatorIndex = 0;
3203 FormatToken *VerilogFirstOfType =
nullptr;
3212 if (Style.isVerilog() && Precedence ==
prec::Comma) {
3213 VerilogFirstOfType =
3214 verilogGroupDecl(VerilogFirstOfType, LatestOperator);
3218 parse(Precedence + 1);
3220 int CurrentPrecedence = getCurrentPrecedence();
3231 if (Precedence == CurrentPrecedence && Current &&
3232 Current->is(TT_SelectorName)) {
3234 addFakeParenthesis(Start,
prec::Level(Precedence));
3238 if ((Style.isCSharp() || Style.isJavaScript() ||
3243 FormatToken *Prev = Current->getPreviousNonComment();
3244 if (Prev && Prev->is(tok::string_literal) &&
3245 (Prev == Start || Prev->endsSequence(tok::string_literal, tok::plus,
3246 TT_StringInConcatenation))) {
3247 Prev->setType(TT_StringInConcatenation);
3254 (Current->closesScope() &&
3255 (Current->MatchingParen || Current->is(TT_TemplateString))) ||
3256 (CurrentPrecedence != -1 && CurrentPrecedence < Precedence) ||
3265 if (Current->opensScope() ||
3266 Current->isOneOf(TT_RequiresClause,
3267 TT_RequiresClauseInARequiresExpression)) {
3270 while (Current && (!Current->closesScope() || Current->opensScope())) {
3277 if (CurrentPrecedence == Precedence) {
3279 LatestOperator->NextOperator = Current;
3280 LatestOperator = Current;
3281 Current->OperatorIndex = OperatorIndex;
3284 next(Precedence > 0);
3289 if (Style.isVerilog() && Precedence ==
prec::Comma && VerilogFirstOfType)
3290 addFakeParenthesis(VerilogFirstOfType,
prec::Comma);
3292 if (LatestOperator && (Current || Precedence > 0)) {
3298 Start->Previous->isOneOf(TT_RequiresClause,
3299 TT_RequiresClauseInARequiresExpression))
3301 auto Ret = Current ? Current : Line.Last;
3302 while (!
Ret->ClosesRequiresClause &&
Ret->Previous)
3308 if (Precedence == PrecedenceArrowAndPeriod) {
3312 addFakeParenthesis(Start,
prec::Level(Precedence), End);
3320 int getCurrentPrecedence() {
3322 const FormatToken *NextNonComment = Current->getNextNonComment();
3323 if (Current->is(TT_ConditionalExpr))
3325 if (NextNonComment && Current->is(TT_SelectorName) &&
3326 (NextNonComment->isOneOf(TT_DictLiteral, TT_JsTypeColon) ||
3327 (Style.isProto() && NextNonComment->is(tok::less)))) {
3330 if (Current->is(TT_JsComputedPropertyName))
3332 if (Current->is(TT_LambdaArrow))
3334 if (Current->is(TT_FatArrow))
3336 if (Current->isOneOf(tok::semi, TT_InlineASMColon, TT_SelectorName) ||
3337 (Current->is(tok::comment) && NextNonComment &&
3338 NextNonComment->is(TT_SelectorName))) {
3341 if (Current->is(TT_RangeBasedForLoopColon))
3344 Current->is(Keywords.kw_instanceof)) {
3347 if (Style.isJavaScript() &&
3348 Current->isOneOf(Keywords.kw_in, Keywords.kw_as)) {
3351 if (Current->is(TT_BinaryOperator) || Current->is(tok::comma))
3352 return Current->getPrecedence();
3353 if (Current->isOneOf(tok::period, tok::arrow) &&
3354 Current->isNot(TT_TrailingReturnArrow)) {
3355 return PrecedenceArrowAndPeriod;
3358 Current->isOneOf(Keywords.kw_extends, Keywords.kw_implements,
3359 Keywords.kw_throws)) {
3364 if (Style.isVerilog() && Current->is(tok::colon))
3370 void addFakeParenthesis(FormatToken *Start,
prec::Level Precedence,
3371 FormatToken *End =
nullptr) {
3376 if (Start->MacroParent)
3379 Start->FakeLParens.push_back(Precedence);
3381 Start->StartsBinaryExpression =
true;
3382 if (!End && Current)
3383 End = Current->getPreviousNonComment();
3387 End->EndsBinaryExpression =
true;
3393 void parseUnaryOperator() {
3394 SmallVector<FormatToken *, 2> Tokens;
3395 while (Current && Current->is(TT_UnaryOperator)) {
3396 Tokens.push_back(Current);
3399 parse(PrecedenceArrowAndPeriod);
3400 for (FormatToken *Token : reverse(Tokens)) {
3406 void parseConditionalExpr() {
3407 while (Current && Current->isTrailingComment())
3409 FormatToken *Start = Current;
3411 if (!Current || Current->isNot(tok::question))
3415 if (!Current || Current->isNot(TT_ConditionalExpr))
3422 void next(
bool SkipPastLeadingComments =
true) {
3424 Current = Current->Next;
3426 (Current->NewlinesBefore == 0 || SkipPastLeadingComments) &&
3427 Current->isTrailingComment()) {
3428 Current = Current->Next;
3434 FormatToken *verilogGroupDecl(FormatToken *FirstOfType,
3435 FormatToken *PreviousComma) {
3439 FormatToken *Start = Current;
3442 while (Start->startsSequence(tok::l_paren, tok::star)) {
3443 if (!(Start = Start->MatchingParen) ||
3444 !(Start = Start->getNextNonComment())) {
3449 FormatToken *Tok = Start;
3451 if (Tok->is(Keywords.kw_assign))
3452 Tok = Tok->getNextNonComment();
3460 FormatToken *
First =
nullptr;
3462 FormatToken *Next = Tok->getNextNonComment();
3464 if (Tok->is(tok::hash)) {
3469 Tok = Tok->getNextNonComment();
3470 }
else if (Tok->is(tok::hashhash)) {
3474 Tok = Tok->getNextNonComment();
3475 }
else if (Keywords.isVerilogQualifier(*Tok) ||
3476 Keywords.isVerilogIdentifier(*Tok)) {
3480 while (Tok && Tok->isOneOf(tok::period, tok::coloncolon) &&
3481 (Tok = Tok->getNextNonComment())) {
3482 if (Keywords.isVerilogIdentifier(*Tok))
3483 Tok = Tok->getNextNonComment();
3487 }
else if (Tok->is(tok::l_paren)) {
3492 Keywords.kw_highz0, Keywords.kw_highz1, Keywords.kw_large,
3493 Keywords.kw_medium, Keywords.kw_pull0, Keywords.kw_pull1,
3494 Keywords.kw_small, Keywords.kw_strong0, Keywords.kw_strong1,
3495 Keywords.kw_supply0, Keywords.kw_supply1, Keywords.kw_weak0,
3496 Keywords.kw_weak1)) {
3497 Tok->setType(TT_VerilogStrength);
3498 Tok = Tok->MatchingParen;
3500 Tok->setType(TT_VerilogStrength);
3501 Tok = Tok->getNextNonComment();
3506 }
else if (Tok->is(Keywords.kw_verilogHash)) {
3508 if (Next->is(tok::l_paren))
3509 Next = Next->MatchingParen;
3511 Tok = Next->getNextNonComment();
3518 FormatToken *Second =
nullptr;
3520 while (Tok && Tok->is(tok::l_square) && (Tok = Tok->MatchingParen))
3521 Tok = Tok->getNextNonComment();
3522 if (Tok && (Tok->is(tok::hash) || Keywords.isVerilogIdentifier(*Tok)))
3527 FormatToken *TypedName =
nullptr;
3531 First->setType(TT_VerilogDimensionedTypeName);
3532 }
else if (
First != Start) {
3540 if (TypedName->is(TT_Unknown))
3541 TypedName->setType(TT_StartOfName);
3543 if (FirstOfType && PreviousComma) {
3544 PreviousComma->setType(TT_VerilogTypeComma);
3545 addFakeParenthesis(FirstOfType,
prec::Comma, PreviousComma->Previous);
3548 FirstOfType = TypedName;
3555 while (Current && Current != FirstOfType) {
3556 if (Current->opensScope()) {
3567 const FormatStyle &Style;
3568 const AdditionalKeywords &Keywords;
3569 const AnnotatedLine &Line;
3570 FormatToken *Current;
3579 assert(
Line->First);
3586 Line->First->OriginalColumn) {
3587 const bool PPDirectiveOrImportStmt =
3590 if (PPDirectiveOrImportStmt)
3596 PPDirectiveOrImportStmt
3598 : NextNonCommentLine->
Level;
3600 NextNonCommentLine =
Line->First->isNot(tok::r_brace) ?
Line :
nullptr;
3609 for (
const auto *Tok =
Line.First; Tok; Tok = Tok->Next)
3618 for (
FormatToken *Tok =
Line.getFirstNonComment(), *Name =
nullptr; Tok;
3619 Tok = Tok->getNextNonComment()) {
3621 if (Tok->is(tok::l_square) && Tok->is(TT_AttributeSquare)) {
3630 if (Tok->is(tok::l_paren) && Tok->is(TT_Unknown) && Tok->MatchingParen) {
3638 if (Tok->isOneOf(tok::kw_friend, tok::kw_inline, tok::kw_virtual,
3639 tok::kw_constexpr, tok::kw_consteval, tok::kw_explicit)) {
3644 if (Tok->is(tok::coloncolon)) {
3651 while (Tok->startsSequence(tok::identifier, tok::coloncolon)) {
3653 Tok = Tok->Next->Next;
3659 if (Tok->is(tok::tilde)) {
3666 if (Tok->isNot(tok::identifier) || Tok->isNot(TT_Unknown))
3677 assert(Tok && Tok->
is(tok::identifier));
3680 if (Prev && Prev->is(tok::tilde))
3683 if (!Prev || !Prev->endsSequence(tok::coloncolon, tok::identifier))
3686 assert(Prev->Previous);
3687 return Prev->Previous->TokenText == Tok->
TokenText;
3691 if (!
Line.InMacroBody)
3692 MacroBodyScopes.clear();
3694 auto &ScopeStack =
Line.InMacroBody ? MacroBodyScopes : Scopes;
3695 AnnotatingParser
Parser(Style,
Line, Keywords, ScopeStack);
3698 if (!
Line.Children.empty()) {
3701 for (
auto &Child :
Line.Children) {
3702 if (InRequiresExpression &&
3703 !Child->First->isOneOf(tok::kw_typename, tok::kw_requires,
3704 TT_CompoundRequirementLBrace)) {
3710 if (!ScopeStack.empty())
3711 ScopeStack.pop_back();
3724 ExpressionParser ExprParser(Style, Keywords,
Line);
3730 if (Tok && ((!ScopeStack.empty() && ScopeStack.back() ==
ST_Class) ||
3732 Tok->setFinalizedType(TT_CtorDtorDeclName);
3733 assert(OpeningParen);
3738 if (
Line.startsWith(TT_ObjCMethodSpecifier))
3740 else if (
Line.startsWith(TT_ObjCDecl))
3742 else if (
Line.startsWith(TT_ObjCProperty))
3746 First->SpacesRequiredBefore = 1;
3747 First->CanBreakBefore =
First->MustBreakBefore;
3756 if (Current.
is(TT_FunctionDeclarationName))
3765 if (Prev->is(tok::coloncolon))
3766 Prev = Prev->Previous;
3773 if (
const auto *PrevPrev =
Previous.getPreviousNonComment();
3774 PrevPrev && PrevPrev->is(TT_ObjCDecl)) {
3778 auto skipOperatorName =
3780 for (; Next; Next = Next->Next) {
3781 if (Next->is(TT_OverloadedOperatorLParen))
3783 if (Next->is(TT_OverloadedOperator))
3785 if (Next->isOneOf(tok::kw_new, tok::kw_delete, tok::kw_co_await)) {
3788 Next->Next->startsSequence(tok::l_square, tok::r_square)) {
3789 Next = Next->Next->Next;
3793 if (Next->startsSequence(tok::l_square, tok::r_square)) {
3798 if ((Next->isTypeName(LangOpts) || Next->is(tok::identifier)) &&
3799 Next->Next && Next->Next->isPointerOrReference()) {
3804 if (Next->is(TT_TemplateOpener) && Next->MatchingParen) {
3805 Next = Next->MatchingParen;
3814 const auto *Next = Current.
Next;
3815 const bool IsCpp = LangOpts.CXXOperatorNames;
3818 if (Current.
is(tok::kw_operator)) {
3819 if (
Previous.Tok.getIdentifierInfo() &&
3820 !
Previous.isOneOf(tok::kw_return, tok::kw_co_return)) {
3825 assert(
Previous.MatchingParen->is(tok::l_paren));
3826 assert(
Previous.MatchingParen->is(TT_TypeDeclarationParen));
3831 Next = skipOperatorName(Next);
3835 for (; Next; Next = Next->Next) {
3836 if (Next->is(TT_TemplateOpener) && Next->MatchingParen) {
3837 Next = Next->MatchingParen;
3838 }
else if (Next->is(tok::coloncolon)) {
3842 if (Next->is(tok::kw_operator)) {
3843 Next = skipOperatorName(Next->Next);
3846 if (Next->isNot(tok::identifier))
3848 }
else if (isCppAttribute(IsCpp, *Next)) {
3849 Next = Next->MatchingParen;
3852 }
else if (Next->is(tok::l_paren)) {
3861 if (!Next || Next->isNot(tok::l_paren) || !Next->MatchingParen)
3863 ClosingParen = Next->MatchingParen;
3864 assert(ClosingParen->
is(tok::r_paren));
3866 if (
Line.Last->is(tok::l_brace))
3868 if (Next->Next == ClosingParen)
3871 if (ClosingParen->
Next && ClosingParen->
Next->
is(TT_PointerOrReference))
3884 if (IsCpp && Next->Next && Next->Next->is(tok::identifier) &&
3885 !
Line.endsWith(tok::semi)) {
3889 for (
const FormatToken *Tok = Next->Next; Tok && Tok != ClosingParen;
3891 if (Tok->is(TT_TypeDeclarationParen))
3893 if (Tok->isOneOf(tok::l_paren, TT_TemplateOpener) && Tok->MatchingParen) {
3894 Tok = Tok->MatchingParen;
3897 if (Tok->is(tok::kw_const) || Tok->isTypeName(LangOpts) ||
3898 Tok->isOneOf(TT_PointerOrReference, TT_StartOfName, tok::ellipsis)) {
3901 if (Tok->isOneOf(tok::l_brace, TT_ObjCMethodExpr) || Tok->Tok.isLiteral())
3907bool TokenAnnotator::mustBreakForReturnType(
const AnnotatedLine &
Line)
const {
3908 assert(
Line.MightBeFunctionDecl);
3916 switch (Style.BreakAfterReturnType) {
3926 return Line.mightBeFunctionDefinition();
3936 Line.Computed =
true;
3944 :
Line.FirstStartColumn +
First->ColumnWidth;
3945 bool AlignArrayOfStructures =
3948 if (AlignArrayOfStructures)
3949 calculateArrayInitializerColumnList(
Line);
3951 const auto *FirstNonComment =
Line.getFirstNonComment();
3952 bool SeenName =
false;
3953 bool LineIsFunctionDeclaration =
false;
3957 for (
auto *Tok = FirstNonComment ? FirstNonComment->Next :
nullptr; Tok;
3959 if (Tok->is(TT_StartOfName))
3961 if (Tok->Previous->EndsCppAttributeGroup)
3962 AfterLastAttribute = Tok;
3963 if (
const bool IsCtorOrDtor = Tok->is(TT_CtorDtorDeclName);
3968 LineIsFunctionDeclaration =
true;
3972 assert(OpeningParen);
3973 if (OpeningParen->is(TT_Unknown))
3974 OpeningParen->setType(TT_FunctionDeclarationLParen);
3981 (LineIsFunctionDeclaration ||
3982 (FirstNonComment && FirstNonComment->is(TT_CtorDtorDeclName))) &&
3983 Line.endsWith(tok::semi, tok::r_brace)) {
3984 auto *Tok =
Line.Last->Previous;
3985 while (Tok->isNot(tok::r_brace))
3986 Tok = Tok->Previous;
3987 if (
auto *LBrace = Tok->MatchingParen; LBrace) {
3988 assert(LBrace->is(tok::l_brace));
3991 LBrace->setFinalizedType(TT_FunctionLBrace);
3995 if (IsCpp && SeenName && AfterLastAttribute &&
3998 if (LineIsFunctionDeclaration)
3999 Line.ReturnTypeWrapped =
true;
4003 if (!LineIsFunctionDeclaration) {
4005 for (
const auto *Tok = FirstNonComment; Tok; Tok = Tok->Next) {
4006 if (Tok->isNot(tok::kw_operator))
4010 }
while (Tok && Tok->isNot(TT_OverloadedOperatorLParen));
4011 if (!Tok || !Tok->MatchingParen)
4013 const auto *LeftParen = Tok;
4014 for (Tok = Tok->Next; Tok && Tok != LeftParen->MatchingParen;
4016 if (Tok->isNot(tok::identifier))
4018 auto *Next = Tok->Next;
4019 const bool NextIsBinaryOperator =
4020 Next && Next->isPointerOrReference() && Next->Next &&
4021 Next->Next->is(tok::identifier);
4022 if (!NextIsBinaryOperator)
4024 Next->setType(TT_BinaryOperator);
4028 }
else if (ClosingParen) {
4029 for (
auto *Tok = ClosingParen->
Next; Tok; Tok = Tok->
Next) {
4030 if (Tok->is(TT_CtorInitializerColon))
4032 if (Tok->is(tok::arrow)) {
4033 Tok->setType(TT_TrailingReturnArrow);
4036 if (Tok->isNot(TT_TrailingAnnotation))
4038 const auto *Next = Tok->Next;
4039 if (!Next || Next->isNot(tok::l_paren))
4041 Tok = Next->MatchingParen;
4048 bool InFunctionDecl =
Line.MightBeFunctionDecl;
4049 for (
auto *Current =
First->Next; Current; Current = Current->Next) {
4051 if (Current->is(TT_LineComment)) {
4053 Current->SpacesRequiredBefore =
4054 (Style.Cpp11BracedListStyle && !Style.SpacesInParensOptions.Other)
4057 }
else if (Prev->
is(TT_VerilogMultiLineListLParen)) {
4058 Current->SpacesRequiredBefore = 0;
4060 Current->SpacesRequiredBefore = Style.SpacesBeforeTrailingComments;
4070 if (!Current->HasUnescapedNewline) {
4073 if (
Parameter->isOneOf(tok::comment, tok::r_brace))
4076 if (
Parameter->Previous->isNot(TT_CtorInitializerComma) &&
4084 }
else if (!Current->Finalized && Current->SpacesRequiredBefore == 0 &&
4085 spaceRequiredBefore(
Line, *Current)) {
4086 Current->SpacesRequiredBefore = 1;
4089 const auto &Children = Prev->
Children;
4090 if (!Children.empty() && Children.back()->Last->is(TT_LineComment)) {
4091 Current->MustBreakBefore =
true;
4093 Current->MustBreakBefore =
4094 Current->MustBreakBefore || mustBreakBefore(
Line, *Current);
4095 if (!Current->MustBreakBefore && InFunctionDecl &&
4096 Current->is(TT_FunctionDeclarationName)) {
4097 Current->MustBreakBefore = mustBreakForReturnType(
Line);
4101 Current->CanBreakBefore =
4102 Current->MustBreakBefore || canBreakBefore(
Line, *Current);
4103 unsigned ChildSize = 0;
4109 if (Current->MustBreakBefore || Prev->
Children.size() > 1 ||
4111 Prev->
Children[0]->First->MustBreakBefore) ||
4112 Current->IsMultiline) {
4113 Current->TotalLength = Prev->
TotalLength + Style.ColumnLimit;
4115 Current->TotalLength = Prev->
TotalLength + Current->ColumnWidth +
4116 ChildSize + Current->SpacesRequiredBefore;
4119 if (Current->is(TT_CtorInitializerColon))
4120 InFunctionDecl =
false;
4131 Current->SplitPenalty = splitPenalty(
Line, *Current, InFunctionDecl);
4133 Current->is(TT_SelectorName) && Current->ParameterIndex > 0) {
4134 if (Current->ParameterIndex == 1)
4135 Current->SplitPenalty += 5 * Current->BindingStrength;
4137 Current->SplitPenalty += 20 * Current->BindingStrength;
4141 calculateUnbreakableTailLengths(
Line);
4142 unsigned IndentLevel =
Line.Level;
4143 for (
auto *Current =
First; Current; Current = Current->Next) {
4145 Current->Role->precomputeFormattingInfos(Current);
4146 if (Current->MatchingParen &&
4147 Current->MatchingParen->opensBlockOrBlockTypeList(Style) &&
4151 Current->IndentLevel = IndentLevel;
4152 if (Current->opensBlockOrBlockTypeList(Style))
4156 LLVM_DEBUG({ printDebugInfo(
Line); });
4159void TokenAnnotator::calculateUnbreakableTailLengths(
4161 unsigned UnbreakableTailLength = 0;
4164 Current->UnbreakableTailLength = UnbreakableTailLength;
4165 if (Current->CanBreakBefore ||
4166 Current->isOneOf(tok::comment, tok::string_literal)) {
4167 UnbreakableTailLength = 0;
4169 UnbreakableTailLength +=
4170 Current->ColumnWidth + Current->SpacesRequiredBefore;
4172 Current = Current->Previous;
4176void TokenAnnotator::calculateArrayInitializerColumnList(
4177 AnnotatedLine &
Line)
const {
4180 auto *CurrentToken =
Line.First;
4181 CurrentToken->ArrayInitializerLineStart =
true;
4183 while (CurrentToken && CurrentToken !=
Line.Last) {
4184 if (CurrentToken->is(tok::l_brace)) {
4185 CurrentToken->IsArrayInitializer =
true;
4186 if (CurrentToken->Next)
4187 CurrentToken->Next->MustBreakBefore =
true;
4189 calculateInitializerColumnList(
Line, CurrentToken->Next, Depth + 1);
4191 CurrentToken = CurrentToken->Next;
4196FormatToken *TokenAnnotator::calculateInitializerColumnList(
4197 AnnotatedLine &
Line, FormatToken *CurrentToken,
unsigned Depth)
const {
4198 while (CurrentToken && CurrentToken !=
Line.Last) {
4199 if (CurrentToken->is(tok::l_brace))
4201 else if (CurrentToken->is(tok::r_brace))
4203 if (Depth == 2 && CurrentToken->isOneOf(tok::l_brace, tok::comma)) {
4204 CurrentToken = CurrentToken->Next;
4207 CurrentToken->StartsColumn =
true;
4208 CurrentToken = CurrentToken->Previous;
4210 CurrentToken = CurrentToken->Next;
4212 return CurrentToken;
4215unsigned TokenAnnotator::splitPenalty(
const AnnotatedLine &
Line,
4216 const FormatToken &Tok,
4217 bool InFunctionDecl)
const {
4218 const FormatToken &
Left = *Tok.Previous;
4219 const FormatToken &
Right = Tok;
4221 if (
Left.is(tok::semi))
4226 if (
Right.isOneOf(Keywords.kw_extends, Keywords.kw_throws))
4228 if (
Right.is(Keywords.kw_implements))
4230 if (
Left.is(tok::comma) &&
Left.NestingLevel == 0)
4232 }
else if (Style.isJavaScript()) {
4233 if (
Right.is(Keywords.kw_function) &&
Left.isNot(tok::comma))
4235 if (
Left.is(TT_JsTypeColon))
4237 if ((
Left.is(TT_TemplateString) &&
Left.TokenText.ends_with(
"${")) ||
4238 (
Right.is(TT_TemplateString) &&
Right.TokenText.starts_with(
"}"))) {
4242 if (
Left.opensScope() &&
Right.closesScope())
4245 if (
Right.is(tok::l_square))
4247 if (
Right.is(tok::period))
4251 if (
Right.is(tok::identifier) &&
Right.Next &&
Right.Next->is(TT_DictLiteral))
4253 if (
Right.is(tok::l_square)) {
4254 if (
Left.is(tok::r_square))
4257 if (
Right.is(TT_LambdaLSquare) &&
Left.is(tok::equal))
4259 if (!
Right.isOneOf(TT_ObjCMethodExpr, TT_LambdaLSquare,
4260 TT_ArrayInitializerLSquare,
4261 TT_DesignatedInitializerLSquare, TT_AttributeSquare)) {
4266 if (
Left.is(tok::coloncolon))
4267 return Style.PenaltyBreakScopeResolution;
4268 if (
Right.isOneOf(TT_StartOfName, TT_FunctionDeclarationName) ||
4269 Right.is(tok::kw_operator)) {
4270 if (
Line.startsWith(tok::kw_for) &&
Right.PartOfMultiVariableDeclStmt)
4272 if (
Left.is(TT_StartOfName))
4274 if (InFunctionDecl &&
Right.NestingLevel == 0)
4275 return Style.PenaltyReturnTypeOnItsOwnLine;
4278 if (
Right.is(TT_PointerOrReference))
4280 if (
Right.is(TT_LambdaArrow))
4282 if (
Left.is(tok::equal) &&
Right.is(tok::l_brace))
4284 if (
Left.is(TT_CastRParen))
4286 if (
Left.isOneOf(tok::kw_class, tok::kw_struct, tok::kw_union))
4288 if (
Left.is(tok::comment))
4291 if (
Left.isOneOf(TT_RangeBasedForLoopColon, TT_InheritanceColon,
4292 TT_CtorInitializerColon)) {
4296 if (
Right.isMemberAccess()) {
4316 const auto *NextOperator =
Right.NextOperator;
4317 const auto Penalty = Style.PenaltyBreakBeforeMemberAccess;
4318 return NextOperator && NextOperator->Previous->closesScope()
4319 ? std::min(Penalty, 35u)
4323 if (
Right.is(TT_TrailingAnnotation) &&
4324 (!
Right.Next ||
Right.Next->isNot(tok::l_paren))) {
4327 if (
Line.startsWith(TT_ObjCMethodSpecifier))
4334 bool is_short_annotation =
Right.TokenText.size() < 10;
4335 return (
Left.is(tok::r_paren) ? 100 : 120) + (is_short_annotation ? 50 : 0);
4339 if (
Line.startsWith(tok::kw_for) &&
Left.is(tok::equal))
4344 if (
Right.is(TT_SelectorName))
4346 if (
Left.is(tok::colon) &&
Left.is(TT_ObjCMethodExpr))
4347 return Line.MightBeFunctionDecl ? 50 : 500;
4353 Left.Previous->isOneOf(tok::identifier, tok::greater)) {
4357 if (
Left.is(tok::l_paren) && Style.PenaltyBreakOpenParenthesis != 0)
4358 return Style.PenaltyBreakOpenParenthesis;
4359 if (
Left.is(tok::l_paren) && InFunctionDecl &&
4363 if (
Left.is(tok::l_paren) &&
Left.Previous &&
4364 (
Left.Previous->isOneOf(tok::kw_for, tok::kw__Generic) ||
4365 Left.Previous->isIf())) {
4368 if (
Left.is(tok::equal) && InFunctionDecl)
4370 if (
Right.is(tok::r_brace))
4372 if (
Left.is(TT_TemplateOpener))
4374 if (
Left.opensScope()) {
4379 (
Left.ParameterCount <= 1 || Style.AllowAllArgumentsOnNextLine)) {
4382 if (
Left.is(tok::l_brace) && !Style.Cpp11BracedListStyle)
4384 return Left.ParameterCount > 1 ? Style.PenaltyBreakBeforeFirstCallParameter
4387 if (
Left.is(TT_JavaAnnotation))
4390 if (
Left.is(TT_UnaryOperator))
4392 if (
Left.isOneOf(tok::plus, tok::comma) &&
Left.Previous &&
4393 Left.Previous->isLabelString() &&
4394 (
Left.NextOperator ||
Left.OperatorIndex != 0)) {
4397 if (
Right.is(tok::plus) &&
Left.isLabelString() &&
4398 (
Right.NextOperator ||
Right.OperatorIndex != 0)) {
4401 if (
Left.is(tok::comma))
4403 if (
Right.is(tok::lessless) &&
Left.isLabelString() &&
4404 (
Right.NextOperator ||
Right.OperatorIndex != 1)) {
4407 if (
Right.is(tok::lessless)) {
4409 if (
Left.isNot(tok::r_paren) ||
Right.OperatorIndex > 0) {
4415 if (
Left.ClosesTemplateDeclaration)
4416 return Style.PenaltyBreakTemplateDeclaration;
4417 if (
Left.ClosesRequiresClause)
4419 if (
Left.is(TT_ConditionalExpr))
4425 return Style.PenaltyBreakAssignment;
4432bool TokenAnnotator::spaceRequiredBeforeParens(
const FormatToken &Right)
const {
4435 if (
Right.is(TT_OverloadedOperatorLParen) &&
4436 Style.SpaceBeforeParensOptions.AfterOverloadedOperator) {
4439 if (Style.SpaceBeforeParensOptions.BeforeNonEmptyParentheses &&
4440 Right.ParameterCount > 0) {
4446bool TokenAnnotator::spaceRequiredBetween(
const AnnotatedLine &
Line,
4447 const FormatToken &Left,
4448 const FormatToken &Right)
const {
4449 if (
Left.is(tok::kw_return) &&
4450 !
Right.isOneOf(tok::semi, tok::r_paren, tok::hashhash)) {
4453 if (
Left.is(tok::kw_throw) &&
Right.is(tok::l_paren) &&
Right.MatchingParen &&
4454 Right.MatchingParen->is(TT_CastRParen)) {
4460 Left.Tok.getObjCKeywordID() == tok::objc_property) {
4463 if (
Right.is(tok::hashhash))
4464 return Left.is(tok::hash);
4465 if (
Left.isOneOf(tok::hashhash, tok::hash))
4466 return Right.is(tok::hash);
4468 Right.MatchingParen == &Left &&
Line.Children.empty()) {
4469 return Style.SpaceInEmptyBlock;
4472 if ((
Left.is(tok::l_paren) &&
Right.is(tok::r_paren)) ||
4475 return Style.SpacesInParensOptions.InEmptyParentheses;
4477 if (Style.SpacesInParensOptions.ExceptDoubleParentheses &&
4478 Left.is(tok::r_paren) &&
Right.is(tok::r_paren)) {
4479 auto *InnerLParen =
Left.MatchingParen;
4480 if (InnerLParen && InnerLParen->Previous ==
Right.MatchingParen) {
4481 InnerLParen->SpacesRequiredBefore = 0;
4485 const FormatToken *LeftParen =
nullptr;
4486 if (
Left.is(tok::l_paren))
4488 else if (
Right.is(tok::r_paren) &&
Right.MatchingParen)
4489 LeftParen =
Right.MatchingParen;
4490 if (LeftParen && (LeftParen->is(TT_ConditionLParen) ||
4491 (LeftParen->Previous &&
4492 isKeywordWithCondition(*LeftParen->Previous)))) {
4493 return Style.SpacesInParensOptions.InConditionalStatements;
4498 if (
Left.is(tok::kw_auto) &&
Right.isOneOf(TT_LambdaLBrace, TT_FunctionLBrace,
4500 TT_FunctionTypeLParen)) {
4505 if (
Left.is(tok::kw_auto) &&
Right.isOneOf(tok::l_paren, tok::l_brace))
4508 const auto *BeforeLeft =
Left.Previous;
4511 if (
Right.is(tok::l_paren) &&
Left.is(tok::kw_co_await) && BeforeLeft &&
4512 BeforeLeft->is(tok::kw_operator)) {
4516 if (
Left.isOneOf(tok::kw_co_await, tok::kw_co_yield, tok::kw_co_return) &&
4517 !
Right.isOneOf(tok::semi, tok::r_paren)) {
4521 if (
Left.is(tok::l_paren) ||
Right.is(tok::r_paren)) {
4522 return (
Right.is(TT_CastRParen) ||
4523 (
Left.MatchingParen &&
Left.MatchingParen->is(TT_CastRParen)))
4524 ? Style.SpacesInParensOptions.InCStyleCasts
4525 : Style.SpacesInParensOptions.Other;
4527 if (
Right.isOneOf(tok::semi, tok::comma))
4530 bool IsLightweightGeneric =
Right.MatchingParen &&
4531 Right.MatchingParen->Next &&
4532 Right.MatchingParen->Next->is(tok::colon);
4533 return !IsLightweightGeneric && Style.ObjCSpaceBeforeProtocolList;
4535 if (
Right.is(tok::less) &&
Left.is(tok::kw_template))
4536 return Style.SpaceAfterTemplateKeyword;
4537 if (
Left.isOneOf(tok::exclaim, tok::tilde))
4539 if (
Left.is(tok::at) &&
4540 Right.isOneOf(tok::identifier, tok::string_literal, tok::char_constant,
4541 tok::numeric_constant, tok::l_paren, tok::l_brace,
4542 tok::kw_true, tok::kw_false)) {
4545 if (
Left.is(tok::colon))
4546 return Left.isNot(TT_ObjCMethodExpr);
4547 if (
Left.is(tok::coloncolon))
4549 if (
Left.is(tok::less) ||
Right.isOneOf(tok::greater, tok::less)) {
4552 (
Left.is(TT_DictLiteral) ||
Right.is(TT_DictLiteral)))) {
4554 if (
Left.is(tok::less) &&
Right.is(tok::greater))
4556 return !Style.Cpp11BracedListStyle;
4559 if (
Right.isNot(TT_OverloadedOperatorLParen))
4562 if (
Right.is(tok::ellipsis)) {
4563 return Left.Tok.isLiteral() || (
Left.is(tok::identifier) && BeforeLeft &&
4564 BeforeLeft->is(tok::kw_case));
4566 if (
Left.is(tok::l_square) &&
Right.is(tok::amp))
4567 return Style.SpacesInSquareBrackets;
4568 if (
Right.is(TT_PointerOrReference)) {
4569 if (
Left.is(tok::r_paren) &&
Line.MightBeFunctionDecl) {
4570 if (!
Left.MatchingParen)
4572 FormatToken *TokenBeforeMatchingParen =
4573 Left.MatchingParen->getPreviousNonComment();
4574 if (!TokenBeforeMatchingParen ||
Left.isNot(TT_TypeDeclarationParen))
4582 (
Left.is(TT_AttributeRParen) ||
4583 Left.canBePointerOrReferenceQualifier())) {
4586 if (
Left.Tok.isLiteral())
4589 if (
Left.isTypeOrIdentifier(LangOpts) &&
Right.Next &&
Right.Next->Next &&
4590 Right.Next->Next->is(TT_RangeBasedForLoopColon)) {
4591 return getTokenPointerOrReferenceAlignment(Right) !=
4594 return !
Left.isOneOf(TT_PointerOrReference, tok::l_paren) &&
4595 (getTokenPointerOrReferenceAlignment(Right) !=
4597 (
Line.IsMultiVariableDeclStmt &&
4598 (
Left.NestingLevel == 0 ||
4599 (
Left.NestingLevel == 1 && startsWithInitStatement(
Line)))));
4601 if (
Right.is(TT_FunctionTypeLParen) &&
Left.isNot(tok::l_paren) &&
4602 (
Left.isNot(TT_PointerOrReference) ||
4604 !
Line.IsMultiVariableDeclStmt))) {
4607 if (
Left.is(TT_PointerOrReference)) {
4612 Right.canBePointerOrReferenceQualifier()) {
4616 if (
Right.Tok.isLiteral())
4619 if (
Right.is(TT_BlockComment))
4623 if (
Right.isOneOf(Keywords.kw_override, Keywords.kw_final, tok::kw_noexcept,
4624 TT_RequiresClause) &&
4625 Right.isNot(TT_StartOfName)) {
4632 if (BeforeLeft && BeforeLeft->isTypeOrIdentifier(LangOpts) &&
Right.Next &&
4633 Right.Next->is(TT_RangeBasedForLoopColon)) {
4634 return getTokenPointerOrReferenceAlignment(Left) !=
4637 if (
Right.isOneOf(TT_PointerOrReference, TT_ArraySubscriptLSquare,
4649 if (
Line.IsMultiVariableDeclStmt &&
4650 (
Left.NestingLevel ==
Line.First->NestingLevel ||
4651 ((
Left.NestingLevel ==
Line.First->NestingLevel + 1) &&
4652 startsWithInitStatement(
Line)))) {
4657 if (BeforeLeft->is(tok::coloncolon)) {
4658 if (
Left.isNot(tok::star))
4661 if (!
Right.startsSequence(tok::identifier, tok::r_paren))
4664 const auto *LParen =
Right.Next->MatchingParen;
4665 return !LParen || LParen->isNot(TT_FunctionTypeLParen);
4667 return !BeforeLeft->isOneOf(tok::l_paren, tok::l_square);
4670 if (
Left.is(tok::ellipsis) && BeforeLeft &&
4671 BeforeLeft->isPointerOrReference()) {
4675 if (
Right.is(tok::star) &&
Left.is(tok::l_paren))
4677 if (
Left.is(tok::star) &&
Right.isPointerOrReference())
4679 if (
Right.isPointerOrReference()) {
4690 if (
Previous->is(tok::coloncolon)) {
4709 if (
Previous->endsSequence(tok::kw_operator))
4713 (Style.SpaceAroundPointerQualifiers ==
4719 if (Style.isCSharp() &&
Left.is(Keywords.kw_is) &&
Right.is(tok::l_square))
4721 const auto SpaceRequiredForArrayInitializerLSquare =
4722 [](
const FormatToken &LSquareTok,
const FormatStyle &Style) {
4723 return Style.SpacesInContainerLiterals ||
4724 (Style.isProto() && !Style.Cpp11BracedListStyle &&
4725 LSquareTok.endsSequence(tok::l_square, tok::colon,
4728 if (
Left.is(tok::l_square)) {
4729 return (
Left.is(TT_ArrayInitializerLSquare) &&
Right.isNot(tok::r_square) &&
4730 SpaceRequiredForArrayInitializerLSquare(Left, Style)) ||
4731 (
Left.isOneOf(TT_ArraySubscriptLSquare, TT_StructuredBindingLSquare,
4732 TT_LambdaLSquare) &&
4733 Style.SpacesInSquareBrackets &&
Right.isNot(tok::r_square));
4735 if (
Right.is(tok::r_square)) {
4736 return Right.MatchingParen &&
4737 ((
Right.MatchingParen->is(TT_ArrayInitializerLSquare) &&
4738 SpaceRequiredForArrayInitializerLSquare(*
Right.MatchingParen,
4740 (Style.SpacesInSquareBrackets &&
4741 Right.MatchingParen->isOneOf(TT_ArraySubscriptLSquare,
4742 TT_StructuredBindingLSquare,
4743 TT_LambdaLSquare)));
4745 if (
Right.is(tok::l_square) &&
4746 !
Right.isOneOf(TT_ObjCMethodExpr, TT_LambdaLSquare,
4747 TT_DesignatedInitializerLSquare,
4748 TT_StructuredBindingLSquare, TT_AttributeSquare) &&
4749 !
Left.isOneOf(tok::numeric_constant, TT_DictLiteral) &&
4750 !(
Left.isNot(tok::r_square) && Style.SpaceBeforeSquareBrackets &&
4751 Right.is(TT_ArraySubscriptLSquare))) {
4754 if (
Left.is(tok::l_brace) &&
Right.is(tok::r_brace))
4755 return !
Left.Children.empty();
4757 (
Right.is(tok::r_brace) &&
Right.MatchingParen &&
4759 return !Style.Cpp11BracedListStyle || Style.SpacesInParensOptions.Other;
4761 if (
Left.is(TT_BlockComment)) {
4763 return Style.isJavaScript() || !
Left.TokenText.ends_with(
"=*/");
4768 if (
Left.is(TT_TemplateCloser) &&
Right.is(TT_AttributeSquare))
4771 if (
Right.is(tok::l_paren)) {
4772 if (
Left.is(TT_TemplateCloser) &&
Right.isNot(TT_FunctionTypeLParen))
4773 return spaceRequiredBeforeParens(Right);
4774 if (
Left.isOneOf(TT_RequiresClause,
4775 TT_RequiresClauseInARequiresExpression)) {
4776 return Style.SpaceBeforeParensOptions.AfterRequiresInClause ||
4777 spaceRequiredBeforeParens(Right);
4779 if (
Left.is(TT_RequiresExpression)) {
4780 return Style.SpaceBeforeParensOptions.AfterRequiresInExpression ||
4781 spaceRequiredBeforeParens(Right);
4783 if (
Left.is(TT_AttributeRParen) ||
4784 (
Left.is(tok::r_square) &&
Left.is(TT_AttributeSquare))) {
4787 if (
Left.is(TT_ForEachMacro)) {
4788 return Style.SpaceBeforeParensOptions.AfterForeachMacros ||
4789 spaceRequiredBeforeParens(Right);
4791 if (
Left.is(TT_IfMacro)) {
4792 return Style.SpaceBeforeParensOptions.AfterIfMacros ||
4793 spaceRequiredBeforeParens(Right);
4796 Left.isOneOf(tok::kw_new, tok::kw_delete) &&
4797 Right.isNot(TT_OverloadedOperatorLParen) &&
4798 !(
Line.MightBeFunctionDecl &&
Left.is(TT_FunctionDeclarationName))) {
4799 const auto *RParen =
Right.MatchingParen;
4800 return Style.SpaceBeforeParensOptions.AfterPlacementOperator ||
4801 (RParen && RParen->is(TT_CastRParen));
4805 if (
Left.is(tok::semi))
4807 if (
Left.isOneOf(tok::pp_elif, tok::kw_for, tok::kw_while, tok::kw_switch,
4808 tok::kw_case, TT_ForEachMacro, TT_ObjCForIn) ||
4810 Right.is(TT_ConditionLParen)) {
4811 return Style.SpaceBeforeParensOptions.AfterControlStatements ||
4812 spaceRequiredBeforeParens(Right);
4817 if (
Right.is(TT_OverloadedOperatorLParen))
4818 return spaceRequiredBeforeParens(Right);
4820 if (
Line.MightBeFunctionDecl &&
Right.is(TT_FunctionDeclarationLParen)) {
4821 if (spaceRequiredBeforeParens(Right))
4823 const auto &Options = Style.SpaceBeforeParensOptions;
4824 return Line.mightBeFunctionDefinition()
4825 ? Options.AfterFunctionDefinitionName
4826 : Options.AfterFunctionDeclarationName;
4830 Left.MatchingParen &&
Left.MatchingParen->is(TT_LambdaLSquare)) {
4831 return Style.SpaceBeforeParensOptions.AfterFunctionDefinitionName ||
4832 spaceRequiredBeforeParens(Right);
4834 if (!BeforeLeft || !BeforeLeft->isOneOf(tok::period, tok::arrow)) {
4835 if (
Left.isOneOf(tok::kw_try, Keywords.kw___except, tok::kw_catch)) {
4836 return Style.SpaceBeforeParensOptions.AfterControlStatements ||
4837 spaceRequiredBeforeParens(Right);
4839 if (
Left.isOneOf(tok::kw_new, tok::kw_delete)) {
4840 return ((!
Line.MightBeFunctionDecl || !BeforeLeft) &&
4842 spaceRequiredBeforeParens(Right);
4845 if (
Left.is(tok::r_square) &&
Left.MatchingParen &&
4846 Left.MatchingParen->Previous &&
4847 Left.MatchingParen->Previous->is(tok::kw_delete)) {
4849 spaceRequiredBeforeParens(Right);
4854 (
Left.Tok.getIdentifierInfo() ||
Left.is(tok::r_paren))) {
4855 return spaceRequiredBeforeParens(Right);
4859 if (
Left.is(tok::at) &&
Right.Tok.getObjCKeywordID() != tok::objc_not_keyword)
4861 if (
Right.is(TT_UnaryOperator)) {
4862 return !
Left.isOneOf(tok::l_paren, tok::l_square, tok::at) &&
4863 (
Left.isNot(tok::colon) ||
Left.isNot(TT_ObjCMethodExpr));
4869 if (!Style.isVerilog() &&
4870 (
Left.isOneOf(tok::identifier, tok::greater, tok::r_square,
4872 Left.isTypeName(LangOpts)) &&
4873 Right.is(tok::l_brace) &&
Right.getNextNonComment() &&
4877 if (
Left.is(tok::period) ||
Right.is(tok::period))
4881 if (
Right.is(tok::hash) &&
Left.is(tok::identifier) &&
4882 (
Left.TokenText ==
"L" ||
Left.TokenText ==
"u" ||
4883 Left.TokenText ==
"U" ||
Left.TokenText ==
"u8" ||
4884 Left.TokenText ==
"LR" ||
Left.TokenText ==
"uR" ||
4885 Left.TokenText ==
"UR" ||
Left.TokenText ==
"u8R")) {
4888 if (
Left.is(TT_TemplateCloser) &&
Left.MatchingParen &&
4889 Left.MatchingParen->Previous &&
4890 (
Left.MatchingParen->Previous->is(tok::period) ||
4891 Left.MatchingParen->Previous->is(tok::coloncolon))) {
4897 if (
Left.is(TT_TemplateCloser) &&
Right.is(tok::l_square))
4899 if (
Left.is(tok::l_brace) &&
Left.endsSequence(TT_DictLiteral, tok::at)) {
4903 if (
Right.is(tok::r_brace) &&
Right.MatchingParen &&
4904 Right.MatchingParen->endsSequence(TT_DictLiteral, tok::at)) {
4908 if (
Right.is(TT_TrailingAnnotation) &&
Right.isOneOf(tok::amp, tok::ampamp) &&
4909 Left.isOneOf(tok::kw_const, tok::kw_volatile) &&
4910 (!
Right.Next ||
Right.Next->is(tok::semi))) {
4920bool TokenAnnotator::spaceRequiredBefore(
const AnnotatedLine &
Line,
4921 const FormatToken &Right)
const {
4922 const FormatToken &
Left = *
Right.Previous;
4927 return Right.hasWhitespaceBefore();
4929 const bool IsVerilog = Style.isVerilog();
4930 assert(!IsVerilog || !IsCpp);
4933 if (Keywords.isWordLike(Right, IsVerilog) &&
4934 Keywords.isWordLike(Left, IsVerilog)) {
4940 if (
Left.is(tok::star) &&
Right.is(tok::comment))
4943 const auto *BeforeLeft =
Left.Previous;
4946 if (
Left.is(TT_OverloadedOperator) &&
4947 Right.isOneOf(TT_TemplateOpener, TT_TemplateCloser)) {
4951 if (
Right.is(tok::period) &&
Left.is(tok::numeric_constant))
4955 if (
Left.is(Keywords.kw_import) &&
Right.isOneOf(tok::less, tok::ellipsis))
4958 if (
Left.isOneOf(Keywords.kw_module, Keywords.kw_import) &&
4959 Right.is(TT_ModulePartitionColon)) {
4963 if (
Right.is(TT_AfterPPDirective))
4967 if (
Left.is(tok::identifier) &&
Right.is(TT_ModulePartitionColon))
4970 if (
Left.is(TT_ModulePartitionColon) &&
4971 Right.isOneOf(tok::identifier, tok::kw_private)) {
4974 if (
Left.is(tok::ellipsis) &&
Right.is(tok::identifier) &&
4975 Line.First->is(Keywords.kw_import)) {
4979 if (
Left.isOneOf(TT_AttributeRParen, TT_AttributeMacro) &&
4980 Right.is(tok::coloncolon)) {
4984 if (
Left.is(tok::kw_operator))
4985 return Right.is(tok::coloncolon);
4987 !
Left.opensScope() && Style.SpaceBeforeCpp11BracedList) {
4990 if (
Left.is(tok::less) &&
Left.is(TT_OverloadedOperator) &&
4991 Right.is(TT_TemplateOpener)) {
4995 if (
Left.is(tok::identifier) &&
Right.is(tok::numeric_constant))
4996 return Right.TokenText[0] !=
'.';
4998 if (
Left.Tok.getIdentifierInfo() &&
Right.Tok.isLiteral())
5000 }
else if (Style.isProto()) {
5001 if (
Right.is(tok::period) && !(BeforeLeft && BeforeLeft->is(tok::period)) &&
5002 Left.isOneOf(Keywords.kw_optional, Keywords.kw_required,
5003 Keywords.kw_repeated, Keywords.kw_extend)) {
5006 if (
Right.is(tok::l_paren) &&
5007 Left.isOneOf(Keywords.kw_returns, Keywords.kw_option)) {
5010 if (
Right.isOneOf(tok::l_brace, tok::less) &&
Left.is(TT_SelectorName))
5013 if (
Left.is(tok::slash) ||
Right.is(tok::slash))
5015 if (
Left.MatchingParen &&
5016 Left.MatchingParen->is(TT_ProtoExtensionLSquare) &&
5017 Right.isOneOf(tok::l_brace, tok::less)) {
5018 return !Style.Cpp11BracedListStyle;
5021 if (
Left.is(tok::percent))
5025 if (
Left.is(tok::numeric_constant) &&
Right.is(tok::percent))
5026 return Right.hasWhitespaceBefore();
5027 }
else if (Style.isJson()) {
5028 if (
Right.is(tok::colon) &&
Left.is(tok::string_literal))
5029 return Style.SpaceBeforeJsonColon;
5030 }
else if (Style.isCSharp()) {
5036 if (
Left.is(tok::kw_this) &&
Right.is(tok::l_square))
5040 if (
Left.is(tok::kw_new) &&
Right.is(tok::l_paren))
5044 if (
Right.is(tok::l_brace))
5048 if (
Left.is(tok::l_brace) &&
Right.isNot(tok::r_brace))
5051 if (
Left.isNot(tok::l_brace) &&
Right.is(tok::r_brace))
5055 if (
Left.is(TT_FatArrow) ||
Right.is(TT_FatArrow))
5059 if (
Left.is(TT_AttributeColon) ||
Right.is(TT_AttributeColon))
5063 if (
Left.is(TT_TemplateCloser) &&
Right.is(TT_StartOfName))
5067 if (
Left.is(tok::l_square) ||
Right.is(tok::r_square))
5068 return Style.SpacesInSquareBrackets;
5071 if (
Right.is(TT_CSharpNullable))
5075 if (
Right.is(TT_NonNullAssertion))
5079 if (
Left.is(tok::comma) &&
Right.is(tok::comma))
5083 if (
Left.is(Keywords.kw_var) &&
Right.is(tok::l_paren))
5087 if (
Right.is(tok::l_paren)) {
5088 if (
Left.isOneOf(tok::kw_using, Keywords.kw_async, Keywords.kw_when,
5089 Keywords.kw_lock)) {
5090 return Style.SpaceBeforeParensOptions.AfterControlStatements ||
5091 spaceRequiredBeforeParens(Right);
5097 if ((
Left.isAccessSpecifierKeyword() ||
5098 Left.isOneOf(tok::kw_virtual, tok::kw_extern, tok::kw_static,
5099 Keywords.kw_internal, Keywords.kw_abstract,
5100 Keywords.kw_sealed, Keywords.kw_override,
5101 Keywords.kw_async, Keywords.kw_unsafe)) &&
5102 Right.is(tok::l_paren)) {
5105 }
else if (Style.isJavaScript()) {
5106 if (
Left.is(TT_FatArrow))
5109 if (
Right.is(tok::l_paren) &&
Left.is(Keywords.kw_await) && BeforeLeft &&
5110 BeforeLeft->is(tok::kw_for)) {
5113 if (
Left.is(Keywords.kw_async) &&
Right.is(tok::l_paren) &&
5114 Right.MatchingParen) {
5115 const FormatToken *Next =
Right.MatchingParen->getNextNonComment();
5118 if (Next && Next->is(TT_FatArrow))
5121 if ((
Left.is(TT_TemplateString) &&
Left.TokenText.ends_with(
"${")) ||
5122 (
Right.is(TT_TemplateString) &&
Right.TokenText.starts_with(
"}"))) {
5127 if (Keywords.isJavaScriptIdentifier(Left,
5129 Right.is(TT_TemplateString)) {
5132 if (
Right.is(tok::star) &&
5133 Left.isOneOf(Keywords.kw_function, Keywords.kw_yield)) {
5136 if (
Right.isOneOf(tok::l_brace, tok::l_square) &&
5137 Left.isOneOf(Keywords.kw_function, Keywords.kw_yield,
5138 Keywords.kw_extends, Keywords.kw_implements)) {
5141 if (
Right.is(tok::l_paren)) {
5143 if (
Line.MustBeDeclaration &&
Left.Tok.getIdentifierInfo())
5147 if (BeforeLeft && BeforeLeft->is(tok::period) &&
5148 Left.Tok.getIdentifierInfo()) {
5152 if (
Left.isOneOf(tok::kw_throw, Keywords.kw_await, Keywords.kw_typeof,
5158 if (
Left.endsSequence(tok::kw_const, Keywords.kw_as))
5160 if ((
Left.isOneOf(Keywords.kw_let, Keywords.kw_var, Keywords.kw_in,
5165 (
Left.is(Keywords.kw_of) && BeforeLeft &&
5166 (BeforeLeft->is(tok::identifier) ||
5167 BeforeLeft->isOneOf(tok::r_square, tok::r_brace)))) &&
5168 (!BeforeLeft || BeforeLeft->isNot(tok::period))) {
5171 if (
Left.isOneOf(tok::kw_for, Keywords.kw_as) && BeforeLeft &&
5172 BeforeLeft->is(tok::period) &&
Right.is(tok::l_paren)) {
5175 if (
Left.is(Keywords.kw_as) &&
5176 Right.isOneOf(tok::l_square, tok::l_brace, tok::l_paren)) {
5179 if (
Left.is(tok::kw_default) && BeforeLeft &&
5180 BeforeLeft->is(tok::kw_export)) {
5183 if (
Left.is(Keywords.kw_is) &&
Right.is(tok::l_brace))
5185 if (
Right.isOneOf(TT_JsTypeColon, TT_JsTypeOptionalQuestion))
5187 if (
Left.is(TT_JsTypeOperator) ||
Right.is(TT_JsTypeOperator))
5189 if ((
Left.is(tok::l_brace) ||
Right.is(tok::r_brace)) &&
5190 Line.First->isOneOf(Keywords.kw_import, tok::kw_export)) {
5193 if (
Left.is(tok::ellipsis))
5195 if (
Left.is(TT_TemplateCloser) &&
5196 !
Right.isOneOf(tok::equal, tok::l_brace, tok::comma, tok::l_square,
5197 Keywords.kw_implements, Keywords.kw_extends)) {
5203 if (
Right.is(TT_NonNullAssertion))
5205 if (
Left.is(TT_NonNullAssertion) &&
5206 Right.isOneOf(Keywords.kw_as, Keywords.kw_in)) {
5210 if (
Left.is(TT_CaseLabelArrow) ||
Right.is(TT_CaseLabelArrow))
5212 if (
Left.is(tok::r_square) &&
Right.is(tok::l_brace))
5215 if (
Left.is(tok::l_square) ||
Right.is(tok::r_square))
5216 return Style.SpacesInSquareBrackets;
5218 if (
Left.is(Keywords.kw_synchronized) &&
Right.is(tok::l_paren)) {
5219 return Style.SpaceBeforeParensOptions.AfterControlStatements ||
5220 spaceRequiredBeforeParens(Right);
5222 if ((
Left.isAccessSpecifierKeyword() ||
5223 Left.isOneOf(tok::kw_static, Keywords.kw_final, Keywords.kw_abstract,
5224 Keywords.kw_native)) &&
5225 Right.is(TT_TemplateOpener)) {
5228 }
else if (IsVerilog) {
5230 if (
Left.is(tok::identifier) &&
Left.TokenText[0] ==
'\\')
5234 if ((
Left.is(TT_VerilogTableItem) &&
5235 !
Right.isOneOf(tok::r_paren, tok::semi)) ||
5236 (
Right.is(TT_VerilogTableItem) &&
Left.isNot(tok::l_paren))) {
5237 const FormatToken *Next =
Right.getNextNonComment();
5238 return !(Next && Next->is(tok::r_paren));
5241 if (
Left.isNot(TT_BinaryOperator) &&
5242 Left.isOneOf(Keywords.kw_verilogHash, Keywords.kw_verilogHashHash)) {
5246 if (
Right.isNot(tok::semi) &&
5247 (
Left.endsSequence(tok::numeric_constant, Keywords.kw_verilogHash) ||
5248 Left.endsSequence(tok::numeric_constant,
5249 Keywords.kw_verilogHashHash) ||
5250 (
Left.is(tok::r_paren) &&
Left.MatchingParen &&
5251 Left.MatchingParen->endsSequence(tok::l_paren, tok::at)))) {
5256 if (
Left.is(Keywords.kw_apostrophe) ||
5257 (
Left.is(TT_VerilogNumberBase) &&
Right.is(tok::numeric_constant))) {
5261 if (
Left.is(tok::arrow) ||
Right.is(tok::arrow))
5266 if (
Left.is(tok::at) &&
Right.isOneOf(tok::l_paren, tok::star, tok::at))
5269 if (
Right.is(tok::l_square) &&
5270 Left.isOneOf(TT_VerilogDimensionedTypeName, Keywords.kw_function)) {
5274 if (
Right.isOneOf(tok::period, Keywords.kw_apostrophe) &&
5275 Keywords.isVerilogIdentifier(Left) &&
Left.getPreviousNonComment() &&
5276 Left.getPreviousNonComment()->is(Keywords.kw_tagged)) {
5282 if ((
Right.is(Keywords.kw_apostrophe) ||
5284 !(
Left.isOneOf(Keywords.kw_assign, Keywords.kw_unique) ||
5285 Keywords.isVerilogWordOperator(Left)) &&
5286 (
Left.isOneOf(tok::r_square, tok::r_paren, tok::r_brace,
5287 tok::numeric_constant) ||
5288 Keywords.isWordLike(Left))) {
5292 if ((
Right.is(tok::star) &&
Left.is(tok::coloncolon)) ||
5293 (
Left.is(tok::star) &&
Right.is(tok::semi))) {
5297 if (
Left.endsSequence(tok::star, tok::l_paren) &&
Right.is(tok::identifier))
5300 if (
Right.is(tok::l_paren) &&
Right.is(TT_VerilogStrength))
5303 if ((
Left.is(tok::l_brace) &&
5304 Right.isOneOf(tok::lessless, tok::greatergreater)) ||
5305 (
Left.endsSequence(tok::lessless, tok::l_brace) ||
5306 Left.endsSequence(tok::greatergreater, tok::l_brace))) {
5309 }
else if (Style.isTableGen()) {
5311 if (
Left.is(tok::l_square) &&
Right.is(tok::l_brace))
5313 if (
Left.is(tok::r_brace) &&
Right.is(tok::r_square))
5316 if (
Right.isOneOf(TT_TableGenDAGArgListColon,
5317 TT_TableGenDAGArgListColonToAlign) ||
5318 Left.isOneOf(TT_TableGenDAGArgListColon,
5319 TT_TableGenDAGArgListColonToAlign)) {
5322 if (
Right.is(TT_TableGenCondOperatorColon))
5324 if (
Left.isOneOf(TT_TableGenDAGArgOperatorID,
5325 TT_TableGenDAGArgOperatorToBreak) &&
5326 Right.isNot(TT_TableGenDAGArgCloser)) {
5330 if (
Right.isOneOf(tok::l_paren, tok::less) &&
5331 Left.isOneOf(TT_TableGenBangOperator, TT_TableGenCondOperator)) {
5336 if (
Left.is(TT_TableGenTrailingPasteOperator) &&
5337 Right.isOneOf(tok::l_brace, tok::colon)) {
5341 if (
Left.is(tok::hash) ||
Right.is(tok::hash))
5344 if (Keywords.isTableGenDefinition(Left))
5348 if (
Left.is(TT_ImplicitStringLiteral))
5349 return Right.hasWhitespaceBefore();
5351 if (
Left.is(TT_ObjCMethodSpecifier))
5353 if (
Left.is(tok::r_paren) &&
Left.isNot(TT_AttributeRParen) &&
5354 canBeObjCSelectorComponent(Right)) {
5362 (
Right.is(tok::equal) ||
Left.is(tok::equal))) {
5366 if (
Right.isOneOf(TT_TrailingReturnArrow, TT_LambdaArrow) ||
5367 Left.isOneOf(TT_TrailingReturnArrow, TT_LambdaArrow)) {
5370 if (
Left.is(tok::comma) &&
Right.isNot(TT_OverloadedOperatorLParen) &&
5373 (
Left.Children.empty() || !
Left.MacroParent)) {
5376 if (
Right.is(tok::comma))
5378 if (
Right.is(TT_ObjCBlockLParen))
5380 if (
Right.is(TT_CtorInitializerColon))
5381 return Style.SpaceBeforeCtorInitializerColon;
5382 if (
Right.is(TT_InheritanceColon) && !Style.SpaceBeforeInheritanceColon)
5384 if (
Right.is(TT_RangeBasedForLoopColon) &&
5385 !Style.SpaceBeforeRangeBasedForLoopColon) {
5388 if (
Left.is(TT_BitFieldColon)) {
5392 if (
Right.is(tok::colon)) {
5393 if (
Right.is(TT_CaseLabelColon))
5394 return Style.SpaceBeforeCaseColon;
5395 if (
Right.is(TT_GotoLabelColon))
5398 if (!
Right.getNextNonComment())
5400 if (
Right.is(TT_ObjCMethodExpr))
5402 if (
Left.is(tok::question))
5404 if (
Right.is(TT_InlineASMColon) &&
Left.is(tok::coloncolon))
5406 if (
Right.is(TT_DictLiteral))
5407 return Style.SpacesInContainerLiterals;
5408 if (
Right.is(TT_AttributeColon))
5410 if (
Right.is(TT_CSharpNamedArgumentColon))
5412 if (
Right.is(TT_GenericSelectionColon))
5414 if (
Right.is(TT_BitFieldColon)) {
5421 if ((
Left.isOneOf(tok::minus, tok::minusminus) &&
5422 Right.isOneOf(tok::minus, tok::minusminus)) ||
5423 (
Left.isOneOf(tok::plus, tok::plusplus) &&
5424 Right.isOneOf(tok::plus, tok::plusplus))) {
5427 if (
Left.is(TT_UnaryOperator)) {
5430 if (
Left.is(tok::amp) &&
Right.is(tok::r_square))
5431 return Style.SpacesInSquareBrackets;
5432 return Style.SpaceAfterLogicalNot &&
Left.is(tok::exclaim);
5437 if (
Left.is(TT_CastRParen)) {
5438 return Style.SpaceAfterCStyleCast ||
5439 Right.isOneOf(TT_BinaryOperator, TT_SelectorName);
5442 auto ShouldAddSpacesInAngles = [
this, &
Right]() {
5446 return Right.hasWhitespaceBefore();
5450 if (
Left.is(tok::greater) &&
Right.is(tok::greater)) {
5453 return !Style.Cpp11BracedListStyle;
5455 return Right.is(TT_TemplateCloser) &&
Left.is(TT_TemplateCloser) &&
5457 ShouldAddSpacesInAngles());
5459 if (
Right.isOneOf(tok::arrow, tok::arrowstar, tok::periodstar) ||
5460 Left.isOneOf(tok::arrow, tok::period, tok::arrowstar, tok::periodstar) ||
5461 (
Right.is(tok::period) &&
Right.isNot(TT_DesignatedInitializerPeriod))) {
5464 if (!Style.SpaceBeforeAssignmentOperators &&
Left.isNot(TT_TemplateCloser) &&
5469 (
Left.is(tok::identifier) ||
Left.is(tok::kw_this))) {
5472 if (
Right.is(tok::coloncolon) &&
Left.is(tok::identifier)) {
5476 return Right.hasWhitespaceBefore();
5478 if (
Right.is(tok::coloncolon) &&
5479 !
Left.isOneOf(tok::l_brace, tok::comment, tok::l_paren)) {
5481 return (
Left.is(TT_TemplateOpener) &&
5483 ShouldAddSpacesInAngles())) ||
5484 !(
Left.isOneOf(tok::l_paren, tok::r_paren, tok::l_square,
5485 tok::kw___super, TT_TemplateOpener,
5486 TT_TemplateCloser)) ||
5487 (
Left.is(tok::l_paren) && Style.SpacesInParensOptions.Other);
5489 if ((
Left.is(TT_TemplateOpener)) != (
Right.is(TT_TemplateCloser)))
5490 return ShouldAddSpacesInAngles();
5491 if (
Left.is(tok::r_paren) &&
Left.isNot(TT_TypeDeclarationParen) &&
5492 Right.is(TT_PointerOrReference) &&
Right.isOneOf(tok::amp, tok::ampamp)) {
5496 if (
Right.is(TT_StructuredBindingLSquare)) {
5497 return !
Left.isOneOf(tok::amp, tok::ampamp) ||
5501 if (
Right.Next &&
Right.Next->is(TT_StructuredBindingLSquare) &&
5502 Right.isOneOf(tok::amp, tok::ampamp)) {
5505 if ((
Right.is(TT_BinaryOperator) &&
Left.isNot(tok::l_paren)) ||
5506 (
Left.isOneOf(TT_BinaryOperator, TT_ConditionalExpr) &&
5507 Right.isNot(tok::r_paren))) {
5510 if (
Right.is(TT_TemplateOpener) &&
Left.is(tok::r_paren) &&
5511 Left.MatchingParen &&
5512 Left.MatchingParen->is(TT_OverloadedOperatorLParen)) {
5515 if (
Right.is(tok::less) &&
Left.isNot(tok::l_paren) &&
5519 if (
Right.is(TT_TrailingUnaryOperator))
5521 if (
Left.is(TT_RegexLiteral))
5523 return spaceRequiredBetween(
Line, Left, Right);
5529 !Tok.
isOneOf(TT_ObjCBlockLBrace, TT_LambdaLBrace, TT_DictLiteral);
5546 !Tok.
isOneOf(TT_ObjCBlockLBrace, TT_DictLiteral);
5549bool TokenAnnotator::mustBreakBefore(
const AnnotatedLine &
Line,
5550 const FormatToken &Right)
const {
5551 const FormatToken &Left = *Right.Previous;
5552 if (Right.NewlinesBefore > 1 && Style.MaxEmptyLinesToKeep > 0 &&
5553 (!Style.RemoveEmptyLinesInUnwrappedLines || &Right ==
Line.First)) {
5557 if (Style.BreakFunctionDefinitionParameters &&
Line.MightBeFunctionDecl &&
5558 Line.mightBeFunctionDefinition() && Left.MightBeFunctionDeclParen &&
5559 Left.ParameterCount > 0) {
5566 Line.MightBeFunctionDecl && !
Left.opensScope() &&
5571 const auto *BeforeLeft =
Left.Previous;
5572 const auto *AfterRight =
Right.Next;
5574 if (Style.isCSharp()) {
5575 if (
Left.is(TT_FatArrow) &&
Right.is(tok::l_brace) &&
5576 Style.BraceWrapping.AfterFunction) {
5579 if (
Right.is(TT_CSharpNamedArgumentColon) ||
5580 Left.is(TT_CSharpNamedArgumentColon)) {
5583 if (
Right.is(TT_CSharpGenericTypeConstraint))
5585 if (AfterRight && AfterRight->is(TT_FatArrow) &&
5586 (
Right.is(tok::numeric_constant) ||
5587 (
Right.is(tok::identifier) &&
Right.TokenText ==
"_"))) {
5592 if (
Left.is(TT_AttributeSquare) &&
Left.is(tok::r_square) &&
5593 (
Right.isAccessSpecifier(
false) ||
5594 Right.is(Keywords.kw_internal))) {
5598 if (
Left.is(TT_AttributeSquare) &&
Right.is(TT_AttributeSquare) &&
5599 Left.is(tok::r_square) &&
Right.is(tok::l_square)) {
5602 }
else if (Style.isJavaScript()) {
5604 if (
Right.is(tok::string_literal) &&
Left.is(tok::plus) && BeforeLeft &&
5605 BeforeLeft->is(tok::string_literal)) {
5608 if (
Left.is(TT_DictLiteral) &&
Left.is(tok::l_brace) &&
Line.Level == 0 &&
5609 BeforeLeft && BeforeLeft->is(tok::equal) &&
5610 Line.First->isOneOf(tok::identifier, Keywords.kw_import, tok::kw_export,
5614 !
Line.First->isOneOf(Keywords.kw_var, Keywords.kw_let)) {
5619 if (
Left.is(tok::l_brace) &&
Line.Level == 0 &&
5620 (
Line.startsWith(tok::kw_enum) ||
5621 Line.startsWith(tok::kw_const, tok::kw_enum) ||
5622 Line.startsWith(tok::kw_export, tok::kw_enum) ||
5623 Line.startsWith(tok::kw_export, tok::kw_const, tok::kw_enum))) {
5628 if (
Right.is(tok::r_brace) &&
Left.is(tok::l_brace) && BeforeLeft &&
5629 BeforeLeft->is(TT_FatArrow)) {
5631 switch (Style.AllowShortLambdasOnASingleLine) {
5637 return !
Left.Children.empty();
5641 return (
Left.NestingLevel == 0 &&
Line.Level == 0) &&
5642 !
Left.Children.empty();
5644 llvm_unreachable(
"Unknown FormatStyle::ShortLambdaStyle enum");
5647 if (
Right.is(tok::r_brace) &&
Left.is(tok::l_brace) &&
5648 !
Left.Children.empty()) {
5652 (
Left.NestingLevel == 0 &&
Line.Level == 0 &&
5653 Style.AllowShortFunctionsOnASingleLine &
5657 if (
Right.is(tok::plus) &&
Left.is(tok::string_literal) && AfterRight &&
5658 AfterRight->is(tok::string_literal)) {
5661 }
else if (Style.isVerilog()) {
5663 if (
Left.is(TT_VerilogAssignComma))
5666 if (
Left.is(TT_VerilogTypeComma))
5670 if (Style.VerilogBreakBetweenInstancePorts &&
5671 (
Left.is(TT_VerilogInstancePortComma) ||
5672 (
Left.is(tok::r_paren) && Keywords.isVerilogIdentifier(Right) &&
5673 Left.MatchingParen &&
5674 Left.MatchingParen->is(TT_VerilogInstancePortLParen)))) {
5679 if (!Keywords.isVerilogBegin(Right) && Keywords.isVerilogEndOfLabel(Left))
5681 }
else if (Style.BreakAdjacentStringLiterals &&
5682 (IsCpp || Style.isProto() ||
5684 if (
Left.isStringLiteral() &&
Right.isStringLiteral())
5689 if (Style.isJson()) {
5693 if (
Left.is(TT_DictLiteral) &&
Left.is(tok::l_brace))
5696 if ((
Left.is(TT_ArrayInitializerLSquare) &&
Left.is(tok::l_square) &&
5697 Right.isNot(tok::r_square)) ||
5698 Left.is(tok::comma)) {
5699 if (
Right.is(tok::l_brace))
5703 for (
const auto *Tok = &Right; Tok; Tok = Tok->Next) {
5704 if (Tok->isOneOf(tok::l_brace, tok::l_square))
5706 if (Tok->isOneOf(tok::r_brace, tok::r_square))
5709 return Style.BreakArrays;
5711 }
else if (Style.isTableGen()) {
5715 if (
Left.is(TT_TableGenCondOperatorComma))
5717 if (
Left.is(TT_TableGenDAGArgOperatorToBreak) &&
5718 Right.isNot(TT_TableGenDAGArgCloser)) {
5721 if (
Left.is(TT_TableGenDAGArgListCommaToBreak))
5723 if (
Right.is(TT_TableGenDAGArgCloser) &&
Right.MatchingParen &&
5724 Right.MatchingParen->is(TT_TableGenDAGArgOpenerToBreak) &&
5731 if (
Line.startsWith(tok::kw_asm) &&
Right.is(TT_InlineASMColon) &&
5741 const FormatToken *BeforeClosingBrace =
nullptr;
5742 if ((
Left.isOneOf(tok::l_brace, TT_ArrayInitializerLSquare) ||
5743 (Style.isJavaScript() &&
Left.is(tok::l_paren))) &&
5745 BeforeClosingBrace =
Left.MatchingParen->Previous;
5746 }
else if (
Right.MatchingParen &&
5747 (
Right.MatchingParen->isOneOf(tok::l_brace,
5748 TT_ArrayInitializerLSquare) ||
5749 (Style.isJavaScript() &&
5750 Right.MatchingParen->is(tok::l_paren)))) {
5751 BeforeClosingBrace = &
Left;
5753 if (BeforeClosingBrace && (BeforeClosingBrace->is(tok::comma) ||
5754 BeforeClosingBrace->isTrailingComment())) {
5759 if (
Right.is(tok::comment)) {
5761 (
Right.NewlinesBefore > 0 &&
Right.HasUnescapedNewline);
5763 if (
Left.isTrailingComment())
5765 if (
Left.IsUnterminatedLiteral)
5768 if (BeforeLeft && BeforeLeft->is(tok::lessless) &&
5769 Left.is(tok::string_literal) &&
Right.is(tok::lessless) && AfterRight &&
5770 AfterRight->is(tok::string_literal)) {
5771 return Right.NewlinesBefore > 0;
5774 if (
Right.is(TT_RequiresClause)) {
5775 switch (Style.RequiresClausePosition) {
5785 if (
Left.ClosesTemplateDeclaration &&
Left.MatchingParen &&
5786 Left.MatchingParen->NestingLevel == 0) {
5790 if (
Right.is(tok::kw_concept))
5794 Right.NewlinesBefore > 0);
5796 if (
Left.ClosesRequiresClause) {
5797 switch (Style.RequiresClausePosition) {
5800 return Right.isNot(tok::semi);
5802 return !
Right.isOneOf(tok::semi, tok::l_brace);
5809 (
Left.is(TT_CtorInitializerComma) ||
5810 Right.is(TT_CtorInitializerColon))) {
5815 Left.isOneOf(TT_CtorInitializerColon, TT_CtorInitializerComma)) {
5821 Right.isOneOf(TT_CtorInitializerComma, TT_CtorInitializerColon)) {
5827 Right.is(TT_CtorInitializerColon)) {
5832 Left.is(TT_CtorInitializerColon)) {
5838 Right.is(TT_InheritanceComma)) {
5842 Left.is(TT_InheritanceComma)) {
5845 if (
Right.is(tok::string_literal) &&
Right.TokenText.starts_with(
"R\"")) {
5849 return Right.IsMultiline &&
Right.NewlinesBefore > 0;
5851 if ((
Left.is(tok::l_brace) ||
5852 (
Left.is(tok::less) && BeforeLeft && BeforeLeft->is(tok::equal))) &&
5858 if (
Right.is(TT_InlineASMBrace))
5859 return Right.HasUnescapedNewline;
5862 auto *FirstNonComment =
Line.getFirstNonComment();
5864 FirstNonComment && (FirstNonComment->is(Keywords.kw_internal) ||
5865 FirstNonComment->isAccessSpecifierKeyword());
5867 if (Style.BraceWrapping.AfterEnum) {
5868 if (
Line.startsWith(tok::kw_enum) ||
5869 Line.startsWith(tok::kw_typedef, tok::kw_enum)) {
5874 FirstNonComment->Next->is(tok::kw_enum)) {
5880 if (Style.BraceWrapping.AfterClass &&
5882 FirstNonComment->Next->is(Keywords.kw_interface)) ||
5883 Line.startsWith(Keywords.kw_interface))) {
5888 if (
Right.isNot(TT_FunctionLBrace)) {
5889 return (
Line.startsWith(tok::kw_class) &&
5890 Style.BraceWrapping.AfterClass) ||
5891 (
Line.startsWith(tok::kw_struct) &&
5892 Style.BraceWrapping.AfterStruct);
5896 if (
Left.is(TT_ObjCBlockLBrace) &&
5902 if (
Left.isOneOf(TT_AttributeRParen, TT_AttributeMacro) &&
5903 Right.is(TT_ObjCDecl)) {
5907 if (
Left.is(TT_LambdaLBrace)) {
5915 (!
Left.Children.empty() &&
5921 if (Style.BraceWrapping.BeforeLambdaBody &&
Right.is(TT_LambdaLBrace) &&
5922 (
Left.isPointerOrReference() ||
Left.is(TT_TemplateCloser))) {
5928 Left.is(TT_LeadingJavaAnnotation) &&
5929 Right.isNot(TT_LeadingJavaAnnotation) &&
Right.isNot(tok::l_paren) &&
5930 (
Line.Last->is(tok::l_brace) || Style.BreakAfterJavaFieldAnnotations)) {
5934 if (
Right.is(TT_ProtoExtensionLSquare))
5964 if (Style.isProto() &&
Right.is(TT_SelectorName) &&
5965 Right.isNot(tok::r_square) && AfterRight) {
5968 if (
Left.is(tok::at))
5974 const auto *LBrace = AfterRight;
5975 if (LBrace && LBrace->is(tok::colon)) {
5976 LBrace = LBrace->Next;
5977 if (LBrace && LBrace->is(tok::at)) {
5978 LBrace = LBrace->Next;
5980 LBrace = LBrace->Next;
5992 ((LBrace->is(tok::l_brace) &&
5993 (LBrace->is(TT_DictLiteral) ||
5994 (LBrace->Next && LBrace->Next->is(tok::r_brace)))) ||
5995 LBrace->is(TT_ArrayInitializerLSquare) || LBrace->is(tok::less))) {
6002 if (
Left.ParameterCount == 0)
6017 if (
Left.isOneOf(tok::r_brace, tok::greater, tok::r_square))
6024bool TokenAnnotator::canBreakBefore(
const AnnotatedLine &
Line,
6025 const FormatToken &Right)
const {
6026 const FormatToken &
Left = *
Right.Previous;
6028 if (Style.isCSharp()) {
6029 if (
Left.isOneOf(TT_CSharpNamedArgumentColon, TT_AttributeColon) ||
6030 Right.isOneOf(TT_CSharpNamedArgumentColon, TT_AttributeColon)) {
6034 if (
Line.First->is(TT_CSharpGenericTypeConstraint))
6035 return Left.is(TT_CSharpGenericTypeConstraintComma);
6037 if (
Right.is(TT_CSharpNullable))
6040 if (
Left.isOneOf(Keywords.kw_throws, Keywords.kw_extends,
6041 Keywords.kw_implements)) {
6044 if (
Right.isOneOf(Keywords.kw_throws, Keywords.kw_extends,
6045 Keywords.kw_implements)) {
6048 }
else if (Style.isJavaScript()) {
6049 const FormatToken *NonComment =
Right.getPreviousNonComment();
6051 (NonComment->isAccessSpecifierKeyword() ||
6052 NonComment->isOneOf(
6053 tok::kw_return, Keywords.kw_yield, tok::kw_continue, tok::kw_break,
6054 tok::kw_throw, Keywords.kw_interface, Keywords.kw_type,
6055 tok::kw_static, Keywords.kw_readonly, Keywords.kw_override,
6056 Keywords.kw_abstract, Keywords.kw_get, Keywords.kw_set,
6057 Keywords.kw_async, Keywords.kw_await))) {
6060 if (
Right.NestingLevel == 0 &&
6061 (
Left.Tok.getIdentifierInfo() ||
6062 Left.isOneOf(tok::r_square, tok::r_paren)) &&
6063 Right.isOneOf(tok::l_square, tok::l_paren)) {
6066 if (NonComment && NonComment->is(tok::identifier) &&
6067 NonComment->TokenText ==
"asserts") {
6070 if (
Left.is(TT_FatArrow) &&
Right.is(tok::l_brace))
6072 if (
Left.is(TT_JsTypeColon))
6075 if (
Left.is(tok::exclaim) &&
Right.is(tok::colon))
6080 if (
Right.is(Keywords.kw_is)) {
6081 const FormatToken *Next =
Right.getNextNonComment();
6089 if (!Next || Next->isNot(tok::colon))
6092 if (
Left.is(Keywords.kw_in))
6094 if (
Right.is(Keywords.kw_in))
6096 if (
Right.is(Keywords.kw_as))
6098 if (
Right.isOneOf(Keywords.kw_extends, Keywords.kw_infer)) {
6104 if (
Left.is(Keywords.kw_as))
6106 if (
Left.is(TT_NonNullAssertion))
6108 if (
Left.is(Keywords.kw_declare) &&
6109 Right.isOneOf(Keywords.kw_module, tok::kw_namespace,
6110 Keywords.kw_function, tok::kw_class, tok::kw_enum,
6111 Keywords.kw_interface, Keywords.kw_type, Keywords.kw_var,
6112 Keywords.kw_let, tok::kw_const)) {
6117 if (
Left.isOneOf(Keywords.kw_module, tok::kw_namespace) &&
6118 Right.isOneOf(tok::identifier, tok::string_literal)) {
6121 if (
Right.is(TT_TemplateString) &&
Right.closesScope())
6125 if (
Left.is(tok::identifier) &&
Right.is(TT_TemplateString))
6127 if (
Left.is(TT_TemplateString) &&
Left.opensScope())
6129 }
else if (Style.isTableGen()) {
6131 if (Keywords.isTableGenDefinition(Left))
6134 if (
Right.is(tok::l_paren)) {
6135 return !
Left.isOneOf(TT_TableGenBangOperator, TT_TableGenCondOperator,
6139 if (
Left.is(TT_TableGenValueSuffix))
6142 if (
Left.is(tok::hash) ||
Right.is(tok::hash))
6144 if (
Left.isOneOf(TT_TableGenBangOperator, TT_TableGenCondOperator))
6151 if (
Right.is(tok::r_brace)) {
6153 (
Right.isBlockIndentedInitRBrace(Style)));
6157 if (
Right.is(tok::r_paren)) {
6159 !
Right.MatchingParen) {
6162 auto Next =
Right.Next;
6163 if (Next && Next->is(tok::r_paren))
6165 if (Next && Next->is(tok::l_paren))
6167 const FormatToken *
Previous =
Right.MatchingParen->Previous;
6171 if (
Left.isOneOf(tok::r_paren, TT_TrailingAnnotation) &&
6172 Right.is(TT_TrailingAnnotation) &&
6177 if (
Left.is(tok::at))
6179 if (
Left.Tok.getObjCKeywordID() == tok::objc_interface)
6181 if (
Left.isOneOf(TT_JavaAnnotation, TT_LeadingJavaAnnotation))
6182 return Right.isNot(tok::l_paren);
6183 if (
Right.is(TT_PointerOrReference)) {
6184 return Line.IsMultiVariableDeclStmt ||
6185 (getTokenPointerOrReferenceAlignment(Right) ==
6187 (!
Right.Next ||
Right.Next->isNot(TT_FunctionDeclarationName)));
6189 if (
Right.isOneOf(TT_StartOfName, TT_FunctionDeclarationName) ||
6190 Right.is(tok::kw_operator)) {
6193 if (
Left.is(TT_PointerOrReference))
6195 if (
Right.isTrailingComment()) {
6202 (
Left.is(TT_CtorInitializerColon) &&
Right.NewlinesBefore > 0 &&
6205 if (
Left.is(tok::question) &&
Right.is(tok::colon))
6207 if (
Right.is(TT_ConditionalExpr) ||
Right.is(tok::question))
6208 return Style.BreakBeforeTernaryOperators;
6209 if (
Left.is(TT_ConditionalExpr) ||
Left.is(tok::question))
6210 return !Style.BreakBeforeTernaryOperators;
6211 if (
Left.is(TT_InheritanceColon))
6213 if (
Right.is(TT_InheritanceColon))
6215 if (
Right.is(TT_ObjCMethodExpr) &&
Right.isNot(tok::r_square) &&
6216 Left.isNot(TT_SelectorName)) {
6220 if (
Right.is(tok::colon) &&
6221 !
Right.isOneOf(TT_CtorInitializerColon, TT_InlineASMColon)) {
6224 if (
Left.is(tok::colon) &&
Left.isOneOf(TT_DictLiteral, TT_ObjCMethodExpr)) {
6225 if (Style.isProto()) {
6226 if (!Style.AlwaysBreakBeforeMultilineStrings &&
Right.isStringLiteral())
6252 if (((
Right.is(tok::l_brace) ||
Right.is(tok::less)) &&
6253 Right.is(TT_DictLiteral)) ||
6254 Right.is(TT_ArrayInitializerLSquare)) {
6260 if (
Right.is(tok::r_square) &&
Right.MatchingParen &&
6261 Right.MatchingParen->is(TT_ProtoExtensionLSquare)) {
6264 if (
Right.is(TT_SelectorName) || (
Right.is(tok::identifier) &&
Right.Next &&
6265 Right.Next->is(TT_ObjCMethodExpr))) {
6266 return Left.isNot(tok::period);
6270 if (
Right.is(tok::kw_concept))
6272 if (
Right.is(TT_RequiresClause))
6274 if (
Left.ClosesTemplateDeclaration) {
6276 Right.NewlinesBefore > 0;
6278 if (
Left.is(TT_FunctionAnnotationRParen))
6280 if (
Left.ClosesRequiresClause)
6282 if (
Right.isOneOf(TT_RangeBasedForLoopColon, TT_OverloadedOperatorLParen,
6283 TT_OverloadedOperator)) {
6286 if (
Left.is(TT_RangeBasedForLoopColon))
6288 if (
Right.is(TT_RangeBasedForLoopColon))
6290 if (
Left.is(TT_TemplateCloser) &&
Right.is(TT_TemplateOpener))
6292 if ((
Left.is(tok::greater) &&
Right.is(tok::greater)) ||
6293 (
Left.is(tok::less) &&
Right.is(tok::less))) {
6296 if (
Right.is(TT_BinaryOperator) &&
6302 if (
Left.isOneOf(TT_TemplateCloser, TT_UnaryOperator) ||
6303 Left.is(tok::kw_operator)) {
6306 if (
Left.is(tok::equal) && !
Right.isOneOf(tok::kw_default, tok::kw_delete) &&
6310 if (
Left.is(tok::equal) &&
Right.is(tok::l_brace) &&
6311 !Style.Cpp11BracedListStyle) {
6314 if (
Left.is(TT_AttributeLParen) ||
6315 (
Left.is(tok::l_paren) &&
Left.is(TT_TypeDeclarationParen))) {
6318 if (
Left.is(tok::l_paren) &&
Left.Previous &&
6319 (
Left.Previous->isOneOf(TT_BinaryOperator, TT_CastRParen))) {
6322 if (
Right.is(TT_ImplicitStringLiteral))
6325 if (
Right.is(TT_TemplateCloser))
6327 if (
Right.is(tok::r_square) &&
Right.MatchingParen &&
6328 Right.MatchingParen->is(TT_LambdaLSquare)) {
6334 if (
Left.is(TT_TrailingAnnotation)) {
6335 return !
Right.isOneOf(tok::l_brace, tok::semi, tok::equal, tok::l_paren,
6336 tok::less, tok::coloncolon);
6339 if (
Right.isAttribute())
6342 if (
Right.is(tok::l_square) &&
Right.is(TT_AttributeSquare))
6343 return Left.isNot(TT_AttributeSquare);
6345 if (
Left.is(tok::identifier) &&
Right.is(tok::string_literal))
6348 if (
Right.is(tok::identifier) &&
Right.Next &&
Right.Next->is(TT_DictLiteral))
6351 if (
Left.is(TT_CtorInitializerColon)) {
6353 (!
Right.isTrailingComment() ||
Right.NewlinesBefore > 0);
6355 if (
Right.is(TT_CtorInitializerColon))
6357 if (
Left.is(TT_CtorInitializerComma) &&
6361 if (
Right.is(TT_CtorInitializerComma) &&
6365 if (
Left.is(TT_InheritanceComma) &&
6369 if (
Right.is(TT_InheritanceComma) &&
6373 if (
Left.is(TT_ArrayInitializerLSquare))
6375 if (
Right.is(tok::kw_typename) &&
Left.isNot(tok::kw_const))
6377 if ((
Left.isBinaryOperator() ||
Left.is(TT_BinaryOperator)) &&
6378 !
Left.isOneOf(tok::arrowstar, tok::lessless) &&
6384 if ((
Left.is(TT_AttributeSquare) &&
Right.is(tok::l_square)) ||
6385 (
Left.is(tok::r_square) &&
Right.is(TT_AttributeSquare))) {
6389 auto ShortLambdaOption = Style.AllowShortLambdasOnASingleLine;
6390 if (Style.BraceWrapping.BeforeLambdaBody &&
Right.is(TT_LambdaLBrace)) {
6397 if (
Right.is(tok::kw_noexcept) &&
Right.is(TT_TrailingAnnotation)) {
6398 switch (Style.AllowBreakBeforeNoexceptSpecifier) {
6404 return Right.Next &&
Right.Next->is(tok::l_paren);
6408 return Left.isOneOf(tok::comma, tok::coloncolon, tok::semi, tok::l_brace,
6409 tok::kw_class, tok::kw_struct, tok::comment) ||
6410 Right.isMemberAccess() ||
6411 Right.isOneOf(TT_TrailingReturnArrow, TT_LambdaArrow, tok::lessless,
6412 tok::colon, tok::l_square, tok::at) ||
6413 (
Left.is(tok::r_paren) &&
6414 Right.isOneOf(tok::identifier, tok::kw_const)) ||
6415 (
Left.is(tok::l_paren) &&
Right.isNot(tok::r_paren)) ||
6416 (
Left.is(TT_TemplateOpener) &&
Right.isNot(TT_TemplateCloser));
6419void TokenAnnotator::printDebugInfo(
const AnnotatedLine &
Line)
const {
6420 llvm::errs() <<
"AnnotatedTokens(L=" <<
Line.Level <<
", P=" <<
Line.PPLevel
6421 <<
", T=" <<
Line.Type <<
", C=" <<
Line.IsContinuation
6423 const FormatToken *Tok =
Line.First;
6425 llvm::errs() <<
" M=" << Tok->MustBreakBefore
6426 <<
" C=" << Tok->CanBreakBefore
6428 <<
" S=" << Tok->SpacesRequiredBefore
6429 <<
" F=" << Tok->Finalized <<
" B=" << Tok->BlockParameterCount
6430 <<
" BK=" << Tok->getBlockKind() <<
" P=" << Tok->SplitPenalty
6431 <<
" Name=" << Tok->Tok.getName() <<
" L=" << Tok->TotalLength
6432 <<
" PPK=" << Tok->getPackingKind() <<
" FakeLParens=";
6434 llvm::errs() << LParen <<
"/";
6435 llvm::errs() <<
" FakeRParens=" << Tok->FakeRParens;
6436 llvm::errs() <<
" II=" << Tok->Tok.getIdentifierInfo();
6437 llvm::errs() <<
" Text='" << Tok->TokenText <<
"'\n";
6439 assert(Tok ==
Line.Last);
6442 llvm::errs() <<
"----\n";
6446TokenAnnotator::getTokenReferenceAlignment(
const FormatToken &
Reference)
const {
6447 assert(
Reference.isOneOf(tok::amp, tok::ampamp));
6448 switch (Style.ReferenceAlignment) {
6450 return Style.PointerAlignment;
6459 return Style.PointerAlignment;
6463TokenAnnotator::getTokenPointerOrReferenceAlignment(
6464 const FormatToken &PointerOrReference)
const {
6465 if (PointerOrReference.isOneOf(tok::amp, tok::ampamp)) {
6466 switch (Style.ReferenceAlignment) {
6468 return Style.PointerAlignment;
6477 assert(PointerOrReference.is(tok::star));
6478 return Style.PointerAlignment;
Defines the SourceManager interface.
bool ColonIsObjCMethodExpr
FormatToken * FirstStartOfName
bool InCpp11AttributeSpecifier
unsigned LongestObjCSelectorName
bool VerilogAssignmentFound
bool InCSharpAttributeSpecifier
tok::TokenKind ContextKind
FormatToken * FirstObjCSelectorName
enum clang::format::@1338::AnnotatingParser::Context::@353 ContextType
bool VerilogMayBeConcatenation
This file implements a token annotator, i.e.
Defines the clang::TokenKind enum and support functions.
#define TRANSFORM_TYPE_TRAIT_DEF(Enum, _)
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
Parser - This implements a parser for the C family of languages.
IdentifierInfo * getIdentifierInfo() const
bool Ret(InterpState &S, CodePtr &PC)
TokenKind
Provides a simple uniform namespace for tokens from all C languages.
The JSON file list parser is used to communicate input to InstallAPI.
if(T->getSizeExpr()) TRY_TO(TraverseStmt(const_cast< Expr * >(T -> getSizeExpr())))
@ Parameter
The parameter type of a method or function.
@ Result
The result type of a method or function.
prec::Level getBinOpPrecedence(tok::TokenKind Kind, bool GreaterThanIsOperator, bool CPlusPlus11)
Return the precedence of the specified binary operator token.
const FunctionProtoType * T
AccessSpecifier
A C++ access specifier (public, private, protected), plus the special value "none" which means differ...