18#include "llvm/ADT/SmallPtrSet.h"
19#include "llvm/Support/Debug.h"
21#define DEBUG_TYPE "format-token-annotator"
27 const FormatStyle &Style) {
28 switch (Style.BreakAfterAttributes) {
29 case FormatStyle::ABS_Always:
31 case FormatStyle::ABS_Leave:
32 return Tok.NewlinesBefore > 0;
42static bool startsWithInitStatement(
const AnnotatedLine &
Line) {
43 return Line.startsWith(tok::kw_for) ||
Line.startsWith(tok::kw_if) ||
44 Line.startsWith(tok::kw_switch);
59 return Tok.Tok.getIdentifierInfo();
65static bool isLambdaParameterList(
const FormatToken *Left) {
67 if (
Left->Previous &&
Left->Previous->is(tok::greater) &&
68 Left->Previous->MatchingParen &&
69 Left->Previous->MatchingParen->is(TT_TemplateOpener)) {
70 Left =
Left->Previous->MatchingParen;
74 return Left->Previous &&
Left->Previous->is(tok::r_square) &&
75 Left->Previous->MatchingParen &&
76 Left->Previous->MatchingParen->is(TT_LambdaLSquare);
82 return Tok.isOneOf(tok::kw_if, tok::kw_for, tok::kw_while, tok::kw_switch,
83 tok::kw_constexpr, tok::kw_catch);
88 if (!IsCpp || !
Tok.startsSequence(tok::l_square, tok::l_square))
91 if (
Tok.Previous &&
Tok.Previous->is(tok::at))
98 if (AttrTok->startsSequence(tok::kw_using, tok::identifier, tok::colon))
100 if (AttrTok->isNot(tok::identifier))
102 while (AttrTok && !AttrTok->startsSequence(tok::r_square, tok::r_square)) {
106 if (AttrTok->is(tok::colon) ||
107 AttrTok->startsSequence(tok::identifier, tok::identifier) ||
108 AttrTok->startsSequence(tok::r_paren, tok::identifier)) {
111 if (AttrTok->is(tok::ellipsis))
113 AttrTok = AttrTok->Next;
115 return AttrTok && AttrTok->startsSequence(tok::r_square, tok::r_square);
123class AnnotatingParser {
125 AnnotatingParser(
const FormatStyle &Style, AnnotatedLine &Line,
126 const AdditionalKeywords &Keywords,
127 SmallVector<ScopeType> &Scopes)
128 : Style(Style), Line(Line), CurrentToken(Line.
First), AutoFound(
false),
130 Keywords(Keywords), Scopes(Scopes), TemplateDeclarationDepth(0) {
131 Contexts.push_back(Context(tok::unknown, 1,
false));
132 resetTokenMetadata();
137 switch (Token.getType()) {
139 case TT_StructLBrace:
142 case TT_CompoundRequirementLBrace:
153 auto *
Left = CurrentToken->Previous;
157 if (NonTemplateLess.count(Left) > 0)
160 const auto *BeforeLess =
Left->Previous;
163 if (BeforeLess->Tok.isLiteral())
165 if (BeforeLess->is(tok::r_brace))
167 if (BeforeLess->is(tok::r_paren) && Contexts.size() > 1 &&
168 !(BeforeLess->MatchingParen &&
169 BeforeLess->MatchingParen->is(TT_OverloadedOperatorLParen))) {
172 if (BeforeLess->is(tok::kw_operator) && CurrentToken->is(tok::l_paren))
176 Left->ParentBracket = Contexts.back().ContextKind;
177 ScopedContextCreator ContextCreator(*
this, tok::less, 12);
178 Contexts.back().IsExpression =
false;
182 if (BeforeLess && BeforeLess->isNot(tok::kw_template))
183 Contexts.back().ContextType = Context::TemplateArgument;
185 if (Style.isJava() && CurrentToken->is(tok::question))
188 for (
bool SeenTernaryOperator =
false, MaybeAngles =
true; CurrentToken;) {
189 const bool InExpr = Contexts[Contexts.size() - 2].IsExpression;
190 if (CurrentToken->is(tok::greater)) {
191 const auto *
Next = CurrentToken->Next;
192 if (CurrentToken->isNot(TT_TemplateCloser)) {
199 if (
Next &&
Next->is(tok::greater) &&
200 Left->ParentBracket != tok::less &&
201 CurrentToken->getStartOfNonWhitespace() ==
202 Next->getStartOfNonWhitespace().getLocWithOffset(-1)) {
205 if (InExpr && SeenTernaryOperator &&
206 (!
Next ||
Next->isNoneOf(tok::l_paren, tok::l_brace))) {
212 Left->MatchingParen = CurrentToken;
213 CurrentToken->MatchingParen =
Left;
219 if (Style.isTextProto() ||
220 (Style.Language == FormatStyle::LK_Proto && BeforeLess &&
221 BeforeLess->isOneOf(TT_SelectorName, TT_DictLiteral))) {
222 CurrentToken->setType(TT_DictLiteral);
224 CurrentToken->setType(TT_TemplateCloser);
225 CurrentToken->Tok.setLength(1);
232 if (BeforeLess && BeforeLess->is(TT_TemplateName)) {
236 if (CurrentToken->is(tok::question) && Style.isJava()) {
240 if (CurrentToken->isOneOf(tok::r_paren, tok::r_square, tok::r_brace))
242 const auto &Prev = *CurrentToken->Previous;
249 if (MaybeAngles && InExpr && !Line.startsWith(tok::kw_template) &&
250 Prev.is(TT_BinaryOperator) &&
251 Prev.isOneOf(tok::pipepipe, tok::ampamp)) {
254 if (Prev.isOneOf(tok::question, tok::colon) && !Style.isProto())
255 SeenTernaryOperator =
true;
256 updateParameterCount(Left, CurrentToken);
257 if (Style.Language == FormatStyle::LK_Proto) {
259 if (CurrentToken->is(tok::colon) ||
260 (CurrentToken->isOneOf(tok::l_brace, tok::less) &&
265 }
else if (Style.isTableGen()) {
266 if (CurrentToken->isOneOf(tok::comma, tok::equal)) {
273 if (!parseTableGenValue())
283 bool parseUntouchableParens() {
284 while (CurrentToken) {
285 CurrentToken->Finalized =
true;
286 switch (CurrentToken->Tok.getKind()) {
289 if (!parseUntouchableParens())
304 bool parseParens(
bool IsIf =
false) {
307 assert(CurrentToken->Previous &&
"Unknown previous token");
308 FormatToken &OpeningParen = *CurrentToken->Previous;
309 assert(OpeningParen.is(tok::l_paren));
310 FormatToken *PrevNonComment = OpeningParen.getPreviousNonComment();
311 OpeningParen.ParentBracket = Contexts.back().ContextKind;
312 ScopedContextCreator ContextCreator(*
this, tok::l_paren, 1);
315 Contexts.back().ColonIsForRangeExpr =
316 Contexts.size() == 2 && Contexts[0].ColonIsForRangeExpr;
318 if (OpeningParen.Previous &&
319 OpeningParen.Previous->is(TT_UntouchableMacroFunc)) {
320 OpeningParen.Finalized =
true;
321 return parseUntouchableParens();
324 bool StartsObjCSelector =
false;
325 if (!Style.isVerilog()) {
326 if (
FormatToken *MaybeSel = OpeningParen.Previous) {
328 if (MaybeSel->is(tok::objc_selector) && MaybeSel->Previous &&
329 MaybeSel->Previous->is(tok::at)) {
330 StartsObjCSelector =
true;
335 if (OpeningParen.is(TT_OverloadedOperatorLParen)) {
338 while (Prev->isNot(tok::kw_operator)) {
339 Prev = Prev->Previous;
340 assert(Prev &&
"Expect a kw_operator prior to the OperatorLParen!");
346 bool OperatorCalledAsMemberFunction =
347 Prev->Previous && Prev->Previous->isOneOf(tok::period, tok::arrow);
348 Contexts.back().IsExpression = OperatorCalledAsMemberFunction;
349 }
else if (OpeningParen.is(TT_VerilogInstancePortLParen)) {
350 Contexts.back().IsExpression =
true;
351 Contexts.back().ContextType = Context::VerilogInstancePortList;
352 }
else if (Style.isJavaScript() &&
353 (Line.startsWith(Keywords.kw_type, tok::identifier) ||
354 Line.startsWith(tok::kw_export, Keywords.kw_type,
358 Contexts.back().IsExpression =
false;
359 }
else if (OpeningParen.Previous &&
360 (OpeningParen.Previous->isOneOf(
361 tok::kw_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 (StartsObjCSelector)
455 OpeningParen.setType(TT_ObjCSelector);
465 bool MightBeFunctionType = !Contexts[Contexts.size() - 2].IsExpression;
466 bool ProbablyFunctionType =
467 CurrentToken->isPointerOrReference() || CurrentToken->is(tok::caret);
468 bool HasMultipleLines =
false;
469 bool HasMultipleParametersOnALine =
false;
470 bool MightBeObjCForRangeLoop =
471 OpeningParen.Previous && OpeningParen.Previous->is(tok::kw_for);
473 while (CurrentToken) {
474 const auto &Prev = *CurrentToken->Previous;
475 const auto *PrevPrev = Prev.Previous;
476 if (Prev.is(TT_PointerOrReference) &&
477 PrevPrev->isOneOf(tok::l_paren, tok::coloncolon)) {
478 ProbablyFunctionType =
true;
480 if (CurrentToken->is(tok::comma))
481 MightBeFunctionType =
false;
482 if (Prev.is(TT_BinaryOperator))
483 Contexts.back().IsExpression =
true;
484 if (CurrentToken->is(tok::r_paren)) {
485 if (Prev.is(TT_PointerOrReference) &&
486 (PrevPrev == &OpeningParen || PrevPrev->is(tok::coloncolon))) {
487 MightBeFunctionType =
true;
489 if (OpeningParen.isNot(TT_CppCastLParen) && MightBeFunctionType &&
490 ProbablyFunctionType && CurrentToken->Next &&
491 (CurrentToken->Next->is(tok::l_paren) ||
492 (CurrentToken->Next->is(tok::l_square) &&
493 (Line.MustBeDeclaration ||
494 (PrevNonComment && PrevNonComment->isTypeName(LangOpts)))))) {
495 OpeningParen.setType(OpeningParen.Next->is(tok::caret)
497 : TT_FunctionTypeLParen);
499 OpeningParen.MatchingParen = CurrentToken;
500 CurrentToken->MatchingParen = &OpeningParen;
502 if (CurrentToken->Next && CurrentToken->Next->is(tok::l_brace) &&
503 OpeningParen.Previous && OpeningParen.Previous->is(tok::l_paren)) {
509 if (
Tok->is(TT_BinaryOperator) &&
Tok->isPointerOrReference())
510 Tok->setType(TT_PointerOrReference);
514 if (StartsObjCSelector) {
515 CurrentToken->setType(TT_ObjCSelector);
516 if (Contexts.back().FirstObjCSelectorName) {
517 Contexts.back().FirstObjCSelectorName->LongestObjCSelectorName =
518 Contexts.back().LongestObjCSelectorName;
522 if (OpeningParen.is(TT_AttributeLParen))
523 CurrentToken->setType(TT_AttributeRParen);
524 if (OpeningParen.is(TT_TypeDeclarationParen))
525 CurrentToken->setType(TT_TypeDeclarationParen);
526 if (OpeningParen.Previous &&
527 OpeningParen.Previous->is(TT_JavaAnnotation)) {
528 CurrentToken->setType(TT_JavaAnnotation);
530 if (OpeningParen.Previous &&
531 OpeningParen.Previous->is(TT_LeadingJavaAnnotation)) {
532 CurrentToken->setType(TT_LeadingJavaAnnotation);
534 if (OpeningParen.Previous &&
535 OpeningParen.Previous->is(TT_AttributeSquare)) {
536 CurrentToken->setType(TT_AttributeSquare);
539 if (!HasMultipleLines)
541 else if (HasMultipleParametersOnALine)
549 if (CurrentToken->isOneOf(tok::r_square, tok::r_brace))
552 if (CurrentToken->is(tok::l_brace) && OpeningParen.is(TT_ObjCBlockLParen))
553 OpeningParen.setType(TT_Unknown);
554 if (CurrentToken->is(tok::comma) && CurrentToken->Next &&
555 !CurrentToken->Next->HasUnescapedNewline &&
556 !CurrentToken->Next->isTrailingComment()) {
557 HasMultipleParametersOnALine =
true;
559 bool ProbablyFunctionTypeLParen =
560 (CurrentToken->is(tok::l_paren) && CurrentToken->Next &&
561 CurrentToken->Next->isOneOf(tok::star, tok::amp, tok::caret));
562 if ((Prev.isOneOf(tok::kw_const, tok::kw_auto) ||
563 Prev.isTypeName(LangOpts)) &&
564 !(CurrentToken->is(tok::l_brace) ||
565 (CurrentToken->is(tok::l_paren) && !ProbablyFunctionTypeLParen))) {
566 Contexts.back().IsExpression =
false;
568 if (CurrentToken->isOneOf(tok::semi, tok::colon)) {
569 MightBeObjCForRangeLoop =
false;
570 if (PossibleObjCForInToken) {
571 PossibleObjCForInToken->setType(TT_Unknown);
572 PossibleObjCForInToken =
nullptr;
575 if (IsIf && CurrentToken->is(tok::semi)) {
576 for (
auto *
Tok = OpeningParen.Next;
577 Tok != CurrentToken &&
578 Tok->isNoneOf(tok::equal, tok::l_paren, tok::l_brace);
580 if (
Tok->isPointerOrReference())
581 Tok->setFinalizedType(TT_PointerOrReference);
584 if (MightBeObjCForRangeLoop && CurrentToken->is(Keywords.kw_in)) {
585 PossibleObjCForInToken = CurrentToken;
586 PossibleObjCForInToken->setType(TT_ObjCForIn);
590 if (CurrentToken->is(tok::comma))
591 Contexts.back().CanBeExpression =
true;
593 if (Style.isTableGen()) {
594 if (CurrentToken->is(tok::comma)) {
595 if (Contexts.back().IsTableGenCondOpe)
596 CurrentToken->setType(TT_TableGenCondOperatorComma);
598 }
else if (CurrentToken->is(tok::colon)) {
599 if (Contexts.back().IsTableGenCondOpe)
600 CurrentToken->setType(TT_TableGenCondOperatorColon);
604 if (!parseTableGenValue())
612 updateParameterCount(&OpeningParen,
Tok);
613 if (CurrentToken && CurrentToken->HasUnescapedNewline)
614 HasMultipleLines =
true;
620 if (!Style.isCSharp())
624 if (
Tok.Previous &&
Tok.Previous->is(tok::identifier))
628 if (
Tok.Previous &&
Tok.Previous->is(tok::r_square)) {
639 if (AttrTok->is(tok::r_square))
643 while (AttrTok && AttrTok->isNot(tok::r_square))
644 AttrTok = AttrTok->Next;
650 AttrTok = AttrTok->Next;
655 if (AttrTok->isAccessSpecifierKeyword() ||
656 AttrTok->isOneOf(tok::comment, tok::kw_class, tok::kw_static,
657 tok::l_square, Keywords.kw_internal)) {
663 AttrTok->Next->startsSequence(tok::identifier, tok::l_paren)) {
679 Left->ParentBracket = Contexts.back().ContextKind;
685 bool CppArrayTemplates =
686 IsCpp && Parent && Parent->is(TT_TemplateCloser) &&
687 (Contexts.back().CanBeExpression || Contexts.back().IsExpression ||
688 Contexts.back().ContextType == Context::TemplateArgument);
690 const bool IsInnerSquare = Contexts.back().InCpp11AttributeSpecifier;
691 const bool IsCpp11AttributeSpecifier =
692 isCppAttribute(IsCpp, *Left) || IsInnerSquare;
695 bool IsCSharpAttributeSpecifier =
696 isCSharpAttributeSpecifier(*Left) ||
697 Contexts.back().InCSharpAttributeSpecifier;
699 bool InsideInlineASM = Line.startsWith(tok::kw_asm);
700 bool IsCppStructuredBinding =
Left->isCppStructuredBinding(IsCpp);
701 bool StartsObjCMethodExpr =
702 !IsCppStructuredBinding && !InsideInlineASM && !CppArrayTemplates &&
703 IsCpp && !IsCpp11AttributeSpecifier && !IsCSharpAttributeSpecifier &&
704 Contexts.back().CanBeExpression &&
Left->isNot(TT_LambdaLSquare) &&
705 CurrentToken->isNoneOf(tok::l_brace, tok::r_square) &&
707 Parent->isOneOf(tok::colon, tok::l_square, tok::l_paren,
708 tok::kw_return, tok::kw_throw) ||
709 Parent->isUnaryOperator() ||
711 Parent->isOneOf(TT_ObjCForIn, TT_CastRParen) ||
714 bool ColonFound =
false;
716 unsigned BindingIncrease = 1;
717 if (IsCppStructuredBinding) {
718 Left->setType(TT_StructuredBindingLSquare);
719 }
else if (
Left->is(TT_Unknown)) {
720 if (StartsObjCMethodExpr) {
721 Left->setType(TT_ObjCMethodExpr);
722 }
else if (InsideInlineASM) {
723 Left->setType(TT_InlineASMSymbolicNameLSquare);
724 }
else if (IsCpp11AttributeSpecifier) {
725 Left->setType(TT_AttributeSquare);
726 if (!IsInnerSquare &&
Left->Previous)
727 Left->Previous->EndsCppAttributeGroup =
false;
728 }
else if (Style.isJavaScript() && Parent &&
729 Contexts.back().ContextKind == tok::l_brace &&
730 Parent->isOneOf(tok::l_brace, tok::comma)) {
731 Left->setType(TT_JsComputedPropertyName);
732 }
else if (IsCpp && Contexts.back().ContextKind == tok::l_brace &&
733 Parent && Parent->isOneOf(tok::l_brace, tok::comma)) {
734 Left->setType(TT_DesignatedInitializerLSquare);
735 }
else if (IsCSharpAttributeSpecifier) {
736 Left->setType(TT_AttributeSquare);
737 }
else if (CurrentToken->is(tok::r_square) && Parent &&
738 Parent->is(TT_TemplateCloser)) {
739 Left->setType(TT_ArraySubscriptLSquare);
740 }
else if (Style.isProto()) {
767 Left->setType(TT_ArrayInitializerLSquare);
768 if (!
Left->endsSequence(tok::l_square, tok::numeric_constant,
770 !
Left->endsSequence(tok::l_square, tok::numeric_constant,
772 !
Left->endsSequence(tok::l_square, tok::colon, TT_SelectorName)) {
773 Left->setType(TT_ProtoExtensionLSquare);
774 BindingIncrease = 10;
776 }
else if (!CppArrayTemplates && Parent &&
777 Parent->isOneOf(TT_BinaryOperator, TT_TemplateCloser, tok::at,
778 tok::comma, tok::l_paren, tok::l_square,
779 tok::question, tok::colon, tok::kw_return,
782 Left->setType(TT_ArrayInitializerLSquare);
784 BindingIncrease = 10;
785 Left->setType(TT_ArraySubscriptLSquare);
789 ScopedContextCreator ContextCreator(*
this, tok::l_square, BindingIncrease);
790 Contexts.back().IsExpression =
true;
791 if (Style.isJavaScript() && Parent && Parent->is(TT_JsTypeColon))
792 Contexts.back().IsExpression =
false;
794 Contexts.back().ColonIsObjCMethodExpr = StartsObjCMethodExpr;
795 Contexts.back().InCpp11AttributeSpecifier = IsCpp11AttributeSpecifier;
796 Contexts.back().InCSharpAttributeSpecifier = IsCSharpAttributeSpecifier;
798 while (CurrentToken) {
799 if (CurrentToken->is(tok::r_square)) {
800 if (IsCpp11AttributeSpecifier) {
801 CurrentToken->setType(TT_AttributeSquare);
803 CurrentToken->EndsCppAttributeGroup =
true;
805 if (IsCSharpAttributeSpecifier) {
806 CurrentToken->setType(TT_AttributeSquare);
807 }
else if (((CurrentToken->Next &&
808 CurrentToken->Next->is(tok::l_paren)) ||
809 (CurrentToken->Previous &&
810 CurrentToken->Previous->Previous == Left)) &&
811 Left->is(TT_ObjCMethodExpr)) {
816 StartsObjCMethodExpr =
false;
817 Left->setType(TT_Unknown);
819 if (StartsObjCMethodExpr && CurrentToken->Previous != Left) {
820 CurrentToken->setType(TT_ObjCMethodExpr);
823 if (!ColonFound && CurrentToken->Previous &&
824 CurrentToken->Previous->is(TT_Unknown) &&
825 canBeObjCSelectorComponent(*CurrentToken->Previous)) {
826 CurrentToken->Previous->setType(TT_SelectorName);
831 if (Parent && Parent->is(TT_PointerOrReference))
832 Parent->overwriteFixedType(TT_BinaryOperator);
834 Left->MatchingParen = CurrentToken;
835 CurrentToken->MatchingParen =
Left;
840 if (!Contexts.back().FirstObjCSelectorName) {
843 Previous->ObjCSelectorNameParts = 1;
844 Contexts.back().FirstObjCSelectorName =
Previous;
847 Left->ParameterCount =
848 Contexts.back().FirstObjCSelectorName->ObjCSelectorNameParts;
850 if (Contexts.back().FirstObjCSelectorName) {
851 Contexts.back().FirstObjCSelectorName->LongestObjCSelectorName =
852 Contexts.back().LongestObjCSelectorName;
853 if (
Left->BlockParameterCount > 1)
854 Contexts.back().FirstObjCSelectorName->LongestObjCSelectorName = 0;
856 if (Style.isTableGen() &&
Left->is(TT_TableGenListOpener))
857 CurrentToken->setType(TT_TableGenListCloser);
861 if (CurrentToken->isOneOf(tok::r_paren, tok::r_brace))
863 if (CurrentToken->is(tok::colon)) {
864 if (IsCpp11AttributeSpecifier &&
865 CurrentToken->endsSequence(tok::colon, tok::identifier,
869 CurrentToken->setType(TT_AttributeColon);
870 }
else if (!Style.isVerilog() && !Line.InPragmaDirective &&
871 Left->isOneOf(TT_ArraySubscriptLSquare,
872 TT_DesignatedInitializerLSquare)) {
873 Left->setType(TT_ObjCMethodExpr);
874 StartsObjCMethodExpr =
true;
875 Contexts.back().ColonIsObjCMethodExpr =
true;
876 if (Parent && Parent->is(tok::r_paren)) {
878 Parent->setType(TT_CastRParen);
883 if (CurrentToken->is(tok::comma) &&
Left->is(TT_ObjCMethodExpr) &&
885 Left->setType(TT_ArrayInitializerLSquare);
888 if (Style.isTableGen()) {
889 if (CurrentToken->isOneOf(tok::comma, tok::minus, tok::ellipsis)) {
895 if (!parseTableGenValue())
898 updateParameterCount(Left,
Tok);
903 updateParameterCount(Left,
Tok);
908 void skipToNextNonComment() {
910 while (CurrentToken && CurrentToken->is(tok::comment))
919 bool parseTableGenValue(
bool ParseNameMode =
false) {
922 while (CurrentToken->is(tok::comment))
924 if (!parseTableGenSimpleValue())
929 if (CurrentToken->is(tok::hash)) {
930 if (CurrentToken->Next &&
931 CurrentToken->Next->isOneOf(tok::colon, tok::semi, tok::l_brace)) {
934 CurrentToken->setType(TT_TableGenTrailingPasteOperator);
939 skipToNextNonComment();
940 HashTok->setType(TT_Unknown);
941 if (!parseTableGenValue(ParseNameMode))
948 if (ParseNameMode && CurrentToken->is(tok::l_brace))
951 if (CurrentToken->isOneOf(tok::l_brace, tok::l_square, tok::period)) {
952 CurrentToken->setType(TT_TableGenValueSuffix);
954 skipToNextNonComment();
955 if (Suffix->is(tok::l_square))
956 return parseSquare();
957 if (Suffix->is(tok::l_brace)) {
958 Scopes.push_back(getScopeType(*Suffix));
968 bool tryToParseTableGenTokVar() {
971 if (CurrentToken->is(tok::identifier) &&
972 CurrentToken->TokenText.front() ==
'$') {
973 skipToNextNonComment();
981 bool parseTableGenDAGArg(
bool AlignColon =
false) {
982 if (tryToParseTableGenTokVar())
984 if (parseTableGenValue()) {
985 if (CurrentToken && CurrentToken->is(tok::colon)) {
987 CurrentToken->setType(TT_TableGenDAGArgListColonToAlign);
989 CurrentToken->setType(TT_TableGenDAGArgListColon);
990 skipToNextNonComment();
991 return tryToParseTableGenTokVar();
1002 auto &Opes = Style.TableGenBreakingDAGArgOperators;
1007 if (
Tok.isNot(tok::identifier) ||
1008 Tok.isOneOf(TT_TableGenBangOperator, TT_TableGenCondOperator)) {
1012 if (!
Tok.Next ||
Tok.Next->is(tok::colon))
1014 return llvm::is_contained(Opes,
Tok.TokenText.str());
1019 bool parseTableGenDAGArgAndList(
FormatToken *Opener) {
1021 if (!parseTableGenDAGArg())
1023 bool BreakInside =
false;
1024 if (Style.TableGenBreakInsideDAGArg != FormatStyle::DAS_DontBreak) {
1027 if (isTableGenDAGArgBreakingOperator(*FirstTok)) {
1030 Opener->setType(TT_TableGenDAGArgOpenerToBreak);
1031 if (FirstTok->isOneOf(TT_TableGenBangOperator,
1032 TT_TableGenCondOperator)) {
1035 CurrentToken->Previous->setType(TT_TableGenDAGArgOperatorToBreak);
1036 }
else if (FirstTok->is(tok::identifier)) {
1037 if (Style.TableGenBreakInsideDAGArg == FormatStyle::DAS_BreakAll)
1038 FirstTok->setType(TT_TableGenDAGArgOperatorToBreak);
1040 FirstTok->setType(TT_TableGenDAGArgOperatorID);
1045 return parseTableGenDAGArgList(Opener, BreakInside);
1050 bool parseTableGenDAGArgList(
FormatToken *Opener,
bool BreakInside) {
1051 ScopedContextCreator ContextCreator(*
this, tok::l_paren, 0);
1052 Contexts.back().IsTableGenDAGArgList =
true;
1053 bool FirstDAGArgListElm =
true;
1054 while (CurrentToken) {
1055 if (!FirstDAGArgListElm && CurrentToken->is(tok::comma)) {
1056 CurrentToken->setType(BreakInside ? TT_TableGenDAGArgListCommaToBreak
1057 : TT_TableGenDAGArgListComma);
1058 skipToNextNonComment();
1060 if (CurrentToken && CurrentToken->is(tok::r_paren)) {
1061 CurrentToken->setType(TT_TableGenDAGArgCloser);
1062 Opener->MatchingParen = CurrentToken;
1063 CurrentToken->MatchingParen = Opener;
1064 skipToNextNonComment();
1067 if (!parseTableGenDAGArg(
1069 Style.AlignConsecutiveTableGenBreakingDAGArgColons.Enabled)) {
1072 FirstDAGArgListElm =
false;
1077 bool parseTableGenSimpleValue() {
1078 assert(Style.isTableGen());
1082 skipToNextNonComment();
1084 if (
Tok->isOneOf(tok::numeric_constant, tok::string_literal,
1085 TT_TableGenMultiLineString, tok::kw_true, tok::kw_false,
1086 tok::question, tok::kw_int)) {
1090 if (
Tok->is(tok::l_brace)) {
1091 Scopes.push_back(getScopeType(*
Tok));
1092 return parseBrace();
1095 if (
Tok->is(tok::l_square)) {
1096 Tok->setType(TT_TableGenListOpener);
1099 if (
Tok->is(tok::less)) {
1100 CurrentToken->setType(TT_TemplateOpener);
1101 return parseAngle();
1107 if (
Tok->is(tok::l_paren)) {
1108 Tok->setType(TT_TableGenDAGArgOpener);
1110 if (Contexts.back().IsTableGenDAGArgList)
1111 Tok->SpacesRequiredBefore = 1;
1112 return parseTableGenDAGArgAndList(
Tok);
1115 if (
Tok->is(TT_TableGenBangOperator)) {
1116 if (CurrentToken && CurrentToken->is(tok::less)) {
1117 CurrentToken->setType(TT_TemplateOpener);
1118 skipToNextNonComment();
1122 if (!CurrentToken || CurrentToken->isNot(tok::l_paren))
1126 Contexts.back().IsTableGenBangOpe =
true;
1127 bool Result = parseParens();
1128 Contexts.back().IsTableGenBangOpe =
false;
1132 if (
Tok->is(TT_TableGenCondOperator)) {
1133 if (!CurrentToken || CurrentToken->isNot(tok::l_paren))
1136 return parseParens();
1141 if (
Tok->is(tok::identifier)) {
1143 if (CurrentToken && CurrentToken->is(tok::less)) {
1144 CurrentToken->setType(TT_TemplateOpener);
1145 skipToNextNonComment();
1146 return parseAngle();
1154 bool couldBeInStructArrayInitializer()
const {
1155 if (Contexts.size() < 2)
1159 const auto End = std::next(Contexts.rbegin(), 2);
1160 auto Last = Contexts.rbegin();
1163 if (
Last->ContextKind == tok::l_brace)
1165 return Depth == 2 &&
Last->ContextKind != tok::l_brace;
1172 assert(CurrentToken->Previous);
1173 FormatToken &OpeningBrace = *CurrentToken->Previous;
1174 assert(OpeningBrace.is(tok::l_brace));
1175 OpeningBrace.ParentBracket = Contexts.back().ContextKind;
1177 if (Contexts.back().CaretFound)
1178 OpeningBrace.overwriteFixedType(TT_ObjCBlockLBrace);
1179 Contexts.back().CaretFound =
false;
1181 ScopedContextCreator ContextCreator(*
this, tok::l_brace, 1);
1182 Contexts.back().ColonIsDictLiteral =
true;
1184 Contexts.back().IsExpression =
true;
1185 if (Style.isJavaScript() && OpeningBrace.Previous &&
1186 OpeningBrace.Previous->is(TT_JsTypeColon)) {
1187 Contexts.back().IsExpression =
false;
1189 if (Style.isVerilog() &&
1190 (!OpeningBrace.getPreviousNonComment() ||
1191 OpeningBrace.getPreviousNonComment()->isNot(Keywords.kw_apostrophe))) {
1192 Contexts.back().VerilogMayBeConcatenation =
true;
1194 if (Style.isTableGen())
1195 Contexts.back().ColonIsDictLiteral =
false;
1197 unsigned CommaCount = 0;
1198 while (CurrentToken) {
1199 if (CurrentToken->is(tok::r_brace)) {
1200 assert(!Scopes.empty());
1201 assert(Scopes.back() == getScopeType(OpeningBrace));
1203 assert(OpeningBrace.Optional == CurrentToken->Optional);
1204 OpeningBrace.MatchingParen = CurrentToken;
1205 CurrentToken->MatchingParen = &OpeningBrace;
1206 if (Style.AlignArrayOfStructures != FormatStyle::AIAS_None) {
1207 if (OpeningBrace.ParentBracket == tok::l_brace &&
1208 couldBeInStructArrayInitializer() && CommaCount > 0) {
1209 Contexts.back().ContextType = Context::StructArrayInitializer;
1215 if (CurrentToken->isOneOf(tok::r_paren, tok::r_square))
1217 updateParameterCount(&OpeningBrace, CurrentToken);
1218 if (CurrentToken->isOneOf(tok::colon, tok::l_brace, tok::less)) {
1220 if (
Previous->is(TT_JsTypeOptionalQuestion))
1222 if ((CurrentToken->is(tok::colon) && !Style.isTableGen() &&
1223 (!Contexts.back().ColonIsDictLiteral || !IsCpp)) ||
1225 OpeningBrace.setType(TT_DictLiteral);
1226 if (
Previous->Tok.getIdentifierInfo() ||
1227 Previous->is(tok::string_literal)) {
1228 Previous->setType(TT_SelectorName);
1231 if (CurrentToken->is(tok::colon) && OpeningBrace.is(TT_Unknown) &&
1232 !Style.isTableGen()) {
1233 OpeningBrace.setType(TT_DictLiteral);
1234 }
else if (Style.isJavaScript()) {
1235 OpeningBrace.overwriteFixedType(TT_DictLiteral);
1238 if (CurrentToken->is(tok::comma)) {
1239 if (Style.isJavaScript())
1240 OpeningBrace.overwriteFixedType(TT_DictLiteral);
1243 if (!consumeToken())
1253 if (Current->is(tok::l_brace) && Current->is(
BK_Block))
1254 ++
Left->BlockParameterCount;
1255 if (Current->is(tok::comma)) {
1256 ++
Left->ParameterCount;
1258 Left->Role.reset(
new CommaSeparatedList(Style));
1259 Left->Role->CommaFound(Current);
1260 }
else if (
Left->ParameterCount == 0 && Current->isNot(tok::comment)) {
1261 Left->ParameterCount = 1;
1265 bool parseConditional() {
1266 while (CurrentToken) {
1267 if (CurrentToken->is(tok::colon) && CurrentToken->is(TT_Unknown)) {
1268 CurrentToken->setType(TT_ConditionalExpr);
1272 if (!consumeToken())
1278 bool parseTemplateDeclaration() {
1279 if (!CurrentToken || CurrentToken->isNot(tok::less))
1282 CurrentToken->setType(TT_TemplateOpener);
1285 TemplateDeclarationDepth++;
1286 const bool WellFormed = parseAngle();
1287 TemplateDeclarationDepth--;
1291 if (CurrentToken && TemplateDeclarationDepth == 0)
1292 CurrentToken->Previous->ClosesTemplateDeclaration =
true;
1297 bool consumeToken() {
1299 const auto *Prev = CurrentToken->getPreviousNonComment();
1300 if (Prev && Prev->is(tok::r_square) && Prev->is(TT_AttributeSquare) &&
1301 CurrentToken->isOneOf(tok::kw_if, tok::kw_switch, tok::kw_case,
1302 tok::kw_default, tok::kw_for, tok::kw_while) &&
1304 CurrentToken->MustBreakBefore =
true;
1311 if (
Tok->is(TT_VerilogTableItem))
1314 if (
Tok->is(TT_TableGenMultiLineString))
1316 auto *Prev =
Tok->getPreviousNonComment();
1317 auto *
Next =
Tok->getNextNonComment();
1318 switch (
bool IsIf =
false;
Tok->Tok.getKind()) {
1321 if (!Prev && Line.MustBeDeclaration)
1322 Tok->setType(TT_ObjCMethodSpecifier);
1329 if (
Tok->isTypeFinalized())
1332 if (Style.isJavaScript()) {
1333 if (Contexts.back().ColonIsForRangeExpr ||
1334 (Contexts.size() == 1 &&
1335 Line.First->isNoneOf(tok::kw_enum, tok::kw_case)) ||
1336 Contexts.back().ContextKind == tok::l_paren ||
1337 Contexts.back().ContextKind == tok::l_square ||
1338 (!Contexts.back().IsExpression &&
1339 Contexts.back().ContextKind == tok::l_brace) ||
1340 (Contexts.size() == 1 &&
1341 Line.MustBeDeclaration)) {
1342 Contexts.back().IsExpression =
false;
1343 Tok->setType(TT_JsTypeColon);
1346 }
else if (Style.isCSharp()) {
1347 if (Contexts.back().InCSharpAttributeSpecifier) {
1348 Tok->setType(TT_AttributeColon);
1351 if (Contexts.back().ContextKind == tok::l_paren) {
1352 Tok->setType(TT_CSharpNamedArgumentColon);
1355 }
else if (Style.isVerilog() &&
Tok->isNot(TT_BinaryOperator)) {
1358 if (Keywords.isVerilogEnd(*Prev) || Keywords.isVerilogBegin(*Prev)) {
1359 Tok->setType(TT_VerilogBlockLabelColon);
1360 }
else if (Contexts.back().ContextKind == tok::l_square) {
1361 Tok->setType(TT_BitFieldColon);
1362 }
else if (Contexts.back().ColonIsDictLiteral) {
1363 Tok->setType(TT_DictLiteral);
1364 }
else if (Contexts.size() == 1) {
1368 Tok->setType(TT_CaseLabelColon);
1369 if (Line.Level > 1 || (!Line.InPPDirective && Line.Level > 0))
1374 if (Line.First->isOneOf(Keywords.kw_module, Keywords.kw_import) ||
1375 Line.First->startsSequence(tok::kw_export, Keywords.kw_module) ||
1376 Line.First->startsSequence(tok::kw_export, Keywords.kw_import)) {
1377 Tok->setType(TT_ModulePartitionColon);
1378 }
else if (Line.First->is(tok::kw_asm)) {
1379 Tok->setType(TT_InlineASMColon);
1380 }
else if (Contexts.back().ColonIsDictLiteral || Style.isProto()) {
1381 Tok->setType(TT_DictLiteral);
1382 if (Style.isTextProto())
1383 Prev->setType(TT_SelectorName);
1384 }
else if (Contexts.back().ColonIsObjCMethodExpr ||
1385 Line.startsWith(TT_ObjCMethodSpecifier)) {
1386 Tok->setType(TT_ObjCMethodExpr);
1387 const auto *PrevPrev = Prev->Previous;
1390 bool UnknownIdentifierInMethodDeclaration =
1391 Line.startsWith(TT_ObjCMethodSpecifier) &&
1392 Prev->is(tok::identifier) && Prev->is(TT_Unknown);
1395 !(PrevPrev->is(TT_CastRParen) ||
1396 (PrevPrev->is(TT_ObjCMethodExpr) && PrevPrev->is(tok::colon))) ||
1397 PrevPrev->is(tok::r_square) ||
1398 Contexts.back().LongestObjCSelectorName == 0 ||
1399 UnknownIdentifierInMethodDeclaration) {
1400 Prev->setType(TT_SelectorName);
1401 if (!Contexts.back().FirstObjCSelectorName)
1402 Contexts.back().FirstObjCSelectorName = Prev;
1403 else if (Prev->ColumnWidth > Contexts.back().LongestObjCSelectorName)
1404 Contexts.back().LongestObjCSelectorName = Prev->ColumnWidth;
1405 Prev->ParameterIndex =
1406 Contexts.back().FirstObjCSelectorName->ObjCSelectorNameParts;
1407 ++Contexts.back().FirstObjCSelectorName->ObjCSelectorNameParts;
1409 }
else if (Contexts.back().ColonIsForRangeExpr) {
1410 Tok->setType(TT_RangeBasedForLoopColon);
1411 for (
auto *Token = Prev;
1412 Token && Token->isNoneOf(tok::semi, tok::l_paren);
1413 Token = Token->Previous) {
1414 if (Token->isPointerOrReference())
1415 Token->setFinalizedType(TT_PointerOrReference);
1417 }
else if (Contexts.back().ContextType == Context::C11GenericSelection) {
1418 Tok->setType(TT_GenericSelectionColon);
1419 if (Prev->isPointerOrReference())
1420 Prev->setFinalizedType(TT_PointerOrReference);
1421 }
else if ((CurrentToken && CurrentToken->is(tok::numeric_constant)) ||
1422 (Prev->is(TT_StartOfName) && !Scopes.empty() &&
1424 Tok->setType(TT_BitFieldColon);
1425 }
else if (Contexts.size() == 1 &&
1426 Line.getFirstNonComment()->isNoneOf(tok::kw_enum, tok::kw_case,
1428 !Line.startsWith(tok::kw_typedef, tok::kw_enum)) {
1429 if (Prev->isOneOf(tok::r_paren, tok::kw_noexcept) ||
1430 Prev->ClosesRequiresClause) {
1431 Tok->setType(TT_CtorInitializerColon);
1432 }
else if (Prev->is(tok::kw_try)) {
1434 FormatToken *PrevPrev = Prev->getPreviousNonComment();
1437 if (PrevPrev && PrevPrev->isOneOf(tok::r_paren, tok::kw_noexcept))
1438 Tok->setType(TT_CtorInitializerColon);
1440 Tok->setType(TT_InheritanceColon);
1441 if (Prev->isAccessSpecifierKeyword())
1444 }
else if (canBeObjCSelectorComponent(*Prev) &&
Next &&
1445 (
Next->isOneOf(tok::r_paren, tok::comma) ||
1446 (canBeObjCSelectorComponent(*
Next) &&
Next->Next &&
1447 Next->Next->is(tok::colon)))) {
1450 Tok->setType(TT_ObjCSelector);
1457 if (Style.isJavaScript() && !Contexts.back().IsExpression)
1458 Tok->setType(TT_JsTypeOperator);
1461 if (Style.isTableGen()) {
1463 if (!parseTableGenValue())
1465 if (CurrentToken && CurrentToken->is(Keywords.kw_then))
1470 CurrentToken->isOneOf(tok::kw_constexpr, tok::identifier)) {
1476 if (CurrentToken && CurrentToken->is(tok::l_paren)) {
1478 if (!parseParens(IsIf))
1483 if (Style.isJavaScript()) {
1485 if ((Prev && Prev->is(tok::period)) || (
Next &&
Next->is(tok::colon)))
1488 if (CurrentToken && CurrentToken->is(Keywords.kw_await))
1491 if (IsCpp && CurrentToken && CurrentToken->is(tok::kw_co_await))
1493 Contexts.back().ColonIsForRangeExpr =
true;
1494 if (!CurrentToken || CurrentToken->isNot(tok::l_paren))
1505 if (Prev && Prev->is(tok::r_paren) && Prev->MatchingParen &&
1506 Prev->MatchingParen->is(TT_OverloadedOperatorLParen)) {
1507 Prev->setType(TT_OverloadedOperator);
1508 Prev->MatchingParen->setType(TT_OverloadedOperator);
1509 Tok->setType(TT_OverloadedOperatorLParen);
1512 if (Style.isVerilog()) {
1518 auto IsInstancePort = [&]() {
1527 if (!Prev || !(PrevPrev = Prev->getPreviousNonComment()))
1530 if (Keywords.isVerilogIdentifier(*Prev) &&
1531 Keywords.isVerilogIdentifier(*PrevPrev)) {
1535 if (Prev->is(Keywords.kw_verilogHash) &&
1536 Keywords.isVerilogIdentifier(*PrevPrev)) {
1540 if (Keywords.isVerilogIdentifier(*Prev) && PrevPrev->is(tok::r_paren))
1543 if (Keywords.isVerilogIdentifier(*Prev) && PrevPrev->is(tok::comma)) {
1544 const FormatToken *PrevParen = PrevPrev->getPreviousNonComment();
1545 if (PrevParen && PrevParen->is(tok::r_paren) &&
1546 PrevParen->MatchingParen &&
1547 PrevParen->MatchingParen->is(TT_VerilogInstancePortLParen)) {
1554 if (IsInstancePort())
1555 Tok->setType(TT_VerilogInstancePortLParen);
1560 if (Line.MustBeDeclaration && Contexts.size() == 1 &&
1561 !Contexts.back().IsExpression && !Line.startsWith(TT_ObjCProperty) &&
1562 !Line.startsWith(tok::l_paren) &&
1563 Tok->isNoneOf(TT_TypeDeclarationParen, TT_RequiresExpressionLParen)) {
1565 (!Prev->isAttribute() &&
1566 Prev->isNoneOf(TT_RequiresClause, TT_LeadingJavaAnnotation,
1567 TT_BinaryOperator))) {
1568 Line.MightBeFunctionDecl =
true;
1569 Tok->MightBeFunctionDeclParen =
true;
1574 if (Style.isTableGen())
1575 Tok->setType(TT_TableGenListOpener);
1581 if (
Tok->is(TT_RequiresExpressionLBrace))
1583 }
else if (Style.isTextProto()) {
1584 if (Prev && Prev->isNot(TT_DictLiteral))
1585 Prev->setType(TT_SelectorName);
1587 Scopes.push_back(getScopeType(*
Tok));
1593 Tok->setType(TT_TemplateOpener);
1599 if (Style.isTextProto() ||
1600 (Style.Language == FormatStyle::LK_Proto && Prev &&
1601 Prev->isOneOf(TT_SelectorName, TT_DictLiteral))) {
1602 Tok->setType(TT_DictLiteral);
1603 if (Prev && Prev->isNot(TT_DictLiteral))
1604 Prev->setType(TT_SelectorName);
1606 if (Style.isTableGen())
1607 Tok->setType(TT_TemplateOpener);
1609 Tok->setType(TT_BinaryOperator);
1610 NonTemplateLess.insert(
Tok);
1620 if (!Scopes.empty())
1627 if (!Style.isTextProto() &&
Tok->is(TT_Unknown))
1628 Tok->setType(TT_BinaryOperator);
1629 if (Prev && Prev->is(TT_TemplateCloser))
1630 Tok->SpacesRequiredBefore = 1;
1632 case tok::kw_operator:
1633 if (Style.isProto())
1636 if (IsCpp && CurrentToken) {
1637 const auto *Info = CurrentToken->Tok.getIdentifierInfo();
1639 if (Info && !(CurrentToken->isPlacementOperator() ||
1640 CurrentToken->is(tok::kw_co_await) ||
1641 Info->isCPlusPlusOperatorKeyword())) {
1643 if (CurrentToken->startsSequence(tok::kw_decltype, tok::l_paren,
1644 tok::kw_auto, tok::r_paren)) {
1646 LParen = CurrentToken->Next->Next->Next->Next;
1649 for (LParen = CurrentToken->Next;
1650 LParen && LParen->isNot(tok::l_paren); LParen = LParen->Next) {
1651 if (LParen->isPointerOrReference())
1652 LParen->setFinalizedType(TT_PointerOrReference);
1655 if (LParen && LParen->is(tok::l_paren)) {
1656 if (!Contexts.back().IsExpression) {
1657 Tok->setFinalizedType(TT_FunctionDeclarationName);
1658 LParen->setFinalizedType(TT_FunctionDeclarationLParen);
1664 while (CurrentToken &&
1665 CurrentToken->isNoneOf(tok::l_paren, tok::semi, tok::r_paren)) {
1666 if (CurrentToken->isOneOf(tok::star, tok::amp))
1667 CurrentToken->setType(TT_PointerOrReference);
1668 auto Next = CurrentToken->getNextNonComment();
1671 if (
Next->is(tok::less))
1677 auto Previous = CurrentToken->getPreviousNonComment();
1679 if (CurrentToken->is(tok::comma) &&
Previous->isNot(tok::kw_operator))
1681 if (
Previous->isOneOf(TT_BinaryOperator, TT_UnaryOperator, tok::comma,
1683 Previous->isPointerOrReference() ||
1685 Previous->TokenText.starts_with(
"\"\"")) {
1686 Previous->setType(TT_OverloadedOperator);
1687 if (CurrentToken->isOneOf(tok::less, tok::greater))
1691 if (CurrentToken && CurrentToken->is(tok::l_paren))
1692 CurrentToken->setType(TT_OverloadedOperatorLParen);
1693 if (CurrentToken && CurrentToken->Previous->is(TT_BinaryOperator))
1694 CurrentToken->Previous->setType(TT_OverloadedOperator);
1697 if (Style.isJavaScript() &&
Next &&
1698 Next->isOneOf(tok::semi, tok::comma, tok::colon, tok::r_paren,
1699 tok::r_brace, tok::r_square)) {
1704 Tok->setType(TT_JsTypeOptionalQuestion);
1709 if (Line.MustBeDeclaration && !Contexts.back().IsExpression &&
1710 Style.isJavaScript()) {
1713 if (Style.isCSharp()) {
1716 if (
Next && (
Next->isOneOf(tok::r_paren, tok::greater) ||
1717 Next->startsSequence(tok::identifier, tok::semi) ||
1718 Next->startsSequence(tok::identifier, tok::equal))) {
1719 Tok->setType(TT_CSharpNullable);
1728 if (!Contexts.back().IsExpression && Line.MustBeDeclaration &&
1729 (!
Next ||
Next->isNoneOf(tok::identifier, tok::string_literal) ||
1730 !
Next->Next ||
Next->Next->isNoneOf(tok::colon, tok::question))) {
1731 Tok->setType(TT_CSharpNullable);
1737 case tok::kw_template:
1738 parseTemplateDeclaration();
1741 switch (Contexts.back().ContextType) {
1742 case Context::CtorInitializer:
1743 Tok->setType(TT_CtorInitializerComma);
1745 case Context::InheritanceList:
1746 Tok->setType(TT_InheritanceComma);
1748 case Context::VerilogInstancePortList:
1749 Tok->setType(TT_VerilogInstancePortComma);
1752 if (Style.isVerilog() && Contexts.size() == 1 &&
1753 Line.startsWith(Keywords.kw_assign)) {
1754 Tok->setFinalizedType(TT_VerilogAssignComma);
1755 }
else if (Contexts.back().FirstStartOfName &&
1756 (Contexts.size() == 1 || startsWithInitStatement(Line))) {
1757 Contexts.back().FirstStartOfName->PartOfMultiVariableDeclStmt =
true;
1758 Line.IsMultiVariableDeclStmt =
true;
1762 if (Contexts.back().ContextType == Context::ForEachMacro)
1763 Contexts.back().IsExpression =
true;
1765 case tok::kw_default:
1767 if (Style.isVerilog() && Keywords.isVerilogEndOfLabel(*
Tok) &&
1768 (Line.Level > 1 || (!Line.InPPDirective && Line.Level > 0))) {
1772 case tok::identifier:
1773 if (
Tok->isOneOf(Keywords.kw___has_include,
1774 Keywords.kw___has_include_next)) {
1778 if (
Next &&
Next->is(tok::l_paren) && Prev &&
1779 Prev->isOneOf(tok::kw___cdecl, tok::kw___stdcall,
1780 tok::kw___fastcall, tok::kw___thiscall,
1781 tok::kw___regcall, tok::kw___vectorcall)) {
1782 Tok->setFinalizedType(TT_FunctionDeclarationName);
1783 Next->setFinalizedType(TT_FunctionDeclarationLParen);
1785 }
else if (Style.isCSharp()) {
1786 if (
Tok->is(Keywords.kw_where) &&
Next &&
Next->isNot(tok::l_paren)) {
1787 Tok->setType(TT_CSharpGenericTypeConstraint);
1788 parseCSharpGenericTypeConstraint();
1790 Line.IsContinuation =
true;
1792 }
else if (Style.isTableGen()) {
1793 if (
Tok->is(Keywords.kw_assert)) {
1794 if (!parseTableGenValue())
1796 }
else if (
Tok->isOneOf(Keywords.kw_def, Keywords.kw_defm) &&
1797 (!
Next ||
Next->isNoneOf(tok::colon, tok::l_brace))) {
1799 if (!parseTableGenValue(
true))
1803 if (Style.AllowBreakBeforeQtProperty &&
1804 Contexts.back().ContextType == Context::QtProperty &&
1805 Tok->isQtProperty()) {
1806 Tok->setFinalizedType(TT_QtProperty);
1810 if (
Tok->isNot(TT_LambdaArrow) && Prev && Prev->is(tok::kw_noexcept))
1811 Tok->setType(TT_TrailingReturnArrow);
1815 if (Style.isTableGen() && !parseTableGenValue())
1824 void parseCSharpGenericTypeConstraint() {
1825 int OpenAngleBracketsCount = 0;
1826 while (CurrentToken) {
1827 if (CurrentToken->is(tok::less)) {
1829 CurrentToken->setType(TT_TemplateOpener);
1830 ++OpenAngleBracketsCount;
1832 }
else if (CurrentToken->is(tok::greater)) {
1833 CurrentToken->setType(TT_TemplateCloser);
1834 --OpenAngleBracketsCount;
1836 }
else if (CurrentToken->is(tok::comma) && OpenAngleBracketsCount == 0) {
1839 CurrentToken->setType(TT_CSharpGenericTypeConstraintComma);
1841 }
else if (CurrentToken->is(Keywords.kw_where)) {
1842 CurrentToken->setType(TT_CSharpGenericTypeConstraint);
1844 }
else if (CurrentToken->is(tok::colon)) {
1845 CurrentToken->setType(TT_CSharpGenericTypeConstraintColon);
1853 void parseIncludeDirective() {
1854 if (CurrentToken && CurrentToken->is(tok::less)) {
1856 while (CurrentToken) {
1859 if (CurrentToken->isNot(tok::comment) &&
1860 !CurrentToken->TokenText.starts_with(
"//")) {
1861 CurrentToken->setType(TT_ImplicitStringLiteral);
1868 void parseWarningOrError() {
1873 while (CurrentToken) {
1874 CurrentToken->setType(TT_ImplicitStringLiteral);
1879 void parsePragma() {
1882 CurrentToken->isOneOf(Keywords.kw_mark, Keywords.kw_option,
1883 Keywords.kw_region)) {
1884 bool IsMarkOrRegion =
1885 CurrentToken->isOneOf(Keywords.kw_mark, Keywords.kw_region);
1888 while (CurrentToken) {
1889 if (IsMarkOrRegion || CurrentToken->Previous->is(TT_BinaryOperator))
1890 CurrentToken->setType(TT_ImplicitStringLiteral);
1896 void parseHasInclude() {
1897 if (!CurrentToken || CurrentToken->isNot(tok::l_paren))
1900 parseIncludeDirective();
1904 LineType parsePreprocessorDirective() {
1905 bool IsFirstToken = CurrentToken->IsFirst;
1911 if (Style.isJavaScript() && IsFirstToken) {
1915 while (CurrentToken) {
1917 CurrentToken->setType(TT_ImplicitStringLiteral);
1923 if (CurrentToken->is(tok::numeric_constant)) {
1924 CurrentToken->SpacesRequiredBefore = 1;
1929 if (!CurrentToken->Tok.getIdentifierInfo())
1933 if (Style.isVerilog() && !Keywords.isVerilogPPDirective(*CurrentToken))
1935 switch (CurrentToken->Tok.getIdentifierInfo()->getPPKeywordID()) {
1936 case tok::pp_include:
1937 case tok::pp_include_next:
1938 case tok::pp_import:
1940 parseIncludeDirective();
1944 case tok::pp_warning:
1945 parseWarningOrError();
1947 case tok::pp_pragma:
1952 Contexts.back().IsExpression =
true;
1955 CurrentToken->SpacesRequiredBefore = 1;
1961 while (CurrentToken) {
1964 if (
Tok->is(tok::l_paren)) {
1966 }
else if (
Tok->isOneOf(Keywords.kw___has_include,
1967 Keywords.kw___has_include_next)) {
1978 NonTemplateLess.clear();
1979 if (!Line.InMacroBody && CurrentToken->is(tok::hash)) {
1983 auto Type = parsePreprocessorDirective();
1991 IdentifierInfo *Info = CurrentToken->Tok.getIdentifierInfo();
1992 if ((Style.isJava() && CurrentToken->is(Keywords.kw_package)) ||
1993 (!Style.isVerilog() && Info &&
1994 Info->getPPKeywordID() == tok::pp_import && CurrentToken->Next &&
1995 CurrentToken->Next->isOneOf(tok::string_literal, tok::identifier,
1998 parseIncludeDirective();
2004 if (CurrentToken->is(tok::less) && Line.Last->is(tok::greater)) {
2005 parseIncludeDirective();
2011 if (Style.Language == FormatStyle::LK_Proto && Line.Level == 0 &&
2012 CurrentToken->isOneOf(Keywords.kw_option, Keywords.kw_package)) {
2014 if (CurrentToken && CurrentToken->is(tok::identifier)) {
2015 while (CurrentToken)
2021 bool KeywordVirtualFound =
false;
2022 bool ImportStatement =
false;
2025 if (Style.isJavaScript() && CurrentToken->is(Keywords.kw_import))
2026 ImportStatement =
true;
2028 while (CurrentToken) {
2029 if (CurrentToken->is(tok::kw_virtual))
2030 KeywordVirtualFound =
true;
2031 if (Style.isJavaScript()) {
2038 if (Line.First->is(tok::kw_export) &&
2039 CurrentToken->is(Keywords.kw_from) && CurrentToken->Next &&
2040 CurrentToken->Next->isStringLiteral()) {
2041 ImportStatement =
true;
2043 if (isClosureImportStatement(*CurrentToken))
2044 ImportStatement =
true;
2046 if (!consumeToken())
2054 if (KeywordVirtualFound)
2056 if (ImportStatement)
2059 if (Line.startsWith(TT_ObjCMethodSpecifier)) {
2060 if (Contexts.back().FirstObjCSelectorName) {
2061 Contexts.back().FirstObjCSelectorName->LongestObjCSelectorName =
2062 Contexts.back().LongestObjCSelectorName;
2067 for (
const auto &ctx : Contexts)
2068 if (ctx.ContextType == Context::StructArrayInitializer)
2078 return Tok.TokenText ==
"goog" &&
Tok.Next &&
Tok.Next->is(tok::period) &&
2080 (
Tok.Next->Next->TokenText ==
"module" ||
2081 Tok.Next->Next->TokenText ==
"provide" ||
2082 Tok.Next->Next->TokenText ==
"require" ||
2083 Tok.Next->Next->TokenText ==
"requireType" ||
2084 Tok.Next->Next->TokenText ==
"forwardDeclare") &&
2085 Tok.Next->Next->Next &&
Tok.Next->Next->Next->is(tok::l_paren);
2088 void resetTokenMetadata() {
2094 if (!CurrentToken->isTypeFinalized() &&
2095 CurrentToken->isNoneOf(
2096 TT_LambdaLSquare, TT_LambdaLBrace, TT_AttributeMacro, TT_IfMacro,
2097 TT_ForEachMacro, TT_TypenameMacro, TT_FunctionLBrace,
2098 TT_ImplicitStringLiteral, TT_InlineASMBrace, TT_FatArrow,
2099 TT_LambdaArrow, TT_NamespaceMacro, TT_OverloadedOperator,
2100 TT_RegexLiteral, TT_TemplateString, TT_ObjCStringLiteral,
2101 TT_UntouchableMacroFunc, TT_StatementAttributeLikeMacro,
2102 TT_FunctionLikeOrFreestandingMacro, TT_ClassLBrace, TT_EnumLBrace,
2103 TT_RecordLBrace, TT_StructLBrace, TT_UnionLBrace, TT_RequiresClause,
2104 TT_RequiresClauseInARequiresExpression, TT_RequiresExpression,
2105 TT_RequiresExpressionLParen, TT_RequiresExpressionLBrace,
2106 TT_CompoundRequirementLBrace, TT_BracedListLBrace,
2107 TT_FunctionLikeMacro)) {
2108 CurrentToken->setType(TT_Unknown);
2110 CurrentToken->Role.reset();
2111 CurrentToken->MatchingParen =
nullptr;
2112 CurrentToken->FakeLParens.clear();
2113 CurrentToken->FakeRParens = 0;
2120 CurrentToken->NestingLevel = Contexts.size() - 1;
2121 CurrentToken->BindingStrength = Contexts.back().BindingStrength;
2122 modifyContext(*CurrentToken);
2123 determineTokenType(*CurrentToken);
2124 CurrentToken = CurrentToken->Next;
2126 resetTokenMetadata();
2134 : ContextKind(ContextKind), BindingStrength(BindingStrength),
2135 IsExpression(IsExpression) {}
2138 unsigned BindingStrength;
2140 unsigned LongestObjCSelectorName = 0;
2141 bool ColonIsForRangeExpr =
false;
2142 bool ColonIsDictLiteral =
false;
2143 bool ColonIsObjCMethodExpr =
false;
2146 bool CanBeExpression =
true;
2147 bool CaretFound =
false;
2148 bool InCpp11AttributeSpecifier =
false;
2149 bool InCSharpAttributeSpecifier =
false;
2150 bool VerilogAssignmentFound =
false;
2153 bool VerilogMayBeConcatenation =
false;
2154 bool IsTableGenDAGArgList =
false;
2155 bool IsTableGenBangOpe =
false;
2156 bool IsTableGenCondOpe =
false;
2169 StructArrayInitializer,
2173 C11GenericSelection,
2176 VerilogInstancePortList,
2177 } ContextType = Unknown;
2182 struct ScopedContextCreator {
2183 AnnotatingParser &P;
2185 ScopedContextCreator(AnnotatingParser &P,
tok::TokenKind ContextKind,
2188 P.Contexts.push_back(Context(ContextKind,
2189 P.Contexts.back().BindingStrength + Increase,
2190 P.Contexts.back().IsExpression));
2193 ~ScopedContextCreator() {
2194 if (P.Style.AlignArrayOfStructures != FormatStyle::AIAS_None) {
2195 if (P.Contexts.back().ContextType == Context::StructArrayInitializer) {
2196 P.Contexts.pop_back();
2197 P.Contexts.back().ContextType = Context::StructArrayInitializer;
2201 P.Contexts.pop_back();
2206 auto AssignmentStartsExpression = [&]() {
2210 if (Line.First->isOneOf(tok::kw_using, tok::kw_return))
2212 if (Line.First->is(tok::kw_template)) {
2213 assert(Current.Previous);
2214 if (Current.Previous->is(tok::kw_operator)) {
2222 if (
Tok->isNot(TT_TemplateOpener)) {
2229 if (Contexts.back().ContextKind == tok::less) {
2230 assert(Current.Previous->Previous);
2231 return Current.Previous->Previous->isNoneOf(tok::kw_typename,
2235 Tok =
Tok->MatchingParen;
2238 Tok =
Tok->getNextNonComment();
2242 if (
Tok->isOneOf(tok::kw_class, tok::kw_enum, tok::kw_struct,
2252 if (Style.isJavaScript() &&
2253 (Line.startsWith(Keywords.kw_type, tok::identifier) ||
2254 Line.startsWith(tok::kw_export, Keywords.kw_type,
2255 tok::identifier))) {
2259 return !Current.Previous || Current.Previous->isNot(tok::kw_operator);
2262 if (AssignmentStartsExpression()) {
2263 Contexts.back().IsExpression =
true;
2264 if (!Line.startsWith(TT_UnaryOperator)) {
2267 Previous->Previous->isNoneOf(tok::comma, tok::semi);
2269 if (
Previous->isOneOf(tok::r_square, tok::r_paren, tok::greater)) {
2276 if (
Previous->isOneOf(TT_BinaryOperator, TT_UnaryOperator) &&
2278 Previous->Previous->isNot(tok::equal)) {
2279 Previous->setType(TT_PointerOrReference);
2283 }
else if (Current.is(tok::lessless) &&
2284 (!Current.Previous ||
2285 Current.Previous->isNot(tok::kw_operator))) {
2286 Contexts.back().IsExpression =
true;
2287 }
else if (Current.isOneOf(tok::kw_return, tok::kw_throw)) {
2288 Contexts.back().IsExpression =
true;
2289 }
else if (Current.is(TT_TrailingReturnArrow)) {
2290 Contexts.back().IsExpression =
false;
2291 }
else if (Current.isOneOf(TT_LambdaArrow, Keywords.kw_assert)) {
2292 Contexts.back().IsExpression = Style.isJava();
2293 }
else if (Current.Previous &&
2294 Current.Previous->is(TT_CtorInitializerColon)) {
2295 Contexts.back().IsExpression =
true;
2296 Contexts.back().ContextType = Context::CtorInitializer;
2297 }
else if (Current.Previous && Current.Previous->is(TT_InheritanceColon)) {
2298 Contexts.back().ContextType = Context::InheritanceList;
2299 }
else if (Current.isOneOf(tok::r_paren, tok::greater, tok::comma)) {
2303 Previous->setType(TT_PointerOrReference);
2305 if (Line.MustBeDeclaration &&
2306 Contexts.front().ContextType != Context::CtorInitializer) {
2307 Contexts.back().IsExpression =
false;
2309 }
else if (Current.is(tok::kw_new)) {
2310 Contexts.back().CanBeExpression =
false;
2311 }
else if (Current.is(tok::semi) ||
2312 (Current.is(tok::exclaim) && Current.Previous &&
2313 Current.Previous->isNot(tok::kw_operator))) {
2317 Contexts.back().IsExpression =
true;
2325 if (Current->is(tok::l_paren))
2327 if (Current->is(tok::r_paren))
2331 Current = Current->Next;
2336 static bool isDeductionGuide(
FormatToken &Current) {
2338 if (Current.Previous && Current.Previous->is(tok::r_paren) &&
2339 Current.startsSequence(tok::arrow, tok::identifier, tok::less)) {
2343 while (TemplateCloser) {
2345 if (TemplateCloser->is(tok::l_paren)) {
2347 TemplateCloser = untilMatchingParen(TemplateCloser);
2348 if (!TemplateCloser)
2351 if (TemplateCloser->is(tok::less))
2353 if (TemplateCloser->is(tok::greater))
2357 TemplateCloser = TemplateCloser->Next;
2361 if (TemplateCloser && TemplateCloser->Next &&
2362 TemplateCloser->Next->is(tok::semi) &&
2363 Current.Previous->MatchingParen) {
2367 Current.Previous->MatchingParen->Previous;
2369 return LeadingIdentifier &&
2370 LeadingIdentifier->TokenText == Current.Next->TokenText;
2377 if (Current.isNot(TT_Unknown)) {
2382 if ((Style.isJavaScript() || Style.isCSharp()) &&
2383 Current.is(tok::exclaim)) {
2384 if (Current.Previous) {
2386 Style.isJavaScript()
2387 ? Keywords.isJavaScriptIdentifier(
2388 *Current.Previous,
true)
2389 : Current.Previous->is(tok::identifier);
2391 Current.Previous->isOneOf(
2392 tok::kw_default, tok::kw_namespace, tok::r_paren, tok::r_square,
2393 tok::r_brace, tok::kw_false, tok::kw_true, Keywords.kw_type,
2394 Keywords.kw_get, Keywords.kw_init, Keywords.kw_set) ||
2395 Current.Previous->Tok.isLiteral()) {
2396 Current.setType(TT_NonNullAssertion);
2401 Current.Next->isOneOf(TT_BinaryOperator, Keywords.kw_as)) {
2402 Current.setType(TT_NonNullAssertion);
2410 if ((Style.isJavaScript() || Style.isJava()) &&
2411 Current.is(Keywords.kw_instanceof)) {
2412 Current.setType(TT_BinaryOperator);
2413 }
else if (isStartOfName(Current) &&
2414 (!Line.MightBeFunctionDecl || Current.NestingLevel != 0)) {
2415 Contexts.back().FirstStartOfName = &Current;
2416 Current.setType(TT_StartOfName);
2417 }
else if (Current.is(tok::semi)) {
2421 Contexts.back().FirstStartOfName =
nullptr;
2422 }
else if (Current.isOneOf(tok::kw_auto, tok::kw___auto_type)) {
2424 }
else if (Current.is(tok::arrow) && Style.isJava()) {
2425 Current.setType(TT_LambdaArrow);
2426 }
else if (Current.is(tok::arrow) && Style.isVerilog()) {
2428 Current.setType(TT_BinaryOperator);
2429 }
else if (Current.is(tok::arrow) && AutoFound &&
2430 Line.MightBeFunctionDecl && Current.NestingLevel == 0 &&
2431 Current.Previous->isNoneOf(tok::kw_operator, tok::identifier)) {
2433 Current.setType(TT_TrailingReturnArrow);
2434 }
else if (Current.is(tok::arrow) && Current.Previous &&
2435 Current.Previous->is(tok::r_brace) &&
2439 Current.setType(TT_TrailingReturnArrow);
2440 }
else if (isDeductionGuide(Current)) {
2442 Current.setType(TT_TrailingReturnArrow);
2443 }
else if (Current.isPointerOrReference()) {
2444 Current.setType(determineStarAmpUsage(
2446 Contexts.back().CanBeExpression && Contexts.back().IsExpression,
2447 Contexts.back().ContextType == Context::TemplateArgument));
2448 }
else if (Current.isOneOf(tok::minus, tok::plus, tok::caret) ||
2449 (Style.isVerilog() && Current.is(tok::pipe))) {
2450 Current.setType(determinePlusMinusCaretUsage(Current));
2451 if (Current.is(TT_UnaryOperator) && Current.is(tok::caret))
2452 Contexts.back().CaretFound =
true;
2453 }
else if (Current.isOneOf(tok::minusminus, tok::plusplus)) {
2454 Current.setType(determineIncrementUsage(Current));
2455 }
else if (Current.isOneOf(tok::exclaim, tok::tilde)) {
2456 Current.setType(TT_UnaryOperator);
2457 }
else if (Current.is(tok::question)) {
2458 if (Style.isJavaScript() && Line.MustBeDeclaration &&
2459 !Contexts.back().IsExpression) {
2462 Current.setType(TT_JsTypeOptionalQuestion);
2463 }
else if (Style.isTableGen()) {
2465 Current.setType(TT_Unknown);
2467 Current.setType(TT_ConditionalExpr);
2469 }
else if (Current.isBinaryOperator() &&
2470 (!Current.Previous || Current.Previous->isNot(tok::l_square)) &&
2471 (Current.isNot(tok::greater) && !Style.isTextProto())) {
2472 if (Style.isVerilog()) {
2473 if (Current.is(tok::lessequal) && Contexts.size() == 1 &&
2474 !Contexts.back().VerilogAssignmentFound) {
2478 Current.setFinalizedType(TT_BinaryOperator);
2481 Contexts.back().VerilogAssignmentFound =
true;
2483 Current.setType(TT_BinaryOperator);
2484 }
else if (Current.is(tok::comment)) {
2485 if (Current.TokenText.starts_with(
"/*")) {
2486 if (Current.TokenText.ends_with(
"*/")) {
2487 Current.setType(TT_BlockComment);
2491 Current.Tok.setKind(tok::unknown);
2494 Current.setType(TT_LineComment);
2496 }
else if (Current.is(tok::string_literal)) {
2497 if (Style.isVerilog() && Contexts.back().VerilogMayBeConcatenation &&
2498 Current.getPreviousNonComment() &&
2499 Current.getPreviousNonComment()->isOneOf(tok::comma, tok::l_brace) &&
2500 Current.getNextNonComment() &&
2501 Current.getNextNonComment()->isOneOf(tok::comma, tok::r_brace)) {
2502 Current.setType(TT_StringInConcatenation);
2504 }
else if (Current.is(tok::l_paren)) {
2505 if (lParenStartsCppCast(Current))
2506 Current.setType(TT_CppCastLParen);
2507 }
else if (Current.is(tok::r_paren)) {
2508 if (rParenEndsCast(Current))
2509 Current.setType(TT_CastRParen);
2510 if (Current.MatchingParen && Current.Next &&
2511 !Current.Next->isBinaryOperator() &&
2512 Current.Next->isNoneOf(
2513 tok::semi, tok::colon, tok::l_brace, tok::l_paren, tok::comma,
2514 tok::period, tok::arrow, tok::coloncolon, tok::kw_noexcept)) {
2515 if (
FormatToken *AfterParen = Current.MatchingParen->Next;
2516 AfterParen && AfterParen->isNot(tok::caret)) {
2518 if (
FormatToken *BeforeParen = Current.MatchingParen->Previous;
2519 BeforeParen && BeforeParen->is(tok::identifier) &&
2520 BeforeParen->isNot(TT_TypenameMacro) &&
2521 BeforeParen->TokenText == BeforeParen->TokenText.upper() &&
2522 (!BeforeParen->Previous ||
2523 BeforeParen->Previous->ClosesTemplateDeclaration ||
2524 BeforeParen->Previous->ClosesRequiresClause)) {
2525 Current.setType(TT_FunctionAnnotationRParen);
2529 }
else if (Current.is(tok::at) && Current.Next && !Style.isJavaScript() &&
2533 switch (Current.Next->Tok.getObjCKeywordID()) {
2534 case tok::objc_interface:
2535 case tok::objc_implementation:
2536 case tok::objc_protocol:
2537 Current.setType(TT_ObjCDecl);
2539 case tok::objc_property:
2540 Current.setType(TT_ObjCProperty);
2545 }
else if (Current.is(tok::period)) {
2546 FormatToken *PreviousNoComment = Current.getPreviousNonComment();
2547 if (PreviousNoComment &&
2548 PreviousNoComment->isOneOf(tok::comma, tok::l_brace)) {
2549 Current.setType(TT_DesignatedInitializerPeriod);
2550 }
else if (Style.isJava() && Current.Previous &&
2551 Current.Previous->isOneOf(TT_JavaAnnotation,
2552 TT_LeadingJavaAnnotation)) {
2553 Current.setType(Current.Previous->getType());
2555 }
else if (canBeObjCSelectorComponent(Current) &&
2558 Current.Previous && Current.Previous->is(TT_CastRParen) &&
2559 Current.Previous->MatchingParen &&
2560 Current.Previous->MatchingParen->Previous &&
2561 Current.Previous->MatchingParen->Previous->is(
2562 TT_ObjCMethodSpecifier)) {
2566 Current.setType(TT_SelectorName);
2567 }
else if (Current.isOneOf(tok::identifier, tok::kw_const, tok::kw_noexcept,
2568 tok::kw_requires) &&
2570 Current.Previous->isNoneOf(tok::equal, tok::at,
2571 TT_CtorInitializerComma,
2572 TT_CtorInitializerColon) &&
2573 Line.MightBeFunctionDecl && Contexts.size() == 1) {
2576 Current.setType(TT_TrailingAnnotation);
2577 }
else if ((Style.isJava() || Style.isJavaScript()) && Current.Previous) {
2578 if (Current.Previous->is(tok::at) &&
2579 Current.isNot(Keywords.kw_interface)) {
2583 Current.setType(TT_LeadingJavaAnnotation);
2585 Current.setType(TT_JavaAnnotation);
2586 }
else if (Current.Previous->is(tok::period) &&
2587 Current.Previous->isOneOf(TT_JavaAnnotation,
2588 TT_LeadingJavaAnnotation)) {
2589 Current.setType(Current.Previous->getType());
2601 if (Style.isVerilog())
2604 if (!
Tok.Previous ||
Tok.isNot(tok::identifier) ||
Tok.is(TT_ClassHeadName))
2607 if (
Tok.endsSequence(Keywords.kw_final, TT_ClassHeadName))
2610 if ((Style.isJavaScript() || Style.isJava()) &&
Tok.is(Keywords.kw_extends))
2613 if (
const auto *NextNonComment =
Tok.getNextNonComment();
2614 (!NextNonComment && !Line.InMacroBody) ||
2616 (NextNonComment->isPointerOrReference() ||
2617 NextNonComment->isOneOf(TT_ClassHeadName, tok::string_literal) ||
2618 (Line.InPragmaDirective && NextNonComment->is(tok::identifier))))) {
2622 if (
Tok.Previous->isOneOf(TT_LeadingJavaAnnotation, Keywords.kw_instanceof,
2626 if (Style.isJavaScript() &&
Tok.Previous->is(Keywords.kw_in))
2633 if (!Style.isJavaScript())
2634 while (PreviousNotConst && PreviousNotConst->is(tok::kw_const))
2635 PreviousNotConst = PreviousNotConst->getPreviousNonComment();
2637 if (!PreviousNotConst)
2640 if (PreviousNotConst->ClosesRequiresClause)
2643 if (Style.isTableGen()) {
2645 if (Keywords.isTableGenDefinition(*PreviousNotConst))
2648 if (Contexts.back().ContextKind != tok::l_brace)
2652 bool IsPPKeyword = PreviousNotConst->is(tok::identifier) &&
2653 PreviousNotConst->Previous &&
2654 PreviousNotConst->Previous->is(tok::hash);
2656 if (PreviousNotConst->is(TT_TemplateCloser)) {
2657 return PreviousNotConst && PreviousNotConst->MatchingParen &&
2658 PreviousNotConst->MatchingParen->Previous &&
2659 PreviousNotConst->MatchingParen->Previous->isNoneOf(
2660 tok::period, tok::kw_template);
2663 if ((PreviousNotConst->is(tok::r_paren) &&
2664 PreviousNotConst->is(TT_TypeDeclarationParen)) ||
2665 PreviousNotConst->is(TT_AttributeRParen)) {
2674 if (PreviousNotConst->isOneOf(tok::identifier, tok::kw_auto) &&
2675 PreviousNotConst->isNot(TT_StatementAttributeLikeMacro)) {
2680 if (PreviousNotConst->is(TT_PointerOrReference))
2684 if (PreviousNotConst->isTypeName(LangOpts))
2688 if (Style.isJava() && PreviousNotConst->is(tok::r_square))
2692 return Style.isJavaScript() && PreviousNotConst->is(tok::kw_const);
2702 if (LeftOfParens && LeftOfParens->is(TT_TemplateCloser) &&
2703 LeftOfParens->MatchingParen) {
2704 auto *Prev = LeftOfParens->MatchingParen->getPreviousNonComment();
2706 Prev->isOneOf(tok::kw_const_cast, tok::kw_dynamic_cast,
2707 tok::kw_reinterpret_cast, tok::kw_static_cast)) {
2718 assert(
Tok.is(tok::r_paren));
2720 if (!
Tok.MatchingParen || !
Tok.Previous)
2724 if (!IsCpp && !Style.isCSharp() && !Style.isJava())
2727 const auto *LParen =
Tok.MatchingParen;
2728 const auto *BeforeRParen =
Tok.Previous;
2729 const auto *AfterRParen =
Tok.Next;
2732 if (BeforeRParen == LParen || !AfterRParen)
2735 if (LParen->is(TT_OverloadedOperatorLParen))
2738 auto *LeftOfParens = LParen->getPreviousNonComment();
2742 if (LeftOfParens->is(tok::r_paren) &&
2743 LeftOfParens->isNot(TT_CastRParen)) {
2744 if (!LeftOfParens->MatchingParen ||
2745 !LeftOfParens->MatchingParen->Previous) {
2748 LeftOfParens = LeftOfParens->MatchingParen->Previous;
2751 if (LeftOfParens->is(tok::r_square)) {
2754 if (
Tok->isNot(tok::r_square))
2757 Tok =
Tok->getPreviousNonComment();
2758 if (!
Tok ||
Tok->isNot(tok::l_square))
2761 Tok =
Tok->getPreviousNonComment();
2762 if (!
Tok ||
Tok->isNot(tok::kw_delete))
2766 if (
FormatToken *MaybeDelete = MayBeArrayDelete(LeftOfParens))
2767 LeftOfParens = MaybeDelete;
2773 if (LeftOfParens->Tok.getIdentifierInfo() && LeftOfParens->Previous &&
2774 LeftOfParens->Previous->is(tok::kw_operator)) {
2780 if (LeftOfParens->Tok.getIdentifierInfo() &&
2781 LeftOfParens->isNoneOf(Keywords.kw_in, tok::kw_return, tok::kw_case,
2782 tok::kw_delete, tok::kw_throw)) {
2788 if (LeftOfParens->isOneOf(tok::at, tok::r_square, TT_OverloadedOperator,
2789 TT_TemplateCloser, tok::ellipsis)) {
2794 if (AfterRParen->is(tok::question) ||
2795 (AfterRParen->is(tok::ampamp) && !BeforeRParen->isTypeName(LangOpts))) {
2800 if (AfterRParen->is(Keywords.kw_in) && Style.isCSharp())
2805 if (AfterRParen->isOneOf(tok::kw_noexcept, tok::kw_volatile, tok::kw_const,
2806 tok::kw_requires, tok::kw_throw, tok::arrow,
2807 Keywords.kw_override, Keywords.kw_final) ||
2808 isCppAttribute(IsCpp, *AfterRParen)) {
2814 if (Style.isJava() && AfterRParen->is(tok::l_paren))
2818 if (AfterRParen->isOneOf(tok::kw_sizeof, tok::kw_alignof) ||
2819 (AfterRParen->Tok.isLiteral() &&
2820 AfterRParen->isNot(tok::string_literal))) {
2825 if (
Tok.isNot(TT_TemplateCloser))
2827 const auto *
Less =
Tok.MatchingParen;
2830 const auto *BeforeLess =
Less->getPreviousNonComment();
2831 return BeforeLess && BeforeLess->isNot(TT_VariableTemplate);
2835 auto IsQualifiedPointerOrReference = [](
const FormatToken *
T,
2836 const LangOptions &LangOpts) {
2838 assert(!
T->isTypeName(LangOpts) &&
"Should have already been checked");
2842 if (
T->is(TT_AttributeRParen)) {
2844 assert(
T->is(tok::r_paren));
2845 assert(
T->MatchingParen);
2846 assert(
T->MatchingParen->is(tok::l_paren));
2847 assert(
T->MatchingParen->is(TT_AttributeLParen));
2848 if (
const auto *
Tok =
T->MatchingParen->Previous;
2849 Tok &&
Tok->isAttribute()) {
2853 }
else if (
T->is(TT_AttributeSquare)) {
2855 if (
T->MatchingParen &&
T->MatchingParen->Previous) {
2856 T =
T->MatchingParen->Previous;
2859 }
else if (
T->canBePointerOrReferenceQualifier()) {
2865 return T &&
T->is(TT_PointerOrReference);
2868 bool ParensAreType = IsNonVariableTemplate(*BeforeRParen) ||
2869 BeforeRParen->is(TT_TypeDeclarationParen) ||
2870 BeforeRParen->isTypeName(LangOpts) ||
2871 IsQualifiedPointerOrReference(BeforeRParen, LangOpts);
2872 bool ParensCouldEndDecl =
2873 AfterRParen->isOneOf(tok::equal, tok::semi, tok::l_brace, tok::greater);
2874 if (ParensAreType && !ParensCouldEndDecl)
2885 for (
const auto *Token = LParen->Next; Token != &
Tok; Token = Token->Next)
2886 if (Token->is(TT_BinaryOperator))
2891 if (AfterRParen->isOneOf(tok::identifier, tok::kw_this))
2895 if (AfterRParen->is(tok::l_paren)) {
2896 for (
const auto *Prev = BeforeRParen; Prev->is(tok::identifier);) {
2897 Prev = Prev->Previous;
2898 if (Prev->is(tok::coloncolon))
2899 Prev = Prev->Previous;
2905 if (!AfterRParen->Next)
2908 if (AfterRParen->is(tok::l_brace) &&
2916 const bool NextIsAmpOrStar = AfterRParen->isOneOf(tok::amp, tok::star);
2917 if (!(AfterRParen->isUnaryOperator() || NextIsAmpOrStar) ||
2918 AfterRParen->is(tok::plus) ||
2919 AfterRParen->Next->isNoneOf(tok::identifier, tok::numeric_constant)) {
2923 if (NextIsAmpOrStar &&
2924 (AfterRParen->Next->is(tok::numeric_constant) || Line.InPPDirective)) {
2928 if (Line.InPPDirective && AfterRParen->is(tok::minus))
2931 const auto *Prev = BeforeRParen;
2934 if (Prev->is(tok::r_paren)) {
2935 if (Prev->is(TT_CastRParen))
2937 Prev = Prev->MatchingParen;
2940 Prev = Prev->Previous;
2941 if (!Prev || Prev->isNot(tok::r_paren))
2943 Prev = Prev->MatchingParen;
2944 return Prev && Prev->is(TT_FunctionTypeLParen);
2948 for (Prev = BeforeRParen; Prev != LParen; Prev = Prev->Previous)
2949 if (Prev->isNoneOf(tok::kw_const, tok::identifier, tok::coloncolon))
2967 if (PrevToken->isOneOf(
2968 TT_ConditionalExpr, tok::l_paren, tok::comma, tok::colon, tok::semi,
2969 tok::equal, tok::question, tok::l_square, tok::l_brace,
2970 tok::kw_case, tok::kw_co_await, tok::kw_co_return, tok::kw_co_yield,
2971 tok::kw_delete, tok::kw_return, tok::kw_throw)) {
2978 if (PrevToken->is(tok::kw_sizeof))
2982 if (PrevToken->isOneOf(TT_CastRParen, TT_UnaryOperator))
2986 if (PrevToken->is(TT_BinaryOperator))
2994 bool InTemplateArgument) {
2995 if (Style.isJavaScript())
2996 return TT_BinaryOperator;
2999 if (Style.isCSharp() &&
Tok.is(tok::ampamp))
3000 return TT_BinaryOperator;
3002 if (Style.isVerilog()) {
3007 if (
Tok.is(tok::star))
3008 return TT_BinaryOperator;
3009 return determineUnaryOperatorByUsage(
Tok) ? TT_UnaryOperator
3010 : TT_BinaryOperator;
3015 return TT_UnaryOperator;
3016 if (PrevToken->isTypeName(LangOpts))
3017 return TT_PointerOrReference;
3018 if (PrevToken->isPlacementOperator() &&
Tok.is(tok::ampamp))
3019 return TT_BinaryOperator;
3021 auto *NextToken =
Tok.getNextNonComment();
3023 return TT_PointerOrReference;
3024 if (NextToken->is(tok::greater)) {
3025 NextToken->setFinalizedType(TT_TemplateCloser);
3026 return TT_PointerOrReference;
3029 if (InTemplateArgument && NextToken->is(tok::kw_noexcept))
3030 return TT_BinaryOperator;
3032 if (NextToken->isOneOf(tok::arrow, tok::equal, tok::comma, tok::r_paren,
3033 TT_RequiresClause) ||
3034 (NextToken->is(tok::kw_noexcept) && !IsExpression) ||
3035 NextToken->canBePointerOrReferenceQualifier() ||
3036 (NextToken->is(tok::l_brace) && !NextToken->getNextNonComment())) {
3037 return TT_PointerOrReference;
3040 if (PrevToken->is(tok::coloncolon))
3041 return TT_PointerOrReference;
3043 if (PrevToken->is(tok::r_paren) && PrevToken->is(TT_TypeDeclarationParen))
3044 return TT_PointerOrReference;
3046 if (determineUnaryOperatorByUsage(
Tok))
3047 return TT_UnaryOperator;
3049 if (NextToken->is(tok::l_square) && NextToken->isNot(TT_LambdaLSquare))
3050 return TT_PointerOrReference;
3051 if (NextToken->is(tok::kw_operator) && !IsExpression)
3052 return TT_PointerOrReference;
3053 if (NextToken->isOneOf(tok::comma, tok::semi))
3054 return TT_PointerOrReference;
3066 if (PrevToken->is(tok::r_brace) &&
Tok.is(tok::star) &&
3067 !PrevToken->MatchingParen) {
3068 return TT_PointerOrReference;
3071 if (PrevToken->endsSequence(tok::r_square, tok::l_square, tok::kw_delete))
3072 return TT_UnaryOperator;
3074 if (PrevToken->Tok.isLiteral() ||
3075 PrevToken->isOneOf(tok::r_paren, tok::r_square, tok::kw_true,
3076 tok::kw_false, tok::r_brace)) {
3077 return TT_BinaryOperator;
3081 while (NextNonParen && NextNonParen->is(tok::l_paren))
3082 NextNonParen = NextNonParen->getNextNonComment();
3083 if (NextNonParen && (NextNonParen->Tok.isLiteral() ||
3084 NextNonParen->isOneOf(tok::kw_true, tok::kw_false) ||
3085 NextNonParen->isUnaryOperator())) {
3086 return TT_BinaryOperator;
3092 if (InTemplateArgument && NextToken->Tok.isAnyIdentifier())
3093 return TT_BinaryOperator;
3097 if (
Tok.is(tok::ampamp) &&
3098 NextToken->isOneOf(tok::l_paren, tok::star, tok::amp)) {
3099 return TT_BinaryOperator;
3106 if (NextToken->Tok.isAnyIdentifier()) {
3107 auto *NextNextToken = NextToken->getNextNonComment();
3108 if (NextNextToken) {
3109 if (NextNextToken->is(tok::arrow))
3110 return TT_BinaryOperator;
3111 if (NextNextToken->isPointerOrReference() &&
3112 !NextToken->isObjCLifetimeQualifier(Style)) {
3113 NextNextToken->setFinalizedType(TT_BinaryOperator);
3114 return TT_BinaryOperator;
3121 if (IsExpression && !Contexts.back().CaretFound)
3122 return TT_BinaryOperator;
3125 if (!Scopes.empty() && Scopes.back() ==
ST_Class)
3126 return TT_PointerOrReference;
3129 auto IsChainedOperatorAmpOrMember = [](
const FormatToken *token) {
3130 return !token || token->isOneOf(tok::amp, tok::period, tok::arrow,
3131 tok::arrowstar, tok::periodstar);
3136 if (
Tok.is(tok::amp) && PrevToken->Tok.isAnyIdentifier() &&
3137 IsChainedOperatorAmpOrMember(PrevToken->getPreviousNonComment()) &&
3138 NextToken && NextToken->Tok.isAnyIdentifier()) {
3139 if (
auto NextNext = NextToken->getNextNonComment();
3141 (IsChainedOperatorAmpOrMember(NextNext) || NextNext->is(tok::semi))) {
3142 return TT_BinaryOperator;
3148 return TT_BinaryOperator;
3151 return TT_PointerOrReference;
3155 if (determineUnaryOperatorByUsage(
Tok))
3156 return TT_UnaryOperator;
3160 return TT_UnaryOperator;
3162 if (PrevToken->is(tok::at))
3163 return TT_UnaryOperator;
3166 return TT_BinaryOperator;
3172 if (!PrevToken || PrevToken->is(TT_CastRParen))
3173 return TT_UnaryOperator;
3174 if (PrevToken->isOneOf(tok::r_paren, tok::r_square, tok::identifier))
3175 return TT_TrailingUnaryOperator;
3177 return TT_UnaryOperator;
3180 SmallVector<Context, 8> Contexts;
3182 const FormatStyle &Style;
3183 AnnotatedLine &Line;
3187 LangOptions LangOpts;
3188 const AdditionalKeywords &Keywords;
3190 SmallVector<ScopeType> &Scopes;
3196 llvm::SmallPtrSet<FormatToken *, 16> NonTemplateLess;
3198 int TemplateDeclarationDepth;
3206class ExpressionParser {
3208 ExpressionParser(
const FormatStyle &Style,
const AdditionalKeywords &Keywords,
3209 AnnotatedLine &Line)
3210 : Style(Style), Keywords(Keywords), Line(Line), Current(Line.
First) {}
3213 void parse(
int Precedence = 0) {
3216 while (Current && (Current->is(tok::kw_return) ||
3217 (Current->is(tok::colon) &&
3218 Current->isOneOf(TT_ObjCMethodExpr, TT_DictLiteral)))) {
3222 if (!Current || Precedence > PrecedenceArrowAndPeriod)
3227 parseConditionalExpr();
3233 if (Precedence == PrecedenceUnaryOperator) {
3234 parseUnaryOperator();
3251 if (Style.isVerilog() && Precedence ==
prec::Comma) {
3252 VerilogFirstOfType =
3253 verilogGroupDecl(VerilogFirstOfType, LatestOperator);
3257 parse(Precedence + 1);
3259 int CurrentPrecedence = getCurrentPrecedence();
3260 if (Style.BreakBinaryOperations == FormatStyle::BBO_OnePerLine &&
3270 if (Precedence == CurrentPrecedence && Current &&
3271 Current->is(TT_SelectorName)) {
3273 addFakeParenthesis(Start,
prec::Level(Precedence));
3277 if ((Style.isCSharp() || Style.isJavaScript() || Style.isJava()) &&
3281 FormatToken *Prev = Current->getPreviousNonComment();
3282 if (Prev && Prev->is(tok::string_literal) &&
3283 (Prev == Start || Prev->endsSequence(tok::string_literal, tok::plus,
3284 TT_StringInConcatenation))) {
3285 Prev->setType(TT_StringInConcatenation);
3292 (Current->closesScope() &&
3293 (Current->MatchingParen || Current->is(TT_TemplateString))) ||
3294 (CurrentPrecedence != -1 && CurrentPrecedence < Precedence) ||
3303 if (Current->opensScope() ||
3304 Current->isOneOf(TT_RequiresClause,
3305 TT_RequiresClauseInARequiresExpression)) {
3308 while (Current && (!Current->closesScope() || Current->opensScope())) {
3315 if (CurrentPrecedence == Precedence) {
3317 LatestOperator->NextOperator = Current;
3318 LatestOperator = Current;
3322 next(Precedence > 0);
3327 if (Style.isVerilog() && Precedence ==
prec::Comma && VerilogFirstOfType)
3328 addFakeParenthesis(VerilogFirstOfType,
prec::Comma);
3330 if (LatestOperator && (Current || Precedence > 0)) {
3336 Start->Previous->isOneOf(TT_RequiresClause,
3337 TT_RequiresClauseInARequiresExpression))
3339 auto Ret = Current ? Current : Line.Last;
3340 while (!
Ret->ClosesRequiresClause &&
Ret->Previous)
3346 if (Precedence == PrecedenceArrowAndPeriod) {
3350 addFakeParenthesis(Start,
prec::Level(Precedence), End);
3358 int getCurrentPrecedence() {
3360 const FormatToken *NextNonComment = Current->getNextNonComment();
3361 if (Current->is(TT_ConditionalExpr))
3363 if (NextNonComment && Current->is(TT_SelectorName) &&
3364 (NextNonComment->isOneOf(TT_DictLiteral, TT_JsTypeColon) ||
3365 (Style.isProto() && NextNonComment->is(tok::less)))) {
3368 if (Current->is(TT_JsComputedPropertyName))
3370 if (Current->is(TT_LambdaArrow))
3372 if (Current->is(TT_FatArrow))
3374 if (Current->isOneOf(tok::semi, TT_InlineASMColon, TT_SelectorName) ||
3375 (Current->is(tok::comment) && NextNonComment &&
3376 NextNonComment->is(TT_SelectorName))) {
3379 if (Current->is(TT_RangeBasedForLoopColon))
3381 if ((Style.isJava() || Style.isJavaScript()) &&
3382 Current->is(Keywords.kw_instanceof)) {
3385 if (Style.isJavaScript() &&
3386 Current->isOneOf(Keywords.kw_in, Keywords.kw_as)) {
3389 if (Current->isOneOf(TT_BinaryOperator, tok::comma))
3390 return Current->getPrecedence();
3391 if (Current->isOneOf(tok::period, tok::arrow) &&
3392 Current->isNot(TT_TrailingReturnArrow)) {
3393 return PrecedenceArrowAndPeriod;
3395 if ((Style.isJava() || Style.isJavaScript()) &&
3396 Current->isOneOf(Keywords.kw_extends, Keywords.kw_implements,
3397 Keywords.kw_throws)) {
3402 if (Style.isVerilog() && Current->is(tok::colon))
3414 if (Start->MacroParent)
3417 Start->FakeLParens.push_back(Precedence);
3419 Start->StartsBinaryExpression =
true;
3420 if (!End && Current)
3421 End = Current->getPreviousNonComment();
3425 End->EndsBinaryExpression =
true;
3431 void parseUnaryOperator() {
3432 SmallVector<FormatToken *, 2> Tokens;
3433 while (Current && Current->is(TT_UnaryOperator)) {
3434 Tokens.push_back(Current);
3437 parse(PrecedenceArrowAndPeriod);
3444 void parseConditionalExpr() {
3445 while (Current && Current->isTrailingComment())
3449 if (!Current || Current->isNot(tok::question))
3453 if (!Current || Current->isNot(TT_ConditionalExpr))
3460 void next(
bool SkipPastLeadingComments =
true) {
3462 Current = Current->Next;
3464 (Current->NewlinesBefore == 0 || SkipPastLeadingComments) &&
3465 Current->isTrailingComment()) {
3466 Current = Current->Next;
3480 while (Start->startsSequence(tok::l_paren, tok::star)) {
3481 if (!(Start = Start->MatchingParen) ||
3482 !(Start = Start->getNextNonComment())) {
3489 if (
Tok->is(Keywords.kw_assign))
3490 Tok =
Tok->getNextNonComment();
3502 if (
Tok->is(tok::hash)) {
3507 Tok =
Tok->getNextNonComment();
3508 }
else if (
Tok->is(tok::hashhash)) {
3512 Tok =
Tok->getNextNonComment();
3513 }
else if (Keywords.isVerilogQualifier(*
Tok) ||
3514 Keywords.isVerilogIdentifier(*
Tok)) {
3518 while (
Tok &&
Tok->isOneOf(tok::period, tok::coloncolon) &&
3519 (
Tok =
Tok->getNextNonComment())) {
3520 if (Keywords.isVerilogIdentifier(*
Tok))
3521 Tok =
Tok->getNextNonComment();
3525 }
else if (
Tok->is(tok::l_paren)) {
3530 Keywords.kw_highz0, Keywords.kw_highz1, Keywords.kw_large,
3531 Keywords.kw_medium, Keywords.kw_pull0, Keywords.kw_pull1,
3532 Keywords.kw_small, Keywords.kw_strong0, Keywords.kw_strong1,
3533 Keywords.kw_supply0, Keywords.kw_supply1, Keywords.kw_weak0,
3534 Keywords.kw_weak1)) {
3535 Tok->setType(TT_VerilogStrength);
3536 Tok =
Tok->MatchingParen;
3538 Tok->setType(TT_VerilogStrength);
3539 Tok =
Tok->getNextNonComment();
3544 }
else if (
Tok->is(Keywords.kw_verilogHash)) {
3546 if (
Next->is(tok::l_paren))
3549 Tok =
Next->getNextNonComment();
3558 while (
Tok &&
Tok->is(tok::l_square) && (
Tok =
Tok->MatchingParen))
3559 Tok =
Tok->getNextNonComment();
3560 if (
Tok && (
Tok->is(tok::hash) || Keywords.isVerilogIdentifier(*
Tok)))
3569 First->setType(TT_VerilogDimensionedTypeName);
3570 }
else if (
First != Start) {
3578 if (TypedName->is(TT_Unknown))
3579 TypedName->setType(TT_StartOfName);
3581 if (FirstOfType && PreviousComma) {
3582 PreviousComma->setType(TT_VerilogTypeComma);
3583 addFakeParenthesis(FirstOfType,
prec::Comma, PreviousComma->Previous);
3586 FirstOfType = TypedName;
3593 while (Current && Current != FirstOfType) {
3594 if (Current->opensScope()) {
3605 const FormatStyle &Style;
3606 const AdditionalKeywords &Keywords;
3607 const AnnotatedLine &Line;
3617 assert(
Line->First);
3624 Line->First->OriginalColumn) {
3625 const bool PPDirectiveOrImportStmt =
3628 if (PPDirectiveOrImportStmt)
3633 Line->Level = Style.IndentPPDirectives < FormatStyle::PPDIS_BeforeHash &&
3634 PPDirectiveOrImportStmt
3636 : NextNonCommentLine->
Level;
3638 NextNonCommentLine =
Line->First->isNot(tok::r_brace) ?
Line :
nullptr;
3657 Tok =
Tok->getNextNonComment()) {
3659 if (
Tok->is(tok::l_square) &&
Tok->is(TT_AttributeSquare)) {
3660 Tok =
Tok->MatchingParen;
3668 if (
Tok->is(tok::l_paren) &&
Tok->is(TT_Unknown) &&
Tok->MatchingParen) {
3676 if (
Tok->isOneOf(tok::kw_friend, tok::kw_inline, tok::kw_virtual,
3677 tok::kw_constexpr, tok::kw_consteval, tok::kw_explicit)) {
3682 if (
Tok->is(tok::coloncolon)) {
3689 while (
Tok->startsSequence(tok::identifier, tok::coloncolon)) {
3697 if (
Tok->is(tok::tilde)) {
3704 if (
Tok->isNot(tok::identifier) ||
Tok->isNot(TT_Unknown))
3715 assert(
Tok &&
Tok->is(tok::identifier));
3716 const auto *Prev =
Tok->Previous;
3718 if (Prev && Prev->is(tok::tilde))
3719 Prev = Prev->Previous;
3721 if (!Prev || !Prev->endsSequence(tok::coloncolon, tok::identifier))
3724 assert(Prev->Previous);
3725 return Prev->Previous->TokenText ==
Tok->TokenText;
3729 if (!
Line.InMacroBody)
3730 MacroBodyScopes.clear();
3732 auto &ScopeStack =
Line.InMacroBody ? MacroBodyScopes : Scopes;
3733 AnnotatingParser
Parser(Style,
Line, Keywords, ScopeStack);
3736 if (!
Line.Children.empty()) {
3739 for (
auto &Child :
Line.Children) {
3740 if (InRequiresExpression &&
3741 Child->First->isNoneOf(tok::kw_typename, tok::kw_requires,
3742 TT_CompoundRequirementLBrace)) {
3748 if (!ScopeStack.empty())
3749 ScopeStack.pop_back();
3762 ExpressionParser ExprParser(Style, Keywords,
Line);
3768 if (
Tok && ((!ScopeStack.empty() && ScopeStack.back() ==
ST_Class) ||
3770 Tok->setFinalizedType(TT_CtorDtorDeclName);
3771 assert(OpeningParen);
3776 if (
Line.startsWith(TT_ObjCMethodSpecifier))
3778 else if (
Line.startsWith(TT_ObjCDecl))
3780 else if (
Line.startsWith(TT_ObjCProperty))
3784 First->SpacesRequiredBefore = 1;
3785 First->CanBreakBefore =
First->MustBreakBefore;
3794 if (Current.
is(TT_FunctionDeclarationName))
3800 const auto *Prev = Current.getPreviousNonComment();
3803 if (Prev->is(tok::coloncolon))
3804 Prev = Prev->Previous;
3811 if (
const auto *PrevPrev =
Previous.getPreviousNonComment();
3812 PrevPrev && PrevPrev->is(TT_ObjCDecl)) {
3816 auto skipOperatorName =
3819 if (
Next->is(TT_OverloadedOperatorLParen))
3821 if (
Next->is(TT_OverloadedOperator))
3823 if (
Next->isPlacementOperator() ||
Next->is(tok::kw_co_await)) {
3826 Next->Next->startsSequence(tok::l_square, tok::r_square)) {
3831 if (
Next->startsSequence(tok::l_square, tok::r_square)) {
3836 if ((
Next->isTypeName(LangOpts) ||
Next->is(tok::identifier)) &&
3837 Next->Next &&
Next->Next->isPointerOrReference()) {
3842 if (
Next->is(TT_TemplateOpener) &&
Next->MatchingParen) {
3853 const bool IsCpp = LangOpts.CXXOperatorNames || LangOpts.C11;
3856 if (Current.
is(tok::kw_operator)) {
3857 if (
Previous.Tok.getIdentifierInfo() &&
3858 Previous.isNoneOf(tok::kw_return, tok::kw_co_return)) {
3863 assert(
Previous.MatchingParen->is(tok::l_paren));
3864 assert(
Previous.MatchingParen->is(TT_TypeDeclarationParen));
3873 while (
Next &&
Next->startsSequence(tok::hashhash, tok::identifier))
3876 if (
Next->is(TT_TemplateOpener) &&
Next->MatchingParen) {
3878 }
else if (
Next->is(tok::coloncolon)) {
3882 if (
Next->is(tok::kw_operator)) {
3883 Next = skipOperatorName(
Next->Next);
3886 if (
Next->isNot(tok::identifier))
3888 }
else if (isCppAttribute(IsCpp, *
Next)) {
3892 }
else if (
Next->is(tok::l_paren)) {
3901 if (!
Next ||
Next->isNot(tok::l_paren) || !
Next->MatchingParen)
3903 ClosingParen =
Next->MatchingParen;
3904 assert(ClosingParen->
is(tok::r_paren));
3906 if (
Line.Last->is(tok::l_brace))
3908 if (
Next->Next == ClosingParen)
3911 if (ClosingParen->
Next && ClosingParen->
Next->
is(TT_PointerOrReference))
3924 if (IsCpp &&
Next->Next &&
Next->Next->is(tok::identifier) &&
3925 !
Line.endsWith(tok::semi)) {
3931 if (
Tok->is(TT_TypeDeclarationParen))
3933 if (
Tok->isOneOf(tok::l_paren, TT_TemplateOpener) &&
Tok->MatchingParen) {
3934 Tok =
Tok->MatchingParen;
3937 if (
Tok->is(tok::kw_const) ||
Tok->isTypeName(LangOpts) ||
3938 Tok->isOneOf(TT_PointerOrReference, TT_StartOfName, tok::ellipsis)) {
3941 if (
Tok->isOneOf(tok::l_brace, TT_ObjCMethodExpr) ||
Tok->Tok.isLiteral())
3947bool TokenAnnotator::mustBreakForReturnType(
const AnnotatedLine &
Line)
const {
3948 assert(
Line.MightBeFunctionDecl);
3950 if ((Style.BreakAfterReturnType == FormatStyle::RTBS_TopLevel ||
3951 Style.BreakAfterReturnType == FormatStyle::RTBS_TopLevelDefinitions) &&
3956 switch (Style.BreakAfterReturnType) {
3957 case FormatStyle::RTBS_None:
3958 case FormatStyle::RTBS_Automatic:
3959 case FormatStyle::RTBS_ExceptShortType:
3961 case FormatStyle::RTBS_All:
3962 case FormatStyle::RTBS_TopLevel:
3964 case FormatStyle::RTBS_AllDefinitions:
3965 case FormatStyle::RTBS_TopLevelDefinitions:
3966 return Line.mightBeFunctionDefinition();
3976 Line.Computed =
true;
3984 :
Line.FirstStartColumn +
First->ColumnWidth;
3985 bool AlignArrayOfStructures =
3986 (Style.AlignArrayOfStructures != FormatStyle::AIAS_None &&
3988 if (AlignArrayOfStructures)
3989 calculateArrayInitializerColumnList(
Line);
3991 const auto *FirstNonComment =
Line.getFirstNonComment();
3992 bool SeenName =
false;
3993 bool LineIsFunctionDeclaration =
false;
3997 for (
auto *
Tok = FirstNonComment && FirstNonComment->isNot(tok::kw_using)
3998 ? FirstNonComment->Next
4001 if (
Tok->is(TT_StartOfName))
4003 if (
Tok->Previous->EndsCppAttributeGroup)
4004 AfterLastAttribute =
Tok;
4005 if (
const bool IsCtorOrDtor =
Tok->is(TT_CtorDtorDeclName);
4009 Tok->setFinalizedType(TT_FunctionDeclarationName);
4010 LineIsFunctionDeclaration =
true;
4014 assert(OpeningParen);
4015 if (OpeningParen->is(TT_Unknown))
4016 OpeningParen->setType(TT_FunctionDeclarationLParen);
4023 if ((LineIsFunctionDeclaration ||
4024 (FirstNonComment && FirstNonComment->is(TT_CtorDtorDeclName))) &&
4025 Line.endsWith(tok::semi, tok::r_brace)) {
4026 auto *
Tok =
Line.Last->Previous;
4027 while (
Tok->isNot(tok::r_brace))
4029 if (
auto *LBrace =
Tok->MatchingParen; LBrace && LBrace->is(TT_Unknown)) {
4030 assert(LBrace->is(tok::l_brace));
4033 LBrace->setFinalizedType(TT_FunctionLBrace);
4037 if (SeenName && AfterLastAttribute &&
4040 if (LineIsFunctionDeclaration)
4041 Line.ReturnTypeWrapped =
true;
4044 if (!LineIsFunctionDeclaration) {
4046 for (
const auto *
Tok = FirstNonComment;
Tok;
Tok =
Tok->Next) {
4047 if (
Tok->isNot(tok::kw_operator))
4051 }
while (
Tok &&
Tok->isNot(TT_OverloadedOperatorLParen));
4052 if (!
Tok || !
Tok->MatchingParen)
4054 const auto *LeftParen =
Tok;
4055 for (
Tok =
Tok->Next;
Tok &&
Tok != LeftParen->MatchingParen;
4057 if (
Tok->isNot(tok::identifier))
4060 const bool NextIsBinaryOperator =
4062 Next->Next->is(tok::identifier);
4063 if (!NextIsBinaryOperator)
4065 Next->setType(TT_BinaryOperator);
4069 }
else if (ClosingParen) {
4071 if (
Tok->is(TT_CtorInitializerColon))
4073 if (
Tok->is(tok::arrow)) {
4074 Tok->setType(TT_TrailingReturnArrow);
4077 if (
Tok->isNot(TT_TrailingAnnotation))
4080 if (!
Next ||
Next->isNot(tok::l_paren))
4089 if (
First->is(TT_ElseLBrace)) {
4090 First->CanBreakBefore =
true;
4091 First->MustBreakBefore =
true;
4094 bool InFunctionDecl =
Line.MightBeFunctionDecl;
4095 bool InParameterList =
false;
4096 for (
auto *Current =
First->Next; Current; Current = Current->Next) {
4098 if (Current->is(TT_LineComment)) {
4100 Current->SpacesRequiredBefore =
4101 (Style.Cpp11BracedListStyle && !Style.SpacesInParensOptions.Other)
4104 }
else if (Prev->
is(TT_VerilogMultiLineListLParen)) {
4105 Current->SpacesRequiredBefore = 0;
4107 Current->SpacesRequiredBefore = Style.SpacesBeforeTrailingComments;
4117 if (!Current->HasUnescapedNewline) {
4120 if (
Parameter->isOneOf(tok::comment, tok::r_brace))
4123 if (
Parameter->Previous->isNot(TT_CtorInitializerComma) &&
4131 }
else if (!Current->Finalized && Current->SpacesRequiredBefore == 0 &&
4132 spaceRequiredBefore(
Line, *Current)) {
4133 Current->SpacesRequiredBefore = 1;
4138 Current->MustBreakBefore =
true;
4140 Current->MustBreakBefore =
4141 Current->MustBreakBefore || mustBreakBefore(
Line, *Current);
4142 if (!Current->MustBreakBefore && InFunctionDecl &&
4143 Current->is(TT_FunctionDeclarationName)) {
4144 Current->MustBreakBefore = mustBreakForReturnType(
Line);
4148 Current->CanBreakBefore =
4149 Current->MustBreakBefore || canBreakBefore(
Line, *Current);
4151 if (Current->is(TT_FunctionDeclarationLParen)) {
4152 InParameterList =
true;
4153 }
else if (Current->is(tok::r_paren)) {
4154 const auto *LParen = Current->MatchingParen;
4155 if (LParen && LParen->is(TT_FunctionDeclarationLParen))
4156 InParameterList =
false;
4157 }
else if (InParameterList &&
4158 Current->endsSequence(TT_AttributeMacro,
4159 TT_PointerOrReference)) {
4160 Current->CanBreakBefore =
false;
4163 unsigned ChildSize = 0;
4166 ChildSize = LastOfChild.isTrailingComment() ? Style.ColumnLimit
4169 if (Current->MustBreakBefore || Prev->
Children.size() > 1 ||
4171 Prev->
Children[0]->First->MustBreakBefore) ||
4172 Current->IsMultiline) {
4173 Current->TotalLength = Prev->
TotalLength + Style.ColumnLimit;
4175 Current->TotalLength = Prev->
TotalLength + Current->ColumnWidth +
4176 ChildSize + Current->SpacesRequiredBefore;
4179 if (Current->is(TT_ControlStatementLBrace)) {
4180 if (Style.ColumnLimit > 0 &&
4181 Style.BraceWrapping.AfterControlStatement ==
4182 FormatStyle::BWACS_MultiLine &&
4183 Line.Level * Style.IndentWidth +
Line.Last->TotalLength >
4184 Style.ColumnLimit) {
4185 Current->CanBreakBefore =
true;
4186 Current->MustBreakBefore =
true;
4188 }
else if (Current->is(TT_CtorInitializerColon)) {
4189 InFunctionDecl =
false;
4201 Current->SplitPenalty = splitPenalty(
Line, *Current, InFunctionDecl);
4202 if (Style.Language == FormatStyle::LK_ObjC &&
4203 Current->is(TT_SelectorName) && Current->ParameterIndex > 0) {
4204 if (Current->ParameterIndex == 1)
4205 Current->SplitPenalty += 5 * Current->BindingStrength;
4207 Current->SplitPenalty += 20 * Current->BindingStrength;
4211 calculateUnbreakableTailLengths(
Line);
4213 for (
auto *Current =
First; Current; Current = Current->Next) {
4215 Current->Role->precomputeFormattingInfos(Current);
4216 if (Current->MatchingParen &&
4217 Current->MatchingParen->opensBlockOrBlockTypeList(Style) &&
4222 if (Current->opensBlockOrBlockTypeList(Style))
4226 LLVM_DEBUG({ printDebugInfo(
Line); });
4229void TokenAnnotator::calculateUnbreakableTailLengths(
4236 Current->
isOneOf(tok::comment, tok::string_literal)) {
4246void TokenAnnotator::calculateArrayInitializerColumnList(
4250 auto *CurrentToken =
Line.First;
4251 CurrentToken->ArrayInitializerLineStart =
true;
4253 while (CurrentToken && CurrentToken !=
Line.Last) {
4254 if (CurrentToken->is(tok::l_brace)) {
4255 CurrentToken->IsArrayInitializer =
true;
4256 if (CurrentToken->Next)
4257 CurrentToken->Next->MustBreakBefore =
true;
4259 calculateInitializerColumnList(
Line, CurrentToken->Next, Depth + 1);
4261 CurrentToken = CurrentToken->Next;
4266FormatToken *TokenAnnotator::calculateInitializerColumnList(
4268 while (CurrentToken && CurrentToken !=
Line.Last) {
4269 if (CurrentToken->is(tok::l_brace))
4271 else if (CurrentToken->is(tok::r_brace))
4273 if (Depth == 2 && CurrentToken->isOneOf(tok::l_brace, tok::comma)) {
4274 CurrentToken = CurrentToken->Next;
4277 CurrentToken->StartsColumn =
true;
4278 CurrentToken = CurrentToken->Previous;
4280 CurrentToken = CurrentToken->Next;
4282 return CurrentToken;
4287 bool InFunctionDecl)
const {
4291 if (
Left.is(tok::semi))
4295 if (Style.isJava()) {
4296 if (
Right.isOneOf(Keywords.kw_extends, Keywords.kw_throws))
4298 if (
Right.is(Keywords.kw_implements))
4300 if (
Left.is(tok::comma) &&
Left.NestingLevel == 0)
4302 }
else if (Style.isJavaScript()) {
4303 if (
Right.is(Keywords.kw_function) &&
Left.isNot(tok::comma))
4305 if (
Left.is(TT_JsTypeColon))
4307 if ((
Left.is(TT_TemplateString) &&
Left.TokenText.ends_with(
"${")) ||
4308 (
Right.is(TT_TemplateString) &&
Right.TokenText.starts_with(
"}"))) {
4312 if (
Left.opensScope() &&
Right.closesScope())
4314 }
else if (Style.Language == FormatStyle::LK_Proto) {
4315 if (
Right.is(tok::l_square))
4317 if (
Right.is(tok::period))
4321 if (
Right.is(tok::identifier) &&
Right.Next &&
Right.Next->is(TT_DictLiteral))
4323 if (
Right.is(tok::l_square)) {
4324 if (
Left.is(tok::r_square))
4327 if (
Right.is(TT_LambdaLSquare) &&
Left.is(tok::equal))
4329 if (
Right.isNoneOf(TT_ObjCMethodExpr, TT_LambdaLSquare,
4330 TT_ArrayInitializerLSquare,
4331 TT_DesignatedInitializerLSquare, TT_AttributeSquare)) {
4336 if (
Left.is(tok::coloncolon))
4337 return Style.PenaltyBreakScopeResolution;
4338 if (
Right.isOneOf(TT_StartOfName, TT_FunctionDeclarationName,
4339 tok::kw_operator)) {
4340 if (
Line.startsWith(tok::kw_for) &&
Right.PartOfMultiVariableDeclStmt)
4342 if (
Left.is(TT_StartOfName))
4344 if (InFunctionDecl &&
Right.NestingLevel == 0)
4345 return Style.PenaltyReturnTypeOnItsOwnLine;
4348 if (
Right.is(TT_PointerOrReference))
4350 if (
Right.is(TT_LambdaArrow))
4352 if (
Left.is(tok::equal) &&
Right.is(tok::l_brace))
4354 if (
Left.is(TT_CastRParen))
4356 if (
Left.isOneOf(tok::kw_class, tok::kw_struct, tok::kw_union))
4358 if (
Left.is(tok::comment))
4361 if (
Left.isOneOf(TT_RangeBasedForLoopColon, TT_InheritanceColon,
4362 TT_CtorInitializerColon)) {
4366 if (
Right.isMemberAccess()) {
4387 const auto Penalty = Style.PenaltyBreakBeforeMemberAccess;
4389 ? std::min(Penalty, 35u)
4393 if (
Right.is(TT_TrailingAnnotation) &&
4394 (!
Right.Next ||
Right.Next->isNot(tok::l_paren))) {
4397 if (
Line.startsWith(TT_ObjCMethodSpecifier))
4404 bool is_short_annotation =
Right.TokenText.size() < 10;
4405 return (
Left.is(tok::r_paren) ? 100 : 120) + (is_short_annotation ? 50 : 0);
4409 if (
Line.startsWith(tok::kw_for) &&
Left.is(tok::equal))
4414 if (
Right.is(TT_SelectorName))
4416 if (
Left.is(tok::colon) &&
Left.is(TT_ObjCMethodExpr))
4417 return Line.MightBeFunctionDecl ? 50 : 500;
4423 Left.Previous->isOneOf(tok::identifier, tok::greater)) {
4427 if (
Left.is(tok::l_paren) && Style.PenaltyBreakOpenParenthesis != 0)
4428 return Style.PenaltyBreakOpenParenthesis;
4429 if (
Left.is(tok::l_paren) && InFunctionDecl &&
4430 Style.AlignAfterOpenBracket != FormatStyle::BAS_DontAlign) {
4433 if (
Left.is(tok::l_paren) &&
Left.Previous &&
4434 (
Left.Previous->isOneOf(tok::kw_for, tok::kw__Generic) ||
4435 Left.Previous->isIf())) {
4438 if (
Left.is(tok::equal) && InFunctionDecl)
4440 if (
Right.is(tok::r_brace))
4442 if (
Left.is(TT_TemplateOpener))
4444 if (
Left.opensScope()) {
4448 if (Style.AlignAfterOpenBracket == FormatStyle::BAS_DontAlign &&
4449 (
Left.ParameterCount <= 1 || Style.AllowAllArgumentsOnNextLine)) {
4452 if (
Left.is(tok::l_brace) && !Style.Cpp11BracedListStyle)
4454 return Left.ParameterCount > 1 ? Style.PenaltyBreakBeforeFirstCallParameter
4457 if (
Left.is(TT_JavaAnnotation))
4460 if (
Left.is(TT_UnaryOperator))
4462 if (
Left.isOneOf(tok::plus, tok::comma) &&
Left.Previous &&
4463 Left.Previous->isLabelString() &&
4464 (
Left.NextOperator ||
Left.OperatorIndex != 0)) {
4467 if (
Right.is(tok::plus) &&
Left.isLabelString() &&
4468 (
Right.NextOperator ||
Right.OperatorIndex != 0)) {
4471 if (
Left.is(tok::comma))
4473 if (
Right.is(tok::lessless) &&
Left.isLabelString() &&
4474 (
Right.NextOperator ||
Right.OperatorIndex != 1)) {
4477 if (
Right.is(tok::lessless)) {
4479 if (
Left.isNot(tok::r_paren) ||
Right.OperatorIndex > 0) {
4485 if (
Left.ClosesTemplateDeclaration)
4486 return Style.PenaltyBreakTemplateDeclaration;
4487 if (
Left.ClosesRequiresClause)
4489 if (
Left.is(TT_ConditionalExpr))
4495 return Style.PenaltyBreakAssignment;
4502bool TokenAnnotator::spaceRequiredBeforeParens(
const FormatToken &Right)
const {
4503 if (Style.SpaceBeforeParens == FormatStyle::SBPO_Always)
4505 if (
Right.is(TT_OverloadedOperatorLParen) &&
4506 Style.SpaceBeforeParensOptions.AfterOverloadedOperator) {
4509 if (Style.SpaceBeforeParensOptions.BeforeNonEmptyParentheses &&
4510 Right.ParameterCount > 0) {
4519 if (
Left.is(tok::kw_return) &&
4520 Right.isNoneOf(tok::semi, tok::r_paren, tok::hashhash)) {
4523 if (
Left.is(tok::kw_throw) &&
Right.is(tok::l_paren) &&
Right.MatchingParen &&
4524 Right.MatchingParen->is(TT_CastRParen)) {
4527 if (
Left.is(Keywords.kw_assert) && Style.isJava())
4530 Left.is(tok::objc_property)) {
4533 if (
Right.is(tok::hashhash))
4534 return Left.is(tok::hash);
4535 if (
Left.isOneOf(tok::hashhash, tok::hash))
4536 return Right.is(tok::hash);
4537 if (Style.SpacesInParens == FormatStyle::SIPO_Custom) {
4538 if (
Left.is(tok::l_paren) &&
Right.is(tok::r_paren))
4539 return Style.SpacesInParensOptions.InEmptyParentheses;
4540 if (Style.SpacesInParensOptions.ExceptDoubleParentheses &&
4541 Left.is(tok::r_paren) &&
Right.is(tok::r_paren)) {
4542 auto *InnerLParen =
Left.MatchingParen;
4543 if (InnerLParen && InnerLParen->Previous ==
Right.MatchingParen) {
4544 InnerLParen->SpacesRequiredBefore = 0;
4549 if (
Left.is(tok::l_paren))
4551 else if (
Right.is(tok::r_paren) &&
Right.MatchingParen)
4552 LeftParen =
Right.MatchingParen;
4553 if (LeftParen && (LeftParen->is(TT_ConditionLParen) ||
4554 (LeftParen->Previous &&
4555 isKeywordWithCondition(*LeftParen->Previous)))) {
4556 return Style.SpacesInParensOptions.InConditionalStatements;
4561 if (
Left.is(tok::kw_auto) &&
Right.isOneOf(TT_LambdaLBrace, TT_FunctionLBrace,
4563 TT_FunctionTypeLParen)) {
4568 if (
Left.is(tok::kw_auto) &&
Right.isOneOf(tok::l_paren, tok::l_brace))
4571 const auto *BeforeLeft =
Left.Previous;
4574 if (
Right.is(tok::l_paren) &&
Left.is(tok::kw_co_await) && BeforeLeft &&
4575 BeforeLeft->is(tok::kw_operator)) {
4579 if (
Left.isOneOf(tok::kw_co_await, tok::kw_co_yield, tok::kw_co_return) &&
4580 Right.isNoneOf(tok::semi, tok::r_paren)) {
4584 if (
Left.is(tok::l_paren) ||
Right.is(tok::r_paren)) {
4585 return (
Right.is(TT_CastRParen) ||
4586 (
Left.MatchingParen &&
Left.MatchingParen->is(TT_CastRParen)))
4587 ? Style.SpacesInParensOptions.InCStyleCasts
4588 : Style.SpacesInParensOptions.Other;
4590 if (
Right.isOneOf(tok::semi, tok::comma))
4593 bool IsLightweightGeneric =
Right.MatchingParen &&
4594 Right.MatchingParen->Next &&
4595 Right.MatchingParen->Next->is(tok::colon);
4596 return !IsLightweightGeneric && Style.ObjCSpaceBeforeProtocolList;
4598 if (
Right.is(tok::less) &&
Left.is(tok::kw_template))
4599 return Style.SpaceAfterTemplateKeyword;
4600 if (
Left.isOneOf(tok::exclaim, tok::tilde))
4602 if (
Left.is(tok::at) &&
4603 Right.isOneOf(tok::identifier, tok::string_literal, tok::char_constant,
4604 tok::numeric_constant, tok::l_paren, tok::l_brace,
4605 tok::kw_true, tok::kw_false)) {
4608 if (
Left.is(tok::colon))
4609 return Left.isNoneOf(TT_ObjCSelector, TT_ObjCMethodExpr);
4610 if (
Left.is(tok::coloncolon))
4612 if (
Left.is(tok::less) ||
Right.isOneOf(tok::greater, tok::less)) {
4613 if (Style.isTextProto() ||
4614 (Style.Language == FormatStyle::LK_Proto &&
4615 (
Left.is(TT_DictLiteral) ||
Right.is(TT_DictLiteral)))) {
4617 if (
Left.is(tok::less) &&
Right.is(tok::greater))
4619 return !Style.Cpp11BracedListStyle;
4622 if (
Right.isNot(TT_OverloadedOperatorLParen))
4625 if (
Right.is(tok::ellipsis)) {
4626 return Left.Tok.isLiteral() || (
Left.is(tok::identifier) && BeforeLeft &&
4627 BeforeLeft->is(tok::kw_case));
4629 if (
Left.is(tok::l_square) &&
Right.is(tok::amp))
4630 return Style.SpacesInSquareBrackets;
4631 if (
Right.is(TT_PointerOrReference)) {
4632 if (
Left.is(tok::r_paren) &&
Line.MightBeFunctionDecl) {
4633 if (!
Left.MatchingParen)
4636 Left.MatchingParen->getPreviousNonComment();
4637 if (!TokenBeforeMatchingParen ||
Left.isNot(TT_TypeDeclarationParen))
4643 if ((Style.SpaceAroundPointerQualifiers == FormatStyle::SAPQ_After ||
4644 Style.SpaceAroundPointerQualifiers == FormatStyle::SAPQ_Both) &&
4645 (
Left.is(TT_AttributeRParen) ||
4646 Left.canBePointerOrReferenceQualifier())) {
4649 if (
Left.Tok.isLiteral())
4652 if (
Left.isTypeOrIdentifier(LangOpts) &&
Right.Next &&
Right.Next->Next &&
4653 Right.Next->Next->is(TT_RangeBasedForLoopColon)) {
4654 return getTokenPointerOrReferenceAlignment(Right) !=
4655 FormatStyle::PAS_Left;
4657 return Left.isNoneOf(TT_PointerOrReference, tok::l_paren) &&
4658 (getTokenPointerOrReferenceAlignment(Right) !=
4659 FormatStyle::PAS_Left ||
4660 (
Line.IsMultiVariableDeclStmt &&
4661 (
Left.NestingLevel == 0 ||
4662 (
Left.NestingLevel == 1 && startsWithInitStatement(
Line)))));
4664 if (
Right.is(TT_FunctionTypeLParen) &&
Left.isNot(tok::l_paren) &&
4665 (
Left.isNot(TT_PointerOrReference) ||
4666 (getTokenPointerOrReferenceAlignment(Left) != FormatStyle::PAS_Right &&
4667 !
Line.IsMultiVariableDeclStmt))) {
4670 if (
Left.is(TT_PointerOrReference)) {
4673 if ((Style.SpaceAroundPointerQualifiers == FormatStyle::SAPQ_Before ||
4674 Style.SpaceAroundPointerQualifiers == FormatStyle::SAPQ_Both) &&
4675 Right.canBePointerOrReferenceQualifier()) {
4679 if (
Right.Tok.isLiteral())
4682 if (
Right.is(TT_BlockComment))
4686 if (
Right.isOneOf(Keywords.kw_override, Keywords.kw_final, tok::kw_noexcept,
4687 TT_RequiresClause) &&
4688 Right.isNot(TT_StartOfName)) {
4695 if (BeforeLeft && BeforeLeft->isTypeOrIdentifier(LangOpts) &&
Right.Next &&
4696 Right.Next->is(TT_RangeBasedForLoopColon)) {
4697 return getTokenPointerOrReferenceAlignment(Left) !=
4698 FormatStyle::PAS_Right;
4700 if (
Right.isOneOf(TT_PointerOrReference, TT_ArraySubscriptLSquare,
4704 if (getTokenPointerOrReferenceAlignment(Left) == FormatStyle::PAS_Right)
4712 if (
Line.IsMultiVariableDeclStmt &&
4713 (
Left.NestingLevel ==
Line.First->NestingLevel ||
4714 ((
Left.NestingLevel ==
Line.First->NestingLevel + 1) &&
4715 startsWithInitStatement(
Line)))) {
4720 if (BeforeLeft->is(tok::coloncolon)) {
4721 if (
Left.isNot(tok::star))
4723 assert(Style.PointerAlignment != FormatStyle::PAS_Right);
4724 if (!
Right.startsSequence(tok::identifier, tok::r_paren))
4727 const auto *LParen =
Right.Next->MatchingParen;
4728 return !LParen || LParen->isNot(TT_FunctionTypeLParen);
4730 return BeforeLeft->isNoneOf(tok::l_paren, tok::l_square);
4733 if (
Left.is(tok::ellipsis) && BeforeLeft &&
4734 BeforeLeft->isPointerOrReference()) {
4735 return Style.PointerAlignment != FormatStyle::PAS_Right;
4738 if (
Right.is(tok::star) &&
Left.is(tok::l_paren))
4740 if (
Left.is(tok::star) &&
Right.isPointerOrReference())
4742 if (
Right.isPointerOrReference()) {
4753 if (
Previous->is(tok::coloncolon)) {
4772 if (
Previous->endsSequence(tok::kw_operator))
4773 return Style.PointerAlignment != FormatStyle::PAS_Left;
4774 if (
Previous->isOneOf(tok::kw_const, tok::kw_volatile)) {
4775 return (Style.PointerAlignment != FormatStyle::PAS_Left) ||
4776 (Style.SpaceAroundPointerQualifiers ==
4777 FormatStyle::SAPQ_After) ||
4778 (Style.SpaceAroundPointerQualifiers == FormatStyle::SAPQ_Both);
4782 if (Style.isCSharp() &&
Left.is(Keywords.kw_is) &&
Right.is(tok::l_square))
4784 const auto SpaceRequiredForArrayInitializerLSquare =
4785 [](
const FormatToken &LSquareTok,
const FormatStyle &Style) {
4786 return Style.SpacesInContainerLiterals ||
4787 (Style.isProto() && !Style.Cpp11BracedListStyle &&
4788 LSquareTok.endsSequence(tok::l_square, tok::colon,
4791 if (
Left.is(tok::l_square)) {
4792 return (
Left.is(TT_ArrayInitializerLSquare) &&
Right.isNot(tok::r_square) &&
4793 SpaceRequiredForArrayInitializerLSquare(Left, Style)) ||
4794 (
Left.isOneOf(TT_ArraySubscriptLSquare, TT_StructuredBindingLSquare,
4795 TT_LambdaLSquare) &&
4796 Style.SpacesInSquareBrackets &&
Right.isNot(tok::r_square));
4798 if (
Right.is(tok::r_square)) {
4799 return Right.MatchingParen &&
4800 ((
Right.MatchingParen->is(TT_ArrayInitializerLSquare) &&
4801 SpaceRequiredForArrayInitializerLSquare(*
Right.MatchingParen,
4803 (Style.SpacesInSquareBrackets &&
4804 Right.MatchingParen->isOneOf(TT_ArraySubscriptLSquare,
4805 TT_StructuredBindingLSquare,
4806 TT_LambdaLSquare)));
4808 if (
Right.is(tok::l_square) &&
4809 Right.isNoneOf(TT_ObjCMethodExpr, TT_LambdaLSquare,
4810 TT_DesignatedInitializerLSquare,
4811 TT_StructuredBindingLSquare, TT_AttributeSquare) &&
4812 Left.isNoneOf(tok::numeric_constant, TT_DictLiteral) &&
4813 !(
Left.isNot(tok::r_square) && Style.SpaceBeforeSquareBrackets &&
4814 Right.is(TT_ArraySubscriptLSquare))) {
4818 (
Right.is(tok::r_brace) &&
Right.MatchingParen &&
4820 return !Style.Cpp11BracedListStyle || Style.SpacesInParensOptions.Other;
4822 if (
Left.is(TT_BlockComment)) {
4824 return Style.isJavaScript() || !
Left.TokenText.ends_with(
"=*/");
4829 if (
Left.is(TT_TemplateCloser) &&
Right.is(TT_AttributeSquare))
4832 if (
Right.is(tok::l_paren)) {
4833 if (
Left.is(TT_TemplateCloser) &&
Right.isNot(TT_FunctionTypeLParen))
4834 return spaceRequiredBeforeParens(Right);
4835 if (
Left.isOneOf(TT_RequiresClause,
4836 TT_RequiresClauseInARequiresExpression)) {
4837 return Style.SpaceBeforeParensOptions.AfterRequiresInClause ||
4838 spaceRequiredBeforeParens(Right);
4840 if (
Left.is(TT_RequiresExpression)) {
4841 return Style.SpaceBeforeParensOptions.AfterRequiresInExpression ||
4842 spaceRequiredBeforeParens(Right);
4844 if (
Left.is(TT_AttributeRParen) ||
4845 (
Left.is(tok::r_square) &&
Left.is(TT_AttributeSquare))) {
4848 if (
Left.is(TT_ForEachMacro)) {
4849 return Style.SpaceBeforeParensOptions.AfterForeachMacros ||
4850 spaceRequiredBeforeParens(Right);
4852 if (
Left.is(TT_IfMacro)) {
4853 return Style.SpaceBeforeParensOptions.AfterIfMacros ||
4854 spaceRequiredBeforeParens(Right);
4856 if (Style.SpaceBeforeParens == FormatStyle::SBPO_Custom &&
4857 Left.isPlacementOperator() &&
4858 Right.isNot(TT_OverloadedOperatorLParen) &&
4859 !(
Line.MightBeFunctionDecl &&
Left.is(TT_FunctionDeclarationName))) {
4860 const auto *RParen =
Right.MatchingParen;
4861 return Style.SpaceBeforeParensOptions.AfterPlacementOperator ||
4862 (RParen && RParen->is(TT_CastRParen));
4866 if (
Left.is(tok::semi))
4868 if (
Left.isOneOf(tok::pp_elif, tok::kw_for, tok::kw_while, tok::kw_switch,
4869 tok::kw_case, TT_ForEachMacro, TT_ObjCForIn) ||
4871 Right.is(TT_ConditionLParen)) {
4872 return Style.SpaceBeforeParensOptions.AfterControlStatements ||
4873 spaceRequiredBeforeParens(Right);
4878 if (
Right.is(TT_OverloadedOperatorLParen))
4879 return spaceRequiredBeforeParens(Right);
4881 if (
Line.MightBeFunctionDecl &&
Right.is(TT_FunctionDeclarationLParen)) {
4882 if (spaceRequiredBeforeParens(Right))
4884 const auto &Options = Style.SpaceBeforeParensOptions;
4885 return Line.mightBeFunctionDefinition()
4886 ? Options.AfterFunctionDefinitionName
4887 : Options.AfterFunctionDeclarationName;
4891 Left.MatchingParen &&
Left.MatchingParen->is(TT_LambdaLSquare)) {
4892 return Style.SpaceBeforeParensOptions.AfterFunctionDefinitionName ||
4893 spaceRequiredBeforeParens(Right);
4895 if (!BeforeLeft || BeforeLeft->isNoneOf(tok::period, tok::arrow)) {
4896 if (
Left.isOneOf(tok::kw_try, Keywords.kw___except, tok::kw_catch)) {
4897 return Style.SpaceBeforeParensOptions.AfterControlStatements ||
4898 spaceRequiredBeforeParens(Right);
4900 if (
Left.isPlacementOperator() ||
4901 (
Left.is(tok::r_square) &&
Left.MatchingParen &&
4902 Left.MatchingParen->Previous &&
4903 Left.MatchingParen->Previous->is(tok::kw_delete))) {
4904 return Style.SpaceBeforeParens != FormatStyle::SBPO_Never ||
4905 spaceRequiredBeforeParens(Right);
4910 (
Left.Tok.getIdentifierInfo() ||
Left.is(tok::r_paren))) {
4911 return spaceRequiredBeforeParens(Right);
4915 if (
Left.is(tok::at) &&
Right.isNot(tok::objc_not_keyword))
4917 if (
Right.is(TT_UnaryOperator)) {
4918 return Left.isNoneOf(tok::l_paren, tok::l_square, tok::at) &&
4919 (
Left.isNot(tok::colon) ||
Left.isNot(TT_ObjCMethodExpr));
4925 if (!Style.isVerilog() &&
4926 (
Left.isOneOf(tok::identifier, tok::greater, tok::r_square,
4928 Left.isTypeName(LangOpts)) &&
4929 Right.is(tok::l_brace) &&
Right.getNextNonComment() &&
4933 if (
Left.is(tok::period) ||
Right.is(tok::period))
4937 if (
Right.is(tok::hash) &&
Left.is(tok::identifier) &&
4938 (
Left.TokenText ==
"L" ||
Left.TokenText ==
"u" ||
4939 Left.TokenText ==
"U" ||
Left.TokenText ==
"u8" ||
4940 Left.TokenText ==
"LR" ||
Left.TokenText ==
"uR" ||
4941 Left.TokenText ==
"UR" ||
Left.TokenText ==
"u8R")) {
4944 if (
Left.is(TT_TemplateCloser) &&
Left.MatchingParen &&
4945 Left.MatchingParen->Previous &&
4946 Left.MatchingParen->Previous->isOneOf(tok::period, tok::coloncolon)) {
4952 if (
Left.is(TT_TemplateCloser) &&
Right.is(tok::l_square))
4954 if (
Left.is(tok::l_brace) &&
Left.endsSequence(TT_DictLiteral, tok::at)) {
4958 if (
Right.is(tok::r_brace) &&
Right.MatchingParen &&
4959 Right.MatchingParen->endsSequence(TT_DictLiteral, tok::at)) {
4963 if (
Right.is(TT_TrailingAnnotation) &&
Right.isOneOf(tok::amp, tok::ampamp) &&
4964 Left.isOneOf(tok::kw_const, tok::kw_volatile) &&
4965 (!
Right.Next ||
Right.Next->is(tok::semi))) {
4969 return getTokenReferenceAlignment(Right) != FormatStyle::PAS_Left;
4982 return Right.hasWhitespaceBefore();
4984 const bool IsVerilog = Style.isVerilog();
4985 assert(!IsVerilog || !IsCpp);
4988 if (Keywords.isWordLike(Right, IsVerilog) &&
4989 Keywords.isWordLike(Left, IsVerilog)) {
4995 if (
Left.is(tok::star) &&
Right.is(tok::comment))
4998 if (
Left.is(tok::l_brace) &&
Right.is(tok::r_brace) &&
4999 Left.Children.empty()) {
5001 return Style.SpaceInEmptyBraces != FormatStyle::SIEB_Never;
5002 if (Style.Cpp11BracedListStyle) {
5003 return Style.SpacesInParens == FormatStyle::SIPO_Custom &&
5004 Style.SpacesInParensOptions.InEmptyParentheses;
5006 return Style.SpaceInEmptyBraces == FormatStyle::SIEB_Always;
5009 const auto *BeforeLeft =
Left.Previous;
5012 if (
Left.is(TT_OverloadedOperator) &&
5013 Right.isOneOf(TT_TemplateOpener, TT_TemplateCloser)) {
5017 if (
Right.is(tok::period) &&
Left.is(tok::numeric_constant))
5021 if (
Left.is(Keywords.kw_import) &&
Right.isOneOf(tok::less, tok::ellipsis))
5024 if (
Left.isOneOf(Keywords.kw_module, Keywords.kw_import) &&
5025 Right.is(TT_ModulePartitionColon)) {
5029 if (
Right.is(TT_AfterPPDirective))
5033 if (
Left.is(tok::identifier) &&
Right.is(TT_ModulePartitionColon))
5036 if (
Left.is(TT_ModulePartitionColon) &&
5037 Right.isOneOf(tok::identifier, tok::kw_private)) {
5040 if (
Left.is(tok::ellipsis) &&
Right.is(tok::identifier) &&
5041 Line.First->is(Keywords.kw_import)) {
5045 if (
Left.isOneOf(TT_AttributeRParen, TT_AttributeMacro) &&
5046 Right.is(tok::coloncolon)) {
5050 if (
Left.is(tok::kw_operator))
5051 return Right.is(tok::coloncolon) || Style.SpaceAfterOperatorKeyword;
5053 !
Left.opensScope() && Style.SpaceBeforeCpp11BracedList) {
5056 if (
Left.is(tok::less) &&
Left.is(TT_OverloadedOperator) &&
5057 Right.is(TT_TemplateOpener)) {
5061 if (
Left.is(tok::identifier) &&
Right.is(tok::numeric_constant))
5062 return Right.TokenText[0] !=
'.';
5064 if (
Left.Tok.getIdentifierInfo() &&
Right.Tok.isLiteral())
5066 }
else if (Style.isProto()) {
5067 if (
Right.is(tok::period) && !(BeforeLeft && BeforeLeft->is(tok::period)) &&
5068 Left.isOneOf(Keywords.kw_optional, Keywords.kw_required,
5069 Keywords.kw_repeated, Keywords.kw_extend)) {
5072 if (
Right.is(tok::l_paren) &&
5073 Left.isOneOf(Keywords.kw_returns, Keywords.kw_option)) {
5076 if (
Right.isOneOf(tok::l_brace, tok::less) &&
Left.is(TT_SelectorName))
5079 if (
Left.is(tok::slash) ||
Right.is(tok::slash))
5081 if (
Left.MatchingParen &&
5082 Left.MatchingParen->is(TT_ProtoExtensionLSquare) &&
5083 Right.isOneOf(tok::l_brace, tok::less)) {
5084 return !Style.Cpp11BracedListStyle;
5087 if (
Left.is(tok::percent))
5091 if (
Left.is(tok::numeric_constant) &&
Right.is(tok::percent))
5092 return Right.hasWhitespaceBefore();
5093 }
else if (Style.isJson()) {
5094 if (
Right.is(tok::colon) &&
Left.is(tok::string_literal))
5095 return Style.SpaceBeforeJsonColon;
5096 }
else if (Style.isCSharp()) {
5102 if (
Left.is(tok::kw_this) &&
Right.is(tok::l_square))
5106 if (
Left.is(tok::kw_new) &&
Right.is(tok::l_paren))
5110 if (
Right.is(tok::l_brace))
5114 if (
Left.is(tok::l_brace) &&
Right.isNot(tok::r_brace))
5117 if (
Left.isNot(tok::l_brace) &&
Right.is(tok::r_brace))
5121 if (
Left.is(TT_FatArrow) ||
Right.is(TT_FatArrow))
5125 if (
Left.is(TT_AttributeColon) ||
Right.is(TT_AttributeColon))
5129 if (
Left.is(TT_TemplateCloser) &&
Right.is(TT_StartOfName))
5133 if (
Left.is(tok::l_square) ||
Right.is(tok::r_square))
5134 return Style.SpacesInSquareBrackets;
5137 if (
Right.is(TT_CSharpNullable))
5141 if (
Right.is(TT_NonNullAssertion))
5145 if (
Left.is(tok::comma) &&
Right.is(tok::comma))
5149 if (
Left.is(Keywords.kw_var) &&
Right.is(tok::l_paren))
5153 if (
Right.is(tok::l_paren)) {
5154 if (
Left.isOneOf(tok::kw_using, Keywords.kw_async, Keywords.kw_when,
5155 Keywords.kw_lock)) {
5156 return Style.SpaceBeforeParensOptions.AfterControlStatements ||
5157 spaceRequiredBeforeParens(Right);
5163 if ((
Left.isAccessSpecifierKeyword() ||
5164 Left.isOneOf(tok::kw_virtual, tok::kw_extern, tok::kw_static,
5165 Keywords.kw_internal, Keywords.kw_abstract,
5166 Keywords.kw_sealed, Keywords.kw_override,
5167 Keywords.kw_async, Keywords.kw_unsafe)) &&
5168 Right.is(tok::l_paren)) {
5171 }
else if (Style.isJavaScript()) {
5172 if (
Left.is(TT_FatArrow))
5175 if (
Right.is(tok::l_paren) &&
Left.is(Keywords.kw_await) && BeforeLeft &&
5176 BeforeLeft->is(tok::kw_for)) {
5179 if (
Left.is(Keywords.kw_async) &&
Right.is(tok::l_paren) &&
5180 Right.MatchingParen) {
5187 if ((
Left.is(TT_TemplateString) &&
Left.TokenText.ends_with(
"${")) ||
5188 (
Right.is(TT_TemplateString) &&
Right.TokenText.starts_with(
"}"))) {
5193 if (Keywords.isJavaScriptIdentifier(Left,
5195 Right.is(TT_TemplateString)) {
5198 if (
Right.is(tok::star) &&
5199 Left.isOneOf(Keywords.kw_function, Keywords.kw_yield)) {
5202 if (
Right.isOneOf(tok::l_brace, tok::l_square) &&
5203 Left.isOneOf(Keywords.kw_function, Keywords.kw_yield,
5204 Keywords.kw_extends, Keywords.kw_implements)) {
5207 if (
Right.is(tok::l_paren)) {
5209 if (
Line.MustBeDeclaration &&
Left.Tok.getIdentifierInfo())
5213 if (BeforeLeft && BeforeLeft->is(tok::period) &&
5214 Left.Tok.getIdentifierInfo()) {
5218 if (
Left.isOneOf(tok::kw_throw, Keywords.kw_await, Keywords.kw_typeof,
5224 if (
Left.endsSequence(tok::kw_const, Keywords.kw_as))
5226 if ((
Left.isOneOf(Keywords.kw_let, Keywords.kw_var, Keywords.kw_in,
5231 (
Left.is(Keywords.kw_of) && BeforeLeft &&
5232 BeforeLeft->isOneOf(tok::identifier, tok::r_square, tok::r_brace))) &&
5233 (!BeforeLeft || BeforeLeft->isNot(tok::period))) {
5236 if (
Left.isOneOf(tok::kw_for, Keywords.kw_as) && BeforeLeft &&
5237 BeforeLeft->is(tok::period) &&
Right.is(tok::l_paren)) {
5240 if (
Left.is(Keywords.kw_as) &&
5241 Right.isOneOf(tok::l_square, tok::l_brace, tok::l_paren)) {
5244 if (
Left.is(tok::kw_default) && BeforeLeft &&
5245 BeforeLeft->is(tok::kw_export)) {
5248 if (
Left.is(Keywords.kw_is) &&
Right.is(tok::l_brace))
5250 if (
Right.isOneOf(TT_JsTypeColon, TT_JsTypeOptionalQuestion))
5252 if (
Left.is(TT_JsTypeOperator) ||
Right.is(TT_JsTypeOperator))
5254 if ((
Left.is(tok::l_brace) ||
Right.is(tok::r_brace)) &&
5255 Line.First->isOneOf(Keywords.kw_import, tok::kw_export)) {
5258 if (
Left.is(tok::ellipsis))
5260 if (
Left.is(TT_TemplateCloser) &&
5261 Right.isNoneOf(tok::equal, tok::l_brace, tok::comma, tok::l_square,
5262 Keywords.kw_implements, Keywords.kw_extends)) {
5268 if (
Right.is(TT_NonNullAssertion))
5270 if (
Left.is(TT_NonNullAssertion) &&
5271 Right.isOneOf(Keywords.kw_as, Keywords.kw_in)) {
5274 }
else if (Style.isJava()) {
5275 if (
Left.is(TT_CaseLabelArrow) ||
Right.is(TT_CaseLabelArrow))
5277 if (
Left.is(tok::r_square) &&
Right.is(tok::l_brace))
5280 if (
Left.is(tok::l_square) ||
Right.is(tok::r_square))
5281 return Style.SpacesInSquareBrackets;
5283 if (
Left.is(Keywords.kw_synchronized) &&
Right.is(tok::l_paren)) {
5284 return Style.SpaceBeforeParensOptions.AfterControlStatements ||
5285 spaceRequiredBeforeParens(Right);
5287 if ((
Left.isAccessSpecifierKeyword() ||
5288 Left.isOneOf(tok::kw_static, Keywords.kw_final, Keywords.kw_abstract,
5289 Keywords.kw_native)) &&
5290 Right.is(TT_TemplateOpener)) {
5293 }
else if (IsVerilog) {
5295 if (
Left.is(tok::identifier) &&
Left.TokenText[0] ==
'\\')
5299 if ((
Left.is(TT_VerilogTableItem) &&
5300 Right.isNoneOf(tok::r_paren, tok::semi)) ||
5301 (
Right.is(TT_VerilogTableItem) &&
Left.isNot(tok::l_paren))) {
5303 return !(
Next &&
Next->is(tok::r_paren));
5306 if (
Left.isNot(TT_BinaryOperator) &&
5307 Left.isOneOf(Keywords.kw_verilogHash, Keywords.kw_verilogHashHash)) {
5311 if (
Right.isNot(tok::semi) &&
5312 (
Left.endsSequence(tok::numeric_constant, Keywords.kw_verilogHash) ||
5313 Left.endsSequence(tok::numeric_constant,
5314 Keywords.kw_verilogHashHash) ||
5315 (
Left.is(tok::r_paren) &&
Left.MatchingParen &&
5316 Left.MatchingParen->endsSequence(tok::l_paren, tok::at)))) {
5321 if (
Left.is(Keywords.kw_apostrophe) ||
5322 (
Left.is(TT_VerilogNumberBase) &&
Right.is(tok::numeric_constant))) {
5326 if (
Left.is(tok::arrow) ||
Right.is(tok::arrow))
5331 if (
Left.is(tok::at) &&
Right.isOneOf(tok::l_paren, tok::star, tok::at))
5334 if (
Right.is(tok::l_square) &&
5335 Left.isOneOf(TT_VerilogDimensionedTypeName, Keywords.kw_function)) {
5339 if (
Right.isOneOf(tok::period, Keywords.kw_apostrophe) &&
5340 Keywords.isVerilogIdentifier(Left) &&
Left.getPreviousNonComment() &&
5341 Left.getPreviousNonComment()->is(Keywords.kw_tagged)) {
5347 if ((
Right.is(Keywords.kw_apostrophe) ||
5349 Left.isNoneOf(Keywords.kw_assign, Keywords.kw_unique) &&
5350 !Keywords.isVerilogWordOperator(Left) &&
5351 (
Left.isOneOf(tok::r_square, tok::r_paren, tok::r_brace,
5352 tok::numeric_constant) ||
5353 Keywords.isWordLike(Left))) {
5357 if ((
Right.is(tok::star) &&
Left.is(tok::coloncolon)) ||
5358 (
Left.is(tok::star) &&
Right.is(tok::semi))) {
5362 if (
Left.endsSequence(tok::star, tok::l_paren) &&
Right.is(tok::identifier))
5365 if (
Right.is(tok::l_paren) &&
Right.is(TT_VerilogStrength))
5368 if ((
Left.is(tok::l_brace) &&
5369 Right.isOneOf(tok::lessless, tok::greatergreater)) ||
5370 (
Left.endsSequence(tok::lessless, tok::l_brace) ||
5371 Left.endsSequence(tok::greatergreater, tok::l_brace))) {
5374 }
else if (Style.isTableGen()) {
5376 if (
Left.is(tok::l_square) &&
Right.is(tok::l_brace))
5378 if (
Left.is(tok::r_brace) &&
Right.is(tok::r_square))
5381 if (
Right.isOneOf(TT_TableGenDAGArgListColon,
5382 TT_TableGenDAGArgListColonToAlign) ||
5383 Left.isOneOf(TT_TableGenDAGArgListColon,
5384 TT_TableGenDAGArgListColonToAlign)) {
5387 if (
Right.is(TT_TableGenCondOperatorColon))
5389 if (
Left.isOneOf(TT_TableGenDAGArgOperatorID,
5390 TT_TableGenDAGArgOperatorToBreak) &&
5391 Right.isNot(TT_TableGenDAGArgCloser)) {
5395 if (
Right.isOneOf(tok::l_paren, tok::less) &&
5396 Left.isOneOf(TT_TableGenBangOperator, TT_TableGenCondOperator)) {
5401 if (
Left.is(TT_TableGenTrailingPasteOperator) &&
5402 Right.isOneOf(tok::l_brace, tok::colon)) {
5406 if (
Left.is(tok::hash) ||
Right.is(tok::hash))
5409 if (Keywords.isTableGenDefinition(Left))
5413 if (
Left.is(TT_ImplicitStringLiteral))
5414 return Right.hasWhitespaceBefore();
5416 if (
Left.is(TT_ObjCMethodSpecifier))
5418 if (
Left.is(tok::r_paren) &&
Left.isNot(TT_AttributeRParen) &&
5419 canBeObjCSelectorComponent(Right)) {
5427 (
Right.is(tok::equal) ||
Left.is(tok::equal))) {
5431 if (
Right.isOneOf(TT_TrailingReturnArrow, TT_LambdaArrow) ||
5432 Left.isOneOf(TT_TrailingReturnArrow, TT_LambdaArrow)) {
5435 if (
Left.is(tok::comma) &&
Right.isNot(TT_OverloadedOperatorLParen) &&
5438 (
Left.Children.empty() || !
Left.MacroParent)) {
5441 if (
Right.is(tok::comma))
5443 if (
Right.is(TT_ObjCBlockLParen))
5445 if (
Right.is(TT_CtorInitializerColon))
5446 return Style.SpaceBeforeCtorInitializerColon;
5447 if (
Right.is(TT_InheritanceColon) && !Style.SpaceBeforeInheritanceColon)
5449 if (
Right.is(TT_RangeBasedForLoopColon) &&
5450 !Style.SpaceBeforeRangeBasedForLoopColon) {
5453 if (
Left.is(TT_BitFieldColon)) {
5454 return Style.BitFieldColonSpacing == FormatStyle::BFCS_Both ||
5455 Style.BitFieldColonSpacing == FormatStyle::BFCS_After;
5457 if (
Right.is(tok::colon)) {
5458 if (
Right.is(TT_CaseLabelColon))
5459 return Style.SpaceBeforeCaseColon;
5460 if (
Right.is(TT_GotoLabelColon))
5463 if (!
Right.getNextNonComment())
5465 if (
Right.isOneOf(TT_ObjCSelector, TT_ObjCMethodExpr))
5467 if (
Left.is(tok::question))
5469 if (
Right.is(TT_InlineASMColon) &&
Left.is(tok::coloncolon))
5471 if (
Right.is(TT_DictLiteral))
5472 return Style.SpacesInContainerLiterals;
5473 if (
Right.is(TT_AttributeColon))
5475 if (
Right.is(TT_CSharpNamedArgumentColon))
5477 if (
Right.is(TT_GenericSelectionColon))
5479 if (
Right.is(TT_BitFieldColon)) {
5480 return Style.BitFieldColonSpacing == FormatStyle::BFCS_Both ||
5481 Style.BitFieldColonSpacing == FormatStyle::BFCS_Before;
5486 if ((
Left.isOneOf(tok::minus, tok::minusminus) &&
5487 Right.isOneOf(tok::minus, tok::minusminus)) ||
5488 (
Left.isOneOf(tok::plus, tok::plusplus) &&
5489 Right.isOneOf(tok::plus, tok::plusplus))) {
5492 if (
Left.is(TT_UnaryOperator)) {
5495 if (
Left.is(tok::amp) &&
Right.is(tok::r_square))
5496 return Style.SpacesInSquareBrackets;
5497 if (
Left.isNot(tok::exclaim))
5499 if (
Left.TokenText ==
"!")
5500 return Style.SpaceAfterLogicalNot;
5501 assert(
Left.TokenText ==
"not");
5502 return Right.isOneOf(tok::coloncolon, TT_UnaryOperator) ||
5503 (
Right.is(tok::l_paren) && Style.SpaceBeforeParensOptions.AfterNot);
5508 if (
Left.is(TT_CastRParen)) {
5509 return Style.SpaceAfterCStyleCast ||
5510 Right.isOneOf(TT_BinaryOperator, TT_SelectorName);
5513 auto ShouldAddSpacesInAngles = [
this, &
Right]() {
5514 if (this->Style.SpacesInAngles == FormatStyle::SIAS_Always)
5516 if (this->Style.SpacesInAngles == FormatStyle::SIAS_Leave)
5517 return Right.hasWhitespaceBefore();
5521 if (
Left.is(tok::greater) &&
Right.is(tok::greater)) {
5522 if (Style.isTextProto() ||
5523 (Style.Language == FormatStyle::LK_Proto &&
Left.is(TT_DictLiteral))) {
5524 return !Style.Cpp11BracedListStyle;
5526 return Right.is(TT_TemplateCloser) &&
Left.is(TT_TemplateCloser) &&
5527 ((Style.Standard < FormatStyle::LS_Cpp11) ||
5528 ShouldAddSpacesInAngles());
5530 if (
Right.isOneOf(tok::arrow, tok::arrowstar, tok::periodstar) ||
5531 Left.isOneOf(tok::arrow, tok::period, tok::arrowstar, tok::periodstar) ||
5532 (
Right.is(tok::period) &&
Right.isNot(TT_DesignatedInitializerPeriod))) {
5535 if (!Style.SpaceBeforeAssignmentOperators &&
Left.isNot(TT_TemplateCloser) &&
5539 if (Style.isJava() &&
Right.is(tok::coloncolon) &&
5540 Left.isOneOf(tok::identifier, tok::kw_this)) {
5543 if (
Right.is(tok::coloncolon) &&
Left.is(tok::identifier)) {
5547 return Right.hasWhitespaceBefore();
5549 if (
Right.is(tok::coloncolon) &&
5550 Left.isNoneOf(tok::l_brace, tok::comment, tok::l_paren)) {
5552 return (
Left.is(TT_TemplateOpener) &&
5553 ((Style.Standard < FormatStyle::LS_Cpp11) ||
5554 ShouldAddSpacesInAngles())) ||
5555 Left.isNoneOf(tok::l_paren, tok::r_paren, tok::l_square,
5556 tok::kw___super, TT_TemplateOpener,
5557 TT_TemplateCloser) ||
5558 (
Left.is(tok::l_paren) && Style.SpacesInParensOptions.Other);
5560 if ((
Left.is(TT_TemplateOpener)) != (
Right.is(TT_TemplateCloser)))
5561 return ShouldAddSpacesInAngles();
5562 if (
Left.is(tok::r_paren) &&
Left.isNot(TT_TypeDeclarationParen) &&
5563 Right.is(TT_PointerOrReference) &&
Right.isOneOf(tok::amp, tok::ampamp)) {
5567 if (
Right.is(TT_StructuredBindingLSquare)) {
5568 return Left.isNoneOf(tok::amp, tok::ampamp) ||
5569 getTokenReferenceAlignment(Left) != FormatStyle::PAS_Right;
5572 if (
Right.Next &&
Right.Next->is(TT_StructuredBindingLSquare) &&
5573 Right.isOneOf(tok::amp, tok::ampamp)) {
5574 return getTokenReferenceAlignment(Right) != FormatStyle::PAS_Left;
5576 if ((
Right.is(TT_BinaryOperator) &&
Left.isNot(tok::l_paren)) ||
5577 (
Left.isOneOf(TT_BinaryOperator, TT_ConditionalExpr) &&
5578 Right.isNot(tok::r_paren))) {
5581 if (
Right.is(TT_TemplateOpener) &&
Left.is(tok::r_paren) &&
5582 Left.MatchingParen &&
5583 Left.MatchingParen->is(TT_OverloadedOperatorLParen)) {
5586 if (
Right.is(tok::less) &&
Left.isNot(tok::l_paren) &&
5590 if (
Right.is(TT_TrailingUnaryOperator))
5592 if (
Left.is(TT_RegexLiteral))
5594 return spaceRequiredBetween(
Line, Left, Right);
5600 Tok.isNoneOf(TT_ObjCBlockLBrace, TT_LambdaLBrace, TT_DictLiteral);
5605 return Tok.MatchingParen &&
Tok.MatchingParen->Next &&
5606 Tok.MatchingParen->Next->isOneOf(tok::comma, tok::r_paren,
5612 FormatStyle::ShortLambdaStyle ShortLambdaOption) {
5613 return Tok.Children.empty() && ShortLambdaOption != FormatStyle::SLS_None;
5618 Tok.isNoneOf(TT_ObjCBlockLBrace, TT_DictLiteral);
5621bool TokenAnnotator::mustBreakBefore(
const AnnotatedLine &
Line,
5623 if (Right.NewlinesBefore > 1 && Style.MaxEmptyLinesToKeep > 0 &&
5624 (!Style.RemoveEmptyLinesInUnwrappedLines || &Right ==
Line.First)) {
5630 if (Style.BreakFunctionDefinitionParameters &&
Line.MightBeFunctionDecl &&
5631 Line.mightBeFunctionDefinition() && Left.MightBeFunctionDeclParen &&
5632 Left.ParameterCount > 0) {
5638 if (Style.BinPackParameters == FormatStyle::BPPS_AlwaysOnePerLine &&
5639 Line.MightBeFunctionDecl && !
Left.opensScope() &&
5644 const auto *BeforeLeft =
Left.Previous;
5645 const auto *AfterRight =
Right.Next;
5647 if (Style.isCSharp()) {
5648 if (
Left.is(TT_FatArrow) &&
Right.is(tok::l_brace) &&
5649 Style.BraceWrapping.AfterFunction) {
5652 if (
Right.is(TT_CSharpNamedArgumentColon) ||
5653 Left.is(TT_CSharpNamedArgumentColon)) {
5656 if (
Right.is(TT_CSharpGenericTypeConstraint))
5658 if (AfterRight && AfterRight->is(TT_FatArrow) &&
5659 (
Right.is(tok::numeric_constant) ||
5660 (
Right.is(tok::identifier) &&
Right.TokenText ==
"_"))) {
5665 if (
Left.is(TT_AttributeSquare) &&
Left.is(tok::r_square) &&
5666 (
Right.isAccessSpecifier(
false) ||
5667 Right.is(Keywords.kw_internal))) {
5671 if (
Left.is(TT_AttributeSquare) &&
Right.is(TT_AttributeSquare) &&
5672 Left.is(tok::r_square) &&
Right.is(tok::l_square)) {
5675 }
else if (Style.isJavaScript()) {
5677 if (
Right.is(tok::string_literal) &&
Left.is(tok::plus) && BeforeLeft &&
5678 BeforeLeft->is(tok::string_literal)) {
5681 if (
Left.is(TT_DictLiteral) &&
Left.is(tok::l_brace) &&
Line.Level == 0 &&
5682 BeforeLeft && BeforeLeft->is(tok::equal) &&
5683 Line.First->isOneOf(tok::identifier, Keywords.kw_import, tok::kw_export,
5687 Line.First->isNoneOf(Keywords.kw_var, Keywords.kw_let)) {
5692 if (
Left.is(tok::l_brace) &&
Line.Level == 0 &&
5693 (
Line.startsWith(tok::kw_enum) ||
5694 Line.startsWith(tok::kw_const, tok::kw_enum) ||
5695 Line.startsWith(tok::kw_export, tok::kw_enum) ||
5696 Line.startsWith(tok::kw_export, tok::kw_const, tok::kw_enum))) {
5701 if (
Right.is(tok::r_brace) &&
Left.is(tok::l_brace) && BeforeLeft &&
5702 BeforeLeft->is(TT_FatArrow)) {
5704 switch (Style.AllowShortLambdasOnASingleLine) {
5705 case FormatStyle::SLS_All:
5707 case FormatStyle::SLS_None:
5709 case FormatStyle::SLS_Empty:
5710 return !
Left.Children.empty();
5711 case FormatStyle::SLS_Inline:
5714 return (
Left.NestingLevel == 0 &&
Line.Level == 0) &&
5715 !
Left.Children.empty();
5717 llvm_unreachable(
"Unknown FormatStyle::ShortLambdaStyle enum");
5720 if (
Right.is(tok::r_brace) &&
Left.is(tok::l_brace) &&
5721 !
Left.Children.empty()) {
5723 return Style.AllowShortFunctionsOnASingleLine == FormatStyle::SFS_None ||
5724 Style.AllowShortFunctionsOnASingleLine == FormatStyle::SFS_Empty ||
5725 (
Left.NestingLevel == 0 &&
Line.Level == 0 &&
5726 Style.AllowShortFunctionsOnASingleLine &
5727 FormatStyle::SFS_InlineOnly);
5729 }
else if (Style.isJava()) {
5730 if (
Right.is(tok::plus) &&
Left.is(tok::string_literal) && AfterRight &&
5731 AfterRight->is(tok::string_literal)) {
5734 }
else if (Style.isVerilog()) {
5736 if (
Left.is(TT_VerilogAssignComma))
5739 if (
Left.is(TT_VerilogTypeComma))
5743 if (Style.VerilogBreakBetweenInstancePorts &&
5744 (
Left.is(TT_VerilogInstancePortComma) ||
5745 (
Left.is(tok::r_paren) && Keywords.isVerilogIdentifier(Right) &&
5746 Left.MatchingParen &&
5747 Left.MatchingParen->is(TT_VerilogInstancePortLParen)))) {
5752 if (!Keywords.isVerilogBegin(Right) && Keywords.isVerilogEndOfLabel(Left))
5754 }
else if (Style.BreakAdjacentStringLiterals &&
5755 (IsCpp || Style.isProto() || Style.isTableGen())) {
5756 if (
Left.isStringLiteral() &&
Right.isStringLiteral())
5761 if (Style.isJson()) {
5765 if (
Left.is(TT_DictLiteral) &&
Left.is(tok::l_brace))
5768 if ((
Left.is(TT_ArrayInitializerLSquare) &&
Left.is(tok::l_square) &&
5769 Right.isNot(tok::r_square)) ||
5770 Left.is(tok::comma)) {
5771 if (
Right.is(tok::l_brace))
5776 if (
Tok->isOneOf(tok::l_brace, tok::l_square))
5778 if (
Tok->isOneOf(tok::r_brace, tok::r_square))
5781 return Style.BreakArrays;
5783 }
else if (Style.isTableGen()) {
5787 if (
Left.is(TT_TableGenCondOperatorComma))
5789 if (
Left.is(TT_TableGenDAGArgOperatorToBreak) &&
5790 Right.isNot(TT_TableGenDAGArgCloser)) {
5793 if (
Left.is(TT_TableGenDAGArgListCommaToBreak))
5795 if (
Right.is(TT_TableGenDAGArgCloser) &&
Right.MatchingParen &&
5796 Right.MatchingParen->is(TT_TableGenDAGArgOpenerToBreak) &&
5797 &Left !=
Right.MatchingParen->Next) {
5799 return Style.TableGenBreakInsideDAGArg == FormatStyle::DAS_BreakAll;
5803 if (
Line.startsWith(tok::kw_asm) &&
Right.is(TT_InlineASMColon) &&
5804 Style.BreakBeforeInlineASMColon == FormatStyle::BBIAS_Always) {
5814 if ((
Left.isOneOf(tok::l_brace, TT_ArrayInitializerLSquare) ||
5815 (Style.isJavaScript() &&
Left.is(tok::l_paren))) &&
5817 BeforeClosingBrace =
Left.MatchingParen->Previous;
5818 }
else if (
Right.MatchingParen &&
5819 (
Right.MatchingParen->isOneOf(tok::l_brace,
5820 TT_ArrayInitializerLSquare) ||
5821 (Style.isJavaScript() &&
5822 Right.MatchingParen->is(tok::l_paren)))) {
5823 BeforeClosingBrace = &
Left;
5825 if (BeforeClosingBrace && (BeforeClosingBrace->is(tok::comma) ||
5826 BeforeClosingBrace->isTrailingComment())) {
5831 if (
Right.is(tok::comment)) {
5833 Right.NewlinesBefore > 0 &&
Right.HasUnescapedNewline;
5835 if (
Left.isTrailingComment())
5837 if (
Left.IsUnterminatedLiteral)
5840 if (BeforeLeft && BeforeLeft->is(tok::lessless) &&
5841 Left.is(tok::string_literal) &&
Right.is(tok::lessless) && AfterRight &&
5842 AfterRight->is(tok::string_literal)) {
5843 return Right.NewlinesBefore > 0;
5846 if (
Right.is(TT_RequiresClause)) {
5847 switch (Style.RequiresClausePosition) {
5848 case FormatStyle::RCPS_OwnLine:
5849 case FormatStyle::RCPS_OwnLineWithBrace:
5850 case FormatStyle::RCPS_WithFollowing:
5857 if (
Left.ClosesTemplateDeclaration &&
Left.MatchingParen &&
5858 Left.MatchingParen->NestingLevel == 0) {
5862 if (
Right.is(tok::kw_concept))
5863 return Style.BreakBeforeConceptDeclarations == FormatStyle::BBCDS_Always;
5864 return Style.BreakTemplateDeclarations == FormatStyle::BTDS_Yes ||
5865 (Style.BreakTemplateDeclarations == FormatStyle::BTDS_Leave &&
5866 Right.NewlinesBefore > 0);
5868 if (
Left.ClosesRequiresClause) {
5869 switch (Style.RequiresClausePosition) {
5870 case FormatStyle::RCPS_OwnLine:
5871 case FormatStyle::RCPS_WithPreceding:
5872 return Right.isNot(tok::semi);
5873 case FormatStyle::RCPS_OwnLineWithBrace:
5874 return Right.isNoneOf(tok::semi, tok::l_brace);
5879 if (Style.PackConstructorInitializers == FormatStyle::PCIS_Never) {
5880 if (Style.BreakConstructorInitializers == FormatStyle::BCIS_BeforeColon &&
5881 (
Left.is(TT_CtorInitializerComma) ||
5882 Right.is(TT_CtorInitializerColon))) {
5886 if (Style.BreakConstructorInitializers == FormatStyle::BCIS_AfterColon &&
5887 Left.isOneOf(TT_CtorInitializerColon, TT_CtorInitializerComma)) {
5891 if (Style.PackConstructorInitializers < FormatStyle::PCIS_CurrentLine &&
5892 Style.BreakConstructorInitializers == FormatStyle::BCIS_BeforeComma &&
5893 Right.isOneOf(TT_CtorInitializerComma, TT_CtorInitializerColon)) {
5896 if (Style.PackConstructorInitializers == FormatStyle::PCIS_NextLineOnly) {
5897 if ((Style.BreakConstructorInitializers == FormatStyle::BCIS_BeforeColon ||
5898 Style.BreakConstructorInitializers == FormatStyle::BCIS_BeforeComma) &&
5899 Right.is(TT_CtorInitializerColon)) {
5903 if (Style.BreakConstructorInitializers == FormatStyle::BCIS_AfterColon &&
5904 Left.is(TT_CtorInitializerColon)) {
5909 if (Style.BreakInheritanceList == FormatStyle::BILS_BeforeComma &&
5910 Right.is(TT_InheritanceComma)) {
5913 if (Style.BreakInheritanceList == FormatStyle::BILS_AfterComma &&
5914 Left.is(TT_InheritanceComma)) {
5917 if (
Right.is(tok::string_literal) &&
Right.TokenText.starts_with(
"R\"")) {
5921 return Right.IsMultiline &&
Right.NewlinesBefore > 0;
5923 if ((
Left.is(tok::l_brace) ||
5924 (
Left.is(tok::less) && BeforeLeft && BeforeLeft->is(tok::equal))) &&
5925 Right.NestingLevel == 1 && Style.Language == FormatStyle::LK_Proto) {
5930 if (
Right.is(TT_InlineASMBrace))
5931 return Right.HasUnescapedNewline;
5934 auto *FirstNonComment =
Line.getFirstNonComment();
5936 FirstNonComment && (FirstNonComment->is(Keywords.kw_internal) ||
5937 FirstNonComment->isAccessSpecifierKeyword());
5939 if (Style.BraceWrapping.AfterEnum) {
5940 if (
Line.startsWith(tok::kw_enum) ||
5941 Line.startsWith(tok::kw_typedef, tok::kw_enum)) {
5946 FirstNonComment->Next->is(tok::kw_enum)) {
5952 if (Style.BraceWrapping.AfterClass &&
5954 FirstNonComment->Next->is(Keywords.kw_interface)) ||
5955 Line.startsWith(Keywords.kw_interface))) {
5960 if (
Right.isNot(TT_FunctionLBrace)) {
5961 return (
Line.startsWith(tok::kw_class) &&
5962 Style.BraceWrapping.AfterClass) ||
5963 (
Line.startsWith(tok::kw_struct) &&
5964 Style.BraceWrapping.AfterStruct);
5968 if (
Left.is(TT_ObjCBlockLBrace) &&
5969 Style.AllowShortBlocksOnASingleLine == FormatStyle::SBS_Never) {
5974 if (
Left.isOneOf(TT_AttributeRParen, TT_AttributeMacro) &&
5975 Right.is(TT_ObjCDecl)) {
5979 if (
Left.is(TT_LambdaLBrace)) {
5981 Style.AllowShortLambdasOnASingleLine == FormatStyle::SLS_Inline) {
5985 if (Style.AllowShortLambdasOnASingleLine == FormatStyle::SLS_None ||
5986 Style.AllowShortLambdasOnASingleLine == FormatStyle::SLS_Inline ||
5987 (!
Left.Children.empty() &&
5988 Style.AllowShortLambdasOnASingleLine == FormatStyle::SLS_Empty)) {
5993 if (Style.BraceWrapping.BeforeLambdaBody &&
Right.is(TT_LambdaLBrace) &&
5994 (
Left.isPointerOrReference() ||
Left.is(TT_TemplateCloser))) {
5999 if ((Style.isJava() || Style.isJavaScript()) &&
6000 Left.is(TT_LeadingJavaAnnotation) &&
6001 Right.isNoneOf(TT_LeadingJavaAnnotation, tok::l_paren) &&
6002 (
Line.Last->is(tok::l_brace) || Style.BreakAfterJavaFieldAnnotations)) {
6006 if (
Right.is(TT_ProtoExtensionLSquare))
6036 if (Style.isProto() &&
Right.is(TT_SelectorName) &&
6037 Right.isNot(tok::r_square) && AfterRight) {
6040 if (
Left.is(tok::at))
6046 const auto *LBrace = AfterRight;
6047 if (LBrace && LBrace->is(tok::colon)) {
6048 LBrace = LBrace->Next;
6049 if (LBrace && LBrace->is(tok::at)) {
6050 LBrace = LBrace->Next;
6052 LBrace = LBrace->Next;
6064 ((LBrace->is(tok::l_brace) &&
6065 (LBrace->is(TT_DictLiteral) ||
6066 (LBrace->Next && LBrace->Next->is(tok::r_brace)))) ||
6067 LBrace->isOneOf(TT_ArrayInitializerLSquare, tok::less))) {
6074 if (
Left.ParameterCount == 0)
6089 if (
Left.isOneOf(tok::r_brace, tok::greater, tok::r_square))
6100 if (Style.isCSharp()) {
6101 if (
Left.isOneOf(TT_CSharpNamedArgumentColon, TT_AttributeColon) ||
6102 Right.isOneOf(TT_CSharpNamedArgumentColon, TT_AttributeColon)) {
6106 if (
Line.First->is(TT_CSharpGenericTypeConstraint))
6107 return Left.is(TT_CSharpGenericTypeConstraintComma);
6109 if (
Right.is(TT_CSharpNullable))
6111 }
else if (Style.isJava()) {
6112 if (
Left.isOneOf(Keywords.kw_throws, Keywords.kw_extends,
6113 Keywords.kw_implements)) {
6116 if (
Right.isOneOf(Keywords.kw_throws, Keywords.kw_extends,
6117 Keywords.kw_implements)) {
6120 }
else if (Style.isJavaScript()) {
6123 (NonComment->isAccessSpecifierKeyword() ||
6124 NonComment->isOneOf(
6125 tok::kw_return, Keywords.kw_yield, tok::kw_continue, tok::kw_break,
6126 tok::kw_throw, Keywords.kw_interface, Keywords.kw_type,
6127 tok::kw_static, Keywords.kw_readonly, Keywords.kw_override,
6128 Keywords.kw_abstract, Keywords.kw_get, Keywords.kw_set,
6129 Keywords.kw_async, Keywords.kw_await))) {
6132 if (
Right.NestingLevel == 0 &&
6133 (
Left.Tok.getIdentifierInfo() ||
6134 Left.isOneOf(tok::r_square, tok::r_paren)) &&
6135 Right.isOneOf(tok::l_square, tok::l_paren)) {
6138 if (NonComment && NonComment->is(tok::identifier) &&
6139 NonComment->TokenText ==
"asserts") {
6142 if (
Left.is(TT_FatArrow) &&
Right.is(tok::l_brace))
6144 if (
Left.is(TT_JsTypeColon))
6147 if (
Left.is(tok::exclaim) &&
Right.is(tok::colon))
6152 if (
Right.is(Keywords.kw_is)) {
6161 if (!
Next ||
Next->isNot(tok::colon))
6164 if (
Left.is(Keywords.kw_in))
6165 return Style.BreakBeforeBinaryOperators == FormatStyle::BOS_None;
6166 if (
Right.is(Keywords.kw_in))
6167 return Style.BreakBeforeBinaryOperators != FormatStyle::BOS_None;
6168 if (
Right.is(Keywords.kw_as))
6170 if (
Right.isOneOf(Keywords.kw_extends, Keywords.kw_infer)) {
6176 if (
Left.is(Keywords.kw_as))
6178 if (
Left.is(TT_NonNullAssertion))
6180 if (
Left.is(Keywords.kw_declare) &&
6181 Right.isOneOf(Keywords.kw_module, tok::kw_namespace,
6182 Keywords.kw_function, tok::kw_class, tok::kw_enum,
6183 Keywords.kw_interface, Keywords.kw_type, Keywords.kw_var,
6184 Keywords.kw_let, tok::kw_const)) {
6189 if (
Left.isOneOf(Keywords.kw_module, tok::kw_namespace) &&
6190 Right.isOneOf(tok::identifier, tok::string_literal)) {
6193 if (
Right.is(TT_TemplateString) &&
Right.closesScope())
6197 if (
Left.is(tok::identifier) &&
Right.is(TT_TemplateString))
6199 if (
Left.is(TT_TemplateString) &&
Left.opensScope())
6201 }
else if (Style.isTableGen()) {
6203 if (Keywords.isTableGenDefinition(Left))
6206 if (
Right.is(tok::l_paren)) {
6207 return Left.isNoneOf(TT_TableGenBangOperator, TT_TableGenCondOperator,
6211 if (
Left.is(TT_TableGenValueSuffix))
6214 if (
Left.is(tok::hash) ||
Right.is(tok::hash))
6216 if (
Left.isOneOf(TT_TableGenBangOperator, TT_TableGenCondOperator))
6223 if (
Right.is(tok::r_brace)) {
6225 (
Right.isBlockIndentedInitRBrace(Style)));
6229 if (
Right.is(tok::r_paren)) {
6230 if (Style.AlignAfterOpenBracket != FormatStyle::BAS_BlockIndent ||
6231 !
Right.MatchingParen) {
6235 if (
Next &&
Next->is(tok::r_paren))
6237 if (
Next &&
Next->is(tok::l_paren))
6243 if (
Left.isOneOf(tok::r_paren, TT_TrailingAnnotation) &&
6244 Right.is(TT_TrailingAnnotation) &&
6245 Style.AlignAfterOpenBracket == FormatStyle::BAS_BlockIndent) {
6249 if (
Right.is(TT_TemplateCloser))
6250 return Style.BreakBeforeTemplateCloser;
6252 if (
Left.isOneOf(tok::at, tok::objc_interface))
6254 if (
Left.isOneOf(TT_JavaAnnotation, TT_LeadingJavaAnnotation))
6255 return Right.isNot(tok::l_paren);
6256 if (
Right.is(TT_PointerOrReference)) {
6257 return Line.IsMultiVariableDeclStmt ||
6258 (getTokenPointerOrReferenceAlignment(Right) ==
6259 FormatStyle::PAS_Right &&
6261 Right.Next->isOneOf(TT_FunctionDeclarationName, tok::kw_const)));
6263 if (
Right.isOneOf(TT_StartOfName, TT_FunctionDeclarationName,
6264 TT_ClassHeadName, TT_QtProperty, tok::kw_operator)) {
6267 if (
Left.is(TT_PointerOrReference))
6269 if (
Right.isTrailingComment()) {
6276 (
Left.is(TT_CtorInitializerColon) &&
Right.NewlinesBefore > 0 &&
6277 Style.BreakConstructorInitializers == FormatStyle::BCIS_AfterColon);
6279 if (
Left.is(tok::question) &&
Right.is(tok::colon))
6281 if (
Right.isOneOf(TT_ConditionalExpr, tok::question))
6282 return Style.BreakBeforeTernaryOperators;
6283 if (
Left.isOneOf(TT_ConditionalExpr, tok::question))
6284 return !Style.BreakBeforeTernaryOperators;
6285 if (
Left.is(TT_InheritanceColon))
6286 return Style.BreakInheritanceList == FormatStyle::BILS_AfterColon;
6287 if (
Right.is(TT_InheritanceColon))
6288 return Style.BreakInheritanceList != FormatStyle::BILS_AfterColon;
6290 if (
Right.is(TT_ObjCMethodExpr) &&
Right.isNot(tok::r_square) &&
6291 Left.isNot(TT_SelectorName)) {
6295 if (
Right.is(tok::colon) &&
6296 Right.isNoneOf(TT_CtorInitializerColon, TT_InlineASMColon,
6297 TT_BitFieldColon)) {
6300 if (
Left.is(tok::colon) &&
Left.isOneOf(TT_DictLiteral, TT_ObjCMethodExpr)) {
6301 if (Style.isProto()) {
6302 if (!Style.AlwaysBreakBeforeMultilineStrings &&
Right.isStringLiteral())
6328 if ((
Right.isOneOf(tok::l_brace, tok::less) &&
6329 Right.is(TT_DictLiteral)) ||
6330 Right.is(TT_ArrayInitializerLSquare)) {
6336 if (
Right.is(tok::r_square) &&
Right.MatchingParen &&
6337 Right.MatchingParen->is(TT_ProtoExtensionLSquare)) {
6340 if (
Right.is(TT_SelectorName) || (
Right.is(tok::identifier) &&
Right.Next &&
6341 Right.Next->is(TT_ObjCMethodExpr))) {
6342 return Left.isNot(tok::period);
6346 if (
Right.is(tok::kw_concept))
6347 return Style.BreakBeforeConceptDeclarations != FormatStyle::BBCDS_Never;
6348 if (
Right.is(TT_RequiresClause))
6350 if (
Left.ClosesTemplateDeclaration) {
6351 return Style.BreakTemplateDeclarations != FormatStyle::BTDS_Leave ||
6352 Right.NewlinesBefore > 0;
6354 if (
Left.is(TT_FunctionAnnotationRParen))
6356 if (
Left.ClosesRequiresClause)
6358 if (
Right.isOneOf(TT_RangeBasedForLoopColon, TT_OverloadedOperatorLParen,
6359 TT_OverloadedOperator)) {
6362 if (
Left.is(TT_RangeBasedForLoopColon))
6364 if (
Right.is(TT_RangeBasedForLoopColon))
6366 if (
Left.is(TT_TemplateCloser) &&
Right.is(TT_TemplateOpener))
6368 if ((
Left.is(tok::greater) &&
Right.is(tok::greater)) ||
6369 (
Left.is(tok::less) &&
Right.is(tok::less))) {
6372 if (
Right.is(TT_BinaryOperator) &&
6373 Style.BreakBeforeBinaryOperators != FormatStyle::BOS_None &&
6374 (Style.BreakBeforeBinaryOperators == FormatStyle::BOS_All ||
6378 if (
Left.isOneOf(TT_TemplateCloser, TT_UnaryOperator, tok::kw_operator))
6380 if (
Left.is(tok::equal) &&
Right.isNoneOf(tok::kw_default, tok::kw_delete) &&
6384 if (
Left.is(tok::equal) &&
Right.is(tok::l_brace) &&
6385 !Style.Cpp11BracedListStyle) {
6388 if (
Left.is(TT_AttributeLParen) ||
6389 (
Left.is(tok::l_paren) &&
Left.is(TT_TypeDeclarationParen))) {
6392 if (
Left.is(tok::l_paren) &&
Left.Previous &&
6393 (
Left.Previous->isOneOf(TT_BinaryOperator, TT_CastRParen))) {
6396 if (
Right.is(TT_ImplicitStringLiteral))
6399 if (
Right.is(tok::r_square) &&
Right.MatchingParen &&
6400 Right.MatchingParen->is(TT_LambdaLSquare)) {
6406 if (
Left.is(TT_TrailingAnnotation)) {
6407 return Right.isNoneOf(tok::l_brace, tok::semi, tok::equal, tok::l_paren,
6408 tok::less, tok::coloncolon);
6411 if (
Right.isAttribute())
6414 if (
Right.is(tok::l_square) &&
Right.is(TT_AttributeSquare))
6415 return Left.isNot(TT_AttributeSquare);
6417 if (
Left.is(tok::identifier) &&
Right.is(tok::string_literal))
6420 if (
Right.is(tok::identifier) &&
Right.Next &&
Right.Next->is(TT_DictLiteral))
6423 if (
Left.is(TT_CtorInitializerColon)) {
6424 return Style.BreakConstructorInitializers == FormatStyle::BCIS_AfterColon &&
6425 (!
Right.isTrailingComment() ||
Right.NewlinesBefore > 0);
6427 if (
Right.is(TT_CtorInitializerColon))
6428 return Style.BreakConstructorInitializers != FormatStyle::BCIS_AfterColon;
6429 if (
Left.is(TT_CtorInitializerComma) &&
6430 Style.BreakConstructorInitializers == FormatStyle::BCIS_BeforeComma) {
6433 if (
Right.is(TT_CtorInitializerComma) &&
6434 Style.BreakConstructorInitializers == FormatStyle::BCIS_BeforeComma) {
6437 if (
Left.is(TT_InheritanceComma) &&
6438 Style.BreakInheritanceList == FormatStyle::BILS_BeforeComma) {
6441 if (
Right.is(TT_InheritanceComma) &&
6442 Style.BreakInheritanceList == FormatStyle::BILS_BeforeComma) {
6445 if (
Left.is(TT_ArrayInitializerLSquare))
6447 if (
Right.is(tok::kw_typename) &&
Left.isNot(tok::kw_const))
6449 if ((
Left.isBinaryOperator() ||
Left.is(TT_BinaryOperator)) &&
6450 Left.isNoneOf(tok::arrowstar, tok::lessless) &&
6451 Style.BreakBeforeBinaryOperators != FormatStyle::BOS_All &&
6452 (Style.BreakBeforeBinaryOperators == FormatStyle::BOS_None ||
6456 if ((
Left.is(TT_AttributeSquare) &&
Right.is(tok::l_square)) ||
6457 (
Left.is(tok::r_square) &&
Right.is(TT_AttributeSquare))) {
6461 auto ShortLambdaOption = Style.AllowShortLambdasOnASingleLine;
6462 if (Style.BraceWrapping.BeforeLambdaBody &&
Right.is(TT_LambdaLBrace)) {
6469 if (
Right.is(tok::kw_noexcept) &&
Right.is(TT_TrailingAnnotation)) {
6470 switch (Style.AllowBreakBeforeNoexceptSpecifier) {
6471 case FormatStyle::BBNSS_Never:
6473 case FormatStyle::BBNSS_Always:
6475 case FormatStyle::BBNSS_OnlyWithParen:
6476 return Right.Next &&
Right.Next->is(tok::l_paren);
6480 return Left.isOneOf(tok::comma, tok::coloncolon, tok::semi, tok::l_brace,
6481 tok::kw_class, tok::kw_struct, tok::comment) ||
6482 Right.isMemberAccess() ||
6483 Right.isOneOf(TT_TrailingReturnArrow, TT_LambdaArrow, tok::lessless,
6484 tok::colon, tok::l_square, tok::at) ||
6485 (
Left.is(tok::r_paren) &&
6486 Right.isOneOf(tok::identifier, tok::kw_const)) ||
6487 (
Left.is(tok::l_paren) &&
Right.isNot(tok::r_paren)) ||
6488 (
Left.is(TT_TemplateOpener) &&
Right.isNot(TT_TemplateCloser));
6492 llvm::errs() <<
"AnnotatedTokens(L=" <<
Line.Level <<
", P=" <<
Line.PPLevel
6493 <<
", T=" <<
Line.Type <<
", C=" <<
Line.IsContinuation
6497 llvm::errs() <<
" I=" <<
Tok->IndentLevel <<
" M=" <<
Tok->MustBreakBefore
6498 <<
" C=" <<
Tok->CanBreakBefore
6500 <<
" S=" <<
Tok->SpacesRequiredBefore
6501 <<
" F=" <<
Tok->Finalized <<
" B=" <<
Tok->BlockParameterCount
6502 <<
" BK=" <<
Tok->getBlockKind() <<
" P=" <<
Tok->SplitPenalty
6503 <<
" Name=" <<
Tok->Tok.getName() <<
" N=" <<
Tok->NestingLevel
6504 <<
" L=" <<
Tok->TotalLength
6505 <<
" PPK=" <<
Tok->getPackingKind() <<
" FakeLParens=";
6507 llvm::errs() << LParen <<
"/";
6508 llvm::errs() <<
" FakeRParens=" <<
Tok->FakeRParens;
6509 llvm::errs() <<
" II=" <<
Tok->Tok.getIdentifierInfo();
6510 llvm::errs() <<
" Text='" <<
Tok->TokenText <<
"'\n";
6515 llvm::errs() <<
"----\n";
6518FormatStyle::PointerAlignmentStyle
6520 assert(
Reference.isOneOf(tok::amp, tok::ampamp));
6521 switch (Style.ReferenceAlignment) {
6522 case FormatStyle::RAS_Pointer:
6523 return Style.PointerAlignment;
6524 case FormatStyle::RAS_Left:
6525 return FormatStyle::PAS_Left;
6526 case FormatStyle::RAS_Right:
6527 return FormatStyle::PAS_Right;
6528 case FormatStyle::RAS_Middle:
6529 return FormatStyle::PAS_Middle;
6532 return Style.PointerAlignment;
6535FormatStyle::PointerAlignmentStyle
6536TokenAnnotator::getTokenPointerOrReferenceAlignment(
6538 if (PointerOrReference.isOneOf(tok::amp, tok::ampamp))
6539 return getTokenReferenceAlignment(PointerOrReference);
6540 assert(PointerOrReference.is(tok::star));
6541 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.