19#include "llvm/ADT/SmallPtrSet.h"
20#include "llvm/Support/Debug.h"
22#define DEBUG_TYPE "format-token-annotator"
43static bool startsWithInitStatement(
const AnnotatedLine &
Line) {
44 return Line.startsWith(tok::kw_for) ||
Line.startsWith(tok::kw_if) ||
45 Line.startsWith(tok::kw_switch);
59static bool canBeObjCSelectorComponent(
const FormatToken &Tok) {
60 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);
81static bool isKeywordWithCondition(
const FormatToken &Tok) {
82 return Tok.isOneOf(tok::kw_if, tok::kw_for, tok::kw_while, tok::kw_switch,
83 tok::kw_constexpr, tok::kw_catch);
87static bool isCppAttribute(
bool IsCpp,
const FormatToken &Tok) {
88 if (!IsCpp || !Tok.startsSequence(tok::l_square, tok::l_square))
91 if (Tok.Previous && Tok.Previous->is(tok::at))
93 const FormatToken *AttrTok = Tok.Next->Next;
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),
129 IsCpp(Style.isCpp()), Keywords(Keywords), Scopes(Scopes) {
130 Contexts.push_back(Context(tok::unknown, 1,
false));
131 resetTokenMetadata();
135 ScopeType getScopeType(
const FormatToken &Token)
const {
136 switch (Token.getType()) {
137 case TT_FunctionLBrace:
138 case TT_LambdaLBrace:
141 case TT_StructLBrace:
150 if (!CurrentToken || !CurrentToken->Previous)
152 if (NonTemplateLess.count(CurrentToken->Previous) > 0)
155 const FormatToken &
Previous = *CurrentToken->Previous;
157 if (
Previous.Previous->Tok.isLiteral())
159 if (
Previous.Previous->is(tok::r_brace))
161 if (
Previous.Previous->is(tok::r_paren) && Contexts.size() > 1 &&
162 (!
Previous.Previous->MatchingParen ||
163 Previous.Previous->MatchingParen->isNot(
164 TT_OverloadedOperatorLParen))) {
167 if (
Previous.Previous->is(tok::kw_operator) &&
168 CurrentToken->is(tok::l_paren)) {
173 FormatToken *Left = CurrentToken->Previous;
174 Left->ParentBracket = Contexts.back().ContextKind;
175 ScopedContextCreator ContextCreator(*
this, tok::less, 12);
179 bool InExprContext = Contexts.back().IsExpression;
181 Contexts.back().IsExpression =
false;
184 if (Left->Previous && Left->Previous->isNot(tok::kw_template))
185 Contexts.back().ContextType = Context::TemplateArgument;
188 CurrentToken->is(tok::question)) {
192 while (CurrentToken) {
193 if (CurrentToken->is(tok::greater)) {
200 if (CurrentToken->Next && CurrentToken->Next->is(tok::greater) &&
201 Left->ParentBracket != tok::less &&
202 CurrentToken->getStartOfNonWhitespace() ==
203 CurrentToken->Next->getStartOfNonWhitespace().getLocWithOffset(
207 Left->MatchingParen = CurrentToken;
208 CurrentToken->MatchingParen = Left;
216 Left->Previous->isOneOf(TT_SelectorName, TT_DictLiteral))) {
217 CurrentToken->setType(TT_DictLiteral);
219 CurrentToken->setType(TT_TemplateCloser);
220 CurrentToken->Tok.setLength(1);
222 if (CurrentToken->Next && CurrentToken->Next->Tok.isLiteral())
227 if (CurrentToken->is(tok::question) &&
232 if (CurrentToken->isOneOf(tok::r_paren, tok::r_square, tok::r_brace) ||
233 (CurrentToken->isOneOf(tok::colon, tok::question) && InExprContext &&
234 !Style.isCSharp() && !Style.isProto())) {
243 if (CurrentToken->Previous->isOneOf(tok::pipepipe, tok::ampamp) &&
244 CurrentToken->Previous->is(TT_BinaryOperator) &&
245 Contexts[Contexts.size() - 2].IsExpression &&
246 !Line.startsWith(tok::kw_template)) {
249 updateParameterCount(Left, CurrentToken);
251 if (FormatToken *
Previous = CurrentToken->getPreviousNonComment()) {
252 if (CurrentToken->is(tok::colon) ||
253 (CurrentToken->isOneOf(tok::l_brace, tok::less) &&
259 if (Style.isTableGen()) {
260 if (CurrentToken->isOneOf(tok::comma, tok::equal)) {
267 if (!parseTableGenValue())
277 bool parseUntouchableParens() {
278 while (CurrentToken) {
279 CurrentToken->Finalized =
true;
280 switch (CurrentToken->Tok.getKind()) {
283 if (!parseUntouchableParens())
298 bool parseParens(
bool LookForDecls =
false) {
301 assert(CurrentToken->Previous &&
"Unknown previous token");
302 FormatToken &OpeningParen = *CurrentToken->Previous;
303 assert(OpeningParen.is(tok::l_paren));
304 FormatToken *PrevNonComment = OpeningParen.getPreviousNonComment();
305 OpeningParen.ParentBracket = Contexts.back().ContextKind;
306 ScopedContextCreator ContextCreator(*
this, tok::l_paren, 1);
309 Contexts.back().ColonIsForRangeExpr =
310 Contexts.size() == 2 && Contexts[0].ColonIsForRangeExpr;
312 if (OpeningParen.Previous &&
313 OpeningParen.Previous->is(TT_UntouchableMacroFunc)) {
314 OpeningParen.Finalized =
true;
315 return parseUntouchableParens();
318 bool StartsObjCMethodExpr =
false;
319 if (!Style.isVerilog()) {
320 if (FormatToken *MaybeSel = OpeningParen.Previous) {
322 if (MaybeSel->isObjCAtKeyword(tok::objc_selector) &&
323 MaybeSel->Previous && MaybeSel->Previous->is(tok::at)) {
324 StartsObjCMethodExpr =
true;
329 if (OpeningParen.is(TT_OverloadedOperatorLParen)) {
331 FormatToken *Prev = &OpeningParen;
332 while (Prev->isNot(tok::kw_operator)) {
333 Prev = Prev->Previous;
334 assert(Prev &&
"Expect a kw_operator prior to the OperatorLParen!");
340 bool OperatorCalledAsMemberFunction =
341 Prev->Previous && Prev->Previous->isOneOf(tok::period, tok::arrow);
342 Contexts.back().IsExpression = OperatorCalledAsMemberFunction;
343 }
else if (OpeningParen.is(TT_VerilogInstancePortLParen)) {
344 Contexts.back().IsExpression =
true;
345 Contexts.back().ContextType = Context::VerilogInstancePortList;
346 }
else if (Style.isJavaScript() &&
347 (Line.startsWith(Keywords.kw_type, tok::identifier) ||
348 Line.startsWith(tok::kw_export, Keywords.kw_type,
352 Contexts.back().IsExpression =
false;
353 }
else if (OpeningParen.Previous &&
354 (OpeningParen.Previous->isOneOf(
355 tok::kw_static_assert, tok::kw_noexcept, tok::kw_explicit,
356 tok::kw_while, tok::l_paren, tok::comma,
357 TT_BinaryOperator) ||
358 OpeningParen.Previous->isIf())) {
360 Contexts.back().IsExpression =
true;
361 }
else if (Style.isJavaScript() && OpeningParen.Previous &&
362 (OpeningParen.Previous->is(Keywords.kw_function) ||
363 (OpeningParen.Previous->endsSequence(tok::identifier,
364 Keywords.kw_function)))) {
366 Contexts.back().IsExpression =
false;
367 }
else if (Style.isJavaScript() && OpeningParen.Previous &&
368 OpeningParen.Previous->is(TT_JsTypeColon)) {
370 Contexts.back().IsExpression =
false;
371 }
else if (isLambdaParameterList(&OpeningParen)) {
373 Contexts.back().IsExpression =
false;
374 }
else if (OpeningParen.is(TT_RequiresExpressionLParen)) {
375 Contexts.back().IsExpression =
false;
376 }
else if (OpeningParen.Previous &&
377 OpeningParen.Previous->is(tok::kw__Generic)) {
378 Contexts.back().ContextType = Context::C11GenericSelection;
379 Contexts.back().IsExpression =
true;
380 }
else if (Line.InPPDirective &&
381 (!OpeningParen.Previous ||
382 OpeningParen.Previous->isNot(tok::identifier))) {
383 Contexts.back().IsExpression =
true;
384 }
else if (Contexts[Contexts.size() - 2].CaretFound) {
386 Contexts.back().IsExpression =
false;
387 }
else if (OpeningParen.Previous &&
388 OpeningParen.Previous->is(TT_ForEachMacro)) {
390 Contexts.back().ContextType = Context::ForEachMacro;
391 Contexts.back().IsExpression =
false;
392 }
else if (OpeningParen.Previous && OpeningParen.Previous->MatchingParen &&
393 OpeningParen.Previous->MatchingParen->isOneOf(
394 TT_ObjCBlockLParen, TT_FunctionTypeLParen)) {
395 Contexts.back().IsExpression =
false;
396 }
else if (!Line.MustBeDeclaration && !Line.InPPDirective) {
398 OpeningParen.Previous &&
399 OpeningParen.Previous->isOneOf(tok::kw_for, tok::kw_catch);
400 Contexts.back().IsExpression = !IsForOrCatch;
403 if (Style.isTableGen()) {
404 if (FormatToken *Prev = OpeningParen.Previous) {
405 if (Prev->is(TT_TableGenCondOperator)) {
406 Contexts.back().IsTableGenCondOpe =
true;
407 Contexts.back().IsExpression =
true;
408 }
else if (Contexts.size() > 1 &&
409 Contexts[Contexts.size() - 2].IsTableGenBangOpe) {
414 Contexts.back().IsTableGenBangOpe =
true;
415 Contexts.back().IsExpression =
true;
418 if (!parseTableGenDAGArg())
420 return parseTableGenDAGArgAndList(&OpeningParen);
427 if (PrevNonComment && OpeningParen.is(TT_Unknown)) {
428 if (PrevNonComment->isAttribute()) {
429 OpeningParen.setType(TT_AttributeLParen);
430 }
else if (PrevNonComment->isOneOf(TT_TypenameMacro, tok::kw_decltype,
433#include
"clang/Basic/TransformTypeTraits.def"
435 OpeningParen.setType(TT_TypeDeclarationParen);
437 if (PrevNonComment->isOneOf(tok::kw_decltype, tok::kw_typeof))
438 Contexts.back().IsExpression =
true;
442 if (StartsObjCMethodExpr) {
443 Contexts.back().ColonIsObjCMethodExpr =
true;
444 OpeningParen.setType(TT_ObjCMethodExpr);
455 bool MightBeFunctionType = !Contexts[Contexts.size() - 2].IsExpression;
456 bool ProbablyFunctionType =
457 CurrentToken->isPointerOrReference() || CurrentToken->is(tok::caret);
458 bool HasMultipleLines =
false;
459 bool HasMultipleParametersOnALine =
false;
460 bool MightBeObjCForRangeLoop =
461 OpeningParen.Previous && OpeningParen.Previous->is(tok::kw_for);
462 FormatToken *PossibleObjCForInToken =
nullptr;
463 while (CurrentToken) {
468 if (LookForDecls && CurrentToken->Next) {
469 FormatToken *Prev = CurrentToken->getPreviousNonComment();
471 FormatToken *PrevPrev = Prev->getPreviousNonComment();
472 FormatToken *Next = CurrentToken->Next;
473 if (PrevPrev && PrevPrev->is(tok::identifier) &&
474 PrevPrev->isNot(TT_TypeName) && Prev->isPointerOrReference() &&
475 CurrentToken->is(tok::identifier) && Next->isNot(tok::equal)) {
476 Prev->setType(TT_BinaryOperator);
477 LookForDecls =
false;
482 if (CurrentToken->Previous->is(TT_PointerOrReference) &&
483 CurrentToken->Previous->Previous->isOneOf(tok::l_paren,
485 ProbablyFunctionType =
true;
487 if (CurrentToken->is(tok::comma))
488 MightBeFunctionType =
false;
489 if (CurrentToken->Previous->is(TT_BinaryOperator))
490 Contexts.back().IsExpression =
true;
491 if (CurrentToken->is(tok::r_paren)) {
492 if (OpeningParen.isNot(TT_CppCastLParen) && MightBeFunctionType &&
493 ProbablyFunctionType && CurrentToken->Next &&
494 (CurrentToken->Next->is(tok::l_paren) ||
495 (CurrentToken->Next->is(tok::l_square) &&
496 Line.MustBeDeclaration))) {
497 OpeningParen.setType(OpeningParen.Next->is(tok::caret)
499 : TT_FunctionTypeLParen);
501 OpeningParen.MatchingParen = CurrentToken;
502 CurrentToken->MatchingParen = &OpeningParen;
504 if (CurrentToken->Next && CurrentToken->Next->is(tok::l_brace) &&
505 OpeningParen.Previous && OpeningParen.Previous->is(tok::l_paren)) {
509 for (FormatToken *Tok = &OpeningParen; Tok != CurrentToken;
511 if (Tok->is(TT_BinaryOperator) && Tok->isPointerOrReference())
512 Tok->setType(TT_PointerOrReference);
516 if (StartsObjCMethodExpr) {
517 CurrentToken->setType(TT_ObjCMethodExpr);
518 if (Contexts.back().FirstObjCSelectorName) {
519 Contexts.back().FirstObjCSelectorName->LongestObjCSelectorName =
520 Contexts.back().LongestObjCSelectorName;
524 if (OpeningParen.is(TT_AttributeLParen))
525 CurrentToken->setType(TT_AttributeRParen);
526 if (OpeningParen.is(TT_TypeDeclarationParen))
527 CurrentToken->setType(TT_TypeDeclarationParen);
528 if (OpeningParen.Previous &&
529 OpeningParen.Previous->is(TT_JavaAnnotation)) {
530 CurrentToken->setType(TT_JavaAnnotation);
532 if (OpeningParen.Previous &&
533 OpeningParen.Previous->is(TT_LeadingJavaAnnotation)) {
534 CurrentToken->setType(TT_LeadingJavaAnnotation);
536 if (OpeningParen.Previous &&
537 OpeningParen.Previous->is(TT_AttributeSquare)) {
538 CurrentToken->setType(TT_AttributeSquare);
541 if (!HasMultipleLines)
543 else if (HasMultipleParametersOnALine)
551 if (CurrentToken->isOneOf(tok::r_square, tok::r_brace))
554 if (CurrentToken->is(tok::l_brace) && OpeningParen.is(TT_ObjCBlockLParen))
555 OpeningParen.setType(TT_Unknown);
556 if (CurrentToken->is(tok::comma) && CurrentToken->Next &&
557 !CurrentToken->Next->HasUnescapedNewline &&
558 !CurrentToken->Next->isTrailingComment()) {
559 HasMultipleParametersOnALine =
true;
561 bool ProbablyFunctionTypeLParen =
562 (CurrentToken->is(tok::l_paren) && CurrentToken->Next &&
563 CurrentToken->Next->isOneOf(tok::star, tok::amp, tok::caret));
564 if ((CurrentToken->Previous->isOneOf(tok::kw_const, tok::kw_auto) ||
565 CurrentToken->Previous->isTypeName(IsCpp)) &&
566 !(CurrentToken->is(tok::l_brace) ||
567 (CurrentToken->is(tok::l_paren) && !ProbablyFunctionTypeLParen))) {
568 Contexts.back().IsExpression =
false;
570 if (CurrentToken->isOneOf(tok::semi, tok::colon)) {
571 MightBeObjCForRangeLoop =
false;
572 if (PossibleObjCForInToken) {
573 PossibleObjCForInToken->setType(TT_Unknown);
574 PossibleObjCForInToken =
nullptr;
577 if (MightBeObjCForRangeLoop && CurrentToken->is(Keywords.kw_in)) {
578 PossibleObjCForInToken = CurrentToken;
579 PossibleObjCForInToken->setType(TT_ObjCForIn);
583 if (CurrentToken->is(tok::comma))
584 Contexts.back().CanBeExpression =
true;
586 if (Style.isTableGen()) {
587 if (CurrentToken->is(tok::comma)) {
588 if (Contexts.back().IsTableGenCondOpe)
589 CurrentToken->setType(TT_TableGenCondOperatorComma);
591 }
else if (CurrentToken->is(tok::colon)) {
592 if (Contexts.back().IsTableGenCondOpe)
593 CurrentToken->setType(TT_TableGenCondOperatorColon);
597 if (!parseTableGenValue())
602 FormatToken *Tok = CurrentToken;
605 updateParameterCount(&OpeningParen, Tok);
606 if (CurrentToken && CurrentToken->HasUnescapedNewline)
607 HasMultipleLines =
true;
612 bool isCSharpAttributeSpecifier(
const FormatToken &Tok) {
613 if (!Style.isCSharp())
617 if (Tok.Previous && Tok.Previous->is(tok::identifier))
621 if (Tok.Previous && Tok.Previous->is(tok::r_square)) {
622 auto *MatchingParen = Tok.Previous->MatchingParen;
623 if (!MatchingParen || MatchingParen->is(TT_ArraySubscriptLSquare))
627 const FormatToken *AttrTok = Tok.Next;
632 if (AttrTok->is(tok::r_square))
636 while (AttrTok && AttrTok->isNot(tok::r_square))
637 AttrTok = AttrTok->Next;
643 AttrTok = AttrTok->Next;
648 if (AttrTok->isOneOf(tok::kw_public, tok::kw_private, tok::kw_protected,
649 tok::comment, tok::kw_class, tok::kw_static,
650 tok::l_square, Keywords.kw_internal)) {
656 AttrTok->Next->startsSequence(tok::identifier, tok::l_paren)) {
671 FormatToken *Left = CurrentToken->Previous;
672 Left->ParentBracket = Contexts.back().ContextKind;
673 FormatToken *
Parent = Left->getPreviousNonComment();
678 bool CppArrayTemplates =
680 (Contexts.back().CanBeExpression || Contexts.back().IsExpression ||
681 Contexts.back().ContextType == Context::TemplateArgument);
683 const bool IsInnerSquare = Contexts.back().InCpp11AttributeSpecifier;
684 const bool IsCpp11AttributeSpecifier =
685 isCppAttribute(IsCpp, *Left) || IsInnerSquare;
688 bool IsCSharpAttributeSpecifier =
689 isCSharpAttributeSpecifier(*Left) ||
690 Contexts.back().InCSharpAttributeSpecifier;
692 bool InsideInlineASM = Line.startsWith(tok::kw_asm);
693 bool IsCppStructuredBinding = Left->isCppStructuredBinding(IsCpp);
694 bool StartsObjCMethodExpr =
695 !IsCppStructuredBinding && !InsideInlineASM && !CppArrayTemplates &&
696 IsCpp && !IsCpp11AttributeSpecifier && !IsCSharpAttributeSpecifier &&
697 Contexts.back().CanBeExpression && Left->isNot(TT_LambdaLSquare) &&
698 !CurrentToken->isOneOf(tok::l_brace, tok::r_square) &&
700 Parent->isOneOf(tok::colon, tok::l_square, tok::l_paren,
701 tok::kw_return, tok::kw_throw) ||
702 Parent->isUnaryOperator() ||
704 Parent->isOneOf(TT_ObjCForIn, TT_CastRParen) ||
707 bool ColonFound =
false;
709 unsigned BindingIncrease = 1;
710 if (IsCppStructuredBinding) {
711 Left->setType(TT_StructuredBindingLSquare);
712 }
else if (Left->is(TT_Unknown)) {
713 if (StartsObjCMethodExpr) {
714 Left->setType(TT_ObjCMethodExpr);
715 }
else if (InsideInlineASM) {
716 Left->setType(TT_InlineASMSymbolicNameLSquare);
717 }
else if (IsCpp11AttributeSpecifier) {
718 Left->setType(TT_AttributeSquare);
719 if (!IsInnerSquare && Left->Previous)
720 Left->Previous->EndsCppAttributeGroup =
false;
721 }
else if (Style.isJavaScript() &&
Parent &&
722 Contexts.back().ContextKind == tok::l_brace &&
723 Parent->isOneOf(tok::l_brace, tok::comma)) {
724 Left->setType(TT_JsComputedPropertyName);
725 }
else if (IsCpp && Contexts.back().ContextKind == tok::l_brace &&
727 Left->setType(TT_DesignatedInitializerLSquare);
728 }
else if (IsCSharpAttributeSpecifier) {
729 Left->setType(TT_AttributeSquare);
730 }
else if (CurrentToken->is(tok::r_square) &&
Parent &&
731 Parent->is(TT_TemplateCloser)) {
732 Left->setType(TT_ArraySubscriptLSquare);
733 }
else if (Style.isProto()) {
760 Left->setType(TT_ArrayInitializerLSquare);
761 if (!Left->endsSequence(tok::l_square, tok::numeric_constant,
763 !Left->endsSequence(tok::l_square, tok::numeric_constant,
765 !Left->endsSequence(tok::l_square, tok::colon, TT_SelectorName)) {
766 Left->setType(TT_ProtoExtensionLSquare);
767 BindingIncrease = 10;
769 }
else if (!CppArrayTemplates &&
Parent &&
770 Parent->isOneOf(TT_BinaryOperator, TT_TemplateCloser, tok::at,
771 tok::comma, tok::l_paren, tok::l_square,
772 tok::question, tok::colon, tok::kw_return,
775 Left->setType(TT_ArrayInitializerLSquare);
777 BindingIncrease = 10;
778 Left->setType(TT_ArraySubscriptLSquare);
782 ScopedContextCreator ContextCreator(*
this, tok::l_square, BindingIncrease);
783 Contexts.back().IsExpression =
true;
784 if (Style.isJavaScript() &&
Parent &&
Parent->is(TT_JsTypeColon))
785 Contexts.back().IsExpression =
false;
787 Contexts.back().ColonIsObjCMethodExpr = StartsObjCMethodExpr;
788 Contexts.back().InCpp11AttributeSpecifier = IsCpp11AttributeSpecifier;
789 Contexts.back().InCSharpAttributeSpecifier = IsCSharpAttributeSpecifier;
791 while (CurrentToken) {
792 if (CurrentToken->is(tok::r_square)) {
793 if (IsCpp11AttributeSpecifier) {
794 CurrentToken->setType(TT_AttributeSquare);
796 CurrentToken->EndsCppAttributeGroup =
true;
798 if (IsCSharpAttributeSpecifier) {
799 CurrentToken->setType(TT_AttributeSquare);
800 }
else if (((CurrentToken->Next &&
801 CurrentToken->Next->is(tok::l_paren)) ||
802 (CurrentToken->Previous &&
803 CurrentToken->Previous->Previous == Left)) &&
804 Left->is(TT_ObjCMethodExpr)) {
809 StartsObjCMethodExpr =
false;
810 Left->setType(TT_Unknown);
812 if (StartsObjCMethodExpr && CurrentToken->Previous != Left) {
813 CurrentToken->setType(TT_ObjCMethodExpr);
816 if (!ColonFound && CurrentToken->Previous &&
817 CurrentToken->Previous->is(TT_Unknown) &&
818 canBeObjCSelectorComponent(*CurrentToken->Previous)) {
819 CurrentToken->Previous->setType(TT_SelectorName);
825 Parent->overwriteFixedType(TT_BinaryOperator);
828 if (CurrentToken->is(TT_ObjCMethodExpr) && CurrentToken->Next &&
829 CurrentToken->Next->is(TT_TrailingReturnArrow)) {
830 CurrentToken->Next->overwriteFixedType(TT_Unknown);
832 Left->MatchingParen = CurrentToken;
833 CurrentToken->MatchingParen = Left;
838 if (!Contexts.back().FirstObjCSelectorName) {
839 FormatToken *
Previous = CurrentToken->getPreviousNonComment();
841 Previous->ObjCSelectorNameParts = 1;
842 Contexts.back().FirstObjCSelectorName =
Previous;
845 Left->ParameterCount =
846 Contexts.back().FirstObjCSelectorName->ObjCSelectorNameParts;
848 if (Contexts.back().FirstObjCSelectorName) {
849 Contexts.back().FirstObjCSelectorName->LongestObjCSelectorName =
850 Contexts.back().LongestObjCSelectorName;
851 if (Left->BlockParameterCount > 1)
852 Contexts.back().FirstObjCSelectorName->LongestObjCSelectorName = 0;
854 if (Style.isTableGen() && Left->is(TT_TableGenListOpener))
855 CurrentToken->setType(TT_TableGenListCloser);
859 if (CurrentToken->isOneOf(tok::r_paren, tok::r_brace))
861 if (CurrentToken->is(tok::colon)) {
862 if (IsCpp11AttributeSpecifier &&
863 CurrentToken->endsSequence(tok::colon, tok::identifier,
867 CurrentToken->setType(TT_AttributeColon);
868 }
else if (!Style.isVerilog() && !Line.InPragmaDirective &&
869 Left->isOneOf(TT_ArraySubscriptLSquare,
870 TT_DesignatedInitializerLSquare)) {
871 Left->setType(TT_ObjCMethodExpr);
872 StartsObjCMethodExpr =
true;
873 Contexts.back().ColonIsObjCMethodExpr =
true;
876 Parent->setType(TT_CastRParen);
881 if (CurrentToken->is(tok::comma) && Left->is(TT_ObjCMethodExpr) &&
883 Left->setType(TT_ArrayInitializerLSquare);
885 FormatToken *Tok = CurrentToken;
886 if (Style.isTableGen()) {
887 if (CurrentToken->isOneOf(tok::comma, tok::minus, tok::ellipsis)) {
893 if (!parseTableGenValue())
896 updateParameterCount(Left, Tok);
901 updateParameterCount(Left, Tok);
906 void skipToNextNonComment() {
908 while (CurrentToken && CurrentToken->is(tok::comment))
917 bool parseTableGenValue(
bool ParseNameMode =
false) {
920 while (CurrentToken->is(tok::comment))
922 if (!parseTableGenSimpleValue())
927 if (CurrentToken->is(tok::hash)) {
928 if (CurrentToken->Next &&
929 CurrentToken->Next->isOneOf(tok::colon, tok::semi, tok::l_brace)) {
932 CurrentToken->setType(TT_TableGenTrailingPasteOperator);
936 FormatToken *HashTok = CurrentToken;
937 skipToNextNonComment();
938 HashTok->setType(TT_Unknown);
939 if (!parseTableGenValue(ParseNameMode))
944 if (ParseNameMode && CurrentToken->is(tok::l_brace))
947 if (CurrentToken->isOneOf(tok::l_brace, tok::l_square, tok::period)) {
948 CurrentToken->setType(TT_TableGenValueSuffix);
949 FormatToken *Suffix = CurrentToken;
950 skipToNextNonComment();
951 if (Suffix->is(tok::l_square))
952 return parseSquare();
953 if (Suffix->is(tok::l_brace)) {
954 Scopes.push_back(getScopeType(*Suffix));
964 bool tryToParseTableGenTokVar() {
967 if (CurrentToken->is(tok::identifier) &&
968 CurrentToken->TokenText.front() ==
'$') {
969 skipToNextNonComment();
977 bool parseTableGenDAGArg(
bool AlignColon =
false) {
978 if (tryToParseTableGenTokVar())
980 if (parseTableGenValue()) {
981 if (CurrentToken && CurrentToken->is(tok::colon)) {
983 CurrentToken->setType(TT_TableGenDAGArgListColonToAlign);
985 CurrentToken->setType(TT_TableGenDAGArgListColon);
986 skipToNextNonComment();
987 return tryToParseTableGenTokVar();
997 bool isTableGenDAGArgBreakingOperator(
const FormatToken &Tok) {
998 auto &Opes = Style.TableGenBreakingDAGArgOperators;
1003 if (Tok.isNot(tok::identifier) ||
1004 Tok.isOneOf(TT_TableGenBangOperator, TT_TableGenCondOperator)) {
1008 if (!Tok.Next || Tok.Next->is(tok::colon))
1010 return std::find(Opes.begin(), Opes.end(), Tok.TokenText.str()) !=
1016 bool parseTableGenDAGArgAndList(FormatToken *Opener) {
1017 FormatToken *FirstTok = CurrentToken;
1018 if (!parseTableGenDAGArg())
1020 bool BreakInside =
false;
1024 if (isTableGenDAGArgBreakingOperator(*FirstTok)) {
1027 Opener->setType(TT_TableGenDAGArgOpenerToBreak);
1028 if (FirstTok->isOneOf(TT_TableGenBangOperator,
1029 TT_TableGenCondOperator)) {
1032 CurrentToken->Previous->setType(TT_TableGenDAGArgOperatorToBreak);
1033 }
else if (FirstTok->is(tok::identifier)) {
1035 FirstTok->setType(TT_TableGenDAGArgOperatorToBreak);
1037 FirstTok->setType(TT_TableGenDAGArgOperatorID);
1042 bool FirstDAGArgListElm =
true;
1043 while (CurrentToken) {
1044 if (!FirstDAGArgListElm && CurrentToken->is(tok::comma)) {
1045 CurrentToken->setType(BreakInside ? TT_TableGenDAGArgListCommaToBreak
1046 : TT_TableGenDAGArgListComma);
1047 skipToNextNonComment();
1049 if (CurrentToken && CurrentToken->is(tok::r_paren)) {
1050 CurrentToken->setType(TT_TableGenDAGArgCloser);
1051 Opener->MatchingParen = CurrentToken;
1052 CurrentToken->MatchingParen = Opener;
1053 skipToNextNonComment();
1056 if (!parseTableGenDAGArg(
1058 Style.AlignConsecutiveTableGenBreakingDAGArgColons.Enabled)) {
1061 FirstDAGArgListElm =
false;
1066 bool parseTableGenSimpleValue() {
1067 assert(Style.isTableGen());
1070 FormatToken *Tok = CurrentToken;
1071 skipToNextNonComment();
1073 if (Tok->isOneOf(tok::numeric_constant, tok::string_literal,
1074 TT_TableGenMultiLineString, tok::kw_true, tok::kw_false,
1075 tok::question, tok::kw_int)) {
1079 if (Tok->is(tok::l_brace)) {
1080 Scopes.push_back(getScopeType(*Tok));
1081 return parseBrace();
1084 if (Tok->is(tok::l_square)) {
1085 Tok->setType(TT_TableGenListOpener);
1088 if (Tok->is(tok::less)) {
1089 CurrentToken->setType(TT_TemplateOpener);
1090 return parseAngle();
1096 if (Tok->is(tok::l_paren)) {
1097 Tok->setType(TT_TableGenDAGArgOpener);
1098 return parseTableGenDAGArgAndList(Tok);
1101 if (Tok->is(TT_TableGenBangOperator)) {
1102 if (CurrentToken && CurrentToken->is(tok::less)) {
1103 CurrentToken->setType(TT_TemplateOpener);
1104 skipToNextNonComment();
1108 if (!CurrentToken || CurrentToken->isNot(tok::l_paren))
1110 skipToNextNonComment();
1112 Contexts.back().IsTableGenBangOpe =
true;
1113 bool Result = parseParens();
1114 Contexts.back().IsTableGenBangOpe =
false;
1118 if (Tok->is(TT_TableGenCondOperator)) {
1120 skipToNextNonComment();
1121 if (!Tok || Tok->isNot(tok::l_paren))
1123 bool Result = parseParens();
1129 if (Tok->is(tok::identifier)) {
1131 if (CurrentToken && CurrentToken->is(tok::less)) {
1132 CurrentToken->setType(TT_TemplateOpener);
1133 skipToNextNonComment();
1134 return parseAngle();
1142 bool couldBeInStructArrayInitializer()
const {
1143 if (Contexts.size() < 2)
1147 const auto End = std::next(Contexts.rbegin(), 2);
1148 auto Last = Contexts.rbegin();
1151 if (
Last->ContextKind == tok::l_brace)
1153 return Depth == 2 &&
Last->ContextKind != tok::l_brace;
1160 assert(CurrentToken->Previous);
1161 FormatToken &OpeningBrace = *CurrentToken->Previous;
1162 assert(OpeningBrace.is(tok::l_brace));
1163 OpeningBrace.ParentBracket = Contexts.back().ContextKind;
1165 if (Contexts.back().CaretFound)
1166 OpeningBrace.overwriteFixedType(TT_ObjCBlockLBrace);
1167 Contexts.back().CaretFound =
false;
1169 ScopedContextCreator ContextCreator(*
this, tok::l_brace, 1);
1170 Contexts.back().ColonIsDictLiteral =
true;
1172 Contexts.back().IsExpression =
true;
1173 if (Style.isJavaScript() && OpeningBrace.Previous &&
1174 OpeningBrace.Previous->is(TT_JsTypeColon)) {
1175 Contexts.back().IsExpression =
false;
1177 if (Style.isVerilog() &&
1178 (!OpeningBrace.getPreviousNonComment() ||
1179 OpeningBrace.getPreviousNonComment()->isNot(Keywords.kw_apostrophe))) {
1180 Contexts.back().VerilogMayBeConcatenation =
true;
1182 if (Style.isTableGen())
1183 Contexts.back().ColonIsDictLiteral =
false;
1185 unsigned CommaCount = 0;
1186 while (CurrentToken) {
1187 if (CurrentToken->is(tok::r_brace)) {
1188 assert(!Scopes.empty());
1189 assert(Scopes.back() == getScopeType(OpeningBrace));
1191 assert(OpeningBrace.Optional == CurrentToken->Optional);
1192 OpeningBrace.MatchingParen = CurrentToken;
1193 CurrentToken->MatchingParen = &OpeningBrace;
1195 if (OpeningBrace.ParentBracket == tok::l_brace &&
1196 couldBeInStructArrayInitializer() && CommaCount > 0) {
1197 Contexts.back().ContextType = Context::StructArrayInitializer;
1203 if (CurrentToken->isOneOf(tok::r_paren, tok::r_square))
1205 updateParameterCount(&OpeningBrace, CurrentToken);
1206 if (CurrentToken->isOneOf(tok::colon, tok::l_brace, tok::less)) {
1207 FormatToken *
Previous = CurrentToken->getPreviousNonComment();
1208 if (
Previous->is(TT_JsTypeOptionalQuestion))
1210 if ((CurrentToken->is(tok::colon) && !Style.isTableGen() &&
1211 (!Contexts.back().ColonIsDictLiteral || !IsCpp)) ||
1213 OpeningBrace.setType(TT_DictLiteral);
1214 if (
Previous->Tok.getIdentifierInfo() ||
1215 Previous->is(tok::string_literal)) {
1216 Previous->setType(TT_SelectorName);
1219 if (CurrentToken->is(tok::colon) && OpeningBrace.is(TT_Unknown) &&
1220 !Style.isTableGen()) {
1221 OpeningBrace.setType(TT_DictLiteral);
1222 }
else if (Style.isJavaScript()) {
1223 OpeningBrace.overwriteFixedType(TT_DictLiteral);
1226 if (CurrentToken->is(tok::comma)) {
1227 if (Style.isJavaScript())
1228 OpeningBrace.overwriteFixedType(TT_DictLiteral);
1231 if (!consumeToken())
1237 void updateParameterCount(FormatToken *Left, FormatToken *Current) {
1241 if (Current->is(tok::l_brace) && Current->is(
BK_Block))
1242 ++Left->BlockParameterCount;
1243 if (Current->is(tok::comma)) {
1244 ++Left->ParameterCount;
1246 Left->Role.reset(
new CommaSeparatedList(Style));
1247 Left->Role->CommaFound(Current);
1248 }
else if (Left->ParameterCount == 0 && Current->isNot(tok::comment)) {
1249 Left->ParameterCount = 1;
1253 bool parseConditional() {
1254 while (CurrentToken) {
1255 if (CurrentToken->is(tok::colon)) {
1256 CurrentToken->setType(TT_ConditionalExpr);
1260 if (!consumeToken())
1266 bool parseTemplateDeclaration() {
1267 if (CurrentToken && CurrentToken->is(tok::less)) {
1268 CurrentToken->setType(TT_TemplateOpener);
1273 CurrentToken->Previous->ClosesTemplateDeclaration =
true;
1279 bool consumeToken() {
1281 const auto *Prev = CurrentToken->getPreviousNonComment();
1282 if (Prev && Prev->is(tok::r_square) && Prev->is(TT_AttributeSquare) &&
1283 CurrentToken->isOneOf(tok::kw_if, tok::kw_switch, tok::kw_case,
1284 tok::kw_default, tok::kw_for, tok::kw_while) &&
1286 CurrentToken->MustBreakBefore =
true;
1289 FormatToken *Tok = CurrentToken;
1293 if (Tok->is(TT_VerilogTableItem))
1296 if (Tok->is(TT_TableGenMultiLineString))
1298 switch (Tok->Tok.getKind()) {
1301 if (!Tok->Previous && Line.MustBeDeclaration)
1302 Tok->setType(TT_ObjCMethodSpecifier);
1309 if (Tok->isTypeFinalized())
1312 if (Style.isJavaScript()) {
1313 if (Contexts.back().ColonIsForRangeExpr ||
1314 (Contexts.size() == 1 &&
1315 !Line.First->isOneOf(tok::kw_enum, tok::kw_case)) ||
1316 Contexts.back().ContextKind == tok::l_paren ||
1317 Contexts.back().ContextKind == tok::l_square ||
1318 (!Contexts.back().IsExpression &&
1319 Contexts.back().ContextKind == tok::l_brace) ||
1320 (Contexts.size() == 1 &&
1321 Line.MustBeDeclaration)) {
1322 Contexts.back().IsExpression =
false;
1323 Tok->setType(TT_JsTypeColon);
1326 }
else if (Style.isCSharp()) {
1327 if (Contexts.back().InCSharpAttributeSpecifier) {
1328 Tok->setType(TT_AttributeColon);
1331 if (Contexts.back().ContextKind == tok::l_paren) {
1332 Tok->setType(TT_CSharpNamedArgumentColon);
1335 }
else if (Style.isVerilog() && Tok->isNot(TT_BinaryOperator)) {
1338 if (Keywords.isVerilogEnd(*Tok->Previous) ||
1339 Keywords.isVerilogBegin(*Tok->Previous)) {
1340 Tok->setType(TT_VerilogBlockLabelColon);
1341 }
else if (Contexts.back().ContextKind == tok::l_square) {
1342 Tok->setType(TT_BitFieldColon);
1343 }
else if (Contexts.back().ColonIsDictLiteral) {
1344 Tok->setType(TT_DictLiteral);
1345 }
else if (Contexts.size() == 1) {
1349 Tok->setType(TT_CaseLabelColon);
1350 if (Line.Level > 1 || (!Line.InPPDirective && Line.Level > 0))
1355 if (Line.First->isOneOf(Keywords.kw_module, Keywords.kw_import) ||
1356 Line.First->startsSequence(tok::kw_export, Keywords.kw_module) ||
1357 Line.First->startsSequence(tok::kw_export, Keywords.kw_import)) {
1358 Tok->setType(TT_ModulePartitionColon);
1359 }
else if (Contexts.back().ColonIsDictLiteral || Style.isProto()) {
1360 Tok->setType(TT_DictLiteral);
1362 if (FormatToken *
Previous = Tok->getPreviousNonComment())
1363 Previous->setType(TT_SelectorName);
1365 }
else if (Contexts.back().ColonIsObjCMethodExpr ||
1366 Line.startsWith(TT_ObjCMethodSpecifier)) {
1367 Tok->setType(TT_ObjCMethodExpr);
1368 const FormatToken *BeforePrevious = Tok->Previous->Previous;
1371 bool UnknownIdentifierInMethodDeclaration =
1372 Line.startsWith(TT_ObjCMethodSpecifier) &&
1373 Tok->Previous->is(tok::identifier) && Tok->Previous->is(TT_Unknown);
1374 if (!BeforePrevious ||
1376 !(BeforePrevious->is(TT_CastRParen) ||
1377 (BeforePrevious->is(TT_ObjCMethodExpr) &&
1378 BeforePrevious->is(tok::colon))) ||
1379 BeforePrevious->is(tok::r_square) ||
1380 Contexts.back().LongestObjCSelectorName == 0 ||
1381 UnknownIdentifierInMethodDeclaration) {
1382 Tok->Previous->setType(TT_SelectorName);
1383 if (!Contexts.back().FirstObjCSelectorName) {
1384 Contexts.back().FirstObjCSelectorName = Tok->Previous;
1385 }
else if (Tok->Previous->ColumnWidth >
1386 Contexts.back().LongestObjCSelectorName) {
1387 Contexts.back().LongestObjCSelectorName =
1388 Tok->Previous->ColumnWidth;
1390 Tok->Previous->ParameterIndex =
1391 Contexts.back().FirstObjCSelectorName->ObjCSelectorNameParts;
1392 ++Contexts.back().FirstObjCSelectorName->ObjCSelectorNameParts;
1394 }
else if (Contexts.back().ColonIsForRangeExpr) {
1395 Tok->setType(TT_RangeBasedForLoopColon);
1396 }
else if (Contexts.back().ContextType == Context::C11GenericSelection) {
1397 Tok->setType(TT_GenericSelectionColon);
1398 }
else if (CurrentToken && CurrentToken->is(tok::numeric_constant)) {
1399 Tok->setType(TT_BitFieldColon);
1400 }
else if (Contexts.size() == 1 &&
1401 !Line.First->isOneOf(tok::kw_enum, tok::kw_case,
1403 FormatToken *Prev = Tok->getPreviousNonComment();
1406 if (Prev->isOneOf(tok::r_paren, tok::kw_noexcept) ||
1407 Prev->ClosesRequiresClause) {
1408 Tok->setType(TT_CtorInitializerColon);
1409 }
else if (Prev->is(tok::kw_try)) {
1411 FormatToken *PrevPrev = Prev->getPreviousNonComment();
1414 if (PrevPrev && PrevPrev->isOneOf(tok::r_paren, tok::kw_noexcept))
1415 Tok->setType(TT_CtorInitializerColon);
1417 Tok->setType(TT_InheritanceColon);
1419 }
else if (canBeObjCSelectorComponent(*Tok->Previous) && Tok->Next &&
1420 (Tok->Next->isOneOf(tok::r_paren, tok::comma) ||
1421 (canBeObjCSelectorComponent(*Tok->Next) && Tok->Next->Next &&
1422 Tok->Next->Next->is(tok::colon)))) {
1425 Tok->setType(TT_ObjCMethodExpr);
1426 }
else if (Contexts.back().ContextKind == tok::l_paren &&
1427 !Line.InPragmaDirective) {
1428 if (Style.isTableGen() && Contexts.back().IsTableGenDAGArg) {
1429 Tok->setType(TT_TableGenDAGArgListColon);
1432 Tok->setType(TT_InlineASMColon);
1439 if (Style.isJavaScript() && !Contexts.back().IsExpression)
1440 Tok->setType(TT_JsTypeOperator);
1443 if (Style.isTableGen()) {
1445 if (!parseTableGenValue())
1447 if (CurrentToken && CurrentToken->is(Keywords.kw_then))
1452 CurrentToken->isOneOf(tok::kw_constexpr, tok::identifier)) {
1457 if (CurrentToken && CurrentToken->is(tok::l_paren)) {
1459 if (!parseParens(
true))
1464 if (Style.isJavaScript()) {
1466 if ((Tok->Previous && Tok->Previous->is(tok::period)) ||
1467 (Tok->Next && Tok->Next->is(tok::colon))) {
1471 if (CurrentToken && CurrentToken->is(Keywords.kw_await))
1474 if (IsCpp && CurrentToken && CurrentToken->is(tok::kw_co_await))
1476 Contexts.back().ColonIsForRangeExpr =
true;
1477 if (!CurrentToken || CurrentToken->isNot(tok::l_paren))
1488 if (Tok->Previous && Tok->Previous->is(tok::r_paren) &&
1489 Tok->Previous->MatchingParen &&
1490 Tok->Previous->MatchingParen->is(TT_OverloadedOperatorLParen)) {
1491 Tok->Previous->setType(TT_OverloadedOperator);
1492 Tok->Previous->MatchingParen->setType(TT_OverloadedOperator);
1493 Tok->setType(TT_OverloadedOperatorLParen);
1496 if (Style.isVerilog()) {
1502 auto IsInstancePort = [&]() {
1503 const FormatToken *Prev = Tok->getPreviousNonComment();
1504 const FormatToken *PrevPrev;
1512 if (!Prev || !(PrevPrev = Prev->getPreviousNonComment()))
1515 if (Keywords.isVerilogIdentifier(*Prev) &&
1516 Keywords.isVerilogIdentifier(*PrevPrev)) {
1520 if (Prev->is(Keywords.kw_verilogHash) &&
1521 Keywords.isVerilogIdentifier(*PrevPrev)) {
1525 if (Keywords.isVerilogIdentifier(*Prev) && PrevPrev->is(tok::r_paren))
1528 if (Keywords.isVerilogIdentifier(*Prev) && PrevPrev->is(tok::comma)) {
1529 const FormatToken *PrevParen = PrevPrev->getPreviousNonComment();
1530 if (PrevParen->is(tok::r_paren) && PrevParen->MatchingParen &&
1531 PrevParen->MatchingParen->is(TT_VerilogInstancePortLParen)) {
1538 if (IsInstancePort())
1539 Tok->setFinalizedType(TT_VerilogInstancePortLParen);
1544 if (Line.MustBeDeclaration && Contexts.size() == 1 &&
1545 !Contexts.back().IsExpression && !Line.startsWith(TT_ObjCProperty) &&
1546 !Line.startsWith(tok::l_paren) &&
1547 !Tok->isOneOf(TT_TypeDeclarationParen, TT_RequiresExpressionLParen)) {
1548 if (
const auto *
Previous = Tok->Previous;
1551 !
Previous->isOneOf(TT_RequiresClause, TT_LeadingJavaAnnotation))) {
1552 Line.MightBeFunctionDecl =
true;
1553 Tok->MightBeFunctionDeclParen =
true;
1558 if (Style.isTableGen())
1559 Tok->setType(TT_TableGenListOpener);
1565 FormatToken *
Previous = Tok->getPreviousNonComment();
1567 Previous->setType(TT_SelectorName);
1569 Scopes.push_back(getScopeType(*Tok));
1575 Tok->setType(TT_TemplateOpener);
1583 Tok->Previous->isOneOf(TT_SelectorName, TT_DictLiteral))) {
1584 Tok->setType(TT_DictLiteral);
1585 FormatToken *
Previous = Tok->getPreviousNonComment();
1587 Previous->setType(TT_SelectorName);
1589 if (Style.isTableGen())
1590 Tok->setType(TT_TemplateOpener);
1592 Tok->setType(TT_BinaryOperator);
1593 NonTemplateLess.insert(Tok);
1603 if (!Scopes.empty())
1611 Tok->setType(TT_BinaryOperator);
1612 if (Tok->Previous && Tok->Previous->is(TT_TemplateCloser))
1613 Tok->SpacesRequiredBefore = 1;
1615 case tok::kw_operator:
1616 if (Style.isProto())
1618 while (CurrentToken &&
1619 !CurrentToken->isOneOf(tok::l_paren, tok::semi, tok::r_paren)) {
1620 if (CurrentToken->isOneOf(tok::star, tok::amp))
1621 CurrentToken->setType(TT_PointerOrReference);
1622 auto Next = CurrentToken->getNextNonComment();
1625 if (Next->is(tok::less))
1631 auto Previous = CurrentToken->getPreviousNonComment();
1633 if (CurrentToken->is(tok::comma) &&
Previous->isNot(tok::kw_operator))
1635 if (
Previous->isOneOf(TT_BinaryOperator, TT_UnaryOperator, tok::comma,
1636 tok::star, tok::arrow, tok::amp, tok::ampamp) ||
1638 Previous->TokenText.starts_with(
"\"\"")) {
1639 Previous->setType(TT_OverloadedOperator);
1640 if (CurrentToken->isOneOf(tok::less, tok::greater))
1644 if (CurrentToken && CurrentToken->is(tok::l_paren))
1645 CurrentToken->setType(TT_OverloadedOperatorLParen);
1646 if (CurrentToken && CurrentToken->Previous->is(TT_BinaryOperator))
1647 CurrentToken->Previous->setType(TT_OverloadedOperator);
1650 if (Style.isJavaScript() && Tok->Next &&
1651 Tok->Next->isOneOf(tok::semi, tok::comma, tok::colon, tok::r_paren,
1652 tok::r_brace, tok::r_square)) {
1657 Tok->setType(TT_JsTypeOptionalQuestion);
1662 if (Line.MustBeDeclaration && !Contexts.back().IsExpression &&
1663 Style.isJavaScript()) {
1666 if (Style.isCSharp()) {
1672 (Tok->Next->startsSequence(tok::question, tok::r_paren) ||
1673 Tok->Next->startsSequence(tok::question, tok::greater) ||
1674 Tok->Next->startsSequence(tok::question, tok::identifier,
1676 Tok->setType(TT_CSharpNullable);
1681 if (Tok->Next && Tok->Next->is(tok::identifier) && Tok->Next->Next &&
1682 Tok->Next->Next->is(tok::equal)) {
1683 Tok->setType(TT_CSharpNullable);
1692 if (!Contexts.back().IsExpression && Line.MustBeDeclaration &&
1694 !Tok->Next->isOneOf(tok::identifier, tok::string_literal) ||
1696 !Tok->Next->Next->isOneOf(tok::colon, tok::question))) {
1697 Tok->setType(TT_CSharpNullable);
1703 case tok::kw_template:
1704 parseTemplateDeclaration();
1707 switch (Contexts.back().ContextType) {
1708 case Context::CtorInitializer:
1709 Tok->setType(TT_CtorInitializerComma);
1711 case Context::InheritanceList:
1712 Tok->setType(TT_InheritanceComma);
1714 case Context::VerilogInstancePortList:
1715 Tok->setFinalizedType(TT_VerilogInstancePortComma);
1718 if (Style.isVerilog() && Contexts.size() == 1 &&
1719 Line.startsWith(Keywords.kw_assign)) {
1720 Tok->setFinalizedType(TT_VerilogAssignComma);
1721 }
else if (Contexts.back().FirstStartOfName &&
1722 (Contexts.size() == 1 || startsWithInitStatement(Line))) {
1723 Contexts.back().FirstStartOfName->PartOfMultiVariableDeclStmt =
true;
1724 Line.IsMultiVariableDeclStmt =
true;
1728 if (Contexts.back().ContextType == Context::ForEachMacro)
1729 Contexts.back().IsExpression =
true;
1731 case tok::kw_default:
1733 if (Style.isVerilog() && Keywords.isVerilogEndOfLabel(*Tok) &&
1734 (Line.Level > 1 || (!Line.InPPDirective && Line.Level > 0))) {
1738 case tok::identifier:
1739 if (Tok->isOneOf(Keywords.kw___has_include,
1740 Keywords.kw___has_include_next)) {
1743 if (Style.isCSharp() && Tok->is(Keywords.kw_where) && Tok->Next &&
1744 Tok->Next->isNot(tok::l_paren)) {
1745 Tok->setType(TT_CSharpGenericTypeConstraint);
1746 parseCSharpGenericTypeConstraint();
1747 if (!Tok->getPreviousNonComment())
1748 Line.IsContinuation =
true;
1750 if (Style.isTableGen()) {
1751 if (Tok->is(Keywords.kw_assert)) {
1752 if (!parseTableGenValue())
1754 }
else if (Tok->isOneOf(Keywords.kw_def, Keywords.kw_defm) &&
1756 !Tok->Next->isOneOf(tok::colon, tok::l_brace))) {
1758 if (!parseTableGenValue(
true))
1764 if (Tok->Previous && Tok->Previous->is(tok::kw_noexcept))
1765 Tok->setType(TT_TrailingReturnArrow);
1769 if (Style.isTableGen() && !parseTableGenValue())
1778 void parseCSharpGenericTypeConstraint() {
1779 int OpenAngleBracketsCount = 0;
1780 while (CurrentToken) {
1781 if (CurrentToken->is(tok::less)) {
1783 CurrentToken->setType(TT_TemplateOpener);
1784 ++OpenAngleBracketsCount;
1786 }
else if (CurrentToken->is(tok::greater)) {
1787 CurrentToken->setType(TT_TemplateCloser);
1788 --OpenAngleBracketsCount;
1790 }
else if (CurrentToken->is(tok::comma) && OpenAngleBracketsCount == 0) {
1793 CurrentToken->setType(TT_CSharpGenericTypeConstraintComma);
1795 }
else if (CurrentToken->is(Keywords.kw_where)) {
1796 CurrentToken->setType(TT_CSharpGenericTypeConstraint);
1798 }
else if (CurrentToken->is(tok::colon)) {
1799 CurrentToken->setType(TT_CSharpGenericTypeConstraintColon);
1807 void parseIncludeDirective() {
1808 if (CurrentToken && CurrentToken->is(tok::less)) {
1810 while (CurrentToken) {
1813 if (CurrentToken->isNot(tok::comment) &&
1814 !CurrentToken->TokenText.starts_with(
"//")) {
1815 CurrentToken->setType(TT_ImplicitStringLiteral);
1822 void parseWarningOrError() {
1827 while (CurrentToken) {
1828 CurrentToken->setType(TT_ImplicitStringLiteral);
1833 void parsePragma() {
1836 CurrentToken->isOneOf(Keywords.kw_mark, Keywords.kw_option,
1837 Keywords.kw_region)) {
1838 bool IsMarkOrRegion =
1839 CurrentToken->isOneOf(Keywords.kw_mark, Keywords.kw_region);
1842 while (CurrentToken) {
1843 if (IsMarkOrRegion || CurrentToken->Previous->is(TT_BinaryOperator))
1844 CurrentToken->setType(TT_ImplicitStringLiteral);
1850 void parseHasInclude() {
1851 if (!CurrentToken || CurrentToken->isNot(tok::l_paren))
1854 parseIncludeDirective();
1858 LineType parsePreprocessorDirective() {
1859 bool IsFirstToken = CurrentToken->IsFirst;
1865 if (Style.isJavaScript() && IsFirstToken) {
1869 while (CurrentToken) {
1871 CurrentToken->setType(TT_ImplicitStringLiteral);
1877 if (CurrentToken->is(tok::numeric_constant)) {
1878 CurrentToken->SpacesRequiredBefore = 1;
1883 if (!CurrentToken->Tok.getIdentifierInfo())
1887 if (Style.isVerilog() && !Keywords.isVerilogPPDirective(*CurrentToken))
1889 switch (CurrentToken->Tok.getIdentifierInfo()->getPPKeywordID()) {
1890 case tok::pp_include:
1891 case tok::pp_include_next:
1892 case tok::pp_import:
1894 parseIncludeDirective();
1898 case tok::pp_warning:
1899 parseWarningOrError();
1901 case tok::pp_pragma:
1906 Contexts.back().IsExpression =
true;
1909 CurrentToken->SpacesRequiredBefore =
true;
1915 while (CurrentToken) {
1916 FormatToken *Tok = CurrentToken;
1918 if (Tok->is(tok::l_paren)) {
1920 }
else if (Tok->isOneOf(Keywords.kw___has_include,
1921 Keywords.kw___has_include_next)) {
1932 NonTemplateLess.clear();
1933 if (!Line.InMacroBody && CurrentToken->is(tok::hash)) {
1937 auto Type = parsePreprocessorDirective();
1945 IdentifierInfo *Info = CurrentToken->Tok.getIdentifierInfo();
1947 CurrentToken->is(Keywords.kw_package)) ||
1948 (!Style.isVerilog() && Info &&
1949 Info->getPPKeywordID() == tok::pp_import && CurrentToken->Next &&
1950 CurrentToken->Next->isOneOf(tok::string_literal, tok::identifier,
1953 parseIncludeDirective();
1959 if (CurrentToken->is(tok::less) && Line.Last->is(tok::greater)) {
1960 parseIncludeDirective();
1967 CurrentToken->isOneOf(Keywords.kw_option, Keywords.kw_package)) {
1969 if (CurrentToken && CurrentToken->is(tok::identifier)) {
1970 while (CurrentToken)
1976 bool KeywordVirtualFound =
false;
1977 bool ImportStatement =
false;
1980 if (Style.isJavaScript() && CurrentToken->is(Keywords.kw_import))
1981 ImportStatement =
true;
1983 while (CurrentToken) {
1984 if (CurrentToken->is(tok::kw_virtual))
1985 KeywordVirtualFound =
true;
1986 if (Style.isJavaScript()) {
1993 if (Line.First->is(tok::kw_export) &&
1994 CurrentToken->is(Keywords.kw_from) && CurrentToken->Next &&
1995 CurrentToken->Next->isStringLiteral()) {
1996 ImportStatement =
true;
1998 if (isClosureImportStatement(*CurrentToken))
1999 ImportStatement =
true;
2001 if (!consumeToken())
2004 if (KeywordVirtualFound)
2006 if (ImportStatement)
2009 if (Line.startsWith(TT_ObjCMethodSpecifier)) {
2010 if (Contexts.back().FirstObjCSelectorName) {
2011 Contexts.back().FirstObjCSelectorName->LongestObjCSelectorName =
2012 Contexts.back().LongestObjCSelectorName;
2017 for (
const auto &ctx : Contexts)
2018 if (ctx.ContextType == Context::StructArrayInitializer)
2025 bool isClosureImportStatement(
const FormatToken &Tok) {
2028 return Tok.TokenText ==
"goog" && Tok.Next && Tok.Next->is(tok::period) &&
2030 (Tok.Next->Next->TokenText ==
"module" ||
2031 Tok.Next->Next->TokenText ==
"provide" ||
2032 Tok.Next->Next->TokenText ==
"require" ||
2033 Tok.Next->Next->TokenText ==
"requireType" ||
2034 Tok.Next->Next->TokenText ==
"forwardDeclare") &&
2035 Tok.Next->Next->Next && Tok.Next->Next->Next->is(tok::l_paren);
2038 void resetTokenMetadata() {
2044 if (!CurrentToken->isTypeFinalized() &&
2045 !CurrentToken->isOneOf(
2046 TT_LambdaLSquare, TT_LambdaLBrace, TT_AttributeMacro, TT_IfMacro,
2047 TT_ForEachMacro, TT_TypenameMacro, TT_FunctionLBrace,
2048 TT_ImplicitStringLiteral, TT_InlineASMBrace, TT_FatArrow,
2049 TT_NamespaceMacro, TT_OverloadedOperator, TT_RegexLiteral,
2050 TT_TemplateString, TT_ObjCStringLiteral, TT_UntouchableMacroFunc,
2051 TT_StatementAttributeLikeMacro, TT_FunctionLikeOrFreestandingMacro,
2052 TT_ClassLBrace, TT_EnumLBrace, TT_RecordLBrace, TT_StructLBrace,
2053 TT_UnionLBrace, TT_RequiresClause,
2054 TT_RequiresClauseInARequiresExpression, TT_RequiresExpression,
2055 TT_RequiresExpressionLParen, TT_RequiresExpressionLBrace,
2056 TT_BracedListLBrace)) {
2057 CurrentToken->setType(TT_Unknown);
2059 CurrentToken->Role.reset();
2060 CurrentToken->MatchingParen =
nullptr;
2061 CurrentToken->FakeLParens.clear();
2062 CurrentToken->FakeRParens = 0;
2069 CurrentToken->NestingLevel = Contexts.size() - 1;
2070 CurrentToken->BindingStrength = Contexts.back().BindingStrength;
2071 modifyContext(*CurrentToken);
2072 determineTokenType(*CurrentToken);
2073 CurrentToken = CurrentToken->Next;
2075 resetTokenMetadata();
2118 StructArrayInitializer,
2122 C11GenericSelection,
2124 VerilogInstancePortList,
2130 struct ScopedContextCreator {
2131 AnnotatingParser &
P;
2137 P.Contexts.back().BindingStrength + Increase,
2138 P.Contexts.back().IsExpression));
2141 ~ScopedContextCreator() {
2143 if (
P.Contexts.back().ContextType == Context::StructArrayInitializer) {
2144 P.Contexts.pop_back();
2145 P.Contexts.back().ContextType = Context::StructArrayInitializer;
2149 P.Contexts.pop_back();
2153 void modifyContext(
const FormatToken &Current) {
2154 auto AssignmentStartsExpression = [&]() {
2158 if (Line.First->isOneOf(tok::kw_using, tok::kw_return))
2160 if (Line.First->is(tok::kw_template)) {
2161 assert(Current.Previous);
2162 if (Current.Previous->is(tok::kw_operator)) {
2168 const FormatToken *Tok = Line.First->getNextNonComment();
2170 if (Tok->isNot(TT_TemplateOpener)) {
2177 if (Contexts.back().ContextKind == tok::less) {
2178 assert(Current.Previous->Previous);
2179 return !Current.Previous->Previous->isOneOf(tok::kw_typename,
2183 Tok = Tok->MatchingParen;
2186 Tok = Tok->getNextNonComment();
2190 if (Tok->isOneOf(tok::kw_class, tok::kw_enum, tok::kw_struct,
2200 if (Style.isJavaScript() &&
2201 (Line.startsWith(Keywords.kw_type, tok::identifier) ||
2202 Line.startsWith(tok::kw_export, Keywords.kw_type,
2203 tok::identifier))) {
2207 return !Current.Previous || Current.Previous->isNot(tok::kw_operator);
2210 if (AssignmentStartsExpression()) {
2211 Contexts.back().IsExpression =
true;
2212 if (!Line.startsWith(TT_UnaryOperator)) {
2213 for (FormatToken *
Previous = Current.Previous;
2215 !
Previous->Previous->isOneOf(tok::comma, tok::semi);
2217 if (
Previous->isOneOf(tok::r_square, tok::r_paren, tok::greater)) {
2224 if (
Previous->isOneOf(TT_BinaryOperator, TT_UnaryOperator) &&
2226 Previous->Previous->isNot(tok::equal)) {
2227 Previous->setType(TT_PointerOrReference);
2231 }
else if (Current.is(tok::lessless) &&
2232 (!Current.Previous ||
2233 Current.Previous->isNot(tok::kw_operator))) {
2234 Contexts.back().IsExpression =
true;
2235 }
else if (Current.isOneOf(tok::kw_return, tok::kw_throw)) {
2236 Contexts.back().IsExpression =
true;
2237 }
else if (Current.is(TT_TrailingReturnArrow)) {
2238 Contexts.back().IsExpression =
false;
2239 }
else if (Current.is(Keywords.kw_assert)) {
2241 }
else if (Current.Previous &&
2242 Current.Previous->is(TT_CtorInitializerColon)) {
2243 Contexts.back().IsExpression =
true;
2244 Contexts.back().ContextType = Context::CtorInitializer;
2245 }
else if (Current.Previous && Current.Previous->is(TT_InheritanceColon)) {
2246 Contexts.back().ContextType = Context::InheritanceList;
2247 }
else if (Current.isOneOf(tok::r_paren, tok::greater, tok::comma)) {
2248 for (FormatToken *
Previous = Current.Previous;
2251 Previous->setType(TT_PointerOrReference);
2253 if (Line.MustBeDeclaration &&
2254 Contexts.front().ContextType != Context::CtorInitializer) {
2255 Contexts.back().IsExpression =
false;
2257 }
else if (Current.is(tok::kw_new)) {
2258 Contexts.back().CanBeExpression =
false;
2259 }
else if (Current.is(tok::semi) ||
2260 (Current.is(tok::exclaim) && Current.Previous &&
2261 Current.Previous->isNot(tok::kw_operator))) {
2265 Contexts.back().IsExpression =
true;
2269 static FormatToken *untilMatchingParen(FormatToken *Current) {
2273 if (Current->is(tok::l_paren))
2275 if (Current->is(tok::r_paren))
2279 Current = Current->Next;
2284 static bool isDeductionGuide(FormatToken &Current) {
2286 if (Current.Previous && Current.Previous->is(tok::r_paren) &&
2287 Current.startsSequence(tok::arrow, tok::identifier, tok::less)) {
2289 FormatToken *TemplateCloser = Current.Next->Next;
2290 int NestingLevel = 0;
2291 while (TemplateCloser) {
2293 if (TemplateCloser->is(tok::l_paren)) {
2295 TemplateCloser = untilMatchingParen(TemplateCloser);
2296 if (!TemplateCloser)
2299 if (TemplateCloser->is(tok::less))
2301 if (TemplateCloser->is(tok::greater))
2303 if (NestingLevel < 1)
2305 TemplateCloser = TemplateCloser->Next;
2309 if (TemplateCloser && TemplateCloser->Next &&
2310 TemplateCloser->Next->is(tok::semi) &&
2311 Current.Previous->MatchingParen) {
2314 FormatToken *LeadingIdentifier =
2315 Current.Previous->MatchingParen->Previous;
2317 return LeadingIdentifier &&
2318 LeadingIdentifier->TokenText == Current.Next->TokenText;
2324 void determineTokenType(FormatToken &Current) {
2325 if (Current.isNot(TT_Unknown)) {
2330 if ((Style.isJavaScript() || Style.isCSharp()) &&
2331 Current.is(tok::exclaim)) {
2332 if (Current.Previous) {
2334 Style.isJavaScript()
2335 ? Keywords.IsJavaScriptIdentifier(
2336 *Current.Previous,
true)
2337 : Current.Previous->is(tok::identifier);
2339 Current.Previous->isOneOf(
2340 tok::kw_default, tok::kw_namespace, tok::r_paren, tok::r_square,
2341 tok::r_brace, tok::kw_false, tok::kw_true, Keywords.kw_type,
2342 Keywords.kw_get, Keywords.kw_init, Keywords.kw_set) ||
2343 Current.Previous->Tok.isLiteral()) {
2344 Current.setType(TT_NonNullAssertion);
2349 Current.Next->isOneOf(TT_BinaryOperator, Keywords.kw_as)) {
2350 Current.setType(TT_NonNullAssertion);
2359 Current.is(Keywords.kw_instanceof)) {
2360 Current.setType(TT_BinaryOperator);
2361 }
else if (isStartOfName(Current) &&
2362 (!Line.MightBeFunctionDecl || Current.NestingLevel != 0)) {
2363 Contexts.back().FirstStartOfName = &Current;
2364 Current.setType(TT_StartOfName);
2365 }
else if (Current.is(tok::semi)) {
2369 Contexts.back().FirstStartOfName =
nullptr;
2370 }
else if (Current.isOneOf(tok::kw_auto, tok::kw___auto_type)) {
2372 }
else if (Current.is(tok::arrow) &&
2374 Current.setType(TT_TrailingReturnArrow);
2375 }
else if (Current.is(tok::arrow) && Style.isVerilog()) {
2377 Current.setType(TT_BinaryOperator);
2378 }
else if (Current.is(tok::arrow) && AutoFound &&
2379 Line.MightBeFunctionDecl && Current.NestingLevel == 0 &&
2380 !Current.Previous->isOneOf(tok::kw_operator, tok::identifier)) {
2382 Current.setType(TT_TrailingReturnArrow);
2383 }
else if (Current.is(tok::arrow) && Current.Previous &&
2384 Current.Previous->is(tok::r_brace)) {
2387 Current.setType(TT_TrailingReturnArrow);
2388 }
else if (isDeductionGuide(Current)) {
2390 Current.setType(TT_TrailingReturnArrow);
2391 }
else if (Current.isPointerOrReference()) {
2392 Current.setType(determineStarAmpUsage(
2394 Contexts.back().CanBeExpression && Contexts.back().IsExpression,
2395 Contexts.back().ContextType == Context::TemplateArgument));
2396 }
else if (Current.isOneOf(tok::minus, tok::plus, tok::caret) ||
2397 (Style.isVerilog() && Current.is(tok::pipe))) {
2398 Current.setType(determinePlusMinusCaretUsage(Current));
2399 if (Current.is(TT_UnaryOperator) && Current.is(tok::caret))
2400 Contexts.back().CaretFound =
true;
2401 }
else if (Current.isOneOf(tok::minusminus, tok::plusplus)) {
2402 Current.setType(determineIncrementUsage(Current));
2403 }
else if (Current.isOneOf(tok::exclaim, tok::tilde)) {
2404 Current.setType(TT_UnaryOperator);
2405 }
else if (Current.is(tok::question)) {
2406 if (Style.isJavaScript() && Line.MustBeDeclaration &&
2407 !Contexts.back().IsExpression) {
2410 Current.setType(TT_JsTypeOptionalQuestion);
2411 }
else if (Style.isTableGen()) {
2413 Current.setType(TT_Unknown);
2415 Current.setType(TT_ConditionalExpr);
2417 }
else if (Current.isBinaryOperator() &&
2418 (!Current.Previous || Current.Previous->isNot(tok::l_square)) &&
2419 (Current.isNot(tok::greater) &&
2421 if (Style.isVerilog()) {
2422 if (Current.is(tok::lessequal) && Contexts.size() == 1 &&
2423 !Contexts.back().VerilogAssignmentFound) {
2427 Current.setFinalizedType(TT_BinaryOperator);
2430 Contexts.back().VerilogAssignmentFound =
true;
2432 Current.setType(TT_BinaryOperator);
2433 }
else if (Current.is(tok::comment)) {
2434 if (Current.TokenText.starts_with(
"/*")) {
2435 if (Current.TokenText.ends_with(
"*/")) {
2436 Current.setType(TT_BlockComment);
2440 Current.Tok.setKind(tok::unknown);
2443 Current.setType(TT_LineComment);
2445 }
else if (Current.is(tok::string_literal)) {
2446 if (Style.isVerilog() && Contexts.back().VerilogMayBeConcatenation &&
2447 Current.getPreviousNonComment() &&
2448 Current.getPreviousNonComment()->isOneOf(tok::comma, tok::l_brace) &&
2449 Current.getNextNonComment() &&
2450 Current.getNextNonComment()->isOneOf(tok::comma, tok::r_brace)) {
2451 Current.setType(TT_StringInConcatenation);
2453 }
else if (Current.is(tok::l_paren)) {
2454 if (lParenStartsCppCast(Current))
2455 Current.setType(TT_CppCastLParen);
2456 }
else if (Current.is(tok::r_paren)) {
2457 if (rParenEndsCast(Current))
2458 Current.setType(TT_CastRParen);
2459 if (Current.MatchingParen && Current.Next &&
2460 !Current.Next->isBinaryOperator() &&
2461 !Current.Next->isOneOf(tok::semi, tok::colon, tok::l_brace,
2462 tok::comma, tok::period, tok::arrow,
2463 tok::coloncolon, tok::kw_noexcept)) {
2464 if (FormatToken *AfterParen = Current.MatchingParen->Next;
2465 AfterParen && AfterParen->isNot(tok::caret)) {
2467 if (FormatToken *BeforeParen = Current.MatchingParen->Previous;
2468 BeforeParen && BeforeParen->is(tok::identifier) &&
2469 BeforeParen->isNot(TT_TypenameMacro) &&
2470 BeforeParen->TokenText == BeforeParen->TokenText.upper() &&
2471 (!BeforeParen->Previous ||
2472 BeforeParen->Previous->ClosesTemplateDeclaration ||
2473 BeforeParen->Previous->ClosesRequiresClause)) {
2474 Current.setType(TT_FunctionAnnotationRParen);
2478 }
else if (Current.is(tok::at) && Current.Next && !Style.isJavaScript() &&
2482 switch (Current.Next->Tok.getObjCKeywordID()) {
2483 case tok::objc_interface:
2484 case tok::objc_implementation:
2485 case tok::objc_protocol:
2486 Current.setType(TT_ObjCDecl);
2488 case tok::objc_property:
2489 Current.setType(TT_ObjCProperty);
2494 }
else if (Current.is(tok::period)) {
2495 FormatToken *PreviousNoComment = Current.getPreviousNonComment();
2496 if (PreviousNoComment &&
2497 PreviousNoComment->isOneOf(tok::comma, tok::l_brace)) {
2498 Current.setType(TT_DesignatedInitializerPeriod);
2500 Current.Previous->isOneOf(TT_JavaAnnotation,
2501 TT_LeadingJavaAnnotation)) {
2502 Current.setType(Current.Previous->getType());
2504 }
else if (canBeObjCSelectorComponent(Current) &&
2507 Current.Previous && Current.Previous->is(TT_CastRParen) &&
2508 Current.Previous->MatchingParen &&
2509 Current.Previous->MatchingParen->Previous &&
2510 Current.Previous->MatchingParen->Previous->is(
2511 TT_ObjCMethodSpecifier)) {
2515 Current.setType(TT_SelectorName);
2516 }
else if (Current.isOneOf(tok::identifier, tok::kw_const, tok::kw_noexcept,
2517 tok::kw_requires) &&
2519 !Current.Previous->isOneOf(tok::equal, tok::at,
2520 TT_CtorInitializerComma,
2521 TT_CtorInitializerColon) &&
2522 Line.MightBeFunctionDecl && Contexts.size() == 1) {
2525 Current.setType(TT_TrailingAnnotation);
2527 Style.isJavaScript()) &&
2529 if (Current.Previous->is(tok::at) &&
2530 Current.isNot(Keywords.kw_interface)) {
2531 const FormatToken &AtToken = *Current.Previous;
2532 const FormatToken *
Previous = AtToken.getPreviousNonComment();
2534 Current.setType(TT_LeadingJavaAnnotation);
2536 Current.setType(TT_JavaAnnotation);
2537 }
else if (Current.Previous->is(tok::period) &&
2538 Current.Previous->isOneOf(TT_JavaAnnotation,
2539 TT_LeadingJavaAnnotation)) {
2540 Current.setType(Current.Previous->getType());
2550 bool isStartOfName(
const FormatToken &Tok) {
2552 if (Style.isVerilog())
2555 if (Tok.isNot(tok::identifier) || !Tok.Previous)
2558 if (
const auto *NextNonComment = Tok.getNextNonComment();
2559 (!NextNonComment && !Line.InMacroBody) ||
2561 (NextNonComment->isPointerOrReference() ||
2562 NextNonComment->is(tok::string_literal) ||
2563 (Line.InPragmaDirective && NextNonComment->is(tok::identifier))))) {
2567 if (Tok.Previous->isOneOf(TT_LeadingJavaAnnotation, Keywords.kw_instanceof,
2571 if (Style.isJavaScript() && Tok.Previous->is(Keywords.kw_in))
2575 FormatToken *PreviousNotConst = Tok.getPreviousNonComment();
2578 if (!Style.isJavaScript())
2579 while (PreviousNotConst && PreviousNotConst->is(tok::kw_const))
2580 PreviousNotConst = PreviousNotConst->getPreviousNonComment();
2582 if (!PreviousNotConst)
2585 if (PreviousNotConst->ClosesRequiresClause)
2588 if (Style.isTableGen()) {
2590 if (Keywords.isTableGenDefinition(*PreviousNotConst))
2593 if (Contexts.back().ContextKind != tok::l_brace)
2597 bool IsPPKeyword = PreviousNotConst->is(tok::identifier) &&
2598 PreviousNotConst->Previous &&
2599 PreviousNotConst->Previous->is(tok::hash);
2601 if (PreviousNotConst->is(TT_TemplateCloser)) {
2602 return PreviousNotConst && PreviousNotConst->MatchingParen &&
2603 PreviousNotConst->MatchingParen->Previous &&
2604 PreviousNotConst->MatchingParen->Previous->isNot(tok::period) &&
2605 PreviousNotConst->MatchingParen->Previous->isNot(tok::kw_template);
2608 if ((PreviousNotConst->is(tok::r_paren) &&
2609 PreviousNotConst->is(TT_TypeDeclarationParen)) ||
2610 PreviousNotConst->is(TT_AttributeRParen)) {
2619 if (PreviousNotConst->isOneOf(tok::identifier, tok::kw_auto))
2623 if (PreviousNotConst->is(TT_PointerOrReference))
2627 if (PreviousNotConst->isTypeName(IsCpp))
2632 PreviousNotConst->is(tok::r_square)) {
2637 return Style.isJavaScript() && PreviousNotConst->is(tok::kw_const);
2641 bool lParenStartsCppCast(
const FormatToken &Tok) {
2646 FormatToken *LeftOfParens = Tok.getPreviousNonComment();
2647 if (LeftOfParens && LeftOfParens->is(TT_TemplateCloser) &&
2648 LeftOfParens->MatchingParen) {
2649 auto *Prev = LeftOfParens->MatchingParen->getPreviousNonComment();
2651 Prev->isOneOf(tok::kw_const_cast, tok::kw_dynamic_cast,
2652 tok::kw_reinterpret_cast, tok::kw_static_cast)) {
2662 bool rParenEndsCast(
const FormatToken &Tok) {
2668 if (Tok.Previous == Tok.MatchingParen || !Tok.Next || !Tok.MatchingParen)
2671 if (Tok.MatchingParen->is(TT_OverloadedOperatorLParen))
2674 FormatToken *LeftOfParens = Tok.MatchingParen->getPreviousNonComment();
2678 if (LeftOfParens->is(tok::r_paren) &&
2679 LeftOfParens->isNot(TT_CastRParen)) {
2680 if (!LeftOfParens->MatchingParen ||
2681 !LeftOfParens->MatchingParen->Previous) {
2684 LeftOfParens = LeftOfParens->MatchingParen->Previous;
2687 if (LeftOfParens->is(tok::r_square)) {
2689 auto MayBeArrayDelete = [](FormatToken *Tok) -> FormatToken * {
2690 if (Tok->isNot(tok::r_square))
2693 Tok = Tok->getPreviousNonComment();
2694 if (!Tok || Tok->isNot(tok::l_square))
2697 Tok = Tok->getPreviousNonComment();
2698 if (!Tok || Tok->isNot(tok::kw_delete))
2702 if (FormatToken *MaybeDelete = MayBeArrayDelete(LeftOfParens))
2703 LeftOfParens = MaybeDelete;
2709 if (LeftOfParens->Tok.getIdentifierInfo() && LeftOfParens->Previous &&
2710 LeftOfParens->Previous->is(tok::kw_operator)) {
2716 if (LeftOfParens->Tok.getIdentifierInfo() &&
2717 !LeftOfParens->isOneOf(Keywords.kw_in, tok::kw_return, tok::kw_case,
2718 tok::kw_delete, tok::kw_throw)) {
2724 if (LeftOfParens->isOneOf(tok::at, tok::r_square, TT_OverloadedOperator,
2725 TT_TemplateCloser, tok::ellipsis)) {
2730 if (Tok.Next->is(tok::question) ||
2731 (Tok.Next->is(tok::ampamp) && !Tok.Previous->isTypeName(IsCpp))) {
2736 if (Tok.Next->is(Keywords.kw_in) && Style.isCSharp())
2741 if (Tok.Next->isOneOf(tok::kw_noexcept, tok::kw_volatile, tok::kw_const,
2742 tok::kw_requires, tok::kw_throw, tok::arrow,
2743 Keywords.kw_override, Keywords.kw_final) ||
2744 isCppAttribute(IsCpp, *Tok.Next)) {
2754 if (Tok.Next->isOneOf(tok::kw_sizeof, tok::kw_alignof) ||
2755 (Tok.Next->Tok.isLiteral() && Tok.Next->isNot(tok::string_literal))) {
2760 auto IsQualifiedPointerOrReference = [](FormatToken *
T,
bool IsCpp) {
2762 assert(!
T->isTypeName(IsCpp) &&
"Should have already been checked");
2766 if (
T->is(TT_AttributeRParen)) {
2768 assert(
T->is(tok::r_paren));
2769 assert(
T->MatchingParen);
2770 assert(
T->MatchingParen->is(tok::l_paren));
2771 assert(
T->MatchingParen->is(TT_AttributeLParen));
2772 if (
const auto *Tok =
T->MatchingParen->Previous;
2773 Tok && Tok->isAttribute()) {
2777 }
else if (
T->is(TT_AttributeSquare)) {
2779 if (
T->MatchingParen &&
T->MatchingParen->Previous) {
2780 T =
T->MatchingParen->Previous;
2783 }
else if (
T->canBePointerOrReferenceQualifier()) {
2789 return T &&
T->is(TT_PointerOrReference);
2791 bool ParensAreType =
2793 Tok.Previous->isOneOf(TT_TemplateCloser, TT_TypeDeclarationParen) ||
2794 Tok.Previous->isTypeName(IsCpp) ||
2795 IsQualifiedPointerOrReference(Tok.Previous, IsCpp);
2796 bool ParensCouldEndDecl =
2797 Tok.Next->isOneOf(tok::equal, tok::semi, tok::l_brace, tok::greater);
2798 if (ParensAreType && !ParensCouldEndDecl)
2809 for (
const FormatToken *Token = Tok.MatchingParen->Next; Token != &Tok;
2810 Token = Token->Next) {
2811 if (Token->is(TT_BinaryOperator))
2817 if (Tok.Next->isOneOf(tok::identifier, tok::kw_this))
2821 if (Tok.Next->is(tok::l_paren) && Tok.Previous && Tok.Previous->Previous) {
2822 if (Tok.Previous->is(tok::identifier) &&
2823 Tok.Previous->Previous->is(tok::l_paren)) {
2828 if (!Tok.Next->Next)
2834 const bool NextIsAmpOrStar = Tok.Next->isOneOf(tok::amp, tok::star);
2835 if (!(Tok.Next->isUnaryOperator() || NextIsAmpOrStar) ||
2836 Tok.Next->is(tok::plus) ||
2837 !Tok.Next->Next->isOneOf(tok::identifier, tok::numeric_constant)) {
2840 if (NextIsAmpOrStar &&
2841 (Tok.Next->Next->is(tok::numeric_constant) || Line.InPPDirective)) {
2844 if (Line.InPPDirective && Tok.Next->is(tok::minus))
2847 for (FormatToken *Prev = Tok.Previous; Prev != Tok.MatchingParen;
2848 Prev = Prev->Previous) {
2849 if (!Prev->isOneOf(tok::kw_const, tok::identifier, tok::coloncolon))
2856 bool determineUnaryOperatorByUsage(
const FormatToken &Tok) {
2857 const FormatToken *PrevToken = Tok.getPreviousNonComment();
2867 if (PrevToken->isOneOf(
2868 TT_ConditionalExpr, tok::l_paren, tok::comma, tok::colon, tok::semi,
2869 tok::equal, tok::question, tok::l_square, tok::l_brace,
2870 tok::kw_case, tok::kw_co_await, tok::kw_co_return, tok::kw_co_yield,
2871 tok::kw_delete, tok::kw_return, tok::kw_throw)) {
2878 if (PrevToken->is(tok::kw_sizeof))
2882 if (PrevToken->isOneOf(TT_CastRParen, TT_UnaryOperator))
2886 if (PrevToken->is(TT_BinaryOperator))
2894 bool InTemplateArgument) {
2895 if (Style.isJavaScript())
2896 return TT_BinaryOperator;
2899 if (Style.isCSharp() && Tok.is(tok::ampamp))
2900 return TT_BinaryOperator;
2902 if (Style.isVerilog()) {
2907 if (Tok.is(tok::star))
2908 return TT_BinaryOperator;
2909 return determineUnaryOperatorByUsage(Tok) ? TT_UnaryOperator
2910 : TT_BinaryOperator;
2913 const FormatToken *PrevToken = Tok.getPreviousNonComment();
2915 return TT_UnaryOperator;
2916 if (PrevToken->is(TT_TypeName))
2917 return TT_PointerOrReference;
2918 if (PrevToken->isOneOf(tok::kw_new, tok::kw_delete) && Tok.is(tok::ampamp))
2919 return TT_BinaryOperator;
2921 const FormatToken *NextToken = Tok.getNextNonComment();
2923 if (InTemplateArgument && NextToken && NextToken->is(tok::kw_noexcept))
2924 return TT_BinaryOperator;
2927 NextToken->isOneOf(tok::arrow, tok::equal, tok::comma, tok::r_paren,
2928 TT_RequiresClause) ||
2930 NextToken->canBePointerOrReferenceQualifier() ||
2931 (NextToken->is(tok::l_brace) && !NextToken->getNextNonComment())) {
2932 return TT_PointerOrReference;
2935 if (PrevToken->is(tok::coloncolon))
2936 return TT_PointerOrReference;
2938 if (PrevToken->is(tok::r_paren) && PrevToken->is(TT_TypeDeclarationParen))
2939 return TT_PointerOrReference;
2941 if (determineUnaryOperatorByUsage(Tok))
2942 return TT_UnaryOperator;
2944 if (NextToken->is(tok::l_square) && NextToken->isNot(TT_LambdaLSquare))
2945 return TT_PointerOrReference;
2947 return TT_PointerOrReference;
2948 if (NextToken->isOneOf(tok::comma, tok::semi))
2949 return TT_PointerOrReference;
2961 if (PrevToken->is(tok::r_brace) && Tok.is(tok::star) &&
2962 !PrevToken->MatchingParen) {
2963 return TT_PointerOrReference;
2966 if (PrevToken->endsSequence(tok::r_square, tok::l_square, tok::kw_delete))
2967 return TT_UnaryOperator;
2969 if (PrevToken->Tok.isLiteral() ||
2970 PrevToken->isOneOf(tok::r_paren, tok::r_square, tok::kw_true,
2971 tok::kw_false, tok::r_brace)) {
2972 return TT_BinaryOperator;
2975 const FormatToken *NextNonParen = NextToken;
2976 while (NextNonParen && NextNonParen->is(tok::l_paren))
2977 NextNonParen = NextNonParen->getNextNonComment();
2978 if (NextNonParen && (NextNonParen->Tok.isLiteral() ||
2979 NextNonParen->isOneOf(tok::kw_true, tok::kw_false) ||
2980 NextNonParen->isUnaryOperator())) {
2981 return TT_BinaryOperator;
2987 if (InTemplateArgument && NextToken->Tok.isAnyIdentifier())
2988 return TT_BinaryOperator;
2992 if (Tok.is(tok::ampamp) &&
2993 NextToken->isOneOf(tok::l_paren, tok::star, tok::amp)) {
2994 return TT_BinaryOperator;
2999 if (NextToken->Tok.isAnyIdentifier()) {
3000 const FormatToken *NextNextToken = NextToken->getNextNonComment();
3001 if (NextNextToken && NextNextToken->is(tok::arrow))
3002 return TT_BinaryOperator;
3008 return TT_BinaryOperator;
3011 if (!Scopes.empty() && Scopes.back() ==
ST_Class)
3012 return TT_PointerOrReference;
3015 auto IsChainedOperatorAmpOrMember = [](
const FormatToken *token) {
3016 return !token || token->isOneOf(tok::amp, tok::period, tok::arrow,
3017 tok::arrowstar, tok::periodstar);
3022 if (Tok.is(tok::amp) && PrevToken && PrevToken->Tok.isAnyIdentifier() &&
3023 IsChainedOperatorAmpOrMember(PrevToken->getPreviousNonComment()) &&
3024 NextToken && NextToken->Tok.isAnyIdentifier()) {
3025 if (
auto NextNext = NextToken->getNextNonComment();
3027 (IsChainedOperatorAmpOrMember(NextNext) || NextNext->is(tok::semi))) {
3028 return TT_BinaryOperator;
3032 return TT_PointerOrReference;
3035 TokenType determinePlusMinusCaretUsage(
const FormatToken &Tok) {
3036 if (determineUnaryOperatorByUsage(Tok))
3037 return TT_UnaryOperator;
3039 const FormatToken *PrevToken = Tok.getPreviousNonComment();
3041 return TT_UnaryOperator;
3043 if (PrevToken->is(tok::at))
3044 return TT_UnaryOperator;
3047 return TT_BinaryOperator;
3051 TokenType determineIncrementUsage(
const FormatToken &Tok) {
3052 const FormatToken *PrevToken = Tok.getPreviousNonComment();
3053 if (!PrevToken || PrevToken->is(TT_CastRParen))
3054 return TT_UnaryOperator;
3055 if (PrevToken->isOneOf(tok::r_paren, tok::r_square, tok::identifier))
3056 return TT_TrailingUnaryOperator;
3058 return TT_UnaryOperator;
3061 SmallVector<Context, 8> Contexts;
3063 const FormatStyle &Style;
3064 AnnotatedLine &Line;
3065 FormatToken *CurrentToken;
3068 const AdditionalKeywords &Keywords;
3070 SmallVector<ScopeType> &Scopes;
3084class ExpressionParser {
3086 ExpressionParser(
const FormatStyle &Style,
const AdditionalKeywords &Keywords,
3087 AnnotatedLine &Line)
3088 : Style(Style), Keywords(Keywords), Line(Line), Current(Line.
First) {}
3091 void parse(
int Precedence = 0) {
3094 while (Current && (Current->is(tok::kw_return) ||
3095 (Current->is(tok::colon) &&
3096 Current->isOneOf(TT_ObjCMethodExpr, TT_DictLiteral)))) {
3100 if (!Current || Precedence > PrecedenceArrowAndPeriod)
3105 parseConditionalExpr();
3111 if (Precedence == PrecedenceUnaryOperator) {
3112 parseUnaryOperator();
3116 FormatToken *Start = Current;
3117 FormatToken *LatestOperator =
nullptr;
3118 unsigned OperatorIndex = 0;
3120 FormatToken *VerilogFirstOfType =
nullptr;
3129 if (Style.isVerilog() && Precedence ==
prec::Comma) {
3130 VerilogFirstOfType =
3131 verilogGroupDecl(VerilogFirstOfType, LatestOperator);
3135 parse(Precedence + 1);
3137 int CurrentPrecedence = getCurrentPrecedence();
3139 if (Precedence == CurrentPrecedence && Current &&
3140 Current->is(TT_SelectorName)) {
3142 addFakeParenthesis(Start,
prec::Level(Precedence));
3146 if ((Style.isCSharp() || Style.isJavaScript() ||
3151 FormatToken *Prev = Current->getPreviousNonComment();
3152 if (Prev && Prev->is(tok::string_literal) &&
3153 (Prev == Start || Prev->endsSequence(tok::string_literal, tok::plus,
3154 TT_StringInConcatenation))) {
3155 Prev->setType(TT_StringInConcatenation);
3162 (Current->closesScope() &&
3163 (Current->MatchingParen || Current->is(TT_TemplateString))) ||
3164 (CurrentPrecedence != -1 && CurrentPrecedence < Precedence) ||
3173 if (Current->opensScope() ||
3174 Current->isOneOf(TT_RequiresClause,
3175 TT_RequiresClauseInARequiresExpression)) {
3178 while (Current && (!Current->closesScope() || Current->opensScope())) {
3185 if (CurrentPrecedence == Precedence) {
3187 LatestOperator->NextOperator = Current;
3188 LatestOperator = Current;
3189 Current->OperatorIndex = OperatorIndex;
3192 next(Precedence > 0);
3197 if (Style.isVerilog() && Precedence ==
prec::Comma && VerilogFirstOfType)
3198 addFakeParenthesis(VerilogFirstOfType,
prec::Comma);
3200 if (LatestOperator && (Current || Precedence > 0)) {
3206 Start->Previous->isOneOf(TT_RequiresClause,
3207 TT_RequiresClauseInARequiresExpression))
3209 auto Ret = Current ? Current : Line.Last;
3210 while (!
Ret->ClosesRequiresClause &&
Ret->Previous)
3216 if (Precedence == PrecedenceArrowAndPeriod) {
3220 addFakeParenthesis(Start,
prec::Level(Precedence), End);
3228 int getCurrentPrecedence() {
3230 const FormatToken *NextNonComment = Current->getNextNonComment();
3231 if (Current->is(TT_ConditionalExpr))
3233 if (NextNonComment && Current->is(TT_SelectorName) &&
3234 (NextNonComment->isOneOf(TT_DictLiteral, TT_JsTypeColon) ||
3235 (Style.isProto() && NextNonComment->is(tok::less)))) {
3238 if (Current->is(TT_JsComputedPropertyName))
3240 if (Current->is(TT_TrailingReturnArrow))
3242 if (Current->is(TT_FatArrow))
3244 if (Current->isOneOf(tok::semi, TT_InlineASMColon, TT_SelectorName) ||
3245 (Current->is(tok::comment) && NextNonComment &&
3246 NextNonComment->is(TT_SelectorName))) {
3249 if (Current->is(TT_RangeBasedForLoopColon))
3252 Current->is(Keywords.kw_instanceof)) {
3255 if (Style.isJavaScript() &&
3256 Current->isOneOf(Keywords.kw_in, Keywords.kw_as)) {
3259 if (Current->is(TT_BinaryOperator) || Current->is(tok::comma))
3260 return Current->getPrecedence();
3261 if (Current->isOneOf(tok::period, tok::arrow) &&
3262 Current->isNot(TT_TrailingReturnArrow)) {
3263 return PrecedenceArrowAndPeriod;
3266 Current->isOneOf(Keywords.kw_extends, Keywords.kw_implements,
3267 Keywords.kw_throws)) {
3272 if (Style.isVerilog() && Current->is(tok::colon))
3278 void addFakeParenthesis(FormatToken *Start,
prec::Level Precedence,
3279 FormatToken *End =
nullptr) {
3284 if (Start->MacroParent)
3287 Start->FakeLParens.push_back(Precedence);
3289 Start->StartsBinaryExpression =
true;
3290 if (!End && Current)
3291 End = Current->getPreviousNonComment();
3295 End->EndsBinaryExpression =
true;
3301 void parseUnaryOperator() {
3303 while (Current && Current->is(TT_UnaryOperator)) {
3304 Tokens.push_back(Current);
3307 parse(PrecedenceArrowAndPeriod);
3308 for (FormatToken *Token : llvm::reverse(Tokens)) {
3314 void parseConditionalExpr() {
3315 while (Current && Current->isTrailingComment())
3317 FormatToken *Start = Current;
3319 if (!Current || Current->isNot(tok::question))
3323 if (!Current || Current->isNot(TT_ConditionalExpr))
3330 void next(
bool SkipPastLeadingComments =
true) {
3332 Current = Current->Next;
3334 (Current->NewlinesBefore == 0 || SkipPastLeadingComments) &&
3335 Current->isTrailingComment()) {
3336 Current = Current->Next;
3342 FormatToken *verilogGroupDecl(FormatToken *FirstOfType,
3343 FormatToken *PreviousComma) {
3347 FormatToken *Start = Current;
3350 while (Start->startsSequence(tok::l_paren, tok::star)) {
3351 if (!(Start = Start->MatchingParen) ||
3352 !(Start = Start->getNextNonComment())) {
3357 FormatToken *Tok = Start;
3359 if (Tok->is(Keywords.kw_assign))
3360 Tok = Tok->getNextNonComment();
3368 FormatToken *
First =
nullptr;
3370 FormatToken *Next = Tok->getNextNonComment();
3372 if (Tok->is(tok::hash)) {
3377 Tok = Tok->getNextNonComment();
3378 }
else if (Tok->is(tok::hashhash)) {
3382 Tok = Tok->getNextNonComment();
3383 }
else if (Keywords.isVerilogQualifier(*Tok) ||
3384 Keywords.isVerilogIdentifier(*Tok)) {
3388 while (Tok && Tok->isOneOf(tok::period, tok::coloncolon) &&
3389 (Tok = Tok->getNextNonComment())) {
3390 if (Keywords.isVerilogIdentifier(*Tok))
3391 Tok = Tok->getNextNonComment();
3395 }
else if (Tok->is(tok::l_paren)) {
3400 Keywords.kw_highz0, Keywords.kw_highz1, Keywords.kw_large,
3401 Keywords.kw_medium, Keywords.kw_pull0, Keywords.kw_pull1,
3402 Keywords.kw_small, Keywords.kw_strong0, Keywords.kw_strong1,
3403 Keywords.kw_supply0, Keywords.kw_supply1, Keywords.kw_weak0,
3404 Keywords.kw_weak1)) {
3405 Tok->setType(TT_VerilogStrength);
3406 Tok = Tok->MatchingParen;
3408 Tok->setType(TT_VerilogStrength);
3409 Tok = Tok->getNextNonComment();
3414 }
else if (Tok->is(tok::hash)) {
3415 if (Next->is(tok::l_paren))
3416 Next = Next->MatchingParen;
3418 Tok = Next->getNextNonComment();
3425 FormatToken *Second =
nullptr;
3427 while (Tok && Tok->is(tok::l_square) && (Tok = Tok->MatchingParen))
3428 Tok = Tok->getNextNonComment();
3429 if (Tok && (Tok->is(tok::hash) || Keywords.isVerilogIdentifier(*Tok)))
3434 FormatToken *TypedName =
nullptr;
3438 First->setType(TT_VerilogDimensionedTypeName);
3439 }
else if (
First != Start) {
3447 if (TypedName->is(TT_Unknown))
3448 TypedName->setType(TT_StartOfName);
3450 if (FirstOfType && PreviousComma) {
3451 PreviousComma->setType(TT_VerilogTypeComma);
3452 addFakeParenthesis(FirstOfType,
prec::Comma, PreviousComma->Previous);
3455 FirstOfType = TypedName;
3462 while (Current && Current != FirstOfType) {
3463 if (Current->opensScope()) {
3474 const FormatStyle &Style;
3475 const AdditionalKeywords &Keywords;
3476 const AnnotatedLine &Line;
3477 FormatToken *Current;
3486 assert(
Line->First);
3493 Line->First->OriginalColumn) {
3494 const bool PPDirectiveOrImportStmt =
3497 if (PPDirectiveOrImportStmt)
3503 PPDirectiveOrImportStmt
3505 : NextNonCommentLine->
Level;
3507 NextNonCommentLine =
Line->First->isNot(tok::r_brace) ?
Line :
nullptr;
3516 for (
const auto *Tok =
Line.First; Tok; Tok = Tok->Next)
3524 for (
FormatToken *Tok =
Line.getFirstNonComment(), *Name =
nullptr; Tok;
3525 Tok = Tok->getNextNonComment()) {
3527 if (Tok->is(tok::l_square) && Tok->is(TT_AttributeSquare)) {
3536 return Tok->
is(tok::l_paren) && Tok->isNot(TT_FunctionTypeLParen) &&
3543 if (Tok->isOneOf(tok::kw_friend, tok::kw_inline, tok::kw_virtual,
3544 tok::kw_constexpr, tok::kw_consteval, tok::kw_explicit)) {
3549 if (Tok->is(tok::coloncolon)) {
3556 while (Tok->startsSequence(tok::identifier, tok::coloncolon)) {
3558 Tok = Tok->Next->Next;
3564 if (Tok->is(tok::tilde)) {
3571 if (Tok->isNot(tok::identifier) || Tok->isNot(TT_Unknown))
3582 assert(Tok && Tok->
is(tok::identifier));
3585 if (Prev && Prev->is(tok::tilde))
3588 if (!Prev || !Prev->endsSequence(tok::coloncolon, tok::identifier))
3591 assert(Prev->Previous);
3592 return Prev->Previous->TokenText == Tok->
TokenText;
3596 AnnotatingParser
Parser(Style,
Line, Keywords, Scopes);
3599 for (
auto &Child :
Line.Children)
3612 ExpressionParser ExprParser(Style, Keywords,
Line);
3617 if (Tok && ((!Scopes.empty() && Scopes.back() ==
ST_Class) ||
3619 Tok->setFinalizedType(TT_CtorDtorDeclName);
3623 if (
Line.startsWith(TT_ObjCMethodSpecifier))
3625 else if (
Line.startsWith(TT_ObjCDecl))
3627 else if (
Line.startsWith(TT_ObjCProperty))
3631 First->SpacesRequiredBefore = 1;
3632 First->CanBreakBefore =
First->MustBreakBefore;
3634 if (
First->is(tok::eof) &&
First->NewlinesBefore == 0 &&
3635 Style.InsertNewlineAtEOF) {
3636 First->NewlinesBefore = 1;
3647 if (Current.
is(TT_FunctionDeclarationName))
3655 if (
const auto *PrevPrev =
Previous.Previous;
3656 PrevPrev && PrevPrev->is(TT_ObjCDecl)) {
3660 auto skipOperatorName =
3662 for (; Next; Next = Next->Next) {
3663 if (Next->is(TT_OverloadedOperatorLParen))
3665 if (Next->is(TT_OverloadedOperator))
3667 if (Next->isOneOf(tok::kw_new, tok::kw_delete)) {
3670 Next->Next->startsSequence(tok::l_square, tok::r_square)) {
3671 Next = Next->Next->Next;
3675 if (Next->startsSequence(tok::l_square, tok::r_square)) {
3680 if ((Next->isTypeName(IsCpp) || Next->is(tok::identifier)) &&
3681 Next->Next && Next->Next->isPointerOrReference()) {
3686 if (Next->is(TT_TemplateOpener) && Next->MatchingParen) {
3687 Next = Next->MatchingParen;
3698 if (Current.
is(tok::kw_operator)) {
3699 if (
Previous.Tok.getIdentifierInfo() &&
3700 !
Previous.isOneOf(tok::kw_return, tok::kw_co_return)) {
3705 assert(
Previous.MatchingParen->is(tok::l_paren));
3706 assert(
Previous.MatchingParen->is(TT_TypeDeclarationParen));
3711 Next = skipOperatorName(Next);
3715 for (; Next; Next = Next->Next) {
3716 if (Next->is(TT_TemplateOpener) && Next->MatchingParen) {
3717 Next = Next->MatchingParen;
3718 }
else if (Next->is(tok::coloncolon)) {
3722 if (Next->is(tok::kw_operator)) {
3723 Next = skipOperatorName(Next->Next);
3726 if (Next->isNot(tok::identifier))
3728 }
else if (isCppAttribute(IsCpp, *Next)) {
3729 Next = Next->MatchingParen;
3732 }
else if (Next->is(tok::l_paren)) {
3741 if (!Next || Next->isNot(tok::l_paren) || !Next->MatchingParen)
3743 ClosingParen = Next->MatchingParen;
3744 assert(ClosingParen->
is(tok::r_paren));
3746 if (
Line.Last->is(tok::l_brace))
3748 if (Next->Next == ClosingParen)
3751 if (ClosingParen->
Next && ClosingParen->
Next->
is(TT_PointerOrReference))
3764 if (IsCpp && Next->Next && Next->Next->is(tok::identifier) &&
3765 !
Line.endsWith(tok::semi)) {
3769 for (
const FormatToken *Tok = Next->Next; Tok && Tok != ClosingParen;
3771 if (Tok->is(TT_TypeDeclarationParen))
3773 if (Tok->isOneOf(tok::l_paren, TT_TemplateOpener) && Tok->MatchingParen) {
3774 Tok = Tok->MatchingParen;
3777 if (Tok->is(tok::kw_const) || Tok->isTypeName(IsCpp) ||
3778 Tok->isOneOf(TT_PointerOrReference, TT_StartOfName, tok::ellipsis)) {
3781 if (Tok->isOneOf(tok::l_brace, TT_ObjCMethodExpr) || Tok->Tok.isLiteral())
3787bool TokenAnnotator::mustBreakForReturnType(
const AnnotatedLine &
Line)
const {
3788 assert(
Line.MightBeFunctionDecl);
3796 switch (Style.BreakAfterReturnType) {
3806 return Line.mightBeFunctionDefinition();
3819 :
Line.FirstStartColumn +
First->ColumnWidth;
3821 bool InFunctionDecl =
Line.MightBeFunctionDecl;
3822 bool AlignArrayOfStructures =
3825 if (AlignArrayOfStructures)
3826 calculateArrayInitializerColumnList(
Line);
3828 bool SeenName =
false;
3829 bool LineIsFunctionDeclaration =
false;
3833 for (
auto *Tok = Current; Tok; Tok = Tok->Next) {
3834 if (Tok->is(TT_StartOfName))
3836 if (Tok->Previous->EndsCppAttributeGroup)
3837 AfterLastAttribute = Tok;
3838 if (
const bool IsCtorOrDtor = Tok->is(TT_CtorDtorDeclName);
3843 LineIsFunctionDeclaration =
true;
3849 if (IsCpp && (LineIsFunctionDeclaration ||
First->is(TT_CtorDtorDeclName)) &&
3850 Line.endsWith(tok::semi, tok::r_brace)) {
3851 auto *Tok =
Line.Last->Previous;
3852 while (Tok->isNot(tok::r_brace))
3853 Tok = Tok->Previous;
3854 if (
auto *LBrace = Tok->MatchingParen; LBrace) {
3855 assert(LBrace->is(tok::l_brace));
3858 LBrace->setFinalizedType(TT_FunctionLBrace);
3862 if (IsCpp && SeenName && AfterLastAttribute &&
3865 if (LineIsFunctionDeclaration)
3866 Line.ReturnTypeWrapped =
true;
3870 if (!LineIsFunctionDeclaration) {
3872 for (
const auto *Tok =
First; Tok; Tok = Tok->Next) {
3873 if (Tok->isNot(tok::kw_operator))
3877 }
while (Tok && Tok->isNot(TT_OverloadedOperatorLParen));
3878 if (!Tok || !Tok->MatchingParen)
3880 const auto *LeftParen = Tok;
3881 for (Tok = Tok->Next; Tok && Tok != LeftParen->MatchingParen;
3883 if (Tok->isNot(tok::identifier))
3885 auto *Next = Tok->Next;
3886 const bool NextIsBinaryOperator =
3887 Next && Next->isPointerOrReference() && Next->Next &&
3888 Next->Next->is(tok::identifier);
3889 if (!NextIsBinaryOperator)
3891 Next->setType(TT_BinaryOperator);
3895 }
else if (ClosingParen) {
3896 for (
auto *Tok = ClosingParen->
Next; Tok; Tok = Tok->
Next) {
3897 if (Tok->is(TT_CtorInitializerColon))
3899 if (Tok->is(tok::arrow)) {
3900 Tok->setType(TT_TrailingReturnArrow);
3903 if (Tok->isNot(TT_TrailingAnnotation))
3905 const auto *Next = Tok->Next;
3906 if (!Next || Next->isNot(tok::l_paren))
3908 Tok = Next->MatchingParen;
3917 if (Current->is(TT_LineComment)) {
3919 Current->SpacesRequiredBefore =
3920 (Style.Cpp11BracedListStyle && !Style.SpacesInParensOptions.Other)
3923 }
else if (Prev->
is(TT_VerilogMultiLineListLParen)) {
3924 Current->SpacesRequiredBefore = 0;
3926 Current->SpacesRequiredBefore = Style.SpacesBeforeTrailingComments;
3936 if (!Current->HasUnescapedNewline) {
3939 if (
Parameter->isOneOf(tok::comment, tok::r_brace))
3942 if (
Parameter->Previous->isNot(TT_CtorInitializerComma) &&
3950 }
else if (!Current->Finalized && Current->SpacesRequiredBefore == 0 &&
3951 spaceRequiredBefore(
Line, *Current)) {
3952 Current->SpacesRequiredBefore = 1;
3955 const auto &Children = Prev->
Children;
3956 if (!Children.empty() && Children.back()->Last->is(TT_LineComment)) {
3957 Current->MustBreakBefore =
true;
3959 Current->MustBreakBefore =
3960 Current->MustBreakBefore || mustBreakBefore(
Line, *Current);
3961 if (!Current->MustBreakBefore && InFunctionDecl &&
3962 Current->is(TT_FunctionDeclarationName)) {
3963 Current->MustBreakBefore = mustBreakForReturnType(
Line);
3967 Current->CanBreakBefore =
3968 Current->MustBreakBefore || canBreakBefore(
Line, *Current);
3969 unsigned ChildSize = 0;
3975 if (Current->MustBreakBefore || Prev->
Children.size() > 1 ||
3977 Prev->
Children[0]->First->MustBreakBefore) ||
3978 Current->IsMultiline) {
3979 Current->TotalLength = Prev->
TotalLength + Style.ColumnLimit;
3981 Current->TotalLength = Prev->
TotalLength + Current->ColumnWidth +
3982 ChildSize + Current->SpacesRequiredBefore;
3985 if (Current->is(TT_CtorInitializerColon))
3986 InFunctionDecl =
false;
3997 Current->SplitPenalty = splitPenalty(
Line, *Current, InFunctionDecl);
3999 Current->is(TT_SelectorName) && Current->ParameterIndex > 0) {
4000 if (Current->ParameterIndex == 1)
4001 Current->SplitPenalty += 5 * Current->BindingStrength;
4003 Current->SplitPenalty += 20 * Current->BindingStrength;
4006 Current = Current->Next;
4009 calculateUnbreakableTailLengths(
Line);
4010 unsigned IndentLevel =
Line.Level;
4011 for (Current =
First; Current; Current = Current->Next) {
4013 Current->Role->precomputeFormattingInfos(Current);
4014 if (Current->MatchingParen &&
4015 Current->MatchingParen->opensBlockOrBlockTypeList(Style) &&
4019 Current->IndentLevel = IndentLevel;
4020 if (Current->opensBlockOrBlockTypeList(Style))
4024 LLVM_DEBUG({ printDebugInfo(
Line); });
4027void TokenAnnotator::calculateUnbreakableTailLengths(
4029 unsigned UnbreakableTailLength = 0;
4032 Current->UnbreakableTailLength = UnbreakableTailLength;
4033 if (Current->CanBreakBefore ||
4034 Current->isOneOf(tok::comment, tok::string_literal)) {
4035 UnbreakableTailLength = 0;
4037 UnbreakableTailLength +=
4038 Current->ColumnWidth + Current->SpacesRequiredBefore;
4040 Current = Current->Previous;
4044void TokenAnnotator::calculateArrayInitializerColumnList(
4045 AnnotatedLine &
Line)
const {
4048 auto *CurrentToken =
Line.First;
4049 CurrentToken->ArrayInitializerLineStart =
true;
4051 while (CurrentToken && CurrentToken !=
Line.Last) {
4052 if (CurrentToken->is(tok::l_brace)) {
4053 CurrentToken->IsArrayInitializer =
true;
4054 if (CurrentToken->Next)
4055 CurrentToken->Next->MustBreakBefore =
true;
4057 calculateInitializerColumnList(
Line, CurrentToken->Next, Depth + 1);
4059 CurrentToken = CurrentToken->Next;
4064FormatToken *TokenAnnotator::calculateInitializerColumnList(
4065 AnnotatedLine &
Line, FormatToken *CurrentToken,
unsigned Depth)
const {
4066 while (CurrentToken && CurrentToken !=
Line.Last) {
4067 if (CurrentToken->is(tok::l_brace))
4069 else if (CurrentToken->is(tok::r_brace))
4071 if (Depth == 2 && CurrentToken->isOneOf(tok::l_brace, tok::comma)) {
4072 CurrentToken = CurrentToken->Next;
4075 CurrentToken->StartsColumn =
true;
4076 CurrentToken = CurrentToken->Previous;
4078 CurrentToken = CurrentToken->Next;
4080 return CurrentToken;
4083unsigned TokenAnnotator::splitPenalty(
const AnnotatedLine &
Line,
4084 const FormatToken &Tok,
4085 bool InFunctionDecl)
const {
4086 const FormatToken &Left = *Tok.Previous;
4087 const FormatToken &Right = Tok;
4089 if (Left.is(tok::semi))
4094 if (Right.isOneOf(Keywords.kw_extends, Keywords.kw_throws))
4096 if (Right.is(Keywords.kw_implements))
4098 if (Left.is(tok::comma) && Left.NestingLevel == 0)
4100 }
else if (Style.isJavaScript()) {
4101 if (Right.is(Keywords.kw_function) && Left.isNot(tok::comma))
4103 if (Left.is(TT_JsTypeColon))
4105 if ((Left.is(TT_TemplateString) && Left.TokenText.ends_with(
"${")) ||
4106 (Right.is(TT_TemplateString) && Right.TokenText.starts_with(
"}"))) {
4110 if (Left.opensScope() && Right.closesScope())
4113 if (Right.is(tok::l_square))
4115 if (Right.is(tok::period))
4119 if (Right.is(tok::identifier) && Right.Next && Right.Next->is(TT_DictLiteral))
4121 if (Right.is(tok::l_square)) {
4122 if (Left.is(tok::r_square))
4125 if (Right.is(TT_LambdaLSquare) && Left.is(tok::equal))
4127 if (!Right.isOneOf(TT_ObjCMethodExpr, TT_LambdaLSquare,
4128 TT_ArrayInitializerLSquare,
4129 TT_DesignatedInitializerLSquare, TT_AttributeSquare)) {
4134 if (Left.is(tok::coloncolon))
4135 return Style.PenaltyBreakScopeResolution;
4136 if (Right.isOneOf(TT_StartOfName, TT_FunctionDeclarationName) ||
4137 Right.is(tok::kw_operator)) {
4138 if (
Line.startsWith(tok::kw_for) && Right.PartOfMultiVariableDeclStmt)
4140 if (Left.is(TT_StartOfName))
4142 if (InFunctionDecl && Right.NestingLevel == 0)
4143 return Style.PenaltyReturnTypeOnItsOwnLine;
4146 if (Right.is(TT_PointerOrReference))
4148 if (Right.is(TT_TrailingReturnArrow))
4150 if (Left.is(tok::equal) && Right.is(tok::l_brace))
4152 if (Left.is(TT_CastRParen))
4154 if (Left.isOneOf(tok::kw_class, tok::kw_struct, tok::kw_union))
4156 if (Left.is(tok::comment))
4159 if (Left.isOneOf(TT_RangeBasedForLoopColon, TT_InheritanceColon,
4160 TT_CtorInitializerColon)) {
4164 if (Right.isMemberAccess()) {
4184 return !Right.NextOperator || !Right.NextOperator->Previous->closesScope()
4189 if (Right.is(TT_TrailingAnnotation) &&
4190 (!Right.Next || Right.Next->isNot(tok::l_paren))) {
4193 if (
Line.startsWith(TT_ObjCMethodSpecifier))
4200 bool is_short_annotation = Right.TokenText.size() < 10;
4201 return (Left.is(tok::r_paren) ? 100 : 120) + (is_short_annotation ? 50 : 0);
4205 if (
Line.startsWith(tok::kw_for) && Left.is(tok::equal))
4210 if (Right.is(TT_SelectorName))
4212 if (Left.is(tok::colon) && Left.is(TT_ObjCMethodExpr))
4213 return Line.MightBeFunctionDecl ? 50 : 500;
4219 Left.Previous->isOneOf(tok::identifier, tok::greater)) {
4223 if (Left.is(tok::l_paren) && Style.PenaltyBreakOpenParenthesis != 0)
4224 return Style.PenaltyBreakOpenParenthesis;
4225 if (Left.is(tok::l_paren) && InFunctionDecl &&
4229 if (Left.is(tok::l_paren) && Left.Previous &&
4230 (Left.Previous->isOneOf(tok::kw_for, tok::kw__Generic) ||
4231 Left.Previous->isIf())) {
4234 if (Left.is(tok::equal) && InFunctionDecl)
4236 if (Right.is(tok::r_brace))
4238 if (Left.is(TT_TemplateOpener))
4240 if (Left.opensScope()) {
4245 (Left.ParameterCount <= 1 || Style.AllowAllArgumentsOnNextLine)) {
4248 if (Left.is(tok::l_brace) && !Style.Cpp11BracedListStyle)
4250 return Left.ParameterCount > 1 ? Style.PenaltyBreakBeforeFirstCallParameter
4253 if (Left.is(TT_JavaAnnotation))
4256 if (Left.is(TT_UnaryOperator))
4258 if (Left.isOneOf(tok::plus, tok::comma) && Left.Previous &&
4259 Left.Previous->isLabelString() &&
4260 (Left.NextOperator || Left.OperatorIndex != 0)) {
4263 if (Right.is(tok::plus) && Left.isLabelString() &&
4264 (Right.NextOperator || Right.OperatorIndex != 0)) {
4267 if (Left.is(tok::comma))
4269 if (Right.is(tok::lessless) && Left.isLabelString() &&
4270 (Right.NextOperator || Right.OperatorIndex != 1)) {
4273 if (Right.is(tok::lessless)) {
4275 if (Left.isNot(tok::r_paren) || Right.OperatorIndex > 0) {
4281 if (Left.ClosesTemplateDeclaration)
4282 return Style.PenaltyBreakTemplateDeclaration;
4283 if (Left.ClosesRequiresClause)
4285 if (Left.is(TT_ConditionalExpr))
4289 Level = Right.getPrecedence();
4291 return Style.PenaltyBreakAssignment;
4298bool TokenAnnotator::spaceRequiredBeforeParens(
const FormatToken &Right)
const {
4301 if (Right.is(TT_OverloadedOperatorLParen) &&
4302 Style.SpaceBeforeParensOptions.AfterOverloadedOperator) {
4305 if (Style.SpaceBeforeParensOptions.BeforeNonEmptyParentheses &&
4306 Right.ParameterCount > 0) {
4312bool TokenAnnotator::spaceRequiredBetween(
const AnnotatedLine &
Line,
4313 const FormatToken &Left,
4314 const FormatToken &Right)
const {
4315 if (Left.is(tok::kw_return) &&
4316 !Right.isOneOf(tok::semi, tok::r_paren, tok::hashhash)) {
4319 if (Left.is(tok::kw_throw) && Right.is(tok::l_paren) && Right.MatchingParen &&
4320 Right.MatchingParen->is(TT_CastRParen)) {
4326 Left.Tok.getObjCKeywordID() == tok::objc_property) {
4329 if (Right.is(tok::hashhash))
4330 return Left.is(tok::hash);
4331 if (Left.isOneOf(tok::hashhash, tok::hash))
4332 return Right.is(tok::hash);
4333 if (Left.is(
BK_Block) && Right.is(tok::r_brace) &&
4334 Right.MatchingParen == &Left &&
Line.Children.empty()) {
4335 return Style.SpaceInEmptyBlock;
4337 if ((Left.is(tok::l_paren) && Right.is(tok::r_paren)) ||
4338 (Left.is(tok::l_brace) && Left.isNot(
BK_Block) &&
4339 Right.is(tok::r_brace) && Right.isNot(
BK_Block))) {
4340 return Style.SpacesInParensOptions.InEmptyParentheses;
4342 if (Style.SpacesInParensOptions.InConditionalStatements) {
4343 const FormatToken *LeftParen =
nullptr;
4344 if (Left.is(tok::l_paren))
4346 else if (Right.is(tok::r_paren) && Right.MatchingParen)
4347 LeftParen = Right.MatchingParen;
4349 if (LeftParen->is(TT_ConditionLParen))
4351 if (LeftParen->Previous && isKeywordWithCondition(*LeftParen->Previous))
4357 if (Left.is(tok::kw_auto) && Right.isOneOf(TT_LambdaLBrace, TT_FunctionLBrace,
4359 TT_FunctionTypeLParen)) {
4364 if (Left.is(tok::kw_auto) && Right.isOneOf(tok::l_paren, tok::l_brace))
4367 const auto *BeforeLeft = Left.Previous;
4370 if (Right.is(tok::l_paren) && Left.is(tok::kw_co_await) && BeforeLeft &&
4371 BeforeLeft->is(tok::kw_operator)) {
4375 if (Left.isOneOf(tok::kw_co_await, tok::kw_co_yield, tok::kw_co_return) &&
4376 !Right.isOneOf(tok::semi, tok::r_paren)) {
4380 if (Left.is(tok::l_paren) || Right.is(tok::r_paren)) {
4381 return (Right.is(TT_CastRParen) ||
4382 (Left.MatchingParen && Left.MatchingParen->is(TT_CastRParen)))
4383 ? Style.SpacesInParensOptions.InCStyleCasts
4384 : Style.SpacesInParensOptions.Other;
4386 if (Right.isOneOf(tok::semi, tok::comma))
4389 bool IsLightweightGeneric = Right.MatchingParen &&
4390 Right.MatchingParen->Next &&
4391 Right.MatchingParen->Next->is(tok::colon);
4392 return !IsLightweightGeneric && Style.ObjCSpaceBeforeProtocolList;
4394 if (Right.is(tok::less) && Left.is(tok::kw_template))
4395 return Style.SpaceAfterTemplateKeyword;
4396 if (Left.isOneOf(tok::exclaim, tok::tilde))
4398 if (Left.is(tok::at) &&
4399 Right.isOneOf(tok::identifier, tok::string_literal, tok::char_constant,
4400 tok::numeric_constant, tok::l_paren, tok::l_brace,
4401 tok::kw_true, tok::kw_false)) {
4404 if (Left.is(tok::colon))
4405 return Left.isNot(TT_ObjCMethodExpr);
4406 if (Left.is(tok::coloncolon)) {
4407 return Right.is(tok::star) && Right.is(TT_PointerOrReference) &&
4410 if (Left.is(tok::less) || Right.isOneOf(tok::greater, tok::less)) {
4413 (Left.is(TT_DictLiteral) || Right.is(TT_DictLiteral)))) {
4415 if (Left.is(tok::less) && Right.is(tok::greater))
4417 return !Style.Cpp11BracedListStyle;
4420 if (Right.isNot(TT_OverloadedOperatorLParen))
4423 if (Right.is(tok::ellipsis)) {
4424 return Left.Tok.isLiteral() || (Left.is(tok::identifier) && BeforeLeft &&
4425 BeforeLeft->is(tok::kw_case));
4427 if (Left.is(tok::l_square) && Right.is(tok::amp))
4428 return Style.SpacesInSquareBrackets;
4429 if (Right.is(TT_PointerOrReference)) {
4430 if (Left.is(tok::r_paren) &&
Line.MightBeFunctionDecl) {
4431 if (!Left.MatchingParen)
4433 FormatToken *TokenBeforeMatchingParen =
4434 Left.MatchingParen->getPreviousNonComment();
4435 if (!TokenBeforeMatchingParen || Left.isNot(TT_TypeDeclarationParen))
4443 (Left.is(TT_AttributeRParen) ||
4444 Left.canBePointerOrReferenceQualifier())) {
4447 if (Left.Tok.isLiteral())
4450 if (Left.isTypeOrIdentifier(IsCpp) && Right.Next && Right.Next->Next &&
4451 Right.Next->Next->is(TT_RangeBasedForLoopColon)) {
4452 return getTokenPointerOrReferenceAlignment(Right) !=
4455 return !Left.isOneOf(TT_PointerOrReference, tok::l_paren) &&
4456 (getTokenPointerOrReferenceAlignment(Right) !=
4458 (
Line.IsMultiVariableDeclStmt &&
4459 (Left.NestingLevel == 0 ||
4460 (Left.NestingLevel == 1 && startsWithInitStatement(
Line)))));
4462 if (Right.is(TT_FunctionTypeLParen) && Left.isNot(tok::l_paren) &&
4463 (Left.isNot(TT_PointerOrReference) ||
4465 !
Line.IsMultiVariableDeclStmt))) {
4468 if (Left.is(TT_PointerOrReference)) {
4473 Right.canBePointerOrReferenceQualifier()) {
4477 if (Right.Tok.isLiteral())
4480 if (Right.is(TT_BlockComment))
4484 if (Right.isOneOf(Keywords.kw_override, Keywords.kw_final, tok::kw_noexcept,
4485 TT_RequiresClause) &&
4486 Right.isNot(TT_StartOfName)) {
4490 if (Right.is(tok::l_brace) && Right.is(
BK_Block))
4493 if (BeforeLeft && BeforeLeft->isTypeOrIdentifier(IsCpp) && Right.Next &&
4494 Right.Next->is(TT_RangeBasedForLoopColon)) {
4495 return getTokenPointerOrReferenceAlignment(Left) !=
4498 if (Right.isOneOf(TT_PointerOrReference, TT_ArraySubscriptLSquare,
4510 if (
Line.IsMultiVariableDeclStmt &&
4511 (Left.NestingLevel ==
Line.First->NestingLevel ||
4512 ((Left.NestingLevel ==
Line.First->NestingLevel + 1) &&
4513 startsWithInitStatement(
Line)))) {
4518 if (BeforeLeft->is(tok::coloncolon)) {
4519 return Left.is(tok::star) &&
4522 return !BeforeLeft->isOneOf(tok::l_paren, tok::l_square);
4525 if (Left.is(tok::ellipsis) && BeforeLeft &&
4526 BeforeLeft->isPointerOrReference()) {
4530 if (Right.is(tok::star) && Left.is(tok::l_paren))
4532 if (Left.is(tok::star) && Right.isPointerOrReference())
4534 if (Right.isPointerOrReference()) {
4535 const FormatToken *
Previous = &Left;
4545 if (
Previous->is(tok::coloncolon)) {
4564 if (
Previous->endsSequence(tok::kw_operator))
4568 (Style.SpaceAroundPointerQualifiers ==
4574 if (Style.isCSharp() && Left.is(Keywords.kw_is) && Right.is(tok::l_square))
4576 const auto SpaceRequiredForArrayInitializerLSquare =
4577 [](
const FormatToken &LSquareTok,
const FormatStyle &Style) {
4578 return Style.SpacesInContainerLiterals ||
4579 (Style.isProto() && !Style.Cpp11BracedListStyle &&
4580 LSquareTok.endsSequence(tok::l_square, tok::colon,
4583 if (Left.is(tok::l_square)) {
4584 return (Left.is(TT_ArrayInitializerLSquare) && Right.isNot(tok::r_square) &&
4585 SpaceRequiredForArrayInitializerLSquare(Left, Style)) ||
4586 (Left.isOneOf(TT_ArraySubscriptLSquare, TT_StructuredBindingLSquare,
4587 TT_LambdaLSquare) &&
4588 Style.SpacesInSquareBrackets && Right.isNot(tok::r_square));
4590 if (Right.is(tok::r_square)) {
4591 return Right.MatchingParen &&
4592 ((Right.MatchingParen->is(TT_ArrayInitializerLSquare) &&
4593 SpaceRequiredForArrayInitializerLSquare(*Right.MatchingParen,
4595 (Style.SpacesInSquareBrackets &&
4596 Right.MatchingParen->isOneOf(TT_ArraySubscriptLSquare,
4597 TT_StructuredBindingLSquare,
4598 TT_LambdaLSquare)));
4600 if (Right.is(tok::l_square) &&
4601 !Right.isOneOf(TT_ObjCMethodExpr, TT_LambdaLSquare,
4602 TT_DesignatedInitializerLSquare,
4603 TT_StructuredBindingLSquare, TT_AttributeSquare) &&
4604 !Left.isOneOf(tok::numeric_constant, TT_DictLiteral) &&
4605 !(Left.isNot(tok::r_square) && Style.SpaceBeforeSquareBrackets &&
4606 Right.is(TT_ArraySubscriptLSquare))) {
4609 if (Left.is(tok::l_brace) && Right.is(tok::r_brace))
4610 return !Left.Children.empty();
4611 if ((Left.is(tok::l_brace) && Left.isNot(
BK_Block)) ||
4612 (Right.is(tok::r_brace) && Right.MatchingParen &&
4613 Right.MatchingParen->isNot(
BK_Block))) {
4614 return !Style.Cpp11BracedListStyle || Style.SpacesInParensOptions.Other;
4616 if (Left.is(TT_BlockComment)) {
4618 return Style.isJavaScript() || !Left.TokenText.ends_with(
"=*/");
4623 if (Left.is(TT_TemplateCloser) && Right.is(TT_AttributeSquare))
4626 if (Right.is(tok::l_paren)) {
4627 if (Left.is(TT_TemplateCloser) && Right.isNot(TT_FunctionTypeLParen))
4628 return spaceRequiredBeforeParens(Right);
4629 if (Left.isOneOf(TT_RequiresClause,
4630 TT_RequiresClauseInARequiresExpression)) {
4631 return Style.SpaceBeforeParensOptions.AfterRequiresInClause ||
4632 spaceRequiredBeforeParens(Right);
4634 if (Left.is(TT_RequiresExpression)) {
4635 return Style.SpaceBeforeParensOptions.AfterRequiresInExpression ||
4636 spaceRequiredBeforeParens(Right);
4638 if (Left.is(TT_AttributeRParen) ||
4639 (Left.is(tok::r_square) && Left.is(TT_AttributeSquare))) {
4642 if (Left.is(TT_ForEachMacro)) {
4643 return Style.SpaceBeforeParensOptions.AfterForeachMacros ||
4644 spaceRequiredBeforeParens(Right);
4646 if (Left.is(TT_IfMacro)) {
4647 return Style.SpaceBeforeParensOptions.AfterIfMacros ||
4648 spaceRequiredBeforeParens(Right);
4651 Left.isOneOf(tok::kw_new, tok::kw_delete) &&
4652 Right.isNot(TT_OverloadedOperatorLParen) &&
4653 !(
Line.MightBeFunctionDecl && Left.is(TT_FunctionDeclarationName))) {
4654 return Style.SpaceBeforeParensOptions.AfterPlacementOperator;
4658 if (Left.is(tok::semi))
4660 if (Left.isOneOf(tok::pp_elif, tok::kw_for, tok::kw_while, tok::kw_switch,
4661 tok::kw_case, TT_ForEachMacro, TT_ObjCForIn) ||
4663 Right.is(TT_ConditionLParen)) {
4664 return Style.SpaceBeforeParensOptions.AfterControlStatements ||
4665 spaceRequiredBeforeParens(Right);
4670 if (Right.is(TT_OverloadedOperatorLParen))
4671 return spaceRequiredBeforeParens(Right);
4673 if (
Line.MightBeFunctionDecl && (Left.is(TT_FunctionDeclarationName))) {
4674 if (
Line.mightBeFunctionDefinition()) {
4675 return Style.SpaceBeforeParensOptions.AfterFunctionDefinitionName ||
4676 spaceRequiredBeforeParens(Right);
4678 return Style.SpaceBeforeParensOptions.AfterFunctionDeclarationName ||
4679 spaceRequiredBeforeParens(Right);
4684 Left.MatchingParen && Left.MatchingParen->is(TT_LambdaLSquare)) {
4685 return Style.SpaceBeforeParensOptions.AfterFunctionDefinitionName ||
4686 spaceRequiredBeforeParens(Right);
4688 if (!BeforeLeft || !BeforeLeft->isOneOf(tok::period, tok::arrow)) {
4689 if (Left.isOneOf(tok::kw_try, Keywords.kw___except, tok::kw_catch)) {
4690 return Style.SpaceBeforeParensOptions.AfterControlStatements ||
4691 spaceRequiredBeforeParens(Right);
4693 if (Left.isOneOf(tok::kw_new, tok::kw_delete)) {
4694 return ((!
Line.MightBeFunctionDecl || !BeforeLeft) &&
4696 spaceRequiredBeforeParens(Right);
4699 if (Left.is(tok::r_square) && Left.MatchingParen &&
4700 Left.MatchingParen->Previous &&
4701 Left.MatchingParen->Previous->is(tok::kw_delete)) {
4703 spaceRequiredBeforeParens(Right);
4708 (Left.Tok.getIdentifierInfo() || Left.is(tok::r_paren))) {
4709 return spaceRequiredBeforeParens(Right);
4713 if (Left.is(tok::at) && Right.Tok.getObjCKeywordID() != tok::objc_not_keyword)
4715 if (Right.is(TT_UnaryOperator)) {
4716 return !Left.isOneOf(tok::l_paren, tok::l_square, tok::at) &&
4717 (Left.isNot(tok::colon) || Left.isNot(TT_ObjCMethodExpr));
4723 if (!Style.isVerilog() &&
4724 (Left.isOneOf(tok::identifier, tok::greater, tok::r_square,
4726 Left.isTypeName(IsCpp)) &&
4727 Right.is(tok::l_brace) && Right.getNextNonComment() &&
4731 if (Left.is(tok::period) || Right.is(tok::period))
4735 if (Right.is(tok::hash) && Left.is(tok::identifier) &&
4736 (Left.TokenText ==
"L" || Left.TokenText ==
"u" ||
4737 Left.TokenText ==
"U" || Left.TokenText ==
"u8" ||
4738 Left.TokenText ==
"LR" || Left.TokenText ==
"uR" ||
4739 Left.TokenText ==
"UR" || Left.TokenText ==
"u8R")) {
4742 if (Left.is(TT_TemplateCloser) && Left.MatchingParen &&
4743 Left.MatchingParen->Previous &&
4744 (Left.MatchingParen->Previous->is(tok::period) ||
4745 Left.MatchingParen->Previous->is(tok::coloncolon))) {
4751 if (Left.is(TT_TemplateCloser) && Right.is(tok::l_square))
4753 if (Left.is(tok::l_brace) && Left.endsSequence(TT_DictLiteral, tok::at)) {
4757 if (Right.is(tok::r_brace) && Right.MatchingParen &&
4758 Right.MatchingParen->endsSequence(TT_DictLiteral, tok::at)) {
4762 if (Right.is(TT_TrailingAnnotation) && Right.isOneOf(tok::amp, tok::ampamp) &&
4763 Left.isOneOf(tok::kw_const, tok::kw_volatile) &&
4764 (!Right.Next || Right.Next->is(tok::semi))) {
4774bool TokenAnnotator::spaceRequiredBefore(
const AnnotatedLine &
Line,
4775 const FormatToken &Right)
const {
4776 const FormatToken &Left = *Right.Previous;
4781 return Right.hasWhitespaceBefore();
4784 if (Keywords.isWordLike(Right) && Keywords.isWordLike(Left))
4789 if (Left.is(tok::star) && Right.is(tok::comment))
4793 if (Left.is(TT_OverloadedOperator) &&
4794 Right.isOneOf(TT_TemplateOpener, TT_TemplateCloser)) {
4798 if (Right.is(tok::period) && Left.is(tok::numeric_constant))
4802 if (Left.is(Keywords.kw_import) && Right.isOneOf(tok::less, tok::ellipsis))
4805 if (Left.isOneOf(Keywords.kw_module, Keywords.kw_import) &&
4806 Right.is(TT_ModulePartitionColon)) {
4810 if (Left.is(tok::identifier) && Right.is(TT_ModulePartitionColon))
4813 if (Left.is(TT_ModulePartitionColon) &&
4814 Right.isOneOf(tok::identifier, tok::kw_private)) {
4817 if (Left.is(tok::ellipsis) && Right.is(tok::identifier) &&
4818 Line.First->is(Keywords.kw_import)) {
4822 if (Left.isOneOf(TT_AttributeRParen, TT_AttributeMacro) &&
4823 Right.is(tok::coloncolon)) {
4827 if (Left.is(tok::kw_operator))
4828 return Right.is(tok::coloncolon);
4830 !Left.opensScope() && Style.SpaceBeforeCpp11BracedList) {
4833 if (Left.is(tok::less) && Left.is(TT_OverloadedOperator) &&
4834 Right.is(TT_TemplateOpener)) {
4837 if (Left.is(tok::identifier) && Right.is(tok::numeric_constant) &&
4838 Right.TokenText[0] ==
'.') {
4841 }
else if (Style.isProto()) {
4842 if (Right.is(tok::period) &&
4843 Left.isOneOf(Keywords.kw_optional, Keywords.kw_required,
4844 Keywords.kw_repeated, Keywords.kw_extend)) {
4847 if (Right.is(tok::l_paren) &&
4848 Left.isOneOf(Keywords.kw_returns, Keywords.kw_option)) {
4851 if (Right.isOneOf(tok::l_brace, tok::less) && Left.is(TT_SelectorName))
4854 if (Left.is(tok::slash) || Right.is(tok::slash))
4856 if (Left.MatchingParen &&
4857 Left.MatchingParen->is(TT_ProtoExtensionLSquare) &&
4858 Right.isOneOf(tok::l_brace, tok::less)) {
4859 return !Style.Cpp11BracedListStyle;
4862 if (Left.is(tok::percent))
4866 if (Left.is(tok::numeric_constant) && Right.is(tok::percent))
4867 return Right.hasWhitespaceBefore();
4868 }
else if (Style.isJson()) {
4869 if (Right.is(tok::colon) && Left.is(tok::string_literal))
4870 return Style.SpaceBeforeJsonColon;
4871 }
else if (Style.isCSharp()) {
4877 if (Left.is(tok::kw_this) && Right.is(tok::l_square))
4881 if (Left.is(tok::kw_new) && Right.is(tok::l_paren))
4885 if (Right.is(tok::l_brace))
4889 if (Left.is(tok::l_brace) && Right.isNot(tok::r_brace))
4892 if (Left.isNot(tok::l_brace) && Right.is(tok::r_brace))
4896 if (Left.is(TT_FatArrow) || Right.is(TT_FatArrow))
4900 if (Left.is(TT_AttributeColon) || Right.is(TT_AttributeColon))
4904 if (Left.is(TT_TemplateCloser) && Right.is(TT_StartOfName))
4908 if (Left.is(tok::l_square) || Right.is(tok::r_square))
4909 return Style.SpacesInSquareBrackets;
4912 if (Right.is(TT_CSharpNullable))
4916 if (Right.is(TT_NonNullAssertion))
4920 if (Left.is(tok::comma) && Right.is(tok::comma))
4924 if (Left.is(Keywords.kw_var) && Right.is(tok::l_paren))
4928 if (Right.is(tok::l_paren)) {
4929 if (Left.isOneOf(tok::kw_using, Keywords.kw_async, Keywords.kw_when,
4930 Keywords.kw_lock)) {
4931 return Style.SpaceBeforeParensOptions.AfterControlStatements ||
4932 spaceRequiredBeforeParens(Right);
4938 if (Left.isOneOf(tok::kw_public, tok::kw_private, tok::kw_protected,
4939 tok::kw_virtual, tok::kw_extern, tok::kw_static,
4940 Keywords.kw_internal, Keywords.kw_abstract,
4941 Keywords.kw_sealed, Keywords.kw_override,
4942 Keywords.kw_async, Keywords.kw_unsafe) &&
4943 Right.is(tok::l_paren)) {
4946 }
else if (Style.isJavaScript()) {
4947 if (Left.is(TT_FatArrow))
4950 if (Right.is(tok::l_paren) && Left.is(Keywords.kw_await) && Left.Previous &&
4951 Left.Previous->is(tok::kw_for)) {
4954 if (Left.is(Keywords.kw_async) && Right.is(tok::l_paren) &&
4955 Right.MatchingParen) {
4956 const FormatToken *Next = Right.MatchingParen->getNextNonComment();
4959 if (Next && Next->is(TT_FatArrow))
4962 if ((Left.is(TT_TemplateString) && Left.TokenText.ends_with(
"${")) ||
4963 (Right.is(TT_TemplateString) && Right.TokenText.starts_with(
"}"))) {
4968 if (Keywords.IsJavaScriptIdentifier(Left,
4970 Right.is(TT_TemplateString)) {
4973 if (Right.is(tok::star) &&
4974 Left.isOneOf(Keywords.kw_function, Keywords.kw_yield)) {
4977 if (Right.isOneOf(tok::l_brace, tok::l_square) &&
4978 Left.isOneOf(Keywords.kw_function, Keywords.kw_yield,
4979 Keywords.kw_extends, Keywords.kw_implements)) {
4982 if (Right.is(tok::l_paren)) {
4984 if (
Line.MustBeDeclaration && Left.Tok.getIdentifierInfo())
4988 if (Left.Previous && Left.Previous->is(tok::period) &&
4989 Left.Tok.getIdentifierInfo()) {
4993 if (Left.isOneOf(tok::kw_throw, Keywords.kw_await, Keywords.kw_typeof,
4999 if (Left.endsSequence(tok::kw_const, Keywords.kw_as))
5001 if ((Left.isOneOf(Keywords.kw_let, Keywords.kw_var, Keywords.kw_in,
5006 (Left.is(Keywords.kw_of) && Left.Previous &&
5007 (Left.Previous->is(tok::identifier) ||
5008 Left.Previous->isOneOf(tok::r_square, tok::r_brace)))) &&
5009 (!Left.Previous || Left.Previous->isNot(tok::period))) {
5012 if (Left.isOneOf(tok::kw_for, Keywords.kw_as) && Left.Previous &&
5013 Left.Previous->is(tok::period) && Right.is(tok::l_paren)) {
5016 if (Left.is(Keywords.kw_as) &&
5017 Right.isOneOf(tok::l_square, tok::l_brace, tok::l_paren)) {
5020 if (Left.is(tok::kw_default) && Left.Previous &&
5021 Left.Previous->is(tok::kw_export)) {
5024 if (Left.is(Keywords.kw_is) && Right.is(tok::l_brace))
5026 if (Right.isOneOf(TT_JsTypeColon, TT_JsTypeOptionalQuestion))
5028 if (Left.is(TT_JsTypeOperator) || Right.is(TT_JsTypeOperator))
5030 if ((Left.is(tok::l_brace) || Right.is(tok::r_brace)) &&
5031 Line.First->isOneOf(Keywords.kw_import, tok::kw_export)) {
5034 if (Left.is(tok::ellipsis))
5036 if (Left.is(TT_TemplateCloser) &&
5037 !Right.isOneOf(tok::equal, tok::l_brace, tok::comma, tok::l_square,
5038 Keywords.kw_implements, Keywords.kw_extends)) {
5044 if (Right.is(TT_NonNullAssertion))
5046 if (Left.is(TT_NonNullAssertion) &&
5047 Right.isOneOf(Keywords.kw_as, Keywords.kw_in)) {
5051 if (Left.is(tok::r_square) && Right.is(tok::l_brace))
5054 if (Left.is(tok::l_square) || Right.is(tok::r_square))
5055 return Style.SpacesInSquareBrackets;
5057 if (Left.is(Keywords.kw_synchronized) && Right.is(tok::l_paren)) {
5058 return Style.SpaceBeforeParensOptions.AfterControlStatements ||
5059 spaceRequiredBeforeParens(Right);
5061 if ((Left.isOneOf(tok::kw_static, tok::kw_public, tok::kw_private,
5062 tok::kw_protected) ||
5063 Left.isOneOf(Keywords.kw_final, Keywords.kw_abstract,
5064 Keywords.kw_native)) &&
5065 Right.is(TT_TemplateOpener)) {
5068 }
else if (Style.isVerilog()) {
5070 if (Style.isVerilog() && Left.is(tok::identifier) &&
5071 Left.TokenText[0] ==
'\\') {
5076 if ((Left.is(TT_VerilogTableItem) &&
5077 !Right.isOneOf(tok::r_paren, tok::semi)) ||
5078 (Right.is(TT_VerilogTableItem) && Left.isNot(tok::l_paren))) {
5079 const FormatToken *Next = Right.getNextNonComment();
5080 return !(Next && Next->is(tok::r_paren));
5083 if (Left.isNot(TT_BinaryOperator) &&
5084 Left.isOneOf(Keywords.kw_verilogHash, Keywords.kw_verilogHashHash)) {
5088 if (Right.isNot(tok::semi) &&
5089 (Left.endsSequence(tok::numeric_constant, Keywords.kw_verilogHash) ||
5090 Left.endsSequence(tok::numeric_constant,
5091 Keywords.kw_verilogHashHash) ||
5092 (Left.is(tok::r_paren) && Left.MatchingParen &&
5093 Left.MatchingParen->endsSequence(tok::l_paren, tok::at)))) {
5098 if (Left.is(Keywords.kw_apostrophe) ||
5099 (Left.is(TT_VerilogNumberBase) && Right.is(tok::numeric_constant))) {
5103 if (Left.is(tok::arrow) || Right.is(tok::arrow))
5108 if (Left.is(tok::at) && Right.isOneOf(tok::l_paren, tok::star, tok::at))
5111 if (Right.is(tok::l_square) &&
5112 Left.isOneOf(TT_VerilogDimensionedTypeName, Keywords.kw_function)) {
5116 if (Right.isOneOf(tok::period, Keywords.kw_apostrophe) &&
5117 Keywords.isVerilogIdentifier(Left) && Left.getPreviousNonComment() &&
5118 Left.getPreviousNonComment()->is(Keywords.kw_tagged)) {
5124 if ((Right.is(Keywords.kw_apostrophe) ||
5126 !(Left.isOneOf(Keywords.kw_assign, Keywords.kw_unique) ||
5127 Keywords.isVerilogWordOperator(Left)) &&
5128 (Left.isOneOf(tok::r_square, tok::r_paren, tok::r_brace,
5129 tok::numeric_constant) ||
5130 Keywords.isWordLike(Left))) {
5134 if ((Right.is(tok::star) && Left.is(tok::coloncolon)) ||
5135 (Left.is(tok::star) && Right.is(tok::semi))) {
5139 if (Left.endsSequence(tok::star, tok::l_paren) && Right.is(tok::identifier))
5142 if (Right.is(tok::l_paren) && Right.is(TT_VerilogStrength))
5145 if ((Left.is(tok::l_brace) &&
5146 Right.isOneOf(tok::lessless, tok::greatergreater)) ||
5147 (Left.endsSequence(tok::lessless, tok::l_brace) ||
5148 Left.endsSequence(tok::greatergreater, tok::l_brace))) {
5151 }
else if (Style.isTableGen()) {
5153 if (Left.is(tok::l_square) && Right.is(tok::l_brace))
5155 if (Left.is(tok::r_brace) && Right.is(tok::r_square))
5158 if (Right.isOneOf(TT_TableGenDAGArgListColon,
5159 TT_TableGenDAGArgListColonToAlign) ||
5160 Left.isOneOf(TT_TableGenDAGArgListColon,
5161 TT_TableGenDAGArgListColonToAlign)) {
5164 if (Right.is(TT_TableGenCondOperatorColon))
5166 if (Left.isOneOf(TT_TableGenDAGArgOperatorID,
5167 TT_TableGenDAGArgOperatorToBreak) &&
5168 Right.isNot(TT_TableGenDAGArgCloser)) {
5172 if (Right.isOneOf(tok::l_paren, tok::less) &&
5173 Left.isOneOf(TT_TableGenBangOperator, TT_TableGenCondOperator)) {
5178 if (Left.is(TT_TableGenTrailingPasteOperator) &&
5179 Right.isOneOf(tok::l_brace, tok::colon)) {
5183 if (Left.is(tok::hash) || Right.is(tok::hash))
5186 if (Keywords.isTableGenDefinition(Left))
5190 if (Left.is(TT_ImplicitStringLiteral))
5191 return Right.hasWhitespaceBefore();
5193 if (Left.is(TT_ObjCMethodSpecifier))
5195 if (Left.is(tok::r_paren) && Left.isNot(TT_AttributeRParen) &&
5196 canBeObjCSelectorComponent(Right)) {
5204 (Right.is(tok::equal) || Left.is(tok::equal))) {
5208 if (Right.is(TT_TrailingReturnArrow) || Left.is(TT_TrailingReturnArrow))
5211 if (Left.is(tok::comma) && Right.isNot(TT_OverloadedOperatorLParen) &&
5214 (Left.Children.empty() || !Left.MacroParent)) {
5217 if (Right.is(tok::comma))
5219 if (Right.is(TT_ObjCBlockLParen))
5221 if (Right.is(TT_CtorInitializerColon))
5222 return Style.SpaceBeforeCtorInitializerColon;
5223 if (Right.is(TT_InheritanceColon) && !Style.SpaceBeforeInheritanceColon)
5225 if (Right.is(TT_RangeBasedForLoopColon) &&
5226 !Style.SpaceBeforeRangeBasedForLoopColon) {
5229 if (Left.is(TT_BitFieldColon)) {
5233 if (Right.is(tok::colon)) {
5234 if (Right.is(TT_CaseLabelColon))
5235 return Style.SpaceBeforeCaseColon;
5236 if (Right.is(TT_GotoLabelColon))
5239 if (!Right.getNextNonComment())
5241 if (Right.is(TT_ObjCMethodExpr))
5243 if (Left.is(tok::question))
5245 if (Right.is(TT_InlineASMColon) && Left.is(tok::coloncolon))
5247 if (Right.is(TT_DictLiteral))
5248 return Style.SpacesInContainerLiterals;
5249 if (Right.is(TT_AttributeColon))
5251 if (Right.is(TT_CSharpNamedArgumentColon))
5253 if (Right.is(TT_GenericSelectionColon))
5255 if (Right.is(TT_BitFieldColon)) {
5262 if ((Left.isOneOf(tok::minus, tok::minusminus) &&
5263 Right.isOneOf(tok::minus, tok::minusminus)) ||
5264 (Left.isOneOf(tok::plus, tok::plusplus) &&
5265 Right.isOneOf(tok::plus, tok::plusplus))) {
5268 if (Left.is(TT_UnaryOperator)) {
5269 if (Right.isNot(tok::l_paren)) {
5273 if (Left.is(tok::exclaim) && Left.TokenText ==
"not")
5275 if (Left.is(tok::tilde) && Left.TokenText ==
"compl")
5279 if (Left.is(tok::amp) && Right.is(tok::r_square))
5280 return Style.SpacesInSquareBrackets;
5282 return (Style.SpaceAfterLogicalNot && Left.is(tok::exclaim)) ||
5283 Right.is(TT_BinaryOperator);
5288 if (Left.is(TT_CastRParen)) {
5289 return Style.SpaceAfterCStyleCast ||
5290 Right.isOneOf(TT_BinaryOperator, TT_SelectorName);
5293 auto ShouldAddSpacesInAngles = [
this, &Right]() {
5297 return Right.hasWhitespaceBefore();
5301 if (Left.is(tok::greater) && Right.is(tok::greater)) {
5304 return !Style.Cpp11BracedListStyle;
5306 return Right.is(TT_TemplateCloser) && Left.is(TT_TemplateCloser) &&
5308 ShouldAddSpacesInAngles());
5310 if (Right.isOneOf(tok::arrow, tok::arrowstar, tok::periodstar) ||
5311 Left.isOneOf(tok::arrow, tok::period, tok::arrowstar, tok::periodstar) ||
5312 (Right.is(tok::period) && Right.isNot(TT_DesignatedInitializerPeriod))) {
5315 if (!Style.SpaceBeforeAssignmentOperators && Left.isNot(TT_TemplateCloser) &&
5320 (Left.is(tok::identifier) || Left.is(tok::kw_this))) {
5323 if (Right.is(tok::coloncolon) && Left.is(tok::identifier)) {
5327 return Right.hasWhitespaceBefore();
5329 if (Right.is(tok::coloncolon) &&
5330 !Left.isOneOf(tok::l_brace, tok::comment, tok::l_paren)) {
5332 return (Left.is(TT_TemplateOpener) &&
5334 ShouldAddSpacesInAngles())) ||
5335 !(Left.isOneOf(tok::l_paren, tok::r_paren, tok::l_square,
5336 tok::kw___super, TT_TemplateOpener,
5337 TT_TemplateCloser)) ||
5338 (Left.is(tok::l_paren) && Style.SpacesInParensOptions.Other);
5340 if ((Left.is(TT_TemplateOpener)) != (Right.is(TT_TemplateCloser)))
5341 return ShouldAddSpacesInAngles();
5343 if (Right.is(TT_StructuredBindingLSquare)) {
5344 return !Left.isOneOf(tok::amp, tok::ampamp) ||
5348 if (Right.Next && Right.Next->is(TT_StructuredBindingLSquare) &&
5349 Right.isOneOf(tok::amp, tok::ampamp)) {
5352 if ((Right.is(TT_BinaryOperator) && Left.isNot(tok::l_paren)) ||
5353 (Left.isOneOf(TT_BinaryOperator, TT_ConditionalExpr) &&
5354 Right.isNot(tok::r_paren))) {
5357 if (Right.is(TT_TemplateOpener) && Left.is(tok::r_paren) &&
5358 Left.MatchingParen &&
5359 Left.MatchingParen->is(TT_OverloadedOperatorLParen)) {
5362 if (Right.is(tok::less) && Left.isNot(tok::l_paren) &&
5366 if (Right.is(TT_TrailingUnaryOperator))
5368 if (Left.is(TT_RegexLiteral))
5370 return spaceRequiredBetween(
Line, Left, Right);
5376 !Tok.
isOneOf(TT_ObjCBlockLBrace, TT_LambdaLBrace, TT_DictLiteral);
5393 !Tok.
isOneOf(TT_ObjCBlockLBrace, TT_DictLiteral);
5396bool TokenAnnotator::mustBreakBefore(
const AnnotatedLine &
Line,
5397 const FormatToken &Right)
const {
5398 const FormatToken &Left = *Right.Previous;
5399 if (Right.NewlinesBefore > 1 && Style.MaxEmptyLinesToKeep > 0)
5402 if (Style.BreakFunctionDefinitionParameters &&
Line.MightBeFunctionDecl &&
5403 Line.mightBeFunctionDefinition() && Left.MightBeFunctionDeclParen &&
5404 Left.ParameterCount > 0) {
5408 if (Style.isCSharp()) {
5409 if (Left.is(TT_FatArrow) && Right.is(tok::l_brace) &&
5410 Style.BraceWrapping.AfterFunction) {
5413 if (Right.is(TT_CSharpNamedArgumentColon) ||
5414 Left.is(TT_CSharpNamedArgumentColon)) {
5417 if (Right.is(TT_CSharpGenericTypeConstraint))
5419 if (Right.Next && Right.Next->is(TT_FatArrow) &&
5420 (Right.is(tok::numeric_constant) ||
5421 (Right.is(tok::identifier) && Right.TokenText ==
"_"))) {
5426 if (Left.is(TT_AttributeSquare) && Left.is(tok::r_square) &&
5427 (Right.isAccessSpecifier(
false) ||
5428 Right.is(Keywords.kw_internal))) {
5432 if (Left.is(TT_AttributeSquare) && Right.is(TT_AttributeSquare) &&
5433 Left.is(tok::r_square) && Right.is(tok::l_square)) {
5437 }
else if (Style.isJavaScript()) {
5439 if (Right.is(tok::string_literal) && Left.is(tok::plus) && Left.Previous &&
5440 Left.Previous->is(tok::string_literal)) {
5443 if (Left.is(TT_DictLiteral) && Left.is(tok::l_brace) &&
Line.Level == 0 &&
5444 Left.Previous && Left.Previous->is(tok::equal) &&
5445 Line.First->isOneOf(tok::identifier, Keywords.kw_import, tok::kw_export,
5449 !
Line.First->isOneOf(Keywords.kw_var, Keywords.kw_let)) {
5454 if (Left.is(tok::l_brace) &&
Line.Level == 0 &&
5455 (
Line.startsWith(tok::kw_enum) ||
5456 Line.startsWith(tok::kw_const, tok::kw_enum) ||
5457 Line.startsWith(tok::kw_export, tok::kw_enum) ||
5458 Line.startsWith(tok::kw_export, tok::kw_const, tok::kw_enum))) {
5463 if (Right.is(tok::r_brace) && Left.is(tok::l_brace) && Left.Previous &&
5464 Left.Previous->is(TT_FatArrow)) {
5466 switch (Style.AllowShortLambdasOnASingleLine) {
5472 return !Left.Children.empty();
5476 return (Left.NestingLevel == 0 &&
Line.Level == 0) &&
5477 !Left.Children.empty();
5479 llvm_unreachable(
"Unknown FormatStyle::ShortLambdaStyle enum");
5482 if (Right.is(tok::r_brace) && Left.is(tok::l_brace) &&
5483 !Left.Children.empty()) {
5487 (Left.NestingLevel == 0 &&
Line.Level == 0 &&
5488 Style.AllowShortFunctionsOnASingleLine &
5492 if (Right.is(tok::plus) && Left.is(tok::string_literal) && Right.Next &&
5493 Right.Next->is(tok::string_literal)) {
5496 }
else if (Style.isVerilog()) {
5498 if (Left.is(TT_VerilogAssignComma))
5501 if (Left.is(TT_VerilogTypeComma))
5505 if (Style.VerilogBreakBetweenInstancePorts &&
5506 (Left.is(TT_VerilogInstancePortComma) ||
5507 (Left.is(tok::r_paren) && Keywords.isVerilogIdentifier(Right) &&
5508 Left.MatchingParen &&
5509 Left.MatchingParen->is(TT_VerilogInstancePortLParen)))) {
5514 if (!Keywords.isVerilogBegin(Right) && Keywords.isVerilogEndOfLabel(Left))
5516 }
else if (Style.BreakAdjacentStringLiterals &&
5517 (IsCpp || Style.isProto() ||
5519 if (Left.isStringLiteral() && Right.isStringLiteral())
5524 if (Style.isJson()) {
5528 if (Left.is(TT_DictLiteral) && Left.is(tok::l_brace))
5531 if ((Left.is(TT_ArrayInitializerLSquare) && Left.is(tok::l_square) &&
5532 Right.isNot(tok::r_square)) ||
5533 Left.is(tok::comma)) {
5534 if (Right.is(tok::l_brace))
5538 for (
const auto *Tok = &Right; Tok; Tok = Tok->Next) {
5539 if (Tok->isOneOf(tok::l_brace, tok::l_square))
5541 if (Tok->isOneOf(tok::r_brace, tok::r_square))
5544 return Style.BreakArrays;
5547 if (Style.isTableGen()) {
5551 if (Left.is(TT_TableGenCondOperatorComma))
5553 if (Left.is(TT_TableGenDAGArgOperatorToBreak) &&
5554 Right.isNot(TT_TableGenDAGArgCloser)) {
5557 if (Left.is(TT_TableGenDAGArgListCommaToBreak))
5559 if (Right.is(TT_TableGenDAGArgCloser) && Right.MatchingParen &&
5560 Right.MatchingParen->is(TT_TableGenDAGArgOpenerToBreak) &&
5561 &Left != Right.MatchingParen->Next) {
5567 if (
Line.startsWith(tok::kw_asm) && Right.is(TT_InlineASMColon) &&
5577 const FormatToken *BeforeClosingBrace =
nullptr;
5578 if ((Left.isOneOf(tok::l_brace, TT_ArrayInitializerLSquare) ||
5579 (Style.isJavaScript() && Left.is(tok::l_paren))) &&
5580 Left.isNot(
BK_Block) && Left.MatchingParen) {
5581 BeforeClosingBrace = Left.MatchingParen->Previous;
5582 }
else if (Right.MatchingParen &&
5583 (Right.MatchingParen->isOneOf(tok::l_brace,
5584 TT_ArrayInitializerLSquare) ||
5585 (Style.isJavaScript() &&
5586 Right.MatchingParen->is(tok::l_paren)))) {
5587 BeforeClosingBrace = &Left;
5589 if (BeforeClosingBrace && (BeforeClosingBrace->is(tok::comma) ||
5590 BeforeClosingBrace->isTrailingComment())) {
5595 if (Right.is(tok::comment)) {
5596 return Left.isNot(
BK_BracedInit) && Left.isNot(TT_CtorInitializerColon) &&
5597 (Right.NewlinesBefore > 0 && Right.HasUnescapedNewline);
5599 if (Left.isTrailingComment())
5601 if (Left.IsUnterminatedLiteral)
5603 if (Right.is(tok::lessless) && Right.Next && Left.is(tok::string_literal) &&
5604 Right.Next->is(tok::string_literal)) {
5607 if (Right.is(TT_RequiresClause)) {
5608 switch (Style.RequiresClausePosition) {
5617 if (Left.ClosesTemplateDeclaration && Left.MatchingParen &&
5618 Left.MatchingParen->NestingLevel == 0) {
5622 if (Right.is(tok::kw_concept))
5626 Right.NewlinesBefore > 0);
5628 if (Left.ClosesRequiresClause && Right.isNot(tok::semi)) {
5629 switch (Style.RequiresClausePosition) {
5639 (Left.is(TT_CtorInitializerComma) ||
5640 Right.is(TT_CtorInitializerColon))) {
5645 Left.isOneOf(TT_CtorInitializerColon, TT_CtorInitializerComma)) {
5651 Right.isOneOf(TT_CtorInitializerComma, TT_CtorInitializerColon)) {
5657 Right.is(TT_CtorInitializerColon)) {
5662 Left.is(TT_CtorInitializerColon)) {
5668 Right.is(TT_InheritanceComma)) {
5672 Left.is(TT_InheritanceComma)) {
5675 if (Right.is(tok::string_literal) && Right.TokenText.starts_with(
"R\"")) {
5679 return Right.IsMultiline && Right.NewlinesBefore > 0;
5681 if ((Left.is(tok::l_brace) || (Left.is(tok::less) && Left.Previous &&
5682 Left.Previous->is(tok::equal))) &&
5688 if (Right.is(TT_InlineASMBrace))
5689 return Right.HasUnescapedNewline;
5692 auto *FirstNonComment =
Line.getFirstNonComment();
5695 FirstNonComment->isOneOf(Keywords.kw_internal, tok::kw_public,
5696 tok::kw_private, tok::kw_protected);
5698 if (Style.BraceWrapping.AfterEnum) {
5699 if (
Line.startsWith(tok::kw_enum) ||
5700 Line.startsWith(tok::kw_typedef, tok::kw_enum)) {
5705 FirstNonComment->Next->is(tok::kw_enum)) {
5711 if (Style.BraceWrapping.AfterClass &&
5713 FirstNonComment->Next->is(Keywords.kw_interface)) ||
5714 Line.startsWith(Keywords.kw_interface))) {
5719 if (Right.isNot(TT_FunctionLBrace)) {
5720 return (
Line.startsWith(tok::kw_class) &&
5721 Style.BraceWrapping.AfterClass) ||
5722 (
Line.startsWith(tok::kw_struct) &&
5723 Style.BraceWrapping.AfterStruct);
5727 if (Left.is(TT_ObjCBlockLBrace) &&
5733 if (Left.isOneOf(TT_AttributeRParen, TT_AttributeMacro) &&
5734 Right.is(TT_ObjCDecl)) {
5738 if (Left.is(TT_LambdaLBrace)) {
5746 (!Left.Children.empty() &&
5752 if (Style.BraceWrapping.BeforeLambdaBody && Right.is(TT_LambdaLBrace) &&
5753 (Left.isPointerOrReference() || Left.is(TT_TemplateCloser))) {
5759 Left.is(TT_LeadingJavaAnnotation) &&
5760 Right.isNot(TT_LeadingJavaAnnotation) && Right.isNot(tok::l_paren) &&
5761 (
Line.Last->is(tok::l_brace) || Style.BreakAfterJavaFieldAnnotations)) {
5765 if (Right.is(TT_ProtoExtensionLSquare))
5795 if (Style.isProto() && Right.is(TT_SelectorName) &&
5796 Right.isNot(tok::r_square) && Right.Next) {
5799 if (Left.is(tok::at))
5805 FormatToken *LBrace = Right.Next;
5806 if (LBrace && LBrace->is(tok::colon)) {
5807 LBrace = LBrace->Next;
5808 if (LBrace && LBrace->is(tok::at)) {
5809 LBrace = LBrace->Next;
5811 LBrace = LBrace->Next;
5823 ((LBrace->is(tok::l_brace) &&
5824 (LBrace->is(TT_DictLiteral) ||
5825 (LBrace->Next && LBrace->Next->is(tok::r_brace)))) ||
5826 LBrace->is(TT_ArrayInitializerLSquare) || LBrace->is(tok::less))) {
5833 if (Left.ParameterCount == 0)
5848 if (Left.isOneOf(tok::r_brace, tok::greater, tok::r_square))
5855bool TokenAnnotator::canBreakBefore(
const AnnotatedLine &
Line,
5856 const FormatToken &Right)
const {
5857 const FormatToken &Left = *Right.Previous;
5859 if (Style.isCSharp()) {
5860 if (Left.isOneOf(TT_CSharpNamedArgumentColon, TT_AttributeColon) ||
5861 Right.isOneOf(TT_CSharpNamedArgumentColon, TT_AttributeColon)) {
5865 if (
Line.First->is(TT_CSharpGenericTypeConstraint))
5866 return Left.is(TT_CSharpGenericTypeConstraintComma);
5868 if (Right.is(TT_CSharpNullable))
5871 if (Left.isOneOf(Keywords.kw_throws, Keywords.kw_extends,
5872 Keywords.kw_implements)) {
5875 if (Right.isOneOf(Keywords.kw_throws, Keywords.kw_extends,
5876 Keywords.kw_implements)) {
5879 }
else if (Style.isJavaScript()) {
5880 const FormatToken *NonComment = Right.getPreviousNonComment();
5882 NonComment->isOneOf(
5883 tok::kw_return, Keywords.kw_yield, tok::kw_continue, tok::kw_break,
5884 tok::kw_throw, Keywords.kw_interface, Keywords.kw_type,
5885 tok::kw_static, tok::kw_public, tok::kw_private, tok::kw_protected,
5886 Keywords.kw_readonly, Keywords.kw_override, Keywords.kw_abstract,
5887 Keywords.kw_get, Keywords.kw_set, Keywords.kw_async,
5888 Keywords.kw_await)) {
5891 if (Right.NestingLevel == 0 &&
5892 (Left.Tok.getIdentifierInfo() ||
5893 Left.isOneOf(tok::r_square, tok::r_paren)) &&
5894 Right.isOneOf(tok::l_square, tok::l_paren)) {
5897 if (NonComment && NonComment->is(tok::identifier) &&
5898 NonComment->TokenText ==
"asserts") {
5901 if (Left.is(TT_FatArrow) && Right.is(tok::l_brace))
5903 if (Left.is(TT_JsTypeColon))
5906 if (Left.is(tok::exclaim) && Right.is(tok::colon))
5911 if (Right.is(Keywords.kw_is)) {
5912 const FormatToken *Next = Right.getNextNonComment();
5920 if (!Next || Next->isNot(tok::colon))
5923 if (Left.is(Keywords.kw_in))
5925 if (Right.is(Keywords.kw_in))
5927 if (Right.is(Keywords.kw_as))
5929 if (Right.isOneOf(Keywords.kw_extends, Keywords.kw_infer)) {
5935 if (Left.is(Keywords.kw_as))
5937 if (Left.is(TT_NonNullAssertion))
5939 if (Left.is(Keywords.kw_declare) &&
5940 Right.isOneOf(Keywords.kw_module, tok::kw_namespace,
5941 Keywords.kw_function, tok::kw_class, tok::kw_enum,
5942 Keywords.kw_interface, Keywords.kw_type, Keywords.kw_var,
5943 Keywords.kw_let, tok::kw_const)) {
5948 if (Left.isOneOf(Keywords.kw_module, tok::kw_namespace) &&
5949 Right.isOneOf(tok::identifier, tok::string_literal)) {
5952 if (Right.is(TT_TemplateString) && Right.closesScope())
5956 if (Left.is(tok::identifier) && Right.is(TT_TemplateString))
5958 if (Left.is(TT_TemplateString) && Left.opensScope())
5960 }
else if (Style.isTableGen()) {
5962 if (Keywords.isTableGenDefinition(Left))
5965 if (Right.is(tok::l_paren)) {
5966 return !Left.isOneOf(TT_TableGenBangOperator, TT_TableGenCondOperator,
5970 if (Left.is(TT_TableGenValueSuffix))
5973 if (Left.is(tok::hash) || Right.is(tok::hash))
5975 if (Left.isOneOf(TT_TableGenBangOperator, TT_TableGenCondOperator))
5979 if (Left.is(tok::at))
5981 if (Left.Tok.getObjCKeywordID() == tok::objc_interface)
5983 if (Left.isOneOf(TT_JavaAnnotation, TT_LeadingJavaAnnotation))
5984 return Right.isNot(tok::l_paren);
5985 if (Right.is(TT_PointerOrReference)) {
5986 return Line.IsMultiVariableDeclStmt ||
5987 (getTokenPointerOrReferenceAlignment(Right) ==
5989 (!Right.Next || Right.Next->isNot(TT_FunctionDeclarationName)));
5991 if (Right.isOneOf(TT_StartOfName, TT_FunctionDeclarationName) ||
5992 Right.is(tok::kw_operator)) {
5995 if (Left.is(TT_PointerOrReference))
5997 if (Right.isTrailingComment()) {
6004 (Left.is(TT_CtorInitializerColon) && Right.NewlinesBefore > 0 &&
6007 if (Left.is(tok::question) && Right.is(tok::colon))
6009 if (Right.is(TT_ConditionalExpr) || Right.is(tok::question))
6010 return Style.BreakBeforeTernaryOperators;
6011 if (Left.is(TT_ConditionalExpr) || Left.is(tok::question))
6012 return !Style.BreakBeforeTernaryOperators;
6013 if (Left.is(TT_InheritanceColon))
6015 if (Right.is(TT_InheritanceColon))
6017 if (Right.is(TT_ObjCMethodExpr) && Right.isNot(tok::r_square) &&
6018 Left.isNot(TT_SelectorName)) {
6022 if (Right.is(tok::colon) &&
6023 !Right.isOneOf(TT_CtorInitializerColon, TT_InlineASMColon)) {
6026 if (Left.is(tok::colon) && Left.isOneOf(TT_DictLiteral, TT_ObjCMethodExpr)) {
6027 if (Style.isProto()) {
6028 if (!Style.AlwaysBreakBeforeMultilineStrings && Right.isStringLiteral())
6054 if (((Right.is(tok::l_brace) || Right.is(tok::less)) &&
6055 Right.is(TT_DictLiteral)) ||
6056 Right.is(TT_ArrayInitializerLSquare)) {
6062 if (Right.is(tok::r_square) && Right.MatchingParen &&
6063 Right.MatchingParen->is(TT_ProtoExtensionLSquare)) {
6066 if (Right.is(TT_SelectorName) || (Right.is(tok::identifier) && Right.Next &&
6067 Right.Next->is(TT_ObjCMethodExpr))) {
6068 return Left.isNot(tok::period);
6072 if (Right.is(tok::kw_concept))
6074 if (Right.is(TT_RequiresClause))
6076 if (Left.ClosesTemplateDeclaration) {
6078 Right.NewlinesBefore > 0;
6080 if (Left.is(TT_FunctionAnnotationRParen))
6082 if (Left.ClosesRequiresClause)
6084 if (Right.isOneOf(TT_RangeBasedForLoopColon, TT_OverloadedOperatorLParen,
6085 TT_OverloadedOperator)) {
6088 if (Left.is(TT_RangeBasedForLoopColon))
6090 if (Right.is(TT_RangeBasedForLoopColon))
6092 if (Left.is(TT_TemplateCloser) && Right.is(TT_TemplateOpener))
6094 if ((Left.is(tok::greater) && Right.is(tok::greater)) ||
6095 (Left.is(tok::less) && Right.is(tok::less))) {
6098 if (Right.is(TT_BinaryOperator) &&
6104 if (Left.isOneOf(TT_TemplateCloser, TT_UnaryOperator) ||
6105 Left.is(tok::kw_operator)) {
6108 if (Left.is(tok::equal) && !Right.isOneOf(tok::kw_default, tok::kw_delete) &&
6112 if (Left.is(tok::equal) && Right.is(tok::l_brace) &&
6113 !Style.Cpp11BracedListStyle) {
6116 if (Left.is(TT_AttributeLParen) ||
6117 (Left.is(tok::l_paren) && Left.is(TT_TypeDeclarationParen))) {
6120 if (Left.is(tok::l_paren) && Left.Previous &&
6121 (Left.Previous->isOneOf(TT_BinaryOperator, TT_CastRParen))) {
6124 if (Right.is(TT_ImplicitStringLiteral))
6127 if (Right.is(TT_TemplateCloser))
6129 if (Right.is(tok::r_square) && Right.MatchingParen &&
6130 Right.MatchingParen->is(TT_LambdaLSquare)) {
6136 if (Right.is(tok::r_brace)) {
6137 return Right.MatchingParen && (Right.MatchingParen->is(
BK_Block) ||
6138 (Right.isBlockIndentedInitRBrace(Style)));
6142 if (Right.is(tok::r_paren)) {
6144 !Right.MatchingParen) {
6147 auto Next = Right.Next;
6148 if (Next && Next->is(tok::r_paren))
6150 if (Next && Next->is(tok::l_paren))
6152 const FormatToken *
Previous = Right.MatchingParen->Previous;
6158 if (Left.is(TT_TrailingAnnotation)) {
6159 return !Right.isOneOf(tok::l_brace, tok::semi, tok::equal, tok::l_paren,
6160 tok::less, tok::coloncolon);
6163 if (Right.isAttribute())
6166 if (Right.is(tok::l_square) && Right.is(TT_AttributeSquare))
6167 return Left.isNot(TT_AttributeSquare);
6169 if (Left.is(tok::identifier) && Right.is(tok::string_literal))
6172 if (Right.is(tok::identifier) && Right.Next && Right.Next->is(TT_DictLiteral))
6175 if (Left.is(TT_CtorInitializerColon)) {
6177 (!Right.isTrailingComment() || Right.NewlinesBefore > 0);
6179 if (Right.is(TT_CtorInitializerColon))
6181 if (Left.is(TT_CtorInitializerComma) &&
6185 if (Right.is(TT_CtorInitializerComma) &&
6189 if (Left.is(TT_InheritanceComma) &&
6193 if (Right.is(TT_InheritanceComma) &&
6197 if (Left.is(TT_ArrayInitializerLSquare))
6199 if (Right.is(tok::kw_typename) && Left.isNot(tok::kw_const))
6201 if ((Left.isBinaryOperator() || Left.is(TT_BinaryOperator)) &&
6202 !Left.isOneOf(tok::arrowstar, tok::lessless) &&
6208 if ((Left.is(TT_AttributeSquare) && Right.is(tok::l_square)) ||
6209 (Left.is(tok::r_square) && Right.is(TT_AttributeSquare))) {
6213 auto ShortLambdaOption = Style.AllowShortLambdasOnASingleLine;
6214 if (Style.BraceWrapping.BeforeLambdaBody && Right.is(TT_LambdaLBrace)) {
6221 if (Right.is(tok::kw_noexcept) && Right.is(TT_TrailingAnnotation)) {
6222 switch (Style.AllowBreakBeforeNoexceptSpecifier) {
6228 return Right.Next && Right.Next->is(tok::l_paren);
6232 return Left.isOneOf(tok::comma, tok::coloncolon, tok::semi, tok::l_brace,
6233 tok::kw_class, tok::kw_struct, tok::comment) ||
6234 Right.isMemberAccess() ||
6235 Right.isOneOf(TT_TrailingReturnArrow, tok::lessless, tok::colon,
6236 tok::l_square, tok::at) ||
6237 (Left.is(tok::r_paren) &&
6238 Right.isOneOf(tok::identifier, tok::kw_const)) ||
6239 (Left.is(tok::l_paren) && Right.isNot(tok::r_paren)) ||
6240 (Left.is(TT_TemplateOpener) && Right.isNot(TT_TemplateCloser));
6243void TokenAnnotator::printDebugInfo(
const AnnotatedLine &
Line)
const {
6244 llvm::errs() <<
"AnnotatedTokens(L=" <<
Line.Level <<
", P=" <<
Line.PPLevel
6245 <<
", T=" <<
Line.Type <<
", C=" <<
Line.IsContinuation
6247 const FormatToken *Tok =
Line.First;
6249 llvm::errs() <<
" M=" << Tok->MustBreakBefore
6250 <<
" C=" << Tok->CanBreakBefore
6252 <<
" S=" << Tok->SpacesRequiredBefore
6253 <<
" F=" << Tok->Finalized <<
" B=" << Tok->BlockParameterCount
6254 <<
" BK=" << Tok->getBlockKind() <<
" P=" << Tok->SplitPenalty
6255 <<
" Name=" << Tok->Tok.getName() <<
" L=" << Tok->TotalLength
6256 <<
" PPK=" << Tok->getPackingKind() <<
" FakeLParens=";
6258 llvm::errs() << LParen <<
"/";
6259 llvm::errs() <<
" FakeRParens=" << Tok->FakeRParens;
6260 llvm::errs() <<
" II=" << Tok->Tok.getIdentifierInfo();
6261 llvm::errs() <<
" Text='" << Tok->TokenText <<
"'\n";
6263 assert(Tok ==
Line.Last);
6266 llvm::errs() <<
"----\n";
6270TokenAnnotator::getTokenReferenceAlignment(
const FormatToken &Reference)
const {
6271 assert(
Reference.isOneOf(tok::amp, tok::ampamp));
6272 switch (Style.ReferenceAlignment) {
6274 return Style.PointerAlignment;
6283 return Style.PointerAlignment;
6287TokenAnnotator::getTokenPointerOrReferenceAlignment(
6288 const FormatToken &PointerOrReference)
const {
6289 if (PointerOrReference.isOneOf(tok::amp, tok::ampamp)) {
6290 switch (Style.ReferenceAlignment) {
6292 return Style.PointerAlignment;
6301 assert(PointerOrReference.is(tok::star));
6302 return Style.PointerAlignment;
Defines the SourceManager interface.
bool ColonIsObjCMethodExpr
FormatToken * FirstStartOfName
bool InCpp11AttributeSpecifier
enum clang::format::@1261::AnnotatingParser::Context::@343 ContextType
unsigned LongestObjCSelectorName
bool VerilogAssignmentFound
bool InCSharpAttributeSpecifier
tok::TokenKind ContextKind
FormatToken * FirstObjCSelectorName
bool VerilogMayBeConcatenation
This file implements a token annotator, i.e.
Defines the clang::TokenKind enum and support functions.
#define TRANSFORM_TYPE_TRAIT_DEF(Enum, _)
Parser - This implements a parser for the C family of languages.
IdentifierInfo * getIdentifierInfo() const
bool Ret(InterpState &S, CodePtr &PC, APValue &Result)
TokenKind
Provides a simple uniform namespace for tokens from all C languages.
The JSON file list parser is used to communicate input to InstallAPI.
if(T->getSizeExpr()) TRY_TO(TraverseStmt(const_cast< Expr * >(T -> getSizeExpr())))
@ Parameter
The parameter type of a method or function.
@ Result
The result type of a method or function.
prec::Level getBinOpPrecedence(tok::TokenKind Kind, bool GreaterThanIsOperator, bool CPlusPlus11)
Return the precedence of the specified binary operator token.
const FunctionProtoType * T
AccessSpecifier
A C++ access specifier (public, private, protected), plus the special value "none" which means differ...