18#include "llvm/ADT/SmallPtrSet.h"
19#include "llvm/Support/Debug.h"
21#define DEBUG_TYPE "format-token-annotator"
27 const FormatStyle &Style) {
28 switch (Style.BreakAfterAttributes) {
29 case FormatStyle::ABS_Always:
31 case FormatStyle::ABS_Leave:
32 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))
91 if (
Tok.Previous &&
Tok.Previous->is(tok::at))
98 if (AttrTok->startsSequence(tok::kw_using, tok::identifier, tok::colon))
100 if (AttrTok->isNot(tok::identifier))
102 while (AttrTok && !AttrTok->startsSequence(tok::r_square, tok::r_square)) {
106 if (AttrTok->is(tok::colon) ||
107 AttrTok->startsSequence(tok::identifier, tok::identifier) ||
108 AttrTok->startsSequence(tok::r_paren, tok::identifier)) {
111 if (AttrTok->is(tok::ellipsis))
113 AttrTok = AttrTok->Next;
115 return AttrTok && AttrTok->startsSequence(tok::r_square, tok::r_square);
123class AnnotatingParser {
125 AnnotatingParser(
const FormatStyle &Style, AnnotatedLine &Line,
126 const AdditionalKeywords &Keywords,
127 SmallVector<ScopeType> &Scopes)
128 : Style(Style), Line(Line), CurrentToken(Line.
First), AutoFound(
false),
130 Keywords(Keywords), Scopes(Scopes), TemplateDeclarationDepth(0) {
131 Contexts.push_back(Context(tok::unknown, 1,
false));
132 resetTokenMetadata();
137 switch (Token.getType()) {
139 case TT_StructLBrace:
142 case TT_CompoundRequirementLBrace:
153 auto *
Left = CurrentToken->Previous;
157 if (NonTemplateLess.count(Left) > 0)
160 const auto *BeforeLess =
Left->Previous;
163 if (BeforeLess->Tok.isLiteral())
165 if (BeforeLess->is(tok::r_brace))
167 if (BeforeLess->is(tok::r_paren) && Contexts.size() > 1 &&
168 !(BeforeLess->MatchingParen &&
169 BeforeLess->MatchingParen->is(TT_OverloadedOperatorLParen))) {
172 if (BeforeLess->is(tok::kw_operator) && CurrentToken->is(tok::l_paren))
176 Left->ParentBracket = Contexts.back().ContextKind;
177 ScopedContextCreator ContextCreator(*
this, tok::less, 12);
178 Contexts.back().IsExpression =
false;
182 if (BeforeLess && BeforeLess->isNot(tok::kw_template))
183 Contexts.back().ContextType = Context::TemplateArgument;
185 if (Style.isJava() && CurrentToken->is(tok::question))
188 for (
bool SeenTernaryOperator =
false, MaybeAngles =
true; CurrentToken;) {
189 const bool InExpr = Contexts[Contexts.size() - 2].IsExpression;
190 if (CurrentToken->is(tok::greater)) {
191 const auto *
Next = CurrentToken->Next;
192 if (CurrentToken->isNot(TT_TemplateCloser)) {
199 if (
Next &&
Next->is(tok::greater) &&
200 Left->ParentBracket != tok::less &&
201 CurrentToken->getStartOfNonWhitespace() ==
202 Next->getStartOfNonWhitespace().getLocWithOffset(-1)) {
205 if (InExpr && SeenTernaryOperator &&
206 (!
Next ||
Next->isNoneOf(tok::l_paren, tok::l_brace))) {
212 Left->MatchingParen = CurrentToken;
213 CurrentToken->MatchingParen =
Left;
219 if (Style.isTextProto() ||
220 (Style.Language == FormatStyle::LK_Proto && BeforeLess &&
221 BeforeLess->isOneOf(TT_SelectorName, TT_DictLiteral))) {
222 CurrentToken->setType(TT_DictLiteral);
224 CurrentToken->setType(TT_TemplateCloser);
225 CurrentToken->Tok.setLength(1);
232 if (BeforeLess && BeforeLess->is(TT_TemplateName)) {
236 if (CurrentToken->is(tok::question) && Style.isJava()) {
240 if (CurrentToken->isOneOf(tok::r_paren, tok::r_square, tok::r_brace))
242 const auto &Prev = *CurrentToken->Previous;
249 if (MaybeAngles && InExpr && !Line.startsWith(tok::kw_template) &&
250 Prev.is(TT_BinaryOperator) &&
251 Prev.isOneOf(tok::pipepipe, tok::ampamp)) {
254 if (Prev.isOneOf(tok::question, tok::colon) && !Style.isProto())
255 SeenTernaryOperator =
true;
256 updateParameterCount(Left, CurrentToken);
257 if (Style.Language == FormatStyle::LK_Proto) {
259 if (CurrentToken->is(tok::colon) ||
260 (CurrentToken->isOneOf(tok::l_brace, tok::less) &&
265 }
else if (Style.isTableGen()) {
266 if (CurrentToken->isOneOf(tok::comma, tok::equal)) {
273 if (!parseTableGenValue())
283 bool parseUntouchableParens() {
284 while (CurrentToken) {
285 CurrentToken->Finalized =
true;
286 switch (CurrentToken->Tok.getKind()) {
289 if (!parseUntouchableParens())
304 bool parseParens(
bool IsIf =
false) {
307 assert(CurrentToken->Previous &&
"Unknown previous token");
308 FormatToken &OpeningParen = *CurrentToken->Previous;
309 assert(OpeningParen.is(tok::l_paren));
310 FormatToken *PrevNonComment = OpeningParen.getPreviousNonComment();
311 OpeningParen.ParentBracket = Contexts.back().ContextKind;
312 ScopedContextCreator ContextCreator(*
this, tok::l_paren, 1);
315 Contexts.back().ColonIsForRangeExpr =
316 Contexts.size() == 2 && Contexts[0].ColonIsForRangeExpr;
318 if (OpeningParen.Previous &&
319 OpeningParen.Previous->is(TT_UntouchableMacroFunc)) {
320 OpeningParen.Finalized =
true;
321 return parseUntouchableParens();
324 bool StartsObjCSelector =
false;
325 if (!Style.isVerilog()) {
326 if (
FormatToken *MaybeSel = OpeningParen.Previous) {
328 if (MaybeSel->is(tok::objc_selector) && MaybeSel->Previous &&
329 MaybeSel->Previous->is(tok::at)) {
330 StartsObjCSelector =
true;
335 if (OpeningParen.is(TT_OverloadedOperatorLParen)) {
338 while (Prev->isNot(tok::kw_operator)) {
339 Prev = Prev->Previous;
340 assert(Prev &&
"Expect a kw_operator prior to the OperatorLParen!");
346 bool OperatorCalledAsMemberFunction =
347 Prev->Previous && Prev->Previous->isOneOf(tok::period, tok::arrow);
348 Contexts.back().IsExpression = OperatorCalledAsMemberFunction;
349 }
else if (OpeningParen.is(TT_VerilogInstancePortLParen)) {
350 Contexts.back().IsExpression =
true;
351 Contexts.back().ContextType = Context::VerilogInstancePortList;
352 }
else if (Style.isJavaScript() &&
353 (Line.startsWith(Keywords.kw_type, tok::identifier) ||
354 Line.startsWith(tok::kw_export, Keywords.kw_type,
358 Contexts.back().IsExpression =
false;
359 }
else if (OpeningParen.Previous &&
360 (OpeningParen.Previous->isOneOf(
361 tok::kw_noexcept, tok::kw_explicit, tok::kw_while,
362 tok::l_paren, tok::comma, TT_CastRParen,
363 TT_BinaryOperator) ||
364 OpeningParen.Previous->isIf())) {
366 Contexts.back().IsExpression =
true;
367 }
else if (Style.isJavaScript() && OpeningParen.Previous &&
368 (OpeningParen.Previous->is(Keywords.kw_function) ||
369 (OpeningParen.Previous->endsSequence(tok::identifier,
370 Keywords.kw_function)))) {
372 Contexts.back().IsExpression =
false;
373 }
else if (Style.isJavaScript() && OpeningParen.Previous &&
374 OpeningParen.Previous->is(TT_JsTypeColon)) {
376 Contexts.back().IsExpression =
false;
377 }
else if (isLambdaParameterList(&OpeningParen)) {
379 OpeningParen.setType(TT_LambdaDefinitionLParen);
380 Contexts.back().IsExpression =
false;
381 }
else if (OpeningParen.is(TT_RequiresExpressionLParen)) {
382 Contexts.back().IsExpression =
false;
383 }
else if (OpeningParen.Previous &&
384 OpeningParen.Previous->is(tok::kw__Generic)) {
385 Contexts.back().ContextType = Context::C11GenericSelection;
386 Contexts.back().IsExpression =
true;
387 }
else if (OpeningParen.Previous &&
388 OpeningParen.Previous->TokenText ==
"Q_PROPERTY") {
389 Contexts.back().ContextType = Context::QtProperty;
390 Contexts.back().IsExpression =
false;
391 }
else if (Line.InPPDirective &&
392 (!OpeningParen.Previous ||
393 OpeningParen.Previous->isNot(tok::identifier))) {
394 Contexts.back().IsExpression =
true;
395 }
else if (Contexts[Contexts.size() - 2].CaretFound) {
397 Contexts.back().IsExpression =
false;
398 }
else if (OpeningParen.Previous &&
399 OpeningParen.Previous->is(TT_ForEachMacro)) {
401 Contexts.back().ContextType = Context::ForEachMacro;
402 Contexts.back().IsExpression =
false;
403 }
else if (OpeningParen.Previous && OpeningParen.Previous->MatchingParen &&
404 OpeningParen.Previous->MatchingParen->isOneOf(
405 TT_ObjCBlockLParen, TT_FunctionTypeLParen)) {
406 Contexts.back().IsExpression =
false;
407 }
else if (!Line.MustBeDeclaration &&
408 (!Line.InPPDirective || (Line.InMacroBody && !Scopes.empty()))) {
410 OpeningParen.Previous &&
411 OpeningParen.Previous->isOneOf(tok::kw_for, tok::kw_catch);
412 Contexts.back().IsExpression = !IsForOrCatch;
415 if (Style.isTableGen()) {
417 if (Prev->is(TT_TableGenCondOperator)) {
418 Contexts.back().IsTableGenCondOpe =
true;
419 Contexts.back().IsExpression =
true;
420 }
else if (Contexts.size() > 1 &&
421 Contexts[Contexts.size() - 2].IsTableGenBangOpe) {
426 Contexts.back().IsTableGenBangOpe =
true;
427 Contexts.back().IsExpression =
true;
430 if (!parseTableGenDAGArg())
432 return parseTableGenDAGArgAndList(&OpeningParen);
439 if (PrevNonComment && OpeningParen.is(TT_Unknown)) {
440 if (PrevNonComment->isAttribute()) {
441 OpeningParen.setType(TT_AttributeLParen);
442 }
else if (PrevNonComment->isOneOf(TT_TypenameMacro, tok::kw_decltype,
445#include
"clang/Basic/TransformTypeTraits.def"
447 OpeningParen.setType(TT_TypeDeclarationParen);
449 if (PrevNonComment->isOneOf(tok::kw_decltype, tok::kw_typeof))
450 Contexts.back().IsExpression =
true;
454 if (StartsObjCSelector)
455 OpeningParen.setType(TT_ObjCSelector);
457 const bool IsStaticAssert =
458 PrevNonComment && PrevNonComment->is(tok::kw_static_assert);
460 Contexts.back().InStaticAssertFirstArgument =
true;
470 bool MightBeFunctionType = !Contexts[Contexts.size() - 2].IsExpression;
471 bool ProbablyFunctionType =
472 CurrentToken->isPointerOrReference() || CurrentToken->is(tok::caret);
473 bool HasMultipleLines =
false;
474 bool HasMultipleParametersOnALine =
false;
475 bool MightBeObjCForRangeLoop =
476 OpeningParen.Previous && OpeningParen.Previous->is(tok::kw_for);
478 while (CurrentToken) {
479 const auto &Prev = *CurrentToken->Previous;
480 const auto *PrevPrev = Prev.Previous;
481 if (Prev.is(TT_PointerOrReference) &&
482 PrevPrev->isOneOf(tok::l_paren, tok::coloncolon)) {
483 ProbablyFunctionType =
true;
485 if (CurrentToken->is(tok::comma))
486 MightBeFunctionType =
false;
487 if (Prev.is(TT_BinaryOperator))
488 Contexts.back().IsExpression =
true;
489 if (CurrentToken->is(tok::r_paren)) {
490 if (Prev.is(TT_PointerOrReference) &&
491 (PrevPrev == &OpeningParen || PrevPrev->is(tok::coloncolon))) {
492 MightBeFunctionType =
true;
494 if (OpeningParen.isNot(TT_CppCastLParen) && MightBeFunctionType &&
495 ProbablyFunctionType && CurrentToken->Next &&
496 (CurrentToken->Next->is(tok::l_paren) ||
497 (CurrentToken->Next->is(tok::l_square) &&
498 (Line.MustBeDeclaration ||
499 (PrevNonComment && PrevNonComment->isTypeName(LangOpts)))))) {
500 OpeningParen.setType(OpeningParen.Next->is(tok::caret)
502 : TT_FunctionTypeLParen);
504 OpeningParen.MatchingParen = CurrentToken;
505 CurrentToken->MatchingParen = &OpeningParen;
507 if (CurrentToken->Next && CurrentToken->Next->is(tok::l_brace) &&
508 OpeningParen.Previous && OpeningParen.Previous->is(tok::l_paren)) {
514 if (
Tok->is(TT_BinaryOperator) &&
Tok->isPointerOrReference())
515 Tok->setType(TT_PointerOrReference);
519 if (StartsObjCSelector) {
520 CurrentToken->setType(TT_ObjCSelector);
521 if (Contexts.back().FirstObjCSelectorName) {
522 Contexts.back().FirstObjCSelectorName->LongestObjCSelectorName =
523 Contexts.back().LongestObjCSelectorName;
527 if (OpeningParen.is(TT_AttributeLParen))
528 CurrentToken->setType(TT_AttributeRParen);
529 if (OpeningParen.is(TT_TypeDeclarationParen))
530 CurrentToken->setType(TT_TypeDeclarationParen);
531 if (OpeningParen.Previous &&
532 OpeningParen.Previous->is(TT_JavaAnnotation)) {
533 CurrentToken->setType(TT_JavaAnnotation);
535 if (OpeningParen.Previous &&
536 OpeningParen.Previous->is(TT_LeadingJavaAnnotation)) {
537 CurrentToken->setType(TT_LeadingJavaAnnotation);
540 if (!HasMultipleLines)
542 else if (HasMultipleParametersOnALine)
550 if (CurrentToken->isOneOf(tok::r_square, tok::r_brace))
553 if (CurrentToken->is(tok::l_brace) && OpeningParen.is(TT_ObjCBlockLParen))
554 OpeningParen.setType(TT_Unknown);
555 if (CurrentToken->is(tok::comma) && CurrentToken->Next &&
556 !CurrentToken->Next->HasUnescapedNewline &&
557 !CurrentToken->Next->isTrailingComment()) {
558 HasMultipleParametersOnALine =
true;
560 bool ProbablyFunctionTypeLParen =
561 (CurrentToken->is(tok::l_paren) && CurrentToken->Next &&
562 CurrentToken->Next->isOneOf(tok::star, tok::amp, tok::caret));
563 if ((Prev.isOneOf(tok::kw_const, tok::kw_auto) ||
564 Prev.isTypeName(LangOpts)) &&
565 !(CurrentToken->is(tok::l_brace) ||
566 (CurrentToken->is(tok::l_paren) && !ProbablyFunctionTypeLParen))) {
567 Contexts.back().IsExpression =
false;
569 if (CurrentToken->isOneOf(tok::semi, tok::colon)) {
570 MightBeObjCForRangeLoop =
false;
571 if (PossibleObjCForInToken) {
572 PossibleObjCForInToken->setType(TT_Unknown);
573 PossibleObjCForInToken =
nullptr;
576 if (IsIf && CurrentToken->is(tok::semi)) {
577 for (
auto *
Tok = OpeningParen.Next;
578 Tok != CurrentToken &&
579 Tok->isNoneOf(tok::equal, tok::l_paren, tok::l_brace);
581 if (
Tok->isPointerOrReference())
582 Tok->setFinalizedType(TT_PointerOrReference);
585 if (MightBeObjCForRangeLoop && CurrentToken->is(Keywords.kw_in)) {
586 PossibleObjCForInToken = CurrentToken;
587 PossibleObjCForInToken->setType(TT_ObjCForIn);
591 if (CurrentToken->is(tok::comma)) {
593 Contexts.back().InStaticAssertFirstArgument =
false;
595 Contexts.back().CanBeExpression =
true;
598 if (Style.isTableGen()) {
599 if (CurrentToken->is(tok::comma)) {
600 if (Contexts.back().IsTableGenCondOpe)
601 CurrentToken->setType(TT_TableGenCondOperatorComma);
603 }
else if (CurrentToken->is(tok::colon)) {
604 if (Contexts.back().IsTableGenCondOpe)
605 CurrentToken->setType(TT_TableGenCondOperatorColon);
609 if (!parseTableGenValue())
617 updateParameterCount(&OpeningParen,
Tok);
618 if (CurrentToken && CurrentToken->HasUnescapedNewline)
619 HasMultipleLines =
true;
625 if (!Style.isCSharp())
629 if (
Tok.Previous &&
Tok.Previous->is(tok::identifier))
633 if (
Tok.Previous &&
Tok.Previous->is(tok::r_square)) {
644 if (AttrTok->is(tok::r_square))
648 while (AttrTok && AttrTok->isNot(tok::r_square))
649 AttrTok = AttrTok->Next;
655 AttrTok = AttrTok->Next;
660 if (AttrTok->isAccessSpecifierKeyword() ||
661 AttrTok->isOneOf(tok::comment, tok::kw_class, tok::kw_static,
662 tok::l_square, Keywords.kw_internal)) {
668 AttrTok->Next->startsSequence(tok::identifier, tok::l_paren)) {
684 Left->ParentBracket = Contexts.back().ContextKind;
690 bool CppArrayTemplates =
691 IsCpp && Parent && Parent->is(TT_TemplateCloser) &&
692 (Contexts.back().CanBeExpression || Contexts.back().IsExpression ||
693 Contexts.back().ContextType == Context::TemplateArgument);
695 const bool IsInnerSquare = Contexts.back().InCpp11AttributeSpecifier;
696 const bool IsCpp11AttributeSpecifier =
697 isCppAttribute(IsCpp, *Left) || IsInnerSquare;
700 bool IsCSharpAttributeSpecifier =
701 isCSharpAttributeSpecifier(*Left) ||
702 Contexts.back().InCSharpAttributeSpecifier;
704 bool InsideInlineASM = Line.startsWith(tok::kw_asm);
705 bool IsCppStructuredBinding =
Left->isCppStructuredBinding(IsCpp);
706 bool StartsObjCMethodExpr =
707 !IsCppStructuredBinding && !InsideInlineASM && !CppArrayTemplates &&
708 IsCpp && !IsCpp11AttributeSpecifier && !IsCSharpAttributeSpecifier &&
709 Contexts.back().CanBeExpression &&
Left->isNot(TT_LambdaLSquare) &&
710 CurrentToken->isNoneOf(tok::l_brace, tok::r_square) &&
712 Parent->isOneOf(tok::colon, tok::l_square, tok::l_paren,
713 tok::kw_return, tok::kw_throw) ||
714 Parent->isUnaryOperator() ||
716 Parent->isOneOf(TT_ObjCForIn, TT_CastRParen) ||
719 bool ColonFound =
false;
721 unsigned BindingIncrease = 1;
722 if (IsCppStructuredBinding) {
723 Left->setType(TT_StructuredBindingLSquare);
724 }
else if (
Left->is(TT_Unknown)) {
725 if (StartsObjCMethodExpr) {
726 Left->setType(TT_ObjCMethodExpr);
727 }
else if (InsideInlineASM) {
728 Left->setType(TT_InlineASMSymbolicNameLSquare);
729 }
else if (IsCpp11AttributeSpecifier) {
730 if (!IsInnerSquare) {
731 Left->setType(TT_AttributeLSquare);
733 Left->Previous->EndsCppAttributeGroup =
false;
735 }
else if (Style.isJavaScript() && Parent &&
736 Contexts.back().ContextKind == tok::l_brace &&
737 Parent->isOneOf(tok::l_brace, tok::comma)) {
738 Left->setType(TT_JsComputedPropertyName);
739 }
else if (IsCpp && Contexts.back().ContextKind == tok::l_brace &&
740 Parent && Parent->isOneOf(tok::l_brace, tok::comma)) {
741 Left->setType(TT_DesignatedInitializerLSquare);
742 }
else if (IsCSharpAttributeSpecifier) {
743 Left->setType(TT_AttributeLSquare);
744 }
else if (CurrentToken->is(tok::r_square) && Parent &&
745 Parent->is(TT_TemplateCloser)) {
746 Left->setType(TT_ArraySubscriptLSquare);
747 }
else if (Style.isProto()) {
774 Left->setType(TT_ArrayInitializerLSquare);
775 if (!
Left->endsSequence(tok::l_square, tok::numeric_constant,
777 !
Left->endsSequence(tok::l_square, tok::numeric_constant,
779 !
Left->endsSequence(tok::l_square, tok::colon, TT_SelectorName)) {
780 Left->setType(TT_ProtoExtensionLSquare);
781 BindingIncrease = 10;
783 }
else if (!CppArrayTemplates && Parent &&
784 Parent->isOneOf(TT_BinaryOperator, TT_TemplateCloser, tok::at,
785 tok::comma, tok::l_paren, tok::l_square,
786 tok::question, tok::colon, tok::kw_return,
789 Left->setType(TT_ArrayInitializerLSquare);
791 BindingIncrease = 10;
792 Left->setType(TT_ArraySubscriptLSquare);
796 ScopedContextCreator ContextCreator(*
this, tok::l_square, BindingIncrease);
797 Contexts.back().IsExpression =
true;
798 if (Style.isJavaScript() && Parent && Parent->is(TT_JsTypeColon))
799 Contexts.back().IsExpression =
false;
801 Contexts.back().ColonIsObjCMethodExpr = StartsObjCMethodExpr;
802 Contexts.back().InCpp11AttributeSpecifier = IsCpp11AttributeSpecifier;
803 Contexts.back().InCSharpAttributeSpecifier = IsCSharpAttributeSpecifier;
805 while (CurrentToken) {
806 if (CurrentToken->is(tok::r_square)) {
807 if (IsCpp11AttributeSpecifier && !IsInnerSquare) {
808 CurrentToken->setType(TT_AttributeRSquare);
809 CurrentToken->EndsCppAttributeGroup =
true;
811 if (IsCSharpAttributeSpecifier) {
812 CurrentToken->setType(TT_AttributeRSquare);
813 }
else if (((CurrentToken->Next &&
814 CurrentToken->Next->is(tok::l_paren)) ||
815 (CurrentToken->Previous &&
816 CurrentToken->Previous->Previous == Left)) &&
817 Left->is(TT_ObjCMethodExpr)) {
822 StartsObjCMethodExpr =
false;
823 Left->setType(TT_Unknown);
825 if (StartsObjCMethodExpr && CurrentToken->Previous != Left) {
826 CurrentToken->setType(TT_ObjCMethodExpr);
829 if (!ColonFound && CurrentToken->Previous &&
830 CurrentToken->Previous->is(TT_Unknown) &&
831 canBeObjCSelectorComponent(*CurrentToken->Previous)) {
832 CurrentToken->Previous->setType(TT_SelectorName);
837 if (Parent && Parent->is(TT_PointerOrReference))
838 Parent->overwriteFixedType(TT_BinaryOperator);
840 Left->MatchingParen = CurrentToken;
841 CurrentToken->MatchingParen =
Left;
846 if (!Contexts.back().FirstObjCSelectorName) {
849 Previous->ObjCSelectorNameParts = 1;
850 Contexts.back().FirstObjCSelectorName =
Previous;
853 Left->ParameterCount =
854 Contexts.back().FirstObjCSelectorName->ObjCSelectorNameParts;
856 if (Contexts.back().FirstObjCSelectorName) {
857 Contexts.back().FirstObjCSelectorName->LongestObjCSelectorName =
858 Contexts.back().LongestObjCSelectorName;
859 if (
Left->BlockParameterCount > 1)
860 Contexts.back().FirstObjCSelectorName->LongestObjCSelectorName = 0;
862 if (Style.isTableGen() &&
Left->is(TT_TableGenListOpener))
863 CurrentToken->setType(TT_TableGenListCloser);
867 if (CurrentToken->isOneOf(tok::r_paren, tok::r_brace))
869 if (CurrentToken->is(tok::colon)) {
870 if (IsCpp11AttributeSpecifier &&
871 CurrentToken->endsSequence(tok::colon, tok::identifier,
875 CurrentToken->setType(TT_AttributeColon);
876 }
else if (!Style.isVerilog() && !Line.InPragmaDirective &&
877 Left->isOneOf(TT_ArraySubscriptLSquare,
878 TT_DesignatedInitializerLSquare)) {
879 Left->setType(TT_ObjCMethodExpr);
880 StartsObjCMethodExpr =
true;
881 Contexts.back().ColonIsObjCMethodExpr =
true;
882 if (Parent && Parent->is(tok::r_paren)) {
884 Parent->setType(TT_CastRParen);
889 if (CurrentToken->is(tok::comma) &&
Left->is(TT_ObjCMethodExpr) &&
891 Left->setType(TT_ArrayInitializerLSquare);
894 if (Style.isTableGen()) {
895 if (CurrentToken->isOneOf(tok::comma, tok::minus, tok::ellipsis)) {
901 if (!parseTableGenValue())
904 updateParameterCount(Left,
Tok);
909 updateParameterCount(Left,
Tok);
914 void skipToNextNonComment() {
916 while (CurrentToken && CurrentToken->is(tok::comment))
925 bool parseTableGenValue(
bool ParseNameMode =
false) {
928 while (CurrentToken->is(tok::comment))
930 if (!parseTableGenSimpleValue())
935 if (CurrentToken->is(tok::hash)) {
936 if (CurrentToken->Next &&
937 CurrentToken->Next->isOneOf(tok::colon, tok::semi, tok::l_brace)) {
940 CurrentToken->setType(TT_TableGenTrailingPasteOperator);
945 skipToNextNonComment();
946 HashTok->setType(TT_Unknown);
947 if (!parseTableGenValue(ParseNameMode))
954 if (ParseNameMode && CurrentToken->is(tok::l_brace))
957 if (CurrentToken->isOneOf(tok::l_brace, tok::l_square, tok::period)) {
958 CurrentToken->setType(TT_TableGenValueSuffix);
960 skipToNextNonComment();
961 if (Suffix->is(tok::l_square))
962 return parseSquare();
963 if (Suffix->is(tok::l_brace)) {
964 Scopes.push_back(getScopeType(*Suffix));
974 bool tryToParseTableGenTokVar() {
977 if (CurrentToken->is(tok::identifier) &&
978 CurrentToken->TokenText.front() ==
'$') {
979 skipToNextNonComment();
987 bool parseTableGenDAGArg(
bool AlignColon =
false) {
988 if (tryToParseTableGenTokVar())
990 if (parseTableGenValue()) {
991 if (CurrentToken && CurrentToken->is(tok::colon)) {
993 CurrentToken->setType(TT_TableGenDAGArgListColonToAlign);
995 CurrentToken->setType(TT_TableGenDAGArgListColon);
996 skipToNextNonComment();
997 return tryToParseTableGenTokVar();
1008 auto &Opes = Style.TableGenBreakingDAGArgOperators;
1013 if (
Tok.isNot(tok::identifier) ||
1014 Tok.isOneOf(TT_TableGenBangOperator, TT_TableGenCondOperator)) {
1018 if (!
Tok.Next ||
Tok.Next->is(tok::colon))
1020 return llvm::is_contained(Opes,
Tok.TokenText.str());
1025 bool parseTableGenDAGArgAndList(
FormatToken *Opener) {
1027 if (!parseTableGenDAGArg())
1029 bool BreakInside =
false;
1030 if (Style.TableGenBreakInsideDAGArg != FormatStyle::DAS_DontBreak) {
1033 if (isTableGenDAGArgBreakingOperator(*FirstTok)) {
1036 Opener->setType(TT_TableGenDAGArgOpenerToBreak);
1037 if (FirstTok->isOneOf(TT_TableGenBangOperator,
1038 TT_TableGenCondOperator)) {
1041 CurrentToken->Previous->setType(TT_TableGenDAGArgOperatorToBreak);
1042 }
else if (FirstTok->is(tok::identifier)) {
1043 if (Style.TableGenBreakInsideDAGArg == FormatStyle::DAS_BreakAll)
1044 FirstTok->setType(TT_TableGenDAGArgOperatorToBreak);
1046 FirstTok->setType(TT_TableGenDAGArgOperatorID);
1051 return parseTableGenDAGArgList(Opener, BreakInside);
1056 bool parseTableGenDAGArgList(
FormatToken *Opener,
bool BreakInside) {
1057 ScopedContextCreator ContextCreator(*
this, tok::l_paren, 0);
1058 Contexts.back().IsTableGenDAGArgList =
true;
1059 bool FirstDAGArgListElm =
true;
1060 while (CurrentToken) {
1061 if (!FirstDAGArgListElm && CurrentToken->is(tok::comma)) {
1062 CurrentToken->setType(BreakInside ? TT_TableGenDAGArgListCommaToBreak
1063 : TT_TableGenDAGArgListComma);
1064 skipToNextNonComment();
1066 if (CurrentToken && CurrentToken->is(tok::r_paren)) {
1067 CurrentToken->setType(TT_TableGenDAGArgCloser);
1068 Opener->MatchingParen = CurrentToken;
1069 CurrentToken->MatchingParen = Opener;
1070 skipToNextNonComment();
1073 if (!parseTableGenDAGArg(
1075 Style.AlignConsecutiveTableGenBreakingDAGArgColons.Enabled)) {
1078 FirstDAGArgListElm =
false;
1083 bool parseTableGenSimpleValue() {
1084 assert(Style.isTableGen());
1088 skipToNextNonComment();
1090 if (
Tok->isOneOf(tok::numeric_constant, tok::string_literal,
1091 TT_TableGenMultiLineString, tok::kw_true, tok::kw_false,
1092 tok::question, tok::kw_int)) {
1096 if (
Tok->is(tok::l_brace)) {
1097 Scopes.push_back(getScopeType(*
Tok));
1098 return parseBrace();
1101 if (
Tok->is(tok::l_square)) {
1102 Tok->setType(TT_TableGenListOpener);
1105 if (
Tok->is(tok::less)) {
1106 CurrentToken->setType(TT_TemplateOpener);
1107 return parseAngle();
1113 if (
Tok->is(tok::l_paren)) {
1114 Tok->setType(TT_TableGenDAGArgOpener);
1116 if (Contexts.back().IsTableGenDAGArgList)
1117 Tok->SpacesRequiredBefore = 1;
1118 return parseTableGenDAGArgAndList(
Tok);
1121 if (
Tok->is(TT_TableGenBangOperator)) {
1122 if (CurrentToken && CurrentToken->is(tok::less)) {
1123 CurrentToken->setType(TT_TemplateOpener);
1124 skipToNextNonComment();
1128 if (!CurrentToken || CurrentToken->isNot(tok::l_paren))
1132 Contexts.back().IsTableGenBangOpe =
true;
1133 bool Result = parseParens();
1134 Contexts.back().IsTableGenBangOpe =
false;
1138 if (
Tok->is(TT_TableGenCondOperator)) {
1139 if (!CurrentToken || CurrentToken->isNot(tok::l_paren))
1142 return parseParens();
1147 if (
Tok->is(tok::identifier)) {
1149 if (CurrentToken && CurrentToken->is(tok::less)) {
1150 CurrentToken->setType(TT_TemplateOpener);
1151 skipToNextNonComment();
1152 return parseAngle();
1160 bool couldBeInStructArrayInitializer()
const {
1161 if (Contexts.size() < 2)
1165 const auto End = std::next(Contexts.rbegin(), 2);
1166 auto Last = Contexts.rbegin();
1169 if (
Last->ContextKind == tok::l_brace)
1171 return Depth == 2 &&
Last->ContextKind != tok::l_brace;
1178 assert(CurrentToken->Previous);
1179 FormatToken &OpeningBrace = *CurrentToken->Previous;
1180 assert(OpeningBrace.is(tok::l_brace));
1181 OpeningBrace.ParentBracket = Contexts.back().ContextKind;
1183 if (Contexts.back().CaretFound)
1184 OpeningBrace.overwriteFixedType(TT_ObjCBlockLBrace);
1185 Contexts.back().CaretFound =
false;
1187 ScopedContextCreator ContextCreator(*
this, tok::l_brace, 1);
1188 Contexts.back().ColonIsDictLiteral =
true;
1190 Contexts.back().IsExpression =
true;
1191 if (Style.isJavaScript() && OpeningBrace.Previous &&
1192 OpeningBrace.Previous->is(TT_JsTypeColon)) {
1193 Contexts.back().IsExpression =
false;
1195 if (Style.isVerilog() &&
1196 (!OpeningBrace.getPreviousNonComment() ||
1197 OpeningBrace.getPreviousNonComment()->isNot(Keywords.kw_apostrophe))) {
1198 Contexts.back().VerilogMayBeConcatenation =
true;
1200 if (Style.isTableGen())
1201 Contexts.back().ColonIsDictLiteral =
false;
1203 unsigned CommaCount = 0;
1204 while (CurrentToken) {
1205 if (CurrentToken->is(tok::r_brace)) {
1206 assert(!Scopes.empty());
1207 assert(Scopes.back() == getScopeType(OpeningBrace));
1209 assert(OpeningBrace.Optional == CurrentToken->Optional);
1210 OpeningBrace.MatchingParen = CurrentToken;
1211 CurrentToken->MatchingParen = &OpeningBrace;
1212 if (Style.AlignArrayOfStructures != FormatStyle::AIAS_None) {
1213 if (OpeningBrace.ParentBracket == tok::l_brace &&
1214 couldBeInStructArrayInitializer() && CommaCount > 0) {
1215 Contexts.back().ContextType = Context::StructArrayInitializer;
1221 if (CurrentToken->isOneOf(tok::r_paren, tok::r_square))
1223 updateParameterCount(&OpeningBrace, CurrentToken);
1224 if (CurrentToken->isOneOf(tok::colon, tok::l_brace, tok::less)) {
1226 if (
Previous->is(TT_JsTypeOptionalQuestion))
1228 if ((CurrentToken->is(tok::colon) && !Style.isTableGen() &&
1229 (!Contexts.back().ColonIsDictLiteral || !IsCpp)) ||
1231 OpeningBrace.setType(TT_DictLiteral);
1232 if (
Previous->Tok.getIdentifierInfo() ||
1233 Previous->is(tok::string_literal)) {
1234 Previous->setType(TT_SelectorName);
1237 if (CurrentToken->is(tok::colon) && OpeningBrace.is(TT_Unknown) &&
1238 !Style.isTableGen()) {
1239 OpeningBrace.setType(TT_DictLiteral);
1240 }
else if (Style.isJavaScript()) {
1241 OpeningBrace.overwriteFixedType(TT_DictLiteral);
1244 if (CurrentToken->is(tok::comma)) {
1245 if (Style.isJavaScript())
1246 OpeningBrace.overwriteFixedType(TT_DictLiteral);
1249 if (!consumeToken())
1259 if (Current->is(tok::l_brace) && Current->is(
BK_Block))
1260 ++
Left->BlockParameterCount;
1261 if (Current->is(tok::comma)) {
1262 ++
Left->ParameterCount;
1264 Left->Role.reset(
new CommaSeparatedList(Style));
1265 Left->Role->CommaFound(Current);
1266 }
else if (
Left->ParameterCount == 0 && Current->isNot(tok::comment)) {
1267 Left->ParameterCount = 1;
1271 bool parseConditional() {
1272 while (CurrentToken) {
1273 if (CurrentToken->is(tok::colon) && CurrentToken->is(TT_Unknown)) {
1274 CurrentToken->setType(TT_ConditionalExpr);
1278 if (!consumeToken())
1284 bool parseTemplateDeclaration() {
1285 if (!CurrentToken || CurrentToken->isNot(tok::less))
1288 CurrentToken->setType(TT_TemplateOpener);
1291 TemplateDeclarationDepth++;
1292 const bool WellFormed = parseAngle();
1293 TemplateDeclarationDepth--;
1297 if (CurrentToken && TemplateDeclarationDepth == 0)
1298 CurrentToken->Previous->ClosesTemplateDeclaration =
true;
1303 bool consumeToken() {
1305 const auto *Prev = CurrentToken->getPreviousNonComment();
1306 if (Prev && Prev->is(TT_AttributeRSquare) &&
1307 CurrentToken->isOneOf(tok::kw_if, tok::kw_switch, tok::kw_case,
1308 tok::kw_default, tok::kw_for, tok::kw_while) &&
1310 CurrentToken->MustBreakBefore =
true;
1317 if (
Tok->is(TT_VerilogTableItem))
1320 if (
Tok->is(TT_TableGenMultiLineString))
1322 auto *Prev =
Tok->getPreviousNonComment();
1323 auto *
Next =
Tok->getNextNonComment();
1324 switch (
bool IsIf =
false;
Tok->Tok.getKind()) {
1327 if (!Prev && Line.MustBeDeclaration)
1328 Tok->setType(TT_ObjCMethodSpecifier);
1335 if (
Tok->isTypeFinalized())
1338 if (Style.isJavaScript()) {
1339 if (Contexts.back().ColonIsForRangeExpr ||
1340 (Contexts.size() == 1 &&
1341 Line.First->isNoneOf(tok::kw_enum, tok::kw_case)) ||
1342 Contexts.back().ContextKind == tok::l_paren ||
1343 Contexts.back().ContextKind == tok::l_square ||
1344 (!Contexts.back().IsExpression &&
1345 Contexts.back().ContextKind == tok::l_brace) ||
1346 (Contexts.size() == 1 &&
1347 Line.MustBeDeclaration)) {
1348 Contexts.back().IsExpression =
false;
1349 Tok->setType(TT_JsTypeColon);
1352 }
else if (Style.isCSharp()) {
1353 if (Contexts.back().InCSharpAttributeSpecifier) {
1354 Tok->setType(TT_AttributeColon);
1357 if (Contexts.back().ContextKind == tok::l_paren) {
1358 Tok->setType(TT_CSharpNamedArgumentColon);
1361 }
else if (Style.isVerilog() &&
Tok->isNot(TT_BinaryOperator)) {
1364 if (Keywords.isVerilogEnd(*Prev) || Keywords.isVerilogBegin(*Prev)) {
1365 Tok->setType(TT_VerilogBlockLabelColon);
1366 }
else if (Contexts.back().ContextKind == tok::l_square) {
1367 Tok->setType(TT_BitFieldColon);
1368 }
else if (Contexts.back().ColonIsDictLiteral) {
1369 Tok->setType(TT_DictLiteral);
1370 }
else if (Contexts.size() == 1) {
1374 Tok->setType(TT_CaseLabelColon);
1375 if (Line.Level > 1 || (!Line.InPPDirective && Line.Level > 0))
1380 if (Line.First->isOneOf(Keywords.kw_module, Keywords.kw_import) ||
1381 Line.First->startsSequence(tok::kw_export, Keywords.kw_module) ||
1382 Line.First->startsSequence(tok::kw_export, Keywords.kw_import)) {
1383 Tok->setType(TT_ModulePartitionColon);
1384 }
else if (Line.First->is(tok::kw_asm)) {
1385 Tok->setType(TT_InlineASMColon);
1386 }
else if (Contexts.back().ColonIsDictLiteral || Style.isProto()) {
1387 Tok->setType(TT_DictLiteral);
1388 if (Style.isTextProto())
1389 Prev->setType(TT_SelectorName);
1390 }
else if (Contexts.back().ColonIsObjCMethodExpr ||
1391 Line.startsWith(TT_ObjCMethodSpecifier)) {
1392 Tok->setType(TT_ObjCMethodExpr);
1393 const auto *PrevPrev = Prev->Previous;
1396 bool UnknownIdentifierInMethodDeclaration =
1397 Line.startsWith(TT_ObjCMethodSpecifier) &&
1398 Prev->is(tok::identifier) && Prev->is(TT_Unknown);
1401 !(PrevPrev->is(TT_CastRParen) ||
1402 (PrevPrev->is(TT_ObjCMethodExpr) && PrevPrev->is(tok::colon))) ||
1403 PrevPrev->is(tok::r_square) ||
1404 Contexts.back().LongestObjCSelectorName == 0 ||
1405 UnknownIdentifierInMethodDeclaration) {
1406 Prev->setType(TT_SelectorName);
1407 if (!Contexts.back().FirstObjCSelectorName)
1408 Contexts.back().FirstObjCSelectorName = Prev;
1409 else if (Prev->ColumnWidth > Contexts.back().LongestObjCSelectorName)
1410 Contexts.back().LongestObjCSelectorName = Prev->ColumnWidth;
1411 Prev->ParameterIndex =
1412 Contexts.back().FirstObjCSelectorName->ObjCSelectorNameParts;
1413 ++Contexts.back().FirstObjCSelectorName->ObjCSelectorNameParts;
1415 }
else if (Contexts.back().ColonIsForRangeExpr) {
1416 Tok->setType(TT_RangeBasedForLoopColon);
1417 for (
auto *Token = Prev;
1418 Token && Token->isNoneOf(tok::semi, tok::l_paren);
1419 Token = Token->Previous) {
1420 if (Token->isPointerOrReference())
1421 Token->setFinalizedType(TT_PointerOrReference);
1423 }
else if (Contexts.back().ContextType == Context::C11GenericSelection) {
1424 Tok->setType(TT_GenericSelectionColon);
1425 if (Prev->isPointerOrReference())
1426 Prev->setFinalizedType(TT_PointerOrReference);
1427 }
else if ((CurrentToken && CurrentToken->is(tok::numeric_constant)) ||
1428 (Prev->is(TT_StartOfName) && !Scopes.empty() &&
1430 Tok->setType(TT_BitFieldColon);
1431 }
else if (Contexts.size() == 1 &&
1432 Line.getFirstNonComment()->isNoneOf(tok::kw_enum, tok::kw_case,
1434 !Line.startsWith(tok::kw_typedef, tok::kw_enum)) {
1435 if (Prev->isOneOf(tok::r_paren, tok::kw_noexcept) ||
1436 Prev->ClosesRequiresClause) {
1437 Tok->setType(TT_CtorInitializerColon);
1438 }
else if (Prev->is(tok::kw_try)) {
1440 FormatToken *PrevPrev = Prev->getPreviousNonComment();
1443 if (PrevPrev && PrevPrev->isOneOf(tok::r_paren, tok::kw_noexcept))
1444 Tok->setType(TT_CtorInitializerColon);
1446 Tok->setType(TT_InheritanceColon);
1447 if (Prev->isAccessSpecifierKeyword())
1450 }
else if (canBeObjCSelectorComponent(*Prev) &&
Next &&
1451 (
Next->isOneOf(tok::r_paren, tok::comma) ||
1452 (canBeObjCSelectorComponent(*
Next) &&
Next->Next &&
1453 Next->Next->is(tok::colon)))) {
1456 Tok->setType(TT_ObjCSelector);
1463 if (Style.isJavaScript() && !Contexts.back().IsExpression)
1464 Tok->setType(TT_JsTypeOperator);
1467 if (Style.isTableGen()) {
1469 if (!parseTableGenValue())
1471 if (CurrentToken && CurrentToken->is(Keywords.kw_then))
1476 CurrentToken->isOneOf(tok::kw_constexpr, tok::identifier)) {
1482 if (CurrentToken && CurrentToken->is(tok::l_paren)) {
1484 if (!parseParens(IsIf))
1489 if (Style.isJavaScript()) {
1491 if ((Prev && Prev->is(tok::period)) || (
Next &&
Next->is(tok::colon)))
1494 if (CurrentToken && CurrentToken->is(Keywords.kw_await))
1497 if (IsCpp && CurrentToken && CurrentToken->is(tok::kw_co_await))
1499 Contexts.back().ColonIsForRangeExpr =
true;
1500 if (!CurrentToken || CurrentToken->isNot(tok::l_paren))
1511 if (Prev && Prev->is(tok::r_paren) && Prev->MatchingParen &&
1512 Prev->MatchingParen->is(TT_OverloadedOperatorLParen)) {
1513 Prev->setType(TT_OverloadedOperator);
1514 Prev->MatchingParen->setType(TT_OverloadedOperator);
1515 Tok->setType(TT_OverloadedOperatorLParen);
1518 if (Style.isVerilog()) {
1524 auto IsInstancePort = [&]() {
1533 if (!Prev || !(PrevPrev = Prev->getPreviousNonComment()))
1536 if (Keywords.isVerilogIdentifier(*Prev) &&
1537 Keywords.isVerilogIdentifier(*PrevPrev)) {
1541 if (Prev->is(Keywords.kw_verilogHash) &&
1542 Keywords.isVerilogIdentifier(*PrevPrev)) {
1546 if (Keywords.isVerilogIdentifier(*Prev) && PrevPrev->is(tok::r_paren))
1549 if (Keywords.isVerilogIdentifier(*Prev) && PrevPrev->is(tok::comma)) {
1550 const FormatToken *PrevParen = PrevPrev->getPreviousNonComment();
1551 if (PrevParen && PrevParen->is(tok::r_paren) &&
1552 PrevParen->MatchingParen &&
1553 PrevParen->MatchingParen->is(TT_VerilogInstancePortLParen)) {
1560 if (IsInstancePort())
1561 Tok->setType(TT_VerilogInstancePortLParen);
1566 if (Line.MustBeDeclaration && Contexts.size() == 1 &&
1567 !Contexts.back().IsExpression && !Line.startsWith(TT_ObjCProperty) &&
1568 !Line.startsWith(tok::l_paren) &&
1569 Tok->isNoneOf(TT_TypeDeclarationParen, TT_RequiresExpressionLParen)) {
1571 (!Prev->isAttribute() &&
1572 Prev->isNoneOf(TT_RequiresClause, TT_LeadingJavaAnnotation,
1573 TT_BinaryOperator))) {
1574 Line.MightBeFunctionDecl =
true;
1575 Tok->MightBeFunctionDeclParen =
true;
1580 if (Style.isTableGen())
1581 Tok->setType(TT_TableGenListOpener);
1587 if (
Tok->is(TT_RequiresExpressionLBrace))
1589 }
else if (Style.isTextProto()) {
1590 if (Prev && Prev->isNot(TT_DictLiteral))
1591 Prev->setType(TT_SelectorName);
1593 Scopes.push_back(getScopeType(*
Tok));
1599 Tok->setType(TT_TemplateOpener);
1605 if (Style.isTextProto() ||
1606 (Style.Language == FormatStyle::LK_Proto && Prev &&
1607 Prev->isOneOf(TT_SelectorName, TT_DictLiteral))) {
1608 Tok->setType(TT_DictLiteral);
1609 if (Prev && Prev->isNot(TT_DictLiteral))
1610 Prev->setType(TT_SelectorName);
1612 if (Style.isTableGen())
1613 Tok->setType(TT_TemplateOpener);
1615 Tok->setType(TT_BinaryOperator);
1616 NonTemplateLess.insert(
Tok);
1626 if (!Scopes.empty())
1633 if (!Style.isTextProto() &&
Tok->is(TT_Unknown))
1634 Tok->setType(TT_BinaryOperator);
1635 if (Prev && Prev->is(TT_TemplateCloser))
1636 Tok->SpacesRequiredBefore = 1;
1638 case tok::kw_operator:
1639 if (Style.isProto())
1642 if (IsCpp && CurrentToken) {
1643 const auto *Info = CurrentToken->Tok.getIdentifierInfo();
1645 if (Info && !(CurrentToken->isPlacementOperator() ||
1646 CurrentToken->is(tok::kw_co_await) ||
1647 Info->isCPlusPlusOperatorKeyword())) {
1649 if (CurrentToken->startsSequence(tok::kw_decltype, tok::l_paren,
1650 tok::kw_auto, tok::r_paren)) {
1652 LParen = CurrentToken->Next->Next->Next->Next;
1655 for (LParen = CurrentToken->Next;
1656 LParen && LParen->isNot(tok::l_paren); LParen = LParen->Next) {
1657 if (LParen->isPointerOrReference())
1658 LParen->setFinalizedType(TT_PointerOrReference);
1661 if (LParen && LParen->is(tok::l_paren)) {
1662 if (!Contexts.back().IsExpression) {
1663 Tok->setFinalizedType(TT_FunctionDeclarationName);
1664 LParen->setFinalizedType(TT_FunctionDeclarationLParen);
1670 while (CurrentToken &&
1671 CurrentToken->isNoneOf(tok::l_paren, tok::semi, tok::r_paren)) {
1672 if (CurrentToken->isOneOf(tok::star, tok::amp))
1673 CurrentToken->setType(TT_PointerOrReference);
1674 auto Next = CurrentToken->getNextNonComment();
1677 if (
Next->is(tok::less))
1683 auto Previous = CurrentToken->getPreviousNonComment();
1685 if (CurrentToken->is(tok::comma) &&
Previous->isNot(tok::kw_operator))
1687 if (
Previous->isOneOf(TT_BinaryOperator, TT_UnaryOperator, tok::comma,
1689 Previous->isPointerOrReference() ||
1691 Previous->TokenText.starts_with(
"\"\"")) {
1692 Previous->setType(TT_OverloadedOperator);
1693 if (CurrentToken->isOneOf(tok::less, tok::greater))
1697 if (CurrentToken && CurrentToken->is(tok::l_paren))
1698 CurrentToken->setType(TT_OverloadedOperatorLParen);
1699 if (CurrentToken && CurrentToken->Previous->is(TT_BinaryOperator))
1700 CurrentToken->Previous->setType(TT_OverloadedOperator);
1703 if (Style.isJavaScript() &&
Next &&
1704 Next->isOneOf(tok::semi, tok::comma, tok::colon, tok::r_paren,
1705 tok::r_brace, tok::r_square)) {
1710 Tok->setType(TT_JsTypeOptionalQuestion);
1715 if (Line.MustBeDeclaration && !Contexts.back().IsExpression &&
1716 Style.isJavaScript()) {
1719 if (Style.isCSharp()) {
1722 if (
Next && (
Next->isOneOf(tok::r_paren, tok::greater) ||
1723 Next->startsSequence(tok::identifier, tok::semi) ||
1724 Next->startsSequence(tok::identifier, tok::equal))) {
1725 Tok->setType(TT_CSharpNullable);
1734 if (!Contexts.back().IsExpression && Line.MustBeDeclaration &&
1735 (!
Next ||
Next->isNoneOf(tok::identifier, tok::string_literal) ||
1736 !
Next->Next ||
Next->Next->isNoneOf(tok::colon, tok::question))) {
1737 Tok->setType(TT_CSharpNullable);
1743 case tok::kw_template:
1744 parseTemplateDeclaration();
1747 switch (Contexts.back().ContextType) {
1748 case Context::CtorInitializer:
1749 Tok->setType(TT_CtorInitializerComma);
1751 case Context::InheritanceList:
1752 Tok->setType(TT_InheritanceComma);
1754 case Context::VerilogInstancePortList:
1755 Tok->setType(TT_VerilogInstancePortComma);
1758 if (Style.isVerilog() && Contexts.size() == 1 &&
1759 Line.startsWith(Keywords.kw_assign)) {
1760 Tok->setFinalizedType(TT_VerilogAssignComma);
1761 }
else if (Contexts.back().FirstStartOfName &&
1762 (Contexts.size() == 1 || startsWithInitStatement(Line))) {
1763 Contexts.back().FirstStartOfName->PartOfMultiVariableDeclStmt =
true;
1764 Line.IsMultiVariableDeclStmt =
true;
1768 if (Contexts.back().ContextType == Context::ForEachMacro)
1769 Contexts.back().IsExpression =
true;
1771 case tok::kw_default:
1773 if (Style.isVerilog() && Keywords.isVerilogEndOfLabel(*
Tok) &&
1774 (Line.Level > 1 || (!Line.InPPDirective && Line.Level > 0))) {
1778 case tok::identifier:
1779 if (
Tok->isOneOf(Keywords.kw___has_include,
1780 Keywords.kw___has_include_next)) {
1784 if (
Next &&
Next->is(tok::l_paren) && Prev &&
1785 Prev->isOneOf(tok::kw___cdecl, tok::kw___stdcall,
1786 tok::kw___fastcall, tok::kw___thiscall,
1787 tok::kw___regcall, tok::kw___vectorcall)) {
1788 Tok->setFinalizedType(TT_FunctionDeclarationName);
1789 Next->setFinalizedType(TT_FunctionDeclarationLParen);
1791 }
else if (Style.isCSharp()) {
1792 if (
Tok->is(Keywords.kw_where) &&
Next &&
Next->isNot(tok::l_paren)) {
1793 Tok->setType(TT_CSharpGenericTypeConstraint);
1794 parseCSharpGenericTypeConstraint();
1796 Line.IsContinuation =
true;
1798 }
else if (Style.isTableGen()) {
1799 if (
Tok->is(Keywords.kw_assert)) {
1800 if (!parseTableGenValue())
1802 }
else if (
Tok->isOneOf(Keywords.kw_def, Keywords.kw_defm) &&
1803 (!
Next ||
Next->isNoneOf(tok::colon, tok::l_brace))) {
1805 if (!parseTableGenValue(
true))
1809 if (Style.AllowBreakBeforeQtProperty &&
1810 Contexts.back().ContextType == Context::QtProperty &&
1811 Tok->isQtProperty()) {
1812 Tok->setFinalizedType(TT_QtProperty);
1816 if (
Tok->isNot(TT_LambdaArrow) && Prev && Prev->is(tok::kw_noexcept))
1817 Tok->setType(TT_TrailingReturnArrow);
1821 if (Style.isTableGen() && !parseTableGenValue())
1830 void parseCSharpGenericTypeConstraint() {
1831 int OpenAngleBracketsCount = 0;
1832 while (CurrentToken) {
1833 if (CurrentToken->is(tok::less)) {
1835 CurrentToken->setType(TT_TemplateOpener);
1836 ++OpenAngleBracketsCount;
1838 }
else if (CurrentToken->is(tok::greater)) {
1839 CurrentToken->setType(TT_TemplateCloser);
1840 --OpenAngleBracketsCount;
1842 }
else if (CurrentToken->is(tok::comma) && OpenAngleBracketsCount == 0) {
1845 CurrentToken->setType(TT_CSharpGenericTypeConstraintComma);
1847 }
else if (CurrentToken->is(Keywords.kw_where)) {
1848 CurrentToken->setType(TT_CSharpGenericTypeConstraint);
1850 }
else if (CurrentToken->is(tok::colon)) {
1851 CurrentToken->setType(TT_CSharpGenericTypeConstraintColon);
1859 void parseIncludeDirective() {
1860 if (CurrentToken && CurrentToken->is(tok::less)) {
1862 while (CurrentToken) {
1865 if (CurrentToken->isNot(tok::comment) &&
1866 !CurrentToken->TokenText.starts_with(
"//")) {
1867 CurrentToken->setType(TT_ImplicitStringLiteral);
1874 void parseWarningOrError() {
1879 while (CurrentToken) {
1880 CurrentToken->setType(TT_ImplicitStringLiteral);
1885 void parsePragma() {
1888 CurrentToken->isOneOf(Keywords.kw_mark, Keywords.kw_option,
1889 Keywords.kw_region)) {
1890 bool IsMarkOrRegion =
1891 CurrentToken->isOneOf(Keywords.kw_mark, Keywords.kw_region);
1894 while (CurrentToken) {
1895 if (IsMarkOrRegion || CurrentToken->Previous->is(TT_BinaryOperator))
1896 CurrentToken->setType(TT_ImplicitStringLiteral);
1902 void parseHasInclude() {
1903 if (!CurrentToken || CurrentToken->isNot(tok::l_paren))
1906 parseIncludeDirective();
1910 LineType parsePreprocessorDirective() {
1911 bool IsFirstToken = CurrentToken->IsFirst;
1917 if (Style.isJavaScript() && IsFirstToken) {
1921 while (CurrentToken) {
1923 CurrentToken->setType(TT_ImplicitStringLiteral);
1929 if (CurrentToken->is(tok::numeric_constant)) {
1930 CurrentToken->SpacesRequiredBefore = 1;
1935 if (!CurrentToken->Tok.getIdentifierInfo())
1939 if (Style.isVerilog() && !Keywords.isVerilogPPDirective(*CurrentToken))
1941 switch (CurrentToken->Tok.getIdentifierInfo()->getPPKeywordID()) {
1942 case tok::pp_include:
1943 case tok::pp_include_next:
1944 case tok::pp_import:
1946 parseIncludeDirective();
1950 case tok::pp_warning:
1951 parseWarningOrError();
1953 case tok::pp_pragma:
1958 Contexts.back().IsExpression =
true;
1961 CurrentToken->SpacesRequiredBefore = 1;
1967 while (CurrentToken) {
1970 if (
Tok->is(tok::l_paren)) {
1972 }
else if (
Tok->isOneOf(Keywords.kw___has_include,
1973 Keywords.kw___has_include_next)) {
1984 NonTemplateLess.clear();
1985 if (!Line.InMacroBody && CurrentToken->is(tok::hash)) {
1989 auto Type = parsePreprocessorDirective();
1997 IdentifierInfo *Info = CurrentToken->Tok.getIdentifierInfo();
1998 if ((Style.isJava() && CurrentToken->is(Keywords.kw_package)) ||
1999 (!Style.isVerilog() && Info &&
2000 Info->getPPKeywordID() == tok::pp_import && CurrentToken->Next &&
2001 CurrentToken->Next->isOneOf(tok::string_literal, tok::identifier,
2004 parseIncludeDirective();
2010 if (CurrentToken->is(tok::less) && Line.Last->is(tok::greater)) {
2011 parseIncludeDirective();
2017 if (Style.Language == FormatStyle::LK_Proto && Line.Level == 0 &&
2018 CurrentToken->isOneOf(Keywords.kw_option, Keywords.kw_package)) {
2020 if (CurrentToken && CurrentToken->is(tok::identifier)) {
2021 while (CurrentToken)
2027 bool KeywordVirtualFound =
false;
2028 bool ImportStatement =
false;
2031 if (Style.isJavaScript() && CurrentToken->is(Keywords.kw_import))
2032 ImportStatement =
true;
2034 while (CurrentToken) {
2035 if (CurrentToken->is(tok::kw_virtual))
2036 KeywordVirtualFound =
true;
2037 if (Style.isJavaScript()) {
2044 if (Line.First->is(tok::kw_export) &&
2045 CurrentToken->is(Keywords.kw_from) && CurrentToken->Next &&
2046 CurrentToken->Next->isStringLiteral()) {
2047 ImportStatement =
true;
2049 if (isClosureImportStatement(*CurrentToken))
2050 ImportStatement =
true;
2052 if (!consumeToken())
2060 if (KeywordVirtualFound)
2062 if (ImportStatement)
2065 if (Line.startsWith(TT_ObjCMethodSpecifier)) {
2066 if (Contexts.back().FirstObjCSelectorName) {
2067 Contexts.back().FirstObjCSelectorName->LongestObjCSelectorName =
2068 Contexts.back().LongestObjCSelectorName;
2073 for (
const auto &ctx : Contexts)
2074 if (ctx.ContextType == Context::StructArrayInitializer)
2084 return Tok.TokenText ==
"goog" &&
Tok.Next &&
Tok.Next->is(tok::period) &&
2086 (
Tok.Next->Next->TokenText ==
"module" ||
2087 Tok.Next->Next->TokenText ==
"provide" ||
2088 Tok.Next->Next->TokenText ==
"require" ||
2089 Tok.Next->Next->TokenText ==
"requireType" ||
2090 Tok.Next->Next->TokenText ==
"forwardDeclare") &&
2091 Tok.Next->Next->Next &&
Tok.Next->Next->Next->is(tok::l_paren);
2094 void resetTokenMetadata() {
2100 if (!CurrentToken->isTypeFinalized() &&
2101 CurrentToken->isNoneOf(
2102 TT_LambdaLSquare, TT_LambdaLBrace, TT_AttributeMacro, TT_IfMacro,
2103 TT_ForEachMacro, TT_TypenameMacro, TT_FunctionLBrace,
2104 TT_ImplicitStringLiteral, TT_InlineASMBrace, TT_FatArrow,
2105 TT_LambdaArrow, TT_NamespaceMacro, TT_OverloadedOperator,
2106 TT_RegexLiteral, TT_TemplateString, TT_ObjCStringLiteral,
2107 TT_UntouchableMacroFunc, TT_StatementAttributeLikeMacro,
2108 TT_FunctionLikeOrFreestandingMacro, TT_ClassLBrace, TT_EnumLBrace,
2109 TT_RecordLBrace, TT_StructLBrace, TT_UnionLBrace, TT_RequiresClause,
2110 TT_RequiresClauseInARequiresExpression, TT_RequiresExpression,
2111 TT_RequiresExpressionLParen, TT_RequiresExpressionLBrace,
2112 TT_CompoundRequirementLBrace, TT_BracedListLBrace,
2113 TT_FunctionLikeMacro)) {
2114 CurrentToken->setType(TT_Unknown);
2116 CurrentToken->Role.reset();
2117 CurrentToken->MatchingParen =
nullptr;
2118 CurrentToken->FakeLParens.clear();
2119 CurrentToken->FakeRParens = 0;
2126 CurrentToken->NestingLevel = Contexts.size() - 1;
2127 CurrentToken->BindingStrength = Contexts.back().BindingStrength;
2128 modifyContext(*CurrentToken);
2129 determineTokenType(*CurrentToken);
2130 CurrentToken = CurrentToken->Next;
2132 resetTokenMetadata();
2140 : ContextKind(ContextKind), BindingStrength(BindingStrength),
2141 IsExpression(IsExpression) {}
2144 unsigned BindingStrength;
2146 unsigned LongestObjCSelectorName = 0;
2147 bool ColonIsForRangeExpr =
false;
2148 bool ColonIsDictLiteral =
false;
2149 bool ColonIsObjCMethodExpr =
false;
2152 bool CanBeExpression =
true;
2153 bool CaretFound =
false;
2154 bool InCpp11AttributeSpecifier =
false;
2155 bool InCSharpAttributeSpecifier =
false;
2156 bool InStaticAssertFirstArgument =
false;
2157 bool VerilogAssignmentFound =
false;
2160 bool VerilogMayBeConcatenation =
false;
2161 bool IsTableGenDAGArgList =
false;
2162 bool IsTableGenBangOpe =
false;
2163 bool IsTableGenCondOpe =
false;
2176 StructArrayInitializer,
2180 C11GenericSelection,
2183 VerilogInstancePortList,
2184 } ContextType = Unknown;
2189 struct ScopedContextCreator {
2190 AnnotatingParser &P;
2192 ScopedContextCreator(AnnotatingParser &P,
tok::TokenKind ContextKind,
2195 P.Contexts.push_back(Context(ContextKind,
2196 P.Contexts.back().BindingStrength + Increase,
2197 P.Contexts.back().IsExpression));
2200 ~ScopedContextCreator() {
2201 if (P.Style.AlignArrayOfStructures != FormatStyle::AIAS_None) {
2202 if (P.Contexts.back().ContextType == Context::StructArrayInitializer) {
2203 P.Contexts.pop_back();
2204 P.Contexts.back().ContextType = Context::StructArrayInitializer;
2208 P.Contexts.pop_back();
2213 auto AssignmentStartsExpression = [&]() {
2217 if (Line.First->isOneOf(tok::kw_using, tok::kw_return))
2219 if (Line.First->is(tok::kw_template)) {
2220 assert(Current.Previous);
2221 if (Current.Previous->is(tok::kw_operator)) {
2229 if (
Tok->isNot(TT_TemplateOpener)) {
2236 if (Contexts.back().ContextKind == tok::less) {
2237 assert(Current.Previous->Previous);
2238 return Current.Previous->Previous->isNoneOf(tok::kw_typename,
2242 Tok =
Tok->MatchingParen;
2245 Tok =
Tok->getNextNonComment();
2249 if (
Tok->isOneOf(tok::kw_class, tok::kw_enum, tok::kw_struct,
2259 if (Style.isJavaScript() &&
2260 (Line.startsWith(Keywords.kw_type, tok::identifier) ||
2261 Line.startsWith(tok::kw_export, Keywords.kw_type,
2262 tok::identifier))) {
2266 return !Current.Previous || Current.Previous->isNot(tok::kw_operator);
2269 if (AssignmentStartsExpression()) {
2270 Contexts.back().IsExpression =
true;
2271 if (!Line.startsWith(TT_UnaryOperator)) {
2274 Previous->Previous->isNoneOf(tok::comma, tok::semi);
2276 if (
Previous->isOneOf(tok::r_square, tok::r_paren, tok::greater)) {
2283 if (
Previous->isOneOf(TT_BinaryOperator, TT_UnaryOperator) &&
2285 Previous->Previous->isNot(tok::equal)) {
2286 Previous->setType(TT_PointerOrReference);
2290 }
else if (Current.is(tok::lessless) &&
2291 (!Current.Previous ||
2292 Current.Previous->isNot(tok::kw_operator))) {
2293 Contexts.back().IsExpression =
true;
2294 }
else if (Current.isOneOf(tok::kw_return, tok::kw_throw)) {
2295 Contexts.back().IsExpression =
true;
2296 }
else if (Current.is(TT_TrailingReturnArrow)) {
2297 Contexts.back().IsExpression =
false;
2298 }
else if (Current.isOneOf(TT_LambdaArrow, Keywords.kw_assert)) {
2299 Contexts.back().IsExpression = Style.isJava();
2300 }
else if (Current.Previous &&
2301 Current.Previous->is(TT_CtorInitializerColon)) {
2302 Contexts.back().IsExpression =
true;
2303 Contexts.back().ContextType = Context::CtorInitializer;
2304 }
else if (Current.Previous && Current.Previous->is(TT_InheritanceColon)) {
2305 Contexts.back().ContextType = Context::InheritanceList;
2306 }
else if (Current.isOneOf(tok::r_paren, tok::greater, tok::comma)) {
2310 Previous->setType(TT_PointerOrReference);
2312 if (Line.MustBeDeclaration &&
2313 Contexts.front().ContextType != Context::CtorInitializer) {
2314 Contexts.back().IsExpression =
false;
2316 }
else if (Current.is(tok::kw_new)) {
2317 Contexts.back().CanBeExpression =
false;
2318 }
else if (Current.is(tok::semi) ||
2319 (Current.is(tok::exclaim) && Current.Previous &&
2320 Current.Previous->isNot(tok::kw_operator))) {
2324 Contexts.back().IsExpression =
true;
2332 if (Current->is(tok::l_paren))
2334 if (Current->is(tok::r_paren))
2338 Current = Current->Next;
2343 static bool isDeductionGuide(
FormatToken &Current) {
2345 if (Current.Previous && Current.Previous->is(tok::r_paren) &&
2346 Current.startsSequence(tok::arrow, tok::identifier, tok::less)) {
2350 while (TemplateCloser) {
2352 if (TemplateCloser->is(tok::l_paren)) {
2354 TemplateCloser = untilMatchingParen(TemplateCloser);
2355 if (!TemplateCloser)
2358 if (TemplateCloser->is(tok::less))
2360 if (TemplateCloser->is(tok::greater))
2364 TemplateCloser = TemplateCloser->Next;
2368 if (TemplateCloser && TemplateCloser->Next &&
2369 TemplateCloser->Next->is(tok::semi) &&
2370 Current.Previous->MatchingParen) {
2374 Current.Previous->MatchingParen->Previous;
2376 return LeadingIdentifier &&
2377 LeadingIdentifier->TokenText == Current.Next->TokenText;
2384 if (Current.isNot(TT_Unknown)) {
2389 if ((Style.isJavaScript() || Style.isCSharp()) &&
2390 Current.is(tok::exclaim)) {
2391 if (Current.Previous) {
2393 Style.isJavaScript()
2394 ? Keywords.isJavaScriptIdentifier(
2395 *Current.Previous,
true)
2396 : Current.Previous->is(tok::identifier);
2398 Current.Previous->isOneOf(
2399 tok::kw_default, tok::kw_namespace, tok::r_paren, tok::r_square,
2400 tok::r_brace, tok::kw_false, tok::kw_true, Keywords.kw_type,
2401 Keywords.kw_get, Keywords.kw_init, Keywords.kw_set) ||
2402 Current.Previous->Tok.isLiteral()) {
2403 Current.setType(TT_NonNullAssertion);
2408 Current.Next->isOneOf(TT_BinaryOperator, Keywords.kw_as)) {
2409 Current.setType(TT_NonNullAssertion);
2417 if ((Style.isJavaScript() || Style.isJava()) &&
2418 Current.is(Keywords.kw_instanceof)) {
2419 Current.setType(TT_BinaryOperator);
2420 }
else if (isStartOfName(Current) &&
2421 (!Line.MightBeFunctionDecl || Current.NestingLevel != 0)) {
2422 Contexts.back().FirstStartOfName = &Current;
2423 Current.setType(TT_StartOfName);
2424 }
else if (Current.is(tok::semi)) {
2428 Contexts.back().FirstStartOfName =
nullptr;
2429 }
else if (Current.isOneOf(tok::kw_auto, tok::kw___auto_type)) {
2431 }
else if (Current.is(tok::arrow) && Style.isJava()) {
2432 Current.setType(TT_LambdaArrow);
2433 }
else if (Current.is(tok::arrow) && Style.isVerilog()) {
2435 Current.setType(TT_BinaryOperator);
2436 }
else if (Current.is(tok::arrow) && AutoFound &&
2437 Line.MightBeFunctionDecl && Current.NestingLevel == 0 &&
2438 Current.Previous->isNoneOf(tok::kw_operator, tok::identifier)) {
2440 Current.setType(TT_TrailingReturnArrow);
2441 }
else if (Current.is(tok::arrow) && Current.Previous &&
2442 Current.Previous->is(tok::r_brace) &&
2446 Current.setType(TT_TrailingReturnArrow);
2447 }
else if (isDeductionGuide(Current)) {
2449 Current.setType(TT_TrailingReturnArrow);
2450 }
else if (Current.isPointerOrReference()) {
2451 Current.setType(determineStarAmpUsage(
2453 (Contexts.back().CanBeExpression && Contexts.back().IsExpression) ||
2454 Contexts.back().InStaticAssertFirstArgument,
2455 Contexts.back().ContextType == Context::TemplateArgument));
2456 }
else if (Current.isOneOf(tok::minus, tok::plus, tok::caret) ||
2457 (Style.isVerilog() && Current.is(tok::pipe))) {
2458 Current.setType(determinePlusMinusCaretUsage(Current));
2459 if (Current.is(TT_UnaryOperator) && Current.is(tok::caret))
2460 Contexts.back().CaretFound =
true;
2461 }
else if (Current.isOneOf(tok::minusminus, tok::plusplus)) {
2462 Current.setType(determineIncrementUsage(Current));
2463 }
else if (Current.isOneOf(tok::exclaim, tok::tilde)) {
2464 Current.setType(TT_UnaryOperator);
2465 }
else if (Current.is(tok::question)) {
2466 if (Style.isJavaScript() && Line.MustBeDeclaration &&
2467 !Contexts.back().IsExpression) {
2470 Current.setType(TT_JsTypeOptionalQuestion);
2471 }
else if (Style.isTableGen()) {
2473 Current.setType(TT_Unknown);
2475 Current.setType(TT_ConditionalExpr);
2477 }
else if (Current.isBinaryOperator() &&
2478 (!Current.Previous || Current.Previous->isNot(tok::l_square)) &&
2479 (Current.isNot(tok::greater) && !Style.isTextProto())) {
2480 if (Style.isVerilog()) {
2481 if (Current.is(tok::lessequal) && Contexts.size() == 1 &&
2482 !Contexts.back().VerilogAssignmentFound) {
2486 Current.setFinalizedType(TT_BinaryOperator);
2489 Contexts.back().VerilogAssignmentFound =
true;
2491 Current.setType(TT_BinaryOperator);
2492 }
else if (Current.is(tok::comment)) {
2493 if (Current.TokenText.starts_with(
"/*")) {
2494 if (Current.TokenText.ends_with(
"*/")) {
2495 Current.setType(TT_BlockComment);
2499 Current.Tok.setKind(tok::unknown);
2502 Current.setType(TT_LineComment);
2504 }
else if (Current.is(tok::string_literal)) {
2505 if (Style.isVerilog() && Contexts.back().VerilogMayBeConcatenation &&
2506 Current.getPreviousNonComment() &&
2507 Current.getPreviousNonComment()->isOneOf(tok::comma, tok::l_brace) &&
2508 Current.getNextNonComment() &&
2509 Current.getNextNonComment()->isOneOf(tok::comma, tok::r_brace)) {
2510 Current.setType(TT_StringInConcatenation);
2512 }
else if (Current.is(tok::l_paren)) {
2513 if (lParenStartsCppCast(Current))
2514 Current.setType(TT_CppCastLParen);
2515 }
else if (Current.is(tok::r_paren)) {
2516 if (rParenEndsCast(Current))
2517 Current.setType(TT_CastRParen);
2518 if (Current.MatchingParen && Current.Next &&
2519 !Current.Next->isBinaryOperator() &&
2520 Current.Next->isNoneOf(
2521 tok::semi, tok::colon, tok::l_brace, tok::l_paren, tok::comma,
2522 tok::period, tok::arrow, tok::coloncolon, tok::kw_noexcept)) {
2523 if (
FormatToken *AfterParen = Current.MatchingParen->Next;
2524 AfterParen && AfterParen->isNot(tok::caret)) {
2526 if (
FormatToken *BeforeParen = Current.MatchingParen->Previous;
2527 BeforeParen && BeforeParen->is(tok::identifier) &&
2528 BeforeParen->isNot(TT_TypenameMacro) &&
2529 BeforeParen->TokenText == BeforeParen->TokenText.upper() &&
2530 (!BeforeParen->Previous ||
2531 BeforeParen->Previous->ClosesTemplateDeclaration ||
2532 BeforeParen->Previous->ClosesRequiresClause)) {
2533 Current.setType(TT_FunctionAnnotationRParen);
2537 }
else if (Current.is(tok::at) && Current.Next && !Style.isJavaScript() &&
2541 switch (Current.Next->Tok.getObjCKeywordID()) {
2542 case tok::objc_interface:
2543 case tok::objc_implementation:
2544 case tok::objc_protocol:
2545 Current.setType(TT_ObjCDecl);
2547 case tok::objc_property:
2548 Current.setType(TT_ObjCProperty);
2553 }
else if (Current.is(tok::period)) {
2554 FormatToken *PreviousNoComment = Current.getPreviousNonComment();
2555 if (PreviousNoComment &&
2556 PreviousNoComment->isOneOf(tok::comma, tok::l_brace)) {
2557 Current.setType(TT_DesignatedInitializerPeriod);
2558 }
else if (Style.isJava() && Current.Previous &&
2559 Current.Previous->isOneOf(TT_JavaAnnotation,
2560 TT_LeadingJavaAnnotation)) {
2561 Current.setType(Current.Previous->getType());
2563 }
else if (canBeObjCSelectorComponent(Current) &&
2566 Current.Previous && Current.Previous->is(TT_CastRParen) &&
2567 Current.Previous->MatchingParen &&
2568 Current.Previous->MatchingParen->Previous &&
2569 Current.Previous->MatchingParen->Previous->is(
2570 TT_ObjCMethodSpecifier)) {
2574 Current.setType(TT_SelectorName);
2575 }
else if (Current.isOneOf(tok::identifier, tok::kw_const, tok::kw_noexcept,
2576 tok::kw_requires) &&
2578 Current.Previous->isNoneOf(tok::equal, tok::at,
2579 TT_CtorInitializerComma,
2580 TT_CtorInitializerColon) &&
2581 Line.MightBeFunctionDecl && Contexts.size() == 1) {
2584 Current.setType(TT_TrailingAnnotation);
2585 }
else if ((Style.isJava() || Style.isJavaScript()) && Current.Previous) {
2586 if (Current.Previous->is(tok::at) &&
2587 Current.isNot(Keywords.kw_interface)) {
2591 Current.setType(TT_LeadingJavaAnnotation);
2593 Current.setType(TT_JavaAnnotation);
2594 }
else if (Current.Previous->is(tok::period) &&
2595 Current.Previous->isOneOf(TT_JavaAnnotation,
2596 TT_LeadingJavaAnnotation)) {
2597 Current.setType(Current.Previous->getType());
2609 if (Style.isVerilog())
2612 if (!
Tok.Previous ||
Tok.isNot(tok::identifier) ||
Tok.is(TT_ClassHeadName))
2615 if (
Tok.endsSequence(Keywords.kw_final, TT_ClassHeadName))
2618 if ((Style.isJavaScript() || Style.isJava()) &&
Tok.is(Keywords.kw_extends))
2621 if (
const auto *NextNonComment =
Tok.getNextNonComment();
2622 (!NextNonComment && !Line.InMacroBody) ||
2624 (NextNonComment->isPointerOrReference() ||
2625 NextNonComment->isOneOf(TT_ClassHeadName, tok::string_literal) ||
2626 (Line.InPragmaDirective && NextNonComment->is(tok::identifier))))) {
2630 if (
Tok.Previous->isOneOf(TT_LeadingJavaAnnotation, Keywords.kw_instanceof,
2634 if (Style.isJavaScript() &&
Tok.Previous->is(Keywords.kw_in))
2641 if (!Style.isJavaScript())
2642 while (PreviousNotConst && PreviousNotConst->is(tok::kw_const))
2643 PreviousNotConst = PreviousNotConst->getPreviousNonComment();
2645 if (!PreviousNotConst)
2648 if (PreviousNotConst->ClosesRequiresClause)
2651 if (Style.isTableGen()) {
2653 if (Keywords.isTableGenDefinition(*PreviousNotConst))
2656 if (Contexts.back().ContextKind != tok::l_brace)
2660 bool IsPPKeyword = PreviousNotConst->is(tok::identifier) &&
2661 PreviousNotConst->Previous &&
2662 PreviousNotConst->Previous->is(tok::hash);
2664 if (PreviousNotConst->is(TT_TemplateCloser)) {
2665 return PreviousNotConst && PreviousNotConst->MatchingParen &&
2666 PreviousNotConst->MatchingParen->Previous &&
2667 PreviousNotConst->MatchingParen->Previous->isNoneOf(
2668 tok::period, tok::kw_template);
2671 if ((PreviousNotConst->is(tok::r_paren) &&
2672 PreviousNotConst->is(TT_TypeDeclarationParen)) ||
2673 PreviousNotConst->is(TT_AttributeRParen)) {
2682 if (PreviousNotConst->isOneOf(tok::identifier, tok::kw_auto) &&
2683 PreviousNotConst->isNot(TT_StatementAttributeLikeMacro)) {
2688 if (PreviousNotConst->is(TT_PointerOrReference) ||
2689 PreviousNotConst->endsSequence(tok::coloncolon,
2690 TT_PointerOrReference)) {
2695 if (PreviousNotConst->isTypeName(LangOpts))
2699 if (Style.isJava() && PreviousNotConst->is(tok::r_square))
2703 return Style.isJavaScript() && PreviousNotConst->is(tok::kw_const);
2713 if (LeftOfParens && LeftOfParens->is(TT_TemplateCloser) &&
2714 LeftOfParens->MatchingParen) {
2715 auto *Prev = LeftOfParens->MatchingParen->getPreviousNonComment();
2717 Prev->isOneOf(tok::kw_const_cast, tok::kw_dynamic_cast,
2718 tok::kw_reinterpret_cast, tok::kw_static_cast)) {
2729 assert(
Tok.is(tok::r_paren));
2731 if (!
Tok.MatchingParen || !
Tok.Previous)
2735 if (!IsCpp && !Style.isCSharp() && !Style.isJava())
2738 const auto *LParen =
Tok.MatchingParen;
2739 const auto *BeforeRParen =
Tok.Previous;
2740 const auto *AfterRParen =
Tok.Next;
2743 if (BeforeRParen == LParen || !AfterRParen)
2746 if (LParen->is(TT_OverloadedOperatorLParen))
2749 auto *LeftOfParens = LParen->getPreviousNonComment();
2753 if (LeftOfParens->is(tok::r_paren) &&
2754 LeftOfParens->isNot(TT_CastRParen)) {
2755 if (!LeftOfParens->MatchingParen ||
2756 !LeftOfParens->MatchingParen->Previous) {
2759 LeftOfParens = LeftOfParens->MatchingParen->Previous;
2762 if (LeftOfParens->is(tok::r_square)) {
2765 if (
Tok->isNot(tok::r_square))
2768 Tok =
Tok->getPreviousNonComment();
2769 if (!
Tok ||
Tok->isNot(tok::l_square))
2772 Tok =
Tok->getPreviousNonComment();
2773 if (!
Tok ||
Tok->isNot(tok::kw_delete))
2777 if (
FormatToken *MaybeDelete = MayBeArrayDelete(LeftOfParens))
2778 LeftOfParens = MaybeDelete;
2784 if (LeftOfParens->Tok.getIdentifierInfo() && LeftOfParens->Previous &&
2785 LeftOfParens->Previous->is(tok::kw_operator)) {
2791 if (LeftOfParens->Tok.getIdentifierInfo() &&
2792 LeftOfParens->isNoneOf(Keywords.kw_in, tok::kw_return, tok::kw_case,
2793 tok::kw_delete, tok::kw_throw)) {
2799 if (LeftOfParens->isOneOf(tok::at, tok::r_square, TT_OverloadedOperator,
2800 TT_TemplateCloser, tok::ellipsis)) {
2805 if (AfterRParen->is(tok::question) ||
2806 (AfterRParen->is(tok::ampamp) && !BeforeRParen->isTypeName(LangOpts))) {
2811 if (AfterRParen->is(Keywords.kw_in) && Style.isCSharp())
2816 if (AfterRParen->isOneOf(tok::kw_noexcept, tok::kw_volatile, tok::kw_const,
2817 tok::kw_requires, tok::kw_throw, tok::arrow,
2818 Keywords.kw_override, Keywords.kw_final) ||
2819 isCppAttribute(IsCpp, *AfterRParen)) {
2825 if (Style.isJava() && AfterRParen->is(tok::l_paren))
2829 if (AfterRParen->isOneOf(tok::kw_sizeof, tok::kw_alignof) ||
2830 (AfterRParen->Tok.isLiteral() &&
2831 AfterRParen->isNot(tok::string_literal))) {
2836 if (
Tok.isNot(TT_TemplateCloser))
2838 const auto *
Less =
Tok.MatchingParen;
2841 const auto *BeforeLess =
Less->getPreviousNonComment();
2842 return BeforeLess && BeforeLess->isNot(TT_VariableTemplate);
2846 auto IsQualifiedPointerOrReference = [](
const FormatToken *
T,
2847 const LangOptions &LangOpts) {
2849 assert(!
T->isTypeName(LangOpts) &&
"Should have already been checked");
2853 if (
T->is(TT_AttributeRParen)) {
2855 assert(
T->is(tok::r_paren));
2856 assert(
T->MatchingParen);
2857 assert(
T->MatchingParen->is(tok::l_paren));
2858 assert(
T->MatchingParen->is(TT_AttributeLParen));
2859 if (
const auto *
Tok =
T->MatchingParen->Previous;
2860 Tok &&
Tok->isAttribute()) {
2864 }
else if (
T->is(TT_AttributeRSquare)) {
2866 if (
T->MatchingParen &&
T->MatchingParen->Previous) {
2867 T =
T->MatchingParen->Previous;
2870 }
else if (
T->canBePointerOrReferenceQualifier()) {
2876 return T &&
T->is(TT_PointerOrReference);
2879 bool ParensAreType = IsNonVariableTemplate(*BeforeRParen) ||
2880 BeforeRParen->is(TT_TypeDeclarationParen) ||
2881 BeforeRParen->isTypeName(LangOpts) ||
2882 IsQualifiedPointerOrReference(BeforeRParen, LangOpts);
2883 bool ParensCouldEndDecl =
2884 AfterRParen->isOneOf(tok::equal, tok::semi, tok::l_brace, tok::greater);
2885 if (ParensAreType && !ParensCouldEndDecl)
2896 for (
const auto *Token = LParen->Next; Token != &
Tok; Token = Token->Next)
2897 if (Token->is(TT_BinaryOperator))
2902 if (AfterRParen->isOneOf(tok::identifier, tok::kw_this))
2906 if (AfterRParen->is(tok::l_paren)) {
2907 for (
const auto *Prev = BeforeRParen; Prev->is(tok::identifier);) {
2908 Prev = Prev->Previous;
2909 if (Prev->is(tok::coloncolon))
2910 Prev = Prev->Previous;
2916 if (!AfterRParen->Next)
2919 if (AfterRParen->is(tok::l_brace) &&
2927 const bool NextIsAmpOrStar = AfterRParen->isOneOf(tok::amp, tok::star);
2928 if (!(AfterRParen->isUnaryOperator() || NextIsAmpOrStar) ||
2929 AfterRParen->is(tok::plus) ||
2930 AfterRParen->Next->isNoneOf(tok::identifier, tok::numeric_constant)) {
2934 if (NextIsAmpOrStar &&
2935 (AfterRParen->Next->is(tok::numeric_constant) || Line.InPPDirective)) {
2939 if (Line.InPPDirective && AfterRParen->is(tok::minus))
2942 const auto *Prev = BeforeRParen;
2945 if (Prev->is(tok::r_paren)) {
2946 if (Prev->is(TT_CastRParen))
2948 Prev = Prev->MatchingParen;
2951 Prev = Prev->Previous;
2952 if (!Prev || Prev->isNot(tok::r_paren))
2954 Prev = Prev->MatchingParen;
2955 return Prev && Prev->is(TT_FunctionTypeLParen);
2959 for (Prev = BeforeRParen; Prev != LParen; Prev = Prev->Previous)
2960 if (Prev->isNoneOf(tok::kw_const, tok::identifier, tok::coloncolon))
2978 if (PrevToken->isOneOf(
2979 TT_ConditionalExpr, tok::l_paren, tok::comma, tok::colon, tok::semi,
2980 tok::equal, tok::question, tok::l_square, tok::l_brace,
2981 tok::kw_case, tok::kw_co_await, tok::kw_co_return, tok::kw_co_yield,
2982 tok::kw_delete, tok::kw_return, tok::kw_throw)) {
2989 if (PrevToken->is(tok::kw_sizeof))
2993 if (PrevToken->isOneOf(TT_CastRParen, TT_UnaryOperator))
2997 if (PrevToken->is(TT_BinaryOperator))
3005 bool InTemplateArgument) {
3006 if (Style.isJavaScript())
3007 return TT_BinaryOperator;
3010 if (Style.isCSharp() &&
Tok.is(tok::ampamp))
3011 return TT_BinaryOperator;
3013 if (Style.isVerilog()) {
3018 if (
Tok.is(tok::star))
3019 return TT_BinaryOperator;
3020 return determineUnaryOperatorByUsage(
Tok) ? TT_UnaryOperator
3021 : TT_BinaryOperator;
3026 return TT_UnaryOperator;
3027 if (PrevToken->isTypeName(LangOpts))
3028 return TT_PointerOrReference;
3029 if (PrevToken->isPlacementOperator() &&
Tok.is(tok::ampamp))
3030 return TT_BinaryOperator;
3032 auto *NextToken =
Tok.getNextNonComment();
3034 return TT_PointerOrReference;
3035 if (NextToken->is(tok::greater)) {
3036 NextToken->setFinalizedType(TT_TemplateCloser);
3037 return TT_PointerOrReference;
3040 if (InTemplateArgument && NextToken->is(tok::kw_noexcept))
3041 return TT_BinaryOperator;
3043 if (NextToken->isOneOf(tok::arrow, tok::equal, tok::comma, tok::r_paren,
3044 TT_RequiresClause) ||
3045 (NextToken->is(tok::kw_noexcept) && !IsExpression) ||
3046 NextToken->canBePointerOrReferenceQualifier() ||
3047 (NextToken->is(tok::l_brace) && !NextToken->getNextNonComment())) {
3048 return TT_PointerOrReference;
3051 if (PrevToken->is(tok::coloncolon))
3052 return TT_PointerOrReference;
3054 if (PrevToken->is(tok::r_paren) && PrevToken->is(TT_TypeDeclarationParen))
3055 return TT_PointerOrReference;
3057 if (determineUnaryOperatorByUsage(
Tok))
3058 return TT_UnaryOperator;
3060 if (NextToken->is(tok::l_square) && NextToken->isNot(TT_LambdaLSquare))
3061 return TT_PointerOrReference;
3062 if (NextToken->is(tok::kw_operator) && !IsExpression)
3063 return TT_PointerOrReference;
3064 if (NextToken->isOneOf(tok::comma, tok::semi))
3065 return TT_PointerOrReference;
3077 if (PrevToken->is(tok::r_brace) &&
Tok.is(tok::star) &&
3078 !PrevToken->MatchingParen) {
3079 return TT_PointerOrReference;
3082 if (PrevToken->endsSequence(tok::r_square, tok::l_square, tok::kw_delete))
3083 return TT_UnaryOperator;
3085 if (PrevToken->Tok.isLiteral() ||
3086 PrevToken->isOneOf(tok::r_paren, tok::r_square, tok::kw_true,
3087 tok::kw_false, tok::r_brace)) {
3088 return TT_BinaryOperator;
3092 while (NextNonParen && NextNonParen->is(tok::l_paren))
3093 NextNonParen = NextNonParen->getNextNonComment();
3094 if (NextNonParen && (NextNonParen->Tok.isLiteral() ||
3095 NextNonParen->isOneOf(tok::kw_true, tok::kw_false) ||
3096 NextNonParen->isUnaryOperator())) {
3097 return TT_BinaryOperator;
3103 if (InTemplateArgument && NextToken->Tok.isAnyIdentifier())
3104 return TT_BinaryOperator;
3108 if (
Tok.is(tok::ampamp) &&
3109 NextToken->isOneOf(tok::l_paren, tok::star, tok::amp)) {
3110 return TT_BinaryOperator;
3117 if (NextToken->Tok.isAnyIdentifier()) {
3118 auto *NextNextToken = NextToken->getNextNonComment();
3119 if (NextNextToken) {
3120 if (NextNextToken->is(tok::arrow))
3121 return TT_BinaryOperator;
3122 if (NextNextToken->isPointerOrReference() &&
3123 !NextToken->isObjCLifetimeQualifier(Style)) {
3124 NextNextToken->setFinalizedType(TT_BinaryOperator);
3125 return TT_BinaryOperator;
3132 if (IsExpression && !Contexts.back().CaretFound &&
3133 Line.getFirstNonComment()->isNot(
3134 TT_RequiresClauseInARequiresExpression)) {
3135 return TT_BinaryOperator;
3139 if (!Scopes.empty() && Scopes.back() ==
ST_Class)
3140 return TT_PointerOrReference;
3143 auto IsChainedOperatorAmpOrMember = [](
const FormatToken *token) {
3144 return !token || token->isOneOf(tok::amp, tok::period, tok::arrow,
3145 tok::arrowstar, tok::periodstar);
3150 if (
Tok.is(tok::amp) && PrevToken->Tok.isAnyIdentifier() &&
3151 IsChainedOperatorAmpOrMember(PrevToken->getPreviousNonComment()) &&
3152 NextToken && NextToken->Tok.isAnyIdentifier()) {
3153 if (
auto NextNext = NextToken->getNextNonComment();
3155 (IsChainedOperatorAmpOrMember(NextNext) || NextNext->is(tok::semi))) {
3156 return TT_BinaryOperator;
3162 return TT_BinaryOperator;
3165 return TT_PointerOrReference;
3169 if (determineUnaryOperatorByUsage(
Tok))
3170 return TT_UnaryOperator;
3174 return TT_UnaryOperator;
3176 if (PrevToken->is(tok::at))
3177 return TT_UnaryOperator;
3180 return TT_BinaryOperator;
3186 if (!PrevToken || PrevToken->is(TT_CastRParen))
3187 return TT_UnaryOperator;
3188 if (PrevToken->isOneOf(tok::r_paren, tok::r_square, tok::identifier))
3189 return TT_TrailingUnaryOperator;
3191 return TT_UnaryOperator;
3194 SmallVector<Context, 8> Contexts;
3196 const FormatStyle &Style;
3197 AnnotatedLine &Line;
3201 LangOptions LangOpts;
3202 const AdditionalKeywords &Keywords;
3204 SmallVector<ScopeType> &Scopes;
3210 llvm::SmallPtrSet<FormatToken *, 16> NonTemplateLess;
3212 int TemplateDeclarationDepth;
3220class ExpressionParser {
3222 ExpressionParser(
const FormatStyle &Style,
const AdditionalKeywords &Keywords,
3223 AnnotatedLine &Line)
3224 : Style(Style), Keywords(Keywords), Line(Line), Current(Line.
First) {}
3227 void parse(
int Precedence = 0) {
3230 while (Current && (Current->is(tok::kw_return) ||
3231 (Current->is(tok::colon) &&
3232 Current->isOneOf(TT_ObjCMethodExpr, TT_DictLiteral)))) {
3236 if (!Current || Precedence > PrecedenceArrowAndPeriod)
3241 parseConditionalExpr();
3247 if (Precedence == PrecedenceUnaryOperator) {
3248 parseUnaryOperator();
3265 if (Style.isVerilog() && Precedence ==
prec::Comma) {
3266 VerilogFirstOfType =
3267 verilogGroupDecl(VerilogFirstOfType, LatestOperator);
3271 parse(Precedence + 1);
3273 int CurrentPrecedence = getCurrentPrecedence();
3274 if (Style.BreakBinaryOperations == FormatStyle::BBO_OnePerLine &&
3284 if (Precedence == CurrentPrecedence && Current &&
3285 Current->is(TT_SelectorName)) {
3287 addFakeParenthesis(Start,
prec::Level(Precedence));
3291 if ((Style.isCSharp() || Style.isJavaScript() || Style.isJava()) &&
3295 FormatToken *Prev = Current->getPreviousNonComment();
3296 if (Prev && Prev->is(tok::string_literal) &&
3297 (Prev == Start || Prev->endsSequence(tok::string_literal, tok::plus,
3298 TT_StringInConcatenation))) {
3299 Prev->setType(TT_StringInConcatenation);
3306 (Current->closesScope() &&
3307 (Current->MatchingParen || Current->is(TT_TemplateString))) ||
3308 (CurrentPrecedence != -1 && CurrentPrecedence < Precedence) ||
3317 if (Current->opensScope() ||
3318 Current->isOneOf(TT_RequiresClause,
3319 TT_RequiresClauseInARequiresExpression)) {
3322 while (Current && (!Current->closesScope() || Current->opensScope())) {
3329 if (CurrentPrecedence == Precedence) {
3331 LatestOperator->NextOperator = Current;
3332 LatestOperator = Current;
3336 next(Precedence > 0);
3341 if (Style.isVerilog() && Precedence ==
prec::Comma && VerilogFirstOfType)
3342 addFakeParenthesis(VerilogFirstOfType,
prec::Comma);
3344 if (LatestOperator && (Current || Precedence > 0)) {
3350 Start->Previous->isOneOf(TT_RequiresClause,
3351 TT_RequiresClauseInARequiresExpression))
3353 auto Ret = Current ? Current : Line.Last;
3354 while (!
Ret->ClosesRequiresClause &&
Ret->Previous)
3360 if (Precedence == PrecedenceArrowAndPeriod) {
3364 addFakeParenthesis(Start,
prec::Level(Precedence), End);
3372 int getCurrentPrecedence() {
3374 const FormatToken *NextNonComment = Current->getNextNonComment();
3375 if (Current->is(TT_ConditionalExpr))
3377 if (NextNonComment && Current->is(TT_SelectorName) &&
3378 (NextNonComment->isOneOf(TT_DictLiteral, TT_JsTypeColon) ||
3379 (Style.isProto() && NextNonComment->is(tok::less)))) {
3382 if (Current->is(TT_JsComputedPropertyName))
3384 if (Current->is(TT_LambdaArrow))
3386 if (Current->is(TT_FatArrow))
3388 if (Current->isOneOf(tok::semi, TT_InlineASMColon, TT_SelectorName) ||
3389 (Current->is(tok::comment) && NextNonComment &&
3390 NextNonComment->is(TT_SelectorName))) {
3393 if (Current->is(TT_RangeBasedForLoopColon))
3395 if ((Style.isJava() || Style.isJavaScript()) &&
3396 Current->is(Keywords.kw_instanceof)) {
3399 if (Style.isJavaScript() &&
3400 Current->isOneOf(Keywords.kw_in, Keywords.kw_as)) {
3403 if (Current->isOneOf(TT_BinaryOperator, tok::comma))
3404 return Current->getPrecedence();
3405 if (Current->isOneOf(tok::period, tok::arrow) &&
3406 Current->isNot(TT_TrailingReturnArrow)) {
3407 return PrecedenceArrowAndPeriod;
3409 if ((Style.isJava() || Style.isJavaScript()) &&
3410 Current->isOneOf(Keywords.kw_extends, Keywords.kw_implements,
3411 Keywords.kw_throws)) {
3416 if (Style.isVerilog() && Current->is(tok::colon))
3428 if (Start->MacroParent)
3431 Start->FakeLParens.push_back(Precedence);
3433 Start->StartsBinaryExpression =
true;
3434 if (!End && Current)
3435 End = Current->getPreviousNonComment();
3439 End->EndsBinaryExpression =
true;
3445 void parseUnaryOperator() {
3446 SmallVector<FormatToken *, 2> Tokens;
3447 while (Current && Current->is(TT_UnaryOperator)) {
3448 Tokens.push_back(Current);
3451 parse(PrecedenceArrowAndPeriod);
3458 void parseConditionalExpr() {
3459 while (Current && Current->isTrailingComment())
3463 if (!Current || Current->isNot(tok::question))
3467 if (!Current || Current->isNot(TT_ConditionalExpr))
3474 void next(
bool SkipPastLeadingComments =
true) {
3476 Current = Current->Next;
3478 (Current->NewlinesBefore == 0 || SkipPastLeadingComments) &&
3479 Current->isTrailingComment()) {
3480 Current = Current->Next;
3494 while (Start->startsSequence(tok::l_paren, tok::star)) {
3495 if (!(Start = Start->MatchingParen) ||
3496 !(Start = Start->getNextNonComment())) {
3503 if (
Tok->is(Keywords.kw_assign))
3504 Tok =
Tok->getNextNonComment();
3516 if (
Tok->is(tok::hash)) {
3521 Tok =
Tok->getNextNonComment();
3522 }
else if (
Tok->is(tok::hashhash)) {
3526 Tok =
Tok->getNextNonComment();
3527 }
else if (Keywords.isVerilogQualifier(*
Tok) ||
3528 Keywords.isVerilogIdentifier(*
Tok)) {
3532 while (
Tok &&
Tok->isOneOf(tok::period, tok::coloncolon) &&
3533 (
Tok =
Tok->getNextNonComment())) {
3534 if (Keywords.isVerilogIdentifier(*
Tok))
3535 Tok =
Tok->getNextNonComment();
3539 }
else if (
Tok->is(tok::l_paren)) {
3544 Keywords.kw_highz0, Keywords.kw_highz1, Keywords.kw_large,
3545 Keywords.kw_medium, Keywords.kw_pull0, Keywords.kw_pull1,
3546 Keywords.kw_small, Keywords.kw_strong0, Keywords.kw_strong1,
3547 Keywords.kw_supply0, Keywords.kw_supply1, Keywords.kw_weak0,
3548 Keywords.kw_weak1)) {
3549 Tok->setType(TT_VerilogStrength);
3550 Tok =
Tok->MatchingParen;
3552 Tok->setType(TT_VerilogStrength);
3553 Tok =
Tok->getNextNonComment();
3558 }
else if (
Tok->is(Keywords.kw_verilogHash)) {
3560 if (
Next->is(tok::l_paren))
3563 Tok =
Next->getNextNonComment();
3572 while (
Tok &&
Tok->is(tok::l_square) && (
Tok =
Tok->MatchingParen))
3573 Tok =
Tok->getNextNonComment();
3574 if (
Tok && (
Tok->is(tok::hash) || Keywords.isVerilogIdentifier(*
Tok)))
3583 First->setType(TT_VerilogDimensionedTypeName);
3584 }
else if (
First != Start) {
3592 if (TypedName->is(TT_Unknown))
3593 TypedName->setType(TT_StartOfName);
3595 if (FirstOfType && PreviousComma) {
3596 PreviousComma->setType(TT_VerilogTypeComma);
3597 addFakeParenthesis(FirstOfType,
prec::Comma, PreviousComma->Previous);
3600 FirstOfType = TypedName;
3607 while (Current && Current != FirstOfType) {
3608 if (Current->opensScope()) {
3619 const FormatStyle &Style;
3620 const AdditionalKeywords &Keywords;
3621 const AnnotatedLine &Line;
3631 assert(
Line->First);
3638 Line->First->OriginalColumn) {
3639 const bool PPDirectiveOrImportStmt =
3642 if (PPDirectiveOrImportStmt)
3647 Line->Level = Style.IndentPPDirectives < FormatStyle::PPDIS_BeforeHash &&
3648 PPDirectiveOrImportStmt
3650 : NextNonCommentLine->
Level;
3652 NextNonCommentLine =
Line->First->isNot(tok::r_brace) ?
Line :
nullptr;
3671 Tok =
Tok->getNextNonComment()) {
3673 if (
Tok->is(TT_AttributeLSquare)) {
3674 Tok =
Tok->MatchingParen;
3682 if (
Tok->is(tok::l_paren) &&
Tok->is(TT_Unknown) &&
Tok->MatchingParen) {
3690 if (
Tok->isOneOf(tok::kw_friend, tok::kw_inline, tok::kw_virtual,
3691 tok::kw_constexpr, tok::kw_consteval, tok::kw_explicit)) {
3696 if (
Tok->is(tok::coloncolon)) {
3703 while (
Tok->startsSequence(tok::identifier, tok::coloncolon)) {
3711 if (
Tok->is(tok::tilde)) {
3718 if (
Tok->isNot(tok::identifier) ||
Tok->isNot(TT_Unknown))
3729 assert(
Tok &&
Tok->is(tok::identifier));
3730 const auto *Prev =
Tok->Previous;
3732 if (Prev && Prev->is(tok::tilde))
3733 Prev = Prev->Previous;
3735 if (!Prev || !Prev->endsSequence(tok::coloncolon, tok::identifier))
3738 assert(Prev->Previous);
3739 return Prev->Previous->TokenText ==
Tok->TokenText;
3743 if (!
Line.InMacroBody)
3744 MacroBodyScopes.clear();
3746 auto &ScopeStack =
Line.InMacroBody ? MacroBodyScopes : Scopes;
3747 AnnotatingParser
Parser(Style,
Line, Keywords, ScopeStack);
3750 if (!
Line.Children.empty()) {
3753 for (
auto &Child :
Line.Children) {
3754 if (InRequiresExpression &&
3755 Child->First->isNoneOf(tok::kw_typename, tok::kw_requires,
3756 TT_CompoundRequirementLBrace)) {
3762 if (!ScopeStack.empty())
3763 ScopeStack.pop_back();
3776 ExpressionParser ExprParser(Style, Keywords,
Line);
3782 if (
Tok && ((!ScopeStack.empty() && ScopeStack.back() ==
ST_Class) ||
3784 Tok->setFinalizedType(TT_CtorDtorDeclName);
3785 assert(OpeningParen);
3790 if (
Line.startsWith(TT_ObjCMethodSpecifier))
3792 else if (
Line.startsWith(TT_ObjCDecl))
3794 else if (
Line.startsWith(TT_ObjCProperty))
3798 First->SpacesRequiredBefore = 1;
3799 First->CanBreakBefore =
First->MustBreakBefore;
3808 if (Current.
is(TT_FunctionDeclarationName))
3811 if (Current.
isNoneOf(tok::identifier, tok::kw_operator))
3814 const auto *Prev = Current.getPreviousNonComment();
3819 if (
const auto *PrevPrev =
Previous.getPreviousNonComment();
3820 PrevPrev && PrevPrev->is(TT_ObjCDecl)) {
3824 auto skipOperatorName =
3827 if (
Next->is(TT_OverloadedOperatorLParen))
3829 if (
Next->is(TT_OverloadedOperator))
3831 if (
Next->isPlacementOperator() ||
Next->is(tok::kw_co_await)) {
3834 Next->Next->startsSequence(tok::l_square, tok::r_square)) {
3839 if (
Next->startsSequence(tok::l_square, tok::r_square)) {
3844 if ((
Next->isTypeName(LangOpts) ||
Next->is(tok::identifier)) &&
3845 Next->Next &&
Next->Next->isPointerOrReference()) {
3850 if (
Next->is(TT_TemplateOpener) &&
Next->MatchingParen) {
3861 const bool IsCpp = LangOpts.CXXOperatorNames || LangOpts.C11;
3864 if (Current.
is(tok::kw_operator)) {
3865 if (
Line.startsWith(tok::kw_friend))
3867 if (
Previous.Tok.getIdentifierInfo() &&
3868 Previous.isNoneOf(tok::kw_return, tok::kw_co_return)) {
3873 assert(
Previous.MatchingParen->is(tok::l_paren));
3874 assert(
Previous.MatchingParen->is(TT_TypeDeclarationParen));
3883 while (
Next &&
Next->startsSequence(tok::hashhash, tok::identifier))
3886 if (
Next->is(TT_TemplateOpener) &&
Next->MatchingParen) {
3888 }
else if (
Next->is(tok::coloncolon)) {
3892 if (
Next->is(tok::kw_operator)) {
3893 Next = skipOperatorName(
Next->Next);
3896 if (
Next->isNot(tok::identifier))
3898 }
else if (isCppAttribute(IsCpp, *
Next)) {
3902 }
else if (
Next->is(tok::l_paren)) {
3911 if (!
Next ||
Next->isNot(tok::l_paren) || !
Next->MatchingParen)
3913 ClosingParen =
Next->MatchingParen;
3914 assert(ClosingParen->
is(tok::r_paren));
3916 if (
Line.Last->is(tok::l_brace))
3918 if (
Next->Next == ClosingParen)
3921 if (ClosingParen->
Next && ClosingParen->
Next->
is(TT_PointerOrReference))
3934 if (IsCpp &&
Next->Next &&
Next->Next->is(tok::identifier) &&
3935 !
Line.endsWith(tok::semi)) {
3941 if (
Tok->is(TT_TypeDeclarationParen))
3943 if (
Tok->isOneOf(tok::l_paren, TT_TemplateOpener) &&
Tok->MatchingParen) {
3944 Tok =
Tok->MatchingParen;
3947 if (
Tok->is(tok::kw_const) ||
Tok->isTypeName(LangOpts) ||
3948 Tok->isOneOf(TT_PointerOrReference, TT_StartOfName, tok::ellipsis)) {
3951 if (
Tok->isOneOf(tok::l_brace, TT_ObjCMethodExpr) ||
Tok->Tok.isLiteral())
3957bool TokenAnnotator::mustBreakForReturnType(
const AnnotatedLine &
Line)
const {
3958 assert(
Line.MightBeFunctionDecl);
3960 if ((Style.BreakAfterReturnType == FormatStyle::RTBS_TopLevel ||
3961 Style.BreakAfterReturnType == FormatStyle::RTBS_TopLevelDefinitions) &&
3966 switch (Style.BreakAfterReturnType) {
3967 case FormatStyle::RTBS_None:
3968 case FormatStyle::RTBS_Automatic:
3969 case FormatStyle::RTBS_ExceptShortType:
3971 case FormatStyle::RTBS_All:
3972 case FormatStyle::RTBS_TopLevel:
3974 case FormatStyle::RTBS_AllDefinitions:
3975 case FormatStyle::RTBS_TopLevelDefinitions:
3976 return Line.mightBeFunctionDefinition();
3986 Line.Computed =
true;
3994 :
Line.FirstStartColumn +
First->ColumnWidth;
3995 bool AlignArrayOfStructures =
3996 (Style.AlignArrayOfStructures != FormatStyle::AIAS_None &&
3998 if (AlignArrayOfStructures)
3999 calculateArrayInitializerColumnList(
Line);
4001 const auto *FirstNonComment =
Line.getFirstNonComment();
4002 bool SeenName =
false;
4003 bool LineIsFunctionDeclaration =
false;
4007 for (
auto *
Tok = FirstNonComment && FirstNonComment->isNot(tok::kw_using)
4008 ? FirstNonComment->Next
4011 if (
Tok->is(TT_StartOfName))
4013 if (
Tok->Previous->EndsCppAttributeGroup)
4014 AfterLastAttribute =
Tok;
4015 if (
const bool IsCtorOrDtor =
Tok->is(TT_CtorDtorDeclName);
4019 Tok->setFinalizedType(TT_FunctionDeclarationName);
4020 LineIsFunctionDeclaration =
true;
4024 assert(OpeningParen);
4025 if (OpeningParen->is(TT_Unknown))
4026 OpeningParen->setType(TT_FunctionDeclarationLParen);
4033 if ((LineIsFunctionDeclaration ||
4034 (FirstNonComment && FirstNonComment->is(TT_CtorDtorDeclName))) &&
4035 Line.endsWith(tok::semi, tok::r_brace)) {
4036 auto *
Tok =
Line.Last->Previous;
4037 while (
Tok->isNot(tok::r_brace))
4039 if (
auto *LBrace =
Tok->MatchingParen; LBrace && LBrace->is(TT_Unknown)) {
4040 assert(LBrace->is(tok::l_brace));
4043 LBrace->setFinalizedType(TT_FunctionLBrace);
4047 if (SeenName && AfterLastAttribute &&
4050 if (LineIsFunctionDeclaration)
4051 Line.ReturnTypeWrapped =
true;
4054 if (!LineIsFunctionDeclaration) {
4056 for (
const auto *
Tok = FirstNonComment;
Tok;
Tok =
Tok->Next) {
4057 if (
Tok->isNot(tok::kw_operator))
4061 }
while (
Tok &&
Tok->isNot(TT_OverloadedOperatorLParen));
4062 if (!
Tok || !
Tok->MatchingParen)
4064 const auto *LeftParen =
Tok;
4065 for (
Tok =
Tok->Next;
Tok &&
Tok != LeftParen->MatchingParen;
4067 if (
Tok->isNot(tok::identifier))
4070 const bool NextIsBinaryOperator =
4072 Next->Next->is(tok::identifier);
4073 if (!NextIsBinaryOperator)
4075 Next->setType(TT_BinaryOperator);
4079 }
else if (ClosingParen) {
4081 if (
Tok->is(TT_CtorInitializerColon))
4083 if (
Tok->is(tok::arrow)) {
4084 Tok->setType(TT_TrailingReturnArrow);
4087 if (
Tok->isNot(TT_TrailingAnnotation))
4090 if (!
Next ||
Next->isNot(tok::l_paren))
4099 if (
First->is(TT_ElseLBrace)) {
4100 First->CanBreakBefore =
true;
4101 First->MustBreakBefore =
true;
4104 bool InFunctionDecl =
Line.MightBeFunctionDecl;
4105 bool InParameterList =
false;
4106 for (
auto *Current =
First->Next; Current; Current = Current->Next) {
4108 if (Current->is(TT_LineComment)) {
4110 Current->SpacesRequiredBefore =
4111 (Style.Cpp11BracedListStyle == FormatStyle::BLS_AlignFirstComment &&
4112 !Style.SpacesInParensOptions.Other)
4115 }
else if (Prev->
is(TT_VerilogMultiLineListLParen)) {
4116 Current->SpacesRequiredBefore = 0;
4118 Current->SpacesRequiredBefore = Style.SpacesBeforeTrailingComments;
4128 if (!Current->HasUnescapedNewline) {
4131 if (
Parameter->isOneOf(tok::comment, tok::r_brace))
4134 if (
Parameter->Previous->isNot(TT_CtorInitializerComma) &&
4142 }
else if (!Current->Finalized && Current->SpacesRequiredBefore == 0 &&
4143 spaceRequiredBefore(
Line, *Current)) {
4144 Current->SpacesRequiredBefore = 1;
4149 Current->MustBreakBefore =
true;
4151 Current->MustBreakBefore =
4152 Current->MustBreakBefore || mustBreakBefore(
Line, *Current);
4153 if (!Current->MustBreakBefore && InFunctionDecl &&
4154 Current->is(TT_FunctionDeclarationName)) {
4155 Current->MustBreakBefore = mustBreakForReturnType(
Line);
4159 Current->CanBreakBefore =
4160 Current->MustBreakBefore || canBreakBefore(
Line, *Current);
4162 if (Current->is(TT_FunctionDeclarationLParen)) {
4163 InParameterList =
true;
4164 }
else if (Current->is(tok::r_paren)) {
4165 const auto *LParen = Current->MatchingParen;
4166 if (LParen && LParen->is(TT_FunctionDeclarationLParen))
4167 InParameterList =
false;
4168 }
else if (InParameterList &&
4169 Current->endsSequence(TT_AttributeMacro,
4170 TT_PointerOrReference)) {
4171 Current->CanBreakBefore =
false;
4174 unsigned ChildSize = 0;
4177 ChildSize = LastOfChild.isTrailingComment() ? Style.ColumnLimit
4180 if (Current->MustBreakBefore || Prev->
Children.size() > 1 ||
4182 Prev->
Children[0]->First->MustBreakBefore) ||
4183 Current->IsMultiline) {
4184 Current->TotalLength = Prev->
TotalLength + Style.ColumnLimit;
4186 Current->TotalLength = Prev->
TotalLength + Current->ColumnWidth +
4187 ChildSize + Current->SpacesRequiredBefore;
4190 if (Current->is(TT_ControlStatementLBrace)) {
4191 if (Style.ColumnLimit > 0 &&
4192 Style.BraceWrapping.AfterControlStatement ==
4193 FormatStyle::BWACS_MultiLine &&
4194 Line.Level * Style.IndentWidth +
Line.Last->TotalLength >
4195 Style.ColumnLimit) {
4196 Current->CanBreakBefore =
true;
4197 Current->MustBreakBefore =
true;
4199 }
else if (Current->is(TT_CtorInitializerColon)) {
4200 InFunctionDecl =
false;
4212 Current->SplitPenalty = splitPenalty(
Line, *Current, InFunctionDecl);
4213 if (Style.Language == FormatStyle::LK_ObjC &&
4214 Current->is(TT_SelectorName) && Current->ParameterIndex > 0) {
4215 if (Current->ParameterIndex == 1)
4216 Current->SplitPenalty += 5 * Current->BindingStrength;
4218 Current->SplitPenalty += 20 * Current->BindingStrength;
4222 calculateUnbreakableTailLengths(
Line);
4224 for (
auto *Current =
First; Current; Current = Current->Next) {
4226 Current->Role->precomputeFormattingInfos(Current);
4227 if (Current->MatchingParen &&
4228 Current->MatchingParen->opensBlockOrBlockTypeList(Style) &&
4233 if (Current->opensBlockOrBlockTypeList(Style))
4237 LLVM_DEBUG({ printDebugInfo(
Line); });
4240void TokenAnnotator::calculateUnbreakableTailLengths(
4247 Current->
isOneOf(tok::comment, tok::string_literal)) {
4257void TokenAnnotator::calculateArrayInitializerColumnList(
4261 auto *CurrentToken =
Line.First;
4262 CurrentToken->ArrayInitializerLineStart =
true;
4264 while (CurrentToken && CurrentToken !=
Line.Last) {
4265 if (CurrentToken->is(tok::l_brace)) {
4266 CurrentToken->IsArrayInitializer =
true;
4267 if (CurrentToken->Next)
4268 CurrentToken->Next->MustBreakBefore =
true;
4270 calculateInitializerColumnList(
Line, CurrentToken->Next, Depth + 1);
4272 CurrentToken = CurrentToken->Next;
4277FormatToken *TokenAnnotator::calculateInitializerColumnList(
4279 while (CurrentToken && CurrentToken !=
Line.Last) {
4280 if (CurrentToken->is(tok::l_brace))
4282 else if (CurrentToken->is(tok::r_brace))
4284 if (Depth == 2 && CurrentToken->isOneOf(tok::l_brace, tok::comma)) {
4285 CurrentToken = CurrentToken->Next;
4288 CurrentToken->StartsColumn =
true;
4289 CurrentToken = CurrentToken->Previous;
4291 CurrentToken = CurrentToken->Next;
4293 return CurrentToken;
4298 bool InFunctionDecl)
const {
4302 if (
Left.is(tok::semi))
4306 if (Style.isJava()) {
4307 if (
Right.isOneOf(Keywords.kw_extends, Keywords.kw_throws))
4309 if (
Right.is(Keywords.kw_implements))
4311 if (
Left.is(tok::comma) &&
Left.NestingLevel == 0)
4313 }
else if (Style.isJavaScript()) {
4314 if (
Right.is(Keywords.kw_function) &&
Left.isNot(tok::comma))
4316 if (
Left.is(TT_JsTypeColon))
4318 if ((
Left.is(TT_TemplateString) &&
Left.TokenText.ends_with(
"${")) ||
4319 (
Right.is(TT_TemplateString) &&
Right.TokenText.starts_with(
"}"))) {
4323 if (
Left.opensScope() &&
Right.closesScope())
4325 }
else if (Style.Language == FormatStyle::LK_Proto) {
4326 if (
Right.is(tok::l_square))
4328 if (
Right.is(tok::period))
4332 if (
Right.is(tok::identifier) &&
Right.Next &&
Right.Next->is(TT_DictLiteral))
4334 if (
Right.is(tok::l_square)) {
4335 if (
Left.is(tok::r_square))
4338 if (
Right.is(TT_LambdaLSquare) &&
Left.is(tok::equal))
4340 if (
Right.isNoneOf(TT_ObjCMethodExpr, TT_LambdaLSquare,
4341 TT_ArrayInitializerLSquare,
4342 TT_DesignatedInitializerLSquare, TT_AttributeLSquare)) {
4347 if (
Left.is(tok::coloncolon))
4348 return Style.PenaltyBreakScopeResolution;
4349 if (
Right.isOneOf(TT_StartOfName, TT_FunctionDeclarationName,
4350 tok::kw_operator)) {
4351 if (
Line.startsWith(tok::kw_for) &&
Right.PartOfMultiVariableDeclStmt)
4353 if (
Left.is(TT_StartOfName))
4355 if (InFunctionDecl &&
Right.NestingLevel == 0)
4356 return Style.PenaltyReturnTypeOnItsOwnLine;
4359 if (
Right.is(TT_PointerOrReference))
4361 if (
Right.is(TT_LambdaArrow))
4363 if (
Left.is(tok::equal) &&
Right.is(tok::l_brace))
4365 if (
Left.is(TT_CastRParen))
4367 if (
Left.isOneOf(tok::kw_class, tok::kw_struct, tok::kw_union))
4369 if (
Left.is(tok::comment))
4372 if (
Left.isOneOf(TT_RangeBasedForLoopColon, TT_InheritanceColon,
4373 TT_CtorInitializerColon)) {
4377 if (
Right.isMemberAccess()) {
4398 const auto Penalty = Style.PenaltyBreakBeforeMemberAccess;
4400 ? std::min(Penalty, 35u)
4404 if (
Right.is(TT_TrailingAnnotation) &&
4405 (!
Right.Next ||
Right.Next->isNot(tok::l_paren))) {
4408 if (
Line.startsWith(TT_ObjCMethodSpecifier))
4415 bool is_short_annotation =
Right.TokenText.size() < 10;
4416 return (
Left.is(tok::r_paren) ? 100 : 120) + (is_short_annotation ? 50 : 0);
4420 if (
Line.startsWith(tok::kw_for) &&
Left.is(tok::equal))
4425 if (
Right.is(TT_SelectorName))
4427 if (
Left.is(tok::colon)) {
4428 if (
Left.is(TT_ObjCMethodExpr))
4429 return Line.MightBeFunctionDecl ? 50 : 500;
4430 if (
Left.is(TT_ObjCSelector))
4438 Left.Previous->isOneOf(tok::identifier, tok::greater)) {
4442 if (
Left.is(tok::l_paren) && Style.PenaltyBreakOpenParenthesis != 0)
4443 return Style.PenaltyBreakOpenParenthesis;
4444 if (
Left.is(tok::l_paren) && InFunctionDecl && Style.AlignAfterOpenBracket)
4446 if (
Left.is(tok::l_paren) &&
Left.Previous &&
4447 (
Left.Previous->isOneOf(tok::kw_for, tok::kw__Generic) ||
4448 Left.Previous->isIf())) {
4451 if (
Left.is(tok::equal) && InFunctionDecl)
4453 if (
Right.is(tok::r_brace))
4455 if (
Left.is(TT_TemplateOpener))
4457 if (
Left.opensScope()) {
4461 if (!Style.AlignAfterOpenBracket &&
4462 (
Left.ParameterCount <= 1 || Style.AllowAllArgumentsOnNextLine)) {
4465 if (
Left.is(tok::l_brace) &&
4466 Style.Cpp11BracedListStyle == FormatStyle::BLS_Block) {
4469 return Left.ParameterCount > 1 ? Style.PenaltyBreakBeforeFirstCallParameter
4472 if (
Left.is(TT_JavaAnnotation))
4475 if (
Left.is(TT_UnaryOperator))
4477 if (
Left.isOneOf(tok::plus, tok::comma) &&
Left.Previous &&
4478 Left.Previous->isLabelString() &&
4479 (
Left.NextOperator ||
Left.OperatorIndex != 0)) {
4482 if (
Right.is(tok::plus) &&
Left.isLabelString() &&
4483 (
Right.NextOperator ||
Right.OperatorIndex != 0)) {
4486 if (
Left.is(tok::comma))
4488 if (
Right.is(tok::lessless) &&
Left.isLabelString() &&
4489 (
Right.NextOperator ||
Right.OperatorIndex != 1)) {
4492 if (
Right.is(tok::lessless)) {
4494 if (
Left.isNot(tok::r_paren) ||
Right.OperatorIndex > 0) {
4500 if (
Left.ClosesTemplateDeclaration)
4501 return Style.PenaltyBreakTemplateDeclaration;
4502 if (
Left.ClosesRequiresClause)
4504 if (
Left.is(TT_ConditionalExpr))
4510 return Style.PenaltyBreakAssignment;
4517bool TokenAnnotator::spaceRequiredBeforeParens(
const FormatToken &Right)
const {
4518 if (Style.SpaceBeforeParens == FormatStyle::SBPO_Always)
4520 if (
Right.is(TT_OverloadedOperatorLParen) &&
4521 Style.SpaceBeforeParensOptions.AfterOverloadedOperator) {
4524 if (Style.SpaceBeforeParensOptions.BeforeNonEmptyParentheses &&
4525 Right.ParameterCount > 0) {
4534 if (
Left.is(tok::kw_return) &&
4535 Right.isNoneOf(tok::semi, tok::r_paren, tok::hashhash)) {
4538 if (
Left.is(tok::kw_throw) &&
Right.is(tok::l_paren) &&
Right.MatchingParen &&
4539 Right.MatchingParen->is(TT_CastRParen)) {
4542 if (
Left.is(Keywords.kw_assert) && Style.isJava())
4545 Left.is(tok::objc_property)) {
4548 if (
Right.is(tok::hashhash))
4549 return Left.is(tok::hash);
4550 if (
Left.isOneOf(tok::hashhash, tok::hash))
4551 return Right.is(tok::hash);
4552 if (Style.SpacesInParens == FormatStyle::SIPO_Custom) {
4553 if (
Left.is(tok::l_paren) &&
Right.is(tok::r_paren))
4554 return Style.SpacesInParensOptions.InEmptyParentheses;
4555 if (Style.SpacesInParensOptions.ExceptDoubleParentheses &&
4556 Left.is(tok::r_paren) &&
Right.is(tok::r_paren)) {
4557 auto *InnerLParen =
Left.MatchingParen;
4558 if (InnerLParen && InnerLParen->Previous ==
Right.MatchingParen) {
4559 InnerLParen->SpacesRequiredBefore = 0;
4564 if (
Left.is(tok::l_paren))
4566 else if (
Right.is(tok::r_paren) &&
Right.MatchingParen)
4567 LeftParen =
Right.MatchingParen;
4568 if (LeftParen && (LeftParen->is(TT_ConditionLParen) ||
4569 (LeftParen->Previous &&
4570 isKeywordWithCondition(*LeftParen->Previous)))) {
4571 return Style.SpacesInParensOptions.InConditionalStatements;
4576 if (
Left.is(tok::kw_auto) &&
Right.isOneOf(TT_LambdaLBrace, TT_FunctionLBrace,
4578 TT_FunctionTypeLParen)) {
4583 if (
Left.is(tok::kw_auto) &&
Right.isOneOf(tok::l_paren, tok::l_brace))
4586 const auto *BeforeLeft =
Left.Previous;
4589 if (
Right.is(tok::l_paren) &&
Left.is(tok::kw_co_await) && BeforeLeft &&
4590 BeforeLeft->is(tok::kw_operator)) {
4594 if (
Left.isOneOf(tok::kw_co_await, tok::kw_co_yield, tok::kw_co_return) &&
4595 Right.isNoneOf(tok::semi, tok::r_paren)) {
4599 if (
Left.is(tok::l_paren) ||
Right.is(tok::r_paren)) {
4600 return (
Right.is(TT_CastRParen) ||
4601 (
Left.MatchingParen &&
Left.MatchingParen->is(TT_CastRParen)))
4602 ? Style.SpacesInParensOptions.InCStyleCasts
4603 : Style.SpacesInParensOptions.Other;
4605 if (
Right.isOneOf(tok::semi, tok::comma))
4608 bool IsLightweightGeneric =
Right.MatchingParen &&
4609 Right.MatchingParen->Next &&
4610 Right.MatchingParen->Next->is(tok::colon);
4611 return !IsLightweightGeneric && Style.ObjCSpaceBeforeProtocolList;
4613 if (
Right.is(tok::less) &&
Left.is(tok::kw_template))
4614 return Style.SpaceAfterTemplateKeyword;
4615 if (
Left.isOneOf(tok::exclaim, tok::tilde))
4617 if (
Left.is(tok::at) &&
4618 Right.isOneOf(tok::identifier, tok::string_literal, tok::char_constant,
4619 tok::numeric_constant, tok::l_paren, tok::l_brace,
4620 tok::kw_true, tok::kw_false)) {
4623 if (
Left.is(tok::colon))
4624 return Left.isNoneOf(TT_ObjCSelector, TT_ObjCMethodExpr);
4625 if (
Left.is(tok::coloncolon))
4627 if (
Left.is(tok::less) ||
Right.isOneOf(tok::greater, tok::less)) {
4628 if (Style.isTextProto() ||
4629 (Style.Language == FormatStyle::LK_Proto &&
4630 (
Left.is(TT_DictLiteral) ||
Right.is(TT_DictLiteral)))) {
4632 if (
Left.is(tok::less) &&
Right.is(tok::greater))
4634 return Style.Cpp11BracedListStyle == FormatStyle::BLS_Block;
4637 if (
Right.isNot(TT_OverloadedOperatorLParen))
4640 if (
Right.is(tok::ellipsis)) {
4641 return Left.Tok.isLiteral() || (
Left.is(tok::identifier) && BeforeLeft &&
4642 BeforeLeft->is(tok::kw_case));
4644 if (
Left.is(tok::l_square) &&
Right.is(tok::amp))
4645 return Style.SpacesInSquareBrackets;
4646 if (
Right.is(TT_PointerOrReference)) {
4647 if (
Left.is(tok::r_paren) &&
Line.MightBeFunctionDecl) {
4648 if (!
Left.MatchingParen)
4651 Left.MatchingParen->getPreviousNonComment();
4652 if (!TokenBeforeMatchingParen ||
Left.isNot(TT_TypeDeclarationParen))
4658 if ((Style.SpaceAroundPointerQualifiers == FormatStyle::SAPQ_After ||
4659 Style.SpaceAroundPointerQualifiers == FormatStyle::SAPQ_Both) &&
4660 (
Left.is(TT_AttributeRParen) ||
4661 Left.canBePointerOrReferenceQualifier())) {
4664 if (
Left.Tok.isLiteral())
4667 if (
Left.isTypeOrIdentifier(LangOpts) &&
Right.Next &&
Right.Next->Next &&
4668 Right.Next->Next->is(TT_RangeBasedForLoopColon)) {
4669 return getTokenPointerOrReferenceAlignment(Right) !=
4670 FormatStyle::PAS_Left;
4672 return Left.isNoneOf(TT_PointerOrReference, tok::l_paren) &&
4673 (getTokenPointerOrReferenceAlignment(Right) !=
4674 FormatStyle::PAS_Left ||
4675 (
Line.IsMultiVariableDeclStmt &&
4676 (
Left.NestingLevel == 0 ||
4677 (
Left.NestingLevel == 1 && startsWithInitStatement(
Line)))));
4679 if (
Right.is(TT_FunctionTypeLParen) &&
Left.isNot(tok::l_paren) &&
4680 (
Left.isNot(TT_PointerOrReference) ||
4681 (getTokenPointerOrReferenceAlignment(Left) != FormatStyle::PAS_Right &&
4682 !
Line.IsMultiVariableDeclStmt))) {
4685 if (
Left.is(TT_PointerOrReference)) {
4688 if ((Style.SpaceAroundPointerQualifiers == FormatStyle::SAPQ_Before ||
4689 Style.SpaceAroundPointerQualifiers == FormatStyle::SAPQ_Both) &&
4690 Right.canBePointerOrReferenceQualifier()) {
4694 if (
Right.Tok.isLiteral())
4697 if (
Right.is(TT_BlockComment))
4701 if (
Right.isOneOf(Keywords.kw_override, Keywords.kw_final, tok::kw_noexcept,
4702 TT_RequiresClause) &&
4703 Right.isNot(TT_StartOfName)) {
4710 if (BeforeLeft && BeforeLeft->isTypeOrIdentifier(LangOpts) &&
Right.Next &&
4711 Right.Next->is(TT_RangeBasedForLoopColon)) {
4712 return getTokenPointerOrReferenceAlignment(Left) !=
4713 FormatStyle::PAS_Right;
4715 if (
Right.isOneOf(TT_PointerOrReference, TT_ArraySubscriptLSquare,
4719 if (getTokenPointerOrReferenceAlignment(Left) == FormatStyle::PAS_Right)
4727 if (
Line.IsMultiVariableDeclStmt &&
4728 (
Left.NestingLevel ==
Line.First->NestingLevel ||
4729 ((
Left.NestingLevel ==
Line.First->NestingLevel + 1) &&
4730 startsWithInitStatement(
Line)))) {
4735 if (BeforeLeft->is(tok::coloncolon)) {
4736 if (
Left.isNot(tok::star))
4738 assert(Style.PointerAlignment != FormatStyle::PAS_Right);
4739 if (!
Right.startsSequence(tok::identifier, tok::r_paren))
4742 const auto *LParen =
Right.Next->MatchingParen;
4743 return !LParen || LParen->isNot(TT_FunctionTypeLParen);
4745 return BeforeLeft->isNoneOf(tok::l_paren, tok::l_square);
4748 if (
Left.is(tok::ellipsis) && BeforeLeft &&
4749 BeforeLeft->isPointerOrReference()) {
4750 return Style.PointerAlignment != FormatStyle::PAS_Right;
4753 if (
Right.is(tok::star) &&
Left.is(tok::l_paren))
4755 if (
Left.is(tok::star) &&
Right.isPointerOrReference())
4757 if (
Right.isPointerOrReference()) {
4768 if (
Previous->is(tok::coloncolon)) {
4787 if (
Previous->endsSequence(tok::kw_operator))
4788 return Style.PointerAlignment != FormatStyle::PAS_Left;
4789 if (
Previous->isOneOf(tok::kw_const, tok::kw_volatile)) {
4790 return (Style.PointerAlignment != FormatStyle::PAS_Left) ||
4791 (Style.SpaceAroundPointerQualifiers ==
4792 FormatStyle::SAPQ_After) ||
4793 (Style.SpaceAroundPointerQualifiers == FormatStyle::SAPQ_Both);
4797 if (Style.isCSharp() &&
Left.is(Keywords.kw_is) &&
Right.is(tok::l_square))
4799 const auto SpaceRequiredForArrayInitializerLSquare =
4800 [](
const FormatToken &LSquareTok,
const FormatStyle &Style) {
4801 return Style.SpacesInContainerLiterals ||
4803 Style.Cpp11BracedListStyle == FormatStyle::BLS_Block &&
4804 LSquareTok.endsSequence(tok::l_square, tok::colon,
4807 if (
Left.is(tok::l_square)) {
4808 return (
Left.is(TT_ArrayInitializerLSquare) &&
Right.isNot(tok::r_square) &&
4809 SpaceRequiredForArrayInitializerLSquare(Left, Style)) ||
4810 (
Left.isOneOf(TT_ArraySubscriptLSquare, TT_StructuredBindingLSquare,
4811 TT_LambdaLSquare) &&
4812 Style.SpacesInSquareBrackets &&
Right.isNot(tok::r_square));
4814 if (
Right.is(tok::r_square)) {
4815 return Right.MatchingParen &&
4816 ((
Right.MatchingParen->is(TT_ArrayInitializerLSquare) &&
4817 SpaceRequiredForArrayInitializerLSquare(*
Right.MatchingParen,
4819 (Style.SpacesInSquareBrackets &&
4820 Right.MatchingParen->isOneOf(TT_ArraySubscriptLSquare,
4821 TT_StructuredBindingLSquare,
4822 TT_LambdaLSquare)));
4824 if (
Right.is(tok::l_square) &&
4825 Right.isNoneOf(TT_ObjCMethodExpr, TT_LambdaLSquare,
4826 TT_DesignatedInitializerLSquare,
4827 TT_StructuredBindingLSquare, TT_AttributeLSquare) &&
4828 Left.isNoneOf(tok::numeric_constant, TT_DictLiteral) &&
4829 !(
Left.isNot(tok::r_square) && Style.SpaceBeforeSquareBrackets &&
4830 Right.is(TT_ArraySubscriptLSquare))) {
4834 (
Right.is(tok::r_brace) &&
Right.MatchingParen &&
4836 return Style.Cpp11BracedListStyle == FormatStyle::BLS_Block ||
4837 Style.SpacesInParensOptions.Other;
4839 if (
Left.is(TT_BlockComment)) {
4841 return Style.isJavaScript() || !
Left.TokenText.ends_with(
"=*/");
4846 if (
Left.is(TT_TemplateCloser) &&
Right.is(TT_AttributeLSquare))
4849 if (
Right.is(tok::l_paren)) {
4850 if (
Left.is(TT_TemplateCloser) &&
Right.isNot(TT_FunctionTypeLParen))
4851 return spaceRequiredBeforeParens(Right);
4852 if (
Left.isOneOf(TT_RequiresClause,
4853 TT_RequiresClauseInARequiresExpression)) {
4854 return Style.SpaceBeforeParensOptions.AfterRequiresInClause ||
4855 spaceRequiredBeforeParens(Right);
4857 if (
Left.is(TT_RequiresExpression)) {
4858 return Style.SpaceBeforeParensOptions.AfterRequiresInExpression ||
4859 spaceRequiredBeforeParens(Right);
4861 if (
Left.isOneOf(TT_AttributeRParen, TT_AttributeRSquare))
4863 if (
Left.is(TT_ForEachMacro)) {
4864 return Style.SpaceBeforeParensOptions.AfterForeachMacros ||
4865 spaceRequiredBeforeParens(Right);
4867 if (
Left.is(TT_IfMacro)) {
4868 return Style.SpaceBeforeParensOptions.AfterIfMacros ||
4869 spaceRequiredBeforeParens(Right);
4871 if (Style.SpaceBeforeParens == FormatStyle::SBPO_Custom &&
4872 Left.isPlacementOperator() &&
4873 Right.isNot(TT_OverloadedOperatorLParen) &&
4874 !(
Line.MightBeFunctionDecl &&
Left.is(TT_FunctionDeclarationName))) {
4875 const auto *RParen =
Right.MatchingParen;
4876 return Style.SpaceBeforeParensOptions.AfterPlacementOperator ||
4877 (RParen && RParen->is(TT_CastRParen));
4881 if (
Left.is(tok::semi))
4883 if (
Left.isOneOf(tok::pp_elif, tok::kw_for, tok::kw_while, tok::kw_switch,
4884 tok::kw_case, TT_ForEachMacro, TT_ObjCForIn) ||
4886 Right.is(TT_ConditionLParen)) {
4887 return Style.SpaceBeforeParensOptions.AfterControlStatements ||
4888 spaceRequiredBeforeParens(Right);
4893 if (
Right.is(TT_OverloadedOperatorLParen))
4894 return spaceRequiredBeforeParens(Right);
4896 if (
Line.MightBeFunctionDecl &&
Right.is(TT_FunctionDeclarationLParen)) {
4897 if (spaceRequiredBeforeParens(Right))
4899 const auto &Options = Style.SpaceBeforeParensOptions;
4900 return Line.mightBeFunctionDefinition()
4901 ? Options.AfterFunctionDefinitionName
4902 : Options.AfterFunctionDeclarationName;
4906 Left.MatchingParen &&
Left.MatchingParen->is(TT_LambdaLSquare)) {
4907 return Style.SpaceBeforeParensOptions.AfterFunctionDefinitionName ||
4908 spaceRequiredBeforeParens(Right);
4910 if (!BeforeLeft || BeforeLeft->isNoneOf(tok::period, tok::arrow)) {
4911 if (
Left.isOneOf(tok::kw_try, Keywords.kw___except, tok::kw_catch)) {
4912 return Style.SpaceBeforeParensOptions.AfterControlStatements ||
4913 spaceRequiredBeforeParens(Right);
4915 if (
Left.isPlacementOperator() ||
4916 (
Left.is(tok::r_square) &&
Left.MatchingParen &&
4917 Left.MatchingParen->Previous &&
4918 Left.MatchingParen->Previous->is(tok::kw_delete))) {
4919 return Style.SpaceBeforeParens != FormatStyle::SBPO_Never ||
4920 spaceRequiredBeforeParens(Right);
4925 (
Left.Tok.getIdentifierInfo() ||
Left.is(tok::r_paren))) {
4926 return spaceRequiredBeforeParens(Right);
4930 if (
Left.is(tok::at) &&
Right.isNot(tok::objc_not_keyword))
4932 if (
Right.is(TT_UnaryOperator)) {
4933 return Left.isNoneOf(tok::l_paren, tok::l_square, tok::at) &&
4934 (
Left.isNot(tok::colon) ||
Left.isNot(TT_ObjCMethodExpr));
4940 if (!Style.isVerilog() &&
4941 (
Left.isOneOf(tok::identifier, tok::greater, tok::r_square,
4943 Left.isTypeName(LangOpts)) &&
4944 Right.is(tok::l_brace) &&
Right.getNextNonComment() &&
4948 if (
Left.is(tok::period) ||
Right.is(tok::period))
4952 if (
Right.is(tok::hash) &&
Left.is(tok::identifier) &&
4953 (
Left.TokenText ==
"L" ||
Left.TokenText ==
"u" ||
4954 Left.TokenText ==
"U" ||
Left.TokenText ==
"u8" ||
4955 Left.TokenText ==
"LR" ||
Left.TokenText ==
"uR" ||
4956 Left.TokenText ==
"UR" ||
Left.TokenText ==
"u8R")) {
4959 if (
Left.is(TT_TemplateCloser) &&
Left.MatchingParen &&
4960 Left.MatchingParen->Previous &&
4961 Left.MatchingParen->Previous->isOneOf(tok::period, tok::coloncolon)) {
4967 if (
Left.is(TT_TemplateCloser) &&
Right.is(tok::l_square))
4969 if (
Left.is(tok::l_brace) &&
Left.endsSequence(TT_DictLiteral, tok::at)) {
4973 if (
Right.is(tok::r_brace) &&
Right.MatchingParen &&
4974 Right.MatchingParen->endsSequence(TT_DictLiteral, tok::at)) {
4978 if (
Right.is(TT_TrailingAnnotation) &&
Right.isOneOf(tok::amp, tok::ampamp) &&
4979 Left.isOneOf(tok::kw_const, tok::kw_volatile) &&
4980 (!
Right.Next ||
Right.Next->is(tok::semi))) {
4984 return getTokenReferenceAlignment(Right) != FormatStyle::PAS_Left;
4997 return Right.hasWhitespaceBefore();
4999 const bool IsVerilog = Style.isVerilog();
5000 assert(!IsVerilog || !IsCpp);
5003 if (Keywords.isWordLike(Right, IsVerilog) &&
5004 Keywords.isWordLike(Left, IsVerilog)) {
5010 if (
Left.is(tok::star) &&
Right.is(tok::comment))
5013 if (
Left.is(tok::l_brace) &&
Right.is(tok::r_brace) &&
5014 Left.Children.empty()) {
5016 return Style.SpaceInEmptyBraces != FormatStyle::SIEB_Never;
5017 if (Style.Cpp11BracedListStyle != FormatStyle::BLS_Block) {
5018 return Style.SpacesInParens == FormatStyle::SIPO_Custom &&
5019 Style.SpacesInParensOptions.InEmptyParentheses;
5021 return Style.SpaceInEmptyBraces == FormatStyle::SIEB_Always;
5024 const auto *BeforeLeft =
Left.Previous;
5027 if (
Left.is(TT_OverloadedOperator) &&
5028 Right.isOneOf(TT_TemplateOpener, TT_TemplateCloser)) {
5032 if (
Right.is(tok::period) &&
Left.is(tok::numeric_constant))
5036 if (
Left.is(Keywords.kw_import) &&
Right.isOneOf(tok::less, tok::ellipsis))
5039 if (
Left.isOneOf(Keywords.kw_module, Keywords.kw_import) &&
5040 Right.is(TT_ModulePartitionColon)) {
5044 if (
Right.is(TT_AfterPPDirective))
5048 if (
Left.is(tok::identifier) &&
Right.is(TT_ModulePartitionColon))
5051 if (
Left.is(TT_ModulePartitionColon) &&
5052 Right.isOneOf(tok::identifier, tok::kw_private)) {
5055 if (
Left.is(tok::ellipsis) &&
Right.is(tok::identifier) &&
5056 Line.First->is(Keywords.kw_import)) {
5060 if (
Left.isOneOf(TT_AttributeRParen, TT_AttributeMacro) &&
5061 Right.is(tok::coloncolon)) {
5065 if (
Left.is(tok::kw_operator))
5066 return Right.is(tok::coloncolon) || Style.SpaceAfterOperatorKeyword;
5068 !
Left.opensScope() && Style.SpaceBeforeCpp11BracedList) {
5071 if (
Left.is(tok::less) &&
Left.is(TT_OverloadedOperator) &&
5072 Right.is(TT_TemplateOpener)) {
5076 if (
Left.is(tok::identifier) &&
Right.is(tok::numeric_constant))
5077 return Right.TokenText[0] !=
'.';
5079 if (
Left.Tok.getIdentifierInfo() &&
Right.Tok.isLiteral())
5081 }
else if (Style.isProto()) {
5082 if (
Right.is(tok::period) && !(BeforeLeft && BeforeLeft->is(tok::period)) &&
5083 Left.isOneOf(Keywords.kw_optional, Keywords.kw_required,
5084 Keywords.kw_repeated, Keywords.kw_extend)) {
5087 if (
Right.is(tok::l_paren) &&
5088 Left.isOneOf(Keywords.kw_returns, Keywords.kw_option)) {
5091 if (
Right.isOneOf(tok::l_brace, tok::less) &&
Left.is(TT_SelectorName))
5094 if (
Left.is(tok::slash) ||
Right.is(tok::slash))
5096 if (
Left.MatchingParen &&
5097 Left.MatchingParen->is(TT_ProtoExtensionLSquare) &&
5098 Right.isOneOf(tok::l_brace, tok::less)) {
5099 return Style.Cpp11BracedListStyle == FormatStyle::BLS_Block;
5102 if (
Left.is(tok::percent))
5106 if (
Left.is(tok::numeric_constant) &&
Right.is(tok::percent))
5107 return Right.hasWhitespaceBefore();
5108 }
else if (Style.isJson()) {
5109 if (
Right.is(tok::colon) &&
Left.is(tok::string_literal))
5110 return Style.SpaceBeforeJsonColon;
5111 }
else if (Style.isCSharp()) {
5117 if (
Left.is(tok::kw_this) &&
Right.is(tok::l_square))
5121 if (
Left.is(tok::kw_new) &&
Right.is(tok::l_paren))
5125 if (
Right.is(tok::l_brace))
5129 if (
Left.is(tok::l_brace) &&
Right.isNot(tok::r_brace))
5132 if (
Left.isNot(tok::l_brace) &&
Right.is(tok::r_brace))
5136 if (
Left.is(TT_FatArrow) ||
Right.is(TT_FatArrow))
5140 if (
Left.is(TT_AttributeColon) ||
Right.is(TT_AttributeColon))
5144 if (
Left.is(TT_TemplateCloser) &&
Right.is(TT_StartOfName))
5148 if (
Left.is(tok::l_square) ||
Right.is(tok::r_square))
5149 return Style.SpacesInSquareBrackets;
5152 if (
Right.is(TT_CSharpNullable))
5156 if (
Right.is(TT_NonNullAssertion))
5160 if (
Left.is(tok::comma) &&
Right.is(tok::comma))
5164 if (
Left.is(Keywords.kw_var) &&
Right.is(tok::l_paren))
5168 if (
Right.is(tok::l_paren)) {
5169 if (
Left.isOneOf(tok::kw_using, Keywords.kw_async, Keywords.kw_when,
5170 Keywords.kw_lock)) {
5171 return Style.SpaceBeforeParensOptions.AfterControlStatements ||
5172 spaceRequiredBeforeParens(Right);
5178 if ((
Left.isAccessSpecifierKeyword() ||
5179 Left.isOneOf(tok::kw_virtual, tok::kw_extern, tok::kw_static,
5180 Keywords.kw_internal, Keywords.kw_abstract,
5181 Keywords.kw_sealed, Keywords.kw_override,
5182 Keywords.kw_async, Keywords.kw_unsafe)) &&
5183 Right.is(tok::l_paren)) {
5186 }
else if (Style.isJavaScript()) {
5187 if (
Left.is(TT_FatArrow))
5190 if (
Right.is(tok::l_paren) &&
Left.is(Keywords.kw_await) && BeforeLeft &&
5191 BeforeLeft->is(tok::kw_for)) {
5194 if (
Left.is(Keywords.kw_async) &&
Right.is(tok::l_paren) &&
5195 Right.MatchingParen) {
5202 if ((
Left.is(TT_TemplateString) &&
Left.TokenText.ends_with(
"${")) ||
5203 (
Right.is(TT_TemplateString) &&
Right.TokenText.starts_with(
"}"))) {
5208 if (Keywords.isJavaScriptIdentifier(Left,
5210 Right.is(TT_TemplateString)) {
5213 if (
Right.is(tok::star) &&
5214 Left.isOneOf(Keywords.kw_function, Keywords.kw_yield)) {
5217 if (
Right.isOneOf(tok::l_brace, tok::l_square) &&
5218 Left.isOneOf(Keywords.kw_function, Keywords.kw_yield,
5219 Keywords.kw_extends, Keywords.kw_implements)) {
5222 if (
Right.is(tok::l_paren)) {
5224 if (
Line.MustBeDeclaration &&
Left.Tok.getIdentifierInfo())
5228 if (BeforeLeft && BeforeLeft->is(tok::period) &&
5229 Left.Tok.getIdentifierInfo()) {
5233 if (
Left.isOneOf(tok::kw_throw, Keywords.kw_await, Keywords.kw_typeof,
5239 if (
Left.endsSequence(tok::kw_const, Keywords.kw_as))
5241 if ((
Left.isOneOf(Keywords.kw_let, Keywords.kw_var, Keywords.kw_in,
5246 (
Left.is(Keywords.kw_of) && BeforeLeft &&
5247 BeforeLeft->isOneOf(tok::identifier, tok::r_square, tok::r_brace))) &&
5248 (!BeforeLeft || BeforeLeft->isNot(tok::period))) {
5251 if (
Left.isOneOf(tok::kw_for, Keywords.kw_as) && BeforeLeft &&
5252 BeforeLeft->is(tok::period) &&
Right.is(tok::l_paren)) {
5255 if (
Left.is(Keywords.kw_as) &&
5256 Right.isOneOf(tok::l_square, tok::l_brace, tok::l_paren)) {
5259 if (
Left.is(tok::kw_default) && BeforeLeft &&
5260 BeforeLeft->is(tok::kw_export)) {
5263 if (
Left.is(Keywords.kw_is) &&
Right.is(tok::l_brace))
5265 if (
Right.isOneOf(TT_JsTypeColon, TT_JsTypeOptionalQuestion))
5267 if (
Left.is(TT_JsTypeOperator) ||
Right.is(TT_JsTypeOperator))
5269 if ((
Left.is(tok::l_brace) ||
Right.is(tok::r_brace)) &&
5270 Line.First->isOneOf(Keywords.kw_import, tok::kw_export)) {
5273 if (
Left.is(tok::ellipsis))
5275 if (
Left.is(TT_TemplateCloser) &&
5276 Right.isNoneOf(tok::equal, tok::l_brace, tok::comma, tok::l_square,
5277 Keywords.kw_implements, Keywords.kw_extends)) {
5283 if (
Right.is(TT_NonNullAssertion))
5285 if (
Left.is(TT_NonNullAssertion) &&
5286 Right.isOneOf(Keywords.kw_as, Keywords.kw_in)) {
5289 }
else if (Style.isJava()) {
5290 if (
Left.is(TT_CaseLabelArrow) ||
Right.is(TT_CaseLabelArrow))
5292 if (
Left.is(tok::r_square) &&
Right.is(tok::l_brace))
5295 if (
Left.is(tok::l_square) ||
Right.is(tok::r_square))
5296 return Style.SpacesInSquareBrackets;
5298 if (
Left.is(Keywords.kw_synchronized) &&
Right.is(tok::l_paren)) {
5299 return Style.SpaceBeforeParensOptions.AfterControlStatements ||
5300 spaceRequiredBeforeParens(Right);
5302 if ((
Left.isAccessSpecifierKeyword() ||
5303 Left.isOneOf(tok::kw_static, Keywords.kw_final, Keywords.kw_abstract,
5304 Keywords.kw_native)) &&
5305 Right.is(TT_TemplateOpener)) {
5308 }
else if (IsVerilog) {
5310 if (
Left.is(tok::identifier) &&
Left.TokenText[0] ==
'\\')
5314 if ((
Left.is(TT_VerilogTableItem) &&
5315 Right.isNoneOf(tok::r_paren, tok::semi)) ||
5316 (
Right.is(TT_VerilogTableItem) &&
Left.isNot(tok::l_paren))) {
5318 return !(
Next &&
Next->is(tok::r_paren));
5321 if (
Left.isNot(TT_BinaryOperator) &&
5322 Left.isOneOf(Keywords.kw_verilogHash, Keywords.kw_verilogHashHash)) {
5326 if (
Right.isNot(tok::semi) &&
5327 (
Left.endsSequence(tok::numeric_constant, Keywords.kw_verilogHash) ||
5328 Left.endsSequence(tok::numeric_constant,
5329 Keywords.kw_verilogHashHash) ||
5330 (
Left.is(tok::r_paren) &&
Left.MatchingParen &&
5331 Left.MatchingParen->endsSequence(tok::l_paren, tok::at)))) {
5336 if (
Left.is(Keywords.kw_apostrophe) ||
5337 (
Left.is(TT_VerilogNumberBase) &&
Right.is(tok::numeric_constant))) {
5341 if (
Left.is(tok::arrow) ||
Right.is(tok::arrow))
5346 if (
Left.is(tok::at) &&
Right.isOneOf(tok::l_paren, tok::star, tok::at))
5349 if (
Right.is(tok::l_square) &&
5350 Left.isOneOf(TT_VerilogDimensionedTypeName, Keywords.kw_function)) {
5354 if (
Right.isOneOf(tok::period, Keywords.kw_apostrophe) &&
5355 Keywords.isVerilogIdentifier(Left) &&
Left.getPreviousNonComment() &&
5356 Left.getPreviousNonComment()->is(Keywords.kw_tagged)) {
5362 if ((
Right.is(Keywords.kw_apostrophe) ||
5364 Left.isNoneOf(Keywords.kw_assign, Keywords.kw_unique) &&
5365 !Keywords.isVerilogWordOperator(Left) &&
5366 (
Left.isOneOf(tok::r_square, tok::r_paren, tok::r_brace,
5367 tok::numeric_constant) ||
5368 Keywords.isWordLike(Left))) {
5372 if ((
Right.is(tok::star) &&
Left.is(tok::coloncolon)) ||
5373 (
Left.is(tok::star) &&
Right.is(tok::semi))) {
5377 if (
Left.endsSequence(tok::star, tok::l_paren) &&
Right.is(tok::identifier))
5380 if (
Right.is(tok::l_paren) &&
Right.is(TT_VerilogStrength))
5383 if ((
Left.is(tok::l_brace) &&
5384 Right.isOneOf(tok::lessless, tok::greatergreater)) ||
5385 (
Left.endsSequence(tok::lessless, tok::l_brace) ||
5386 Left.endsSequence(tok::greatergreater, tok::l_brace))) {
5389 }
else if (Style.isTableGen()) {
5391 if (
Left.is(tok::l_square) &&
Right.is(tok::l_brace))
5393 if (
Left.is(tok::r_brace) &&
Right.is(tok::r_square))
5396 if (
Right.isOneOf(TT_TableGenDAGArgListColon,
5397 TT_TableGenDAGArgListColonToAlign) ||
5398 Left.isOneOf(TT_TableGenDAGArgListColon,
5399 TT_TableGenDAGArgListColonToAlign)) {
5402 if (
Right.is(TT_TableGenCondOperatorColon))
5404 if (
Left.isOneOf(TT_TableGenDAGArgOperatorID,
5405 TT_TableGenDAGArgOperatorToBreak) &&
5406 Right.isNot(TT_TableGenDAGArgCloser)) {
5410 if (
Right.isOneOf(tok::l_paren, tok::less) &&
5411 Left.isOneOf(TT_TableGenBangOperator, TT_TableGenCondOperator)) {
5416 if (
Left.is(TT_TableGenTrailingPasteOperator) &&
5417 Right.isOneOf(tok::l_brace, tok::colon)) {
5421 if (
Left.is(tok::hash) ||
Right.is(tok::hash))
5424 if (Keywords.isTableGenDefinition(Left))
5428 if (
Left.is(TT_ImplicitStringLiteral))
5429 return Right.hasWhitespaceBefore();
5431 if (
Left.is(TT_ObjCMethodSpecifier))
5433 if (
Left.is(tok::r_paren) &&
Left.isNot(TT_AttributeRParen) &&
5434 canBeObjCSelectorComponent(Right)) {
5442 (
Right.is(tok::equal) ||
Left.is(tok::equal))) {
5446 if (
Right.isOneOf(TT_TrailingReturnArrow, TT_LambdaArrow) ||
5447 Left.isOneOf(TT_TrailingReturnArrow, TT_LambdaArrow)) {
5450 if (
Left.is(tok::comma) &&
Right.isNot(TT_OverloadedOperatorLParen) &&
5453 (
Left.Children.empty() || !
Left.MacroParent)) {
5456 if (
Right.is(tok::comma))
5458 if (
Right.is(TT_ObjCBlockLParen))
5460 if (
Right.is(TT_CtorInitializerColon))
5461 return Style.SpaceBeforeCtorInitializerColon;
5462 if (
Right.is(TT_InheritanceColon) && !Style.SpaceBeforeInheritanceColon)
5464 if (
Right.is(TT_RangeBasedForLoopColon) &&
5465 !Style.SpaceBeforeRangeBasedForLoopColon) {
5468 if (
Left.is(TT_BitFieldColon)) {
5469 return Style.BitFieldColonSpacing == FormatStyle::BFCS_Both ||
5470 Style.BitFieldColonSpacing == FormatStyle::BFCS_After;
5472 if (
Right.is(tok::colon)) {
5473 if (
Right.is(TT_CaseLabelColon))
5474 return Style.SpaceBeforeCaseColon;
5475 if (
Right.is(TT_GotoLabelColon))
5478 if (!
Right.getNextNonComment())
5480 if (
Right.isOneOf(TT_ObjCSelector, TT_ObjCMethodExpr))
5482 if (
Left.is(tok::question))
5484 if (
Right.is(TT_InlineASMColon) &&
Left.is(tok::coloncolon))
5486 if (
Right.is(TT_DictLiteral))
5487 return Style.SpacesInContainerLiterals;
5488 if (
Right.is(TT_AttributeColon))
5490 if (
Right.is(TT_CSharpNamedArgumentColon))
5492 if (
Right.is(TT_GenericSelectionColon))
5494 if (
Right.is(TT_BitFieldColon)) {
5495 return Style.BitFieldColonSpacing == FormatStyle::BFCS_Both ||
5496 Style.BitFieldColonSpacing == FormatStyle::BFCS_Before;
5501 if ((
Left.isOneOf(tok::minus, tok::minusminus) &&
5502 Right.isOneOf(tok::minus, tok::minusminus)) ||
5503 (
Left.isOneOf(tok::plus, tok::plusplus) &&
5504 Right.isOneOf(tok::plus, tok::plusplus))) {
5507 if (
Left.is(TT_UnaryOperator)) {
5510 if (
Left.is(tok::amp) &&
Right.is(tok::r_square))
5511 return Style.SpacesInSquareBrackets;
5512 if (
Left.isNot(tok::exclaim))
5514 if (
Left.TokenText ==
"!")
5515 return Style.SpaceAfterLogicalNot;
5516 assert(
Left.TokenText ==
"not");
5517 return Right.isOneOf(tok::coloncolon, TT_UnaryOperator) ||
5518 (
Right.is(tok::l_paren) && Style.SpaceBeforeParensOptions.AfterNot);
5523 if (
Left.is(TT_CastRParen)) {
5524 return Style.SpaceAfterCStyleCast ||
5525 Right.isOneOf(TT_BinaryOperator, TT_SelectorName);
5528 auto ShouldAddSpacesInAngles = [
this, &
Right]() {
5529 if (this->Style.SpacesInAngles == FormatStyle::SIAS_Always)
5531 if (this->Style.SpacesInAngles == FormatStyle::SIAS_Leave)
5532 return Right.hasWhitespaceBefore();
5536 if (
Left.is(tok::greater) &&
Right.is(tok::greater)) {
5537 if (Style.isTextProto() ||
5538 (Style.Language == FormatStyle::LK_Proto &&
Left.is(TT_DictLiteral))) {
5539 return Style.Cpp11BracedListStyle == FormatStyle::BLS_Block;
5541 return Right.is(TT_TemplateCloser) &&
Left.is(TT_TemplateCloser) &&
5542 ((Style.Standard < FormatStyle::LS_Cpp11) ||
5543 ShouldAddSpacesInAngles());
5545 if (
Right.isOneOf(tok::arrow, tok::arrowstar, tok::periodstar) ||
5546 Left.isOneOf(tok::arrow, tok::period, tok::arrowstar, tok::periodstar) ||
5547 (
Right.is(tok::period) &&
Right.isNot(TT_DesignatedInitializerPeriod))) {
5550 if (!Style.SpaceBeforeAssignmentOperators &&
Left.isNot(TT_TemplateCloser) &&
5554 if (Style.isJava() &&
Right.is(tok::coloncolon) &&
5555 Left.isOneOf(tok::identifier, tok::kw_this)) {
5558 if (
Right.is(tok::coloncolon) &&
Left.is(tok::identifier)) {
5562 return Right.hasWhitespaceBefore();
5564 if (
Right.is(tok::coloncolon) &&
5565 Left.isNoneOf(tok::l_brace, tok::comment, tok::l_paren)) {
5567 return (
Left.is(TT_TemplateOpener) &&
5568 ((Style.Standard < FormatStyle::LS_Cpp11) ||
5569 ShouldAddSpacesInAngles())) ||
5570 Left.isNoneOf(tok::l_paren, tok::r_paren, tok::l_square,
5571 tok::kw___super, TT_TemplateOpener,
5572 TT_TemplateCloser) ||
5573 (
Left.is(tok::l_paren) && Style.SpacesInParensOptions.Other);
5575 if ((
Left.is(TT_TemplateOpener)) != (
Right.is(TT_TemplateCloser)))
5576 return ShouldAddSpacesInAngles();
5577 if (
Left.is(tok::r_paren) &&
Left.isNot(TT_TypeDeclarationParen) &&
5578 Right.is(TT_PointerOrReference) &&
Right.isOneOf(tok::amp, tok::ampamp)) {
5582 if (
Right.is(TT_StructuredBindingLSquare)) {
5583 return Left.isNoneOf(tok::amp, tok::ampamp) ||
5584 getTokenReferenceAlignment(Left) != FormatStyle::PAS_Right;
5587 if (
Right.Next &&
Right.Next->is(TT_StructuredBindingLSquare) &&
5588 Right.isOneOf(tok::amp, tok::ampamp)) {
5589 return getTokenReferenceAlignment(Right) != FormatStyle::PAS_Left;
5591 if ((
Right.is(TT_BinaryOperator) &&
Left.isNot(tok::l_paren)) ||
5592 (
Left.isOneOf(TT_BinaryOperator, TT_ConditionalExpr) &&
5593 Right.isNot(tok::r_paren))) {
5596 if (
Right.is(TT_TemplateOpener) &&
Left.is(tok::r_paren) &&
5597 Left.MatchingParen &&
5598 Left.MatchingParen->is(TT_OverloadedOperatorLParen)) {
5601 if (
Right.is(tok::less) &&
Left.isNot(tok::l_paren) &&
5605 if (
Right.is(TT_TrailingUnaryOperator))
5607 if (
Left.is(TT_RegexLiteral))
5609 return spaceRequiredBetween(
Line, Left, Right);
5615 Tok.isNoneOf(TT_ObjCBlockLBrace, TT_LambdaLBrace, TT_DictLiteral);
5620 return Tok.MatchingParen &&
Tok.MatchingParen->Next &&
5621 Tok.MatchingParen->Next->isOneOf(tok::comma, tok::r_paren,
5627 FormatStyle::ShortLambdaStyle ShortLambdaOption) {
5628 return Tok.Children.empty() && ShortLambdaOption != FormatStyle::SLS_None;
5633 Tok.isNoneOf(TT_ObjCBlockLBrace, TT_DictLiteral);
5636bool TokenAnnotator::mustBreakBefore(
const AnnotatedLine &
Line,
5638 if (Right.NewlinesBefore > 1 && Style.MaxEmptyLinesToKeep > 0 &&
5639 (!Style.RemoveEmptyLinesInUnwrappedLines || &Right ==
Line.First)) {
5645 if (Style.BreakFunctionDefinitionParameters &&
Line.MightBeFunctionDecl &&
5646 Line.mightBeFunctionDefinition() && Left.MightBeFunctionDeclParen &&
5647 Left.ParameterCount > 0) {
5653 if (Style.BinPackParameters == FormatStyle::BPPS_AlwaysOnePerLine &&
5654 Line.MightBeFunctionDecl && !
Left.opensScope() &&
5659 const auto *BeforeLeft =
Left.Previous;
5660 const auto *AfterRight =
Right.Next;
5662 if (Style.isCSharp()) {
5663 if (
Left.is(TT_FatArrow) &&
Right.is(tok::l_brace) &&
5664 Style.BraceWrapping.AfterFunction) {
5667 if (
Right.is(TT_CSharpNamedArgumentColon) ||
5668 Left.is(TT_CSharpNamedArgumentColon)) {
5671 if (
Right.is(TT_CSharpGenericTypeConstraint))
5673 if (AfterRight && AfterRight->is(TT_FatArrow) &&
5674 (
Right.is(tok::numeric_constant) ||
5675 (
Right.is(tok::identifier) &&
Right.TokenText ==
"_"))) {
5680 if (
Left.is(TT_AttributeRSquare) &&
5681 (
Right.isAccessSpecifier(
false) ||
5682 Right.is(Keywords.kw_internal))) {
5686 if (
Left.is(TT_AttributeRSquare) &&
Right.is(TT_AttributeLSquare))
5688 }
else if (Style.isJavaScript()) {
5690 if (
Right.is(tok::string_literal) &&
Left.is(tok::plus) && BeforeLeft &&
5691 BeforeLeft->is(tok::string_literal)) {
5694 if (
Left.is(TT_DictLiteral) &&
Left.is(tok::l_brace) &&
Line.Level == 0 &&
5695 BeforeLeft && BeforeLeft->is(tok::equal) &&
5696 Line.First->isOneOf(tok::identifier, Keywords.kw_import, tok::kw_export,
5700 Line.First->isNoneOf(Keywords.kw_var, Keywords.kw_let)) {
5705 if (
Left.is(tok::l_brace) &&
Line.Level == 0 &&
5706 (
Line.startsWith(tok::kw_enum) ||
5707 Line.startsWith(tok::kw_const, tok::kw_enum) ||
5708 Line.startsWith(tok::kw_export, tok::kw_enum) ||
5709 Line.startsWith(tok::kw_export, tok::kw_const, tok::kw_enum))) {
5714 if (
Right.is(tok::r_brace) &&
Left.is(tok::l_brace) && BeforeLeft &&
5715 BeforeLeft->is(TT_FatArrow)) {
5717 switch (Style.AllowShortLambdasOnASingleLine) {
5718 case FormatStyle::SLS_All:
5720 case FormatStyle::SLS_None:
5722 case FormatStyle::SLS_Empty:
5723 return !
Left.Children.empty();
5724 case FormatStyle::SLS_Inline:
5727 return (
Left.NestingLevel == 0 &&
Line.Level == 0) &&
5728 !
Left.Children.empty();
5730 llvm_unreachable(
"Unknown FormatStyle::ShortLambdaStyle enum");
5733 if (
Right.is(tok::r_brace) &&
Left.is(tok::l_brace) &&
5734 !
Left.Children.empty()) {
5736 return Style.AllowShortFunctionsOnASingleLine == FormatStyle::SFS_None ||
5737 Style.AllowShortFunctionsOnASingleLine == FormatStyle::SFS_Empty ||
5738 (
Left.NestingLevel == 0 &&
Line.Level == 0 &&
5739 Style.AllowShortFunctionsOnASingleLine &
5740 FormatStyle::SFS_InlineOnly);
5742 }
else if (Style.isJava()) {
5743 if (
Right.is(tok::plus) &&
Left.is(tok::string_literal) && AfterRight &&
5744 AfterRight->is(tok::string_literal)) {
5747 }
else if (Style.isVerilog()) {
5749 if (
Left.is(TT_VerilogAssignComma))
5752 if (
Left.is(TT_VerilogTypeComma))
5756 if (Style.VerilogBreakBetweenInstancePorts &&
5757 (
Left.is(TT_VerilogInstancePortComma) ||
5758 (
Left.is(tok::r_paren) && Keywords.isVerilogIdentifier(Right) &&
5759 Left.MatchingParen &&
5760 Left.MatchingParen->is(TT_VerilogInstancePortLParen)))) {
5765 if (!Keywords.isVerilogBegin(Right) && Keywords.isVerilogEndOfLabel(Left))
5767 }
else if (Style.BreakAdjacentStringLiterals &&
5768 (IsCpp || Style.isProto() || Style.isTableGen())) {
5769 if (
Left.isStringLiteral() &&
Right.isStringLiteral())
5774 if (Style.isJson()) {
5778 if (
Left.is(TT_DictLiteral) &&
Left.is(tok::l_brace))
5781 if ((
Left.is(TT_ArrayInitializerLSquare) &&
Left.is(tok::l_square) &&
5782 Right.isNot(tok::r_square)) ||
5783 Left.is(tok::comma)) {
5784 if (
Right.is(tok::l_brace))
5789 if (
Tok->isOneOf(tok::l_brace, tok::l_square))
5791 if (
Tok->isOneOf(tok::r_brace, tok::r_square))
5794 return Style.BreakArrays;
5796 }
else if (Style.isTableGen()) {
5800 if (
Left.is(TT_TableGenCondOperatorComma))
5802 if (
Left.is(TT_TableGenDAGArgOperatorToBreak) &&
5803 Right.isNot(TT_TableGenDAGArgCloser)) {
5806 if (
Left.is(TT_TableGenDAGArgListCommaToBreak))
5808 if (
Right.is(TT_TableGenDAGArgCloser) &&
Right.MatchingParen &&
5809 Right.MatchingParen->is(TT_TableGenDAGArgOpenerToBreak) &&
5810 &Left !=
Right.MatchingParen->Next) {
5812 return Style.TableGenBreakInsideDAGArg == FormatStyle::DAS_BreakAll;
5816 if (
Line.startsWith(tok::kw_asm) &&
Right.is(TT_InlineASMColon) &&
5817 Style.BreakBeforeInlineASMColon == FormatStyle::BBIAS_Always) {
5827 if ((
Left.isOneOf(tok::l_brace, TT_ArrayInitializerLSquare) ||
5828 (Style.isJavaScript() &&
Left.is(tok::l_paren))) &&
5830 BeforeClosingBrace =
Left.MatchingParen->Previous;
5831 }
else if (
Right.MatchingParen &&
5832 (
Right.MatchingParen->isOneOf(tok::l_brace,
5833 TT_ArrayInitializerLSquare) ||
5834 (Style.isJavaScript() &&
5835 Right.MatchingParen->is(tok::l_paren)))) {
5836 BeforeClosingBrace = &
Left;
5838 if (BeforeClosingBrace && (BeforeClosingBrace->is(tok::comma) ||
5839 BeforeClosingBrace->isTrailingComment())) {
5844 if (
Right.is(tok::comment)) {
5846 Right.NewlinesBefore > 0 &&
Right.HasUnescapedNewline;
5848 if (
Left.isTrailingComment())
5850 if (
Left.IsUnterminatedLiteral)
5853 if (BeforeLeft && BeforeLeft->is(tok::lessless) &&
5854 Left.is(tok::string_literal) &&
Right.is(tok::lessless) && AfterRight &&
5855 AfterRight->is(tok::string_literal)) {
5856 return Right.NewlinesBefore > 0;
5859 if (
Right.is(TT_RequiresClause)) {
5860 switch (Style.RequiresClausePosition) {
5861 case FormatStyle::RCPS_OwnLine:
5862 case FormatStyle::RCPS_OwnLineWithBrace:
5863 case FormatStyle::RCPS_WithFollowing:
5870 if (
Left.ClosesTemplateDeclaration &&
Left.MatchingParen &&
5871 Left.MatchingParen->NestingLevel == 0) {
5875 if (
Right.is(tok::kw_concept))
5876 return Style.BreakBeforeConceptDeclarations == FormatStyle::BBCDS_Always;
5877 return Style.BreakTemplateDeclarations == FormatStyle::BTDS_Yes ||
5878 (Style.BreakTemplateDeclarations == FormatStyle::BTDS_Leave &&
5879 Right.NewlinesBefore > 0);
5881 if (
Left.ClosesRequiresClause) {
5882 switch (Style.RequiresClausePosition) {
5883 case FormatStyle::RCPS_OwnLine:
5884 case FormatStyle::RCPS_WithPreceding:
5885 return Right.isNot(tok::semi);
5886 case FormatStyle::RCPS_OwnLineWithBrace:
5887 return Right.isNoneOf(tok::semi, tok::l_brace);
5892 if (Style.PackConstructorInitializers == FormatStyle::PCIS_Never) {
5893 if (Style.BreakConstructorInitializers == FormatStyle::BCIS_BeforeColon &&
5894 (
Left.is(TT_CtorInitializerComma) ||
5895 Right.is(TT_CtorInitializerColon))) {
5899 if (Style.BreakConstructorInitializers == FormatStyle::BCIS_AfterColon &&
5900 Left.isOneOf(TT_CtorInitializerColon, TT_CtorInitializerComma)) {
5904 if (Style.PackConstructorInitializers < FormatStyle::PCIS_CurrentLine &&
5905 Style.BreakConstructorInitializers == FormatStyle::BCIS_BeforeComma &&
5906 Right.isOneOf(TT_CtorInitializerComma, TT_CtorInitializerColon)) {
5909 if (Style.PackConstructorInitializers == FormatStyle::PCIS_NextLineOnly) {
5910 if ((Style.BreakConstructorInitializers == FormatStyle::BCIS_BeforeColon ||
5911 Style.BreakConstructorInitializers == FormatStyle::BCIS_BeforeComma) &&
5912 Right.is(TT_CtorInitializerColon)) {
5916 if (Style.BreakConstructorInitializers == FormatStyle::BCIS_AfterColon &&
5917 Left.is(TT_CtorInitializerColon)) {
5922 if (Style.BreakInheritanceList == FormatStyle::BILS_BeforeComma &&
5923 Right.is(TT_InheritanceComma)) {
5926 if (Style.BreakInheritanceList == FormatStyle::BILS_AfterComma &&
5927 Left.is(TT_InheritanceComma)) {
5930 if (
Right.is(tok::string_literal) &&
Right.TokenText.starts_with(
"R\"")) {
5934 return Right.IsMultiline &&
Right.NewlinesBefore > 0;
5936 if ((
Left.is(tok::l_brace) ||
5937 (
Left.is(tok::less) && BeforeLeft && BeforeLeft->is(tok::equal))) &&
5938 Right.NestingLevel == 1 && Style.Language == FormatStyle::LK_Proto) {
5943 if (
Right.is(TT_InlineASMBrace))
5944 return Right.HasUnescapedNewline;
5947 auto *FirstNonComment =
Line.getFirstNonComment();
5949 FirstNonComment && (FirstNonComment->is(Keywords.kw_internal) ||
5950 FirstNonComment->isAccessSpecifierKeyword());
5952 if (Style.BraceWrapping.AfterEnum) {
5953 if (
Line.startsWith(tok::kw_enum) ||
5954 Line.startsWith(tok::kw_typedef, tok::kw_enum)) {
5959 FirstNonComment->Next->is(tok::kw_enum)) {
5965 if (Style.BraceWrapping.AfterClass &&
5967 FirstNonComment->Next->is(Keywords.kw_interface)) ||
5968 Line.startsWith(Keywords.kw_interface))) {
5973 if (
Right.isNot(TT_FunctionLBrace)) {
5974 return (
Line.startsWith(tok::kw_class) &&
5975 Style.BraceWrapping.AfterClass) ||
5976 (
Line.startsWith(tok::kw_struct) &&
5977 Style.BraceWrapping.AfterStruct);
5981 if (
Left.is(TT_ObjCBlockLBrace) &&
5982 Style.AllowShortBlocksOnASingleLine == FormatStyle::SBS_Never) {
5987 if (
Left.isOneOf(TT_AttributeRParen, TT_AttributeMacro) &&
5988 Right.is(TT_ObjCDecl)) {
5992 if (
Left.is(TT_LambdaLBrace)) {
5994 Style.AllowShortLambdasOnASingleLine == FormatStyle::SLS_Inline) {
5998 if (Style.AllowShortLambdasOnASingleLine == FormatStyle::SLS_None ||
5999 Style.AllowShortLambdasOnASingleLine == FormatStyle::SLS_Inline ||
6000 (!
Left.Children.empty() &&
6001 Style.AllowShortLambdasOnASingleLine == FormatStyle::SLS_Empty)) {
6006 if (Style.BraceWrapping.BeforeLambdaBody &&
Right.is(TT_LambdaLBrace) &&
6007 (
Left.isPointerOrReference() ||
Left.is(TT_TemplateCloser))) {
6012 if ((Style.isJava() || Style.isJavaScript()) &&
6013 Left.is(TT_LeadingJavaAnnotation) &&
6014 Right.isNoneOf(TT_LeadingJavaAnnotation, tok::l_paren) &&
6015 (
Line.Last->is(tok::l_brace) || Style.BreakAfterJavaFieldAnnotations)) {
6019 if (
Right.is(TT_ProtoExtensionLSquare))
6049 if (Style.isProto() &&
Right.is(TT_SelectorName) &&
6050 Right.isNot(tok::r_square) && AfterRight) {
6053 if (
Left.is(tok::at))
6059 const auto *LBrace = AfterRight;
6060 if (LBrace && LBrace->is(tok::colon)) {
6061 LBrace = LBrace->Next;
6062 if (LBrace && LBrace->is(tok::at)) {
6063 LBrace = LBrace->Next;
6065 LBrace = LBrace->Next;
6077 ((LBrace->is(tok::l_brace) &&
6078 (LBrace->is(TT_DictLiteral) ||
6079 (LBrace->Next && LBrace->Next->is(tok::r_brace)))) ||
6080 LBrace->isOneOf(TT_ArrayInitializerLSquare, tok::less))) {
6087 if (
Left.ParameterCount == 0)
6102 if (
Left.isOneOf(tok::r_brace, tok::greater, tok::r_square))
6113 if (Style.isCSharp()) {
6114 if (
Left.isOneOf(TT_CSharpNamedArgumentColon, TT_AttributeColon) ||
6115 Right.isOneOf(TT_CSharpNamedArgumentColon, TT_AttributeColon)) {
6119 if (
Line.First->is(TT_CSharpGenericTypeConstraint))
6120 return Left.is(TT_CSharpGenericTypeConstraintComma);
6122 if (
Right.is(TT_CSharpNullable))
6124 }
else if (Style.isJava()) {
6125 if (
Left.isOneOf(Keywords.kw_throws, Keywords.kw_extends,
6126 Keywords.kw_implements)) {
6129 if (
Right.isOneOf(Keywords.kw_throws, Keywords.kw_extends,
6130 Keywords.kw_implements)) {
6133 }
else if (Style.isJavaScript()) {
6136 (NonComment->isAccessSpecifierKeyword() ||
6137 NonComment->isOneOf(
6138 tok::kw_return, Keywords.kw_yield, tok::kw_continue, tok::kw_break,
6139 tok::kw_throw, Keywords.kw_interface, Keywords.kw_type,
6140 tok::kw_static, Keywords.kw_readonly, Keywords.kw_override,
6141 Keywords.kw_abstract, Keywords.kw_get, Keywords.kw_set,
6142 Keywords.kw_async, Keywords.kw_await))) {
6145 if (
Right.NestingLevel == 0 &&
6146 (
Left.Tok.getIdentifierInfo() ||
6147 Left.isOneOf(tok::r_square, tok::r_paren)) &&
6148 Right.isOneOf(tok::l_square, tok::l_paren)) {
6151 if (NonComment && NonComment->is(tok::identifier) &&
6152 NonComment->TokenText ==
"asserts") {
6155 if (
Left.is(TT_FatArrow) &&
Right.is(tok::l_brace))
6157 if (
Left.is(TT_JsTypeColon))
6160 if (
Left.is(tok::exclaim) &&
Right.is(tok::colon))
6165 if (
Right.is(Keywords.kw_is)) {
6174 if (!
Next ||
Next->isNot(tok::colon))
6177 if (
Left.is(Keywords.kw_in))
6178 return Style.BreakBeforeBinaryOperators == FormatStyle::BOS_None;
6179 if (
Right.is(Keywords.kw_in))
6180 return Style.BreakBeforeBinaryOperators != FormatStyle::BOS_None;
6181 if (
Right.is(Keywords.kw_as))
6183 if (
Right.isOneOf(Keywords.kw_extends, Keywords.kw_infer)) {
6189 if (
Left.is(Keywords.kw_as))
6191 if (
Left.is(TT_NonNullAssertion))
6193 if (
Left.is(Keywords.kw_declare) &&
6194 Right.isOneOf(Keywords.kw_module, tok::kw_namespace,
6195 Keywords.kw_function, tok::kw_class, tok::kw_enum,
6196 Keywords.kw_interface, Keywords.kw_type, Keywords.kw_var,
6197 Keywords.kw_let, tok::kw_const)) {
6202 if (
Left.isOneOf(Keywords.kw_module, tok::kw_namespace) &&
6203 Right.isOneOf(tok::identifier, tok::string_literal)) {
6206 if (
Right.is(TT_TemplateString) &&
Right.closesScope())
6210 if (
Left.is(tok::identifier) &&
Right.is(TT_TemplateString))
6212 if (
Left.is(TT_TemplateString) &&
Left.opensScope())
6214 }
else if (Style.isTableGen()) {
6216 if (Keywords.isTableGenDefinition(Left))
6219 if (
Right.is(tok::l_paren)) {
6220 return Left.isNoneOf(TT_TableGenBangOperator, TT_TableGenCondOperator,
6224 if (
Left.is(TT_TableGenValueSuffix))
6227 if (
Left.is(tok::hash) ||
Right.is(tok::hash))
6229 if (
Left.isOneOf(TT_TableGenBangOperator, TT_TableGenCondOperator))
6236 if (
Right.is(tok::r_brace)) {
6238 (
Right.isBlockIndentedInitRBrace(Style)));
6243 if (
Right.is(tok::r_paren)) {
6244 if (!
Right.MatchingParen)
6247 if (
Next &&
Next->is(tok::r_paren))
6249 if (
Next &&
Next->is(tok::l_paren))
6255 return Style.BreakBeforeCloseBracketIf;
6257 return Style.BreakBeforeCloseBracketLoop;
6259 return Style.BreakBeforeCloseBracketSwitch;
6260 return Style.BreakBeforeCloseBracketFunction;
6263 if (
Left.isOneOf(tok::r_paren, TT_TrailingAnnotation) &&
6264 Right.is(TT_TrailingAnnotation) &&
6265 Style.BreakBeforeCloseBracketFunction) {
6269 if (
Right.is(TT_TemplateCloser))
6270 return Style.BreakBeforeTemplateCloser;
6272 if (
Left.isOneOf(tok::at, tok::objc_interface))
6274 if (
Left.isOneOf(TT_JavaAnnotation, TT_LeadingJavaAnnotation))
6275 return Right.isNot(tok::l_paren);
6276 if (
Right.is(TT_PointerOrReference)) {
6277 return Line.IsMultiVariableDeclStmt ||
6278 (getTokenPointerOrReferenceAlignment(Right) ==
6279 FormatStyle::PAS_Right &&
6281 Right.Next->isOneOf(TT_FunctionDeclarationName, tok::kw_const)));
6283 if (
Right.isOneOf(TT_StartOfName, TT_FunctionDeclarationName,
6284 TT_ClassHeadName, TT_QtProperty, tok::kw_operator)) {
6287 if (
Left.is(TT_PointerOrReference))
6289 if (
Right.isTrailingComment()) {
6296 (
Left.is(TT_CtorInitializerColon) &&
Right.NewlinesBefore > 0 &&
6297 Style.BreakConstructorInitializers == FormatStyle::BCIS_AfterColon);
6299 if (
Left.is(tok::question) &&
Right.is(tok::colon))
6301 if (
Right.isOneOf(TT_ConditionalExpr, tok::question))
6302 return Style.BreakBeforeTernaryOperators;
6303 if (
Left.isOneOf(TT_ConditionalExpr, tok::question))
6304 return !Style.BreakBeforeTernaryOperators;
6305 if (
Left.is(TT_InheritanceColon))
6306 return Style.BreakInheritanceList == FormatStyle::BILS_AfterColon;
6307 if (
Right.is(TT_InheritanceColon))
6308 return Style.BreakInheritanceList != FormatStyle::BILS_AfterColon;
6310 if (
Right.is(TT_ObjCMethodExpr) &&
Right.isNot(tok::r_square) &&
6311 Left.isNot(TT_SelectorName)) {
6315 if (
Right.is(tok::colon) &&
6316 Right.isNoneOf(TT_CtorInitializerColon, TT_InlineASMColon,
6317 TT_BitFieldColon)) {
6320 if (
Left.is(tok::colon) &&
Left.isOneOf(TT_ObjCSelector, TT_ObjCMethodExpr))
6322 if (
Left.is(tok::colon) &&
Left.is(TT_DictLiteral)) {
6323 if (Style.isProto()) {
6324 if (!Style.AlwaysBreakBeforeMultilineStrings &&
Right.isStringLiteral())
6350 if ((
Right.isOneOf(tok::l_brace, tok::less) &&
6351 Right.is(TT_DictLiteral)) ||
6352 Right.is(TT_ArrayInitializerLSquare)) {
6358 if (
Right.is(tok::r_square) &&
Right.MatchingParen &&
6359 Right.MatchingParen->is(TT_ProtoExtensionLSquare)) {
6362 if (
Right.is(TT_SelectorName) || (
Right.is(tok::identifier) &&
Right.Next &&
6363 Right.Next->is(TT_ObjCMethodExpr))) {
6364 return Left.isNot(tok::period);
6368 if (
Right.is(tok::kw_concept))
6369 return Style.BreakBeforeConceptDeclarations != FormatStyle::BBCDS_Never;
6370 if (
Right.is(TT_RequiresClause))
6372 if (
Left.ClosesTemplateDeclaration) {
6373 return Style.BreakTemplateDeclarations != FormatStyle::BTDS_Leave ||
6374 Right.NewlinesBefore > 0;
6376 if (
Left.is(TT_FunctionAnnotationRParen))
6378 if (
Left.ClosesRequiresClause)
6380 if (
Right.isOneOf(TT_RangeBasedForLoopColon, TT_OverloadedOperatorLParen,
6381 TT_OverloadedOperator)) {
6384 if (
Left.is(TT_RangeBasedForLoopColon))
6386 if (
Right.is(TT_RangeBasedForLoopColon))
6388 if (
Left.is(TT_TemplateCloser) &&
Right.is(TT_TemplateOpener))
6390 if ((
Left.is(tok::greater) &&
Right.is(tok::greater)) ||
6391 (
Left.is(tok::less) &&
Right.is(tok::less))) {
6394 if (
Right.is(TT_BinaryOperator) &&
6395 Style.BreakBeforeBinaryOperators != FormatStyle::BOS_None &&
6396 (Style.BreakBeforeBinaryOperators == FormatStyle::BOS_All ||
6400 if (
Left.isOneOf(TT_TemplateCloser, TT_UnaryOperator, tok::kw_operator))
6402 if (
Left.is(tok::equal) &&
Right.isNoneOf(tok::kw_default, tok::kw_delete) &&
6406 if (
Left.is(tok::equal) &&
Right.is(tok::l_brace) &&
6407 Style.Cpp11BracedListStyle == FormatStyle::BLS_Block) {
6410 if (
Left.is(TT_AttributeLParen) ||
6411 (
Left.is(tok::l_paren) &&
Left.is(TT_TypeDeclarationParen))) {
6414 if (
Left.is(tok::l_paren) &&
Left.Previous &&
6415 (
Left.Previous->isOneOf(TT_BinaryOperator, TT_CastRParen))) {
6418 if (
Right.is(TT_ImplicitStringLiteral))
6421 if (
Right.is(tok::r_square) &&
Right.MatchingParen &&
6422 Right.MatchingParen->is(TT_LambdaLSquare)) {
6428 if (
Left.is(TT_TrailingAnnotation)) {
6429 return Right.isNoneOf(tok::l_brace, tok::semi, tok::equal, tok::l_paren,
6430 tok::less, tok::coloncolon);
6433 if (
Right.isAttribute())
6436 if (
Right.is(TT_AttributeLSquare)) {
6437 assert(
Left.isNot(tok::l_square));
6441 if (
Left.is(tok::identifier) &&
Right.is(tok::string_literal))
6444 if (
Right.is(tok::identifier) &&
Right.Next &&
Right.Next->is(TT_DictLiteral))
6447 if (
Left.is(TT_CtorInitializerColon)) {
6448 return Style.BreakConstructorInitializers == FormatStyle::BCIS_AfterColon &&
6449 (!
Right.isTrailingComment() ||
Right.NewlinesBefore > 0);
6451 if (
Right.is(TT_CtorInitializerColon))
6452 return Style.BreakConstructorInitializers != FormatStyle::BCIS_AfterColon;
6453 if (
Left.is(TT_CtorInitializerComma) &&
6454 Style.BreakConstructorInitializers == FormatStyle::BCIS_BeforeComma) {
6457 if (
Right.is(TT_CtorInitializerComma) &&
6458 Style.BreakConstructorInitializers == FormatStyle::BCIS_BeforeComma) {
6461 if (
Left.is(TT_InheritanceComma) &&
6462 Style.BreakInheritanceList == FormatStyle::BILS_BeforeComma) {
6465 if (
Right.is(TT_InheritanceComma) &&
6466 Style.BreakInheritanceList == FormatStyle::BILS_BeforeComma) {
6469 if (
Left.is(TT_ArrayInitializerLSquare))
6471 if (
Right.is(tok::kw_typename) &&
Left.isNot(tok::kw_const))
6473 if ((
Left.isBinaryOperator() ||
Left.is(TT_BinaryOperator)) &&
6474 Left.isNoneOf(tok::arrowstar, tok::lessless) &&
6475 Style.BreakBeforeBinaryOperators != FormatStyle::BOS_All &&
6476 (Style.BreakBeforeBinaryOperators == FormatStyle::BOS_None ||
6480 if (
Left.is(TT_AttributeLSquare) &&
Right.is(tok::l_square)) {
6481 assert(
Right.isNot(TT_AttributeLSquare));
6484 if (
Left.is(tok::r_square) &&
Right.is(TT_AttributeRSquare)) {
6485 assert(
Left.isNot(TT_AttributeRSquare));
6489 auto ShortLambdaOption = Style.AllowShortLambdasOnASingleLine;
6490 if (Style.BraceWrapping.BeforeLambdaBody &&
Right.is(TT_LambdaLBrace)) {
6497 if (
Right.is(tok::kw_noexcept) &&
Right.is(TT_TrailingAnnotation)) {
6498 switch (Style.AllowBreakBeforeNoexceptSpecifier) {
6499 case FormatStyle::BBNSS_Never:
6501 case FormatStyle::BBNSS_Always:
6503 case FormatStyle::BBNSS_OnlyWithParen:
6504 return Right.Next &&
Right.Next->is(tok::l_paren);
6508 return Left.isOneOf(tok::comma, tok::coloncolon, tok::semi, tok::l_brace,
6509 tok::kw_class, tok::kw_struct, tok::comment) ||
6510 Right.isMemberAccess() ||
6511 Right.isOneOf(TT_TrailingReturnArrow, TT_LambdaArrow, tok::lessless,
6512 tok::colon, tok::l_square, tok::at) ||
6513 (
Left.is(tok::r_paren) &&
6514 Right.isOneOf(tok::identifier, tok::kw_const)) ||
6515 (
Left.is(tok::l_paren) &&
Right.isNot(tok::r_paren)) ||
6516 (
Left.is(TT_TemplateOpener) &&
Right.isNot(TT_TemplateCloser));
6520 llvm::errs() <<
"AnnotatedTokens(L=" <<
Line.Level <<
", P=" <<
Line.PPLevel
6521 <<
", T=" <<
Line.Type <<
", C=" <<
Line.IsContinuation
6525 llvm::errs() <<
" I=" <<
Tok->IndentLevel <<
" M=" <<
Tok->MustBreakBefore
6526 <<
" C=" <<
Tok->CanBreakBefore
6528 <<
" S=" <<
Tok->SpacesRequiredBefore
6529 <<
" F=" <<
Tok->Finalized <<
" B=" <<
Tok->BlockParameterCount
6530 <<
" BK=" <<
Tok->getBlockKind() <<
" P=" <<
Tok->SplitPenalty
6531 <<
" Name=" <<
Tok->Tok.getName() <<
" N=" <<
Tok->NestingLevel
6532 <<
" L=" <<
Tok->TotalLength
6533 <<
" PPK=" <<
Tok->getPackingKind() <<
" FakeLParens=";
6535 llvm::errs() << LParen <<
"/";
6536 llvm::errs() <<
" FakeRParens=" <<
Tok->FakeRParens;
6537 llvm::errs() <<
" II=" <<
Tok->Tok.getIdentifierInfo();
6538 llvm::errs() <<
" Text='" <<
Tok->TokenText <<
"'\n";
6543 llvm::errs() <<
"----\n";
6546FormatStyle::PointerAlignmentStyle
6548 assert(
Reference.isOneOf(tok::amp, tok::ampamp));
6549 switch (Style.ReferenceAlignment) {
6550 case FormatStyle::RAS_Pointer:
6551 return Style.PointerAlignment;
6552 case FormatStyle::RAS_Left:
6553 return FormatStyle::PAS_Left;
6554 case FormatStyle::RAS_Right:
6555 return FormatStyle::PAS_Right;
6556 case FormatStyle::RAS_Middle:
6557 return FormatStyle::PAS_Middle;
6560 return Style.PointerAlignment;
6563FormatStyle::PointerAlignmentStyle
6564TokenAnnotator::getTokenPointerOrReferenceAlignment(
6566 if (PointerOrReference.isOneOf(tok::amp, tok::ampamp))
6567 return getTokenReferenceAlignment(PointerOrReference);
6568 assert(PointerOrReference.is(tok::star));
6569 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.
bool Ret(InterpState &S, CodePtr &PC)
TokenKind
Provides a simple uniform namespace for tokens from all C languages.
The JSON file list parser is used to communicate input to InstallAPI.
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
@ 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.