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) ||
587 Prev.is(TT_TrailingCommaVaOptRParen)) {
589 Contexts.back().InStaticAssertFirstArgument =
false;
591 Contexts.back().CanBeExpression =
true;
594 if (Style.isTableGen()) {
595 if (CurrentToken->is(tok::comma)) {
596 if (Contexts.back().IsTableGenCondOpe)
597 CurrentToken->setType(TT_TableGenCondOperatorComma);
599 }
else if (CurrentToken->is(tok::colon)) {
600 if (Contexts.back().IsTableGenCondOpe)
601 CurrentToken->setType(TT_TableGenCondOperatorColon);
605 if (!parseTableGenValue())
613 updateParameterCount(&OpeningParen,
Tok);
614 if (CurrentToken && CurrentToken->HasUnescapedNewline)
615 HasMultipleLines =
true;
621 if (!Style.isCSharp())
625 if (
Tok.Previous &&
Tok.Previous->is(tok::identifier))
629 if (
Tok.Previous &&
Tok.Previous->is(tok::r_square)) {
640 if (AttrTok->is(tok::r_square))
644 while (AttrTok && AttrTok->isNot(tok::r_square))
645 AttrTok = AttrTok->Next;
651 AttrTok = AttrTok->Next;
656 if (AttrTok->isAccessSpecifierKeyword() ||
657 AttrTok->isOneOf(tok::comment, tok::kw_class, tok::kw_static,
658 tok::l_square, Keywords.kw_internal)) {
664 AttrTok->Next->startsSequence(tok::identifier, tok::l_paren)) {
680 Left->ParentBracket = Contexts.back().ContextKind;
681 FormatToken *Parent = Left->getPreviousNonComment();
686 bool CppArrayTemplates =
687 IsCpp && Parent && Parent->is(TT_TemplateCloser) &&
688 (Contexts.back().CanBeExpression || Contexts.back().IsExpression ||
689 Contexts.back().ContextType == Context::TemplateArgument);
691 const bool IsInnerSquare = Contexts.back().InCpp11AttributeSpecifier;
692 const bool IsCpp11AttributeSpecifier =
693 isCppAttribute(IsCpp, *Left) || IsInnerSquare;
696 bool IsCSharpAttributeSpecifier =
697 isCSharpAttributeSpecifier(*Left) ||
698 Contexts.back().InCSharpAttributeSpecifier;
700 bool InsideInlineASM =
Line.startsWith(tok::kw_asm);
701 bool IsCppStructuredBinding = Left->isCppStructuredBinding(IsCpp);
702 bool StartsObjCMethodExpr =
703 !IsCppStructuredBinding && !InsideInlineASM && !CppArrayTemplates &&
704 IsCpp && !IsCpp11AttributeSpecifier && !IsCSharpAttributeSpecifier &&
705 Contexts.back().CanBeExpression && Left->isNot(TT_LambdaLSquare) &&
706 CurrentToken->isNoneOf(tok::l_brace, tok::r_square) &&
710 (!Parent || !Parent->is(tok::comma) ||
711 Contexts.back().ContextKind != tok::l_brace) &&
713 Parent->isOneOf(tok::colon, tok::l_square, tok::l_paren,
714 tok::kw_return, tok::kw_throw) ||
715 Parent->isUnaryOperator() ||
717 Parent->isOneOf(TT_ObjCForIn, TT_CastRParen) ||
720 bool ColonFound =
false;
722 unsigned BindingIncrease = 1;
723 if (IsCppStructuredBinding) {
724 Left->setType(TT_StructuredBindingLSquare);
725 }
else if (Left->is(TT_Unknown)) {
726 if (StartsObjCMethodExpr) {
727 Left->setType(TT_ObjCMethodExpr);
728 }
else if (InsideInlineASM) {
729 Left->setType(TT_InlineASMSymbolicNameLSquare);
730 }
else if (IsCpp11AttributeSpecifier) {
731 if (!IsInnerSquare) {
732 Left->setType(TT_AttributeLSquare);
734 Left->Previous->EndsCppAttributeGroup =
false;
736 }
else if (Style.isJavaScript() && Parent &&
737 Contexts.back().ContextKind == tok::l_brace &&
738 Parent->isOneOf(tok::l_brace, tok::comma)) {
739 Left->setType(TT_JsComputedPropertyName);
740 }
else if (IsCpp && Contexts.back().ContextKind == tok::l_brace &&
742 Parent->isOneOf(tok::l_brace, tok::comma,
743 TT_TrailingCommaVaOptRParen)) {
744 Left->setType(TT_DesignatedInitializerLSquare);
745 }
else if (IsCSharpAttributeSpecifier) {
746 Left->setType(TT_AttributeLSquare);
747 }
else if (CurrentToken->is(tok::r_square) && Parent &&
748 Parent->is(TT_TemplateCloser)) {
749 Left->setType(TT_ArraySubscriptLSquare);
750 }
else if (Style.isProto()) {
777 Left->setType(TT_ArrayInitializerLSquare);
778 if (!Left->endsSequence(tok::l_square, tok::numeric_constant,
780 !Left->endsSequence(tok::l_square, tok::numeric_constant,
782 !Left->endsSequence(tok::l_square, tok::colon, TT_SelectorName)) {
783 Left->setType(TT_ProtoExtensionLSquare);
784 BindingIncrease = 10;
786 }
else if (!CppArrayTemplates && Parent &&
787 Parent->isOneOf(TT_BinaryOperator, TT_TemplateCloser, tok::at,
788 tok::comma, tok::l_paren, tok::l_square,
789 tok::question, tok::colon, tok::kw_return,
792 Left->setType(TT_ArrayInitializerLSquare);
794 BindingIncrease = 10;
795 Left->setType(TT_ArraySubscriptLSquare);
799 ScopedContextCreator ContextCreator(*
this, tok::l_square, BindingIncrease);
800 Contexts.back().IsExpression =
true;
801 if (Style.isJavaScript() && Parent && Parent->is(TT_JsTypeColon))
802 Contexts.back().IsExpression =
false;
804 Contexts.back().ColonIsObjCMethodExpr = StartsObjCMethodExpr;
805 Contexts.back().InCpp11AttributeSpecifier = IsCpp11AttributeSpecifier;
806 Contexts.back().InCSharpAttributeSpecifier = IsCSharpAttributeSpecifier;
808 while (CurrentToken) {
809 if (CurrentToken->is(tok::r_square)) {
810 if (IsCpp11AttributeSpecifier && !IsInnerSquare) {
811 CurrentToken->setType(TT_AttributeRSquare);
812 CurrentToken->EndsCppAttributeGroup =
true;
814 if (IsCSharpAttributeSpecifier) {
815 CurrentToken->setType(TT_AttributeRSquare);
816 }
else if (((CurrentToken->Next &&
817 CurrentToken->Next->is(tok::l_paren)) ||
818 (CurrentToken->Previous &&
819 CurrentToken->Previous->Previous == Left)) &&
820 Left->is(TT_ObjCMethodExpr)) {
825 StartsObjCMethodExpr =
false;
826 Left->setType(TT_Unknown);
828 if (StartsObjCMethodExpr && CurrentToken->Previous != Left) {
829 CurrentToken->setType(TT_ObjCMethodExpr);
832 if (!ColonFound && CurrentToken->Previous &&
833 CurrentToken->Previous->is(TT_Unknown) &&
834 canBeObjCSelectorComponent(*CurrentToken->Previous)) {
835 CurrentToken->Previous->setType(TT_SelectorName);
840 if (Parent && Parent->is(TT_PointerOrReference))
841 Parent->overwriteFixedType(TT_BinaryOperator);
843 Left->MatchingParen = CurrentToken;
844 CurrentToken->MatchingParen = Left;
849 if (!Contexts.back().FirstObjCSelectorName) {
852 Previous->ObjCSelectorNameParts = 1;
853 Contexts.back().FirstObjCSelectorName =
Previous;
856 Left->ParameterCount =
857 Contexts.back().FirstObjCSelectorName->ObjCSelectorNameParts;
859 if (Contexts.back().FirstObjCSelectorName) {
860 Contexts.back().FirstObjCSelectorName->LongestObjCSelectorName =
861 Contexts.back().LongestObjCSelectorName;
862 if (Left->BlockParameterCount > 1)
863 Contexts.back().FirstObjCSelectorName->LongestObjCSelectorName = 0;
865 if (Style.isTableGen() && Left->is(TT_TableGenListOpener))
866 CurrentToken->setType(TT_TableGenListCloser);
870 if (CurrentToken->isOneOf(tok::r_paren, tok::r_brace))
872 if (CurrentToken->is(tok::colon)) {
873 if (IsCpp11AttributeSpecifier &&
874 CurrentToken->endsSequence(tok::colon, tok::identifier,
878 CurrentToken->setType(TT_AttributeColon);
879 }
else if (!Style.isVerilog() && !
Line.InPragmaDirective &&
880 Left->isOneOf(TT_ArraySubscriptLSquare,
881 TT_DesignatedInitializerLSquare)) {
882 Left->setType(TT_ObjCMethodExpr);
883 StartsObjCMethodExpr =
true;
884 Contexts.back().ColonIsObjCMethodExpr =
true;
885 if (Parent && Parent->is(tok::r_paren) &&
886 !Parent->isTypeFinalized()) {
888 Parent->setType(TT_CastRParen);
893 if (CurrentToken->is(tok::comma) && Left->is(TT_ObjCMethodExpr) &&
895 Left->setType(TT_ArrayInitializerLSquare);
898 if (Style.isTableGen()) {
899 if (CurrentToken->isOneOf(tok::comma, tok::minus, tok::ellipsis)) {
905 if (!parseTableGenValue())
908 updateParameterCount(Left,
Tok);
913 updateParameterCount(Left,
Tok);
918 void skipToNextNonComment() {
920 while (CurrentToken && CurrentToken->is(tok::comment))
929 bool parseTableGenValue(
bool ParseNameMode =
false) {
932 while (CurrentToken->is(tok::comment))
934 if (!parseTableGenSimpleValue())
939 if (CurrentToken->is(tok::hash)) {
940 if (CurrentToken->Next &&
941 CurrentToken->Next->isOneOf(tok::colon, tok::semi, tok::l_brace)) {
944 CurrentToken->setType(TT_TableGenTrailingPasteOperator);
949 skipToNextNonComment();
950 HashTok->setType(TT_Unknown);
951 if (!parseTableGenValue(ParseNameMode))
958 if (ParseNameMode && CurrentToken->is(tok::l_brace))
961 if (CurrentToken->isOneOf(tok::l_brace, tok::l_square, tok::period)) {
962 CurrentToken->setType(TT_TableGenValueSuffix);
964 skipToNextNonComment();
965 if (Suffix->is(tok::l_square))
966 return parseSquare();
967 if (Suffix->is(tok::l_brace)) {
968 Scopes.push_back(getScopeType(*Suffix));
978 bool tryToParseTableGenTokVar() {
981 if (CurrentToken->is(tok::identifier) &&
982 CurrentToken->TokenText.front() ==
'$') {
983 skipToNextNonComment();
991 bool parseTableGenDAGArg(
bool AlignColon =
false) {
992 if (tryToParseTableGenTokVar())
994 if (parseTableGenValue()) {
995 if (CurrentToken && CurrentToken->is(tok::colon)) {
997 CurrentToken->setType(TT_TableGenDAGArgListColonToAlign);
999 CurrentToken->setType(TT_TableGenDAGArgListColon);
1000 skipToNextNonComment();
1001 return tryToParseTableGenTokVar();
1012 auto &Opes = Style.TableGenBreakingDAGArgOperators;
1017 if (
Tok.isNot(tok::identifier) ||
1018 Tok.isOneOf(TT_TableGenBangOperator, TT_TableGenCondOperator)) {
1022 if (!
Tok.Next ||
Tok.Next->is(tok::colon))
1024 return llvm::is_contained(Opes,
Tok.TokenText.str());
1029 bool parseTableGenDAGArgAndList(
FormatToken *Opener) {
1031 if (!parseTableGenDAGArg())
1033 bool BreakInside =
false;
1037 if (isTableGenDAGArgBreakingOperator(*FirstTok)) {
1040 Opener->setType(TT_TableGenDAGArgOpenerToBreak);
1041 if (FirstTok->isOneOf(TT_TableGenBangOperator,
1042 TT_TableGenCondOperator)) {
1045 CurrentToken->Previous->setType(TT_TableGenDAGArgOperatorToBreak);
1046 }
else if (FirstTok->is(tok::identifier)) {
1048 FirstTok->setType(TT_TableGenDAGArgOperatorToBreak);
1050 FirstTok->setType(TT_TableGenDAGArgOperatorID);
1055 return parseTableGenDAGArgList(Opener, BreakInside);
1060 bool parseTableGenDAGArgList(
FormatToken *Opener,
bool BreakInside) {
1061 ScopedContextCreator ContextCreator(*
this, tok::l_paren, 0);
1062 Contexts.back().IsTableGenDAGArgList =
true;
1063 bool FirstDAGArgListElm =
true;
1064 while (CurrentToken) {
1065 if (!FirstDAGArgListElm && CurrentToken->is(tok::comma)) {
1066 CurrentToken->setType(BreakInside ? TT_TableGenDAGArgListCommaToBreak
1067 : TT_TableGenDAGArgListComma);
1068 skipToNextNonComment();
1070 if (CurrentToken && CurrentToken->is(tok::r_paren)) {
1071 CurrentToken->setType(TT_TableGenDAGArgCloser);
1072 Opener->MatchingParen = CurrentToken;
1073 CurrentToken->MatchingParen = Opener;
1074 skipToNextNonComment();
1077 if (!parseTableGenDAGArg(
1079 Style.AlignConsecutiveTableGenBreakingDAGArgColons.Enabled)) {
1082 FirstDAGArgListElm =
false;
1087 bool parseTableGenSimpleValue() {
1088 assert(Style.isTableGen());
1092 skipToNextNonComment();
1094 if (
Tok->isOneOf(tok::numeric_constant, tok::string_literal,
1095 TT_TableGenMultiLineString, tok::kw_true, tok::kw_false,
1096 tok::question, tok::kw_int)) {
1100 if (
Tok->is(tok::l_brace)) {
1101 Scopes.push_back(getScopeType(*
Tok));
1102 return parseBrace();
1105 if (
Tok->is(tok::l_square)) {
1106 Tok->setType(TT_TableGenListOpener);
1109 if (
Tok->is(tok::less)) {
1110 CurrentToken->setType(TT_TemplateOpener);
1111 return parseAngle();
1117 if (
Tok->is(tok::l_paren)) {
1118 Tok->setType(TT_TableGenDAGArgOpener);
1120 if (Contexts.back().IsTableGenDAGArgList)
1121 Tok->SpacesRequiredBefore = 1;
1122 return parseTableGenDAGArgAndList(
Tok);
1125 if (
Tok->is(TT_TableGenBangOperator)) {
1126 if (CurrentToken && CurrentToken->is(tok::less)) {
1127 CurrentToken->setType(TT_TemplateOpener);
1128 skipToNextNonComment();
1132 if (!CurrentToken || CurrentToken->isNot(tok::l_paren))
1136 Contexts.back().IsTableGenBangOpe =
true;
1137 bool Result = parseParens();
1138 Contexts.back().IsTableGenBangOpe =
false;
1142 if (
Tok->is(TT_TableGenCondOperator)) {
1143 if (!CurrentToken || CurrentToken->isNot(tok::l_paren))
1146 return parseParens();
1151 if (
Tok->is(tok::identifier)) {
1153 if (CurrentToken && CurrentToken->is(tok::less)) {
1154 CurrentToken->setType(TT_TemplateOpener);
1155 skipToNextNonComment();
1156 return parseAngle();
1164 bool couldBeInStructArrayInitializer()
const {
1165 if (Contexts.size() < 2)
1169 const auto End = std::next(Contexts.rbegin(), 2);
1170 auto Last = Contexts.rbegin();
1173 if (
Last->ContextKind == tok::l_brace)
1175 return Depth == 2 &&
Last->ContextKind != tok::l_brace;
1182 assert(CurrentToken->Previous);
1183 FormatToken &OpeningBrace = *CurrentToken->Previous;
1184 assert(OpeningBrace.is(tok::l_brace));
1185 OpeningBrace.ParentBracket = Contexts.back().ContextKind;
1187 if (Contexts.back().CaretFound)
1188 OpeningBrace.overwriteFixedType(TT_ObjCBlockLBrace);
1189 Contexts.back().CaretFound =
false;
1191 ScopedContextCreator ContextCreator(*
this, tok::l_brace, 1);
1192 Contexts.back().ColonIsDictLiteral =
true;
1194 Contexts.back().IsExpression =
true;
1195 if (Style.isJavaScript() && OpeningBrace.Previous &&
1196 OpeningBrace.Previous->is(TT_JsTypeColon)) {
1197 Contexts.back().IsExpression =
false;
1199 if (Style.isVerilog() &&
1200 (!OpeningBrace.getPreviousNonComment() ||
1201 OpeningBrace.getPreviousNonComment()->isNot(Keywords.kw_apostrophe))) {
1202 Contexts.back().VerilogMayBeConcatenation =
true;
1204 if (Style.isTableGen())
1205 Contexts.back().ColonIsDictLiteral =
false;
1207 unsigned CommaCount = 0;
1208 while (CurrentToken) {
1209 assert(!Scopes.empty());
1210 if (CurrentToken->is(tok::r_brace)) {
1211 assert(Scopes.back() == getScopeType(OpeningBrace));
1213 assert(OpeningBrace.Optional == CurrentToken->Optional);
1214 OpeningBrace.MatchingParen = CurrentToken;
1215 CurrentToken->MatchingParen = &OpeningBrace;
1217 if (OpeningBrace.ParentBracket == tok::l_brace &&
1218 couldBeInStructArrayInitializer() && CommaCount > 0) {
1219 Contexts.back().ContextType = Context::StructArrayInitializer;
1225 if (CurrentToken->isOneOf(tok::r_paren, tok::r_square))
1227 updateParameterCount(&OpeningBrace, CurrentToken);
1228 if (CurrentToken->isOneOf(tok::colon, tok::l_brace, tok::less)) {
1230 if (
Previous->is(TT_JsTypeOptionalQuestion))
1232 if ((CurrentToken->is(tok::colon) && !Style.isTableGen() &&
1233 (!Contexts.back().ColonIsDictLiteral || !IsCpp)) ||
1235 OpeningBrace.setType(TT_DictLiteral);
1236 Scopes.back() = getScopeType(OpeningBrace);
1237 if (
Previous->Tok.getIdentifierInfo() ||
1238 Previous->is(tok::string_literal)) {
1239 Previous->setType(TT_SelectorName);
1242 if (CurrentToken->is(tok::colon) && OpeningBrace.is(TT_Unknown) &&
1243 !Style.isTableGen()) {
1244 OpeningBrace.setType(TT_DictLiteral);
1245 Scopes.back() = getScopeType(OpeningBrace);
1246 }
else if (Style.isJavaScript()) {
1247 OpeningBrace.overwriteFixedType(TT_DictLiteral);
1248 Scopes.back() = getScopeType(OpeningBrace);
1251 bool IsBracedListComma =
false;
1252 if (CurrentToken->is(tok::comma)) {
1253 if (Style.isJavaScript()) {
1254 OpeningBrace.overwriteFixedType(TT_DictLiteral);
1255 Scopes.back() = getScopeType(OpeningBrace);
1261 if (!consumeToken())
1263 if (IsBracedListComma)
1264 Contexts.back().IsExpression =
true;
1273 if (Current->is(tok::l_brace) && Current->is(
BK_Block))
1274 ++Left->BlockParameterCount;
1276 if (Current->is(tok::comma))
1278 else if (Current->Previous->is(TT_TrailingCommaVaOptRParen))
1279 Comma = Current->Previous;
1281 ++Left->ParameterCount;
1283 Left->Role.reset(
new CommaSeparatedList(Style));
1284 Left->Role->CommaFound(Comma);
1285 }
else if (Left->ParameterCount == 0 && Current->isNot(tok::comment)) {
1286 Left->ParameterCount = 1;
1290 bool parseConditional() {
1291 while (CurrentToken) {
1292 if (CurrentToken->is(tok::colon) && CurrentToken->is(TT_Unknown)) {
1293 CurrentToken->setType(TT_ConditionalExpr);
1300 if (CurrentToken->is(tok::r_brace))
1302 if (!consumeToken())
1308 bool parseTemplateDeclaration() {
1309 if (!CurrentToken || CurrentToken->isNot(tok::less))
1312 CurrentToken->setType(TT_TemplateOpener);
1315 TemplateDeclarationDepth++;
1316 const bool WellFormed = parseAngle();
1317 TemplateDeclarationDepth--;
1321 if (CurrentToken && TemplateDeclarationDepth == 0)
1322 CurrentToken->Previous->ClosesTemplateDeclaration =
true;
1327 bool consumeToken() {
1329 const auto *Prev = CurrentToken->getPreviousNonComment();
1330 if (Prev && Prev->is(TT_AttributeRSquare) &&
1331 CurrentToken->isOneOf(tok::kw_if, tok::kw_switch, tok::kw_case,
1332 tok::kw_default, tok::kw_for, tok::kw_while) &&
1333 CurrentToken->mustBreakAfterAttributes(Style)) {
1334 CurrentToken->MustBreakBefore =
true;
1341 if (
Tok->is(TT_VerilogTableItem))
1344 if (
Tok->is(TT_TableGenMultiLineString))
1346 auto *Prev =
Tok->getPreviousNonComment();
1347 auto *
Next =
Tok->getNextNonComment();
1348 switch (
bool IsIf =
false;
Tok->Tok.getKind()) {
1351 if (!Prev &&
Line.MustBeDeclaration)
1352 Tok->setType(TT_ObjCMethodSpecifier);
1359 if (
Tok->isTypeFinalized())
1362 if (Style.isJavaScript()) {
1363 if (Contexts.back().ColonIsForRangeExpr ||
1364 (Contexts.size() == 1 &&
1365 Line.First->isNoneOf(tok::kw_enum, tok::kw_case)) ||
1366 Contexts.back().ContextKind == tok::l_paren ||
1367 Contexts.back().ContextKind == tok::l_square ||
1368 (!Contexts.back().IsExpression &&
1369 Contexts.back().ContextKind == tok::l_brace) ||
1370 (Contexts.size() == 1 &&
1371 Line.MustBeDeclaration)) {
1372 Contexts.back().IsExpression =
false;
1373 Tok->setType(TT_JsTypeColon);
1376 }
else if (Style.isCSharp()) {
1377 if (Contexts.back().InCSharpAttributeSpecifier) {
1378 Tok->setType(TT_AttributeColon);
1381 if (Contexts.back().ContextKind == tok::l_paren) {
1382 Tok->setType(TT_CSharpNamedArgumentColon);
1385 }
else if (Style.isVerilog() &&
Tok->isNot(TT_BinaryOperator)) {
1388 if (Keywords.isVerilogEnd(*Prev) || Keywords.isVerilogBegin(*Prev)) {
1389 Tok->setType(TT_VerilogBlockLabelColon);
1390 }
else if (Contexts.back().ContextKind == tok::l_square) {
1391 Tok->setType(TT_BitFieldColon);
1392 }
else if (Contexts.back().ColonIsDictLiteral) {
1393 Tok->setType(TT_DictLiteral);
1394 }
else if (Contexts.size() == 1) {
1398 Tok->setType(TT_CaseLabelColon);
1399 if (
Line.Level > 1 || (!
Line.InPPDirective &&
Line.Level > 0))
1404 if (
Line.First->is(tok::kw_asm)) {
1405 Tok->setType(TT_InlineASMColon);
1406 }
else if (Contexts.back().ColonIsDictLiteral || Style.isProto()) {
1407 Tok->setType(TT_DictLiteral);
1408 if (Style.isTextProto())
1409 Prev->setType(TT_SelectorName);
1410 }
else if (Contexts.back().ColonIsObjCMethodExpr ||
1411 Line.startsWith(TT_ObjCMethodSpecifier)) {
1412 Tok->setType(TT_ObjCMethodExpr);
1413 const auto *PrevPrev = Prev->Previous;
1416 bool UnknownIdentifierInMethodDeclaration =
1417 Line.startsWith(TT_ObjCMethodSpecifier) &&
1418 Prev->is(tok::identifier) && Prev->is(TT_Unknown);
1421 !(PrevPrev->is(TT_CastRParen) ||
1422 (PrevPrev->is(TT_ObjCMethodExpr) && PrevPrev->is(tok::colon))) ||
1423 PrevPrev->is(tok::r_square) ||
1424 Contexts.back().LongestObjCSelectorName == 0 ||
1425 UnknownIdentifierInMethodDeclaration) {
1426 if (!Prev->isTypeFinalized())
1427 Prev->setType(TT_SelectorName);
1428 if (!Contexts.back().FirstObjCSelectorName)
1429 Contexts.back().FirstObjCSelectorName = Prev;
1430 else if (Prev->ColumnWidth > Contexts.back().LongestObjCSelectorName)
1431 Contexts.back().LongestObjCSelectorName = Prev->ColumnWidth;
1432 Prev->ParameterIndex =
1433 Contexts.back().FirstObjCSelectorName->ObjCSelectorNameParts;
1434 ++Contexts.back().FirstObjCSelectorName->ObjCSelectorNameParts;
1436 }
else if (Contexts.back().ColonIsForRangeExpr) {
1437 Tok->setType(TT_RangeBasedForLoopColon);
1438 for (
auto *Token = Prev;
1439 Token && Token->isNoneOf(tok::semi, tok::l_paren);
1440 Token = Token->Previous) {
1441 if (Token->isPointerOrReference())
1442 Token->setFinalizedType(TT_PointerOrReference);
1444 }
else if (Contexts.back().ContextType == Context::C11GenericSelection) {
1445 Tok->setType(TT_GenericSelectionColon);
1446 if (Prev->isPointerOrReference())
1447 Prev->setFinalizedType(TT_PointerOrReference);
1448 }
else if ((CurrentToken && CurrentToken->is(tok::numeric_constant)) ||
1449 (Prev->is(TT_StartOfName) && !Scopes.empty() &&
1451 Tok->setType(TT_BitFieldColon);
1452 }
else if (Contexts.size() == 1 &&
1453 Line.getFirstNonComment()->isNoneOf(tok::kw_enum, tok::kw_case,
1455 !
Line.startsWith(tok::kw_typedef, tok::kw_enum)) {
1456 if (Prev->isOneOf(tok::r_paren, tok::kw_noexcept) ||
1457 Prev->ClosesRequiresClause) {
1458 Tok->setType(TT_CtorInitializerColon);
1459 }
else if (Prev->is(tok::kw_try)) {
1461 FormatToken *PrevPrev = Prev->getPreviousNonComment();
1464 if (PrevPrev && PrevPrev->isOneOf(tok::r_paren, tok::kw_noexcept))
1465 Tok->setType(TT_CtorInitializerColon);
1467 Tok->setType(TT_InheritanceColon);
1468 if (Prev->isAccessSpecifierKeyword())
1471 }
else if (canBeObjCSelectorComponent(*Prev) &&
Next &&
1472 (
Next->isOneOf(tok::r_paren, tok::comma) ||
1473 (canBeObjCSelectorComponent(*
Next) &&
Next->Next &&
1474 Next->Next->is(tok::colon)))) {
1477 Tok->setType(TT_ObjCSelector);
1484 if (Style.isJavaScript() && !Contexts.back().IsExpression)
1485 Tok->setType(TT_JsTypeOperator);
1488 if (Style.isTableGen()) {
1490 if (!parseTableGenValue())
1492 if (CurrentToken && CurrentToken->is(Keywords.kw_then))
1497 CurrentToken->isOneOf(tok::kw_constexpr, tok::identifier)) {
1503 if (CurrentToken && CurrentToken->is(tok::l_paren)) {
1505 if (!parseParens(IsIf))
1510 if (Style.isJavaScript()) {
1512 if ((Prev && Prev->is(tok::period)) || (
Next &&
Next->is(tok::colon)))
1515 if (CurrentToken && CurrentToken->is(Keywords.kw_await))
1518 if (IsCpp && CurrentToken && CurrentToken->is(tok::kw_co_await))
1520 Contexts.back().ColonIsForRangeExpr =
true;
1521 if (!CurrentToken || CurrentToken->isNot(tok::l_paren))
1532 if (Prev && Prev->is(tok::r_paren) && !Prev->isTypeFinalized() &&
1533 Prev->MatchingParen &&
1534 Prev->MatchingParen->is(TT_OverloadedOperatorLParen)) {
1535 Prev->setType(TT_OverloadedOperator);
1536 Prev->MatchingParen->setType(TT_OverloadedOperator);
1537 Tok->setType(TT_OverloadedOperatorLParen);
1540 if (Style.isVerilog()) {
1546 auto IsInstancePort = [&]() {
1555 if (!Prev || !(PrevPrev = Prev->getPreviousNonComment()))
1558 if (Keywords.isVerilogIdentifier(*Prev) &&
1559 Keywords.isVerilogIdentifier(*PrevPrev)) {
1563 if (Prev->is(Keywords.kw_verilogHash) &&
1564 Keywords.isVerilogIdentifier(*PrevPrev)) {
1568 if (Keywords.isVerilogIdentifier(*Prev) && PrevPrev->is(tok::r_paren))
1571 if (Keywords.isVerilogIdentifier(*Prev) && PrevPrev->is(tok::comma)) {
1572 const FormatToken *PrevParen = PrevPrev->getPreviousNonComment();
1573 if (PrevParen && PrevParen->is(tok::r_paren) &&
1574 PrevParen->MatchingParen &&
1575 PrevParen->MatchingParen->is(TT_VerilogInstancePortLParen)) {
1582 if (IsInstancePort())
1583 Tok->setType(TT_VerilogInstancePortLParen);
1588 if (
Line.MustBeDeclaration && Contexts.size() == 1 &&
1589 !Contexts.back().IsExpression && !
Line.startsWith(TT_ObjCProperty) &&
1590 !
Line.startsWith(tok::l_paren) &&
1591 Tok->isNoneOf(TT_TypeDeclarationParen, TT_RequiresExpressionLParen)) {
1593 (!Prev->isAttribute() &&
1594 Prev->isNoneOf(TT_RequiresClause, TT_LeadingJavaAnnotation,
1595 TT_BinaryOperator))) {
1596 Line.MightBeFunctionDecl =
true;
1597 Tok->MightBeFunctionDeclParen =
true;
1602 if (Style.isTableGen())
1603 Tok->setType(TT_TableGenListOpener);
1609 if (
Tok->is(TT_RequiresExpressionLBrace))
1611 }
else if (Style.isTextProto()) {
1612 if (Prev && Prev->isNot(TT_DictLiteral))
1613 Prev->setType(TT_SelectorName);
1615 Scopes.push_back(getScopeType(*
Tok));
1621 Tok->setType(TT_TemplateOpener);
1627 if (Style.isTextProto() ||
1629 Prev->isOneOf(TT_SelectorName, TT_DictLiteral))) {
1630 Tok->setType(TT_DictLiteral);
1631 if (Prev && Prev->isNot(TT_DictLiteral))
1632 Prev->setType(TT_SelectorName);
1634 if (Style.isTableGen())
1635 Tok->setType(TT_TemplateOpener);
1637 Tok->setType(TT_BinaryOperator);
1638 NonTemplateLess.insert(
Tok);
1648 if (!Scopes.empty())
1655 if (!Style.isTextProto() &&
Tok->is(TT_Unknown))
1656 Tok->setType(TT_BinaryOperator);
1657 if (Prev && Prev->is(TT_TemplateCloser))
1658 Tok->SpacesRequiredBefore = 1;
1660 case tok::kw_operator:
1661 if (Style.isProto())
1664 if (IsCpp && CurrentToken) {
1665 const auto *Info = CurrentToken->Tok.getIdentifierInfo();
1667 if (Info && !(CurrentToken->isPlacementOperator() ||
1668 CurrentToken->is(tok::kw_co_await) ||
1669 Info->isCPlusPlusOperatorKeyword())) {
1671 if (CurrentToken->startsSequence(tok::kw_decltype, tok::l_paren,
1672 tok::kw_auto, tok::r_paren)) {
1674 LParen = CurrentToken->Next->Next->Next->Next;
1677 for (LParen = CurrentToken->Next;
1678 LParen && LParen->isNot(tok::l_paren); LParen = LParen->Next) {
1679 if (LParen->isPointerOrReference())
1680 LParen->setFinalizedType(TT_PointerOrReference);
1683 if (LParen && LParen->is(tok::l_paren)) {
1684 if (!Contexts.back().IsExpression) {
1685 Tok->setFinalizedType(TT_FunctionDeclarationName);
1686 LParen->setFinalizedType(TT_FunctionDeclarationLParen);
1692 while (CurrentToken &&
1693 CurrentToken->isNoneOf(tok::l_paren, tok::semi, tok::r_paren,
1695 if (CurrentToken->isOneOf(tok::star, tok::amp))
1696 CurrentToken->setType(TT_PointerOrReference);
1697 auto Next = CurrentToken->getNextNonComment();
1700 if (
Next->is(tok::less))
1706 auto Previous = CurrentToken->getPreviousNonComment();
1708 if (CurrentToken->is(tok::comma) &&
Previous->isNot(tok::kw_operator))
1710 if (
Previous->isOneOf(TT_BinaryOperator, TT_UnaryOperator, tok::comma,
1713 Previous->isPointerOrReference()) ||
1715 Previous->TokenText.starts_with(
"\"\"")) {
1716 Previous->setType(TT_OverloadedOperator);
1717 if (CurrentToken->isOneOf(tok::less, tok::greater))
1721 if (CurrentToken && CurrentToken->is(tok::l_paren))
1722 CurrentToken->setType(TT_OverloadedOperatorLParen);
1723 if (CurrentToken && CurrentToken->Previous->is(TT_BinaryOperator))
1724 CurrentToken->Previous->setType(TT_OverloadedOperator);
1727 if (Style.isJavaScript() &&
Next &&
1728 Next->isOneOf(tok::semi, tok::comma, tok::colon, tok::r_paren,
1729 tok::r_brace, tok::r_square)) {
1734 Tok->setType(TT_JsTypeOptionalQuestion);
1739 if (
Line.MustBeDeclaration && !Contexts.back().IsExpression &&
1740 Style.isJavaScript()) {
1743 if (Style.isCSharp()) {
1746 if (
Next && (
Next->isOneOf(tok::r_paren, tok::greater) ||
1747 Next->startsSequence(tok::identifier, tok::semi) ||
1748 Next->startsSequence(tok::identifier, tok::equal))) {
1749 Tok->setType(TT_CSharpNullable);
1758 if (!Contexts.back().IsExpression &&
Line.MustBeDeclaration &&
1759 (!
Next ||
Next->isNoneOf(tok::identifier, tok::string_literal) ||
1760 !
Next->Next ||
Next->Next->isNoneOf(tok::colon, tok::question))) {
1761 Tok->setType(TT_CSharpNullable);
1767 case tok::kw_template:
1768 parseTemplateDeclaration();
1771 switch (Contexts.back().ContextType) {
1772 case Context::CtorInitializer:
1773 Tok->setType(TT_CtorInitializerComma);
1775 case Context::InheritanceList:
1776 Tok->setType(TT_InheritanceComma);
1778 case Context::VerilogInstancePortList:
1779 Tok->setType(TT_VerilogInstancePortComma);
1782 if (Style.isVerilog() && Contexts.size() == 1 &&
1783 Line.startsWith(Keywords.kw_assign)) {
1784 Tok->setFinalizedType(TT_VerilogAssignComma);
1785 }
else if (Contexts.back().FirstStartOfName &&
1786 (Contexts.size() == 1 || startsWithInitStatement(
Line))) {
1787 Contexts.back().FirstStartOfName->PartOfMultiVariableDeclStmt =
true;
1788 Line.IsMultiVariableDeclStmt =
true;
1792 if (Contexts.back().ContextType == Context::ForEachMacro)
1793 Contexts.back().IsExpression =
true;
1795 case tok::kw_default:
1797 if (Style.isVerilog() && Keywords.isVerilogEndOfLabel(*
Tok) &&
1798 (
Line.Level > 1 || (!
Line.InPPDirective &&
Line.Level > 0))) {
1802 case tok::identifier:
1803 if (
Tok->isOneOf(Keywords.kw___has_include,
1804 Keywords.kw___has_include_next)) {
1808 if (
Next &&
Next->is(tok::l_paren) && Prev &&
1809 Prev->isOneOf(tok::kw___cdecl, tok::kw___stdcall,
1810 tok::kw___fastcall, tok::kw___thiscall,
1811 tok::kw___regcall, tok::kw___vectorcall)) {
1812 Tok->setFinalizedType(TT_FunctionDeclarationName);
1813 Next->setFinalizedType(TT_FunctionDeclarationLParen);
1815 }
else if (Style.isCSharp()) {
1816 if (
Tok->is(Keywords.kw_where) &&
Next &&
Next->isNot(tok::l_paren)) {
1817 Tok->setType(TT_CSharpGenericTypeConstraint);
1818 parseCSharpGenericTypeConstraint();
1820 Line.IsContinuation =
true;
1822 }
else if (Style.isTableGen()) {
1823 if (
Tok->is(Keywords.kw_assert)) {
1824 if (!parseTableGenValue())
1826 }
else if (
Tok->isOneOf(Keywords.kw_def, Keywords.kw_defm) &&
1827 (!
Next ||
Next->isNoneOf(tok::colon, tok::l_brace))) {
1829 if (!parseTableGenValue(
true))
1833 if (Style.AllowBreakBeforeQtProperty &&
1834 Contexts.back().ContextType == Context::QtProperty &&
1835 Tok->isQtProperty()) {
1836 Tok->setFinalizedType(TT_QtProperty);
1840 if (
Tok->isNot(TT_LambdaArrow) && Prev && Prev->is(tok::kw_noexcept))
1841 Tok->setType(TT_TrailingReturnArrow);
1845 if (Style.isTableGen() && !parseTableGenValue())
1847 if (!Scopes.empty() && Scopes.back() ==
ST_Enum)
1848 Tok->setFinalizedType(TT_EnumEqual);
1856 void parseCSharpGenericTypeConstraint() {
1857 int OpenAngleBracketsCount = 0;
1858 while (CurrentToken) {
1859 if (CurrentToken->is(tok::less)) {
1861 CurrentToken->setType(TT_TemplateOpener);
1862 ++OpenAngleBracketsCount;
1864 }
else if (CurrentToken->is(tok::greater)) {
1865 CurrentToken->setType(TT_TemplateCloser);
1866 --OpenAngleBracketsCount;
1868 }
else if (CurrentToken->is(tok::comma) && OpenAngleBracketsCount == 0) {
1871 CurrentToken->setType(TT_CSharpGenericTypeConstraintComma);
1873 }
else if (CurrentToken->is(Keywords.kw_where)) {
1874 CurrentToken->setType(TT_CSharpGenericTypeConstraint);
1876 }
else if (CurrentToken->is(tok::colon)) {
1877 CurrentToken->setType(TT_CSharpGenericTypeConstraintColon);
1885 void parseIncludeDirective() {
1886 if (CurrentToken && CurrentToken->is(tok::less)) {
1888 while (CurrentToken) {
1891 if (CurrentToken->isNot(tok::comment) &&
1892 !CurrentToken->TokenText.starts_with(
"//")) {
1893 CurrentToken->overwriteFixedType(TT_ImplicitStringLiteral);
1900 void parseWarningOrError() {
1905 while (CurrentToken) {
1906 CurrentToken->overwriteFixedType(TT_ImplicitStringLiteral);
1911 void parsePragma() {
1914 CurrentToken->isOneOf(Keywords.kw_mark, Keywords.kw_option,
1915 Keywords.kw_region)) {
1916 bool IsMarkOrRegion =
1917 CurrentToken->isOneOf(Keywords.kw_mark, Keywords.kw_region);
1920 while (CurrentToken) {
1921 if (IsMarkOrRegion || CurrentToken->Previous->is(TT_BinaryOperator))
1922 CurrentToken->overwriteFixedType(TT_ImplicitStringLiteral);
1928 void parseHasInclude() {
1929 if (!CurrentToken || CurrentToken->isNot(tok::l_paren))
1932 parseIncludeDirective();
1936 LineType parsePreprocessorDirective() {
1937 bool IsFirstToken = CurrentToken->IsFirst;
1943 if (Style.isJavaScript() && IsFirstToken) {
1947 while (CurrentToken) {
1949 CurrentToken->setType(TT_ImplicitStringLiteral);
1955 if (CurrentToken->is(tok::numeric_constant)) {
1956 CurrentToken->SpacesRequiredBefore = 1;
1961 if (!CurrentToken->Tok.getIdentifierInfo())
1965 if (Style.isVerilog() && !Keywords.isVerilogPPDirective(*CurrentToken))
1967 switch (CurrentToken->Tok.getIdentifierInfo()->getPPKeywordID()) {
1968 case tok::pp_include:
1969 case tok::pp_include_next:
1970 case tok::pp_import:
1972 parseIncludeDirective();
1976 case tok::pp_warning:
1977 parseWarningOrError();
1979 case tok::pp_pragma:
1984 Contexts.back().IsExpression =
true;
1987 CurrentToken->SpacesRequiredBefore = 1;
1993 while (CurrentToken) {
1996 if (
Tok->is(tok::l_paren)) {
1998 }
else if (
Tok->isOneOf(Keywords.kw___has_include,
1999 Keywords.kw___has_include_next)) {
2007 LineType parseLine() {
2010 NonTemplateLess.clear();
2011 if (!
Line.InMacroBody && CurrentToken->is(tok::hash)) {
2015 auto Type = parsePreprocessorDirective();
2023 IdentifierInfo *Info = CurrentToken->Tok.getIdentifierInfo();
2024 if ((Style.isJava() && CurrentToken->is(Keywords.kw_package)) ||
2025 (!Style.isVerilog() && Info &&
2026 Info->getPPKeywordID() == tok::pp_import && CurrentToken->Next &&
2027 CurrentToken->Next->isOneOf(tok::string_literal, tok::identifier,
2030 parseIncludeDirective();
2036 if (CurrentToken->is(tok::less) &&
Line.Last->is(tok::greater)) {
2037 parseIncludeDirective();
2044 CurrentToken->isOneOf(Keywords.kw_option, Keywords.kw_package)) {
2046 if (CurrentToken && CurrentToken->is(tok::identifier)) {
2047 while (CurrentToken)
2053 bool KeywordVirtualFound =
false;
2054 bool ImportStatement =
false;
2057 if (Style.isJavaScript() && CurrentToken->is(Keywords.kw_import))
2058 ImportStatement =
true;
2060 while (CurrentToken) {
2061 if (CurrentToken->is(tok::kw_virtual))
2062 KeywordVirtualFound =
true;
2063 if (Style.isJavaScript()) {
2070 if (
Line.First->is(tok::kw_export) &&
2071 CurrentToken->is(Keywords.kw_from) && CurrentToken->Next &&
2072 CurrentToken->Next->isStringLiteral()) {
2073 ImportStatement =
true;
2075 if (isClosureImportStatement(*CurrentToken))
2076 ImportStatement =
true;
2078 if (!consumeToken())
2086 if (KeywordVirtualFound)
2088 if (ImportStatement)
2091 if (
Line.startsWith(TT_ObjCMethodSpecifier)) {
2092 if (Contexts.back().FirstObjCSelectorName) {
2093 Contexts.back().FirstObjCSelectorName->LongestObjCSelectorName =
2094 Contexts.back().LongestObjCSelectorName;
2099 for (
const auto &ctx : Contexts)
2100 if (ctx.ContextType == Context::StructArrayInitializer)
2110 return Tok.TokenText ==
"goog" &&
Tok.Next &&
Tok.Next->is(tok::period) &&
2112 (
Tok.Next->Next->TokenText ==
"module" ||
2113 Tok.Next->Next->TokenText ==
"provide" ||
2114 Tok.Next->Next->TokenText ==
"require" ||
2115 Tok.Next->Next->TokenText ==
"requireType" ||
2116 Tok.Next->Next->TokenText ==
"forwardDeclare") &&
2117 Tok.Next->Next->Next &&
Tok.Next->Next->Next->is(tok::l_paren);
2120 void resetTokenMetadata() {
2126 if (!CurrentToken->isTypeFinalized() &&
2127 CurrentToken->isNoneOf(
2128 TT_LambdaLSquare, TT_LambdaLBrace, TT_AttributeMacro, TT_IfMacro,
2129 TT_ForEachMacro, TT_TypenameMacro, TT_FunctionLBrace,
2130 TT_ImplicitStringLiteral, TT_InlineASMBrace, TT_FatArrow,
2131 TT_LambdaArrow, TT_NamespaceMacro, TT_OverloadedOperator,
2132 TT_RegexLiteral, TT_TemplateString, TT_ObjCStringLiteral,
2133 TT_UntouchableMacroFunc, TT_StatementAttributeLikeMacro,
2134 TT_FunctionLikeOrFreestandingMacro, TT_ClassLBrace, TT_EnumLBrace,
2135 TT_RecordLBrace, TT_StructLBrace, TT_UnionLBrace, TT_RequiresClause,
2136 TT_RequiresClauseInARequiresExpression, TT_RequiresExpression,
2137 TT_RequiresExpressionLParen, TT_RequiresExpressionLBrace,
2138 TT_CompoundRequirementLBrace, TT_BracedListLBrace,
2139 TT_FunctionLikeMacro)) {
2140 CurrentToken->setType(TT_Unknown);
2142 CurrentToken->Role.reset();
2143 CurrentToken->MatchingParen =
nullptr;
2144 CurrentToken->FakeLParens.clear();
2145 CurrentToken->FakeRParens = 0;
2152 CurrentToken->NestingLevel = Contexts.size() - 1;
2153 CurrentToken->BindingStrength = Contexts.back().BindingStrength;
2154 modifyContext(*CurrentToken);
2155 determineTokenType(*CurrentToken);
2156 CurrentToken = CurrentToken->Next;
2158 resetTokenMetadata();
2167 IsExpression(IsExpression) {}
2173 bool ColonIsForRangeExpr =
false;
2174 bool ColonIsDictLiteral =
false;
2175 bool ColonIsObjCMethodExpr =
false;
2178 bool CanBeExpression =
true;
2179 bool CaretFound =
false;
2180 bool InCpp11AttributeSpecifier =
false;
2181 bool InCSharpAttributeSpecifier =
false;
2182 bool InStaticAssertFirstArgument =
false;
2183 bool VerilogAssignmentFound =
false;
2186 bool VerilogMayBeConcatenation =
false;
2187 bool IsTableGenDAGArgList =
false;
2188 bool IsTableGenBangOpe =
false;
2189 bool IsTableGenCondOpe =
false;
2202 StructArrayInitializer,
2206 C11GenericSelection,
2209 VerilogInstancePortList,
2215 struct ScopedContextCreator {
2216 AnnotatingParser &P;
2218 ScopedContextCreator(AnnotatingParser &P,
tok::TokenKind ContextKind,
2221 P.Contexts.push_back(Context(ContextKind,
2222 P.Contexts.back().BindingStrength + Increase,
2223 P.Contexts.back().IsExpression));
2226 ~ScopedContextCreator() {
2228 if (P.Contexts.back().ContextType == Context::StructArrayInitializer) {
2229 P.Contexts.pop_back();
2230 P.Contexts.back().ContextType = Context::StructArrayInitializer;
2234 P.Contexts.pop_back();
2239 auto AssignmentStartsExpression = [&]() {
2243 if (
Line.First->isOneOf(tok::kw_using, tok::kw_return))
2245 if (
Line.First->is(tok::kw_template)) {
2246 assert(Current.Previous);
2247 if (Current.Previous->is(tok::kw_operator)) {
2255 if (
Tok->isNot(TT_TemplateOpener)) {
2262 if (Contexts.back().ContextKind == tok::less) {
2263 assert(Current.Previous->Previous);
2264 return Current.Previous->Previous->isNoneOf(tok::kw_typename,
2268 Tok =
Tok->MatchingParen;
2271 Tok =
Tok->getNextNonComment();
2275 if (
Tok->isOneOf(tok::kw_class, tok::kw_enum, tok::kw_struct,
2285 if (Style.isJavaScript() &&
2286 (
Line.startsWith(Keywords.kw_type, tok::identifier) ||
2287 Line.startsWith(tok::kw_export, Keywords.kw_type,
2288 tok::identifier))) {
2292 return !Current.Previous || Current.Previous->isNot(tok::kw_operator);
2295 if (AssignmentStartsExpression()) {
2296 Contexts.back().IsExpression =
true;
2297 if (!
Line.startsWith(TT_UnaryOperator)) {
2300 Previous->Previous->isNoneOf(tok::comma, tok::semi);
2302 if (
Previous->isOneOf(tok::r_square, tok::r_paren, tok::greater)) {
2309 if (!
Previous->isTypeFinalized() &&
2310 Previous->isOneOf(TT_BinaryOperator, TT_UnaryOperator) &&
2312 Previous->Previous->isNot(tok::equal)) {
2313 Previous->setType(TT_PointerOrReference);
2317 }
else if (Current.is(tok::lessless) &&
2318 (!Current.Previous ||
2319 Current.Previous->isNot(tok::kw_operator))) {
2320 Contexts.back().IsExpression =
true;
2321 }
else if (Current.isOneOf(tok::kw_return, tok::kw_throw)) {
2322 Contexts.back().IsExpression =
true;
2323 }
else if (Current.is(TT_TrailingReturnArrow)) {
2324 Contexts.back().IsExpression =
false;
2325 }
else if (Current.isOneOf(TT_LambdaArrow, Keywords.kw_assert)) {
2326 Contexts.back().IsExpression = Style.isJava();
2327 }
else if (Current.Previous &&
2328 Current.Previous->is(TT_CtorInitializerColon)) {
2329 Contexts.back().IsExpression =
true;
2330 Contexts.back().ContextType = Context::CtorInitializer;
2331 }
else if (Current.Previous && Current.Previous->is(TT_InheritanceColon)) {
2332 Contexts.back().ContextType = Context::InheritanceList;
2333 }
else if (Current.isOneOf(tok::r_paren, tok::greater, tok::comma)) {
2337 Previous->setType(TT_PointerOrReference);
2339 if (
Line.MustBeDeclaration &&
2340 Contexts.front().ContextType != Context::CtorInitializer) {
2341 Contexts.back().IsExpression =
false;
2343 }
else if (Current.is(tok::kw_new)) {
2344 Contexts.back().CanBeExpression =
false;
2345 }
else if (Current.is(tok::semi) ||
2346 (Current.is(tok::exclaim) && Current.Previous &&
2347 Current.Previous->isNot(tok::kw_operator))) {
2351 Contexts.back().IsExpression =
true;
2359 if (Current->is(tok::l_paren))
2361 if (Current->is(tok::r_paren))
2365 Current = Current->Next;
2370 static bool isDeductionGuide(
FormatToken &Current) {
2372 if (Current.Previous && Current.Previous->is(tok::r_paren) &&
2373 Current.startsSequence(tok::arrow, tok::identifier, tok::less)) {
2377 while (TemplateCloser) {
2379 if (TemplateCloser->is(tok::l_paren)) {
2381 TemplateCloser = untilMatchingParen(TemplateCloser);
2382 if (!TemplateCloser)
2385 if (TemplateCloser->is(tok::less))
2387 if (TemplateCloser->is(tok::greater))
2391 TemplateCloser = TemplateCloser->Next;
2395 if (TemplateCloser && TemplateCloser->Next &&
2396 TemplateCloser->Next->is(tok::semi) &&
2397 Current.Previous->MatchingParen) {
2401 Current.Previous->MatchingParen->Previous;
2403 return LeadingIdentifier &&
2404 LeadingIdentifier->TokenText == Current.Next->TokenText;
2411 if (Current.isNot(TT_Unknown)) {
2416 if ((Style.isJavaScript() || Style.isCSharp()) &&
2417 Current.is(tok::exclaim)) {
2418 if (Current.Previous) {
2420 Style.isJavaScript()
2421 ? Keywords.isJavaScriptIdentifier(
2422 *Current.Previous,
true)
2423 : Current.Previous->is(tok::identifier);
2425 Current.Previous->isOneOf(
2426 tok::kw_default, tok::kw_namespace, tok::r_paren, tok::r_square,
2427 tok::r_brace, tok::kw_false, tok::kw_true, Keywords.kw_type,
2428 Keywords.kw_get, Keywords.kw_init, Keywords.kw_set) ||
2429 Current.Previous->Tok.isLiteral()) {
2430 Current.setType(TT_NonNullAssertion);
2435 Current.Next->isOneOf(TT_BinaryOperator, Keywords.kw_as)) {
2436 Current.setType(TT_NonNullAssertion);
2444 if ((Style.isJavaScript() || Style.isJava()) &&
2445 Current.is(Keywords.kw_instanceof)) {
2446 Current.setType(TT_BinaryOperator);
2447 }
else if (isStartOfName(Current) &&
2448 (!
Line.MightBeFunctionDecl || Current.NestingLevel != 0)) {
2449 Contexts.back().FirstStartOfName = &Current;
2450 Current.setType(TT_StartOfName);
2451 }
else if (Current.is(tok::semi)) {
2455 Contexts.back().FirstStartOfName =
nullptr;
2456 }
else if (Current.isOneOf(tok::kw_auto, tok::kw___auto_type)) {
2458 }
else if (Current.is(tok::arrow) && Style.isJava()) {
2459 Current.setType(TT_LambdaArrow);
2460 }
else if (Current.is(tok::arrow) && Style.isVerilog()) {
2462 Current.setType(TT_BinaryOperator);
2463 }
else if (Current.is(tok::arrow) && AutoFound &&
2464 Line.MightBeFunctionDecl && Current.NestingLevel == 0 &&
2465 Current.Previous->isNoneOf(tok::kw_operator, tok::identifier)) {
2467 Current.setType(TT_TrailingReturnArrow);
2468 }
else if (Current.is(tok::arrow) && Current.Previous &&
2469 Current.Previous->is(tok::r_brace) &&
2473 Current.setType(TT_TrailingReturnArrow);
2474 }
else if (isDeductionGuide(Current)) {
2476 Current.setType(TT_TrailingReturnArrow);
2477 }
else if (Current.isPointerOrReference()) {
2478 Current.setType(determineStarAmpUsage(
2480 (Contexts.back().CanBeExpression && Contexts.back().IsExpression) ||
2481 Contexts.back().InStaticAssertFirstArgument,
2482 Contexts.back().ContextType == Context::TemplateArgument));
2483 }
else if (Current.isOneOf(tok::minus, tok::plus, tok::caret) ||
2484 (Style.isVerilog() && Current.is(tok::pipe))) {
2485 Current.setType(determinePlusMinusCaretUsage(Current));
2486 if (Current.is(TT_UnaryOperator) && Current.is(tok::caret))
2487 Contexts.back().CaretFound =
true;
2488 }
else if (Current.isOneOf(tok::minusminus, tok::plusplus)) {
2489 Current.setType(determineIncrementUsage(Current));
2490 }
else if (Current.isOneOf(tok::exclaim, tok::tilde)) {
2491 Current.setType(TT_UnaryOperator);
2492 }
else if (Current.is(tok::question)) {
2493 if (Style.isJavaScript() &&
Line.MustBeDeclaration &&
2494 !Contexts.back().IsExpression) {
2497 Current.setType(TT_JsTypeOptionalQuestion);
2498 }
else if (Style.isTableGen()) {
2500 Current.setType(TT_Unknown);
2502 Current.setType(TT_ConditionalExpr);
2504 Contexts.back().IsExpression =
true;
2506 }
else if (Current.isBinaryOperator() &&
2507 (!Current.Previous || Current.Previous->isNot(tok::l_square)) &&
2508 (Current.isNot(tok::greater) && !Style.isTextProto())) {
2509 if (Style.isVerilog()) {
2510 if (Current.is(tok::lessequal) && Contexts.size() == 1 &&
2511 !Contexts.back().VerilogAssignmentFound) {
2515 Current.setFinalizedType(TT_BinaryOperator);
2518 Contexts.back().VerilogAssignmentFound =
true;
2520 Current.setType(TT_BinaryOperator);
2521 }
else if (Current.is(tok::comment)) {
2522 if (Current.TokenText.starts_with(
"/*")) {
2523 if (Current.TokenText.ends_with(
"*/")) {
2524 Current.setType(TT_BlockComment);
2528 Current.Tok.setKind(tok::unknown);
2531 Current.setType(TT_LineComment);
2533 }
else if (Current.is(tok::string_literal)) {
2534 if (Style.isVerilog() && Contexts.back().VerilogMayBeConcatenation &&
2535 Current.getPreviousNonComment() &&
2536 Current.getPreviousNonComment()->isOneOf(tok::comma, tok::l_brace) &&
2537 Current.getNextNonComment() &&
2538 Current.getNextNonComment()->isOneOf(tok::comma, tok::r_brace)) {
2539 Current.setType(TT_StringInConcatenation);
2541 }
else if (Current.is(tok::l_paren)) {
2542 if (lParenStartsCppCast(Current))
2543 Current.setType(TT_CppCastLParen);
2544 }
else if (Current.is(tok::r_paren)) {
2545 if (rParenEndsCast(Current))
2546 Current.setType(TT_CastRParen);
2547 if (Current.MatchingParen && Current.MatchingParen->is(TT_InlineASMParen))
2548 Current.setType(TT_InlineASMParen);
2549 if (Current.MatchingParen && Current.Next &&
2550 !Current.Next->isBinaryOperator() &&
2551 Current.Next->isNoneOf(
2552 tok::semi, tok::colon, tok::l_brace, tok::l_paren, tok::comma,
2553 tok::period, tok::arrow, tok::coloncolon, tok::kw_noexcept)) {
2554 if (
FormatToken *AfterParen = Current.MatchingParen->Next;
2555 AfterParen && AfterParen->isNot(tok::caret)) {
2557 if (
FormatToken *BeforeParen = Current.MatchingParen->Previous;
2558 BeforeParen && BeforeParen->is(tok::identifier) &&
2559 BeforeParen->isNot(TT_TypenameMacro) &&
2560 BeforeParen->TokenText == BeforeParen->TokenText.upper() &&
2561 (!BeforeParen->Previous ||
2562 BeforeParen->Previous->ClosesTemplateDeclaration ||
2563 BeforeParen->Previous->ClosesRequiresClause)) {
2564 Current.setType(TT_FunctionAnnotationRParen);
2568 }
else if (Current.is(tok::at) && Current.Next && !Style.isJavaScript() &&
2572 switch (Current.Next->Tok.getObjCKeywordID()) {
2573 case tok::objc_interface:
2574 case tok::objc_implementation:
2575 case tok::objc_protocol:
2576 Current.setType(TT_ObjCDecl);
2578 case tok::objc_property:
2579 Current.setType(TT_ObjCProperty);
2584 }
else if (Current.is(tok::period)) {
2585 FormatToken *PreviousNoComment = Current.getPreviousNonComment();
2586 if (PreviousNoComment &&
2587 PreviousNoComment->isOneOf(tok::comma, TT_TrailingCommaVaOptRParen,
2589 Current.setType(TT_DesignatedInitializerPeriod);
2590 }
else if (Style.isJava() && Current.Previous &&
2591 Current.Previous->isOneOf(TT_JavaAnnotation,
2592 TT_LeadingJavaAnnotation)) {
2593 Current.setType(Current.Previous->getType());
2595 }
else if (canBeObjCSelectorComponent(Current) &&
2598 Current.Previous && Current.Previous->is(TT_CastRParen) &&
2599 Current.Previous->MatchingParen &&
2600 Current.Previous->MatchingParen->Previous &&
2601 Current.Previous->MatchingParen->Previous->is(
2602 TT_ObjCMethodSpecifier)) {
2606 Current.setType(TT_SelectorName);
2607 }
else if (Current.isOneOf(tok::identifier, tok::kw_const, tok::kw_noexcept,
2608 tok::kw_requires) &&
2610 Current.Previous->isNoneOf(tok::equal, tok::at,
2611 TT_CtorInitializerComma,
2612 TT_CtorInitializerColon) &&
2613 Line.MightBeFunctionDecl && Contexts.size() == 1) {
2616 Current.setType(TT_TrailingAnnotation);
2617 }
else if ((Style.isJava() || Style.isJavaScript()) && Current.Previous) {
2618 if (Current.Previous->is(tok::at) &&
2619 Current.isNot(Keywords.kw_interface)) {
2623 Current.setType(TT_LeadingJavaAnnotation);
2625 Current.setType(TT_JavaAnnotation);
2626 }
else if (Current.Previous->is(tok::period) &&
2627 Current.Previous->isOneOf(TT_JavaAnnotation,
2628 TT_LeadingJavaAnnotation)) {
2629 Current.setType(Current.Previous->getType());
2641 if (Style.isVerilog())
2644 if (!
Tok.Previous ||
Tok.isNot(tok::identifier) ||
Tok.is(TT_ClassHeadName))
2647 if (
Tok.endsSequence(Keywords.kw_final, TT_ClassHeadName))
2650 if ((Style.isJavaScript() || Style.isJava()) &&
Tok.is(Keywords.kw_extends))
2653 if (
const auto *NextNonComment =
Tok.getNextNonComment();
2654 (!NextNonComment && !
Line.InMacroBody) ||
2656 (NextNonComment->isPointerOrReference() ||
2657 NextNonComment->isOneOf(TT_ClassHeadName, tok::string_literal) ||
2658 (
Line.InPragmaDirective && NextNonComment->is(tok::identifier))))) {
2662 if (
Tok.Previous->isOneOf(TT_LeadingJavaAnnotation, Keywords.kw_instanceof,
2666 if (Style.isJavaScript() &&
Tok.Previous->is(Keywords.kw_in))
2673 if (!Style.isJavaScript())
2674 while (PreviousNotConst && PreviousNotConst->is(tok::kw_const))
2675 PreviousNotConst = PreviousNotConst->getPreviousNonComment();
2677 if (!PreviousNotConst)
2680 if (PreviousNotConst->ClosesRequiresClause)
2683 if (Style.isTableGen()) {
2685 if (Keywords.isTableGenDefinition(*PreviousNotConst))
2688 if (Contexts.back().ContextKind != tok::l_brace)
2692 bool IsPPKeyword = PreviousNotConst->is(tok::identifier) &&
2693 PreviousNotConst->Previous &&
2694 PreviousNotConst->Previous->is(tok::hash);
2696 if (PreviousNotConst->is(TT_TemplateCloser)) {
2697 return PreviousNotConst && PreviousNotConst->MatchingParen &&
2698 PreviousNotConst->MatchingParen->Previous &&
2699 PreviousNotConst->MatchingParen->Previous->isNoneOf(
2700 tok::period, tok::kw_template);
2703 if ((PreviousNotConst->is(tok::r_paren) &&
2704 PreviousNotConst->is(TT_TypeDeclarationParen)) ||
2705 PreviousNotConst->is(TT_AttributeRParen)) {
2714 if (PreviousNotConst->isOneOf(tok::identifier, tok::kw_auto) &&
2715 !PreviousNotConst->endsSequence(Keywords.kw_import, tok::kw_export) &&
2716 PreviousNotConst->isNot(TT_StatementAttributeLikeMacro)) {
2721 if (PreviousNotConst->is(TT_PointerOrReference) ||
2722 PreviousNotConst->endsSequence(tok::coloncolon,
2723 TT_PointerOrReference)) {
2728 if (PreviousNotConst->isTypeName(LangOpts))
2732 if (Style.isJava() && PreviousNotConst->is(tok::r_square))
2736 return Style.isJavaScript() && PreviousNotConst->is(tok::kw_const);
2746 if (LeftOfParens && LeftOfParens->is(TT_TemplateCloser) &&
2747 LeftOfParens->MatchingParen) {
2748 auto *Prev = LeftOfParens->MatchingParen->getPreviousNonComment();
2750 Prev->isOneOf(tok::kw_const_cast, tok::kw_dynamic_cast,
2751 tok::kw_reinterpret_cast, tok::kw_static_cast)) {
2762 assert(
Tok.is(tok::r_paren));
2764 if (!
Tok.MatchingParen || !
Tok.Previous)
2768 if (!IsCpp && !Style.isCSharp() && !Style.isJava())
2771 const auto *LParen =
Tok.MatchingParen;
2772 const auto *BeforeRParen =
Tok.Previous;
2773 const auto *AfterRParen =
Tok.Next;
2776 if (BeforeRParen == LParen || !AfterRParen)
2779 if (LParen->isOneOf(TT_OverloadedOperatorLParen, TT_FunctionTypeLParen))
2782 auto *LeftOfParens = LParen->getPreviousNonComment();
2786 if (LeftOfParens->is(tok::r_paren) &&
2787 LeftOfParens->isNoneOf(TT_CastRParen, TT_TrailingCommaVaOptRParen)) {
2788 if (!LeftOfParens->MatchingParen ||
2789 !LeftOfParens->MatchingParen->Previous) {
2792 LeftOfParens = LeftOfParens->MatchingParen->Previous;
2795 if (LeftOfParens->is(tok::r_square)) {
2798 if (
Tok->isNot(tok::r_square))
2801 Tok =
Tok->getPreviousNonComment();
2802 if (!
Tok ||
Tok->isNot(tok::l_square))
2805 Tok =
Tok->getPreviousNonComment();
2806 if (!
Tok ||
Tok->isNot(tok::kw_delete))
2810 if (
FormatToken *MaybeDelete = MayBeArrayDelete(LeftOfParens))
2811 LeftOfParens = MaybeDelete;
2817 if (LeftOfParens->Tok.getIdentifierInfo() && LeftOfParens->Previous &&
2818 LeftOfParens->Previous->is(tok::kw_operator)) {
2824 if (LeftOfParens->Tok.getIdentifierInfo() &&
2825 LeftOfParens->isNoneOf(TT_ObjCForIn, tok::kw_return, tok::kw_case,
2826 tok::kw_delete, tok::kw_throw)) {
2832 if (LeftOfParens->isOneOf(tok::at, tok::r_square, TT_OverloadedOperator,
2833 TT_TemplateCloser, tok::ellipsis)) {
2838 if (AfterRParen->is(tok::question) ||
2839 (AfterRParen->is(tok::ampamp) && !BeforeRParen->isTypeName(LangOpts))) {
2844 if (AfterRParen->is(Keywords.kw_in) && Style.isCSharp())
2849 if (AfterRParen->isOneOf(tok::kw_noexcept, tok::kw_volatile, tok::kw_const,
2850 tok::kw_requires, tok::kw_throw, tok::arrow,
2851 Keywords.kw_override, Keywords.kw_final) ||
2852 isCppAttribute(IsCpp, *AfterRParen)) {
2858 if (Style.isJava() && AfterRParen->is(tok::l_paren))
2862 if (AfterRParen->isOneOf(tok::kw_sizeof, tok::kw_alignof) ||
2863 (AfterRParen->Tok.isLiteral() &&
2864 AfterRParen->isNot(tok::string_literal))) {
2869 if (
Tok.isNot(TT_TemplateCloser))
2871 const auto *
Less =
Tok.MatchingParen;
2874 const auto *BeforeLess =
Less->getPreviousNonComment();
2875 return BeforeLess && BeforeLess->isNot(TT_VariableTemplate);
2879 auto IsQualifiedPointerOrReference = [](
const FormatToken *T,
2880 const LangOptions &LangOpts) {
2882 assert(!T->isTypeName(LangOpts) &&
"Should have already been checked");
2886 if (T->is(TT_AttributeRParen)) {
2888 assert(T->is(tok::r_paren));
2889 assert(T->MatchingParen);
2890 assert(T->MatchingParen->is(tok::l_paren));
2891 assert(T->MatchingParen->is(TT_AttributeLParen));
2892 if (
const auto *
Tok = T->MatchingParen->Previous;
2893 Tok &&
Tok->isAttribute()) {
2897 }
else if (T->is(TT_AttributeRSquare)) {
2899 if (T->MatchingParen && T->MatchingParen->Previous) {
2900 T = T->MatchingParen->Previous;
2903 }
else if (T->canBePointerOrReferenceQualifier()) {
2909 return T && T->is(TT_PointerOrReference);
2912 bool ParensAreType = IsNonVariableTemplate(*BeforeRParen) ||
2913 BeforeRParen->is(TT_TypeDeclarationParen) ||
2914 BeforeRParen->isTypeName(LangOpts) ||
2915 IsQualifiedPointerOrReference(BeforeRParen, LangOpts);
2916 bool ParensCouldEndDecl =
2917 AfterRParen->isOneOf(tok::equal, tok::semi, tok::l_brace, tok::greater);
2918 if (ParensAreType && !ParensCouldEndDecl)
2929 for (
const auto *Token = LParen->Next; Token != &
Tok; Token = Token->Next)
2930 if (Token->is(TT_BinaryOperator))
2935 if (AfterRParen->isOneOf(tok::identifier, tok::kw_this))
2939 if (AfterRParen->is(tok::l_paren)) {
2940 for (
const auto *Prev = BeforeRParen; Prev->is(tok::identifier);) {
2941 Prev = Prev->Previous;
2942 if (Prev->is(tok::coloncolon))
2943 Prev = Prev->Previous;
2949 if (!AfterRParen->Next)
2962 const bool NextIsAmpOrStar = AfterRParen->isOneOf(tok::amp, tok::star);
2963 if (!(AfterRParen->isUnaryOperator() || NextIsAmpOrStar) ||
2964 AfterRParen->is(tok::plus) ||
2965 AfterRParen->Next->isNoneOf(tok::identifier, tok::numeric_constant)) {
2969 if (NextIsAmpOrStar &&
2970 (AfterRParen->Next->is(tok::numeric_constant) ||
Line.InPPDirective)) {
2974 if (
Line.InPPDirective && AfterRParen->is(tok::minus))
2977 const auto *Prev = BeforeRParen;
2980 if (Prev->is(tok::r_paren)) {
2981 if (Prev->is(TT_CastRParen))
2983 Prev = Prev->MatchingParen;
2986 Prev = Prev->Previous;
2987 if (!Prev || Prev->isNot(tok::r_paren))
2989 Prev = Prev->MatchingParen;
2990 return Prev && Prev->is(TT_FunctionTypeLParen);
2994 for (Prev = BeforeRParen; Prev != LParen; Prev = Prev->Previous)
2995 if (Prev->isNoneOf(tok::kw_const, tok::identifier, tok::coloncolon))
3013 if (PrevToken->isOneOf(TT_ConditionalExpr, TT_TrailingCommaVaOptRParen,
3014 tok::l_paren, tok::comma, tok::colon, tok::semi,
3015 tok::equal, tok::question, tok::l_square,
3016 tok::l_brace, tok::kw_case, tok::kw_co_await,
3017 tok::kw_co_return, tok::kw_co_yield, tok::kw_delete,
3018 tok::kw_return, tok::kw_throw)) {
3025 if (PrevToken->is(tok::kw_sizeof))
3029 if (PrevToken->isOneOf(TT_CastRParen, TT_UnaryOperator))
3033 if (PrevToken->is(TT_BinaryOperator))
3040 TokenType determineStarAmpUsage(
const FormatToken &
Tok,
bool IsExpression,
3041 bool InTemplateArgument) {
3042 if (Style.isJavaScript())
3043 return TT_BinaryOperator;
3046 if (Style.isCSharp() &&
Tok.is(tok::ampamp))
3047 return TT_BinaryOperator;
3049 if (Style.isVerilog()) {
3054 if (
Tok.is(tok::star))
3055 return TT_BinaryOperator;
3056 return determineUnaryOperatorByUsage(
Tok) ? TT_UnaryOperator
3057 : TT_BinaryOperator;
3062 return TT_UnaryOperator;
3063 if (PrevToken->isTypeName(LangOpts))
3064 return TT_PointerOrReference;
3065 if (PrevToken->isPlacementOperator() &&
Tok.is(tok::ampamp))
3066 return TT_BinaryOperator;
3068 auto *NextToken =
Tok.getNextNonComment();
3070 return TT_PointerOrReference;
3071 if (NextToken->is(tok::greater))
3072 return TT_PointerOrReference;
3074 if (InTemplateArgument && NextToken->is(tok::kw_noexcept))
3075 return TT_BinaryOperator;
3077 if (NextToken->isOneOf(tok::arrow, tok::equal, tok::comma, tok::r_paren,
3078 tok::semi, TT_RequiresClause) ||
3079 (NextToken->is(tok::kw_noexcept) && !IsExpression) ||
3080 NextToken->canBePointerOrReferenceQualifier() ||
3081 (NextToken->is(tok::l_brace) && !NextToken->getNextNonComment())) {
3082 return TT_PointerOrReference;
3085 if (PrevToken->is(tok::coloncolon))
3086 return TT_PointerOrReference;
3088 if (PrevToken->is(tok::r_paren) && PrevToken->is(TT_TypeDeclarationParen))
3089 return TT_PointerOrReference;
3091 if (determineUnaryOperatorByUsage(
Tok))
3092 return TT_UnaryOperator;
3094 if (NextToken->is(tok::l_square) && NextToken->isNot(TT_LambdaLSquare))
3095 return TT_PointerOrReference;
3096 if (NextToken->is(tok::kw_operator) && !IsExpression)
3097 return TT_PointerOrReference;
3109 if (PrevToken->is(tok::r_brace) &&
Tok.is(tok::star) &&
3110 !PrevToken->MatchingParen) {
3111 return TT_PointerOrReference;
3114 if (PrevToken->endsSequence(tok::r_square, tok::l_square, tok::kw_delete))
3115 return TT_UnaryOperator;
3117 if (PrevToken->Tok.isLiteral() ||
3118 PrevToken->isOneOf(tok::r_paren, tok::r_square, tok::kw_true,
3119 tok::kw_false, tok::r_brace)) {
3120 return TT_BinaryOperator;
3124 while (NextNonParen && NextNonParen->is(tok::l_paren))
3125 NextNonParen = NextNonParen->getNextNonComment();
3126 if (NextNonParen && (NextNonParen->Tok.isLiteral() ||
3127 NextNonParen->isOneOf(tok::kw_true, tok::kw_false) ||
3128 NextNonParen->isUnaryOperator())) {
3129 return TT_BinaryOperator;
3135 if (InTemplateArgument && NextToken->Tok.isAnyIdentifier())
3136 return TT_BinaryOperator;
3140 if (
Tok.is(tok::ampamp) &&
3141 NextToken->isOneOf(tok::l_paren, tok::star, tok::amp)) {
3142 return TT_BinaryOperator;
3149 if (NextToken->Tok.isAnyIdentifier()) {
3150 auto *NextNextToken = NextToken->getNextNonComment();
3151 if (NextNextToken) {
3152 if (NextNextToken->is(tok::arrow))
3153 return TT_BinaryOperator;
3154 if (NextNextToken->isPointerOrReference() &&
3155 !NextToken->isObjCLifetimeQualifier(Style)) {
3156 NextNextToken->setFinalizedType(TT_BinaryOperator);
3157 return TT_BinaryOperator;
3164 if (IsExpression && !Contexts.back().CaretFound &&
3165 Line.getFirstNonComment()->isNot(
3166 TT_RequiresClauseInARequiresExpression)) {
3167 return TT_BinaryOperator;
3171 if (!Scopes.empty() && Scopes.back() ==
ST_Class)
3172 return TT_PointerOrReference;
3175 auto IsChainedOperatorAmpOrMember = [](
const FormatToken *token) {
3176 return !token || token->isOneOf(tok::amp, tok::period, tok::arrow,
3177 tok::arrowstar, tok::periodstar);
3182 if (
Tok.is(tok::amp) && PrevToken->Tok.isAnyIdentifier() &&
3183 IsChainedOperatorAmpOrMember(PrevToken->getPreviousNonComment()) &&
3184 NextToken && NextToken->Tok.isAnyIdentifier()) {
3185 if (
auto NextNext = NextToken->getNextNonComment();
3187 (IsChainedOperatorAmpOrMember(NextNext) || NextNext->is(tok::semi))) {
3188 return TT_BinaryOperator;
3194 return TT_BinaryOperator;
3197 return TT_PointerOrReference;
3201 if (determineUnaryOperatorByUsage(
Tok))
3202 return TT_UnaryOperator;
3206 return TT_UnaryOperator;
3208 if (PrevToken->is(tok::at))
3209 return TT_UnaryOperator;
3212 return TT_BinaryOperator;
3219 PrevToken->isOneOf(TT_CastRParen, TT_TrailingCommaVaOptRParen)) {
3220 return TT_UnaryOperator;
3222 if (PrevToken->isOneOf(tok::r_paren, tok::r_square, tok::identifier))
3223 return TT_TrailingUnaryOperator;
3225 return TT_UnaryOperator;
3231 AnnotatedLine &
Line;
3235 LangOptions LangOpts;
3236 const AdditionalKeywords &Keywords;
3246 int TemplateDeclarationDepth;
3254class ExpressionParser {
3256 ExpressionParser(
const FormatStyle &Style,
const AdditionalKeywords &Keywords,
3257 AnnotatedLine &
Line)
3258 : Style(Style), Keywords(Keywords),
Line(
Line), Current(
Line.First) {}
3261 void parse(
int Precedence = 0) {
3264 while (Current && (Current->is(tok::kw_return) ||
3265 (Current->is(tok::colon) &&
3266 Current->isOneOf(TT_ObjCMethodExpr, TT_DictLiteral)))) {
3270 if (!Current || Precedence > PrecedenceArrowAndPeriod)
3275 parseConditionalExpr();
3281 if (Precedence == PrecedenceUnaryOperator) {
3282 parseUnaryOperator();
3299 if (Style.isVerilog() && Precedence ==
prec::Comma) {
3300 VerilogFirstOfType =
3301 verilogGroupDecl(VerilogFirstOfType, LatestOperator);
3305 parse(Precedence + 1);
3308 if (TrailingCommaRParen) {
3312 LatestOperator->NextOperator = TrailingCommaRParen;
3313 LatestOperator = TrailingCommaRParen;
3315 TrailingCommaRParen =
nullptr;
3319 int CurrentPrecedence = getCurrentPrecedence();
3328 if (Style.BreakBinaryOperations.PerOperator.empty() &&
3329 Style.BreakBinaryOperations.Default ==
3335 if (Precedence == CurrentPrecedence && Current &&
3336 Current->is(TT_SelectorName)) {
3338 addFakeParenthesis(Start,
prec::Level(Precedence));
3342 if ((Style.isCSharp() || Style.isJavaScript() || Style.isJava()) &&
3346 FormatToken *Prev = Current->getPreviousNonComment();
3347 if (Prev && Prev->is(tok::string_literal) &&
3348 (Prev == Start || Prev->endsSequence(tok::string_literal, tok::plus,
3349 TT_StringInConcatenation))) {
3350 Prev->setType(TT_StringInConcatenation);
3357 (Current->closesScope() &&
3358 (Current->MatchingParen || Current->is(TT_TemplateString))) ||
3359 (CurrentPrecedence != -1 && CurrentPrecedence < Precedence) ||
3368 if (Current->opensScope() ||
3369 Current->isOneOf(TT_RequiresClause,
3370 TT_RequiresClauseInARequiresExpression)) {
3373 while (Current && (!Current->closesScope() || Current->opensScope())) {
3377 if (Current && Current->is(TT_TrailingCommaVaOptRParen))
3378 TrailingCommaRParen = Current;
3382 if (CurrentPrecedence == Precedence) {
3384 LatestOperator->NextOperator = Current;
3385 LatestOperator = Current;
3389 next(Precedence > 0);
3394 if (Style.isVerilog() && Precedence ==
prec::Comma && VerilogFirstOfType)
3395 addFakeParenthesis(VerilogFirstOfType,
prec::Comma);
3397 if (LatestOperator && (Current || Precedence > 0)) {
3403 Start->Previous->isOneOf(TT_RequiresClause,
3404 TT_RequiresClauseInARequiresExpression))
3406 auto Ret = Current ? Current :
Line.Last;
3407 while (!Ret->ClosesRequiresClause && Ret->Previous)
3408 Ret = Ret->Previous;
3413 if (Precedence == PrecedenceArrowAndPeriod) {
3417 addFakeParenthesis(Start,
prec::Level(Precedence), End);
3425 int getCurrentPrecedence() {
3427 const FormatToken *NextNonComment = Current->getNextNonComment();
3428 if (Current->is(TT_ConditionalExpr))
3430 if (NextNonComment && Current->is(TT_SelectorName) &&
3431 (NextNonComment->isOneOf(TT_DictLiteral, TT_JsTypeColon) ||
3432 (Style.isProto() && NextNonComment->is(tok::less)))) {
3435 if (Current->is(TT_JsComputedPropertyName))
3437 if (Current->is(TT_LambdaArrow))
3439 if (Current->is(TT_FatArrow))
3441 if (Current->isOneOf(tok::semi, TT_InlineASMColon, TT_SelectorName) ||
3442 (Current->is(tok::comment) && NextNonComment &&
3443 NextNonComment->is(TT_SelectorName))) {
3446 if (Current->is(TT_RangeBasedForLoopColon))
3448 if ((Style.isJava() || Style.isJavaScript()) &&
3449 Current->is(Keywords.kw_instanceof)) {
3452 if (Style.isJavaScript() &&
3453 Current->isOneOf(Keywords.kw_in, Keywords.kw_as)) {
3456 if (Current->isOneOf(TT_BinaryOperator, TT_EnumEqual, tok::comma))
3457 return Current->getPrecedence();
3458 if (Current->isOneOf(tok::period, tok::arrow) &&
3459 Current->isNot(TT_TrailingReturnArrow)) {
3460 return PrecedenceArrowAndPeriod;
3462 if ((Style.isJava() || Style.isJavaScript()) &&
3463 Current->isOneOf(Keywords.kw_extends, Keywords.kw_implements,
3464 Keywords.kw_throws)) {
3469 if (Style.isVerilog() && Current->is(tok::colon))
3481 if (Start->MacroParent)
3484 Start->FakeLParens.push_back(Precedence);
3486 Start->StartsBinaryExpression =
true;
3487 if (!End && Current)
3488 End = Current->getPreviousNonComment();
3492 End->EndsBinaryExpression =
true;
3498 void parseUnaryOperator() {
3500 while (Current && Current->is(TT_UnaryOperator)) {
3501 Tokens.push_back(Current);
3504 parse(PrecedenceArrowAndPeriod);
3511 void parseConditionalExpr() {
3512 while (Current && Current->isTrailingComment())
3516 if (!Current || Current->isNot(tok::question))
3520 if (!Current || Current->isNot(TT_ConditionalExpr))
3527 void next(
bool SkipPastLeadingComments =
true) {
3529 Current = Current->Next;
3531 (Current->NewlinesBefore == 0 || SkipPastLeadingComments) &&
3532 Current->isTrailingComment()) {
3533 Current = Current->Next;
3547 while (Start->startsSequence(tok::l_paren, tok::star)) {
3548 if (!(Start = Start->MatchingParen) ||
3549 !(Start = Start->getNextNonComment())) {
3556 if (
Tok->is(Keywords.kw_assign))
3557 Tok =
Tok->getNextNonComment();
3569 if (
Tok->is(tok::hash)) {
3574 Tok =
Tok->getNextNonComment();
3575 }
else if (
Tok->is(tok::hashhash)) {
3579 Tok =
Tok->getNextNonComment();
3580 }
else if (Keywords.isVerilogQualifier(*
Tok) ||
3581 Keywords.isVerilogIdentifier(*
Tok)) {
3585 while (
Tok &&
Tok->isOneOf(tok::period, tok::coloncolon) &&
3586 (
Tok =
Tok->getNextNonComment())) {
3587 if (Keywords.isVerilogIdentifier(*
Tok))
3588 Tok =
Tok->getNextNonComment();
3592 }
else if (
Tok->is(tok::l_paren)) {
3597 Keywords.kw_highz0, Keywords.kw_highz1, Keywords.kw_large,
3598 Keywords.kw_medium, Keywords.kw_pull0, Keywords.kw_pull1,
3599 Keywords.kw_small, Keywords.kw_strong0, Keywords.kw_strong1,
3600 Keywords.kw_supply0, Keywords.kw_supply1, Keywords.kw_weak0,
3601 Keywords.kw_weak1)) {
3602 Tok->setType(TT_VerilogStrength);
3603 Tok =
Tok->MatchingParen;
3605 Tok->setType(TT_VerilogStrength);
3606 Tok =
Tok->getNextNonComment();
3611 }
else if (
Tok->is(Keywords.kw_verilogHash)) {
3613 if (
Next->is(tok::l_paren))
3616 Tok =
Next->getNextNonComment();
3625 while (
Tok &&
Tok->is(tok::l_square) && (
Tok =
Tok->MatchingParen))
3626 Tok =
Tok->getNextNonComment();
3627 if (
Tok && (
Tok->is(tok::hash) || Keywords.isVerilogIdentifier(*
Tok)))
3636 First->setType(TT_VerilogDimensionedTypeName);
3637 }
else if (
First != Start) {
3645 if (TypedName->is(TT_Unknown))
3646 TypedName->setType(TT_StartOfName);
3648 if (FirstOfType && PreviousComma) {
3649 PreviousComma->setType(TT_VerilogTypeComma);
3650 addFakeParenthesis(FirstOfType,
prec::Comma, PreviousComma->Previous);
3653 FirstOfType = TypedName;
3660 while (Current && Current != FirstOfType) {
3661 if (Current->opensScope()) {
3673 const AdditionalKeywords &Keywords;
3674 const AnnotatedLine &
Line;
3686 assert(
Line->First);
3690 if (
const auto Column =
Line->First->OriginalColumn;
3694 const bool PPDirectiveOrImportStmt =
3697 if (PPDirectiveOrImportStmt)
3699 if (
const auto IndentWidth = Style.IndentWidth;
3701 Column % IndentWidth == 0) {
3709 PPDirectiveOrImportStmt
3711 : NextNonCommentLine->
Level;
3714 NextNonCommentLine =
Line->First->isNot(tok::r_brace) ?
Line :
nullptr;
3734 if (
Tok->isNot(tok::identifier))
3737 Tok =
Tok->getNextNonComment();
3743 if (
Tok->is(tok::coloncolon))
3744 return Tok->getNextNonComment();
3748 if (
Tok->is(TT_TemplateOpener)) {
3749 Tok =
Tok->MatchingParen;
3753 Tok =
Tok->getNextNonComment();
3758 return Tok->is(tok::coloncolon) ?
Tok->getNextNonComment() :
nullptr;
3766 Tok =
Tok->getNextNonComment()) {
3768 if (
Tok->is(TT_AttributeLSquare)) {
3769 Tok =
Tok->MatchingParen;
3777 if (
Tok->is(tok::l_paren) &&
Tok->is(TT_Unknown) &&
Tok->MatchingParen) {
3785 if (
Tok->isOneOf(tok::kw_friend, tok::kw_inline, tok::kw_virtual,
3786 tok::kw_constexpr, tok::kw_consteval, tok::kw_explicit)) {
3792 if (
Tok->is(tok::kw_template)) {
3793 Tok =
Tok->getNextNonComment();
3799 if (
Tok->isNot(TT_TemplateOpener))
3802 Tok =
Tok->MatchingParen;
3810 if (
Tok->is(tok::coloncolon)) {
3821 if (
Tok->is(tok::tilde)) {
3828 if (
Tok->isNot(tok::identifier) ||
Tok->isNot(TT_Unknown))
3839 assert(
Tok &&
Tok->is(tok::identifier));
3840 const auto *Prev =
Tok->Previous;
3842 if (Prev && Prev->is(tok::tilde))
3843 Prev = Prev->Previous;
3846 if (!Prev || (!Prev->endsSequence(tok::coloncolon, tok::identifier) &&
3847 !Prev->endsSequence(tok::coloncolon, TT_TemplateCloser))) {
3851 assert(Prev->Previous);
3852 if (Prev->Previous->is(TT_TemplateCloser) && Prev->Previous->MatchingParen) {
3853 Prev = Prev->Previous->MatchingParen;
3854 assert(Prev->Previous);
3857 return Prev->Previous->TokenText ==
Tok->TokenText;
3861 if (!
Line.InMacroBody)
3862 MacroBodyScopes.clear();
3864 auto &ScopeStack =
Line.InMacroBody ? MacroBodyScopes : Scopes;
3865 AnnotatingParser
Parser(Style,
Line, Keywords, ScopeStack);
3868 if (!
Line.Children.empty()) {
3871 for (
auto &Child :
Line.Children) {
3872 if (InRequiresExpression &&
3873 Child->First->isNoneOf(tok::kw_typename, tok::kw_requires,
3874 TT_CompoundRequirementLBrace)) {
3880 if (!ScopeStack.empty())
3881 ScopeStack.pop_back();
3894 ExpressionParser ExprParser(Style, Keywords,
Line);
3900 if (
Tok && ((!ScopeStack.empty() && ScopeStack.back() ==
ST_Class) ||
3902 Tok->setFinalizedType(TT_CtorDtorDeclName);
3903 assert(OpeningParen);
3908 if (
Line.startsWith(TT_ObjCMethodSpecifier))
3910 else if (
Line.startsWith(TT_ObjCDecl))
3912 else if (
Line.startsWith(TT_ObjCProperty))
3916 First->SpacesRequiredBefore = 1;
3917 First->CanBreakBefore =
First->MustBreakBefore;
3926 if (Current.
is(TT_FunctionDeclarationName))
3929 if (Current.
isNoneOf(tok::identifier, tok::kw_operator))
3932 const auto *Prev = Current.getPreviousNonComment();
3937 if (
const auto *PrevPrev =
Previous.getPreviousNonComment();
3938 PrevPrev && PrevPrev->is(TT_ObjCDecl)) {
3942 auto skipOperatorName =
3945 if (
Next->is(TT_OverloadedOperatorLParen))
3947 if (
Next->is(TT_OverloadedOperator))
3949 if (
Next->isPlacementOperator() ||
Next->is(tok::kw_co_await)) {
3952 Next->Next->startsSequence(tok::l_square, tok::r_square)) {
3957 if (
Next->startsSequence(tok::l_square, tok::r_square)) {
3962 if ((
Next->isTypeName(LangOpts) ||
Next->is(tok::identifier)) &&
3963 Next->Next &&
Next->Next->isPointerOrReference()) {
3968 if (
Next->is(TT_TemplateOpener) &&
Next->MatchingParen) {
3979 const bool IsCpp = LangOpts.CXXOperatorNames || LangOpts.C11;
3982 if (Current.
is(tok::kw_operator)) {
3983 if (
Line.startsWith(tok::kw_friend))
3985 if (
Previous.Tok.getIdentifierInfo() &&
3986 Previous.isNoneOf(tok::kw_return, tok::kw_co_return)) {
3991 assert(
Previous.MatchingParen->is(tok::l_paren));
3992 assert(
Previous.MatchingParen->is(TT_TypeDeclarationParen));
4001 while (
Next &&
Next->startsSequence(tok::hashhash, tok::identifier))
4004 if (
Next->is(TT_TemplateOpener) &&
Next->MatchingParen) {
4006 }
else if (
Next->is(tok::coloncolon)) {
4010 if (
Next->is(tok::kw_operator)) {
4011 Next = skipOperatorName(
Next->Next);
4014 if (
Next->isNot(tok::identifier))
4016 }
else if (isCppAttribute(IsCpp, *
Next)) {
4020 }
else if (
Next->is(tok::l_paren)) {
4029 if (!
Next ||
Next->isNot(tok::l_paren) || !
Next->MatchingParen)
4031 ClosingParen =
Next->MatchingParen;
4032 assert(ClosingParen->
is(tok::r_paren));
4034 if (
Line.Last->is(tok::l_brace))
4036 if (
Next->Next == ClosingParen)
4039 if (ClosingParen->
Next && ClosingParen->
Next->
is(TT_PointerOrReference))
4052 if (IsCpp &&
Next->Next &&
Next->Next->is(tok::identifier) &&
4053 !
Line.endsWith(tok::semi)) {
4059 if (
Tok->is(TT_TypeDeclarationParen))
4061 if (
Tok->isOneOf(tok::l_paren, TT_TemplateOpener) &&
Tok->MatchingParen) {
4062 Tok =
Tok->MatchingParen;
4065 if (
Tok->is(tok::kw_const) ||
Tok->isTypeName(LangOpts) ||
4066 Tok->isOneOf(TT_PointerOrReference, TT_StartOfName, tok::ellipsis)) {
4069 if (
Tok->isOneOf(tok::l_brace, TT_ObjCMethodExpr) ||
Tok->Tok.isLiteral())
4075bool TokenAnnotator::mustBreakForReturnType(
const AnnotatedLine &
Line)
const {
4076 assert(
Line.MightBeFunctionDecl);
4084 switch (Style.BreakAfterReturnType) {
4094 return Line.mightBeFunctionDefinition();
4100bool TokenAnnotator::mustBreakBeforeReturnType(
4102 assert(
Line.MightBeFunctionDecl);
4104 switch (Style.BreakBeforeReturnType) {
4110 return Line.Level == 0;
4112 return Line.mightBeFunctionDefinition();
4114 return Line.Level == 0 &&
Line.mightBeFunctionDefinition();
4121 auto *
Tok =
Line.getFirstNonComment();
4125 if (
Tok->is(tok::kw_template)) {
4126 auto *Opener =
Tok->Next;
4127 while (Opener && Opener->isNot(TT_TemplateOpener))
4128 Opener = Opener->Next;
4129 if (!Opener || !Opener->MatchingParen)
4131 Tok = Opener->MatchingParen->Next;
4134 if (
Tok &&
Tok->is(TT_RequiresClause)) {
4135 while (
Tok && !
Tok->ClosesRequiresClause)
4143 Tok->isOneOf(tok::kw___attribute, tok::kw___declspec,
4144 TT_AttributeMacro)) {
4146 if (
Next &&
Next->is(tok::l_paren) &&
Next->MatchingParen)
4147 Tok =
Next->MatchingParen->Next;
4152 if (
Tok->is(TT_AttributeLSquare) &&
Tok->MatchingParen) {
4153 Tok =
Tok->MatchingParen->Next;
4165 Line.Computed =
true;
4173 :
Line.FirstStartColumn +
First->ColumnWidth;
4174 bool AlignArrayOfStructures =
4177 if (AlignArrayOfStructures)
4178 calculateArrayInitializerColumnList(
Line);
4180 const auto *FirstNonComment =
Line.getFirstNonComment();
4181 bool SeenName =
false;
4182 bool LineIsFunctionDeclaration =
false;
4186 for (
auto *
Tok = FirstNonComment && FirstNonComment->isNot(tok::kw_using)
4187 ? FirstNonComment->Next
4190 if (
Tok->is(TT_StartOfName))
4192 if (
Tok->Previous->EndsCppAttributeGroup)
4193 AfterLastAttribute =
Tok;
4194 if (
const bool IsCtorOrDtor =
Tok->is(TT_CtorDtorDeclName);
4198 Tok->setFinalizedType(TT_FunctionDeclarationName);
4199 LineIsFunctionDeclaration =
true;
4203 assert(OpeningParen);
4204 if (OpeningParen->is(TT_Unknown))
4205 OpeningParen->setType(TT_FunctionDeclarationLParen);
4212 if ((LineIsFunctionDeclaration ||
4213 (FirstNonComment && FirstNonComment->is(TT_CtorDtorDeclName))) &&
4214 Line.endsWith(tok::semi, tok::r_brace)) {
4215 auto *
Tok =
Line.Last->Previous;
4216 while (
Tok->isNot(tok::r_brace))
4218 if (
auto *LBrace =
Tok->MatchingParen; LBrace && LBrace->is(TT_Unknown)) {
4219 assert(LBrace->is(tok::l_brace));
4222 LBrace->setFinalizedType(TT_FunctionLBrace);
4226 if (SeenName && AfterLastAttribute &&
4227 AfterLastAttribute->mustBreakAfterAttributes(Style)) {
4229 if (LineIsFunctionDeclaration)
4230 Line.ReturnTypeWrapped =
true;
4233 if (!LineIsFunctionDeclaration) {
4234 Line.ReturnTypeWrapped =
false;
4236 for (
const auto *
Tok = FirstNonComment;
Tok;
Tok =
Tok->Next) {
4237 if (
Tok->isNot(tok::kw_operator))
4241 }
while (
Tok &&
Tok->isNot(TT_OverloadedOperatorLParen));
4242 if (!
Tok || !
Tok->MatchingParen)
4244 const auto *LeftParen =
Tok;
4245 for (
Tok =
Tok->Next;
Tok &&
Tok != LeftParen->MatchingParen;
4247 if (
Tok->isNot(tok::identifier))
4250 const bool NextIsBinaryOperator =
4252 Next->Next->is(tok::identifier);
4253 if (!NextIsBinaryOperator)
4255 Next->setType(TT_BinaryOperator);
4259 }
else if (ClosingParen) {
4261 if (
Tok->is(TT_CtorInitializerColon))
4263 if (
Tok->is(tok::arrow)) {
4264 Tok->overwriteFixedType(TT_TrailingReturnArrow);
4267 if (
Tok->isNot(TT_TrailingAnnotation))
4270 if (!
Next ||
Next->isNot(tok::l_paren))
4279 if (
Line.MightBeFunctionDecl && LineIsFunctionDeclaration &&
4280 mustBreakBeforeReturnType(
Line)) {
4282 ReturnTypeStart && ReturnTypeStart != FirstNonComment &&
4283 ReturnTypeStart->
isNoneOf(TT_FunctionDeclarationName,
4284 TT_CtorDtorDeclName, tok::tilde)) {
4285 ReturnTypeStart->MustBreakBefore =
true;
4286 Line.ReturnTypeWrapped =
true;
4290 if (
First->is(TT_ElseLBrace)) {
4291 First->CanBreakBefore =
true;
4292 First->MustBreakBefore =
true;
4295 bool InFunctionDecl =
Line.MightBeFunctionDecl;
4296 bool InParameterList =
false;
4297 for (
auto *Current =
First->Next; Current; Current = Current->Next) {
4299 if (Current->is(TT_LineComment)) {
4301 Current->SpacesRequiredBefore =
4303 !Style.SpacesInParensOptions.Other)
4306 }
else if (Prev->
is(TT_VerilogMultiLineListLParen)) {
4307 Current->SpacesRequiredBefore = 0;
4309 Current->SpacesRequiredBefore = Style.SpacesBeforeTrailingComments;
4319 if (!Current->HasUnescapedNewline) {
4322 if (
Parameter->isOneOf(tok::comment, tok::r_brace))
4325 if (
Parameter->Previous->isNot(TT_CtorInitializerComma) &&
4333 }
else if (!Current->Finalized && Current->SpacesRequiredBefore == 0 &&
4334 spaceRequiredBefore(
Line, *Current)) {
4335 Current->SpacesRequiredBefore = 1;
4340 Current->MustBreakBefore =
true;
4342 Current->MustBreakBefore =
4343 Current->MustBreakBefore || mustBreakBefore(
Line, *Current);
4344 if (!Current->MustBreakBefore && InFunctionDecl &&
4345 Current->is(TT_FunctionDeclarationName)) {
4346 Current->MustBreakBefore = mustBreakForReturnType(
Line);
4350 Current->CanBreakBefore =
4351 !
Line.IsModuleOrImportDecl &&
4352 (Current->MustBreakBefore || canBreakBefore(
Line, *Current));
4354 if (Current->is(TT_FunctionDeclarationLParen)) {
4355 InParameterList =
true;
4356 }
else if (Current->is(tok::r_paren)) {
4357 const auto *LParen = Current->MatchingParen;
4358 if (LParen && LParen->is(TT_FunctionDeclarationLParen))
4359 InParameterList =
false;
4360 }
else if (InParameterList &&
4361 Current->endsSequence(TT_AttributeMacro,
4362 TT_PointerOrReference)) {
4363 Current->CanBreakBefore =
false;
4366 unsigned ChildSize = 0;
4369 ChildSize = LastOfChild.isTrailingComment() ? Style.ColumnLimit
4372 if (Current->MustBreakBefore || Prev->
Children.size() > 1 ||
4374 Prev->
Children[0]->First->MustBreakBefore) ||
4375 Current->IsMultiline) {
4376 Current->TotalLength = Prev->
TotalLength + Style.ColumnLimit;
4378 Current->TotalLength = Prev->
TotalLength + Current->ColumnWidth +
4379 ChildSize + Current->SpacesRequiredBefore;
4387 Prev->
isOneOf(tok::l_paren, tok::l_brace,
4388 TT_ArrayInitializerLSquare) &&
4391 for (
auto *ParamTok = Current; ParamTok && ParamTok != RParen;
4392 ParamTok = ParamTok->Next) {
4395 ParamTok->MustBreakBefore =
true;
4396 ParamTok->CanBreakBefore =
true;
4398 if (ParamTok->opensScope()) {
4399 ParamTok = ParamTok->MatchingParen;
4406 if (Current->is(TT_ControlStatementLBrace)) {
4407 if (Style.ColumnLimit > 0 &&
4408 Style.BraceWrapping.AfterControlStatement ==
4410 Line.Level * Style.IndentWidth +
Line.Last->TotalLength >
4411 Style.ColumnLimit) {
4412 Current->CanBreakBefore =
true;
4413 Current->MustBreakBefore =
true;
4415 }
else if (Current->is(TT_CtorInitializerColon)) {
4416 InFunctionDecl =
false;
4428 Current->SplitPenalty = splitPenalty(
Line, *Current, InFunctionDecl);
4430 Current->is(TT_SelectorName) && Current->ParameterIndex > 0) {
4431 if (Current->ParameterIndex == 1)
4432 Current->SplitPenalty += 5 * Current->BindingStrength;
4434 Current->SplitPenalty += 20 * Current->BindingStrength;
4438 calculateUnbreakableTailLengths(
Line);
4440 for (
auto *Current =
First; Current; Current = Current->Next) {
4442 Current->Role->precomputeFormattingInfos(Current);
4443 if (Current->MatchingParen &&
4444 Current->MatchingParen->opensBlockOrBlockTypeList(Style) &&
4449 if (Current->opensBlockOrBlockTypeList(Style))
4453 LLVM_DEBUG({ printDebugInfo(
Line); });
4456void TokenAnnotator::calculateUnbreakableTailLengths(
4463 Current->
isOneOf(tok::comment, tok::string_literal)) {
4473void TokenAnnotator::calculateArrayInitializerColumnList(
4477 auto *CurrentToken =
Line.First;
4478 CurrentToken->ArrayInitializerLineStart =
true;
4480 while (CurrentToken && CurrentToken !=
Line.Last) {
4481 if (CurrentToken->is(tok::l_brace)) {
4482 CurrentToken->IsArrayInitializer =
true;
4483 if (CurrentToken->Next)
4484 CurrentToken->Next->MustBreakBefore =
true;
4486 calculateInitializerColumnList(
Line, CurrentToken->Next, Depth + 1);
4488 CurrentToken = CurrentToken->Next;
4493FormatToken *TokenAnnotator::calculateInitializerColumnList(
4495 while (CurrentToken && CurrentToken !=
Line.Last) {
4496 if (CurrentToken->is(tok::l_brace))
4498 else if (CurrentToken->is(tok::r_brace))
4500 if (Depth == 2 && CurrentToken->isOneOf(tok::l_brace, tok::comma)) {
4501 CurrentToken = CurrentToken->Next;
4504 CurrentToken->StartsColumn =
true;
4505 CurrentToken = CurrentToken->Previous;
4507 CurrentToken = CurrentToken->Next;
4509 return CurrentToken;
4514 bool InFunctionDecl)
const {
4518 if (
Left.is(tok::semi))
4522 if (Style.isJava()) {
4523 if (
Right.isOneOf(Keywords.kw_extends, Keywords.kw_throws))
4525 if (
Right.is(Keywords.kw_implements))
4527 if (
Left.is(tok::comma) &&
Left.NestingLevel == 0)
4529 }
else if (Style.isJavaScript()) {
4530 if (
Right.is(Keywords.kw_function) &&
Left.isNot(tok::comma))
4532 if (
Left.is(TT_JsTypeColon))
4534 if ((
Left.is(TT_TemplateString) &&
Left.TokenText.ends_with(
"${")) ||
4535 (
Right.is(TT_TemplateString) &&
Right.TokenText.starts_with(
"}"))) {
4539 if (
Left.opensScope() &&
Right.closesScope())
4542 if (
Right.is(tok::l_square))
4544 if (
Right.is(tok::period))
4548 if (
Right.is(tok::identifier) &&
Right.Next &&
Right.Next->is(TT_DictLiteral))
4550 if (
Right.is(tok::l_square)) {
4551 if (
Left.is(tok::r_square))
4554 if (
Right.is(TT_LambdaLSquare) &&
Left.is(tok::equal))
4556 if (
Right.isNoneOf(TT_ObjCMethodExpr, TT_LambdaLSquare,
4557 TT_ArrayInitializerLSquare,
4558 TT_DesignatedInitializerLSquare, TT_AttributeLSquare)) {
4563 if (
Left.is(tok::coloncolon))
4564 return Style.PenaltyBreakScopeResolution;
4565 if (
Right.isOneOf(TT_StartOfName, TT_FunctionDeclarationName,
4566 tok::kw_operator)) {
4567 if (
Line.startsWith(tok::kw_for) &&
Right.PartOfMultiVariableDeclStmt)
4569 if (
Left.is(TT_StartOfName))
4571 if (InFunctionDecl &&
Right.NestingLevel == 0)
4572 return Style.PenaltyReturnTypeOnItsOwnLine;
4575 if (
Right.is(TT_PointerOrReference))
4577 if (
Right.is(TT_LambdaArrow))
4579 if (
Left.is(tok::equal) &&
Right.is(tok::l_brace))
4581 if (
Left.is(TT_CastRParen))
4583 if (
Left.isOneOf(tok::kw_class, tok::kw_struct, tok::kw_union))
4585 if (
Left.is(tok::comment))
4588 if (
Left.isOneOf(TT_RangeBasedForLoopColon, TT_InheritanceColon,
4589 TT_CtorInitializerColon)) {
4593 if (
Right.isMemberAccess()) {
4614 const auto Penalty = Style.PenaltyBreakBeforeMemberAccess;
4616 ? std::min(Penalty, 35u)
4620 if (
Right.is(TT_TrailingAnnotation) &&
4621 (!
Right.Next ||
Right.Next->isNot(tok::l_paren))) {
4624 if (
Line.startsWith(TT_ObjCMethodSpecifier))
4631 bool is_short_annotation =
Right.TokenText.size() < 10;
4632 return (
Left.is(tok::r_paren) ? 100 : 120) + (is_short_annotation ? 50 : 0);
4636 if (
Line.startsWith(tok::kw_for) &&
Left.is(tok::equal))
4641 if (
Right.is(TT_SelectorName))
4643 if (
Left.is(tok::colon)) {
4644 if (
Left.is(TT_ObjCMethodExpr))
4645 return Line.MightBeFunctionDecl ? 50 : 500;
4646 if (
Left.is(TT_ObjCSelector))
4654 Left.Previous->isOneOf(tok::identifier, tok::greater)) {
4658 if (
Left.is(tok::l_paren) && Style.PenaltyBreakOpenParenthesis != 0)
4659 return Style.PenaltyBreakOpenParenthesis;
4660 if (
Left.is(tok::l_paren) && InFunctionDecl && Style.AlignAfterOpenBracket)
4662 if (
Left.is(tok::l_paren) &&
Left.Previous &&
4663 (
Left.Previous->isOneOf(tok::kw_for, tok::kw__Generic) ||
4664 Left.Previous->isIf())) {
4667 if (
Left.is(tok::equal) && InFunctionDecl)
4669 if (
Right.is(tok::r_brace))
4671 if (
Left.is(TT_TemplateOpener))
4673 if (
Left.opensScope()) {
4677 if (!Style.AlignAfterOpenBracket &&
4678 (
Left.ParameterCount <= 1 || Style.AllowAllArgumentsOnNextLine)) {
4681 if (
Left.is(tok::l_brace) &&
4685 return Left.ParameterCount > 1 ? Style.PenaltyBreakBeforeFirstCallParameter
4688 if (
Left.is(TT_JavaAnnotation))
4691 if (
Left.is(TT_UnaryOperator))
4693 if (
Left.isOneOf(tok::plus, tok::comma) &&
Left.Previous &&
4694 Left.Previous->isLabelString() &&
4695 (
Left.NextOperator ||
Left.OperatorIndex != 0)) {
4698 if (
Right.is(tok::plus) &&
Left.isLabelString() &&
4699 (
Right.NextOperator ||
Right.OperatorIndex != 0)) {
4702 if (
Left.is(tok::comma))
4704 if (
Right.is(tok::lessless) &&
Left.isLabelString() &&
4705 (
Right.NextOperator ||
Right.OperatorIndex != 1)) {
4708 if (
Right.is(tok::lessless)) {
4710 if (
Left.isNot(tok::r_paren) ||
Right.OperatorIndex > 0) {
4716 if (
Left.ClosesTemplateDeclaration)
4717 return Style.PenaltyBreakTemplateDeclaration;
4718 if (
Left.ClosesRequiresClause)
4720 if (
Left.is(TT_ConditionalExpr))
4726 return Style.PenaltyBreakAssignment;
4733bool TokenAnnotator::spaceRequiredBeforeParens(
const FormatToken &Right)
const {
4736 if (
Right.is(TT_OverloadedOperatorLParen) &&
4737 Style.SpaceBeforeParensOptions.AfterOverloadedOperator) {
4740 if (Style.SpaceBeforeParensOptions.BeforeNonEmptyParentheses &&
4741 Right.ParameterCount > 0) {
4750 if (
Left.is(tok::kw_return) &&
4751 Right.isNoneOf(tok::semi, tok::r_paren, tok::hashhash)) {
4754 if (
Left.is(tok::kw_throw) &&
Right.is(tok::l_paren) &&
Right.MatchingParen &&
4755 Right.MatchingParen->is(TT_CastRParen)) {
4758 if (
Left.is(Keywords.kw_assert) && Style.isJava())
4761 Left.is(tok::objc_property)) {
4764 if (
Right.is(tok::hashhash))
4765 return Left.is(tok::hash);
4766 if (
Left.isOneOf(tok::hashhash, tok::hash))
4767 return Right.is(tok::hash);
4769 if (
Left.is(tok::l_paren) &&
Right.is(tok::r_paren))
4770 return Style.SpacesInParensOptions.InEmptyParentheses;
4771 if (Style.SpacesInParensOptions.ExceptDoubleParentheses &&
4772 Left.is(tok::r_paren) &&
Right.is(tok::r_paren)) {
4773 auto *InnerLParen =
Left.MatchingParen;
4774 if (InnerLParen && InnerLParen->Previous ==
Right.MatchingParen) {
4775 InnerLParen->SpacesRequiredBefore = 0;
4780 if (
Left.is(tok::l_paren))
4782 else if (
Right.is(tok::r_paren) &&
Right.MatchingParen)
4783 LeftParen =
Right.MatchingParen;
4784 if (LeftParen && (LeftParen->is(TT_ConditionLParen) ||
4785 (LeftParen->Previous &&
4786 isKeywordWithCondition(*LeftParen->Previous)))) {
4787 return Style.SpacesInParensOptions.InConditionalStatements;
4792 if (
Left.is(tok::kw_auto) &&
Right.isOneOf(TT_LambdaLBrace, TT_FunctionLBrace,
4794 TT_FunctionTypeLParen)) {
4799 if (
Left.is(tok::kw_auto) &&
Right.isOneOf(tok::l_paren, tok::l_brace))
4802 const auto *BeforeLeft =
Left.Previous;
4805 if (
Right.is(tok::l_paren) &&
Left.is(tok::kw_co_await) && BeforeLeft &&
4806 BeforeLeft->is(tok::kw_operator)) {
4810 if (
Left.isOneOf(tok::kw_co_await, tok::kw_co_yield, tok::kw_co_return) &&
4811 Right.isNoneOf(tok::semi, tok::r_paren)) {
4815 if (
Left.is(tok::l_paren) ||
Right.is(tok::r_paren)) {
4816 return (
Right.is(TT_CastRParen) ||
4817 (
Left.MatchingParen &&
Left.MatchingParen->is(TT_CastRParen)))
4818 ? Style.SpacesInParensOptions.InCStyleCasts
4819 : Style.SpacesInParensOptions.Other;
4821 if (
Right.isOneOf(tok::semi, tok::comma))
4824 bool IsLightweightGeneric =
Right.MatchingParen &&
4825 Right.MatchingParen->Next &&
4826 Right.MatchingParen->Next->is(tok::colon);
4827 return !IsLightweightGeneric && Style.ObjCSpaceBeforeProtocolList;
4829 if (
Right.is(tok::less) &&
Left.is(tok::kw_template))
4830 return Style.SpaceAfterTemplateKeyword;
4831 if (
Left.isOneOf(tok::exclaim, tok::tilde))
4833 if (
Left.is(tok::at) &&
4834 Right.isOneOf(tok::identifier, tok::string_literal, tok::char_constant,
4835 tok::numeric_constant, tok::l_paren, tok::l_brace,
4836 tok::kw_true, tok::kw_false)) {
4839 if (
Left.is(tok::colon))
4840 return Left.isNoneOf(TT_ObjCSelector, TT_ObjCMethodExpr);
4841 if (
Left.is(tok::coloncolon))
4843 if (
Left.is(tok::less) ||
Right.isOneOf(tok::greater, tok::less)) {
4844 if (Style.isTextProto() ||
4846 (
Left.is(TT_DictLiteral) ||
Right.is(TT_DictLiteral)))) {
4848 if (
Left.is(tok::less) &&
Right.is(tok::greater))
4853 if (
Right.isNot(TT_OverloadedOperatorLParen))
4856 if (
Right.is(tok::ellipsis)) {
4857 return Left.Tok.isLiteral() || (
Left.is(tok::identifier) && BeforeLeft &&
4858 BeforeLeft->is(tok::kw_case));
4860 if (
Left.is(tok::l_square) &&
Right.is(tok::amp))
4861 return Style.SpacesInSquareBrackets;
4862 if (
Right.is(TT_PointerOrReference)) {
4863 if (
Left.is(tok::r_paren) &&
Line.MightBeFunctionDecl) {
4864 if (!
Left.MatchingParen)
4867 Left.MatchingParen->getPreviousNonComment();
4868 if (!TokenBeforeMatchingParen ||
Left.isNot(TT_TypeDeclarationParen))
4876 (
Left.is(TT_AttributeRParen) ||
4877 Left.canBePointerOrReferenceQualifier())) {
4880 if (
Left.Tok.isLiteral())
4883 if (
Left.isTypeOrIdentifier(LangOpts) &&
Right.Next &&
Right.Next->Next &&
4884 Right.Next->Next->is(TT_RangeBasedForLoopColon)) {
4885 return getTokenPointerOrReferenceAlignment(Right) !=
4888 return Left.isNoneOf(TT_PointerOrReference, tok::l_paren) &&
4889 (getTokenPointerOrReferenceAlignment(Right) !=
4891 (
Line.IsMultiVariableDeclStmt &&
4892 (
Left.NestingLevel == 0 ||
4893 (
Left.NestingLevel == 1 && startsWithInitStatement(
Line)))));
4895 if (
Right.is(TT_FunctionTypeLParen) &&
Left.isNot(tok::l_paren) &&
4896 (
Left.isNot(TT_PointerOrReference) ||
4898 !
Line.IsMultiVariableDeclStmt))) {
4901 if (
Left.is(TT_PointerOrReference)) {
4906 Right.canBePointerOrReferenceQualifier()) {
4910 if (
Right.Tok.isLiteral())
4913 if (
Right.is(TT_BlockComment))
4917 if (
Right.isOneOf(Keywords.kw_override, Keywords.kw_final, tok::kw_noexcept,
4918 TT_RequiresClause) &&
4919 Right.isNot(TT_StartOfName)) {
4926 if (BeforeLeft && BeforeLeft->isTypeOrIdentifier(LangOpts) &&
Right.Next &&
4927 Right.Next->is(TT_RangeBasedForLoopColon)) {
4928 return getTokenPointerOrReferenceAlignment(Left) !=
4931 if (
Right.isOneOf(TT_PointerOrReference, TT_ArraySubscriptLSquare,
4943 if (
Line.IsMultiVariableDeclStmt &&
4944 (
Left.NestingLevel ==
Line.First->NestingLevel ||
4945 ((
Left.NestingLevel ==
Line.First->NestingLevel + 1) &&
4946 startsWithInitStatement(
Line)))) {
4951 if (BeforeLeft->is(tok::coloncolon)) {
4952 if (
Left.isNot(tok::star))
4955 if (!
Right.startsSequence(tok::identifier, tok::r_paren))
4958 const auto *LParen =
Right.Next->MatchingParen;
4959 return !LParen || LParen->isNot(TT_FunctionTypeLParen);
4961 return BeforeLeft->isNoneOf(tok::l_paren, tok::l_square);
4964 if (
Left.is(tok::ellipsis) && BeforeLeft &&
4965 BeforeLeft->isPointerOrReference()) {
4969 if (
Right.is(tok::star) &&
Left.is(tok::l_paren))
4971 if (
Left.is(tok::star) &&
Right.isPointerOrReference())
4973 if (
Right.isPointerOrReference()) {
4984 if (
Previous->is(tok::coloncolon)) {
5003 if (
Previous->endsSequence(tok::kw_operator))
5005 if (
Previous->isOneOf(tok::kw_const, tok::kw_volatile)) {
5007 (Style.SpaceAroundPointerQualifiers ==
5013 if (Style.isCSharp() &&
Left.is(Keywords.kw_is) &&
Right.is(tok::l_square))
5015 const auto SpaceRequiredForArrayInitializerLSquare =
5016 [](
const FormatToken &LSquareTok,
const FormatStyle &Style) {
5017 return Style.SpacesInContainerLiterals ||
5020 LSquareTok.endsSequence(tok::l_square, tok::colon,
5023 if (
Left.is(tok::l_square)) {
5024 return (
Left.is(TT_ArrayInitializerLSquare) &&
Right.isNot(tok::r_square) &&
5025 SpaceRequiredForArrayInitializerLSquare(Left, Style)) ||
5026 (
Left.isOneOf(TT_ArraySubscriptLSquare, TT_StructuredBindingLSquare,
5027 TT_LambdaLSquare) &&
5028 Style.SpacesInSquareBrackets &&
Right.isNot(tok::r_square));
5030 if (
Right.is(tok::r_square)) {
5031 return Right.MatchingParen &&
5032 ((
Right.MatchingParen->is(TT_ArrayInitializerLSquare) &&
5033 SpaceRequiredForArrayInitializerLSquare(*
Right.MatchingParen,
5035 (Style.SpacesInSquareBrackets &&
5036 Right.MatchingParen->isOneOf(TT_ArraySubscriptLSquare,
5037 TT_StructuredBindingLSquare,
5038 TT_LambdaLSquare)));
5040 if (
Right.is(tok::l_square) &&
5041 Right.isNoneOf(TT_ObjCMethodExpr, TT_LambdaLSquare,
5042 TT_DesignatedInitializerLSquare,
5043 TT_StructuredBindingLSquare, TT_AttributeLSquare) &&
5044 Left.isNoneOf(tok::numeric_constant, TT_DictLiteral) &&
5045 !(
Left.isNot(tok::r_square) && Style.SpaceBeforeSquareBrackets &&
5046 Right.is(TT_ArraySubscriptLSquare))) {
5050 (
Right.is(tok::r_brace) &&
Right.MatchingParen &&
5053 Style.SpacesInParensOptions.Other;
5055 if (
Left.is(TT_BlockComment)) {
5057 return Style.isJavaScript() || !
Left.TokenText.ends_with(
"=*/");
5062 if (
Left.is(TT_TemplateCloser) &&
Right.is(TT_AttributeLSquare))
5065 if (
Right.is(tok::l_paren)) {
5067 if (
Line.MightBeFunctionDecl &&
Right.is(TT_FunctionDeclarationLParen)) {
5068 if (spaceRequiredBeforeParens(Right))
5070 const auto &Options = Style.SpaceBeforeParensOptions;
5071 return Line.mightBeFunctionDefinition()
5072 ? Options.AfterFunctionDefinitionName
5073 : Options.AfterFunctionDeclarationName;
5075 if (
Left.is(TT_TemplateCloser) &&
Right.isNot(TT_FunctionTypeLParen))
5076 return spaceRequiredBeforeParens(Right);
5077 if (
Left.isOneOf(TT_RequiresClause,
5078 TT_RequiresClauseInARequiresExpression)) {
5079 return Style.SpaceBeforeParensOptions.AfterRequiresInClause ||
5080 spaceRequiredBeforeParens(Right);
5082 if (
Left.is(TT_RequiresExpression)) {
5083 return Style.SpaceBeforeParensOptions.AfterRequiresInExpression ||
5084 spaceRequiredBeforeParens(Right);
5086 if (
Left.isOneOf(TT_AttributeRParen, TT_AttributeRSquare))
5088 if (
Left.is(TT_ForEachMacro)) {
5089 return Style.SpaceBeforeParensOptions.AfterForeachMacros ||
5090 spaceRequiredBeforeParens(Right);
5092 if (
Left.is(TT_IfMacro)) {
5093 return Style.SpaceBeforeParensOptions.AfterIfMacros ||
5094 spaceRequiredBeforeParens(Right);
5097 Left.isPlacementOperator() &&
5098 Right.isNot(TT_OverloadedOperatorLParen) &&
5099 !(
Line.MightBeFunctionDecl &&
Left.is(TT_FunctionDeclarationName))) {
5100 const auto *RParen =
Right.MatchingParen;
5101 return Style.SpaceBeforeParensOptions.AfterPlacementOperator ||
5102 (RParen && RParen->is(TT_CastRParen));
5106 if (
Left.is(tok::semi))
5108 if (
Left.isOneOf(tok::pp_elif, tok::kw_for, tok::kw_while, tok::kw_switch,
5109 tok::kw_case, TT_ForEachMacro, TT_ObjCForIn) ||
5111 Right.is(TT_ConditionLParen)) {
5112 return Style.SpaceBeforeParensOptions.AfterControlStatements ||
5113 spaceRequiredBeforeParens(Right);
5118 if (
Right.is(TT_OverloadedOperatorLParen))
5119 return spaceRequiredBeforeParens(Right);
5123 Left.MatchingParen &&
Left.MatchingParen->is(TT_LambdaLSquare)) {
5124 return Style.SpaceBeforeParensOptions.AfterFunctionDefinitionName ||
5125 spaceRequiredBeforeParens(Right);
5127 if (!BeforeLeft || BeforeLeft->isNoneOf(tok::period, tok::arrow)) {
5128 if (
Left.isOneOf(tok::kw_try, Keywords.kw___except, tok::kw_catch)) {
5129 return Style.SpaceBeforeParensOptions.AfterControlStatements ||
5130 spaceRequiredBeforeParens(Right);
5132 if (
Left.isPlacementOperator() ||
5133 (
Left.is(tok::r_square) &&
Left.MatchingParen &&
5134 Left.MatchingParen->Previous &&
5135 Left.MatchingParen->Previous->is(tok::kw_delete))) {
5137 spaceRequiredBeforeParens(Right);
5141 if (
Tok.isNot(tok::l_paren))
5143 const auto *RParen =
Tok.MatchingParen;
5146 const auto *
Next = RParen->Next;
5153 (
Left.Tok.getIdentifierInfo() ||
Left.is(tok::r_paren))) {
5154 return spaceRequiredBeforeParens(Right);
5158 if (
Left.is(tok::at) &&
Right.isNot(tok::objc_not_keyword))
5160 if (
Right.is(TT_UnaryOperator)) {
5161 return Left.isNoneOf(tok::l_paren, tok::l_square, tok::at) &&
5162 (
Left.isNot(tok::colon) ||
Left.isNot(TT_ObjCMethodExpr));
5168 if (!Style.isVerilog() &&
5169 (
Left.isOneOf(tok::identifier, tok::greater, tok::r_square,
5171 Left.isTypeName(LangOpts)) &&
5172 Right.is(tok::l_brace) &&
Right.getNextNonComment() &&
5176 if (
Left.is(tok::period) ||
Right.is(tok::period))
5180 if (
Right.is(tok::hash) &&
Left.is(tok::identifier) &&
5181 (
Left.TokenText ==
"L" ||
Left.TokenText ==
"u" ||
5182 Left.TokenText ==
"U" ||
Left.TokenText ==
"u8" ||
5183 Left.TokenText ==
"LR" ||
Left.TokenText ==
"uR" ||
5184 Left.TokenText ==
"UR" ||
Left.TokenText ==
"u8R")) {
5187 if (
Left.is(TT_TemplateCloser) &&
Left.MatchingParen &&
5188 Left.MatchingParen->Previous &&
5189 Left.MatchingParen->Previous->isOneOf(tok::period, tok::coloncolon)) {
5195 if (
Left.is(TT_TemplateCloser) &&
Right.is(tok::l_square))
5197 if (
Left.is(tok::l_brace) &&
Left.endsSequence(TT_DictLiteral, tok::at)) {
5201 if (
Right.is(tok::r_brace) &&
Right.MatchingParen &&
5202 Right.MatchingParen->endsSequence(TT_DictLiteral, tok::at)) {
5206 if (
Right.is(TT_TrailingAnnotation) &&
Right.isOneOf(tok::amp, tok::ampamp) &&
5207 Left.isOneOf(tok::kw_const, tok::kw_volatile) &&
5208 (!
Right.Next ||
Right.Next->is(tok::semi))) {
5225 return Right.hasWhitespaceBefore();
5227 const bool IsVerilog = Style.isVerilog();
5228 assert(!IsVerilog || !IsCpp);
5231 if (Keywords.isWordLike(Right, IsVerilog) &&
5232 Keywords.isWordLike(Left, IsVerilog)) {
5238 if (
Left.is(tok::star) &&
Right.is(tok::comment))
5241 if (
Left.is(tok::l_brace) &&
Right.is(tok::r_brace) &&
5242 Left.Children.empty()) {
5247 Style.SpacesInParensOptions.InEmptyParentheses;
5252 const auto *BeforeLeft =
Left.Previous;
5255 if (
Left.is(TT_OverloadedOperator) &&
5256 Right.isOneOf(TT_TemplateOpener, TT_TemplateCloser)) {
5260 if (
Right.is(tok::period) &&
Left.is(tok::numeric_constant))
5264 if (
Left.is(Keywords.kw_import) &&
5265 Right.isOneOf(tok::less, tok::ellipsis) &&
5266 (!BeforeLeft || BeforeLeft->is(tok::kw_export))) {
5270 if (
Left.is(Keywords.kw_import) &&
Right.is(TT_ModulePartitionColon))
5273 if (
Right.is(TT_AfterPPDirective))
5277 if (
Left.is(tok::identifier) &&
Right.is(TT_ModulePartitionColon))
5280 if (
Left.is(TT_ModulePartitionColon) &&
Right.is(tok::identifier))
5282 if (
Left.is(tok::ellipsis) &&
Right.is(tok::identifier) &&
5283 Line.First->is(Keywords.kw_import)) {
5287 if (
Left.isOneOf(TT_AttributeRParen, TT_AttributeMacro) &&
5288 Right.is(tok::coloncolon)) {
5292 if (
Left.is(tok::kw_operator))
5293 return Right.is(tok::coloncolon) || Style.SpaceAfterOperatorKeyword;
5295 !
Left.opensScope() && Style.SpaceBeforeCpp11BracedList) {
5298 if (
Left.is(tok::less) &&
Left.is(TT_OverloadedOperator) &&
5299 Right.is(TT_TemplateOpener)) {
5303 if (
Left.is(tok::identifier) &&
Right.is(tok::numeric_constant))
5304 return Right.TokenText[0] !=
'.';
5306 if (
Left.Tok.getIdentifierInfo() &&
Right.Tok.isLiteral())
5308 }
else if (Style.isProto()) {
5309 if (
Right.is(tok::period) && !(BeforeLeft && BeforeLeft->is(tok::period)) &&
5310 Left.isOneOf(Keywords.kw_optional, Keywords.kw_required,
5311 Keywords.kw_repeated, Keywords.kw_extend)) {
5314 if (
Right.is(tok::l_paren) &&
5315 Left.isOneOf(Keywords.kw_returns, Keywords.kw_option)) {
5318 if (
Right.isOneOf(tok::l_brace, tok::less) &&
Left.is(TT_SelectorName))
5321 if (
Left.is(tok::slash) ||
Right.is(tok::slash))
5323 if (
Left.MatchingParen &&
5324 Left.MatchingParen->is(TT_ProtoExtensionLSquare) &&
5325 Right.isOneOf(tok::l_brace, tok::less)) {
5329 if (
Left.is(tok::percent))
5333 if (
Left.is(tok::numeric_constant) &&
Right.is(tok::percent))
5334 return Right.hasWhitespaceBefore();
5335 }
else if (Style.isJson()) {
5336 if (
Right.is(tok::colon) &&
Left.is(tok::string_literal))
5337 return Style.SpaceBeforeJsonColon;
5338 }
else if (Style.isCSharp()) {
5344 if (
Left.is(tok::kw_this) &&
Right.is(tok::l_square))
5348 if (
Left.is(tok::kw_new) &&
Right.is(tok::l_paren))
5352 if (
Right.is(tok::l_brace))
5356 if (
Left.is(tok::l_brace) &&
Right.isNot(tok::r_brace))
5359 if (
Left.isNot(tok::l_brace) &&
Right.is(tok::r_brace))
5363 if (
Left.is(TT_FatArrow) ||
Right.is(TT_FatArrow))
5367 if (
Left.is(TT_AttributeColon) ||
Right.is(TT_AttributeColon))
5371 if (
Left.is(TT_TemplateCloser) &&
Right.is(TT_StartOfName))
5375 if (
Left.is(tok::l_square) ||
Right.is(tok::r_square))
5376 return Style.SpacesInSquareBrackets;
5379 if (
Right.is(TT_CSharpNullable))
5383 if (
Right.is(TT_NonNullAssertion))
5387 if (
Left.is(tok::comma) &&
Right.is(tok::comma))
5391 if (
Left.is(Keywords.kw_var) &&
Right.is(tok::l_paren))
5395 if (
Right.is(tok::l_paren)) {
5396 if (
Left.isOneOf(tok::kw_using, Keywords.kw_async, Keywords.kw_when,
5397 Keywords.kw_lock)) {
5398 return Style.SpaceBeforeParensOptions.AfterControlStatements ||
5399 spaceRequiredBeforeParens(Right);
5405 if ((
Left.isAccessSpecifierKeyword() ||
5406 Left.isOneOf(tok::kw_virtual, tok::kw_extern, tok::kw_static,
5407 Keywords.kw_internal, Keywords.kw_abstract,
5408 Keywords.kw_sealed, Keywords.kw_override,
5409 Keywords.kw_async, Keywords.kw_unsafe)) &&
5410 Right.is(tok::l_paren)) {
5413 }
else if (Style.isJavaScript()) {
5414 if (
Left.is(TT_FatArrow))
5417 if (
Right.is(tok::l_paren) &&
Left.is(Keywords.kw_await) && BeforeLeft &&
5418 BeforeLeft->is(tok::kw_for)) {
5421 if (
Left.is(Keywords.kw_async) &&
Right.is(tok::l_paren) &&
5422 Right.MatchingParen) {
5429 if ((
Left.is(TT_TemplateString) &&
Left.TokenText.ends_with(
"${")) ||
5430 (
Right.is(TT_TemplateString) &&
Right.TokenText.starts_with(
"}"))) {
5435 if (Keywords.isJavaScriptIdentifier(Left,
5437 Right.is(TT_TemplateString)) {
5440 if (
Right.is(tok::star) &&
5441 Left.isOneOf(Keywords.kw_function, Keywords.kw_yield)) {
5444 if (
Right.isOneOf(tok::l_brace, tok::l_square) &&
5445 Left.isOneOf(Keywords.kw_function, Keywords.kw_yield,
5446 Keywords.kw_extends, Keywords.kw_implements)) {
5449 if (
Right.is(tok::l_paren)) {
5451 if (
Line.MustBeDeclaration &&
Left.Tok.getIdentifierInfo())
5455 if (BeforeLeft && BeforeLeft->is(tok::period) &&
5456 Left.Tok.getIdentifierInfo()) {
5460 if (
Left.isOneOf(tok::kw_throw, Keywords.kw_await, Keywords.kw_typeof,
5466 if (
Left.endsSequence(tok::kw_const, Keywords.kw_as))
5468 if ((
Left.isOneOf(Keywords.kw_let, Keywords.kw_var, Keywords.kw_in,
5473 (
Left.is(Keywords.kw_of) && BeforeLeft &&
5474 BeforeLeft->isOneOf(tok::identifier, tok::r_square, tok::r_brace))) &&
5475 (!BeforeLeft || BeforeLeft->isNot(tok::period))) {
5478 if (
Left.isOneOf(tok::kw_for, Keywords.kw_as) && BeforeLeft &&
5479 BeforeLeft->is(tok::period) &&
Right.is(tok::l_paren)) {
5482 if (
Left.is(Keywords.kw_as) &&
5483 Right.isOneOf(tok::l_square, tok::l_brace, tok::l_paren)) {
5486 if (
Left.is(tok::kw_default) && BeforeLeft &&
5487 BeforeLeft->is(tok::kw_export)) {
5490 if (
Left.is(Keywords.kw_is) &&
Right.is(tok::l_brace))
5492 if (
Right.isOneOf(TT_JsTypeColon, TT_JsTypeOptionalQuestion))
5494 if (
Left.is(TT_JsTypeOperator) ||
Right.is(TT_JsTypeOperator))
5496 if ((
Left.is(tok::l_brace) ||
Right.is(tok::r_brace)) &&
5497 Line.First->isOneOf(Keywords.kw_import, tok::kw_export)) {
5500 if (
Left.is(tok::ellipsis))
5502 if (
Left.is(TT_TemplateCloser) &&
5503 Right.isNoneOf(tok::equal, tok::l_brace, tok::comma, tok::l_square,
5504 Keywords.kw_implements, Keywords.kw_extends)) {
5510 if (
Right.is(TT_NonNullAssertion))
5512 if (
Left.is(TT_NonNullAssertion) &&
5513 Right.isOneOf(Keywords.kw_as, Keywords.kw_in)) {
5516 }
else if (Style.isJava()) {
5517 if (
Left.is(TT_CaseLabelArrow) ||
Right.is(TT_CaseLabelArrow))
5519 if (
Left.is(tok::r_square) &&
Right.is(tok::l_brace))
5522 if (
Left.is(tok::l_square) ||
Right.is(tok::r_square))
5523 return Style.SpacesInSquareBrackets;
5525 if (
Left.is(Keywords.kw_synchronized) &&
Right.is(tok::l_paren)) {
5526 return Style.SpaceBeforeParensOptions.AfterControlStatements ||
5527 spaceRequiredBeforeParens(Right);
5529 if ((
Left.isAccessSpecifierKeyword() ||
5530 Left.isOneOf(tok::kw_static, Keywords.kw_final, Keywords.kw_abstract,
5531 Keywords.kw_native)) &&
5532 Right.is(TT_TemplateOpener)) {
5535 }
else if (IsVerilog) {
5537 if (
Left.is(tok::identifier) &&
Left.TokenText[0] ==
'\\')
5541 if ((
Left.is(TT_VerilogTableItem) &&
5542 Right.isNoneOf(tok::r_paren, tok::semi)) ||
5543 (
Right.is(TT_VerilogTableItem) &&
Left.isNot(tok::l_paren))) {
5545 return !(
Next &&
Next->is(tok::r_paren));
5548 if (
Left.isNot(TT_BinaryOperator) &&
5549 Left.isOneOf(Keywords.kw_verilogHash, Keywords.kw_verilogHashHash)) {
5553 if (
Right.isNot(tok::semi) &&
5554 (
Left.endsSequence(tok::numeric_constant, Keywords.kw_verilogHash) ||
5555 Left.endsSequence(tok::numeric_constant,
5556 Keywords.kw_verilogHashHash) ||
5557 (
Left.is(tok::r_paren) &&
Left.MatchingParen &&
5558 Left.MatchingParen->endsSequence(tok::l_paren, tok::at)))) {
5563 if (
Left.is(Keywords.kw_apostrophe) ||
5564 (
Left.is(TT_VerilogNumberBase) &&
Right.is(tok::numeric_constant))) {
5568 if (
Left.is(tok::arrow) ||
Right.is(tok::arrow))
5573 if (
Left.is(tok::at) &&
Right.isOneOf(tok::l_paren, tok::star, tok::at))
5576 if (
Right.is(tok::l_square) &&
5577 Left.isOneOf(TT_VerilogDimensionedTypeName, Keywords.kw_function)) {
5581 if (
Right.isOneOf(tok::period, Keywords.kw_apostrophe) &&
5582 Keywords.isVerilogIdentifier(Left) &&
Left.getPreviousNonComment() &&
5583 Left.getPreviousNonComment()->is(Keywords.kw_tagged)) {
5589 if ((
Right.is(Keywords.kw_apostrophe) ||
5591 Left.isNoneOf(Keywords.kw_assign, Keywords.kw_unique) &&
5592 !Keywords.isVerilogWordOperator(Left) &&
5593 (
Left.isOneOf(tok::r_square, tok::r_paren, tok::r_brace,
5594 tok::numeric_constant) ||
5595 Keywords.isWordLike(Left))) {
5599 if ((
Right.is(tok::star) &&
Left.is(tok::coloncolon)) ||
5600 (
Left.is(tok::star) &&
Right.is(tok::semi))) {
5604 if (
Left.endsSequence(tok::star, tok::l_paren) &&
Right.is(tok::identifier))
5607 if (
Right.is(tok::colon) &&
5608 (
Right.TokenText ==
":=" ||
Right.TokenText ==
":/")) {
5612 if (
Right.is(tok::l_paren) &&
Right.is(TT_VerilogStrength))
5615 if ((
Left.is(tok::l_brace) &&
5616 Right.isOneOf(tok::lessless, tok::greatergreater)) ||
5617 (
Left.endsSequence(tok::lessless, tok::l_brace) ||
5618 Left.endsSequence(tok::greatergreater, tok::l_brace))) {
5621 }
else if (Style.isTableGen()) {
5623 if (
Left.is(tok::l_square) &&
Right.is(tok::l_brace))
5625 if (
Left.is(tok::r_brace) &&
Right.is(tok::r_square))
5628 if (
Right.isOneOf(TT_TableGenDAGArgListColon,
5629 TT_TableGenDAGArgListColonToAlign) ||
5630 Left.isOneOf(TT_TableGenDAGArgListColon,
5631 TT_TableGenDAGArgListColonToAlign)) {
5634 if (
Right.is(TT_TableGenCondOperatorColon))
5636 if (
Left.isOneOf(TT_TableGenDAGArgOperatorID,
5637 TT_TableGenDAGArgOperatorToBreak) &&
5638 Right.isNot(TT_TableGenDAGArgCloser)) {
5642 if (
Right.isOneOf(tok::l_paren, tok::less) &&
5643 Left.isOneOf(TT_TableGenBangOperator, TT_TableGenCondOperator)) {
5648 if (
Left.is(TT_TableGenTrailingPasteOperator) &&
5649 Right.isOneOf(tok::l_brace, tok::colon)) {
5653 if (
Left.is(tok::hash) ||
Right.is(tok::hash))
5656 if (Keywords.isTableGenDefinition(Left))
5660 if (
Left.is(TT_ImplicitStringLiteral))
5661 return Right.hasWhitespaceBefore();
5663 if (
Left.is(TT_ObjCMethodSpecifier))
5664 return Style.ObjCSpaceAfterMethodDeclarationPrefix;
5665 if (
Left.is(tok::r_paren) &&
Left.isNot(TT_AttributeRParen) &&
5666 canBeObjCSelectorComponent(Right)) {
5674 (
Right.is(tok::equal) ||
Left.is(tok::equal))) {
5678 if (
Right.isOneOf(TT_TrailingReturnArrow, TT_LambdaArrow) ||
5679 Left.isOneOf(TT_TrailingReturnArrow, TT_LambdaArrow)) {
5682 if (
Left.is(tok::comma) &&
Right.isNot(TT_OverloadedOperatorLParen) &&
5685 (
Left.Children.empty() || !
Left.MacroParent)) {
5688 if (
Right.is(tok::comma))
5690 if (
Left.is(TT_TrailingCommaVaOptRParen) && !
Right.closesScope() &&
5691 Right.isNot(tok::semi)) {
5694 if (
Right.is(TT_ObjCBlockLParen))
5696 if (
Right.is(TT_CtorInitializerColon))
5697 return Style.SpaceBeforeCtorInitializerColon;
5698 if (
Right.is(TT_InheritanceColon) && !Style.SpaceBeforeInheritanceColon)
5700 if (
Right.is(TT_EnumUnderlyingTypeColon) &&
5701 !Style.SpaceBeforeEnumUnderlyingTypeColon) {
5704 if (
Right.is(TT_RangeBasedForLoopColon) &&
5705 !Style.SpaceBeforeRangeBasedForLoopColon) {
5708 if (
Left.is(TT_BitFieldColon)) {
5712 if (
Right.is(tok::colon)) {
5713 if (
Right.is(TT_CaseLabelColon))
5714 return Style.SpaceBeforeCaseColon;
5715 if (
Right.is(TT_GotoLabelColon))
5718 if (!
Right.getNextNonComment())
5720 if (
Right.isOneOf(TT_ObjCSelector, TT_ObjCMethodExpr))
5722 if (
Left.is(tok::question))
5724 if (
Right.is(TT_InlineASMColon) &&
Left.is(tok::coloncolon))
5726 if (
Right.is(TT_DictLiteral))
5727 return Style.SpacesInContainerLiterals;
5728 if (
Right.is(TT_AttributeColon))
5730 if (
Right.is(TT_CSharpNamedArgumentColon))
5732 if (
Right.is(TT_GenericSelectionColon))
5734 if (
Right.is(TT_BitFieldColon)) {
5741 if ((
Left.isOneOf(tok::minus, tok::minusminus) &&
5742 Right.isOneOf(tok::minus, tok::minusminus)) ||
5743 (
Left.isOneOf(tok::plus, tok::plusplus) &&
5744 Right.isOneOf(tok::plus, tok::plusplus))) {
5747 if (
Left.is(TT_UnaryOperator)) {
5750 if (
Left.is(tok::amp) &&
Right.is(tok::r_square))
5751 return Style.SpacesInSquareBrackets;
5752 if (
Left.isNot(tok::exclaim))
5754 if (
Left.TokenText ==
"!")
5755 return Style.SpaceAfterLogicalNot;
5756 assert(
Left.TokenText ==
"not");
5757 return Right.isOneOf(tok::coloncolon, TT_UnaryOperator) ||
5758 (
Right.is(tok::l_paren) && Style.SpaceBeforeParensOptions.AfterNot);
5763 if (
Left.is(TT_CastRParen)) {
5764 return Style.SpaceAfterCStyleCast ||
5765 Right.isOneOf(TT_BinaryOperator, TT_SelectorName);
5768 auto ShouldAddSpacesInAngles = [
this, &
Right]() {
5772 return Right.hasWhitespaceBefore();
5776 if (
Left.is(tok::greater) &&
Right.is(tok::greater)) {
5777 if (Style.isTextProto() ||
5781 return Right.is(TT_TemplateCloser) &&
Left.is(TT_TemplateCloser) &&
5783 ShouldAddSpacesInAngles());
5785 if (
Right.isOneOf(tok::arrow, tok::arrowstar, tok::periodstar) ||
5786 Left.isOneOf(tok::arrow, tok::period, tok::arrowstar, tok::periodstar) ||
5787 (
Right.is(tok::period) &&
Right.isNot(TT_DesignatedInitializerPeriod))) {
5790 if (!Style.SpaceBeforeAssignmentOperators &&
Left.isNot(TT_TemplateCloser) &&
5794 if (Style.isJava() &&
Right.is(tok::coloncolon) &&
5795 Left.isOneOf(tok::identifier, tok::kw_this)) {
5798 if (
Right.is(tok::coloncolon) &&
Left.is(tok::identifier)) {
5800 return Left.isPossibleMacro(
true) &&
5801 Right.hasWhitespaceBefore();
5803 if (
Right.is(tok::coloncolon) &&
5804 Left.isNoneOf(tok::l_brace, tok::comment, tok::l_paren)) {
5806 return (
Left.is(TT_TemplateOpener) &&
5808 ShouldAddSpacesInAngles())) ||
5809 Left.isNoneOf(tok::l_paren, tok::r_paren, tok::l_square,
5810 tok::kw___super, TT_TemplateOpener,
5811 TT_TemplateCloser) ||
5812 (
Left.is(tok::l_paren) && Style.SpacesInParensOptions.Other);
5814 if ((
Left.is(TT_TemplateOpener)) != (
Right.is(TT_TemplateCloser)))
5815 return ShouldAddSpacesInAngles();
5816 if (
Left.is(tok::r_paren) &&
Left.isNot(TT_TypeDeclarationParen) &&
5817 Right.is(TT_PointerOrReference) &&
Right.isOneOf(tok::amp, tok::ampamp)) {
5821 if (
Right.is(TT_StructuredBindingLSquare)) {
5822 return Left.isNoneOf(tok::amp, tok::ampamp) ||
5826 if (
Right.Next &&
Right.Next->is(TT_StructuredBindingLSquare) &&
5827 Right.isOneOf(tok::amp, tok::ampamp)) {
5830 if ((
Right.is(TT_BinaryOperator) &&
Left.isNot(tok::l_paren)) ||
5831 (
Left.isOneOf(TT_BinaryOperator, TT_EnumEqual, TT_ConditionalExpr) &&
5832 Right.isNot(tok::r_paren))) {
5835 if (
Right.is(TT_TemplateOpener) &&
Left.is(tok::r_paren) &&
5836 Left.MatchingParen &&
5837 Left.MatchingParen->is(TT_OverloadedOperatorLParen)) {
5840 if (
Right.is(tok::less) &&
Left.isNot(tok::l_paren) &&
5844 if (
Right.is(TT_TrailingUnaryOperator))
5846 if (
Left.is(TT_RegexLiteral))
5848 return spaceRequiredBetween(
Line, Left, Right);
5854 Tok.isNoneOf(TT_ObjCBlockLBrace, TT_LambdaLBrace, TT_DictLiteral);
5859 return Tok.MatchingParen &&
Tok.MatchingParen->Next &&
5860 Tok.MatchingParen->Next->isOneOf(tok::comma, tok::r_paren,
5872 Tok.isNoneOf(TT_ObjCBlockLBrace, TT_DictLiteral);
5875bool TokenAnnotator::mustBreakBefore(AnnotatedLine &
Line,
5877 if (Right.NewlinesBefore > 1 && Style.MaxEmptyLinesToKeep > 0 &&
5878 (!Style.RemoveEmptyLinesInUnwrappedLines || &Right ==
Line.First)) {
5884 if (Style.BreakFunctionDeclarationParameters &&
Line.MightBeFunctionDecl &&
5885 !
Line.mightBeFunctionDefinition() && Left.MightBeFunctionDeclParen &&
5886 Left.ParameterCount > 0) {
5890 if (Style.BreakFunctionDefinitionParameters &&
Line.MightBeFunctionDecl &&
5891 Line.mightBeFunctionDefinition() &&
Left.MightBeFunctionDeclParen &&
5892 Left.ParameterCount > 0) {
5899 Line.MightBeFunctionDecl && !
Left.opensScope() &&
5904 const auto *BeforeLeft =
Left.Previous;
5905 const auto *AfterRight =
Right.Next;
5907 if (Style.isCSharp()) {
5908 if (
Left.is(TT_FatArrow) &&
Right.is(tok::l_brace) &&
5909 Style.BraceWrapping.AfterFunction) {
5912 if (
Right.is(TT_CSharpNamedArgumentColon) ||
5913 Left.is(TT_CSharpNamedArgumentColon)) {
5916 if (
Right.is(TT_CSharpGenericTypeConstraint))
5918 if (AfterRight && AfterRight->is(TT_FatArrow) &&
5919 (
Right.is(tok::numeric_constant) ||
5920 (
Right.is(tok::identifier) &&
Right.TokenText ==
"_"))) {
5925 if (
Left.is(TT_AttributeRSquare) &&
5926 (
Right.isAccessSpecifier(
false) ||
5927 Right.is(Keywords.kw_internal))) {
5931 if (
Left.is(TT_AttributeRSquare) &&
Right.is(TT_AttributeLSquare))
5933 }
else if (Style.isJavaScript()) {
5935 if (
Right.is(tok::string_literal) &&
Left.is(tok::plus) && BeforeLeft &&
5936 BeforeLeft->is(tok::string_literal)) {
5939 if (
Left.is(TT_DictLiteral) &&
Left.is(tok::l_brace) &&
Line.Level == 0 &&
5940 BeforeLeft && BeforeLeft->is(tok::equal) &&
5941 Line.First->isOneOf(tok::identifier, Keywords.kw_import, tok::kw_export,
5945 Line.First->isNoneOf(Keywords.kw_var, Keywords.kw_let)) {
5950 if (
Left.is(tok::l_brace) &&
Line.Level == 0 &&
5951 (
Line.startsWith(tok::kw_enum) ||
5952 Line.startsWith(tok::kw_const, tok::kw_enum) ||
5953 Line.startsWith(tok::kw_export, tok::kw_enum) ||
5954 Line.startsWith(tok::kw_export, tok::kw_const, tok::kw_enum))) {
5959 if (
Right.is(tok::r_brace) &&
Left.is(tok::l_brace) && BeforeLeft &&
5960 BeforeLeft->is(TT_FatArrow)) {
5962 switch (Style.AllowShortLambdasOnASingleLine) {
5968 return !
Left.Children.empty();
5972 return (
Left.NestingLevel == 0 &&
Line.Level == 0) &&
5973 !
Left.Children.empty();
5975 llvm_unreachable(
"Unknown FormatStyle::ShortLambdaStyle enum");
5978 if (
Right.is(tok::r_brace) &&
Left.is(tok::l_brace) &&
5979 !
Left.Children.empty()) {
5981 if (
Left.NestingLevel == 0 &&
Line.Level == 0)
5982 return !Style.AllowShortFunctionsOnASingleLine.Other;
5984 return !Style.AllowShortFunctionsOnASingleLine.Inline;
5986 }
else if (Style.isJava()) {
5987 if (
Right.is(tok::plus) &&
Left.is(tok::string_literal) && AfterRight &&
5988 AfterRight->is(tok::string_literal)) {
5991 }
else if (Style.isVerilog()) {
5993 if (
Left.is(TT_VerilogAssignComma))
5996 if (
Left.is(TT_VerilogTypeComma))
6000 if (Style.VerilogBreakBetweenInstancePorts &&
6001 (
Left.is(TT_VerilogInstancePortComma) ||
6002 (
Left.is(tok::r_paren) && Keywords.isVerilogIdentifier(Right) &&
6003 Left.MatchingParen &&
6004 Left.MatchingParen->is(TT_VerilogInstancePortLParen)))) {
6009 if (!Keywords.isVerilogBegin(Right) && Keywords.isVerilogEndOfLabel(Left))
6011 }
else if (Style.BreakAdjacentStringLiterals &&
6012 (IsCpp || Style.isProto() || Style.isTableGen())) {
6013 if (
Left.isStringLiteral() &&
Right.isStringLiteral())
6018 if (Style.isJson()) {
6022 if (
Left.is(TT_DictLiteral) &&
Left.is(tok::l_brace))
6025 if ((
Left.is(TT_ArrayInitializerLSquare) &&
Left.is(tok::l_square) &&
6026 Right.isNot(tok::r_square)) ||
6027 Left.is(tok::comma)) {
6028 if (
Right.is(tok::l_brace))
6033 if (
Tok->isOneOf(tok::l_brace, tok::l_square))
6035 if (
Tok->isOneOf(tok::r_brace, tok::r_square))
6038 return Style.BreakArrays;
6040 }
else if (Style.isTableGen()) {
6044 if (
Left.is(TT_TableGenCondOperatorComma))
6046 if (
Left.is(TT_TableGenDAGArgOperatorToBreak) &&
6047 Right.isNot(TT_TableGenDAGArgCloser)) {
6050 if (
Left.is(TT_TableGenDAGArgListCommaToBreak))
6052 if (
Right.is(TT_TableGenDAGArgCloser) &&
Right.MatchingParen &&
6053 Right.MatchingParen->is(TT_TableGenDAGArgOpenerToBreak) &&
6054 &Left !=
Right.MatchingParen->Next) {
6060 if (
Line.startsWith(tok::kw_asm) &&
Right.is(TT_InlineASMColon) &&
6071 if ((
Left.isOneOf(tok::l_brace, TT_ArrayInitializerLSquare) ||
6072 (Style.isJavaScript() &&
Left.is(tok::l_paren))) &&
6074 BeforeClosingBrace =
Left.MatchingParen->Previous;
6075 }
else if (
Right.MatchingParen &&
6076 (
Right.MatchingParen->isOneOf(tok::l_brace,
6077 TT_ArrayInitializerLSquare) ||
6078 (Style.isJavaScript() &&
6079 Right.MatchingParen->is(tok::l_paren)))) {
6080 BeforeClosingBrace = &
Left;
6082 if (BeforeClosingBrace &&
6083 (BeforeClosingBrace->isOneOf(tok::comma, TT_TrailingCommaVaOptRParen) ||
6084 BeforeClosingBrace->isTrailingComment())) {
6089 if (
Right.is(tok::comment)) {
6091 Right.NewlinesBefore > 0 &&
Right.HasUnescapedNewline;
6093 if (
Left.isTrailingComment())
6095 if (
Left.IsUnterminatedLiteral)
6098 if (BeforeLeft && BeforeLeft->is(tok::lessless) &&
6099 Left.is(tok::string_literal) &&
Right.is(tok::lessless) && AfterRight &&
6100 AfterRight->is(tok::string_literal)) {
6101 return Right.NewlinesBefore > 0;
6104 if (
Right.is(TT_RequiresClause)) {
6105 switch (Style.RequiresClausePosition) {
6115 if (
Left.ClosesTemplateDeclaration &&
Left.MatchingParen &&
6116 Left.MatchingParen->NestingLevel == 0) {
6120 if (
Right.is(tok::kw_concept))
6124 Right.NewlinesBefore > 0);
6126 if (
Left.ClosesRequiresClause) {
6127 switch (Style.RequiresClausePosition) {
6130 return Right.isNot(tok::semi);
6132 return Right.isNoneOf(tok::semi, tok::l_brace);
6139 (
Left.is(TT_CtorInitializerComma) ||
6140 Right.is(TT_CtorInitializerColon))) {
6145 Left.isOneOf(TT_CtorInitializerColon, TT_CtorInitializerComma)) {
6150 Left.is(TT_CtorInitializerComma)) {
6156 Right.isOneOf(TT_CtorInitializerComma, TT_CtorInitializerColon)) {
6162 Right.is(TT_CtorInitializerColon)) {
6167 Left.is(TT_CtorInitializerColon)) {
6173 Right.is(TT_InheritanceComma)) {
6177 Left.is(TT_InheritanceComma)) {
6180 if (
Right.is(tok::string_literal) &&
Right.TokenText.starts_with(
"R\"")) {
6184 return Right.IsMultiline &&
Right.NewlinesBefore > 0;
6186 if ((
Left.is(tok::l_brace) ||
6187 (
Left.is(tok::less) && BeforeLeft && BeforeLeft->is(tok::equal))) &&
6193 if (
Right.is(TT_InlineASMBrace))
6194 return Right.HasUnescapedNewline;
6197 auto *FirstNonComment =
Line.getFirstNonComment();
6199 FirstNonComment && (FirstNonComment->is(Keywords.kw_internal) ||
6200 FirstNonComment->isAccessSpecifierKeyword());
6202 if (Style.BraceWrapping.AfterEnum) {
6203 if (
Line.startsWith(tok::kw_enum) ||
6204 Line.startsWith(tok::kw_typedef, tok::kw_enum) ||
6205 Line.startsWith(tok::kw_export, tok::kw_enum)) {
6210 FirstNonComment->Next->is(tok::kw_enum)) {
6216 if (Style.BraceWrapping.AfterClass &&
6218 FirstNonComment->Next->is(Keywords.kw_interface)) ||
6219 Line.startsWith(Keywords.kw_interface))) {
6224 if (
Right.isNot(TT_FunctionLBrace)) {
6226 ((
Line.startsWith(tok::kw_class) &&
6227 Style.BraceWrapping.AfterClass) ||
6228 (
Line.startsWith(tok::kw_struct) &&
6229 Style.BraceWrapping.AfterStruct) ||
6230 (
Line.startsWith(tok::kw_union) &&
6231 Style.BraceWrapping.AfterUnion));
6235 if (
Left.is(TT_ObjCBlockLBrace) &&
6241 if (
Left.isOneOf(TT_AttributeRParen, TT_AttributeMacro) &&
6242 Right.is(TT_ObjCDecl)) {
6246 if (
Left.is(TT_LambdaLBrace)) {
6254 (!
Left.Children.empty() &&
6260 if (Style.BraceWrapping.BeforeLambdaBody &&
Right.is(TT_LambdaLBrace) &&
6261 (
Left.isPointerOrReference() ||
Left.is(TT_TemplateCloser))) {
6267 if (Style.BraceWrapping.BeforeLambdaBody &&
Right.is(TT_LambdaLBrace) &&
6269 const auto SLS = Style.AllowShortLambdasOnASingleLine;
6277 if ((Style.isJava() || Style.isJavaScript()) &&
6278 Left.is(TT_LeadingJavaAnnotation) &&
6279 Right.isNoneOf(TT_LeadingJavaAnnotation, tok::l_paren) &&
6280 (
Line.Last->is(tok::l_brace) || Style.BreakAfterJavaFieldAnnotations)) {
6284 if (
Right.is(TT_ProtoExtensionLSquare))
6314 if (Style.isProto() &&
Right.is(TT_SelectorName) &&
6315 Right.isNot(tok::r_square) && AfterRight) {
6318 if (
Left.is(tok::at))
6324 const auto *LBrace = AfterRight;
6325 if (LBrace && LBrace->is(tok::colon)) {
6326 LBrace = LBrace->Next;
6327 if (LBrace && LBrace->is(tok::at)) {
6328 LBrace = LBrace->Next;
6330 LBrace = LBrace->Next;
6342 ((LBrace->is(tok::l_brace) &&
6343 (LBrace->is(TT_DictLiteral) ||
6344 (LBrace->Next && LBrace->Next->is(tok::r_brace)))) ||
6345 LBrace->isOneOf(TT_ArrayInitializerLSquare, tok::less))) {
6352 if (
Left.ParameterCount == 0)
6367 if (
Left.isOneOf(tok::r_brace, tok::greater, tok::r_square))
6372 Left.is(TT_AttributeRSquare) &&
Right.NewlinesBefore > 0) {
6373 Line.ReturnTypeWrapped =
true;
6384 if (Style.isCSharp()) {
6385 if (
Left.isOneOf(TT_CSharpNamedArgumentColon, TT_AttributeColon) ||
6386 Right.isOneOf(TT_CSharpNamedArgumentColon, TT_AttributeColon)) {
6390 if (
Line.First->is(TT_CSharpGenericTypeConstraint))
6391 return Left.is(TT_CSharpGenericTypeConstraintComma);
6393 if (
Right.is(TT_CSharpNullable))
6395 }
else if (Style.isJava()) {
6396 if (
Left.isOneOf(Keywords.kw_throws, Keywords.kw_extends,
6397 Keywords.kw_implements)) {
6400 if (
Right.isOneOf(Keywords.kw_throws, Keywords.kw_extends,
6401 Keywords.kw_implements)) {
6404 }
else if (Style.isJavaScript()) {
6407 (NonComment->isAccessSpecifierKeyword() ||
6408 NonComment->isOneOf(
6409 tok::kw_return, Keywords.kw_yield, tok::kw_continue, tok::kw_break,
6410 tok::kw_throw, Keywords.kw_interface, Keywords.kw_type,
6411 tok::kw_static, Keywords.kw_readonly, Keywords.kw_override,
6412 Keywords.kw_abstract, Keywords.kw_get, Keywords.kw_set,
6413 Keywords.kw_async, Keywords.kw_await))) {
6416 if (
Right.NestingLevel == 0 &&
6417 (
Left.Tok.getIdentifierInfo() ||
6418 Left.isOneOf(tok::r_square, tok::r_paren)) &&
6419 Right.isOneOf(tok::l_square, tok::l_paren)) {
6422 if (NonComment && NonComment->is(tok::identifier) &&
6423 NonComment->TokenText ==
"asserts") {
6426 if (
Left.is(TT_FatArrow) &&
Right.is(tok::l_brace))
6428 if (
Left.is(TT_JsTypeColon))
6431 if (
Left.is(tok::exclaim) &&
Right.is(tok::colon))
6436 if (
Right.is(Keywords.kw_is)) {
6445 if (!
Next ||
Next->isNot(tok::colon))
6448 if (
Left.is(Keywords.kw_in))
6450 if (
Right.is(Keywords.kw_in))
6452 if (
Right.is(Keywords.kw_as))
6454 if (
Right.isOneOf(Keywords.kw_extends, Keywords.kw_infer)) {
6460 if (
Left.is(Keywords.kw_as))
6462 if (
Left.is(TT_NonNullAssertion))
6464 if (
Left.is(Keywords.kw_declare) &&
6465 Right.isOneOf(Keywords.kw_module, tok::kw_namespace,
6466 Keywords.kw_function, tok::kw_class, tok::kw_enum,
6467 Keywords.kw_interface, Keywords.kw_type, Keywords.kw_var,
6468 Keywords.kw_let, tok::kw_const)) {
6473 if (
Left.isOneOf(Keywords.kw_module, tok::kw_namespace) &&
6474 Right.isOneOf(tok::identifier, tok::string_literal)) {
6477 if (
Right.is(TT_TemplateString) &&
Right.closesScope())
6481 if (
Left.is(tok::identifier) &&
Right.is(TT_TemplateString))
6483 if (
Left.is(TT_TemplateString) &&
Left.opensScope())
6485 }
else if (Style.isTableGen()) {
6487 if (Keywords.isTableGenDefinition(Left))
6490 if (
Right.is(tok::l_paren)) {
6491 return Left.isNoneOf(TT_TableGenBangOperator, TT_TableGenCondOperator,
6495 if (
Left.is(TT_TableGenValueSuffix))
6498 if (
Left.is(tok::hash) ||
Right.is(tok::hash))
6500 if (
Left.isOneOf(TT_TableGenBangOperator, TT_TableGenCondOperator))
6507 if (
Right.is(tok::r_brace)) {
6509 (
Right.isBlockIndentedInitRBrace(Style)));
6514 if (
Right.is(tok::r_paren)) {
6515 if (!
Right.MatchingParen)
6518 if (
Next &&
Next->is(tok::r_paren))
6520 if (
Next &&
Next->is(tok::l_paren))
6526 return Style.BreakBeforeCloseBracketIf;
6528 return Style.BreakBeforeCloseBracketLoop;
6530 return Style.BreakBeforeCloseBracketSwitch;
6531 return Style.BreakBeforeCloseBracketFunction;
6534 if (
Left.isOneOf(tok::r_paren, TT_TrailingAnnotation) &&
6535 Right.is(TT_TrailingAnnotation) &&
6536 Style.BreakBeforeCloseBracketFunction) {
6540 if (
Right.is(TT_TemplateCloser))
6541 return Style.BreakBeforeTemplateCloser;
6543 if (
Left.isOneOf(tok::at, tok::objc_interface))
6545 if (
Left.isOneOf(TT_JavaAnnotation, TT_LeadingJavaAnnotation))
6546 return Right.isNot(tok::l_paren);
6547 if (
Right.is(TT_PointerOrReference)) {
6548 return Line.IsMultiVariableDeclStmt ||
6549 (getTokenPointerOrReferenceAlignment(Right) ==
6552 Right.Next->isOneOf(TT_FunctionDeclarationName, tok::kw_const)));
6554 if (
Left.is(tok::hashhash) ||
Right.is(tok::hashhash))
6556 if (
Right.isOneOf(TT_StartOfName, TT_FunctionDeclarationName,
6557 TT_ClassHeadName, TT_QtProperty, tok::kw_operator)) {
6560 if (
Left.is(TT_PointerOrReference))
6562 if (
Right.isTrailingComment()) {
6569 (
Left.is(TT_CtorInitializerColon) &&
Right.NewlinesBefore > 0 &&
6572 if (
Left.is(tok::question) &&
Right.is(tok::colon))
6574 if (
Right.isOneOf(TT_ConditionalExpr, tok::question))
6575 return Style.BreakBeforeTernaryOperators;
6576 if (
Left.isOneOf(TT_ConditionalExpr, tok::question))
6577 return !Style.BreakBeforeTernaryOperators;
6578 if (
Left.is(TT_InheritanceColon))
6580 if (
Right.is(TT_InheritanceColon))
6583 if (
Right.is(TT_ObjCMethodExpr) &&
Right.isNot(tok::r_square) &&
6584 Left.isNot(TT_SelectorName)) {
6588 if (
Right.is(tok::colon) &&
6589 Right.isNoneOf(TT_CtorInitializerColon, TT_InlineASMColon,
6590 TT_BitFieldColon)) {
6593 if (
Left.is(tok::colon) &&
Left.isOneOf(TT_ObjCSelector, TT_ObjCMethodExpr))
6595 if (
Left.is(tok::colon) &&
Left.is(TT_DictLiteral)) {
6596 if (Style.isProto()) {
6597 if (!Style.AlwaysBreakBeforeMultilineStrings &&
Right.isStringLiteral())
6623 if ((
Right.isOneOf(tok::l_brace, tok::less) &&
6624 Right.is(TT_DictLiteral)) ||
6625 Right.is(TT_ArrayInitializerLSquare)) {
6631 if (
Right.is(tok::r_square) &&
Right.MatchingParen &&
6632 Right.MatchingParen->is(TT_ProtoExtensionLSquare)) {
6635 if (
Right.is(TT_SelectorName) || (
Right.is(tok::identifier) &&
Right.Next &&
6636 Right.Next->is(TT_ObjCMethodExpr))) {
6637 return Left.isNot(tok::period);
6641 if (
Right.is(tok::kw_concept))
6643 if (
Right.is(TT_RequiresClause))
6645 if (
Left.ClosesTemplateDeclaration) {
6647 Right.NewlinesBefore > 0;
6649 if (
Left.is(TT_FunctionAnnotationRParen))
6651 if (
Left.ClosesRequiresClause)
6653 if (
Right.isOneOf(TT_RangeBasedForLoopColon, TT_OverloadedOperatorLParen,
6654 TT_OverloadedOperator)) {
6657 if (
Left.is(TT_RangeBasedForLoopColon))
6659 if (
Right.is(TT_RangeBasedForLoopColon))
6661 if (
Left.is(TT_TemplateCloser) &&
Right.is(TT_TemplateOpener))
6663 if ((
Left.is(tok::greater) &&
Right.is(tok::greater)) ||
6664 (
Left.is(tok::less) &&
Right.is(tok::less))) {
6667 if (
Right.is(TT_BinaryOperator) &&
6673 if (
Left.isOneOf(TT_TemplateCloser, TT_UnaryOperator, tok::kw_operator))
6675 if (
Left.is(tok::equal) &&
Right.isNoneOf(tok::kw_default, tok::kw_delete) &&
6679 if (
Left.is(tok::equal) &&
Right.is(tok::l_brace) &&
6683 if (
Left.is(TT_AttributeLParen) ||
6684 (
Left.is(tok::l_paren) &&
Left.is(TT_TypeDeclarationParen))) {
6687 if (
Left.is(tok::l_paren) &&
Left.Previous &&
6688 (
Left.Previous->isOneOf(TT_BinaryOperator, TT_CastRParen))) {
6691 if (
Right.is(TT_ImplicitStringLiteral))
6694 if (
Right.is(tok::r_square) &&
Right.MatchingParen &&
6695 Right.MatchingParen->is(TT_LambdaLSquare)) {
6701 if (
Left.is(TT_TrailingAnnotation)) {
6702 return Right.isNoneOf(tok::l_brace, tok::semi, tok::equal, tok::l_paren,
6703 tok::less, tok::coloncolon);
6706 if (
Right.isAttribute())
6709 if (
Right.is(TT_AttributeLSquare)) {
6710 assert(
Left.isNot(tok::l_square));
6714 if (
Left.is(tok::identifier) &&
Right.is(tok::string_literal))
6717 if (
Right.is(tok::identifier) &&
Right.Next &&
Right.Next->is(TT_DictLiteral))
6720 if (
Left.is(TT_CtorInitializerColon)) {
6721 return (Style.BreakConstructorInitializers ==
6723 Style.BreakConstructorInitializers ==
6725 (!
Right.isTrailingComment() ||
Right.NewlinesBefore > 0);
6727 if (
Right.is(TT_CtorInitializerColon)) {
6731 if (
Left.is(TT_CtorInitializerComma) &&
6735 if (
Right.is(TT_CtorInitializerComma) &&
6739 if (
Left.is(TT_InheritanceComma) &&
6743 if (
Right.is(TT_InheritanceComma) &&
6747 if (
Left.is(TT_ArrayInitializerLSquare))
6749 if (
Right.is(tok::kw_typename) &&
Left.isNot(tok::kw_const))
6751 if ((
Left.isBinaryOperator() ||
Left.is(TT_BinaryOperator)) &&
6752 Left.isNoneOf(tok::arrowstar, tok::lessless) &&
6758 if (
Left.is(TT_AttributeLSquare) &&
Right.is(tok::l_square)) {
6759 assert(
Right.isNot(TT_AttributeLSquare));
6762 if (
Left.is(tok::r_square) &&
Right.is(TT_AttributeRSquare)) {
6763 assert(
Left.isNot(TT_AttributeRSquare));
6767 if (Style.BraceWrapping.AfterRequiresExpression &&
6768 Right.is(TT_RequiresExpressionLBrace)) {
6772 auto ShortLambdaOption = Style.AllowShortLambdasOnASingleLine;
6773 if (Style.BraceWrapping.BeforeLambdaBody &&
Right.is(TT_LambdaLBrace)) {
6780 if (
Right.is(tok::kw_noexcept) &&
Right.is(TT_TrailingAnnotation)) {
6781 switch (Style.AllowBreakBeforeNoexceptSpecifier) {
6787 return Right.Next &&
Right.Next->is(tok::l_paren);
6791 return Left.isOneOf(tok::comma, TT_TrailingCommaVaOptRParen, tok::coloncolon,
6792 tok::semi, tok::l_brace, tok::kw_class, tok::kw_struct,
6794 Right.isMemberAccess() ||
6795 Right.isOneOf(TT_TrailingReturnArrow, TT_LambdaArrow, tok::lessless,
6796 tok::colon, tok::l_square, tok::at) ||
6797 (
Left.is(tok::r_paren) &&
6798 Right.isOneOf(tok::identifier, tok::kw_const)) ||
6799 (
Left.is(tok::l_paren) &&
Right.isNot(tok::r_paren)) ||
6800 (
Left.is(TT_TemplateOpener) &&
Right.isNot(TT_TemplateCloser));
6804 llvm::errs() <<
"AnnotatedTokens(L=" <<
Line.Level <<
", P=" <<
Line.PPLevel
6805 <<
", T=" <<
Line.Type <<
", C=" <<
Line.IsContinuation
6809 llvm::errs() <<
" I=" <<
Tok->IndentLevel <<
" M=" <<
Tok->MustBreakBefore
6810 <<
" C=" <<
Tok->CanBreakBefore
6812 <<
" S=" <<
Tok->SpacesRequiredBefore
6813 <<
" F=" <<
Tok->Finalized <<
" B=" <<
Tok->BlockParameterCount
6814 <<
" BK=" <<
Tok->getBlockKind() <<
" P=" <<
Tok->SplitPenalty
6815 <<
" Name=" <<
Tok->Tok.getName() <<
" N=" <<
Tok->NestingLevel
6816 <<
" L=" <<
Tok->TotalLength
6817 <<
" PPK=" <<
Tok->getPackingKind() <<
" FakeLParens=";
6819 llvm::errs() << LParen <<
"/";
6820 llvm::errs() <<
" FakeRParens=" <<
Tok->FakeRParens;
6821 llvm::errs() <<
" II=" <<
Tok->Tok.getIdentifierInfo();
6822 llvm::errs() <<
" Text='" <<
Tok->TokenText <<
"'\n";
6827 llvm::errs() <<
"----\n";
6832 assert(
Reference.isOneOf(tok::amp, tok::ampamp));
6833 switch (Style.ReferenceAlignment) {
6835 return Style.PointerAlignment;
6844 return Style.PointerAlignment;
6848TokenAnnotator::getTokenPointerOrReferenceAlignment(
6850 if (PointerOrReference.isOneOf(tok::amp, tok::ampamp))
6851 return getTokenReferenceAlignment(PointerOrReference);
6852 assert(PointerOrReference.is(tok::star));
6853 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.