18#include "llvm/ADT/SmallPtrSet.h"
19#include "llvm/Support/Debug.h"
21#define DEBUG_TYPE "format-token-annotator"
29static bool startsWithInitStatement(
const AnnotatedLine &
Line) {
30 return Line.startsWith(tok::kw_for) ||
Line.startsWith(tok::kw_if) ||
31 Line.startsWith(tok::kw_switch);
46 return Tok.Tok.getIdentifierInfo();
52static bool isLambdaParameterList(
const FormatToken *Left) {
54 if (Left->Previous && Left->Previous->is(tok::greater) &&
55 Left->Previous->MatchingParen &&
56 Left->Previous->MatchingParen->is(TT_TemplateOpener)) {
57 Left = Left->Previous->MatchingParen;
61 return Left->Previous && Left->Previous->is(tok::r_square) &&
62 Left->Previous->MatchingParen &&
63 Left->Previous->MatchingParen->is(TT_LambdaLSquare);
69 return Tok.isOneOf(tok::kw_if, tok::kw_for, tok::kw_while, tok::kw_switch,
70 tok::kw_constexpr, tok::kw_catch);
75 if (!IsCpp || !
Tok.startsSequence(tok::l_square, tok::l_square))
79 if (
Tok.Previous &&
Tok.Previous->isOneOf(tok::at, tok::l_square))
86 if (AttrTok->startsSequence(tok::kw_using, tok::identifier, tok::colon))
88 if (AttrTok->isNot(tok::identifier))
90 while (AttrTok && !AttrTok->startsSequence(tok::r_square, tok::r_square)) {
94 if (AttrTok->is(tok::colon) ||
95 AttrTok->startsSequence(tok::identifier, tok::identifier) ||
96 AttrTok->startsSequence(tok::r_paren, tok::identifier)) {
99 if (AttrTok->is(tok::ellipsis))
101 AttrTok = AttrTok->Next;
103 return AttrTok && AttrTok->startsSequence(tok::r_square, tok::r_square);
111class AnnotatingParser {
114 const AdditionalKeywords &Keywords,
116 : Style(Style),
Line(
Line), CurrentToken(
Line.First), AutoFound(
false),
118 Keywords(Keywords), Scopes(Scopes), TemplateDeclarationDepth(0) {
119 Contexts.push_back(Context(tok::unknown, 1,
false));
120 resetTokenMetadata();
125 switch (Token.getType()) {
127 case TT_StructLBrace:
132 case TT_CompoundRequirementLBrace:
143 auto *Left = CurrentToken->Previous;
147 if (NonTemplateLess.count(Left) > 0)
150 const auto *BeforeLess = Left->Previous;
153 if (BeforeLess->Tok.isLiteral())
155 if (BeforeLess->is(tok::r_brace))
157 if (BeforeLess->is(tok::r_paren) && Contexts.size() > 1 &&
158 !(BeforeLess->MatchingParen &&
159 BeforeLess->MatchingParen->is(TT_OverloadedOperatorLParen))) {
162 if (BeforeLess->is(tok::kw_operator) && CurrentToken->is(tok::l_paren))
166 Left->ParentBracket = Contexts.back().ContextKind;
167 ScopedContextCreator ContextCreator(*
this, tok::less, 12);
168 Contexts.back().IsExpression =
false;
172 if (BeforeLess && BeforeLess->isNot(tok::kw_template))
173 Contexts.back().ContextType = Context::TemplateArgument;
175 if (Style.isJava() && CurrentToken->is(tok::question))
178 for (
bool SeenTernaryOperator =
false, MaybeAngles =
true; CurrentToken;) {
179 const auto &ParentContext = Contexts[Contexts.size() - 2];
180 const bool InExpr = ParentContext.IsExpression;
181 if (CurrentToken->is(tok::greater)) {
182 const auto *
Next = CurrentToken->Next;
183 if (CurrentToken->isNot(TT_TemplateCloser)) {
190 if (
Next &&
Next->is(tok::greater) &&
191 Left->ParentBracket != tok::less &&
192 CurrentToken->getStartOfNonWhitespace() ==
193 Next->getStartOfNonWhitespace().getLocWithOffset(-1)) {
196 if (InExpr && SeenTernaryOperator &&
197 (!
Next ||
Next->isNoneOf(tok::l_paren, tok::l_brace))) {
202 if (ParentContext.InStaticAssertFirstArgument &&
Next &&
203 Next->isOneOf(tok::minus, tok::identifier)) {
207 Left->MatchingParen = CurrentToken;
208 CurrentToken->MatchingParen = Left;
214 if (Style.isTextProto() ||
216 BeforeLess->isOneOf(TT_SelectorName, TT_DictLiteral))) {
217 CurrentToken->setType(TT_DictLiteral);
219 CurrentToken->setType(TT_TemplateCloser);
220 CurrentToken->Tok.setLength(1);
227 if (BeforeLess && BeforeLess->is(TT_TemplateName)) {
231 if (CurrentToken->is(tok::question) && Style.isJava()) {
235 if (CurrentToken->isOneOf(tok::r_paren, tok::r_square, tok::r_brace))
237 const auto &Prev = *CurrentToken->Previous;
244 if (MaybeAngles && InExpr && !
Line.startsWith(tok::kw_template) &&
245 Prev.is(TT_BinaryOperator) &&
246 Prev.isOneOf(tok::pipepipe, tok::ampamp)) {
249 if (Prev.isOneOf(tok::question, tok::colon) && !Style.isProto())
250 SeenTernaryOperator =
true;
251 updateParameterCount(Left, CurrentToken);
254 if (CurrentToken->is(tok::colon) ||
255 (CurrentToken->isOneOf(tok::l_brace, tok::less) &&
260 }
else if (Style.isTableGen()) {
261 if (CurrentToken->isOneOf(tok::comma, tok::equal)) {
268 if (!parseTableGenValue())
278 bool parseUntouchableParens() {
279 while (CurrentToken) {
280 CurrentToken->Finalized =
true;
281 switch (CurrentToken->Tok.getKind()) {
284 if (!parseUntouchableParens())
299 bool parseParens(
bool IsIf =
false) {
302 assert(CurrentToken->Previous &&
"Unknown previous token");
303 FormatToken &OpeningParen = *CurrentToken->Previous;
304 assert(OpeningParen.is(tok::l_paren));
305 FormatToken *PrevNonComment = OpeningParen.getPreviousNonComment();
306 OpeningParen.ParentBracket = Contexts.back().ContextKind;
307 ScopedContextCreator ContextCreator(*
this, tok::l_paren, 1);
310 Contexts.back().ColonIsForRangeExpr =
311 Contexts.size() == 2 && Contexts[0].ColonIsForRangeExpr;
313 if (OpeningParen.Previous &&
314 OpeningParen.Previous->is(TT_UntouchableMacroFunc)) {
315 OpeningParen.Finalized =
true;
316 return parseUntouchableParens();
319 bool StartsObjCSelector =
false;
320 if (!Style.isVerilog()) {
321 if (
FormatToken *MaybeSel = OpeningParen.Previous) {
323 if (MaybeSel->is(tok::objc_selector) && MaybeSel->Previous &&
324 MaybeSel->Previous->is(tok::at)) {
325 StartsObjCSelector =
true;
330 if (OpeningParen.is(TT_OverloadedOperatorLParen)) {
333 while (Prev->isNot(tok::kw_operator)) {
334 Prev = Prev->Previous;
335 assert(Prev &&
"Expect a kw_operator prior to the OperatorLParen!");
341 bool OperatorCalledAsMemberFunction =
342 Prev->Previous && Prev->Previous->isOneOf(tok::period, tok::arrow);
343 Contexts.back().IsExpression = OperatorCalledAsMemberFunction;
344 }
else if (OpeningParen.is(TT_VerilogInstancePortLParen)) {
345 Contexts.back().IsExpression =
true;
346 Contexts.back().ContextType = Context::VerilogInstancePortList;
347 }
else if (Style.isJavaScript() &&
348 (
Line.startsWith(Keywords.kw_type, tok::identifier) ||
349 Line.startsWith(tok::kw_export, Keywords.kw_type,
353 Contexts.back().IsExpression =
false;
354 }
else if (OpeningParen.Previous &&
355 (OpeningParen.Previous->isOneOf(
356 tok::kw_noexcept, tok::kw_explicit, tok::kw_while,
357 tok::l_paren, tok::comma, TT_CastRParen,
358 TT_BinaryOperator) ||
359 OpeningParen.Previous->isIf())) {
361 Contexts.back().IsExpression =
true;
362 }
else if (Style.isJavaScript() && OpeningParen.Previous &&
363 (OpeningParen.Previous->is(Keywords.kw_function) ||
364 (OpeningParen.Previous->endsSequence(tok::identifier,
365 Keywords.kw_function)))) {
367 Contexts.back().IsExpression =
false;
368 }
else if (Style.isJavaScript() && OpeningParen.Previous &&
369 OpeningParen.Previous->is(TT_JsTypeColon)) {
371 Contexts.back().IsExpression =
false;
372 }
else if (isLambdaParameterList(&OpeningParen)) {
374 OpeningParen.setType(TT_LambdaDefinitionLParen);
375 Contexts.back().IsExpression =
false;
376 }
else if (OpeningParen.is(TT_RequiresExpressionLParen)) {
377 Contexts.back().IsExpression =
false;
378 }
else if (OpeningParen.Previous &&
379 OpeningParen.Previous->is(tok::kw__Generic)) {
380 Contexts.back().ContextType = Context::C11GenericSelection;
381 Contexts.back().IsExpression =
true;
382 }
else if (OpeningParen.Previous &&
383 OpeningParen.Previous->TokenText ==
"Q_PROPERTY") {
384 Contexts.back().ContextType = Context::QtProperty;
385 Contexts.back().IsExpression =
false;
386 }
else if (
Line.InPPDirective &&
387 (!OpeningParen.Previous ||
388 OpeningParen.Previous->isNot(tok::identifier))) {
389 Contexts.back().IsExpression =
true;
390 }
else if (Contexts[Contexts.size() - 2].CaretFound) {
392 Contexts.back().IsExpression =
false;
393 }
else if (OpeningParen.Previous &&
394 OpeningParen.Previous->is(TT_ForEachMacro)) {
396 Contexts.back().ContextType = Context::ForEachMacro;
397 Contexts.back().IsExpression =
false;
398 }
else if (OpeningParen.Previous && OpeningParen.Previous->MatchingParen &&
399 OpeningParen.Previous->MatchingParen->isOneOf(
400 TT_ObjCBlockLParen, TT_FunctionTypeLParen)) {
401 Contexts.back().IsExpression =
false;
402 }
else if (!
Line.MustBeDeclaration &&
403 (!
Line.InPPDirective || (
Line.InMacroBody && !Scopes.empty()))) {
405 OpeningParen.Previous &&
406 OpeningParen.Previous->isOneOf(tok::kw_for, tok::kw_catch);
407 Contexts.back().IsExpression = !IsForOrCatch;
410 if (Style.isTableGen()) {
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/BuiltinTraits.inc"
442 OpeningParen.setType(TT_TypeDeclarationParen);
444 if (PrevNonComment->isOneOf(tok::kw_decltype, tok::kw_typeof))
445 Contexts.back().IsExpression =
true;
449 if (StartsObjCSelector)
450 OpeningParen.setType(TT_ObjCSelector);
452 const bool IsStaticAssert =
453 PrevNonComment && PrevNonComment->is(tok::kw_static_assert);
455 Contexts.back().InStaticAssertFirstArgument =
true;
465 bool MightBeFunctionType = !Contexts[Contexts.size() - 2].IsExpression;
466 bool ProbablyFunctionType =
467 CurrentToken->isPointerOrReference() || CurrentToken->is(tok::caret);
468 bool HasMultipleLines =
false;
469 bool HasMultipleParametersOnALine =
false;
470 bool MightBeObjCForRangeLoop =
471 OpeningParen.Previous && OpeningParen.Previous->is(tok::kw_for);
473 while (CurrentToken) {
474 const auto &Prev = *CurrentToken->Previous;
475 const auto *PrevPrev = Prev.Previous;
476 if (Prev.is(TT_PointerOrReference) &&
477 PrevPrev->isOneOf(tok::l_paren, tok::coloncolon)) {
478 ProbablyFunctionType =
true;
480 if (CurrentToken->is(tok::comma))
481 MightBeFunctionType =
false;
482 if (Prev.is(TT_BinaryOperator))
483 Contexts.back().IsExpression =
true;
484 if (CurrentToken->is(tok::r_paren)) {
485 if (Prev.is(TT_PointerOrReference) &&
486 (PrevPrev == &OpeningParen || PrevPrev->is(tok::coloncolon))) {
487 MightBeFunctionType =
true;
489 if (OpeningParen.isNot(TT_CppCastLParen) && MightBeFunctionType &&
490 ProbablyFunctionType && CurrentToken->Next &&
491 (CurrentToken->Next->is(tok::l_paren) ||
492 (CurrentToken->Next->is(tok::l_square) &&
493 (
Line.MustBeDeclaration ||
494 (PrevNonComment && PrevNonComment->isTypeName(LangOpts)))))) {
495 OpeningParen.setType(OpeningParen.Next->is(tok::caret)
497 : TT_FunctionTypeLParen);
499 OpeningParen.MatchingParen = CurrentToken;
500 CurrentToken->MatchingParen = &OpeningParen;
502 if (CurrentToken->Next && CurrentToken->Next->is(tok::l_brace) &&
503 OpeningParen.Previous && OpeningParen.Previous->is(tok::l_paren)) {
509 if (
Tok->is(TT_BinaryOperator) &&
Tok->isPointerOrReference())
510 Tok->setType(TT_PointerOrReference);
514 if (StartsObjCSelector) {
515 CurrentToken->setType(TT_ObjCSelector);
516 if (Contexts.back().FirstObjCSelectorName) {
517 Contexts.back().FirstObjCSelectorName->LongestObjCSelectorName =
518 Contexts.back().LongestObjCSelectorName;
522 if (OpeningParen.is(TT_AttributeLParen))
523 CurrentToken->setType(TT_AttributeRParen);
524 if (OpeningParen.is(TT_TypeDeclarationParen))
525 CurrentToken->setType(TT_TypeDeclarationParen);
526 if (OpeningParen.Previous &&
527 OpeningParen.Previous->is(TT_JavaAnnotation)) {
528 CurrentToken->setType(TT_JavaAnnotation);
530 if (OpeningParen.Previous &&
531 OpeningParen.Previous->is(TT_LeadingJavaAnnotation)) {
532 CurrentToken->setType(TT_LeadingJavaAnnotation);
535 if (!HasMultipleLines)
537 else if (HasMultipleParametersOnALine)
545 if (CurrentToken->isOneOf(tok::r_square, tok::r_brace))
548 if (CurrentToken->is(tok::l_brace) && OpeningParen.is(TT_ObjCBlockLParen))
549 OpeningParen.setType(TT_Unknown);
550 if (CurrentToken->is(tok::comma) && CurrentToken->Next &&
551 !CurrentToken->Next->HasUnescapedNewline &&
552 !CurrentToken->Next->isTrailingComment()) {
553 HasMultipleParametersOnALine =
true;
555 bool ProbablyFunctionTypeLParen =
556 (CurrentToken->is(tok::l_paren) && CurrentToken->Next &&
557 CurrentToken->Next->isOneOf(tok::star, tok::amp, tok::caret));
558 if ((Prev.isOneOf(tok::kw_const, tok::kw_auto) ||
559 Prev.isTypeName(LangOpts)) &&
560 !(CurrentToken->is(tok::l_brace) ||
561 (CurrentToken->is(tok::l_paren) && !ProbablyFunctionTypeLParen))) {
562 Contexts.back().IsExpression =
false;
564 if (CurrentToken->isOneOf(tok::semi, tok::colon)) {
565 MightBeObjCForRangeLoop =
false;
566 if (PossibleObjCForInToken) {
567 PossibleObjCForInToken->setType(TT_Unknown);
568 PossibleObjCForInToken =
nullptr;
571 if (IsIf && CurrentToken->is(tok::semi)) {
572 for (
auto *
Tok = OpeningParen.Next;
573 Tok != CurrentToken &&
574 Tok->isNoneOf(tok::equal, tok::l_paren, tok::l_brace);
576 if (
Tok->isPointerOrReference())
577 Tok->setFinalizedType(TT_PointerOrReference);
580 if (MightBeObjCForRangeLoop && CurrentToken->is(Keywords.kw_in)) {
581 PossibleObjCForInToken = CurrentToken;
582 PossibleObjCForInToken->setType(TT_ObjCForIn);
586 if (CurrentToken->is(tok::comma)) {
588 Contexts.back().InStaticAssertFirstArgument =
false;
590 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())
612 updateParameterCount(&OpeningParen,
Tok);
613 if (CurrentToken && CurrentToken->HasUnescapedNewline)
614 HasMultipleLines =
true;
620 if (!Style.isCSharp())
624 if (
Tok.Previous &&
Tok.Previous->is(tok::identifier))
628 if (
Tok.Previous &&
Tok.Previous->is(tok::r_square)) {
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)) {
679 Left->ParentBracket = Contexts.back().ContextKind;
680 FormatToken *Parent = Left->getPreviousNonComment();
685 bool CppArrayTemplates =
686 IsCpp && Parent && Parent->is(TT_TemplateCloser) &&
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->isNoneOf(tok::l_brace, tok::r_square) &&
709 (!Parent || !Parent->is(tok::comma) ||
710 Contexts.back().ContextKind != tok::l_brace) &&
712 Parent->isOneOf(tok::colon, tok::l_square, tok::l_paren,
713 tok::kw_return, tok::kw_throw) ||
714 Parent->isUnaryOperator() ||
716 Parent->isOneOf(TT_ObjCForIn, TT_CastRParen) ||
719 bool ColonFound =
false;
721 unsigned BindingIncrease = 1;
722 if (IsCppStructuredBinding) {
723 Left->setType(TT_StructuredBindingLSquare);
724 }
else if (Left->is(TT_Unknown)) {
725 if (StartsObjCMethodExpr) {
726 Left->setType(TT_ObjCMethodExpr);
727 }
else if (InsideInlineASM) {
728 Left->setType(TT_InlineASMSymbolicNameLSquare);
729 }
else if (IsCpp11AttributeSpecifier) {
730 if (!IsInnerSquare) {
731 Left->setType(TT_AttributeLSquare);
733 Left->Previous->EndsCppAttributeGroup =
false;
735 }
else if (Style.isJavaScript() && Parent &&
736 Contexts.back().ContextKind == tok::l_brace &&
737 Parent->isOneOf(tok::l_brace, tok::comma)) {
738 Left->setType(TT_JsComputedPropertyName);
739 }
else if (IsCpp && Contexts.back().ContextKind == tok::l_brace &&
740 Parent && Parent->isOneOf(tok::l_brace, tok::comma)) {
741 Left->setType(TT_DesignatedInitializerLSquare);
742 }
else if (IsCSharpAttributeSpecifier) {
743 Left->setType(TT_AttributeLSquare);
744 }
else if (CurrentToken->is(tok::r_square) && Parent &&
745 Parent->is(TT_TemplateCloser)) {
746 Left->setType(TT_ArraySubscriptLSquare);
747 }
else if (Style.isProto()) {
774 Left->setType(TT_ArrayInitializerLSquare);
775 if (!Left->endsSequence(tok::l_square, tok::numeric_constant,
777 !Left->endsSequence(tok::l_square, tok::numeric_constant,
779 !Left->endsSequence(tok::l_square, tok::colon, TT_SelectorName)) {
780 Left->setType(TT_ProtoExtensionLSquare);
781 BindingIncrease = 10;
783 }
else if (!CppArrayTemplates && Parent &&
784 Parent->isOneOf(TT_BinaryOperator, TT_TemplateCloser, tok::at,
785 tok::comma, tok::l_paren, tok::l_square,
786 tok::question, tok::colon, tok::kw_return,
789 Left->setType(TT_ArrayInitializerLSquare);
791 BindingIncrease = 10;
792 Left->setType(TT_ArraySubscriptLSquare);
796 ScopedContextCreator ContextCreator(*
this, tok::l_square, BindingIncrease);
797 Contexts.back().IsExpression =
true;
798 if (Style.isJavaScript() && Parent && Parent->is(TT_JsTypeColon))
799 Contexts.back().IsExpression =
false;
801 Contexts.back().ColonIsObjCMethodExpr = StartsObjCMethodExpr;
802 Contexts.back().InCpp11AttributeSpecifier = IsCpp11AttributeSpecifier;
803 Contexts.back().InCSharpAttributeSpecifier = IsCSharpAttributeSpecifier;
805 while (CurrentToken) {
806 if (CurrentToken->is(tok::r_square)) {
807 if (IsCpp11AttributeSpecifier && !IsInnerSquare) {
808 CurrentToken->setType(TT_AttributeRSquare);
809 CurrentToken->EndsCppAttributeGroup =
true;
811 if (IsCSharpAttributeSpecifier) {
812 CurrentToken->setType(TT_AttributeRSquare);
813 }
else if (((CurrentToken->Next &&
814 CurrentToken->Next->is(tok::l_paren)) ||
815 (CurrentToken->Previous &&
816 CurrentToken->Previous->Previous == Left)) &&
817 Left->is(TT_ObjCMethodExpr)) {
822 StartsObjCMethodExpr =
false;
823 Left->setType(TT_Unknown);
825 if (StartsObjCMethodExpr && CurrentToken->Previous != Left) {
826 CurrentToken->setType(TT_ObjCMethodExpr);
829 if (!ColonFound && CurrentToken->Previous &&
830 CurrentToken->Previous->is(TT_Unknown) &&
831 canBeObjCSelectorComponent(*CurrentToken->Previous)) {
832 CurrentToken->Previous->setType(TT_SelectorName);
837 if (Parent && Parent->is(TT_PointerOrReference))
838 Parent->overwriteFixedType(TT_BinaryOperator);
840 Left->MatchingParen = CurrentToken;
841 CurrentToken->MatchingParen = Left;
846 if (!Contexts.back().FirstObjCSelectorName) {
849 Previous->ObjCSelectorNameParts = 1;
850 Contexts.back().FirstObjCSelectorName =
Previous;
853 Left->ParameterCount =
854 Contexts.back().FirstObjCSelectorName->ObjCSelectorNameParts;
856 if (Contexts.back().FirstObjCSelectorName) {
857 Contexts.back().FirstObjCSelectorName->LongestObjCSelectorName =
858 Contexts.back().LongestObjCSelectorName;
859 if (Left->BlockParameterCount > 1)
860 Contexts.back().FirstObjCSelectorName->LongestObjCSelectorName = 0;
862 if (Style.isTableGen() && Left->is(TT_TableGenListOpener))
863 CurrentToken->setType(TT_TableGenListCloser);
867 if (CurrentToken->isOneOf(tok::r_paren, tok::r_brace))
869 if (CurrentToken->is(tok::colon)) {
870 if (IsCpp11AttributeSpecifier &&
871 CurrentToken->endsSequence(tok::colon, tok::identifier,
875 CurrentToken->setType(TT_AttributeColon);
876 }
else if (!Style.isVerilog() && !
Line.InPragmaDirective &&
877 Left->isOneOf(TT_ArraySubscriptLSquare,
878 TT_DesignatedInitializerLSquare)) {
879 Left->setType(TT_ObjCMethodExpr);
880 StartsObjCMethodExpr =
true;
881 Contexts.back().ColonIsObjCMethodExpr =
true;
882 if (Parent && Parent->is(tok::r_paren)) {
884 Parent->setType(TT_CastRParen);
889 if (CurrentToken->is(tok::comma) && Left->is(TT_ObjCMethodExpr) &&
891 Left->setType(TT_ArrayInitializerLSquare);
894 if (Style.isTableGen()) {
895 if (CurrentToken->isOneOf(tok::comma, tok::minus, tok::ellipsis)) {
901 if (!parseTableGenValue())
904 updateParameterCount(Left,
Tok);
909 updateParameterCount(Left,
Tok);
914 void skipToNextNonComment() {
916 while (CurrentToken && CurrentToken->is(tok::comment))
925 bool parseTableGenValue(
bool ParseNameMode =
false) {
928 while (CurrentToken->is(tok::comment))
930 if (!parseTableGenSimpleValue())
935 if (CurrentToken->is(tok::hash)) {
936 if (CurrentToken->Next &&
937 CurrentToken->Next->isOneOf(tok::colon, tok::semi, tok::l_brace)) {
940 CurrentToken->setType(TT_TableGenTrailingPasteOperator);
945 skipToNextNonComment();
946 HashTok->setType(TT_Unknown);
947 if (!parseTableGenValue(ParseNameMode))
954 if (ParseNameMode && CurrentToken->is(tok::l_brace))
957 if (CurrentToken->isOneOf(tok::l_brace, tok::l_square, tok::period)) {
958 CurrentToken->setType(TT_TableGenValueSuffix);
960 skipToNextNonComment();
961 if (Suffix->is(tok::l_square))
962 return parseSquare();
963 if (Suffix->is(tok::l_brace)) {
964 Scopes.push_back(getScopeType(*Suffix));
974 bool tryToParseTableGenTokVar() {
977 if (CurrentToken->is(tok::identifier) &&
978 CurrentToken->TokenText.front() ==
'$') {
979 skipToNextNonComment();
987 bool parseTableGenDAGArg(
bool AlignColon =
false) {
988 if (tryToParseTableGenTokVar())
990 if (parseTableGenValue()) {
991 if (CurrentToken && CurrentToken->is(tok::colon)) {
993 CurrentToken->setType(TT_TableGenDAGArgListColonToAlign);
995 CurrentToken->setType(TT_TableGenDAGArgListColon);
996 skipToNextNonComment();
997 return tryToParseTableGenTokVar();
1008 auto &Opes = Style.TableGenBreakingDAGArgOperators;
1013 if (
Tok.isNot(tok::identifier) ||
1014 Tok.isOneOf(TT_TableGenBangOperator, TT_TableGenCondOperator)) {
1018 if (!
Tok.Next ||
Tok.Next->is(tok::colon))
1020 return llvm::is_contained(Opes,
Tok.TokenText.str());
1025 bool parseTableGenDAGArgAndList(
FormatToken *Opener) {
1027 if (!parseTableGenDAGArg())
1029 bool BreakInside =
false;
1033 if (isTableGenDAGArgBreakingOperator(*FirstTok)) {
1036 Opener->setType(TT_TableGenDAGArgOpenerToBreak);
1037 if (FirstTok->isOneOf(TT_TableGenBangOperator,
1038 TT_TableGenCondOperator)) {
1041 CurrentToken->Previous->setType(TT_TableGenDAGArgOperatorToBreak);
1042 }
else if (FirstTok->is(tok::identifier)) {
1044 FirstTok->setType(TT_TableGenDAGArgOperatorToBreak);
1046 FirstTok->setType(TT_TableGenDAGArgOperatorID);
1051 return parseTableGenDAGArgList(Opener, BreakInside);
1056 bool parseTableGenDAGArgList(
FormatToken *Opener,
bool BreakInside) {
1057 ScopedContextCreator ContextCreator(*
this, tok::l_paren, 0);
1058 Contexts.back().IsTableGenDAGArgList =
true;
1059 bool FirstDAGArgListElm =
true;
1060 while (CurrentToken) {
1061 if (!FirstDAGArgListElm && CurrentToken->is(tok::comma)) {
1062 CurrentToken->setType(BreakInside ? TT_TableGenDAGArgListCommaToBreak
1063 : TT_TableGenDAGArgListComma);
1064 skipToNextNonComment();
1066 if (CurrentToken && CurrentToken->is(tok::r_paren)) {
1067 CurrentToken->setType(TT_TableGenDAGArgCloser);
1068 Opener->MatchingParen = CurrentToken;
1069 CurrentToken->MatchingParen = Opener;
1070 skipToNextNonComment();
1073 if (!parseTableGenDAGArg(
1075 Style.AlignConsecutiveTableGenBreakingDAGArgColons.Enabled)) {
1078 FirstDAGArgListElm =
false;
1083 bool parseTableGenSimpleValue() {
1084 assert(Style.isTableGen());
1088 skipToNextNonComment();
1090 if (
Tok->isOneOf(tok::numeric_constant, tok::string_literal,
1091 TT_TableGenMultiLineString, tok::kw_true, tok::kw_false,
1092 tok::question, tok::kw_int)) {
1096 if (
Tok->is(tok::l_brace)) {
1097 Scopes.push_back(getScopeType(*
Tok));
1098 return parseBrace();
1101 if (
Tok->is(tok::l_square)) {
1102 Tok->setType(TT_TableGenListOpener);
1105 if (
Tok->is(tok::less)) {
1106 CurrentToken->setType(TT_TemplateOpener);
1107 return parseAngle();
1113 if (
Tok->is(tok::l_paren)) {
1114 Tok->setType(TT_TableGenDAGArgOpener);
1116 if (Contexts.back().IsTableGenDAGArgList)
1117 Tok->SpacesRequiredBefore = 1;
1118 return parseTableGenDAGArgAndList(
Tok);
1121 if (
Tok->is(TT_TableGenBangOperator)) {
1122 if (CurrentToken && CurrentToken->is(tok::less)) {
1123 CurrentToken->setType(TT_TemplateOpener);
1124 skipToNextNonComment();
1128 if (!CurrentToken || CurrentToken->isNot(tok::l_paren))
1132 Contexts.back().IsTableGenBangOpe =
true;
1133 bool Result = parseParens();
1134 Contexts.back().IsTableGenBangOpe =
false;
1138 if (
Tok->is(TT_TableGenCondOperator)) {
1139 if (!CurrentToken || CurrentToken->isNot(tok::l_paren))
1142 return parseParens();
1147 if (
Tok->is(tok::identifier)) {
1149 if (CurrentToken && CurrentToken->is(tok::less)) {
1150 CurrentToken->setType(TT_TemplateOpener);
1151 skipToNextNonComment();
1152 return parseAngle();
1160 bool couldBeInStructArrayInitializer()
const {
1161 if (Contexts.size() < 2)
1165 const auto End = std::next(Contexts.rbegin(), 2);
1166 auto Last = Contexts.rbegin();
1169 if (
Last->ContextKind == tok::l_brace)
1171 return Depth == 2 &&
Last->ContextKind != tok::l_brace;
1178 assert(CurrentToken->Previous);
1179 FormatToken &OpeningBrace = *CurrentToken->Previous;
1180 assert(OpeningBrace.is(tok::l_brace));
1181 OpeningBrace.ParentBracket = Contexts.back().ContextKind;
1183 if (Contexts.back().CaretFound)
1184 OpeningBrace.overwriteFixedType(TT_ObjCBlockLBrace);
1185 Contexts.back().CaretFound =
false;
1187 ScopedContextCreator ContextCreator(*
this, tok::l_brace, 1);
1188 Contexts.back().ColonIsDictLiteral =
true;
1190 Contexts.back().IsExpression =
true;
1191 if (Style.isJavaScript() && OpeningBrace.Previous &&
1192 OpeningBrace.Previous->is(TT_JsTypeColon)) {
1193 Contexts.back().IsExpression =
false;
1195 if (Style.isVerilog() &&
1196 (!OpeningBrace.getPreviousNonComment() ||
1197 OpeningBrace.getPreviousNonComment()->isNot(Keywords.kw_apostrophe))) {
1198 Contexts.back().VerilogMayBeConcatenation =
true;
1200 if (Style.isTableGen())
1201 Contexts.back().ColonIsDictLiteral =
false;
1203 unsigned CommaCount = 0;
1204 while (CurrentToken) {
1205 assert(!Scopes.empty());
1206 if (CurrentToken->is(tok::r_brace)) {
1207 assert(Scopes.back() == getScopeType(OpeningBrace));
1209 assert(OpeningBrace.Optional == CurrentToken->Optional);
1210 OpeningBrace.MatchingParen = CurrentToken;
1211 CurrentToken->MatchingParen = &OpeningBrace;
1213 if (OpeningBrace.ParentBracket == tok::l_brace &&
1214 couldBeInStructArrayInitializer() && CommaCount > 0) {
1215 Contexts.back().ContextType = Context::StructArrayInitializer;
1221 if (CurrentToken->isOneOf(tok::r_paren, tok::r_square))
1223 updateParameterCount(&OpeningBrace, CurrentToken);
1224 if (CurrentToken->isOneOf(tok::colon, tok::l_brace, tok::less)) {
1226 if (
Previous->is(TT_JsTypeOptionalQuestion))
1228 if ((CurrentToken->is(tok::colon) && !Style.isTableGen() &&
1229 (!Contexts.back().ColonIsDictLiteral || !IsCpp)) ||
1231 OpeningBrace.setType(TT_DictLiteral);
1232 Scopes.back() = getScopeType(OpeningBrace);
1233 if (
Previous->Tok.getIdentifierInfo() ||
1234 Previous->is(tok::string_literal)) {
1235 Previous->setType(TT_SelectorName);
1238 if (CurrentToken->is(tok::colon) && OpeningBrace.is(TT_Unknown) &&
1239 !Style.isTableGen()) {
1240 OpeningBrace.setType(TT_DictLiteral);
1241 Scopes.back() = getScopeType(OpeningBrace);
1242 }
else if (Style.isJavaScript()) {
1243 OpeningBrace.overwriteFixedType(TT_DictLiteral);
1244 Scopes.back() = getScopeType(OpeningBrace);
1247 bool IsBracedListComma =
false;
1248 if (CurrentToken->is(tok::comma)) {
1249 if (Style.isJavaScript()) {
1250 OpeningBrace.overwriteFixedType(TT_DictLiteral);
1251 Scopes.back() = getScopeType(OpeningBrace);
1257 if (!consumeToken())
1259 if (IsBracedListComma)
1260 Contexts.back().IsExpression =
true;
1269 if (Current->is(tok::l_brace) && Current->is(
BK_Block))
1270 ++Left->BlockParameterCount;
1271 if (Current->is(tok::comma)) {
1272 ++Left->ParameterCount;
1274 Left->Role.reset(
new CommaSeparatedList(Style));
1275 Left->Role->CommaFound(Current);
1276 }
else if (Left->ParameterCount == 0 && Current->isNot(tok::comment)) {
1277 Left->ParameterCount = 1;
1281 bool parseConditional() {
1282 while (CurrentToken) {
1283 if (CurrentToken->is(tok::colon) && CurrentToken->is(TT_Unknown)) {
1284 CurrentToken->setType(TT_ConditionalExpr);
1291 if (CurrentToken->is(tok::r_brace))
1293 if (!consumeToken())
1299 bool parseTemplateDeclaration() {
1300 if (!CurrentToken || CurrentToken->isNot(tok::less))
1303 CurrentToken->setType(TT_TemplateOpener);
1306 TemplateDeclarationDepth++;
1307 const bool WellFormed = parseAngle();
1308 TemplateDeclarationDepth--;
1312 if (CurrentToken && TemplateDeclarationDepth == 0)
1313 CurrentToken->Previous->ClosesTemplateDeclaration =
true;
1318 bool consumeToken() {
1320 const auto *Prev = CurrentToken->getPreviousNonComment();
1321 if (Prev && Prev->is(TT_AttributeRSquare) &&
1322 CurrentToken->isOneOf(tok::kw_if, tok::kw_switch, tok::kw_case,
1323 tok::kw_default, tok::kw_for, tok::kw_while) &&
1324 CurrentToken->mustBreakAfterAttributes(Style)) {
1325 CurrentToken->MustBreakBefore =
true;
1332 if (
Tok->is(TT_VerilogTableItem))
1335 if (
Tok->is(TT_TableGenMultiLineString))
1337 auto *Prev =
Tok->getPreviousNonComment();
1338 auto *
Next =
Tok->getNextNonComment();
1339 switch (
bool IsIf =
false;
Tok->Tok.getKind()) {
1342 if (!Prev &&
Line.MustBeDeclaration)
1343 Tok->setType(TT_ObjCMethodSpecifier);
1350 if (
Tok->isTypeFinalized())
1353 if (Style.isJavaScript()) {
1354 if (Contexts.back().ColonIsForRangeExpr ||
1355 (Contexts.size() == 1 &&
1356 Line.First->isNoneOf(tok::kw_enum, tok::kw_case)) ||
1357 Contexts.back().ContextKind == tok::l_paren ||
1358 Contexts.back().ContextKind == tok::l_square ||
1359 (!Contexts.back().IsExpression &&
1360 Contexts.back().ContextKind == tok::l_brace) ||
1361 (Contexts.size() == 1 &&
1362 Line.MustBeDeclaration)) {
1363 Contexts.back().IsExpression =
false;
1364 Tok->setType(TT_JsTypeColon);
1367 }
else if (Style.isCSharp()) {
1368 if (Contexts.back().InCSharpAttributeSpecifier) {
1369 Tok->setType(TT_AttributeColon);
1372 if (Contexts.back().ContextKind == tok::l_paren) {
1373 Tok->setType(TT_CSharpNamedArgumentColon);
1376 }
else if (Style.isVerilog() &&
Tok->isNot(TT_BinaryOperator)) {
1379 if (Keywords.isVerilogEnd(*Prev) || Keywords.isVerilogBegin(*Prev)) {
1380 Tok->setType(TT_VerilogBlockLabelColon);
1381 }
else if (Contexts.back().ContextKind == tok::l_square) {
1382 Tok->setType(TT_BitFieldColon);
1383 }
else if (Contexts.back().ColonIsDictLiteral) {
1384 Tok->setType(TT_DictLiteral);
1385 }
else if (Contexts.size() == 1) {
1389 Tok->setType(TT_CaseLabelColon);
1390 if (
Line.Level > 1 || (!
Line.InPPDirective &&
Line.Level > 0))
1395 if (
Line.First->is(tok::kw_asm)) {
1396 Tok->setType(TT_InlineASMColon);
1397 }
else if (Contexts.back().ColonIsDictLiteral || Style.isProto()) {
1398 Tok->setType(TT_DictLiteral);
1399 if (Style.isTextProto())
1400 Prev->setType(TT_SelectorName);
1401 }
else if (Contexts.back().ColonIsObjCMethodExpr ||
1402 Line.startsWith(TT_ObjCMethodSpecifier)) {
1403 Tok->setType(TT_ObjCMethodExpr);
1404 const auto *PrevPrev = Prev->Previous;
1407 bool UnknownIdentifierInMethodDeclaration =
1408 Line.startsWith(TT_ObjCMethodSpecifier) &&
1409 Prev->is(tok::identifier) && Prev->is(TT_Unknown);
1412 !(PrevPrev->is(TT_CastRParen) ||
1413 (PrevPrev->is(TT_ObjCMethodExpr) && PrevPrev->is(tok::colon))) ||
1414 PrevPrev->is(tok::r_square) ||
1415 Contexts.back().LongestObjCSelectorName == 0 ||
1416 UnknownIdentifierInMethodDeclaration) {
1417 Prev->setType(TT_SelectorName);
1418 if (!Contexts.back().FirstObjCSelectorName)
1419 Contexts.back().FirstObjCSelectorName = Prev;
1420 else if (Prev->ColumnWidth > Contexts.back().LongestObjCSelectorName)
1421 Contexts.back().LongestObjCSelectorName = Prev->ColumnWidth;
1422 Prev->ParameterIndex =
1423 Contexts.back().FirstObjCSelectorName->ObjCSelectorNameParts;
1424 ++Contexts.back().FirstObjCSelectorName->ObjCSelectorNameParts;
1426 }
else if (Contexts.back().ColonIsForRangeExpr) {
1427 Tok->setType(TT_RangeBasedForLoopColon);
1428 for (
auto *Token = Prev;
1429 Token && Token->isNoneOf(tok::semi, tok::l_paren);
1430 Token = Token->Previous) {
1431 if (Token->isPointerOrReference())
1432 Token->setFinalizedType(TT_PointerOrReference);
1434 }
else if (Contexts.back().ContextType == Context::C11GenericSelection) {
1435 Tok->setType(TT_GenericSelectionColon);
1436 if (Prev->isPointerOrReference())
1437 Prev->setFinalizedType(TT_PointerOrReference);
1438 }
else if ((CurrentToken && CurrentToken->is(tok::numeric_constant)) ||
1439 (Prev->is(TT_StartOfName) && !Scopes.empty() &&
1441 Tok->setType(TT_BitFieldColon);
1442 }
else if (Contexts.size() == 1 &&
1443 Line.getFirstNonComment()->isNoneOf(tok::kw_enum, tok::kw_case,
1445 !
Line.startsWith(tok::kw_typedef, tok::kw_enum)) {
1446 if (Prev->isOneOf(tok::r_paren, tok::kw_noexcept) ||
1447 Prev->ClosesRequiresClause) {
1448 Tok->setType(TT_CtorInitializerColon);
1449 }
else if (Prev->is(tok::kw_try)) {
1451 FormatToken *PrevPrev = Prev->getPreviousNonComment();
1454 if (PrevPrev && PrevPrev->isOneOf(tok::r_paren, tok::kw_noexcept))
1455 Tok->setType(TT_CtorInitializerColon);
1457 Tok->setType(TT_InheritanceColon);
1458 if (Prev->isAccessSpecifierKeyword())
1461 }
else if (canBeObjCSelectorComponent(*Prev) &&
Next &&
1462 (
Next->isOneOf(tok::r_paren, tok::comma) ||
1463 (canBeObjCSelectorComponent(*
Next) &&
Next->Next &&
1464 Next->Next->is(tok::colon)))) {
1467 Tok->setType(TT_ObjCSelector);
1474 if (Style.isJavaScript() && !Contexts.back().IsExpression)
1475 Tok->setType(TT_JsTypeOperator);
1478 if (Style.isTableGen()) {
1480 if (!parseTableGenValue())
1482 if (CurrentToken && CurrentToken->is(Keywords.kw_then))
1487 CurrentToken->isOneOf(tok::kw_constexpr, tok::identifier)) {
1493 if (CurrentToken && CurrentToken->is(tok::l_paren)) {
1495 if (!parseParens(IsIf))
1500 if (Style.isJavaScript()) {
1502 if ((Prev && Prev->is(tok::period)) || (
Next &&
Next->is(tok::colon)))
1505 if (CurrentToken && CurrentToken->is(Keywords.kw_await))
1508 if (IsCpp && CurrentToken && CurrentToken->is(tok::kw_co_await))
1510 Contexts.back().ColonIsForRangeExpr =
true;
1511 if (!CurrentToken || CurrentToken->isNot(tok::l_paren))
1522 if (Prev && Prev->is(tok::r_paren) && Prev->MatchingParen &&
1523 Prev->MatchingParen->is(TT_OverloadedOperatorLParen)) {
1524 Prev->setType(TT_OverloadedOperator);
1525 Prev->MatchingParen->setType(TT_OverloadedOperator);
1526 Tok->setType(TT_OverloadedOperatorLParen);
1529 if (Style.isVerilog()) {
1535 auto IsInstancePort = [&]() {
1544 if (!Prev || !(PrevPrev = Prev->getPreviousNonComment()))
1547 if (Keywords.isVerilogIdentifier(*Prev) &&
1548 Keywords.isVerilogIdentifier(*PrevPrev)) {
1552 if (Prev->is(Keywords.kw_verilogHash) &&
1553 Keywords.isVerilogIdentifier(*PrevPrev)) {
1557 if (Keywords.isVerilogIdentifier(*Prev) && PrevPrev->is(tok::r_paren))
1560 if (Keywords.isVerilogIdentifier(*Prev) && PrevPrev->is(tok::comma)) {
1561 const FormatToken *PrevParen = PrevPrev->getPreviousNonComment();
1562 if (PrevParen && PrevParen->is(tok::r_paren) &&
1563 PrevParen->MatchingParen &&
1564 PrevParen->MatchingParen->is(TT_VerilogInstancePortLParen)) {
1571 if (IsInstancePort())
1572 Tok->setType(TT_VerilogInstancePortLParen);
1577 if (
Line.MustBeDeclaration && Contexts.size() == 1 &&
1578 !Contexts.back().IsExpression && !
Line.startsWith(TT_ObjCProperty) &&
1579 !
Line.startsWith(tok::l_paren) &&
1580 Tok->isNoneOf(TT_TypeDeclarationParen, TT_RequiresExpressionLParen)) {
1582 (!Prev->isAttribute() &&
1583 Prev->isNoneOf(TT_RequiresClause, TT_LeadingJavaAnnotation,
1584 TT_BinaryOperator))) {
1585 Line.MightBeFunctionDecl =
true;
1586 Tok->MightBeFunctionDeclParen =
true;
1591 if (Style.isTableGen())
1592 Tok->setType(TT_TableGenListOpener);
1598 if (
Tok->is(TT_RequiresExpressionLBrace))
1600 }
else if (Style.isTextProto()) {
1601 if (Prev && Prev->isNot(TT_DictLiteral))
1602 Prev->setType(TT_SelectorName);
1604 Scopes.push_back(getScopeType(*
Tok));
1610 Tok->setType(TT_TemplateOpener);
1616 if (Style.isTextProto() ||
1618 Prev->isOneOf(TT_SelectorName, TT_DictLiteral))) {
1619 Tok->setType(TT_DictLiteral);
1620 if (Prev && Prev->isNot(TT_DictLiteral))
1621 Prev->setType(TT_SelectorName);
1623 if (Style.isTableGen())
1624 Tok->setType(TT_TemplateOpener);
1626 Tok->setType(TT_BinaryOperator);
1627 NonTemplateLess.insert(
Tok);
1637 if (!Scopes.empty())
1644 if (!Style.isTextProto() &&
Tok->is(TT_Unknown))
1645 Tok->setType(TT_BinaryOperator);
1646 if (Prev && Prev->is(TT_TemplateCloser))
1647 Tok->SpacesRequiredBefore = 1;
1649 case tok::kw_operator:
1650 if (Style.isProto())
1653 if (IsCpp && CurrentToken) {
1654 const auto *Info = CurrentToken->Tok.getIdentifierInfo();
1656 if (Info && !(CurrentToken->isPlacementOperator() ||
1657 CurrentToken->is(tok::kw_co_await) ||
1658 Info->isCPlusPlusOperatorKeyword())) {
1660 if (CurrentToken->startsSequence(tok::kw_decltype, tok::l_paren,
1661 tok::kw_auto, tok::r_paren)) {
1663 LParen = CurrentToken->Next->Next->Next->Next;
1666 for (LParen = CurrentToken->Next;
1667 LParen && LParen->isNot(tok::l_paren); LParen = LParen->Next) {
1668 if (LParen->isPointerOrReference())
1669 LParen->setFinalizedType(TT_PointerOrReference);
1672 if (LParen && LParen->is(tok::l_paren)) {
1673 if (!Contexts.back().IsExpression) {
1674 Tok->setFinalizedType(TT_FunctionDeclarationName);
1675 LParen->setFinalizedType(TT_FunctionDeclarationLParen);
1681 while (CurrentToken &&
1682 CurrentToken->isNoneOf(tok::l_paren, tok::semi, tok::r_paren,
1684 if (CurrentToken->isOneOf(tok::star, tok::amp))
1685 CurrentToken->setType(TT_PointerOrReference);
1686 auto Next = CurrentToken->getNextNonComment();
1689 if (
Next->is(tok::less))
1695 auto Previous = CurrentToken->getPreviousNonComment();
1697 if (CurrentToken->is(tok::comma) &&
Previous->isNot(tok::kw_operator))
1699 if (
Previous->isOneOf(TT_BinaryOperator, TT_UnaryOperator, tok::comma,
1702 Previous->isPointerOrReference()) ||
1704 Previous->TokenText.starts_with(
"\"\"")) {
1705 Previous->setType(TT_OverloadedOperator);
1706 if (CurrentToken->isOneOf(tok::less, tok::greater))
1710 if (CurrentToken && CurrentToken->is(tok::l_paren))
1711 CurrentToken->setType(TT_OverloadedOperatorLParen);
1712 if (CurrentToken && CurrentToken->Previous->is(TT_BinaryOperator))
1713 CurrentToken->Previous->setType(TT_OverloadedOperator);
1716 if (Style.isJavaScript() &&
Next &&
1717 Next->isOneOf(tok::semi, tok::comma, tok::colon, tok::r_paren,
1718 tok::r_brace, tok::r_square)) {
1723 Tok->setType(TT_JsTypeOptionalQuestion);
1728 if (
Line.MustBeDeclaration && !Contexts.back().IsExpression &&
1729 Style.isJavaScript()) {
1732 if (Style.isCSharp()) {
1735 if (
Next && (
Next->isOneOf(tok::r_paren, tok::greater) ||
1736 Next->startsSequence(tok::identifier, tok::semi) ||
1737 Next->startsSequence(tok::identifier, tok::equal))) {
1738 Tok->setType(TT_CSharpNullable);
1747 if (!Contexts.back().IsExpression &&
Line.MustBeDeclaration &&
1748 (!
Next ||
Next->isNoneOf(tok::identifier, tok::string_literal) ||
1749 !
Next->Next ||
Next->Next->isNoneOf(tok::colon, tok::question))) {
1750 Tok->setType(TT_CSharpNullable);
1756 case tok::kw_template:
1757 parseTemplateDeclaration();
1760 switch (Contexts.back().ContextType) {
1761 case Context::CtorInitializer:
1762 Tok->setType(TT_CtorInitializerComma);
1764 case Context::InheritanceList:
1765 Tok->setType(TT_InheritanceComma);
1767 case Context::VerilogInstancePortList:
1768 Tok->setType(TT_VerilogInstancePortComma);
1771 if (Style.isVerilog() && Contexts.size() == 1 &&
1772 Line.startsWith(Keywords.kw_assign)) {
1773 Tok->setFinalizedType(TT_VerilogAssignComma);
1774 }
else if (Contexts.back().FirstStartOfName &&
1775 (Contexts.size() == 1 || startsWithInitStatement(
Line))) {
1776 Contexts.back().FirstStartOfName->PartOfMultiVariableDeclStmt =
true;
1777 Line.IsMultiVariableDeclStmt =
true;
1781 if (Contexts.back().ContextType == Context::ForEachMacro)
1782 Contexts.back().IsExpression =
true;
1784 case tok::kw_default:
1786 if (Style.isVerilog() && Keywords.isVerilogEndOfLabel(*
Tok) &&
1787 (
Line.Level > 1 || (!
Line.InPPDirective &&
Line.Level > 0))) {
1791 case tok::identifier:
1792 if (
Tok->isOneOf(Keywords.kw___has_include,
1793 Keywords.kw___has_include_next)) {
1797 if (
Next &&
Next->is(tok::l_paren) && Prev &&
1798 Prev->isOneOf(tok::kw___cdecl, tok::kw___stdcall,
1799 tok::kw___fastcall, tok::kw___thiscall,
1800 tok::kw___regcall, tok::kw___vectorcall)) {
1801 Tok->setFinalizedType(TT_FunctionDeclarationName);
1802 Next->setFinalizedType(TT_FunctionDeclarationLParen);
1804 }
else if (Style.isCSharp()) {
1805 if (
Tok->is(Keywords.kw_where) &&
Next &&
Next->isNot(tok::l_paren)) {
1806 Tok->setType(TT_CSharpGenericTypeConstraint);
1807 parseCSharpGenericTypeConstraint();
1809 Line.IsContinuation =
true;
1811 }
else if (Style.isTableGen()) {
1812 if (
Tok->is(Keywords.kw_assert)) {
1813 if (!parseTableGenValue())
1815 }
else if (
Tok->isOneOf(Keywords.kw_def, Keywords.kw_defm) &&
1816 (!
Next ||
Next->isNoneOf(tok::colon, tok::l_brace))) {
1818 if (!parseTableGenValue(
true))
1822 if (Style.AllowBreakBeforeQtProperty &&
1823 Contexts.back().ContextType == Context::QtProperty &&
1824 Tok->isQtProperty()) {
1825 Tok->setFinalizedType(TT_QtProperty);
1829 if (
Tok->isNot(TT_LambdaArrow) && Prev && Prev->is(tok::kw_noexcept))
1830 Tok->setType(TT_TrailingReturnArrow);
1834 if (Style.isTableGen() && !parseTableGenValue())
1836 if (!Scopes.empty() && Scopes.back() ==
ST_Enum)
1837 Tok->setFinalizedType(TT_EnumEqual);
1845 void parseCSharpGenericTypeConstraint() {
1846 int OpenAngleBracketsCount = 0;
1847 while (CurrentToken) {
1848 if (CurrentToken->is(tok::less)) {
1850 CurrentToken->setType(TT_TemplateOpener);
1851 ++OpenAngleBracketsCount;
1853 }
else if (CurrentToken->is(tok::greater)) {
1854 CurrentToken->setType(TT_TemplateCloser);
1855 --OpenAngleBracketsCount;
1857 }
else if (CurrentToken->is(tok::comma) && OpenAngleBracketsCount == 0) {
1860 CurrentToken->setType(TT_CSharpGenericTypeConstraintComma);
1862 }
else if (CurrentToken->is(Keywords.kw_where)) {
1863 CurrentToken->setType(TT_CSharpGenericTypeConstraint);
1865 }
else if (CurrentToken->is(tok::colon)) {
1866 CurrentToken->setType(TT_CSharpGenericTypeConstraintColon);
1874 void parseIncludeDirective() {
1875 if (CurrentToken && CurrentToken->is(tok::less)) {
1877 while (CurrentToken) {
1880 if (CurrentToken->isNot(tok::comment) &&
1881 !CurrentToken->TokenText.starts_with(
"//")) {
1882 CurrentToken->setType(TT_ImplicitStringLiteral);
1889 void parseWarningOrError() {
1894 while (CurrentToken) {
1895 CurrentToken->setType(TT_ImplicitStringLiteral);
1900 void parsePragma() {
1903 CurrentToken->isOneOf(Keywords.kw_mark, Keywords.kw_option,
1904 Keywords.kw_region)) {
1905 bool IsMarkOrRegion =
1906 CurrentToken->isOneOf(Keywords.kw_mark, Keywords.kw_region);
1909 while (CurrentToken) {
1910 if (IsMarkOrRegion || CurrentToken->Previous->is(TT_BinaryOperator))
1911 CurrentToken->setType(TT_ImplicitStringLiteral);
1917 void parseHasInclude() {
1918 if (!CurrentToken || CurrentToken->isNot(tok::l_paren))
1921 parseIncludeDirective();
1925 LineType parsePreprocessorDirective() {
1926 bool IsFirstToken = CurrentToken->IsFirst;
1932 if (Style.isJavaScript() && IsFirstToken) {
1936 while (CurrentToken) {
1938 CurrentToken->setType(TT_ImplicitStringLiteral);
1944 if (CurrentToken->is(tok::numeric_constant)) {
1945 CurrentToken->SpacesRequiredBefore = 1;
1950 if (!CurrentToken->Tok.getIdentifierInfo())
1954 if (Style.isVerilog() && !Keywords.isVerilogPPDirective(*CurrentToken))
1956 switch (CurrentToken->Tok.getIdentifierInfo()->getPPKeywordID()) {
1957 case tok::pp_include:
1958 case tok::pp_include_next:
1959 case tok::pp_import:
1961 parseIncludeDirective();
1965 case tok::pp_warning:
1966 parseWarningOrError();
1968 case tok::pp_pragma:
1973 Contexts.back().IsExpression =
true;
1976 CurrentToken->SpacesRequiredBefore = 1;
1982 while (CurrentToken) {
1985 if (
Tok->is(tok::l_paren)) {
1987 }
else if (
Tok->isOneOf(Keywords.kw___has_include,
1988 Keywords.kw___has_include_next)) {
1996 LineType parseLine() {
1999 NonTemplateLess.clear();
2000 if (!
Line.InMacroBody && CurrentToken->is(tok::hash)) {
2004 auto Type = parsePreprocessorDirective();
2012 IdentifierInfo *Info = CurrentToken->Tok.getIdentifierInfo();
2013 if ((Style.isJava() && CurrentToken->is(Keywords.kw_package)) ||
2014 (!Style.isVerilog() && Info &&
2015 Info->getPPKeywordID() == tok::pp_import && CurrentToken->Next &&
2016 CurrentToken->Next->isOneOf(tok::string_literal, tok::identifier,
2019 parseIncludeDirective();
2025 if (CurrentToken->is(tok::less) &&
Line.Last->is(tok::greater)) {
2026 parseIncludeDirective();
2033 CurrentToken->isOneOf(Keywords.kw_option, Keywords.kw_package)) {
2035 if (CurrentToken && CurrentToken->is(tok::identifier)) {
2036 while (CurrentToken)
2042 bool KeywordVirtualFound =
false;
2043 bool ImportStatement =
false;
2046 if (Style.isJavaScript() && CurrentToken->is(Keywords.kw_import))
2047 ImportStatement =
true;
2049 while (CurrentToken) {
2050 if (CurrentToken->is(tok::kw_virtual))
2051 KeywordVirtualFound =
true;
2052 if (Style.isJavaScript()) {
2059 if (
Line.First->is(tok::kw_export) &&
2060 CurrentToken->is(Keywords.kw_from) && CurrentToken->Next &&
2061 CurrentToken->Next->isStringLiteral()) {
2062 ImportStatement =
true;
2064 if (isClosureImportStatement(*CurrentToken))
2065 ImportStatement =
true;
2067 if (!consumeToken())
2075 if (KeywordVirtualFound)
2077 if (ImportStatement)
2080 if (
Line.startsWith(TT_ObjCMethodSpecifier)) {
2081 if (Contexts.back().FirstObjCSelectorName) {
2082 Contexts.back().FirstObjCSelectorName->LongestObjCSelectorName =
2083 Contexts.back().LongestObjCSelectorName;
2088 for (
const auto &ctx : Contexts)
2089 if (ctx.ContextType == Context::StructArrayInitializer)
2099 return Tok.TokenText ==
"goog" &&
Tok.Next &&
Tok.Next->is(tok::period) &&
2101 (
Tok.Next->Next->TokenText ==
"module" ||
2102 Tok.Next->Next->TokenText ==
"provide" ||
2103 Tok.Next->Next->TokenText ==
"require" ||
2104 Tok.Next->Next->TokenText ==
"requireType" ||
2105 Tok.Next->Next->TokenText ==
"forwardDeclare") &&
2106 Tok.Next->Next->Next &&
Tok.Next->Next->Next->is(tok::l_paren);
2109 void resetTokenMetadata() {
2115 if (!CurrentToken->isTypeFinalized() &&
2116 CurrentToken->isNoneOf(
2117 TT_LambdaLSquare, TT_LambdaLBrace, TT_AttributeMacro, TT_IfMacro,
2118 TT_ForEachMacro, TT_TypenameMacro, TT_FunctionLBrace,
2119 TT_ImplicitStringLiteral, TT_InlineASMBrace, TT_FatArrow,
2120 TT_LambdaArrow, TT_NamespaceMacro, TT_OverloadedOperator,
2121 TT_RegexLiteral, TT_TemplateString, TT_ObjCStringLiteral,
2122 TT_UntouchableMacroFunc, TT_StatementAttributeLikeMacro,
2123 TT_FunctionLikeOrFreestandingMacro, TT_ClassLBrace, TT_EnumLBrace,
2124 TT_RecordLBrace, TT_StructLBrace, TT_UnionLBrace, TT_RequiresClause,
2125 TT_RequiresClauseInARequiresExpression, TT_RequiresExpression,
2126 TT_RequiresExpressionLParen, TT_RequiresExpressionLBrace,
2127 TT_CompoundRequirementLBrace, TT_BracedListLBrace,
2128 TT_FunctionLikeMacro)) {
2129 CurrentToken->setType(TT_Unknown);
2131 CurrentToken->Role.reset();
2132 CurrentToken->MatchingParen =
nullptr;
2133 CurrentToken->FakeLParens.clear();
2134 CurrentToken->FakeRParens = 0;
2141 CurrentToken->NestingLevel = Contexts.size() - 1;
2142 CurrentToken->BindingStrength = Contexts.back().BindingStrength;
2143 modifyContext(*CurrentToken);
2144 determineTokenType(*CurrentToken);
2145 CurrentToken = CurrentToken->Next;
2147 resetTokenMetadata();
2156 IsExpression(IsExpression) {}
2162 bool ColonIsForRangeExpr =
false;
2163 bool ColonIsDictLiteral =
false;
2164 bool ColonIsObjCMethodExpr =
false;
2167 bool CanBeExpression =
true;
2168 bool CaretFound =
false;
2169 bool InCpp11AttributeSpecifier =
false;
2170 bool InCSharpAttributeSpecifier =
false;
2171 bool InStaticAssertFirstArgument =
false;
2172 bool VerilogAssignmentFound =
false;
2175 bool VerilogMayBeConcatenation =
false;
2176 bool IsTableGenDAGArgList =
false;
2177 bool IsTableGenBangOpe =
false;
2178 bool IsTableGenCondOpe =
false;
2191 StructArrayInitializer,
2195 C11GenericSelection,
2198 VerilogInstancePortList,
2204 struct ScopedContextCreator {
2205 AnnotatingParser &P;
2207 ScopedContextCreator(AnnotatingParser &P,
tok::TokenKind ContextKind,
2210 P.Contexts.push_back(Context(ContextKind,
2211 P.Contexts.back().BindingStrength + Increase,
2212 P.Contexts.back().IsExpression));
2215 ~ScopedContextCreator() {
2217 if (P.Contexts.back().ContextType == Context::StructArrayInitializer) {
2218 P.Contexts.pop_back();
2219 P.Contexts.back().ContextType = Context::StructArrayInitializer;
2223 P.Contexts.pop_back();
2228 auto AssignmentStartsExpression = [&]() {
2232 if (
Line.First->isOneOf(tok::kw_using, tok::kw_return))
2234 if (
Line.First->is(tok::kw_template)) {
2235 assert(Current.Previous);
2236 if (Current.Previous->is(tok::kw_operator)) {
2244 if (
Tok->isNot(TT_TemplateOpener)) {
2251 if (Contexts.back().ContextKind == tok::less) {
2252 assert(Current.Previous->Previous);
2253 return Current.Previous->Previous->isNoneOf(tok::kw_typename,
2257 Tok =
Tok->MatchingParen;
2260 Tok =
Tok->getNextNonComment();
2264 if (
Tok->isOneOf(tok::kw_class, tok::kw_enum, tok::kw_struct,
2274 if (Style.isJavaScript() &&
2275 (
Line.startsWith(Keywords.kw_type, tok::identifier) ||
2276 Line.startsWith(tok::kw_export, Keywords.kw_type,
2277 tok::identifier))) {
2281 return !Current.Previous || Current.Previous->isNot(tok::kw_operator);
2284 if (AssignmentStartsExpression()) {
2285 Contexts.back().IsExpression =
true;
2286 if (!
Line.startsWith(TT_UnaryOperator)) {
2289 Previous->Previous->isNoneOf(tok::comma, tok::semi);
2291 if (
Previous->isOneOf(tok::r_square, tok::r_paren, tok::greater)) {
2298 if (!
Previous->isTypeFinalized() &&
2299 Previous->isOneOf(TT_BinaryOperator, TT_UnaryOperator) &&
2301 Previous->Previous->isNot(tok::equal)) {
2302 Previous->setType(TT_PointerOrReference);
2306 }
else if (Current.is(tok::lessless) &&
2307 (!Current.Previous ||
2308 Current.Previous->isNot(tok::kw_operator))) {
2309 Contexts.back().IsExpression =
true;
2310 }
else if (Current.isOneOf(tok::kw_return, tok::kw_throw)) {
2311 Contexts.back().IsExpression =
true;
2312 }
else if (Current.is(TT_TrailingReturnArrow)) {
2313 Contexts.back().IsExpression =
false;
2314 }
else if (Current.isOneOf(TT_LambdaArrow, Keywords.kw_assert)) {
2315 Contexts.back().IsExpression = Style.isJava();
2316 }
else if (Current.Previous &&
2317 Current.Previous->is(TT_CtorInitializerColon)) {
2318 Contexts.back().IsExpression =
true;
2319 Contexts.back().ContextType = Context::CtorInitializer;
2320 }
else if (Current.Previous && Current.Previous->is(TT_InheritanceColon)) {
2321 Contexts.back().ContextType = Context::InheritanceList;
2322 }
else if (Current.isOneOf(tok::r_paren, tok::greater, tok::comma)) {
2326 Previous->setType(TT_PointerOrReference);
2328 if (
Line.MustBeDeclaration &&
2329 Contexts.front().ContextType != Context::CtorInitializer) {
2330 Contexts.back().IsExpression =
false;
2332 }
else if (Current.is(tok::kw_new)) {
2333 Contexts.back().CanBeExpression =
false;
2334 }
else if (Current.is(tok::semi) ||
2335 (Current.is(tok::exclaim) && Current.Previous &&
2336 Current.Previous->isNot(tok::kw_operator))) {
2340 Contexts.back().IsExpression =
true;
2348 if (Current->is(tok::l_paren))
2350 if (Current->is(tok::r_paren))
2354 Current = Current->Next;
2359 static bool isDeductionGuide(
FormatToken &Current) {
2361 if (Current.Previous && Current.Previous->is(tok::r_paren) &&
2362 Current.startsSequence(tok::arrow, tok::identifier, tok::less)) {
2366 while (TemplateCloser) {
2368 if (TemplateCloser->is(tok::l_paren)) {
2370 TemplateCloser = untilMatchingParen(TemplateCloser);
2371 if (!TemplateCloser)
2374 if (TemplateCloser->is(tok::less))
2376 if (TemplateCloser->is(tok::greater))
2380 TemplateCloser = TemplateCloser->Next;
2384 if (TemplateCloser && TemplateCloser->Next &&
2385 TemplateCloser->Next->is(tok::semi) &&
2386 Current.Previous->MatchingParen) {
2390 Current.Previous->MatchingParen->Previous;
2392 return LeadingIdentifier &&
2393 LeadingIdentifier->TokenText == Current.Next->TokenText;
2400 if (Current.isNot(TT_Unknown)) {
2405 if ((Style.isJavaScript() || Style.isCSharp()) &&
2406 Current.is(tok::exclaim)) {
2407 if (Current.Previous) {
2409 Style.isJavaScript()
2410 ? Keywords.isJavaScriptIdentifier(
2411 *Current.Previous,
true)
2412 : Current.Previous->is(tok::identifier);
2414 Current.Previous->isOneOf(
2415 tok::kw_default, tok::kw_namespace, tok::r_paren, tok::r_square,
2416 tok::r_brace, tok::kw_false, tok::kw_true, Keywords.kw_type,
2417 Keywords.kw_get, Keywords.kw_init, Keywords.kw_set) ||
2418 Current.Previous->Tok.isLiteral()) {
2419 Current.setType(TT_NonNullAssertion);
2424 Current.Next->isOneOf(TT_BinaryOperator, Keywords.kw_as)) {
2425 Current.setType(TT_NonNullAssertion);
2433 if ((Style.isJavaScript() || Style.isJava()) &&
2434 Current.is(Keywords.kw_instanceof)) {
2435 Current.setType(TT_BinaryOperator);
2436 }
else if (isStartOfName(Current) &&
2437 (!
Line.MightBeFunctionDecl || Current.NestingLevel != 0)) {
2438 Contexts.back().FirstStartOfName = &Current;
2439 Current.setType(TT_StartOfName);
2440 }
else if (Current.is(tok::semi)) {
2444 Contexts.back().FirstStartOfName =
nullptr;
2445 }
else if (Current.isOneOf(tok::kw_auto, tok::kw___auto_type)) {
2447 }
else if (Current.is(tok::arrow) && Style.isJava()) {
2448 Current.setType(TT_LambdaArrow);
2449 }
else if (Current.is(tok::arrow) && Style.isVerilog()) {
2451 Current.setType(TT_BinaryOperator);
2452 }
else if (Current.is(tok::arrow) && AutoFound &&
2453 Line.MightBeFunctionDecl && Current.NestingLevel == 0 &&
2454 Current.Previous->isNoneOf(tok::kw_operator, tok::identifier)) {
2456 Current.setType(TT_TrailingReturnArrow);
2457 }
else if (Current.is(tok::arrow) && Current.Previous &&
2458 Current.Previous->is(tok::r_brace) &&
2462 Current.setType(TT_TrailingReturnArrow);
2463 }
else if (isDeductionGuide(Current)) {
2465 Current.setType(TT_TrailingReturnArrow);
2466 }
else if (Current.isPointerOrReference()) {
2467 Current.setType(determineStarAmpUsage(
2469 (Contexts.back().CanBeExpression && Contexts.back().IsExpression) ||
2470 Contexts.back().InStaticAssertFirstArgument,
2471 Contexts.back().ContextType == Context::TemplateArgument));
2472 }
else if (Current.isOneOf(tok::minus, tok::plus, tok::caret) ||
2473 (Style.isVerilog() && Current.is(tok::pipe))) {
2474 Current.setType(determinePlusMinusCaretUsage(Current));
2475 if (Current.is(TT_UnaryOperator) && Current.is(tok::caret))
2476 Contexts.back().CaretFound =
true;
2477 }
else if (Current.isOneOf(tok::minusminus, tok::plusplus)) {
2478 Current.setType(determineIncrementUsage(Current));
2479 }
else if (Current.isOneOf(tok::exclaim, tok::tilde)) {
2480 Current.setType(TT_UnaryOperator);
2481 }
else if (Current.is(tok::question)) {
2482 if (Style.isJavaScript() &&
Line.MustBeDeclaration &&
2483 !Contexts.back().IsExpression) {
2486 Current.setType(TT_JsTypeOptionalQuestion);
2487 }
else if (Style.isTableGen()) {
2489 Current.setType(TT_Unknown);
2491 Current.setType(TT_ConditionalExpr);
2493 Contexts.back().IsExpression =
true;
2495 }
else if (Current.isBinaryOperator() &&
2496 (!Current.Previous || Current.Previous->isNot(tok::l_square)) &&
2497 (Current.isNot(tok::greater) && !Style.isTextProto())) {
2498 if (Style.isVerilog()) {
2499 if (Current.is(tok::lessequal) && Contexts.size() == 1 &&
2500 !Contexts.back().VerilogAssignmentFound) {
2504 Current.setFinalizedType(TT_BinaryOperator);
2507 Contexts.back().VerilogAssignmentFound =
true;
2509 Current.setType(TT_BinaryOperator);
2510 }
else if (Current.is(tok::comment)) {
2511 if (Current.TokenText.starts_with(
"/*")) {
2512 if (Current.TokenText.ends_with(
"*/")) {
2513 Current.setType(TT_BlockComment);
2517 Current.Tok.setKind(tok::unknown);
2520 Current.setType(TT_LineComment);
2522 }
else if (Current.is(tok::string_literal)) {
2523 if (Style.isVerilog() && Contexts.back().VerilogMayBeConcatenation &&
2524 Current.getPreviousNonComment() &&
2525 Current.getPreviousNonComment()->isOneOf(tok::comma, tok::l_brace) &&
2526 Current.getNextNonComment() &&
2527 Current.getNextNonComment()->isOneOf(tok::comma, tok::r_brace)) {
2528 Current.setType(TT_StringInConcatenation);
2530 }
else if (Current.is(tok::l_paren)) {
2531 if (lParenStartsCppCast(Current))
2532 Current.setType(TT_CppCastLParen);
2533 }
else if (Current.is(tok::r_paren)) {
2534 if (rParenEndsCast(Current))
2535 Current.setType(TT_CastRParen);
2536 if (Current.MatchingParen && Current.MatchingParen->is(TT_InlineASMParen))
2537 Current.setType(TT_InlineASMParen);
2538 if (Current.MatchingParen && Current.Next &&
2539 !Current.Next->isBinaryOperator() &&
2540 Current.Next->isNoneOf(
2541 tok::semi, tok::colon, tok::l_brace, tok::l_paren, tok::comma,
2542 tok::period, tok::arrow, tok::coloncolon, tok::kw_noexcept)) {
2543 if (
FormatToken *AfterParen = Current.MatchingParen->Next;
2544 AfterParen && AfterParen->isNot(tok::caret)) {
2546 if (
FormatToken *BeforeParen = Current.MatchingParen->Previous;
2547 BeforeParen && BeforeParen->is(tok::identifier) &&
2548 BeforeParen->isNot(TT_TypenameMacro) &&
2549 BeforeParen->TokenText == BeforeParen->TokenText.upper() &&
2550 (!BeforeParen->Previous ||
2551 BeforeParen->Previous->ClosesTemplateDeclaration ||
2552 BeforeParen->Previous->ClosesRequiresClause)) {
2553 Current.setType(TT_FunctionAnnotationRParen);
2557 }
else if (Current.is(tok::at) && Current.Next && !Style.isJavaScript() &&
2561 switch (Current.Next->Tok.getObjCKeywordID()) {
2562 case tok::objc_interface:
2563 case tok::objc_implementation:
2564 case tok::objc_protocol:
2565 Current.setType(TT_ObjCDecl);
2567 case tok::objc_property:
2568 Current.setType(TT_ObjCProperty);
2573 }
else if (Current.is(tok::period)) {
2574 FormatToken *PreviousNoComment = Current.getPreviousNonComment();
2575 if (PreviousNoComment &&
2576 PreviousNoComment->isOneOf(tok::comma, tok::l_brace)) {
2577 Current.setType(TT_DesignatedInitializerPeriod);
2578 }
else if (Style.isJava() && Current.Previous &&
2579 Current.Previous->isOneOf(TT_JavaAnnotation,
2580 TT_LeadingJavaAnnotation)) {
2581 Current.setType(Current.Previous->getType());
2583 }
else if (canBeObjCSelectorComponent(Current) &&
2586 Current.Previous && Current.Previous->is(TT_CastRParen) &&
2587 Current.Previous->MatchingParen &&
2588 Current.Previous->MatchingParen->Previous &&
2589 Current.Previous->MatchingParen->Previous->is(
2590 TT_ObjCMethodSpecifier)) {
2594 Current.setType(TT_SelectorName);
2595 }
else if (Current.isOneOf(tok::identifier, tok::kw_const, tok::kw_noexcept,
2596 tok::kw_requires) &&
2598 Current.Previous->isNoneOf(tok::equal, tok::at,
2599 TT_CtorInitializerComma,
2600 TT_CtorInitializerColon) &&
2601 Line.MightBeFunctionDecl && Contexts.size() == 1) {
2604 Current.setType(TT_TrailingAnnotation);
2605 }
else if ((Style.isJava() || Style.isJavaScript()) && Current.Previous) {
2606 if (Current.Previous->is(tok::at) &&
2607 Current.isNot(Keywords.kw_interface)) {
2611 Current.setType(TT_LeadingJavaAnnotation);
2613 Current.setType(TT_JavaAnnotation);
2614 }
else if (Current.Previous->is(tok::period) &&
2615 Current.Previous->isOneOf(TT_JavaAnnotation,
2616 TT_LeadingJavaAnnotation)) {
2617 Current.setType(Current.Previous->getType());
2629 if (Style.isVerilog())
2632 if (!
Tok.Previous ||
Tok.isNot(tok::identifier) ||
Tok.is(TT_ClassHeadName))
2635 if (
Tok.endsSequence(Keywords.kw_final, TT_ClassHeadName))
2638 if ((Style.isJavaScript() || Style.isJava()) &&
Tok.is(Keywords.kw_extends))
2641 if (
const auto *NextNonComment =
Tok.getNextNonComment();
2642 (!NextNonComment && !
Line.InMacroBody) ||
2644 (NextNonComment->isPointerOrReference() ||
2645 NextNonComment->isOneOf(TT_ClassHeadName, tok::string_literal) ||
2646 (
Line.InPragmaDirective && NextNonComment->is(tok::identifier))))) {
2650 if (
Tok.Previous->isOneOf(TT_LeadingJavaAnnotation, Keywords.kw_instanceof,
2654 if (Style.isJavaScript() &&
Tok.Previous->is(Keywords.kw_in))
2661 if (!Style.isJavaScript())
2662 while (PreviousNotConst && PreviousNotConst->is(tok::kw_const))
2663 PreviousNotConst = PreviousNotConst->getPreviousNonComment();
2665 if (!PreviousNotConst)
2668 if (PreviousNotConst->ClosesRequiresClause)
2671 if (Style.isTableGen()) {
2673 if (Keywords.isTableGenDefinition(*PreviousNotConst))
2676 if (Contexts.back().ContextKind != tok::l_brace)
2680 bool IsPPKeyword = PreviousNotConst->is(tok::identifier) &&
2681 PreviousNotConst->Previous &&
2682 PreviousNotConst->Previous->is(tok::hash);
2684 if (PreviousNotConst->is(TT_TemplateCloser)) {
2685 return PreviousNotConst && PreviousNotConst->MatchingParen &&
2686 PreviousNotConst->MatchingParen->Previous &&
2687 PreviousNotConst->MatchingParen->Previous->isNoneOf(
2688 tok::period, tok::kw_template);
2691 if ((PreviousNotConst->is(tok::r_paren) &&
2692 PreviousNotConst->is(TT_TypeDeclarationParen)) ||
2693 PreviousNotConst->is(TT_AttributeRParen)) {
2702 if (PreviousNotConst->isOneOf(tok::identifier, tok::kw_auto) &&
2703 !PreviousNotConst->endsSequence(Keywords.kw_import, tok::kw_export) &&
2704 PreviousNotConst->isNot(TT_StatementAttributeLikeMacro)) {
2709 if (PreviousNotConst->is(TT_PointerOrReference) ||
2710 PreviousNotConst->endsSequence(tok::coloncolon,
2711 TT_PointerOrReference)) {
2716 if (PreviousNotConst->isTypeName(LangOpts))
2720 if (Style.isJava() && PreviousNotConst->is(tok::r_square))
2724 return Style.isJavaScript() && PreviousNotConst->is(tok::kw_const);
2734 if (LeftOfParens && LeftOfParens->is(TT_TemplateCloser) &&
2735 LeftOfParens->MatchingParen) {
2736 auto *Prev = LeftOfParens->MatchingParen->getPreviousNonComment();
2738 Prev->isOneOf(tok::kw_const_cast, tok::kw_dynamic_cast,
2739 tok::kw_reinterpret_cast, tok::kw_static_cast)) {
2750 assert(
Tok.is(tok::r_paren));
2752 if (!
Tok.MatchingParen || !
Tok.Previous)
2756 if (!IsCpp && !Style.isCSharp() && !Style.isJava())
2759 const auto *LParen =
Tok.MatchingParen;
2760 const auto *BeforeRParen =
Tok.Previous;
2761 const auto *AfterRParen =
Tok.Next;
2764 if (BeforeRParen == LParen || !AfterRParen)
2767 if (LParen->isOneOf(TT_OverloadedOperatorLParen, TT_FunctionTypeLParen))
2770 auto *LeftOfParens = LParen->getPreviousNonComment();
2774 if (LeftOfParens->is(tok::r_paren) &&
2775 LeftOfParens->isNot(TT_CastRParen)) {
2776 if (!LeftOfParens->MatchingParen ||
2777 !LeftOfParens->MatchingParen->Previous) {
2780 LeftOfParens = LeftOfParens->MatchingParen->Previous;
2783 if (LeftOfParens->is(tok::r_square)) {
2786 if (
Tok->isNot(tok::r_square))
2789 Tok =
Tok->getPreviousNonComment();
2790 if (!
Tok ||
Tok->isNot(tok::l_square))
2793 Tok =
Tok->getPreviousNonComment();
2794 if (!
Tok ||
Tok->isNot(tok::kw_delete))
2798 if (
FormatToken *MaybeDelete = MayBeArrayDelete(LeftOfParens))
2799 LeftOfParens = MaybeDelete;
2805 if (LeftOfParens->Tok.getIdentifierInfo() && LeftOfParens->Previous &&
2806 LeftOfParens->Previous->is(tok::kw_operator)) {
2812 if (LeftOfParens->Tok.getIdentifierInfo() &&
2813 LeftOfParens->isNoneOf(TT_ObjCForIn, tok::kw_return, tok::kw_case,
2814 tok::kw_delete, tok::kw_throw)) {
2820 if (LeftOfParens->isOneOf(tok::at, tok::r_square, TT_OverloadedOperator,
2821 TT_TemplateCloser, tok::ellipsis)) {
2826 if (AfterRParen->is(tok::question) ||
2827 (AfterRParen->is(tok::ampamp) && !BeforeRParen->isTypeName(LangOpts))) {
2832 if (AfterRParen->is(Keywords.kw_in) && Style.isCSharp())
2837 if (AfterRParen->isOneOf(tok::kw_noexcept, tok::kw_volatile, tok::kw_const,
2838 tok::kw_requires, tok::kw_throw, tok::arrow,
2839 Keywords.kw_override, Keywords.kw_final) ||
2840 isCppAttribute(IsCpp, *AfterRParen)) {
2846 if (Style.isJava() && AfterRParen->is(tok::l_paren))
2850 if (AfterRParen->isOneOf(tok::kw_sizeof, tok::kw_alignof) ||
2851 (AfterRParen->Tok.isLiteral() &&
2852 AfterRParen->isNot(tok::string_literal))) {
2857 if (
Tok.isNot(TT_TemplateCloser))
2859 const auto *
Less =
Tok.MatchingParen;
2862 const auto *BeforeLess =
Less->getPreviousNonComment();
2863 return BeforeLess && BeforeLess->isNot(TT_VariableTemplate);
2867 auto IsQualifiedPointerOrReference = [](
const FormatToken *T,
2868 const LangOptions &LangOpts) {
2870 assert(!T->isTypeName(LangOpts) &&
"Should have already been checked");
2874 if (T->is(TT_AttributeRParen)) {
2876 assert(T->is(tok::r_paren));
2877 assert(T->MatchingParen);
2878 assert(T->MatchingParen->is(tok::l_paren));
2879 assert(T->MatchingParen->is(TT_AttributeLParen));
2880 if (
const auto *
Tok = T->MatchingParen->Previous;
2881 Tok &&
Tok->isAttribute()) {
2885 }
else if (T->is(TT_AttributeRSquare)) {
2887 if (T->MatchingParen && T->MatchingParen->Previous) {
2888 T = T->MatchingParen->Previous;
2891 }
else if (T->canBePointerOrReferenceQualifier()) {
2897 return T && T->is(TT_PointerOrReference);
2900 bool ParensAreType = IsNonVariableTemplate(*BeforeRParen) ||
2901 BeforeRParen->is(TT_TypeDeclarationParen) ||
2902 BeforeRParen->isTypeName(LangOpts) ||
2903 IsQualifiedPointerOrReference(BeforeRParen, LangOpts);
2904 bool ParensCouldEndDecl =
2905 AfterRParen->isOneOf(tok::equal, tok::semi, tok::l_brace, tok::greater);
2906 if (ParensAreType && !ParensCouldEndDecl)
2917 for (
const auto *Token = LParen->Next; Token != &
Tok; Token = Token->Next)
2918 if (Token->is(TT_BinaryOperator))
2923 if (AfterRParen->isOneOf(tok::identifier, tok::kw_this))
2927 if (AfterRParen->is(tok::l_paren)) {
2928 for (
const auto *Prev = BeforeRParen; Prev->is(tok::identifier);) {
2929 Prev = Prev->Previous;
2930 if (Prev->is(tok::coloncolon))
2931 Prev = Prev->Previous;
2937 if (!AfterRParen->Next)
2950 const bool NextIsAmpOrStar = AfterRParen->isOneOf(tok::amp, tok::star);
2951 if (!(AfterRParen->isUnaryOperator() || NextIsAmpOrStar) ||
2952 AfterRParen->is(tok::plus) ||
2953 AfterRParen->Next->isNoneOf(tok::identifier, tok::numeric_constant)) {
2957 if (NextIsAmpOrStar &&
2958 (AfterRParen->Next->is(tok::numeric_constant) ||
Line.InPPDirective)) {
2962 if (
Line.InPPDirective && AfterRParen->is(tok::minus))
2965 const auto *Prev = BeforeRParen;
2968 if (Prev->is(tok::r_paren)) {
2969 if (Prev->is(TT_CastRParen))
2971 Prev = Prev->MatchingParen;
2974 Prev = Prev->Previous;
2975 if (!Prev || Prev->isNot(tok::r_paren))
2977 Prev = Prev->MatchingParen;
2978 return Prev && Prev->is(TT_FunctionTypeLParen);
2982 for (Prev = BeforeRParen; Prev != LParen; Prev = Prev->Previous)
2983 if (Prev->isNoneOf(tok::kw_const, tok::identifier, tok::coloncolon))
3001 if (PrevToken->isOneOf(
3002 TT_ConditionalExpr, tok::l_paren, tok::comma, tok::colon, tok::semi,
3003 tok::equal, tok::question, tok::l_square, tok::l_brace,
3004 tok::kw_case, tok::kw_co_await, tok::kw_co_return, tok::kw_co_yield,
3005 tok::kw_delete, tok::kw_return, tok::kw_throw)) {
3012 if (PrevToken->is(tok::kw_sizeof))
3016 if (PrevToken->isOneOf(TT_CastRParen, TT_UnaryOperator))
3020 if (PrevToken->is(TT_BinaryOperator))
3027 TokenType determineStarAmpUsage(
const FormatToken &
Tok,
bool IsExpression,
3028 bool InTemplateArgument) {
3029 if (Style.isJavaScript())
3030 return TT_BinaryOperator;
3033 if (Style.isCSharp() &&
Tok.is(tok::ampamp))
3034 return TT_BinaryOperator;
3036 if (Style.isVerilog()) {
3041 if (
Tok.is(tok::star))
3042 return TT_BinaryOperator;
3043 return determineUnaryOperatorByUsage(
Tok) ? TT_UnaryOperator
3044 : TT_BinaryOperator;
3049 return TT_UnaryOperator;
3050 if (PrevToken->isTypeName(LangOpts))
3051 return TT_PointerOrReference;
3052 if (PrevToken->isPlacementOperator() &&
Tok.is(tok::ampamp))
3053 return TT_BinaryOperator;
3055 auto *NextToken =
Tok.getNextNonComment();
3057 return TT_PointerOrReference;
3058 if (NextToken->is(tok::greater))
3059 return TT_PointerOrReference;
3061 if (InTemplateArgument && NextToken->is(tok::kw_noexcept))
3062 return TT_BinaryOperator;
3064 if (NextToken->isOneOf(tok::arrow, tok::equal, tok::comma, tok::r_paren,
3065 tok::semi, TT_RequiresClause) ||
3066 (NextToken->is(tok::kw_noexcept) && !IsExpression) ||
3067 NextToken->canBePointerOrReferenceQualifier() ||
3068 (NextToken->is(tok::l_brace) && !NextToken->getNextNonComment())) {
3069 return TT_PointerOrReference;
3072 if (PrevToken->is(tok::coloncolon))
3073 return TT_PointerOrReference;
3075 if (PrevToken->is(tok::r_paren) && PrevToken->is(TT_TypeDeclarationParen))
3076 return TT_PointerOrReference;
3078 if (determineUnaryOperatorByUsage(
Tok))
3079 return TT_UnaryOperator;
3081 if (NextToken->is(tok::l_square) && NextToken->isNot(TT_LambdaLSquare))
3082 return TT_PointerOrReference;
3083 if (NextToken->is(tok::kw_operator) && !IsExpression)
3084 return TT_PointerOrReference;
3096 if (PrevToken->is(tok::r_brace) &&
Tok.is(tok::star) &&
3097 !PrevToken->MatchingParen) {
3098 return TT_PointerOrReference;
3101 if (PrevToken->endsSequence(tok::r_square, tok::l_square, tok::kw_delete))
3102 return TT_UnaryOperator;
3104 if (PrevToken->Tok.isLiteral() ||
3105 PrevToken->isOneOf(tok::r_paren, tok::r_square, tok::kw_true,
3106 tok::kw_false, tok::r_brace)) {
3107 return TT_BinaryOperator;
3111 while (NextNonParen && NextNonParen->is(tok::l_paren))
3112 NextNonParen = NextNonParen->getNextNonComment();
3113 if (NextNonParen && (NextNonParen->Tok.isLiteral() ||
3114 NextNonParen->isOneOf(tok::kw_true, tok::kw_false) ||
3115 NextNonParen->isUnaryOperator())) {
3116 return TT_BinaryOperator;
3122 if (InTemplateArgument && NextToken->Tok.isAnyIdentifier())
3123 return TT_BinaryOperator;
3127 if (
Tok.is(tok::ampamp) &&
3128 NextToken->isOneOf(tok::l_paren, tok::star, tok::amp)) {
3129 return TT_BinaryOperator;
3136 if (NextToken->Tok.isAnyIdentifier()) {
3137 auto *NextNextToken = NextToken->getNextNonComment();
3138 if (NextNextToken) {
3139 if (NextNextToken->is(tok::arrow))
3140 return TT_BinaryOperator;
3141 if (NextNextToken->isPointerOrReference() &&
3142 !NextToken->isObjCLifetimeQualifier(Style)) {
3143 NextNextToken->setFinalizedType(TT_BinaryOperator);
3144 return TT_BinaryOperator;
3151 if (IsExpression && !Contexts.back().CaretFound &&
3152 Line.getFirstNonComment()->isNot(
3153 TT_RequiresClauseInARequiresExpression)) {
3154 return TT_BinaryOperator;
3158 if (!Scopes.empty() && Scopes.back() ==
ST_Class)
3159 return TT_PointerOrReference;
3162 auto IsChainedOperatorAmpOrMember = [](
const FormatToken *token) {
3163 return !token || token->isOneOf(tok::amp, tok::period, tok::arrow,
3164 tok::arrowstar, tok::periodstar);
3169 if (
Tok.is(tok::amp) && PrevToken->Tok.isAnyIdentifier() &&
3170 IsChainedOperatorAmpOrMember(PrevToken->getPreviousNonComment()) &&
3171 NextToken && NextToken->Tok.isAnyIdentifier()) {
3172 if (
auto NextNext = NextToken->getNextNonComment();
3174 (IsChainedOperatorAmpOrMember(NextNext) || NextNext->is(tok::semi))) {
3175 return TT_BinaryOperator;
3181 return TT_BinaryOperator;
3184 return TT_PointerOrReference;
3188 if (determineUnaryOperatorByUsage(
Tok))
3189 return TT_UnaryOperator;
3193 return TT_UnaryOperator;
3195 if (PrevToken->is(tok::at))
3196 return TT_UnaryOperator;
3199 return TT_BinaryOperator;
3205 if (!PrevToken || PrevToken->is(TT_CastRParen))
3206 return TT_UnaryOperator;
3207 if (PrevToken->isOneOf(tok::r_paren, tok::r_square, tok::identifier))
3208 return TT_TrailingUnaryOperator;
3210 return TT_UnaryOperator;
3216 AnnotatedLine &
Line;
3220 LangOptions LangOpts;
3221 const AdditionalKeywords &Keywords;
3231 int TemplateDeclarationDepth;
3239class ExpressionParser {
3241 ExpressionParser(
const FormatStyle &Style,
const AdditionalKeywords &Keywords,
3242 AnnotatedLine &
Line)
3243 : Style(Style), Keywords(Keywords),
Line(
Line), Current(
Line.First) {}
3246 void parse(
int Precedence = 0) {
3249 while (Current && (Current->is(tok::kw_return) ||
3250 (Current->is(tok::colon) &&
3251 Current->isOneOf(TT_ObjCMethodExpr, TT_DictLiteral)))) {
3255 if (!Current || Precedence > PrecedenceArrowAndPeriod)
3260 parseConditionalExpr();
3266 if (Precedence == PrecedenceUnaryOperator) {
3267 parseUnaryOperator();
3284 if (Style.isVerilog() && Precedence ==
prec::Comma) {
3285 VerilogFirstOfType =
3286 verilogGroupDecl(VerilogFirstOfType, LatestOperator);
3290 parse(Precedence + 1);
3292 int CurrentPrecedence = getCurrentPrecedence();
3301 if (Style.BreakBinaryOperations.PerOperator.empty() &&
3302 Style.BreakBinaryOperations.Default ==
3308 if (Precedence == CurrentPrecedence && Current &&
3309 Current->is(TT_SelectorName)) {
3311 addFakeParenthesis(Start,
prec::Level(Precedence));
3315 if ((Style.isCSharp() || Style.isJavaScript() || Style.isJava()) &&
3319 FormatToken *Prev = Current->getPreviousNonComment();
3320 if (Prev && Prev->is(tok::string_literal) &&
3321 (Prev == Start || Prev->endsSequence(tok::string_literal, tok::plus,
3322 TT_StringInConcatenation))) {
3323 Prev->setType(TT_StringInConcatenation);
3330 (Current->closesScope() &&
3331 (Current->MatchingParen || Current->is(TT_TemplateString))) ||
3332 (CurrentPrecedence != -1 && CurrentPrecedence < Precedence) ||
3341 if (Current->opensScope() ||
3342 Current->isOneOf(TT_RequiresClause,
3343 TT_RequiresClauseInARequiresExpression)) {
3346 while (Current && (!Current->closesScope() || Current->opensScope())) {
3353 if (CurrentPrecedence == Precedence) {
3355 LatestOperator->NextOperator = Current;
3356 LatestOperator = Current;
3360 next(Precedence > 0);
3365 if (Style.isVerilog() && Precedence ==
prec::Comma && VerilogFirstOfType)
3366 addFakeParenthesis(VerilogFirstOfType,
prec::Comma);
3368 if (LatestOperator && (Current || Precedence > 0)) {
3374 Start->Previous->isOneOf(TT_RequiresClause,
3375 TT_RequiresClauseInARequiresExpression))
3377 auto Ret = Current ? Current :
Line.Last;
3378 while (!Ret->ClosesRequiresClause && Ret->Previous)
3379 Ret = Ret->Previous;
3384 if (Precedence == PrecedenceArrowAndPeriod) {
3388 addFakeParenthesis(Start,
prec::Level(Precedence), End);
3396 int getCurrentPrecedence() {
3398 const FormatToken *NextNonComment = Current->getNextNonComment();
3399 if (Current->is(TT_ConditionalExpr))
3401 if (NextNonComment && Current->is(TT_SelectorName) &&
3402 (NextNonComment->isOneOf(TT_DictLiteral, TT_JsTypeColon) ||
3403 (Style.isProto() && NextNonComment->is(tok::less)))) {
3406 if (Current->is(TT_JsComputedPropertyName))
3408 if (Current->is(TT_LambdaArrow))
3410 if (Current->is(TT_FatArrow))
3412 if (Current->isOneOf(tok::semi, TT_InlineASMColon, TT_SelectorName) ||
3413 (Current->is(tok::comment) && NextNonComment &&
3414 NextNonComment->is(TT_SelectorName))) {
3417 if (Current->is(TT_RangeBasedForLoopColon))
3419 if ((Style.isJava() || Style.isJavaScript()) &&
3420 Current->is(Keywords.kw_instanceof)) {
3423 if (Style.isJavaScript() &&
3424 Current->isOneOf(Keywords.kw_in, Keywords.kw_as)) {
3427 if (Current->isOneOf(TT_BinaryOperator, TT_EnumEqual, tok::comma))
3428 return Current->getPrecedence();
3429 if (Current->isOneOf(tok::period, tok::arrow) &&
3430 Current->isNot(TT_TrailingReturnArrow)) {
3431 return PrecedenceArrowAndPeriod;
3433 if ((Style.isJava() || Style.isJavaScript()) &&
3434 Current->isOneOf(Keywords.kw_extends, Keywords.kw_implements,
3435 Keywords.kw_throws)) {
3440 if (Style.isVerilog() && Current->is(tok::colon))
3452 if (Start->MacroParent)
3455 Start->FakeLParens.push_back(Precedence);
3457 Start->StartsBinaryExpression =
true;
3458 if (!End && Current)
3459 End = Current->getPreviousNonComment();
3463 End->EndsBinaryExpression =
true;
3469 void parseUnaryOperator() {
3471 while (Current && Current->is(TT_UnaryOperator)) {
3472 Tokens.push_back(Current);
3475 parse(PrecedenceArrowAndPeriod);
3482 void parseConditionalExpr() {
3483 while (Current && Current->isTrailingComment())
3487 if (!Current || Current->isNot(tok::question))
3491 if (!Current || Current->isNot(TT_ConditionalExpr))
3498 void next(
bool SkipPastLeadingComments =
true) {
3500 Current = Current->Next;
3502 (Current->NewlinesBefore == 0 || SkipPastLeadingComments) &&
3503 Current->isTrailingComment()) {
3504 Current = Current->Next;
3518 while (Start->startsSequence(tok::l_paren, tok::star)) {
3519 if (!(Start = Start->MatchingParen) ||
3520 !(Start = Start->getNextNonComment())) {
3527 if (
Tok->is(Keywords.kw_assign))
3528 Tok =
Tok->getNextNonComment();
3540 if (
Tok->is(tok::hash)) {
3545 Tok =
Tok->getNextNonComment();
3546 }
else if (
Tok->is(tok::hashhash)) {
3550 Tok =
Tok->getNextNonComment();
3551 }
else if (Keywords.isVerilogQualifier(*
Tok) ||
3552 Keywords.isVerilogIdentifier(*
Tok)) {
3556 while (
Tok &&
Tok->isOneOf(tok::period, tok::coloncolon) &&
3557 (
Tok =
Tok->getNextNonComment())) {
3558 if (Keywords.isVerilogIdentifier(*
Tok))
3559 Tok =
Tok->getNextNonComment();
3563 }
else if (
Tok->is(tok::l_paren)) {
3568 Keywords.kw_highz0, Keywords.kw_highz1, Keywords.kw_large,
3569 Keywords.kw_medium, Keywords.kw_pull0, Keywords.kw_pull1,
3570 Keywords.kw_small, Keywords.kw_strong0, Keywords.kw_strong1,
3571 Keywords.kw_supply0, Keywords.kw_supply1, Keywords.kw_weak0,
3572 Keywords.kw_weak1)) {
3573 Tok->setType(TT_VerilogStrength);
3574 Tok =
Tok->MatchingParen;
3576 Tok->setType(TT_VerilogStrength);
3577 Tok =
Tok->getNextNonComment();
3582 }
else if (
Tok->is(Keywords.kw_verilogHash)) {
3584 if (
Next->is(tok::l_paren))
3587 Tok =
Next->getNextNonComment();
3596 while (
Tok &&
Tok->is(tok::l_square) && (
Tok =
Tok->MatchingParen))
3597 Tok =
Tok->getNextNonComment();
3598 if (
Tok && (
Tok->is(tok::hash) || Keywords.isVerilogIdentifier(*
Tok)))
3607 First->setType(TT_VerilogDimensionedTypeName);
3608 }
else if (
First != Start) {
3616 if (TypedName->is(TT_Unknown))
3617 TypedName->setType(TT_StartOfName);
3619 if (FirstOfType && PreviousComma) {
3620 PreviousComma->setType(TT_VerilogTypeComma);
3621 addFakeParenthesis(FirstOfType,
prec::Comma, PreviousComma->Previous);
3624 FirstOfType = TypedName;
3631 while (Current && Current != FirstOfType) {
3632 if (Current->opensScope()) {
3644 const AdditionalKeywords &Keywords;
3645 const AnnotatedLine &
Line;
3655 assert(
Line->First);
3659 if (
const auto Column =
Line->First->OriginalColumn;
3663 const bool PPDirectiveOrImportStmt =
3666 if (PPDirectiveOrImportStmt)
3668 if (
const auto IndentWidth = Style.IndentWidth;
3670 Column % IndentWidth == 0) {
3678 PPDirectiveOrImportStmt
3680 : NextNonCommentLine->
Level;
3683 NextNonCommentLine =
Line->First->isNot(tok::r_brace) ?
Line :
nullptr;
3703 if (
Tok->isNot(tok::identifier))
3706 Tok =
Tok->getNextNonComment();
3712 if (
Tok->is(tok::coloncolon))
3713 return Tok->getNextNonComment();
3717 if (
Tok->is(TT_TemplateOpener)) {
3718 Tok =
Tok->MatchingParen;
3722 Tok =
Tok->getNextNonComment();
3727 return Tok->is(tok::coloncolon) ?
Tok->getNextNonComment() :
nullptr;
3735 Tok =
Tok->getNextNonComment()) {
3737 if (
Tok->is(TT_AttributeLSquare)) {
3738 Tok =
Tok->MatchingParen;
3746 if (
Tok->is(tok::l_paren) &&
Tok->is(TT_Unknown) &&
Tok->MatchingParen) {
3754 if (
Tok->isOneOf(tok::kw_friend, tok::kw_inline, tok::kw_virtual,
3755 tok::kw_constexpr, tok::kw_consteval, tok::kw_explicit)) {
3761 if (
Tok->is(tok::kw_template)) {
3762 Tok =
Tok->getNextNonComment();
3768 if (
Tok->isNot(TT_TemplateOpener))
3771 Tok =
Tok->MatchingParen;
3779 if (
Tok->is(tok::coloncolon)) {
3790 if (
Tok->is(tok::tilde)) {
3797 if (
Tok->isNot(tok::identifier) ||
Tok->isNot(TT_Unknown))
3808 assert(
Tok &&
Tok->is(tok::identifier));
3809 const auto *Prev =
Tok->Previous;
3811 if (Prev && Prev->is(tok::tilde))
3812 Prev = Prev->Previous;
3815 if (!Prev || (!Prev->endsSequence(tok::coloncolon, tok::identifier) &&
3816 !Prev->endsSequence(tok::coloncolon, TT_TemplateCloser))) {
3820 assert(Prev->Previous);
3821 if (Prev->Previous->is(TT_TemplateCloser) && Prev->Previous->MatchingParen) {
3822 Prev = Prev->Previous->MatchingParen;
3823 assert(Prev->Previous);
3826 return Prev->Previous->TokenText ==
Tok->TokenText;
3830 if (!
Line.InMacroBody)
3831 MacroBodyScopes.clear();
3833 auto &ScopeStack =
Line.InMacroBody ? MacroBodyScopes : Scopes;
3834 AnnotatingParser
Parser(Style,
Line, Keywords, ScopeStack);
3837 if (!
Line.Children.empty()) {
3840 for (
auto &Child :
Line.Children) {
3841 if (InRequiresExpression &&
3842 Child->First->isNoneOf(tok::kw_typename, tok::kw_requires,
3843 TT_CompoundRequirementLBrace)) {
3849 if (!ScopeStack.empty())
3850 ScopeStack.pop_back();
3863 ExpressionParser ExprParser(Style, Keywords,
Line);
3869 if (
Tok && ((!ScopeStack.empty() && ScopeStack.back() ==
ST_Class) ||
3871 Tok->setFinalizedType(TT_CtorDtorDeclName);
3872 assert(OpeningParen);
3877 if (
Line.startsWith(TT_ObjCMethodSpecifier))
3879 else if (
Line.startsWith(TT_ObjCDecl))
3881 else if (
Line.startsWith(TT_ObjCProperty))
3885 First->SpacesRequiredBefore = 1;
3886 First->CanBreakBefore =
First->MustBreakBefore;
3895 if (Current.
is(TT_FunctionDeclarationName))
3898 if (Current.
isNoneOf(tok::identifier, tok::kw_operator))
3901 const auto *Prev = Current.getPreviousNonComment();
3906 if (
const auto *PrevPrev =
Previous.getPreviousNonComment();
3907 PrevPrev && PrevPrev->is(TT_ObjCDecl)) {
3911 auto skipOperatorName =
3914 if (
Next->is(TT_OverloadedOperatorLParen))
3916 if (
Next->is(TT_OverloadedOperator))
3918 if (
Next->isPlacementOperator() ||
Next->is(tok::kw_co_await)) {
3921 Next->Next->startsSequence(tok::l_square, tok::r_square)) {
3926 if (
Next->startsSequence(tok::l_square, tok::r_square)) {
3931 if ((
Next->isTypeName(LangOpts) ||
Next->is(tok::identifier)) &&
3932 Next->Next &&
Next->Next->isPointerOrReference()) {
3937 if (
Next->is(TT_TemplateOpener) &&
Next->MatchingParen) {
3948 const bool IsCpp = LangOpts.CXXOperatorNames || LangOpts.C11;
3951 if (Current.
is(tok::kw_operator)) {
3952 if (
Line.startsWith(tok::kw_friend))
3954 if (
Previous.Tok.getIdentifierInfo() &&
3955 Previous.isNoneOf(tok::kw_return, tok::kw_co_return)) {
3960 assert(
Previous.MatchingParen->is(tok::l_paren));
3961 assert(
Previous.MatchingParen->is(TT_TypeDeclarationParen));
3970 while (
Next &&
Next->startsSequence(tok::hashhash, tok::identifier))
3973 if (
Next->is(TT_TemplateOpener) &&
Next->MatchingParen) {
3975 }
else if (
Next->is(tok::coloncolon)) {
3979 if (
Next->is(tok::kw_operator)) {
3980 Next = skipOperatorName(
Next->Next);
3983 if (
Next->isNot(tok::identifier))
3985 }
else if (isCppAttribute(IsCpp, *
Next)) {
3989 }
else if (
Next->is(tok::l_paren)) {
3998 if (!
Next ||
Next->isNot(tok::l_paren) || !
Next->MatchingParen)
4000 ClosingParen =
Next->MatchingParen;
4001 assert(ClosingParen->
is(tok::r_paren));
4003 if (
Line.Last->is(tok::l_brace))
4005 if (
Next->Next == ClosingParen)
4008 if (ClosingParen->
Next && ClosingParen->
Next->
is(TT_PointerOrReference))
4021 if (IsCpp &&
Next->Next &&
Next->Next->is(tok::identifier) &&
4022 !
Line.endsWith(tok::semi)) {
4028 if (
Tok->is(TT_TypeDeclarationParen))
4030 if (
Tok->isOneOf(tok::l_paren, TT_TemplateOpener) &&
Tok->MatchingParen) {
4031 Tok =
Tok->MatchingParen;
4034 if (
Tok->is(tok::kw_const) ||
Tok->isTypeName(LangOpts) ||
4035 Tok->isOneOf(TT_PointerOrReference, TT_StartOfName, tok::ellipsis)) {
4038 if (
Tok->isOneOf(tok::l_brace, TT_ObjCMethodExpr) ||
Tok->Tok.isLiteral())
4044bool TokenAnnotator::mustBreakForReturnType(
const AnnotatedLine &
Line)
const {
4045 assert(
Line.MightBeFunctionDecl);
4053 switch (Style.BreakAfterReturnType) {
4063 return Line.mightBeFunctionDefinition();
4069bool TokenAnnotator::mustBreakBeforeReturnType(
4071 assert(
Line.MightBeFunctionDecl);
4073 switch (Style.BreakBeforeReturnType) {
4079 return Line.Level == 0;
4081 return Line.mightBeFunctionDefinition();
4083 return Line.Level == 0 &&
Line.mightBeFunctionDefinition();
4090 auto *
Tok =
Line.getFirstNonComment();
4094 if (
Tok->is(tok::kw_template)) {
4095 auto *Opener =
Tok->Next;
4096 while (Opener && Opener->isNot(TT_TemplateOpener))
4097 Opener = Opener->Next;
4098 if (!Opener || !Opener->MatchingParen)
4100 Tok = Opener->MatchingParen->Next;
4103 if (
Tok &&
Tok->is(TT_RequiresClause)) {
4104 while (
Tok && !
Tok->ClosesRequiresClause)
4112 Tok->isOneOf(tok::kw___attribute, tok::kw___declspec,
4113 TT_AttributeMacro)) {
4115 if (
Next &&
Next->is(tok::l_paren) &&
Next->MatchingParen)
4116 Tok =
Next->MatchingParen->Next;
4121 if (
Tok->is(TT_AttributeLSquare) &&
Tok->MatchingParen) {
4122 Tok =
Tok->MatchingParen->Next;
4134 Line.Computed =
true;
4142 :
Line.FirstStartColumn +
First->ColumnWidth;
4143 bool AlignArrayOfStructures =
4146 if (AlignArrayOfStructures)
4147 calculateArrayInitializerColumnList(
Line);
4149 const auto *FirstNonComment =
Line.getFirstNonComment();
4150 bool SeenName =
false;
4151 bool LineIsFunctionDeclaration =
false;
4155 for (
auto *
Tok = FirstNonComment && FirstNonComment->isNot(tok::kw_using)
4156 ? FirstNonComment->Next
4159 if (
Tok->is(TT_StartOfName))
4161 if (
Tok->Previous->EndsCppAttributeGroup)
4162 AfterLastAttribute =
Tok;
4163 if (
const bool IsCtorOrDtor =
Tok->is(TT_CtorDtorDeclName);
4167 Tok->setFinalizedType(TT_FunctionDeclarationName);
4168 LineIsFunctionDeclaration =
true;
4172 assert(OpeningParen);
4173 if (OpeningParen->is(TT_Unknown))
4174 OpeningParen->setType(TT_FunctionDeclarationLParen);
4181 if ((LineIsFunctionDeclaration ||
4182 (FirstNonComment && FirstNonComment->is(TT_CtorDtorDeclName))) &&
4183 Line.endsWith(tok::semi, tok::r_brace)) {
4184 auto *
Tok =
Line.Last->Previous;
4185 while (
Tok->isNot(tok::r_brace))
4187 if (
auto *LBrace =
Tok->MatchingParen; LBrace && LBrace->is(TT_Unknown)) {
4188 assert(LBrace->is(tok::l_brace));
4191 LBrace->setFinalizedType(TT_FunctionLBrace);
4195 if (SeenName && AfterLastAttribute &&
4196 AfterLastAttribute->mustBreakAfterAttributes(Style)) {
4198 if (LineIsFunctionDeclaration)
4199 Line.ReturnTypeWrapped =
true;
4202 if (!LineIsFunctionDeclaration) {
4203 Line.ReturnTypeWrapped =
false;
4205 for (
const auto *
Tok = FirstNonComment;
Tok;
Tok =
Tok->Next) {
4206 if (
Tok->isNot(tok::kw_operator))
4210 }
while (
Tok &&
Tok->isNot(TT_OverloadedOperatorLParen));
4211 if (!
Tok || !
Tok->MatchingParen)
4213 const auto *LeftParen =
Tok;
4214 for (
Tok =
Tok->Next;
Tok &&
Tok != LeftParen->MatchingParen;
4216 if (
Tok->isNot(tok::identifier))
4219 const bool NextIsBinaryOperator =
4221 Next->Next->is(tok::identifier);
4222 if (!NextIsBinaryOperator)
4224 Next->setType(TT_BinaryOperator);
4228 }
else if (ClosingParen) {
4230 if (
Tok->is(TT_CtorInitializerColon))
4232 if (
Tok->is(tok::arrow)) {
4233 Tok->overwriteFixedType(TT_TrailingReturnArrow);
4236 if (
Tok->isNot(TT_TrailingAnnotation))
4239 if (!
Next ||
Next->isNot(tok::l_paren))
4248 if (
Line.MightBeFunctionDecl && LineIsFunctionDeclaration &&
4249 mustBreakBeforeReturnType(
Line)) {
4251 ReturnTypeStart && ReturnTypeStart != FirstNonComment &&
4252 ReturnTypeStart->
isNoneOf(TT_FunctionDeclarationName,
4253 TT_CtorDtorDeclName, tok::tilde)) {
4254 ReturnTypeStart->MustBreakBefore =
true;
4255 Line.ReturnTypeWrapped =
true;
4259 if (
First->is(TT_ElseLBrace)) {
4260 First->CanBreakBefore =
true;
4261 First->MustBreakBefore =
true;
4264 bool InFunctionDecl =
Line.MightBeFunctionDecl;
4265 bool InParameterList =
false;
4266 for (
auto *Current =
First->Next; Current; Current = Current->Next) {
4268 if (Current->is(TT_LineComment)) {
4270 Current->SpacesRequiredBefore =
4272 !Style.SpacesInParensOptions.Other)
4275 }
else if (Prev->
is(TT_VerilogMultiLineListLParen)) {
4276 Current->SpacesRequiredBefore = 0;
4278 Current->SpacesRequiredBefore = Style.SpacesBeforeTrailingComments;
4288 if (!Current->HasUnescapedNewline) {
4291 if (
Parameter->isOneOf(tok::comment, tok::r_brace))
4294 if (
Parameter->Previous->isNot(TT_CtorInitializerComma) &&
4302 }
else if (!Current->Finalized && Current->SpacesRequiredBefore == 0 &&
4303 spaceRequiredBefore(
Line, *Current)) {
4304 Current->SpacesRequiredBefore = 1;
4309 Current->MustBreakBefore =
true;
4311 Current->MustBreakBefore =
4312 Current->MustBreakBefore || mustBreakBefore(
Line, *Current);
4313 if (!Current->MustBreakBefore && InFunctionDecl &&
4314 Current->is(TT_FunctionDeclarationName)) {
4315 Current->MustBreakBefore = mustBreakForReturnType(
Line);
4319 Current->CanBreakBefore =
4320 !
Line.IsModuleOrImportDecl &&
4321 (Current->MustBreakBefore || canBreakBefore(
Line, *Current));
4323 if (Current->is(TT_FunctionDeclarationLParen)) {
4324 InParameterList =
true;
4325 }
else if (Current->is(tok::r_paren)) {
4326 const auto *LParen = Current->MatchingParen;
4327 if (LParen && LParen->is(TT_FunctionDeclarationLParen))
4328 InParameterList =
false;
4329 }
else if (InParameterList &&
4330 Current->endsSequence(TT_AttributeMacro,
4331 TT_PointerOrReference)) {
4332 Current->CanBreakBefore =
false;
4335 unsigned ChildSize = 0;
4338 ChildSize = LastOfChild.isTrailingComment() ? Style.ColumnLimit
4341 if (Current->MustBreakBefore || Prev->
Children.size() > 1 ||
4343 Prev->
Children[0]->First->MustBreakBefore) ||
4344 Current->IsMultiline) {
4345 Current->TotalLength = Prev->
TotalLength + Style.ColumnLimit;
4347 Current->TotalLength = Prev->
TotalLength + Current->ColumnWidth +
4348 ChildSize + Current->SpacesRequiredBefore;
4356 Prev->
isOneOf(tok::l_paren, tok::l_brace,
4357 TT_ArrayInitializerLSquare) &&
4360 for (
auto *ParamTok = Current; ParamTok && ParamTok != RParen;
4361 ParamTok = ParamTok->Next) {
4364 ParamTok->MustBreakBefore =
true;
4365 ParamTok->CanBreakBefore =
true;
4367 if (ParamTok->opensScope())
4368 ParamTok = ParamTok->MatchingParen;
4372 if (Current->is(TT_ControlStatementLBrace)) {
4373 if (Style.ColumnLimit > 0 &&
4374 Style.BraceWrapping.AfterControlStatement ==
4376 Line.Level * Style.IndentWidth +
Line.Last->TotalLength >
4377 Style.ColumnLimit) {
4378 Current->CanBreakBefore =
true;
4379 Current->MustBreakBefore =
true;
4381 }
else if (Current->is(TT_CtorInitializerColon)) {
4382 InFunctionDecl =
false;
4394 Current->SplitPenalty = splitPenalty(
Line, *Current, InFunctionDecl);
4396 Current->is(TT_SelectorName) && Current->ParameterIndex > 0) {
4397 if (Current->ParameterIndex == 1)
4398 Current->SplitPenalty += 5 * Current->BindingStrength;
4400 Current->SplitPenalty += 20 * Current->BindingStrength;
4404 calculateUnbreakableTailLengths(
Line);
4406 for (
auto *Current =
First; Current; Current = Current->Next) {
4408 Current->Role->precomputeFormattingInfos(Current);
4409 if (Current->MatchingParen &&
4410 Current->MatchingParen->opensBlockOrBlockTypeList(Style) &&
4415 if (Current->opensBlockOrBlockTypeList(Style))
4419 LLVM_DEBUG({ printDebugInfo(
Line); });
4422void TokenAnnotator::calculateUnbreakableTailLengths(
4429 Current->
isOneOf(tok::comment, tok::string_literal)) {
4439void TokenAnnotator::calculateArrayInitializerColumnList(
4443 auto *CurrentToken =
Line.First;
4444 CurrentToken->ArrayInitializerLineStart =
true;
4446 while (CurrentToken && CurrentToken !=
Line.Last) {
4447 if (CurrentToken->is(tok::l_brace)) {
4448 CurrentToken->IsArrayInitializer =
true;
4449 if (CurrentToken->Next)
4450 CurrentToken->Next->MustBreakBefore =
true;
4452 calculateInitializerColumnList(
Line, CurrentToken->Next, Depth + 1);
4454 CurrentToken = CurrentToken->Next;
4459FormatToken *TokenAnnotator::calculateInitializerColumnList(
4461 while (CurrentToken && CurrentToken !=
Line.Last) {
4462 if (CurrentToken->is(tok::l_brace))
4464 else if (CurrentToken->is(tok::r_brace))
4466 if (Depth == 2 && CurrentToken->isOneOf(tok::l_brace, tok::comma)) {
4467 CurrentToken = CurrentToken->Next;
4470 CurrentToken->StartsColumn =
true;
4471 CurrentToken = CurrentToken->Previous;
4473 CurrentToken = CurrentToken->Next;
4475 return CurrentToken;
4480 bool InFunctionDecl)
const {
4484 if (
Left.is(tok::semi))
4488 if (Style.isJava()) {
4489 if (
Right.isOneOf(Keywords.kw_extends, Keywords.kw_throws))
4491 if (
Right.is(Keywords.kw_implements))
4493 if (
Left.is(tok::comma) &&
Left.NestingLevel == 0)
4495 }
else if (Style.isJavaScript()) {
4496 if (
Right.is(Keywords.kw_function) &&
Left.isNot(tok::comma))
4498 if (
Left.is(TT_JsTypeColon))
4500 if ((
Left.is(TT_TemplateString) &&
Left.TokenText.ends_with(
"${")) ||
4501 (
Right.is(TT_TemplateString) &&
Right.TokenText.starts_with(
"}"))) {
4505 if (
Left.opensScope() &&
Right.closesScope())
4508 if (
Right.is(tok::l_square))
4510 if (
Right.is(tok::period))
4514 if (
Right.is(tok::identifier) &&
Right.Next &&
Right.Next->is(TT_DictLiteral))
4516 if (
Right.is(tok::l_square)) {
4517 if (
Left.is(tok::r_square))
4520 if (
Right.is(TT_LambdaLSquare) &&
Left.is(tok::equal))
4522 if (
Right.isNoneOf(TT_ObjCMethodExpr, TT_LambdaLSquare,
4523 TT_ArrayInitializerLSquare,
4524 TT_DesignatedInitializerLSquare, TT_AttributeLSquare)) {
4529 if (
Left.is(tok::coloncolon))
4530 return Style.PenaltyBreakScopeResolution;
4531 if (
Right.isOneOf(TT_StartOfName, TT_FunctionDeclarationName,
4532 tok::kw_operator)) {
4533 if (
Line.startsWith(tok::kw_for) &&
Right.PartOfMultiVariableDeclStmt)
4535 if (
Left.is(TT_StartOfName))
4537 if (InFunctionDecl &&
Right.NestingLevel == 0)
4538 return Style.PenaltyReturnTypeOnItsOwnLine;
4541 if (
Right.is(TT_PointerOrReference))
4543 if (
Right.is(TT_LambdaArrow))
4545 if (
Left.is(tok::equal) &&
Right.is(tok::l_brace))
4547 if (
Left.is(TT_CastRParen))
4549 if (
Left.isOneOf(tok::kw_class, tok::kw_struct, tok::kw_union))
4551 if (
Left.is(tok::comment))
4554 if (
Left.isOneOf(TT_RangeBasedForLoopColon, TT_InheritanceColon,
4555 TT_CtorInitializerColon)) {
4559 if (
Right.isMemberAccess()) {
4580 const auto Penalty = Style.PenaltyBreakBeforeMemberAccess;
4582 ? std::min(Penalty, 35u)
4586 if (
Right.is(TT_TrailingAnnotation) &&
4587 (!
Right.Next ||
Right.Next->isNot(tok::l_paren))) {
4590 if (
Line.startsWith(TT_ObjCMethodSpecifier))
4597 bool is_short_annotation =
Right.TokenText.size() < 10;
4598 return (
Left.is(tok::r_paren) ? 100 : 120) + (is_short_annotation ? 50 : 0);
4602 if (
Line.startsWith(tok::kw_for) &&
Left.is(tok::equal))
4607 if (
Right.is(TT_SelectorName))
4609 if (
Left.is(tok::colon)) {
4610 if (
Left.is(TT_ObjCMethodExpr))
4611 return Line.MightBeFunctionDecl ? 50 : 500;
4612 if (
Left.is(TT_ObjCSelector))
4620 Left.Previous->isOneOf(tok::identifier, tok::greater)) {
4624 if (
Left.is(tok::l_paren) && Style.PenaltyBreakOpenParenthesis != 0)
4625 return Style.PenaltyBreakOpenParenthesis;
4626 if (
Left.is(tok::l_paren) && InFunctionDecl && Style.AlignAfterOpenBracket)
4628 if (
Left.is(tok::l_paren) &&
Left.Previous &&
4629 (
Left.Previous->isOneOf(tok::kw_for, tok::kw__Generic) ||
4630 Left.Previous->isIf())) {
4633 if (
Left.is(tok::equal) && InFunctionDecl)
4635 if (
Right.is(tok::r_brace))
4637 if (
Left.is(TT_TemplateOpener))
4639 if (
Left.opensScope()) {
4643 if (!Style.AlignAfterOpenBracket &&
4644 (
Left.ParameterCount <= 1 || Style.AllowAllArgumentsOnNextLine)) {
4647 if (
Left.is(tok::l_brace) &&
4651 return Left.ParameterCount > 1 ? Style.PenaltyBreakBeforeFirstCallParameter
4654 if (
Left.is(TT_JavaAnnotation))
4657 if (
Left.is(TT_UnaryOperator))
4659 if (
Left.isOneOf(tok::plus, tok::comma) &&
Left.Previous &&
4660 Left.Previous->isLabelString() &&
4661 (
Left.NextOperator ||
Left.OperatorIndex != 0)) {
4664 if (
Right.is(tok::plus) &&
Left.isLabelString() &&
4665 (
Right.NextOperator ||
Right.OperatorIndex != 0)) {
4668 if (
Left.is(tok::comma))
4670 if (
Right.is(tok::lessless) &&
Left.isLabelString() &&
4671 (
Right.NextOperator ||
Right.OperatorIndex != 1)) {
4674 if (
Right.is(tok::lessless)) {
4676 if (
Left.isNot(tok::r_paren) ||
Right.OperatorIndex > 0) {
4682 if (
Left.ClosesTemplateDeclaration)
4683 return Style.PenaltyBreakTemplateDeclaration;
4684 if (
Left.ClosesRequiresClause)
4686 if (
Left.is(TT_ConditionalExpr))
4692 return Style.PenaltyBreakAssignment;
4699bool TokenAnnotator::spaceRequiredBeforeParens(
const FormatToken &Right)
const {
4702 if (
Right.is(TT_OverloadedOperatorLParen) &&
4703 Style.SpaceBeforeParensOptions.AfterOverloadedOperator) {
4706 if (Style.SpaceBeforeParensOptions.BeforeNonEmptyParentheses &&
4707 Right.ParameterCount > 0) {
4716 if (
Left.is(tok::kw_return) &&
4717 Right.isNoneOf(tok::semi, tok::r_paren, tok::hashhash)) {
4720 if (
Left.is(tok::kw_throw) &&
Right.is(tok::l_paren) &&
Right.MatchingParen &&
4721 Right.MatchingParen->is(TT_CastRParen)) {
4724 if (
Left.is(Keywords.kw_assert) && Style.isJava())
4727 Left.is(tok::objc_property)) {
4730 if (
Right.is(tok::hashhash))
4731 return Left.is(tok::hash);
4732 if (
Left.isOneOf(tok::hashhash, tok::hash))
4733 return Right.is(tok::hash);
4735 if (
Left.is(tok::l_paren) &&
Right.is(tok::r_paren))
4736 return Style.SpacesInParensOptions.InEmptyParentheses;
4737 if (Style.SpacesInParensOptions.ExceptDoubleParentheses &&
4738 Left.is(tok::r_paren) &&
Right.is(tok::r_paren)) {
4739 auto *InnerLParen =
Left.MatchingParen;
4740 if (InnerLParen && InnerLParen->Previous ==
Right.MatchingParen) {
4741 InnerLParen->SpacesRequiredBefore = 0;
4746 if (
Left.is(tok::l_paren))
4748 else if (
Right.is(tok::r_paren) &&
Right.MatchingParen)
4749 LeftParen =
Right.MatchingParen;
4750 if (LeftParen && (LeftParen->is(TT_ConditionLParen) ||
4751 (LeftParen->Previous &&
4752 isKeywordWithCondition(*LeftParen->Previous)))) {
4753 return Style.SpacesInParensOptions.InConditionalStatements;
4758 if (
Left.is(tok::kw_auto) &&
Right.isOneOf(TT_LambdaLBrace, TT_FunctionLBrace,
4760 TT_FunctionTypeLParen)) {
4765 if (
Left.is(tok::kw_auto) &&
Right.isOneOf(tok::l_paren, tok::l_brace))
4768 const auto *BeforeLeft =
Left.Previous;
4771 if (
Right.is(tok::l_paren) &&
Left.is(tok::kw_co_await) && BeforeLeft &&
4772 BeforeLeft->is(tok::kw_operator)) {
4776 if (
Left.isOneOf(tok::kw_co_await, tok::kw_co_yield, tok::kw_co_return) &&
4777 Right.isNoneOf(tok::semi, tok::r_paren)) {
4781 if (
Left.is(tok::l_paren) ||
Right.is(tok::r_paren)) {
4782 return (
Right.is(TT_CastRParen) ||
4783 (
Left.MatchingParen &&
Left.MatchingParen->is(TT_CastRParen)))
4784 ? Style.SpacesInParensOptions.InCStyleCasts
4785 : Style.SpacesInParensOptions.Other;
4787 if (
Right.isOneOf(tok::semi, tok::comma))
4790 bool IsLightweightGeneric =
Right.MatchingParen &&
4791 Right.MatchingParen->Next &&
4792 Right.MatchingParen->Next->is(tok::colon);
4793 return !IsLightweightGeneric && Style.ObjCSpaceBeforeProtocolList;
4795 if (
Right.is(tok::less) &&
Left.is(tok::kw_template))
4796 return Style.SpaceAfterTemplateKeyword;
4797 if (
Left.isOneOf(tok::exclaim, tok::tilde))
4799 if (
Left.is(tok::at) &&
4800 Right.isOneOf(tok::identifier, tok::string_literal, tok::char_constant,
4801 tok::numeric_constant, tok::l_paren, tok::l_brace,
4802 tok::kw_true, tok::kw_false)) {
4805 if (
Left.is(tok::colon))
4806 return Left.isNoneOf(TT_ObjCSelector, TT_ObjCMethodExpr);
4807 if (
Left.is(tok::coloncolon))
4809 if (
Left.is(tok::less) ||
Right.isOneOf(tok::greater, tok::less)) {
4810 if (Style.isTextProto() ||
4812 (
Left.is(TT_DictLiteral) ||
Right.is(TT_DictLiteral)))) {
4814 if (
Left.is(tok::less) &&
Right.is(tok::greater))
4819 if (
Right.isNot(TT_OverloadedOperatorLParen))
4822 if (
Right.is(tok::ellipsis)) {
4823 return Left.Tok.isLiteral() || (
Left.is(tok::identifier) && BeforeLeft &&
4824 BeforeLeft->is(tok::kw_case));
4826 if (
Left.is(tok::l_square) &&
Right.is(tok::amp))
4827 return Style.SpacesInSquareBrackets;
4828 if (
Right.is(TT_PointerOrReference)) {
4829 if (
Left.is(tok::r_paren) &&
Line.MightBeFunctionDecl) {
4830 if (!
Left.MatchingParen)
4833 Left.MatchingParen->getPreviousNonComment();
4834 if (!TokenBeforeMatchingParen ||
Left.isNot(TT_TypeDeclarationParen))
4842 (
Left.is(TT_AttributeRParen) ||
4843 Left.canBePointerOrReferenceQualifier())) {
4846 if (
Left.Tok.isLiteral())
4849 if (
Left.isTypeOrIdentifier(LangOpts) &&
Right.Next &&
Right.Next->Next &&
4850 Right.Next->Next->is(TT_RangeBasedForLoopColon)) {
4851 return getTokenPointerOrReferenceAlignment(Right) !=
4854 return Left.isNoneOf(TT_PointerOrReference, tok::l_paren) &&
4855 (getTokenPointerOrReferenceAlignment(Right) !=
4857 (
Line.IsMultiVariableDeclStmt &&
4858 (
Left.NestingLevel == 0 ||
4859 (
Left.NestingLevel == 1 && startsWithInitStatement(
Line)))));
4861 if (
Right.is(TT_FunctionTypeLParen) &&
Left.isNot(tok::l_paren) &&
4862 (
Left.isNot(TT_PointerOrReference) ||
4864 !
Line.IsMultiVariableDeclStmt))) {
4867 if (
Left.is(TT_PointerOrReference)) {
4872 Right.canBePointerOrReferenceQualifier()) {
4876 if (
Right.Tok.isLiteral())
4879 if (
Right.is(TT_BlockComment))
4883 if (
Right.isOneOf(Keywords.kw_override, Keywords.kw_final, tok::kw_noexcept,
4884 TT_RequiresClause) &&
4885 Right.isNot(TT_StartOfName)) {
4892 if (BeforeLeft && BeforeLeft->isTypeOrIdentifier(LangOpts) &&
Right.Next &&
4893 Right.Next->is(TT_RangeBasedForLoopColon)) {
4894 return getTokenPointerOrReferenceAlignment(Left) !=
4897 if (
Right.isOneOf(TT_PointerOrReference, TT_ArraySubscriptLSquare,
4909 if (
Line.IsMultiVariableDeclStmt &&
4910 (
Left.NestingLevel ==
Line.First->NestingLevel ||
4911 ((
Left.NestingLevel ==
Line.First->NestingLevel + 1) &&
4912 startsWithInitStatement(
Line)))) {
4917 if (BeforeLeft->is(tok::coloncolon)) {
4918 if (
Left.isNot(tok::star))
4921 if (!
Right.startsSequence(tok::identifier, tok::r_paren))
4924 const auto *LParen =
Right.Next->MatchingParen;
4925 return !LParen || LParen->isNot(TT_FunctionTypeLParen);
4927 return BeforeLeft->isNoneOf(tok::l_paren, tok::l_square);
4930 if (
Left.is(tok::ellipsis) && BeforeLeft &&
4931 BeforeLeft->isPointerOrReference()) {
4935 if (
Right.is(tok::star) &&
Left.is(tok::l_paren))
4937 if (
Left.is(tok::star) &&
Right.isPointerOrReference())
4939 if (
Right.isPointerOrReference()) {
4950 if (
Previous->is(tok::coloncolon)) {
4969 if (
Previous->endsSequence(tok::kw_operator))
4971 if (
Previous->isOneOf(tok::kw_const, tok::kw_volatile)) {
4973 (Style.SpaceAroundPointerQualifiers ==
4979 if (Style.isCSharp() &&
Left.is(Keywords.kw_is) &&
Right.is(tok::l_square))
4981 const auto SpaceRequiredForArrayInitializerLSquare =
4982 [](
const FormatToken &LSquareTok,
const FormatStyle &Style) {
4983 return Style.SpacesInContainerLiterals ||
4986 LSquareTok.endsSequence(tok::l_square, tok::colon,
4989 if (
Left.is(tok::l_square)) {
4990 return (
Left.is(TT_ArrayInitializerLSquare) &&
Right.isNot(tok::r_square) &&
4991 SpaceRequiredForArrayInitializerLSquare(Left, Style)) ||
4992 (
Left.isOneOf(TT_ArraySubscriptLSquare, TT_StructuredBindingLSquare,
4993 TT_LambdaLSquare) &&
4994 Style.SpacesInSquareBrackets &&
Right.isNot(tok::r_square));
4996 if (
Right.is(tok::r_square)) {
4997 return Right.MatchingParen &&
4998 ((
Right.MatchingParen->is(TT_ArrayInitializerLSquare) &&
4999 SpaceRequiredForArrayInitializerLSquare(*
Right.MatchingParen,
5001 (Style.SpacesInSquareBrackets &&
5002 Right.MatchingParen->isOneOf(TT_ArraySubscriptLSquare,
5003 TT_StructuredBindingLSquare,
5004 TT_LambdaLSquare)));
5006 if (
Right.is(tok::l_square) &&
5007 Right.isNoneOf(TT_ObjCMethodExpr, TT_LambdaLSquare,
5008 TT_DesignatedInitializerLSquare,
5009 TT_StructuredBindingLSquare, TT_AttributeLSquare) &&
5010 Left.isNoneOf(tok::numeric_constant, TT_DictLiteral) &&
5011 !(
Left.isNot(tok::r_square) && Style.SpaceBeforeSquareBrackets &&
5012 Right.is(TT_ArraySubscriptLSquare))) {
5016 (
Right.is(tok::r_brace) &&
Right.MatchingParen &&
5019 Style.SpacesInParensOptions.Other;
5021 if (
Left.is(TT_BlockComment)) {
5023 return Style.isJavaScript() || !
Left.TokenText.ends_with(
"=*/");
5028 if (
Left.is(TT_TemplateCloser) &&
Right.is(TT_AttributeLSquare))
5031 if (
Right.is(tok::l_paren)) {
5033 if (
Line.MightBeFunctionDecl &&
Right.is(TT_FunctionDeclarationLParen)) {
5034 if (spaceRequiredBeforeParens(Right))
5036 const auto &Options = Style.SpaceBeforeParensOptions;
5037 return Line.mightBeFunctionDefinition()
5038 ? Options.AfterFunctionDefinitionName
5039 : Options.AfterFunctionDeclarationName;
5041 if (
Left.is(TT_TemplateCloser) &&
Right.isNot(TT_FunctionTypeLParen))
5042 return spaceRequiredBeforeParens(Right);
5043 if (
Left.isOneOf(TT_RequiresClause,
5044 TT_RequiresClauseInARequiresExpression)) {
5045 return Style.SpaceBeforeParensOptions.AfterRequiresInClause ||
5046 spaceRequiredBeforeParens(Right);
5048 if (
Left.is(TT_RequiresExpression)) {
5049 return Style.SpaceBeforeParensOptions.AfterRequiresInExpression ||
5050 spaceRequiredBeforeParens(Right);
5052 if (
Left.isOneOf(TT_AttributeRParen, TT_AttributeRSquare))
5054 if (
Left.is(TT_ForEachMacro)) {
5055 return Style.SpaceBeforeParensOptions.AfterForeachMacros ||
5056 spaceRequiredBeforeParens(Right);
5058 if (
Left.is(TT_IfMacro)) {
5059 return Style.SpaceBeforeParensOptions.AfterIfMacros ||
5060 spaceRequiredBeforeParens(Right);
5063 Left.isPlacementOperator() &&
5064 Right.isNot(TT_OverloadedOperatorLParen) &&
5065 !(
Line.MightBeFunctionDecl &&
Left.is(TT_FunctionDeclarationName))) {
5066 const auto *RParen =
Right.MatchingParen;
5067 return Style.SpaceBeforeParensOptions.AfterPlacementOperator ||
5068 (RParen && RParen->is(TT_CastRParen));
5072 if (
Left.is(tok::semi))
5074 if (
Left.isOneOf(tok::pp_elif, tok::kw_for, tok::kw_while, tok::kw_switch,
5075 tok::kw_case, TT_ForEachMacro, TT_ObjCForIn) ||
5077 Right.is(TT_ConditionLParen)) {
5078 return Style.SpaceBeforeParensOptions.AfterControlStatements ||
5079 spaceRequiredBeforeParens(Right);
5084 if (
Right.is(TT_OverloadedOperatorLParen))
5085 return spaceRequiredBeforeParens(Right);
5089 Left.MatchingParen &&
Left.MatchingParen->is(TT_LambdaLSquare)) {
5090 return Style.SpaceBeforeParensOptions.AfterFunctionDefinitionName ||
5091 spaceRequiredBeforeParens(Right);
5093 if (!BeforeLeft || BeforeLeft->isNoneOf(tok::period, tok::arrow)) {
5094 if (
Left.isOneOf(tok::kw_try, Keywords.kw___except, tok::kw_catch)) {
5095 return Style.SpaceBeforeParensOptions.AfterControlStatements ||
5096 spaceRequiredBeforeParens(Right);
5098 if (
Left.isPlacementOperator() ||
5099 (
Left.is(tok::r_square) &&
Left.MatchingParen &&
5100 Left.MatchingParen->Previous &&
5101 Left.MatchingParen->Previous->is(tok::kw_delete))) {
5103 spaceRequiredBeforeParens(Right);
5107 if (
Tok.isNot(tok::l_paren))
5109 const auto *RParen =
Tok.MatchingParen;
5112 const auto *
Next = RParen->Next;
5119 (
Left.Tok.getIdentifierInfo() ||
Left.is(tok::r_paren))) {
5120 return spaceRequiredBeforeParens(Right);
5124 if (
Left.is(tok::at) &&
Right.isNot(tok::objc_not_keyword))
5126 if (
Right.is(TT_UnaryOperator)) {
5127 return Left.isNoneOf(tok::l_paren, tok::l_square, tok::at) &&
5128 (
Left.isNot(tok::colon) ||
Left.isNot(TT_ObjCMethodExpr));
5134 if (!Style.isVerilog() &&
5135 (
Left.isOneOf(tok::identifier, tok::greater, tok::r_square,
5137 Left.isTypeName(LangOpts)) &&
5138 Right.is(tok::l_brace) &&
Right.getNextNonComment() &&
5142 if (
Left.is(tok::period) ||
Right.is(tok::period))
5146 if (
Right.is(tok::hash) &&
Left.is(tok::identifier) &&
5147 (
Left.TokenText ==
"L" ||
Left.TokenText ==
"u" ||
5148 Left.TokenText ==
"U" ||
Left.TokenText ==
"u8" ||
5149 Left.TokenText ==
"LR" ||
Left.TokenText ==
"uR" ||
5150 Left.TokenText ==
"UR" ||
Left.TokenText ==
"u8R")) {
5153 if (
Left.is(TT_TemplateCloser) &&
Left.MatchingParen &&
5154 Left.MatchingParen->Previous &&
5155 Left.MatchingParen->Previous->isOneOf(tok::period, tok::coloncolon)) {
5161 if (
Left.is(TT_TemplateCloser) &&
Right.is(tok::l_square))
5163 if (
Left.is(tok::l_brace) &&
Left.endsSequence(TT_DictLiteral, tok::at)) {
5167 if (
Right.is(tok::r_brace) &&
Right.MatchingParen &&
5168 Right.MatchingParen->endsSequence(TT_DictLiteral, tok::at)) {
5172 if (
Right.is(TT_TrailingAnnotation) &&
Right.isOneOf(tok::amp, tok::ampamp) &&
5173 Left.isOneOf(tok::kw_const, tok::kw_volatile) &&
5174 (!
Right.Next ||
Right.Next->is(tok::semi))) {
5191 return Right.hasWhitespaceBefore();
5193 const bool IsVerilog = Style.isVerilog();
5194 assert(!IsVerilog || !IsCpp);
5197 if (Keywords.isWordLike(Right, IsVerilog) &&
5198 Keywords.isWordLike(Left, IsVerilog)) {
5204 if (
Left.is(tok::star) &&
Right.is(tok::comment))
5207 if (
Left.is(tok::l_brace) &&
Right.is(tok::r_brace) &&
5208 Left.Children.empty()) {
5213 Style.SpacesInParensOptions.InEmptyParentheses;
5218 const auto *BeforeLeft =
Left.Previous;
5221 if (
Left.is(TT_OverloadedOperator) &&
5222 Right.isOneOf(TT_TemplateOpener, TT_TemplateCloser)) {
5226 if (
Right.is(tok::period) &&
Left.is(tok::numeric_constant))
5230 if (
Left.is(Keywords.kw_import) &&
5231 Right.isOneOf(tok::less, tok::ellipsis) &&
5232 (!BeforeLeft || BeforeLeft->is(tok::kw_export))) {
5236 if (
Left.is(Keywords.kw_import) &&
Right.is(TT_ModulePartitionColon))
5239 if (
Right.is(TT_AfterPPDirective))
5243 if (
Left.is(tok::identifier) &&
Right.is(TT_ModulePartitionColon))
5246 if (
Left.is(TT_ModulePartitionColon) &&
Right.is(tok::identifier))
5248 if (
Left.is(tok::ellipsis) &&
Right.is(tok::identifier) &&
5249 Line.First->is(Keywords.kw_import)) {
5253 if (
Left.isOneOf(TT_AttributeRParen, TT_AttributeMacro) &&
5254 Right.is(tok::coloncolon)) {
5258 if (
Left.is(tok::kw_operator))
5259 return Right.is(tok::coloncolon) || Style.SpaceAfterOperatorKeyword;
5261 !
Left.opensScope() && Style.SpaceBeforeCpp11BracedList) {
5264 if (
Left.is(tok::less) &&
Left.is(TT_OverloadedOperator) &&
5265 Right.is(TT_TemplateOpener)) {
5269 if (
Left.is(tok::identifier) &&
Right.is(tok::numeric_constant))
5270 return Right.TokenText[0] !=
'.';
5272 if (
Left.Tok.getIdentifierInfo() &&
Right.Tok.isLiteral())
5274 }
else if (Style.isProto()) {
5275 if (
Right.is(tok::period) && !(BeforeLeft && BeforeLeft->is(tok::period)) &&
5276 Left.isOneOf(Keywords.kw_optional, Keywords.kw_required,
5277 Keywords.kw_repeated, Keywords.kw_extend)) {
5280 if (
Right.is(tok::l_paren) &&
5281 Left.isOneOf(Keywords.kw_returns, Keywords.kw_option)) {
5284 if (
Right.isOneOf(tok::l_brace, tok::less) &&
Left.is(TT_SelectorName))
5287 if (
Left.is(tok::slash) ||
Right.is(tok::slash))
5289 if (
Left.MatchingParen &&
5290 Left.MatchingParen->is(TT_ProtoExtensionLSquare) &&
5291 Right.isOneOf(tok::l_brace, tok::less)) {
5295 if (
Left.is(tok::percent))
5299 if (
Left.is(tok::numeric_constant) &&
Right.is(tok::percent))
5300 return Right.hasWhitespaceBefore();
5301 }
else if (Style.isJson()) {
5302 if (
Right.is(tok::colon) &&
Left.is(tok::string_literal))
5303 return Style.SpaceBeforeJsonColon;
5304 }
else if (Style.isCSharp()) {
5310 if (
Left.is(tok::kw_this) &&
Right.is(tok::l_square))
5314 if (
Left.is(tok::kw_new) &&
Right.is(tok::l_paren))
5318 if (
Right.is(tok::l_brace))
5322 if (
Left.is(tok::l_brace) &&
Right.isNot(tok::r_brace))
5325 if (
Left.isNot(tok::l_brace) &&
Right.is(tok::r_brace))
5329 if (
Left.is(TT_FatArrow) ||
Right.is(TT_FatArrow))
5333 if (
Left.is(TT_AttributeColon) ||
Right.is(TT_AttributeColon))
5337 if (
Left.is(TT_TemplateCloser) &&
Right.is(TT_StartOfName))
5341 if (
Left.is(tok::l_square) ||
Right.is(tok::r_square))
5342 return Style.SpacesInSquareBrackets;
5345 if (
Right.is(TT_CSharpNullable))
5349 if (
Right.is(TT_NonNullAssertion))
5353 if (
Left.is(tok::comma) &&
Right.is(tok::comma))
5357 if (
Left.is(Keywords.kw_var) &&
Right.is(tok::l_paren))
5361 if (
Right.is(tok::l_paren)) {
5362 if (
Left.isOneOf(tok::kw_using, Keywords.kw_async, Keywords.kw_when,
5363 Keywords.kw_lock)) {
5364 return Style.SpaceBeforeParensOptions.AfterControlStatements ||
5365 spaceRequiredBeforeParens(Right);
5371 if ((
Left.isAccessSpecifierKeyword() ||
5372 Left.isOneOf(tok::kw_virtual, tok::kw_extern, tok::kw_static,
5373 Keywords.kw_internal, Keywords.kw_abstract,
5374 Keywords.kw_sealed, Keywords.kw_override,
5375 Keywords.kw_async, Keywords.kw_unsafe)) &&
5376 Right.is(tok::l_paren)) {
5379 }
else if (Style.isJavaScript()) {
5380 if (
Left.is(TT_FatArrow))
5383 if (
Right.is(tok::l_paren) &&
Left.is(Keywords.kw_await) && BeforeLeft &&
5384 BeforeLeft->is(tok::kw_for)) {
5387 if (
Left.is(Keywords.kw_async) &&
Right.is(tok::l_paren) &&
5388 Right.MatchingParen) {
5395 if ((
Left.is(TT_TemplateString) &&
Left.TokenText.ends_with(
"${")) ||
5396 (
Right.is(TT_TemplateString) &&
Right.TokenText.starts_with(
"}"))) {
5401 if (Keywords.isJavaScriptIdentifier(Left,
5403 Right.is(TT_TemplateString)) {
5406 if (
Right.is(tok::star) &&
5407 Left.isOneOf(Keywords.kw_function, Keywords.kw_yield)) {
5410 if (
Right.isOneOf(tok::l_brace, tok::l_square) &&
5411 Left.isOneOf(Keywords.kw_function, Keywords.kw_yield,
5412 Keywords.kw_extends, Keywords.kw_implements)) {
5415 if (
Right.is(tok::l_paren)) {
5417 if (
Line.MustBeDeclaration &&
Left.Tok.getIdentifierInfo())
5421 if (BeforeLeft && BeforeLeft->is(tok::period) &&
5422 Left.Tok.getIdentifierInfo()) {
5426 if (
Left.isOneOf(tok::kw_throw, Keywords.kw_await, Keywords.kw_typeof,
5432 if (
Left.endsSequence(tok::kw_const, Keywords.kw_as))
5434 if ((
Left.isOneOf(Keywords.kw_let, Keywords.kw_var, Keywords.kw_in,
5439 (
Left.is(Keywords.kw_of) && BeforeLeft &&
5440 BeforeLeft->isOneOf(tok::identifier, tok::r_square, tok::r_brace))) &&
5441 (!BeforeLeft || BeforeLeft->isNot(tok::period))) {
5444 if (
Left.isOneOf(tok::kw_for, Keywords.kw_as) && BeforeLeft &&
5445 BeforeLeft->is(tok::period) &&
Right.is(tok::l_paren)) {
5448 if (
Left.is(Keywords.kw_as) &&
5449 Right.isOneOf(tok::l_square, tok::l_brace, tok::l_paren)) {
5452 if (
Left.is(tok::kw_default) && BeforeLeft &&
5453 BeforeLeft->is(tok::kw_export)) {
5456 if (
Left.is(Keywords.kw_is) &&
Right.is(tok::l_brace))
5458 if (
Right.isOneOf(TT_JsTypeColon, TT_JsTypeOptionalQuestion))
5460 if (
Left.is(TT_JsTypeOperator) ||
Right.is(TT_JsTypeOperator))
5462 if ((
Left.is(tok::l_brace) ||
Right.is(tok::r_brace)) &&
5463 Line.First->isOneOf(Keywords.kw_import, tok::kw_export)) {
5466 if (
Left.is(tok::ellipsis))
5468 if (
Left.is(TT_TemplateCloser) &&
5469 Right.isNoneOf(tok::equal, tok::l_brace, tok::comma, tok::l_square,
5470 Keywords.kw_implements, Keywords.kw_extends)) {
5476 if (
Right.is(TT_NonNullAssertion))
5478 if (
Left.is(TT_NonNullAssertion) &&
5479 Right.isOneOf(Keywords.kw_as, Keywords.kw_in)) {
5482 }
else if (Style.isJava()) {
5483 if (
Left.is(TT_CaseLabelArrow) ||
Right.is(TT_CaseLabelArrow))
5485 if (
Left.is(tok::r_square) &&
Right.is(tok::l_brace))
5488 if (
Left.is(tok::l_square) ||
Right.is(tok::r_square))
5489 return Style.SpacesInSquareBrackets;
5491 if (
Left.is(Keywords.kw_synchronized) &&
Right.is(tok::l_paren)) {
5492 return Style.SpaceBeforeParensOptions.AfterControlStatements ||
5493 spaceRequiredBeforeParens(Right);
5495 if ((
Left.isAccessSpecifierKeyword() ||
5496 Left.isOneOf(tok::kw_static, Keywords.kw_final, Keywords.kw_abstract,
5497 Keywords.kw_native)) &&
5498 Right.is(TT_TemplateOpener)) {
5501 }
else if (IsVerilog) {
5503 if (
Left.is(tok::identifier) &&
Left.TokenText[0] ==
'\\')
5507 if ((
Left.is(TT_VerilogTableItem) &&
5508 Right.isNoneOf(tok::r_paren, tok::semi)) ||
5509 (
Right.is(TT_VerilogTableItem) &&
Left.isNot(tok::l_paren))) {
5511 return !(
Next &&
Next->is(tok::r_paren));
5514 if (
Left.isNot(TT_BinaryOperator) &&
5515 Left.isOneOf(Keywords.kw_verilogHash, Keywords.kw_verilogHashHash)) {
5519 if (
Right.isNot(tok::semi) &&
5520 (
Left.endsSequence(tok::numeric_constant, Keywords.kw_verilogHash) ||
5521 Left.endsSequence(tok::numeric_constant,
5522 Keywords.kw_verilogHashHash) ||
5523 (
Left.is(tok::r_paren) &&
Left.MatchingParen &&
5524 Left.MatchingParen->endsSequence(tok::l_paren, tok::at)))) {
5529 if (
Left.is(Keywords.kw_apostrophe) ||
5530 (
Left.is(TT_VerilogNumberBase) &&
Right.is(tok::numeric_constant))) {
5534 if (
Left.is(tok::arrow) ||
Right.is(tok::arrow))
5539 if (
Left.is(tok::at) &&
Right.isOneOf(tok::l_paren, tok::star, tok::at))
5542 if (
Right.is(tok::l_square) &&
5543 Left.isOneOf(TT_VerilogDimensionedTypeName, Keywords.kw_function)) {
5547 if (
Right.isOneOf(tok::period, Keywords.kw_apostrophe) &&
5548 Keywords.isVerilogIdentifier(Left) &&
Left.getPreviousNonComment() &&
5549 Left.getPreviousNonComment()->is(Keywords.kw_tagged)) {
5555 if ((
Right.is(Keywords.kw_apostrophe) ||
5557 Left.isNoneOf(Keywords.kw_assign, Keywords.kw_unique) &&
5558 !Keywords.isVerilogWordOperator(Left) &&
5559 (
Left.isOneOf(tok::r_square, tok::r_paren, tok::r_brace,
5560 tok::numeric_constant) ||
5561 Keywords.isWordLike(Left))) {
5565 if ((
Right.is(tok::star) &&
Left.is(tok::coloncolon)) ||
5566 (
Left.is(tok::star) &&
Right.is(tok::semi))) {
5570 if (
Left.endsSequence(tok::star, tok::l_paren) &&
Right.is(tok::identifier))
5573 if (
Right.is(tok::colon) &&
5574 (
Right.TokenText ==
":=" ||
Right.TokenText ==
":/")) {
5578 if (
Right.is(tok::l_paren) &&
Right.is(TT_VerilogStrength))
5581 if ((
Left.is(tok::l_brace) &&
5582 Right.isOneOf(tok::lessless, tok::greatergreater)) ||
5583 (
Left.endsSequence(tok::lessless, tok::l_brace) ||
5584 Left.endsSequence(tok::greatergreater, tok::l_brace))) {
5587 }
else if (Style.isTableGen()) {
5589 if (
Left.is(tok::l_square) &&
Right.is(tok::l_brace))
5591 if (
Left.is(tok::r_brace) &&
Right.is(tok::r_square))
5594 if (
Right.isOneOf(TT_TableGenDAGArgListColon,
5595 TT_TableGenDAGArgListColonToAlign) ||
5596 Left.isOneOf(TT_TableGenDAGArgListColon,
5597 TT_TableGenDAGArgListColonToAlign)) {
5600 if (
Right.is(TT_TableGenCondOperatorColon))
5602 if (
Left.isOneOf(TT_TableGenDAGArgOperatorID,
5603 TT_TableGenDAGArgOperatorToBreak) &&
5604 Right.isNot(TT_TableGenDAGArgCloser)) {
5608 if (
Right.isOneOf(tok::l_paren, tok::less) &&
5609 Left.isOneOf(TT_TableGenBangOperator, TT_TableGenCondOperator)) {
5614 if (
Left.is(TT_TableGenTrailingPasteOperator) &&
5615 Right.isOneOf(tok::l_brace, tok::colon)) {
5619 if (
Left.is(tok::hash) ||
Right.is(tok::hash))
5622 if (Keywords.isTableGenDefinition(Left))
5626 if (
Left.is(TT_ImplicitStringLiteral))
5627 return Right.hasWhitespaceBefore();
5629 if (
Left.is(TT_ObjCMethodSpecifier))
5630 return Style.ObjCSpaceAfterMethodDeclarationPrefix;
5631 if (
Left.is(tok::r_paren) &&
Left.isNot(TT_AttributeRParen) &&
5632 canBeObjCSelectorComponent(Right)) {
5640 (
Right.is(tok::equal) ||
Left.is(tok::equal))) {
5644 if (
Right.isOneOf(TT_TrailingReturnArrow, TT_LambdaArrow) ||
5645 Left.isOneOf(TT_TrailingReturnArrow, TT_LambdaArrow)) {
5648 if (
Left.is(tok::comma) &&
Right.isNot(TT_OverloadedOperatorLParen) &&
5651 (
Left.Children.empty() || !
Left.MacroParent)) {
5654 if (
Right.is(tok::comma))
5656 if (
Right.is(TT_ObjCBlockLParen))
5658 if (
Right.is(TT_CtorInitializerColon))
5659 return Style.SpaceBeforeCtorInitializerColon;
5660 if (
Right.is(TT_InheritanceColon) && !Style.SpaceBeforeInheritanceColon)
5662 if (
Right.is(TT_EnumUnderlyingTypeColon) &&
5663 !Style.SpaceBeforeEnumUnderlyingTypeColon) {
5666 if (
Right.is(TT_RangeBasedForLoopColon) &&
5667 !Style.SpaceBeforeRangeBasedForLoopColon) {
5670 if (
Left.is(TT_BitFieldColon)) {
5674 if (
Right.is(tok::colon)) {
5675 if (
Right.is(TT_CaseLabelColon))
5676 return Style.SpaceBeforeCaseColon;
5677 if (
Right.is(TT_GotoLabelColon))
5680 if (!
Right.getNextNonComment())
5682 if (
Right.isOneOf(TT_ObjCSelector, TT_ObjCMethodExpr))
5684 if (
Left.is(tok::question))
5686 if (
Right.is(TT_InlineASMColon) &&
Left.is(tok::coloncolon))
5688 if (
Right.is(TT_DictLiteral))
5689 return Style.SpacesInContainerLiterals;
5690 if (
Right.is(TT_AttributeColon))
5692 if (
Right.is(TT_CSharpNamedArgumentColon))
5694 if (
Right.is(TT_GenericSelectionColon))
5696 if (
Right.is(TT_BitFieldColon)) {
5703 if ((
Left.isOneOf(tok::minus, tok::minusminus) &&
5704 Right.isOneOf(tok::minus, tok::minusminus)) ||
5705 (
Left.isOneOf(tok::plus, tok::plusplus) &&
5706 Right.isOneOf(tok::plus, tok::plusplus))) {
5709 if (
Left.is(TT_UnaryOperator)) {
5712 if (
Left.is(tok::amp) &&
Right.is(tok::r_square))
5713 return Style.SpacesInSquareBrackets;
5714 if (
Left.isNot(tok::exclaim))
5716 if (
Left.TokenText ==
"!")
5717 return Style.SpaceAfterLogicalNot;
5718 assert(
Left.TokenText ==
"not");
5719 return Right.isOneOf(tok::coloncolon, TT_UnaryOperator) ||
5720 (
Right.is(tok::l_paren) && Style.SpaceBeforeParensOptions.AfterNot);
5725 if (
Left.is(TT_CastRParen)) {
5726 return Style.SpaceAfterCStyleCast ||
5727 Right.isOneOf(TT_BinaryOperator, TT_SelectorName);
5730 auto ShouldAddSpacesInAngles = [
this, &
Right]() {
5734 return Right.hasWhitespaceBefore();
5738 if (
Left.is(tok::greater) &&
Right.is(tok::greater)) {
5739 if (Style.isTextProto() ||
5743 return Right.is(TT_TemplateCloser) &&
Left.is(TT_TemplateCloser) &&
5745 ShouldAddSpacesInAngles());
5747 if (
Right.isOneOf(tok::arrow, tok::arrowstar, tok::periodstar) ||
5748 Left.isOneOf(tok::arrow, tok::period, tok::arrowstar, tok::periodstar) ||
5749 (
Right.is(tok::period) &&
Right.isNot(TT_DesignatedInitializerPeriod))) {
5752 if (!Style.SpaceBeforeAssignmentOperators &&
Left.isNot(TT_TemplateCloser) &&
5756 if (Style.isJava() &&
Right.is(tok::coloncolon) &&
5757 Left.isOneOf(tok::identifier, tok::kw_this)) {
5760 if (
Right.is(tok::coloncolon) &&
Left.is(tok::identifier)) {
5762 return Left.isPossibleMacro(
true) &&
5763 Right.hasWhitespaceBefore();
5765 if (
Right.is(tok::coloncolon) &&
5766 Left.isNoneOf(tok::l_brace, tok::comment, tok::l_paren)) {
5768 return (
Left.is(TT_TemplateOpener) &&
5770 ShouldAddSpacesInAngles())) ||
5771 Left.isNoneOf(tok::l_paren, tok::r_paren, tok::l_square,
5772 tok::kw___super, TT_TemplateOpener,
5773 TT_TemplateCloser) ||
5774 (
Left.is(tok::l_paren) && Style.SpacesInParensOptions.Other);
5776 if ((
Left.is(TT_TemplateOpener)) != (
Right.is(TT_TemplateCloser)))
5777 return ShouldAddSpacesInAngles();
5778 if (
Left.is(tok::r_paren) &&
Left.isNot(TT_TypeDeclarationParen) &&
5779 Right.is(TT_PointerOrReference) &&
Right.isOneOf(tok::amp, tok::ampamp)) {
5783 if (
Right.is(TT_StructuredBindingLSquare)) {
5784 return Left.isNoneOf(tok::amp, tok::ampamp) ||
5788 if (
Right.Next &&
Right.Next->is(TT_StructuredBindingLSquare) &&
5789 Right.isOneOf(tok::amp, tok::ampamp)) {
5792 if ((
Right.is(TT_BinaryOperator) &&
Left.isNot(tok::l_paren)) ||
5793 (
Left.isOneOf(TT_BinaryOperator, TT_EnumEqual, TT_ConditionalExpr) &&
5794 Right.isNot(tok::r_paren))) {
5797 if (
Right.is(TT_TemplateOpener) &&
Left.is(tok::r_paren) &&
5798 Left.MatchingParen &&
5799 Left.MatchingParen->is(TT_OverloadedOperatorLParen)) {
5802 if (
Right.is(tok::less) &&
Left.isNot(tok::l_paren) &&
5806 if (
Right.is(TT_TrailingUnaryOperator))
5808 if (
Left.is(TT_RegexLiteral))
5810 return spaceRequiredBetween(
Line, Left, Right);
5816 Tok.isNoneOf(TT_ObjCBlockLBrace, TT_LambdaLBrace, TT_DictLiteral);
5821 return Tok.MatchingParen &&
Tok.MatchingParen->Next &&
5822 Tok.MatchingParen->Next->isOneOf(tok::comma, tok::r_paren,
5834 Tok.isNoneOf(TT_ObjCBlockLBrace, TT_DictLiteral);
5837bool TokenAnnotator::mustBreakBefore(AnnotatedLine &
Line,
5839 if (Right.NewlinesBefore > 1 && Style.MaxEmptyLinesToKeep > 0 &&
5840 (!Style.RemoveEmptyLinesInUnwrappedLines || &Right ==
Line.First)) {
5846 if (Style.BreakFunctionDeclarationParameters &&
Line.MightBeFunctionDecl &&
5847 !
Line.mightBeFunctionDefinition() && Left.MightBeFunctionDeclParen &&
5848 Left.ParameterCount > 0) {
5852 if (Style.BreakFunctionDefinitionParameters &&
Line.MightBeFunctionDecl &&
5853 Line.mightBeFunctionDefinition() &&
Left.MightBeFunctionDeclParen &&
5854 Left.ParameterCount > 0) {
5861 Line.MightBeFunctionDecl && !
Left.opensScope() &&
5866 const auto *BeforeLeft =
Left.Previous;
5867 const auto *AfterRight =
Right.Next;
5869 if (Style.isCSharp()) {
5870 if (
Left.is(TT_FatArrow) &&
Right.is(tok::l_brace) &&
5871 Style.BraceWrapping.AfterFunction) {
5874 if (
Right.is(TT_CSharpNamedArgumentColon) ||
5875 Left.is(TT_CSharpNamedArgumentColon)) {
5878 if (
Right.is(TT_CSharpGenericTypeConstraint))
5880 if (AfterRight && AfterRight->is(TT_FatArrow) &&
5881 (
Right.is(tok::numeric_constant) ||
5882 (
Right.is(tok::identifier) &&
Right.TokenText ==
"_"))) {
5887 if (
Left.is(TT_AttributeRSquare) &&
5888 (
Right.isAccessSpecifier(
false) ||
5889 Right.is(Keywords.kw_internal))) {
5893 if (
Left.is(TT_AttributeRSquare) &&
Right.is(TT_AttributeLSquare))
5895 }
else if (Style.isJavaScript()) {
5897 if (
Right.is(tok::string_literal) &&
Left.is(tok::plus) && BeforeLeft &&
5898 BeforeLeft->is(tok::string_literal)) {
5901 if (
Left.is(TT_DictLiteral) &&
Left.is(tok::l_brace) &&
Line.Level == 0 &&
5902 BeforeLeft && BeforeLeft->is(tok::equal) &&
5903 Line.First->isOneOf(tok::identifier, Keywords.kw_import, tok::kw_export,
5907 Line.First->isNoneOf(Keywords.kw_var, Keywords.kw_let)) {
5912 if (
Left.is(tok::l_brace) &&
Line.Level == 0 &&
5913 (
Line.startsWith(tok::kw_enum) ||
5914 Line.startsWith(tok::kw_const, tok::kw_enum) ||
5915 Line.startsWith(tok::kw_export, tok::kw_enum) ||
5916 Line.startsWith(tok::kw_export, tok::kw_const, tok::kw_enum))) {
5921 if (
Right.is(tok::r_brace) &&
Left.is(tok::l_brace) && BeforeLeft &&
5922 BeforeLeft->is(TT_FatArrow)) {
5924 switch (Style.AllowShortLambdasOnASingleLine) {
5930 return !
Left.Children.empty();
5934 return (
Left.NestingLevel == 0 &&
Line.Level == 0) &&
5935 !
Left.Children.empty();
5937 llvm_unreachable(
"Unknown FormatStyle::ShortLambdaStyle enum");
5940 if (
Right.is(tok::r_brace) &&
Left.is(tok::l_brace) &&
5941 !
Left.Children.empty()) {
5943 if (
Left.NestingLevel == 0 &&
Line.Level == 0)
5944 return !Style.AllowShortFunctionsOnASingleLine.Other;
5946 return !Style.AllowShortFunctionsOnASingleLine.Inline;
5948 }
else if (Style.isJava()) {
5949 if (
Right.is(tok::plus) &&
Left.is(tok::string_literal) && AfterRight &&
5950 AfterRight->is(tok::string_literal)) {
5953 }
else if (Style.isVerilog()) {
5955 if (
Left.is(TT_VerilogAssignComma))
5958 if (
Left.is(TT_VerilogTypeComma))
5962 if (Style.VerilogBreakBetweenInstancePorts &&
5963 (
Left.is(TT_VerilogInstancePortComma) ||
5964 (
Left.is(tok::r_paren) && Keywords.isVerilogIdentifier(Right) &&
5965 Left.MatchingParen &&
5966 Left.MatchingParen->is(TT_VerilogInstancePortLParen)))) {
5971 if (!Keywords.isVerilogBegin(Right) && Keywords.isVerilogEndOfLabel(Left))
5973 }
else if (Style.BreakAdjacentStringLiterals &&
5974 (IsCpp || Style.isProto() || Style.isTableGen())) {
5975 if (
Left.isStringLiteral() &&
Right.isStringLiteral())
5980 if (Style.isJson()) {
5984 if (
Left.is(TT_DictLiteral) &&
Left.is(tok::l_brace))
5987 if ((
Left.is(TT_ArrayInitializerLSquare) &&
Left.is(tok::l_square) &&
5988 Right.isNot(tok::r_square)) ||
5989 Left.is(tok::comma)) {
5990 if (
Right.is(tok::l_brace))
5995 if (
Tok->isOneOf(tok::l_brace, tok::l_square))
5997 if (
Tok->isOneOf(tok::r_brace, tok::r_square))
6000 return Style.BreakArrays;
6002 }
else if (Style.isTableGen()) {
6006 if (
Left.is(TT_TableGenCondOperatorComma))
6008 if (
Left.is(TT_TableGenDAGArgOperatorToBreak) &&
6009 Right.isNot(TT_TableGenDAGArgCloser)) {
6012 if (
Left.is(TT_TableGenDAGArgListCommaToBreak))
6014 if (
Right.is(TT_TableGenDAGArgCloser) &&
Right.MatchingParen &&
6015 Right.MatchingParen->is(TT_TableGenDAGArgOpenerToBreak) &&
6016 &Left !=
Right.MatchingParen->Next) {
6022 if (
Line.startsWith(tok::kw_asm) &&
Right.is(TT_InlineASMColon) &&
6033 if ((
Left.isOneOf(tok::l_brace, TT_ArrayInitializerLSquare) ||
6034 (Style.isJavaScript() &&
Left.is(tok::l_paren))) &&
6036 BeforeClosingBrace =
Left.MatchingParen->Previous;
6037 }
else if (
Right.MatchingParen &&
6038 (
Right.MatchingParen->isOneOf(tok::l_brace,
6039 TT_ArrayInitializerLSquare) ||
6040 (Style.isJavaScript() &&
6041 Right.MatchingParen->is(tok::l_paren)))) {
6042 BeforeClosingBrace = &
Left;
6044 if (BeforeClosingBrace && (BeforeClosingBrace->is(tok::comma) ||
6045 BeforeClosingBrace->isTrailingComment())) {
6050 if (
Right.is(tok::comment)) {
6052 Right.NewlinesBefore > 0 &&
Right.HasUnescapedNewline;
6054 if (
Left.isTrailingComment())
6056 if (
Left.IsUnterminatedLiteral)
6059 if (BeforeLeft && BeforeLeft->is(tok::lessless) &&
6060 Left.is(tok::string_literal) &&
Right.is(tok::lessless) && AfterRight &&
6061 AfterRight->is(tok::string_literal)) {
6062 return Right.NewlinesBefore > 0;
6065 if (
Right.is(TT_RequiresClause)) {
6066 switch (Style.RequiresClausePosition) {
6076 if (
Left.ClosesTemplateDeclaration &&
Left.MatchingParen &&
6077 Left.MatchingParen->NestingLevel == 0) {
6081 if (
Right.is(tok::kw_concept))
6085 Right.NewlinesBefore > 0);
6087 if (
Left.ClosesRequiresClause) {
6088 switch (Style.RequiresClausePosition) {
6091 return Right.isNot(tok::semi);
6093 return Right.isNoneOf(tok::semi, tok::l_brace);
6100 (
Left.is(TT_CtorInitializerComma) ||
6101 Right.is(TT_CtorInitializerColon))) {
6106 Left.isOneOf(TT_CtorInitializerColon, TT_CtorInitializerComma)) {
6111 Left.is(TT_CtorInitializerComma)) {
6117 Right.isOneOf(TT_CtorInitializerComma, TT_CtorInitializerColon)) {
6123 Right.is(TT_CtorInitializerColon)) {
6128 Left.is(TT_CtorInitializerColon)) {
6134 Right.is(TT_InheritanceComma)) {
6138 Left.is(TT_InheritanceComma)) {
6141 if (
Right.is(tok::string_literal) &&
Right.TokenText.starts_with(
"R\"")) {
6145 return Right.IsMultiline &&
Right.NewlinesBefore > 0;
6147 if ((
Left.is(tok::l_brace) ||
6148 (
Left.is(tok::less) && BeforeLeft && BeforeLeft->is(tok::equal))) &&
6154 if (
Right.is(TT_InlineASMBrace))
6155 return Right.HasUnescapedNewline;
6158 auto *FirstNonComment =
Line.getFirstNonComment();
6160 FirstNonComment && (FirstNonComment->is(Keywords.kw_internal) ||
6161 FirstNonComment->isAccessSpecifierKeyword());
6163 if (Style.BraceWrapping.AfterEnum) {
6164 if (
Line.startsWith(tok::kw_enum) ||
6165 Line.startsWith(tok::kw_typedef, tok::kw_enum) ||
6166 Line.startsWith(tok::kw_export, tok::kw_enum)) {
6171 FirstNonComment->Next->is(tok::kw_enum)) {
6177 if (Style.BraceWrapping.AfterClass &&
6179 FirstNonComment->Next->is(Keywords.kw_interface)) ||
6180 Line.startsWith(Keywords.kw_interface))) {
6185 if (
Right.isNot(TT_FunctionLBrace)) {
6187 ((
Line.startsWith(tok::kw_class) &&
6188 Style.BraceWrapping.AfterClass) ||
6189 (
Line.startsWith(tok::kw_struct) &&
6190 Style.BraceWrapping.AfterStruct) ||
6191 (
Line.startsWith(tok::kw_union) &&
6192 Style.BraceWrapping.AfterUnion));
6196 if (
Left.is(TT_ObjCBlockLBrace) &&
6202 if (
Left.isOneOf(TT_AttributeRParen, TT_AttributeMacro) &&
6203 Right.is(TT_ObjCDecl)) {
6207 if (
Left.is(TT_LambdaLBrace)) {
6215 (!
Left.Children.empty() &&
6221 if (Style.BraceWrapping.BeforeLambdaBody &&
Right.is(TT_LambdaLBrace) &&
6222 (
Left.isPointerOrReference() ||
Left.is(TT_TemplateCloser))) {
6228 if (Style.BraceWrapping.BeforeLambdaBody &&
Right.is(TT_LambdaLBrace) &&
6230 const auto SLS = Style.AllowShortLambdasOnASingleLine;
6238 if ((Style.isJava() || Style.isJavaScript()) &&
6239 Left.is(TT_LeadingJavaAnnotation) &&
6240 Right.isNoneOf(TT_LeadingJavaAnnotation, tok::l_paren) &&
6241 (
Line.Last->is(tok::l_brace) || Style.BreakAfterJavaFieldAnnotations)) {
6245 if (
Right.is(TT_ProtoExtensionLSquare))
6275 if (Style.isProto() &&
Right.is(TT_SelectorName) &&
6276 Right.isNot(tok::r_square) && AfterRight) {
6279 if (
Left.is(tok::at))
6285 const auto *LBrace = AfterRight;
6286 if (LBrace && LBrace->is(tok::colon)) {
6287 LBrace = LBrace->Next;
6288 if (LBrace && LBrace->is(tok::at)) {
6289 LBrace = LBrace->Next;
6291 LBrace = LBrace->Next;
6303 ((LBrace->is(tok::l_brace) &&
6304 (LBrace->is(TT_DictLiteral) ||
6305 (LBrace->Next && LBrace->Next->is(tok::r_brace)))) ||
6306 LBrace->isOneOf(TT_ArrayInitializerLSquare, tok::less))) {
6313 if (
Left.ParameterCount == 0)
6328 if (
Left.isOneOf(tok::r_brace, tok::greater, tok::r_square))
6333 Left.is(TT_AttributeRSquare) &&
Right.NewlinesBefore > 0) {
6334 Line.ReturnTypeWrapped =
true;
6345 if (Style.isCSharp()) {
6346 if (
Left.isOneOf(TT_CSharpNamedArgumentColon, TT_AttributeColon) ||
6347 Right.isOneOf(TT_CSharpNamedArgumentColon, TT_AttributeColon)) {
6351 if (
Line.First->is(TT_CSharpGenericTypeConstraint))
6352 return Left.is(TT_CSharpGenericTypeConstraintComma);
6354 if (
Right.is(TT_CSharpNullable))
6356 }
else if (Style.isJava()) {
6357 if (
Left.isOneOf(Keywords.kw_throws, Keywords.kw_extends,
6358 Keywords.kw_implements)) {
6361 if (
Right.isOneOf(Keywords.kw_throws, Keywords.kw_extends,
6362 Keywords.kw_implements)) {
6365 }
else if (Style.isJavaScript()) {
6368 (NonComment->isAccessSpecifierKeyword() ||
6369 NonComment->isOneOf(
6370 tok::kw_return, Keywords.kw_yield, tok::kw_continue, tok::kw_break,
6371 tok::kw_throw, Keywords.kw_interface, Keywords.kw_type,
6372 tok::kw_static, Keywords.kw_readonly, Keywords.kw_override,
6373 Keywords.kw_abstract, Keywords.kw_get, Keywords.kw_set,
6374 Keywords.kw_async, Keywords.kw_await))) {
6377 if (
Right.NestingLevel == 0 &&
6378 (
Left.Tok.getIdentifierInfo() ||
6379 Left.isOneOf(tok::r_square, tok::r_paren)) &&
6380 Right.isOneOf(tok::l_square, tok::l_paren)) {
6383 if (NonComment && NonComment->is(tok::identifier) &&
6384 NonComment->TokenText ==
"asserts") {
6387 if (
Left.is(TT_FatArrow) &&
Right.is(tok::l_brace))
6389 if (
Left.is(TT_JsTypeColon))
6392 if (
Left.is(tok::exclaim) &&
Right.is(tok::colon))
6397 if (
Right.is(Keywords.kw_is)) {
6406 if (!
Next ||
Next->isNot(tok::colon))
6409 if (
Left.is(Keywords.kw_in))
6411 if (
Right.is(Keywords.kw_in))
6413 if (
Right.is(Keywords.kw_as))
6415 if (
Right.isOneOf(Keywords.kw_extends, Keywords.kw_infer)) {
6421 if (
Left.is(Keywords.kw_as))
6423 if (
Left.is(TT_NonNullAssertion))
6425 if (
Left.is(Keywords.kw_declare) &&
6426 Right.isOneOf(Keywords.kw_module, tok::kw_namespace,
6427 Keywords.kw_function, tok::kw_class, tok::kw_enum,
6428 Keywords.kw_interface, Keywords.kw_type, Keywords.kw_var,
6429 Keywords.kw_let, tok::kw_const)) {
6434 if (
Left.isOneOf(Keywords.kw_module, tok::kw_namespace) &&
6435 Right.isOneOf(tok::identifier, tok::string_literal)) {
6438 if (
Right.is(TT_TemplateString) &&
Right.closesScope())
6442 if (
Left.is(tok::identifier) &&
Right.is(TT_TemplateString))
6444 if (
Left.is(TT_TemplateString) &&
Left.opensScope())
6446 }
else if (Style.isTableGen()) {
6448 if (Keywords.isTableGenDefinition(Left))
6451 if (
Right.is(tok::l_paren)) {
6452 return Left.isNoneOf(TT_TableGenBangOperator, TT_TableGenCondOperator,
6456 if (
Left.is(TT_TableGenValueSuffix))
6459 if (
Left.is(tok::hash) ||
Right.is(tok::hash))
6461 if (
Left.isOneOf(TT_TableGenBangOperator, TT_TableGenCondOperator))
6468 if (
Right.is(tok::r_brace)) {
6470 (
Right.isBlockIndentedInitRBrace(Style)));
6475 if (
Right.is(tok::r_paren)) {
6476 if (!
Right.MatchingParen)
6479 if (
Next &&
Next->is(tok::r_paren))
6481 if (
Next &&
Next->is(tok::l_paren))
6487 return Style.BreakBeforeCloseBracketIf;
6489 return Style.BreakBeforeCloseBracketLoop;
6491 return Style.BreakBeforeCloseBracketSwitch;
6492 return Style.BreakBeforeCloseBracketFunction;
6495 if (
Left.isOneOf(tok::r_paren, TT_TrailingAnnotation) &&
6496 Right.is(TT_TrailingAnnotation) &&
6497 Style.BreakBeforeCloseBracketFunction) {
6501 if (
Right.is(TT_TemplateCloser))
6502 return Style.BreakBeforeTemplateCloser;
6504 if (
Left.isOneOf(tok::at, tok::objc_interface))
6506 if (
Left.isOneOf(TT_JavaAnnotation, TT_LeadingJavaAnnotation))
6507 return Right.isNot(tok::l_paren);
6508 if (
Right.is(TT_PointerOrReference)) {
6509 return Line.IsMultiVariableDeclStmt ||
6510 (getTokenPointerOrReferenceAlignment(Right) ==
6513 Right.Next->isOneOf(TT_FunctionDeclarationName, tok::kw_const)));
6515 if (
Left.is(tok::hashhash) ||
Right.is(tok::hashhash))
6517 if (
Right.isOneOf(TT_StartOfName, TT_FunctionDeclarationName,
6518 TT_ClassHeadName, TT_QtProperty, tok::kw_operator)) {
6521 if (
Left.is(TT_PointerOrReference))
6523 if (
Right.isTrailingComment()) {
6530 (
Left.is(TT_CtorInitializerColon) &&
Right.NewlinesBefore > 0 &&
6533 if (
Left.is(tok::question) &&
Right.is(tok::colon))
6535 if (
Right.isOneOf(TT_ConditionalExpr, tok::question))
6536 return Style.BreakBeforeTernaryOperators;
6537 if (
Left.isOneOf(TT_ConditionalExpr, tok::question))
6538 return !Style.BreakBeforeTernaryOperators;
6539 if (
Left.is(TT_InheritanceColon))
6541 if (
Right.is(TT_InheritanceColon))
6544 if (
Right.is(TT_ObjCMethodExpr) &&
Right.isNot(tok::r_square) &&
6545 Left.isNot(TT_SelectorName)) {
6549 if (
Right.is(tok::colon) &&
6550 Right.isNoneOf(TT_CtorInitializerColon, TT_InlineASMColon,
6551 TT_BitFieldColon)) {
6554 if (
Left.is(tok::colon) &&
Left.isOneOf(TT_ObjCSelector, TT_ObjCMethodExpr))
6556 if (
Left.is(tok::colon) &&
Left.is(TT_DictLiteral)) {
6557 if (Style.isProto()) {
6558 if (!Style.AlwaysBreakBeforeMultilineStrings &&
Right.isStringLiteral())
6584 if ((
Right.isOneOf(tok::l_brace, tok::less) &&
6585 Right.is(TT_DictLiteral)) ||
6586 Right.is(TT_ArrayInitializerLSquare)) {
6592 if (
Right.is(tok::r_square) &&
Right.MatchingParen &&
6593 Right.MatchingParen->is(TT_ProtoExtensionLSquare)) {
6596 if (
Right.is(TT_SelectorName) || (
Right.is(tok::identifier) &&
Right.Next &&
6597 Right.Next->is(TT_ObjCMethodExpr))) {
6598 return Left.isNot(tok::period);
6602 if (
Right.is(tok::kw_concept))
6604 if (
Right.is(TT_RequiresClause))
6606 if (
Left.ClosesTemplateDeclaration) {
6608 Right.NewlinesBefore > 0;
6610 if (
Left.is(TT_FunctionAnnotationRParen))
6612 if (
Left.ClosesRequiresClause)
6614 if (
Right.isOneOf(TT_RangeBasedForLoopColon, TT_OverloadedOperatorLParen,
6615 TT_OverloadedOperator)) {
6618 if (
Left.is(TT_RangeBasedForLoopColon))
6620 if (
Right.is(TT_RangeBasedForLoopColon))
6622 if (
Left.is(TT_TemplateCloser) &&
Right.is(TT_TemplateOpener))
6624 if ((
Left.is(tok::greater) &&
Right.is(tok::greater)) ||
6625 (
Left.is(tok::less) &&
Right.is(tok::less))) {
6628 if (
Right.is(TT_BinaryOperator) &&
6634 if (
Left.isOneOf(TT_TemplateCloser, TT_UnaryOperator, tok::kw_operator))
6636 if (
Left.is(tok::equal) &&
Right.isNoneOf(tok::kw_default, tok::kw_delete) &&
6640 if (
Left.is(tok::equal) &&
Right.is(tok::l_brace) &&
6644 if (
Left.is(TT_AttributeLParen) ||
6645 (
Left.is(tok::l_paren) &&
Left.is(TT_TypeDeclarationParen))) {
6648 if (
Left.is(tok::l_paren) &&
Left.Previous &&
6649 (
Left.Previous->isOneOf(TT_BinaryOperator, TT_CastRParen))) {
6652 if (
Right.is(TT_ImplicitStringLiteral))
6655 if (
Right.is(tok::r_square) &&
Right.MatchingParen &&
6656 Right.MatchingParen->is(TT_LambdaLSquare)) {
6662 if (
Left.is(TT_TrailingAnnotation)) {
6663 return Right.isNoneOf(tok::l_brace, tok::semi, tok::equal, tok::l_paren,
6664 tok::less, tok::coloncolon);
6667 if (
Right.isAttribute())
6670 if (
Right.is(TT_AttributeLSquare)) {
6671 assert(
Left.isNot(tok::l_square));
6675 if (
Left.is(tok::identifier) &&
Right.is(tok::string_literal))
6678 if (
Right.is(tok::identifier) &&
Right.Next &&
Right.Next->is(TT_DictLiteral))
6681 if (
Left.is(TT_CtorInitializerColon)) {
6682 return (Style.BreakConstructorInitializers ==
6684 Style.BreakConstructorInitializers ==
6686 (!
Right.isTrailingComment() ||
Right.NewlinesBefore > 0);
6688 if (
Right.is(TT_CtorInitializerColon)) {
6692 if (
Left.is(TT_CtorInitializerComma) &&
6696 if (
Right.is(TT_CtorInitializerComma) &&
6700 if (
Left.is(TT_InheritanceComma) &&
6704 if (
Right.is(TT_InheritanceComma) &&
6708 if (
Left.is(TT_ArrayInitializerLSquare))
6710 if (
Right.is(tok::kw_typename) &&
Left.isNot(tok::kw_const))
6712 if ((
Left.isBinaryOperator() ||
Left.is(TT_BinaryOperator)) &&
6713 Left.isNoneOf(tok::arrowstar, tok::lessless) &&
6719 if (
Left.is(TT_AttributeLSquare) &&
Right.is(tok::l_square)) {
6720 assert(
Right.isNot(TT_AttributeLSquare));
6723 if (
Left.is(tok::r_square) &&
Right.is(TT_AttributeRSquare)) {
6724 assert(
Left.isNot(TT_AttributeRSquare));
6728 if (Style.BraceWrapping.AfterRequiresExpression &&
6729 Right.is(TT_RequiresExpressionLBrace)) {
6733 auto ShortLambdaOption = Style.AllowShortLambdasOnASingleLine;
6734 if (Style.BraceWrapping.BeforeLambdaBody &&
Right.is(TT_LambdaLBrace)) {
6741 if (
Right.is(tok::kw_noexcept) &&
Right.is(TT_TrailingAnnotation)) {
6742 switch (Style.AllowBreakBeforeNoexceptSpecifier) {
6748 return Right.Next &&
Right.Next->is(tok::l_paren);
6752 return Left.isOneOf(tok::comma, tok::coloncolon, tok::semi, tok::l_brace,
6753 tok::kw_class, tok::kw_struct, tok::comment) ||
6754 Right.isMemberAccess() ||
6755 Right.isOneOf(TT_TrailingReturnArrow, TT_LambdaArrow, tok::lessless,
6756 tok::colon, tok::l_square, tok::at) ||
6757 (
Left.is(tok::r_paren) &&
6758 Right.isOneOf(tok::identifier, tok::kw_const)) ||
6759 (
Left.is(tok::l_paren) &&
Right.isNot(tok::r_paren)) ||
6760 (
Left.is(TT_TemplateOpener) &&
Right.isNot(TT_TemplateCloser));
6764 llvm::errs() <<
"AnnotatedTokens(L=" <<
Line.Level <<
", P=" <<
Line.PPLevel
6765 <<
", T=" <<
Line.Type <<
", C=" <<
Line.IsContinuation
6769 llvm::errs() <<
" I=" <<
Tok->IndentLevel <<
" M=" <<
Tok->MustBreakBefore
6770 <<
" C=" <<
Tok->CanBreakBefore
6772 <<
" S=" <<
Tok->SpacesRequiredBefore
6773 <<
" F=" <<
Tok->Finalized <<
" B=" <<
Tok->BlockParameterCount
6774 <<
" BK=" <<
Tok->getBlockKind() <<
" P=" <<
Tok->SplitPenalty
6775 <<
" Name=" <<
Tok->Tok.getName() <<
" N=" <<
Tok->NestingLevel
6776 <<
" L=" <<
Tok->TotalLength
6777 <<
" PPK=" <<
Tok->getPackingKind() <<
" FakeLParens=";
6779 llvm::errs() << LParen <<
"/";
6780 llvm::errs() <<
" FakeRParens=" <<
Tok->FakeRParens;
6781 llvm::errs() <<
" II=" <<
Tok->Tok.getIdentifierInfo();
6782 llvm::errs() <<
" Text='" <<
Tok->TokenText <<
"'\n";
6787 llvm::errs() <<
"----\n";
6792 assert(
Reference.isOneOf(tok::amp, tok::ampamp));
6793 switch (Style.ReferenceAlignment) {
6795 return Style.PointerAlignment;
6804 return Style.PointerAlignment;
6808TokenAnnotator::getTokenPointerOrReferenceAlignment(
6810 if (PointerOrReference.isOneOf(tok::amp, tok::ampamp))
6811 return getTokenReferenceAlignment(PointerOrReference);
6812 assert(PointerOrReference.is(tok::star));
6813 return Style.PointerAlignment;
Result
Implement __builtin_bit_cast and related operations.
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.
TokenKind
Provides a simple uniform namespace for tokens from all C languages.
Top level wrappers for InstallAPI frontend operations.
AccessSpecifier
A C++ access specifier (public, private, protected), plus the special value "none" which means differ...
@ Parameter
The parameter type of a method or function.
@ Result
The result type of a method or function.
bool isReturnTypePrefixSpecifier(const FormatToken &Tok)
prec::Level getBinOpPrecedence(tok::TokenKind Kind, bool GreaterThanIsOperator, bool CPlusPlus11)
Return the precedence of the specified binary operator token.