19#include "llvm/ADT/SmallPtrSet.h"
20#include "llvm/Support/Debug.h"
22#define DEBUG_TYPE "format-token-annotator"
43static bool startsWithInitStatement(
const AnnotatedLine &
Line) {
44 return Line.startsWith(tok::kw_for) ||
Line.startsWith(tok::kw_if) ||
45 Line.startsWith(tok::kw_switch);
59static bool canBeObjCSelectorComponent(
const FormatToken &Tok) {
60 return Tok.Tok.getIdentifierInfo();
66static bool isLambdaParameterList(
const FormatToken *Left) {
68 if (
Left->Previous &&
Left->Previous->is(tok::greater) &&
69 Left->Previous->MatchingParen &&
70 Left->Previous->MatchingParen->is(TT_TemplateOpener)) {
71 Left =
Left->Previous->MatchingParen;
75 return Left->Previous &&
Left->Previous->is(tok::r_square) &&
76 Left->Previous->MatchingParen &&
77 Left->Previous->MatchingParen->is(TT_LambdaLSquare);
82static bool isKeywordWithCondition(
const FormatToken &Tok) {
83 return Tok.isOneOf(tok::kw_if, tok::kw_for, tok::kw_while, tok::kw_switch,
84 tok::kw_constexpr, tok::kw_catch);
88static bool isCppAttribute(
bool IsCpp,
const FormatToken &Tok) {
89 if (!IsCpp || !Tok.startsSequence(tok::l_square, tok::l_square))
92 if (Tok.Previous && Tok.Previous->is(tok::at))
94 const FormatToken *AttrTok = Tok.Next->Next;
99 if (AttrTok->startsSequence(tok::kw_using, tok::identifier, tok::colon))
101 if (AttrTok->isNot(tok::identifier))
103 while (AttrTok && !AttrTok->startsSequence(tok::r_square, tok::r_square)) {
107 if (AttrTok->is(tok::colon) ||
108 AttrTok->startsSequence(tok::identifier, tok::identifier) ||
109 AttrTok->startsSequence(tok::r_paren, tok::identifier)) {
112 if (AttrTok->is(tok::ellipsis))
114 AttrTok = AttrTok->Next;
116 return AttrTok && AttrTok->startsSequence(tok::r_square, tok::r_square);
124class AnnotatingParser {
126 AnnotatingParser(
const FormatStyle &Style, AnnotatedLine &Line,
127 const AdditionalKeywords &Keywords,
128 SmallVector<ScopeType> &Scopes)
129 : Style(Style), Line(Line), CurrentToken(Line.
First), AutoFound(
false),
131 Keywords(Keywords), Scopes(Scopes), TemplateDeclarationDepth(0) {
132 assert(IsCpp == LangOpts.CXXOperatorNames);
133 Contexts.push_back(Context(tok::unknown, 1,
false));
134 resetTokenMetadata();
138 ScopeType getScopeType(
const FormatToken &Token)
const {
139 switch (Token.getType()) {
140 case TT_FunctionLBrace:
141 case TT_LambdaLBrace:
144 case TT_StructLBrace:
153 if (!CurrentToken || !CurrentToken->Previous)
155 if (NonTemplateLess.count(CurrentToken->Previous) > 0)
158 if (
const auto &
Previous = *CurrentToken->Previous;
160 if (
Previous.Previous->Tok.isLiteral())
162 if (
Previous.Previous->is(tok::r_brace))
164 if (
Previous.Previous->is(tok::r_paren) && Contexts.size() > 1 &&
165 (!
Previous.Previous->MatchingParen ||
166 Previous.Previous->MatchingParen->isNot(
167 TT_OverloadedOperatorLParen))) {
170 if (
Previous.Previous->is(tok::kw_operator) &&
171 CurrentToken->is(tok::l_paren)) {
176 FormatToken *
Left = CurrentToken->Previous;
177 Left->ParentBracket = Contexts.back().ContextKind;
178 ScopedContextCreator ContextCreator(*
this, tok::less, 12);
179 Contexts.back().IsExpression =
false;
181 const auto *BeforeLess =
Left->Previous;
185 if (BeforeLess && BeforeLess->isNot(tok::kw_template))
186 Contexts.back().ContextType = Context::TemplateArgument;
189 CurrentToken->is(tok::question)) {
193 for (
bool SeenTernaryOperator =
false; CurrentToken;) {
194 const bool InExpr = Contexts[Contexts.size() - 2].IsExpression;
195 if (CurrentToken->is(tok::greater)) {
196 const auto *Next = CurrentToken->Next;
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))) {
213 Left->MatchingParen = CurrentToken;
214 CurrentToken->MatchingParen =
Left;
222 BeforeLess->isOneOf(TT_SelectorName, TT_DictLiteral))) {
223 CurrentToken->setType(TT_DictLiteral);
225 CurrentToken->setType(TT_TemplateCloser);
226 CurrentToken->Tok.setLength(1);
228 if (Next && Next->Tok.isLiteral())
233 if (CurrentToken->is(tok::question) &&
238 if (CurrentToken->isOneOf(tok::r_paren, tok::r_square, tok::r_brace))
240 const auto &Prev = *CurrentToken->Previous;
247 if (InExpr && !Line.startsWith(tok::kw_template) &&
248 Prev.is(TT_BinaryOperator)) {
249 const auto Precedence = Prev.getPrecedence();
253 if (Prev.is(TT_ConditionalExpr))
254 SeenTernaryOperator =
true;
255 updateParameterCount(Left, CurrentToken);
257 if (FormatToken *
Previous = CurrentToken->getPreviousNonComment()) {
258 if (CurrentToken->is(tok::colon) ||
259 (CurrentToken->isOneOf(tok::l_brace, tok::less) &&
265 if (Style.isTableGen()) {
266 if (CurrentToken->isOneOf(tok::comma, tok::equal)) {
273 if (!parseTableGenValue())
283 bool parseUntouchableParens() {
284 while (CurrentToken) {
285 CurrentToken->Finalized =
true;
286 switch (CurrentToken->Tok.getKind()) {
289 if (!parseUntouchableParens())
304 bool parseParens(
bool LookForDecls =
false) {
307 assert(CurrentToken->Previous &&
"Unknown previous token");
308 FormatToken &OpeningParen = *CurrentToken->Previous;
309 assert(OpeningParen.is(tok::l_paren));
310 FormatToken *PrevNonComment = OpeningParen.getPreviousNonComment();
311 OpeningParen.ParentBracket = Contexts.back().ContextKind;
312 ScopedContextCreator ContextCreator(*
this, tok::l_paren, 1);
315 Contexts.back().ColonIsForRangeExpr =
316 Contexts.size() == 2 && Contexts[0].ColonIsForRangeExpr;
318 if (OpeningParen.Previous &&
319 OpeningParen.Previous->is(TT_UntouchableMacroFunc)) {
320 OpeningParen.Finalized =
true;
321 return parseUntouchableParens();
324 bool StartsObjCMethodExpr =
false;
325 if (!Style.isVerilog()) {
326 if (FormatToken *MaybeSel = OpeningParen.Previous) {
328 if (MaybeSel->isObjCAtKeyword(tok::objc_selector) &&
329 MaybeSel->Previous && MaybeSel->Previous->is(tok::at)) {
330 StartsObjCMethodExpr =
true;
335 if (OpeningParen.is(TT_OverloadedOperatorLParen)) {
337 FormatToken *Prev = &OpeningParen;
338 while (Prev->isNot(tok::kw_operator)) {
339 Prev = Prev->Previous;
340 assert(Prev &&
"Expect a kw_operator prior to the OperatorLParen!");
346 bool OperatorCalledAsMemberFunction =
347 Prev->Previous && Prev->Previous->isOneOf(tok::period, tok::arrow);
348 Contexts.back().IsExpression = OperatorCalledAsMemberFunction;
349 }
else if (OpeningParen.is(TT_VerilogInstancePortLParen)) {
350 Contexts.back().IsExpression =
true;
351 Contexts.back().ContextType = Context::VerilogInstancePortList;
352 }
else if (Style.isJavaScript() &&
353 (Line.startsWith(Keywords.kw_type, tok::identifier) ||
354 Line.startsWith(tok::kw_export, Keywords.kw_type,
358 Contexts.back().IsExpression =
false;
359 }
else if (OpeningParen.Previous &&
360 (OpeningParen.Previous->isOneOf(
361 tok::kw_static_assert, tok::kw_noexcept, tok::kw_explicit,
362 tok::kw_while, tok::l_paren, tok::comma,
363 TT_BinaryOperator) ||
364 OpeningParen.Previous->isIf())) {
366 Contexts.back().IsExpression =
true;
367 }
else if (Style.isJavaScript() && OpeningParen.Previous &&
368 (OpeningParen.Previous->is(Keywords.kw_function) ||
369 (OpeningParen.Previous->endsSequence(tok::identifier,
370 Keywords.kw_function)))) {
372 Contexts.back().IsExpression =
false;
373 }
else if (Style.isJavaScript() && OpeningParen.Previous &&
374 OpeningParen.Previous->is(TT_JsTypeColon)) {
376 Contexts.back().IsExpression =
false;
377 }
else if (isLambdaParameterList(&OpeningParen)) {
379 OpeningParen.setType(TT_LambdaDefinitionLParen);
380 Contexts.back().IsExpression =
false;
381 }
else if (OpeningParen.is(TT_RequiresExpressionLParen)) {
382 Contexts.back().IsExpression =
false;
383 }
else if (OpeningParen.Previous &&
384 OpeningParen.Previous->is(tok::kw__Generic)) {
385 Contexts.back().ContextType = Context::C11GenericSelection;
386 Contexts.back().IsExpression =
true;
387 }
else if (Line.InPPDirective &&
388 (!OpeningParen.Previous ||
389 OpeningParen.Previous->isNot(tok::identifier))) {
390 Contexts.back().IsExpression =
true;
391 }
else if (Contexts[Contexts.size() - 2].CaretFound) {
393 Contexts.back().IsExpression =
false;
394 }
else if (OpeningParen.Previous &&
395 OpeningParen.Previous->is(TT_ForEachMacro)) {
397 Contexts.back().ContextType = Context::ForEachMacro;
398 Contexts.back().IsExpression =
false;
399 }
else if (OpeningParen.Previous && OpeningParen.Previous->MatchingParen &&
400 OpeningParen.Previous->MatchingParen->isOneOf(
401 TT_ObjCBlockLParen, TT_FunctionTypeLParen)) {
402 Contexts.back().IsExpression =
false;
403 }
else if (!Line.MustBeDeclaration && !Line.InPPDirective) {
405 OpeningParen.Previous &&
406 OpeningParen.Previous->isOneOf(tok::kw_for, tok::kw_catch);
407 Contexts.back().IsExpression = !IsForOrCatch;
410 if (Style.isTableGen()) {
411 if (FormatToken *Prev = OpeningParen.Previous) {
412 if (Prev->is(TT_TableGenCondOperator)) {
413 Contexts.back().IsTableGenCondOpe =
true;
414 Contexts.back().IsExpression =
true;
415 }
else if (Contexts.size() > 1 &&
416 Contexts[Contexts.size() - 2].IsTableGenBangOpe) {
421 Contexts.back().IsTableGenBangOpe =
true;
422 Contexts.back().IsExpression =
true;
425 if (!parseTableGenDAGArg())
427 return parseTableGenDAGArgAndList(&OpeningParen);
434 if (PrevNonComment && OpeningParen.is(TT_Unknown)) {
435 if (PrevNonComment->isAttribute()) {
436 OpeningParen.setType(TT_AttributeLParen);
437 }
else if (PrevNonComment->isOneOf(TT_TypenameMacro, tok::kw_decltype,
440#include
"clang/Basic/TransformTypeTraits.def"
442 OpeningParen.setType(TT_TypeDeclarationParen);
444 if (PrevNonComment->isOneOf(tok::kw_decltype, tok::kw_typeof))
445 Contexts.back().IsExpression =
true;
449 if (StartsObjCMethodExpr) {
450 Contexts.back().ColonIsObjCMethodExpr =
true;
451 OpeningParen.setType(TT_ObjCMethodExpr);
462 bool MightBeFunctionType = !Contexts[Contexts.size() - 2].IsExpression;
463 bool ProbablyFunctionType =
464 CurrentToken->isPointerOrReference() || CurrentToken->is(tok::caret);
465 bool HasMultipleLines =
false;
466 bool HasMultipleParametersOnALine =
false;
467 bool MightBeObjCForRangeLoop =
468 OpeningParen.Previous && OpeningParen.Previous->is(tok::kw_for);
469 FormatToken *PossibleObjCForInToken =
nullptr;
470 while (CurrentToken) {
475 if (LookForDecls && CurrentToken->Next) {
476 FormatToken *Prev = CurrentToken->getPreviousNonComment();
478 FormatToken *PrevPrev = Prev->getPreviousNonComment();
479 FormatToken *Next = CurrentToken->Next;
480 if (PrevPrev && PrevPrev->is(tok::identifier) &&
481 PrevPrev->isNot(TT_TypeName) && Prev->isPointerOrReference() &&
482 CurrentToken->is(tok::identifier) && Next->isNot(tok::equal)) {
483 Prev->setType(TT_BinaryOperator);
484 LookForDecls =
false;
489 if (CurrentToken->Previous->is(TT_PointerOrReference) &&
490 CurrentToken->Previous->Previous->isOneOf(tok::l_paren,
492 ProbablyFunctionType =
true;
494 if (CurrentToken->is(tok::comma))
495 MightBeFunctionType =
false;
496 if (CurrentToken->Previous->is(TT_BinaryOperator))
497 Contexts.back().IsExpression =
true;
498 if (CurrentToken->is(tok::r_paren)) {
499 if (OpeningParen.isNot(TT_CppCastLParen) && MightBeFunctionType &&
500 ProbablyFunctionType && CurrentToken->Next &&
501 (CurrentToken->Next->is(tok::l_paren) ||
502 (CurrentToken->Next->is(tok::l_square) &&
503 Line.MustBeDeclaration))) {
504 OpeningParen.setType(OpeningParen.Next->is(tok::caret)
506 : TT_FunctionTypeLParen);
508 OpeningParen.MatchingParen = CurrentToken;
509 CurrentToken->MatchingParen = &OpeningParen;
511 if (CurrentToken->Next && CurrentToken->Next->is(tok::l_brace) &&
512 OpeningParen.Previous && OpeningParen.Previous->is(tok::l_paren)) {
516 for (FormatToken *Tok = &OpeningParen; Tok != CurrentToken;
518 if (Tok->is(TT_BinaryOperator) && Tok->isPointerOrReference())
519 Tok->setType(TT_PointerOrReference);
523 if (StartsObjCMethodExpr) {
524 CurrentToken->setType(TT_ObjCMethodExpr);
525 if (Contexts.back().FirstObjCSelectorName) {
526 Contexts.back().FirstObjCSelectorName->LongestObjCSelectorName =
527 Contexts.back().LongestObjCSelectorName;
531 if (OpeningParen.is(TT_AttributeLParen))
532 CurrentToken->setType(TT_AttributeRParen);
533 if (OpeningParen.is(TT_TypeDeclarationParen))
534 CurrentToken->setType(TT_TypeDeclarationParen);
535 if (OpeningParen.Previous &&
536 OpeningParen.Previous->is(TT_JavaAnnotation)) {
537 CurrentToken->setType(TT_JavaAnnotation);
539 if (OpeningParen.Previous &&
540 OpeningParen.Previous->is(TT_LeadingJavaAnnotation)) {
541 CurrentToken->setType(TT_LeadingJavaAnnotation);
543 if (OpeningParen.Previous &&
544 OpeningParen.Previous->is(TT_AttributeSquare)) {
545 CurrentToken->setType(TT_AttributeSquare);
548 if (!HasMultipleLines)
550 else if (HasMultipleParametersOnALine)
558 if (CurrentToken->isOneOf(tok::r_square, tok::r_brace))
561 if (CurrentToken->is(tok::l_brace) && OpeningParen.is(TT_ObjCBlockLParen))
562 OpeningParen.setType(TT_Unknown);
563 if (CurrentToken->is(tok::comma) && CurrentToken->Next &&
564 !CurrentToken->Next->HasUnescapedNewline &&
565 !CurrentToken->Next->isTrailingComment()) {
566 HasMultipleParametersOnALine =
true;
568 bool ProbablyFunctionTypeLParen =
569 (CurrentToken->is(tok::l_paren) && CurrentToken->Next &&
570 CurrentToken->Next->isOneOf(tok::star, tok::amp, tok::caret));
571 if ((CurrentToken->Previous->isOneOf(tok::kw_const, tok::kw_auto) ||
572 CurrentToken->Previous->isTypeName(LangOpts)) &&
573 !(CurrentToken->is(tok::l_brace) ||
574 (CurrentToken->is(tok::l_paren) && !ProbablyFunctionTypeLParen))) {
575 Contexts.back().IsExpression =
false;
577 if (CurrentToken->isOneOf(tok::semi, tok::colon)) {
578 MightBeObjCForRangeLoop =
false;
579 if (PossibleObjCForInToken) {
580 PossibleObjCForInToken->setType(TT_Unknown);
581 PossibleObjCForInToken =
nullptr;
584 if (MightBeObjCForRangeLoop && CurrentToken->is(Keywords.kw_in)) {
585 PossibleObjCForInToken = CurrentToken;
586 PossibleObjCForInToken->setType(TT_ObjCForIn);
590 if (CurrentToken->is(tok::comma))
591 Contexts.back().CanBeExpression =
true;
593 if (Style.isTableGen()) {
594 if (CurrentToken->is(tok::comma)) {
595 if (Contexts.back().IsTableGenCondOpe)
596 CurrentToken->setType(TT_TableGenCondOperatorComma);
598 }
else if (CurrentToken->is(tok::colon)) {
599 if (Contexts.back().IsTableGenCondOpe)
600 CurrentToken->setType(TT_TableGenCondOperatorColon);
604 if (!parseTableGenValue())
609 FormatToken *Tok = CurrentToken;
612 updateParameterCount(&OpeningParen, Tok);
613 if (CurrentToken && CurrentToken->HasUnescapedNewline)
614 HasMultipleLines =
true;
619 bool isCSharpAttributeSpecifier(
const FormatToken &Tok) {
620 if (!Style.isCSharp())
624 if (Tok.Previous && Tok.Previous->is(tok::identifier))
628 if (Tok.Previous && Tok.Previous->is(tok::r_square)) {
629 auto *MatchingParen = Tok.Previous->MatchingParen;
630 if (!MatchingParen || MatchingParen->is(TT_ArraySubscriptLSquare))
634 const FormatToken *AttrTok = Tok.Next;
639 if (AttrTok->is(tok::r_square))
643 while (AttrTok && AttrTok->isNot(tok::r_square))
644 AttrTok = AttrTok->Next;
650 AttrTok = AttrTok->Next;
655 if (AttrTok->isAccessSpecifierKeyword() ||
656 AttrTok->isOneOf(tok::comment, tok::kw_class, tok::kw_static,
657 tok::l_square, Keywords.kw_internal)) {
663 AttrTok->Next->startsSequence(tok::identifier, tok::l_paren)) {
678 FormatToken *
Left = CurrentToken->Previous;
679 Left->ParentBracket = Contexts.back().ContextKind;
680 FormatToken *
Parent =
Left->getPreviousNonComment();
685 bool CppArrayTemplates =
687 (Contexts.back().CanBeExpression || Contexts.back().IsExpression ||
688 Contexts.back().ContextType == Context::TemplateArgument);
690 const bool IsInnerSquare = Contexts.back().InCpp11AttributeSpecifier;
691 const bool IsCpp11AttributeSpecifier =
692 isCppAttribute(IsCpp, *Left) || IsInnerSquare;
695 bool IsCSharpAttributeSpecifier =
696 isCSharpAttributeSpecifier(*Left) ||
697 Contexts.back().InCSharpAttributeSpecifier;
699 bool InsideInlineASM = Line.startsWith(tok::kw_asm);
700 bool IsCppStructuredBinding =
Left->isCppStructuredBinding(IsCpp);
701 bool StartsObjCMethodExpr =
702 !IsCppStructuredBinding && !InsideInlineASM && !CppArrayTemplates &&
703 IsCpp && !IsCpp11AttributeSpecifier && !IsCSharpAttributeSpecifier &&
704 Contexts.back().CanBeExpression &&
Left->isNot(TT_LambdaLSquare) &&
705 !CurrentToken->isOneOf(tok::l_brace, tok::r_square) &&
707 Parent->isOneOf(tok::colon, tok::l_square, tok::l_paren,
708 tok::kw_return, tok::kw_throw) ||
709 Parent->isUnaryOperator() ||
711 Parent->isOneOf(TT_ObjCForIn, TT_CastRParen) ||
714 bool ColonFound =
false;
716 unsigned BindingIncrease = 1;
717 if (IsCppStructuredBinding) {
718 Left->setType(TT_StructuredBindingLSquare);
719 }
else if (
Left->is(TT_Unknown)) {
720 if (StartsObjCMethodExpr) {
721 Left->setType(TT_ObjCMethodExpr);
722 }
else if (InsideInlineASM) {
723 Left->setType(TT_InlineASMSymbolicNameLSquare);
724 }
else if (IsCpp11AttributeSpecifier) {
725 Left->setType(TT_AttributeSquare);
726 if (!IsInnerSquare &&
Left->Previous)
727 Left->Previous->EndsCppAttributeGroup =
false;
728 }
else if (Style.isJavaScript() &&
Parent &&
729 Contexts.back().ContextKind == tok::l_brace &&
730 Parent->isOneOf(tok::l_brace, tok::comma)) {
731 Left->setType(TT_JsComputedPropertyName);
732 }
else if (IsCpp && Contexts.back().ContextKind == tok::l_brace &&
734 Left->setType(TT_DesignatedInitializerLSquare);
735 }
else if (IsCSharpAttributeSpecifier) {
736 Left->setType(TT_AttributeSquare);
737 }
else if (CurrentToken->is(tok::r_square) &&
Parent &&
738 Parent->is(TT_TemplateCloser)) {
739 Left->setType(TT_ArraySubscriptLSquare);
740 }
else if (Style.isProto()) {
767 Left->setType(TT_ArrayInitializerLSquare);
768 if (!
Left->endsSequence(tok::l_square, tok::numeric_constant,
770 !
Left->endsSequence(tok::l_square, tok::numeric_constant,
772 !
Left->endsSequence(tok::l_square, tok::colon, TT_SelectorName)) {
773 Left->setType(TT_ProtoExtensionLSquare);
774 BindingIncrease = 10;
776 }
else if (!CppArrayTemplates &&
Parent &&
777 Parent->isOneOf(TT_BinaryOperator, TT_TemplateCloser, tok::at,
778 tok::comma, tok::l_paren, tok::l_square,
779 tok::question, tok::colon, tok::kw_return,
782 Left->setType(TT_ArrayInitializerLSquare);
784 BindingIncrease = 10;
785 Left->setType(TT_ArraySubscriptLSquare);
789 ScopedContextCreator ContextCreator(*
this, tok::l_square, BindingIncrease);
790 Contexts.back().IsExpression =
true;
791 if (Style.isJavaScript() &&
Parent &&
Parent->is(TT_JsTypeColon))
792 Contexts.back().IsExpression =
false;
794 Contexts.back().ColonIsObjCMethodExpr = StartsObjCMethodExpr;
795 Contexts.back().InCpp11AttributeSpecifier = IsCpp11AttributeSpecifier;
796 Contexts.back().InCSharpAttributeSpecifier = IsCSharpAttributeSpecifier;
798 while (CurrentToken) {
799 if (CurrentToken->is(tok::r_square)) {
800 if (IsCpp11AttributeSpecifier) {
801 CurrentToken->setType(TT_AttributeSquare);
803 CurrentToken->EndsCppAttributeGroup =
true;
805 if (IsCSharpAttributeSpecifier) {
806 CurrentToken->setType(TT_AttributeSquare);
807 }
else if (((CurrentToken->Next &&
808 CurrentToken->Next->is(tok::l_paren)) ||
809 (CurrentToken->Previous &&
810 CurrentToken->Previous->Previous == Left)) &&
811 Left->is(TT_ObjCMethodExpr)) {
816 StartsObjCMethodExpr =
false;
817 Left->setType(TT_Unknown);
819 if (StartsObjCMethodExpr && CurrentToken->Previous != Left) {
820 CurrentToken->setType(TT_ObjCMethodExpr);
823 if (!ColonFound && CurrentToken->Previous &&
824 CurrentToken->Previous->is(TT_Unknown) &&
825 canBeObjCSelectorComponent(*CurrentToken->Previous)) {
826 CurrentToken->Previous->setType(TT_SelectorName);
832 Parent->overwriteFixedType(TT_BinaryOperator);
835 if (CurrentToken->is(TT_ObjCMethodExpr) && CurrentToken->Next &&
836 CurrentToken->Next->is(TT_TrailingReturnArrow)) {
837 CurrentToken->Next->overwriteFixedType(TT_Unknown);
839 Left->MatchingParen = CurrentToken;
840 CurrentToken->MatchingParen =
Left;
845 if (!Contexts.back().FirstObjCSelectorName) {
846 FormatToken *
Previous = CurrentToken->getPreviousNonComment();
848 Previous->ObjCSelectorNameParts = 1;
849 Contexts.back().FirstObjCSelectorName =
Previous;
852 Left->ParameterCount =
853 Contexts.back().FirstObjCSelectorName->ObjCSelectorNameParts;
855 if (Contexts.back().FirstObjCSelectorName) {
856 Contexts.back().FirstObjCSelectorName->LongestObjCSelectorName =
857 Contexts.back().LongestObjCSelectorName;
858 if (
Left->BlockParameterCount > 1)
859 Contexts.back().FirstObjCSelectorName->LongestObjCSelectorName = 0;
861 if (Style.isTableGen() &&
Left->is(TT_TableGenListOpener))
862 CurrentToken->setType(TT_TableGenListCloser);
866 if (CurrentToken->isOneOf(tok::r_paren, tok::r_brace))
868 if (CurrentToken->is(tok::colon)) {
869 if (IsCpp11AttributeSpecifier &&
870 CurrentToken->endsSequence(tok::colon, tok::identifier,
874 CurrentToken->setType(TT_AttributeColon);
875 }
else if (!Style.isVerilog() && !Line.InPragmaDirective &&
876 Left->isOneOf(TT_ArraySubscriptLSquare,
877 TT_DesignatedInitializerLSquare)) {
878 Left->setType(TT_ObjCMethodExpr);
879 StartsObjCMethodExpr =
true;
880 Contexts.back().ColonIsObjCMethodExpr =
true;
883 Parent->setType(TT_CastRParen);
888 if (CurrentToken->is(tok::comma) &&
Left->is(TT_ObjCMethodExpr) &&
890 Left->setType(TT_ArrayInitializerLSquare);
892 FormatToken *Tok = CurrentToken;
893 if (Style.isTableGen()) {
894 if (CurrentToken->isOneOf(tok::comma, tok::minus, tok::ellipsis)) {
900 if (!parseTableGenValue())
903 updateParameterCount(Left, Tok);
908 updateParameterCount(Left, Tok);
913 void skipToNextNonComment() {
915 while (CurrentToken && CurrentToken->is(tok::comment))
924 bool parseTableGenValue(
bool ParseNameMode =
false) {
927 while (CurrentToken->is(tok::comment))
929 if (!parseTableGenSimpleValue())
934 if (CurrentToken->is(tok::hash)) {
935 if (CurrentToken->Next &&
936 CurrentToken->Next->isOneOf(tok::colon, tok::semi, tok::l_brace)) {
939 CurrentToken->setType(TT_TableGenTrailingPasteOperator);
943 FormatToken *HashTok = CurrentToken;
944 skipToNextNonComment();
945 HashTok->setType(TT_Unknown);
946 if (!parseTableGenValue(ParseNameMode))
951 if (ParseNameMode && CurrentToken->is(tok::l_brace))
954 if (CurrentToken->isOneOf(tok::l_brace, tok::l_square, tok::period)) {
955 CurrentToken->setType(TT_TableGenValueSuffix);
956 FormatToken *Suffix = CurrentToken;
957 skipToNextNonComment();
958 if (Suffix->is(tok::l_square))
959 return parseSquare();
960 if (Suffix->is(tok::l_brace)) {
961 Scopes.push_back(getScopeType(*Suffix));
971 bool tryToParseTableGenTokVar() {
974 if (CurrentToken->is(tok::identifier) &&
975 CurrentToken->TokenText.front() ==
'$') {
976 skipToNextNonComment();
984 bool parseTableGenDAGArg(
bool AlignColon =
false) {
985 if (tryToParseTableGenTokVar())
987 if (parseTableGenValue()) {
988 if (CurrentToken && CurrentToken->is(tok::colon)) {
990 CurrentToken->setType(TT_TableGenDAGArgListColonToAlign);
992 CurrentToken->setType(TT_TableGenDAGArgListColon);
993 skipToNextNonComment();
994 return tryToParseTableGenTokVar();
1004 bool isTableGenDAGArgBreakingOperator(
const FormatToken &Tok) {
1005 auto &Opes = Style.TableGenBreakingDAGArgOperators;
1010 if (Tok.isNot(tok::identifier) ||
1011 Tok.isOneOf(TT_TableGenBangOperator, TT_TableGenCondOperator)) {
1015 if (!Tok.Next || Tok.Next->is(tok::colon))
1017 return llvm::is_contained(Opes, Tok.TokenText.str());
1022 bool parseTableGenDAGArgAndList(FormatToken *Opener) {
1023 FormatToken *FirstTok = CurrentToken;
1024 if (!parseTableGenDAGArg())
1026 bool BreakInside =
false;
1030 if (isTableGenDAGArgBreakingOperator(*FirstTok)) {
1033 Opener->setType(TT_TableGenDAGArgOpenerToBreak);
1034 if (FirstTok->isOneOf(TT_TableGenBangOperator,
1035 TT_TableGenCondOperator)) {
1038 CurrentToken->Previous->setType(TT_TableGenDAGArgOperatorToBreak);
1039 }
else if (FirstTok->is(tok::identifier)) {
1041 FirstTok->setType(TT_TableGenDAGArgOperatorToBreak);
1043 FirstTok->setType(TT_TableGenDAGArgOperatorID);
1048 bool FirstDAGArgListElm =
true;
1049 while (CurrentToken) {
1050 if (!FirstDAGArgListElm && CurrentToken->is(tok::comma)) {
1051 CurrentToken->setType(BreakInside ? TT_TableGenDAGArgListCommaToBreak
1052 : TT_TableGenDAGArgListComma);
1053 skipToNextNonComment();
1055 if (CurrentToken && CurrentToken->is(tok::r_paren)) {
1056 CurrentToken->setType(TT_TableGenDAGArgCloser);
1057 Opener->MatchingParen = CurrentToken;
1058 CurrentToken->MatchingParen = Opener;
1059 skipToNextNonComment();
1062 if (!parseTableGenDAGArg(
1064 Style.AlignConsecutiveTableGenBreakingDAGArgColons.Enabled)) {
1067 FirstDAGArgListElm =
false;
1072 bool parseTableGenSimpleValue() {
1073 assert(Style.isTableGen());
1076 FormatToken *Tok = CurrentToken;
1077 skipToNextNonComment();
1079 if (Tok->isOneOf(tok::numeric_constant, tok::string_literal,
1080 TT_TableGenMultiLineString, tok::kw_true, tok::kw_false,
1081 tok::question, tok::kw_int)) {
1085 if (Tok->is(tok::l_brace)) {
1086 Scopes.push_back(getScopeType(*Tok));
1087 return parseBrace();
1090 if (Tok->is(tok::l_square)) {
1091 Tok->setType(TT_TableGenListOpener);
1094 if (Tok->is(tok::less)) {
1095 CurrentToken->setType(TT_TemplateOpener);
1096 return parseAngle();
1102 if (Tok->is(tok::l_paren)) {
1103 Tok->setType(TT_TableGenDAGArgOpener);
1104 return parseTableGenDAGArgAndList(Tok);
1107 if (Tok->is(TT_TableGenBangOperator)) {
1108 if (CurrentToken && CurrentToken->is(tok::less)) {
1109 CurrentToken->setType(TT_TemplateOpener);
1110 skipToNextNonComment();
1114 if (!CurrentToken || CurrentToken->isNot(tok::l_paren))
1116 skipToNextNonComment();
1118 Contexts.back().IsTableGenBangOpe =
true;
1119 bool Result = parseParens();
1120 Contexts.back().IsTableGenBangOpe =
false;
1124 if (Tok->is(TT_TableGenCondOperator)) {
1126 skipToNextNonComment();
1127 if (!Tok || Tok->isNot(tok::l_paren))
1129 bool Result = 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)) {
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 (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 }
else if (Contexts.back().ContextType == Context::C11GenericSelection) {
1411 Tok->setType(TT_GenericSelectionColon);
1412 }
else if (CurrentToken && CurrentToken->is(tok::numeric_constant)) {
1413 Tok->setType(TT_BitFieldColon);
1414 }
else if (Contexts.size() == 1 &&
1415 !Line.First->isOneOf(tok::kw_enum, tok::kw_case,
1417 FormatToken *Prev = Tok->getPreviousNonComment();
1420 if (Prev->isOneOf(tok::r_paren, tok::kw_noexcept) ||
1421 Prev->ClosesRequiresClause) {
1422 Tok->setType(TT_CtorInitializerColon);
1423 }
else if (Prev->is(tok::kw_try)) {
1425 FormatToken *PrevPrev = Prev->getPreviousNonComment();
1428 if (PrevPrev && PrevPrev->isOneOf(tok::r_paren, tok::kw_noexcept))
1429 Tok->setType(TT_CtorInitializerColon);
1431 Tok->setType(TT_InheritanceColon);
1432 if (Prev->isAccessSpecifierKeyword())
1435 }
else if (canBeObjCSelectorComponent(*Tok->Previous) && Tok->Next &&
1436 (Tok->Next->isOneOf(tok::r_paren, tok::comma) ||
1437 (canBeObjCSelectorComponent(*Tok->Next) && Tok->Next->Next &&
1438 Tok->Next->Next->is(tok::colon)))) {
1441 Tok->setType(TT_ObjCMethodExpr);
1448 if (Style.isJavaScript() && !Contexts.back().IsExpression)
1449 Tok->setType(TT_JsTypeOperator);
1452 if (Style.isTableGen()) {
1454 if (!parseTableGenValue())
1456 if (CurrentToken && CurrentToken->is(Keywords.kw_then))
1461 CurrentToken->isOneOf(tok::kw_constexpr, tok::identifier)) {
1466 if (CurrentToken && CurrentToken->is(tok::l_paren)) {
1468 if (!parseParens(
true))
1473 if (Style.isJavaScript()) {
1475 if ((Tok->Previous && Tok->Previous->is(tok::period)) ||
1476 (Tok->Next && Tok->Next->is(tok::colon))) {
1480 if (CurrentToken && CurrentToken->is(Keywords.kw_await))
1483 if (IsCpp && CurrentToken && CurrentToken->is(tok::kw_co_await))
1485 Contexts.back().ColonIsForRangeExpr =
true;
1486 if (!CurrentToken || CurrentToken->isNot(tok::l_paren))
1497 if (Tok->Previous && Tok->Previous->is(tok::r_paren) &&
1498 Tok->Previous->MatchingParen &&
1499 Tok->Previous->MatchingParen->is(TT_OverloadedOperatorLParen)) {
1500 Tok->Previous->setType(TT_OverloadedOperator);
1501 Tok->Previous->MatchingParen->setType(TT_OverloadedOperator);
1502 Tok->setType(TT_OverloadedOperatorLParen);
1505 if (Style.isVerilog()) {
1511 auto IsInstancePort = [&]() {
1512 const FormatToken *Prev = Tok->getPreviousNonComment();
1513 const FormatToken *PrevPrev;
1521 if (!Prev || !(PrevPrev = Prev->getPreviousNonComment()))
1524 if (Keywords.isVerilogIdentifier(*Prev) &&
1525 Keywords.isVerilogIdentifier(*PrevPrev)) {
1529 if (Prev->is(Keywords.kw_verilogHash) &&
1530 Keywords.isVerilogIdentifier(*PrevPrev)) {
1534 if (Keywords.isVerilogIdentifier(*Prev) && PrevPrev->is(tok::r_paren))
1537 if (Keywords.isVerilogIdentifier(*Prev) && PrevPrev->is(tok::comma)) {
1538 const FormatToken *PrevParen = PrevPrev->getPreviousNonComment();
1539 if (PrevParen->is(tok::r_paren) && PrevParen->MatchingParen &&
1540 PrevParen->MatchingParen->is(TT_VerilogInstancePortLParen)) {
1547 if (IsInstancePort())
1548 Tok->setFinalizedType(TT_VerilogInstancePortLParen);
1553 if (Line.MustBeDeclaration && Contexts.size() == 1 &&
1554 !Contexts.back().IsExpression && !Line.startsWith(TT_ObjCProperty) &&
1555 !Line.startsWith(tok::l_paren) &&
1556 !Tok->isOneOf(TT_TypeDeclarationParen, TT_RequiresExpressionLParen)) {
1557 if (
const auto *
Previous = Tok->Previous;
1560 !
Previous->isOneOf(TT_RequiresClause, TT_LeadingJavaAnnotation))) {
1561 Line.MightBeFunctionDecl =
true;
1562 Tok->MightBeFunctionDeclParen =
true;
1567 if (Style.isTableGen())
1568 Tok->setType(TT_TableGenListOpener);
1574 FormatToken *
Previous = Tok->getPreviousNonComment();
1576 Previous->setType(TT_SelectorName);
1578 Scopes.push_back(getScopeType(*Tok));
1584 Tok->setType(TT_TemplateOpener);
1592 Tok->Previous->isOneOf(TT_SelectorName, TT_DictLiteral))) {
1593 Tok->setType(TT_DictLiteral);
1594 FormatToken *
Previous = Tok->getPreviousNonComment();
1596 Previous->setType(TT_SelectorName);
1598 if (Style.isTableGen())
1599 Tok->setType(TT_TemplateOpener);
1601 Tok->setType(TT_BinaryOperator);
1602 NonTemplateLess.insert(Tok);
1612 if (!Scopes.empty())
1620 Tok->setType(TT_BinaryOperator);
1621 if (Tok->Previous && Tok->Previous->is(TT_TemplateCloser))
1622 Tok->SpacesRequiredBefore = 1;
1624 case tok::kw_operator:
1625 if (Style.isProto())
1627 while (CurrentToken &&
1628 !CurrentToken->isOneOf(tok::l_paren, tok::semi, tok::r_paren)) {
1629 if (CurrentToken->isOneOf(tok::star, tok::amp))
1630 CurrentToken->setType(TT_PointerOrReference);
1631 auto Next = CurrentToken->getNextNonComment();
1634 if (Next->is(tok::less))
1640 auto Previous = CurrentToken->getPreviousNonComment();
1642 if (CurrentToken->is(tok::comma) &&
Previous->isNot(tok::kw_operator))
1644 if (
Previous->isOneOf(TT_BinaryOperator, TT_UnaryOperator, tok::comma,
1645 tok::star, tok::arrow, tok::amp, tok::ampamp) ||
1647 Previous->TokenText.starts_with(
"\"\"")) {
1648 Previous->setType(TT_OverloadedOperator);
1649 if (CurrentToken->isOneOf(tok::less, tok::greater))
1653 if (CurrentToken && CurrentToken->is(tok::l_paren))
1654 CurrentToken->setType(TT_OverloadedOperatorLParen);
1655 if (CurrentToken && CurrentToken->Previous->is(TT_BinaryOperator))
1656 CurrentToken->Previous->setType(TT_OverloadedOperator);
1659 if (Style.isJavaScript() && Tok->Next &&
1660 Tok->Next->isOneOf(tok::semi, tok::comma, tok::colon, tok::r_paren,
1661 tok::r_brace, tok::r_square)) {
1666 Tok->setType(TT_JsTypeOptionalQuestion);
1671 if (Line.MustBeDeclaration && !Contexts.back().IsExpression &&
1672 Style.isJavaScript()) {
1675 if (Style.isCSharp()) {
1681 (Tok->Next->startsSequence(tok::question, tok::r_paren) ||
1682 Tok->Next->startsSequence(tok::question, tok::greater) ||
1683 Tok->Next->startsSequence(tok::question, tok::identifier,
1685 Tok->setType(TT_CSharpNullable);
1690 if (Tok->Next && Tok->Next->is(tok::identifier) && Tok->Next->Next &&
1691 Tok->Next->Next->is(tok::equal)) {
1692 Tok->setType(TT_CSharpNullable);
1701 if (!Contexts.back().IsExpression && Line.MustBeDeclaration &&
1703 !Tok->Next->isOneOf(tok::identifier, tok::string_literal) ||
1705 !Tok->Next->Next->isOneOf(tok::colon, tok::question))) {
1706 Tok->setType(TT_CSharpNullable);
1712 case tok::kw_template:
1713 parseTemplateDeclaration();
1716 switch (Contexts.back().ContextType) {
1717 case Context::CtorInitializer:
1718 Tok->setType(TT_CtorInitializerComma);
1720 case Context::InheritanceList:
1721 Tok->setType(TT_InheritanceComma);
1723 case Context::VerilogInstancePortList:
1724 Tok->setFinalizedType(TT_VerilogInstancePortComma);
1727 if (Style.isVerilog() && Contexts.size() == 1 &&
1728 Line.startsWith(Keywords.kw_assign)) {
1729 Tok->setFinalizedType(TT_VerilogAssignComma);
1730 }
else if (Contexts.back().FirstStartOfName &&
1731 (Contexts.size() == 1 || startsWithInitStatement(Line))) {
1732 Contexts.back().FirstStartOfName->PartOfMultiVariableDeclStmt =
true;
1733 Line.IsMultiVariableDeclStmt =
true;
1737 if (Contexts.back().ContextType == Context::ForEachMacro)
1738 Contexts.back().IsExpression =
true;
1740 case tok::kw_default:
1742 if (Style.isVerilog() && Keywords.isVerilogEndOfLabel(*Tok) &&
1743 (Line.Level > 1 || (!Line.InPPDirective && Line.Level > 0))) {
1747 case tok::identifier:
1748 if (Tok->isOneOf(Keywords.kw___has_include,
1749 Keywords.kw___has_include_next)) {
1752 if (Style.isCSharp() && Tok->is(Keywords.kw_where) && Tok->Next &&
1753 Tok->Next->isNot(tok::l_paren)) {
1754 Tok->setType(TT_CSharpGenericTypeConstraint);
1755 parseCSharpGenericTypeConstraint();
1756 if (!Tok->getPreviousNonComment())
1757 Line.IsContinuation =
true;
1759 if (Style.isTableGen()) {
1760 if (Tok->is(Keywords.kw_assert)) {
1761 if (!parseTableGenValue())
1763 }
else if (Tok->isOneOf(Keywords.kw_def, Keywords.kw_defm) &&
1765 !Tok->Next->isOneOf(tok::colon, tok::l_brace))) {
1767 if (!parseTableGenValue(
true))
1773 if (Tok->Previous && Tok->Previous->is(tok::kw_noexcept))
1774 Tok->setType(TT_TrailingReturnArrow);
1778 if (Style.isTableGen() && !parseTableGenValue())
1787 void parseCSharpGenericTypeConstraint() {
1788 int OpenAngleBracketsCount = 0;
1789 while (CurrentToken) {
1790 if (CurrentToken->is(tok::less)) {
1792 CurrentToken->setType(TT_TemplateOpener);
1793 ++OpenAngleBracketsCount;
1795 }
else if (CurrentToken->is(tok::greater)) {
1796 CurrentToken->setType(TT_TemplateCloser);
1797 --OpenAngleBracketsCount;
1799 }
else if (CurrentToken->is(tok::comma) && OpenAngleBracketsCount == 0) {
1802 CurrentToken->setType(TT_CSharpGenericTypeConstraintComma);
1804 }
else if (CurrentToken->is(Keywords.kw_where)) {
1805 CurrentToken->setType(TT_CSharpGenericTypeConstraint);
1807 }
else if (CurrentToken->is(tok::colon)) {
1808 CurrentToken->setType(TT_CSharpGenericTypeConstraintColon);
1816 void parseIncludeDirective() {
1817 if (CurrentToken && CurrentToken->is(tok::less)) {
1819 while (CurrentToken) {
1822 if (CurrentToken->isNot(tok::comment) &&
1823 !CurrentToken->TokenText.starts_with(
"//")) {
1824 CurrentToken->setType(TT_ImplicitStringLiteral);
1831 void parseWarningOrError() {
1836 while (CurrentToken) {
1837 CurrentToken->setType(TT_ImplicitStringLiteral);
1842 void parsePragma() {
1845 CurrentToken->isOneOf(Keywords.kw_mark, Keywords.kw_option,
1846 Keywords.kw_region)) {
1847 bool IsMarkOrRegion =
1848 CurrentToken->isOneOf(Keywords.kw_mark, Keywords.kw_region);
1851 while (CurrentToken) {
1852 if (IsMarkOrRegion || CurrentToken->Previous->is(TT_BinaryOperator))
1853 CurrentToken->setType(TT_ImplicitStringLiteral);
1859 void parseHasInclude() {
1860 if (!CurrentToken || CurrentToken->isNot(tok::l_paren))
1863 parseIncludeDirective();
1867 LineType parsePreprocessorDirective() {
1868 bool IsFirstToken = CurrentToken->IsFirst;
1874 if (Style.isJavaScript() && IsFirstToken) {
1878 while (CurrentToken) {
1880 CurrentToken->setType(TT_ImplicitStringLiteral);
1886 if (CurrentToken->is(tok::numeric_constant)) {
1887 CurrentToken->SpacesRequiredBefore = 1;
1892 if (!CurrentToken->Tok.getIdentifierInfo())
1896 if (Style.isVerilog() && !Keywords.isVerilogPPDirective(*CurrentToken))
1898 switch (CurrentToken->Tok.getIdentifierInfo()->getPPKeywordID()) {
1899 case tok::pp_include:
1900 case tok::pp_include_next:
1901 case tok::pp_import:
1903 parseIncludeDirective();
1907 case tok::pp_warning:
1908 parseWarningOrError();
1910 case tok::pp_pragma:
1915 Contexts.back().IsExpression =
true;
1918 CurrentToken->SpacesRequiredBefore =
true;
1924 while (CurrentToken) {
1925 FormatToken *Tok = CurrentToken;
1927 if (Tok->is(tok::l_paren)) {
1929 }
else if (Tok->isOneOf(Keywords.kw___has_include,
1930 Keywords.kw___has_include_next)) {
1941 NonTemplateLess.clear();
1942 if (!Line.InMacroBody && CurrentToken->is(tok::hash)) {
1946 auto Type = parsePreprocessorDirective();
1954 IdentifierInfo *Info = CurrentToken->Tok.getIdentifierInfo();
1956 CurrentToken->is(Keywords.kw_package)) ||
1957 (!Style.isVerilog() && Info &&
1958 Info->getPPKeywordID() == tok::pp_import && CurrentToken->Next &&
1959 CurrentToken->Next->isOneOf(tok::string_literal, tok::identifier,
1962 parseIncludeDirective();
1968 if (CurrentToken->is(tok::less) && Line.Last->is(tok::greater)) {
1969 parseIncludeDirective();
1976 CurrentToken->isOneOf(Keywords.kw_option, Keywords.kw_package)) {
1978 if (CurrentToken && CurrentToken->is(tok::identifier)) {
1979 while (CurrentToken)
1985 bool KeywordVirtualFound =
false;
1986 bool ImportStatement =
false;
1989 if (Style.isJavaScript() && CurrentToken->is(Keywords.kw_import))
1990 ImportStatement =
true;
1992 while (CurrentToken) {
1993 if (CurrentToken->is(tok::kw_virtual))
1994 KeywordVirtualFound =
true;
1995 if (Style.isJavaScript()) {
2002 if (Line.First->is(tok::kw_export) &&
2003 CurrentToken->is(Keywords.kw_from) && CurrentToken->Next &&
2004 CurrentToken->Next->isStringLiteral()) {
2005 ImportStatement =
true;
2007 if (isClosureImportStatement(*CurrentToken))
2008 ImportStatement =
true;
2010 if (!consumeToken())
2015 if (KeywordVirtualFound)
2017 if (ImportStatement)
2020 if (Line.startsWith(TT_ObjCMethodSpecifier)) {
2021 if (Contexts.back().FirstObjCSelectorName) {
2022 Contexts.back().FirstObjCSelectorName->LongestObjCSelectorName =
2023 Contexts.back().LongestObjCSelectorName;
2028 for (
const auto &ctx : Contexts)
2029 if (ctx.ContextType == Context::StructArrayInitializer)
2036 bool isClosureImportStatement(
const FormatToken &Tok) {
2039 return Tok.TokenText ==
"goog" && Tok.Next && Tok.Next->is(tok::period) &&
2041 (Tok.Next->Next->TokenText ==
"module" ||
2042 Tok.Next->Next->TokenText ==
"provide" ||
2043 Tok.Next->Next->TokenText ==
"require" ||
2044 Tok.Next->Next->TokenText ==
"requireType" ||
2045 Tok.Next->Next->TokenText ==
"forwardDeclare") &&
2046 Tok.Next->Next->Next && Tok.Next->Next->Next->is(tok::l_paren);
2049 void resetTokenMetadata() {
2055 if (!CurrentToken->isTypeFinalized() &&
2056 !CurrentToken->isOneOf(
2057 TT_LambdaLSquare, TT_LambdaLBrace, TT_AttributeMacro, TT_IfMacro,
2058 TT_ForEachMacro, TT_TypenameMacro, TT_FunctionLBrace,
2059 TT_ImplicitStringLiteral, TT_InlineASMBrace, TT_FatArrow,
2060 TT_NamespaceMacro, TT_OverloadedOperator, TT_RegexLiteral,
2061 TT_TemplateString, TT_ObjCStringLiteral, TT_UntouchableMacroFunc,
2062 TT_StatementAttributeLikeMacro, TT_FunctionLikeOrFreestandingMacro,
2063 TT_ClassLBrace, TT_EnumLBrace, TT_RecordLBrace, TT_StructLBrace,
2064 TT_UnionLBrace, TT_RequiresClause,
2065 TT_RequiresClauseInARequiresExpression, TT_RequiresExpression,
2066 TT_RequiresExpressionLParen, TT_RequiresExpressionLBrace,
2067 TT_BracedListLBrace)) {
2068 CurrentToken->setType(TT_Unknown);
2070 CurrentToken->Role.reset();
2071 CurrentToken->MatchingParen =
nullptr;
2072 CurrentToken->FakeLParens.clear();
2073 CurrentToken->FakeRParens = 0;
2080 CurrentToken->NestingLevel = Contexts.size() - 1;
2081 CurrentToken->BindingStrength = Contexts.back().BindingStrength;
2082 modifyContext(*CurrentToken);
2083 determineTokenType(*CurrentToken);
2084 CurrentToken = CurrentToken->Next;
2086 resetTokenMetadata();
2129 StructArrayInitializer,
2133 C11GenericSelection,
2135 VerilogInstancePortList,
2141 struct ScopedContextCreator {
2142 AnnotatingParser &
P;
2148 P.Contexts.back().BindingStrength + Increase,
2149 P.Contexts.back().IsExpression));
2152 ~ScopedContextCreator() {
2154 if (
P.Contexts.back().ContextType == Context::StructArrayInitializer) {
2155 P.Contexts.pop_back();
2156 P.Contexts.back().ContextType = Context::StructArrayInitializer;
2160 P.Contexts.pop_back();
2164 void modifyContext(
const FormatToken &Current) {
2165 auto AssignmentStartsExpression = [&]() {
2169 if (Line.First->isOneOf(tok::kw_using, tok::kw_return))
2171 if (Line.First->is(tok::kw_template)) {
2172 assert(Current.Previous);
2173 if (Current.Previous->is(tok::kw_operator)) {
2179 const FormatToken *Tok = Line.First->getNextNonComment();
2181 if (Tok->isNot(TT_TemplateOpener)) {
2188 if (Contexts.back().ContextKind == tok::less) {
2189 assert(Current.Previous->Previous);
2190 return !Current.Previous->Previous->isOneOf(tok::kw_typename,
2194 Tok = Tok->MatchingParen;
2197 Tok = Tok->getNextNonComment();
2201 if (Tok->isOneOf(tok::kw_class, tok::kw_enum, tok::kw_struct,
2211 if (Style.isJavaScript() &&
2212 (Line.startsWith(Keywords.kw_type, tok::identifier) ||
2213 Line.startsWith(tok::kw_export, Keywords.kw_type,
2214 tok::identifier))) {
2218 return !Current.Previous || Current.Previous->isNot(tok::kw_operator);
2221 if (AssignmentStartsExpression()) {
2222 Contexts.back().IsExpression =
true;
2223 if (!Line.startsWith(TT_UnaryOperator)) {
2224 for (FormatToken *
Previous = Current.Previous;
2226 !
Previous->Previous->isOneOf(tok::comma, tok::semi);
2228 if (
Previous->isOneOf(tok::r_square, tok::r_paren, tok::greater)) {
2235 if (
Previous->isOneOf(TT_BinaryOperator, TT_UnaryOperator) &&
2237 Previous->Previous->isNot(tok::equal)) {
2238 Previous->setType(TT_PointerOrReference);
2242 }
else if (Current.is(tok::lessless) &&
2243 (!Current.Previous ||
2244 Current.Previous->isNot(tok::kw_operator))) {
2245 Contexts.back().IsExpression =
true;
2246 }
else if (Current.isOneOf(tok::kw_return, tok::kw_throw)) {
2247 Contexts.back().IsExpression =
true;
2248 }
else if (Current.is(TT_TrailingReturnArrow)) {
2249 Contexts.back().IsExpression =
false;
2250 }
else if (Current.is(Keywords.kw_assert)) {
2252 }
else if (Current.Previous &&
2253 Current.Previous->is(TT_CtorInitializerColon)) {
2254 Contexts.back().IsExpression =
true;
2255 Contexts.back().ContextType = Context::CtorInitializer;
2256 }
else if (Current.Previous && Current.Previous->is(TT_InheritanceColon)) {
2257 Contexts.back().ContextType = Context::InheritanceList;
2258 }
else if (Current.isOneOf(tok::r_paren, tok::greater, tok::comma)) {
2259 for (FormatToken *
Previous = Current.Previous;
2262 Previous->setType(TT_PointerOrReference);
2264 if (Line.MustBeDeclaration &&
2265 Contexts.front().ContextType != Context::CtorInitializer) {
2266 Contexts.back().IsExpression =
false;
2268 }
else if (Current.is(tok::kw_new)) {
2269 Contexts.back().CanBeExpression =
false;
2270 }
else if (Current.is(tok::semi) ||
2271 (Current.is(tok::exclaim) && Current.Previous &&
2272 Current.Previous->isNot(tok::kw_operator))) {
2276 Contexts.back().IsExpression =
true;
2280 static FormatToken *untilMatchingParen(FormatToken *Current) {
2284 if (Current->is(tok::l_paren))
2286 if (Current->is(tok::r_paren))
2290 Current = Current->Next;
2295 static bool isDeductionGuide(FormatToken &Current) {
2297 if (Current.Previous && Current.Previous->is(tok::r_paren) &&
2298 Current.startsSequence(tok::arrow, tok::identifier, tok::less)) {
2300 FormatToken *TemplateCloser = Current.Next->Next;
2301 int NestingLevel = 0;
2302 while (TemplateCloser) {
2304 if (TemplateCloser->is(tok::l_paren)) {
2306 TemplateCloser = untilMatchingParen(TemplateCloser);
2307 if (!TemplateCloser)
2310 if (TemplateCloser->is(tok::less))
2312 if (TemplateCloser->is(tok::greater))
2314 if (NestingLevel < 1)
2316 TemplateCloser = TemplateCloser->Next;
2320 if (TemplateCloser && TemplateCloser->Next &&
2321 TemplateCloser->Next->is(tok::semi) &&
2322 Current.Previous->MatchingParen) {
2325 FormatToken *LeadingIdentifier =
2326 Current.Previous->MatchingParen->Previous;
2328 return LeadingIdentifier &&
2329 LeadingIdentifier->TokenText == Current.Next->TokenText;
2335 void determineTokenType(FormatToken &Current) {
2336 if (Current.isNot(TT_Unknown)) {
2341 if ((Style.isJavaScript() || Style.isCSharp()) &&
2342 Current.is(tok::exclaim)) {
2343 if (Current.Previous) {
2345 Style.isJavaScript()
2346 ? Keywords.isJavaScriptIdentifier(
2347 *Current.Previous,
true)
2348 : Current.Previous->is(tok::identifier);
2350 Current.Previous->isOneOf(
2351 tok::kw_default, tok::kw_namespace, tok::r_paren, tok::r_square,
2352 tok::r_brace, tok::kw_false, tok::kw_true, Keywords.kw_type,
2353 Keywords.kw_get, Keywords.kw_init, Keywords.kw_set) ||
2354 Current.Previous->Tok.isLiteral()) {
2355 Current.setType(TT_NonNullAssertion);
2360 Current.Next->isOneOf(TT_BinaryOperator, Keywords.kw_as)) {
2361 Current.setType(TT_NonNullAssertion);
2370 Current.is(Keywords.kw_instanceof)) {
2371 Current.setType(TT_BinaryOperator);
2372 }
else if (isStartOfName(Current) &&
2373 (!Line.MightBeFunctionDecl || Current.NestingLevel != 0)) {
2374 Contexts.back().FirstStartOfName = &Current;
2375 Current.setType(TT_StartOfName);
2376 }
else if (Current.is(tok::semi)) {
2380 Contexts.back().FirstStartOfName =
nullptr;
2381 }
else if (Current.isOneOf(tok::kw_auto, tok::kw___auto_type)) {
2383 }
else if (Current.is(tok::arrow) &&
2385 Current.setType(TT_TrailingReturnArrow);
2386 }
else if (Current.is(tok::arrow) && Style.isVerilog()) {
2388 Current.setType(TT_BinaryOperator);
2389 }
else if (Current.is(tok::arrow) && AutoFound &&
2390 Line.MightBeFunctionDecl && Current.NestingLevel == 0 &&
2391 !Current.Previous->isOneOf(tok::kw_operator, tok::identifier)) {
2393 Current.setType(TT_TrailingReturnArrow);
2394 }
else if (Current.is(tok::arrow) && Current.Previous &&
2395 Current.Previous->is(tok::r_brace)) {
2398 Current.setType(TT_TrailingReturnArrow);
2399 }
else if (isDeductionGuide(Current)) {
2401 Current.setType(TT_TrailingReturnArrow);
2402 }
else if (Current.isPointerOrReference()) {
2403 Current.setType(determineStarAmpUsage(
2405 Contexts.back().CanBeExpression && Contexts.back().IsExpression,
2406 Contexts.back().ContextType == Context::TemplateArgument));
2407 }
else if (Current.isOneOf(tok::minus, tok::plus, tok::caret) ||
2408 (Style.isVerilog() && Current.is(tok::pipe))) {
2409 Current.setType(determinePlusMinusCaretUsage(Current));
2410 if (Current.is(TT_UnaryOperator) && Current.is(tok::caret))
2411 Contexts.back().CaretFound =
true;
2412 }
else if (Current.isOneOf(tok::minusminus, tok::plusplus)) {
2413 Current.setType(determineIncrementUsage(Current));
2414 }
else if (Current.isOneOf(tok::exclaim, tok::tilde)) {
2415 Current.setType(TT_UnaryOperator);
2416 }
else if (Current.is(tok::question)) {
2417 if (Style.isJavaScript() && Line.MustBeDeclaration &&
2418 !Contexts.back().IsExpression) {
2421 Current.setType(TT_JsTypeOptionalQuestion);
2422 }
else if (Style.isTableGen()) {
2424 Current.setType(TT_Unknown);
2426 Current.setType(TT_ConditionalExpr);
2428 }
else if (Current.isBinaryOperator() &&
2429 (!Current.Previous || Current.Previous->isNot(tok::l_square)) &&
2430 (Current.isNot(tok::greater) &&
2432 if (Style.isVerilog()) {
2433 if (Current.is(tok::lessequal) && Contexts.size() == 1 &&
2434 !Contexts.back().VerilogAssignmentFound) {
2438 Current.setFinalizedType(TT_BinaryOperator);
2441 Contexts.back().VerilogAssignmentFound =
true;
2443 Current.setType(TT_BinaryOperator);
2444 }
else if (Current.is(tok::comment)) {
2445 if (Current.TokenText.starts_with(
"/*")) {
2446 if (Current.TokenText.ends_with(
"*/")) {
2447 Current.setType(TT_BlockComment);
2451 Current.Tok.setKind(tok::unknown);
2454 Current.setType(TT_LineComment);
2456 }
else if (Current.is(tok::string_literal)) {
2457 if (Style.isVerilog() && Contexts.back().VerilogMayBeConcatenation &&
2458 Current.getPreviousNonComment() &&
2459 Current.getPreviousNonComment()->isOneOf(tok::comma, tok::l_brace) &&
2460 Current.getNextNonComment() &&
2461 Current.getNextNonComment()->isOneOf(tok::comma, tok::r_brace)) {
2462 Current.setType(TT_StringInConcatenation);
2464 }
else if (Current.is(tok::l_paren)) {
2465 if (lParenStartsCppCast(Current))
2466 Current.setType(TT_CppCastLParen);
2467 }
else if (Current.is(tok::r_paren)) {
2468 if (rParenEndsCast(Current))
2469 Current.setType(TT_CastRParen);
2470 if (Current.MatchingParen && Current.Next &&
2471 !Current.Next->isBinaryOperator() &&
2472 !Current.Next->isOneOf(
2473 tok::semi, tok::colon, tok::l_brace, tok::l_paren, tok::comma,
2474 tok::period, tok::arrow, tok::coloncolon, tok::kw_noexcept)) {
2475 if (FormatToken *AfterParen = Current.MatchingParen->Next;
2476 AfterParen && AfterParen->isNot(tok::caret)) {
2478 if (FormatToken *BeforeParen = Current.MatchingParen->Previous;
2479 BeforeParen && BeforeParen->is(tok::identifier) &&
2480 BeforeParen->isNot(TT_TypenameMacro) &&
2481 BeforeParen->TokenText == BeforeParen->TokenText.upper() &&
2482 (!BeforeParen->Previous ||
2483 BeforeParen->Previous->ClosesTemplateDeclaration ||
2484 BeforeParen->Previous->ClosesRequiresClause)) {
2485 Current.setType(TT_FunctionAnnotationRParen);
2489 }
else if (Current.is(tok::at) && Current.Next && !Style.isJavaScript() &&
2493 switch (Current.Next->Tok.getObjCKeywordID()) {
2494 case tok::objc_interface:
2495 case tok::objc_implementation:
2496 case tok::objc_protocol:
2497 Current.setType(TT_ObjCDecl);
2499 case tok::objc_property:
2500 Current.setType(TT_ObjCProperty);
2505 }
else if (Current.is(tok::period)) {
2506 FormatToken *PreviousNoComment = Current.getPreviousNonComment();
2507 if (PreviousNoComment &&
2508 PreviousNoComment->isOneOf(tok::comma, tok::l_brace)) {
2509 Current.setType(TT_DesignatedInitializerPeriod);
2511 Current.Previous->isOneOf(TT_JavaAnnotation,
2512 TT_LeadingJavaAnnotation)) {
2513 Current.setType(Current.Previous->getType());
2515 }
else if (canBeObjCSelectorComponent(Current) &&
2518 Current.Previous && Current.Previous->is(TT_CastRParen) &&
2519 Current.Previous->MatchingParen &&
2520 Current.Previous->MatchingParen->Previous &&
2521 Current.Previous->MatchingParen->Previous->is(
2522 TT_ObjCMethodSpecifier)) {
2526 Current.setType(TT_SelectorName);
2527 }
else if (Current.isOneOf(tok::identifier, tok::kw_const, tok::kw_noexcept,
2528 tok::kw_requires) &&
2530 !Current.Previous->isOneOf(tok::equal, tok::at,
2531 TT_CtorInitializerComma,
2532 TT_CtorInitializerColon) &&
2533 Line.MightBeFunctionDecl && Contexts.size() == 1) {
2536 Current.setType(TT_TrailingAnnotation);
2538 Style.isJavaScript()) &&
2540 if (Current.Previous->is(tok::at) &&
2541 Current.isNot(Keywords.kw_interface)) {
2542 const FormatToken &AtToken = *Current.Previous;
2543 const FormatToken *
Previous = AtToken.getPreviousNonComment();
2545 Current.setType(TT_LeadingJavaAnnotation);
2547 Current.setType(TT_JavaAnnotation);
2548 }
else if (Current.Previous->is(tok::period) &&
2549 Current.Previous->isOneOf(TT_JavaAnnotation,
2550 TT_LeadingJavaAnnotation)) {
2551 Current.setType(Current.Previous->getType());
2561 bool isStartOfName(
const FormatToken &Tok) {
2563 if (Style.isVerilog())
2566 if (Tok.isNot(tok::identifier) || !Tok.Previous)
2569 if (
const auto *NextNonComment = Tok.getNextNonComment();
2570 (!NextNonComment && !Line.InMacroBody) ||
2572 (NextNonComment->isPointerOrReference() ||
2573 NextNonComment->is(tok::string_literal) ||
2574 (Line.InPragmaDirective && NextNonComment->is(tok::identifier))))) {
2578 if (Tok.Previous->isOneOf(TT_LeadingJavaAnnotation, Keywords.kw_instanceof,
2582 if (Style.isJavaScript() && Tok.Previous->is(Keywords.kw_in))
2586 FormatToken *PreviousNotConst = Tok.getPreviousNonComment();
2589 if (!Style.isJavaScript())
2590 while (PreviousNotConst && PreviousNotConst->is(tok::kw_const))
2591 PreviousNotConst = PreviousNotConst->getPreviousNonComment();
2593 if (!PreviousNotConst)
2596 if (PreviousNotConst->ClosesRequiresClause)
2599 if (Style.isTableGen()) {
2601 if (Keywords.isTableGenDefinition(*PreviousNotConst))
2604 if (Contexts.back().ContextKind != tok::l_brace)
2608 bool IsPPKeyword = PreviousNotConst->is(tok::identifier) &&
2609 PreviousNotConst->Previous &&
2610 PreviousNotConst->Previous->is(tok::hash);
2612 if (PreviousNotConst->is(TT_TemplateCloser)) {
2613 return PreviousNotConst && PreviousNotConst->MatchingParen &&
2614 PreviousNotConst->MatchingParen->Previous &&
2615 PreviousNotConst->MatchingParen->Previous->isNot(tok::period) &&
2616 PreviousNotConst->MatchingParen->Previous->isNot(tok::kw_template);
2619 if ((PreviousNotConst->is(tok::r_paren) &&
2620 PreviousNotConst->is(TT_TypeDeclarationParen)) ||
2621 PreviousNotConst->is(TT_AttributeRParen)) {
2630 if (PreviousNotConst->isOneOf(tok::identifier, tok::kw_auto) &&
2631 PreviousNotConst->isNot(TT_StatementAttributeLikeMacro)) {
2636 if (PreviousNotConst->is(TT_PointerOrReference))
2640 if (PreviousNotConst->isTypeName(LangOpts))
2645 PreviousNotConst->is(tok::r_square)) {
2650 return Style.isJavaScript() && PreviousNotConst->is(tok::kw_const);
2654 bool lParenStartsCppCast(
const FormatToken &Tok) {
2659 FormatToken *LeftOfParens = Tok.getPreviousNonComment();
2660 if (LeftOfParens && LeftOfParens->is(TT_TemplateCloser) &&
2661 LeftOfParens->MatchingParen) {
2662 auto *Prev = LeftOfParens->MatchingParen->getPreviousNonComment();
2664 Prev->isOneOf(tok::kw_const_cast, tok::kw_dynamic_cast,
2665 tok::kw_reinterpret_cast, tok::kw_static_cast)) {
2675 bool rParenEndsCast(
const FormatToken &Tok) {
2676 assert(Tok.is(tok::r_paren));
2678 if (!Tok.MatchingParen || !Tok.Previous)
2685 const auto *LParen = Tok.MatchingParen;
2686 const auto *BeforeRParen = Tok.Previous;
2687 const auto *AfterRParen = Tok.Next;
2690 if (BeforeRParen == LParen || !AfterRParen)
2693 if (LParen->is(TT_OverloadedOperatorLParen))
2696 auto *LeftOfParens = LParen->getPreviousNonComment();
2700 if (LeftOfParens->is(tok::r_paren) &&
2701 LeftOfParens->isNot(TT_CastRParen)) {
2702 if (!LeftOfParens->MatchingParen ||
2703 !LeftOfParens->MatchingParen->Previous) {
2706 LeftOfParens = LeftOfParens->MatchingParen->Previous;
2709 if (LeftOfParens->is(tok::r_square)) {
2711 auto MayBeArrayDelete = [](FormatToken *Tok) -> FormatToken * {
2712 if (Tok->isNot(tok::r_square))
2715 Tok = Tok->getPreviousNonComment();
2716 if (!Tok || Tok->isNot(tok::l_square))
2719 Tok = Tok->getPreviousNonComment();
2720 if (!Tok || Tok->isNot(tok::kw_delete))
2724 if (FormatToken *MaybeDelete = MayBeArrayDelete(LeftOfParens))
2725 LeftOfParens = MaybeDelete;
2731 if (LeftOfParens->Tok.getIdentifierInfo() && LeftOfParens->Previous &&
2732 LeftOfParens->Previous->is(tok::kw_operator)) {
2738 if (LeftOfParens->Tok.getIdentifierInfo() &&
2739 !LeftOfParens->isOneOf(Keywords.kw_in, tok::kw_return, tok::kw_case,
2740 tok::kw_delete, tok::kw_throw)) {
2746 if (LeftOfParens->isOneOf(tok::at, tok::r_square, TT_OverloadedOperator,
2747 TT_TemplateCloser, tok::ellipsis)) {
2752 if (AfterRParen->is(tok::question) ||
2753 (AfterRParen->is(tok::ampamp) && !BeforeRParen->isTypeName(LangOpts))) {
2758 if (AfterRParen->is(Keywords.kw_in) && Style.isCSharp())
2763 if (AfterRParen->isOneOf(tok::kw_noexcept, tok::kw_volatile, tok::kw_const,
2764 tok::kw_requires, tok::kw_throw, tok::arrow,
2765 Keywords.kw_override, Keywords.kw_final) ||
2766 isCppAttribute(IsCpp, *AfterRParen)) {
2776 if (AfterRParen->isOneOf(tok::kw_sizeof, tok::kw_alignof) ||
2777 (AfterRParen->Tok.isLiteral() &&
2778 AfterRParen->isNot(tok::string_literal))) {
2783 auto IsQualifiedPointerOrReference = [](
const FormatToken *
T,
2784 const LangOptions &LangOpts) {
2786 assert(!
T->isTypeName(LangOpts) &&
"Should have already been checked");
2790 if (
T->is(TT_AttributeRParen)) {
2792 assert(
T->is(tok::r_paren));
2793 assert(
T->MatchingParen);
2794 assert(
T->MatchingParen->is(tok::l_paren));
2795 assert(
T->MatchingParen->is(TT_AttributeLParen));
2796 if (
const auto *Tok =
T->MatchingParen->Previous;
2797 Tok && Tok->isAttribute()) {
2801 }
else if (
T->is(TT_AttributeSquare)) {
2803 if (
T->MatchingParen &&
T->MatchingParen->Previous) {
2804 T =
T->MatchingParen->Previous;
2807 }
else if (
T->canBePointerOrReferenceQualifier()) {
2813 return T &&
T->is(TT_PointerOrReference);
2815 bool ParensAreType =
2816 BeforeRParen->isOneOf(TT_TemplateCloser, TT_TypeDeclarationParen) ||
2817 BeforeRParen->isTypeName(LangOpts) ||
2818 IsQualifiedPointerOrReference(BeforeRParen, LangOpts);
2819 bool ParensCouldEndDecl =
2820 AfterRParen->isOneOf(tok::equal, tok::semi, tok::l_brace, tok::greater);
2821 if (ParensAreType && !ParensCouldEndDecl)
2832 for (
const auto *Token = LParen->Next; Token != &Tok; Token = Token->Next)
2833 if (Token->is(TT_BinaryOperator))
2838 if (AfterRParen->isOneOf(tok::identifier, tok::kw_this))
2842 if (AfterRParen->is(tok::l_paren) && BeforeRParen->Previous) {
2843 if (BeforeRParen->is(tok::identifier) &&
2844 BeforeRParen->Previous->is(tok::l_paren)) {
2849 if (!AfterRParen->Next)
2852 if (AfterRParen->is(tok::l_brace) &&
2860 const bool NextIsAmpOrStar = AfterRParen->isOneOf(tok::amp, tok::star);
2861 if (!(AfterRParen->isUnaryOperator() || NextIsAmpOrStar) ||
2862 AfterRParen->is(tok::plus) ||
2863 !AfterRParen->Next->isOneOf(tok::identifier, tok::numeric_constant)) {
2867 if (NextIsAmpOrStar &&
2868 (AfterRParen->Next->is(tok::numeric_constant) || Line.InPPDirective)) {
2872 if (Line.InPPDirective && AfterRParen->is(tok::minus))
2876 for (
auto *Prev = BeforeRParen; Prev != LParen; Prev = Prev->Previous) {
2877 if (Prev->is(tok::r_paren)) {
2878 Prev = Prev->MatchingParen;
2881 if (Prev->is(TT_FunctionTypeLParen))
2885 if (!Prev->isOneOf(tok::kw_const, tok::identifier, tok::coloncolon))
2893 bool determineUnaryOperatorByUsage(
const FormatToken &Tok) {
2894 const FormatToken *PrevToken = Tok.getPreviousNonComment();
2904 if (PrevToken->isOneOf(
2905 TT_ConditionalExpr, tok::l_paren, tok::comma, tok::colon, tok::semi,
2906 tok::equal, tok::question, tok::l_square, tok::l_brace,
2907 tok::kw_case, tok::kw_co_await, tok::kw_co_return, tok::kw_co_yield,
2908 tok::kw_delete, tok::kw_return, tok::kw_throw)) {
2915 if (PrevToken->is(tok::kw_sizeof))
2919 if (PrevToken->isOneOf(TT_CastRParen, TT_UnaryOperator))
2923 if (PrevToken->is(TT_BinaryOperator))
2931 bool InTemplateArgument) {
2932 if (Style.isJavaScript())
2933 return TT_BinaryOperator;
2936 if (Style.isCSharp() && Tok.is(tok::ampamp))
2937 return TT_BinaryOperator;
2939 if (Style.isVerilog()) {
2944 if (Tok.is(tok::star))
2945 return TT_BinaryOperator;
2946 return determineUnaryOperatorByUsage(Tok) ? TT_UnaryOperator
2947 : TT_BinaryOperator;
2950 const FormatToken *PrevToken = Tok.getPreviousNonComment();
2952 return TT_UnaryOperator;
2953 if (PrevToken->is(TT_TypeName))
2954 return TT_PointerOrReference;
2955 if (PrevToken->isOneOf(tok::kw_new, tok::kw_delete) && Tok.is(tok::ampamp))
2956 return TT_BinaryOperator;
2958 const FormatToken *NextToken = Tok.getNextNonComment();
2960 if (InTemplateArgument && NextToken && NextToken->is(tok::kw_noexcept))
2961 return TT_BinaryOperator;
2964 NextToken->isOneOf(tok::arrow, tok::equal, tok::comma, tok::r_paren,
2965 TT_RequiresClause) ||
2967 NextToken->canBePointerOrReferenceQualifier() ||
2968 (NextToken->is(tok::l_brace) && !NextToken->getNextNonComment())) {
2969 return TT_PointerOrReference;
2972 if (PrevToken->is(tok::coloncolon))
2973 return TT_PointerOrReference;
2975 if (PrevToken->is(tok::r_paren) && PrevToken->is(TT_TypeDeclarationParen))
2976 return TT_PointerOrReference;
2978 if (determineUnaryOperatorByUsage(Tok))
2979 return TT_UnaryOperator;
2981 if (NextToken->is(tok::l_square) && NextToken->isNot(TT_LambdaLSquare))
2982 return TT_PointerOrReference;
2984 return TT_PointerOrReference;
2985 if (NextToken->isOneOf(tok::comma, tok::semi))
2986 return TT_PointerOrReference;
2998 if (PrevToken->is(tok::r_brace) && Tok.is(tok::star) &&
2999 !PrevToken->MatchingParen) {
3000 return TT_PointerOrReference;
3003 if (PrevToken->endsSequence(tok::r_square, tok::l_square, tok::kw_delete))
3004 return TT_UnaryOperator;
3006 if (PrevToken->Tok.isLiteral() ||
3007 PrevToken->isOneOf(tok::r_paren, tok::r_square, tok::kw_true,
3008 tok::kw_false, tok::r_brace)) {
3009 return TT_BinaryOperator;
3012 const FormatToken *NextNonParen = NextToken;
3013 while (NextNonParen && NextNonParen->is(tok::l_paren))
3014 NextNonParen = NextNonParen->getNextNonComment();
3015 if (NextNonParen && (NextNonParen->Tok.isLiteral() ||
3016 NextNonParen->isOneOf(tok::kw_true, tok::kw_false) ||
3017 NextNonParen->isUnaryOperator())) {
3018 return TT_BinaryOperator;
3024 if (InTemplateArgument && NextToken->Tok.isAnyIdentifier())
3025 return TT_BinaryOperator;
3029 if (Tok.is(tok::ampamp) &&
3030 NextToken->isOneOf(tok::l_paren, tok::star, tok::amp)) {
3031 return TT_BinaryOperator;
3036 if (NextToken->Tok.isAnyIdentifier()) {
3037 const FormatToken *NextNextToken = NextToken->getNextNonComment();
3038 if (NextNextToken && NextNextToken->is(tok::arrow))
3039 return TT_BinaryOperator;
3045 return TT_BinaryOperator;
3048 if (!Scopes.empty() && Scopes.back() ==
ST_Class)
3049 return TT_PointerOrReference;
3052 auto IsChainedOperatorAmpOrMember = [](
const FormatToken *token) {
3053 return !token || token->isOneOf(tok::amp, tok::period, tok::arrow,
3054 tok::arrowstar, tok::periodstar);
3059 if (Tok.is(tok::amp) && PrevToken && PrevToken->Tok.isAnyIdentifier() &&
3060 IsChainedOperatorAmpOrMember(PrevToken->getPreviousNonComment()) &&
3061 NextToken && NextToken->Tok.isAnyIdentifier()) {
3062 if (
auto NextNext = NextToken->getNextNonComment();
3064 (IsChainedOperatorAmpOrMember(NextNext) || NextNext->is(tok::semi))) {
3065 return TT_BinaryOperator;
3069 return TT_PointerOrReference;
3072 TokenType determinePlusMinusCaretUsage(
const FormatToken &Tok) {
3073 if (determineUnaryOperatorByUsage(Tok))
3074 return TT_UnaryOperator;
3076 const FormatToken *PrevToken = Tok.getPreviousNonComment();
3078 return TT_UnaryOperator;
3080 if (PrevToken->is(tok::at))
3081 return TT_UnaryOperator;
3084 return TT_BinaryOperator;
3088 TokenType determineIncrementUsage(
const FormatToken &Tok) {
3089 const FormatToken *PrevToken = Tok.getPreviousNonComment();
3090 if (!PrevToken || PrevToken->is(TT_CastRParen))
3091 return TT_UnaryOperator;
3092 if (PrevToken->isOneOf(tok::r_paren, tok::r_square, tok::identifier))
3093 return TT_TrailingUnaryOperator;
3095 return TT_UnaryOperator;
3098 SmallVector<Context, 8> Contexts;
3100 const FormatStyle &Style;
3101 AnnotatedLine &Line;
3102 FormatToken *CurrentToken;
3105 LangOptions LangOpts;
3106 const AdditionalKeywords &Keywords;
3108 SmallVector<ScopeType> &Scopes;
3116 int TemplateDeclarationDepth;
3124class ExpressionParser {
3126 ExpressionParser(
const FormatStyle &Style,
const AdditionalKeywords &Keywords,
3127 AnnotatedLine &Line)
3128 : Style(Style), Keywords(Keywords), Line(Line), Current(Line.
First) {}
3131 void parse(
int Precedence = 0) {
3134 while (Current && (Current->is(tok::kw_return) ||
3135 (Current->is(tok::colon) &&
3136 Current->isOneOf(TT_ObjCMethodExpr, TT_DictLiteral)))) {
3140 if (!Current || Precedence > PrecedenceArrowAndPeriod)
3145 parseConditionalExpr();
3151 if (Precedence == PrecedenceUnaryOperator) {
3152 parseUnaryOperator();
3156 FormatToken *Start = Current;
3157 FormatToken *LatestOperator =
nullptr;
3158 unsigned OperatorIndex = 0;
3160 FormatToken *VerilogFirstOfType =
nullptr;
3169 if (Style.isVerilog() && Precedence ==
prec::Comma) {
3170 VerilogFirstOfType =
3171 verilogGroupDecl(VerilogFirstOfType, LatestOperator);
3175 parse(Precedence + 1);
3177 int CurrentPrecedence = getCurrentPrecedence();
3188 if (Precedence == CurrentPrecedence && Current &&
3189 Current->is(TT_SelectorName)) {
3191 addFakeParenthesis(Start,
prec::Level(Precedence));
3195 if ((Style.isCSharp() || Style.isJavaScript() ||
3200 FormatToken *Prev = Current->getPreviousNonComment();
3201 if (Prev && Prev->is(tok::string_literal) &&
3202 (Prev == Start || Prev->endsSequence(tok::string_literal, tok::plus,
3203 TT_StringInConcatenation))) {
3204 Prev->setType(TT_StringInConcatenation);
3211 (Current->closesScope() &&
3212 (Current->MatchingParen || Current->is(TT_TemplateString))) ||
3213 (CurrentPrecedence != -1 && CurrentPrecedence < Precedence) ||
3222 if (Current->opensScope() ||
3223 Current->isOneOf(TT_RequiresClause,
3224 TT_RequiresClauseInARequiresExpression)) {
3227 while (Current && (!Current->closesScope() || Current->opensScope())) {
3234 if (CurrentPrecedence == Precedence) {
3236 LatestOperator->NextOperator = Current;
3237 LatestOperator = Current;
3238 Current->OperatorIndex = OperatorIndex;
3241 next(Precedence > 0);
3246 if (Style.isVerilog() && Precedence ==
prec::Comma && VerilogFirstOfType)
3247 addFakeParenthesis(VerilogFirstOfType,
prec::Comma);
3249 if (LatestOperator && (Current || Precedence > 0)) {
3255 Start->Previous->isOneOf(TT_RequiresClause,
3256 TT_RequiresClauseInARequiresExpression))
3258 auto Ret = Current ? Current : Line.Last;
3259 while (!
Ret->ClosesRequiresClause &&
Ret->Previous)
3265 if (Precedence == PrecedenceArrowAndPeriod) {
3269 addFakeParenthesis(Start,
prec::Level(Precedence), End);
3277 int getCurrentPrecedence() {
3279 const FormatToken *NextNonComment = Current->getNextNonComment();
3280 if (Current->is(TT_ConditionalExpr))
3282 if (NextNonComment && Current->is(TT_SelectorName) &&
3283 (NextNonComment->isOneOf(TT_DictLiteral, TT_JsTypeColon) ||
3284 (Style.isProto() && NextNonComment->is(tok::less)))) {
3287 if (Current->is(TT_JsComputedPropertyName))
3289 if (Current->is(TT_TrailingReturnArrow))
3291 if (Current->is(TT_FatArrow))
3293 if (Current->isOneOf(tok::semi, TT_InlineASMColon, TT_SelectorName) ||
3294 (Current->is(tok::comment) && NextNonComment &&
3295 NextNonComment->is(TT_SelectorName))) {
3298 if (Current->is(TT_RangeBasedForLoopColon))
3301 Current->is(Keywords.kw_instanceof)) {
3304 if (Style.isJavaScript() &&
3305 Current->isOneOf(Keywords.kw_in, Keywords.kw_as)) {
3308 if (Current->is(TT_BinaryOperator) || Current->is(tok::comma))
3309 return Current->getPrecedence();
3310 if (Current->isOneOf(tok::period, tok::arrow) &&
3311 Current->isNot(TT_TrailingReturnArrow)) {
3312 return PrecedenceArrowAndPeriod;
3315 Current->isOneOf(Keywords.kw_extends, Keywords.kw_implements,
3316 Keywords.kw_throws)) {
3321 if (Style.isVerilog() && Current->is(tok::colon))
3327 void addFakeParenthesis(FormatToken *Start,
prec::Level Precedence,
3328 FormatToken *End =
nullptr) {
3333 if (Start->MacroParent)
3336 Start->FakeLParens.push_back(Precedence);
3338 Start->StartsBinaryExpression =
true;
3339 if (!End && Current)
3340 End = Current->getPreviousNonComment();
3344 End->EndsBinaryExpression =
true;
3350 void parseUnaryOperator() {
3352 while (Current && Current->is(TT_UnaryOperator)) {
3353 Tokens.push_back(Current);
3356 parse(PrecedenceArrowAndPeriod);
3357 for (FormatToken *Token : llvm::reverse(Tokens)) {
3363 void parseConditionalExpr() {
3364 while (Current && Current->isTrailingComment())
3366 FormatToken *Start = Current;
3368 if (!Current || Current->isNot(tok::question))
3372 if (!Current || Current->isNot(TT_ConditionalExpr))
3379 void next(
bool SkipPastLeadingComments =
true) {
3381 Current = Current->Next;
3383 (Current->NewlinesBefore == 0 || SkipPastLeadingComments) &&
3384 Current->isTrailingComment()) {
3385 Current = Current->Next;
3391 FormatToken *verilogGroupDecl(FormatToken *FirstOfType,
3392 FormatToken *PreviousComma) {
3396 FormatToken *Start = Current;
3399 while (Start->startsSequence(tok::l_paren, tok::star)) {
3400 if (!(Start = Start->MatchingParen) ||
3401 !(Start = Start->getNextNonComment())) {
3406 FormatToken *Tok = Start;
3408 if (Tok->is(Keywords.kw_assign))
3409 Tok = Tok->getNextNonComment();
3417 FormatToken *
First =
nullptr;
3419 FormatToken *Next = Tok->getNextNonComment();
3421 if (Tok->is(tok::hash)) {
3426 Tok = Tok->getNextNonComment();
3427 }
else if (Tok->is(tok::hashhash)) {
3431 Tok = Tok->getNextNonComment();
3432 }
else if (Keywords.isVerilogQualifier(*Tok) ||
3433 Keywords.isVerilogIdentifier(*Tok)) {
3437 while (Tok && Tok->isOneOf(tok::period, tok::coloncolon) &&
3438 (Tok = Tok->getNextNonComment())) {
3439 if (Keywords.isVerilogIdentifier(*Tok))
3440 Tok = Tok->getNextNonComment();
3444 }
else if (Tok->is(tok::l_paren)) {
3449 Keywords.kw_highz0, Keywords.kw_highz1, Keywords.kw_large,
3450 Keywords.kw_medium, Keywords.kw_pull0, Keywords.kw_pull1,
3451 Keywords.kw_small, Keywords.kw_strong0, Keywords.kw_strong1,
3452 Keywords.kw_supply0, Keywords.kw_supply1, Keywords.kw_weak0,
3453 Keywords.kw_weak1)) {
3454 Tok->setType(TT_VerilogStrength);
3455 Tok = Tok->MatchingParen;
3457 Tok->setType(TT_VerilogStrength);
3458 Tok = Tok->getNextNonComment();
3463 }
else if (Tok->is(Keywords.kw_verilogHash)) {
3465 if (Next->is(tok::l_paren))
3466 Next = Next->MatchingParen;
3468 Tok = Next->getNextNonComment();
3475 FormatToken *Second =
nullptr;
3477 while (Tok && Tok->is(tok::l_square) && (Tok = Tok->MatchingParen))
3478 Tok = Tok->getNextNonComment();
3479 if (Tok && (Tok->is(tok::hash) || Keywords.isVerilogIdentifier(*Tok)))
3484 FormatToken *TypedName =
nullptr;
3488 First->setType(TT_VerilogDimensionedTypeName);
3489 }
else if (
First != Start) {
3497 if (TypedName->is(TT_Unknown))
3498 TypedName->setType(TT_StartOfName);
3500 if (FirstOfType && PreviousComma) {
3501 PreviousComma->setType(TT_VerilogTypeComma);
3502 addFakeParenthesis(FirstOfType,
prec::Comma, PreviousComma->Previous);
3505 FirstOfType = TypedName;
3512 while (Current && Current != FirstOfType) {
3513 if (Current->opensScope()) {
3524 const FormatStyle &Style;
3525 const AdditionalKeywords &Keywords;
3526 const AnnotatedLine &Line;
3527 FormatToken *Current;
3536 assert(
Line->First);
3543 Line->First->OriginalColumn) {
3544 const bool PPDirectiveOrImportStmt =
3547 if (PPDirectiveOrImportStmt)
3553 PPDirectiveOrImportStmt
3555 : NextNonCommentLine->
Level;
3557 NextNonCommentLine =
Line->First->isNot(tok::r_brace) ?
Line :
nullptr;
3566 for (
const auto *Tok =
Line.First; Tok; Tok = Tok->Next)
3575 for (
FormatToken *Tok =
Line.getFirstNonComment(), *Name =
nullptr; Tok;
3576 Tok = Tok->getNextNonComment()) {
3578 if (Tok->is(tok::l_square) && Tok->is(TT_AttributeSquare)) {
3587 if (Tok->is(tok::l_paren) && Tok->isNot(TT_FunctionTypeLParen) &&
3588 Tok->MatchingParen) {
3596 if (Tok->isOneOf(tok::kw_friend, tok::kw_inline, tok::kw_virtual,
3597 tok::kw_constexpr, tok::kw_consteval, tok::kw_explicit)) {
3602 if (Tok->is(tok::coloncolon)) {
3609 while (Tok->startsSequence(tok::identifier, tok::coloncolon)) {
3611 Tok = Tok->Next->Next;
3617 if (Tok->is(tok::tilde)) {
3624 if (Tok->isNot(tok::identifier) || Tok->isNot(TT_Unknown))
3635 assert(Tok && Tok->
is(tok::identifier));
3638 if (Prev && Prev->is(tok::tilde))
3641 if (!Prev || !Prev->endsSequence(tok::coloncolon, tok::identifier))
3644 assert(Prev->Previous);
3645 return Prev->Previous->TokenText == Tok->
TokenText;
3649 AnnotatingParser
Parser(Style,
Line, Keywords, Scopes);
3652 for (
auto &Child :
Line.Children)
3665 ExpressionParser ExprParser(Style, Keywords,
Line);
3671 if (Tok && ((!Scopes.empty() && Scopes.back() ==
ST_Class) ||
3673 Tok->setFinalizedType(TT_CtorDtorDeclName);
3674 assert(OpeningParen);
3679 if (
Line.startsWith(TT_ObjCMethodSpecifier))
3681 else if (
Line.startsWith(TT_ObjCDecl))
3683 else if (
Line.startsWith(TT_ObjCProperty))
3687 First->SpacesRequiredBefore = 1;
3688 First->CanBreakBefore =
First->MustBreakBefore;
3690 if (
First->is(tok::eof) &&
First->NewlinesBefore == 0 &&
3691 Style.InsertNewlineAtEOF) {
3692 First->NewlinesBefore = 1;
3702 if (Current.
is(TT_FunctionDeclarationName))
3712 if (
const auto *PrevPrev =
Previous.getPreviousNonComment();
3713 PrevPrev && PrevPrev->is(TT_ObjCDecl)) {
3717 auto skipOperatorName =
3719 for (; Next; Next = Next->Next) {
3720 if (Next->is(TT_OverloadedOperatorLParen))
3722 if (Next->is(TT_OverloadedOperator))
3724 if (Next->isOneOf(tok::kw_new, tok::kw_delete)) {
3727 Next->Next->startsSequence(tok::l_square, tok::r_square)) {
3728 Next = Next->Next->Next;
3732 if (Next->startsSequence(tok::l_square, tok::r_square)) {
3737 if ((Next->isTypeName(LangOpts) || Next->is(tok::identifier)) &&
3738 Next->Next && Next->Next->isPointerOrReference()) {
3743 if (Next->is(TT_TemplateOpener) && Next->MatchingParen) {
3744 Next = Next->MatchingParen;
3753 const auto *Next = Current.
Next;
3754 const bool IsCpp = LangOpts.CXXOperatorNames;
3757 if (Current.
is(tok::kw_operator)) {
3758 if (
Previous.Tok.getIdentifierInfo() &&
3759 !
Previous.isOneOf(tok::kw_return, tok::kw_co_return)) {
3764 assert(
Previous.MatchingParen->is(tok::l_paren));
3765 assert(
Previous.MatchingParen->is(TT_TypeDeclarationParen));
3770 Next = skipOperatorName(Next);
3774 for (; Next; Next = Next->Next) {
3775 if (Next->is(TT_TemplateOpener) && Next->MatchingParen) {
3776 Next = Next->MatchingParen;
3777 }
else if (Next->is(tok::coloncolon)) {
3781 if (Next->is(tok::kw_operator)) {
3782 Next = skipOperatorName(Next->Next);
3785 if (Next->isNot(tok::identifier))
3787 }
else if (isCppAttribute(IsCpp, *Next)) {
3788 Next = Next->MatchingParen;
3791 }
else if (Next->is(tok::l_paren)) {
3800 if (!Next || Next->isNot(tok::l_paren) || !Next->MatchingParen)
3802 ClosingParen = Next->MatchingParen;
3803 assert(ClosingParen->
is(tok::r_paren));
3805 if (
Line.Last->is(tok::l_brace))
3807 if (Next->Next == ClosingParen)
3810 if (ClosingParen->
Next && ClosingParen->
Next->
is(TT_PointerOrReference))
3823 if (IsCpp && Next->Next && Next->Next->is(tok::identifier) &&
3824 !
Line.endsWith(tok::semi)) {
3828 for (
const FormatToken *Tok = Next->Next; Tok && Tok != ClosingParen;
3830 if (Tok->is(TT_TypeDeclarationParen))
3832 if (Tok->isOneOf(tok::l_paren, TT_TemplateOpener) && Tok->MatchingParen) {
3833 Tok = Tok->MatchingParen;
3836 if (Tok->is(tok::kw_const) || Tok->isTypeName(LangOpts) ||
3837 Tok->isOneOf(TT_PointerOrReference, TT_StartOfName, tok::ellipsis)) {
3840 if (Tok->isOneOf(tok::l_brace, TT_ObjCMethodExpr) || Tok->Tok.isLiteral())
3846bool TokenAnnotator::mustBreakForReturnType(
const AnnotatedLine &
Line)
const {
3847 assert(
Line.MightBeFunctionDecl);
3855 switch (Style.BreakAfterReturnType) {
3865 return Line.mightBeFunctionDefinition();
3878 :
Line.FirstStartColumn +
First->ColumnWidth;
3879 bool AlignArrayOfStructures =
3882 if (AlignArrayOfStructures)
3883 calculateArrayInitializerColumnList(
Line);
3885 const auto *FirstNonComment =
Line.getFirstNonComment();
3886 bool SeenName =
false;
3887 bool LineIsFunctionDeclaration =
false;
3891 for (
auto *Tok = FirstNonComment ? FirstNonComment->Next :
nullptr; Tok;
3893 if (Tok->is(TT_StartOfName))
3895 if (Tok->Previous->EndsCppAttributeGroup)
3896 AfterLastAttribute = Tok;
3897 if (
const bool IsCtorOrDtor = Tok->is(TT_CtorDtorDeclName);
3902 LineIsFunctionDeclaration =
true;
3906 assert(OpeningParen);
3907 if (OpeningParen->is(TT_Unknown))
3908 OpeningParen->setType(TT_FunctionDeclarationLParen);
3915 (LineIsFunctionDeclaration ||
3916 (FirstNonComment && FirstNonComment->is(TT_CtorDtorDeclName))) &&
3917 Line.endsWith(tok::semi, tok::r_brace)) {
3918 auto *Tok =
Line.Last->Previous;
3919 while (Tok->isNot(tok::r_brace))
3920 Tok = Tok->Previous;
3921 if (
auto *LBrace = Tok->MatchingParen; LBrace) {
3922 assert(LBrace->is(tok::l_brace));
3925 LBrace->setFinalizedType(TT_FunctionLBrace);
3929 if (IsCpp && SeenName && AfterLastAttribute &&
3932 if (LineIsFunctionDeclaration)
3933 Line.ReturnTypeWrapped =
true;
3937 if (!LineIsFunctionDeclaration) {
3939 for (
const auto *Tok = FirstNonComment; Tok; Tok = Tok->Next) {
3940 if (Tok->isNot(tok::kw_operator))
3944 }
while (Tok && Tok->isNot(TT_OverloadedOperatorLParen));
3945 if (!Tok || !Tok->MatchingParen)
3947 const auto *LeftParen = Tok;
3948 for (Tok = Tok->Next; Tok && Tok != LeftParen->MatchingParen;
3950 if (Tok->isNot(tok::identifier))
3952 auto *Next = Tok->Next;
3953 const bool NextIsBinaryOperator =
3954 Next && Next->isPointerOrReference() && Next->Next &&
3955 Next->Next->is(tok::identifier);
3956 if (!NextIsBinaryOperator)
3958 Next->setType(TT_BinaryOperator);
3962 }
else if (ClosingParen) {
3963 for (
auto *Tok = ClosingParen->
Next; Tok; Tok = Tok->
Next) {
3964 if (Tok->is(TT_CtorInitializerColon))
3966 if (Tok->is(tok::arrow)) {
3967 Tok->setType(TT_TrailingReturnArrow);
3970 if (Tok->isNot(TT_TrailingAnnotation))
3972 const auto *Next = Tok->Next;
3973 if (!Next || Next->isNot(tok::l_paren))
3975 Tok = Next->MatchingParen;
3982 bool InFunctionDecl =
Line.MightBeFunctionDecl;
3983 for (
auto *Current =
First->Next; Current; Current = Current->Next) {
3985 if (Current->is(TT_LineComment)) {
3987 Current->SpacesRequiredBefore =
3988 (Style.Cpp11BracedListStyle && !Style.SpacesInParensOptions.Other)
3991 }
else if (Prev->
is(TT_VerilogMultiLineListLParen)) {
3992 Current->SpacesRequiredBefore = 0;
3994 Current->SpacesRequiredBefore = Style.SpacesBeforeTrailingComments;
4004 if (!Current->HasUnescapedNewline) {
4007 if (
Parameter->isOneOf(tok::comment, tok::r_brace))
4010 if (
Parameter->Previous->isNot(TT_CtorInitializerComma) &&
4018 }
else if (!Current->Finalized && Current->SpacesRequiredBefore == 0 &&
4019 spaceRequiredBefore(
Line, *Current)) {
4020 Current->SpacesRequiredBefore = 1;
4023 const auto &Children = Prev->
Children;
4024 if (!Children.empty() && Children.back()->Last->is(TT_LineComment)) {
4025 Current->MustBreakBefore =
true;
4027 Current->MustBreakBefore =
4028 Current->MustBreakBefore || mustBreakBefore(
Line, *Current);
4029 if (!Current->MustBreakBefore && InFunctionDecl &&
4030 Current->is(TT_FunctionDeclarationName)) {
4031 Current->MustBreakBefore = mustBreakForReturnType(
Line);
4035 Current->CanBreakBefore =
4036 Current->MustBreakBefore || canBreakBefore(
Line, *Current);
4037 unsigned ChildSize = 0;
4043 if (Current->MustBreakBefore || Prev->
Children.size() > 1 ||
4045 Prev->
Children[0]->First->MustBreakBefore) ||
4046 Current->IsMultiline) {
4047 Current->TotalLength = Prev->
TotalLength + Style.ColumnLimit;
4049 Current->TotalLength = Prev->
TotalLength + Current->ColumnWidth +
4050 ChildSize + Current->SpacesRequiredBefore;
4053 if (Current->is(TT_CtorInitializerColon))
4054 InFunctionDecl =
false;
4065 Current->SplitPenalty = splitPenalty(
Line, *Current, InFunctionDecl);
4067 Current->is(TT_SelectorName) && Current->ParameterIndex > 0) {
4068 if (Current->ParameterIndex == 1)
4069 Current->SplitPenalty += 5 * Current->BindingStrength;
4071 Current->SplitPenalty += 20 * Current->BindingStrength;
4075 calculateUnbreakableTailLengths(
Line);
4076 unsigned IndentLevel =
Line.Level;
4077 for (
auto *Current =
First; Current; Current = Current->Next) {
4079 Current->Role->precomputeFormattingInfos(Current);
4080 if (Current->MatchingParen &&
4081 Current->MatchingParen->opensBlockOrBlockTypeList(Style) &&
4085 Current->IndentLevel = IndentLevel;
4086 if (Current->opensBlockOrBlockTypeList(Style))
4090 LLVM_DEBUG({ printDebugInfo(
Line); });
4093void TokenAnnotator::calculateUnbreakableTailLengths(
4095 unsigned UnbreakableTailLength = 0;
4098 Current->UnbreakableTailLength = UnbreakableTailLength;
4099 if (Current->CanBreakBefore ||
4100 Current->isOneOf(tok::comment, tok::string_literal)) {
4101 UnbreakableTailLength = 0;
4103 UnbreakableTailLength +=
4104 Current->ColumnWidth + Current->SpacesRequiredBefore;
4106 Current = Current->Previous;
4110void TokenAnnotator::calculateArrayInitializerColumnList(
4111 AnnotatedLine &
Line)
const {
4114 auto *CurrentToken =
Line.First;
4115 CurrentToken->ArrayInitializerLineStart =
true;
4117 while (CurrentToken && CurrentToken !=
Line.Last) {
4118 if (CurrentToken->is(tok::l_brace)) {
4119 CurrentToken->IsArrayInitializer =
true;
4120 if (CurrentToken->Next)
4121 CurrentToken->Next->MustBreakBefore =
true;
4123 calculateInitializerColumnList(
Line, CurrentToken->Next, Depth + 1);
4125 CurrentToken = CurrentToken->Next;
4130FormatToken *TokenAnnotator::calculateInitializerColumnList(
4131 AnnotatedLine &
Line, FormatToken *CurrentToken,
unsigned Depth)
const {
4132 while (CurrentToken && CurrentToken !=
Line.Last) {
4133 if (CurrentToken->is(tok::l_brace))
4135 else if (CurrentToken->is(tok::r_brace))
4137 if (Depth == 2 && CurrentToken->isOneOf(tok::l_brace, tok::comma)) {
4138 CurrentToken = CurrentToken->Next;
4141 CurrentToken->StartsColumn =
true;
4142 CurrentToken = CurrentToken->Previous;
4144 CurrentToken = CurrentToken->Next;
4146 return CurrentToken;
4149unsigned TokenAnnotator::splitPenalty(
const AnnotatedLine &
Line,
4150 const FormatToken &Tok,
4151 bool InFunctionDecl)
const {
4152 const FormatToken &
Left = *Tok.Previous;
4153 const FormatToken &
Right = Tok;
4155 if (
Left.is(tok::semi))
4160 if (
Right.isOneOf(Keywords.kw_extends, Keywords.kw_throws))
4162 if (
Right.is(Keywords.kw_implements))
4164 if (
Left.is(tok::comma) &&
Left.NestingLevel == 0)
4166 }
else if (Style.isJavaScript()) {
4167 if (
Right.is(Keywords.kw_function) &&
Left.isNot(tok::comma))
4169 if (
Left.is(TT_JsTypeColon))
4171 if ((
Left.is(TT_TemplateString) &&
Left.TokenText.ends_with(
"${")) ||
4172 (
Right.is(TT_TemplateString) &&
Right.TokenText.starts_with(
"}"))) {
4176 if (
Left.opensScope() &&
Right.closesScope())
4179 if (
Right.is(tok::l_square))
4181 if (
Right.is(tok::period))
4185 if (
Right.is(tok::identifier) &&
Right.Next &&
Right.Next->is(TT_DictLiteral))
4187 if (
Right.is(tok::l_square)) {
4188 if (
Left.is(tok::r_square))
4191 if (
Right.is(TT_LambdaLSquare) &&
Left.is(tok::equal))
4193 if (!
Right.isOneOf(TT_ObjCMethodExpr, TT_LambdaLSquare,
4194 TT_ArrayInitializerLSquare,
4195 TT_DesignatedInitializerLSquare, TT_AttributeSquare)) {
4200 if (
Left.is(tok::coloncolon))
4201 return Style.PenaltyBreakScopeResolution;
4202 if (
Right.isOneOf(TT_StartOfName, TT_FunctionDeclarationName) ||
4203 Right.is(tok::kw_operator)) {
4204 if (
Line.startsWith(tok::kw_for) &&
Right.PartOfMultiVariableDeclStmt)
4206 if (
Left.is(TT_StartOfName))
4208 if (InFunctionDecl &&
Right.NestingLevel == 0)
4209 return Style.PenaltyReturnTypeOnItsOwnLine;
4212 if (
Right.is(TT_PointerOrReference))
4214 if (
Right.is(TT_TrailingReturnArrow))
4216 if (
Left.is(tok::equal) &&
Right.is(tok::l_brace))
4218 if (
Left.is(TT_CastRParen))
4220 if (
Left.isOneOf(tok::kw_class, tok::kw_struct, tok::kw_union))
4222 if (
Left.is(tok::comment))
4225 if (
Left.isOneOf(TT_RangeBasedForLoopColon, TT_InheritanceColon,
4226 TT_CtorInitializerColon)) {
4230 if (
Right.isMemberAccess()) {
4250 return !
Right.NextOperator || !
Right.NextOperator->Previous->closesScope()
4255 if (
Right.is(TT_TrailingAnnotation) &&
4256 (!
Right.Next ||
Right.Next->isNot(tok::l_paren))) {
4259 if (
Line.startsWith(TT_ObjCMethodSpecifier))
4266 bool is_short_annotation =
Right.TokenText.size() < 10;
4267 return (
Left.is(tok::r_paren) ? 100 : 120) + (is_short_annotation ? 50 : 0);
4271 if (
Line.startsWith(tok::kw_for) &&
Left.is(tok::equal))
4276 if (
Right.is(TT_SelectorName))
4278 if (
Left.is(tok::colon) &&
Left.is(TT_ObjCMethodExpr))
4279 return Line.MightBeFunctionDecl ? 50 : 500;
4285 Left.Previous->isOneOf(tok::identifier, tok::greater)) {
4289 if (
Left.is(tok::l_paren) && Style.PenaltyBreakOpenParenthesis != 0)
4290 return Style.PenaltyBreakOpenParenthesis;
4291 if (
Left.is(tok::l_paren) && InFunctionDecl &&
4295 if (
Left.is(tok::l_paren) &&
Left.Previous &&
4296 (
Left.Previous->isOneOf(tok::kw_for, tok::kw__Generic) ||
4297 Left.Previous->isIf())) {
4300 if (
Left.is(tok::equal) && InFunctionDecl)
4302 if (
Right.is(tok::r_brace))
4304 if (
Left.is(TT_TemplateOpener))
4306 if (
Left.opensScope()) {
4311 (
Left.ParameterCount <= 1 || Style.AllowAllArgumentsOnNextLine)) {
4314 if (
Left.is(tok::l_brace) && !Style.Cpp11BracedListStyle)
4316 return Left.ParameterCount > 1 ? Style.PenaltyBreakBeforeFirstCallParameter
4319 if (
Left.is(TT_JavaAnnotation))
4322 if (
Left.is(TT_UnaryOperator))
4324 if (
Left.isOneOf(tok::plus, tok::comma) &&
Left.Previous &&
4325 Left.Previous->isLabelString() &&
4326 (
Left.NextOperator ||
Left.OperatorIndex != 0)) {
4329 if (
Right.is(tok::plus) &&
Left.isLabelString() &&
4330 (
Right.NextOperator ||
Right.OperatorIndex != 0)) {
4333 if (
Left.is(tok::comma))
4335 if (
Right.is(tok::lessless) &&
Left.isLabelString() &&
4336 (
Right.NextOperator ||
Right.OperatorIndex != 1)) {
4339 if (
Right.is(tok::lessless)) {
4341 if (
Left.isNot(tok::r_paren) ||
Right.OperatorIndex > 0) {
4347 if (
Left.ClosesTemplateDeclaration)
4348 return Style.PenaltyBreakTemplateDeclaration;
4349 if (
Left.ClosesRequiresClause)
4351 if (
Left.is(TT_ConditionalExpr))
4357 return Style.PenaltyBreakAssignment;
4364bool TokenAnnotator::spaceRequiredBeforeParens(
const FormatToken &Right)
const {
4367 if (
Right.is(TT_OverloadedOperatorLParen) &&
4368 Style.SpaceBeforeParensOptions.AfterOverloadedOperator) {
4371 if (Style.SpaceBeforeParensOptions.BeforeNonEmptyParentheses &&
4372 Right.ParameterCount > 0) {
4378bool TokenAnnotator::spaceRequiredBetween(
const AnnotatedLine &
Line,
4379 const FormatToken &Left,
4380 const FormatToken &Right)
const {
4381 if (
Left.is(tok::kw_return) &&
4382 !
Right.isOneOf(tok::semi, tok::r_paren, tok::hashhash)) {
4385 if (
Left.is(tok::kw_throw) &&
Right.is(tok::l_paren) &&
Right.MatchingParen &&
4386 Right.MatchingParen->is(TT_CastRParen)) {
4392 Left.Tok.getObjCKeywordID() == tok::objc_property) {
4395 if (
Right.is(tok::hashhash))
4396 return Left.is(tok::hash);
4397 if (
Left.isOneOf(tok::hashhash, tok::hash))
4398 return Right.is(tok::hash);
4400 Right.MatchingParen == &Left &&
Line.Children.empty()) {
4401 return Style.SpaceInEmptyBlock;
4403 if ((
Left.is(tok::l_paren) &&
Right.is(tok::r_paren)) ||
4406 return Style.SpacesInParensOptions.InEmptyParentheses;
4409 Style.SpacesInParensOptions.ExceptDoubleParentheses &&
4410 Left.is(tok::r_paren) &&
Right.is(tok::r_paren)) {
4411 auto *InnerLParen =
Left.MatchingParen;
4412 if (InnerLParen && InnerLParen->Previous ==
Right.MatchingParen) {
4413 InnerLParen->SpacesRequiredBefore = 0;
4417 if (Style.SpacesInParensOptions.InConditionalStatements) {
4418 const FormatToken *LeftParen =
nullptr;
4419 if (
Left.is(tok::l_paren))
4421 else if (
Right.is(tok::r_paren) &&
Right.MatchingParen)
4422 LeftParen =
Right.MatchingParen;
4424 if (LeftParen->is(TT_ConditionLParen))
4426 if (LeftParen->Previous && isKeywordWithCondition(*LeftParen->Previous))
4432 if (
Left.is(tok::kw_auto) &&
Right.isOneOf(TT_LambdaLBrace, TT_FunctionLBrace,
4434 TT_FunctionTypeLParen)) {
4439 if (
Left.is(tok::kw_auto) &&
Right.isOneOf(tok::l_paren, tok::l_brace))
4442 const auto *BeforeLeft =
Left.Previous;
4445 if (
Right.is(tok::l_paren) &&
Left.is(tok::kw_co_await) && BeforeLeft &&
4446 BeforeLeft->is(tok::kw_operator)) {
4450 if (
Left.isOneOf(tok::kw_co_await, tok::kw_co_yield, tok::kw_co_return) &&
4451 !
Right.isOneOf(tok::semi, tok::r_paren)) {
4455 if (
Left.is(tok::l_paren) ||
Right.is(tok::r_paren)) {
4456 return (
Right.is(TT_CastRParen) ||
4457 (
Left.MatchingParen &&
Left.MatchingParen->is(TT_CastRParen)))
4458 ? Style.SpacesInParensOptions.InCStyleCasts
4459 : Style.SpacesInParensOptions.Other;
4461 if (
Right.isOneOf(tok::semi, tok::comma))
4464 bool IsLightweightGeneric =
Right.MatchingParen &&
4465 Right.MatchingParen->Next &&
4466 Right.MatchingParen->Next->is(tok::colon);
4467 return !IsLightweightGeneric && Style.ObjCSpaceBeforeProtocolList;
4469 if (
Right.is(tok::less) &&
Left.is(tok::kw_template))
4470 return Style.SpaceAfterTemplateKeyword;
4471 if (
Left.isOneOf(tok::exclaim, tok::tilde))
4473 if (
Left.is(tok::at) &&
4474 Right.isOneOf(tok::identifier, tok::string_literal, tok::char_constant,
4475 tok::numeric_constant, tok::l_paren, tok::l_brace,
4476 tok::kw_true, tok::kw_false)) {
4479 if (
Left.is(tok::colon))
4480 return Left.isNot(TT_ObjCMethodExpr);
4481 if (
Left.is(tok::coloncolon)) {
4482 return Right.is(tok::star) &&
Right.is(TT_PointerOrReference) &&
4485 if (
Left.is(tok::less) ||
Right.isOneOf(tok::greater, tok::less)) {
4488 (
Left.is(TT_DictLiteral) ||
Right.is(TT_DictLiteral)))) {
4490 if (
Left.is(tok::less) &&
Right.is(tok::greater))
4492 return !Style.Cpp11BracedListStyle;
4495 if (
Right.isNot(TT_OverloadedOperatorLParen))
4498 if (
Right.is(tok::ellipsis)) {
4499 return Left.Tok.isLiteral() || (
Left.is(tok::identifier) && BeforeLeft &&
4500 BeforeLeft->is(tok::kw_case));
4502 if (
Left.is(tok::l_square) &&
Right.is(tok::amp))
4503 return Style.SpacesInSquareBrackets;
4504 if (
Right.is(TT_PointerOrReference)) {
4505 if (
Left.is(tok::r_paren) &&
Line.MightBeFunctionDecl) {
4506 if (!
Left.MatchingParen)
4508 FormatToken *TokenBeforeMatchingParen =
4509 Left.MatchingParen->getPreviousNonComment();
4510 if (!TokenBeforeMatchingParen ||
Left.isNot(TT_TypeDeclarationParen))
4518 (
Left.is(TT_AttributeRParen) ||
4519 Left.canBePointerOrReferenceQualifier())) {
4522 if (
Left.Tok.isLiteral())
4525 if (
Left.isTypeOrIdentifier(LangOpts) &&
Right.Next &&
Right.Next->Next &&
4526 Right.Next->Next->is(TT_RangeBasedForLoopColon)) {
4527 return getTokenPointerOrReferenceAlignment(Right) !=
4530 return !
Left.isOneOf(TT_PointerOrReference, tok::l_paren) &&
4531 (getTokenPointerOrReferenceAlignment(Right) !=
4533 (
Line.IsMultiVariableDeclStmt &&
4534 (
Left.NestingLevel == 0 ||
4535 (
Left.NestingLevel == 1 && startsWithInitStatement(
Line)))));
4537 if (
Right.is(TT_FunctionTypeLParen) &&
Left.isNot(tok::l_paren) &&
4538 (
Left.isNot(TT_PointerOrReference) ||
4540 !
Line.IsMultiVariableDeclStmt))) {
4543 if (
Left.is(TT_PointerOrReference)) {
4548 Right.canBePointerOrReferenceQualifier()) {
4552 if (
Right.Tok.isLiteral())
4555 if (
Right.is(TT_BlockComment))
4559 if (
Right.isOneOf(Keywords.kw_override, Keywords.kw_final, tok::kw_noexcept,
4560 TT_RequiresClause) &&
4561 Right.isNot(TT_StartOfName)) {
4568 if (BeforeLeft && BeforeLeft->isTypeOrIdentifier(LangOpts) &&
Right.Next &&
4569 Right.Next->is(TT_RangeBasedForLoopColon)) {
4570 return getTokenPointerOrReferenceAlignment(Left) !=
4573 if (
Right.isOneOf(TT_PointerOrReference, TT_ArraySubscriptLSquare,
4585 if (
Line.IsMultiVariableDeclStmt &&
4586 (
Left.NestingLevel ==
Line.First->NestingLevel ||
4587 ((
Left.NestingLevel ==
Line.First->NestingLevel + 1) &&
4588 startsWithInitStatement(
Line)))) {
4593 if (BeforeLeft->is(tok::coloncolon)) {
4594 return Left.is(tok::star) &&
4597 return !BeforeLeft->isOneOf(tok::l_paren, tok::l_square);
4600 if (
Left.is(tok::ellipsis) && BeforeLeft &&
4601 BeforeLeft->isPointerOrReference()) {
4605 if (
Right.is(tok::star) &&
Left.is(tok::l_paren))
4607 if (
Left.is(tok::star) &&
Right.isPointerOrReference())
4609 if (
Right.isPointerOrReference()) {
4620 if (
Previous->is(tok::coloncolon)) {
4639 if (
Previous->endsSequence(tok::kw_operator))
4643 (Style.SpaceAroundPointerQualifiers ==
4649 if (Style.isCSharp() &&
Left.is(Keywords.kw_is) &&
Right.is(tok::l_square))
4651 const auto SpaceRequiredForArrayInitializerLSquare =
4652 [](
const FormatToken &LSquareTok,
const FormatStyle &Style) {
4653 return Style.SpacesInContainerLiterals ||
4654 (Style.isProto() && !Style.Cpp11BracedListStyle &&
4655 LSquareTok.endsSequence(tok::l_square, tok::colon,
4658 if (
Left.is(tok::l_square)) {
4659 return (
Left.is(TT_ArrayInitializerLSquare) &&
Right.isNot(tok::r_square) &&
4660 SpaceRequiredForArrayInitializerLSquare(Left, Style)) ||
4661 (
Left.isOneOf(TT_ArraySubscriptLSquare, TT_StructuredBindingLSquare,
4662 TT_LambdaLSquare) &&
4663 Style.SpacesInSquareBrackets &&
Right.isNot(tok::r_square));
4665 if (
Right.is(tok::r_square)) {
4666 return Right.MatchingParen &&
4667 ((
Right.MatchingParen->is(TT_ArrayInitializerLSquare) &&
4668 SpaceRequiredForArrayInitializerLSquare(*
Right.MatchingParen,
4670 (Style.SpacesInSquareBrackets &&
4671 Right.MatchingParen->isOneOf(TT_ArraySubscriptLSquare,
4672 TT_StructuredBindingLSquare,
4673 TT_LambdaLSquare)));
4675 if (
Right.is(tok::l_square) &&
4676 !
Right.isOneOf(TT_ObjCMethodExpr, TT_LambdaLSquare,
4677 TT_DesignatedInitializerLSquare,
4678 TT_StructuredBindingLSquare, TT_AttributeSquare) &&
4679 !
Left.isOneOf(tok::numeric_constant, TT_DictLiteral) &&
4680 !(
Left.isNot(tok::r_square) && Style.SpaceBeforeSquareBrackets &&
4681 Right.is(TT_ArraySubscriptLSquare))) {
4684 if (
Left.is(tok::l_brace) &&
Right.is(tok::r_brace))
4685 return !
Left.Children.empty();
4687 (
Right.is(tok::r_brace) &&
Right.MatchingParen &&
4689 return !Style.Cpp11BracedListStyle || Style.SpacesInParensOptions.Other;
4691 if (
Left.is(TT_BlockComment)) {
4693 return Style.isJavaScript() || !
Left.TokenText.ends_with(
"=*/");
4698 if (
Left.is(TT_TemplateCloser) &&
Right.is(TT_AttributeSquare))
4701 if (
Right.is(tok::l_paren)) {
4702 if (
Left.is(TT_TemplateCloser) &&
Right.isNot(TT_FunctionTypeLParen))
4703 return spaceRequiredBeforeParens(Right);
4704 if (
Left.isOneOf(TT_RequiresClause,
4705 TT_RequiresClauseInARequiresExpression)) {
4706 return Style.SpaceBeforeParensOptions.AfterRequiresInClause ||
4707 spaceRequiredBeforeParens(Right);
4709 if (
Left.is(TT_RequiresExpression)) {
4710 return Style.SpaceBeforeParensOptions.AfterRequiresInExpression ||
4711 spaceRequiredBeforeParens(Right);
4713 if (
Left.is(TT_AttributeRParen) ||
4714 (
Left.is(tok::r_square) &&
Left.is(TT_AttributeSquare))) {
4717 if (
Left.is(TT_ForEachMacro)) {
4718 return Style.SpaceBeforeParensOptions.AfterForeachMacros ||
4719 spaceRequiredBeforeParens(Right);
4721 if (
Left.is(TT_IfMacro)) {
4722 return Style.SpaceBeforeParensOptions.AfterIfMacros ||
4723 spaceRequiredBeforeParens(Right);
4726 Left.isOneOf(tok::kw_new, tok::kw_delete) &&
4727 Right.isNot(TT_OverloadedOperatorLParen) &&
4728 !(
Line.MightBeFunctionDecl &&
Left.is(TT_FunctionDeclarationName))) {
4729 return Style.SpaceBeforeParensOptions.AfterPlacementOperator;
4733 if (
Left.is(tok::semi))
4735 if (
Left.isOneOf(tok::pp_elif, tok::kw_for, tok::kw_while, tok::kw_switch,
4736 tok::kw_case, TT_ForEachMacro, TT_ObjCForIn) ||
4738 Right.is(TT_ConditionLParen)) {
4739 return Style.SpaceBeforeParensOptions.AfterControlStatements ||
4740 spaceRequiredBeforeParens(Right);
4745 if (
Right.is(TT_OverloadedOperatorLParen))
4746 return spaceRequiredBeforeParens(Right);
4748 if (
Line.MightBeFunctionDecl &&
Right.is(TT_FunctionDeclarationLParen)) {
4749 if (spaceRequiredBeforeParens(Right))
4751 const auto &Options = Style.SpaceBeforeParensOptions;
4752 return Line.mightBeFunctionDefinition()
4753 ? Options.AfterFunctionDefinitionName
4754 : Options.AfterFunctionDeclarationName;
4758 Left.MatchingParen &&
Left.MatchingParen->is(TT_LambdaLSquare)) {
4759 return Style.SpaceBeforeParensOptions.AfterFunctionDefinitionName ||
4760 spaceRequiredBeforeParens(Right);
4762 if (!BeforeLeft || !BeforeLeft->isOneOf(tok::period, tok::arrow)) {
4763 if (
Left.isOneOf(tok::kw_try, Keywords.kw___except, tok::kw_catch)) {
4764 return Style.SpaceBeforeParensOptions.AfterControlStatements ||
4765 spaceRequiredBeforeParens(Right);
4767 if (
Left.isOneOf(tok::kw_new, tok::kw_delete)) {
4768 return ((!
Line.MightBeFunctionDecl || !BeforeLeft) &&
4770 spaceRequiredBeforeParens(Right);
4773 if (
Left.is(tok::r_square) &&
Left.MatchingParen &&
4774 Left.MatchingParen->Previous &&
4775 Left.MatchingParen->Previous->is(tok::kw_delete)) {
4777 spaceRequiredBeforeParens(Right);
4782 (
Left.Tok.getIdentifierInfo() ||
Left.is(tok::r_paren))) {
4783 return spaceRequiredBeforeParens(Right);
4787 if (
Left.is(tok::at) &&
Right.Tok.getObjCKeywordID() != tok::objc_not_keyword)
4789 if (
Right.is(TT_UnaryOperator)) {
4790 return !
Left.isOneOf(tok::l_paren, tok::l_square, tok::at) &&
4791 (
Left.isNot(tok::colon) ||
Left.isNot(TT_ObjCMethodExpr));
4797 if (!Style.isVerilog() &&
4798 (
Left.isOneOf(tok::identifier, tok::greater, tok::r_square,
4800 Left.isTypeName(LangOpts)) &&
4801 Right.is(tok::l_brace) &&
Right.getNextNonComment() &&
4805 if (
Left.is(tok::period) ||
Right.is(tok::period))
4809 if (
Right.is(tok::hash) &&
Left.is(tok::identifier) &&
4810 (
Left.TokenText ==
"L" ||
Left.TokenText ==
"u" ||
4811 Left.TokenText ==
"U" ||
Left.TokenText ==
"u8" ||
4812 Left.TokenText ==
"LR" ||
Left.TokenText ==
"uR" ||
4813 Left.TokenText ==
"UR" ||
Left.TokenText ==
"u8R")) {
4816 if (
Left.is(TT_TemplateCloser) &&
Left.MatchingParen &&
4817 Left.MatchingParen->Previous &&
4818 (
Left.MatchingParen->Previous->is(tok::period) ||
4819 Left.MatchingParen->Previous->is(tok::coloncolon))) {
4825 if (
Left.is(TT_TemplateCloser) &&
Right.is(tok::l_square))
4827 if (
Left.is(tok::l_brace) &&
Left.endsSequence(TT_DictLiteral, tok::at)) {
4831 if (
Right.is(tok::r_brace) &&
Right.MatchingParen &&
4832 Right.MatchingParen->endsSequence(TT_DictLiteral, tok::at)) {
4836 if (
Right.is(TT_TrailingAnnotation) &&
Right.isOneOf(tok::amp, tok::ampamp) &&
4837 Left.isOneOf(tok::kw_const, tok::kw_volatile) &&
4838 (!
Right.Next ||
Right.Next->is(tok::semi))) {
4848bool TokenAnnotator::spaceRequiredBefore(
const AnnotatedLine &
Line,
4849 const FormatToken &Right)
const {
4850 const FormatToken &
Left = *
Right.Previous;
4855 return Right.hasWhitespaceBefore();
4857 const bool IsVerilog = Style.isVerilog();
4858 assert(!IsVerilog || !IsCpp);
4861 if (Keywords.isWordLike(Right, IsVerilog) &&
4862 Keywords.isWordLike(Left, IsVerilog)) {
4868 if (
Left.is(tok::star) &&
Right.is(tok::comment))
4872 if (
Left.is(TT_OverloadedOperator) &&
4873 Right.isOneOf(TT_TemplateOpener, TT_TemplateCloser)) {
4877 if (
Right.is(tok::period) &&
Left.is(tok::numeric_constant))
4881 if (
Left.is(Keywords.kw_import) &&
Right.isOneOf(tok::less, tok::ellipsis))
4884 if (
Left.isOneOf(Keywords.kw_module, Keywords.kw_import) &&
4885 Right.is(TT_ModulePartitionColon)) {
4889 if (
Left.is(tok::identifier) &&
Right.is(TT_ModulePartitionColon))
4892 if (
Left.is(TT_ModulePartitionColon) &&
4893 Right.isOneOf(tok::identifier, tok::kw_private)) {
4896 if (
Left.is(tok::ellipsis) &&
Right.is(tok::identifier) &&
4897 Line.First->is(Keywords.kw_import)) {
4901 if (
Left.isOneOf(TT_AttributeRParen, TT_AttributeMacro) &&
4902 Right.is(tok::coloncolon)) {
4906 if (
Left.is(tok::kw_operator))
4907 return Right.is(tok::coloncolon);
4909 !
Left.opensScope() && Style.SpaceBeforeCpp11BracedList) {
4912 if (
Left.is(tok::less) &&
Left.is(TT_OverloadedOperator) &&
4913 Right.is(TT_TemplateOpener)) {
4917 if (
Left.is(tok::identifier) &&
Right.is(tok::numeric_constant))
4918 return Right.TokenText[0] !=
'.';
4920 if (
Left.Tok.getIdentifierInfo() &&
Right.Tok.isLiteral())
4922 }
else if (Style.isProto()) {
4923 if (
Right.is(tok::period) &&
4924 Left.isOneOf(Keywords.kw_optional, Keywords.kw_required,
4925 Keywords.kw_repeated, Keywords.kw_extend)) {
4928 if (
Right.is(tok::l_paren) &&
4929 Left.isOneOf(Keywords.kw_returns, Keywords.kw_option)) {
4932 if (
Right.isOneOf(tok::l_brace, tok::less) &&
Left.is(TT_SelectorName))
4935 if (
Left.is(tok::slash) ||
Right.is(tok::slash))
4937 if (
Left.MatchingParen &&
4938 Left.MatchingParen->is(TT_ProtoExtensionLSquare) &&
4939 Right.isOneOf(tok::l_brace, tok::less)) {
4940 return !Style.Cpp11BracedListStyle;
4943 if (
Left.is(tok::percent))
4947 if (
Left.is(tok::numeric_constant) &&
Right.is(tok::percent))
4948 return Right.hasWhitespaceBefore();
4949 }
else if (Style.isJson()) {
4950 if (
Right.is(tok::colon) &&
Left.is(tok::string_literal))
4951 return Style.SpaceBeforeJsonColon;
4952 }
else if (Style.isCSharp()) {
4958 if (
Left.is(tok::kw_this) &&
Right.is(tok::l_square))
4962 if (
Left.is(tok::kw_new) &&
Right.is(tok::l_paren))
4966 if (
Right.is(tok::l_brace))
4970 if (
Left.is(tok::l_brace) &&
Right.isNot(tok::r_brace))
4973 if (
Left.isNot(tok::l_brace) &&
Right.is(tok::r_brace))
4977 if (
Left.is(TT_FatArrow) ||
Right.is(TT_FatArrow))
4981 if (
Left.is(TT_AttributeColon) ||
Right.is(TT_AttributeColon))
4985 if (
Left.is(TT_TemplateCloser) &&
Right.is(TT_StartOfName))
4989 if (
Left.is(tok::l_square) ||
Right.is(tok::r_square))
4990 return Style.SpacesInSquareBrackets;
4993 if (
Right.is(TT_CSharpNullable))
4997 if (
Right.is(TT_NonNullAssertion))
5001 if (
Left.is(tok::comma) &&
Right.is(tok::comma))
5005 if (
Left.is(Keywords.kw_var) &&
Right.is(tok::l_paren))
5009 if (
Right.is(tok::l_paren)) {
5010 if (
Left.isOneOf(tok::kw_using, Keywords.kw_async, Keywords.kw_when,
5011 Keywords.kw_lock)) {
5012 return Style.SpaceBeforeParensOptions.AfterControlStatements ||
5013 spaceRequiredBeforeParens(Right);
5019 if ((
Left.isAccessSpecifierKeyword() ||
5020 Left.isOneOf(tok::kw_virtual, tok::kw_extern, tok::kw_static,
5021 Keywords.kw_internal, Keywords.kw_abstract,
5022 Keywords.kw_sealed, Keywords.kw_override,
5023 Keywords.kw_async, Keywords.kw_unsafe)) &&
5024 Right.is(tok::l_paren)) {
5027 }
else if (Style.isJavaScript()) {
5028 if (
Left.is(TT_FatArrow))
5031 if (
Right.is(tok::l_paren) &&
Left.is(Keywords.kw_await) &&
Left.Previous &&
5032 Left.Previous->is(tok::kw_for)) {
5035 if (
Left.is(Keywords.kw_async) &&
Right.is(tok::l_paren) &&
5036 Right.MatchingParen) {
5037 const FormatToken *Next =
Right.MatchingParen->getNextNonComment();
5040 if (Next && Next->is(TT_FatArrow))
5043 if ((
Left.is(TT_TemplateString) &&
Left.TokenText.ends_with(
"${")) ||
5044 (
Right.is(TT_TemplateString) &&
Right.TokenText.starts_with(
"}"))) {
5049 if (Keywords.isJavaScriptIdentifier(Left,
5051 Right.is(TT_TemplateString)) {
5054 if (
Right.is(tok::star) &&
5055 Left.isOneOf(Keywords.kw_function, Keywords.kw_yield)) {
5058 if (
Right.isOneOf(tok::l_brace, tok::l_square) &&
5059 Left.isOneOf(Keywords.kw_function, Keywords.kw_yield,
5060 Keywords.kw_extends, Keywords.kw_implements)) {
5063 if (
Right.is(tok::l_paren)) {
5065 if (
Line.MustBeDeclaration &&
Left.Tok.getIdentifierInfo())
5069 if (
Left.Previous &&
Left.Previous->is(tok::period) &&
5070 Left.Tok.getIdentifierInfo()) {
5074 if (
Left.isOneOf(tok::kw_throw, Keywords.kw_await, Keywords.kw_typeof,
5080 if (
Left.endsSequence(tok::kw_const, Keywords.kw_as))
5082 if ((
Left.isOneOf(Keywords.kw_let, Keywords.kw_var, Keywords.kw_in,
5087 (
Left.is(Keywords.kw_of) &&
Left.Previous &&
5088 (
Left.Previous->is(tok::identifier) ||
5089 Left.Previous->isOneOf(tok::r_square, tok::r_brace)))) &&
5090 (!
Left.Previous ||
Left.Previous->isNot(tok::period))) {
5093 if (
Left.isOneOf(tok::kw_for, Keywords.kw_as) &&
Left.Previous &&
5094 Left.Previous->is(tok::period) &&
Right.is(tok::l_paren)) {
5097 if (
Left.is(Keywords.kw_as) &&
5098 Right.isOneOf(tok::l_square, tok::l_brace, tok::l_paren)) {
5101 if (
Left.is(tok::kw_default) &&
Left.Previous &&
5102 Left.Previous->is(tok::kw_export)) {
5105 if (
Left.is(Keywords.kw_is) &&
Right.is(tok::l_brace))
5107 if (
Right.isOneOf(TT_JsTypeColon, TT_JsTypeOptionalQuestion))
5109 if (
Left.is(TT_JsTypeOperator) ||
Right.is(TT_JsTypeOperator))
5111 if ((
Left.is(tok::l_brace) ||
Right.is(tok::r_brace)) &&
5112 Line.First->isOneOf(Keywords.kw_import, tok::kw_export)) {
5115 if (
Left.is(tok::ellipsis))
5117 if (
Left.is(TT_TemplateCloser) &&
5118 !
Right.isOneOf(tok::equal, tok::l_brace, tok::comma, tok::l_square,
5119 Keywords.kw_implements, Keywords.kw_extends)) {
5125 if (
Right.is(TT_NonNullAssertion))
5127 if (
Left.is(TT_NonNullAssertion) &&
5128 Right.isOneOf(Keywords.kw_as, Keywords.kw_in)) {
5132 if (
Left.is(TT_CaseLabelArrow) ||
Right.is(TT_CaseLabelArrow))
5134 if (
Left.is(tok::r_square) &&
Right.is(tok::l_brace))
5137 if (
Left.is(tok::l_square) ||
Right.is(tok::r_square))
5138 return Style.SpacesInSquareBrackets;
5140 if (
Left.is(Keywords.kw_synchronized) &&
Right.is(tok::l_paren)) {
5141 return Style.SpaceBeforeParensOptions.AfterControlStatements ||
5142 spaceRequiredBeforeParens(Right);
5144 if ((
Left.isAccessSpecifierKeyword() ||
5145 Left.isOneOf(tok::kw_static, Keywords.kw_final, Keywords.kw_abstract,
5146 Keywords.kw_native)) &&
5147 Right.is(TT_TemplateOpener)) {
5150 }
else if (IsVerilog) {
5152 if (
Left.is(tok::identifier) &&
Left.TokenText[0] ==
'\\')
5156 if ((
Left.is(TT_VerilogTableItem) &&
5157 !
Right.isOneOf(tok::r_paren, tok::semi)) ||
5158 (
Right.is(TT_VerilogTableItem) &&
Left.isNot(tok::l_paren))) {
5159 const FormatToken *Next =
Right.getNextNonComment();
5160 return !(Next && Next->is(tok::r_paren));
5163 if (
Left.isNot(TT_BinaryOperator) &&
5164 Left.isOneOf(Keywords.kw_verilogHash, Keywords.kw_verilogHashHash)) {
5168 if (
Right.isNot(tok::semi) &&
5169 (
Left.endsSequence(tok::numeric_constant, Keywords.kw_verilogHash) ||
5170 Left.endsSequence(tok::numeric_constant,
5171 Keywords.kw_verilogHashHash) ||
5172 (
Left.is(tok::r_paren) &&
Left.MatchingParen &&
5173 Left.MatchingParen->endsSequence(tok::l_paren, tok::at)))) {
5178 if (
Left.is(Keywords.kw_apostrophe) ||
5179 (
Left.is(TT_VerilogNumberBase) &&
Right.is(tok::numeric_constant))) {
5183 if (
Left.is(tok::arrow) ||
Right.is(tok::arrow))
5188 if (
Left.is(tok::at) &&
Right.isOneOf(tok::l_paren, tok::star, tok::at))
5191 if (
Right.is(tok::l_square) &&
5192 Left.isOneOf(TT_VerilogDimensionedTypeName, Keywords.kw_function)) {
5196 if (
Right.isOneOf(tok::period, Keywords.kw_apostrophe) &&
5197 Keywords.isVerilogIdentifier(Left) &&
Left.getPreviousNonComment() &&
5198 Left.getPreviousNonComment()->is(Keywords.kw_tagged)) {
5204 if ((
Right.is(Keywords.kw_apostrophe) ||
5206 !(
Left.isOneOf(Keywords.kw_assign, Keywords.kw_unique) ||
5207 Keywords.isVerilogWordOperator(Left)) &&
5208 (
Left.isOneOf(tok::r_square, tok::r_paren, tok::r_brace,
5209 tok::numeric_constant) ||
5210 Keywords.isWordLike(Left))) {
5214 if ((
Right.is(tok::star) &&
Left.is(tok::coloncolon)) ||
5215 (
Left.is(tok::star) &&
Right.is(tok::semi))) {
5219 if (
Left.endsSequence(tok::star, tok::l_paren) &&
Right.is(tok::identifier))
5222 if (
Right.is(tok::l_paren) &&
Right.is(TT_VerilogStrength))
5225 if ((
Left.is(tok::l_brace) &&
5226 Right.isOneOf(tok::lessless, tok::greatergreater)) ||
5227 (
Left.endsSequence(tok::lessless, tok::l_brace) ||
5228 Left.endsSequence(tok::greatergreater, tok::l_brace))) {
5231 }
else if (Style.isTableGen()) {
5233 if (
Left.is(tok::l_square) &&
Right.is(tok::l_brace))
5235 if (
Left.is(tok::r_brace) &&
Right.is(tok::r_square))
5238 if (
Right.isOneOf(TT_TableGenDAGArgListColon,
5239 TT_TableGenDAGArgListColonToAlign) ||
5240 Left.isOneOf(TT_TableGenDAGArgListColon,
5241 TT_TableGenDAGArgListColonToAlign)) {
5244 if (
Right.is(TT_TableGenCondOperatorColon))
5246 if (
Left.isOneOf(TT_TableGenDAGArgOperatorID,
5247 TT_TableGenDAGArgOperatorToBreak) &&
5248 Right.isNot(TT_TableGenDAGArgCloser)) {
5252 if (
Right.isOneOf(tok::l_paren, tok::less) &&
5253 Left.isOneOf(TT_TableGenBangOperator, TT_TableGenCondOperator)) {
5258 if (
Left.is(TT_TableGenTrailingPasteOperator) &&
5259 Right.isOneOf(tok::l_brace, tok::colon)) {
5263 if (
Left.is(tok::hash) ||
Right.is(tok::hash))
5266 if (Keywords.isTableGenDefinition(Left))
5270 if (
Left.is(TT_ImplicitStringLiteral))
5271 return Right.hasWhitespaceBefore();
5273 if (
Left.is(TT_ObjCMethodSpecifier))
5275 if (
Left.is(tok::r_paren) &&
Left.isNot(TT_AttributeRParen) &&
5276 canBeObjCSelectorComponent(Right)) {
5284 (
Right.is(tok::equal) ||
Left.is(tok::equal))) {
5288 if (
Right.is(TT_TrailingReturnArrow) ||
Left.is(TT_TrailingReturnArrow))
5291 if (
Left.is(tok::comma) &&
Right.isNot(TT_OverloadedOperatorLParen) &&
5294 (
Left.Children.empty() || !
Left.MacroParent)) {
5297 if (
Right.is(tok::comma))
5299 if (
Right.is(TT_ObjCBlockLParen))
5301 if (
Right.is(TT_CtorInitializerColon))
5302 return Style.SpaceBeforeCtorInitializerColon;
5303 if (
Right.is(TT_InheritanceColon) && !Style.SpaceBeforeInheritanceColon)
5305 if (
Right.is(TT_RangeBasedForLoopColon) &&
5306 !Style.SpaceBeforeRangeBasedForLoopColon) {
5309 if (
Left.is(TT_BitFieldColon)) {
5313 if (
Right.is(tok::colon)) {
5314 if (
Right.is(TT_CaseLabelColon))
5315 return Style.SpaceBeforeCaseColon;
5316 if (
Right.is(TT_GotoLabelColon))
5319 if (!
Right.getNextNonComment())
5321 if (
Right.is(TT_ObjCMethodExpr))
5323 if (
Left.is(tok::question))
5325 if (
Right.is(TT_InlineASMColon) &&
Left.is(tok::coloncolon))
5327 if (
Right.is(TT_DictLiteral))
5328 return Style.SpacesInContainerLiterals;
5329 if (
Right.is(TT_AttributeColon))
5331 if (
Right.is(TT_CSharpNamedArgumentColon))
5333 if (
Right.is(TT_GenericSelectionColon))
5335 if (
Right.is(TT_BitFieldColon)) {
5342 if ((
Left.isOneOf(tok::minus, tok::minusminus) &&
5343 Right.isOneOf(tok::minus, tok::minusminus)) ||
5344 (
Left.isOneOf(tok::plus, tok::plusplus) &&
5345 Right.isOneOf(tok::plus, tok::plusplus))) {
5348 if (
Left.is(TT_UnaryOperator)) {
5351 if (
Left.is(tok::amp) &&
Right.is(tok::r_square))
5352 return Style.SpacesInSquareBrackets;
5353 return Style.SpaceAfterLogicalNot &&
Left.is(tok::exclaim);
5358 if (
Left.is(TT_CastRParen)) {
5359 return Style.SpaceAfterCStyleCast ||
5360 Right.isOneOf(TT_BinaryOperator, TT_SelectorName);
5363 auto ShouldAddSpacesInAngles = [
this, &
Right]() {
5367 return Right.hasWhitespaceBefore();
5371 if (
Left.is(tok::greater) &&
Right.is(tok::greater)) {
5374 return !Style.Cpp11BracedListStyle;
5376 return Right.is(TT_TemplateCloser) &&
Left.is(TT_TemplateCloser) &&
5378 ShouldAddSpacesInAngles());
5380 if (
Right.isOneOf(tok::arrow, tok::arrowstar, tok::periodstar) ||
5381 Left.isOneOf(tok::arrow, tok::period, tok::arrowstar, tok::periodstar) ||
5382 (
Right.is(tok::period) &&
Right.isNot(TT_DesignatedInitializerPeriod))) {
5385 if (!Style.SpaceBeforeAssignmentOperators &&
Left.isNot(TT_TemplateCloser) &&
5390 (
Left.is(tok::identifier) ||
Left.is(tok::kw_this))) {
5393 if (
Right.is(tok::coloncolon) &&
Left.is(tok::identifier)) {
5397 return Right.hasWhitespaceBefore();
5399 if (
Right.is(tok::coloncolon) &&
5400 !
Left.isOneOf(tok::l_brace, tok::comment, tok::l_paren)) {
5402 return (
Left.is(TT_TemplateOpener) &&
5404 ShouldAddSpacesInAngles())) ||
5405 !(
Left.isOneOf(tok::l_paren, tok::r_paren, tok::l_square,
5406 tok::kw___super, TT_TemplateOpener,
5407 TT_TemplateCloser)) ||
5408 (
Left.is(tok::l_paren) && Style.SpacesInParensOptions.Other);
5410 if ((
Left.is(TT_TemplateOpener)) != (
Right.is(TT_TemplateCloser)))
5411 return ShouldAddSpacesInAngles();
5413 if (
Right.is(TT_StructuredBindingLSquare)) {
5414 return !
Left.isOneOf(tok::amp, tok::ampamp) ||
5418 if (
Right.Next &&
Right.Next->is(TT_StructuredBindingLSquare) &&
5419 Right.isOneOf(tok::amp, tok::ampamp)) {
5422 if ((
Right.is(TT_BinaryOperator) &&
Left.isNot(tok::l_paren)) ||
5423 (
Left.isOneOf(TT_BinaryOperator, TT_ConditionalExpr) &&
5424 Right.isNot(tok::r_paren))) {
5427 if (
Right.is(TT_TemplateOpener) &&
Left.is(tok::r_paren) &&
5428 Left.MatchingParen &&
5429 Left.MatchingParen->is(TT_OverloadedOperatorLParen)) {
5432 if (
Right.is(tok::less) &&
Left.isNot(tok::l_paren) &&
5436 if (
Right.is(TT_TrailingUnaryOperator))
5438 if (
Left.is(TT_RegexLiteral))
5440 return spaceRequiredBetween(
Line, Left, Right);
5446 !Tok.
isOneOf(TT_ObjCBlockLBrace, TT_LambdaLBrace, TT_DictLiteral);
5463 !Tok.
isOneOf(TT_ObjCBlockLBrace, TT_DictLiteral);
5466bool TokenAnnotator::mustBreakBefore(
const AnnotatedLine &
Line,
5467 const FormatToken &Right)
const {
5468 const FormatToken &Left = *Right.Previous;
5469 if (Right.NewlinesBefore > 1 && Style.MaxEmptyLinesToKeep > 0)
5472 if (Style.BreakFunctionDefinitionParameters &&
Line.MightBeFunctionDecl &&
5473 Line.mightBeFunctionDefinition() && Left.MightBeFunctionDeclParen &&
5474 Left.ParameterCount > 0) {
5478 const auto *BeforeLeft = Left.Previous;
5479 const auto *AfterRight = Right.Next;
5481 if (Style.isCSharp()) {
5482 if (Left.is(TT_FatArrow) && Right.is(tok::l_brace) &&
5483 Style.BraceWrapping.AfterFunction) {
5486 if (
Right.is(TT_CSharpNamedArgumentColon) ||
5487 Left.is(TT_CSharpNamedArgumentColon)) {
5490 if (
Right.is(TT_CSharpGenericTypeConstraint))
5492 if (AfterRight && AfterRight->is(TT_FatArrow) &&
5493 (
Right.is(tok::numeric_constant) ||
5494 (
Right.is(tok::identifier) &&
Right.TokenText ==
"_"))) {
5499 if (
Left.is(TT_AttributeSquare) &&
Left.is(tok::r_square) &&
5500 (
Right.isAccessSpecifier(
false) ||
5501 Right.is(Keywords.kw_internal))) {
5505 if (
Left.is(TT_AttributeSquare) &&
Right.is(TT_AttributeSquare) &&
5506 Left.is(tok::r_square) &&
Right.is(tok::l_square)) {
5509 }
else if (Style.isJavaScript()) {
5511 if (
Right.is(tok::string_literal) &&
Left.is(tok::plus) && BeforeLeft &&
5512 BeforeLeft->is(tok::string_literal)) {
5515 if (
Left.is(TT_DictLiteral) &&
Left.is(tok::l_brace) &&
Line.Level == 0 &&
5516 BeforeLeft && BeforeLeft->is(tok::equal) &&
5517 Line.First->isOneOf(tok::identifier, Keywords.kw_import, tok::kw_export,
5521 !
Line.First->isOneOf(Keywords.kw_var, Keywords.kw_let)) {
5526 if (
Left.is(tok::l_brace) &&
Line.Level == 0 &&
5527 (
Line.startsWith(tok::kw_enum) ||
5528 Line.startsWith(tok::kw_const, tok::kw_enum) ||
5529 Line.startsWith(tok::kw_export, tok::kw_enum) ||
5530 Line.startsWith(tok::kw_export, tok::kw_const, tok::kw_enum))) {
5535 if (
Right.is(tok::r_brace) &&
Left.is(tok::l_brace) && BeforeLeft &&
5536 BeforeLeft->is(TT_FatArrow)) {
5538 switch (Style.AllowShortLambdasOnASingleLine) {
5544 return !
Left.Children.empty();
5548 return (
Left.NestingLevel == 0 &&
Line.Level == 0) &&
5549 !
Left.Children.empty();
5551 llvm_unreachable(
"Unknown FormatStyle::ShortLambdaStyle enum");
5554 if (
Right.is(tok::r_brace) &&
Left.is(tok::l_brace) &&
5555 !
Left.Children.empty()) {
5559 (
Left.NestingLevel == 0 &&
Line.Level == 0 &&
5560 Style.AllowShortFunctionsOnASingleLine &
5564 if (
Right.is(tok::plus) &&
Left.is(tok::string_literal) && AfterRight &&
5565 AfterRight->is(tok::string_literal)) {
5568 }
else if (Style.isVerilog()) {
5570 if (
Left.is(TT_VerilogAssignComma))
5573 if (
Left.is(TT_VerilogTypeComma))
5577 if (Style.VerilogBreakBetweenInstancePorts &&
5578 (
Left.is(TT_VerilogInstancePortComma) ||
5579 (
Left.is(tok::r_paren) && Keywords.isVerilogIdentifier(Right) &&
5580 Left.MatchingParen &&
5581 Left.MatchingParen->is(TT_VerilogInstancePortLParen)))) {
5586 if (!Keywords.isVerilogBegin(Right) && Keywords.isVerilogEndOfLabel(Left))
5588 }
else if (Style.BreakAdjacentStringLiterals &&
5589 (IsCpp || Style.isProto() ||
5591 if (
Left.isStringLiteral() &&
Right.isStringLiteral())
5596 if (Style.isJson()) {
5600 if (
Left.is(TT_DictLiteral) &&
Left.is(tok::l_brace))
5603 if ((
Left.is(TT_ArrayInitializerLSquare) &&
Left.is(tok::l_square) &&
5604 Right.isNot(tok::r_square)) ||
5605 Left.is(tok::comma)) {
5606 if (
Right.is(tok::l_brace))
5610 for (
const auto *Tok = &Right; Tok; Tok = Tok->Next) {
5611 if (Tok->isOneOf(tok::l_brace, tok::l_square))
5613 if (Tok->isOneOf(tok::r_brace, tok::r_square))
5616 return Style.BreakArrays;
5618 }
else if (Style.isTableGen()) {
5622 if (
Left.is(TT_TableGenCondOperatorComma))
5624 if (
Left.is(TT_TableGenDAGArgOperatorToBreak) &&
5625 Right.isNot(TT_TableGenDAGArgCloser)) {
5628 if (
Left.is(TT_TableGenDAGArgListCommaToBreak))
5630 if (
Right.is(TT_TableGenDAGArgCloser) &&
Right.MatchingParen &&
5631 Right.MatchingParen->is(TT_TableGenDAGArgOpenerToBreak) &&
5638 if (
Line.startsWith(tok::kw_asm) &&
Right.is(TT_InlineASMColon) &&
5648 const FormatToken *BeforeClosingBrace =
nullptr;
5649 if ((
Left.isOneOf(tok::l_brace, TT_ArrayInitializerLSquare) ||
5650 (Style.isJavaScript() &&
Left.is(tok::l_paren))) &&
5652 BeforeClosingBrace =
Left.MatchingParen->Previous;
5653 }
else if (
Right.MatchingParen &&
5654 (
Right.MatchingParen->isOneOf(tok::l_brace,
5655 TT_ArrayInitializerLSquare) ||
5656 (Style.isJavaScript() &&
5657 Right.MatchingParen->is(tok::l_paren)))) {
5658 BeforeClosingBrace = &
Left;
5660 if (BeforeClosingBrace && (BeforeClosingBrace->is(tok::comma) ||
5661 BeforeClosingBrace->isTrailingComment())) {
5666 if (
Right.is(tok::comment)) {
5668 (
Right.NewlinesBefore > 0 &&
Right.HasUnescapedNewline);
5670 if (
Left.isTrailingComment())
5672 if (
Left.IsUnterminatedLiteral)
5675 if (BeforeLeft && BeforeLeft->is(tok::lessless) &&
5676 Left.is(tok::string_literal) &&
Right.is(tok::lessless) && AfterRight &&
5677 AfterRight->is(tok::string_literal)) {
5678 return Right.NewlinesBefore > 0;
5681 if (
Right.is(TT_RequiresClause)) {
5682 switch (Style.RequiresClausePosition) {
5692 if (
Left.ClosesTemplateDeclaration &&
Left.MatchingParen &&
5693 Left.MatchingParen->NestingLevel == 0) {
5697 if (
Right.is(tok::kw_concept))
5701 Right.NewlinesBefore > 0);
5703 if (
Left.ClosesRequiresClause) {
5704 switch (Style.RequiresClausePosition) {
5707 return Right.isNot(tok::semi);
5709 return !
Right.isOneOf(tok::semi, tok::l_brace);
5716 (
Left.is(TT_CtorInitializerComma) ||
5717 Right.is(TT_CtorInitializerColon))) {
5722 Left.isOneOf(TT_CtorInitializerColon, TT_CtorInitializerComma)) {
5728 Right.isOneOf(TT_CtorInitializerComma, TT_CtorInitializerColon)) {
5734 Right.is(TT_CtorInitializerColon)) {
5739 Left.is(TT_CtorInitializerColon)) {
5745 Right.is(TT_InheritanceComma)) {
5749 Left.is(TT_InheritanceComma)) {
5752 if (
Right.is(tok::string_literal) &&
Right.TokenText.starts_with(
"R\"")) {
5756 return Right.IsMultiline &&
Right.NewlinesBefore > 0;
5758 if ((
Left.is(tok::l_brace) ||
5759 (
Left.is(tok::less) && BeforeLeft && BeforeLeft->is(tok::equal))) &&
5765 if (
Right.is(TT_InlineASMBrace))
5766 return Right.HasUnescapedNewline;
5769 auto *FirstNonComment =
Line.getFirstNonComment();
5771 FirstNonComment && (FirstNonComment->is(Keywords.kw_internal) ||
5772 FirstNonComment->isAccessSpecifierKeyword());
5774 if (Style.BraceWrapping.AfterEnum) {
5775 if (
Line.startsWith(tok::kw_enum) ||
5776 Line.startsWith(tok::kw_typedef, tok::kw_enum)) {
5781 FirstNonComment->Next->is(tok::kw_enum)) {
5787 if (Style.BraceWrapping.AfterClass &&
5789 FirstNonComment->Next->is(Keywords.kw_interface)) ||
5790 Line.startsWith(Keywords.kw_interface))) {
5795 if (
Right.isNot(TT_FunctionLBrace)) {
5796 return (
Line.startsWith(tok::kw_class) &&
5797 Style.BraceWrapping.AfterClass) ||
5798 (
Line.startsWith(tok::kw_struct) &&
5799 Style.BraceWrapping.AfterStruct);
5803 if (
Left.is(TT_ObjCBlockLBrace) &&
5809 if (
Left.isOneOf(TT_AttributeRParen, TT_AttributeMacro) &&
5810 Right.is(TT_ObjCDecl)) {
5814 if (
Left.is(TT_LambdaLBrace)) {
5822 (!
Left.Children.empty() &&
5828 if (Style.BraceWrapping.BeforeLambdaBody &&
Right.is(TT_LambdaLBrace) &&
5829 (
Left.isPointerOrReference() ||
Left.is(TT_TemplateCloser))) {
5835 Left.is(TT_LeadingJavaAnnotation) &&
5836 Right.isNot(TT_LeadingJavaAnnotation) &&
Right.isNot(tok::l_paren) &&
5837 (
Line.Last->is(tok::l_brace) || Style.BreakAfterJavaFieldAnnotations)) {
5841 if (
Right.is(TT_ProtoExtensionLSquare))
5871 if (Style.isProto() &&
Right.is(TT_SelectorName) &&
5872 Right.isNot(tok::r_square) && AfterRight) {
5875 if (
Left.is(tok::at))
5881 const auto *LBrace = AfterRight;
5882 if (LBrace && LBrace->is(tok::colon)) {
5883 LBrace = LBrace->Next;
5884 if (LBrace && LBrace->is(tok::at)) {
5885 LBrace = LBrace->Next;
5887 LBrace = LBrace->Next;
5899 ((LBrace->is(tok::l_brace) &&
5900 (LBrace->is(TT_DictLiteral) ||
5901 (LBrace->Next && LBrace->Next->is(tok::r_brace)))) ||
5902 LBrace->is(TT_ArrayInitializerLSquare) || LBrace->is(tok::less))) {
5909 if (
Left.ParameterCount == 0)
5924 if (
Left.isOneOf(tok::r_brace, tok::greater, tok::r_square))
5931bool TokenAnnotator::canBreakBefore(
const AnnotatedLine &
Line,
5932 const FormatToken &Right)
const {
5933 const FormatToken &
Left = *
Right.Previous;
5935 if (Style.isCSharp()) {
5936 if (
Left.isOneOf(TT_CSharpNamedArgumentColon, TT_AttributeColon) ||
5937 Right.isOneOf(TT_CSharpNamedArgumentColon, TT_AttributeColon)) {
5941 if (
Line.First->is(TT_CSharpGenericTypeConstraint))
5942 return Left.is(TT_CSharpGenericTypeConstraintComma);
5944 if (
Right.is(TT_CSharpNullable))
5947 if (
Left.isOneOf(Keywords.kw_throws, Keywords.kw_extends,
5948 Keywords.kw_implements)) {
5951 if (
Right.isOneOf(Keywords.kw_throws, Keywords.kw_extends,
5952 Keywords.kw_implements)) {
5955 }
else if (Style.isJavaScript()) {
5956 const FormatToken *NonComment =
Right.getPreviousNonComment();
5958 (NonComment->isAccessSpecifierKeyword() ||
5959 NonComment->isOneOf(
5960 tok::kw_return, Keywords.kw_yield, tok::kw_continue, tok::kw_break,
5961 tok::kw_throw, Keywords.kw_interface, Keywords.kw_type,
5962 tok::kw_static, Keywords.kw_readonly, Keywords.kw_override,
5963 Keywords.kw_abstract, Keywords.kw_get, Keywords.kw_set,
5964 Keywords.kw_async, Keywords.kw_await))) {
5967 if (
Right.NestingLevel == 0 &&
5968 (
Left.Tok.getIdentifierInfo() ||
5969 Left.isOneOf(tok::r_square, tok::r_paren)) &&
5970 Right.isOneOf(tok::l_square, tok::l_paren)) {
5973 if (NonComment && NonComment->is(tok::identifier) &&
5974 NonComment->TokenText ==
"asserts") {
5977 if (
Left.is(TT_FatArrow) &&
Right.is(tok::l_brace))
5979 if (
Left.is(TT_JsTypeColon))
5982 if (
Left.is(tok::exclaim) &&
Right.is(tok::colon))
5987 if (
Right.is(Keywords.kw_is)) {
5988 const FormatToken *Next =
Right.getNextNonComment();
5996 if (!Next || Next->isNot(tok::colon))
5999 if (
Left.is(Keywords.kw_in))
6001 if (
Right.is(Keywords.kw_in))
6003 if (
Right.is(Keywords.kw_as))
6005 if (
Right.isOneOf(Keywords.kw_extends, Keywords.kw_infer)) {
6011 if (
Left.is(Keywords.kw_as))
6013 if (
Left.is(TT_NonNullAssertion))
6015 if (
Left.is(Keywords.kw_declare) &&
6016 Right.isOneOf(Keywords.kw_module, tok::kw_namespace,
6017 Keywords.kw_function, tok::kw_class, tok::kw_enum,
6018 Keywords.kw_interface, Keywords.kw_type, Keywords.kw_var,
6019 Keywords.kw_let, tok::kw_const)) {
6024 if (
Left.isOneOf(Keywords.kw_module, tok::kw_namespace) &&
6025 Right.isOneOf(tok::identifier, tok::string_literal)) {
6028 if (
Right.is(TT_TemplateString) &&
Right.closesScope())
6032 if (
Left.is(tok::identifier) &&
Right.is(TT_TemplateString))
6034 if (
Left.is(TT_TemplateString) &&
Left.opensScope())
6036 }
else if (Style.isTableGen()) {
6038 if (Keywords.isTableGenDefinition(Left))
6041 if (
Right.is(tok::l_paren)) {
6042 return !
Left.isOneOf(TT_TableGenBangOperator, TT_TableGenCondOperator,
6046 if (
Left.is(TT_TableGenValueSuffix))
6049 if (
Left.is(tok::hash) ||
Right.is(tok::hash))
6051 if (
Left.isOneOf(TT_TableGenBangOperator, TT_TableGenCondOperator))
6055 if (
Left.is(tok::at))
6057 if (
Left.Tok.getObjCKeywordID() == tok::objc_interface)
6059 if (
Left.isOneOf(TT_JavaAnnotation, TT_LeadingJavaAnnotation))
6060 return Right.isNot(tok::l_paren);
6061 if (
Right.is(TT_PointerOrReference)) {
6062 return Line.IsMultiVariableDeclStmt ||
6063 (getTokenPointerOrReferenceAlignment(Right) ==
6065 (!
Right.Next ||
Right.Next->isNot(TT_FunctionDeclarationName)));
6067 if (
Right.isOneOf(TT_StartOfName, TT_FunctionDeclarationName) ||
6068 Right.is(tok::kw_operator)) {
6071 if (
Left.is(TT_PointerOrReference))
6073 if (
Right.isTrailingComment()) {
6080 (
Left.is(TT_CtorInitializerColon) &&
Right.NewlinesBefore > 0 &&
6083 if (
Left.is(tok::question) &&
Right.is(tok::colon))
6085 if (
Right.is(TT_ConditionalExpr) ||
Right.is(tok::question))
6086 return Style.BreakBeforeTernaryOperators;
6087 if (
Left.is(TT_ConditionalExpr) ||
Left.is(tok::question))
6088 return !Style.BreakBeforeTernaryOperators;
6089 if (
Left.is(TT_InheritanceColon))
6091 if (
Right.is(TT_InheritanceColon))
6093 if (
Right.is(TT_ObjCMethodExpr) &&
Right.isNot(tok::r_square) &&
6094 Left.isNot(TT_SelectorName)) {
6098 if (
Right.is(tok::colon) &&
6099 !
Right.isOneOf(TT_CtorInitializerColon, TT_InlineASMColon)) {
6102 if (
Left.is(tok::colon) &&
Left.isOneOf(TT_DictLiteral, TT_ObjCMethodExpr)) {
6103 if (Style.isProto()) {
6104 if (!Style.AlwaysBreakBeforeMultilineStrings &&
Right.isStringLiteral())
6130 if (((
Right.is(tok::l_brace) ||
Right.is(tok::less)) &&
6131 Right.is(TT_DictLiteral)) ||
6132 Right.is(TT_ArrayInitializerLSquare)) {
6138 if (
Right.is(tok::r_square) &&
Right.MatchingParen &&
6139 Right.MatchingParen->is(TT_ProtoExtensionLSquare)) {
6142 if (
Right.is(TT_SelectorName) || (
Right.is(tok::identifier) &&
Right.Next &&
6143 Right.Next->is(TT_ObjCMethodExpr))) {
6144 return Left.isNot(tok::period);
6148 if (
Right.is(tok::kw_concept))
6150 if (
Right.is(TT_RequiresClause))
6152 if (
Left.ClosesTemplateDeclaration) {
6154 Right.NewlinesBefore > 0;
6156 if (
Left.is(TT_FunctionAnnotationRParen))
6158 if (
Left.ClosesRequiresClause)
6160 if (
Right.isOneOf(TT_RangeBasedForLoopColon, TT_OverloadedOperatorLParen,
6161 TT_OverloadedOperator)) {
6164 if (
Left.is(TT_RangeBasedForLoopColon))
6166 if (
Right.is(TT_RangeBasedForLoopColon))
6168 if (
Left.is(TT_TemplateCloser) &&
Right.is(TT_TemplateOpener))
6170 if ((
Left.is(tok::greater) &&
Right.is(tok::greater)) ||
6171 (
Left.is(tok::less) &&
Right.is(tok::less))) {
6174 if (
Right.is(TT_BinaryOperator) &&
6180 if (
Left.isOneOf(TT_TemplateCloser, TT_UnaryOperator) ||
6181 Left.is(tok::kw_operator)) {
6184 if (
Left.is(tok::equal) && !
Right.isOneOf(tok::kw_default, tok::kw_delete) &&
6188 if (
Left.is(tok::equal) &&
Right.is(tok::l_brace) &&
6189 !Style.Cpp11BracedListStyle) {
6192 if (
Left.is(TT_AttributeLParen) ||
6193 (
Left.is(tok::l_paren) &&
Left.is(TT_TypeDeclarationParen))) {
6196 if (
Left.is(tok::l_paren) &&
Left.Previous &&
6197 (
Left.Previous->isOneOf(TT_BinaryOperator, TT_CastRParen))) {
6200 if (
Right.is(TT_ImplicitStringLiteral))
6203 if (
Right.is(TT_TemplateCloser))
6205 if (
Right.is(tok::r_square) &&
Right.MatchingParen &&
6206 Right.MatchingParen->is(TT_LambdaLSquare)) {
6212 if (
Right.is(tok::r_brace)) {
6214 (
Right.isBlockIndentedInitRBrace(Style)));
6218 if (
Right.is(tok::r_paren)) {
6220 !
Right.MatchingParen) {
6223 auto Next =
Right.Next;
6224 if (Next && Next->is(tok::r_paren))
6226 if (Next && Next->is(tok::l_paren))
6228 const FormatToken *
Previous =
Right.MatchingParen->Previous;
6232 if (
Left.isOneOf(tok::r_paren, TT_TrailingAnnotation) &&
6233 Right.is(TT_TrailingAnnotation) &&
6240 if (
Left.is(TT_TrailingAnnotation)) {
6241 return !
Right.isOneOf(tok::l_brace, tok::semi, tok::equal, tok::l_paren,
6242 tok::less, tok::coloncolon);
6245 if (
Right.isAttribute())
6248 if (
Right.is(tok::l_square) &&
Right.is(TT_AttributeSquare))
6249 return Left.isNot(TT_AttributeSquare);
6251 if (
Left.is(tok::identifier) &&
Right.is(tok::string_literal))
6254 if (
Right.is(tok::identifier) &&
Right.Next &&
Right.Next->is(TT_DictLiteral))
6257 if (
Left.is(TT_CtorInitializerColon)) {
6259 (!
Right.isTrailingComment() ||
Right.NewlinesBefore > 0);
6261 if (
Right.is(TT_CtorInitializerColon))
6263 if (
Left.is(TT_CtorInitializerComma) &&
6267 if (
Right.is(TT_CtorInitializerComma) &&
6271 if (
Left.is(TT_InheritanceComma) &&
6275 if (
Right.is(TT_InheritanceComma) &&
6279 if (
Left.is(TT_ArrayInitializerLSquare))
6281 if (
Right.is(tok::kw_typename) &&
Left.isNot(tok::kw_const))
6283 if ((
Left.isBinaryOperator() ||
Left.is(TT_BinaryOperator)) &&
6284 !
Left.isOneOf(tok::arrowstar, tok::lessless) &&
6290 if ((
Left.is(TT_AttributeSquare) &&
Right.is(tok::l_square)) ||
6291 (
Left.is(tok::r_square) &&
Right.is(TT_AttributeSquare))) {
6295 auto ShortLambdaOption = Style.AllowShortLambdasOnASingleLine;
6296 if (Style.BraceWrapping.BeforeLambdaBody &&
Right.is(TT_LambdaLBrace)) {
6303 if (
Right.is(tok::kw_noexcept) &&
Right.is(TT_TrailingAnnotation)) {
6304 switch (Style.AllowBreakBeforeNoexceptSpecifier) {
6310 return Right.Next &&
Right.Next->is(tok::l_paren);
6314 return Left.isOneOf(tok::comma, tok::coloncolon, tok::semi, tok::l_brace,
6315 tok::kw_class, tok::kw_struct, tok::comment) ||
6316 Right.isMemberAccess() ||
6317 Right.isOneOf(TT_TrailingReturnArrow, tok::lessless, tok::colon,
6318 tok::l_square, tok::at) ||
6319 (
Left.is(tok::r_paren) &&
6320 Right.isOneOf(tok::identifier, tok::kw_const)) ||
6321 (
Left.is(tok::l_paren) &&
Right.isNot(tok::r_paren)) ||
6322 (
Left.is(TT_TemplateOpener) &&
Right.isNot(TT_TemplateCloser));
6325void TokenAnnotator::printDebugInfo(
const AnnotatedLine &
Line)
const {
6326 llvm::errs() <<
"AnnotatedTokens(L=" <<
Line.Level <<
", P=" <<
Line.PPLevel
6327 <<
", T=" <<
Line.Type <<
", C=" <<
Line.IsContinuation
6329 const FormatToken *Tok =
Line.First;
6331 llvm::errs() <<
" M=" << Tok->MustBreakBefore
6332 <<
" C=" << Tok->CanBreakBefore
6334 <<
" S=" << Tok->SpacesRequiredBefore
6335 <<
" F=" << Tok->Finalized <<
" B=" << Tok->BlockParameterCount
6336 <<
" BK=" << Tok->getBlockKind() <<
" P=" << Tok->SplitPenalty
6337 <<
" Name=" << Tok->Tok.getName() <<
" L=" << Tok->TotalLength
6338 <<
" PPK=" << Tok->getPackingKind() <<
" FakeLParens=";
6340 llvm::errs() << LParen <<
"/";
6341 llvm::errs() <<
" FakeRParens=" << Tok->FakeRParens;
6342 llvm::errs() <<
" II=" << Tok->Tok.getIdentifierInfo();
6343 llvm::errs() <<
" Text='" << Tok->TokenText <<
"'\n";
6345 assert(Tok ==
Line.Last);
6348 llvm::errs() <<
"----\n";
6352TokenAnnotator::getTokenReferenceAlignment(
const FormatToken &
Reference)
const {
6353 assert(
Reference.isOneOf(tok::amp, tok::ampamp));
6354 switch (Style.ReferenceAlignment) {
6356 return Style.PointerAlignment;
6365 return Style.PointerAlignment;
6369TokenAnnotator::getTokenPointerOrReferenceAlignment(
6370 const FormatToken &PointerOrReference)
const {
6371 if (PointerOrReference.isOneOf(tok::amp, tok::ampamp)) {
6372 switch (Style.ReferenceAlignment) {
6374 return Style.PointerAlignment;
6383 assert(PointerOrReference.is(tok::star));
6384 return Style.PointerAlignment;
Defines the SourceManager interface.
bool ColonIsObjCMethodExpr
FormatToken * FirstStartOfName
bool InCpp11AttributeSpecifier
enum clang::format::@1291::AnnotatingParser::Context::@350 ContextType
unsigned LongestObjCSelectorName
bool VerilogAssignmentFound
bool InCSharpAttributeSpecifier
tok::TokenKind ContextKind
FormatToken * FirstObjCSelectorName
bool VerilogMayBeConcatenation
This file implements a token annotator, i.e.
Defines the clang::TokenKind enum and support functions.
#define TRANSFORM_TYPE_TRAIT_DEF(Enum, _)
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
Parser - This implements a parser for the C family of languages.
IdentifierInfo * getIdentifierInfo() const
bool Ret(InterpState &S, CodePtr &PC, APValue &Result)
TokenKind
Provides a simple uniform namespace for tokens from all C languages.
The JSON file list parser is used to communicate input to InstallAPI.
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...