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->isOneOf(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 StartsObjCMethodExpr =
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 StartsObjCMethodExpr =
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_static_assert, tok::kw_noexcept, tok::kw_explicit,
362 tok::kw_while, 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 (StartsObjCMethodExpr) {
455 Contexts.back().ColonIsObjCMethodExpr =
true;
456 OpeningParen.setType(TT_ObjCMethodExpr);
467 bool MightBeFunctionType = !Contexts[Contexts.size() - 2].IsExpression;
468 bool ProbablyFunctionType =
469 CurrentToken->isPointerOrReference() || CurrentToken->is(tok::caret);
470 bool HasMultipleLines =
false;
471 bool HasMultipleParametersOnALine =
false;
472 bool MightBeObjCForRangeLoop =
473 OpeningParen.Previous && OpeningParen.Previous->is(tok::kw_for);
475 while (CurrentToken) {
476 const auto &Prev = *CurrentToken->Previous;
477 const auto *PrevPrev = Prev.Previous;
478 if (Prev.is(TT_PointerOrReference) &&
479 PrevPrev->isOneOf(tok::l_paren, tok::coloncolon)) {
480 ProbablyFunctionType =
true;
482 if (CurrentToken->is(tok::comma))
483 MightBeFunctionType =
false;
484 if (Prev.is(TT_BinaryOperator))
485 Contexts.back().IsExpression =
true;
486 if (CurrentToken->is(tok::r_paren)) {
487 if (Prev.is(TT_PointerOrReference) &&
488 (PrevPrev == &OpeningParen || PrevPrev->is(tok::coloncolon))) {
489 MightBeFunctionType =
true;
491 if (OpeningParen.isNot(TT_CppCastLParen) && MightBeFunctionType &&
492 ProbablyFunctionType && CurrentToken->Next &&
493 (CurrentToken->Next->is(tok::l_paren) ||
494 (CurrentToken->Next->is(tok::l_square) &&
495 (Line.MustBeDeclaration ||
496 (PrevNonComment && PrevNonComment->isTypeName(LangOpts)))))) {
497 OpeningParen.setType(OpeningParen.Next->is(tok::caret)
499 : TT_FunctionTypeLParen);
501 OpeningParen.MatchingParen = CurrentToken;
502 CurrentToken->MatchingParen = &OpeningParen;
504 if (CurrentToken->Next && CurrentToken->Next->is(tok::l_brace) &&
505 OpeningParen.Previous && OpeningParen.Previous->is(tok::l_paren)) {
511 if (
Tok->is(TT_BinaryOperator) &&
Tok->isPointerOrReference())
512 Tok->setType(TT_PointerOrReference);
516 if (StartsObjCMethodExpr) {
517 CurrentToken->setType(TT_ObjCMethodExpr);
518 if (Contexts.back().FirstObjCSelectorName) {
519 Contexts.back().FirstObjCSelectorName->LongestObjCSelectorName =
520 Contexts.back().LongestObjCSelectorName;
524 if (OpeningParen.is(TT_AttributeLParen))
525 CurrentToken->setType(TT_AttributeRParen);
526 if (OpeningParen.is(TT_TypeDeclarationParen))
527 CurrentToken->setType(TT_TypeDeclarationParen);
528 if (OpeningParen.Previous &&
529 OpeningParen.Previous->is(TT_JavaAnnotation)) {
530 CurrentToken->setType(TT_JavaAnnotation);
532 if (OpeningParen.Previous &&
533 OpeningParen.Previous->is(TT_LeadingJavaAnnotation)) {
534 CurrentToken->setType(TT_LeadingJavaAnnotation);
536 if (OpeningParen.Previous &&
537 OpeningParen.Previous->is(TT_AttributeSquare)) {
538 CurrentToken->setType(TT_AttributeSquare);
541 if (!HasMultipleLines)
543 else if (HasMultipleParametersOnALine)
551 if (CurrentToken->isOneOf(tok::r_square, tok::r_brace))
554 if (CurrentToken->is(tok::l_brace) && OpeningParen.is(TT_ObjCBlockLParen))
555 OpeningParen.setType(TT_Unknown);
556 if (CurrentToken->is(tok::comma) && CurrentToken->Next &&
557 !CurrentToken->Next->HasUnescapedNewline &&
558 !CurrentToken->Next->isTrailingComment()) {
559 HasMultipleParametersOnALine =
true;
561 bool ProbablyFunctionTypeLParen =
562 (CurrentToken->is(tok::l_paren) && CurrentToken->Next &&
563 CurrentToken->Next->isOneOf(tok::star, tok::amp, tok::caret));
564 if ((Prev.isOneOf(tok::kw_const, tok::kw_auto) ||
565 Prev.isTypeName(LangOpts)) &&
566 !(CurrentToken->is(tok::l_brace) ||
567 (CurrentToken->is(tok::l_paren) && !ProbablyFunctionTypeLParen))) {
568 Contexts.back().IsExpression =
false;
570 if (CurrentToken->isOneOf(tok::semi, tok::colon)) {
571 MightBeObjCForRangeLoop =
false;
572 if (PossibleObjCForInToken) {
573 PossibleObjCForInToken->setType(TT_Unknown);
574 PossibleObjCForInToken =
nullptr;
577 if (IsIf && CurrentToken->is(tok::semi)) {
578 for (
auto *
Tok = OpeningParen.Next;
579 Tok != CurrentToken &&
580 !
Tok->isOneOf(tok::equal, tok::l_paren, tok::l_brace);
582 if (
Tok->isPointerOrReference())
583 Tok->setFinalizedType(TT_PointerOrReference);
586 if (MightBeObjCForRangeLoop && CurrentToken->is(Keywords.kw_in)) {
587 PossibleObjCForInToken = CurrentToken;
588 PossibleObjCForInToken->setType(TT_ObjCForIn);
592 if (CurrentToken->is(tok::comma))
593 Contexts.back().CanBeExpression =
true;
595 if (Style.isTableGen()) {
596 if (CurrentToken->is(tok::comma)) {
597 if (Contexts.back().IsTableGenCondOpe)
598 CurrentToken->setType(TT_TableGenCondOperatorComma);
600 }
else if (CurrentToken->is(tok::colon)) {
601 if (Contexts.back().IsTableGenCondOpe)
602 CurrentToken->setType(TT_TableGenCondOperatorColon);
606 if (!parseTableGenValue())
614 updateParameterCount(&OpeningParen,
Tok);
615 if (CurrentToken && CurrentToken->HasUnescapedNewline)
616 HasMultipleLines =
true;
622 if (!Style.isCSharp())
626 if (
Tok.Previous &&
Tok.Previous->is(tok::identifier))
630 if (
Tok.Previous &&
Tok.Previous->is(tok::r_square)) {
641 if (AttrTok->is(tok::r_square))
645 while (AttrTok && AttrTok->isNot(tok::r_square))
646 AttrTok = AttrTok->Next;
652 AttrTok = AttrTok->Next;
657 if (AttrTok->isAccessSpecifierKeyword() ||
658 AttrTok->isOneOf(tok::comment, tok::kw_class, tok::kw_static,
659 tok::l_square, Keywords.kw_internal)) {
665 AttrTok->Next->startsSequence(tok::identifier, tok::l_paren)) {
681 Left->ParentBracket = Contexts.back().ContextKind;
687 bool CppArrayTemplates =
688 IsCpp && Parent && Parent->is(TT_TemplateCloser) &&
689 (Contexts.back().CanBeExpression || Contexts.back().IsExpression ||
690 Contexts.back().ContextType == Context::TemplateArgument);
692 const bool IsInnerSquare = Contexts.back().InCpp11AttributeSpecifier;
693 const bool IsCpp11AttributeSpecifier =
694 isCppAttribute(IsCpp, *Left) || IsInnerSquare;
697 bool IsCSharpAttributeSpecifier =
698 isCSharpAttributeSpecifier(*Left) ||
699 Contexts.back().InCSharpAttributeSpecifier;
701 bool InsideInlineASM = Line.startsWith(tok::kw_asm);
702 bool IsCppStructuredBinding =
Left->isCppStructuredBinding(IsCpp);
703 bool StartsObjCMethodExpr =
704 !IsCppStructuredBinding && !InsideInlineASM && !CppArrayTemplates &&
705 IsCpp && !IsCpp11AttributeSpecifier && !IsCSharpAttributeSpecifier &&
706 Contexts.back().CanBeExpression &&
Left->isNot(TT_LambdaLSquare) &&
707 !CurrentToken->isOneOf(tok::l_brace, tok::r_square) &&
709 Parent->isOneOf(tok::colon, tok::l_square, tok::l_paren,
710 tok::kw_return, tok::kw_throw) ||
711 Parent->isUnaryOperator() ||
713 Parent->isOneOf(TT_ObjCForIn, TT_CastRParen) ||
716 bool ColonFound =
false;
718 unsigned BindingIncrease = 1;
719 if (IsCppStructuredBinding) {
720 Left->setType(TT_StructuredBindingLSquare);
721 }
else if (
Left->is(TT_Unknown)) {
722 if (StartsObjCMethodExpr) {
723 Left->setType(TT_ObjCMethodExpr);
724 }
else if (InsideInlineASM) {
725 Left->setType(TT_InlineASMSymbolicNameLSquare);
726 }
else if (IsCpp11AttributeSpecifier) {
727 Left->setType(TT_AttributeSquare);
728 if (!IsInnerSquare &&
Left->Previous)
729 Left->Previous->EndsCppAttributeGroup =
false;
730 }
else if (Style.isJavaScript() && Parent &&
731 Contexts.back().ContextKind == tok::l_brace &&
732 Parent->isOneOf(tok::l_brace, tok::comma)) {
733 Left->setType(TT_JsComputedPropertyName);
734 }
else if (IsCpp && Contexts.back().ContextKind == tok::l_brace &&
735 Parent && Parent->isOneOf(tok::l_brace, tok::comma)) {
736 Left->setType(TT_DesignatedInitializerLSquare);
737 }
else if (IsCSharpAttributeSpecifier) {
738 Left->setType(TT_AttributeSquare);
739 }
else if (CurrentToken->is(tok::r_square) && Parent &&
740 Parent->is(TT_TemplateCloser)) {
741 Left->setType(TT_ArraySubscriptLSquare);
742 }
else if (Style.isProto()) {
769 Left->setType(TT_ArrayInitializerLSquare);
770 if (!
Left->endsSequence(tok::l_square, tok::numeric_constant,
772 !
Left->endsSequence(tok::l_square, tok::numeric_constant,
774 !
Left->endsSequence(tok::l_square, tok::colon, TT_SelectorName)) {
775 Left->setType(TT_ProtoExtensionLSquare);
776 BindingIncrease = 10;
778 }
else if (!CppArrayTemplates && Parent &&
779 Parent->isOneOf(TT_BinaryOperator, TT_TemplateCloser, tok::at,
780 tok::comma, tok::l_paren, tok::l_square,
781 tok::question, tok::colon, tok::kw_return,
784 Left->setType(TT_ArrayInitializerLSquare);
786 BindingIncrease = 10;
787 Left->setType(TT_ArraySubscriptLSquare);
791 ScopedContextCreator ContextCreator(*
this, tok::l_square, BindingIncrease);
792 Contexts.back().IsExpression =
true;
793 if (Style.isJavaScript() && Parent && Parent->is(TT_JsTypeColon))
794 Contexts.back().IsExpression =
false;
796 Contexts.back().ColonIsObjCMethodExpr = StartsObjCMethodExpr;
797 Contexts.back().InCpp11AttributeSpecifier = IsCpp11AttributeSpecifier;
798 Contexts.back().InCSharpAttributeSpecifier = IsCSharpAttributeSpecifier;
800 while (CurrentToken) {
801 if (CurrentToken->is(tok::r_square)) {
802 if (IsCpp11AttributeSpecifier) {
803 CurrentToken->setType(TT_AttributeSquare);
805 CurrentToken->EndsCppAttributeGroup =
true;
807 if (IsCSharpAttributeSpecifier) {
808 CurrentToken->setType(TT_AttributeSquare);
809 }
else if (((CurrentToken->Next &&
810 CurrentToken->Next->is(tok::l_paren)) ||
811 (CurrentToken->Previous &&
812 CurrentToken->Previous->Previous == Left)) &&
813 Left->is(TT_ObjCMethodExpr)) {
818 StartsObjCMethodExpr =
false;
819 Left->setType(TT_Unknown);
821 if (StartsObjCMethodExpr && CurrentToken->Previous != Left) {
822 CurrentToken->setType(TT_ObjCMethodExpr);
825 if (!ColonFound && CurrentToken->Previous &&
826 CurrentToken->Previous->is(TT_Unknown) &&
827 canBeObjCSelectorComponent(*CurrentToken->Previous)) {
828 CurrentToken->Previous->setType(TT_SelectorName);
833 if (Parent && Parent->is(TT_PointerOrReference))
834 Parent->overwriteFixedType(TT_BinaryOperator);
836 Left->MatchingParen = CurrentToken;
837 CurrentToken->MatchingParen =
Left;
842 if (!Contexts.back().FirstObjCSelectorName) {
845 Previous->ObjCSelectorNameParts = 1;
846 Contexts.back().FirstObjCSelectorName =
Previous;
849 Left->ParameterCount =
850 Contexts.back().FirstObjCSelectorName->ObjCSelectorNameParts;
852 if (Contexts.back().FirstObjCSelectorName) {
853 Contexts.back().FirstObjCSelectorName->LongestObjCSelectorName =
854 Contexts.back().LongestObjCSelectorName;
855 if (
Left->BlockParameterCount > 1)
856 Contexts.back().FirstObjCSelectorName->LongestObjCSelectorName = 0;
858 if (Style.isTableGen() &&
Left->is(TT_TableGenListOpener))
859 CurrentToken->setType(TT_TableGenListCloser);
863 if (CurrentToken->isOneOf(tok::r_paren, tok::r_brace))
865 if (CurrentToken->is(tok::colon)) {
866 if (IsCpp11AttributeSpecifier &&
867 CurrentToken->endsSequence(tok::colon, tok::identifier,
871 CurrentToken->setType(TT_AttributeColon);
872 }
else if (!Style.isVerilog() && !Line.InPragmaDirective &&
873 Left->isOneOf(TT_ArraySubscriptLSquare,
874 TT_DesignatedInitializerLSquare)) {
875 Left->setType(TT_ObjCMethodExpr);
876 StartsObjCMethodExpr =
true;
877 Contexts.back().ColonIsObjCMethodExpr =
true;
878 if (Parent && Parent->is(tok::r_paren)) {
880 Parent->setType(TT_CastRParen);
885 if (CurrentToken->is(tok::comma) &&
Left->is(TT_ObjCMethodExpr) &&
887 Left->setType(TT_ArrayInitializerLSquare);
890 if (Style.isTableGen()) {
891 if (CurrentToken->isOneOf(tok::comma, tok::minus, tok::ellipsis)) {
897 if (!parseTableGenValue())
900 updateParameterCount(Left,
Tok);
905 updateParameterCount(Left,
Tok);
910 void skipToNextNonComment() {
912 while (CurrentToken && CurrentToken->is(tok::comment))
921 bool parseTableGenValue(
bool ParseNameMode =
false) {
924 while (CurrentToken->is(tok::comment))
926 if (!parseTableGenSimpleValue())
931 if (CurrentToken->is(tok::hash)) {
932 if (CurrentToken->Next &&
933 CurrentToken->Next->isOneOf(tok::colon, tok::semi, tok::l_brace)) {
936 CurrentToken->setType(TT_TableGenTrailingPasteOperator);
941 skipToNextNonComment();
942 HashTok->setType(TT_Unknown);
943 if (!parseTableGenValue(ParseNameMode))
950 if (ParseNameMode && CurrentToken->is(tok::l_brace))
953 if (CurrentToken->isOneOf(tok::l_brace, tok::l_square, tok::period)) {
954 CurrentToken->setType(TT_TableGenValueSuffix);
956 skipToNextNonComment();
957 if (Suffix->is(tok::l_square))
958 return parseSquare();
959 if (Suffix->is(tok::l_brace)) {
960 Scopes.push_back(getScopeType(*Suffix));
970 bool tryToParseTableGenTokVar() {
973 if (CurrentToken->is(tok::identifier) &&
974 CurrentToken->TokenText.front() ==
'$') {
975 skipToNextNonComment();
983 bool parseTableGenDAGArg(
bool AlignColon =
false) {
984 if (tryToParseTableGenTokVar())
986 if (parseTableGenValue()) {
987 if (CurrentToken && CurrentToken->is(tok::colon)) {
989 CurrentToken->setType(TT_TableGenDAGArgListColonToAlign);
991 CurrentToken->setType(TT_TableGenDAGArgListColon);
992 skipToNextNonComment();
993 return tryToParseTableGenTokVar();
1004 auto &Opes = Style.TableGenBreakingDAGArgOperators;
1009 if (
Tok.isNot(tok::identifier) ||
1010 Tok.isOneOf(TT_TableGenBangOperator, TT_TableGenCondOperator)) {
1014 if (!
Tok.Next ||
Tok.Next->is(tok::colon))
1016 return llvm::is_contained(Opes,
Tok.TokenText.str());
1021 bool parseTableGenDAGArgAndList(
FormatToken *Opener) {
1023 if (!parseTableGenDAGArg())
1025 bool BreakInside =
false;
1026 if (Style.TableGenBreakInsideDAGArg != FormatStyle::DAS_DontBreak) {
1029 if (isTableGenDAGArgBreakingOperator(*FirstTok)) {
1032 Opener->setType(TT_TableGenDAGArgOpenerToBreak);
1033 if (FirstTok->isOneOf(TT_TableGenBangOperator,
1034 TT_TableGenCondOperator)) {
1037 CurrentToken->Previous->setType(TT_TableGenDAGArgOperatorToBreak);
1038 }
else if (FirstTok->is(tok::identifier)) {
1039 if (Style.TableGenBreakInsideDAGArg == FormatStyle::DAS_BreakAll)
1040 FirstTok->setType(TT_TableGenDAGArgOperatorToBreak);
1042 FirstTok->setType(TT_TableGenDAGArgOperatorID);
1047 return parseTableGenDAGArgList(Opener, BreakInside);
1052 bool parseTableGenDAGArgList(
FormatToken *Opener,
bool BreakInside) {
1053 ScopedContextCreator ContextCreator(*
this, tok::l_paren, 0);
1054 Contexts.back().IsTableGenDAGArgList =
true;
1055 bool FirstDAGArgListElm =
true;
1056 while (CurrentToken) {
1057 if (!FirstDAGArgListElm && CurrentToken->is(tok::comma)) {
1058 CurrentToken->setType(BreakInside ? TT_TableGenDAGArgListCommaToBreak
1059 : TT_TableGenDAGArgListComma);
1060 skipToNextNonComment();
1062 if (CurrentToken && CurrentToken->is(tok::r_paren)) {
1063 CurrentToken->setType(TT_TableGenDAGArgCloser);
1064 Opener->MatchingParen = CurrentToken;
1065 CurrentToken->MatchingParen = Opener;
1066 skipToNextNonComment();
1069 if (!parseTableGenDAGArg(
1071 Style.AlignConsecutiveTableGenBreakingDAGArgColons.Enabled)) {
1074 FirstDAGArgListElm =
false;
1079 bool parseTableGenSimpleValue() {
1080 assert(Style.isTableGen());
1084 skipToNextNonComment();
1086 if (
Tok->isOneOf(tok::numeric_constant, tok::string_literal,
1087 TT_TableGenMultiLineString, tok::kw_true, tok::kw_false,
1088 tok::question, tok::kw_int)) {
1092 if (
Tok->is(tok::l_brace)) {
1093 Scopes.push_back(getScopeType(*
Tok));
1094 return parseBrace();
1097 if (
Tok->is(tok::l_square)) {
1098 Tok->setType(TT_TableGenListOpener);
1101 if (
Tok->is(tok::less)) {
1102 CurrentToken->setType(TT_TemplateOpener);
1103 return parseAngle();
1109 if (
Tok->is(tok::l_paren)) {
1110 Tok->setType(TT_TableGenDAGArgOpener);
1112 if (Contexts.back().IsTableGenDAGArgList)
1113 Tok->SpacesRequiredBefore = 1;
1114 return parseTableGenDAGArgAndList(
Tok);
1117 if (
Tok->is(TT_TableGenBangOperator)) {
1118 if (CurrentToken && CurrentToken->is(tok::less)) {
1119 CurrentToken->setType(TT_TemplateOpener);
1120 skipToNextNonComment();
1124 if (!CurrentToken || CurrentToken->isNot(tok::l_paren))
1128 Contexts.back().IsTableGenBangOpe =
true;
1129 bool Result = parseParens();
1130 Contexts.back().IsTableGenBangOpe =
false;
1134 if (
Tok->is(TT_TableGenCondOperator)) {
1135 if (!CurrentToken || CurrentToken->isNot(tok::l_paren))
1138 return parseParens();
1143 if (
Tok->is(tok::identifier)) {
1145 if (CurrentToken && CurrentToken->is(tok::less)) {
1146 CurrentToken->setType(TT_TemplateOpener);
1147 skipToNextNonComment();
1148 return parseAngle();
1156 bool couldBeInStructArrayInitializer()
const {
1157 if (Contexts.size() < 2)
1161 const auto End = std::next(Contexts.rbegin(), 2);
1162 auto Last = Contexts.rbegin();
1165 if (
Last->ContextKind == tok::l_brace)
1167 return Depth == 2 &&
Last->ContextKind != tok::l_brace;
1174 assert(CurrentToken->Previous);
1175 FormatToken &OpeningBrace = *CurrentToken->Previous;
1176 assert(OpeningBrace.is(tok::l_brace));
1177 OpeningBrace.ParentBracket = Contexts.back().ContextKind;
1179 if (Contexts.back().CaretFound)
1180 OpeningBrace.overwriteFixedType(TT_ObjCBlockLBrace);
1181 Contexts.back().CaretFound =
false;
1183 ScopedContextCreator ContextCreator(*
this, tok::l_brace, 1);
1184 Contexts.back().ColonIsDictLiteral =
true;
1186 Contexts.back().IsExpression =
true;
1187 if (Style.isJavaScript() && OpeningBrace.Previous &&
1188 OpeningBrace.Previous->is(TT_JsTypeColon)) {
1189 Contexts.back().IsExpression =
false;
1191 if (Style.isVerilog() &&
1192 (!OpeningBrace.getPreviousNonComment() ||
1193 OpeningBrace.getPreviousNonComment()->isNot(Keywords.kw_apostrophe))) {
1194 Contexts.back().VerilogMayBeConcatenation =
true;
1196 if (Style.isTableGen())
1197 Contexts.back().ColonIsDictLiteral =
false;
1199 unsigned CommaCount = 0;
1200 while (CurrentToken) {
1201 if (CurrentToken->is(tok::r_brace)) {
1202 assert(!Scopes.empty());
1203 assert(Scopes.back() == getScopeType(OpeningBrace));
1205 assert(OpeningBrace.Optional == CurrentToken->Optional);
1206 OpeningBrace.MatchingParen = CurrentToken;
1207 CurrentToken->MatchingParen = &OpeningBrace;
1208 if (Style.AlignArrayOfStructures != FormatStyle::AIAS_None) {
1209 if (OpeningBrace.ParentBracket == tok::l_brace &&
1210 couldBeInStructArrayInitializer() && CommaCount > 0) {
1211 Contexts.back().ContextType = Context::StructArrayInitializer;
1217 if (CurrentToken->isOneOf(tok::r_paren, tok::r_square))
1219 updateParameterCount(&OpeningBrace, CurrentToken);
1220 if (CurrentToken->isOneOf(tok::colon, tok::l_brace, tok::less)) {
1222 if (
Previous->is(TT_JsTypeOptionalQuestion))
1224 if ((CurrentToken->is(tok::colon) && !Style.isTableGen() &&
1225 (!Contexts.back().ColonIsDictLiteral || !IsCpp)) ||
1227 OpeningBrace.setType(TT_DictLiteral);
1228 if (
Previous->Tok.getIdentifierInfo() ||
1229 Previous->is(tok::string_literal)) {
1230 Previous->setType(TT_SelectorName);
1233 if (CurrentToken->is(tok::colon) && OpeningBrace.is(TT_Unknown) &&
1234 !Style.isTableGen()) {
1235 OpeningBrace.setType(TT_DictLiteral);
1236 }
else if (Style.isJavaScript()) {
1237 OpeningBrace.overwriteFixedType(TT_DictLiteral);
1240 if (CurrentToken->is(tok::comma)) {
1241 if (Style.isJavaScript())
1242 OpeningBrace.overwriteFixedType(TT_DictLiteral);
1245 if (!consumeToken())
1255 if (Current->is(tok::l_brace) && Current->is(
BK_Block))
1256 ++
Left->BlockParameterCount;
1257 if (Current->is(tok::comma)) {
1258 ++
Left->ParameterCount;
1260 Left->Role.reset(
new CommaSeparatedList(Style));
1261 Left->Role->CommaFound(Current);
1262 }
else if (
Left->ParameterCount == 0 && Current->isNot(tok::comment)) {
1263 Left->ParameterCount = 1;
1267 bool parseConditional() {
1268 while (CurrentToken) {
1269 if (CurrentToken->is(tok::colon) && CurrentToken->is(TT_Unknown)) {
1270 CurrentToken->setType(TT_ConditionalExpr);
1274 if (!consumeToken())
1280 bool parseTemplateDeclaration() {
1281 if (!CurrentToken || CurrentToken->isNot(tok::less))
1284 CurrentToken->setType(TT_TemplateOpener);
1287 TemplateDeclarationDepth++;
1288 const bool WellFormed = parseAngle();
1289 TemplateDeclarationDepth--;
1293 if (CurrentToken && TemplateDeclarationDepth == 0)
1294 CurrentToken->Previous->ClosesTemplateDeclaration =
true;
1299 bool consumeToken() {
1301 const auto *Prev = CurrentToken->getPreviousNonComment();
1302 if (Prev && Prev->is(tok::r_square) && Prev->is(TT_AttributeSquare) &&
1303 CurrentToken->isOneOf(tok::kw_if, tok::kw_switch, tok::kw_case,
1304 tok::kw_default, tok::kw_for, tok::kw_while) &&
1306 CurrentToken->MustBreakBefore =
true;
1313 if (
Tok->is(TT_VerilogTableItem))
1316 if (
Tok->is(TT_TableGenMultiLineString))
1318 auto *Prev =
Tok->getPreviousNonComment();
1319 auto *
Next =
Tok->getNextNonComment();
1320 switch (
bool IsIf =
false;
Tok->Tok.getKind()) {
1323 if (!Prev && Line.MustBeDeclaration)
1324 Tok->setType(TT_ObjCMethodSpecifier);
1331 if (
Tok->isTypeFinalized())
1334 if (Style.isJavaScript()) {
1335 if (Contexts.back().ColonIsForRangeExpr ||
1336 (Contexts.size() == 1 &&
1337 !Line.First->isOneOf(tok::kw_enum, tok::kw_case)) ||
1338 Contexts.back().ContextKind == tok::l_paren ||
1339 Contexts.back().ContextKind == tok::l_square ||
1340 (!Contexts.back().IsExpression &&
1341 Contexts.back().ContextKind == tok::l_brace) ||
1342 (Contexts.size() == 1 &&
1343 Line.MustBeDeclaration)) {
1344 Contexts.back().IsExpression =
false;
1345 Tok->setType(TT_JsTypeColon);
1348 }
else if (Style.isCSharp()) {
1349 if (Contexts.back().InCSharpAttributeSpecifier) {
1350 Tok->setType(TT_AttributeColon);
1353 if (Contexts.back().ContextKind == tok::l_paren) {
1354 Tok->setType(TT_CSharpNamedArgumentColon);
1357 }
else if (Style.isVerilog() &&
Tok->isNot(TT_BinaryOperator)) {
1360 if (Keywords.isVerilogEnd(*Prev) || Keywords.isVerilogBegin(*Prev)) {
1361 Tok->setType(TT_VerilogBlockLabelColon);
1362 }
else if (Contexts.back().ContextKind == tok::l_square) {
1363 Tok->setType(TT_BitFieldColon);
1364 }
else if (Contexts.back().ColonIsDictLiteral) {
1365 Tok->setType(TT_DictLiteral);
1366 }
else if (Contexts.size() == 1) {
1370 Tok->setType(TT_CaseLabelColon);
1371 if (Line.Level > 1 || (!Line.InPPDirective && Line.Level > 0))
1376 if (Line.First->isOneOf(Keywords.kw_module, Keywords.kw_import) ||
1377 Line.First->startsSequence(tok::kw_export, Keywords.kw_module) ||
1378 Line.First->startsSequence(tok::kw_export, Keywords.kw_import)) {
1379 Tok->setType(TT_ModulePartitionColon);
1380 }
else if (Line.First->is(tok::kw_asm)) {
1381 Tok->setType(TT_InlineASMColon);
1382 }
else if (Contexts.back().ColonIsDictLiteral || Style.isProto()) {
1383 Tok->setType(TT_DictLiteral);
1384 if (Style.isTextProto())
1385 Prev->setType(TT_SelectorName);
1386 }
else if (Contexts.back().ColonIsObjCMethodExpr ||
1387 Line.startsWith(TT_ObjCMethodSpecifier)) {
1388 Tok->setType(TT_ObjCMethodExpr);
1389 const auto *PrevPrev = Prev->Previous;
1392 bool UnknownIdentifierInMethodDeclaration =
1393 Line.startsWith(TT_ObjCMethodSpecifier) &&
1394 Prev->is(tok::identifier) && Prev->is(TT_Unknown);
1397 !(PrevPrev->is(TT_CastRParen) ||
1398 (PrevPrev->is(TT_ObjCMethodExpr) && PrevPrev->is(tok::colon))) ||
1399 PrevPrev->is(tok::r_square) ||
1400 Contexts.back().LongestObjCSelectorName == 0 ||
1401 UnknownIdentifierInMethodDeclaration) {
1402 Prev->setType(TT_SelectorName);
1403 if (!Contexts.back().FirstObjCSelectorName)
1404 Contexts.back().FirstObjCSelectorName = Prev;
1405 else if (Prev->ColumnWidth > Contexts.back().LongestObjCSelectorName)
1406 Contexts.back().LongestObjCSelectorName = Prev->ColumnWidth;
1407 Prev->ParameterIndex =
1408 Contexts.back().FirstObjCSelectorName->ObjCSelectorNameParts;
1409 ++Contexts.back().FirstObjCSelectorName->ObjCSelectorNameParts;
1411 }
else if (Contexts.back().ColonIsForRangeExpr) {
1412 Tok->setType(TT_RangeBasedForLoopColon);
1413 for (
auto *Token = Prev;
1414 Token && !Token->isOneOf(tok::semi, tok::l_paren);
1415 Token = Token->Previous) {
1416 if (Token->isPointerOrReference())
1417 Token->setFinalizedType(TT_PointerOrReference);
1419 }
else if (Contexts.back().ContextType == Context::C11GenericSelection) {
1420 Tok->setType(TT_GenericSelectionColon);
1421 if (Prev->isPointerOrReference())
1422 Prev->setFinalizedType(TT_PointerOrReference);
1423 }
else if ((CurrentToken && CurrentToken->is(tok::numeric_constant)) ||
1424 (Prev->is(TT_StartOfName) && !Scopes.empty() &&
1426 Tok->setType(TT_BitFieldColon);
1427 }
else if (Contexts.size() == 1 &&
1428 !Line.getFirstNonComment()->isOneOf(tok::kw_enum, tok::kw_case,
1430 !Line.startsWith(tok::kw_typedef, tok::kw_enum)) {
1431 if (Prev->isOneOf(tok::r_paren, tok::kw_noexcept) ||
1432 Prev->ClosesRequiresClause) {
1433 Tok->setType(TT_CtorInitializerColon);
1434 }
else if (Prev->is(tok::kw_try)) {
1436 FormatToken *PrevPrev = Prev->getPreviousNonComment();
1439 if (PrevPrev && PrevPrev->isOneOf(tok::r_paren, tok::kw_noexcept))
1440 Tok->setType(TT_CtorInitializerColon);
1442 Tok->setType(TT_InheritanceColon);
1443 if (Prev->isAccessSpecifierKeyword())
1446 }
else if (canBeObjCSelectorComponent(*Prev) &&
Next &&
1447 (
Next->isOneOf(tok::r_paren, tok::comma) ||
1448 (canBeObjCSelectorComponent(*
Next) &&
Next->Next &&
1449 Next->Next->is(tok::colon)))) {
1452 Tok->setType(TT_ObjCMethodExpr);
1459 if (Style.isJavaScript() && !Contexts.back().IsExpression)
1460 Tok->setType(TT_JsTypeOperator);
1463 if (Style.isTableGen()) {
1465 if (!parseTableGenValue())
1467 if (CurrentToken && CurrentToken->is(Keywords.kw_then))
1472 CurrentToken->isOneOf(tok::kw_constexpr, tok::identifier)) {
1478 if (CurrentToken && CurrentToken->is(tok::l_paren)) {
1480 if (!parseParens(IsIf))
1485 if (Style.isJavaScript()) {
1487 if ((Prev && Prev->is(tok::period)) || (
Next &&
Next->is(tok::colon)))
1490 if (CurrentToken && CurrentToken->is(Keywords.kw_await))
1493 if (IsCpp && CurrentToken && CurrentToken->is(tok::kw_co_await))
1495 Contexts.back().ColonIsForRangeExpr =
true;
1496 if (!CurrentToken || CurrentToken->isNot(tok::l_paren))
1507 if (Prev && Prev->is(tok::r_paren) && Prev->MatchingParen &&
1508 Prev->MatchingParen->is(TT_OverloadedOperatorLParen)) {
1509 Prev->setType(TT_OverloadedOperator);
1510 Prev->MatchingParen->setType(TT_OverloadedOperator);
1511 Tok->setType(TT_OverloadedOperatorLParen);
1514 if (Style.isVerilog()) {
1520 auto IsInstancePort = [&]() {
1529 if (!Prev || !(PrevPrev = Prev->getPreviousNonComment()))
1532 if (Keywords.isVerilogIdentifier(*Prev) &&
1533 Keywords.isVerilogIdentifier(*PrevPrev)) {
1537 if (Prev->is(Keywords.kw_verilogHash) &&
1538 Keywords.isVerilogIdentifier(*PrevPrev)) {
1542 if (Keywords.isVerilogIdentifier(*Prev) && PrevPrev->is(tok::r_paren))
1545 if (Keywords.isVerilogIdentifier(*Prev) && PrevPrev->is(tok::comma)) {
1546 const FormatToken *PrevParen = PrevPrev->getPreviousNonComment();
1547 if (PrevParen && PrevParen->is(tok::r_paren) &&
1548 PrevParen->MatchingParen &&
1549 PrevParen->MatchingParen->is(TT_VerilogInstancePortLParen)) {
1556 if (IsInstancePort())
1557 Tok->setType(TT_VerilogInstancePortLParen);
1562 if (Line.MustBeDeclaration && Contexts.size() == 1 &&
1563 !Contexts.back().IsExpression && !Line.startsWith(TT_ObjCProperty) &&
1564 !Line.startsWith(tok::l_paren) &&
1565 !
Tok->isOneOf(TT_TypeDeclarationParen, TT_RequiresExpressionLParen)) {
1567 (!Prev->isAttribute() &&
1568 !Prev->isOneOf(TT_RequiresClause, TT_LeadingJavaAnnotation,
1569 TT_BinaryOperator))) {
1570 Line.MightBeFunctionDecl =
true;
1571 Tok->MightBeFunctionDeclParen =
true;
1576 if (Style.isTableGen())
1577 Tok->setType(TT_TableGenListOpener);
1583 if (
Tok->is(TT_RequiresExpressionLBrace))
1585 }
else if (Style.isTextProto()) {
1586 if (Prev && Prev->isNot(TT_DictLiteral))
1587 Prev->setType(TT_SelectorName);
1589 Scopes.push_back(getScopeType(*
Tok));
1595 Tok->setType(TT_TemplateOpener);
1601 if (Style.isTextProto() ||
1602 (Style.Language == FormatStyle::LK_Proto && Prev &&
1603 Prev->isOneOf(TT_SelectorName, TT_DictLiteral))) {
1604 Tok->setType(TT_DictLiteral);
1605 if (Prev && Prev->isNot(TT_DictLiteral))
1606 Prev->setType(TT_SelectorName);
1608 if (Style.isTableGen())
1609 Tok->setType(TT_TemplateOpener);
1611 Tok->setType(TT_BinaryOperator);
1612 NonTemplateLess.insert(
Tok);
1622 if (!Scopes.empty())
1629 if (!Style.isTextProto() &&
Tok->is(TT_Unknown))
1630 Tok->setType(TT_BinaryOperator);
1631 if (Prev && Prev->is(TT_TemplateCloser))
1632 Tok->SpacesRequiredBefore = 1;
1634 case tok::kw_operator:
1635 if (Style.isProto())
1638 if (IsCpp && CurrentToken) {
1639 const auto *Info = CurrentToken->Tok.getIdentifierInfo();
1641 if (Info && !(CurrentToken->isPlacementOperator() ||
1642 CurrentToken->is(tok::kw_co_await) ||
1643 Info->isCPlusPlusOperatorKeyword())) {
1645 if (CurrentToken->startsSequence(tok::kw_decltype, tok::l_paren,
1646 tok::kw_auto, tok::r_paren)) {
1648 LParen = CurrentToken->Next->Next->Next->Next;
1651 for (LParen = CurrentToken->Next;
1652 LParen && LParen->isNot(tok::l_paren); LParen = LParen->Next) {
1653 if (LParen->isPointerOrReference())
1654 LParen->setFinalizedType(TT_PointerOrReference);
1657 if (LParen && LParen->is(tok::l_paren)) {
1658 if (!Contexts.back().IsExpression) {
1659 Tok->setFinalizedType(TT_FunctionDeclarationName);
1660 LParen->setFinalizedType(TT_FunctionDeclarationLParen);
1666 while (CurrentToken &&
1667 !CurrentToken->isOneOf(tok::l_paren, tok::semi, tok::r_paren)) {
1668 if (CurrentToken->isOneOf(tok::star, tok::amp))
1669 CurrentToken->setType(TT_PointerOrReference);
1670 auto Next = CurrentToken->getNextNonComment();
1673 if (
Next->is(tok::less))
1679 auto Previous = CurrentToken->getPreviousNonComment();
1681 if (CurrentToken->is(tok::comma) &&
Previous->isNot(tok::kw_operator))
1683 if (
Previous->isOneOf(TT_BinaryOperator, TT_UnaryOperator, tok::comma,
1685 Previous->isPointerOrReference() ||
1687 Previous->TokenText.starts_with(
"\"\"")) {
1688 Previous->setType(TT_OverloadedOperator);
1689 if (CurrentToken->isOneOf(tok::less, tok::greater))
1693 if (CurrentToken && CurrentToken->is(tok::l_paren))
1694 CurrentToken->setType(TT_OverloadedOperatorLParen);
1695 if (CurrentToken && CurrentToken->Previous->is(TT_BinaryOperator))
1696 CurrentToken->Previous->setType(TT_OverloadedOperator);
1699 if (Style.isJavaScript() &&
Next &&
1700 Next->isOneOf(tok::semi, tok::comma, tok::colon, tok::r_paren,
1701 tok::r_brace, tok::r_square)) {
1706 Tok->setType(TT_JsTypeOptionalQuestion);
1711 if (Line.MustBeDeclaration && !Contexts.back().IsExpression &&
1712 Style.isJavaScript()) {
1715 if (Style.isCSharp()) {
1718 if (
Next && (
Next->isOneOf(tok::r_paren, tok::greater) ||
1719 Next->startsSequence(tok::identifier, tok::semi) ||
1720 Next->startsSequence(tok::identifier, tok::equal))) {
1721 Tok->setType(TT_CSharpNullable);
1730 if (!Contexts.back().IsExpression && Line.MustBeDeclaration &&
1731 (!
Next || !
Next->isOneOf(tok::identifier, tok::string_literal) ||
1732 !
Next->Next || !
Next->Next->isOneOf(tok::colon, tok::question))) {
1733 Tok->setType(TT_CSharpNullable);
1739 case tok::kw_template:
1740 parseTemplateDeclaration();
1743 switch (Contexts.back().ContextType) {
1744 case Context::CtorInitializer:
1745 Tok->setType(TT_CtorInitializerComma);
1747 case Context::InheritanceList:
1748 Tok->setType(TT_InheritanceComma);
1750 case Context::VerilogInstancePortList:
1751 Tok->setType(TT_VerilogInstancePortComma);
1754 if (Style.isVerilog() && Contexts.size() == 1 &&
1755 Line.startsWith(Keywords.kw_assign)) {
1756 Tok->setFinalizedType(TT_VerilogAssignComma);
1757 }
else if (Contexts.back().FirstStartOfName &&
1758 (Contexts.size() == 1 || startsWithInitStatement(Line))) {
1759 Contexts.back().FirstStartOfName->PartOfMultiVariableDeclStmt =
true;
1760 Line.IsMultiVariableDeclStmt =
true;
1764 if (Contexts.back().ContextType == Context::ForEachMacro)
1765 Contexts.back().IsExpression =
true;
1767 case tok::kw_default:
1769 if (Style.isVerilog() && Keywords.isVerilogEndOfLabel(*
Tok) &&
1770 (Line.Level > 1 || (!Line.InPPDirective && Line.Level > 0))) {
1774 case tok::identifier:
1775 if (
Tok->isOneOf(Keywords.kw___has_include,
1776 Keywords.kw___has_include_next)) {
1780 if (
Next &&
Next->is(tok::l_paren) && Prev &&
1781 Prev->isOneOf(tok::kw___cdecl, tok::kw___stdcall,
1782 tok::kw___fastcall, tok::kw___thiscall,
1783 tok::kw___regcall, tok::kw___vectorcall)) {
1784 Tok->setFinalizedType(TT_FunctionDeclarationName);
1785 Next->setFinalizedType(TT_FunctionDeclarationLParen);
1787 }
else if (Style.isCSharp()) {
1788 if (
Tok->is(Keywords.kw_where) &&
Next &&
Next->isNot(tok::l_paren)) {
1789 Tok->setType(TT_CSharpGenericTypeConstraint);
1790 parseCSharpGenericTypeConstraint();
1792 Line.IsContinuation =
true;
1794 }
else if (Style.isTableGen()) {
1795 if (
Tok->is(Keywords.kw_assert)) {
1796 if (!parseTableGenValue())
1798 }
else if (
Tok->isOneOf(Keywords.kw_def, Keywords.kw_defm) &&
1799 (!
Next || !
Next->isOneOf(tok::colon, tok::l_brace))) {
1801 if (!parseTableGenValue(
true))
1805 if (Style.AllowBreakBeforeQtProperty &&
1806 Contexts.back().ContextType == Context::QtProperty &&
1807 Tok->isQtProperty()) {
1808 Tok->setFinalizedType(TT_QtProperty);
1812 if (
Tok->isNot(TT_LambdaArrow) && Prev && Prev->is(tok::kw_noexcept))
1813 Tok->setType(TT_TrailingReturnArrow);
1817 if (Style.isTableGen() && !parseTableGenValue())
1826 void parseCSharpGenericTypeConstraint() {
1827 int OpenAngleBracketsCount = 0;
1828 while (CurrentToken) {
1829 if (CurrentToken->is(tok::less)) {
1831 CurrentToken->setType(TT_TemplateOpener);
1832 ++OpenAngleBracketsCount;
1834 }
else if (CurrentToken->is(tok::greater)) {
1835 CurrentToken->setType(TT_TemplateCloser);
1836 --OpenAngleBracketsCount;
1838 }
else if (CurrentToken->is(tok::comma) && OpenAngleBracketsCount == 0) {
1841 CurrentToken->setType(TT_CSharpGenericTypeConstraintComma);
1843 }
else if (CurrentToken->is(Keywords.kw_where)) {
1844 CurrentToken->setType(TT_CSharpGenericTypeConstraint);
1846 }
else if (CurrentToken->is(tok::colon)) {
1847 CurrentToken->setType(TT_CSharpGenericTypeConstraintColon);
1855 void parseIncludeDirective() {
1856 if (CurrentToken && CurrentToken->is(tok::less)) {
1858 while (CurrentToken) {
1861 if (CurrentToken->isNot(tok::comment) &&
1862 !CurrentToken->TokenText.starts_with(
"//")) {
1863 CurrentToken->setType(TT_ImplicitStringLiteral);
1870 void parseWarningOrError() {
1875 while (CurrentToken) {
1876 CurrentToken->setType(TT_ImplicitStringLiteral);
1881 void parsePragma() {
1884 CurrentToken->isOneOf(Keywords.kw_mark, Keywords.kw_option,
1885 Keywords.kw_region)) {
1886 bool IsMarkOrRegion =
1887 CurrentToken->isOneOf(Keywords.kw_mark, Keywords.kw_region);
1890 while (CurrentToken) {
1891 if (IsMarkOrRegion || CurrentToken->Previous->is(TT_BinaryOperator))
1892 CurrentToken->setType(TT_ImplicitStringLiteral);
1898 void parseHasInclude() {
1899 if (!CurrentToken || CurrentToken->isNot(tok::l_paren))
1902 parseIncludeDirective();
1906 LineType parsePreprocessorDirective() {
1907 bool IsFirstToken = CurrentToken->IsFirst;
1913 if (Style.isJavaScript() && IsFirstToken) {
1917 while (CurrentToken) {
1919 CurrentToken->setType(TT_ImplicitStringLiteral);
1925 if (CurrentToken->is(tok::numeric_constant)) {
1926 CurrentToken->SpacesRequiredBefore = 1;
1931 if (!CurrentToken->Tok.getIdentifierInfo())
1935 if (Style.isVerilog() && !Keywords.isVerilogPPDirective(*CurrentToken))
1937 switch (CurrentToken->Tok.getIdentifierInfo()->getPPKeywordID()) {
1938 case tok::pp_include:
1939 case tok::pp_include_next:
1940 case tok::pp_import:
1942 parseIncludeDirective();
1946 case tok::pp_warning:
1947 parseWarningOrError();
1949 case tok::pp_pragma:
1954 Contexts.back().IsExpression =
true;
1957 CurrentToken->SpacesRequiredBefore = 1;
1963 while (CurrentToken) {
1966 if (
Tok->is(tok::l_paren)) {
1968 }
else if (
Tok->isOneOf(Keywords.kw___has_include,
1969 Keywords.kw___has_include_next)) {
1980 NonTemplateLess.clear();
1981 if (!Line.InMacroBody && CurrentToken->is(tok::hash)) {
1985 auto Type = parsePreprocessorDirective();
1993 IdentifierInfo *Info = CurrentToken->Tok.getIdentifierInfo();
1994 if ((Style.isJava() && CurrentToken->is(Keywords.kw_package)) ||
1995 (!Style.isVerilog() && Info &&
1996 Info->getPPKeywordID() == tok::pp_import && CurrentToken->Next &&
1997 CurrentToken->Next->isOneOf(tok::string_literal, tok::identifier,
2000 parseIncludeDirective();
2006 if (CurrentToken->is(tok::less) && Line.Last->is(tok::greater)) {
2007 parseIncludeDirective();
2013 if (Style.Language == FormatStyle::LK_Proto && Line.Level == 0 &&
2014 CurrentToken->isOneOf(Keywords.kw_option, Keywords.kw_package)) {
2016 if (CurrentToken && CurrentToken->is(tok::identifier)) {
2017 while (CurrentToken)
2023 bool KeywordVirtualFound =
false;
2024 bool ImportStatement =
false;
2027 if (Style.isJavaScript() && CurrentToken->is(Keywords.kw_import))
2028 ImportStatement =
true;
2030 while (CurrentToken) {
2031 if (CurrentToken->is(tok::kw_virtual))
2032 KeywordVirtualFound =
true;
2033 if (Style.isJavaScript()) {
2040 if (Line.First->is(tok::kw_export) &&
2041 CurrentToken->is(Keywords.kw_from) && CurrentToken->Next &&
2042 CurrentToken->Next->isStringLiteral()) {
2043 ImportStatement =
true;
2045 if (isClosureImportStatement(*CurrentToken))
2046 ImportStatement =
true;
2048 if (!consumeToken())
2056 if (KeywordVirtualFound)
2058 if (ImportStatement)
2061 if (Line.startsWith(TT_ObjCMethodSpecifier)) {
2062 if (Contexts.back().FirstObjCSelectorName) {
2063 Contexts.back().FirstObjCSelectorName->LongestObjCSelectorName =
2064 Contexts.back().LongestObjCSelectorName;
2069 for (
const auto &ctx : Contexts)
2070 if (ctx.ContextType == Context::StructArrayInitializer)
2080 return Tok.TokenText ==
"goog" &&
Tok.Next &&
Tok.Next->is(tok::period) &&
2082 (
Tok.Next->Next->TokenText ==
"module" ||
2083 Tok.Next->Next->TokenText ==
"provide" ||
2084 Tok.Next->Next->TokenText ==
"require" ||
2085 Tok.Next->Next->TokenText ==
"requireType" ||
2086 Tok.Next->Next->TokenText ==
"forwardDeclare") &&
2087 Tok.Next->Next->Next &&
Tok.Next->Next->Next->is(tok::l_paren);
2090 void resetTokenMetadata() {
2096 if (!CurrentToken->isTypeFinalized() &&
2097 !CurrentToken->isOneOf(
2098 TT_LambdaLSquare, TT_LambdaLBrace, TT_AttributeMacro, TT_IfMacro,
2099 TT_ForEachMacro, TT_TypenameMacro, TT_FunctionLBrace,
2100 TT_ImplicitStringLiteral, TT_InlineASMBrace, TT_FatArrow,
2101 TT_LambdaArrow, TT_NamespaceMacro, TT_OverloadedOperator,
2102 TT_RegexLiteral, TT_TemplateString, TT_ObjCStringLiteral,
2103 TT_UntouchableMacroFunc, TT_StatementAttributeLikeMacro,
2104 TT_FunctionLikeOrFreestandingMacro, TT_ClassLBrace, TT_EnumLBrace,
2105 TT_RecordLBrace, TT_StructLBrace, TT_UnionLBrace, TT_RequiresClause,
2106 TT_RequiresClauseInARequiresExpression, TT_RequiresExpression,
2107 TT_RequiresExpressionLParen, TT_RequiresExpressionLBrace,
2108 TT_CompoundRequirementLBrace, TT_BracedListLBrace,
2109 TT_FunctionLikeMacro)) {
2110 CurrentToken->setType(TT_Unknown);
2112 CurrentToken->Role.reset();
2113 CurrentToken->MatchingParen =
nullptr;
2114 CurrentToken->FakeLParens.clear();
2115 CurrentToken->FakeRParens = 0;
2122 CurrentToken->NestingLevel = Contexts.size() - 1;
2123 CurrentToken->BindingStrength = Contexts.back().BindingStrength;
2124 modifyContext(*CurrentToken);
2125 determineTokenType(*CurrentToken);
2126 CurrentToken = CurrentToken->Next;
2128 resetTokenMetadata();
2136 : ContextKind(ContextKind), BindingStrength(BindingStrength),
2137 IsExpression(IsExpression) {}
2140 unsigned BindingStrength;
2142 unsigned LongestObjCSelectorName = 0;
2143 bool ColonIsForRangeExpr =
false;
2144 bool ColonIsDictLiteral =
false;
2145 bool ColonIsObjCMethodExpr =
false;
2148 bool CanBeExpression =
true;
2149 bool CaretFound =
false;
2150 bool InCpp11AttributeSpecifier =
false;
2151 bool InCSharpAttributeSpecifier =
false;
2152 bool VerilogAssignmentFound =
false;
2155 bool VerilogMayBeConcatenation =
false;
2156 bool IsTableGenDAGArgList =
false;
2157 bool IsTableGenBangOpe =
false;
2158 bool IsTableGenCondOpe =
false;
2171 StructArrayInitializer,
2175 C11GenericSelection,
2178 VerilogInstancePortList,
2179 } ContextType = Unknown;
2184 struct ScopedContextCreator {
2185 AnnotatingParser &P;
2187 ScopedContextCreator(AnnotatingParser &P,
tok::TokenKind ContextKind,
2190 P.Contexts.push_back(Context(ContextKind,
2191 P.Contexts.back().BindingStrength + Increase,
2192 P.Contexts.back().IsExpression));
2195 ~ScopedContextCreator() {
2196 if (P.Style.AlignArrayOfStructures != FormatStyle::AIAS_None) {
2197 if (P.Contexts.back().ContextType == Context::StructArrayInitializer) {
2198 P.Contexts.pop_back();
2199 P.Contexts.back().ContextType = Context::StructArrayInitializer;
2203 P.Contexts.pop_back();
2208 auto AssignmentStartsExpression = [&]() {
2212 if (Line.First->isOneOf(tok::kw_using, tok::kw_return))
2214 if (Line.First->is(tok::kw_template)) {
2215 assert(Current.Previous);
2216 if (Current.Previous->is(tok::kw_operator)) {
2224 if (
Tok->isNot(TT_TemplateOpener)) {
2231 if (Contexts.back().ContextKind == tok::less) {
2232 assert(Current.Previous->Previous);
2233 return !Current.Previous->Previous->isOneOf(tok::kw_typename,
2237 Tok =
Tok->MatchingParen;
2240 Tok =
Tok->getNextNonComment();
2244 if (
Tok->isOneOf(tok::kw_class, tok::kw_enum, tok::kw_struct,
2254 if (Style.isJavaScript() &&
2255 (Line.startsWith(Keywords.kw_type, tok::identifier) ||
2256 Line.startsWith(tok::kw_export, Keywords.kw_type,
2257 tok::identifier))) {
2261 return !Current.Previous || Current.Previous->isNot(tok::kw_operator);
2264 if (AssignmentStartsExpression()) {
2265 Contexts.back().IsExpression =
true;
2266 if (!Line.startsWith(TT_UnaryOperator)) {
2269 !
Previous->Previous->isOneOf(tok::comma, tok::semi);
2271 if (
Previous->isOneOf(tok::r_square, tok::r_paren, tok::greater)) {
2278 if (
Previous->isOneOf(TT_BinaryOperator, TT_UnaryOperator) &&
2280 Previous->Previous->isNot(tok::equal)) {
2281 Previous->setType(TT_PointerOrReference);
2285 }
else if (Current.is(tok::lessless) &&
2286 (!Current.Previous ||
2287 Current.Previous->isNot(tok::kw_operator))) {
2288 Contexts.back().IsExpression =
true;
2289 }
else if (Current.isOneOf(tok::kw_return, tok::kw_throw)) {
2290 Contexts.back().IsExpression =
true;
2291 }
else if (Current.is(TT_TrailingReturnArrow)) {
2292 Contexts.back().IsExpression =
false;
2293 }
else if (Current.isOneOf(TT_LambdaArrow, Keywords.kw_assert)) {
2294 Contexts.back().IsExpression = Style.isJava();
2295 }
else if (Current.Previous &&
2296 Current.Previous->is(TT_CtorInitializerColon)) {
2297 Contexts.back().IsExpression =
true;
2298 Contexts.back().ContextType = Context::CtorInitializer;
2299 }
else if (Current.Previous && Current.Previous->is(TT_InheritanceColon)) {
2300 Contexts.back().ContextType = Context::InheritanceList;
2301 }
else if (Current.isOneOf(tok::r_paren, tok::greater, tok::comma)) {
2305 Previous->setType(TT_PointerOrReference);
2307 if (Line.MustBeDeclaration &&
2308 Contexts.front().ContextType != Context::CtorInitializer) {
2309 Contexts.back().IsExpression =
false;
2311 }
else if (Current.is(tok::kw_new)) {
2312 Contexts.back().CanBeExpression =
false;
2313 }
else if (Current.is(tok::semi) ||
2314 (Current.is(tok::exclaim) && Current.Previous &&
2315 Current.Previous->isNot(tok::kw_operator))) {
2319 Contexts.back().IsExpression =
true;
2327 if (Current->is(tok::l_paren))
2329 if (Current->is(tok::r_paren))
2333 Current = Current->Next;
2338 static bool isDeductionGuide(
FormatToken &Current) {
2340 if (Current.Previous && Current.Previous->is(tok::r_paren) &&
2341 Current.startsSequence(tok::arrow, tok::identifier, tok::less)) {
2345 while (TemplateCloser) {
2347 if (TemplateCloser->is(tok::l_paren)) {
2349 TemplateCloser = untilMatchingParen(TemplateCloser);
2350 if (!TemplateCloser)
2353 if (TemplateCloser->is(tok::less))
2355 if (TemplateCloser->is(tok::greater))
2359 TemplateCloser = TemplateCloser->Next;
2363 if (TemplateCloser && TemplateCloser->Next &&
2364 TemplateCloser->Next->is(tok::semi) &&
2365 Current.Previous->MatchingParen) {
2369 Current.Previous->MatchingParen->Previous;
2371 return LeadingIdentifier &&
2372 LeadingIdentifier->TokenText == Current.Next->TokenText;
2379 if (Current.isNot(TT_Unknown)) {
2384 if ((Style.isJavaScript() || Style.isCSharp()) &&
2385 Current.is(tok::exclaim)) {
2386 if (Current.Previous) {
2388 Style.isJavaScript()
2389 ? Keywords.isJavaScriptIdentifier(
2390 *Current.Previous,
true)
2391 : Current.Previous->is(tok::identifier);
2393 Current.Previous->isOneOf(
2394 tok::kw_default, tok::kw_namespace, tok::r_paren, tok::r_square,
2395 tok::r_brace, tok::kw_false, tok::kw_true, Keywords.kw_type,
2396 Keywords.kw_get, Keywords.kw_init, Keywords.kw_set) ||
2397 Current.Previous->Tok.isLiteral()) {
2398 Current.setType(TT_NonNullAssertion);
2403 Current.Next->isOneOf(TT_BinaryOperator, Keywords.kw_as)) {
2404 Current.setType(TT_NonNullAssertion);
2412 if ((Style.isJavaScript() || Style.isJava()) &&
2413 Current.is(Keywords.kw_instanceof)) {
2414 Current.setType(TT_BinaryOperator);
2415 }
else if (isStartOfName(Current) &&
2416 (!Line.MightBeFunctionDecl || Current.NestingLevel != 0)) {
2417 Contexts.back().FirstStartOfName = &Current;
2418 Current.setType(TT_StartOfName);
2419 }
else if (Current.is(tok::semi)) {
2423 Contexts.back().FirstStartOfName =
nullptr;
2424 }
else if (Current.isOneOf(tok::kw_auto, tok::kw___auto_type)) {
2426 }
else if (Current.is(tok::arrow) && Style.isJava()) {
2427 Current.setType(TT_LambdaArrow);
2428 }
else if (Current.is(tok::arrow) && Style.isVerilog()) {
2430 Current.setType(TT_BinaryOperator);
2431 }
else if (Current.is(tok::arrow) && AutoFound &&
2432 Line.MightBeFunctionDecl && Current.NestingLevel == 0 &&
2433 !Current.Previous->isOneOf(tok::kw_operator, tok::identifier)) {
2435 Current.setType(TT_TrailingReturnArrow);
2436 }
else if (Current.is(tok::arrow) && Current.Previous &&
2437 Current.Previous->is(tok::r_brace) &&
2441 Current.setType(TT_TrailingReturnArrow);
2442 }
else if (isDeductionGuide(Current)) {
2444 Current.setType(TT_TrailingReturnArrow);
2445 }
else if (Current.isPointerOrReference()) {
2446 Current.setType(determineStarAmpUsage(
2448 Contexts.back().CanBeExpression && Contexts.back().IsExpression,
2449 Contexts.back().ContextType == Context::TemplateArgument));
2450 }
else if (Current.isOneOf(tok::minus, tok::plus, tok::caret) ||
2451 (Style.isVerilog() && Current.is(tok::pipe))) {
2452 Current.setType(determinePlusMinusCaretUsage(Current));
2453 if (Current.is(TT_UnaryOperator) && Current.is(tok::caret))
2454 Contexts.back().CaretFound =
true;
2455 }
else if (Current.isOneOf(tok::minusminus, tok::plusplus)) {
2456 Current.setType(determineIncrementUsage(Current));
2457 }
else if (Current.isOneOf(tok::exclaim, tok::tilde)) {
2458 Current.setType(TT_UnaryOperator);
2459 }
else if (Current.is(tok::question)) {
2460 if (Style.isJavaScript() && Line.MustBeDeclaration &&
2461 !Contexts.back().IsExpression) {
2464 Current.setType(TT_JsTypeOptionalQuestion);
2465 }
else if (Style.isTableGen()) {
2467 Current.setType(TT_Unknown);
2469 Current.setType(TT_ConditionalExpr);
2471 }
else if (Current.isBinaryOperator() &&
2472 (!Current.Previous || Current.Previous->isNot(tok::l_square)) &&
2473 (Current.isNot(tok::greater) && !Style.isTextProto())) {
2474 if (Style.isVerilog()) {
2475 if (Current.is(tok::lessequal) && Contexts.size() == 1 &&
2476 !Contexts.back().VerilogAssignmentFound) {
2480 Current.setFinalizedType(TT_BinaryOperator);
2483 Contexts.back().VerilogAssignmentFound =
true;
2485 Current.setType(TT_BinaryOperator);
2486 }
else if (Current.is(tok::comment)) {
2487 if (Current.TokenText.starts_with(
"/*")) {
2488 if (Current.TokenText.ends_with(
"*/")) {
2489 Current.setType(TT_BlockComment);
2493 Current.Tok.setKind(tok::unknown);
2496 Current.setType(TT_LineComment);
2498 }
else if (Current.is(tok::string_literal)) {
2499 if (Style.isVerilog() && Contexts.back().VerilogMayBeConcatenation &&
2500 Current.getPreviousNonComment() &&
2501 Current.getPreviousNonComment()->isOneOf(tok::comma, tok::l_brace) &&
2502 Current.getNextNonComment() &&
2503 Current.getNextNonComment()->isOneOf(tok::comma, tok::r_brace)) {
2504 Current.setType(TT_StringInConcatenation);
2506 }
else if (Current.is(tok::l_paren)) {
2507 if (lParenStartsCppCast(Current))
2508 Current.setType(TT_CppCastLParen);
2509 }
else if (Current.is(tok::r_paren)) {
2510 if (rParenEndsCast(Current))
2511 Current.setType(TT_CastRParen);
2512 if (Current.MatchingParen && Current.Next &&
2513 !Current.Next->isBinaryOperator() &&
2514 !Current.Next->isOneOf(
2515 tok::semi, tok::colon, tok::l_brace, tok::l_paren, tok::comma,
2516 tok::period, tok::arrow, tok::coloncolon, tok::kw_noexcept)) {
2517 if (
FormatToken *AfterParen = Current.MatchingParen->Next;
2518 AfterParen && AfterParen->isNot(tok::caret)) {
2520 if (
FormatToken *BeforeParen = Current.MatchingParen->Previous;
2521 BeforeParen && BeforeParen->is(tok::identifier) &&
2522 BeforeParen->isNot(TT_TypenameMacro) &&
2523 BeforeParen->TokenText == BeforeParen->TokenText.upper() &&
2524 (!BeforeParen->Previous ||
2525 BeforeParen->Previous->ClosesTemplateDeclaration ||
2526 BeforeParen->Previous->ClosesRequiresClause)) {
2527 Current.setType(TT_FunctionAnnotationRParen);
2531 }
else if (Current.is(tok::at) && Current.Next && !Style.isJavaScript() &&
2535 switch (Current.Next->Tok.getObjCKeywordID()) {
2536 case tok::objc_interface:
2537 case tok::objc_implementation:
2538 case tok::objc_protocol:
2539 Current.setType(TT_ObjCDecl);
2541 case tok::objc_property:
2542 Current.setType(TT_ObjCProperty);
2547 }
else if (Current.is(tok::period)) {
2548 FormatToken *PreviousNoComment = Current.getPreviousNonComment();
2549 if (PreviousNoComment &&
2550 PreviousNoComment->isOneOf(tok::comma, tok::l_brace)) {
2551 Current.setType(TT_DesignatedInitializerPeriod);
2552 }
else if (Style.isJava() && Current.Previous &&
2553 Current.Previous->isOneOf(TT_JavaAnnotation,
2554 TT_LeadingJavaAnnotation)) {
2555 Current.setType(Current.Previous->getType());
2557 }
else if (canBeObjCSelectorComponent(Current) &&
2560 Current.Previous && Current.Previous->is(TT_CastRParen) &&
2561 Current.Previous->MatchingParen &&
2562 Current.Previous->MatchingParen->Previous &&
2563 Current.Previous->MatchingParen->Previous->is(
2564 TT_ObjCMethodSpecifier)) {
2568 Current.setType(TT_SelectorName);
2569 }
else if (Current.isOneOf(tok::identifier, tok::kw_const, tok::kw_noexcept,
2570 tok::kw_requires) &&
2572 !Current.Previous->isOneOf(tok::equal, tok::at,
2573 TT_CtorInitializerComma,
2574 TT_CtorInitializerColon) &&
2575 Line.MightBeFunctionDecl && Contexts.size() == 1) {
2578 Current.setType(TT_TrailingAnnotation);
2579 }
else if ((Style.isJava() || Style.isJavaScript()) && Current.Previous) {
2580 if (Current.Previous->is(tok::at) &&
2581 Current.isNot(Keywords.kw_interface)) {
2585 Current.setType(TT_LeadingJavaAnnotation);
2587 Current.setType(TT_JavaAnnotation);
2588 }
else if (Current.Previous->is(tok::period) &&
2589 Current.Previous->isOneOf(TT_JavaAnnotation,
2590 TT_LeadingJavaAnnotation)) {
2591 Current.setType(Current.Previous->getType());
2603 if (Style.isVerilog())
2606 if (!
Tok.Previous ||
Tok.isNot(tok::identifier) ||
Tok.is(TT_ClassHeadName))
2609 if (
Tok.endsSequence(Keywords.kw_final, TT_ClassHeadName))
2612 if ((Style.isJavaScript() || Style.isJava()) &&
Tok.is(Keywords.kw_extends))
2615 if (
const auto *NextNonComment =
Tok.getNextNonComment();
2616 (!NextNonComment && !Line.InMacroBody) ||
2618 (NextNonComment->isPointerOrReference() ||
2619 NextNonComment->isOneOf(TT_ClassHeadName, tok::string_literal) ||
2620 (Line.InPragmaDirective && NextNonComment->is(tok::identifier))))) {
2624 if (
Tok.Previous->isOneOf(TT_LeadingJavaAnnotation, Keywords.kw_instanceof,
2628 if (Style.isJavaScript() &&
Tok.Previous->is(Keywords.kw_in))
2635 if (!Style.isJavaScript())
2636 while (PreviousNotConst && PreviousNotConst->is(tok::kw_const))
2637 PreviousNotConst = PreviousNotConst->getPreviousNonComment();
2639 if (!PreviousNotConst)
2642 if (PreviousNotConst->ClosesRequiresClause)
2645 if (Style.isTableGen()) {
2647 if (Keywords.isTableGenDefinition(*PreviousNotConst))
2650 if (Contexts.back().ContextKind != tok::l_brace)
2654 bool IsPPKeyword = PreviousNotConst->is(tok::identifier) &&
2655 PreviousNotConst->Previous &&
2656 PreviousNotConst->Previous->is(tok::hash);
2658 if (PreviousNotConst->is(TT_TemplateCloser)) {
2659 return PreviousNotConst && PreviousNotConst->MatchingParen &&
2660 PreviousNotConst->MatchingParen->Previous &&
2661 !PreviousNotConst->MatchingParen->Previous->isOneOf(
2662 tok::period, tok::kw_template);
2665 if ((PreviousNotConst->is(tok::r_paren) &&
2666 PreviousNotConst->is(TT_TypeDeclarationParen)) ||
2667 PreviousNotConst->is(TT_AttributeRParen)) {
2676 if (PreviousNotConst->isOneOf(tok::identifier, tok::kw_auto) &&
2677 PreviousNotConst->isNot(TT_StatementAttributeLikeMacro)) {
2682 if (PreviousNotConst->is(TT_PointerOrReference))
2686 if (PreviousNotConst->isTypeName(LangOpts))
2690 if (Style.isJava() && PreviousNotConst->is(tok::r_square))
2694 return Style.isJavaScript() && PreviousNotConst->is(tok::kw_const);
2704 if (LeftOfParens && LeftOfParens->is(TT_TemplateCloser) &&
2705 LeftOfParens->MatchingParen) {
2706 auto *Prev = LeftOfParens->MatchingParen->getPreviousNonComment();
2708 Prev->isOneOf(tok::kw_const_cast, tok::kw_dynamic_cast,
2709 tok::kw_reinterpret_cast, tok::kw_static_cast)) {
2720 assert(
Tok.is(tok::r_paren));
2722 if (!
Tok.MatchingParen || !
Tok.Previous)
2726 if (!IsCpp && !Style.isCSharp() && !Style.isJava())
2729 const auto *LParen =
Tok.MatchingParen;
2730 const auto *BeforeRParen =
Tok.Previous;
2731 const auto *AfterRParen =
Tok.Next;
2734 if (BeforeRParen == LParen || !AfterRParen)
2737 if (LParen->is(TT_OverloadedOperatorLParen))
2740 auto *LeftOfParens = LParen->getPreviousNonComment();
2744 if (LeftOfParens->is(tok::r_paren) &&
2745 LeftOfParens->isNot(TT_CastRParen)) {
2746 if (!LeftOfParens->MatchingParen ||
2747 !LeftOfParens->MatchingParen->Previous) {
2750 LeftOfParens = LeftOfParens->MatchingParen->Previous;
2753 if (LeftOfParens->is(tok::r_square)) {
2756 if (
Tok->isNot(tok::r_square))
2759 Tok =
Tok->getPreviousNonComment();
2760 if (!
Tok ||
Tok->isNot(tok::l_square))
2763 Tok =
Tok->getPreviousNonComment();
2764 if (!
Tok ||
Tok->isNot(tok::kw_delete))
2768 if (
FormatToken *MaybeDelete = MayBeArrayDelete(LeftOfParens))
2769 LeftOfParens = MaybeDelete;
2775 if (LeftOfParens->Tok.getIdentifierInfo() && LeftOfParens->Previous &&
2776 LeftOfParens->Previous->is(tok::kw_operator)) {
2782 if (LeftOfParens->Tok.getIdentifierInfo() &&
2783 !LeftOfParens->isOneOf(Keywords.kw_in, tok::kw_return, tok::kw_case,
2784 tok::kw_delete, tok::kw_throw)) {
2790 if (LeftOfParens->isOneOf(tok::at, tok::r_square, TT_OverloadedOperator,
2791 TT_TemplateCloser, tok::ellipsis)) {
2796 if (AfterRParen->is(tok::question) ||
2797 (AfterRParen->is(tok::ampamp) && !BeforeRParen->isTypeName(LangOpts))) {
2802 if (AfterRParen->is(Keywords.kw_in) && Style.isCSharp())
2807 if (AfterRParen->isOneOf(tok::kw_noexcept, tok::kw_volatile, tok::kw_const,
2808 tok::kw_requires, tok::kw_throw, tok::arrow,
2809 Keywords.kw_override, Keywords.kw_final) ||
2810 isCppAttribute(IsCpp, *AfterRParen)) {
2816 if (Style.isJava() && AfterRParen->is(tok::l_paren))
2820 if (AfterRParen->isOneOf(tok::kw_sizeof, tok::kw_alignof) ||
2821 (AfterRParen->Tok.isLiteral() &&
2822 AfterRParen->isNot(tok::string_literal))) {
2827 if (
Tok.isNot(TT_TemplateCloser))
2829 const auto *
Less =
Tok.MatchingParen;
2832 const auto *BeforeLess =
Less->getPreviousNonComment();
2833 return BeforeLess && BeforeLess->isNot(TT_VariableTemplate);
2837 auto IsQualifiedPointerOrReference = [](
const FormatToken *
T,
2838 const LangOptions &LangOpts) {
2840 assert(!
T->isTypeName(LangOpts) &&
"Should have already been checked");
2844 if (
T->is(TT_AttributeRParen)) {
2846 assert(
T->is(tok::r_paren));
2847 assert(
T->MatchingParen);
2848 assert(
T->MatchingParen->is(tok::l_paren));
2849 assert(
T->MatchingParen->is(TT_AttributeLParen));
2850 if (
const auto *
Tok =
T->MatchingParen->Previous;
2851 Tok &&
Tok->isAttribute()) {
2855 }
else if (
T->is(TT_AttributeSquare)) {
2857 if (
T->MatchingParen &&
T->MatchingParen->Previous) {
2858 T =
T->MatchingParen->Previous;
2861 }
else if (
T->canBePointerOrReferenceQualifier()) {
2867 return T &&
T->is(TT_PointerOrReference);
2870 bool ParensAreType = IsNonVariableTemplate(*BeforeRParen) ||
2871 BeforeRParen->is(TT_TypeDeclarationParen) ||
2872 BeforeRParen->isTypeName(LangOpts) ||
2873 IsQualifiedPointerOrReference(BeforeRParen, LangOpts);
2874 bool ParensCouldEndDecl =
2875 AfterRParen->isOneOf(tok::equal, tok::semi, tok::l_brace, tok::greater);
2876 if (ParensAreType && !ParensCouldEndDecl)
2887 for (
const auto *Token = LParen->Next; Token != &
Tok; Token = Token->Next)
2888 if (Token->is(TT_BinaryOperator))
2893 if (AfterRParen->isOneOf(tok::identifier, tok::kw_this))
2897 if (AfterRParen->is(tok::l_paren)) {
2898 for (
const auto *Prev = BeforeRParen; Prev->is(tok::identifier);) {
2899 Prev = Prev->Previous;
2900 if (Prev->is(tok::coloncolon))
2901 Prev = Prev->Previous;
2907 if (!AfterRParen->Next)
2910 if (AfterRParen->is(tok::l_brace) &&
2918 const bool NextIsAmpOrStar = AfterRParen->isOneOf(tok::amp, tok::star);
2919 if (!(AfterRParen->isUnaryOperator() || NextIsAmpOrStar) ||
2920 AfterRParen->is(tok::plus) ||
2921 !AfterRParen->Next->isOneOf(tok::identifier, tok::numeric_constant)) {
2925 if (NextIsAmpOrStar &&
2926 (AfterRParen->Next->is(tok::numeric_constant) || Line.InPPDirective)) {
2930 if (Line.InPPDirective && AfterRParen->is(tok::minus))
2933 const auto *Prev = BeforeRParen;
2936 if (Prev->is(tok::r_paren)) {
2937 if (Prev->is(TT_CastRParen))
2939 Prev = Prev->MatchingParen;
2942 Prev = Prev->Previous;
2943 if (!Prev || Prev->isNot(tok::r_paren))
2945 Prev = Prev->MatchingParen;
2946 return Prev && Prev->is(TT_FunctionTypeLParen);
2950 for (Prev = BeforeRParen; Prev != LParen; Prev = Prev->Previous)
2951 if (!Prev->isOneOf(tok::kw_const, tok::identifier, tok::coloncolon))
2969 if (PrevToken->isOneOf(
2970 TT_ConditionalExpr, tok::l_paren, tok::comma, tok::colon, tok::semi,
2971 tok::equal, tok::question, tok::l_square, tok::l_brace,
2972 tok::kw_case, tok::kw_co_await, tok::kw_co_return, tok::kw_co_yield,
2973 tok::kw_delete, tok::kw_return, tok::kw_throw)) {
2980 if (PrevToken->is(tok::kw_sizeof))
2984 if (PrevToken->isOneOf(TT_CastRParen, TT_UnaryOperator))
2988 if (PrevToken->is(TT_BinaryOperator))
2996 bool InTemplateArgument) {
2997 if (Style.isJavaScript())
2998 return TT_BinaryOperator;
3001 if (Style.isCSharp() &&
Tok.is(tok::ampamp))
3002 return TT_BinaryOperator;
3004 if (Style.isVerilog()) {
3009 if (
Tok.is(tok::star))
3010 return TT_BinaryOperator;
3011 return determineUnaryOperatorByUsage(
Tok) ? TT_UnaryOperator
3012 : TT_BinaryOperator;
3017 return TT_UnaryOperator;
3018 if (PrevToken->isTypeName(LangOpts))
3019 return TT_PointerOrReference;
3020 if (PrevToken->isPlacementOperator() &&
Tok.is(tok::ampamp))
3021 return TT_BinaryOperator;
3023 auto *NextToken =
Tok.getNextNonComment();
3025 return TT_PointerOrReference;
3026 if (NextToken->is(tok::greater)) {
3027 NextToken->setFinalizedType(TT_TemplateCloser);
3028 return TT_PointerOrReference;
3031 if (InTemplateArgument && NextToken->is(tok::kw_noexcept))
3032 return TT_BinaryOperator;
3034 if (NextToken->isOneOf(tok::arrow, tok::equal, tok::comma, tok::r_paren,
3035 TT_RequiresClause) ||
3036 (NextToken->is(tok::kw_noexcept) && !IsExpression) ||
3037 NextToken->canBePointerOrReferenceQualifier() ||
3038 (NextToken->is(tok::l_brace) && !NextToken->getNextNonComment())) {
3039 return TT_PointerOrReference;
3042 if (PrevToken->is(tok::coloncolon))
3043 return TT_PointerOrReference;
3045 if (PrevToken->is(tok::r_paren) && PrevToken->is(TT_TypeDeclarationParen))
3046 return TT_PointerOrReference;
3048 if (determineUnaryOperatorByUsage(
Tok))
3049 return TT_UnaryOperator;
3051 if (NextToken->is(tok::l_square) && NextToken->isNot(TT_LambdaLSquare))
3052 return TT_PointerOrReference;
3053 if (NextToken->is(tok::kw_operator) && !IsExpression)
3054 return TT_PointerOrReference;
3055 if (NextToken->isOneOf(tok::comma, tok::semi))
3056 return TT_PointerOrReference;
3068 if (PrevToken->is(tok::r_brace) &&
Tok.is(tok::star) &&
3069 !PrevToken->MatchingParen) {
3070 return TT_PointerOrReference;
3073 if (PrevToken->endsSequence(tok::r_square, tok::l_square, tok::kw_delete))
3074 return TT_UnaryOperator;
3076 if (PrevToken->Tok.isLiteral() ||
3077 PrevToken->isOneOf(tok::r_paren, tok::r_square, tok::kw_true,
3078 tok::kw_false, tok::r_brace)) {
3079 return TT_BinaryOperator;
3083 while (NextNonParen && NextNonParen->is(tok::l_paren))
3084 NextNonParen = NextNonParen->getNextNonComment();
3085 if (NextNonParen && (NextNonParen->Tok.isLiteral() ||
3086 NextNonParen->isOneOf(tok::kw_true, tok::kw_false) ||
3087 NextNonParen->isUnaryOperator())) {
3088 return TT_BinaryOperator;
3094 if (InTemplateArgument && NextToken->Tok.isAnyIdentifier())
3095 return TT_BinaryOperator;
3099 if (
Tok.is(tok::ampamp) &&
3100 NextToken->isOneOf(tok::l_paren, tok::star, tok::amp)) {
3101 return TT_BinaryOperator;
3108 if (NextToken->Tok.isAnyIdentifier()) {
3109 auto *NextNextToken = NextToken->getNextNonComment();
3110 if (NextNextToken) {
3111 if (NextNextToken->is(tok::arrow))
3112 return TT_BinaryOperator;
3113 if (NextNextToken->isPointerOrReference() &&
3114 !NextToken->isObjCLifetimeQualifier(Style)) {
3115 NextNextToken->setFinalizedType(TT_BinaryOperator);
3116 return TT_BinaryOperator;
3123 if (IsExpression && !Contexts.back().CaretFound)
3124 return TT_BinaryOperator;
3127 if (!Scopes.empty() && Scopes.back() ==
ST_Class)
3128 return TT_PointerOrReference;
3131 auto IsChainedOperatorAmpOrMember = [](
const FormatToken *token) {
3132 return !token || token->isOneOf(tok::amp, tok::period, tok::arrow,
3133 tok::arrowstar, tok::periodstar);
3138 if (
Tok.is(tok::amp) && PrevToken->Tok.isAnyIdentifier() &&
3139 IsChainedOperatorAmpOrMember(PrevToken->getPreviousNonComment()) &&
3140 NextToken && NextToken->Tok.isAnyIdentifier()) {
3141 if (
auto NextNext = NextToken->getNextNonComment();
3143 (IsChainedOperatorAmpOrMember(NextNext) || NextNext->is(tok::semi))) {
3144 return TT_BinaryOperator;
3150 return TT_BinaryOperator;
3153 return TT_PointerOrReference;
3157 if (determineUnaryOperatorByUsage(
Tok))
3158 return TT_UnaryOperator;
3162 return TT_UnaryOperator;
3164 if (PrevToken->is(tok::at))
3165 return TT_UnaryOperator;
3168 return TT_BinaryOperator;
3174 if (!PrevToken || PrevToken->is(TT_CastRParen))
3175 return TT_UnaryOperator;
3176 if (PrevToken->isOneOf(tok::r_paren, tok::r_square, tok::identifier))
3177 return TT_TrailingUnaryOperator;
3179 return TT_UnaryOperator;
3182 SmallVector<Context, 8> Contexts;
3184 const FormatStyle &Style;
3185 AnnotatedLine &Line;
3189 LangOptions LangOpts;
3190 const AdditionalKeywords &Keywords;
3192 SmallVector<ScopeType> &Scopes;
3198 llvm::SmallPtrSet<FormatToken *, 16> NonTemplateLess;
3200 int TemplateDeclarationDepth;
3208class ExpressionParser {
3210 ExpressionParser(
const FormatStyle &Style,
const AdditionalKeywords &Keywords,
3211 AnnotatedLine &Line)
3212 : Style(Style), Keywords(Keywords), Line(Line), Current(Line.
First) {}
3215 void parse(
int Precedence = 0) {
3218 while (Current && (Current->is(tok::kw_return) ||
3219 (Current->is(tok::colon) &&
3220 Current->isOneOf(TT_ObjCMethodExpr, TT_DictLiteral)))) {
3224 if (!Current || Precedence > PrecedenceArrowAndPeriod)
3229 parseConditionalExpr();
3235 if (Precedence == PrecedenceUnaryOperator) {
3236 parseUnaryOperator();
3253 if (Style.isVerilog() && Precedence ==
prec::Comma) {
3254 VerilogFirstOfType =
3255 verilogGroupDecl(VerilogFirstOfType, LatestOperator);
3259 parse(Precedence + 1);
3261 int CurrentPrecedence = getCurrentPrecedence();
3262 if (Style.BreakBinaryOperations == FormatStyle::BBO_OnePerLine &&
3272 if (Precedence == CurrentPrecedence && Current &&
3273 Current->is(TT_SelectorName)) {
3275 addFakeParenthesis(Start,
prec::Level(Precedence));
3279 if ((Style.isCSharp() || Style.isJavaScript() || Style.isJava()) &&
3283 FormatToken *Prev = Current->getPreviousNonComment();
3284 if (Prev && Prev->is(tok::string_literal) &&
3285 (Prev == Start || Prev->endsSequence(tok::string_literal, tok::plus,
3286 TT_StringInConcatenation))) {
3287 Prev->setType(TT_StringInConcatenation);
3294 (Current->closesScope() &&
3295 (Current->MatchingParen || Current->is(TT_TemplateString))) ||
3296 (CurrentPrecedence != -1 && CurrentPrecedence < Precedence) ||
3305 if (Current->opensScope() ||
3306 Current->isOneOf(TT_RequiresClause,
3307 TT_RequiresClauseInARequiresExpression)) {
3310 while (Current && (!Current->closesScope() || Current->opensScope())) {
3317 if (CurrentPrecedence == Precedence) {
3319 LatestOperator->NextOperator = Current;
3320 LatestOperator = Current;
3324 next(Precedence > 0);
3329 if (Style.isVerilog() && Precedence ==
prec::Comma && VerilogFirstOfType)
3330 addFakeParenthesis(VerilogFirstOfType,
prec::Comma);
3332 if (LatestOperator && (Current || Precedence > 0)) {
3338 Start->Previous->isOneOf(TT_RequiresClause,
3339 TT_RequiresClauseInARequiresExpression))
3341 auto Ret = Current ? Current : Line.Last;
3342 while (!
Ret->ClosesRequiresClause &&
Ret->Previous)
3348 if (Precedence == PrecedenceArrowAndPeriod) {
3352 addFakeParenthesis(Start,
prec::Level(Precedence), End);
3360 int getCurrentPrecedence() {
3362 const FormatToken *NextNonComment = Current->getNextNonComment();
3363 if (Current->is(TT_ConditionalExpr))
3365 if (NextNonComment && Current->is(TT_SelectorName) &&
3366 (NextNonComment->isOneOf(TT_DictLiteral, TT_JsTypeColon) ||
3367 (Style.isProto() && NextNonComment->is(tok::less)))) {
3370 if (Current->is(TT_JsComputedPropertyName))
3372 if (Current->is(TT_LambdaArrow))
3374 if (Current->is(TT_FatArrow))
3376 if (Current->isOneOf(tok::semi, TT_InlineASMColon, TT_SelectorName) ||
3377 (Current->is(tok::comment) && NextNonComment &&
3378 NextNonComment->is(TT_SelectorName))) {
3381 if (Current->is(TT_RangeBasedForLoopColon))
3383 if ((Style.isJava() || Style.isJavaScript()) &&
3384 Current->is(Keywords.kw_instanceof)) {
3387 if (Style.isJavaScript() &&
3388 Current->isOneOf(Keywords.kw_in, Keywords.kw_as)) {
3391 if (Current->isOneOf(TT_BinaryOperator, tok::comma))
3392 return Current->getPrecedence();
3393 if (Current->isOneOf(tok::period, tok::arrow) &&
3394 Current->isNot(TT_TrailingReturnArrow)) {
3395 return PrecedenceArrowAndPeriod;
3397 if ((Style.isJava() || Style.isJavaScript()) &&
3398 Current->isOneOf(Keywords.kw_extends, Keywords.kw_implements,
3399 Keywords.kw_throws)) {
3404 if (Style.isVerilog() && Current->is(tok::colon))
3416 if (Start->MacroParent)
3419 Start->FakeLParens.push_back(Precedence);
3421 Start->StartsBinaryExpression =
true;
3422 if (!End && Current)
3423 End = Current->getPreviousNonComment();
3427 End->EndsBinaryExpression =
true;
3433 void parseUnaryOperator() {
3434 SmallVector<FormatToken *, 2> Tokens;
3435 while (Current && Current->is(TT_UnaryOperator)) {
3436 Tokens.push_back(Current);
3439 parse(PrecedenceArrowAndPeriod);
3446 void parseConditionalExpr() {
3447 while (Current && Current->isTrailingComment())
3451 if (!Current || Current->isNot(tok::question))
3455 if (!Current || Current->isNot(TT_ConditionalExpr))
3462 void next(
bool SkipPastLeadingComments =
true) {
3464 Current = Current->Next;
3466 (Current->NewlinesBefore == 0 || SkipPastLeadingComments) &&
3467 Current->isTrailingComment()) {
3468 Current = Current->Next;
3482 while (Start->startsSequence(tok::l_paren, tok::star)) {
3483 if (!(Start = Start->MatchingParen) ||
3484 !(Start = Start->getNextNonComment())) {
3491 if (
Tok->is(Keywords.kw_assign))
3492 Tok =
Tok->getNextNonComment();
3504 if (
Tok->is(tok::hash)) {
3509 Tok =
Tok->getNextNonComment();
3510 }
else if (
Tok->is(tok::hashhash)) {
3514 Tok =
Tok->getNextNonComment();
3515 }
else if (Keywords.isVerilogQualifier(*
Tok) ||
3516 Keywords.isVerilogIdentifier(*
Tok)) {
3520 while (
Tok &&
Tok->isOneOf(tok::period, tok::coloncolon) &&
3521 (
Tok =
Tok->getNextNonComment())) {
3522 if (Keywords.isVerilogIdentifier(*
Tok))
3523 Tok =
Tok->getNextNonComment();
3527 }
else if (
Tok->is(tok::l_paren)) {
3532 Keywords.kw_highz0, Keywords.kw_highz1, Keywords.kw_large,
3533 Keywords.kw_medium, Keywords.kw_pull0, Keywords.kw_pull1,
3534 Keywords.kw_small, Keywords.kw_strong0, Keywords.kw_strong1,
3535 Keywords.kw_supply0, Keywords.kw_supply1, Keywords.kw_weak0,
3536 Keywords.kw_weak1)) {
3537 Tok->setType(TT_VerilogStrength);
3538 Tok =
Tok->MatchingParen;
3540 Tok->setType(TT_VerilogStrength);
3541 Tok =
Tok->getNextNonComment();
3546 }
else if (
Tok->is(Keywords.kw_verilogHash)) {
3548 if (
Next->is(tok::l_paren))
3551 Tok =
Next->getNextNonComment();
3560 while (
Tok &&
Tok->is(tok::l_square) && (
Tok =
Tok->MatchingParen))
3561 Tok =
Tok->getNextNonComment();
3562 if (
Tok && (
Tok->is(tok::hash) || Keywords.isVerilogIdentifier(*
Tok)))
3571 First->setType(TT_VerilogDimensionedTypeName);
3572 }
else if (
First != Start) {
3580 if (TypedName->is(TT_Unknown))
3581 TypedName->setType(TT_StartOfName);
3583 if (FirstOfType && PreviousComma) {
3584 PreviousComma->setType(TT_VerilogTypeComma);
3585 addFakeParenthesis(FirstOfType,
prec::Comma, PreviousComma->Previous);
3588 FirstOfType = TypedName;
3595 while (Current && Current != FirstOfType) {
3596 if (Current->opensScope()) {
3607 const FormatStyle &Style;
3608 const AdditionalKeywords &Keywords;
3609 const AnnotatedLine &Line;
3619 assert(
Line->First);
3626 Line->First->OriginalColumn) {
3627 const bool PPDirectiveOrImportStmt =
3630 if (PPDirectiveOrImportStmt)
3635 Line->Level = Style.IndentPPDirectives < FormatStyle::PPDIS_BeforeHash &&
3636 PPDirectiveOrImportStmt
3638 : NextNonCommentLine->
Level;
3640 NextNonCommentLine =
Line->First->isNot(tok::r_brace) ?
Line :
nullptr;
3659 Tok =
Tok->getNextNonComment()) {
3661 if (
Tok->is(tok::l_square) &&
Tok->is(TT_AttributeSquare)) {
3662 Tok =
Tok->MatchingParen;
3670 if (
Tok->is(tok::l_paren) &&
Tok->is(TT_Unknown) &&
Tok->MatchingParen) {
3678 if (
Tok->isOneOf(tok::kw_friend, tok::kw_inline, tok::kw_virtual,
3679 tok::kw_constexpr, tok::kw_consteval, tok::kw_explicit)) {
3684 if (
Tok->is(tok::coloncolon)) {
3691 while (
Tok->startsSequence(tok::identifier, tok::coloncolon)) {
3699 if (
Tok->is(tok::tilde)) {
3706 if (
Tok->isNot(tok::identifier) ||
Tok->isNot(TT_Unknown))
3717 assert(
Tok &&
Tok->is(tok::identifier));
3718 const auto *Prev =
Tok->Previous;
3720 if (Prev && Prev->is(tok::tilde))
3721 Prev = Prev->Previous;
3723 if (!Prev || !Prev->endsSequence(tok::coloncolon, tok::identifier))
3726 assert(Prev->Previous);
3727 return Prev->Previous->TokenText ==
Tok->TokenText;
3731 if (!
Line.InMacroBody)
3732 MacroBodyScopes.clear();
3734 auto &ScopeStack =
Line.InMacroBody ? MacroBodyScopes : Scopes;
3735 AnnotatingParser
Parser(Style,
Line, Keywords, ScopeStack);
3738 if (!
Line.Children.empty()) {
3741 for (
auto &Child :
Line.Children) {
3742 if (InRequiresExpression &&
3743 !Child->First->isOneOf(tok::kw_typename, tok::kw_requires,
3744 TT_CompoundRequirementLBrace)) {
3750 if (!ScopeStack.empty())
3751 ScopeStack.pop_back();
3764 ExpressionParser ExprParser(Style, Keywords,
Line);
3770 if (
Tok && ((!ScopeStack.empty() && ScopeStack.back() ==
ST_Class) ||
3772 Tok->setFinalizedType(TT_CtorDtorDeclName);
3773 assert(OpeningParen);
3778 if (
Line.startsWith(TT_ObjCMethodSpecifier))
3780 else if (
Line.startsWith(TT_ObjCDecl))
3782 else if (
Line.startsWith(TT_ObjCProperty))
3786 First->SpacesRequiredBefore = 1;
3787 First->CanBreakBefore =
First->MustBreakBefore;
3796 if (Current.
is(TT_FunctionDeclarationName))
3802 const auto *Prev = Current.getPreviousNonComment();
3805 if (Prev->is(tok::coloncolon))
3806 Prev = Prev->Previous;
3813 if (
const auto *PrevPrev =
Previous.getPreviousNonComment();
3814 PrevPrev && PrevPrev->is(TT_ObjCDecl)) {
3818 auto skipOperatorName =
3821 if (
Next->is(TT_OverloadedOperatorLParen))
3823 if (
Next->is(TT_OverloadedOperator))
3825 if (
Next->isPlacementOperator() ||
Next->is(tok::kw_co_await)) {
3828 Next->Next->startsSequence(tok::l_square, tok::r_square)) {
3833 if (
Next->startsSequence(tok::l_square, tok::r_square)) {
3838 if ((
Next->isTypeName(LangOpts) ||
Next->is(tok::identifier)) &&
3839 Next->Next &&
Next->Next->isPointerOrReference()) {
3844 if (
Next->is(TT_TemplateOpener) &&
Next->MatchingParen) {
3855 const bool IsCpp = LangOpts.CXXOperatorNames || LangOpts.C11;
3858 if (Current.
is(tok::kw_operator)) {
3859 if (
Previous.Tok.getIdentifierInfo() &&
3860 !
Previous.isOneOf(tok::kw_return, tok::kw_co_return)) {
3865 assert(
Previous.MatchingParen->is(tok::l_paren));
3866 assert(
Previous.MatchingParen->is(TT_TypeDeclarationParen));
3875 while (
Next &&
Next->startsSequence(tok::hashhash, tok::identifier))
3878 if (
Next->is(TT_TemplateOpener) &&
Next->MatchingParen) {
3880 }
else if (
Next->is(tok::coloncolon)) {
3884 if (
Next->is(tok::kw_operator)) {
3885 Next = skipOperatorName(
Next->Next);
3888 if (
Next->isNot(tok::identifier))
3890 }
else if (isCppAttribute(IsCpp, *
Next)) {
3894 }
else if (
Next->is(tok::l_paren)) {
3903 if (!
Next ||
Next->isNot(tok::l_paren) || !
Next->MatchingParen)
3905 ClosingParen =
Next->MatchingParen;
3906 assert(ClosingParen->
is(tok::r_paren));
3908 if (
Line.Last->is(tok::l_brace))
3910 if (
Next->Next == ClosingParen)
3913 if (ClosingParen->
Next && ClosingParen->
Next->
is(TT_PointerOrReference))
3926 if (IsCpp &&
Next->Next &&
Next->Next->is(tok::identifier) &&
3927 !
Line.endsWith(tok::semi)) {
3933 if (
Tok->is(TT_TypeDeclarationParen))
3935 if (
Tok->isOneOf(tok::l_paren, TT_TemplateOpener) &&
Tok->MatchingParen) {
3936 Tok =
Tok->MatchingParen;
3939 if (
Tok->is(tok::kw_const) ||
Tok->isTypeName(LangOpts) ||
3940 Tok->isOneOf(TT_PointerOrReference, TT_StartOfName, tok::ellipsis)) {
3943 if (
Tok->isOneOf(tok::l_brace, TT_ObjCMethodExpr) ||
Tok->Tok.isLiteral())
3949bool TokenAnnotator::mustBreakForReturnType(
const AnnotatedLine &
Line)
const {
3950 assert(
Line.MightBeFunctionDecl);
3952 if ((Style.BreakAfterReturnType == FormatStyle::RTBS_TopLevel ||
3953 Style.BreakAfterReturnType == FormatStyle::RTBS_TopLevelDefinitions) &&
3958 switch (Style.BreakAfterReturnType) {
3959 case FormatStyle::RTBS_None:
3960 case FormatStyle::RTBS_Automatic:
3961 case FormatStyle::RTBS_ExceptShortType:
3963 case FormatStyle::RTBS_All:
3964 case FormatStyle::RTBS_TopLevel:
3966 case FormatStyle::RTBS_AllDefinitions:
3967 case FormatStyle::RTBS_TopLevelDefinitions:
3968 return Line.mightBeFunctionDefinition();
3978 Line.Computed =
true;
3986 :
Line.FirstStartColumn +
First->ColumnWidth;
3987 bool AlignArrayOfStructures =
3988 (Style.AlignArrayOfStructures != FormatStyle::AIAS_None &&
3990 if (AlignArrayOfStructures)
3991 calculateArrayInitializerColumnList(
Line);
3993 const auto *FirstNonComment =
Line.getFirstNonComment();
3994 bool SeenName =
false;
3995 bool LineIsFunctionDeclaration =
false;
3999 for (
auto *
Tok = FirstNonComment && FirstNonComment->isNot(tok::kw_using)
4000 ? FirstNonComment->Next
4003 if (
Tok->is(TT_StartOfName))
4005 if (
Tok->Previous->EndsCppAttributeGroup)
4006 AfterLastAttribute =
Tok;
4007 if (
const bool IsCtorOrDtor =
Tok->is(TT_CtorDtorDeclName);
4011 Tok->setFinalizedType(TT_FunctionDeclarationName);
4012 LineIsFunctionDeclaration =
true;
4016 assert(OpeningParen);
4017 if (OpeningParen->is(TT_Unknown))
4018 OpeningParen->setType(TT_FunctionDeclarationLParen);
4025 if ((LineIsFunctionDeclaration ||
4026 (FirstNonComment && FirstNonComment->is(TT_CtorDtorDeclName))) &&
4027 Line.endsWith(tok::semi, tok::r_brace)) {
4028 auto *
Tok =
Line.Last->Previous;
4029 while (
Tok->isNot(tok::r_brace))
4031 if (
auto *LBrace =
Tok->MatchingParen; LBrace && LBrace->is(TT_Unknown)) {
4032 assert(LBrace->is(tok::l_brace));
4035 LBrace->setFinalizedType(TT_FunctionLBrace);
4039 if (SeenName && AfterLastAttribute &&
4042 if (LineIsFunctionDeclaration)
4043 Line.ReturnTypeWrapped =
true;
4046 if (!LineIsFunctionDeclaration) {
4048 for (
const auto *
Tok = FirstNonComment;
Tok;
Tok =
Tok->Next) {
4049 if (
Tok->isNot(tok::kw_operator))
4053 }
while (
Tok &&
Tok->isNot(TT_OverloadedOperatorLParen));
4054 if (!
Tok || !
Tok->MatchingParen)
4056 const auto *LeftParen =
Tok;
4057 for (
Tok =
Tok->Next;
Tok &&
Tok != LeftParen->MatchingParen;
4059 if (
Tok->isNot(tok::identifier))
4062 const bool NextIsBinaryOperator =
4064 Next->Next->is(tok::identifier);
4065 if (!NextIsBinaryOperator)
4067 Next->setType(TT_BinaryOperator);
4071 }
else if (ClosingParen) {
4073 if (
Tok->is(TT_CtorInitializerColon))
4075 if (
Tok->is(tok::arrow)) {
4076 Tok->setType(TT_TrailingReturnArrow);
4079 if (
Tok->isNot(TT_TrailingAnnotation))
4082 if (!
Next ||
Next->isNot(tok::l_paren))
4091 if (
First->is(TT_ElseLBrace)) {
4092 First->CanBreakBefore =
true;
4093 First->MustBreakBefore =
true;
4096 bool InFunctionDecl =
Line.MightBeFunctionDecl;
4097 bool InParameterList =
false;
4098 for (
auto *Current =
First->Next; Current; Current = Current->Next) {
4100 if (Current->is(TT_LineComment)) {
4102 Current->SpacesRequiredBefore =
4103 (Style.Cpp11BracedListStyle && !Style.SpacesInParensOptions.Other)
4106 }
else if (Prev->
is(TT_VerilogMultiLineListLParen)) {
4107 Current->SpacesRequiredBefore = 0;
4109 Current->SpacesRequiredBefore = Style.SpacesBeforeTrailingComments;
4119 if (!Current->HasUnescapedNewline) {
4122 if (
Parameter->isOneOf(tok::comment, tok::r_brace))
4125 if (
Parameter->Previous->isNot(TT_CtorInitializerComma) &&
4133 }
else if (!Current->Finalized && Current->SpacesRequiredBefore == 0 &&
4134 spaceRequiredBefore(
Line, *Current)) {
4135 Current->SpacesRequiredBefore = 1;
4140 Current->MustBreakBefore =
true;
4142 Current->MustBreakBefore =
4143 Current->MustBreakBefore || mustBreakBefore(
Line, *Current);
4144 if (!Current->MustBreakBefore && InFunctionDecl &&
4145 Current->is(TT_FunctionDeclarationName)) {
4146 Current->MustBreakBefore = mustBreakForReturnType(
Line);
4150 Current->CanBreakBefore =
4151 Current->MustBreakBefore || canBreakBefore(
Line, *Current);
4153 if (Current->is(TT_FunctionDeclarationLParen)) {
4154 InParameterList =
true;
4155 }
else if (Current->is(tok::r_paren)) {
4156 const auto *LParen = Current->MatchingParen;
4157 if (LParen && LParen->is(TT_FunctionDeclarationLParen))
4158 InParameterList =
false;
4159 }
else if (InParameterList &&
4160 Current->endsSequence(TT_AttributeMacro,
4161 TT_PointerOrReference)) {
4162 Current->CanBreakBefore =
false;
4165 unsigned ChildSize = 0;
4168 ChildSize = LastOfChild.isTrailingComment() ? Style.ColumnLimit
4171 if (Current->MustBreakBefore || Prev->
Children.size() > 1 ||
4173 Prev->
Children[0]->First->MustBreakBefore) ||
4174 Current->IsMultiline) {
4175 Current->TotalLength = Prev->
TotalLength + Style.ColumnLimit;
4177 Current->TotalLength = Prev->
TotalLength + Current->ColumnWidth +
4178 ChildSize + Current->SpacesRequiredBefore;
4181 if (Current->is(TT_ControlStatementLBrace)) {
4182 if (Style.ColumnLimit > 0 &&
4183 Style.BraceWrapping.AfterControlStatement ==
4184 FormatStyle::BWACS_MultiLine &&
4185 Line.Level * Style.IndentWidth +
Line.Last->TotalLength >
4186 Style.ColumnLimit) {
4187 Current->CanBreakBefore =
true;
4188 Current->MustBreakBefore =
true;
4190 }
else if (Current->is(TT_CtorInitializerColon)) {
4191 InFunctionDecl =
false;
4203 Current->SplitPenalty = splitPenalty(
Line, *Current, InFunctionDecl);
4204 if (Style.Language == FormatStyle::LK_ObjC &&
4205 Current->is(TT_SelectorName) && Current->ParameterIndex > 0) {
4206 if (Current->ParameterIndex == 1)
4207 Current->SplitPenalty += 5 * Current->BindingStrength;
4209 Current->SplitPenalty += 20 * Current->BindingStrength;
4213 calculateUnbreakableTailLengths(
Line);
4215 for (
auto *Current =
First; Current; Current = Current->Next) {
4217 Current->Role->precomputeFormattingInfos(Current);
4218 if (Current->MatchingParen &&
4219 Current->MatchingParen->opensBlockOrBlockTypeList(Style) &&
4224 if (Current->opensBlockOrBlockTypeList(Style))
4228 LLVM_DEBUG({ printDebugInfo(
Line); });
4231void TokenAnnotator::calculateUnbreakableTailLengths(
4238 Current->
isOneOf(tok::comment, tok::string_literal)) {
4248void TokenAnnotator::calculateArrayInitializerColumnList(
4252 auto *CurrentToken =
Line.First;
4253 CurrentToken->ArrayInitializerLineStart =
true;
4255 while (CurrentToken && CurrentToken !=
Line.Last) {
4256 if (CurrentToken->is(tok::l_brace)) {
4257 CurrentToken->IsArrayInitializer =
true;
4258 if (CurrentToken->Next)
4259 CurrentToken->Next->MustBreakBefore =
true;
4261 calculateInitializerColumnList(
Line, CurrentToken->Next, Depth + 1);
4263 CurrentToken = CurrentToken->Next;
4268FormatToken *TokenAnnotator::calculateInitializerColumnList(
4270 while (CurrentToken && CurrentToken !=
Line.Last) {
4271 if (CurrentToken->is(tok::l_brace))
4273 else if (CurrentToken->is(tok::r_brace))
4275 if (Depth == 2 && CurrentToken->isOneOf(tok::l_brace, tok::comma)) {
4276 CurrentToken = CurrentToken->Next;
4279 CurrentToken->StartsColumn =
true;
4280 CurrentToken = CurrentToken->Previous;
4282 CurrentToken = CurrentToken->Next;
4284 return CurrentToken;
4289 bool InFunctionDecl)
const {
4293 if (
Left.is(tok::semi))
4297 if (Style.isJava()) {
4298 if (
Right.isOneOf(Keywords.kw_extends, Keywords.kw_throws))
4300 if (
Right.is(Keywords.kw_implements))
4302 if (
Left.is(tok::comma) &&
Left.NestingLevel == 0)
4304 }
else if (Style.isJavaScript()) {
4305 if (
Right.is(Keywords.kw_function) &&
Left.isNot(tok::comma))
4307 if (
Left.is(TT_JsTypeColon))
4309 if ((
Left.is(TT_TemplateString) &&
Left.TokenText.ends_with(
"${")) ||
4310 (
Right.is(TT_TemplateString) &&
Right.TokenText.starts_with(
"}"))) {
4314 if (
Left.opensScope() &&
Right.closesScope())
4316 }
else if (Style.Language == FormatStyle::LK_Proto) {
4317 if (
Right.is(tok::l_square))
4319 if (
Right.is(tok::period))
4323 if (
Right.is(tok::identifier) &&
Right.Next &&
Right.Next->is(TT_DictLiteral))
4325 if (
Right.is(tok::l_square)) {
4326 if (
Left.is(tok::r_square))
4329 if (
Right.is(TT_LambdaLSquare) &&
Left.is(tok::equal))
4331 if (!
Right.isOneOf(TT_ObjCMethodExpr, TT_LambdaLSquare,
4332 TT_ArrayInitializerLSquare,
4333 TT_DesignatedInitializerLSquare, TT_AttributeSquare)) {
4338 if (
Left.is(tok::coloncolon))
4339 return Style.PenaltyBreakScopeResolution;
4340 if (
Right.isOneOf(TT_StartOfName, TT_FunctionDeclarationName,
4341 tok::kw_operator)) {
4342 if (
Line.startsWith(tok::kw_for) &&
Right.PartOfMultiVariableDeclStmt)
4344 if (
Left.is(TT_StartOfName))
4346 if (InFunctionDecl &&
Right.NestingLevel == 0)
4347 return Style.PenaltyReturnTypeOnItsOwnLine;
4350 if (
Right.is(TT_PointerOrReference))
4352 if (
Right.is(TT_LambdaArrow))
4354 if (
Left.is(tok::equal) &&
Right.is(tok::l_brace))
4356 if (
Left.is(TT_CastRParen))
4358 if (
Left.isOneOf(tok::kw_class, tok::kw_struct, tok::kw_union))
4360 if (
Left.is(tok::comment))
4363 if (
Left.isOneOf(TT_RangeBasedForLoopColon, TT_InheritanceColon,
4364 TT_CtorInitializerColon)) {
4368 if (
Right.isMemberAccess()) {
4389 const auto Penalty = Style.PenaltyBreakBeforeMemberAccess;
4391 ? std::min(Penalty, 35u)
4395 if (
Right.is(TT_TrailingAnnotation) &&
4396 (!
Right.Next ||
Right.Next->isNot(tok::l_paren))) {
4399 if (
Line.startsWith(TT_ObjCMethodSpecifier))
4406 bool is_short_annotation =
Right.TokenText.size() < 10;
4407 return (
Left.is(tok::r_paren) ? 100 : 120) + (is_short_annotation ? 50 : 0);
4411 if (
Line.startsWith(tok::kw_for) &&
Left.is(tok::equal))
4416 if (
Right.is(TT_SelectorName))
4418 if (
Left.is(tok::colon) &&
Left.is(TT_ObjCMethodExpr))
4419 return Line.MightBeFunctionDecl ? 50 : 500;
4425 Left.Previous->isOneOf(tok::identifier, tok::greater)) {
4429 if (
Left.is(tok::l_paren) && Style.PenaltyBreakOpenParenthesis != 0)
4430 return Style.PenaltyBreakOpenParenthesis;
4431 if (
Left.is(tok::l_paren) && InFunctionDecl &&
4432 Style.AlignAfterOpenBracket != FormatStyle::BAS_DontAlign) {
4435 if (
Left.is(tok::l_paren) &&
Left.Previous &&
4436 (
Left.Previous->isOneOf(tok::kw_for, tok::kw__Generic) ||
4437 Left.Previous->isIf())) {
4440 if (
Left.is(tok::equal) && InFunctionDecl)
4442 if (
Right.is(tok::r_brace))
4444 if (
Left.is(TT_TemplateOpener))
4446 if (
Left.opensScope()) {
4450 if (Style.AlignAfterOpenBracket == FormatStyle::BAS_DontAlign &&
4451 (
Left.ParameterCount <= 1 || Style.AllowAllArgumentsOnNextLine)) {
4454 if (
Left.is(tok::l_brace) && !Style.Cpp11BracedListStyle)
4456 return Left.ParameterCount > 1 ? Style.PenaltyBreakBeforeFirstCallParameter
4459 if (
Left.is(TT_JavaAnnotation))
4462 if (
Left.is(TT_UnaryOperator))
4464 if (
Left.isOneOf(tok::plus, tok::comma) &&
Left.Previous &&
4465 Left.Previous->isLabelString() &&
4466 (
Left.NextOperator ||
Left.OperatorIndex != 0)) {
4469 if (
Right.is(tok::plus) &&
Left.isLabelString() &&
4470 (
Right.NextOperator ||
Right.OperatorIndex != 0)) {
4473 if (
Left.is(tok::comma))
4475 if (
Right.is(tok::lessless) &&
Left.isLabelString() &&
4476 (
Right.NextOperator ||
Right.OperatorIndex != 1)) {
4479 if (
Right.is(tok::lessless)) {
4481 if (
Left.isNot(tok::r_paren) ||
Right.OperatorIndex > 0) {
4487 if (
Left.ClosesTemplateDeclaration)
4488 return Style.PenaltyBreakTemplateDeclaration;
4489 if (
Left.ClosesRequiresClause)
4491 if (
Left.is(TT_ConditionalExpr))
4497 return Style.PenaltyBreakAssignment;
4504bool TokenAnnotator::spaceRequiredBeforeParens(
const FormatToken &Right)
const {
4505 if (Style.SpaceBeforeParens == FormatStyle::SBPO_Always)
4507 if (
Right.is(TT_OverloadedOperatorLParen) &&
4508 Style.SpaceBeforeParensOptions.AfterOverloadedOperator) {
4511 if (Style.SpaceBeforeParensOptions.BeforeNonEmptyParentheses &&
4512 Right.ParameterCount > 0) {
4521 if (
Left.is(tok::kw_return) &&
4522 !
Right.isOneOf(tok::semi, tok::r_paren, tok::hashhash)) {
4525 if (
Left.is(tok::kw_throw) &&
Right.is(tok::l_paren) &&
Right.MatchingParen &&
4526 Right.MatchingParen->is(TT_CastRParen)) {
4529 if (
Left.is(Keywords.kw_assert) && Style.isJava())
4532 Left.is(tok::objc_property)) {
4535 if (
Right.is(tok::hashhash))
4536 return Left.is(tok::hash);
4537 if (
Left.isOneOf(tok::hashhash, tok::hash))
4538 return Right.is(tok::hash);
4539 if (Style.SpacesInParens == FormatStyle::SIPO_Custom) {
4540 if (
Left.is(tok::l_paren) &&
Right.is(tok::r_paren))
4541 return Style.SpacesInParensOptions.InEmptyParentheses;
4542 if (Style.SpacesInParensOptions.ExceptDoubleParentheses &&
4543 Left.is(tok::r_paren) &&
Right.is(tok::r_paren)) {
4544 auto *InnerLParen =
Left.MatchingParen;
4545 if (InnerLParen && InnerLParen->Previous ==
Right.MatchingParen) {
4546 InnerLParen->SpacesRequiredBefore = 0;
4551 if (
Left.is(tok::l_paren))
4553 else if (
Right.is(tok::r_paren) &&
Right.MatchingParen)
4554 LeftParen =
Right.MatchingParen;
4555 if (LeftParen && (LeftParen->is(TT_ConditionLParen) ||
4556 (LeftParen->Previous &&
4557 isKeywordWithCondition(*LeftParen->Previous)))) {
4558 return Style.SpacesInParensOptions.InConditionalStatements;
4563 if (
Left.is(tok::kw_auto) &&
Right.isOneOf(TT_LambdaLBrace, TT_FunctionLBrace,
4565 TT_FunctionTypeLParen)) {
4570 if (
Left.is(tok::kw_auto) &&
Right.isOneOf(tok::l_paren, tok::l_brace))
4573 const auto *BeforeLeft =
Left.Previous;
4576 if (
Right.is(tok::l_paren) &&
Left.is(tok::kw_co_await) && BeforeLeft &&
4577 BeforeLeft->is(tok::kw_operator)) {
4581 if (
Left.isOneOf(tok::kw_co_await, tok::kw_co_yield, tok::kw_co_return) &&
4582 !
Right.isOneOf(tok::semi, tok::r_paren)) {
4586 if (
Left.is(tok::l_paren) ||
Right.is(tok::r_paren)) {
4587 return (
Right.is(TT_CastRParen) ||
4588 (
Left.MatchingParen &&
Left.MatchingParen->is(TT_CastRParen)))
4589 ? Style.SpacesInParensOptions.InCStyleCasts
4590 : Style.SpacesInParensOptions.Other;
4592 if (
Right.isOneOf(tok::semi, tok::comma))
4595 bool IsLightweightGeneric =
Right.MatchingParen &&
4596 Right.MatchingParen->Next &&
4597 Right.MatchingParen->Next->is(tok::colon);
4598 return !IsLightweightGeneric && Style.ObjCSpaceBeforeProtocolList;
4600 if (
Right.is(tok::less) &&
Left.is(tok::kw_template))
4601 return Style.SpaceAfterTemplateKeyword;
4602 if (
Left.isOneOf(tok::exclaim, tok::tilde))
4604 if (
Left.is(tok::at) &&
4605 Right.isOneOf(tok::identifier, tok::string_literal, tok::char_constant,
4606 tok::numeric_constant, tok::l_paren, tok::l_brace,
4607 tok::kw_true, tok::kw_false)) {
4610 if (
Left.is(tok::colon))
4611 return Left.isNot(TT_ObjCMethodExpr);
4612 if (
Left.is(tok::coloncolon))
4614 if (
Left.is(tok::less) ||
Right.isOneOf(tok::greater, tok::less)) {
4615 if (Style.isTextProto() ||
4616 (Style.Language == FormatStyle::LK_Proto &&
4617 (
Left.is(TT_DictLiteral) ||
Right.is(TT_DictLiteral)))) {
4619 if (
Left.is(tok::less) &&
Right.is(tok::greater))
4621 return !Style.Cpp11BracedListStyle;
4624 if (
Right.isNot(TT_OverloadedOperatorLParen))
4627 if (
Right.is(tok::ellipsis)) {
4628 return Left.Tok.isLiteral() || (
Left.is(tok::identifier) && BeforeLeft &&
4629 BeforeLeft->is(tok::kw_case));
4631 if (
Left.is(tok::l_square) &&
Right.is(tok::amp))
4632 return Style.SpacesInSquareBrackets;
4633 if (
Right.is(TT_PointerOrReference)) {
4634 if (
Left.is(tok::r_paren) &&
Line.MightBeFunctionDecl) {
4635 if (!
Left.MatchingParen)
4638 Left.MatchingParen->getPreviousNonComment();
4639 if (!TokenBeforeMatchingParen ||
Left.isNot(TT_TypeDeclarationParen))
4645 if ((Style.SpaceAroundPointerQualifiers == FormatStyle::SAPQ_After ||
4646 Style.SpaceAroundPointerQualifiers == FormatStyle::SAPQ_Both) &&
4647 (
Left.is(TT_AttributeRParen) ||
4648 Left.canBePointerOrReferenceQualifier())) {
4651 if (
Left.Tok.isLiteral())
4654 if (
Left.isTypeOrIdentifier(LangOpts) &&
Right.Next &&
Right.Next->Next &&
4655 Right.Next->Next->is(TT_RangeBasedForLoopColon)) {
4656 return getTokenPointerOrReferenceAlignment(Right) !=
4657 FormatStyle::PAS_Left;
4659 return !
Left.isOneOf(TT_PointerOrReference, tok::l_paren) &&
4660 (getTokenPointerOrReferenceAlignment(Right) !=
4661 FormatStyle::PAS_Left ||
4662 (
Line.IsMultiVariableDeclStmt &&
4663 (
Left.NestingLevel == 0 ||
4664 (
Left.NestingLevel == 1 && startsWithInitStatement(
Line)))));
4666 if (
Right.is(TT_FunctionTypeLParen) &&
Left.isNot(tok::l_paren) &&
4667 (
Left.isNot(TT_PointerOrReference) ||
4668 (getTokenPointerOrReferenceAlignment(Left) != FormatStyle::PAS_Right &&
4669 !
Line.IsMultiVariableDeclStmt))) {
4672 if (
Left.is(TT_PointerOrReference)) {
4675 if ((Style.SpaceAroundPointerQualifiers == FormatStyle::SAPQ_Before ||
4676 Style.SpaceAroundPointerQualifiers == FormatStyle::SAPQ_Both) &&
4677 Right.canBePointerOrReferenceQualifier()) {
4681 if (
Right.Tok.isLiteral())
4684 if (
Right.is(TT_BlockComment))
4688 if (
Right.isOneOf(Keywords.kw_override, Keywords.kw_final, tok::kw_noexcept,
4689 TT_RequiresClause) &&
4690 Right.isNot(TT_StartOfName)) {
4697 if (BeforeLeft && BeforeLeft->isTypeOrIdentifier(LangOpts) &&
Right.Next &&
4698 Right.Next->is(TT_RangeBasedForLoopColon)) {
4699 return getTokenPointerOrReferenceAlignment(Left) !=
4700 FormatStyle::PAS_Right;
4702 if (
Right.isOneOf(TT_PointerOrReference, TT_ArraySubscriptLSquare,
4706 if (getTokenPointerOrReferenceAlignment(Left) == FormatStyle::PAS_Right)
4714 if (
Line.IsMultiVariableDeclStmt &&
4715 (
Left.NestingLevel ==
Line.First->NestingLevel ||
4716 ((
Left.NestingLevel ==
Line.First->NestingLevel + 1) &&
4717 startsWithInitStatement(
Line)))) {
4722 if (BeforeLeft->is(tok::coloncolon)) {
4723 if (
Left.isNot(tok::star))
4725 assert(Style.PointerAlignment != FormatStyle::PAS_Right);
4726 if (!
Right.startsSequence(tok::identifier, tok::r_paren))
4729 const auto *LParen =
Right.Next->MatchingParen;
4730 return !LParen || LParen->isNot(TT_FunctionTypeLParen);
4732 return !BeforeLeft->isOneOf(tok::l_paren, tok::l_square);
4735 if (
Left.is(tok::ellipsis) && BeforeLeft &&
4736 BeforeLeft->isPointerOrReference()) {
4737 return Style.PointerAlignment != FormatStyle::PAS_Right;
4740 if (
Right.is(tok::star) &&
Left.is(tok::l_paren))
4742 if (
Left.is(tok::star) &&
Right.isPointerOrReference())
4744 if (
Right.isPointerOrReference()) {
4755 if (
Previous->is(tok::coloncolon)) {
4774 if (
Previous->endsSequence(tok::kw_operator))
4775 return Style.PointerAlignment != FormatStyle::PAS_Left;
4776 if (
Previous->isOneOf(tok::kw_const, tok::kw_volatile)) {
4777 return (Style.PointerAlignment != FormatStyle::PAS_Left) ||
4778 (Style.SpaceAroundPointerQualifiers ==
4779 FormatStyle::SAPQ_After) ||
4780 (Style.SpaceAroundPointerQualifiers == FormatStyle::SAPQ_Both);
4784 if (Style.isCSharp() &&
Left.is(Keywords.kw_is) &&
Right.is(tok::l_square))
4786 const auto SpaceRequiredForArrayInitializerLSquare =
4787 [](
const FormatToken &LSquareTok,
const FormatStyle &Style) {
4788 return Style.SpacesInContainerLiterals ||
4789 (Style.isProto() && !Style.Cpp11BracedListStyle &&
4790 LSquareTok.endsSequence(tok::l_square, tok::colon,
4793 if (
Left.is(tok::l_square)) {
4794 return (
Left.is(TT_ArrayInitializerLSquare) &&
Right.isNot(tok::r_square) &&
4795 SpaceRequiredForArrayInitializerLSquare(Left, Style)) ||
4796 (
Left.isOneOf(TT_ArraySubscriptLSquare, TT_StructuredBindingLSquare,
4797 TT_LambdaLSquare) &&
4798 Style.SpacesInSquareBrackets &&
Right.isNot(tok::r_square));
4800 if (
Right.is(tok::r_square)) {
4801 return Right.MatchingParen &&
4802 ((
Right.MatchingParen->is(TT_ArrayInitializerLSquare) &&
4803 SpaceRequiredForArrayInitializerLSquare(*
Right.MatchingParen,
4805 (Style.SpacesInSquareBrackets &&
4806 Right.MatchingParen->isOneOf(TT_ArraySubscriptLSquare,
4807 TT_StructuredBindingLSquare,
4808 TT_LambdaLSquare)));
4810 if (
Right.is(tok::l_square) &&
4811 !
Right.isOneOf(TT_ObjCMethodExpr, TT_LambdaLSquare,
4812 TT_DesignatedInitializerLSquare,
4813 TT_StructuredBindingLSquare, TT_AttributeSquare) &&
4814 !
Left.isOneOf(tok::numeric_constant, TT_DictLiteral) &&
4815 !(
Left.isNot(tok::r_square) && Style.SpaceBeforeSquareBrackets &&
4816 Right.is(TT_ArraySubscriptLSquare))) {
4820 (
Right.is(tok::r_brace) &&
Right.MatchingParen &&
4822 return !Style.Cpp11BracedListStyle || Style.SpacesInParensOptions.Other;
4824 if (
Left.is(TT_BlockComment)) {
4826 return Style.isJavaScript() || !
Left.TokenText.ends_with(
"=*/");
4831 if (
Left.is(TT_TemplateCloser) &&
Right.is(TT_AttributeSquare))
4834 if (
Right.is(tok::l_paren)) {
4835 if (
Left.is(TT_TemplateCloser) &&
Right.isNot(TT_FunctionTypeLParen))
4836 return spaceRequiredBeforeParens(Right);
4837 if (
Left.isOneOf(TT_RequiresClause,
4838 TT_RequiresClauseInARequiresExpression)) {
4839 return Style.SpaceBeforeParensOptions.AfterRequiresInClause ||
4840 spaceRequiredBeforeParens(Right);
4842 if (
Left.is(TT_RequiresExpression)) {
4843 return Style.SpaceBeforeParensOptions.AfterRequiresInExpression ||
4844 spaceRequiredBeforeParens(Right);
4846 if (
Left.is(TT_AttributeRParen) ||
4847 (
Left.is(tok::r_square) &&
Left.is(TT_AttributeSquare))) {
4850 if (
Left.is(TT_ForEachMacro)) {
4851 return Style.SpaceBeforeParensOptions.AfterForeachMacros ||
4852 spaceRequiredBeforeParens(Right);
4854 if (
Left.is(TT_IfMacro)) {
4855 return Style.SpaceBeforeParensOptions.AfterIfMacros ||
4856 spaceRequiredBeforeParens(Right);
4858 if (Style.SpaceBeforeParens == FormatStyle::SBPO_Custom &&
4859 Left.isPlacementOperator() &&
4860 Right.isNot(TT_OverloadedOperatorLParen) &&
4861 !(
Line.MightBeFunctionDecl &&
Left.is(TT_FunctionDeclarationName))) {
4862 const auto *RParen =
Right.MatchingParen;
4863 return Style.SpaceBeforeParensOptions.AfterPlacementOperator ||
4864 (RParen && RParen->is(TT_CastRParen));
4868 if (
Left.is(tok::semi))
4870 if (
Left.isOneOf(tok::pp_elif, tok::kw_for, tok::kw_while, tok::kw_switch,
4871 tok::kw_case, TT_ForEachMacro, TT_ObjCForIn) ||
4873 Right.is(TT_ConditionLParen)) {
4874 return Style.SpaceBeforeParensOptions.AfterControlStatements ||
4875 spaceRequiredBeforeParens(Right);
4880 if (
Right.is(TT_OverloadedOperatorLParen))
4881 return spaceRequiredBeforeParens(Right);
4883 if (
Line.MightBeFunctionDecl &&
Right.is(TT_FunctionDeclarationLParen)) {
4884 if (spaceRequiredBeforeParens(Right))
4886 const auto &Options = Style.SpaceBeforeParensOptions;
4887 return Line.mightBeFunctionDefinition()
4888 ? Options.AfterFunctionDefinitionName
4889 : Options.AfterFunctionDeclarationName;
4893 Left.MatchingParen &&
Left.MatchingParen->is(TT_LambdaLSquare)) {
4894 return Style.SpaceBeforeParensOptions.AfterFunctionDefinitionName ||
4895 spaceRequiredBeforeParens(Right);
4897 if (!BeforeLeft || !BeforeLeft->isOneOf(tok::period, tok::arrow)) {
4898 if (
Left.isOneOf(tok::kw_try, Keywords.kw___except, tok::kw_catch)) {
4899 return Style.SpaceBeforeParensOptions.AfterControlStatements ||
4900 spaceRequiredBeforeParens(Right);
4902 if (
Left.isPlacementOperator() ||
4903 (
Left.is(tok::r_square) &&
Left.MatchingParen &&
4904 Left.MatchingParen->Previous &&
4905 Left.MatchingParen->Previous->is(tok::kw_delete))) {
4906 return Style.SpaceBeforeParens != FormatStyle::SBPO_Never ||
4907 spaceRequiredBeforeParens(Right);
4912 (
Left.Tok.getIdentifierInfo() ||
Left.is(tok::r_paren))) {
4913 return spaceRequiredBeforeParens(Right);
4917 if (
Left.is(tok::at) &&
Right.isNot(tok::objc_not_keyword))
4919 if (
Right.is(TT_UnaryOperator)) {
4920 return !
Left.isOneOf(tok::l_paren, tok::l_square, tok::at) &&
4921 (
Left.isNot(tok::colon) ||
Left.isNot(TT_ObjCMethodExpr));
4927 if (!Style.isVerilog() &&
4928 (
Left.isOneOf(tok::identifier, tok::greater, tok::r_square,
4930 Left.isTypeName(LangOpts)) &&
4931 Right.is(tok::l_brace) &&
Right.getNextNonComment() &&
4935 if (
Left.is(tok::period) ||
Right.is(tok::period))
4939 if (
Right.is(tok::hash) &&
Left.is(tok::identifier) &&
4940 (
Left.TokenText ==
"L" ||
Left.TokenText ==
"u" ||
4941 Left.TokenText ==
"U" ||
Left.TokenText ==
"u8" ||
4942 Left.TokenText ==
"LR" ||
Left.TokenText ==
"uR" ||
4943 Left.TokenText ==
"UR" ||
Left.TokenText ==
"u8R")) {
4946 if (
Left.is(TT_TemplateCloser) &&
Left.MatchingParen &&
4947 Left.MatchingParen->Previous &&
4948 Left.MatchingParen->Previous->isOneOf(tok::period, tok::coloncolon)) {
4954 if (
Left.is(TT_TemplateCloser) &&
Right.is(tok::l_square))
4956 if (
Left.is(tok::l_brace) &&
Left.endsSequence(TT_DictLiteral, tok::at)) {
4960 if (
Right.is(tok::r_brace) &&
Right.MatchingParen &&
4961 Right.MatchingParen->endsSequence(TT_DictLiteral, tok::at)) {
4965 if (
Right.is(TT_TrailingAnnotation) &&
Right.isOneOf(tok::amp, tok::ampamp) &&
4966 Left.isOneOf(tok::kw_const, tok::kw_volatile) &&
4967 (!
Right.Next ||
Right.Next->is(tok::semi))) {
4971 return getTokenReferenceAlignment(Right) != FormatStyle::PAS_Left;
4984 return Right.hasWhitespaceBefore();
4986 const bool IsVerilog = Style.isVerilog();
4987 assert(!IsVerilog || !IsCpp);
4990 if (Keywords.isWordLike(Right, IsVerilog) &&
4991 Keywords.isWordLike(Left, IsVerilog)) {
4997 if (
Left.is(tok::star) &&
Right.is(tok::comment))
5000 if (
Left.is(tok::l_brace) &&
Right.is(tok::r_brace) &&
5001 Left.Children.empty()) {
5003 return Style.SpaceInEmptyBraces != FormatStyle::SIEB_Never;
5004 if (Style.Cpp11BracedListStyle) {
5005 return Style.SpacesInParens == FormatStyle::SIPO_Custom &&
5006 Style.SpacesInParensOptions.InEmptyParentheses;
5008 return Style.SpaceInEmptyBraces == FormatStyle::SIEB_Always;
5011 const auto *BeforeLeft =
Left.Previous;
5014 if (
Left.is(TT_OverloadedOperator) &&
5015 Right.isOneOf(TT_TemplateOpener, TT_TemplateCloser)) {
5019 if (
Right.is(tok::period) &&
Left.is(tok::numeric_constant))
5023 if (
Left.is(Keywords.kw_import) &&
Right.isOneOf(tok::less, tok::ellipsis))
5026 if (
Left.isOneOf(Keywords.kw_module, Keywords.kw_import) &&
5027 Right.is(TT_ModulePartitionColon)) {
5031 if (
Right.is(TT_AfterPPDirective))
5035 if (
Left.is(tok::identifier) &&
Right.is(TT_ModulePartitionColon))
5038 if (
Left.is(TT_ModulePartitionColon) &&
5039 Right.isOneOf(tok::identifier, tok::kw_private)) {
5042 if (
Left.is(tok::ellipsis) &&
Right.is(tok::identifier) &&
5043 Line.First->is(Keywords.kw_import)) {
5047 if (
Left.isOneOf(TT_AttributeRParen, TT_AttributeMacro) &&
5048 Right.is(tok::coloncolon)) {
5052 if (
Left.is(tok::kw_operator))
5053 return Right.is(tok::coloncolon) || Style.SpaceAfterOperatorKeyword;
5055 !
Left.opensScope() && Style.SpaceBeforeCpp11BracedList) {
5058 if (
Left.is(tok::less) &&
Left.is(TT_OverloadedOperator) &&
5059 Right.is(TT_TemplateOpener)) {
5063 if (
Left.is(tok::identifier) &&
Right.is(tok::numeric_constant))
5064 return Right.TokenText[0] !=
'.';
5066 if (
Left.Tok.getIdentifierInfo() &&
Right.Tok.isLiteral())
5068 }
else if (Style.isProto()) {
5069 if (
Right.is(tok::period) && !(BeforeLeft && BeforeLeft->is(tok::period)) &&
5070 Left.isOneOf(Keywords.kw_optional, Keywords.kw_required,
5071 Keywords.kw_repeated, Keywords.kw_extend)) {
5074 if (
Right.is(tok::l_paren) &&
5075 Left.isOneOf(Keywords.kw_returns, Keywords.kw_option)) {
5078 if (
Right.isOneOf(tok::l_brace, tok::less) &&
Left.is(TT_SelectorName))
5081 if (
Left.is(tok::slash) ||
Right.is(tok::slash))
5083 if (
Left.MatchingParen &&
5084 Left.MatchingParen->is(TT_ProtoExtensionLSquare) &&
5085 Right.isOneOf(tok::l_brace, tok::less)) {
5086 return !Style.Cpp11BracedListStyle;
5089 if (
Left.is(tok::percent))
5093 if (
Left.is(tok::numeric_constant) &&
Right.is(tok::percent))
5094 return Right.hasWhitespaceBefore();
5095 }
else if (Style.isJson()) {
5096 if (
Right.is(tok::colon) &&
Left.is(tok::string_literal))
5097 return Style.SpaceBeforeJsonColon;
5098 }
else if (Style.isCSharp()) {
5104 if (
Left.is(tok::kw_this) &&
Right.is(tok::l_square))
5108 if (
Left.is(tok::kw_new) &&
Right.is(tok::l_paren))
5112 if (
Right.is(tok::l_brace))
5116 if (
Left.is(tok::l_brace) &&
Right.isNot(tok::r_brace))
5119 if (
Left.isNot(tok::l_brace) &&
Right.is(tok::r_brace))
5123 if (
Left.is(TT_FatArrow) ||
Right.is(TT_FatArrow))
5127 if (
Left.is(TT_AttributeColon) ||
Right.is(TT_AttributeColon))
5131 if (
Left.is(TT_TemplateCloser) &&
Right.is(TT_StartOfName))
5135 if (
Left.is(tok::l_square) ||
Right.is(tok::r_square))
5136 return Style.SpacesInSquareBrackets;
5139 if (
Right.is(TT_CSharpNullable))
5143 if (
Right.is(TT_NonNullAssertion))
5147 if (
Left.is(tok::comma) &&
Right.is(tok::comma))
5151 if (
Left.is(Keywords.kw_var) &&
Right.is(tok::l_paren))
5155 if (
Right.is(tok::l_paren)) {
5156 if (
Left.isOneOf(tok::kw_using, Keywords.kw_async, Keywords.kw_when,
5157 Keywords.kw_lock)) {
5158 return Style.SpaceBeforeParensOptions.AfterControlStatements ||
5159 spaceRequiredBeforeParens(Right);
5165 if ((
Left.isAccessSpecifierKeyword() ||
5166 Left.isOneOf(tok::kw_virtual, tok::kw_extern, tok::kw_static,
5167 Keywords.kw_internal, Keywords.kw_abstract,
5168 Keywords.kw_sealed, Keywords.kw_override,
5169 Keywords.kw_async, Keywords.kw_unsafe)) &&
5170 Right.is(tok::l_paren)) {
5173 }
else if (Style.isJavaScript()) {
5174 if (
Left.is(TT_FatArrow))
5177 if (
Right.is(tok::l_paren) &&
Left.is(Keywords.kw_await) && BeforeLeft &&
5178 BeforeLeft->is(tok::kw_for)) {
5181 if (
Left.is(Keywords.kw_async) &&
Right.is(tok::l_paren) &&
5182 Right.MatchingParen) {
5189 if ((
Left.is(TT_TemplateString) &&
Left.TokenText.ends_with(
"${")) ||
5190 (
Right.is(TT_TemplateString) &&
Right.TokenText.starts_with(
"}"))) {
5195 if (Keywords.isJavaScriptIdentifier(Left,
5197 Right.is(TT_TemplateString)) {
5200 if (
Right.is(tok::star) &&
5201 Left.isOneOf(Keywords.kw_function, Keywords.kw_yield)) {
5204 if (
Right.isOneOf(tok::l_brace, tok::l_square) &&
5205 Left.isOneOf(Keywords.kw_function, Keywords.kw_yield,
5206 Keywords.kw_extends, Keywords.kw_implements)) {
5209 if (
Right.is(tok::l_paren)) {
5211 if (
Line.MustBeDeclaration &&
Left.Tok.getIdentifierInfo())
5215 if (BeforeLeft && BeforeLeft->is(tok::period) &&
5216 Left.Tok.getIdentifierInfo()) {
5220 if (
Left.isOneOf(tok::kw_throw, Keywords.kw_await, Keywords.kw_typeof,
5226 if (
Left.endsSequence(tok::kw_const, Keywords.kw_as))
5228 if ((
Left.isOneOf(Keywords.kw_let, Keywords.kw_var, Keywords.kw_in,
5233 (
Left.is(Keywords.kw_of) && BeforeLeft &&
5234 BeforeLeft->isOneOf(tok::identifier, tok::r_square, tok::r_brace))) &&
5235 (!BeforeLeft || BeforeLeft->isNot(tok::period))) {
5238 if (
Left.isOneOf(tok::kw_for, Keywords.kw_as) && BeforeLeft &&
5239 BeforeLeft->is(tok::period) &&
Right.is(tok::l_paren)) {
5242 if (
Left.is(Keywords.kw_as) &&
5243 Right.isOneOf(tok::l_square, tok::l_brace, tok::l_paren)) {
5246 if (
Left.is(tok::kw_default) && BeforeLeft &&
5247 BeforeLeft->is(tok::kw_export)) {
5250 if (
Left.is(Keywords.kw_is) &&
Right.is(tok::l_brace))
5252 if (
Right.isOneOf(TT_JsTypeColon, TT_JsTypeOptionalQuestion))
5254 if (
Left.is(TT_JsTypeOperator) ||
Right.is(TT_JsTypeOperator))
5256 if ((
Left.is(tok::l_brace) ||
Right.is(tok::r_brace)) &&
5257 Line.First->isOneOf(Keywords.kw_import, tok::kw_export)) {
5260 if (
Left.is(tok::ellipsis))
5262 if (
Left.is(TT_TemplateCloser) &&
5263 !
Right.isOneOf(tok::equal, tok::l_brace, tok::comma, tok::l_square,
5264 Keywords.kw_implements, Keywords.kw_extends)) {
5270 if (
Right.is(TT_NonNullAssertion))
5272 if (
Left.is(TT_NonNullAssertion) &&
5273 Right.isOneOf(Keywords.kw_as, Keywords.kw_in)) {
5276 }
else if (Style.isJava()) {
5277 if (
Left.is(TT_CaseLabelArrow) ||
Right.is(TT_CaseLabelArrow))
5279 if (
Left.is(tok::r_square) &&
Right.is(tok::l_brace))
5282 if (
Left.is(tok::l_square) ||
Right.is(tok::r_square))
5283 return Style.SpacesInSquareBrackets;
5285 if (
Left.is(Keywords.kw_synchronized) &&
Right.is(tok::l_paren)) {
5286 return Style.SpaceBeforeParensOptions.AfterControlStatements ||
5287 spaceRequiredBeforeParens(Right);
5289 if ((
Left.isAccessSpecifierKeyword() ||
5290 Left.isOneOf(tok::kw_static, Keywords.kw_final, Keywords.kw_abstract,
5291 Keywords.kw_native)) &&
5292 Right.is(TT_TemplateOpener)) {
5295 }
else if (IsVerilog) {
5297 if (
Left.is(tok::identifier) &&
Left.TokenText[0] ==
'\\')
5301 if ((
Left.is(TT_VerilogTableItem) &&
5302 !
Right.isOneOf(tok::r_paren, tok::semi)) ||
5303 (
Right.is(TT_VerilogTableItem) &&
Left.isNot(tok::l_paren))) {
5305 return !(
Next &&
Next->is(tok::r_paren));
5308 if (
Left.isNot(TT_BinaryOperator) &&
5309 Left.isOneOf(Keywords.kw_verilogHash, Keywords.kw_verilogHashHash)) {
5313 if (
Right.isNot(tok::semi) &&
5314 (
Left.endsSequence(tok::numeric_constant, Keywords.kw_verilogHash) ||
5315 Left.endsSequence(tok::numeric_constant,
5316 Keywords.kw_verilogHashHash) ||
5317 (
Left.is(tok::r_paren) &&
Left.MatchingParen &&
5318 Left.MatchingParen->endsSequence(tok::l_paren, tok::at)))) {
5323 if (
Left.is(Keywords.kw_apostrophe) ||
5324 (
Left.is(TT_VerilogNumberBase) &&
Right.is(tok::numeric_constant))) {
5328 if (
Left.is(tok::arrow) ||
Right.is(tok::arrow))
5333 if (
Left.is(tok::at) &&
Right.isOneOf(tok::l_paren, tok::star, tok::at))
5336 if (
Right.is(tok::l_square) &&
5337 Left.isOneOf(TT_VerilogDimensionedTypeName, Keywords.kw_function)) {
5341 if (
Right.isOneOf(tok::period, Keywords.kw_apostrophe) &&
5342 Keywords.isVerilogIdentifier(Left) &&
Left.getPreviousNonComment() &&
5343 Left.getPreviousNonComment()->is(Keywords.kw_tagged)) {
5349 if ((
Right.is(Keywords.kw_apostrophe) ||
5351 !(
Left.isOneOf(Keywords.kw_assign, Keywords.kw_unique) ||
5352 Keywords.isVerilogWordOperator(Left)) &&
5353 (
Left.isOneOf(tok::r_square, tok::r_paren, tok::r_brace,
5354 tok::numeric_constant) ||
5355 Keywords.isWordLike(Left))) {
5359 if ((
Right.is(tok::star) &&
Left.is(tok::coloncolon)) ||
5360 (
Left.is(tok::star) &&
Right.is(tok::semi))) {
5364 if (
Left.endsSequence(tok::star, tok::l_paren) &&
Right.is(tok::identifier))
5367 if (
Right.is(tok::l_paren) &&
Right.is(TT_VerilogStrength))
5370 if ((
Left.is(tok::l_brace) &&
5371 Right.isOneOf(tok::lessless, tok::greatergreater)) ||
5372 (
Left.endsSequence(tok::lessless, tok::l_brace) ||
5373 Left.endsSequence(tok::greatergreater, tok::l_brace))) {
5376 }
else if (Style.isTableGen()) {
5378 if (
Left.is(tok::l_square) &&
Right.is(tok::l_brace))
5380 if (
Left.is(tok::r_brace) &&
Right.is(tok::r_square))
5383 if (
Right.isOneOf(TT_TableGenDAGArgListColon,
5384 TT_TableGenDAGArgListColonToAlign) ||
5385 Left.isOneOf(TT_TableGenDAGArgListColon,
5386 TT_TableGenDAGArgListColonToAlign)) {
5389 if (
Right.is(TT_TableGenCondOperatorColon))
5391 if (
Left.isOneOf(TT_TableGenDAGArgOperatorID,
5392 TT_TableGenDAGArgOperatorToBreak) &&
5393 Right.isNot(TT_TableGenDAGArgCloser)) {
5397 if (
Right.isOneOf(tok::l_paren, tok::less) &&
5398 Left.isOneOf(TT_TableGenBangOperator, TT_TableGenCondOperator)) {
5403 if (
Left.is(TT_TableGenTrailingPasteOperator) &&
5404 Right.isOneOf(tok::l_brace, tok::colon)) {
5408 if (
Left.is(tok::hash) ||
Right.is(tok::hash))
5411 if (Keywords.isTableGenDefinition(Left))
5415 if (
Left.is(TT_ImplicitStringLiteral))
5416 return Right.hasWhitespaceBefore();
5418 if (
Left.is(TT_ObjCMethodSpecifier))
5420 if (
Left.is(tok::r_paren) &&
Left.isNot(TT_AttributeRParen) &&
5421 canBeObjCSelectorComponent(Right)) {
5429 (
Right.is(tok::equal) ||
Left.is(tok::equal))) {
5433 if (
Right.isOneOf(TT_TrailingReturnArrow, TT_LambdaArrow) ||
5434 Left.isOneOf(TT_TrailingReturnArrow, TT_LambdaArrow)) {
5437 if (
Left.is(tok::comma) &&
Right.isNot(TT_OverloadedOperatorLParen) &&
5440 (
Left.Children.empty() || !
Left.MacroParent)) {
5443 if (
Right.is(tok::comma))
5445 if (
Right.is(TT_ObjCBlockLParen))
5447 if (
Right.is(TT_CtorInitializerColon))
5448 return Style.SpaceBeforeCtorInitializerColon;
5449 if (
Right.is(TT_InheritanceColon) && !Style.SpaceBeforeInheritanceColon)
5451 if (
Right.is(TT_RangeBasedForLoopColon) &&
5452 !Style.SpaceBeforeRangeBasedForLoopColon) {
5455 if (
Left.is(TT_BitFieldColon)) {
5456 return Style.BitFieldColonSpacing == FormatStyle::BFCS_Both ||
5457 Style.BitFieldColonSpacing == FormatStyle::BFCS_After;
5459 if (
Right.is(tok::colon)) {
5460 if (
Right.is(TT_CaseLabelColon))
5461 return Style.SpaceBeforeCaseColon;
5462 if (
Right.is(TT_GotoLabelColon))
5465 if (!
Right.getNextNonComment())
5467 if (
Right.is(TT_ObjCMethodExpr))
5469 if (
Left.is(tok::question))
5471 if (
Right.is(TT_InlineASMColon) &&
Left.is(tok::coloncolon))
5473 if (
Right.is(TT_DictLiteral))
5474 return Style.SpacesInContainerLiterals;
5475 if (
Right.is(TT_AttributeColon))
5477 if (
Right.is(TT_CSharpNamedArgumentColon))
5479 if (
Right.is(TT_GenericSelectionColon))
5481 if (
Right.is(TT_BitFieldColon)) {
5482 return Style.BitFieldColonSpacing == FormatStyle::BFCS_Both ||
5483 Style.BitFieldColonSpacing == FormatStyle::BFCS_Before;
5488 if ((
Left.isOneOf(tok::minus, tok::minusminus) &&
5489 Right.isOneOf(tok::minus, tok::minusminus)) ||
5490 (
Left.isOneOf(tok::plus, tok::plusplus) &&
5491 Right.isOneOf(tok::plus, tok::plusplus))) {
5494 if (
Left.is(TT_UnaryOperator)) {
5497 if (
Left.is(tok::amp) &&
Right.is(tok::r_square))
5498 return Style.SpacesInSquareBrackets;
5499 if (
Left.isNot(tok::exclaim))
5501 if (
Left.TokenText ==
"!")
5502 return Style.SpaceAfterLogicalNot;
5503 assert(
Left.TokenText ==
"not");
5504 return Right.isOneOf(tok::coloncolon, TT_UnaryOperator) ||
5505 (
Right.is(tok::l_paren) && Style.SpaceBeforeParensOptions.AfterNot);
5510 if (
Left.is(TT_CastRParen)) {
5511 return Style.SpaceAfterCStyleCast ||
5512 Right.isOneOf(TT_BinaryOperator, TT_SelectorName);
5515 auto ShouldAddSpacesInAngles = [
this, &
Right]() {
5516 if (this->Style.SpacesInAngles == FormatStyle::SIAS_Always)
5518 if (this->Style.SpacesInAngles == FormatStyle::SIAS_Leave)
5519 return Right.hasWhitespaceBefore();
5523 if (
Left.is(tok::greater) &&
Right.is(tok::greater)) {
5524 if (Style.isTextProto() ||
5525 (Style.Language == FormatStyle::LK_Proto &&
Left.is(TT_DictLiteral))) {
5526 return !Style.Cpp11BracedListStyle;
5528 return Right.is(TT_TemplateCloser) &&
Left.is(TT_TemplateCloser) &&
5529 ((Style.Standard < FormatStyle::LS_Cpp11) ||
5530 ShouldAddSpacesInAngles());
5532 if (
Right.isOneOf(tok::arrow, tok::arrowstar, tok::periodstar) ||
5533 Left.isOneOf(tok::arrow, tok::period, tok::arrowstar, tok::periodstar) ||
5534 (
Right.is(tok::period) &&
Right.isNot(TT_DesignatedInitializerPeriod))) {
5537 if (!Style.SpaceBeforeAssignmentOperators &&
Left.isNot(TT_TemplateCloser) &&
5541 if (Style.isJava() &&
Right.is(tok::coloncolon) &&
5542 Left.isOneOf(tok::identifier, tok::kw_this)) {
5545 if (
Right.is(tok::coloncolon) &&
Left.is(tok::identifier)) {
5549 return Right.hasWhitespaceBefore();
5551 if (
Right.is(tok::coloncolon) &&
5552 !
Left.isOneOf(tok::l_brace, tok::comment, tok::l_paren)) {
5554 return (
Left.is(TT_TemplateOpener) &&
5555 ((Style.Standard < FormatStyle::LS_Cpp11) ||
5556 ShouldAddSpacesInAngles())) ||
5557 !(
Left.isOneOf(tok::l_paren, tok::r_paren, tok::l_square,
5558 tok::kw___super, TT_TemplateOpener,
5559 TT_TemplateCloser)) ||
5560 (
Left.is(tok::l_paren) && Style.SpacesInParensOptions.Other);
5562 if ((
Left.is(TT_TemplateOpener)) != (
Right.is(TT_TemplateCloser)))
5563 return ShouldAddSpacesInAngles();
5564 if (
Left.is(tok::r_paren) &&
Left.isNot(TT_TypeDeclarationParen) &&
5565 Right.is(TT_PointerOrReference) &&
Right.isOneOf(tok::amp, tok::ampamp)) {
5569 if (
Right.is(TT_StructuredBindingLSquare)) {
5570 return !
Left.isOneOf(tok::amp, tok::ampamp) ||
5571 getTokenReferenceAlignment(Left) != FormatStyle::PAS_Right;
5574 if (
Right.Next &&
Right.Next->is(TT_StructuredBindingLSquare) &&
5575 Right.isOneOf(tok::amp, tok::ampamp)) {
5576 return getTokenReferenceAlignment(Right) != FormatStyle::PAS_Left;
5578 if ((
Right.is(TT_BinaryOperator) &&
Left.isNot(tok::l_paren)) ||
5579 (
Left.isOneOf(TT_BinaryOperator, TT_ConditionalExpr) &&
5580 Right.isNot(tok::r_paren))) {
5583 if (
Right.is(TT_TemplateOpener) &&
Left.is(tok::r_paren) &&
5584 Left.MatchingParen &&
5585 Left.MatchingParen->is(TT_OverloadedOperatorLParen)) {
5588 if (
Right.is(tok::less) &&
Left.isNot(tok::l_paren) &&
5592 if (
Right.is(TT_TrailingUnaryOperator))
5594 if (
Left.is(TT_RegexLiteral))
5596 return spaceRequiredBetween(
Line, Left, Right);
5602 !
Tok.isOneOf(TT_ObjCBlockLBrace, TT_LambdaLBrace, TT_DictLiteral);
5607 return Tok.MatchingParen &&
Tok.MatchingParen->Next &&
5608 Tok.MatchingParen->Next->isOneOf(tok::comma, tok::r_paren,
5614 FormatStyle::ShortLambdaStyle ShortLambdaOption) {
5615 return Tok.Children.empty() && ShortLambdaOption != FormatStyle::SLS_None;
5620 !
Tok.isOneOf(TT_ObjCBlockLBrace, TT_DictLiteral);
5623bool TokenAnnotator::mustBreakBefore(
const AnnotatedLine &
Line,
5625 if (Right.NewlinesBefore > 1 && Style.MaxEmptyLinesToKeep > 0 &&
5626 (!Style.RemoveEmptyLinesInUnwrappedLines || &Right ==
Line.First)) {
5632 if (Style.BreakFunctionDefinitionParameters &&
Line.MightBeFunctionDecl &&
5633 Line.mightBeFunctionDefinition() && Left.MightBeFunctionDeclParen &&
5634 Left.ParameterCount > 0) {
5640 if (Style.BinPackParameters == FormatStyle::BPPS_AlwaysOnePerLine &&
5641 Line.MightBeFunctionDecl && !
Left.opensScope() &&
5646 const auto *BeforeLeft =
Left.Previous;
5647 const auto *AfterRight =
Right.Next;
5649 if (Style.isCSharp()) {
5650 if (
Left.is(TT_FatArrow) &&
Right.is(tok::l_brace) &&
5651 Style.BraceWrapping.AfterFunction) {
5654 if (
Right.is(TT_CSharpNamedArgumentColon) ||
5655 Left.is(TT_CSharpNamedArgumentColon)) {
5658 if (
Right.is(TT_CSharpGenericTypeConstraint))
5660 if (AfterRight && AfterRight->is(TT_FatArrow) &&
5661 (
Right.is(tok::numeric_constant) ||
5662 (
Right.is(tok::identifier) &&
Right.TokenText ==
"_"))) {
5667 if (
Left.is(TT_AttributeSquare) &&
Left.is(tok::r_square) &&
5668 (
Right.isAccessSpecifier(
false) ||
5669 Right.is(Keywords.kw_internal))) {
5673 if (
Left.is(TT_AttributeSquare) &&
Right.is(TT_AttributeSquare) &&
5674 Left.is(tok::r_square) &&
Right.is(tok::l_square)) {
5677 }
else if (Style.isJavaScript()) {
5679 if (
Right.is(tok::string_literal) &&
Left.is(tok::plus) && BeforeLeft &&
5680 BeforeLeft->is(tok::string_literal)) {
5683 if (
Left.is(TT_DictLiteral) &&
Left.is(tok::l_brace) &&
Line.Level == 0 &&
5684 BeforeLeft && BeforeLeft->is(tok::equal) &&
5685 Line.First->isOneOf(tok::identifier, Keywords.kw_import, tok::kw_export,
5689 !
Line.First->isOneOf(Keywords.kw_var, Keywords.kw_let)) {
5694 if (
Left.is(tok::l_brace) &&
Line.Level == 0 &&
5695 (
Line.startsWith(tok::kw_enum) ||
5696 Line.startsWith(tok::kw_const, tok::kw_enum) ||
5697 Line.startsWith(tok::kw_export, tok::kw_enum) ||
5698 Line.startsWith(tok::kw_export, tok::kw_const, tok::kw_enum))) {
5703 if (
Right.is(tok::r_brace) &&
Left.is(tok::l_brace) && BeforeLeft &&
5704 BeforeLeft->is(TT_FatArrow)) {
5706 switch (Style.AllowShortLambdasOnASingleLine) {
5707 case FormatStyle::SLS_All:
5709 case FormatStyle::SLS_None:
5711 case FormatStyle::SLS_Empty:
5712 return !
Left.Children.empty();
5713 case FormatStyle::SLS_Inline:
5716 return (
Left.NestingLevel == 0 &&
Line.Level == 0) &&
5717 !
Left.Children.empty();
5719 llvm_unreachable(
"Unknown FormatStyle::ShortLambdaStyle enum");
5722 if (
Right.is(tok::r_brace) &&
Left.is(tok::l_brace) &&
5723 !
Left.Children.empty()) {
5725 return Style.AllowShortFunctionsOnASingleLine == FormatStyle::SFS_None ||
5726 Style.AllowShortFunctionsOnASingleLine == FormatStyle::SFS_Empty ||
5727 (
Left.NestingLevel == 0 &&
Line.Level == 0 &&
5728 Style.AllowShortFunctionsOnASingleLine &
5729 FormatStyle::SFS_InlineOnly);
5731 }
else if (Style.isJava()) {
5732 if (
Right.is(tok::plus) &&
Left.is(tok::string_literal) && AfterRight &&
5733 AfterRight->is(tok::string_literal)) {
5736 }
else if (Style.isVerilog()) {
5738 if (
Left.is(TT_VerilogAssignComma))
5741 if (
Left.is(TT_VerilogTypeComma))
5745 if (Style.VerilogBreakBetweenInstancePorts &&
5746 (
Left.is(TT_VerilogInstancePortComma) ||
5747 (
Left.is(tok::r_paren) && Keywords.isVerilogIdentifier(Right) &&
5748 Left.MatchingParen &&
5749 Left.MatchingParen->is(TT_VerilogInstancePortLParen)))) {
5754 if (!Keywords.isVerilogBegin(Right) && Keywords.isVerilogEndOfLabel(Left))
5756 }
else if (Style.BreakAdjacentStringLiterals &&
5757 (IsCpp || Style.isProto() || Style.isTableGen())) {
5758 if (
Left.isStringLiteral() &&
Right.isStringLiteral())
5763 if (Style.isJson()) {
5767 if (
Left.is(TT_DictLiteral) &&
Left.is(tok::l_brace))
5770 if ((
Left.is(TT_ArrayInitializerLSquare) &&
Left.is(tok::l_square) &&
5771 Right.isNot(tok::r_square)) ||
5772 Left.is(tok::comma)) {
5773 if (
Right.is(tok::l_brace))
5778 if (
Tok->isOneOf(tok::l_brace, tok::l_square))
5780 if (
Tok->isOneOf(tok::r_brace, tok::r_square))
5783 return Style.BreakArrays;
5785 }
else if (Style.isTableGen()) {
5789 if (
Left.is(TT_TableGenCondOperatorComma))
5791 if (
Left.is(TT_TableGenDAGArgOperatorToBreak) &&
5792 Right.isNot(TT_TableGenDAGArgCloser)) {
5795 if (
Left.is(TT_TableGenDAGArgListCommaToBreak))
5797 if (
Right.is(TT_TableGenDAGArgCloser) &&
Right.MatchingParen &&
5798 Right.MatchingParen->is(TT_TableGenDAGArgOpenerToBreak) &&
5799 &Left !=
Right.MatchingParen->Next) {
5801 return Style.TableGenBreakInsideDAGArg == FormatStyle::DAS_BreakAll;
5805 if (
Line.startsWith(tok::kw_asm) &&
Right.is(TT_InlineASMColon) &&
5806 Style.BreakBeforeInlineASMColon == FormatStyle::BBIAS_Always) {
5816 if ((
Left.isOneOf(tok::l_brace, TT_ArrayInitializerLSquare) ||
5817 (Style.isJavaScript() &&
Left.is(tok::l_paren))) &&
5819 BeforeClosingBrace =
Left.MatchingParen->Previous;
5820 }
else if (
Right.MatchingParen &&
5821 (
Right.MatchingParen->isOneOf(tok::l_brace,
5822 TT_ArrayInitializerLSquare) ||
5823 (Style.isJavaScript() &&
5824 Right.MatchingParen->is(tok::l_paren)))) {
5825 BeforeClosingBrace = &
Left;
5827 if (BeforeClosingBrace && (BeforeClosingBrace->is(tok::comma) ||
5828 BeforeClosingBrace->isTrailingComment())) {
5833 if (
Right.is(tok::comment)) {
5835 Right.NewlinesBefore > 0 &&
Right.HasUnescapedNewline;
5837 if (
Left.isTrailingComment())
5839 if (
Left.IsUnterminatedLiteral)
5842 if (BeforeLeft && BeforeLeft->is(tok::lessless) &&
5843 Left.is(tok::string_literal) &&
Right.is(tok::lessless) && AfterRight &&
5844 AfterRight->is(tok::string_literal)) {
5845 return Right.NewlinesBefore > 0;
5848 if (
Right.is(TT_RequiresClause)) {
5849 switch (Style.RequiresClausePosition) {
5850 case FormatStyle::RCPS_OwnLine:
5851 case FormatStyle::RCPS_OwnLineWithBrace:
5852 case FormatStyle::RCPS_WithFollowing:
5859 if (
Left.ClosesTemplateDeclaration &&
Left.MatchingParen &&
5860 Left.MatchingParen->NestingLevel == 0) {
5864 if (
Right.is(tok::kw_concept))
5865 return Style.BreakBeforeConceptDeclarations == FormatStyle::BBCDS_Always;
5866 return Style.BreakTemplateDeclarations == FormatStyle::BTDS_Yes ||
5867 (Style.BreakTemplateDeclarations == FormatStyle::BTDS_Leave &&
5868 Right.NewlinesBefore > 0);
5870 if (
Left.ClosesRequiresClause) {
5871 switch (Style.RequiresClausePosition) {
5872 case FormatStyle::RCPS_OwnLine:
5873 case FormatStyle::RCPS_WithPreceding:
5874 return Right.isNot(tok::semi);
5875 case FormatStyle::RCPS_OwnLineWithBrace:
5876 return !
Right.isOneOf(tok::semi, tok::l_brace);
5881 if (Style.PackConstructorInitializers == FormatStyle::PCIS_Never) {
5882 if (Style.BreakConstructorInitializers == FormatStyle::BCIS_BeforeColon &&
5883 (
Left.is(TT_CtorInitializerComma) ||
5884 Right.is(TT_CtorInitializerColon))) {
5888 if (Style.BreakConstructorInitializers == FormatStyle::BCIS_AfterColon &&
5889 Left.isOneOf(TT_CtorInitializerColon, TT_CtorInitializerComma)) {
5893 if (Style.PackConstructorInitializers < FormatStyle::PCIS_CurrentLine &&
5894 Style.BreakConstructorInitializers == FormatStyle::BCIS_BeforeComma &&
5895 Right.isOneOf(TT_CtorInitializerComma, TT_CtorInitializerColon)) {
5898 if (Style.PackConstructorInitializers == FormatStyle::PCIS_NextLineOnly) {
5899 if ((Style.BreakConstructorInitializers == FormatStyle::BCIS_BeforeColon ||
5900 Style.BreakConstructorInitializers == FormatStyle::BCIS_BeforeComma) &&
5901 Right.is(TT_CtorInitializerColon)) {
5905 if (Style.BreakConstructorInitializers == FormatStyle::BCIS_AfterColon &&
5906 Left.is(TT_CtorInitializerColon)) {
5911 if (Style.BreakInheritanceList == FormatStyle::BILS_BeforeComma &&
5912 Right.is(TT_InheritanceComma)) {
5915 if (Style.BreakInheritanceList == FormatStyle::BILS_AfterComma &&
5916 Left.is(TT_InheritanceComma)) {
5919 if (
Right.is(tok::string_literal) &&
Right.TokenText.starts_with(
"R\"")) {
5923 return Right.IsMultiline &&
Right.NewlinesBefore > 0;
5925 if ((
Left.is(tok::l_brace) ||
5926 (
Left.is(tok::less) && BeforeLeft && BeforeLeft->is(tok::equal))) &&
5927 Right.NestingLevel == 1 && Style.Language == FormatStyle::LK_Proto) {
5932 if (
Right.is(TT_InlineASMBrace))
5933 return Right.HasUnescapedNewline;
5936 auto *FirstNonComment =
Line.getFirstNonComment();
5938 FirstNonComment && (FirstNonComment->is(Keywords.kw_internal) ||
5939 FirstNonComment->isAccessSpecifierKeyword());
5941 if (Style.BraceWrapping.AfterEnum) {
5942 if (
Line.startsWith(tok::kw_enum) ||
5943 Line.startsWith(tok::kw_typedef, tok::kw_enum)) {
5948 FirstNonComment->Next->is(tok::kw_enum)) {
5954 if (Style.BraceWrapping.AfterClass &&
5956 FirstNonComment->Next->is(Keywords.kw_interface)) ||
5957 Line.startsWith(Keywords.kw_interface))) {
5962 if (
Right.isNot(TT_FunctionLBrace)) {
5963 return (
Line.startsWith(tok::kw_class) &&
5964 Style.BraceWrapping.AfterClass) ||
5965 (
Line.startsWith(tok::kw_struct) &&
5966 Style.BraceWrapping.AfterStruct);
5970 if (
Left.is(TT_ObjCBlockLBrace) &&
5971 Style.AllowShortBlocksOnASingleLine == FormatStyle::SBS_Never) {
5976 if (
Left.isOneOf(TT_AttributeRParen, TT_AttributeMacro) &&
5977 Right.is(TT_ObjCDecl)) {
5981 if (
Left.is(TT_LambdaLBrace)) {
5983 Style.AllowShortLambdasOnASingleLine == FormatStyle::SLS_Inline) {
5987 if (Style.AllowShortLambdasOnASingleLine == FormatStyle::SLS_None ||
5988 Style.AllowShortLambdasOnASingleLine == FormatStyle::SLS_Inline ||
5989 (!
Left.Children.empty() &&
5990 Style.AllowShortLambdasOnASingleLine == FormatStyle::SLS_Empty)) {
5995 if (Style.BraceWrapping.BeforeLambdaBody &&
Right.is(TT_LambdaLBrace) &&
5996 (
Left.isPointerOrReference() ||
Left.is(TT_TemplateCloser))) {
6001 if ((Style.isJava() || Style.isJavaScript()) &&
6002 Left.is(TT_LeadingJavaAnnotation) &&
6003 !
Right.isOneOf(TT_LeadingJavaAnnotation, tok::l_paren) &&
6004 (
Line.Last->is(tok::l_brace) || Style.BreakAfterJavaFieldAnnotations)) {
6008 if (
Right.is(TT_ProtoExtensionLSquare))
6038 if (Style.isProto() &&
Right.is(TT_SelectorName) &&
6039 Right.isNot(tok::r_square) && AfterRight) {
6042 if (
Left.is(tok::at))
6048 const auto *LBrace = AfterRight;
6049 if (LBrace && LBrace->is(tok::colon)) {
6050 LBrace = LBrace->Next;
6051 if (LBrace && LBrace->is(tok::at)) {
6052 LBrace = LBrace->Next;
6054 LBrace = LBrace->Next;
6066 ((LBrace->is(tok::l_brace) &&
6067 (LBrace->is(TT_DictLiteral) ||
6068 (LBrace->Next && LBrace->Next->is(tok::r_brace)))) ||
6069 LBrace->isOneOf(TT_ArrayInitializerLSquare, tok::less))) {
6076 if (
Left.ParameterCount == 0)
6091 if (
Left.isOneOf(tok::r_brace, tok::greater, tok::r_square))
6102 if (Style.isCSharp()) {
6103 if (
Left.isOneOf(TT_CSharpNamedArgumentColon, TT_AttributeColon) ||
6104 Right.isOneOf(TT_CSharpNamedArgumentColon, TT_AttributeColon)) {
6108 if (
Line.First->is(TT_CSharpGenericTypeConstraint))
6109 return Left.is(TT_CSharpGenericTypeConstraintComma);
6111 if (
Right.is(TT_CSharpNullable))
6113 }
else if (Style.isJava()) {
6114 if (
Left.isOneOf(Keywords.kw_throws, Keywords.kw_extends,
6115 Keywords.kw_implements)) {
6118 if (
Right.isOneOf(Keywords.kw_throws, Keywords.kw_extends,
6119 Keywords.kw_implements)) {
6122 }
else if (Style.isJavaScript()) {
6125 (NonComment->isAccessSpecifierKeyword() ||
6126 NonComment->isOneOf(
6127 tok::kw_return, Keywords.kw_yield, tok::kw_continue, tok::kw_break,
6128 tok::kw_throw, Keywords.kw_interface, Keywords.kw_type,
6129 tok::kw_static, Keywords.kw_readonly, Keywords.kw_override,
6130 Keywords.kw_abstract, Keywords.kw_get, Keywords.kw_set,
6131 Keywords.kw_async, Keywords.kw_await))) {
6134 if (
Right.NestingLevel == 0 &&
6135 (
Left.Tok.getIdentifierInfo() ||
6136 Left.isOneOf(tok::r_square, tok::r_paren)) &&
6137 Right.isOneOf(tok::l_square, tok::l_paren)) {
6140 if (NonComment && NonComment->is(tok::identifier) &&
6141 NonComment->TokenText ==
"asserts") {
6144 if (
Left.is(TT_FatArrow) &&
Right.is(tok::l_brace))
6146 if (
Left.is(TT_JsTypeColon))
6149 if (
Left.is(tok::exclaim) &&
Right.is(tok::colon))
6154 if (
Right.is(Keywords.kw_is)) {
6163 if (!
Next ||
Next->isNot(tok::colon))
6166 if (
Left.is(Keywords.kw_in))
6167 return Style.BreakBeforeBinaryOperators == FormatStyle::BOS_None;
6168 if (
Right.is(Keywords.kw_in))
6169 return Style.BreakBeforeBinaryOperators != FormatStyle::BOS_None;
6170 if (
Right.is(Keywords.kw_as))
6172 if (
Right.isOneOf(Keywords.kw_extends, Keywords.kw_infer)) {
6178 if (
Left.is(Keywords.kw_as))
6180 if (
Left.is(TT_NonNullAssertion))
6182 if (
Left.is(Keywords.kw_declare) &&
6183 Right.isOneOf(Keywords.kw_module, tok::kw_namespace,
6184 Keywords.kw_function, tok::kw_class, tok::kw_enum,
6185 Keywords.kw_interface, Keywords.kw_type, Keywords.kw_var,
6186 Keywords.kw_let, tok::kw_const)) {
6191 if (
Left.isOneOf(Keywords.kw_module, tok::kw_namespace) &&
6192 Right.isOneOf(tok::identifier, tok::string_literal)) {
6195 if (
Right.is(TT_TemplateString) &&
Right.closesScope())
6199 if (
Left.is(tok::identifier) &&
Right.is(TT_TemplateString))
6201 if (
Left.is(TT_TemplateString) &&
Left.opensScope())
6203 }
else if (Style.isTableGen()) {
6205 if (Keywords.isTableGenDefinition(Left))
6208 if (
Right.is(tok::l_paren)) {
6209 return !
Left.isOneOf(TT_TableGenBangOperator, TT_TableGenCondOperator,
6213 if (
Left.is(TT_TableGenValueSuffix))
6216 if (
Left.is(tok::hash) ||
Right.is(tok::hash))
6218 if (
Left.isOneOf(TT_TableGenBangOperator, TT_TableGenCondOperator))
6225 if (
Right.is(tok::r_brace)) {
6227 (
Right.isBlockIndentedInitRBrace(Style)));
6231 if (
Right.is(tok::r_paren)) {
6232 if (Style.AlignAfterOpenBracket != FormatStyle::BAS_BlockIndent ||
6233 !
Right.MatchingParen) {
6237 if (
Next &&
Next->is(tok::r_paren))
6239 if (
Next &&
Next->is(tok::l_paren))
6245 if (
Left.isOneOf(tok::r_paren, TT_TrailingAnnotation) &&
6246 Right.is(TT_TrailingAnnotation) &&
6247 Style.AlignAfterOpenBracket == FormatStyle::BAS_BlockIndent) {
6251 if (
Right.is(TT_TemplateCloser))
6252 return Style.BreakBeforeTemplateCloser;
6254 if (
Left.isOneOf(tok::at, tok::objc_interface))
6256 if (
Left.isOneOf(TT_JavaAnnotation, TT_LeadingJavaAnnotation))
6257 return Right.isNot(tok::l_paren);
6258 if (
Right.is(TT_PointerOrReference)) {
6259 return Line.IsMultiVariableDeclStmt ||
6260 (getTokenPointerOrReferenceAlignment(Right) ==
6261 FormatStyle::PAS_Right &&
6263 Right.Next->isOneOf(TT_FunctionDeclarationName, tok::kw_const)));
6265 if (
Right.isOneOf(TT_StartOfName, TT_FunctionDeclarationName,
6266 TT_ClassHeadName, TT_QtProperty, tok::kw_operator)) {
6269 if (
Left.is(TT_PointerOrReference))
6271 if (
Right.isTrailingComment()) {
6278 (
Left.is(TT_CtorInitializerColon) &&
Right.NewlinesBefore > 0 &&
6279 Style.BreakConstructorInitializers == FormatStyle::BCIS_AfterColon);
6281 if (
Left.is(tok::question) &&
Right.is(tok::colon))
6283 if (
Right.isOneOf(TT_ConditionalExpr, tok::question))
6284 return Style.BreakBeforeTernaryOperators;
6285 if (
Left.isOneOf(TT_ConditionalExpr, tok::question))
6286 return !Style.BreakBeforeTernaryOperators;
6287 if (
Left.is(TT_InheritanceColon))
6288 return Style.BreakInheritanceList == FormatStyle::BILS_AfterColon;
6289 if (
Right.is(TT_InheritanceColon))
6290 return Style.BreakInheritanceList != FormatStyle::BILS_AfterColon;
6291 if (
Right.is(TT_ObjCMethodExpr) &&
Right.isNot(tok::r_square) &&
6292 Left.isNot(TT_SelectorName)) {
6296 if (
Right.is(tok::colon) &&
6297 !
Right.isOneOf(TT_CtorInitializerColon, TT_InlineASMColon,
6298 TT_BitFieldColon)) {
6301 if (
Left.is(tok::colon) &&
Left.isOneOf(TT_DictLiteral, TT_ObjCMethodExpr)) {
6302 if (Style.isProto()) {
6303 if (!Style.AlwaysBreakBeforeMultilineStrings &&
Right.isStringLiteral())
6329 if ((
Right.isOneOf(tok::l_brace, tok::less) &&
6330 Right.is(TT_DictLiteral)) ||
6331 Right.is(TT_ArrayInitializerLSquare)) {
6337 if (
Right.is(tok::r_square) &&
Right.MatchingParen &&
6338 Right.MatchingParen->is(TT_ProtoExtensionLSquare)) {
6341 if (
Right.is(TT_SelectorName) || (
Right.is(tok::identifier) &&
Right.Next &&
6342 Right.Next->is(TT_ObjCMethodExpr))) {
6343 return Left.isNot(tok::period);
6347 if (
Right.is(tok::kw_concept))
6348 return Style.BreakBeforeConceptDeclarations != FormatStyle::BBCDS_Never;
6349 if (
Right.is(TT_RequiresClause))
6351 if (
Left.ClosesTemplateDeclaration) {
6352 return Style.BreakTemplateDeclarations != FormatStyle::BTDS_Leave ||
6353 Right.NewlinesBefore > 0;
6355 if (
Left.is(TT_FunctionAnnotationRParen))
6357 if (
Left.ClosesRequiresClause)
6359 if (
Right.isOneOf(TT_RangeBasedForLoopColon, TT_OverloadedOperatorLParen,
6360 TT_OverloadedOperator)) {
6363 if (
Left.is(TT_RangeBasedForLoopColon))
6365 if (
Right.is(TT_RangeBasedForLoopColon))
6367 if (
Left.is(TT_TemplateCloser) &&
Right.is(TT_TemplateOpener))
6369 if ((
Left.is(tok::greater) &&
Right.is(tok::greater)) ||
6370 (
Left.is(tok::less) &&
Right.is(tok::less))) {
6373 if (
Right.is(TT_BinaryOperator) &&
6374 Style.BreakBeforeBinaryOperators != FormatStyle::BOS_None &&
6375 (Style.BreakBeforeBinaryOperators == FormatStyle::BOS_All ||
6379 if (
Left.isOneOf(TT_TemplateCloser, TT_UnaryOperator, tok::kw_operator))
6381 if (
Left.is(tok::equal) && !
Right.isOneOf(tok::kw_default, tok::kw_delete) &&
6385 if (
Left.is(tok::equal) &&
Right.is(tok::l_brace) &&
6386 !Style.Cpp11BracedListStyle) {
6389 if (
Left.is(TT_AttributeLParen) ||
6390 (
Left.is(tok::l_paren) &&
Left.is(TT_TypeDeclarationParen))) {
6393 if (
Left.is(tok::l_paren) &&
Left.Previous &&
6394 (
Left.Previous->isOneOf(TT_BinaryOperator, TT_CastRParen))) {
6397 if (
Right.is(TT_ImplicitStringLiteral))
6400 if (
Right.is(tok::r_square) &&
Right.MatchingParen &&
6401 Right.MatchingParen->is(TT_LambdaLSquare)) {
6407 if (
Left.is(TT_TrailingAnnotation)) {
6408 return !
Right.isOneOf(tok::l_brace, tok::semi, tok::equal, tok::l_paren,
6409 tok::less, tok::coloncolon);
6412 if (
Right.isAttribute())
6415 if (
Right.is(tok::l_square) &&
Right.is(TT_AttributeSquare))
6416 return Left.isNot(TT_AttributeSquare);
6418 if (
Left.is(tok::identifier) &&
Right.is(tok::string_literal))
6421 if (
Right.is(tok::identifier) &&
Right.Next &&
Right.Next->is(TT_DictLiteral))
6424 if (
Left.is(TT_CtorInitializerColon)) {
6425 return Style.BreakConstructorInitializers == FormatStyle::BCIS_AfterColon &&
6426 (!
Right.isTrailingComment() ||
Right.NewlinesBefore > 0);
6428 if (
Right.is(TT_CtorInitializerColon))
6429 return Style.BreakConstructorInitializers != FormatStyle::BCIS_AfterColon;
6430 if (
Left.is(TT_CtorInitializerComma) &&
6431 Style.BreakConstructorInitializers == FormatStyle::BCIS_BeforeComma) {
6434 if (
Right.is(TT_CtorInitializerComma) &&
6435 Style.BreakConstructorInitializers == FormatStyle::BCIS_BeforeComma) {
6438 if (
Left.is(TT_InheritanceComma) &&
6439 Style.BreakInheritanceList == FormatStyle::BILS_BeforeComma) {
6442 if (
Right.is(TT_InheritanceComma) &&
6443 Style.BreakInheritanceList == FormatStyle::BILS_BeforeComma) {
6446 if (
Left.is(TT_ArrayInitializerLSquare))
6448 if (
Right.is(tok::kw_typename) &&
Left.isNot(tok::kw_const))
6450 if ((
Left.isBinaryOperator() ||
Left.is(TT_BinaryOperator)) &&
6451 !
Left.isOneOf(tok::arrowstar, tok::lessless) &&
6452 Style.BreakBeforeBinaryOperators != FormatStyle::BOS_All &&
6453 (Style.BreakBeforeBinaryOperators == FormatStyle::BOS_None ||
6457 if ((
Left.is(TT_AttributeSquare) &&
Right.is(tok::l_square)) ||
6458 (
Left.is(tok::r_square) &&
Right.is(TT_AttributeSquare))) {
6462 auto ShortLambdaOption = Style.AllowShortLambdasOnASingleLine;
6463 if (Style.BraceWrapping.BeforeLambdaBody &&
Right.is(TT_LambdaLBrace)) {
6470 if (
Right.is(tok::kw_noexcept) &&
Right.is(TT_TrailingAnnotation)) {
6471 switch (Style.AllowBreakBeforeNoexceptSpecifier) {
6472 case FormatStyle::BBNSS_Never:
6474 case FormatStyle::BBNSS_Always:
6476 case FormatStyle::BBNSS_OnlyWithParen:
6477 return Right.Next &&
Right.Next->is(tok::l_paren);
6481 return Left.isOneOf(tok::comma, tok::coloncolon, tok::semi, tok::l_brace,
6482 tok::kw_class, tok::kw_struct, tok::comment) ||
6483 Right.isMemberAccess() ||
6484 Right.isOneOf(TT_TrailingReturnArrow, TT_LambdaArrow, tok::lessless,
6485 tok::colon, tok::l_square, tok::at) ||
6486 (
Left.is(tok::r_paren) &&
6487 Right.isOneOf(tok::identifier, tok::kw_const)) ||
6488 (
Left.is(tok::l_paren) &&
Right.isNot(tok::r_paren)) ||
6489 (
Left.is(TT_TemplateOpener) &&
Right.isNot(TT_TemplateCloser));
6493 llvm::errs() <<
"AnnotatedTokens(L=" <<
Line.Level <<
", P=" <<
Line.PPLevel
6494 <<
", T=" <<
Line.Type <<
", C=" <<
Line.IsContinuation
6498 llvm::errs() <<
" I=" <<
Tok->IndentLevel <<
" M=" <<
Tok->MustBreakBefore
6499 <<
" C=" <<
Tok->CanBreakBefore
6501 <<
" S=" <<
Tok->SpacesRequiredBefore
6502 <<
" F=" <<
Tok->Finalized <<
" B=" <<
Tok->BlockParameterCount
6503 <<
" BK=" <<
Tok->getBlockKind() <<
" P=" <<
Tok->SplitPenalty
6504 <<
" Name=" <<
Tok->Tok.getName() <<
" N=" <<
Tok->NestingLevel
6505 <<
" L=" <<
Tok->TotalLength
6506 <<
" PPK=" <<
Tok->getPackingKind() <<
" FakeLParens=";
6508 llvm::errs() << LParen <<
"/";
6509 llvm::errs() <<
" FakeRParens=" <<
Tok->FakeRParens;
6510 llvm::errs() <<
" II=" <<
Tok->Tok.getIdentifierInfo();
6511 llvm::errs() <<
" Text='" <<
Tok->TokenText <<
"'\n";
6516 llvm::errs() <<
"----\n";
6519FormatStyle::PointerAlignmentStyle
6521 assert(
Reference.isOneOf(tok::amp, tok::ampamp));
6522 switch (Style.ReferenceAlignment) {
6523 case FormatStyle::RAS_Pointer:
6524 return Style.PointerAlignment;
6525 case FormatStyle::RAS_Left:
6526 return FormatStyle::PAS_Left;
6527 case FormatStyle::RAS_Right:
6528 return FormatStyle::PAS_Right;
6529 case FormatStyle::RAS_Middle:
6530 return FormatStyle::PAS_Middle;
6533 return Style.PointerAlignment;
6536FormatStyle::PointerAlignmentStyle
6537TokenAnnotator::getTokenPointerOrReferenceAlignment(
6539 if (PointerOrReference.isOneOf(tok::amp, tok::ampamp))
6540 return getTokenReferenceAlignment(PointerOrReference);
6541 assert(PointerOrReference.is(tok::star));
6542 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.
IdentifierInfo * getIdentifierInfo() const
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.