18#include "llvm/ADT/SmallPtrSet.h"
19#include "llvm/Support/Debug.h"
21#define DEBUG_TYPE "format-token-annotator"
28 switch (Style.BreakAfterAttributes) {
34 return Tok.NewlinesBefore > 0;
42static bool startsWithInitStatement(
const AnnotatedLine &
Line) {
43 return Line.startsWith(tok::kw_for) ||
Line.startsWith(tok::kw_if) ||
44 Line.startsWith(tok::kw_switch);
59 return Tok.Tok.getIdentifierInfo();
65static bool isLambdaParameterList(
const FormatToken *Left) {
67 if (
Left->Previous &&
Left->Previous->is(tok::greater) &&
68 Left->Previous->MatchingParen &&
69 Left->Previous->MatchingParen->is(TT_TemplateOpener)) {
70 Left =
Left->Previous->MatchingParen;
74 return Left->Previous &&
Left->Previous->is(tok::r_square) &&
75 Left->Previous->MatchingParen &&
76 Left->Previous->MatchingParen->is(TT_LambdaLSquare);
82 return Tok.isOneOf(tok::kw_if, tok::kw_for, tok::kw_while, tok::kw_switch,
83 tok::kw_constexpr, tok::kw_catch);
88 if (!IsCpp || !
Tok.startsSequence(tok::l_square, tok::l_square))
92 if (
Tok.Previous &&
Tok.Previous->isOneOf(tok::at, tok::l_square))
99 if (AttrTok->startsSequence(tok::kw_using, tok::identifier, tok::colon))
101 if (AttrTok->isNot(tok::identifier))
103 while (AttrTok && !AttrTok->startsSequence(tok::r_square, tok::r_square)) {
107 if (AttrTok->is(tok::colon) ||
108 AttrTok->startsSequence(tok::identifier, tok::identifier) ||
109 AttrTok->startsSequence(tok::r_paren, tok::identifier)) {
112 if (AttrTok->is(tok::ellipsis))
114 AttrTok = AttrTok->Next;
116 return AttrTok && AttrTok->startsSequence(tok::r_square, tok::r_square);
124class AnnotatingParser {
126 AnnotatingParser(
const FormatStyle &Style, AnnotatedLine &Line,
127 const AdditionalKeywords &Keywords,
128 SmallVector<ScopeType> &Scopes)
129 : Style(Style), Line(Line), CurrentToken(Line.
First), AutoFound(
false),
131 Keywords(Keywords), Scopes(Scopes), TemplateDeclarationDepth(0) {
132 Contexts.push_back(Context(tok::unknown, 1,
false));
133 resetTokenMetadata();
138 switch (Token.getType()) {
140 case TT_StructLBrace:
145 case TT_CompoundRequirementLBrace:
156 auto *
Left = CurrentToken->Previous;
160 if (NonTemplateLess.count(Left) > 0)
163 const auto *BeforeLess =
Left->Previous;
166 if (BeforeLess->Tok.isLiteral())
168 if (BeforeLess->is(tok::r_brace))
170 if (BeforeLess->is(tok::r_paren) && Contexts.size() > 1 &&
171 !(BeforeLess->MatchingParen &&
172 BeforeLess->MatchingParen->is(TT_OverloadedOperatorLParen))) {
175 if (BeforeLess->is(tok::kw_operator) && CurrentToken->is(tok::l_paren))
179 Left->ParentBracket = Contexts.back().ContextKind;
180 ScopedContextCreator ContextCreator(*
this, tok::less, 12);
181 Contexts.back().IsExpression =
false;
185 if (BeforeLess && BeforeLess->isNot(tok::kw_template))
186 Contexts.back().ContextType = Context::TemplateArgument;
188 if (Style.isJava() && CurrentToken->is(tok::question))
191 for (
bool SeenTernaryOperator =
false, MaybeAngles =
true; CurrentToken;) {
192 const auto &ParentContext = Contexts[Contexts.size() - 2];
193 const bool InExpr = ParentContext.IsExpression;
194 if (CurrentToken->is(tok::greater)) {
195 const auto *
Next = CurrentToken->Next;
196 if (CurrentToken->isNot(TT_TemplateCloser)) {
203 if (
Next &&
Next->is(tok::greater) &&
204 Left->ParentBracket != tok::less &&
205 CurrentToken->getStartOfNonWhitespace() ==
206 Next->getStartOfNonWhitespace().getLocWithOffset(-1)) {
209 if (InExpr && SeenTernaryOperator &&
210 (!
Next ||
Next->isNoneOf(tok::l_paren, tok::l_brace))) {
215 if (ParentContext.InStaticAssertFirstArgument &&
Next &&
216 Next->isOneOf(tok::minus, tok::identifier)) {
220 Left->MatchingParen = CurrentToken;
221 CurrentToken->MatchingParen =
Left;
227 if (Style.isTextProto() ||
229 BeforeLess->isOneOf(TT_SelectorName, TT_DictLiteral))) {
230 CurrentToken->setType(TT_DictLiteral);
232 CurrentToken->setType(TT_TemplateCloser);
233 CurrentToken->Tok.setLength(1);
240 if (BeforeLess && BeforeLess->is(TT_TemplateName)) {
244 if (CurrentToken->is(tok::question) && Style.isJava()) {
248 if (CurrentToken->isOneOf(tok::r_paren, tok::r_square, tok::r_brace))
250 const auto &Prev = *CurrentToken->Previous;
257 if (MaybeAngles && InExpr && !Line.startsWith(tok::kw_template) &&
258 Prev.is(TT_BinaryOperator) &&
259 Prev.isOneOf(tok::pipepipe, tok::ampamp)) {
262 if (Prev.isOneOf(tok::question, tok::colon) && !Style.isProto())
263 SeenTernaryOperator =
true;
264 updateParameterCount(Left, CurrentToken);
267 if (CurrentToken->is(tok::colon) ||
268 (CurrentToken->isOneOf(tok::l_brace, tok::less) &&
273 }
else if (Style.isTableGen()) {
274 if (CurrentToken->isOneOf(tok::comma, tok::equal)) {
281 if (!parseTableGenValue())
291 bool parseUntouchableParens() {
292 while (CurrentToken) {
293 CurrentToken->Finalized =
true;
294 switch (CurrentToken->Tok.getKind()) {
297 if (!parseUntouchableParens())
312 bool parseParens(
bool IsIf =
false) {
315 assert(CurrentToken->Previous &&
"Unknown previous token");
316 FormatToken &OpeningParen = *CurrentToken->Previous;
317 assert(OpeningParen.is(tok::l_paren));
318 FormatToken *PrevNonComment = OpeningParen.getPreviousNonComment();
319 OpeningParen.ParentBracket = Contexts.back().ContextKind;
320 ScopedContextCreator ContextCreator(*
this, tok::l_paren, 1);
323 Contexts.back().ColonIsForRangeExpr =
324 Contexts.size() == 2 && Contexts[0].ColonIsForRangeExpr;
326 if (OpeningParen.Previous &&
327 OpeningParen.Previous->is(TT_UntouchableMacroFunc)) {
328 OpeningParen.Finalized =
true;
329 return parseUntouchableParens();
332 bool StartsObjCSelector =
false;
333 if (!Style.isVerilog()) {
334 if (
FormatToken *MaybeSel = OpeningParen.Previous) {
336 if (MaybeSel->is(tok::objc_selector) && MaybeSel->Previous &&
337 MaybeSel->Previous->is(tok::at)) {
338 StartsObjCSelector =
true;
343 if (OpeningParen.is(TT_OverloadedOperatorLParen)) {
346 while (Prev->isNot(tok::kw_operator)) {
347 Prev = Prev->Previous;
348 assert(Prev &&
"Expect a kw_operator prior to the OperatorLParen!");
354 bool OperatorCalledAsMemberFunction =
355 Prev->Previous && Prev->Previous->isOneOf(tok::period, tok::arrow);
356 Contexts.back().IsExpression = OperatorCalledAsMemberFunction;
357 }
else if (OpeningParen.is(TT_VerilogInstancePortLParen)) {
358 Contexts.back().IsExpression =
true;
359 Contexts.back().ContextType = Context::VerilogInstancePortList;
360 }
else if (Style.isJavaScript() &&
361 (Line.startsWith(Keywords.kw_type, tok::identifier) ||
362 Line.startsWith(tok::kw_export, Keywords.kw_type,
366 Contexts.back().IsExpression =
false;
367 }
else if (OpeningParen.Previous &&
368 (OpeningParen.Previous->isOneOf(
369 tok::kw_noexcept, tok::kw_explicit, tok::kw_while,
370 tok::l_paren, tok::comma, TT_CastRParen,
371 TT_BinaryOperator) ||
372 OpeningParen.Previous->isIf())) {
374 Contexts.back().IsExpression =
true;
375 }
else if (Style.isJavaScript() && OpeningParen.Previous &&
376 (OpeningParen.Previous->is(Keywords.kw_function) ||
377 (OpeningParen.Previous->endsSequence(tok::identifier,
378 Keywords.kw_function)))) {
380 Contexts.back().IsExpression =
false;
381 }
else if (Style.isJavaScript() && OpeningParen.Previous &&
382 OpeningParen.Previous->is(TT_JsTypeColon)) {
384 Contexts.back().IsExpression =
false;
385 }
else if (isLambdaParameterList(&OpeningParen)) {
387 OpeningParen.setType(TT_LambdaDefinitionLParen);
388 Contexts.back().IsExpression =
false;
389 }
else if (OpeningParen.is(TT_RequiresExpressionLParen)) {
390 Contexts.back().IsExpression =
false;
391 }
else if (OpeningParen.Previous &&
392 OpeningParen.Previous->is(tok::kw__Generic)) {
393 Contexts.back().ContextType = Context::C11GenericSelection;
394 Contexts.back().IsExpression =
true;
395 }
else if (OpeningParen.Previous &&
396 OpeningParen.Previous->TokenText ==
"Q_PROPERTY") {
397 Contexts.back().ContextType = Context::QtProperty;
398 Contexts.back().IsExpression =
false;
399 }
else if (Line.InPPDirective &&
400 (!OpeningParen.Previous ||
401 OpeningParen.Previous->isNot(tok::identifier))) {
402 Contexts.back().IsExpression =
true;
403 }
else if (Contexts[Contexts.size() - 2].CaretFound) {
405 Contexts.back().IsExpression =
false;
406 }
else if (OpeningParen.Previous &&
407 OpeningParen.Previous->is(TT_ForEachMacro)) {
409 Contexts.back().ContextType = Context::ForEachMacro;
410 Contexts.back().IsExpression =
false;
411 }
else if (OpeningParen.Previous && OpeningParen.Previous->MatchingParen &&
412 OpeningParen.Previous->MatchingParen->isOneOf(
413 TT_ObjCBlockLParen, TT_FunctionTypeLParen)) {
414 Contexts.back().IsExpression =
false;
415 }
else if (!Line.MustBeDeclaration &&
416 (!Line.InPPDirective || (Line.InMacroBody && !Scopes.empty()))) {
418 OpeningParen.Previous &&
419 OpeningParen.Previous->isOneOf(tok::kw_for, tok::kw_catch);
420 Contexts.back().IsExpression = !IsForOrCatch;
423 if (Style.isTableGen()) {
425 if (Prev->is(TT_TableGenCondOperator)) {
426 Contexts.back().IsTableGenCondOpe =
true;
427 Contexts.back().IsExpression =
true;
428 }
else if (Contexts.size() > 1 &&
429 Contexts[Contexts.size() - 2].IsTableGenBangOpe) {
434 Contexts.back().IsTableGenBangOpe =
true;
435 Contexts.back().IsExpression =
true;
438 if (!parseTableGenDAGArg())
440 return parseTableGenDAGArgAndList(&OpeningParen);
447 if (PrevNonComment && OpeningParen.is(TT_Unknown)) {
448 if (PrevNonComment->isAttribute()) {
449 OpeningParen.setType(TT_AttributeLParen);
450 }
else if (PrevNonComment->isOneOf(TT_TypenameMacro, tok::kw_decltype,
453#include
"clang/Basic/BuiltinTraits.inc"
455 OpeningParen.setType(TT_TypeDeclarationParen);
457 if (PrevNonComment->isOneOf(tok::kw_decltype, tok::kw_typeof))
458 Contexts.back().IsExpression =
true;
462 if (StartsObjCSelector)
463 OpeningParen.setType(TT_ObjCSelector);
465 const bool IsStaticAssert =
466 PrevNonComment && PrevNonComment->is(tok::kw_static_assert);
468 Contexts.back().InStaticAssertFirstArgument =
true;
478 bool MightBeFunctionType = !Contexts[Contexts.size() - 2].IsExpression;
479 bool ProbablyFunctionType =
480 CurrentToken->isPointerOrReference() || CurrentToken->is(tok::caret);
481 bool HasMultipleLines =
false;
482 bool HasMultipleParametersOnALine =
false;
483 bool MightBeObjCForRangeLoop =
484 OpeningParen.Previous && OpeningParen.Previous->is(tok::kw_for);
486 while (CurrentToken) {
487 const auto &Prev = *CurrentToken->Previous;
488 const auto *PrevPrev = Prev.Previous;
489 if (Prev.is(TT_PointerOrReference) &&
490 PrevPrev->isOneOf(tok::l_paren, tok::coloncolon)) {
491 ProbablyFunctionType =
true;
493 if (CurrentToken->is(tok::comma))
494 MightBeFunctionType =
false;
495 if (Prev.is(TT_BinaryOperator))
496 Contexts.back().IsExpression =
true;
497 if (CurrentToken->is(tok::r_paren)) {
498 if (Prev.is(TT_PointerOrReference) &&
499 (PrevPrev == &OpeningParen || PrevPrev->is(tok::coloncolon))) {
500 MightBeFunctionType =
true;
502 if (OpeningParen.isNot(TT_CppCastLParen) && MightBeFunctionType &&
503 ProbablyFunctionType && CurrentToken->Next &&
504 (CurrentToken->Next->is(tok::l_paren) ||
505 (CurrentToken->Next->is(tok::l_square) &&
506 (Line.MustBeDeclaration ||
507 (PrevNonComment && PrevNonComment->isTypeName(LangOpts)))))) {
508 OpeningParen.setType(OpeningParen.Next->is(tok::caret)
510 : TT_FunctionTypeLParen);
512 OpeningParen.MatchingParen = CurrentToken;
513 CurrentToken->MatchingParen = &OpeningParen;
515 if (CurrentToken->Next && CurrentToken->Next->is(tok::l_brace) &&
516 OpeningParen.Previous && OpeningParen.Previous->is(tok::l_paren)) {
522 if (
Tok->is(TT_BinaryOperator) &&
Tok->isPointerOrReference())
523 Tok->setType(TT_PointerOrReference);
527 if (StartsObjCSelector) {
528 CurrentToken->setType(TT_ObjCSelector);
529 if (Contexts.back().FirstObjCSelectorName) {
530 Contexts.back().FirstObjCSelectorName->LongestObjCSelectorName =
531 Contexts.back().LongestObjCSelectorName;
535 if (OpeningParen.is(TT_AttributeLParen))
536 CurrentToken->setType(TT_AttributeRParen);
537 if (OpeningParen.is(TT_TypeDeclarationParen))
538 CurrentToken->setType(TT_TypeDeclarationParen);
539 if (OpeningParen.Previous &&
540 OpeningParen.Previous->is(TT_JavaAnnotation)) {
541 CurrentToken->setType(TT_JavaAnnotation);
543 if (OpeningParen.Previous &&
544 OpeningParen.Previous->is(TT_LeadingJavaAnnotation)) {
545 CurrentToken->setType(TT_LeadingJavaAnnotation);
548 if (!HasMultipleLines)
550 else if (HasMultipleParametersOnALine)
558 if (CurrentToken->isOneOf(tok::r_square, tok::r_brace))
561 if (CurrentToken->is(tok::l_brace) && OpeningParen.is(TT_ObjCBlockLParen))
562 OpeningParen.setType(TT_Unknown);
563 if (CurrentToken->is(tok::comma) && CurrentToken->Next &&
564 !CurrentToken->Next->HasUnescapedNewline &&
565 !CurrentToken->Next->isTrailingComment()) {
566 HasMultipleParametersOnALine =
true;
568 bool ProbablyFunctionTypeLParen =
569 (CurrentToken->is(tok::l_paren) && CurrentToken->Next &&
570 CurrentToken->Next->isOneOf(tok::star, tok::amp, tok::caret));
571 if ((Prev.isOneOf(tok::kw_const, tok::kw_auto) ||
572 Prev.isTypeName(LangOpts)) &&
573 !(CurrentToken->is(tok::l_brace) ||
574 (CurrentToken->is(tok::l_paren) && !ProbablyFunctionTypeLParen))) {
575 Contexts.back().IsExpression =
false;
577 if (CurrentToken->isOneOf(tok::semi, tok::colon)) {
578 MightBeObjCForRangeLoop =
false;
579 if (PossibleObjCForInToken) {
580 PossibleObjCForInToken->setType(TT_Unknown);
581 PossibleObjCForInToken =
nullptr;
584 if (IsIf && CurrentToken->is(tok::semi)) {
585 for (
auto *
Tok = OpeningParen.Next;
586 Tok != CurrentToken &&
587 Tok->isNoneOf(tok::equal, tok::l_paren, tok::l_brace);
589 if (
Tok->isPointerOrReference())
590 Tok->setFinalizedType(TT_PointerOrReference);
593 if (MightBeObjCForRangeLoop && CurrentToken->is(Keywords.kw_in)) {
594 PossibleObjCForInToken = CurrentToken;
595 PossibleObjCForInToken->setType(TT_ObjCForIn);
599 if (CurrentToken->is(tok::comma)) {
601 Contexts.back().InStaticAssertFirstArgument =
false;
603 Contexts.back().CanBeExpression =
true;
606 if (Style.isTableGen()) {
607 if (CurrentToken->is(tok::comma)) {
608 if (Contexts.back().IsTableGenCondOpe)
609 CurrentToken->setType(TT_TableGenCondOperatorComma);
611 }
else if (CurrentToken->is(tok::colon)) {
612 if (Contexts.back().IsTableGenCondOpe)
613 CurrentToken->setType(TT_TableGenCondOperatorColon);
617 if (!parseTableGenValue())
625 updateParameterCount(&OpeningParen,
Tok);
626 if (CurrentToken && CurrentToken->HasUnescapedNewline)
627 HasMultipleLines =
true;
633 if (!Style.isCSharp())
637 if (
Tok.Previous &&
Tok.Previous->is(tok::identifier))
641 if (
Tok.Previous &&
Tok.Previous->is(tok::r_square)) {
652 if (AttrTok->is(tok::r_square))
656 while (AttrTok && AttrTok->isNot(tok::r_square))
657 AttrTok = AttrTok->Next;
663 AttrTok = AttrTok->Next;
668 if (AttrTok->isAccessSpecifierKeyword() ||
669 AttrTok->isOneOf(tok::comment, tok::kw_class, tok::kw_static,
670 tok::l_square, Keywords.kw_internal)) {
676 AttrTok->Next->startsSequence(tok::identifier, tok::l_paren)) {
692 Left->ParentBracket = Contexts.back().ContextKind;
698 bool CppArrayTemplates =
699 IsCpp && Parent && Parent->is(TT_TemplateCloser) &&
700 (Contexts.back().CanBeExpression || Contexts.back().IsExpression ||
701 Contexts.back().ContextType == Context::TemplateArgument);
703 const bool IsInnerSquare = Contexts.back().InCpp11AttributeSpecifier;
704 const bool IsCpp11AttributeSpecifier =
705 isCppAttribute(IsCpp, *Left) || IsInnerSquare;
708 bool IsCSharpAttributeSpecifier =
709 isCSharpAttributeSpecifier(*Left) ||
710 Contexts.back().InCSharpAttributeSpecifier;
712 bool InsideInlineASM = Line.startsWith(tok::kw_asm);
713 bool IsCppStructuredBinding =
Left->isCppStructuredBinding(IsCpp);
714 bool StartsObjCMethodExpr =
715 !IsCppStructuredBinding && !InsideInlineASM && !CppArrayTemplates &&
716 IsCpp && !IsCpp11AttributeSpecifier && !IsCSharpAttributeSpecifier &&
717 Contexts.back().CanBeExpression &&
Left->isNot(TT_LambdaLSquare) &&
718 CurrentToken->isNoneOf(tok::l_brace, tok::r_square) &&
722 (!Parent || !Parent->is(tok::comma) ||
723 Contexts.back().ContextKind != tok::l_brace) &&
725 Parent->isOneOf(tok::colon, tok::l_square, tok::l_paren,
726 tok::kw_return, tok::kw_throw) ||
727 Parent->isUnaryOperator() ||
729 Parent->isOneOf(TT_ObjCForIn, TT_CastRParen) ||
732 bool ColonFound =
false;
734 unsigned BindingIncrease = 1;
735 if (IsCppStructuredBinding) {
736 Left->setType(TT_StructuredBindingLSquare);
737 }
else if (
Left->is(TT_Unknown)) {
738 if (StartsObjCMethodExpr) {
739 Left->setType(TT_ObjCMethodExpr);
740 }
else if (InsideInlineASM) {
741 Left->setType(TT_InlineASMSymbolicNameLSquare);
742 }
else if (IsCpp11AttributeSpecifier) {
743 if (!IsInnerSquare) {
744 Left->setType(TT_AttributeLSquare);
746 Left->Previous->EndsCppAttributeGroup =
false;
748 }
else if (Style.isJavaScript() && Parent &&
749 Contexts.back().ContextKind == tok::l_brace &&
750 Parent->isOneOf(tok::l_brace, tok::comma)) {
751 Left->setType(TT_JsComputedPropertyName);
752 }
else if (IsCpp && Contexts.back().ContextKind == tok::l_brace &&
753 Parent && Parent->isOneOf(tok::l_brace, tok::comma)) {
754 Left->setType(TT_DesignatedInitializerLSquare);
755 }
else if (IsCSharpAttributeSpecifier) {
756 Left->setType(TT_AttributeLSquare);
757 }
else if (CurrentToken->is(tok::r_square) && Parent &&
758 Parent->is(TT_TemplateCloser)) {
759 Left->setType(TT_ArraySubscriptLSquare);
760 }
else if (Style.isProto()) {
787 Left->setType(TT_ArrayInitializerLSquare);
788 if (!
Left->endsSequence(tok::l_square, tok::numeric_constant,
790 !
Left->endsSequence(tok::l_square, tok::numeric_constant,
792 !
Left->endsSequence(tok::l_square, tok::colon, TT_SelectorName)) {
793 Left->setType(TT_ProtoExtensionLSquare);
794 BindingIncrease = 10;
796 }
else if (!CppArrayTemplates && Parent &&
797 Parent->isOneOf(TT_BinaryOperator, TT_TemplateCloser, tok::at,
798 tok::comma, tok::l_paren, tok::l_square,
799 tok::question, tok::colon, tok::kw_return,
802 Left->setType(TT_ArrayInitializerLSquare);
804 BindingIncrease = 10;
805 Left->setType(TT_ArraySubscriptLSquare);
809 ScopedContextCreator ContextCreator(*
this, tok::l_square, BindingIncrease);
810 Contexts.back().IsExpression =
true;
811 if (Style.isJavaScript() && Parent && Parent->is(TT_JsTypeColon))
812 Contexts.back().IsExpression =
false;
814 Contexts.back().ColonIsObjCMethodExpr = StartsObjCMethodExpr;
815 Contexts.back().InCpp11AttributeSpecifier = IsCpp11AttributeSpecifier;
816 Contexts.back().InCSharpAttributeSpecifier = IsCSharpAttributeSpecifier;
818 while (CurrentToken) {
819 if (CurrentToken->is(tok::r_square)) {
820 if (IsCpp11AttributeSpecifier && !IsInnerSquare) {
821 CurrentToken->setType(TT_AttributeRSquare);
822 CurrentToken->EndsCppAttributeGroup =
true;
824 if (IsCSharpAttributeSpecifier) {
825 CurrentToken->setType(TT_AttributeRSquare);
826 }
else if (((CurrentToken->Next &&
827 CurrentToken->Next->is(tok::l_paren)) ||
828 (CurrentToken->Previous &&
829 CurrentToken->Previous->Previous == Left)) &&
830 Left->is(TT_ObjCMethodExpr)) {
835 StartsObjCMethodExpr =
false;
836 Left->setType(TT_Unknown);
838 if (StartsObjCMethodExpr && CurrentToken->Previous != Left) {
839 CurrentToken->setType(TT_ObjCMethodExpr);
842 if (!ColonFound && CurrentToken->Previous &&
843 CurrentToken->Previous->is(TT_Unknown) &&
844 canBeObjCSelectorComponent(*CurrentToken->Previous)) {
845 CurrentToken->Previous->setType(TT_SelectorName);
850 if (Parent && Parent->is(TT_PointerOrReference))
851 Parent->overwriteFixedType(TT_BinaryOperator);
853 Left->MatchingParen = CurrentToken;
854 CurrentToken->MatchingParen =
Left;
859 if (!Contexts.back().FirstObjCSelectorName) {
862 Previous->ObjCSelectorNameParts = 1;
863 Contexts.back().FirstObjCSelectorName =
Previous;
866 Left->ParameterCount =
867 Contexts.back().FirstObjCSelectorName->ObjCSelectorNameParts;
869 if (Contexts.back().FirstObjCSelectorName) {
870 Contexts.back().FirstObjCSelectorName->LongestObjCSelectorName =
871 Contexts.back().LongestObjCSelectorName;
872 if (
Left->BlockParameterCount > 1)
873 Contexts.back().FirstObjCSelectorName->LongestObjCSelectorName = 0;
875 if (Style.isTableGen() &&
Left->is(TT_TableGenListOpener))
876 CurrentToken->setType(TT_TableGenListCloser);
880 if (CurrentToken->isOneOf(tok::r_paren, tok::r_brace))
882 if (CurrentToken->is(tok::colon)) {
883 if (IsCpp11AttributeSpecifier &&
884 CurrentToken->endsSequence(tok::colon, tok::identifier,
888 CurrentToken->setType(TT_AttributeColon);
889 }
else if (!Style.isVerilog() && !Line.InPragmaDirective &&
890 Left->isOneOf(TT_ArraySubscriptLSquare,
891 TT_DesignatedInitializerLSquare)) {
892 Left->setType(TT_ObjCMethodExpr);
893 StartsObjCMethodExpr =
true;
894 Contexts.back().ColonIsObjCMethodExpr =
true;
895 if (Parent && Parent->is(tok::r_paren)) {
897 Parent->setType(TT_CastRParen);
902 if (CurrentToken->is(tok::comma) &&
Left->is(TT_ObjCMethodExpr) &&
904 Left->setType(TT_ArrayInitializerLSquare);
907 if (Style.isTableGen()) {
908 if (CurrentToken->isOneOf(tok::comma, tok::minus, tok::ellipsis)) {
914 if (!parseTableGenValue())
917 updateParameterCount(Left,
Tok);
922 updateParameterCount(Left,
Tok);
927 void skipToNextNonComment() {
929 while (CurrentToken && CurrentToken->is(tok::comment))
938 bool parseTableGenValue(
bool ParseNameMode =
false) {
941 while (CurrentToken->is(tok::comment))
943 if (!parseTableGenSimpleValue())
948 if (CurrentToken->is(tok::hash)) {
949 if (CurrentToken->Next &&
950 CurrentToken->Next->isOneOf(tok::colon, tok::semi, tok::l_brace)) {
953 CurrentToken->setType(TT_TableGenTrailingPasteOperator);
958 skipToNextNonComment();
959 HashTok->setType(TT_Unknown);
960 if (!parseTableGenValue(ParseNameMode))
967 if (ParseNameMode && CurrentToken->is(tok::l_brace))
970 if (CurrentToken->isOneOf(tok::l_brace, tok::l_square, tok::period)) {
971 CurrentToken->setType(TT_TableGenValueSuffix);
973 skipToNextNonComment();
974 if (Suffix->is(tok::l_square))
975 return parseSquare();
976 if (Suffix->is(tok::l_brace)) {
977 Scopes.push_back(getScopeType(*Suffix));
987 bool tryToParseTableGenTokVar() {
990 if (CurrentToken->is(tok::identifier) &&
991 CurrentToken->TokenText.front() ==
'$') {
992 skipToNextNonComment();
1000 bool parseTableGenDAGArg(
bool AlignColon =
false) {
1001 if (tryToParseTableGenTokVar())
1003 if (parseTableGenValue()) {
1004 if (CurrentToken && CurrentToken->is(tok::colon)) {
1006 CurrentToken->setType(TT_TableGenDAGArgListColonToAlign);
1008 CurrentToken->setType(TT_TableGenDAGArgListColon);
1009 skipToNextNonComment();
1010 return tryToParseTableGenTokVar();
1021 auto &Opes = Style.TableGenBreakingDAGArgOperators;
1026 if (
Tok.isNot(tok::identifier) ||
1027 Tok.isOneOf(TT_TableGenBangOperator, TT_TableGenCondOperator)) {
1031 if (!
Tok.Next ||
Tok.Next->is(tok::colon))
1033 return llvm::is_contained(Opes,
Tok.TokenText.str());
1038 bool parseTableGenDAGArgAndList(
FormatToken *Opener) {
1040 if (!parseTableGenDAGArg())
1042 bool BreakInside =
false;
1046 if (isTableGenDAGArgBreakingOperator(*FirstTok)) {
1049 Opener->setType(TT_TableGenDAGArgOpenerToBreak);
1050 if (FirstTok->isOneOf(TT_TableGenBangOperator,
1051 TT_TableGenCondOperator)) {
1054 CurrentToken->Previous->setType(TT_TableGenDAGArgOperatorToBreak);
1055 }
else if (FirstTok->is(tok::identifier)) {
1057 FirstTok->setType(TT_TableGenDAGArgOperatorToBreak);
1059 FirstTok->setType(TT_TableGenDAGArgOperatorID);
1064 return parseTableGenDAGArgList(Opener, BreakInside);
1069 bool parseTableGenDAGArgList(
FormatToken *Opener,
bool BreakInside) {
1070 ScopedContextCreator ContextCreator(*
this, tok::l_paren, 0);
1071 Contexts.back().IsTableGenDAGArgList =
true;
1072 bool FirstDAGArgListElm =
true;
1073 while (CurrentToken) {
1074 if (!FirstDAGArgListElm && CurrentToken->is(tok::comma)) {
1075 CurrentToken->setType(BreakInside ? TT_TableGenDAGArgListCommaToBreak
1076 : TT_TableGenDAGArgListComma);
1077 skipToNextNonComment();
1079 if (CurrentToken && CurrentToken->is(tok::r_paren)) {
1080 CurrentToken->setType(TT_TableGenDAGArgCloser);
1081 Opener->MatchingParen = CurrentToken;
1082 CurrentToken->MatchingParen = Opener;
1083 skipToNextNonComment();
1086 if (!parseTableGenDAGArg(
1088 Style.AlignConsecutiveTableGenBreakingDAGArgColons.Enabled)) {
1091 FirstDAGArgListElm =
false;
1096 bool parseTableGenSimpleValue() {
1097 assert(Style.isTableGen());
1101 skipToNextNonComment();
1103 if (
Tok->isOneOf(tok::numeric_constant, tok::string_literal,
1104 TT_TableGenMultiLineString, tok::kw_true, tok::kw_false,
1105 tok::question, tok::kw_int)) {
1109 if (
Tok->is(tok::l_brace)) {
1110 Scopes.push_back(getScopeType(*
Tok));
1111 return parseBrace();
1114 if (
Tok->is(tok::l_square)) {
1115 Tok->setType(TT_TableGenListOpener);
1118 if (
Tok->is(tok::less)) {
1119 CurrentToken->setType(TT_TemplateOpener);
1120 return parseAngle();
1126 if (
Tok->is(tok::l_paren)) {
1127 Tok->setType(TT_TableGenDAGArgOpener);
1129 if (Contexts.back().IsTableGenDAGArgList)
1130 Tok->SpacesRequiredBefore = 1;
1131 return parseTableGenDAGArgAndList(
Tok);
1134 if (
Tok->is(TT_TableGenBangOperator)) {
1135 if (CurrentToken && CurrentToken->is(tok::less)) {
1136 CurrentToken->setType(TT_TemplateOpener);
1137 skipToNextNonComment();
1141 if (!CurrentToken || CurrentToken->isNot(tok::l_paren))
1145 Contexts.back().IsTableGenBangOpe =
true;
1146 bool Result = parseParens();
1147 Contexts.back().IsTableGenBangOpe =
false;
1151 if (
Tok->is(TT_TableGenCondOperator)) {
1152 if (!CurrentToken || CurrentToken->isNot(tok::l_paren))
1155 return parseParens();
1160 if (
Tok->is(tok::identifier)) {
1162 if (CurrentToken && CurrentToken->is(tok::less)) {
1163 CurrentToken->setType(TT_TemplateOpener);
1164 skipToNextNonComment();
1165 return parseAngle();
1173 bool couldBeInStructArrayInitializer()
const {
1174 if (Contexts.size() < 2)
1178 const auto End = std::next(Contexts.rbegin(), 2);
1179 auto Last = Contexts.rbegin();
1182 if (
Last->ContextKind == tok::l_brace)
1184 return Depth == 2 &&
Last->ContextKind != tok::l_brace;
1191 assert(CurrentToken->Previous);
1192 FormatToken &OpeningBrace = *CurrentToken->Previous;
1193 assert(OpeningBrace.is(tok::l_brace));
1194 OpeningBrace.ParentBracket = Contexts.back().ContextKind;
1196 if (Contexts.back().CaretFound)
1197 OpeningBrace.overwriteFixedType(TT_ObjCBlockLBrace);
1198 Contexts.back().CaretFound =
false;
1200 ScopedContextCreator ContextCreator(*
this, tok::l_brace, 1);
1201 Contexts.back().ColonIsDictLiteral =
true;
1203 Contexts.back().IsExpression =
true;
1204 if (Style.isJavaScript() && OpeningBrace.Previous &&
1205 OpeningBrace.Previous->is(TT_JsTypeColon)) {
1206 Contexts.back().IsExpression =
false;
1208 if (Style.isVerilog() &&
1209 (!OpeningBrace.getPreviousNonComment() ||
1210 OpeningBrace.getPreviousNonComment()->isNot(Keywords.kw_apostrophe))) {
1211 Contexts.back().VerilogMayBeConcatenation =
true;
1213 if (Style.isTableGen())
1214 Contexts.back().ColonIsDictLiteral =
false;
1216 unsigned CommaCount = 0;
1217 while (CurrentToken) {
1218 assert(!Scopes.empty());
1219 if (CurrentToken->is(tok::r_brace)) {
1220 assert(Scopes.back() == getScopeType(OpeningBrace));
1222 assert(OpeningBrace.Optional == CurrentToken->Optional);
1223 OpeningBrace.MatchingParen = CurrentToken;
1224 CurrentToken->MatchingParen = &OpeningBrace;
1226 if (OpeningBrace.ParentBracket == tok::l_brace &&
1227 couldBeInStructArrayInitializer() && CommaCount > 0) {
1228 Contexts.back().ContextType = Context::StructArrayInitializer;
1234 if (CurrentToken->isOneOf(tok::r_paren, tok::r_square))
1236 updateParameterCount(&OpeningBrace, CurrentToken);
1237 if (CurrentToken->isOneOf(tok::colon, tok::l_brace, tok::less)) {
1239 if (
Previous->is(TT_JsTypeOptionalQuestion))
1241 if ((CurrentToken->is(tok::colon) && !Style.isTableGen() &&
1242 (!Contexts.back().ColonIsDictLiteral || !IsCpp)) ||
1244 OpeningBrace.setType(TT_DictLiteral);
1245 Scopes.back() = getScopeType(OpeningBrace);
1246 if (
Previous->Tok.getIdentifierInfo() ||
1247 Previous->is(tok::string_literal)) {
1248 Previous->setType(TT_SelectorName);
1251 if (CurrentToken->is(tok::colon) && OpeningBrace.is(TT_Unknown) &&
1252 !Style.isTableGen()) {
1253 OpeningBrace.setType(TT_DictLiteral);
1254 Scopes.back() = getScopeType(OpeningBrace);
1255 }
else if (Style.isJavaScript()) {
1256 OpeningBrace.overwriteFixedType(TT_DictLiteral);
1257 Scopes.back() = getScopeType(OpeningBrace);
1260 bool IsBracedListComma =
false;
1261 if (CurrentToken->is(tok::comma)) {
1262 if (Style.isJavaScript()) {
1263 OpeningBrace.overwriteFixedType(TT_DictLiteral);
1264 Scopes.back() = getScopeType(OpeningBrace);
1270 if (!consumeToken())
1272 if (IsBracedListComma)
1273 Contexts.back().IsExpression =
true;
1282 if (Current->is(tok::l_brace) && Current->is(
BK_Block))
1283 ++
Left->BlockParameterCount;
1284 if (Current->is(tok::comma)) {
1285 ++
Left->ParameterCount;
1287 Left->Role.reset(
new CommaSeparatedList(Style));
1288 Left->Role->CommaFound(Current);
1289 }
else if (
Left->ParameterCount == 0 && Current->isNot(tok::comment)) {
1290 Left->ParameterCount = 1;
1294 bool parseConditional() {
1295 while (CurrentToken) {
1296 if (CurrentToken->is(tok::colon) && CurrentToken->is(TT_Unknown)) {
1297 CurrentToken->setType(TT_ConditionalExpr);
1304 if (CurrentToken->is(tok::r_brace))
1306 if (!consumeToken())
1312 bool parseTemplateDeclaration() {
1313 if (!CurrentToken || CurrentToken->isNot(tok::less))
1316 CurrentToken->setType(TT_TemplateOpener);
1319 TemplateDeclarationDepth++;
1320 const bool WellFormed = parseAngle();
1321 TemplateDeclarationDepth--;
1325 if (CurrentToken && TemplateDeclarationDepth == 0)
1326 CurrentToken->Previous->ClosesTemplateDeclaration =
true;
1331 bool consumeToken() {
1333 const auto *Prev = CurrentToken->getPreviousNonComment();
1334 if (Prev && Prev->is(TT_AttributeRSquare) &&
1335 CurrentToken->isOneOf(tok::kw_if, tok::kw_switch, tok::kw_case,
1336 tok::kw_default, tok::kw_for, tok::kw_while) &&
1338 CurrentToken->MustBreakBefore =
true;
1345 if (
Tok->is(TT_VerilogTableItem))
1348 if (
Tok->is(TT_TableGenMultiLineString))
1350 auto *Prev =
Tok->getPreviousNonComment();
1351 auto *
Next =
Tok->getNextNonComment();
1352 switch (
bool IsIf =
false;
Tok->Tok.getKind()) {
1355 if (!Prev && Line.MustBeDeclaration)
1356 Tok->setType(TT_ObjCMethodSpecifier);
1363 if (
Tok->isTypeFinalized())
1366 if (Style.isJavaScript()) {
1367 if (Contexts.back().ColonIsForRangeExpr ||
1368 (Contexts.size() == 1 &&
1369 Line.First->isNoneOf(tok::kw_enum, tok::kw_case)) ||
1370 Contexts.back().ContextKind == tok::l_paren ||
1371 Contexts.back().ContextKind == tok::l_square ||
1372 (!Contexts.back().IsExpression &&
1373 Contexts.back().ContextKind == tok::l_brace) ||
1374 (Contexts.size() == 1 &&
1375 Line.MustBeDeclaration)) {
1376 Contexts.back().IsExpression =
false;
1377 Tok->setType(TT_JsTypeColon);
1380 }
else if (Style.isCSharp()) {
1381 if (Contexts.back().InCSharpAttributeSpecifier) {
1382 Tok->setType(TT_AttributeColon);
1385 if (Contexts.back().ContextKind == tok::l_paren) {
1386 Tok->setType(TT_CSharpNamedArgumentColon);
1389 }
else if (Style.isVerilog() &&
Tok->isNot(TT_BinaryOperator)) {
1392 if (Keywords.isVerilogEnd(*Prev) || Keywords.isVerilogBegin(*Prev)) {
1393 Tok->setType(TT_VerilogBlockLabelColon);
1394 }
else if (Contexts.back().ContextKind == tok::l_square) {
1395 Tok->setType(TT_BitFieldColon);
1396 }
else if (Contexts.back().ColonIsDictLiteral) {
1397 Tok->setType(TT_DictLiteral);
1398 }
else if (Contexts.size() == 1) {
1402 Tok->setType(TT_CaseLabelColon);
1403 if (Line.Level > 1 || (!Line.InPPDirective && Line.Level > 0))
1408 if (Line.First->is(tok::kw_asm)) {
1409 Tok->setType(TT_InlineASMColon);
1410 }
else if (Contexts.back().ColonIsDictLiteral || Style.isProto()) {
1411 Tok->setType(TT_DictLiteral);
1412 if (Style.isTextProto())
1413 Prev->setType(TT_SelectorName);
1414 }
else if (Contexts.back().ColonIsObjCMethodExpr ||
1415 Line.startsWith(TT_ObjCMethodSpecifier)) {
1416 Tok->setType(TT_ObjCMethodExpr);
1417 const auto *PrevPrev = Prev->Previous;
1420 bool UnknownIdentifierInMethodDeclaration =
1421 Line.startsWith(TT_ObjCMethodSpecifier) &&
1422 Prev->is(tok::identifier) && Prev->is(TT_Unknown);
1425 !(PrevPrev->is(TT_CastRParen) ||
1426 (PrevPrev->is(TT_ObjCMethodExpr) && PrevPrev->is(tok::colon))) ||
1427 PrevPrev->is(tok::r_square) ||
1428 Contexts.back().LongestObjCSelectorName == 0 ||
1429 UnknownIdentifierInMethodDeclaration) {
1430 Prev->setType(TT_SelectorName);
1431 if (!Contexts.back().FirstObjCSelectorName)
1432 Contexts.back().FirstObjCSelectorName = Prev;
1433 else if (Prev->ColumnWidth > Contexts.back().LongestObjCSelectorName)
1434 Contexts.back().LongestObjCSelectorName = Prev->ColumnWidth;
1435 Prev->ParameterIndex =
1436 Contexts.back().FirstObjCSelectorName->ObjCSelectorNameParts;
1437 ++Contexts.back().FirstObjCSelectorName->ObjCSelectorNameParts;
1439 }
else if (Contexts.back().ColonIsForRangeExpr) {
1440 Tok->setType(TT_RangeBasedForLoopColon);
1441 for (
auto *Token = Prev;
1442 Token && Token->isNoneOf(tok::semi, tok::l_paren);
1443 Token = Token->Previous) {
1444 if (Token->isPointerOrReference())
1445 Token->setFinalizedType(TT_PointerOrReference);
1447 }
else if (Contexts.back().ContextType == Context::C11GenericSelection) {
1448 Tok->setType(TT_GenericSelectionColon);
1449 if (Prev->isPointerOrReference())
1450 Prev->setFinalizedType(TT_PointerOrReference);
1451 }
else if ((CurrentToken && CurrentToken->is(tok::numeric_constant)) ||
1452 (Prev->is(TT_StartOfName) && !Scopes.empty() &&
1454 Tok->setType(TT_BitFieldColon);
1455 }
else if (Contexts.size() == 1 &&
1456 Line.getFirstNonComment()->isNoneOf(tok::kw_enum, tok::kw_case,
1458 !Line.startsWith(tok::kw_typedef, tok::kw_enum)) {
1459 if (Prev->isOneOf(tok::r_paren, tok::kw_noexcept) ||
1460 Prev->ClosesRequiresClause) {
1461 Tok->setType(TT_CtorInitializerColon);
1462 }
else if (Prev->is(tok::kw_try)) {
1464 FormatToken *PrevPrev = Prev->getPreviousNonComment();
1467 if (PrevPrev && PrevPrev->isOneOf(tok::r_paren, tok::kw_noexcept))
1468 Tok->setType(TT_CtorInitializerColon);
1470 Tok->setType(TT_InheritanceColon);
1471 if (Prev->isAccessSpecifierKeyword())
1474 }
else if (canBeObjCSelectorComponent(*Prev) &&
Next &&
1475 (
Next->isOneOf(tok::r_paren, tok::comma) ||
1476 (canBeObjCSelectorComponent(*
Next) &&
Next->Next &&
1477 Next->Next->is(tok::colon)))) {
1480 Tok->setType(TT_ObjCSelector);
1487 if (Style.isJavaScript() && !Contexts.back().IsExpression)
1488 Tok->setType(TT_JsTypeOperator);
1491 if (Style.isTableGen()) {
1493 if (!parseTableGenValue())
1495 if (CurrentToken && CurrentToken->is(Keywords.kw_then))
1500 CurrentToken->isOneOf(tok::kw_constexpr, tok::identifier)) {
1506 if (CurrentToken && CurrentToken->is(tok::l_paren)) {
1508 if (!parseParens(IsIf))
1513 if (Style.isJavaScript()) {
1515 if ((Prev && Prev->is(tok::period)) || (
Next &&
Next->is(tok::colon)))
1518 if (CurrentToken && CurrentToken->is(Keywords.kw_await))
1521 if (IsCpp && CurrentToken && CurrentToken->is(tok::kw_co_await))
1523 Contexts.back().ColonIsForRangeExpr =
true;
1524 if (!CurrentToken || CurrentToken->isNot(tok::l_paren))
1535 if (Prev && Prev->is(tok::r_paren) && Prev->MatchingParen &&
1536 Prev->MatchingParen->is(TT_OverloadedOperatorLParen)) {
1537 Prev->setType(TT_OverloadedOperator);
1538 Prev->MatchingParen->setType(TT_OverloadedOperator);
1539 Tok->setType(TT_OverloadedOperatorLParen);
1542 if (Style.isVerilog()) {
1548 auto IsInstancePort = [&]() {
1557 if (!Prev || !(PrevPrev = Prev->getPreviousNonComment()))
1560 if (Keywords.isVerilogIdentifier(*Prev) &&
1561 Keywords.isVerilogIdentifier(*PrevPrev)) {
1565 if (Prev->is(Keywords.kw_verilogHash) &&
1566 Keywords.isVerilogIdentifier(*PrevPrev)) {
1570 if (Keywords.isVerilogIdentifier(*Prev) && PrevPrev->is(tok::r_paren))
1573 if (Keywords.isVerilogIdentifier(*Prev) && PrevPrev->is(tok::comma)) {
1574 const FormatToken *PrevParen = PrevPrev->getPreviousNonComment();
1575 if (PrevParen && PrevParen->is(tok::r_paren) &&
1576 PrevParen->MatchingParen &&
1577 PrevParen->MatchingParen->is(TT_VerilogInstancePortLParen)) {
1584 if (IsInstancePort())
1585 Tok->setType(TT_VerilogInstancePortLParen);
1590 if (Line.MustBeDeclaration && Contexts.size() == 1 &&
1591 !Contexts.back().IsExpression && !Line.startsWith(TT_ObjCProperty) &&
1592 !Line.startsWith(tok::l_paren) &&
1593 Tok->isNoneOf(TT_TypeDeclarationParen, TT_RequiresExpressionLParen)) {
1595 (!Prev->isAttribute() &&
1596 Prev->isNoneOf(TT_RequiresClause, TT_LeadingJavaAnnotation,
1597 TT_BinaryOperator))) {
1598 Line.MightBeFunctionDecl =
true;
1599 Tok->MightBeFunctionDeclParen =
true;
1604 if (Style.isTableGen())
1605 Tok->setType(TT_TableGenListOpener);
1611 if (
Tok->is(TT_RequiresExpressionLBrace))
1613 }
else if (Style.isTextProto()) {
1614 if (Prev && Prev->isNot(TT_DictLiteral))
1615 Prev->setType(TT_SelectorName);
1617 Scopes.push_back(getScopeType(*
Tok));
1623 Tok->setType(TT_TemplateOpener);
1629 if (Style.isTextProto() ||
1631 Prev->isOneOf(TT_SelectorName, TT_DictLiteral))) {
1632 Tok->setType(TT_DictLiteral);
1633 if (Prev && Prev->isNot(TT_DictLiteral))
1634 Prev->setType(TT_SelectorName);
1636 if (Style.isTableGen())
1637 Tok->setType(TT_TemplateOpener);
1639 Tok->setType(TT_BinaryOperator);
1640 NonTemplateLess.insert(
Tok);
1650 if (!Scopes.empty())
1657 if (!Style.isTextProto() &&
Tok->is(TT_Unknown))
1658 Tok->setType(TT_BinaryOperator);
1659 if (Prev && Prev->is(TT_TemplateCloser))
1660 Tok->SpacesRequiredBefore = 1;
1662 case tok::kw_operator:
1663 if (Style.isProto())
1666 if (IsCpp && CurrentToken) {
1667 const auto *Info = CurrentToken->Tok.getIdentifierInfo();
1669 if (Info && !(CurrentToken->isPlacementOperator() ||
1670 CurrentToken->is(tok::kw_co_await) ||
1671 Info->isCPlusPlusOperatorKeyword())) {
1673 if (CurrentToken->startsSequence(tok::kw_decltype, tok::l_paren,
1674 tok::kw_auto, tok::r_paren)) {
1676 LParen = CurrentToken->Next->Next->Next->Next;
1679 for (LParen = CurrentToken->Next;
1680 LParen && LParen->isNot(tok::l_paren); LParen = LParen->Next) {
1681 if (LParen->isPointerOrReference())
1682 LParen->setFinalizedType(TT_PointerOrReference);
1685 if (LParen && LParen->is(tok::l_paren)) {
1686 if (!Contexts.back().IsExpression) {
1687 Tok->setFinalizedType(TT_FunctionDeclarationName);
1688 LParen->setFinalizedType(TT_FunctionDeclarationLParen);
1694 while (CurrentToken &&
1695 CurrentToken->isNoneOf(tok::l_paren, tok::semi, tok::r_paren,
1697 if (CurrentToken->isOneOf(tok::star, tok::amp))
1698 CurrentToken->setType(TT_PointerOrReference);
1699 auto Next = CurrentToken->getNextNonComment();
1702 if (
Next->is(tok::less))
1708 auto Previous = CurrentToken->getPreviousNonComment();
1710 if (CurrentToken->is(tok::comma) &&
Previous->isNot(tok::kw_operator))
1712 if (
Previous->isOneOf(TT_BinaryOperator, TT_UnaryOperator, tok::comma,
1715 Previous->isPointerOrReference()) ||
1717 Previous->TokenText.starts_with(
"\"\"")) {
1718 Previous->setType(TT_OverloadedOperator);
1719 if (CurrentToken->isOneOf(tok::less, tok::greater))
1723 if (CurrentToken && CurrentToken->is(tok::l_paren))
1724 CurrentToken->setType(TT_OverloadedOperatorLParen);
1725 if (CurrentToken && CurrentToken->Previous->is(TT_BinaryOperator))
1726 CurrentToken->Previous->setType(TT_OverloadedOperator);
1729 if (Style.isJavaScript() &&
Next &&
1730 Next->isOneOf(tok::semi, tok::comma, tok::colon, tok::r_paren,
1731 tok::r_brace, tok::r_square)) {
1736 Tok->setType(TT_JsTypeOptionalQuestion);
1741 if (Line.MustBeDeclaration && !Contexts.back().IsExpression &&
1742 Style.isJavaScript()) {
1745 if (Style.isCSharp()) {
1748 if (
Next && (
Next->isOneOf(tok::r_paren, tok::greater) ||
1749 Next->startsSequence(tok::identifier, tok::semi) ||
1750 Next->startsSequence(tok::identifier, tok::equal))) {
1751 Tok->setType(TT_CSharpNullable);
1760 if (!Contexts.back().IsExpression && Line.MustBeDeclaration &&
1761 (!
Next ||
Next->isNoneOf(tok::identifier, tok::string_literal) ||
1762 !
Next->Next ||
Next->Next->isNoneOf(tok::colon, tok::question))) {
1763 Tok->setType(TT_CSharpNullable);
1769 case tok::kw_template:
1770 parseTemplateDeclaration();
1773 switch (Contexts.back().ContextType) {
1774 case Context::CtorInitializer:
1775 Tok->setType(TT_CtorInitializerComma);
1777 case Context::InheritanceList:
1778 Tok->setType(TT_InheritanceComma);
1780 case Context::VerilogInstancePortList:
1781 Tok->setType(TT_VerilogInstancePortComma);
1784 if (Style.isVerilog() && Contexts.size() == 1 &&
1785 Line.startsWith(Keywords.kw_assign)) {
1786 Tok->setFinalizedType(TT_VerilogAssignComma);
1787 }
else if (Contexts.back().FirstStartOfName &&
1788 (Contexts.size() == 1 || startsWithInitStatement(Line))) {
1789 Contexts.back().FirstStartOfName->PartOfMultiVariableDeclStmt =
true;
1790 Line.IsMultiVariableDeclStmt =
true;
1794 if (Contexts.back().ContextType == Context::ForEachMacro)
1795 Contexts.back().IsExpression =
true;
1797 case tok::kw_default:
1799 if (Style.isVerilog() && Keywords.isVerilogEndOfLabel(*
Tok) &&
1800 (Line.Level > 1 || (!Line.InPPDirective && Line.Level > 0))) {
1804 case tok::identifier:
1805 if (
Tok->isOneOf(Keywords.kw___has_include,
1806 Keywords.kw___has_include_next)) {
1810 if (
Next &&
Next->is(tok::l_paren) && Prev &&
1811 Prev->isOneOf(tok::kw___cdecl, tok::kw___stdcall,
1812 tok::kw___fastcall, tok::kw___thiscall,
1813 tok::kw___regcall, tok::kw___vectorcall)) {
1814 Tok->setFinalizedType(TT_FunctionDeclarationName);
1815 Next->setFinalizedType(TT_FunctionDeclarationLParen);
1817 }
else if (Style.isCSharp()) {
1818 if (
Tok->is(Keywords.kw_where) &&
Next &&
Next->isNot(tok::l_paren)) {
1819 Tok->setType(TT_CSharpGenericTypeConstraint);
1820 parseCSharpGenericTypeConstraint();
1822 Line.IsContinuation =
true;
1824 }
else if (Style.isTableGen()) {
1825 if (
Tok->is(Keywords.kw_assert)) {
1826 if (!parseTableGenValue())
1828 }
else if (
Tok->isOneOf(Keywords.kw_def, Keywords.kw_defm) &&
1829 (!
Next ||
Next->isNoneOf(tok::colon, tok::l_brace))) {
1831 if (!parseTableGenValue(
true))
1835 if (Style.AllowBreakBeforeQtProperty &&
1836 Contexts.back().ContextType == Context::QtProperty &&
1837 Tok->isQtProperty()) {
1838 Tok->setFinalizedType(TT_QtProperty);
1842 if (
Tok->isNot(TT_LambdaArrow) && Prev && Prev->is(tok::kw_noexcept))
1843 Tok->setType(TT_TrailingReturnArrow);
1847 if (Style.isTableGen() && !parseTableGenValue())
1849 if (!Scopes.empty() && Scopes.back() ==
ST_Enum)
1850 Tok->setFinalizedType(TT_EnumEqual);
1858 void parseCSharpGenericTypeConstraint() {
1859 int OpenAngleBracketsCount = 0;
1860 while (CurrentToken) {
1861 if (CurrentToken->is(tok::less)) {
1863 CurrentToken->setType(TT_TemplateOpener);
1864 ++OpenAngleBracketsCount;
1866 }
else if (CurrentToken->is(tok::greater)) {
1867 CurrentToken->setType(TT_TemplateCloser);
1868 --OpenAngleBracketsCount;
1870 }
else if (CurrentToken->is(tok::comma) && OpenAngleBracketsCount == 0) {
1873 CurrentToken->setType(TT_CSharpGenericTypeConstraintComma);
1875 }
else if (CurrentToken->is(Keywords.kw_where)) {
1876 CurrentToken->setType(TT_CSharpGenericTypeConstraint);
1878 }
else if (CurrentToken->is(tok::colon)) {
1879 CurrentToken->setType(TT_CSharpGenericTypeConstraintColon);
1887 void parseIncludeDirective() {
1888 if (CurrentToken && CurrentToken->is(tok::less)) {
1890 while (CurrentToken) {
1893 if (CurrentToken->isNot(tok::comment) &&
1894 !CurrentToken->TokenText.starts_with(
"//")) {
1895 CurrentToken->setType(TT_ImplicitStringLiteral);
1902 void parseWarningOrError() {
1907 while (CurrentToken) {
1908 CurrentToken->setType(TT_ImplicitStringLiteral);
1913 void parsePragma() {
1916 CurrentToken->isOneOf(Keywords.kw_mark, Keywords.kw_option,
1917 Keywords.kw_region)) {
1918 bool IsMarkOrRegion =
1919 CurrentToken->isOneOf(Keywords.kw_mark, Keywords.kw_region);
1922 while (CurrentToken) {
1923 if (IsMarkOrRegion || CurrentToken->Previous->is(TT_BinaryOperator))
1924 CurrentToken->setType(TT_ImplicitStringLiteral);
1930 void parseHasInclude() {
1931 if (!CurrentToken || CurrentToken->isNot(tok::l_paren))
1934 parseIncludeDirective();
1938 LineType parsePreprocessorDirective() {
1939 bool IsFirstToken = CurrentToken->IsFirst;
1945 if (Style.isJavaScript() && IsFirstToken) {
1949 while (CurrentToken) {
1951 CurrentToken->setType(TT_ImplicitStringLiteral);
1957 if (CurrentToken->is(tok::numeric_constant)) {
1958 CurrentToken->SpacesRequiredBefore = 1;
1963 if (!CurrentToken->Tok.getIdentifierInfo())
1967 if (Style.isVerilog() && !Keywords.isVerilogPPDirective(*CurrentToken))
1969 switch (CurrentToken->Tok.getIdentifierInfo()->getPPKeywordID()) {
1970 case tok::pp_include:
1971 case tok::pp_include_next:
1972 case tok::pp_import:
1974 parseIncludeDirective();
1978 case tok::pp_warning:
1979 parseWarningOrError();
1981 case tok::pp_pragma:
1986 Contexts.back().IsExpression =
true;
1989 CurrentToken->SpacesRequiredBefore = 1;
1995 while (CurrentToken) {
1998 if (
Tok->is(tok::l_paren)) {
2000 }
else if (
Tok->isOneOf(Keywords.kw___has_include,
2001 Keywords.kw___has_include_next)) {
2012 NonTemplateLess.clear();
2013 if (!Line.InMacroBody && CurrentToken->is(tok::hash)) {
2017 auto Type = parsePreprocessorDirective();
2025 IdentifierInfo *Info = CurrentToken->Tok.getIdentifierInfo();
2026 if ((Style.isJava() && CurrentToken->is(Keywords.kw_package)) ||
2027 (!Style.isVerilog() && Info &&
2028 Info->getPPKeywordID() == tok::pp_import && CurrentToken->Next &&
2029 CurrentToken->Next->isOneOf(tok::string_literal, tok::identifier,
2032 parseIncludeDirective();
2038 if (CurrentToken->is(tok::less) && Line.Last->is(tok::greater)) {
2039 parseIncludeDirective();
2046 CurrentToken->isOneOf(Keywords.kw_option, Keywords.kw_package)) {
2048 if (CurrentToken && CurrentToken->is(tok::identifier)) {
2049 while (CurrentToken)
2055 bool KeywordVirtualFound =
false;
2056 bool ImportStatement =
false;
2059 if (Style.isJavaScript() && CurrentToken->is(Keywords.kw_import))
2060 ImportStatement =
true;
2062 while (CurrentToken) {
2063 if (CurrentToken->is(tok::kw_virtual))
2064 KeywordVirtualFound =
true;
2065 if (Style.isJavaScript()) {
2072 if (Line.First->is(tok::kw_export) &&
2073 CurrentToken->is(Keywords.kw_from) && CurrentToken->Next &&
2074 CurrentToken->Next->isStringLiteral()) {
2075 ImportStatement =
true;
2077 if (isClosureImportStatement(*CurrentToken))
2078 ImportStatement =
true;
2080 if (!consumeToken())
2088 if (KeywordVirtualFound)
2090 if (ImportStatement)
2093 if (Line.startsWith(TT_ObjCMethodSpecifier)) {
2094 if (Contexts.back().FirstObjCSelectorName) {
2095 Contexts.back().FirstObjCSelectorName->LongestObjCSelectorName =
2096 Contexts.back().LongestObjCSelectorName;
2101 for (
const auto &ctx : Contexts)
2102 if (ctx.ContextType == Context::StructArrayInitializer)
2112 return Tok.TokenText ==
"goog" &&
Tok.Next &&
Tok.Next->is(tok::period) &&
2114 (
Tok.Next->Next->TokenText ==
"module" ||
2115 Tok.Next->Next->TokenText ==
"provide" ||
2116 Tok.Next->Next->TokenText ==
"require" ||
2117 Tok.Next->Next->TokenText ==
"requireType" ||
2118 Tok.Next->Next->TokenText ==
"forwardDeclare") &&
2119 Tok.Next->Next->Next &&
Tok.Next->Next->Next->is(tok::l_paren);
2122 void resetTokenMetadata() {
2128 if (!CurrentToken->isTypeFinalized() &&
2129 CurrentToken->isNoneOf(
2130 TT_LambdaLSquare, TT_LambdaLBrace, TT_AttributeMacro, TT_IfMacro,
2131 TT_ForEachMacro, TT_TypenameMacro, TT_FunctionLBrace,
2132 TT_ImplicitStringLiteral, TT_InlineASMBrace, TT_FatArrow,
2133 TT_LambdaArrow, TT_NamespaceMacro, TT_OverloadedOperator,
2134 TT_RegexLiteral, TT_TemplateString, TT_ObjCStringLiteral,
2135 TT_UntouchableMacroFunc, TT_StatementAttributeLikeMacro,
2136 TT_FunctionLikeOrFreestandingMacro, TT_ClassLBrace, TT_EnumLBrace,
2137 TT_RecordLBrace, TT_StructLBrace, TT_UnionLBrace, TT_RequiresClause,
2138 TT_RequiresClauseInARequiresExpression, TT_RequiresExpression,
2139 TT_RequiresExpressionLParen, TT_RequiresExpressionLBrace,
2140 TT_CompoundRequirementLBrace, TT_BracedListLBrace,
2141 TT_FunctionLikeMacro)) {
2142 CurrentToken->setType(TT_Unknown);
2144 CurrentToken->Role.reset();
2145 CurrentToken->MatchingParen =
nullptr;
2146 CurrentToken->FakeLParens.clear();
2147 CurrentToken->FakeRParens = 0;
2154 CurrentToken->NestingLevel = Contexts.size() - 1;
2155 CurrentToken->BindingStrength = Contexts.back().BindingStrength;
2156 modifyContext(*CurrentToken);
2157 determineTokenType(*CurrentToken);
2158 CurrentToken = CurrentToken->Next;
2160 resetTokenMetadata();
2168 : ContextKind(ContextKind), BindingStrength(BindingStrength),
2169 IsExpression(IsExpression) {}
2172 unsigned BindingStrength;
2174 unsigned LongestObjCSelectorName = 0;
2175 bool ColonIsForRangeExpr =
false;
2176 bool ColonIsDictLiteral =
false;
2177 bool ColonIsObjCMethodExpr =
false;
2180 bool CanBeExpression =
true;
2181 bool CaretFound =
false;
2182 bool InCpp11AttributeSpecifier =
false;
2183 bool InCSharpAttributeSpecifier =
false;
2184 bool InStaticAssertFirstArgument =
false;
2185 bool VerilogAssignmentFound =
false;
2188 bool VerilogMayBeConcatenation =
false;
2189 bool IsTableGenDAGArgList =
false;
2190 bool IsTableGenBangOpe =
false;
2191 bool IsTableGenCondOpe =
false;
2204 StructArrayInitializer,
2208 C11GenericSelection,
2211 VerilogInstancePortList,
2212 } ContextType = Unknown;
2217 struct ScopedContextCreator {
2218 AnnotatingParser &P;
2220 ScopedContextCreator(AnnotatingParser &P,
tok::TokenKind ContextKind,
2223 P.Contexts.push_back(Context(ContextKind,
2224 P.Contexts.back().BindingStrength + Increase,
2225 P.Contexts.back().IsExpression));
2228 ~ScopedContextCreator() {
2230 if (P.Contexts.back().ContextType == Context::StructArrayInitializer) {
2231 P.Contexts.pop_back();
2232 P.Contexts.back().ContextType = Context::StructArrayInitializer;
2236 P.Contexts.pop_back();
2241 auto AssignmentStartsExpression = [&]() {
2245 if (Line.First->isOneOf(tok::kw_using, tok::kw_return))
2247 if (Line.First->is(tok::kw_template)) {
2248 assert(Current.Previous);
2249 if (Current.Previous->is(tok::kw_operator)) {
2257 if (
Tok->isNot(TT_TemplateOpener)) {
2264 if (Contexts.back().ContextKind == tok::less) {
2265 assert(Current.Previous->Previous);
2266 return Current.Previous->Previous->isNoneOf(tok::kw_typename,
2270 Tok =
Tok->MatchingParen;
2273 Tok =
Tok->getNextNonComment();
2277 if (
Tok->isOneOf(tok::kw_class, tok::kw_enum, tok::kw_struct,
2287 if (Style.isJavaScript() &&
2288 (Line.startsWith(Keywords.kw_type, tok::identifier) ||
2289 Line.startsWith(tok::kw_export, Keywords.kw_type,
2290 tok::identifier))) {
2294 return !Current.Previous || Current.Previous->isNot(tok::kw_operator);
2297 if (AssignmentStartsExpression()) {
2298 Contexts.back().IsExpression =
true;
2299 if (!Line.startsWith(TT_UnaryOperator)) {
2302 Previous->Previous->isNoneOf(tok::comma, tok::semi);
2304 if (
Previous->isOneOf(tok::r_square, tok::r_paren, tok::greater)) {
2311 if (!
Previous->isTypeFinalized() &&
2312 Previous->isOneOf(TT_BinaryOperator, TT_UnaryOperator) &&
2314 Previous->Previous->isNot(tok::equal)) {
2315 Previous->setType(TT_PointerOrReference);
2319 }
else if (Current.is(tok::lessless) &&
2320 (!Current.Previous ||
2321 Current.Previous->isNot(tok::kw_operator))) {
2322 Contexts.back().IsExpression =
true;
2323 }
else if (Current.isOneOf(tok::kw_return, tok::kw_throw)) {
2324 Contexts.back().IsExpression =
true;
2325 }
else if (Current.is(TT_TrailingReturnArrow)) {
2326 Contexts.back().IsExpression =
false;
2327 }
else if (Current.isOneOf(TT_LambdaArrow, Keywords.kw_assert)) {
2328 Contexts.back().IsExpression = Style.isJava();
2329 }
else if (Current.Previous &&
2330 Current.Previous->is(TT_CtorInitializerColon)) {
2331 Contexts.back().IsExpression =
true;
2332 Contexts.back().ContextType = Context::CtorInitializer;
2333 }
else if (Current.Previous && Current.Previous->is(TT_InheritanceColon)) {
2334 Contexts.back().ContextType = Context::InheritanceList;
2335 }
else if (Current.isOneOf(tok::r_paren, tok::greater, tok::comma)) {
2339 Previous->setType(TT_PointerOrReference);
2341 if (Line.MustBeDeclaration &&
2342 Contexts.front().ContextType != Context::CtorInitializer) {
2343 Contexts.back().IsExpression =
false;
2345 }
else if (Current.is(tok::kw_new)) {
2346 Contexts.back().CanBeExpression =
false;
2347 }
else if (Current.is(tok::semi) ||
2348 (Current.is(tok::exclaim) && Current.Previous &&
2349 Current.Previous->isNot(tok::kw_operator))) {
2353 Contexts.back().IsExpression =
true;
2361 if (Current->is(tok::l_paren))
2363 if (Current->is(tok::r_paren))
2367 Current = Current->Next;
2372 static bool isDeductionGuide(
FormatToken &Current) {
2374 if (Current.Previous && Current.Previous->is(tok::r_paren) &&
2375 Current.startsSequence(tok::arrow, tok::identifier, tok::less)) {
2379 while (TemplateCloser) {
2381 if (TemplateCloser->is(tok::l_paren)) {
2383 TemplateCloser = untilMatchingParen(TemplateCloser);
2384 if (!TemplateCloser)
2387 if (TemplateCloser->is(tok::less))
2389 if (TemplateCloser->is(tok::greater))
2393 TemplateCloser = TemplateCloser->Next;
2397 if (TemplateCloser && TemplateCloser->Next &&
2398 TemplateCloser->Next->is(tok::semi) &&
2399 Current.Previous->MatchingParen) {
2403 Current.Previous->MatchingParen->Previous;
2405 return LeadingIdentifier &&
2406 LeadingIdentifier->TokenText == Current.Next->TokenText;
2413 if (Current.isNot(TT_Unknown)) {
2418 if ((Style.isJavaScript() || Style.isCSharp()) &&
2419 Current.is(tok::exclaim)) {
2420 if (Current.Previous) {
2422 Style.isJavaScript()
2423 ? Keywords.isJavaScriptIdentifier(
2424 *Current.Previous,
true)
2425 : Current.Previous->is(tok::identifier);
2427 Current.Previous->isOneOf(
2428 tok::kw_default, tok::kw_namespace, tok::r_paren, tok::r_square,
2429 tok::r_brace, tok::kw_false, tok::kw_true, Keywords.kw_type,
2430 Keywords.kw_get, Keywords.kw_init, Keywords.kw_set) ||
2431 Current.Previous->Tok.isLiteral()) {
2432 Current.setType(TT_NonNullAssertion);
2437 Current.Next->isOneOf(TT_BinaryOperator, Keywords.kw_as)) {
2438 Current.setType(TT_NonNullAssertion);
2446 if ((Style.isJavaScript() || Style.isJava()) &&
2447 Current.is(Keywords.kw_instanceof)) {
2448 Current.setType(TT_BinaryOperator);
2449 }
else if (isStartOfName(Current) &&
2450 (!Line.MightBeFunctionDecl || Current.NestingLevel != 0)) {
2451 Contexts.back().FirstStartOfName = &Current;
2452 Current.setType(TT_StartOfName);
2453 }
else if (Current.is(tok::semi)) {
2457 Contexts.back().FirstStartOfName =
nullptr;
2458 }
else if (Current.isOneOf(tok::kw_auto, tok::kw___auto_type)) {
2460 }
else if (Current.is(tok::arrow) && Style.isJava()) {
2461 Current.setType(TT_LambdaArrow);
2462 }
else if (Current.is(tok::arrow) && Style.isVerilog()) {
2464 Current.setType(TT_BinaryOperator);
2465 }
else if (Current.is(tok::arrow) && AutoFound &&
2466 Line.MightBeFunctionDecl && Current.NestingLevel == 0 &&
2467 Current.Previous->isNoneOf(tok::kw_operator, tok::identifier)) {
2469 Current.setType(TT_TrailingReturnArrow);
2470 }
else if (Current.is(tok::arrow) && Current.Previous &&
2471 Current.Previous->is(tok::r_brace) &&
2475 Current.setType(TT_TrailingReturnArrow);
2476 }
else if (isDeductionGuide(Current)) {
2478 Current.setType(TT_TrailingReturnArrow);
2479 }
else if (Current.isPointerOrReference()) {
2480 Current.setType(determineStarAmpUsage(
2482 (Contexts.back().CanBeExpression && Contexts.back().IsExpression) ||
2483 Contexts.back().InStaticAssertFirstArgument,
2484 Contexts.back().ContextType == Context::TemplateArgument));
2485 }
else if (Current.isOneOf(tok::minus, tok::plus, tok::caret) ||
2486 (Style.isVerilog() && Current.is(tok::pipe))) {
2487 Current.setType(determinePlusMinusCaretUsage(Current));
2488 if (Current.is(TT_UnaryOperator) && Current.is(tok::caret))
2489 Contexts.back().CaretFound =
true;
2490 }
else if (Current.isOneOf(tok::minusminus, tok::plusplus)) {
2491 Current.setType(determineIncrementUsage(Current));
2492 }
else if (Current.isOneOf(tok::exclaim, tok::tilde)) {
2493 Current.setType(TT_UnaryOperator);
2494 }
else if (Current.is(tok::question)) {
2495 if (Style.isJavaScript() && Line.MustBeDeclaration &&
2496 !Contexts.back().IsExpression) {
2499 Current.setType(TT_JsTypeOptionalQuestion);
2500 }
else if (Style.isTableGen()) {
2502 Current.setType(TT_Unknown);
2504 Current.setType(TT_ConditionalExpr);
2506 Contexts.back().IsExpression =
true;
2508 }
else if (Current.isBinaryOperator() &&
2509 (!Current.Previous || Current.Previous->isNot(tok::l_square)) &&
2510 (Current.isNot(tok::greater) && !Style.isTextProto())) {
2511 if (Style.isVerilog()) {
2512 if (Current.is(tok::lessequal) && Contexts.size() == 1 &&
2513 !Contexts.back().VerilogAssignmentFound) {
2517 Current.setFinalizedType(TT_BinaryOperator);
2520 Contexts.back().VerilogAssignmentFound =
true;
2522 Current.setType(TT_BinaryOperator);
2523 }
else if (Current.is(tok::comment)) {
2524 if (Current.TokenText.starts_with(
"/*")) {
2525 if (Current.TokenText.ends_with(
"*/")) {
2526 Current.setType(TT_BlockComment);
2530 Current.Tok.setKind(tok::unknown);
2533 Current.setType(TT_LineComment);
2535 }
else if (Current.is(tok::string_literal)) {
2536 if (Style.isVerilog() && Contexts.back().VerilogMayBeConcatenation &&
2537 Current.getPreviousNonComment() &&
2538 Current.getPreviousNonComment()->isOneOf(tok::comma, tok::l_brace) &&
2539 Current.getNextNonComment() &&
2540 Current.getNextNonComment()->isOneOf(tok::comma, tok::r_brace)) {
2541 Current.setType(TT_StringInConcatenation);
2543 }
else if (Current.is(tok::l_paren)) {
2544 if (lParenStartsCppCast(Current))
2545 Current.setType(TT_CppCastLParen);
2546 }
else if (Current.is(tok::r_paren)) {
2547 if (rParenEndsCast(Current))
2548 Current.setType(TT_CastRParen);
2549 if (Current.MatchingParen && Current.MatchingParen->is(TT_InlineASMParen))
2550 Current.setType(TT_InlineASMParen);
2551 if (Current.MatchingParen && Current.Next &&
2552 !Current.Next->isBinaryOperator() &&
2553 Current.Next->isNoneOf(
2554 tok::semi, tok::colon, tok::l_brace, tok::l_paren, tok::comma,
2555 tok::period, tok::arrow, tok::coloncolon, tok::kw_noexcept)) {
2556 if (
FormatToken *AfterParen = Current.MatchingParen->Next;
2557 AfterParen && AfterParen->isNot(tok::caret)) {
2559 if (
FormatToken *BeforeParen = Current.MatchingParen->Previous;
2560 BeforeParen && BeforeParen->is(tok::identifier) &&
2561 BeforeParen->isNot(TT_TypenameMacro) &&
2562 BeforeParen->TokenText == BeforeParen->TokenText.upper() &&
2563 (!BeforeParen->Previous ||
2564 BeforeParen->Previous->ClosesTemplateDeclaration ||
2565 BeforeParen->Previous->ClosesRequiresClause)) {
2566 Current.setType(TT_FunctionAnnotationRParen);
2570 }
else if (Current.is(tok::at) && Current.Next && !Style.isJavaScript() &&
2574 switch (Current.Next->Tok.getObjCKeywordID()) {
2575 case tok::objc_interface:
2576 case tok::objc_implementation:
2577 case tok::objc_protocol:
2578 Current.setType(TT_ObjCDecl);
2580 case tok::objc_property:
2581 Current.setType(TT_ObjCProperty);
2586 }
else if (Current.is(tok::period)) {
2587 FormatToken *PreviousNoComment = Current.getPreviousNonComment();
2588 if (PreviousNoComment &&
2589 PreviousNoComment->isOneOf(tok::comma, tok::l_brace)) {
2590 Current.setType(TT_DesignatedInitializerPeriod);
2591 }
else if (Style.isJava() && Current.Previous &&
2592 Current.Previous->isOneOf(TT_JavaAnnotation,
2593 TT_LeadingJavaAnnotation)) {
2594 Current.setType(Current.Previous->getType());
2596 }
else if (canBeObjCSelectorComponent(Current) &&
2599 Current.Previous && Current.Previous->is(TT_CastRParen) &&
2600 Current.Previous->MatchingParen &&
2601 Current.Previous->MatchingParen->Previous &&
2602 Current.Previous->MatchingParen->Previous->is(
2603 TT_ObjCMethodSpecifier)) {
2607 Current.setType(TT_SelectorName);
2608 }
else if (Current.isOneOf(tok::identifier, tok::kw_const, tok::kw_noexcept,
2609 tok::kw_requires) &&
2611 Current.Previous->isNoneOf(tok::equal, tok::at,
2612 TT_CtorInitializerComma,
2613 TT_CtorInitializerColon) &&
2614 Line.MightBeFunctionDecl && Contexts.size() == 1) {
2617 Current.setType(TT_TrailingAnnotation);
2618 }
else if ((Style.isJava() || Style.isJavaScript()) && Current.Previous) {
2619 if (Current.Previous->is(tok::at) &&
2620 Current.isNot(Keywords.kw_interface)) {
2624 Current.setType(TT_LeadingJavaAnnotation);
2626 Current.setType(TT_JavaAnnotation);
2627 }
else if (Current.Previous->is(tok::period) &&
2628 Current.Previous->isOneOf(TT_JavaAnnotation,
2629 TT_LeadingJavaAnnotation)) {
2630 Current.setType(Current.Previous->getType());
2642 if (Style.isVerilog())
2645 if (!
Tok.Previous ||
Tok.isNot(tok::identifier) ||
Tok.is(TT_ClassHeadName))
2648 if (
Tok.endsSequence(Keywords.kw_final, TT_ClassHeadName))
2651 if ((Style.isJavaScript() || Style.isJava()) &&
Tok.is(Keywords.kw_extends))
2654 if (
const auto *NextNonComment =
Tok.getNextNonComment();
2655 (!NextNonComment && !Line.InMacroBody) ||
2657 (NextNonComment->isPointerOrReference() ||
2658 NextNonComment->isOneOf(TT_ClassHeadName, tok::string_literal) ||
2659 (Line.InPragmaDirective && NextNonComment->is(tok::identifier))))) {
2663 if (
Tok.Previous->isOneOf(TT_LeadingJavaAnnotation, Keywords.kw_instanceof,
2667 if (Style.isJavaScript() &&
Tok.Previous->is(Keywords.kw_in))
2674 if (!Style.isJavaScript())
2675 while (PreviousNotConst && PreviousNotConst->is(tok::kw_const))
2676 PreviousNotConst = PreviousNotConst->getPreviousNonComment();
2678 if (!PreviousNotConst)
2681 if (PreviousNotConst->ClosesRequiresClause)
2684 if (Style.isTableGen()) {
2686 if (Keywords.isTableGenDefinition(*PreviousNotConst))
2689 if (Contexts.back().ContextKind != tok::l_brace)
2693 bool IsPPKeyword = PreviousNotConst->is(tok::identifier) &&
2694 PreviousNotConst->Previous &&
2695 PreviousNotConst->Previous->is(tok::hash);
2697 if (PreviousNotConst->is(TT_TemplateCloser)) {
2698 return PreviousNotConst && PreviousNotConst->MatchingParen &&
2699 PreviousNotConst->MatchingParen->Previous &&
2700 PreviousNotConst->MatchingParen->Previous->isNoneOf(
2701 tok::period, tok::kw_template);
2704 if ((PreviousNotConst->is(tok::r_paren) &&
2705 PreviousNotConst->is(TT_TypeDeclarationParen)) ||
2706 PreviousNotConst->is(TT_AttributeRParen)) {
2715 if (PreviousNotConst->isOneOf(tok::identifier, tok::kw_auto) &&
2716 !PreviousNotConst->endsSequence(Keywords.kw_import, tok::kw_export) &&
2717 PreviousNotConst->isNot(TT_StatementAttributeLikeMacro)) {
2722 if (PreviousNotConst->is(TT_PointerOrReference) ||
2723 PreviousNotConst->endsSequence(tok::coloncolon,
2724 TT_PointerOrReference)) {
2729 if (PreviousNotConst->isTypeName(LangOpts))
2733 if (Style.isJava() && PreviousNotConst->is(tok::r_square))
2737 return Style.isJavaScript() && PreviousNotConst->is(tok::kw_const);
2747 if (LeftOfParens && LeftOfParens->is(TT_TemplateCloser) &&
2748 LeftOfParens->MatchingParen) {
2749 auto *Prev = LeftOfParens->MatchingParen->getPreviousNonComment();
2751 Prev->isOneOf(tok::kw_const_cast, tok::kw_dynamic_cast,
2752 tok::kw_reinterpret_cast, tok::kw_static_cast)) {
2763 assert(
Tok.is(tok::r_paren));
2765 if (!
Tok.MatchingParen || !
Tok.Previous)
2769 if (!IsCpp && !Style.isCSharp() && !Style.isJava())
2772 const auto *LParen =
Tok.MatchingParen;
2773 const auto *BeforeRParen =
Tok.Previous;
2774 const auto *AfterRParen =
Tok.Next;
2777 if (BeforeRParen == LParen || !AfterRParen)
2780 if (LParen->isOneOf(TT_OverloadedOperatorLParen, TT_FunctionTypeLParen))
2783 auto *LeftOfParens = LParen->getPreviousNonComment();
2787 if (LeftOfParens->is(tok::r_paren) &&
2788 LeftOfParens->isNot(TT_CastRParen)) {
2789 if (!LeftOfParens->MatchingParen ||
2790 !LeftOfParens->MatchingParen->Previous) {
2793 LeftOfParens = LeftOfParens->MatchingParen->Previous;
2796 if (LeftOfParens->is(tok::r_square)) {
2799 if (
Tok->isNot(tok::r_square))
2802 Tok =
Tok->getPreviousNonComment();
2803 if (!
Tok ||
Tok->isNot(tok::l_square))
2806 Tok =
Tok->getPreviousNonComment();
2807 if (!
Tok ||
Tok->isNot(tok::kw_delete))
2811 if (
FormatToken *MaybeDelete = MayBeArrayDelete(LeftOfParens))
2812 LeftOfParens = MaybeDelete;
2818 if (LeftOfParens->Tok.getIdentifierInfo() && LeftOfParens->Previous &&
2819 LeftOfParens->Previous->is(tok::kw_operator)) {
2825 if (LeftOfParens->Tok.getIdentifierInfo() &&
2826 LeftOfParens->isNoneOf(TT_ObjCForIn, tok::kw_return, tok::kw_case,
2827 tok::kw_delete, tok::kw_throw)) {
2833 if (LeftOfParens->isOneOf(tok::at, tok::r_square, TT_OverloadedOperator,
2834 TT_TemplateCloser, tok::ellipsis)) {
2839 if (AfterRParen->is(tok::question) ||
2840 (AfterRParen->is(tok::ampamp) && !BeforeRParen->isTypeName(LangOpts))) {
2845 if (AfterRParen->is(Keywords.kw_in) && Style.isCSharp())
2850 if (AfterRParen->isOneOf(tok::kw_noexcept, tok::kw_volatile, tok::kw_const,
2851 tok::kw_requires, tok::kw_throw, tok::arrow,
2852 Keywords.kw_override, Keywords.kw_final) ||
2853 isCppAttribute(IsCpp, *AfterRParen)) {
2859 if (Style.isJava() && AfterRParen->is(tok::l_paren))
2863 if (AfterRParen->isOneOf(tok::kw_sizeof, tok::kw_alignof) ||
2864 (AfterRParen->Tok.isLiteral() &&
2865 AfterRParen->isNot(tok::string_literal))) {
2870 if (
Tok.isNot(TT_TemplateCloser))
2872 const auto *
Less =
Tok.MatchingParen;
2875 const auto *BeforeLess =
Less->getPreviousNonComment();
2876 return BeforeLess && BeforeLess->isNot(TT_VariableTemplate);
2880 auto IsQualifiedPointerOrReference = [](
const FormatToken *
T,
2881 const LangOptions &LangOpts) {
2883 assert(!
T->isTypeName(LangOpts) &&
"Should have already been checked");
2887 if (
T->is(TT_AttributeRParen)) {
2889 assert(
T->is(tok::r_paren));
2890 assert(
T->MatchingParen);
2891 assert(
T->MatchingParen->is(tok::l_paren));
2892 assert(
T->MatchingParen->is(TT_AttributeLParen));
2893 if (
const auto *
Tok =
T->MatchingParen->Previous;
2894 Tok &&
Tok->isAttribute()) {
2898 }
else if (
T->is(TT_AttributeRSquare)) {
2900 if (
T->MatchingParen &&
T->MatchingParen->Previous) {
2901 T =
T->MatchingParen->Previous;
2904 }
else if (
T->canBePointerOrReferenceQualifier()) {
2910 return T &&
T->is(TT_PointerOrReference);
2913 bool ParensAreType = IsNonVariableTemplate(*BeforeRParen) ||
2914 BeforeRParen->is(TT_TypeDeclarationParen) ||
2915 BeforeRParen->isTypeName(LangOpts) ||
2916 IsQualifiedPointerOrReference(BeforeRParen, LangOpts);
2917 bool ParensCouldEndDecl =
2918 AfterRParen->isOneOf(tok::equal, tok::semi, tok::l_brace, tok::greater);
2919 if (ParensAreType && !ParensCouldEndDecl)
2930 for (
const auto *Token = LParen->Next; Token != &
Tok; Token = Token->Next)
2931 if (Token->is(TT_BinaryOperator))
2936 if (AfterRParen->isOneOf(tok::identifier, tok::kw_this))
2940 if (AfterRParen->is(tok::l_paren)) {
2941 for (
const auto *Prev = BeforeRParen; Prev->is(tok::identifier);) {
2942 Prev = Prev->Previous;
2943 if (Prev->is(tok::coloncolon))
2944 Prev = Prev->Previous;
2950 if (!AfterRParen->Next)
2963 const bool NextIsAmpOrStar = AfterRParen->isOneOf(tok::amp, tok::star);
2964 if (!(AfterRParen->isUnaryOperator() || NextIsAmpOrStar) ||
2965 AfterRParen->is(tok::plus) ||
2966 AfterRParen->Next->isNoneOf(tok::identifier, tok::numeric_constant)) {
2970 if (NextIsAmpOrStar &&
2971 (AfterRParen->Next->is(tok::numeric_constant) || Line.InPPDirective)) {
2975 if (Line.InPPDirective && AfterRParen->is(tok::minus))
2978 const auto *Prev = BeforeRParen;
2981 if (Prev->is(tok::r_paren)) {
2982 if (Prev->is(TT_CastRParen))
2984 Prev = Prev->MatchingParen;
2987 Prev = Prev->Previous;
2988 if (!Prev || Prev->isNot(tok::r_paren))
2990 Prev = Prev->MatchingParen;
2991 return Prev && Prev->is(TT_FunctionTypeLParen);
2995 for (Prev = BeforeRParen; Prev != LParen; Prev = Prev->Previous)
2996 if (Prev->isNoneOf(tok::kw_const, tok::identifier, tok::coloncolon))
3014 if (PrevToken->isOneOf(
3015 TT_ConditionalExpr, tok::l_paren, tok::comma, tok::colon, tok::semi,
3016 tok::equal, tok::question, tok::l_square, tok::l_brace,
3017 tok::kw_case, tok::kw_co_await, tok::kw_co_return, tok::kw_co_yield,
3018 tok::kw_delete, 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))
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;
3218 if (!PrevToken || PrevToken->is(TT_CastRParen))
3219 return TT_UnaryOperator;
3220 if (PrevToken->isOneOf(tok::r_paren, tok::r_square, tok::identifier))
3221 return TT_TrailingUnaryOperator;
3223 return TT_UnaryOperator;
3226 SmallVector<Context, 8> Contexts;
3228 const FormatStyle &Style;
3229 AnnotatedLine &Line;
3233 LangOptions LangOpts;
3234 const AdditionalKeywords &Keywords;
3236 SmallVector<ScopeType> &Scopes;
3242 llvm::SmallPtrSet<FormatToken *, 16> NonTemplateLess;
3244 int TemplateDeclarationDepth;
3252class ExpressionParser {
3254 ExpressionParser(
const FormatStyle &Style,
const AdditionalKeywords &Keywords,
3255 AnnotatedLine &Line)
3256 : Style(Style), Keywords(Keywords), Line(Line), Current(Line.
First) {}
3259 void parse(
int Precedence = 0) {
3262 while (Current && (Current->is(tok::kw_return) ||
3263 (Current->is(tok::colon) &&
3264 Current->isOneOf(TT_ObjCMethodExpr, TT_DictLiteral)))) {
3268 if (!Current || Precedence > PrecedenceArrowAndPeriod)
3273 parseConditionalExpr();
3279 if (Precedence == PrecedenceUnaryOperator) {
3280 parseUnaryOperator();
3297 if (Style.isVerilog() && Precedence ==
prec::Comma) {
3298 VerilogFirstOfType =
3299 verilogGroupDecl(VerilogFirstOfType, LatestOperator);
3303 parse(Precedence + 1);
3305 int CurrentPrecedence = getCurrentPrecedence();
3314 if (Style.BreakBinaryOperations.PerOperator.empty() &&
3315 Style.BreakBinaryOperations.Default ==
3321 if (Precedence == CurrentPrecedence && Current &&
3322 Current->is(TT_SelectorName)) {
3324 addFakeParenthesis(Start,
prec::Level(Precedence));
3328 if ((Style.isCSharp() || Style.isJavaScript() || Style.isJava()) &&
3332 FormatToken *Prev = Current->getPreviousNonComment();
3333 if (Prev && Prev->is(tok::string_literal) &&
3334 (Prev == Start || Prev->endsSequence(tok::string_literal, tok::plus,
3335 TT_StringInConcatenation))) {
3336 Prev->setType(TT_StringInConcatenation);
3343 (Current->closesScope() &&
3344 (Current->MatchingParen || Current->is(TT_TemplateString))) ||
3345 (CurrentPrecedence != -1 && CurrentPrecedence < Precedence) ||
3354 if (Current->opensScope() ||
3355 Current->isOneOf(TT_RequiresClause,
3356 TT_RequiresClauseInARequiresExpression)) {
3359 while (Current && (!Current->closesScope() || Current->opensScope())) {
3366 if (CurrentPrecedence == Precedence) {
3368 LatestOperator->NextOperator = Current;
3369 LatestOperator = Current;
3373 next(Precedence > 0);
3378 if (Style.isVerilog() && Precedence ==
prec::Comma && VerilogFirstOfType)
3379 addFakeParenthesis(VerilogFirstOfType,
prec::Comma);
3381 if (LatestOperator && (Current || Precedence > 0)) {
3387 Start->Previous->isOneOf(TT_RequiresClause,
3388 TT_RequiresClauseInARequiresExpression))
3390 auto Ret = Current ? Current : Line.Last;
3391 while (!
Ret->ClosesRequiresClause &&
Ret->Previous)
3397 if (Precedence == PrecedenceArrowAndPeriod) {
3401 addFakeParenthesis(Start,
prec::Level(Precedence), End);
3409 int getCurrentPrecedence() {
3411 const FormatToken *NextNonComment = Current->getNextNonComment();
3412 if (Current->is(TT_ConditionalExpr))
3414 if (NextNonComment && Current->is(TT_SelectorName) &&
3415 (NextNonComment->isOneOf(TT_DictLiteral, TT_JsTypeColon) ||
3416 (Style.isProto() && NextNonComment->is(tok::less)))) {
3419 if (Current->is(TT_JsComputedPropertyName))
3421 if (Current->is(TT_LambdaArrow))
3423 if (Current->is(TT_FatArrow))
3425 if (Current->isOneOf(tok::semi, TT_InlineASMColon, TT_SelectorName) ||
3426 (Current->is(tok::comment) && NextNonComment &&
3427 NextNonComment->is(TT_SelectorName))) {
3430 if (Current->is(TT_RangeBasedForLoopColon))
3432 if ((Style.isJava() || Style.isJavaScript()) &&
3433 Current->is(Keywords.kw_instanceof)) {
3436 if (Style.isJavaScript() &&
3437 Current->isOneOf(Keywords.kw_in, Keywords.kw_as)) {
3440 if (Current->isOneOf(TT_BinaryOperator, tok::comma))
3441 return Current->getPrecedence();
3442 if (Current->isOneOf(tok::period, tok::arrow) &&
3443 Current->isNot(TT_TrailingReturnArrow)) {
3444 return PrecedenceArrowAndPeriod;
3446 if ((Style.isJava() || Style.isJavaScript()) &&
3447 Current->isOneOf(Keywords.kw_extends, Keywords.kw_implements,
3448 Keywords.kw_throws)) {
3453 if (Style.isVerilog() && Current->is(tok::colon))
3465 if (Start->MacroParent)
3468 Start->FakeLParens.push_back(Precedence);
3470 Start->StartsBinaryExpression =
true;
3471 if (!End && Current)
3472 End = Current->getPreviousNonComment();
3476 End->EndsBinaryExpression =
true;
3482 void parseUnaryOperator() {
3483 SmallVector<FormatToken *, 2> Tokens;
3484 while (Current && Current->is(TT_UnaryOperator)) {
3485 Tokens.push_back(Current);
3488 parse(PrecedenceArrowAndPeriod);
3495 void parseConditionalExpr() {
3496 while (Current && Current->isTrailingComment())
3500 if (!Current || Current->isNot(tok::question))
3504 if (!Current || Current->isNot(TT_ConditionalExpr))
3511 void next(
bool SkipPastLeadingComments =
true) {
3513 Current = Current->Next;
3515 (Current->NewlinesBefore == 0 || SkipPastLeadingComments) &&
3516 Current->isTrailingComment()) {
3517 Current = Current->Next;
3531 while (Start->startsSequence(tok::l_paren, tok::star)) {
3532 if (!(Start = Start->MatchingParen) ||
3533 !(Start = Start->getNextNonComment())) {
3540 if (
Tok->is(Keywords.kw_assign))
3541 Tok =
Tok->getNextNonComment();
3553 if (
Tok->is(tok::hash)) {
3558 Tok =
Tok->getNextNonComment();
3559 }
else if (
Tok->is(tok::hashhash)) {
3563 Tok =
Tok->getNextNonComment();
3564 }
else if (Keywords.isVerilogQualifier(*
Tok) ||
3565 Keywords.isVerilogIdentifier(*
Tok)) {
3569 while (
Tok &&
Tok->isOneOf(tok::period, tok::coloncolon) &&
3570 (
Tok =
Tok->getNextNonComment())) {
3571 if (Keywords.isVerilogIdentifier(*
Tok))
3572 Tok =
Tok->getNextNonComment();
3576 }
else if (
Tok->is(tok::l_paren)) {
3581 Keywords.kw_highz0, Keywords.kw_highz1, Keywords.kw_large,
3582 Keywords.kw_medium, Keywords.kw_pull0, Keywords.kw_pull1,
3583 Keywords.kw_small, Keywords.kw_strong0, Keywords.kw_strong1,
3584 Keywords.kw_supply0, Keywords.kw_supply1, Keywords.kw_weak0,
3585 Keywords.kw_weak1)) {
3586 Tok->setType(TT_VerilogStrength);
3587 Tok =
Tok->MatchingParen;
3589 Tok->setType(TT_VerilogStrength);
3590 Tok =
Tok->getNextNonComment();
3595 }
else if (
Tok->is(Keywords.kw_verilogHash)) {
3597 if (
Next->is(tok::l_paren))
3600 Tok =
Next->getNextNonComment();
3609 while (
Tok &&
Tok->is(tok::l_square) && (
Tok =
Tok->MatchingParen))
3610 Tok =
Tok->getNextNonComment();
3611 if (
Tok && (
Tok->is(tok::hash) || Keywords.isVerilogIdentifier(*
Tok)))
3620 First->setType(TT_VerilogDimensionedTypeName);
3621 }
else if (
First != Start) {
3629 if (TypedName->is(TT_Unknown))
3630 TypedName->setType(TT_StartOfName);
3632 if (FirstOfType && PreviousComma) {
3633 PreviousComma->setType(TT_VerilogTypeComma);
3634 addFakeParenthesis(FirstOfType,
prec::Comma, PreviousComma->Previous);
3637 FirstOfType = TypedName;
3644 while (Current && Current != FirstOfType) {
3645 if (Current->opensScope()) {
3656 const FormatStyle &Style;
3657 const AdditionalKeywords &Keywords;
3658 const AnnotatedLine &Line;
3668 assert(
Line->First);
3672 if (
const auto Column =
Line->First->OriginalColumn;
3676 const bool PPDirectiveOrImportStmt =
3679 if (PPDirectiveOrImportStmt)
3681 if (
const auto IndentWidth = Style.IndentWidth;
3683 Column % IndentWidth == 0) {
3691 PPDirectiveOrImportStmt
3693 : NextNonCommentLine->
Level;
3696 NextNonCommentLine =
Line->First->isNot(tok::r_brace) ?
Line :
nullptr;
3716 if (
Tok->isNot(tok::identifier))
3719 Tok =
Tok->getNextNonComment();
3725 if (
Tok->is(tok::coloncolon))
3726 return Tok->getNextNonComment();
3730 if (
Tok->is(TT_TemplateOpener)) {
3731 Tok =
Tok->MatchingParen;
3735 Tok =
Tok->getNextNonComment();
3740 return Tok->is(tok::coloncolon) ?
Tok->getNextNonComment() :
nullptr;
3748 Tok =
Tok->getNextNonComment()) {
3750 if (
Tok->is(TT_AttributeLSquare)) {
3751 Tok =
Tok->MatchingParen;
3759 if (
Tok->is(tok::l_paren) &&
Tok->is(TT_Unknown) &&
Tok->MatchingParen) {
3767 if (
Tok->isOneOf(tok::kw_friend, tok::kw_inline, tok::kw_virtual,
3768 tok::kw_constexpr, tok::kw_consteval, tok::kw_explicit)) {
3774 if (
Tok->is(tok::kw_template)) {
3775 Tok =
Tok->getNextNonComment();
3781 if (
Tok->isNot(TT_TemplateOpener))
3784 Tok =
Tok->MatchingParen;
3792 if (
Tok->is(tok::coloncolon)) {
3803 if (
Tok->is(tok::tilde)) {
3810 if (
Tok->isNot(tok::identifier) ||
Tok->isNot(TT_Unknown))
3821 assert(
Tok &&
Tok->is(tok::identifier));
3822 const auto *Prev =
Tok->Previous;
3824 if (Prev && Prev->is(tok::tilde))
3825 Prev = Prev->Previous;
3828 if (!Prev || (!Prev->endsSequence(tok::coloncolon, tok::identifier) &&
3829 !Prev->endsSequence(tok::coloncolon, TT_TemplateCloser))) {
3833 assert(Prev->Previous);
3834 if (Prev->Previous->is(TT_TemplateCloser) && Prev->Previous->MatchingParen) {
3835 Prev = Prev->Previous->MatchingParen;
3836 assert(Prev->Previous);
3839 return Prev->Previous->TokenText ==
Tok->TokenText;
3843 if (!
Line.InMacroBody)
3844 MacroBodyScopes.clear();
3846 auto &ScopeStack =
Line.InMacroBody ? MacroBodyScopes : Scopes;
3847 AnnotatingParser
Parser(Style,
Line, Keywords, ScopeStack);
3850 if (!
Line.Children.empty()) {
3853 for (
auto &Child :
Line.Children) {
3854 if (InRequiresExpression &&
3855 Child->First->isNoneOf(tok::kw_typename, tok::kw_requires,
3856 TT_CompoundRequirementLBrace)) {
3862 if (!ScopeStack.empty())
3863 ScopeStack.pop_back();
3876 ExpressionParser ExprParser(Style, Keywords,
Line);
3882 if (
Tok && ((!ScopeStack.empty() && ScopeStack.back() ==
ST_Class) ||
3884 Tok->setFinalizedType(TT_CtorDtorDeclName);
3885 assert(OpeningParen);
3890 if (
Line.startsWith(TT_ObjCMethodSpecifier))
3892 else if (
Line.startsWith(TT_ObjCDecl))
3894 else if (
Line.startsWith(TT_ObjCProperty))
3898 First->SpacesRequiredBefore = 1;
3899 First->CanBreakBefore =
First->MustBreakBefore;
3908 if (Current.
is(TT_FunctionDeclarationName))
3911 if (Current.
isNoneOf(tok::identifier, tok::kw_operator))
3914 const auto *Prev = Current.getPreviousNonComment();
3919 if (
const auto *PrevPrev =
Previous.getPreviousNonComment();
3920 PrevPrev && PrevPrev->is(TT_ObjCDecl)) {
3924 auto skipOperatorName =
3927 if (
Next->is(TT_OverloadedOperatorLParen))
3929 if (
Next->is(TT_OverloadedOperator))
3931 if (
Next->isPlacementOperator() ||
Next->is(tok::kw_co_await)) {
3934 Next->Next->startsSequence(tok::l_square, tok::r_square)) {
3939 if (
Next->startsSequence(tok::l_square, tok::r_square)) {
3944 if ((
Next->isTypeName(LangOpts) ||
Next->is(tok::identifier)) &&
3945 Next->Next &&
Next->Next->isPointerOrReference()) {
3950 if (
Next->is(TT_TemplateOpener) &&
Next->MatchingParen) {
3961 const bool IsCpp = LangOpts.CXXOperatorNames || LangOpts.C11;
3964 if (Current.
is(tok::kw_operator)) {
3965 if (
Line.startsWith(tok::kw_friend))
3967 if (
Previous.Tok.getIdentifierInfo() &&
3968 Previous.isNoneOf(tok::kw_return, tok::kw_co_return)) {
3973 assert(
Previous.MatchingParen->is(tok::l_paren));
3974 assert(
Previous.MatchingParen->is(TT_TypeDeclarationParen));
3983 while (
Next &&
Next->startsSequence(tok::hashhash, tok::identifier))
3986 if (
Next->is(TT_TemplateOpener) &&
Next->MatchingParen) {
3988 }
else if (
Next->is(tok::coloncolon)) {
3992 if (
Next->is(tok::kw_operator)) {
3993 Next = skipOperatorName(
Next->Next);
3996 if (
Next->isNot(tok::identifier))
3998 }
else if (isCppAttribute(IsCpp, *
Next)) {
4002 }
else if (
Next->is(tok::l_paren)) {
4011 if (!
Next ||
Next->isNot(tok::l_paren) || !
Next->MatchingParen)
4013 ClosingParen =
Next->MatchingParen;
4014 assert(ClosingParen->
is(tok::r_paren));
4016 if (
Line.Last->is(tok::l_brace))
4018 if (
Next->Next == ClosingParen)
4021 if (ClosingParen->
Next && ClosingParen->
Next->
is(TT_PointerOrReference))
4034 if (IsCpp &&
Next->Next &&
Next->Next->is(tok::identifier) &&
4035 !
Line.endsWith(tok::semi)) {
4041 if (
Tok->is(TT_TypeDeclarationParen))
4043 if (
Tok->isOneOf(tok::l_paren, TT_TemplateOpener) &&
Tok->MatchingParen) {
4044 Tok =
Tok->MatchingParen;
4047 if (
Tok->is(tok::kw_const) ||
Tok->isTypeName(LangOpts) ||
4048 Tok->isOneOf(TT_PointerOrReference, TT_StartOfName, tok::ellipsis)) {
4051 if (
Tok->isOneOf(tok::l_brace, TT_ObjCMethodExpr) ||
Tok->Tok.isLiteral())
4057bool TokenAnnotator::mustBreakForReturnType(
const AnnotatedLine &
Line)
const {
4058 assert(
Line.MightBeFunctionDecl);
4066 switch (Style.BreakAfterReturnType) {
4076 return Line.mightBeFunctionDefinition();
4082bool TokenAnnotator::mustBreakBeforeReturnType(
4084 assert(
Line.MightBeFunctionDecl);
4086 switch (Style.BreakBeforeReturnType) {
4092 return Line.Level == 0;
4094 return Line.mightBeFunctionDefinition();
4096 return Line.Level == 0 &&
Line.mightBeFunctionDefinition();
4103 auto *
Tok =
Line.getFirstNonComment();
4107 if (
Tok->is(tok::kw_template)) {
4108 auto *Opener =
Tok->Next;
4109 while (Opener && Opener->isNot(TT_TemplateOpener))
4110 Opener = Opener->Next;
4111 if (!Opener || !Opener->MatchingParen)
4113 Tok = Opener->MatchingParen->Next;
4116 if (
Tok &&
Tok->is(TT_RequiresClause)) {
4117 while (
Tok && !
Tok->ClosesRequiresClause)
4125 Tok->isOneOf(tok::kw___attribute, tok::kw___declspec,
4126 TT_AttributeMacro)) {
4128 if (
Next &&
Next->is(tok::l_paren) &&
Next->MatchingParen)
4129 Tok =
Next->MatchingParen->Next;
4134 if (
Tok->is(TT_AttributeLSquare) &&
Tok->MatchingParen) {
4135 Tok =
Tok->MatchingParen->Next;
4147 Line.Computed =
true;
4155 :
Line.FirstStartColumn +
First->ColumnWidth;
4156 bool AlignArrayOfStructures =
4159 if (AlignArrayOfStructures)
4160 calculateArrayInitializerColumnList(
Line);
4162 const auto *FirstNonComment =
Line.getFirstNonComment();
4163 bool SeenName =
false;
4164 bool LineIsFunctionDeclaration =
false;
4168 for (
auto *
Tok = FirstNonComment && FirstNonComment->isNot(tok::kw_using)
4169 ? FirstNonComment->Next
4172 if (
Tok->is(TT_StartOfName))
4174 if (
Tok->Previous->EndsCppAttributeGroup)
4175 AfterLastAttribute =
Tok;
4176 if (
const bool IsCtorOrDtor =
Tok->is(TT_CtorDtorDeclName);
4180 Tok->setFinalizedType(TT_FunctionDeclarationName);
4181 LineIsFunctionDeclaration =
true;
4185 assert(OpeningParen);
4186 if (OpeningParen->is(TT_Unknown))
4187 OpeningParen->setType(TT_FunctionDeclarationLParen);
4194 if ((LineIsFunctionDeclaration ||
4195 (FirstNonComment && FirstNonComment->is(TT_CtorDtorDeclName))) &&
4196 Line.endsWith(tok::semi, tok::r_brace)) {
4197 auto *
Tok =
Line.Last->Previous;
4198 while (
Tok->isNot(tok::r_brace))
4200 if (
auto *LBrace =
Tok->MatchingParen; LBrace && LBrace->is(TT_Unknown)) {
4201 assert(LBrace->is(tok::l_brace));
4204 LBrace->setFinalizedType(TT_FunctionLBrace);
4208 if (SeenName && AfterLastAttribute &&
4211 if (LineIsFunctionDeclaration)
4212 Line.ReturnTypeWrapped =
true;
4215 if (!LineIsFunctionDeclaration) {
4216 Line.ReturnTypeWrapped =
false;
4218 for (
const auto *
Tok = FirstNonComment;
Tok;
Tok =
Tok->Next) {
4219 if (
Tok->isNot(tok::kw_operator))
4223 }
while (
Tok &&
Tok->isNot(TT_OverloadedOperatorLParen));
4224 if (!
Tok || !
Tok->MatchingParen)
4226 const auto *LeftParen =
Tok;
4227 for (
Tok =
Tok->Next;
Tok &&
Tok != LeftParen->MatchingParen;
4229 if (
Tok->isNot(tok::identifier))
4232 const bool NextIsBinaryOperator =
4234 Next->Next->is(tok::identifier);
4235 if (!NextIsBinaryOperator)
4237 Next->setType(TT_BinaryOperator);
4241 }
else if (ClosingParen) {
4243 if (
Tok->is(TT_CtorInitializerColon))
4245 if (
Tok->is(tok::arrow)) {
4246 Tok->overwriteFixedType(TT_TrailingReturnArrow);
4249 if (
Tok->isNot(TT_TrailingAnnotation))
4252 if (!
Next ||
Next->isNot(tok::l_paren))
4261 if (
Line.MightBeFunctionDecl && LineIsFunctionDeclaration &&
4262 mustBreakBeforeReturnType(
Line)) {
4264 ReturnTypeStart && ReturnTypeStart != FirstNonComment &&
4265 ReturnTypeStart->
isNoneOf(TT_FunctionDeclarationName,
4266 TT_CtorDtorDeclName, tok::tilde)) {
4267 ReturnTypeStart->MustBreakBefore =
true;
4268 Line.ReturnTypeWrapped =
true;
4272 if (
First->is(TT_ElseLBrace)) {
4273 First->CanBreakBefore =
true;
4274 First->MustBreakBefore =
true;
4277 bool InFunctionDecl =
Line.MightBeFunctionDecl;
4278 bool InParameterList =
false;
4279 for (
auto *Current =
First->Next; Current; Current = Current->Next) {
4281 if (Current->is(TT_LineComment)) {
4283 Current->SpacesRequiredBefore =
4285 !Style.SpacesInParensOptions.Other)
4288 }
else if (Prev->
is(TT_VerilogMultiLineListLParen)) {
4289 Current->SpacesRequiredBefore = 0;
4291 Current->SpacesRequiredBefore = Style.SpacesBeforeTrailingComments;
4301 if (!Current->HasUnescapedNewline) {
4304 if (
Parameter->isOneOf(tok::comment, tok::r_brace))
4307 if (
Parameter->Previous->isNot(TT_CtorInitializerComma) &&
4315 }
else if (!Current->Finalized && Current->SpacesRequiredBefore == 0 &&
4316 spaceRequiredBefore(
Line, *Current)) {
4317 Current->SpacesRequiredBefore = 1;
4322 Current->MustBreakBefore =
true;
4324 Current->MustBreakBefore =
4325 Current->MustBreakBefore || mustBreakBefore(
Line, *Current);
4326 if (!Current->MustBreakBefore && InFunctionDecl &&
4327 Current->is(TT_FunctionDeclarationName)) {
4328 Current->MustBreakBefore = mustBreakForReturnType(
Line);
4332 Current->CanBreakBefore =
4333 !
Line.IsModuleOrImportDecl &&
4334 (Current->MustBreakBefore || canBreakBefore(
Line, *Current));
4336 if (Current->is(TT_FunctionDeclarationLParen)) {
4337 InParameterList =
true;
4338 }
else if (Current->is(tok::r_paren)) {
4339 const auto *LParen = Current->MatchingParen;
4340 if (LParen && LParen->is(TT_FunctionDeclarationLParen))
4341 InParameterList =
false;
4342 }
else if (InParameterList &&
4343 Current->endsSequence(TT_AttributeMacro,
4344 TT_PointerOrReference)) {
4345 Current->CanBreakBefore =
false;
4348 unsigned ChildSize = 0;
4351 ChildSize = LastOfChild.isTrailingComment() ? Style.ColumnLimit
4354 if (Current->MustBreakBefore || Prev->
Children.size() > 1 ||
4356 Prev->
Children[0]->First->MustBreakBefore) ||
4357 Current->IsMultiline) {
4358 Current->TotalLength = Prev->
TotalLength + Style.ColumnLimit;
4360 Current->TotalLength = Prev->
TotalLength + Current->ColumnWidth +
4361 ChildSize + Current->SpacesRequiredBefore;
4369 Prev->
isOneOf(tok::l_paren, tok::l_brace,
4370 TT_ArrayInitializerLSquare) &&
4373 for (
auto *ParamTok = Current; ParamTok && ParamTok != RParen;
4374 ParamTok = ParamTok->Next) {
4375 if (ParamTok->opensScope()) {
4376 ParamTok = ParamTok->MatchingParen;
4381 ParamTok->MustBreakBefore =
true;
4382 ParamTok->CanBreakBefore =
true;
4387 if (Current->is(TT_ControlStatementLBrace)) {
4388 if (Style.ColumnLimit > 0 &&
4389 Style.BraceWrapping.AfterControlStatement ==
4391 Line.Level * Style.IndentWidth +
Line.Last->TotalLength >
4392 Style.ColumnLimit) {
4393 Current->CanBreakBefore =
true;
4394 Current->MustBreakBefore =
true;
4396 }
else if (Current->is(TT_CtorInitializerColon)) {
4397 InFunctionDecl =
false;
4409 Current->SplitPenalty = splitPenalty(
Line, *Current, InFunctionDecl);
4411 Current->is(TT_SelectorName) && Current->ParameterIndex > 0) {
4412 if (Current->ParameterIndex == 1)
4413 Current->SplitPenalty += 5 * Current->BindingStrength;
4415 Current->SplitPenalty += 20 * Current->BindingStrength;
4419 calculateUnbreakableTailLengths(
Line);
4421 for (
auto *Current =
First; Current; Current = Current->Next) {
4423 Current->Role->precomputeFormattingInfos(Current);
4424 if (Current->MatchingParen &&
4425 Current->MatchingParen->opensBlockOrBlockTypeList(Style) &&
4430 if (Current->opensBlockOrBlockTypeList(Style))
4434 LLVM_DEBUG({ printDebugInfo(
Line); });
4437void TokenAnnotator::calculateUnbreakableTailLengths(
4444 Current->
isOneOf(tok::comment, tok::string_literal)) {
4454void TokenAnnotator::calculateArrayInitializerColumnList(
4458 auto *CurrentToken =
Line.First;
4459 CurrentToken->ArrayInitializerLineStart =
true;
4461 while (CurrentToken && CurrentToken !=
Line.Last) {
4462 if (CurrentToken->is(tok::l_brace)) {
4463 CurrentToken->IsArrayInitializer =
true;
4464 if (CurrentToken->Next)
4465 CurrentToken->Next->MustBreakBefore =
true;
4467 calculateInitializerColumnList(
Line, CurrentToken->Next, Depth + 1);
4469 CurrentToken = CurrentToken->Next;
4474FormatToken *TokenAnnotator::calculateInitializerColumnList(
4476 while (CurrentToken && CurrentToken !=
Line.Last) {
4477 if (CurrentToken->is(tok::l_brace))
4479 else if (CurrentToken->is(tok::r_brace))
4481 if (Depth == 2 && CurrentToken->isOneOf(tok::l_brace, tok::comma)) {
4482 CurrentToken = CurrentToken->Next;
4485 CurrentToken->StartsColumn =
true;
4486 CurrentToken = CurrentToken->Previous;
4488 CurrentToken = CurrentToken->Next;
4490 return CurrentToken;
4495 bool InFunctionDecl)
const {
4499 if (
Left.is(tok::semi))
4503 if (Style.isJava()) {
4504 if (
Right.isOneOf(Keywords.kw_extends, Keywords.kw_throws))
4506 if (
Right.is(Keywords.kw_implements))
4508 if (
Left.is(tok::comma) &&
Left.NestingLevel == 0)
4510 }
else if (Style.isJavaScript()) {
4511 if (
Right.is(Keywords.kw_function) &&
Left.isNot(tok::comma))
4513 if (
Left.is(TT_JsTypeColon))
4515 if ((
Left.is(TT_TemplateString) &&
Left.TokenText.ends_with(
"${")) ||
4516 (
Right.is(TT_TemplateString) &&
Right.TokenText.starts_with(
"}"))) {
4520 if (
Left.opensScope() &&
Right.closesScope())
4523 if (
Right.is(tok::l_square))
4525 if (
Right.is(tok::period))
4529 if (
Right.is(tok::identifier) &&
Right.Next &&
Right.Next->is(TT_DictLiteral))
4531 if (
Right.is(tok::l_square)) {
4532 if (
Left.is(tok::r_square))
4535 if (
Right.is(TT_LambdaLSquare) &&
Left.is(tok::equal))
4537 if (
Right.isNoneOf(TT_ObjCMethodExpr, TT_LambdaLSquare,
4538 TT_ArrayInitializerLSquare,
4539 TT_DesignatedInitializerLSquare, TT_AttributeLSquare)) {
4544 if (
Left.is(tok::coloncolon))
4545 return Style.PenaltyBreakScopeResolution;
4546 if (
Right.isOneOf(TT_StartOfName, TT_FunctionDeclarationName,
4547 tok::kw_operator)) {
4548 if (
Line.startsWith(tok::kw_for) &&
Right.PartOfMultiVariableDeclStmt)
4550 if (
Left.is(TT_StartOfName))
4552 if (InFunctionDecl &&
Right.NestingLevel == 0)
4553 return Style.PenaltyReturnTypeOnItsOwnLine;
4556 if (
Right.is(TT_PointerOrReference))
4558 if (
Right.is(TT_LambdaArrow))
4560 if (
Left.is(tok::equal) &&
Right.is(tok::l_brace))
4562 if (
Left.is(TT_CastRParen))
4564 if (
Left.isOneOf(tok::kw_class, tok::kw_struct, tok::kw_union))
4566 if (
Left.is(tok::comment))
4569 if (
Left.isOneOf(TT_RangeBasedForLoopColon, TT_InheritanceColon,
4570 TT_CtorInitializerColon)) {
4574 if (
Right.isMemberAccess()) {
4595 const auto Penalty = Style.PenaltyBreakBeforeMemberAccess;
4597 ? std::min(Penalty, 35u)
4601 if (
Right.is(TT_TrailingAnnotation) &&
4602 (!
Right.Next ||
Right.Next->isNot(tok::l_paren))) {
4605 if (
Line.startsWith(TT_ObjCMethodSpecifier))
4612 bool is_short_annotation =
Right.TokenText.size() < 10;
4613 return (
Left.is(tok::r_paren) ? 100 : 120) + (is_short_annotation ? 50 : 0);
4617 if (
Line.startsWith(tok::kw_for) &&
Left.is(tok::equal))
4622 if (
Right.is(TT_SelectorName))
4624 if (
Left.is(tok::colon)) {
4625 if (
Left.is(TT_ObjCMethodExpr))
4626 return Line.MightBeFunctionDecl ? 50 : 500;
4627 if (
Left.is(TT_ObjCSelector))
4635 Left.Previous->isOneOf(tok::identifier, tok::greater)) {
4639 if (
Left.is(tok::l_paren) && Style.PenaltyBreakOpenParenthesis != 0)
4640 return Style.PenaltyBreakOpenParenthesis;
4641 if (
Left.is(tok::l_paren) && InFunctionDecl && Style.AlignAfterOpenBracket)
4643 if (
Left.is(tok::l_paren) &&
Left.Previous &&
4644 (
Left.Previous->isOneOf(tok::kw_for, tok::kw__Generic) ||
4645 Left.Previous->isIf())) {
4648 if (
Left.is(tok::equal) && InFunctionDecl)
4650 if (
Right.is(tok::r_brace))
4652 if (
Left.is(TT_TemplateOpener))
4654 if (
Left.opensScope()) {
4658 if (!Style.AlignAfterOpenBracket &&
4659 (
Left.ParameterCount <= 1 || Style.AllowAllArgumentsOnNextLine)) {
4662 if (
Left.is(tok::l_brace) &&
4666 return Left.ParameterCount > 1 ? Style.PenaltyBreakBeforeFirstCallParameter
4669 if (
Left.is(TT_JavaAnnotation))
4672 if (
Left.is(TT_UnaryOperator))
4674 if (
Left.isOneOf(tok::plus, tok::comma) &&
Left.Previous &&
4675 Left.Previous->isLabelString() &&
4676 (
Left.NextOperator ||
Left.OperatorIndex != 0)) {
4679 if (
Right.is(tok::plus) &&
Left.isLabelString() &&
4680 (
Right.NextOperator ||
Right.OperatorIndex != 0)) {
4683 if (
Left.is(tok::comma))
4685 if (
Right.is(tok::lessless) &&
Left.isLabelString() &&
4686 (
Right.NextOperator ||
Right.OperatorIndex != 1)) {
4689 if (
Right.is(tok::lessless)) {
4691 if (
Left.isNot(tok::r_paren) ||
Right.OperatorIndex > 0) {
4697 if (
Left.ClosesTemplateDeclaration)
4698 return Style.PenaltyBreakTemplateDeclaration;
4699 if (
Left.ClosesRequiresClause)
4701 if (
Left.is(TT_ConditionalExpr))
4707 return Style.PenaltyBreakAssignment;
4714bool TokenAnnotator::spaceRequiredBeforeParens(
const FormatToken &Right)
const {
4717 if (
Right.is(TT_OverloadedOperatorLParen) &&
4718 Style.SpaceBeforeParensOptions.AfterOverloadedOperator) {
4721 if (Style.SpaceBeforeParensOptions.BeforeNonEmptyParentheses &&
4722 Right.ParameterCount > 0) {
4731 if (
Left.is(tok::kw_return) &&
4732 Right.isNoneOf(tok::semi, tok::r_paren, tok::hashhash)) {
4735 if (
Left.is(tok::kw_throw) &&
Right.is(tok::l_paren) &&
Right.MatchingParen &&
4736 Right.MatchingParen->is(TT_CastRParen)) {
4739 if (
Left.is(Keywords.kw_assert) && Style.isJava())
4742 Left.is(tok::objc_property)) {
4745 if (
Right.is(tok::hashhash))
4746 return Left.is(tok::hash);
4747 if (
Left.isOneOf(tok::hashhash, tok::hash))
4748 return Right.is(tok::hash);
4750 if (
Left.is(tok::l_paren) &&
Right.is(tok::r_paren))
4751 return Style.SpacesInParensOptions.InEmptyParentheses;
4752 if (Style.SpacesInParensOptions.ExceptDoubleParentheses &&
4753 Left.is(tok::r_paren) &&
Right.is(tok::r_paren)) {
4754 auto *InnerLParen =
Left.MatchingParen;
4755 if (InnerLParen && InnerLParen->Previous ==
Right.MatchingParen) {
4756 InnerLParen->SpacesRequiredBefore = 0;
4761 if (
Left.is(tok::l_paren))
4763 else if (
Right.is(tok::r_paren) &&
Right.MatchingParen)
4764 LeftParen =
Right.MatchingParen;
4765 if (LeftParen && (LeftParen->is(TT_ConditionLParen) ||
4766 (LeftParen->Previous &&
4767 isKeywordWithCondition(*LeftParen->Previous)))) {
4768 return Style.SpacesInParensOptions.InConditionalStatements;
4773 if (
Left.is(tok::kw_auto) &&
Right.isOneOf(TT_LambdaLBrace, TT_FunctionLBrace,
4775 TT_FunctionTypeLParen)) {
4780 if (
Left.is(tok::kw_auto) &&
Right.isOneOf(tok::l_paren, tok::l_brace))
4783 const auto *BeforeLeft =
Left.Previous;
4786 if (
Right.is(tok::l_paren) &&
Left.is(tok::kw_co_await) && BeforeLeft &&
4787 BeforeLeft->is(tok::kw_operator)) {
4791 if (
Left.isOneOf(tok::kw_co_await, tok::kw_co_yield, tok::kw_co_return) &&
4792 Right.isNoneOf(tok::semi, tok::r_paren)) {
4796 if (
Left.is(tok::l_paren) ||
Right.is(tok::r_paren)) {
4797 return (
Right.is(TT_CastRParen) ||
4798 (
Left.MatchingParen &&
Left.MatchingParen->is(TT_CastRParen)))
4799 ? Style.SpacesInParensOptions.InCStyleCasts
4800 : Style.SpacesInParensOptions.Other;
4802 if (
Right.isOneOf(tok::semi, tok::comma))
4805 bool IsLightweightGeneric =
Right.MatchingParen &&
4806 Right.MatchingParen->Next &&
4807 Right.MatchingParen->Next->is(tok::colon);
4808 return !IsLightweightGeneric && Style.ObjCSpaceBeforeProtocolList;
4810 if (
Right.is(tok::less) &&
Left.is(tok::kw_template))
4811 return Style.SpaceAfterTemplateKeyword;
4812 if (
Left.isOneOf(tok::exclaim, tok::tilde))
4814 if (
Left.is(tok::at) &&
4815 Right.isOneOf(tok::identifier, tok::string_literal, tok::char_constant,
4816 tok::numeric_constant, tok::l_paren, tok::l_brace,
4817 tok::kw_true, tok::kw_false)) {
4820 if (
Left.is(tok::colon))
4821 return Left.isNoneOf(TT_ObjCSelector, TT_ObjCMethodExpr);
4822 if (
Left.is(tok::coloncolon))
4824 if (
Left.is(tok::less) ||
Right.isOneOf(tok::greater, tok::less)) {
4825 if (Style.isTextProto() ||
4827 (
Left.is(TT_DictLiteral) ||
Right.is(TT_DictLiteral)))) {
4829 if (
Left.is(tok::less) &&
Right.is(tok::greater))
4834 if (
Right.isNot(TT_OverloadedOperatorLParen))
4837 if (
Right.is(tok::ellipsis)) {
4838 return Left.Tok.isLiteral() || (
Left.is(tok::identifier) && BeforeLeft &&
4839 BeforeLeft->is(tok::kw_case));
4841 if (
Left.is(tok::l_square) &&
Right.is(tok::amp))
4842 return Style.SpacesInSquareBrackets;
4843 if (
Right.is(TT_PointerOrReference)) {
4844 if (
Left.is(tok::r_paren) &&
Line.MightBeFunctionDecl) {
4845 if (!
Left.MatchingParen)
4848 Left.MatchingParen->getPreviousNonComment();
4849 if (!TokenBeforeMatchingParen ||
Left.isNot(TT_TypeDeclarationParen))
4857 (
Left.is(TT_AttributeRParen) ||
4858 Left.canBePointerOrReferenceQualifier())) {
4861 if (
Left.Tok.isLiteral())
4864 if (
Left.isTypeOrIdentifier(LangOpts) &&
Right.Next &&
Right.Next->Next &&
4865 Right.Next->Next->is(TT_RangeBasedForLoopColon)) {
4866 return getTokenPointerOrReferenceAlignment(Right) !=
4869 return Left.isNoneOf(TT_PointerOrReference, tok::l_paren) &&
4870 (getTokenPointerOrReferenceAlignment(Right) !=
4872 (
Line.IsMultiVariableDeclStmt &&
4873 (
Left.NestingLevel == 0 ||
4874 (
Left.NestingLevel == 1 && startsWithInitStatement(
Line)))));
4876 if (
Right.is(TT_FunctionTypeLParen) &&
Left.isNot(tok::l_paren) &&
4877 (
Left.isNot(TT_PointerOrReference) ||
4879 !
Line.IsMultiVariableDeclStmt))) {
4882 if (
Left.is(TT_PointerOrReference)) {
4887 Right.canBePointerOrReferenceQualifier()) {
4891 if (
Right.Tok.isLiteral())
4894 if (
Right.is(TT_BlockComment))
4898 if (
Right.isOneOf(Keywords.kw_override, Keywords.kw_final, tok::kw_noexcept,
4899 TT_RequiresClause) &&
4900 Right.isNot(TT_StartOfName)) {
4907 if (BeforeLeft && BeforeLeft->isTypeOrIdentifier(LangOpts) &&
Right.Next &&
4908 Right.Next->is(TT_RangeBasedForLoopColon)) {
4909 return getTokenPointerOrReferenceAlignment(Left) !=
4912 if (
Right.isOneOf(TT_PointerOrReference, TT_ArraySubscriptLSquare,
4924 if (
Line.IsMultiVariableDeclStmt &&
4925 (
Left.NestingLevel ==
Line.First->NestingLevel ||
4926 ((
Left.NestingLevel ==
Line.First->NestingLevel + 1) &&
4927 startsWithInitStatement(
Line)))) {
4932 if (BeforeLeft->is(tok::coloncolon)) {
4933 if (
Left.isNot(tok::star))
4936 if (!
Right.startsSequence(tok::identifier, tok::r_paren))
4939 const auto *LParen =
Right.Next->MatchingParen;
4940 return !LParen || LParen->isNot(TT_FunctionTypeLParen);
4942 return BeforeLeft->isNoneOf(tok::l_paren, tok::l_square);
4945 if (
Left.is(tok::ellipsis) && BeforeLeft &&
4946 BeforeLeft->isPointerOrReference()) {
4950 if (
Right.is(tok::star) &&
Left.is(tok::l_paren))
4952 if (
Left.is(tok::star) &&
Right.isPointerOrReference())
4954 if (
Right.isPointerOrReference()) {
4965 if (
Previous->is(tok::coloncolon)) {
4984 if (
Previous->endsSequence(tok::kw_operator))
4986 if (
Previous->isOneOf(tok::kw_const, tok::kw_volatile)) {
4988 (Style.SpaceAroundPointerQualifiers ==
4994 if (Style.isCSharp() &&
Left.is(Keywords.kw_is) &&
Right.is(tok::l_square))
4996 const auto SpaceRequiredForArrayInitializerLSquare =
4997 [](
const FormatToken &LSquareTok,
const FormatStyle &Style) {
4998 return Style.SpacesInContainerLiterals ||
5001 LSquareTok.endsSequence(tok::l_square, tok::colon,
5004 if (
Left.is(tok::l_square)) {
5005 return (
Left.is(TT_ArrayInitializerLSquare) &&
Right.isNot(tok::r_square) &&
5006 SpaceRequiredForArrayInitializerLSquare(Left, Style)) ||
5007 (
Left.isOneOf(TT_ArraySubscriptLSquare, TT_StructuredBindingLSquare,
5008 TT_LambdaLSquare) &&
5009 Style.SpacesInSquareBrackets &&
Right.isNot(tok::r_square));
5011 if (
Right.is(tok::r_square)) {
5012 return Right.MatchingParen &&
5013 ((
Right.MatchingParen->is(TT_ArrayInitializerLSquare) &&
5014 SpaceRequiredForArrayInitializerLSquare(*
Right.MatchingParen,
5016 (Style.SpacesInSquareBrackets &&
5017 Right.MatchingParen->isOneOf(TT_ArraySubscriptLSquare,
5018 TT_StructuredBindingLSquare,
5019 TT_LambdaLSquare)));
5021 if (
Right.is(tok::l_square) &&
5022 Right.isNoneOf(TT_ObjCMethodExpr, TT_LambdaLSquare,
5023 TT_DesignatedInitializerLSquare,
5024 TT_StructuredBindingLSquare, TT_AttributeLSquare) &&
5025 Left.isNoneOf(tok::numeric_constant, TT_DictLiteral) &&
5026 !(
Left.isNot(tok::r_square) && Style.SpaceBeforeSquareBrackets &&
5027 Right.is(TT_ArraySubscriptLSquare))) {
5031 (
Right.is(tok::r_brace) &&
Right.MatchingParen &&
5034 Style.SpacesInParensOptions.Other;
5036 if (
Left.is(TT_BlockComment)) {
5038 return Style.isJavaScript() || !
Left.TokenText.ends_with(
"=*/");
5043 if (
Left.is(TT_TemplateCloser) &&
Right.is(TT_AttributeLSquare))
5046 if (
Right.is(tok::l_paren)) {
5048 if (
Line.MightBeFunctionDecl &&
Right.is(TT_FunctionDeclarationLParen)) {
5049 if (spaceRequiredBeforeParens(Right))
5051 const auto &Options = Style.SpaceBeforeParensOptions;
5052 return Line.mightBeFunctionDefinition()
5053 ? Options.AfterFunctionDefinitionName
5054 : Options.AfterFunctionDeclarationName;
5056 if (
Left.is(TT_TemplateCloser) &&
Right.isNot(TT_FunctionTypeLParen))
5057 return spaceRequiredBeforeParens(Right);
5058 if (
Left.isOneOf(TT_RequiresClause,
5059 TT_RequiresClauseInARequiresExpression)) {
5060 return Style.SpaceBeforeParensOptions.AfterRequiresInClause ||
5061 spaceRequiredBeforeParens(Right);
5063 if (
Left.is(TT_RequiresExpression)) {
5064 return Style.SpaceBeforeParensOptions.AfterRequiresInExpression ||
5065 spaceRequiredBeforeParens(Right);
5067 if (
Left.isOneOf(TT_AttributeRParen, TT_AttributeRSquare))
5069 if (
Left.is(TT_ForEachMacro)) {
5070 return Style.SpaceBeforeParensOptions.AfterForeachMacros ||
5071 spaceRequiredBeforeParens(Right);
5073 if (
Left.is(TT_IfMacro)) {
5074 return Style.SpaceBeforeParensOptions.AfterIfMacros ||
5075 spaceRequiredBeforeParens(Right);
5078 Left.isPlacementOperator() &&
5079 Right.isNot(TT_OverloadedOperatorLParen) &&
5080 !(
Line.MightBeFunctionDecl &&
Left.is(TT_FunctionDeclarationName))) {
5081 const auto *RParen =
Right.MatchingParen;
5082 return Style.SpaceBeforeParensOptions.AfterPlacementOperator ||
5083 (RParen && RParen->is(TT_CastRParen));
5087 if (
Left.is(tok::semi))
5089 if (
Left.isOneOf(tok::pp_elif, tok::kw_for, tok::kw_while, tok::kw_switch,
5090 tok::kw_case, TT_ForEachMacro, TT_ObjCForIn) ||
5092 Right.is(TT_ConditionLParen)) {
5093 return Style.SpaceBeforeParensOptions.AfterControlStatements ||
5094 spaceRequiredBeforeParens(Right);
5099 if (
Right.is(TT_OverloadedOperatorLParen))
5100 return spaceRequiredBeforeParens(Right);
5104 Left.MatchingParen &&
Left.MatchingParen->is(TT_LambdaLSquare)) {
5105 return Style.SpaceBeforeParensOptions.AfterFunctionDefinitionName ||
5106 spaceRequiredBeforeParens(Right);
5108 if (!BeforeLeft || BeforeLeft->isNoneOf(tok::period, tok::arrow)) {
5109 if (
Left.isOneOf(tok::kw_try, Keywords.kw___except, tok::kw_catch)) {
5110 return Style.SpaceBeforeParensOptions.AfterControlStatements ||
5111 spaceRequiredBeforeParens(Right);
5113 if (
Left.isPlacementOperator() ||
5114 (
Left.is(tok::r_square) &&
Left.MatchingParen &&
5115 Left.MatchingParen->Previous &&
5116 Left.MatchingParen->Previous->is(tok::kw_delete))) {
5118 spaceRequiredBeforeParens(Right);
5122 if (
Tok.isNot(tok::l_paren))
5124 const auto *RParen =
Tok.MatchingParen;
5127 const auto *
Next = RParen->Next;
5134 (
Left.Tok.getIdentifierInfo() ||
Left.is(tok::r_paren))) {
5135 return spaceRequiredBeforeParens(Right);
5139 if (
Left.is(tok::at) &&
Right.isNot(tok::objc_not_keyword))
5141 if (
Right.is(TT_UnaryOperator)) {
5142 return Left.isNoneOf(tok::l_paren, tok::l_square, tok::at) &&
5143 (
Left.isNot(tok::colon) ||
Left.isNot(TT_ObjCMethodExpr));
5149 if (!Style.isVerilog() &&
5150 (
Left.isOneOf(tok::identifier, tok::greater, tok::r_square,
5152 Left.isTypeName(LangOpts)) &&
5153 Right.is(tok::l_brace) &&
Right.getNextNonComment() &&
5157 if (
Left.is(tok::period) ||
Right.is(tok::period))
5161 if (
Right.is(tok::hash) &&
Left.is(tok::identifier) &&
5162 (
Left.TokenText ==
"L" ||
Left.TokenText ==
"u" ||
5163 Left.TokenText ==
"U" ||
Left.TokenText ==
"u8" ||
5164 Left.TokenText ==
"LR" ||
Left.TokenText ==
"uR" ||
5165 Left.TokenText ==
"UR" ||
Left.TokenText ==
"u8R")) {
5168 if (
Left.is(TT_TemplateCloser) &&
Left.MatchingParen &&
5169 Left.MatchingParen->Previous &&
5170 Left.MatchingParen->Previous->isOneOf(tok::period, tok::coloncolon)) {
5176 if (
Left.is(TT_TemplateCloser) &&
Right.is(tok::l_square))
5178 if (
Left.is(tok::l_brace) &&
Left.endsSequence(TT_DictLiteral, tok::at)) {
5182 if (
Right.is(tok::r_brace) &&
Right.MatchingParen &&
5183 Right.MatchingParen->endsSequence(TT_DictLiteral, tok::at)) {
5187 if (
Right.is(TT_TrailingAnnotation) &&
Right.isOneOf(tok::amp, tok::ampamp) &&
5188 Left.isOneOf(tok::kw_const, tok::kw_volatile) &&
5189 (!
Right.Next ||
Right.Next->is(tok::semi))) {
5206 return Right.hasWhitespaceBefore();
5208 const bool IsVerilog = Style.isVerilog();
5209 assert(!IsVerilog || !IsCpp);
5212 if (Keywords.isWordLike(Right, IsVerilog) &&
5213 Keywords.isWordLike(Left, IsVerilog)) {
5219 if (
Left.is(tok::star) &&
Right.is(tok::comment))
5222 if (
Left.is(tok::l_brace) &&
Right.is(tok::r_brace) &&
5223 Left.Children.empty()) {
5228 Style.SpacesInParensOptions.InEmptyParentheses;
5233 const auto *BeforeLeft =
Left.Previous;
5236 if (
Left.is(TT_OverloadedOperator) &&
5237 Right.isOneOf(TT_TemplateOpener, TT_TemplateCloser)) {
5241 if (
Right.is(tok::period) &&
Left.is(tok::numeric_constant))
5245 if (
Left.is(Keywords.kw_import) &&
5246 Right.isOneOf(tok::less, tok::ellipsis) &&
5247 (!BeforeLeft || BeforeLeft->is(tok::kw_export))) {
5251 if (
Left.is(Keywords.kw_import) &&
Right.is(TT_ModulePartitionColon))
5254 if (
Right.is(TT_AfterPPDirective))
5258 if (
Left.is(tok::identifier) &&
Right.is(TT_ModulePartitionColon))
5261 if (
Left.is(TT_ModulePartitionColon) &&
Right.is(tok::identifier))
5263 if (
Left.is(tok::ellipsis) &&
Right.is(tok::identifier) &&
5264 Line.First->is(Keywords.kw_import)) {
5268 if (
Left.isOneOf(TT_AttributeRParen, TT_AttributeMacro) &&
5269 Right.is(tok::coloncolon)) {
5273 if (
Left.is(tok::kw_operator))
5274 return Right.is(tok::coloncolon) || Style.SpaceAfterOperatorKeyword;
5276 !
Left.opensScope() && Style.SpaceBeforeCpp11BracedList) {
5279 if (
Left.is(tok::less) &&
Left.is(TT_OverloadedOperator) &&
5280 Right.is(TT_TemplateOpener)) {
5284 if (
Left.is(tok::identifier) &&
Right.is(tok::numeric_constant))
5285 return Right.TokenText[0] !=
'.';
5287 if (
Left.Tok.getIdentifierInfo() &&
Right.Tok.isLiteral())
5289 }
else if (Style.isProto()) {
5290 if (
Right.is(tok::period) && !(BeforeLeft && BeforeLeft->is(tok::period)) &&
5291 Left.isOneOf(Keywords.kw_optional, Keywords.kw_required,
5292 Keywords.kw_repeated, Keywords.kw_extend)) {
5295 if (
Right.is(tok::l_paren) &&
5296 Left.isOneOf(Keywords.kw_returns, Keywords.kw_option)) {
5299 if (
Right.isOneOf(tok::l_brace, tok::less) &&
Left.is(TT_SelectorName))
5302 if (
Left.is(tok::slash) ||
Right.is(tok::slash))
5304 if (
Left.MatchingParen &&
5305 Left.MatchingParen->is(TT_ProtoExtensionLSquare) &&
5306 Right.isOneOf(tok::l_brace, tok::less)) {
5310 if (
Left.is(tok::percent))
5314 if (
Left.is(tok::numeric_constant) &&
Right.is(tok::percent))
5315 return Right.hasWhitespaceBefore();
5316 }
else if (Style.isJson()) {
5317 if (
Right.is(tok::colon) &&
Left.is(tok::string_literal))
5318 return Style.SpaceBeforeJsonColon;
5319 }
else if (Style.isCSharp()) {
5325 if (
Left.is(tok::kw_this) &&
Right.is(tok::l_square))
5329 if (
Left.is(tok::kw_new) &&
Right.is(tok::l_paren))
5333 if (
Right.is(tok::l_brace))
5337 if (
Left.is(tok::l_brace) &&
Right.isNot(tok::r_brace))
5340 if (
Left.isNot(tok::l_brace) &&
Right.is(tok::r_brace))
5344 if (
Left.is(TT_FatArrow) ||
Right.is(TT_FatArrow))
5348 if (
Left.is(TT_AttributeColon) ||
Right.is(TT_AttributeColon))
5352 if (
Left.is(TT_TemplateCloser) &&
Right.is(TT_StartOfName))
5356 if (
Left.is(tok::l_square) ||
Right.is(tok::r_square))
5357 return Style.SpacesInSquareBrackets;
5360 if (
Right.is(TT_CSharpNullable))
5364 if (
Right.is(TT_NonNullAssertion))
5368 if (
Left.is(tok::comma) &&
Right.is(tok::comma))
5372 if (
Left.is(Keywords.kw_var) &&
Right.is(tok::l_paren))
5376 if (
Right.is(tok::l_paren)) {
5377 if (
Left.isOneOf(tok::kw_using, Keywords.kw_async, Keywords.kw_when,
5378 Keywords.kw_lock)) {
5379 return Style.SpaceBeforeParensOptions.AfterControlStatements ||
5380 spaceRequiredBeforeParens(Right);
5386 if ((
Left.isAccessSpecifierKeyword() ||
5387 Left.isOneOf(tok::kw_virtual, tok::kw_extern, tok::kw_static,
5388 Keywords.kw_internal, Keywords.kw_abstract,
5389 Keywords.kw_sealed, Keywords.kw_override,
5390 Keywords.kw_async, Keywords.kw_unsafe)) &&
5391 Right.is(tok::l_paren)) {
5394 }
else if (Style.isJavaScript()) {
5395 if (
Left.is(TT_FatArrow))
5398 if (
Right.is(tok::l_paren) &&
Left.is(Keywords.kw_await) && BeforeLeft &&
5399 BeforeLeft->is(tok::kw_for)) {
5402 if (
Left.is(Keywords.kw_async) &&
Right.is(tok::l_paren) &&
5403 Right.MatchingParen) {
5410 if ((
Left.is(TT_TemplateString) &&
Left.TokenText.ends_with(
"${")) ||
5411 (
Right.is(TT_TemplateString) &&
Right.TokenText.starts_with(
"}"))) {
5416 if (Keywords.isJavaScriptIdentifier(Left,
5418 Right.is(TT_TemplateString)) {
5421 if (
Right.is(tok::star) &&
5422 Left.isOneOf(Keywords.kw_function, Keywords.kw_yield)) {
5425 if (
Right.isOneOf(tok::l_brace, tok::l_square) &&
5426 Left.isOneOf(Keywords.kw_function, Keywords.kw_yield,
5427 Keywords.kw_extends, Keywords.kw_implements)) {
5430 if (
Right.is(tok::l_paren)) {
5432 if (
Line.MustBeDeclaration &&
Left.Tok.getIdentifierInfo())
5436 if (BeforeLeft && BeforeLeft->is(tok::period) &&
5437 Left.Tok.getIdentifierInfo()) {
5441 if (
Left.isOneOf(tok::kw_throw, Keywords.kw_await, Keywords.kw_typeof,
5447 if (
Left.endsSequence(tok::kw_const, Keywords.kw_as))
5449 if ((
Left.isOneOf(Keywords.kw_let, Keywords.kw_var, Keywords.kw_in,
5454 (
Left.is(Keywords.kw_of) && BeforeLeft &&
5455 BeforeLeft->isOneOf(tok::identifier, tok::r_square, tok::r_brace))) &&
5456 (!BeforeLeft || BeforeLeft->isNot(tok::period))) {
5459 if (
Left.isOneOf(tok::kw_for, Keywords.kw_as) && BeforeLeft &&
5460 BeforeLeft->is(tok::period) &&
Right.is(tok::l_paren)) {
5463 if (
Left.is(Keywords.kw_as) &&
5464 Right.isOneOf(tok::l_square, tok::l_brace, tok::l_paren)) {
5467 if (
Left.is(tok::kw_default) && BeforeLeft &&
5468 BeforeLeft->is(tok::kw_export)) {
5471 if (
Left.is(Keywords.kw_is) &&
Right.is(tok::l_brace))
5473 if (
Right.isOneOf(TT_JsTypeColon, TT_JsTypeOptionalQuestion))
5475 if (
Left.is(TT_JsTypeOperator) ||
Right.is(TT_JsTypeOperator))
5477 if ((
Left.is(tok::l_brace) ||
Right.is(tok::r_brace)) &&
5478 Line.First->isOneOf(Keywords.kw_import, tok::kw_export)) {
5481 if (
Left.is(tok::ellipsis))
5483 if (
Left.is(TT_TemplateCloser) &&
5484 Right.isNoneOf(tok::equal, tok::l_brace, tok::comma, tok::l_square,
5485 Keywords.kw_implements, Keywords.kw_extends)) {
5491 if (
Right.is(TT_NonNullAssertion))
5493 if (
Left.is(TT_NonNullAssertion) &&
5494 Right.isOneOf(Keywords.kw_as, Keywords.kw_in)) {
5497 }
else if (Style.isJava()) {
5498 if (
Left.is(TT_CaseLabelArrow) ||
Right.is(TT_CaseLabelArrow))
5500 if (
Left.is(tok::r_square) &&
Right.is(tok::l_brace))
5503 if (
Left.is(tok::l_square) ||
Right.is(tok::r_square))
5504 return Style.SpacesInSquareBrackets;
5506 if (
Left.is(Keywords.kw_synchronized) &&
Right.is(tok::l_paren)) {
5507 return Style.SpaceBeforeParensOptions.AfterControlStatements ||
5508 spaceRequiredBeforeParens(Right);
5510 if ((
Left.isAccessSpecifierKeyword() ||
5511 Left.isOneOf(tok::kw_static, Keywords.kw_final, Keywords.kw_abstract,
5512 Keywords.kw_native)) &&
5513 Right.is(TT_TemplateOpener)) {
5516 }
else if (IsVerilog) {
5518 if (
Left.is(tok::identifier) &&
Left.TokenText[0] ==
'\\')
5522 if ((
Left.is(TT_VerilogTableItem) &&
5523 Right.isNoneOf(tok::r_paren, tok::semi)) ||
5524 (
Right.is(TT_VerilogTableItem) &&
Left.isNot(tok::l_paren))) {
5526 return !(
Next &&
Next->is(tok::r_paren));
5529 if (
Left.isNot(TT_BinaryOperator) &&
5530 Left.isOneOf(Keywords.kw_verilogHash, Keywords.kw_verilogHashHash)) {
5534 if (
Right.isNot(tok::semi) &&
5535 (
Left.endsSequence(tok::numeric_constant, Keywords.kw_verilogHash) ||
5536 Left.endsSequence(tok::numeric_constant,
5537 Keywords.kw_verilogHashHash) ||
5538 (
Left.is(tok::r_paren) &&
Left.MatchingParen &&
5539 Left.MatchingParen->endsSequence(tok::l_paren, tok::at)))) {
5544 if (
Left.is(Keywords.kw_apostrophe) ||
5545 (
Left.is(TT_VerilogNumberBase) &&
Right.is(tok::numeric_constant))) {
5549 if (
Left.is(tok::arrow) ||
Right.is(tok::arrow))
5554 if (
Left.is(tok::at) &&
Right.isOneOf(tok::l_paren, tok::star, tok::at))
5557 if (
Right.is(tok::l_square) &&
5558 Left.isOneOf(TT_VerilogDimensionedTypeName, Keywords.kw_function)) {
5562 if (
Right.isOneOf(tok::period, Keywords.kw_apostrophe) &&
5563 Keywords.isVerilogIdentifier(Left) &&
Left.getPreviousNonComment() &&
5564 Left.getPreviousNonComment()->is(Keywords.kw_tagged)) {
5570 if ((
Right.is(Keywords.kw_apostrophe) ||
5572 Left.isNoneOf(Keywords.kw_assign, Keywords.kw_unique) &&
5573 !Keywords.isVerilogWordOperator(Left) &&
5574 (
Left.isOneOf(tok::r_square, tok::r_paren, tok::r_brace,
5575 tok::numeric_constant) ||
5576 Keywords.isWordLike(Left))) {
5580 if ((
Right.is(tok::star) &&
Left.is(tok::coloncolon)) ||
5581 (
Left.is(tok::star) &&
Right.is(tok::semi))) {
5585 if (
Left.endsSequence(tok::star, tok::l_paren) &&
Right.is(tok::identifier))
5588 if (
Right.is(tok::l_paren) &&
Right.is(TT_VerilogStrength))
5591 if ((
Left.is(tok::l_brace) &&
5592 Right.isOneOf(tok::lessless, tok::greatergreater)) ||
5593 (
Left.endsSequence(tok::lessless, tok::l_brace) ||
5594 Left.endsSequence(tok::greatergreater, tok::l_brace))) {
5597 }
else if (Style.isTableGen()) {
5599 if (
Left.is(tok::l_square) &&
Right.is(tok::l_brace))
5601 if (
Left.is(tok::r_brace) &&
Right.is(tok::r_square))
5604 if (
Right.isOneOf(TT_TableGenDAGArgListColon,
5605 TT_TableGenDAGArgListColonToAlign) ||
5606 Left.isOneOf(TT_TableGenDAGArgListColon,
5607 TT_TableGenDAGArgListColonToAlign)) {
5610 if (
Right.is(TT_TableGenCondOperatorColon))
5612 if (
Left.isOneOf(TT_TableGenDAGArgOperatorID,
5613 TT_TableGenDAGArgOperatorToBreak) &&
5614 Right.isNot(TT_TableGenDAGArgCloser)) {
5618 if (
Right.isOneOf(tok::l_paren, tok::less) &&
5619 Left.isOneOf(TT_TableGenBangOperator, TT_TableGenCondOperator)) {
5624 if (
Left.is(TT_TableGenTrailingPasteOperator) &&
5625 Right.isOneOf(tok::l_brace, tok::colon)) {
5629 if (
Left.is(tok::hash) ||
Right.is(tok::hash))
5632 if (Keywords.isTableGenDefinition(Left))
5636 if (
Left.is(TT_ImplicitStringLiteral))
5637 return Right.hasWhitespaceBefore();
5639 if (
Left.is(TT_ObjCMethodSpecifier))
5640 return Style.ObjCSpaceAfterMethodDeclarationPrefix;
5641 if (
Left.is(tok::r_paren) &&
Left.isNot(TT_AttributeRParen) &&
5642 canBeObjCSelectorComponent(Right)) {
5650 (
Right.is(tok::equal) ||
Left.is(tok::equal))) {
5654 if (
Right.isOneOf(TT_TrailingReturnArrow, TT_LambdaArrow) ||
5655 Left.isOneOf(TT_TrailingReturnArrow, TT_LambdaArrow)) {
5658 if (
Left.is(tok::comma) &&
Right.isNot(TT_OverloadedOperatorLParen) &&
5661 (
Left.Children.empty() || !
Left.MacroParent)) {
5664 if (
Right.is(tok::comma))
5666 if (
Right.is(TT_ObjCBlockLParen))
5668 if (
Right.is(TT_CtorInitializerColon))
5669 return Style.SpaceBeforeCtorInitializerColon;
5670 if (
Right.is(TT_InheritanceColon) && !Style.SpaceBeforeInheritanceColon)
5672 if (
Right.is(TT_EnumUnderlyingTypeColon) &&
5673 !Style.SpaceBeforeEnumUnderlyingTypeColon) {
5676 if (
Right.is(TT_RangeBasedForLoopColon) &&
5677 !Style.SpaceBeforeRangeBasedForLoopColon) {
5680 if (
Left.is(TT_BitFieldColon)) {
5684 if (
Right.is(tok::colon)) {
5685 if (
Right.is(TT_CaseLabelColon))
5686 return Style.SpaceBeforeCaseColon;
5687 if (
Right.is(TT_GotoLabelColon))
5690 if (!
Right.getNextNonComment())
5692 if (
Right.isOneOf(TT_ObjCSelector, TT_ObjCMethodExpr))
5694 if (
Left.is(tok::question))
5696 if (
Right.is(TT_InlineASMColon) &&
Left.is(tok::coloncolon))
5698 if (
Right.is(TT_DictLiteral))
5699 return Style.SpacesInContainerLiterals;
5700 if (
Right.is(TT_AttributeColon))
5702 if (
Right.is(TT_CSharpNamedArgumentColon))
5704 if (
Right.is(TT_GenericSelectionColon))
5706 if (
Right.is(TT_BitFieldColon)) {
5713 if ((
Left.isOneOf(tok::minus, tok::minusminus) &&
5714 Right.isOneOf(tok::minus, tok::minusminus)) ||
5715 (
Left.isOneOf(tok::plus, tok::plusplus) &&
5716 Right.isOneOf(tok::plus, tok::plusplus))) {
5719 if (
Left.is(TT_UnaryOperator)) {
5722 if (
Left.is(tok::amp) &&
Right.is(tok::r_square))
5723 return Style.SpacesInSquareBrackets;
5724 if (
Left.isNot(tok::exclaim))
5726 if (
Left.TokenText ==
"!")
5727 return Style.SpaceAfterLogicalNot;
5728 assert(
Left.TokenText ==
"not");
5729 return Right.isOneOf(tok::coloncolon, TT_UnaryOperator) ||
5730 (
Right.is(tok::l_paren) && Style.SpaceBeforeParensOptions.AfterNot);
5735 if (
Left.is(TT_CastRParen)) {
5736 return Style.SpaceAfterCStyleCast ||
5737 Right.isOneOf(TT_BinaryOperator, TT_SelectorName);
5740 auto ShouldAddSpacesInAngles = [
this, &
Right]() {
5744 return Right.hasWhitespaceBefore();
5748 if (
Left.is(tok::greater) &&
Right.is(tok::greater)) {
5749 if (Style.isTextProto() ||
5753 return Right.is(TT_TemplateCloser) &&
Left.is(TT_TemplateCloser) &&
5755 ShouldAddSpacesInAngles());
5757 if (
Right.isOneOf(tok::arrow, tok::arrowstar, tok::periodstar) ||
5758 Left.isOneOf(tok::arrow, tok::period, tok::arrowstar, tok::periodstar) ||
5759 (
Right.is(tok::period) &&
Right.isNot(TT_DesignatedInitializerPeriod))) {
5762 if (!Style.SpaceBeforeAssignmentOperators &&
Left.isNot(TT_TemplateCloser) &&
5766 if (Style.isJava() &&
Right.is(tok::coloncolon) &&
5767 Left.isOneOf(tok::identifier, tok::kw_this)) {
5770 if (
Right.is(tok::coloncolon) &&
Left.is(tok::identifier)) {
5772 return Left.isPossibleMacro(
true) &&
5773 Right.hasWhitespaceBefore();
5775 if (
Right.is(tok::coloncolon) &&
5776 Left.isNoneOf(tok::l_brace, tok::comment, tok::l_paren)) {
5778 return (
Left.is(TT_TemplateOpener) &&
5780 ShouldAddSpacesInAngles())) ||
5781 Left.isNoneOf(tok::l_paren, tok::r_paren, tok::l_square,
5782 tok::kw___super, TT_TemplateOpener,
5783 TT_TemplateCloser) ||
5784 (
Left.is(tok::l_paren) && Style.SpacesInParensOptions.Other);
5786 if ((
Left.is(TT_TemplateOpener)) != (
Right.is(TT_TemplateCloser)))
5787 return ShouldAddSpacesInAngles();
5788 if (
Left.is(tok::r_paren) &&
Left.isNot(TT_TypeDeclarationParen) &&
5789 Right.is(TT_PointerOrReference) &&
Right.isOneOf(tok::amp, tok::ampamp)) {
5793 if (
Right.is(TT_StructuredBindingLSquare)) {
5794 return Left.isNoneOf(tok::amp, tok::ampamp) ||
5798 if (
Right.Next &&
Right.Next->is(TT_StructuredBindingLSquare) &&
5799 Right.isOneOf(tok::amp, tok::ampamp)) {
5802 if ((
Right.is(TT_BinaryOperator) &&
Left.isNot(tok::l_paren)) ||
5803 (
Left.isOneOf(TT_BinaryOperator, TT_EnumEqual, TT_ConditionalExpr) &&
5804 Right.isNot(tok::r_paren))) {
5807 if (
Right.is(TT_TemplateOpener) &&
Left.is(tok::r_paren) &&
5808 Left.MatchingParen &&
5809 Left.MatchingParen->is(TT_OverloadedOperatorLParen)) {
5812 if (
Right.is(tok::less) &&
Left.isNot(tok::l_paren) &&
5816 if (
Right.is(TT_TrailingUnaryOperator))
5818 if (
Left.is(TT_RegexLiteral))
5820 return spaceRequiredBetween(
Line, Left, Right);
5826 Tok.isNoneOf(TT_ObjCBlockLBrace, TT_LambdaLBrace, TT_DictLiteral);
5831 return Tok.MatchingParen &&
Tok.MatchingParen->Next &&
5832 Tok.MatchingParen->Next->isOneOf(tok::comma, tok::r_paren,
5844 Tok.isNoneOf(TT_ObjCBlockLBrace, TT_DictLiteral);
5847bool TokenAnnotator::mustBreakBefore(AnnotatedLine &
Line,
5849 if (Right.NewlinesBefore > 1 && Style.MaxEmptyLinesToKeep > 0 &&
5850 (!Style.RemoveEmptyLinesInUnwrappedLines || &Right ==
Line.First)) {
5856 if (Style.BreakFunctionDeclarationParameters &&
Line.MightBeFunctionDecl &&
5857 !
Line.mightBeFunctionDefinition() && Left.MightBeFunctionDeclParen &&
5858 Left.ParameterCount > 0) {
5862 if (Style.BreakFunctionDefinitionParameters &&
Line.MightBeFunctionDecl &&
5863 Line.mightBeFunctionDefinition() &&
Left.MightBeFunctionDeclParen &&
5864 Left.ParameterCount > 0) {
5871 Line.MightBeFunctionDecl && !
Left.opensScope() &&
5876 const auto *BeforeLeft =
Left.Previous;
5877 const auto *AfterRight =
Right.Next;
5879 if (Style.isCSharp()) {
5880 if (
Left.is(TT_FatArrow) &&
Right.is(tok::l_brace) &&
5881 Style.BraceWrapping.AfterFunction) {
5884 if (
Right.is(TT_CSharpNamedArgumentColon) ||
5885 Left.is(TT_CSharpNamedArgumentColon)) {
5888 if (
Right.is(TT_CSharpGenericTypeConstraint))
5890 if (AfterRight && AfterRight->is(TT_FatArrow) &&
5891 (
Right.is(tok::numeric_constant) ||
5892 (
Right.is(tok::identifier) &&
Right.TokenText ==
"_"))) {
5897 if (
Left.is(TT_AttributeRSquare) &&
5898 (
Right.isAccessSpecifier(
false) ||
5899 Right.is(Keywords.kw_internal))) {
5903 if (
Left.is(TT_AttributeRSquare) &&
Right.is(TT_AttributeLSquare))
5905 }
else if (Style.isJavaScript()) {
5907 if (
Right.is(tok::string_literal) &&
Left.is(tok::plus) && BeforeLeft &&
5908 BeforeLeft->is(tok::string_literal)) {
5911 if (
Left.is(TT_DictLiteral) &&
Left.is(tok::l_brace) &&
Line.Level == 0 &&
5912 BeforeLeft && BeforeLeft->is(tok::equal) &&
5913 Line.First->isOneOf(tok::identifier, Keywords.kw_import, tok::kw_export,
5917 Line.First->isNoneOf(Keywords.kw_var, Keywords.kw_let)) {
5922 if (
Left.is(tok::l_brace) &&
Line.Level == 0 &&
5923 (
Line.startsWith(tok::kw_enum) ||
5924 Line.startsWith(tok::kw_const, tok::kw_enum) ||
5925 Line.startsWith(tok::kw_export, tok::kw_enum) ||
5926 Line.startsWith(tok::kw_export, tok::kw_const, tok::kw_enum))) {
5931 if (
Right.is(tok::r_brace) &&
Left.is(tok::l_brace) && BeforeLeft &&
5932 BeforeLeft->is(TT_FatArrow)) {
5934 switch (Style.AllowShortLambdasOnASingleLine) {
5940 return !
Left.Children.empty();
5944 return (
Left.NestingLevel == 0 &&
Line.Level == 0) &&
5945 !
Left.Children.empty();
5947 llvm_unreachable(
"Unknown FormatStyle::ShortLambdaStyle enum");
5950 if (
Right.is(tok::r_brace) &&
Left.is(tok::l_brace) &&
5951 !
Left.Children.empty()) {
5953 if (
Left.NestingLevel == 0 &&
Line.Level == 0)
5954 return !Style.AllowShortFunctionsOnASingleLine.Other;
5956 return !Style.AllowShortFunctionsOnASingleLine.Inline;
5958 }
else if (Style.isJava()) {
5959 if (
Right.is(tok::plus) &&
Left.is(tok::string_literal) && AfterRight &&
5960 AfterRight->is(tok::string_literal)) {
5963 }
else if (Style.isVerilog()) {
5965 if (
Left.is(TT_VerilogAssignComma))
5968 if (
Left.is(TT_VerilogTypeComma))
5972 if (Style.VerilogBreakBetweenInstancePorts &&
5973 (
Left.is(TT_VerilogInstancePortComma) ||
5974 (
Left.is(tok::r_paren) && Keywords.isVerilogIdentifier(Right) &&
5975 Left.MatchingParen &&
5976 Left.MatchingParen->is(TT_VerilogInstancePortLParen)))) {
5981 if (!Keywords.isVerilogBegin(Right) && Keywords.isVerilogEndOfLabel(Left))
5983 }
else if (Style.BreakAdjacentStringLiterals &&
5984 (IsCpp || Style.isProto() || Style.isTableGen())) {
5985 if (
Left.isStringLiteral() &&
Right.isStringLiteral())
5990 if (Style.isJson()) {
5994 if (
Left.is(TT_DictLiteral) &&
Left.is(tok::l_brace))
5997 if ((
Left.is(TT_ArrayInitializerLSquare) &&
Left.is(tok::l_square) &&
5998 Right.isNot(tok::r_square)) ||
5999 Left.is(tok::comma)) {
6000 if (
Right.is(tok::l_brace))
6005 if (
Tok->isOneOf(tok::l_brace, tok::l_square))
6007 if (
Tok->isOneOf(tok::r_brace, tok::r_square))
6010 return Style.BreakArrays;
6012 }
else if (Style.isTableGen()) {
6016 if (
Left.is(TT_TableGenCondOperatorComma))
6018 if (
Left.is(TT_TableGenDAGArgOperatorToBreak) &&
6019 Right.isNot(TT_TableGenDAGArgCloser)) {
6022 if (
Left.is(TT_TableGenDAGArgListCommaToBreak))
6024 if (
Right.is(TT_TableGenDAGArgCloser) &&
Right.MatchingParen &&
6025 Right.MatchingParen->is(TT_TableGenDAGArgOpenerToBreak) &&
6026 &Left !=
Right.MatchingParen->Next) {
6032 if (
Line.startsWith(tok::kw_asm) &&
Right.is(TT_InlineASMColon) &&
6043 if ((
Left.isOneOf(tok::l_brace, TT_ArrayInitializerLSquare) ||
6044 (Style.isJavaScript() &&
Left.is(tok::l_paren))) &&
6046 BeforeClosingBrace =
Left.MatchingParen->Previous;
6047 }
else if (
Right.MatchingParen &&
6048 (
Right.MatchingParen->isOneOf(tok::l_brace,
6049 TT_ArrayInitializerLSquare) ||
6050 (Style.isJavaScript() &&
6051 Right.MatchingParen->is(tok::l_paren)))) {
6052 BeforeClosingBrace = &
Left;
6054 if (BeforeClosingBrace && (BeforeClosingBrace->is(tok::comma) ||
6055 BeforeClosingBrace->isTrailingComment())) {
6060 if (
Right.is(tok::comment)) {
6062 Right.NewlinesBefore > 0 &&
Right.HasUnescapedNewline;
6064 if (
Left.isTrailingComment())
6066 if (
Left.IsUnterminatedLiteral)
6069 if (BeforeLeft && BeforeLeft->is(tok::lessless) &&
6070 Left.is(tok::string_literal) &&
Right.is(tok::lessless) && AfterRight &&
6071 AfterRight->is(tok::string_literal)) {
6072 return Right.NewlinesBefore > 0;
6075 if (
Right.is(TT_RequiresClause)) {
6076 switch (Style.RequiresClausePosition) {
6086 if (
Left.ClosesTemplateDeclaration &&
Left.MatchingParen &&
6087 Left.MatchingParen->NestingLevel == 0) {
6091 if (
Right.is(tok::kw_concept))
6095 Right.NewlinesBefore > 0);
6097 if (
Left.ClosesRequiresClause) {
6098 switch (Style.RequiresClausePosition) {
6101 return Right.isNot(tok::semi);
6103 return Right.isNoneOf(tok::semi, tok::l_brace);
6110 (
Left.is(TT_CtorInitializerComma) ||
6111 Right.is(TT_CtorInitializerColon))) {
6116 Left.isOneOf(TT_CtorInitializerColon, TT_CtorInitializerComma)) {
6121 Left.is(TT_CtorInitializerComma)) {
6127 Right.isOneOf(TT_CtorInitializerComma, TT_CtorInitializerColon)) {
6133 Right.is(TT_CtorInitializerColon)) {
6138 Left.is(TT_CtorInitializerColon)) {
6144 Right.is(TT_InheritanceComma)) {
6148 Left.is(TT_InheritanceComma)) {
6151 if (
Right.is(tok::string_literal) &&
Right.TokenText.starts_with(
"R\"")) {
6155 return Right.IsMultiline &&
Right.NewlinesBefore > 0;
6157 if ((
Left.is(tok::l_brace) ||
6158 (
Left.is(tok::less) && BeforeLeft && BeforeLeft->is(tok::equal))) &&
6164 if (
Right.is(TT_InlineASMBrace))
6165 return Right.HasUnescapedNewline;
6168 auto *FirstNonComment =
Line.getFirstNonComment();
6170 FirstNonComment && (FirstNonComment->is(Keywords.kw_internal) ||
6171 FirstNonComment->isAccessSpecifierKeyword());
6173 if (Style.BraceWrapping.AfterEnum) {
6174 if (
Line.startsWith(tok::kw_enum) ||
6175 Line.startsWith(tok::kw_typedef, tok::kw_enum) ||
6176 Line.startsWith(tok::kw_export, tok::kw_enum)) {
6181 FirstNonComment->Next->is(tok::kw_enum)) {
6187 if (Style.BraceWrapping.AfterClass &&
6189 FirstNonComment->Next->is(Keywords.kw_interface)) ||
6190 Line.startsWith(Keywords.kw_interface))) {
6195 if (
Right.isNot(TT_FunctionLBrace)) {
6197 ((
Line.startsWith(tok::kw_class) &&
6198 Style.BraceWrapping.AfterClass) ||
6199 (
Line.startsWith(tok::kw_struct) &&
6200 Style.BraceWrapping.AfterStruct) ||
6201 (
Line.startsWith(tok::kw_union) &&
6202 Style.BraceWrapping.AfterUnion));
6206 if (
Left.is(TT_ObjCBlockLBrace) &&
6212 if (
Left.isOneOf(TT_AttributeRParen, TT_AttributeMacro) &&
6213 Right.is(TT_ObjCDecl)) {
6217 if (
Left.is(TT_LambdaLBrace)) {
6225 (!
Left.Children.empty() &&
6231 if (Style.BraceWrapping.BeforeLambdaBody &&
Right.is(TT_LambdaLBrace) &&
6232 (
Left.isPointerOrReference() ||
Left.is(TT_TemplateCloser))) {
6237 if ((Style.isJava() || Style.isJavaScript()) &&
6238 Left.is(TT_LeadingJavaAnnotation) &&
6239 Right.isNoneOf(TT_LeadingJavaAnnotation, tok::l_paren) &&
6240 (
Line.Last->is(tok::l_brace) || Style.BreakAfterJavaFieldAnnotations)) {
6244 if (
Right.is(TT_ProtoExtensionLSquare))
6274 if (Style.isProto() &&
Right.is(TT_SelectorName) &&
6275 Right.isNot(tok::r_square) && AfterRight) {
6278 if (
Left.is(tok::at))
6284 const auto *LBrace = AfterRight;
6285 if (LBrace && LBrace->is(tok::colon)) {
6286 LBrace = LBrace->Next;
6287 if (LBrace && LBrace->is(tok::at)) {
6288 LBrace = LBrace->Next;
6290 LBrace = LBrace->Next;
6302 ((LBrace->is(tok::l_brace) &&
6303 (LBrace->is(TT_DictLiteral) ||
6304 (LBrace->Next && LBrace->Next->is(tok::r_brace)))) ||
6305 LBrace->isOneOf(TT_ArrayInitializerLSquare, tok::less))) {
6312 if (
Left.ParameterCount == 0)
6327 if (
Left.isOneOf(tok::r_brace, tok::greater, tok::r_square))
6332 Left.is(TT_AttributeRSquare) &&
Right.NewlinesBefore > 0) {
6333 Line.ReturnTypeWrapped =
true;
6344 if (Style.isCSharp()) {
6345 if (
Left.isOneOf(TT_CSharpNamedArgumentColon, TT_AttributeColon) ||
6346 Right.isOneOf(TT_CSharpNamedArgumentColon, TT_AttributeColon)) {
6350 if (
Line.First->is(TT_CSharpGenericTypeConstraint))
6351 return Left.is(TT_CSharpGenericTypeConstraintComma);
6353 if (
Right.is(TT_CSharpNullable))
6355 }
else if (Style.isJava()) {
6356 if (
Left.isOneOf(Keywords.kw_throws, Keywords.kw_extends,
6357 Keywords.kw_implements)) {
6360 if (
Right.isOneOf(Keywords.kw_throws, Keywords.kw_extends,
6361 Keywords.kw_implements)) {
6364 }
else if (Style.isJavaScript()) {
6367 (NonComment->isAccessSpecifierKeyword() ||
6368 NonComment->isOneOf(
6369 tok::kw_return, Keywords.kw_yield, tok::kw_continue, tok::kw_break,
6370 tok::kw_throw, Keywords.kw_interface, Keywords.kw_type,
6371 tok::kw_static, Keywords.kw_readonly, Keywords.kw_override,
6372 Keywords.kw_abstract, Keywords.kw_get, Keywords.kw_set,
6373 Keywords.kw_async, Keywords.kw_await))) {
6376 if (
Right.NestingLevel == 0 &&
6377 (
Left.Tok.getIdentifierInfo() ||
6378 Left.isOneOf(tok::r_square, tok::r_paren)) &&
6379 Right.isOneOf(tok::l_square, tok::l_paren)) {
6382 if (NonComment && NonComment->is(tok::identifier) &&
6383 NonComment->TokenText ==
"asserts") {
6386 if (
Left.is(TT_FatArrow) &&
Right.is(tok::l_brace))
6388 if (
Left.is(TT_JsTypeColon))
6391 if (
Left.is(tok::exclaim) &&
Right.is(tok::colon))
6396 if (
Right.is(Keywords.kw_is)) {
6405 if (!
Next ||
Next->isNot(tok::colon))
6408 if (
Left.is(Keywords.kw_in))
6410 if (
Right.is(Keywords.kw_in))
6412 if (
Right.is(Keywords.kw_as))
6414 if (
Right.isOneOf(Keywords.kw_extends, Keywords.kw_infer)) {
6420 if (
Left.is(Keywords.kw_as))
6422 if (
Left.is(TT_NonNullAssertion))
6424 if (
Left.is(Keywords.kw_declare) &&
6425 Right.isOneOf(Keywords.kw_module, tok::kw_namespace,
6426 Keywords.kw_function, tok::kw_class, tok::kw_enum,
6427 Keywords.kw_interface, Keywords.kw_type, Keywords.kw_var,
6428 Keywords.kw_let, tok::kw_const)) {
6433 if (
Left.isOneOf(Keywords.kw_module, tok::kw_namespace) &&
6434 Right.isOneOf(tok::identifier, tok::string_literal)) {
6437 if (
Right.is(TT_TemplateString) &&
Right.closesScope())
6441 if (
Left.is(tok::identifier) &&
Right.is(TT_TemplateString))
6443 if (
Left.is(TT_TemplateString) &&
Left.opensScope())
6445 }
else if (Style.isTableGen()) {
6447 if (Keywords.isTableGenDefinition(Left))
6450 if (
Right.is(tok::l_paren)) {
6451 return Left.isNoneOf(TT_TableGenBangOperator, TT_TableGenCondOperator,
6455 if (
Left.is(TT_TableGenValueSuffix))
6458 if (
Left.is(tok::hash) ||
Right.is(tok::hash))
6460 if (
Left.isOneOf(TT_TableGenBangOperator, TT_TableGenCondOperator))
6467 if (
Right.is(tok::r_brace)) {
6469 (
Right.isBlockIndentedInitRBrace(Style)));
6474 if (
Right.is(tok::r_paren)) {
6475 if (!
Right.MatchingParen)
6478 if (
Next &&
Next->is(tok::r_paren))
6480 if (
Next &&
Next->is(tok::l_paren))
6486 return Style.BreakBeforeCloseBracketIf;
6488 return Style.BreakBeforeCloseBracketLoop;
6490 return Style.BreakBeforeCloseBracketSwitch;
6491 return Style.BreakBeforeCloseBracketFunction;
6494 if (
Left.isOneOf(tok::r_paren, TT_TrailingAnnotation) &&
6495 Right.is(TT_TrailingAnnotation) &&
6496 Style.BreakBeforeCloseBracketFunction) {
6500 if (
Right.is(TT_TemplateCloser))
6501 return Style.BreakBeforeTemplateCloser;
6503 if (
Left.isOneOf(tok::at, tok::objc_interface))
6505 if (
Left.isOneOf(TT_JavaAnnotation, TT_LeadingJavaAnnotation))
6506 return Right.isNot(tok::l_paren);
6507 if (
Right.is(TT_PointerOrReference)) {
6508 return Line.IsMultiVariableDeclStmt ||
6509 (getTokenPointerOrReferenceAlignment(Right) ==
6512 Right.Next->isOneOf(TT_FunctionDeclarationName, tok::kw_const)));
6514 if (
Left.is(tok::hashhash) ||
Right.is(tok::hashhash))
6516 if (
Right.isOneOf(TT_StartOfName, TT_FunctionDeclarationName,
6517 TT_ClassHeadName, TT_QtProperty, tok::kw_operator)) {
6520 if (
Left.is(TT_PointerOrReference))
6522 if (
Right.isTrailingComment()) {
6529 (
Left.is(TT_CtorInitializerColon) &&
Right.NewlinesBefore > 0 &&
6532 if (
Left.is(tok::question) &&
Right.is(tok::colon))
6534 if (
Right.isOneOf(TT_ConditionalExpr, tok::question))
6535 return Style.BreakBeforeTernaryOperators;
6536 if (
Left.isOneOf(TT_ConditionalExpr, tok::question))
6537 return !Style.BreakBeforeTernaryOperators;
6538 if (
Left.is(TT_InheritanceColon))
6540 if (
Right.is(TT_InheritanceColon))
6543 if (
Right.is(TT_ObjCMethodExpr) &&
Right.isNot(tok::r_square) &&
6544 Left.isNot(TT_SelectorName)) {
6548 if (
Right.is(tok::colon) &&
6549 Right.isNoneOf(TT_CtorInitializerColon, TT_InlineASMColon,
6550 TT_BitFieldColon)) {
6553 if (
Left.is(tok::colon) &&
Left.isOneOf(TT_ObjCSelector, TT_ObjCMethodExpr))
6555 if (
Left.is(tok::colon) &&
Left.is(TT_DictLiteral)) {
6556 if (Style.isProto()) {
6557 if (!Style.AlwaysBreakBeforeMultilineStrings &&
Right.isStringLiteral())
6583 if ((
Right.isOneOf(tok::l_brace, tok::less) &&
6584 Right.is(TT_DictLiteral)) ||
6585 Right.is(TT_ArrayInitializerLSquare)) {
6591 if (
Right.is(tok::r_square) &&
Right.MatchingParen &&
6592 Right.MatchingParen->is(TT_ProtoExtensionLSquare)) {
6595 if (
Right.is(TT_SelectorName) || (
Right.is(tok::identifier) &&
Right.Next &&
6596 Right.Next->is(TT_ObjCMethodExpr))) {
6597 return Left.isNot(tok::period);
6601 if (
Right.is(tok::kw_concept))
6603 if (
Right.is(TT_RequiresClause))
6605 if (
Left.ClosesTemplateDeclaration) {
6607 Right.NewlinesBefore > 0;
6609 if (
Left.is(TT_FunctionAnnotationRParen))
6611 if (
Left.ClosesRequiresClause)
6613 if (
Right.isOneOf(TT_RangeBasedForLoopColon, TT_OverloadedOperatorLParen,
6614 TT_OverloadedOperator)) {
6617 if (
Left.is(TT_RangeBasedForLoopColon))
6619 if (
Right.is(TT_RangeBasedForLoopColon))
6621 if (
Left.is(TT_TemplateCloser) &&
Right.is(TT_TemplateOpener))
6623 if ((
Left.is(tok::greater) &&
Right.is(tok::greater)) ||
6624 (
Left.is(tok::less) &&
Right.is(tok::less))) {
6627 if (
Right.is(TT_BinaryOperator) &&
6633 if (
Left.isOneOf(TT_TemplateCloser, TT_UnaryOperator, tok::kw_operator))
6635 if (
Left.is(tok::equal) &&
Right.isNoneOf(tok::kw_default, tok::kw_delete) &&
6639 if (
Left.is(tok::equal) &&
Right.is(tok::l_brace) &&
6643 if (
Left.is(TT_AttributeLParen) ||
6644 (
Left.is(tok::l_paren) &&
Left.is(TT_TypeDeclarationParen))) {
6647 if (
Left.is(tok::l_paren) &&
Left.Previous &&
6648 (
Left.Previous->isOneOf(TT_BinaryOperator, TT_CastRParen))) {
6651 if (
Right.is(TT_ImplicitStringLiteral))
6654 if (
Right.is(tok::r_square) &&
Right.MatchingParen &&
6655 Right.MatchingParen->is(TT_LambdaLSquare)) {
6661 if (
Left.is(TT_TrailingAnnotation)) {
6662 return Right.isNoneOf(tok::l_brace, tok::semi, tok::equal, tok::l_paren,
6663 tok::less, tok::coloncolon);
6666 if (
Right.isAttribute())
6669 if (
Right.is(TT_AttributeLSquare)) {
6670 assert(
Left.isNot(tok::l_square));
6674 if (
Left.is(tok::identifier) &&
Right.is(tok::string_literal))
6677 if (
Right.is(tok::identifier) &&
Right.Next &&
Right.Next->is(TT_DictLiteral))
6680 if (
Left.is(TT_CtorInitializerColon)) {
6681 return (Style.BreakConstructorInitializers ==
6683 Style.BreakConstructorInitializers ==
6685 (!
Right.isTrailingComment() ||
Right.NewlinesBefore > 0);
6687 if (
Right.is(TT_CtorInitializerColon)) {
6691 if (
Left.is(TT_CtorInitializerComma) &&
6695 if (
Right.is(TT_CtorInitializerComma) &&
6699 if (
Left.is(TT_InheritanceComma) &&
6703 if (
Right.is(TT_InheritanceComma) &&
6707 if (
Left.is(TT_ArrayInitializerLSquare))
6709 if (
Right.is(tok::kw_typename) &&
Left.isNot(tok::kw_const))
6711 if ((
Left.isBinaryOperator() ||
Left.is(TT_BinaryOperator)) &&
6712 Left.isNoneOf(tok::arrowstar, tok::lessless) &&
6718 if (
Left.is(TT_AttributeLSquare) &&
Right.is(tok::l_square)) {
6719 assert(
Right.isNot(TT_AttributeLSquare));
6722 if (
Left.is(tok::r_square) &&
Right.is(TT_AttributeRSquare)) {
6723 assert(
Left.isNot(TT_AttributeRSquare));
6727 auto ShortLambdaOption = Style.AllowShortLambdasOnASingleLine;
6728 if (Style.BraceWrapping.BeforeLambdaBody &&
Right.is(TT_LambdaLBrace)) {
6735 if (
Right.is(tok::kw_noexcept) &&
Right.is(TT_TrailingAnnotation)) {
6736 switch (Style.AllowBreakBeforeNoexceptSpecifier) {
6742 return Right.Next &&
Right.Next->is(tok::l_paren);
6746 return Left.isOneOf(tok::comma, tok::coloncolon, tok::semi, tok::l_brace,
6747 tok::kw_class, tok::kw_struct, tok::comment) ||
6748 Right.isMemberAccess() ||
6749 Right.isOneOf(TT_TrailingReturnArrow, TT_LambdaArrow, tok::lessless,
6750 tok::colon, tok::l_square, tok::at) ||
6751 (
Left.is(tok::r_paren) &&
6752 Right.isOneOf(tok::identifier, tok::kw_const)) ||
6753 (
Left.is(tok::l_paren) &&
Right.isNot(tok::r_paren)) ||
6754 (
Left.is(TT_TemplateOpener) &&
Right.isNot(TT_TemplateCloser));
6758 llvm::errs() <<
"AnnotatedTokens(L=" <<
Line.Level <<
", P=" <<
Line.PPLevel
6759 <<
", T=" <<
Line.Type <<
", C=" <<
Line.IsContinuation
6763 llvm::errs() <<
" I=" <<
Tok->IndentLevel <<
" M=" <<
Tok->MustBreakBefore
6764 <<
" C=" <<
Tok->CanBreakBefore
6766 <<
" S=" <<
Tok->SpacesRequiredBefore
6767 <<
" F=" <<
Tok->Finalized <<
" B=" <<
Tok->BlockParameterCount
6768 <<
" BK=" <<
Tok->getBlockKind() <<
" P=" <<
Tok->SplitPenalty
6769 <<
" Name=" <<
Tok->Tok.getName() <<
" N=" <<
Tok->NestingLevel
6770 <<
" L=" <<
Tok->TotalLength
6771 <<
" PPK=" <<
Tok->getPackingKind() <<
" FakeLParens=";
6773 llvm::errs() << LParen <<
"/";
6774 llvm::errs() <<
" FakeRParens=" <<
Tok->FakeRParens;
6775 llvm::errs() <<
" II=" <<
Tok->Tok.getIdentifierInfo();
6776 llvm::errs() <<
" Text='" <<
Tok->TokenText <<
"'\n";
6781 llvm::errs() <<
"----\n";
6786 assert(
Reference.isOneOf(tok::amp, tok::ampamp));
6787 switch (Style.ReferenceAlignment) {
6789 return Style.PointerAlignment;
6798 return Style.PointerAlignment;
6802TokenAnnotator::getTokenPointerOrReferenceAlignment(
6804 if (PointerOrReference.isOneOf(tok::amp, tok::ampamp))
6805 return getTokenReferenceAlignment(PointerOrReference);
6806 assert(PointerOrReference.is(tok::star));
6807 return Style.PointerAlignment;
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.
PRESERVE_NONE bool Ret(InterpState &S)
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...
nullptr
This class represents a compute construct, representing a 'Kind' of ‘parallel’, 'serial',...
@ Parameter
The parameter type of a method or function.
@ Result
The result type of a method or function.
const FunctionProtoType * T
bool isReturnTypePrefixSpecifier(const FormatToken &Tok)
@ Type
The name was classified as a type.
prec::Level getBinOpPrecedence(tok::TokenKind Kind, bool GreaterThanIsOperator, bool CPlusPlus11)
Return the precedence of the specified binary operator token.