19 #include "llvm/ADT/SmallPtrSet.h"
20 #include "llvm/Support/Debug.h"
22 #define DEBUG_TYPE "format-token-annotator"
43 static bool startsWithInitStatement(
const AnnotatedLine &Line) {
59 static bool canBeObjCSelectorComponent(
const FormatToken &Tok) {
60 return Tok.Tok.getIdentifierInfo();
65 static 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);
81 static 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);
87 static 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);
123 class AnnotatingParser {
125 AnnotatingParser(
const FormatStyle &Style, AnnotatedLine &Line,
126 const AdditionalKeywords &Keywords,
127 SmallVector<ScopeType> &Scopes)
128 : Style(Style), Line(Line), CurrentToken(Line.
First), AutoFound(
false),
130 Keywords(Keywords), Scopes(Scopes) {
131 assert(IsCpp == LangOpts.CXXOperatorNames);
132 Contexts.push_back(Context(tok::unknown, 1,
false));
133 resetTokenMetadata();
137 ScopeType getScopeType(
const FormatToken &Token)
const {
138 switch (Token.getType()) {
139 case TT_FunctionLBrace:
140 case TT_LambdaLBrace:
143 case TT_StructLBrace:
152 if (!CurrentToken || !CurrentToken->Previous)
154 if (NonTemplateLess.count(CurrentToken->Previous) > 0)
157 const FormatToken &
Previous = *CurrentToken->Previous;
159 if (
Previous.Previous->Tok.isLiteral())
161 if (
Previous.Previous->is(tok::r_brace))
163 if (
Previous.Previous->is(tok::r_paren) && Contexts.size() > 1 &&
164 (!
Previous.Previous->MatchingParen ||
165 Previous.Previous->MatchingParen->isNot(
166 TT_OverloadedOperatorLParen))) {
169 if (
Previous.Previous->is(tok::kw_operator) &&
170 CurrentToken->is(tok::l_paren)) {
175 FormatToken *Left = CurrentToken->Previous;
176 Left->ParentBracket = Contexts.back().ContextKind;
177 ScopedContextCreator ContextCreator(*
this, tok::less, 12);
181 bool InExprContext = Contexts.back().IsExpression;
183 Contexts.back().IsExpression =
false;
186 if (Left->Previous && Left->Previous->isNot(tok::kw_template))
187 Contexts.back().ContextType = Context::TemplateArgument;
190 CurrentToken->is(tok::question)) {
194 while (CurrentToken) {
195 if (CurrentToken->is(tok::greater)) {
202 if (CurrentToken->Next && CurrentToken->Next->is(tok::greater) &&
203 Left->ParentBracket != tok::less &&
204 CurrentToken->getStartOfNonWhitespace() ==
205 CurrentToken->Next->getStartOfNonWhitespace().getLocWithOffset(
209 Left->MatchingParen = CurrentToken;
210 CurrentToken->MatchingParen = Left;
218 Left->Previous->isOneOf(TT_SelectorName, TT_DictLiteral))) {
219 CurrentToken->setType(TT_DictLiteral);
221 CurrentToken->setType(TT_TemplateCloser);
222 CurrentToken->Tok.setLength(1);
224 if (CurrentToken->Next && CurrentToken->Next->Tok.isLiteral())
229 if (CurrentToken->is(tok::question) &&
234 if (CurrentToken->isOneOf(tok::r_paren, tok::r_square, tok::r_brace) ||
235 (CurrentToken->isOneOf(tok::colon, tok::question) && InExprContext &&
236 !Style.isCSharp() && !Style.isProto())) {
245 if (CurrentToken->Previous->isOneOf(tok::pipepipe, tok::ampamp) &&
246 CurrentToken->Previous->is(TT_BinaryOperator) &&
247 Contexts[Contexts.size() - 2].IsExpression &&
248 !Line.startsWith(tok::kw_template)) {
251 updateParameterCount(Left, CurrentToken);
253 if (FormatToken *
Previous = CurrentToken->getPreviousNonComment()) {
254 if (CurrentToken->is(tok::colon) ||
255 (CurrentToken->isOneOf(tok::l_brace, tok::less) &&
261 if (Style.isTableGen()) {
262 if (CurrentToken->isOneOf(tok::comma, tok::equal)) {
269 if (!parseTableGenValue())
279 bool parseUntouchableParens() {
280 while (CurrentToken) {
281 CurrentToken->Finalized =
true;
282 switch (CurrentToken->Tok.getKind()) {
285 if (!parseUntouchableParens())
300 bool parseParens(
bool LookForDecls =
false) {
303 assert(CurrentToken->Previous &&
"Unknown previous token");
304 FormatToken &OpeningParen = *CurrentToken->Previous;
305 assert(OpeningParen.is(tok::l_paren));
306 FormatToken *PrevNonComment = OpeningParen.getPreviousNonComment();
307 OpeningParen.ParentBracket = Contexts.back().ContextKind;
308 ScopedContextCreator ContextCreator(*
this, tok::l_paren, 1);
311 Contexts.back().ColonIsForRangeExpr =
312 Contexts.size() == 2 && Contexts[0].ColonIsForRangeExpr;
314 if (OpeningParen.Previous &&
315 OpeningParen.Previous->is(TT_UntouchableMacroFunc)) {
316 OpeningParen.Finalized =
true;
317 return parseUntouchableParens();
320 bool StartsObjCMethodExpr =
false;
321 if (!Style.isVerilog()) {
322 if (FormatToken *MaybeSel = OpeningParen.Previous) {
324 if (MaybeSel->isObjCAtKeyword(tok::objc_selector) &&
325 MaybeSel->Previous && MaybeSel->Previous->is(tok::at)) {
326 StartsObjCMethodExpr =
true;
331 if (OpeningParen.is(TT_OverloadedOperatorLParen)) {
333 FormatToken *Prev = &OpeningParen;
334 while (Prev->isNot(tok::kw_operator)) {
335 Prev = Prev->Previous;
336 assert(Prev &&
"Expect a kw_operator prior to the OperatorLParen!");
342 bool OperatorCalledAsMemberFunction =
343 Prev->Previous && Prev->Previous->isOneOf(tok::period, tok::arrow);
344 Contexts.back().IsExpression = OperatorCalledAsMemberFunction;
345 }
else if (OpeningParen.is(TT_VerilogInstancePortLParen)) {
346 Contexts.back().IsExpression =
true;
347 Contexts.back().ContextType = Context::VerilogInstancePortList;
348 }
else if (Style.isJavaScript() &&
349 (Line.startsWith(Keywords.kw_type, tok::identifier) ||
350 Line.startsWith(tok::kw_export, Keywords.kw_type,
354 Contexts.back().IsExpression =
false;
355 }
else if (OpeningParen.Previous &&
356 (OpeningParen.Previous->isOneOf(
357 tok::kw_static_assert, tok::kw_noexcept, tok::kw_explicit,
358 tok::kw_while, tok::l_paren, tok::comma,
359 TT_BinaryOperator) ||
360 OpeningParen.Previous->isIf())) {
362 Contexts.back().IsExpression =
true;
363 }
else if (Style.isJavaScript() && OpeningParen.Previous &&
364 (OpeningParen.Previous->is(Keywords.kw_function) ||
365 (OpeningParen.Previous->endsSequence(tok::identifier,
366 Keywords.kw_function)))) {
368 Contexts.back().IsExpression =
false;
369 }
else if (Style.isJavaScript() && OpeningParen.Previous &&
370 OpeningParen.Previous->is(TT_JsTypeColon)) {
372 Contexts.back().IsExpression =
false;
373 }
else if (isLambdaParameterList(&OpeningParen)) {
375 Contexts.back().IsExpression =
false;
376 }
else if (OpeningParen.is(TT_RequiresExpressionLParen)) {
377 Contexts.back().IsExpression =
false;
378 }
else if (OpeningParen.Previous &&
379 OpeningParen.Previous->is(tok::kw__Generic)) {
380 Contexts.back().ContextType = Context::C11GenericSelection;
381 Contexts.back().IsExpression =
true;
382 }
else if (Line.InPPDirective &&
383 (!OpeningParen.Previous ||
384 OpeningParen.Previous->isNot(tok::identifier))) {
385 Contexts.back().IsExpression =
true;
386 }
else if (Contexts[Contexts.size() - 2].CaretFound) {
388 Contexts.back().IsExpression =
false;
389 }
else if (OpeningParen.Previous &&
390 OpeningParen.Previous->is(TT_ForEachMacro)) {
392 Contexts.back().ContextType = Context::ForEachMacro;
393 Contexts.back().IsExpression =
false;
394 }
else if (OpeningParen.Previous && OpeningParen.Previous->MatchingParen &&
395 OpeningParen.Previous->MatchingParen->isOneOf(
396 TT_ObjCBlockLParen, TT_FunctionTypeLParen)) {
397 Contexts.back().IsExpression =
false;
398 }
else if (!Line.MustBeDeclaration && !Line.InPPDirective) {
400 OpeningParen.Previous &&
401 OpeningParen.Previous->isOneOf(tok::kw_for, tok::kw_catch);
402 Contexts.back().IsExpression = !IsForOrCatch;
405 if (Style.isTableGen()) {
406 if (FormatToken *Prev = OpeningParen.Previous) {
407 if (Prev->is(TT_TableGenCondOperator)) {
408 Contexts.back().IsTableGenCondOpe =
true;
409 Contexts.back().IsExpression =
true;
410 }
else if (Contexts.size() > 1 &&
411 Contexts[Contexts.size() - 2].IsTableGenBangOpe) {
416 Contexts.back().IsTableGenBangOpe =
true;
417 Contexts.back().IsExpression =
true;
420 if (!parseTableGenDAGArg())
422 return parseTableGenDAGArgAndList(&OpeningParen);
429 if (PrevNonComment && OpeningParen.is(TT_Unknown)) {
430 if (PrevNonComment->isAttribute()) {
431 OpeningParen.setType(TT_AttributeLParen);
432 }
else if (PrevNonComment->isOneOf(TT_TypenameMacro, tok::kw_decltype,
435 #include
"clang/Basic/TransformTypeTraits.def"
437 OpeningParen.setType(TT_TypeDeclarationParen);
439 if (PrevNonComment->isOneOf(tok::kw_decltype, tok::kw_typeof))
440 Contexts.back().IsExpression =
true;
444 if (StartsObjCMethodExpr) {
445 Contexts.back().ColonIsObjCMethodExpr =
true;
446 OpeningParen.setType(TT_ObjCMethodExpr);
457 bool MightBeFunctionType = !Contexts[Contexts.size() - 2].IsExpression;
458 bool ProbablyFunctionType =
459 CurrentToken->isPointerOrReference() || CurrentToken->is(tok::caret);
460 bool HasMultipleLines =
false;
461 bool HasMultipleParametersOnALine =
false;
462 bool MightBeObjCForRangeLoop =
463 OpeningParen.Previous && OpeningParen.Previous->is(tok::kw_for);
464 FormatToken *PossibleObjCForInToken =
nullptr;
465 while (CurrentToken) {
470 if (LookForDecls && CurrentToken->Next) {
471 FormatToken *Prev = CurrentToken->getPreviousNonComment();
473 FormatToken *PrevPrev = Prev->getPreviousNonComment();
474 FormatToken *Next = CurrentToken->Next;
475 if (PrevPrev && PrevPrev->is(tok::identifier) &&
476 PrevPrev->isNot(TT_TypeName) && Prev->isPointerOrReference() &&
477 CurrentToken->is(tok::identifier) && Next->isNot(tok::equal)) {
478 Prev->setType(TT_BinaryOperator);
479 LookForDecls =
false;
484 if (CurrentToken->Previous->is(TT_PointerOrReference) &&
485 CurrentToken->Previous->Previous->isOneOf(tok::l_paren,
487 ProbablyFunctionType =
true;
489 if (CurrentToken->is(tok::comma))
490 MightBeFunctionType =
false;
491 if (CurrentToken->Previous->is(TT_BinaryOperator))
492 Contexts.back().IsExpression =
true;
493 if (CurrentToken->is(tok::r_paren)) {
494 if (OpeningParen.isNot(TT_CppCastLParen) && MightBeFunctionType &&
495 ProbablyFunctionType && CurrentToken->Next &&
496 (CurrentToken->Next->is(tok::l_paren) ||
497 (CurrentToken->Next->is(tok::l_square) &&
498 Line.MustBeDeclaration))) {
499 OpeningParen.setType(OpeningParen.Next->is(tok::caret)
501 : TT_FunctionTypeLParen);
503 OpeningParen.MatchingParen = CurrentToken;
504 CurrentToken->MatchingParen = &OpeningParen;
506 if (CurrentToken->Next && CurrentToken->Next->is(tok::l_brace) &&
507 OpeningParen.Previous && OpeningParen.Previous->is(tok::l_paren)) {
511 for (FormatToken *Tok = &OpeningParen; Tok != CurrentToken;
513 if (Tok->is(TT_BinaryOperator) && Tok->isPointerOrReference())
514 Tok->setType(TT_PointerOrReference);
518 if (StartsObjCMethodExpr) {
519 CurrentToken->setType(TT_ObjCMethodExpr);
520 if (Contexts.back().FirstObjCSelectorName) {
521 Contexts.back().FirstObjCSelectorName->LongestObjCSelectorName =
522 Contexts.back().LongestObjCSelectorName;
526 if (OpeningParen.is(TT_AttributeLParen))
527 CurrentToken->setType(TT_AttributeRParen);
528 if (OpeningParen.is(TT_TypeDeclarationParen))
529 CurrentToken->setType(TT_TypeDeclarationParen);
530 if (OpeningParen.Previous &&
531 OpeningParen.Previous->is(TT_JavaAnnotation)) {
532 CurrentToken->setType(TT_JavaAnnotation);
534 if (OpeningParen.Previous &&
535 OpeningParen.Previous->is(TT_LeadingJavaAnnotation)) {
536 CurrentToken->setType(TT_LeadingJavaAnnotation);
538 if (OpeningParen.Previous &&
539 OpeningParen.Previous->is(TT_AttributeSquare)) {
540 CurrentToken->setType(TT_AttributeSquare);
543 if (!HasMultipleLines)
545 else if (HasMultipleParametersOnALine)
553 if (CurrentToken->isOneOf(tok::r_square, tok::r_brace))
556 if (CurrentToken->is(tok::l_brace) && OpeningParen.is(TT_ObjCBlockLParen))
557 OpeningParen.setType(TT_Unknown);
558 if (CurrentToken->is(tok::comma) && CurrentToken->Next &&
559 !CurrentToken->Next->HasUnescapedNewline &&
560 !CurrentToken->Next->isTrailingComment()) {
561 HasMultipleParametersOnALine =
true;
563 bool ProbablyFunctionTypeLParen =
564 (CurrentToken->is(tok::l_paren) && CurrentToken->Next &&
565 CurrentToken->Next->isOneOf(tok::star, tok::amp, tok::caret));
566 if ((CurrentToken->Previous->isOneOf(tok::kw_const, tok::kw_auto) ||
567 CurrentToken->Previous->isTypeName(LangOpts)) &&
568 !(CurrentToken->is(tok::l_brace) ||
569 (CurrentToken->is(tok::l_paren) && !ProbablyFunctionTypeLParen))) {
570 Contexts.back().IsExpression =
false;
572 if (CurrentToken->isOneOf(tok::semi, tok::colon)) {
573 MightBeObjCForRangeLoop =
false;
574 if (PossibleObjCForInToken) {
575 PossibleObjCForInToken->setType(TT_Unknown);
576 PossibleObjCForInToken =
nullptr;
579 if (MightBeObjCForRangeLoop && CurrentToken->is(Keywords.kw_in)) {
580 PossibleObjCForInToken = CurrentToken;
581 PossibleObjCForInToken->setType(TT_ObjCForIn);
585 if (CurrentToken->is(tok::comma))
586 Contexts.back().CanBeExpression =
true;
588 if (Style.isTableGen()) {
589 if (CurrentToken->is(tok::comma)) {
590 if (Contexts.back().IsTableGenCondOpe)
591 CurrentToken->setType(TT_TableGenCondOperatorComma);
593 }
else if (CurrentToken->is(tok::colon)) {
594 if (Contexts.back().IsTableGenCondOpe)
595 CurrentToken->setType(TT_TableGenCondOperatorColon);
599 if (!parseTableGenValue())
604 FormatToken *Tok = CurrentToken;
607 updateParameterCount(&OpeningParen, Tok);
608 if (CurrentToken && CurrentToken->HasUnescapedNewline)
609 HasMultipleLines =
true;
614 bool isCSharpAttributeSpecifier(
const FormatToken &Tok) {
615 if (!Style.isCSharp())
619 if (Tok.Previous && Tok.Previous->is(tok::identifier))
623 if (Tok.Previous && Tok.Previous->is(tok::r_square)) {
624 auto *MatchingParen = Tok.Previous->MatchingParen;
625 if (!MatchingParen || MatchingParen->is(TT_ArraySubscriptLSquare))
629 const FormatToken *AttrTok = Tok.Next;
634 if (AttrTok->is(tok::r_square))
638 while (AttrTok && AttrTok->isNot(tok::r_square))
639 AttrTok = AttrTok->Next;
645 AttrTok = AttrTok->Next;
650 if (AttrTok->isOneOf(tok::kw_public, tok::kw_private, tok::kw_protected,
651 tok::comment, tok::kw_class, tok::kw_static,
652 tok::l_square, Keywords.kw_internal)) {
658 AttrTok->Next->startsSequence(tok::identifier, tok::l_paren)) {
673 FormatToken *Left = CurrentToken->Previous;
674 Left->ParentBracket = Contexts.back().ContextKind;
675 FormatToken *
Parent = Left->getPreviousNonComment();
680 bool CppArrayTemplates =
682 (Contexts.back().CanBeExpression || Contexts.back().IsExpression ||
683 Contexts.back().ContextType == Context::TemplateArgument);
685 const bool IsInnerSquare = Contexts.back().InCpp11AttributeSpecifier;
686 const bool IsCpp11AttributeSpecifier =
687 isCppAttribute(IsCpp, *Left) || IsInnerSquare;
690 bool IsCSharpAttributeSpecifier =
691 isCSharpAttributeSpecifier(*Left) ||
692 Contexts.back().InCSharpAttributeSpecifier;
694 bool InsideInlineASM = Line.startsWith(tok::kw_asm);
695 bool IsCppStructuredBinding = Left->isCppStructuredBinding(IsCpp);
696 bool StartsObjCMethodExpr =
697 !IsCppStructuredBinding && !InsideInlineASM && !CppArrayTemplates &&
698 IsCpp && !IsCpp11AttributeSpecifier && !IsCSharpAttributeSpecifier &&
699 Contexts.back().CanBeExpression && Left->isNot(TT_LambdaLSquare) &&
700 !CurrentToken->isOneOf(tok::l_brace, tok::r_square) &&
702 Parent->isOneOf(tok::colon, tok::l_square, tok::l_paren,
703 tok::kw_return, tok::kw_throw) ||
704 Parent->isUnaryOperator() ||
706 Parent->isOneOf(TT_ObjCForIn, TT_CastRParen) ||
709 bool ColonFound =
false;
711 unsigned BindingIncrease = 1;
712 if (IsCppStructuredBinding) {
713 Left->setType(TT_StructuredBindingLSquare);
714 }
else if (Left->is(TT_Unknown)) {
715 if (StartsObjCMethodExpr) {
716 Left->setType(TT_ObjCMethodExpr);
717 }
else if (InsideInlineASM) {
718 Left->setType(TT_InlineASMSymbolicNameLSquare);
719 }
else if (IsCpp11AttributeSpecifier) {
720 Left->setType(TT_AttributeSquare);
721 if (!IsInnerSquare && Left->Previous)
722 Left->Previous->EndsCppAttributeGroup =
false;
723 }
else if (Style.isJavaScript() &&
Parent &&
724 Contexts.back().ContextKind == tok::l_brace &&
725 Parent->isOneOf(tok::l_brace, tok::comma)) {
726 Left->setType(TT_JsComputedPropertyName);
727 }
else if (IsCpp && Contexts.back().ContextKind == tok::l_brace &&
729 Left->setType(TT_DesignatedInitializerLSquare);
730 }
else if (IsCSharpAttributeSpecifier) {
731 Left->setType(TT_AttributeSquare);
732 }
else if (CurrentToken->is(tok::r_square) &&
Parent &&
733 Parent->is(TT_TemplateCloser)) {
734 Left->setType(TT_ArraySubscriptLSquare);
735 }
else if (Style.isProto()) {
762 Left->setType(TT_ArrayInitializerLSquare);
763 if (!Left->endsSequence(tok::l_square, tok::numeric_constant,
765 !Left->endsSequence(tok::l_square, tok::numeric_constant,
767 !Left->endsSequence(tok::l_square, tok::colon, TT_SelectorName)) {
768 Left->setType(TT_ProtoExtensionLSquare);
769 BindingIncrease = 10;
771 }
else if (!CppArrayTemplates &&
Parent &&
772 Parent->isOneOf(TT_BinaryOperator, TT_TemplateCloser, tok::at,
773 tok::comma, tok::l_paren, tok::l_square,
774 tok::question, tok::colon, tok::kw_return,
777 Left->setType(TT_ArrayInitializerLSquare);
779 BindingIncrease = 10;
780 Left->setType(TT_ArraySubscriptLSquare);
784 ScopedContextCreator ContextCreator(*
this, tok::l_square, BindingIncrease);
785 Contexts.back().IsExpression =
true;
786 if (Style.isJavaScript() &&
Parent &&
Parent->is(TT_JsTypeColon))
787 Contexts.back().IsExpression =
false;
789 Contexts.back().ColonIsObjCMethodExpr = StartsObjCMethodExpr;
790 Contexts.back().InCpp11AttributeSpecifier = IsCpp11AttributeSpecifier;
791 Contexts.back().InCSharpAttributeSpecifier = IsCSharpAttributeSpecifier;
793 while (CurrentToken) {
794 if (CurrentToken->is(tok::r_square)) {
795 if (IsCpp11AttributeSpecifier) {
796 CurrentToken->setType(TT_AttributeSquare);
798 CurrentToken->EndsCppAttributeGroup =
true;
800 if (IsCSharpAttributeSpecifier) {
801 CurrentToken->setType(TT_AttributeSquare);
802 }
else if (((CurrentToken->Next &&
803 CurrentToken->Next->is(tok::l_paren)) ||
804 (CurrentToken->Previous &&
805 CurrentToken->Previous->Previous == Left)) &&
806 Left->is(TT_ObjCMethodExpr)) {
811 StartsObjCMethodExpr =
false;
812 Left->setType(TT_Unknown);
814 if (StartsObjCMethodExpr && CurrentToken->Previous != Left) {
815 CurrentToken->setType(TT_ObjCMethodExpr);
818 if (!ColonFound && CurrentToken->Previous &&
819 CurrentToken->Previous->is(TT_Unknown) &&
820 canBeObjCSelectorComponent(*CurrentToken->Previous)) {
821 CurrentToken->Previous->setType(TT_SelectorName);
827 Parent->overwriteFixedType(TT_BinaryOperator);
830 if (CurrentToken->is(TT_ObjCMethodExpr) && CurrentToken->Next &&
831 CurrentToken->Next->is(TT_TrailingReturnArrow)) {
832 CurrentToken->Next->overwriteFixedType(TT_Unknown);
834 Left->MatchingParen = CurrentToken;
835 CurrentToken->MatchingParen = Left;
840 if (!Contexts.back().FirstObjCSelectorName) {
841 FormatToken *
Previous = CurrentToken->getPreviousNonComment();
843 Previous->ObjCSelectorNameParts = 1;
844 Contexts.back().FirstObjCSelectorName =
Previous;
847 Left->ParameterCount =
848 Contexts.back().FirstObjCSelectorName->ObjCSelectorNameParts;
850 if (Contexts.back().FirstObjCSelectorName) {
851 Contexts.back().FirstObjCSelectorName->LongestObjCSelectorName =
852 Contexts.back().LongestObjCSelectorName;
853 if (Left->BlockParameterCount > 1)
854 Contexts.back().FirstObjCSelectorName->LongestObjCSelectorName = 0;
856 if (Style.isTableGen() && Left->is(TT_TableGenListOpener))
857 CurrentToken->setType(TT_TableGenListCloser);
861 if (CurrentToken->isOneOf(tok::r_paren, tok::r_brace))
863 if (CurrentToken->is(tok::colon)) {
864 if (IsCpp11AttributeSpecifier &&
865 CurrentToken->endsSequence(tok::colon, tok::identifier,
869 CurrentToken->setType(TT_AttributeColon);
870 }
else if (!Style.isVerilog() && !Line.InPragmaDirective &&
871 Left->isOneOf(TT_ArraySubscriptLSquare,
872 TT_DesignatedInitializerLSquare)) {
873 Left->setType(TT_ObjCMethodExpr);
874 StartsObjCMethodExpr =
true;
875 Contexts.back().ColonIsObjCMethodExpr =
true;
878 Parent->setType(TT_CastRParen);
883 if (CurrentToken->is(tok::comma) && Left->is(TT_ObjCMethodExpr) &&
885 Left->setType(TT_ArrayInitializerLSquare);
887 FormatToken *Tok = CurrentToken;
888 if (Style.isTableGen()) {
889 if (CurrentToken->isOneOf(tok::comma, tok::minus, tok::ellipsis)) {
895 if (!parseTableGenValue())
898 updateParameterCount(Left, Tok);
903 updateParameterCount(Left, Tok);
908 void skipToNextNonComment() {
910 while (CurrentToken && CurrentToken->is(tok::comment))
919 bool parseTableGenValue(
bool ParseNameMode =
false) {
922 while (CurrentToken->is(tok::comment))
924 if (!parseTableGenSimpleValue())
929 if (CurrentToken->is(tok::hash)) {
930 if (CurrentToken->Next &&
931 CurrentToken->Next->isOneOf(tok::colon, tok::semi, tok::l_brace)) {
934 CurrentToken->setType(TT_TableGenTrailingPasteOperator);
938 FormatToken *HashTok = CurrentToken;
939 skipToNextNonComment();
940 HashTok->setType(TT_Unknown);
941 if (!parseTableGenValue(ParseNameMode))
946 if (ParseNameMode && CurrentToken->is(tok::l_brace))
949 if (CurrentToken->isOneOf(tok::l_brace, tok::l_square, tok::period)) {
950 CurrentToken->setType(TT_TableGenValueSuffix);
951 FormatToken *Suffix = CurrentToken;
952 skipToNextNonComment();
953 if (Suffix->is(tok::l_square))
954 return parseSquare();
955 if (Suffix->is(tok::l_brace)) {
956 Scopes.push_back(getScopeType(*Suffix));
966 bool tryToParseTableGenTokVar() {
969 if (CurrentToken->is(tok::identifier) &&
970 CurrentToken->TokenText.front() ==
'$') {
971 skipToNextNonComment();
979 bool parseTableGenDAGArg(
bool AlignColon =
false) {
980 if (tryToParseTableGenTokVar())
982 if (parseTableGenValue()) {
983 if (CurrentToken && CurrentToken->is(tok::colon)) {
985 CurrentToken->setType(TT_TableGenDAGArgListColonToAlign);
987 CurrentToken->setType(TT_TableGenDAGArgListColon);
988 skipToNextNonComment();
989 return tryToParseTableGenTokVar();
999 bool isTableGenDAGArgBreakingOperator(
const FormatToken &Tok) {
1000 auto &Opes = Style.TableGenBreakingDAGArgOperators;
1005 if (Tok.isNot(tok::identifier) ||
1006 Tok.isOneOf(TT_TableGenBangOperator, TT_TableGenCondOperator)) {
1010 if (!Tok.Next || Tok.Next->is(tok::colon))
1012 return std::find(Opes.begin(), Opes.end(), Tok.TokenText.str()) !=
1018 bool parseTableGenDAGArgAndList(FormatToken *Opener) {
1019 FormatToken *FirstTok = CurrentToken;
1020 if (!parseTableGenDAGArg())
1022 bool BreakInside =
false;
1026 if (isTableGenDAGArgBreakingOperator(*FirstTok)) {
1029 Opener->setType(TT_TableGenDAGArgOpenerToBreak);
1030 if (FirstTok->isOneOf(TT_TableGenBangOperator,
1031 TT_TableGenCondOperator)) {
1034 CurrentToken->Previous->setType(TT_TableGenDAGArgOperatorToBreak);
1035 }
else if (FirstTok->is(tok::identifier)) {
1037 FirstTok->setType(TT_TableGenDAGArgOperatorToBreak);
1039 FirstTok->setType(TT_TableGenDAGArgOperatorID);
1044 bool FirstDAGArgListElm =
true;
1045 while (CurrentToken) {
1046 if (!FirstDAGArgListElm && CurrentToken->is(tok::comma)) {
1047 CurrentToken->setType(BreakInside ? TT_TableGenDAGArgListCommaToBreak
1048 : TT_TableGenDAGArgListComma);
1049 skipToNextNonComment();
1051 if (CurrentToken && CurrentToken->is(tok::r_paren)) {
1052 CurrentToken->setType(TT_TableGenDAGArgCloser);
1053 Opener->MatchingParen = CurrentToken;
1054 CurrentToken->MatchingParen = Opener;
1055 skipToNextNonComment();
1058 if (!parseTableGenDAGArg(
1060 Style.AlignConsecutiveTableGenBreakingDAGArgColons.Enabled)) {
1063 FirstDAGArgListElm =
false;
1068 bool parseTableGenSimpleValue() {
1069 assert(Style.isTableGen());
1072 FormatToken *Tok = CurrentToken;
1073 skipToNextNonComment();
1075 if (Tok->isOneOf(tok::numeric_constant, tok::string_literal,
1076 TT_TableGenMultiLineString, tok::kw_true, tok::kw_false,
1077 tok::question, tok::kw_int)) {
1081 if (Tok->is(tok::l_brace)) {
1082 Scopes.push_back(getScopeType(*Tok));
1083 return parseBrace();
1086 if (Tok->is(tok::l_square)) {
1087 Tok->setType(TT_TableGenListOpener);
1090 if (Tok->is(tok::less)) {
1091 CurrentToken->setType(TT_TemplateOpener);
1092 return parseAngle();
1098 if (Tok->is(tok::l_paren)) {
1099 Tok->setType(TT_TableGenDAGArgOpener);
1100 return parseTableGenDAGArgAndList(Tok);
1103 if (Tok->is(TT_TableGenBangOperator)) {
1104 if (CurrentToken && CurrentToken->is(tok::less)) {
1105 CurrentToken->setType(TT_TemplateOpener);
1106 skipToNextNonComment();
1110 if (!CurrentToken || CurrentToken->isNot(tok::l_paren))
1112 skipToNextNonComment();
1114 Contexts.back().IsTableGenBangOpe =
true;
1115 bool Result = parseParens();
1116 Contexts.back().IsTableGenBangOpe =
false;
1120 if (Tok->is(TT_TableGenCondOperator)) {
1122 skipToNextNonComment();
1123 if (!Tok || Tok->isNot(tok::l_paren))
1125 bool Result = parseParens();
1131 if (Tok->is(tok::identifier)) {
1133 if (CurrentToken && CurrentToken->is(tok::less)) {
1134 CurrentToken->setType(TT_TemplateOpener);
1135 skipToNextNonComment();
1136 return parseAngle();
1144 bool couldBeInStructArrayInitializer()
const {
1145 if (Contexts.size() < 2)
1149 const auto End = std::next(Contexts.rbegin(), 2);
1150 auto Last = Contexts.rbegin();
1153 if (
Last->ContextKind == tok::l_brace)
1155 return Depth == 2 &&
Last->ContextKind != tok::l_brace;
1162 assert(CurrentToken->Previous);
1163 FormatToken &OpeningBrace = *CurrentToken->Previous;
1164 assert(OpeningBrace.is(tok::l_brace));
1165 OpeningBrace.ParentBracket = Contexts.back().ContextKind;
1167 if (Contexts.back().CaretFound)
1168 OpeningBrace.overwriteFixedType(TT_ObjCBlockLBrace);
1169 Contexts.back().CaretFound =
false;
1171 ScopedContextCreator ContextCreator(*
this, tok::l_brace, 1);
1172 Contexts.back().ColonIsDictLiteral =
true;
1174 Contexts.back().IsExpression =
true;
1175 if (Style.isJavaScript() && OpeningBrace.Previous &&
1176 OpeningBrace.Previous->is(TT_JsTypeColon)) {
1177 Contexts.back().IsExpression =
false;
1179 if (Style.isVerilog() &&
1180 (!OpeningBrace.getPreviousNonComment() ||
1181 OpeningBrace.getPreviousNonComment()->isNot(Keywords.kw_apostrophe))) {
1182 Contexts.back().VerilogMayBeConcatenation =
true;
1184 if (Style.isTableGen())
1185 Contexts.back().ColonIsDictLiteral =
false;
1187 unsigned CommaCount = 0;
1188 while (CurrentToken) {
1189 if (CurrentToken->is(tok::r_brace)) {
1190 assert(!Scopes.empty());
1191 assert(Scopes.back() == getScopeType(OpeningBrace));
1193 assert(OpeningBrace.Optional == CurrentToken->Optional);
1194 OpeningBrace.MatchingParen = CurrentToken;
1195 CurrentToken->MatchingParen = &OpeningBrace;
1197 if (OpeningBrace.ParentBracket == tok::l_brace &&
1198 couldBeInStructArrayInitializer() && CommaCount > 0) {
1199 Contexts.back().ContextType = Context::StructArrayInitializer;
1205 if (CurrentToken->isOneOf(tok::r_paren, tok::r_square))
1207 updateParameterCount(&OpeningBrace, CurrentToken);
1208 if (CurrentToken->isOneOf(tok::colon, tok::l_brace, tok::less)) {
1209 FormatToken *
Previous = CurrentToken->getPreviousNonComment();
1210 if (
Previous->is(TT_JsTypeOptionalQuestion))
1212 if ((CurrentToken->is(tok::colon) && !Style.isTableGen() &&
1213 (!Contexts.back().ColonIsDictLiteral || !IsCpp)) ||
1215 OpeningBrace.setType(TT_DictLiteral);
1216 if (
Previous->Tok.getIdentifierInfo() ||
1217 Previous->is(tok::string_literal)) {
1218 Previous->setType(TT_SelectorName);
1221 if (CurrentToken->is(tok::colon) && OpeningBrace.is(TT_Unknown) &&
1222 !Style.isTableGen()) {
1223 OpeningBrace.setType(TT_DictLiteral);
1224 }
else if (Style.isJavaScript()) {
1225 OpeningBrace.overwriteFixedType(TT_DictLiteral);
1228 if (CurrentToken->is(tok::comma)) {
1229 if (Style.isJavaScript())
1230 OpeningBrace.overwriteFixedType(TT_DictLiteral);
1233 if (!consumeToken())
1239 void updateParameterCount(FormatToken *Left, FormatToken *Current) {
1243 if (Current->is(tok::l_brace) && Current->is(
BK_Block))
1244 ++Left->BlockParameterCount;
1245 if (Current->is(tok::comma)) {
1246 ++Left->ParameterCount;
1248 Left->Role.reset(
new CommaSeparatedList(Style));
1249 Left->Role->CommaFound(Current);
1250 }
else if (Left->ParameterCount == 0 && Current->isNot(tok::comment)) {
1251 Left->ParameterCount = 1;
1255 bool parseConditional() {
1256 while (CurrentToken) {
1257 if (CurrentToken->is(tok::colon)) {
1258 CurrentToken->setType(TT_ConditionalExpr);
1262 if (!consumeToken())
1268 bool parseTemplateDeclaration() {
1269 if (CurrentToken && CurrentToken->is(tok::less)) {
1270 CurrentToken->setType(TT_TemplateOpener);
1275 CurrentToken->Previous->ClosesTemplateDeclaration =
true;
1281 bool consumeToken() {
1283 const auto *Prev = CurrentToken->getPreviousNonComment();
1284 if (Prev && Prev->is(tok::r_square) && Prev->is(TT_AttributeSquare) &&
1285 CurrentToken->isOneOf(tok::kw_if, tok::kw_switch, tok::kw_case,
1286 tok::kw_default, tok::kw_for, tok::kw_while) &&
1288 CurrentToken->MustBreakBefore =
true;
1291 FormatToken *Tok = CurrentToken;
1295 if (Tok->is(TT_VerilogTableItem))
1298 if (Tok->is(TT_TableGenMultiLineString))
1300 switch (Tok->Tok.getKind()) {
1303 if (!Tok->Previous && Line.MustBeDeclaration)
1304 Tok->setType(TT_ObjCMethodSpecifier);
1311 if (Tok->isTypeFinalized())
1314 if (Style.isJavaScript()) {
1315 if (Contexts.back().ColonIsForRangeExpr ||
1316 (Contexts.size() == 1 &&
1317 !Line.First->isOneOf(tok::kw_enum, tok::kw_case)) ||
1318 Contexts.back().ContextKind == tok::l_paren ||
1319 Contexts.back().ContextKind == tok::l_square ||
1320 (!Contexts.back().IsExpression &&
1321 Contexts.back().ContextKind == tok::l_brace) ||
1322 (Contexts.size() == 1 &&
1323 Line.MustBeDeclaration)) {
1324 Contexts.back().IsExpression =
false;
1325 Tok->setType(TT_JsTypeColon);
1328 }
else if (Style.isCSharp()) {
1329 if (Contexts.back().InCSharpAttributeSpecifier) {
1330 Tok->setType(TT_AttributeColon);
1333 if (Contexts.back().ContextKind == tok::l_paren) {
1334 Tok->setType(TT_CSharpNamedArgumentColon);
1337 }
else if (Style.isVerilog() && Tok->isNot(TT_BinaryOperator)) {
1340 if (Keywords.isVerilogEnd(*Tok->Previous) ||
1341 Keywords.isVerilogBegin(*Tok->Previous)) {
1342 Tok->setType(TT_VerilogBlockLabelColon);
1343 }
else if (Contexts.back().ContextKind == tok::l_square) {
1344 Tok->setType(TT_BitFieldColon);
1345 }
else if (Contexts.back().ColonIsDictLiteral) {
1346 Tok->setType(TT_DictLiteral);
1347 }
else if (Contexts.size() == 1) {
1351 Tok->setType(TT_CaseLabelColon);
1352 if (Line.Level > 1 || (!Line.InPPDirective && Line.Level > 0))
1357 if (Line.First->isOneOf(Keywords.kw_module, Keywords.kw_import) ||
1358 Line.First->startsSequence(tok::kw_export, Keywords.kw_module) ||
1359 Line.First->startsSequence(tok::kw_export, Keywords.kw_import)) {
1360 Tok->setType(TT_ModulePartitionColon);
1361 }
else if (Contexts.back().ColonIsDictLiteral || Style.isProto()) {
1362 Tok->setType(TT_DictLiteral);
1364 if (FormatToken *
Previous = Tok->getPreviousNonComment())
1365 Previous->setType(TT_SelectorName);
1367 }
else if (Contexts.back().ColonIsObjCMethodExpr ||
1368 Line.startsWith(TT_ObjCMethodSpecifier)) {
1369 Tok->setType(TT_ObjCMethodExpr);
1370 const FormatToken *BeforePrevious = Tok->Previous->Previous;
1373 bool UnknownIdentifierInMethodDeclaration =
1374 Line.startsWith(TT_ObjCMethodSpecifier) &&
1375 Tok->Previous->is(tok::identifier) && Tok->Previous->is(TT_Unknown);
1376 if (!BeforePrevious ||
1378 !(BeforePrevious->is(TT_CastRParen) ||
1379 (BeforePrevious->is(TT_ObjCMethodExpr) &&
1380 BeforePrevious->is(tok::colon))) ||
1381 BeforePrevious->is(tok::r_square) ||
1382 Contexts.back().LongestObjCSelectorName == 0 ||
1383 UnknownIdentifierInMethodDeclaration) {
1384 Tok->Previous->setType(TT_SelectorName);
1385 if (!Contexts.back().FirstObjCSelectorName) {
1386 Contexts.back().FirstObjCSelectorName = Tok->Previous;
1387 }
else if (Tok->Previous->ColumnWidth >
1388 Contexts.back().LongestObjCSelectorName) {
1389 Contexts.back().LongestObjCSelectorName =
1390 Tok->Previous->ColumnWidth;
1392 Tok->Previous->ParameterIndex =
1393 Contexts.back().FirstObjCSelectorName->ObjCSelectorNameParts;
1394 ++Contexts.back().FirstObjCSelectorName->ObjCSelectorNameParts;
1396 }
else if (Contexts.back().ColonIsForRangeExpr) {
1397 Tok->setType(TT_RangeBasedForLoopColon);
1398 }
else if (Contexts.back().ContextType == Context::C11GenericSelection) {
1399 Tok->setType(TT_GenericSelectionColon);
1400 }
else if (CurrentToken && CurrentToken->is(tok::numeric_constant)) {
1401 Tok->setType(TT_BitFieldColon);
1402 }
else if (Contexts.size() == 1 &&
1403 !Line.First->isOneOf(tok::kw_enum, tok::kw_case,
1405 FormatToken *Prev = Tok->getPreviousNonComment();
1408 if (Prev->isOneOf(tok::r_paren, tok::kw_noexcept) ||
1409 Prev->ClosesRequiresClause) {
1410 Tok->setType(TT_CtorInitializerColon);
1411 }
else if (Prev->is(tok::kw_try)) {
1413 FormatToken *PrevPrev = Prev->getPreviousNonComment();
1416 if (PrevPrev && PrevPrev->isOneOf(tok::r_paren, tok::kw_noexcept))
1417 Tok->setType(TT_CtorInitializerColon);
1419 Tok->setType(TT_InheritanceColon);
1421 }
else if (canBeObjCSelectorComponent(*Tok->Previous) && Tok->Next &&
1422 (Tok->Next->isOneOf(tok::r_paren, tok::comma) ||
1423 (canBeObjCSelectorComponent(*Tok->Next) && Tok->Next->Next &&
1424 Tok->Next->Next->is(tok::colon)))) {
1427 Tok->setType(TT_ObjCMethodExpr);
1428 }
else if (Contexts.back().ContextKind == tok::l_paren &&
1429 !Line.InPragmaDirective) {
1430 if (Style.isTableGen() && Contexts.back().IsTableGenDAGArg) {
1431 Tok->setType(TT_TableGenDAGArgListColon);
1434 Tok->setType(TT_InlineASMColon);
1441 if (Style.isJavaScript() && !Contexts.back().IsExpression)
1442 Tok->setType(TT_JsTypeOperator);
1445 if (Style.isTableGen()) {
1447 if (!parseTableGenValue())
1449 if (CurrentToken && CurrentToken->is(Keywords.kw_then))
1454 CurrentToken->isOneOf(tok::kw_constexpr, tok::identifier)) {
1459 if (CurrentToken && CurrentToken->is(tok::l_paren)) {
1461 if (!parseParens(
true))
1466 if (Style.isJavaScript()) {
1468 if ((Tok->Previous && Tok->Previous->is(tok::period)) ||
1469 (Tok->Next && Tok->Next->is(tok::colon))) {
1473 if (CurrentToken && CurrentToken->is(Keywords.kw_await))
1476 if (IsCpp && CurrentToken && CurrentToken->is(tok::kw_co_await))
1478 Contexts.back().ColonIsForRangeExpr =
true;
1479 if (!CurrentToken || CurrentToken->isNot(tok::l_paren))
1490 if (Tok->Previous && Tok->Previous->is(tok::r_paren) &&
1491 Tok->Previous->MatchingParen &&
1492 Tok->Previous->MatchingParen->is(TT_OverloadedOperatorLParen)) {
1493 Tok->Previous->setType(TT_OverloadedOperator);
1494 Tok->Previous->MatchingParen->setType(TT_OverloadedOperator);
1495 Tok->setType(TT_OverloadedOperatorLParen);
1498 if (Style.isVerilog()) {
1504 auto IsInstancePort = [&]() {
1505 const FormatToken *Prev = Tok->getPreviousNonComment();
1506 const FormatToken *PrevPrev;
1514 if (!Prev || !(PrevPrev = Prev->getPreviousNonComment()))
1517 if (Keywords.isVerilogIdentifier(*Prev) &&
1518 Keywords.isVerilogIdentifier(*PrevPrev)) {
1522 if (Prev->is(Keywords.kw_verilogHash) &&
1523 Keywords.isVerilogIdentifier(*PrevPrev)) {
1527 if (Keywords.isVerilogIdentifier(*Prev) && PrevPrev->is(tok::r_paren))
1530 if (Keywords.isVerilogIdentifier(*Prev) && PrevPrev->is(tok::comma)) {
1531 const FormatToken *PrevParen = PrevPrev->getPreviousNonComment();
1532 if (PrevParen->is(tok::r_paren) && PrevParen->MatchingParen &&
1533 PrevParen->MatchingParen->is(TT_VerilogInstancePortLParen)) {
1540 if (IsInstancePort())
1541 Tok->setFinalizedType(TT_VerilogInstancePortLParen);
1546 if (Line.MustBeDeclaration && Contexts.size() == 1 &&
1547 !Contexts.back().IsExpression && !Line.startsWith(TT_ObjCProperty) &&
1548 !Line.startsWith(tok::l_paren) &&
1549 !Tok->isOneOf(TT_TypeDeclarationParen, TT_RequiresExpressionLParen)) {
1550 if (
const auto *
Previous = Tok->Previous;
1553 !
Previous->isOneOf(TT_RequiresClause, TT_LeadingJavaAnnotation))) {
1554 Line.MightBeFunctionDecl =
true;
1555 Tok->MightBeFunctionDeclParen =
true;
1560 if (Style.isTableGen())
1561 Tok->setType(TT_TableGenListOpener);
1567 FormatToken *
Previous = Tok->getPreviousNonComment();
1569 Previous->setType(TT_SelectorName);
1571 Scopes.push_back(getScopeType(*Tok));
1577 Tok->setType(TT_TemplateOpener);
1585 Tok->Previous->isOneOf(TT_SelectorName, TT_DictLiteral))) {
1586 Tok->setType(TT_DictLiteral);
1587 FormatToken *
Previous = Tok->getPreviousNonComment();
1589 Previous->setType(TT_SelectorName);
1591 if (Style.isTableGen())
1592 Tok->setType(TT_TemplateOpener);
1594 Tok->setType(TT_BinaryOperator);
1595 NonTemplateLess.insert(Tok);
1605 if (!Scopes.empty())
1613 Tok->setType(TT_BinaryOperator);
1614 if (Tok->Previous && Tok->Previous->is(TT_TemplateCloser))
1615 Tok->SpacesRequiredBefore = 1;
1617 case tok::kw_operator:
1618 if (Style.isProto())
1620 while (CurrentToken &&
1621 !CurrentToken->isOneOf(tok::l_paren, tok::semi, tok::r_paren)) {
1622 if (CurrentToken->isOneOf(tok::star, tok::amp))
1623 CurrentToken->setType(TT_PointerOrReference);
1624 auto Next = CurrentToken->getNextNonComment();
1627 if (Next->is(tok::less))
1633 auto Previous = CurrentToken->getPreviousNonComment();
1635 if (CurrentToken->is(tok::comma) &&
Previous->isNot(tok::kw_operator))
1637 if (
Previous->isOneOf(TT_BinaryOperator, TT_UnaryOperator, tok::comma,
1638 tok::star, tok::arrow, tok::amp, tok::ampamp) ||
1640 Previous->TokenText.starts_with(
"\"\"")) {
1641 Previous->setType(TT_OverloadedOperator);
1642 if (CurrentToken->isOneOf(tok::less, tok::greater))
1646 if (CurrentToken && CurrentToken->is(tok::l_paren))
1647 CurrentToken->setType(TT_OverloadedOperatorLParen);
1648 if (CurrentToken && CurrentToken->Previous->is(TT_BinaryOperator))
1649 CurrentToken->Previous->setType(TT_OverloadedOperator);
1652 if (Style.isJavaScript() && Tok->Next &&
1653 Tok->Next->isOneOf(tok::semi, tok::comma, tok::colon, tok::r_paren,
1654 tok::r_brace, tok::r_square)) {
1659 Tok->setType(TT_JsTypeOptionalQuestion);
1664 if (Line.MustBeDeclaration && !Contexts.back().IsExpression &&
1665 Style.isJavaScript()) {
1668 if (Style.isCSharp()) {
1674 (Tok->Next->startsSequence(tok::question, tok::r_paren) ||
1675 Tok->Next->startsSequence(tok::question, tok::greater) ||
1676 Tok->Next->startsSequence(tok::question, tok::identifier,
1678 Tok->setType(TT_CSharpNullable);
1683 if (Tok->Next && Tok->Next->is(tok::identifier) && Tok->Next->Next &&
1684 Tok->Next->Next->is(tok::equal)) {
1685 Tok->setType(TT_CSharpNullable);
1694 if (!Contexts.back().IsExpression && Line.MustBeDeclaration &&
1696 !Tok->Next->isOneOf(tok::identifier, tok::string_literal) ||
1698 !Tok->Next->Next->isOneOf(tok::colon, tok::question))) {
1699 Tok->setType(TT_CSharpNullable);
1705 case tok::kw_template:
1706 parseTemplateDeclaration();
1709 switch (Contexts.back().ContextType) {
1710 case Context::CtorInitializer:
1711 Tok->setType(TT_CtorInitializerComma);
1713 case Context::InheritanceList:
1714 Tok->setType(TT_InheritanceComma);
1716 case Context::VerilogInstancePortList:
1717 Tok->setFinalizedType(TT_VerilogInstancePortComma);
1720 if (Style.isVerilog() && Contexts.size() == 1 &&
1721 Line.startsWith(Keywords.kw_assign)) {
1722 Tok->setFinalizedType(TT_VerilogAssignComma);
1723 }
else if (Contexts.back().FirstStartOfName &&
1724 (Contexts.size() == 1 || startsWithInitStatement(Line))) {
1725 Contexts.back().FirstStartOfName->PartOfMultiVariableDeclStmt =
true;
1726 Line.IsMultiVariableDeclStmt =
true;
1730 if (Contexts.back().ContextType == Context::ForEachMacro)
1731 Contexts.back().IsExpression =
true;
1733 case tok::kw_default:
1735 if (Style.isVerilog() && Keywords.isVerilogEndOfLabel(*Tok) &&
1736 (Line.Level > 1 || (!Line.InPPDirective && Line.Level > 0))) {
1740 case tok::identifier:
1741 if (Tok->isOneOf(Keywords.kw___has_include,
1742 Keywords.kw___has_include_next)) {
1745 if (Style.isCSharp() && Tok->is(Keywords.kw_where) && Tok->Next &&
1746 Tok->Next->isNot(tok::l_paren)) {
1747 Tok->setType(TT_CSharpGenericTypeConstraint);
1748 parseCSharpGenericTypeConstraint();
1749 if (!Tok->getPreviousNonComment())
1750 Line.IsContinuation =
true;
1752 if (Style.isTableGen()) {
1753 if (Tok->is(Keywords.kw_assert)) {
1754 if (!parseTableGenValue())
1756 }
else if (Tok->isOneOf(Keywords.kw_def, Keywords.kw_defm) &&
1758 !Tok->Next->isOneOf(tok::colon, tok::l_brace))) {
1760 if (!parseTableGenValue(
true))
1766 if (Tok->Previous && Tok->Previous->is(tok::kw_noexcept))
1767 Tok->setType(TT_TrailingReturnArrow);
1771 if (Style.isTableGen() && !parseTableGenValue())
1780 void parseCSharpGenericTypeConstraint() {
1781 int OpenAngleBracketsCount = 0;
1782 while (CurrentToken) {
1783 if (CurrentToken->is(tok::less)) {
1785 CurrentToken->setType(TT_TemplateOpener);
1786 ++OpenAngleBracketsCount;
1788 }
else if (CurrentToken->is(tok::greater)) {
1789 CurrentToken->setType(TT_TemplateCloser);
1790 --OpenAngleBracketsCount;
1792 }
else if (CurrentToken->is(tok::comma) && OpenAngleBracketsCount == 0) {
1795 CurrentToken->setType(TT_CSharpGenericTypeConstraintComma);
1797 }
else if (CurrentToken->is(Keywords.kw_where)) {
1798 CurrentToken->setType(TT_CSharpGenericTypeConstraint);
1800 }
else if (CurrentToken->is(tok::colon)) {
1801 CurrentToken->setType(TT_CSharpGenericTypeConstraintColon);
1809 void parseIncludeDirective() {
1810 if (CurrentToken && CurrentToken->is(tok::less)) {
1812 while (CurrentToken) {
1815 if (CurrentToken->isNot(tok::comment) &&
1816 !CurrentToken->TokenText.starts_with(
"//")) {
1817 CurrentToken->setType(TT_ImplicitStringLiteral);
1824 void parseWarningOrError() {
1829 while (CurrentToken) {
1830 CurrentToken->setType(TT_ImplicitStringLiteral);
1835 void parsePragma() {
1838 CurrentToken->isOneOf(Keywords.kw_mark, Keywords.kw_option,
1839 Keywords.kw_region)) {
1840 bool IsMarkOrRegion =
1841 CurrentToken->isOneOf(Keywords.kw_mark, Keywords.kw_region);
1844 while (CurrentToken) {
1845 if (IsMarkOrRegion || CurrentToken->Previous->is(TT_BinaryOperator))
1846 CurrentToken->setType(TT_ImplicitStringLiteral);
1852 void parseHasInclude() {
1853 if (!CurrentToken || CurrentToken->isNot(tok::l_paren))
1856 parseIncludeDirective();
1860 LineType parsePreprocessorDirective() {
1861 bool IsFirstToken = CurrentToken->IsFirst;
1867 if (Style.isJavaScript() && IsFirstToken) {
1871 while (CurrentToken) {
1873 CurrentToken->setType(TT_ImplicitStringLiteral);
1879 if (CurrentToken->is(tok::numeric_constant)) {
1880 CurrentToken->SpacesRequiredBefore = 1;
1885 if (!CurrentToken->Tok.getIdentifierInfo())
1889 if (Style.isVerilog() && !Keywords.isVerilogPPDirective(*CurrentToken))
1891 switch (CurrentToken->Tok.getIdentifierInfo()->getPPKeywordID()) {
1896 parseIncludeDirective();
1900 case tok::pp_warning:
1901 parseWarningOrError();
1903 case tok::pp_pragma:
1908 Contexts.back().IsExpression =
true;
1911 CurrentToken->SpacesRequiredBefore =
true;
1917 while (CurrentToken) {
1918 FormatToken *Tok = CurrentToken;
1920 if (Tok->is(tok::l_paren)) {
1922 }
else if (Tok->isOneOf(Keywords.kw___has_include,
1923 Keywords.kw___has_include_next)) {
1934 NonTemplateLess.clear();
1935 if (!Line.InMacroBody && CurrentToken->is(tok::hash)) {
1939 auto Type = parsePreprocessorDirective();
1947 IdentifierInfo *Info = CurrentToken->Tok.getIdentifierInfo();
1949 CurrentToken->is(Keywords.kw_package)) ||
1950 (!Style.isVerilog() && Info &&
1951 Info->getPPKeywordID() ==
tok::pp_import && CurrentToken->Next &&
1952 CurrentToken->Next->isOneOf(tok::string_literal, tok::identifier,
1955 parseIncludeDirective();
1961 if (CurrentToken->is(tok::less) && Line.Last->is(tok::greater)) {
1962 parseIncludeDirective();
1969 CurrentToken->isOneOf(Keywords.kw_option, Keywords.kw_package)) {
1971 if (CurrentToken && CurrentToken->is(tok::identifier)) {
1972 while (CurrentToken)
1978 bool KeywordVirtualFound =
false;
1979 bool ImportStatement =
false;
1982 if (Style.isJavaScript() && CurrentToken->is(Keywords.kw_import))
1983 ImportStatement =
true;
1985 while (CurrentToken) {
1986 if (CurrentToken->is(tok::kw_virtual))
1987 KeywordVirtualFound =
true;
1988 if (Style.isJavaScript()) {
1995 if (Line.First->is(tok::kw_export) &&
1996 CurrentToken->is(Keywords.kw_from) && CurrentToken->Next &&
1997 CurrentToken->Next->isStringLiteral()) {
1998 ImportStatement =
true;
2000 if (isClosureImportStatement(*CurrentToken))
2001 ImportStatement =
true;
2003 if (!consumeToken())
2006 if (KeywordVirtualFound)
2008 if (ImportStatement)
2011 if (Line.startsWith(TT_ObjCMethodSpecifier)) {
2012 if (Contexts.back().FirstObjCSelectorName) {
2013 Contexts.back().FirstObjCSelectorName->LongestObjCSelectorName =
2014 Contexts.back().LongestObjCSelectorName;
2019 for (
const auto &ctx : Contexts)
2020 if (ctx.ContextType == Context::StructArrayInitializer)
2027 bool isClosureImportStatement(
const FormatToken &Tok) {
2030 return Tok.TokenText ==
"goog" && Tok.Next && Tok.Next->is(tok::period) &&
2032 (Tok.Next->Next->TokenText ==
"module" ||
2033 Tok.Next->Next->TokenText ==
"provide" ||
2034 Tok.Next->Next->TokenText ==
"require" ||
2035 Tok.Next->Next->TokenText ==
"requireType" ||
2036 Tok.Next->Next->TokenText ==
"forwardDeclare") &&
2037 Tok.Next->Next->Next && Tok.Next->Next->Next->is(tok::l_paren);
2040 void resetTokenMetadata() {
2046 if (!CurrentToken->isTypeFinalized() &&
2047 !CurrentToken->isOneOf(
2048 TT_LambdaLSquare, TT_LambdaLBrace, TT_AttributeMacro, TT_IfMacro,
2049 TT_ForEachMacro, TT_TypenameMacro, TT_FunctionLBrace,
2050 TT_ImplicitStringLiteral, TT_InlineASMBrace, TT_FatArrow,
2051 TT_NamespaceMacro, TT_OverloadedOperator, TT_RegexLiteral,
2052 TT_TemplateString, TT_ObjCStringLiteral, TT_UntouchableMacroFunc,
2053 TT_StatementAttributeLikeMacro, TT_FunctionLikeOrFreestandingMacro,
2054 TT_ClassLBrace, TT_EnumLBrace, TT_RecordLBrace, TT_StructLBrace,
2055 TT_UnionLBrace, TT_RequiresClause,
2056 TT_RequiresClauseInARequiresExpression, TT_RequiresExpression,
2057 TT_RequiresExpressionLParen, TT_RequiresExpressionLBrace,
2058 TT_BracedListLBrace)) {
2059 CurrentToken->setType(TT_Unknown);
2061 CurrentToken->Role.reset();
2062 CurrentToken->MatchingParen =
nullptr;
2063 CurrentToken->FakeLParens.clear();
2064 CurrentToken->FakeRParens = 0;
2071 CurrentToken->NestingLevel = Contexts.size() - 1;
2072 CurrentToken->BindingStrength = Contexts.back().BindingStrength;
2073 modifyContext(*CurrentToken);
2074 determineTokenType(*CurrentToken);
2075 CurrentToken = CurrentToken->Next;
2077 resetTokenMetadata();
2120 StructArrayInitializer,
2124 C11GenericSelection,
2126 VerilogInstancePortList,
2132 struct ScopedContextCreator {
2133 AnnotatingParser &
P;
2139 P.Contexts.back().BindingStrength + Increase,
2140 P.Contexts.back().IsExpression));
2143 ~ScopedContextCreator() {
2145 if (
P.Contexts.back().ContextType == Context::StructArrayInitializer) {
2146 P.Contexts.pop_back();
2147 P.Contexts.back().ContextType = Context::StructArrayInitializer;
2151 P.Contexts.pop_back();
2155 void modifyContext(
const FormatToken &Current) {
2156 auto AssignmentStartsExpression = [&]() {
2160 if (Line.First->isOneOf(tok::kw_using, tok::kw_return))
2162 if (Line.First->is(tok::kw_template)) {
2163 assert(Current.Previous);
2164 if (Current.Previous->is(tok::kw_operator)) {
2170 const FormatToken *Tok = Line.First->getNextNonComment();
2172 if (Tok->isNot(TT_TemplateOpener)) {
2179 if (Contexts.back().ContextKind == tok::less) {
2180 assert(Current.Previous->Previous);
2181 return !Current.Previous->Previous->isOneOf(tok::kw_typename,
2185 Tok = Tok->MatchingParen;
2188 Tok = Tok->getNextNonComment();
2192 if (Tok->isOneOf(tok::kw_class, tok::kw_enum, tok::kw_struct,
2202 if (Style.isJavaScript() &&
2203 (Line.startsWith(Keywords.kw_type, tok::identifier) ||
2204 Line.startsWith(tok::kw_export, Keywords.kw_type,
2205 tok::identifier))) {
2209 return !Current.Previous || Current.Previous->isNot(tok::kw_operator);
2212 if (AssignmentStartsExpression()) {
2213 Contexts.back().IsExpression =
true;
2214 if (!Line.startsWith(TT_UnaryOperator)) {
2215 for (FormatToken *
Previous = Current.Previous;
2217 !
Previous->Previous->isOneOf(tok::comma, tok::semi);
2219 if (
Previous->isOneOf(tok::r_square, tok::r_paren, tok::greater)) {
2226 if (
Previous->isOneOf(TT_BinaryOperator, TT_UnaryOperator) &&
2228 Previous->Previous->isNot(tok::equal)) {
2229 Previous->setType(TT_PointerOrReference);
2233 }
else if (Current.is(tok::lessless) &&
2234 (!Current.Previous ||
2235 Current.Previous->isNot(tok::kw_operator))) {
2236 Contexts.back().IsExpression =
true;
2237 }
else if (Current.isOneOf(tok::kw_return, tok::kw_throw)) {
2238 Contexts.back().IsExpression =
true;
2239 }
else if (Current.is(TT_TrailingReturnArrow)) {
2240 Contexts.back().IsExpression =
false;
2241 }
else if (Current.is(Keywords.kw_assert)) {
2243 }
else if (Current.Previous &&
2244 Current.Previous->is(TT_CtorInitializerColon)) {
2245 Contexts.back().IsExpression =
true;
2246 Contexts.back().ContextType = Context::CtorInitializer;
2247 }
else if (Current.Previous && Current.Previous->is(TT_InheritanceColon)) {
2248 Contexts.back().ContextType = Context::InheritanceList;
2249 }
else if (Current.isOneOf(tok::r_paren, tok::greater, tok::comma)) {
2250 for (FormatToken *
Previous = Current.Previous;
2253 Previous->setType(TT_PointerOrReference);
2255 if (Line.MustBeDeclaration &&
2256 Contexts.front().ContextType != Context::CtorInitializer) {
2257 Contexts.back().IsExpression =
false;
2259 }
else if (Current.is(tok::kw_new)) {
2260 Contexts.back().CanBeExpression =
false;
2261 }
else if (Current.is(tok::semi) ||
2262 (Current.is(tok::exclaim) && Current.Previous &&
2263 Current.Previous->isNot(tok::kw_operator))) {
2267 Contexts.back().IsExpression =
true;
2271 static FormatToken *untilMatchingParen(FormatToken *Current) {
2275 if (Current->is(tok::l_paren))
2277 if (Current->is(tok::r_paren))
2281 Current = Current->Next;
2286 static bool isDeductionGuide(FormatToken &Current) {
2288 if (Current.Previous && Current.Previous->is(tok::r_paren) &&
2289 Current.startsSequence(tok::arrow, tok::identifier, tok::less)) {
2291 FormatToken *TemplateCloser = Current.Next->Next;
2292 int NestingLevel = 0;
2293 while (TemplateCloser) {
2295 if (TemplateCloser->is(tok::l_paren)) {
2297 TemplateCloser = untilMatchingParen(TemplateCloser);
2298 if (!TemplateCloser)
2301 if (TemplateCloser->is(tok::less))
2303 if (TemplateCloser->is(tok::greater))
2305 if (NestingLevel < 1)
2307 TemplateCloser = TemplateCloser->Next;
2311 if (TemplateCloser && TemplateCloser->Next &&
2312 TemplateCloser->Next->is(tok::semi) &&
2313 Current.Previous->MatchingParen) {
2316 FormatToken *LeadingIdentifier =
2317 Current.Previous->MatchingParen->Previous;
2319 return LeadingIdentifier &&
2320 LeadingIdentifier->TokenText == Current.Next->TokenText;
2326 void determineTokenType(FormatToken &Current) {
2327 if (Current.isNot(TT_Unknown)) {
2332 if ((Style.isJavaScript() || Style.isCSharp()) &&
2333 Current.is(tok::exclaim)) {
2334 if (Current.Previous) {
2336 Style.isJavaScript()
2337 ? Keywords.IsJavaScriptIdentifier(
2338 *Current.Previous,
true)
2339 : Current.Previous->is(tok::identifier);
2341 Current.Previous->isOneOf(
2342 tok::kw_default, tok::kw_namespace, tok::r_paren, tok::r_square,
2343 tok::r_brace, tok::kw_false, tok::kw_true, Keywords.kw_type,
2344 Keywords.kw_get, Keywords.kw_init, Keywords.kw_set) ||
2345 Current.Previous->Tok.isLiteral()) {
2346 Current.setType(TT_NonNullAssertion);
2351 Current.Next->isOneOf(TT_BinaryOperator, Keywords.kw_as)) {
2352 Current.setType(TT_NonNullAssertion);
2361 Current.is(Keywords.kw_instanceof)) {
2362 Current.setType(TT_BinaryOperator);
2363 }
else if (isStartOfName(Current) &&
2364 (!Line.MightBeFunctionDecl || Current.NestingLevel != 0)) {
2365 Contexts.back().FirstStartOfName = &Current;
2366 Current.setType(TT_StartOfName);
2367 }
else if (Current.is(tok::semi)) {
2371 Contexts.back().FirstStartOfName =
nullptr;
2372 }
else if (Current.isOneOf(tok::kw_auto, tok::kw___auto_type)) {
2374 }
else if (Current.is(tok::arrow) &&
2376 Current.setType(TT_TrailingReturnArrow);
2377 }
else if (Current.is(tok::arrow) && Style.isVerilog()) {
2379 Current.setType(TT_BinaryOperator);
2380 }
else if (Current.is(tok::arrow) && AutoFound &&
2381 Line.MightBeFunctionDecl && Current.NestingLevel == 0 &&
2382 !Current.Previous->isOneOf(tok::kw_operator, tok::identifier)) {
2384 Current.setType(TT_TrailingReturnArrow);
2385 }
else if (Current.is(tok::arrow) && Current.Previous &&
2386 Current.Previous->is(tok::r_brace)) {
2389 Current.setType(TT_TrailingReturnArrow);
2390 }
else if (isDeductionGuide(Current)) {
2392 Current.setType(TT_TrailingReturnArrow);
2393 }
else if (Current.isPointerOrReference()) {
2394 Current.setType(determineStarAmpUsage(
2396 Contexts.back().CanBeExpression && Contexts.back().IsExpression,
2397 Contexts.back().ContextType == Context::TemplateArgument));
2398 }
else if (Current.isOneOf(tok::minus, tok::plus, tok::caret) ||
2399 (Style.isVerilog() && Current.is(tok::pipe))) {
2400 Current.setType(determinePlusMinusCaretUsage(Current));
2401 if (Current.is(TT_UnaryOperator) && Current.is(tok::caret))
2402 Contexts.back().CaretFound =
true;
2403 }
else if (Current.isOneOf(tok::minusminus, tok::plusplus)) {
2404 Current.setType(determineIncrementUsage(Current));
2405 }
else if (Current.isOneOf(tok::exclaim, tok::tilde)) {
2406 Current.setType(TT_UnaryOperator);
2407 }
else if (Current.is(tok::question)) {
2408 if (Style.isJavaScript() && Line.MustBeDeclaration &&
2409 !Contexts.back().IsExpression) {
2412 Current.setType(TT_JsTypeOptionalQuestion);
2413 }
else if (Style.isTableGen()) {
2415 Current.setType(TT_Unknown);
2417 Current.setType(TT_ConditionalExpr);
2419 }
else if (Current.isBinaryOperator() &&
2420 (!Current.Previous || Current.Previous->isNot(tok::l_square)) &&
2421 (Current.isNot(tok::greater) &&
2423 if (Style.isVerilog()) {
2424 if (Current.is(tok::lessequal) && Contexts.size() == 1 &&
2425 !Contexts.back().VerilogAssignmentFound) {
2429 Current.setFinalizedType(TT_BinaryOperator);
2432 Contexts.back().VerilogAssignmentFound =
true;
2434 Current.setType(TT_BinaryOperator);
2435 }
else if (Current.is(tok::comment)) {
2436 if (Current.TokenText.starts_with(
"/*")) {
2437 if (Current.TokenText.ends_with(
"*/")) {
2438 Current.setType(TT_BlockComment);
2442 Current.Tok.setKind(tok::unknown);
2445 Current.setType(TT_LineComment);
2447 }
else if (Current.is(tok::string_literal)) {
2448 if (Style.isVerilog() && Contexts.back().VerilogMayBeConcatenation &&
2449 Current.getPreviousNonComment() &&
2450 Current.getPreviousNonComment()->isOneOf(tok::comma, tok::l_brace) &&
2451 Current.getNextNonComment() &&
2452 Current.getNextNonComment()->isOneOf(tok::comma, tok::r_brace)) {
2453 Current.setType(TT_StringInConcatenation);
2455 }
else if (Current.is(tok::l_paren)) {
2456 if (lParenStartsCppCast(Current))
2457 Current.setType(TT_CppCastLParen);
2458 }
else if (Current.is(tok::r_paren)) {
2459 if (rParenEndsCast(Current))
2460 Current.setType(TT_CastRParen);
2461 if (Current.MatchingParen && Current.Next &&
2462 !Current.Next->isBinaryOperator() &&
2463 !Current.Next->isOneOf(tok::semi, tok::colon, tok::l_brace,
2464 tok::comma, tok::period, tok::arrow,
2465 tok::coloncolon, tok::kw_noexcept)) {
2466 if (FormatToken *AfterParen = Current.MatchingParen->Next;
2467 AfterParen && AfterParen->isNot(tok::caret)) {
2469 if (FormatToken *BeforeParen = Current.MatchingParen->Previous;
2470 BeforeParen && BeforeParen->is(tok::identifier) &&
2471 BeforeParen->isNot(TT_TypenameMacro) &&
2472 BeforeParen->TokenText == BeforeParen->TokenText.upper() &&
2473 (!BeforeParen->Previous ||
2474 BeforeParen->Previous->ClosesTemplateDeclaration ||
2475 BeforeParen->Previous->ClosesRequiresClause)) {
2476 Current.setType(TT_FunctionAnnotationRParen);
2480 }
else if (Current.is(tok::at) && Current.Next && !Style.isJavaScript() &&
2484 switch (Current.Next->Tok.getObjCKeywordID()) {
2485 case tok::objc_interface:
2486 case tok::objc_implementation:
2487 case tok::objc_protocol:
2488 Current.setType(TT_ObjCDecl);
2490 case tok::objc_property:
2491 Current.setType(TT_ObjCProperty);
2496 }
else if (Current.is(tok::period)) {
2497 FormatToken *PreviousNoComment = Current.getPreviousNonComment();
2498 if (PreviousNoComment &&
2499 PreviousNoComment->isOneOf(tok::comma, tok::l_brace)) {
2500 Current.setType(TT_DesignatedInitializerPeriod);
2502 Current.Previous->isOneOf(TT_JavaAnnotation,
2503 TT_LeadingJavaAnnotation)) {
2504 Current.setType(Current.Previous->getType());
2506 }
else if (canBeObjCSelectorComponent(Current) &&
2509 Current.Previous && Current.Previous->is(TT_CastRParen) &&
2510 Current.Previous->MatchingParen &&
2511 Current.Previous->MatchingParen->Previous &&
2512 Current.Previous->MatchingParen->Previous->is(
2513 TT_ObjCMethodSpecifier)) {
2517 Current.setType(TT_SelectorName);
2518 }
else if (Current.isOneOf(tok::identifier, tok::kw_const, tok::kw_noexcept,
2519 tok::kw_requires) &&
2521 !Current.Previous->isOneOf(tok::equal, tok::at,
2522 TT_CtorInitializerComma,
2523 TT_CtorInitializerColon) &&
2524 Line.MightBeFunctionDecl && Contexts.size() == 1) {
2527 Current.setType(TT_TrailingAnnotation);
2529 Style.isJavaScript()) &&
2531 if (Current.Previous->is(tok::at) &&
2532 Current.isNot(Keywords.kw_interface)) {
2533 const FormatToken &AtToken = *Current.Previous;
2534 const FormatToken *
Previous = AtToken.getPreviousNonComment();
2536 Current.setType(TT_LeadingJavaAnnotation);
2538 Current.setType(TT_JavaAnnotation);
2539 }
else if (Current.Previous->is(tok::period) &&
2540 Current.Previous->isOneOf(TT_JavaAnnotation,
2541 TT_LeadingJavaAnnotation)) {
2542 Current.setType(Current.Previous->getType());
2552 bool isStartOfName(
const FormatToken &Tok) {
2554 if (Style.isVerilog())
2557 if (Tok.isNot(tok::identifier) || !Tok.Previous)
2560 if (
const auto *NextNonComment = Tok.getNextNonComment();
2561 (!NextNonComment && !Line.InMacroBody) ||
2563 (NextNonComment->isPointerOrReference() ||
2564 NextNonComment->is(tok::string_literal) ||
2565 (Line.InPragmaDirective && NextNonComment->is(tok::identifier))))) {
2569 if (Tok.Previous->isOneOf(TT_LeadingJavaAnnotation, Keywords.kw_instanceof,
2573 if (Style.isJavaScript() && Tok.Previous->is(Keywords.kw_in))
2577 FormatToken *PreviousNotConst = Tok.getPreviousNonComment();
2580 if (!Style.isJavaScript())
2581 while (PreviousNotConst && PreviousNotConst->is(tok::kw_const))
2582 PreviousNotConst = PreviousNotConst->getPreviousNonComment();
2584 if (!PreviousNotConst)
2587 if (PreviousNotConst->ClosesRequiresClause)
2590 if (Style.isTableGen()) {
2592 if (Keywords.isTableGenDefinition(*PreviousNotConst))
2595 if (Contexts.back().ContextKind != tok::l_brace)
2599 bool IsPPKeyword = PreviousNotConst->is(tok::identifier) &&
2600 PreviousNotConst->Previous &&
2601 PreviousNotConst->Previous->is(tok::hash);
2603 if (PreviousNotConst->is(TT_TemplateCloser)) {
2604 return PreviousNotConst && PreviousNotConst->MatchingParen &&
2605 PreviousNotConst->MatchingParen->Previous &&
2606 PreviousNotConst->MatchingParen->Previous->isNot(tok::period) &&
2607 PreviousNotConst->MatchingParen->Previous->isNot(tok::kw_template);
2610 if ((PreviousNotConst->is(tok::r_paren) &&
2611 PreviousNotConst->is(TT_TypeDeclarationParen)) ||
2612 PreviousNotConst->is(TT_AttributeRParen)) {
2621 if (PreviousNotConst->isOneOf(tok::identifier, tok::kw_auto))
2625 if (PreviousNotConst->is(TT_PointerOrReference))
2629 if (PreviousNotConst->isTypeName(LangOpts))
2634 PreviousNotConst->is(tok::r_square)) {
2639 return Style.isJavaScript() && PreviousNotConst->is(tok::kw_const);
2643 bool lParenStartsCppCast(
const FormatToken &Tok) {
2648 FormatToken *LeftOfParens = Tok.getPreviousNonComment();
2649 if (LeftOfParens && LeftOfParens->is(TT_TemplateCloser) &&
2650 LeftOfParens->MatchingParen) {
2651 auto *Prev = LeftOfParens->MatchingParen->getPreviousNonComment();
2653 Prev->isOneOf(tok::kw_const_cast, tok::kw_dynamic_cast,
2654 tok::kw_reinterpret_cast, tok::kw_static_cast)) {
2664 bool rParenEndsCast(
const FormatToken &Tok) {
2670 if (Tok.Previous == Tok.MatchingParen || !Tok.Next || !Tok.MatchingParen)
2673 if (Tok.MatchingParen->is(TT_OverloadedOperatorLParen))
2676 FormatToken *LeftOfParens = Tok.MatchingParen->getPreviousNonComment();
2680 if (LeftOfParens->is(tok::r_paren) &&
2681 LeftOfParens->isNot(TT_CastRParen)) {
2682 if (!LeftOfParens->MatchingParen ||
2683 !LeftOfParens->MatchingParen->Previous) {
2686 LeftOfParens = LeftOfParens->MatchingParen->Previous;
2689 if (LeftOfParens->is(tok::r_square)) {
2691 auto MayBeArrayDelete = [](FormatToken *Tok) -> FormatToken * {
2692 if (Tok->isNot(tok::r_square))
2695 Tok = Tok->getPreviousNonComment();
2696 if (!Tok || Tok->isNot(tok::l_square))
2699 Tok = Tok->getPreviousNonComment();
2700 if (!Tok || Tok->isNot(tok::kw_delete))
2704 if (FormatToken *MaybeDelete = MayBeArrayDelete(LeftOfParens))
2705 LeftOfParens = MaybeDelete;
2711 if (LeftOfParens->Tok.getIdentifierInfo() && LeftOfParens->Previous &&
2712 LeftOfParens->Previous->is(tok::kw_operator)) {
2718 if (LeftOfParens->Tok.getIdentifierInfo() &&
2719 !LeftOfParens->isOneOf(Keywords.kw_in, tok::kw_return, tok::kw_case,
2720 tok::kw_delete, tok::kw_throw)) {
2726 if (LeftOfParens->isOneOf(tok::at, tok::r_square, TT_OverloadedOperator,
2727 TT_TemplateCloser, tok::ellipsis)) {
2732 if (Tok.Next->is(tok::question) ||
2733 (Tok.Next->is(tok::ampamp) && !Tok.Previous->isTypeName(LangOpts))) {
2738 if (Tok.Next->is(Keywords.kw_in) && Style.isCSharp())
2743 if (Tok.Next->isOneOf(tok::kw_noexcept, tok::kw_volatile, tok::kw_const,
2744 tok::kw_requires, tok::kw_throw, tok::arrow,
2745 Keywords.kw_override, Keywords.kw_final) ||
2746 isCppAttribute(IsCpp, *Tok.Next)) {
2756 if (Tok.Next->isOneOf(tok::kw_sizeof, tok::kw_alignof) ||
2757 (Tok.Next->Tok.isLiteral() && Tok.Next->isNot(tok::string_literal))) {
2762 auto IsQualifiedPointerOrReference = [](FormatToken *
T,
2763 const LangOptions &LangOpts) {
2765 assert(!
T->isTypeName(LangOpts) &&
"Should have already been checked");
2769 if (
T->is(TT_AttributeRParen)) {
2771 assert(
T->is(tok::r_paren));
2772 assert(
T->MatchingParen);
2773 assert(
T->MatchingParen->is(tok::l_paren));
2774 assert(
T->MatchingParen->is(TT_AttributeLParen));
2775 if (
const auto *Tok =
T->MatchingParen->Previous;
2776 Tok && Tok->isAttribute()) {
2780 }
else if (
T->is(TT_AttributeSquare)) {
2782 if (
T->MatchingParen &&
T->MatchingParen->Previous) {
2783 T =
T->MatchingParen->Previous;
2786 }
else if (
T->canBePointerOrReferenceQualifier()) {
2792 return T &&
T->is(TT_PointerOrReference);
2794 bool ParensAreType =
2796 Tok.Previous->isOneOf(TT_TemplateCloser, TT_TypeDeclarationParen) ||
2797 Tok.Previous->isTypeName(LangOpts) ||
2798 IsQualifiedPointerOrReference(Tok.Previous, LangOpts);
2799 bool ParensCouldEndDecl =
2800 Tok.Next->isOneOf(tok::equal, tok::semi, tok::l_brace, tok::greater);
2801 if (ParensAreType && !ParensCouldEndDecl)
2812 for (
const FormatToken *Token = Tok.MatchingParen->Next; Token != &Tok;
2813 Token = Token->Next) {
2814 if (Token->is(TT_BinaryOperator))
2820 if (Tok.Next->isOneOf(tok::identifier, tok::kw_this))
2824 if (Tok.Next->is(tok::l_paren) && Tok.Previous && Tok.Previous->Previous) {
2825 if (Tok.Previous->is(tok::identifier) &&
2826 Tok.Previous->Previous->is(tok::l_paren)) {
2831 if (!Tok.Next->Next)
2837 const bool NextIsAmpOrStar = Tok.Next->isOneOf(tok::amp, tok::star);
2838 if (!(Tok.Next->isUnaryOperator() || NextIsAmpOrStar) ||
2839 Tok.Next->is(tok::plus) ||
2840 !Tok.Next->Next->isOneOf(tok::identifier, tok::numeric_constant)) {
2843 if (NextIsAmpOrStar &&
2844 (Tok.Next->Next->is(tok::numeric_constant) || Line.InPPDirective)) {
2847 if (Line.InPPDirective && Tok.Next->is(tok::minus))
2850 for (FormatToken *Prev = Tok.Previous; Prev != Tok.MatchingParen;
2851 Prev = Prev->Previous) {
2852 if (!Prev->isOneOf(tok::kw_const, tok::identifier, tok::coloncolon))
2859 bool determineUnaryOperatorByUsage(
const FormatToken &Tok) {
2860 const FormatToken *PrevToken = Tok.getPreviousNonComment();
2870 if (PrevToken->isOneOf(
2871 TT_ConditionalExpr, tok::l_paren, tok::comma, tok::colon, tok::semi,
2872 tok::equal, tok::question, tok::l_square, tok::l_brace,
2873 tok::kw_case, tok::kw_co_await, tok::kw_co_return, tok::kw_co_yield,
2874 tok::kw_delete, tok::kw_return, tok::kw_throw)) {
2881 if (PrevToken->is(tok::kw_sizeof))
2885 if (PrevToken->isOneOf(TT_CastRParen, TT_UnaryOperator))
2889 if (PrevToken->is(TT_BinaryOperator))
2897 bool InTemplateArgument) {
2898 if (Style.isJavaScript())
2899 return TT_BinaryOperator;
2902 if (Style.isCSharp() && Tok.is(tok::ampamp))
2903 return TT_BinaryOperator;
2905 if (Style.isVerilog()) {
2910 if (Tok.is(tok::star))
2911 return TT_BinaryOperator;
2912 return determineUnaryOperatorByUsage(Tok) ? TT_UnaryOperator
2913 : TT_BinaryOperator;
2916 const FormatToken *PrevToken = Tok.getPreviousNonComment();
2918 return TT_UnaryOperator;
2919 if (PrevToken->is(TT_TypeName))
2920 return TT_PointerOrReference;
2921 if (PrevToken->isOneOf(tok::kw_new, tok::kw_delete) && Tok.is(tok::ampamp))
2922 return TT_BinaryOperator;
2924 const FormatToken *NextToken = Tok.getNextNonComment();
2926 if (InTemplateArgument && NextToken && NextToken->is(tok::kw_noexcept))
2927 return TT_BinaryOperator;
2930 NextToken->isOneOf(tok::arrow, tok::equal, tok::comma, tok::r_paren,
2931 TT_RequiresClause) ||
2933 NextToken->canBePointerOrReferenceQualifier() ||
2934 (NextToken->is(tok::l_brace) && !NextToken->getNextNonComment())) {
2935 return TT_PointerOrReference;
2938 if (PrevToken->is(tok::coloncolon))
2939 return TT_PointerOrReference;
2941 if (PrevToken->is(tok::r_paren) && PrevToken->is(TT_TypeDeclarationParen))
2942 return TT_PointerOrReference;
2944 if (determineUnaryOperatorByUsage(Tok))
2945 return TT_UnaryOperator;
2947 if (NextToken->is(tok::l_square) && NextToken->isNot(TT_LambdaLSquare))
2948 return TT_PointerOrReference;
2950 return TT_PointerOrReference;
2951 if (NextToken->isOneOf(tok::comma, tok::semi))
2952 return TT_PointerOrReference;
2964 if (PrevToken->is(tok::r_brace) && Tok.is(tok::star) &&
2965 !PrevToken->MatchingParen) {
2966 return TT_PointerOrReference;
2969 if (PrevToken->endsSequence(tok::r_square, tok::l_square, tok::kw_delete))
2970 return TT_UnaryOperator;
2972 if (PrevToken->Tok.isLiteral() ||
2973 PrevToken->isOneOf(tok::r_paren, tok::r_square, tok::kw_true,
2974 tok::kw_false, tok::r_brace)) {
2975 return TT_BinaryOperator;
2978 const FormatToken *NextNonParen = NextToken;
2979 while (NextNonParen && NextNonParen->is(tok::l_paren))
2980 NextNonParen = NextNonParen->getNextNonComment();
2981 if (NextNonParen && (NextNonParen->Tok.isLiteral() ||
2982 NextNonParen->isOneOf(tok::kw_true, tok::kw_false) ||
2983 NextNonParen->isUnaryOperator())) {
2984 return TT_BinaryOperator;
2990 if (InTemplateArgument && NextToken->Tok.isAnyIdentifier())
2991 return TT_BinaryOperator;
2995 if (Tok.is(tok::ampamp) &&
2996 NextToken->isOneOf(tok::l_paren, tok::star, tok::amp)) {
2997 return TT_BinaryOperator;
3002 if (NextToken->Tok.isAnyIdentifier()) {
3003 const FormatToken *NextNextToken = NextToken->getNextNonComment();
3004 if (NextNextToken && NextNextToken->is(tok::arrow))
3005 return TT_BinaryOperator;
3011 return TT_BinaryOperator;
3014 if (!Scopes.empty() && Scopes.back() ==
ST_Class)
3015 return TT_PointerOrReference;
3018 auto IsChainedOperatorAmpOrMember = [](
const FormatToken *token) {
3019 return !token || token->isOneOf(tok::amp, tok::period, tok::arrow,
3020 tok::arrowstar, tok::periodstar);
3025 if (Tok.is(tok::amp) && PrevToken && PrevToken->Tok.isAnyIdentifier() &&
3026 IsChainedOperatorAmpOrMember(PrevToken->getPreviousNonComment()) &&
3027 NextToken && NextToken->Tok.isAnyIdentifier()) {
3028 if (
auto NextNext = NextToken->getNextNonComment();
3030 (IsChainedOperatorAmpOrMember(NextNext) || NextNext->is(tok::semi))) {
3031 return TT_BinaryOperator;
3035 return TT_PointerOrReference;
3038 TokenType determinePlusMinusCaretUsage(
const FormatToken &Tok) {
3039 if (determineUnaryOperatorByUsage(Tok))
3040 return TT_UnaryOperator;
3042 const FormatToken *PrevToken = Tok.getPreviousNonComment();
3044 return TT_UnaryOperator;
3046 if (PrevToken->is(tok::at))
3047 return TT_UnaryOperator;
3050 return TT_BinaryOperator;
3054 TokenType determineIncrementUsage(
const FormatToken &Tok) {
3055 const FormatToken *PrevToken = Tok.getPreviousNonComment();
3056 if (!PrevToken || PrevToken->is(TT_CastRParen))
3057 return TT_UnaryOperator;
3058 if (PrevToken->isOneOf(tok::r_paren, tok::r_square, tok::identifier))
3059 return TT_TrailingUnaryOperator;
3061 return TT_UnaryOperator;
3064 SmallVector<Context, 8> Contexts;
3066 const FormatStyle &Style;
3067 AnnotatedLine &Line;
3068 FormatToken *CurrentToken;
3071 LangOptions LangOpts;
3072 const AdditionalKeywords &Keywords;
3074 SmallVector<ScopeType> &Scopes;
3088 class ExpressionParser {
3090 ExpressionParser(
const FormatStyle &Style,
const AdditionalKeywords &Keywords,
3091 AnnotatedLine &Line)
3092 : Style(Style), Keywords(Keywords), Line(Line), Current(Line.
First) {}
3095 void parse(
int Precedence = 0) {
3098 while (Current && (Current->is(tok::kw_return) ||
3099 (Current->is(tok::colon) &&
3100 Current->isOneOf(TT_ObjCMethodExpr, TT_DictLiteral)))) {
3104 if (!Current || Precedence > PrecedenceArrowAndPeriod)
3109 parseConditionalExpr();
3115 if (Precedence == PrecedenceUnaryOperator) {
3116 parseUnaryOperator();
3120 FormatToken *Start = Current;
3121 FormatToken *LatestOperator =
nullptr;
3122 unsigned OperatorIndex = 0;
3124 FormatToken *VerilogFirstOfType =
nullptr;
3133 if (Style.isVerilog() && Precedence ==
prec::Comma) {
3134 VerilogFirstOfType =
3135 verilogGroupDecl(VerilogFirstOfType, LatestOperator);
3139 parse(Precedence + 1);
3141 int CurrentPrecedence = getCurrentPrecedence();
3143 if (Precedence == CurrentPrecedence && Current &&
3144 Current->is(TT_SelectorName)) {
3146 addFakeParenthesis(Start,
prec::Level(Precedence));
3150 if ((Style.isCSharp() || Style.isJavaScript() ||
3155 FormatToken *Prev = Current->getPreviousNonComment();
3156 if (Prev && Prev->is(tok::string_literal) &&
3157 (Prev == Start || Prev->endsSequence(tok::string_literal, tok::plus,
3158 TT_StringInConcatenation))) {
3159 Prev->setType(TT_StringInConcatenation);
3166 (Current->closesScope() &&
3167 (Current->MatchingParen || Current->is(TT_TemplateString))) ||
3168 (CurrentPrecedence != -1 && CurrentPrecedence < Precedence) ||
3177 if (Current->opensScope() ||
3178 Current->isOneOf(TT_RequiresClause,
3179 TT_RequiresClauseInARequiresExpression)) {
3182 while (Current && (!Current->closesScope() || Current->opensScope())) {
3189 if (CurrentPrecedence == Precedence) {
3191 LatestOperator->NextOperator = Current;
3192 LatestOperator = Current;
3193 Current->OperatorIndex = OperatorIndex;
3196 next(Precedence > 0);
3201 if (Style.isVerilog() && Precedence ==
prec::Comma && VerilogFirstOfType)
3202 addFakeParenthesis(VerilogFirstOfType,
prec::Comma);
3204 if (LatestOperator && (Current || Precedence > 0)) {
3210 Start->Previous->isOneOf(TT_RequiresClause,
3211 TT_RequiresClauseInARequiresExpression))
3213 auto Ret = Current ? Current : Line.Last;
3214 while (!
Ret->ClosesRequiresClause &&
Ret->Previous)
3220 if (Precedence == PrecedenceArrowAndPeriod) {
3232 int getCurrentPrecedence() {
3234 const FormatToken *NextNonComment = Current->getNextNonComment();
3235 if (Current->is(TT_ConditionalExpr))
3237 if (NextNonComment && Current->is(TT_SelectorName) &&
3238 (NextNonComment->isOneOf(TT_DictLiteral, TT_JsTypeColon) ||
3239 (Style.isProto() && NextNonComment->is(tok::less)))) {
3242 if (Current->is(TT_JsComputedPropertyName))
3244 if (Current->is(TT_TrailingReturnArrow))
3246 if (Current->is(TT_FatArrow))
3248 if (Current->isOneOf(tok::semi, TT_InlineASMColon, TT_SelectorName) ||
3249 (Current->is(tok::comment) && NextNonComment &&
3250 NextNonComment->is(TT_SelectorName))) {
3253 if (Current->is(TT_RangeBasedForLoopColon))
3256 Current->is(Keywords.kw_instanceof)) {
3259 if (Style.isJavaScript() &&
3260 Current->isOneOf(Keywords.kw_in, Keywords.kw_as)) {
3263 if (Current->is(TT_BinaryOperator) || Current->is(tok::comma))
3264 return Current->getPrecedence();
3265 if (Current->isOneOf(tok::period, tok::arrow) &&
3266 Current->isNot(TT_TrailingReturnArrow)) {
3267 return PrecedenceArrowAndPeriod;
3270 Current->isOneOf(Keywords.kw_extends, Keywords.kw_implements,
3271 Keywords.kw_throws)) {
3276 if (Style.isVerilog() && Current->is(tok::colon))
3282 void addFakeParenthesis(FormatToken *Start,
prec::Level Precedence,
3283 FormatToken *
End =
nullptr) {
3288 if (Start->MacroParent)
3291 Start->FakeLParens.push_back(Precedence);
3293 Start->StartsBinaryExpression =
true;
3294 if (!
End && Current)
3295 End = Current->getPreviousNonComment();
3299 End->EndsBinaryExpression =
true;
3305 void parseUnaryOperator() {
3307 while (Current && Current->is(TT_UnaryOperator)) {
3308 Tokens.push_back(Current);
3311 parse(PrecedenceArrowAndPeriod);
3312 for (FormatToken *Token : llvm::reverse(Tokens)) {
3318 void parseConditionalExpr() {
3319 while (Current && Current->isTrailingComment())
3321 FormatToken *Start = Current;
3323 if (!Current || Current->isNot(tok::question))
3327 if (!Current || Current->isNot(TT_ConditionalExpr))
3334 void next(
bool SkipPastLeadingComments =
true) {
3336 Current = Current->Next;
3338 (Current->NewlinesBefore == 0 || SkipPastLeadingComments) &&
3339 Current->isTrailingComment()) {
3340 Current = Current->Next;
3346 FormatToken *verilogGroupDecl(FormatToken *FirstOfType,
3347 FormatToken *PreviousComma) {
3351 FormatToken *Start = Current;
3354 while (Start->startsSequence(tok::l_paren, tok::star)) {
3355 if (!(Start = Start->MatchingParen) ||
3356 !(Start = Start->getNextNonComment())) {
3361 FormatToken *Tok = Start;
3363 if (Tok->is(Keywords.kw_assign))
3364 Tok = Tok->getNextNonComment();
3372 FormatToken *
First =
nullptr;
3374 FormatToken *Next = Tok->getNextNonComment();
3376 if (Tok->is(tok::hash)) {
3381 Tok = Tok->getNextNonComment();
3382 }
else if (Tok->is(tok::hashhash)) {
3386 Tok = Tok->getNextNonComment();
3387 }
else if (Keywords.isVerilogQualifier(*Tok) ||
3388 Keywords.isVerilogIdentifier(*Tok)) {
3392 while (Tok && Tok->isOneOf(tok::period, tok::coloncolon) &&
3393 (Tok = Tok->getNextNonComment())) {
3394 if (Keywords.isVerilogIdentifier(*Tok))
3395 Tok = Tok->getNextNonComment();
3399 }
else if (Tok->is(tok::l_paren)) {
3404 Keywords.kw_highz0, Keywords.kw_highz1, Keywords.kw_large,
3405 Keywords.kw_medium, Keywords.kw_pull0, Keywords.kw_pull1,
3406 Keywords.kw_small, Keywords.kw_strong0, Keywords.kw_strong1,
3407 Keywords.kw_supply0, Keywords.kw_supply1, Keywords.kw_weak0,
3408 Keywords.kw_weak1)) {
3409 Tok->setType(TT_VerilogStrength);
3410 Tok = Tok->MatchingParen;
3412 Tok->setType(TT_VerilogStrength);
3413 Tok = Tok->getNextNonComment();
3418 }
else if (Tok->is(tok::hash)) {
3419 if (Next->is(tok::l_paren))
3420 Next = Next->MatchingParen;
3422 Tok = Next->getNextNonComment();
3429 FormatToken *Second =
nullptr;
3431 while (Tok && Tok->is(tok::l_square) && (Tok = Tok->MatchingParen))
3432 Tok = Tok->getNextNonComment();
3433 if (Tok && (Tok->is(tok::hash) || Keywords.isVerilogIdentifier(*Tok)))
3438 FormatToken *TypedName =
nullptr;
3441 if (First &&
First->is(TT_Unknown))
3442 First->setType(TT_VerilogDimensionedTypeName);
3443 }
else if (First != Start) {
3451 if (TypedName->is(TT_Unknown))
3452 TypedName->setType(TT_StartOfName);
3454 if (FirstOfType && PreviousComma) {
3455 PreviousComma->setType(TT_VerilogTypeComma);
3456 addFakeParenthesis(FirstOfType,
prec::Comma, PreviousComma->Previous);
3459 FirstOfType = TypedName;
3466 while (Current && Current != FirstOfType) {
3467 if (Current->opensScope()) {
3478 const FormatStyle &Style;
3479 const AdditionalKeywords &Keywords;
3480 const AnnotatedLine &Line;
3481 FormatToken *Current;
3490 assert(Line->First);
3497 Line->First->OriginalColumn) {
3498 const bool PPDirectiveOrImportStmt =
3501 if (PPDirectiveOrImportStmt)
3507 PPDirectiveOrImportStmt
3509 : NextNonCommentLine->
Level;
3511 NextNonCommentLine = Line->First->isNot(tok::r_brace) ? Line :
nullptr;
3519 unsigned Result = 0;
3520 for (
const auto *Tok = Line.
First; Tok; Tok = Tok->
Next)
3521 Result =
std::max(Result, Tok->NestingLevel);
3529 Tok = Tok->getNextNonComment()) {
3531 if (Tok->is(tok::l_square) && Tok->is(TT_AttributeSquare)) {
3540 return Tok->
is(tok::l_paren) && Tok->isNot(TT_FunctionTypeLParen) &&
3547 if (Tok->isOneOf(tok::kw_friend, tok::kw_inline, tok::kw_virtual,
3548 tok::kw_constexpr, tok::kw_consteval, tok::kw_explicit)) {
3553 if (Tok->is(tok::coloncolon)) {
3560 while (Tok->startsSequence(tok::identifier, tok::coloncolon)) {
3562 Tok = Tok->Next->Next;
3568 if (Tok->is(tok::tilde)) {
3575 if (Tok->isNot(tok::identifier) || Tok->isNot(TT_Unknown))
3586 assert(Tok && Tok->
is(tok::identifier));
3589 if (Prev && Prev->is(tok::tilde))
3592 if (!Prev || !Prev->endsSequence(tok::coloncolon, tok::identifier))
3595 assert(Prev->Previous);
3596 return Prev->Previous->TokenText == Tok->
TokenText;
3600 AnnotatingParser
Parser(Style, Line, Keywords, Scopes);
3601 Line.Type =
Parser.parseLine();
3603 for (
auto &Child : Line.Children)
3616 ExpressionParser ExprParser(Style, Keywords, Line);
3621 if (Tok && ((!Scopes.empty() && Scopes.back() ==
ST_Class) ||
3623 Tok->setFinalizedType(TT_CtorDtorDeclName);
3627 if (Line.startsWith(TT_ObjCMethodSpecifier))
3629 else if (Line.startsWith(TT_ObjCDecl))
3631 else if (Line.startsWith(TT_ObjCProperty))
3634 auto *
First = Line.First;
3635 First->SpacesRequiredBefore = 1;
3636 First->CanBreakBefore =
First->MustBreakBefore;
3639 Style.InsertNewlineAtEOF) {
3640 First->NewlinesBefore = 1;
3652 if (Current.
is(TT_FunctionDeclarationName))
3660 if (
const auto *PrevPrev =
Previous.Previous;
3661 PrevPrev && PrevPrev->is(TT_ObjCDecl)) {
3665 auto skipOperatorName =
3667 for (; Next; Next = Next->Next) {
3668 if (Next->is(TT_OverloadedOperatorLParen))
3670 if (Next->is(TT_OverloadedOperator))
3672 if (Next->isOneOf(tok::kw_new, tok::kw_delete)) {
3675 Next->Next->startsSequence(tok::l_square, tok::r_square)) {
3676 Next = Next->Next->Next;
3680 if (Next->startsSequence(tok::l_square, tok::r_square)) {
3685 if ((Next->isTypeName(LangOpts) || Next->is(tok::identifier)) &&
3686 Next->Next && Next->Next->isPointerOrReference()) {
3691 if (Next->is(TT_TemplateOpener) && Next->MatchingParen) {
3692 Next = Next->MatchingParen;
3701 const auto *Next = Current.
Next;
3702 const bool IsCpp = LangOpts.CXXOperatorNames;
3705 if (Current.
is(tok::kw_operator)) {
3706 if (
Previous.Tok.getIdentifierInfo() &&
3707 !
Previous.isOneOf(tok::kw_return, tok::kw_co_return)) {
3712 assert(
Previous.MatchingParen->is(tok::l_paren));
3713 assert(
Previous.MatchingParen->is(TT_TypeDeclarationParen));
3718 Next = skipOperatorName(Next);
3722 for (; Next; Next = Next->Next) {
3723 if (Next->is(TT_TemplateOpener) && Next->MatchingParen) {
3724 Next = Next->MatchingParen;
3725 }
else if (Next->is(tok::coloncolon)) {
3729 if (Next->is(tok::kw_operator)) {
3730 Next = skipOperatorName(Next->Next);
3733 if (Next->isNot(tok::identifier))
3735 }
else if (isCppAttribute(IsCpp, *Next)) {
3736 Next = Next->MatchingParen;
3739 }
else if (Next->is(tok::l_paren)) {
3748 if (!Next || Next->isNot(tok::l_paren) || !Next->MatchingParen)
3750 ClosingParen = Next->MatchingParen;
3751 assert(ClosingParen->
is(tok::r_paren));
3753 if (Line.
Last->
is(tok::l_brace))
3755 if (Next->Next == ClosingParen)
3758 if (ClosingParen->
Next && ClosingParen->
Next->
is(TT_PointerOrReference))
3771 if (IsCpp && Next->Next && Next->Next->is(tok::identifier) &&
3776 for (
const FormatToken *Tok = Next->Next; Tok && Tok != ClosingParen;
3778 if (Tok->is(TT_TypeDeclarationParen))
3780 if (Tok->isOneOf(tok::l_paren, TT_TemplateOpener) && Tok->MatchingParen) {
3781 Tok = Tok->MatchingParen;
3784 if (Tok->is(tok::kw_const) || Tok->isTypeName(LangOpts) ||
3785 Tok->isOneOf(TT_PointerOrReference, TT_StartOfName, tok::ellipsis)) {
3788 if (Tok->isOneOf(tok::l_brace, TT_ObjCMethodExpr) || Tok->Tok.isLiteral())
3794 bool TokenAnnotator::mustBreakForReturnType(
const AnnotatedLine &Line)
const {
3795 assert(Line.MightBeFunctionDecl);
3803 switch (Style.BreakAfterReturnType) {
3813 return Line.mightBeFunctionDefinition();
3823 auto *
First = Line.First;
3826 : Line.FirstStartColumn +
First->ColumnWidth;
3828 bool InFunctionDecl = Line.MightBeFunctionDecl;
3829 bool AlignArrayOfStructures =
3832 if (AlignArrayOfStructures)
3833 calculateArrayInitializerColumnList(Line);
3835 bool SeenName =
false;
3836 bool LineIsFunctionDeclaration =
false;
3840 for (
auto *Tok = Current; Tok; Tok = Tok->Next) {
3841 if (Tok->is(TT_StartOfName))
3843 if (Tok->Previous->EndsCppAttributeGroup)
3844 AfterLastAttribute = Tok;
3845 if (
const bool IsCtorOrDtor = Tok->is(TT_CtorDtorDeclName);
3850 LineIsFunctionDeclaration =
true;
3856 if (IsCpp && (LineIsFunctionDeclaration ||
First->is(TT_CtorDtorDeclName)) &&
3857 Line.endsWith(tok::semi, tok::r_brace)) {
3858 auto *Tok = Line.Last->Previous;
3859 while (Tok->isNot(tok::r_brace))
3860 Tok = Tok->Previous;
3861 if (
auto *LBrace = Tok->MatchingParen; LBrace) {
3862 assert(LBrace->is(tok::l_brace));
3865 LBrace->setFinalizedType(TT_FunctionLBrace);
3869 if (IsCpp && SeenName && AfterLastAttribute &&
3872 if (LineIsFunctionDeclaration)
3873 Line.ReturnTypeWrapped =
true;
3877 if (!LineIsFunctionDeclaration) {
3879 for (
const auto *Tok =
First; Tok; Tok = Tok->Next) {
3880 if (Tok->isNot(tok::kw_operator))
3884 }
while (Tok && Tok->isNot(TT_OverloadedOperatorLParen));
3885 if (!Tok || !Tok->MatchingParen)
3887 const auto *LeftParen = Tok;
3888 for (Tok = Tok->Next; Tok && Tok != LeftParen->MatchingParen;
3890 if (Tok->isNot(tok::identifier))
3892 auto *Next = Tok->Next;
3893 const bool NextIsBinaryOperator =
3894 Next && Next->isPointerOrReference() && Next->Next &&
3895 Next->Next->is(tok::identifier);
3896 if (!NextIsBinaryOperator)
3898 Next->setType(TT_BinaryOperator);
3902 }
else if (ClosingParen) {
3903 for (
auto *Tok = ClosingParen->
Next; Tok; Tok = Tok->
Next) {
3904 if (Tok->is(TT_CtorInitializerColon))
3906 if (Tok->is(tok::arrow)) {
3907 Tok->setType(TT_TrailingReturnArrow);
3910 if (Tok->isNot(TT_TrailingAnnotation))
3912 const auto *Next = Tok->Next;
3913 if (!Next || Next->isNot(tok::l_paren))
3915 Tok = Next->MatchingParen;
3924 if (Current->is(TT_LineComment)) {
3926 Current->SpacesRequiredBefore =
3927 (Style.Cpp11BracedListStyle && !Style.SpacesInParensOptions.Other)
3930 }
else if (Prev->
is(TT_VerilogMultiLineListLParen)) {
3931 Current->SpacesRequiredBefore = 0;
3933 Current->SpacesRequiredBefore = Style.SpacesBeforeTrailingComments;
3943 if (!Current->HasUnescapedNewline) {
3946 if (
Parameter->isOneOf(tok::comment, tok::r_brace))
3949 if (
Parameter->Previous->isNot(TT_CtorInitializerComma) &&
3957 }
else if (!Current->Finalized && Current->SpacesRequiredBefore == 0 &&
3958 spaceRequiredBefore(Line, *Current)) {
3959 Current->SpacesRequiredBefore = 1;
3962 const auto &Children = Prev->
Children;
3963 if (!Children.empty() && Children.back()->Last->is(TT_LineComment)) {
3964 Current->MustBreakBefore =
true;
3966 Current->MustBreakBefore =
3967 Current->MustBreakBefore || mustBreakBefore(Line, *Current);
3968 if (!Current->MustBreakBefore && InFunctionDecl &&
3969 Current->is(TT_FunctionDeclarationName)) {
3970 Current->MustBreakBefore = mustBreakForReturnType(Line);
3974 Current->CanBreakBefore =
3975 Current->MustBreakBefore || canBreakBefore(Line, *Current);
3976 unsigned ChildSize = 0;
3982 if (Current->MustBreakBefore || Prev->
Children.size() > 1 ||
3984 Prev->
Children[0]->First->MustBreakBefore) ||
3985 Current->IsMultiline) {
3986 Current->TotalLength = Prev->
TotalLength + Style.ColumnLimit;
3988 Current->TotalLength = Prev->
TotalLength + Current->ColumnWidth +
3989 ChildSize + Current->SpacesRequiredBefore;
3992 if (Current->is(TT_CtorInitializerColon))
3993 InFunctionDecl =
false;
4004 Current->SplitPenalty = splitPenalty(Line, *Current, InFunctionDecl);
4006 Current->is(TT_SelectorName) && Current->ParameterIndex > 0) {
4007 if (Current->ParameterIndex == 1)
4008 Current->SplitPenalty += 5 * Current->BindingStrength;
4010 Current->SplitPenalty += 20 * Current->BindingStrength;
4013 Current = Current->Next;
4016 calculateUnbreakableTailLengths(Line);
4017 unsigned IndentLevel = Line.Level;
4018 for (Current =
First; Current; Current = Current->Next) {
4020 Current->Role->precomputeFormattingInfos(Current);
4021 if (Current->MatchingParen &&
4022 Current->MatchingParen->opensBlockOrBlockTypeList(Style) &&
4026 Current->IndentLevel = IndentLevel;
4027 if (Current->opensBlockOrBlockTypeList(Style))
4031 LLVM_DEBUG({ printDebugInfo(Line); });
4034 void TokenAnnotator::calculateUnbreakableTailLengths(
4036 unsigned UnbreakableTailLength = 0;
4039 Current->UnbreakableTailLength = UnbreakableTailLength;
4040 if (Current->CanBreakBefore ||
4041 Current->isOneOf(tok::comment, tok::string_literal)) {
4042 UnbreakableTailLength = 0;
4044 UnbreakableTailLength +=
4045 Current->ColumnWidth + Current->SpacesRequiredBefore;
4047 Current = Current->Previous;
4051 void TokenAnnotator::calculateArrayInitializerColumnList(
4052 AnnotatedLine &Line)
const {
4053 if (Line.First == Line.Last)
4055 auto *CurrentToken = Line.First;
4056 CurrentToken->ArrayInitializerLineStart =
true;
4058 while (CurrentToken && CurrentToken != Line.Last) {
4059 if (CurrentToken->is(tok::l_brace)) {
4060 CurrentToken->IsArrayInitializer =
true;
4061 if (CurrentToken->Next)
4062 CurrentToken->Next->MustBreakBefore =
true;
4064 calculateInitializerColumnList(Line, CurrentToken->Next,
Depth + 1);
4066 CurrentToken = CurrentToken->Next;
4071 FormatToken *TokenAnnotator::calculateInitializerColumnList(
4072 AnnotatedLine &Line, FormatToken *CurrentToken,
unsigned Depth)
const {
4073 while (CurrentToken && CurrentToken != Line.Last) {
4074 if (CurrentToken->is(tok::l_brace))
4076 else if (CurrentToken->is(tok::r_brace))
4078 if (
Depth == 2 && CurrentToken->isOneOf(tok::l_brace, tok::comma)) {
4079 CurrentToken = CurrentToken->Next;
4082 CurrentToken->StartsColumn =
true;
4083 CurrentToken = CurrentToken->Previous;
4085 CurrentToken = CurrentToken->Next;
4087 return CurrentToken;
4090 unsigned TokenAnnotator::splitPenalty(
const AnnotatedLine &Line,
4091 const FormatToken &Tok,
4092 bool InFunctionDecl)
const {
4093 const FormatToken &Left = *Tok.Previous;
4094 const FormatToken &Right = Tok;
4096 if (Left.is(tok::semi))
4101 if (Right.isOneOf(Keywords.kw_extends, Keywords.kw_throws))
4103 if (Right.is(Keywords.kw_implements))
4105 if (Left.is(tok::comma) && Left.NestingLevel == 0)
4107 }
else if (Style.isJavaScript()) {
4108 if (Right.is(Keywords.kw_function) && Left.isNot(tok::comma))
4110 if (Left.is(TT_JsTypeColon))
4112 if ((Left.is(TT_TemplateString) && Left.TokenText.ends_with(
"${")) ||
4113 (Right.is(TT_TemplateString) && Right.TokenText.starts_with(
"}"))) {
4117 if (Left.opensScope() && Right.closesScope())
4120 if (Right.is(tok::l_square))
4122 if (Right.is(tok::period))
4126 if (Right.is(tok::identifier) && Right.Next && Right.Next->is(TT_DictLiteral))
4128 if (Right.is(tok::l_square)) {
4129 if (Left.is(tok::r_square))
4132 if (Right.is(TT_LambdaLSquare) && Left.is(tok::equal))
4134 if (!Right.isOneOf(TT_ObjCMethodExpr, TT_LambdaLSquare,
4135 TT_ArrayInitializerLSquare,
4136 TT_DesignatedInitializerLSquare, TT_AttributeSquare)) {
4141 if (Left.is(tok::coloncolon))
4142 return Style.PenaltyBreakScopeResolution;
4143 if (Right.isOneOf(TT_StartOfName, TT_FunctionDeclarationName) ||
4144 Right.is(tok::kw_operator)) {
4145 if (Line.startsWith(tok::kw_for) && Right.PartOfMultiVariableDeclStmt)
4147 if (Left.is(TT_StartOfName))
4149 if (InFunctionDecl && Right.NestingLevel == 0)
4150 return Style.PenaltyReturnTypeOnItsOwnLine;
4153 if (Right.is(TT_PointerOrReference))
4155 if (Right.is(TT_TrailingReturnArrow))
4157 if (Left.is(tok::equal) && Right.is(tok::l_brace))
4159 if (Left.is(TT_CastRParen))
4161 if (Left.isOneOf(tok::kw_class, tok::kw_struct, tok::kw_union))
4163 if (Left.is(tok::comment))
4166 if (Left.isOneOf(TT_RangeBasedForLoopColon, TT_InheritanceColon,
4167 TT_CtorInitializerColon)) {
4171 if (Right.isMemberAccess()) {
4191 return !Right.NextOperator || !Right.NextOperator->Previous->closesScope()
4196 if (Right.is(TT_TrailingAnnotation) &&
4197 (!Right.Next || Right.Next->isNot(tok::l_paren))) {
4200 if (Line.startsWith(TT_ObjCMethodSpecifier))
4207 bool is_short_annotation = Right.TokenText.size() < 10;
4208 return (Left.is(tok::r_paren) ? 100 : 120) + (is_short_annotation ? 50 : 0);
4212 if (Line.startsWith(tok::kw_for) && Left.is(tok::equal))
4217 if (Right.is(TT_SelectorName))
4219 if (Left.is(tok::colon) && Left.is(TT_ObjCMethodExpr))
4220 return Line.MightBeFunctionDecl ? 50 : 500;
4225 if (Line.Type ==
LT_ObjCDecl && Left.is(tok::l_paren) && Left.Previous &&
4226 Left.Previous->isOneOf(tok::identifier, tok::greater)) {
4230 if (Left.is(tok::l_paren) && Style.PenaltyBreakOpenParenthesis != 0)
4231 return Style.PenaltyBreakOpenParenthesis;
4232 if (Left.is(tok::l_paren) && InFunctionDecl &&
4236 if (Left.is(tok::l_paren) && Left.Previous &&
4237 (Left.Previous->isOneOf(tok::kw_for, tok::kw__Generic) ||
4238 Left.Previous->isIf())) {
4241 if (Left.is(tok::equal) && InFunctionDecl)
4243 if (Right.is(tok::r_brace))
4245 if (Left.is(TT_TemplateOpener))
4247 if (Left.opensScope()) {
4252 (Left.ParameterCount <= 1 || Style.AllowAllArgumentsOnNextLine)) {
4255 if (Left.is(tok::l_brace) && !Style.Cpp11BracedListStyle)
4257 return Left.ParameterCount > 1 ? Style.PenaltyBreakBeforeFirstCallParameter
4260 if (Left.is(TT_JavaAnnotation))
4263 if (Left.is(TT_UnaryOperator))
4265 if (Left.isOneOf(tok::plus, tok::comma) && Left.Previous &&
4266 Left.Previous->isLabelString() &&
4267 (Left.NextOperator || Left.OperatorIndex != 0)) {
4270 if (Right.is(tok::plus) && Left.isLabelString() &&
4271 (Right.NextOperator || Right.OperatorIndex != 0)) {
4274 if (Left.is(tok::comma))
4276 if (Right.is(tok::lessless) && Left.isLabelString() &&
4277 (Right.NextOperator || Right.OperatorIndex != 1)) {
4280 if (Right.is(tok::lessless)) {
4282 if (Left.isNot(tok::r_paren) || Right.OperatorIndex > 0) {
4288 if (Left.ClosesTemplateDeclaration)
4289 return Style.PenaltyBreakTemplateDeclaration;
4290 if (Left.ClosesRequiresClause)
4292 if (Left.is(TT_ConditionalExpr))
4296 Level = Right.getPrecedence();
4298 return Style.PenaltyBreakAssignment;
4305 bool TokenAnnotator::spaceRequiredBeforeParens(
const FormatToken &Right)
const {
4308 if (Right.is(TT_OverloadedOperatorLParen) &&
4309 Style.SpaceBeforeParensOptions.AfterOverloadedOperator) {
4312 if (Style.SpaceBeforeParensOptions.BeforeNonEmptyParentheses &&
4313 Right.ParameterCount > 0) {
4319 bool TokenAnnotator::spaceRequiredBetween(
const AnnotatedLine &Line,
4320 const FormatToken &Left,
4321 const FormatToken &Right)
const {
4322 if (Left.is(tok::kw_return) &&
4323 !Right.isOneOf(tok::semi, tok::r_paren, tok::hashhash)) {
4326 if (Left.is(tok::kw_throw) && Right.is(tok::l_paren) && Right.MatchingParen &&
4327 Right.MatchingParen->is(TT_CastRParen)) {
4333 Left.Tok.getObjCKeywordID() == tok::objc_property) {
4336 if (Right.is(tok::hashhash))
4337 return Left.is(tok::hash);
4338 if (Left.isOneOf(tok::hashhash, tok::hash))
4339 return Right.is(tok::hash);
4340 if (Left.is(
BK_Block) && Right.is(tok::r_brace) &&
4341 Right.MatchingParen == &Left && Line.Children.empty()) {
4342 return Style.SpaceInEmptyBlock;
4344 if ((Left.is(tok::l_paren) && Right.is(tok::r_paren)) ||
4345 (Left.is(tok::l_brace) && Left.isNot(
BK_Block) &&
4346 Right.is(tok::r_brace) && Right.isNot(
BK_Block))) {
4347 return Style.SpacesInParensOptions.InEmptyParentheses;
4349 if (Style.SpacesInParensOptions.InConditionalStatements) {
4350 const FormatToken *LeftParen =
nullptr;
4351 if (Left.is(tok::l_paren))
4353 else if (Right.is(tok::r_paren) && Right.MatchingParen)
4354 LeftParen = Right.MatchingParen;
4356 if (LeftParen->is(TT_ConditionLParen))
4358 if (LeftParen->Previous && isKeywordWithCondition(*LeftParen->Previous))
4364 if (Left.is(tok::kw_auto) && Right.isOneOf(TT_LambdaLBrace, TT_FunctionLBrace,
4366 TT_FunctionTypeLParen)) {
4371 if (Left.is(tok::kw_auto) && Right.isOneOf(tok::l_paren, tok::l_brace))
4374 const auto *BeforeLeft = Left.Previous;
4377 if (Right.is(tok::l_paren) && Left.is(tok::kw_co_await) && BeforeLeft &&
4378 BeforeLeft->is(tok::kw_operator)) {
4382 if (Left.isOneOf(tok::kw_co_await, tok::kw_co_yield, tok::kw_co_return) &&
4383 !Right.isOneOf(tok::semi, tok::r_paren)) {
4387 if (Left.is(tok::l_paren) || Right.is(tok::r_paren)) {
4388 return (Right.is(TT_CastRParen) ||
4389 (Left.MatchingParen && Left.MatchingParen->is(TT_CastRParen)))
4390 ? Style.SpacesInParensOptions.InCStyleCasts
4391 : Style.SpacesInParensOptions.Other;
4393 if (Right.isOneOf(tok::semi, tok::comma))
4395 if (Right.is(tok::less) && Line.Type ==
LT_ObjCDecl) {
4396 bool IsLightweightGeneric = Right.MatchingParen &&
4397 Right.MatchingParen->Next &&
4398 Right.MatchingParen->Next->is(tok::colon);
4399 return !IsLightweightGeneric && Style.ObjCSpaceBeforeProtocolList;
4401 if (Right.is(tok::less) && Left.is(tok::kw_template))
4402 return Style.SpaceAfterTemplateKeyword;
4403 if (Left.isOneOf(tok::exclaim, tok::tilde))
4405 if (Left.is(tok::at) &&
4406 Right.isOneOf(tok::identifier, tok::string_literal, tok::char_constant,
4407 tok::numeric_constant, tok::l_paren, tok::l_brace,
4408 tok::kw_true, tok::kw_false)) {
4411 if (Left.is(tok::colon))
4412 return Left.isNot(TT_ObjCMethodExpr);
4413 if (Left.is(tok::coloncolon)) {
4414 return Right.is(tok::star) && Right.is(TT_PointerOrReference) &&
4417 if (Left.is(tok::less) || Right.isOneOf(tok::greater, tok::less)) {
4420 (Left.is(TT_DictLiteral) || Right.is(TT_DictLiteral)))) {
4422 if (Left.is(tok::less) && Right.is(tok::greater))
4424 return !Style.Cpp11BracedListStyle;
4427 if (Right.isNot(TT_OverloadedOperatorLParen))
4430 if (Right.is(tok::ellipsis)) {
4431 return Left.Tok.isLiteral() || (Left.is(tok::identifier) && BeforeLeft &&
4432 BeforeLeft->is(tok::kw_case));
4434 if (Left.is(tok::l_square) && Right.is(tok::amp))
4435 return Style.SpacesInSquareBrackets;
4436 if (Right.is(TT_PointerOrReference)) {
4437 if (Left.is(tok::r_paren) && Line.MightBeFunctionDecl) {
4438 if (!Left.MatchingParen)
4440 FormatToken *TokenBeforeMatchingParen =
4441 Left.MatchingParen->getPreviousNonComment();
4442 if (!TokenBeforeMatchingParen || Left.isNot(TT_TypeDeclarationParen))
4450 (Left.is(TT_AttributeRParen) ||
4451 Left.canBePointerOrReferenceQualifier())) {
4454 if (Left.Tok.isLiteral())
4457 if (Left.isTypeOrIdentifier(LangOpts) && Right.Next && Right.Next->Next &&
4458 Right.Next->Next->is(TT_RangeBasedForLoopColon)) {
4459 return getTokenPointerOrReferenceAlignment(Right) !=
4462 return !Left.isOneOf(TT_PointerOrReference, tok::l_paren) &&
4463 (getTokenPointerOrReferenceAlignment(Right) !=
4465 (Line.IsMultiVariableDeclStmt &&
4466 (Left.NestingLevel == 0 ||
4467 (Left.NestingLevel == 1 && startsWithInitStatement(Line)))));
4469 if (Right.is(TT_FunctionTypeLParen) && Left.isNot(tok::l_paren) &&
4470 (Left.isNot(TT_PointerOrReference) ||
4472 !Line.IsMultiVariableDeclStmt))) {
4475 if (Left.is(TT_PointerOrReference)) {
4480 Right.canBePointerOrReferenceQualifier()) {
4484 if (Right.Tok.isLiteral())
4487 if (Right.is(TT_BlockComment))
4491 if (Right.isOneOf(Keywords.kw_override, Keywords.kw_final, tok::kw_noexcept,
4492 TT_RequiresClause) &&
4493 Right.isNot(TT_StartOfName)) {
4497 if (Right.is(tok::l_brace) && Right.is(
BK_Block))
4500 if (BeforeLeft && BeforeLeft->isTypeOrIdentifier(LangOpts) && Right.Next &&
4501 Right.Next->is(TT_RangeBasedForLoopColon)) {
4502 return getTokenPointerOrReferenceAlignment(Left) !=
4505 if (Right.isOneOf(TT_PointerOrReference, TT_ArraySubscriptLSquare,
4517 if (Line.IsMultiVariableDeclStmt &&
4518 (Left.NestingLevel == Line.First->NestingLevel ||
4519 ((Left.NestingLevel == Line.First->NestingLevel + 1) &&
4520 startsWithInitStatement(Line)))) {
4525 if (BeforeLeft->is(tok::coloncolon)) {
4526 return Left.is(tok::star) &&
4529 return !BeforeLeft->isOneOf(tok::l_paren, tok::l_square);
4532 if (Left.is(tok::ellipsis) && BeforeLeft &&
4533 BeforeLeft->isPointerOrReference()) {
4537 if (Right.is(tok::star) && Left.is(tok::l_paren))
4539 if (Left.is(tok::star) && Right.isPointerOrReference())
4541 if (Right.isPointerOrReference()) {
4542 const FormatToken *
Previous = &Left;
4552 if (
Previous->is(tok::coloncolon)) {
4571 if (
Previous->endsSequence(tok::kw_operator))
4575 (Style.SpaceAroundPointerQualifiers ==
4581 if (Style.isCSharp() && Left.is(Keywords.kw_is) && Right.is(tok::l_square))
4583 const auto SpaceRequiredForArrayInitializerLSquare =
4584 [](
const FormatToken &LSquareTok,
const FormatStyle &Style) {
4585 return Style.SpacesInContainerLiterals ||
4586 (Style.isProto() && !Style.Cpp11BracedListStyle &&
4587 LSquareTok.endsSequence(tok::l_square, tok::colon,
4590 if (Left.is(tok::l_square)) {
4591 return (Left.is(TT_ArrayInitializerLSquare) && Right.isNot(tok::r_square) &&
4592 SpaceRequiredForArrayInitializerLSquare(Left, Style)) ||
4593 (Left.isOneOf(TT_ArraySubscriptLSquare, TT_StructuredBindingLSquare,
4594 TT_LambdaLSquare) &&
4595 Style.SpacesInSquareBrackets && Right.isNot(tok::r_square));
4597 if (Right.is(tok::r_square)) {
4598 return Right.MatchingParen &&
4599 ((Right.MatchingParen->is(TT_ArrayInitializerLSquare) &&
4600 SpaceRequiredForArrayInitializerLSquare(*Right.MatchingParen,
4602 (Style.SpacesInSquareBrackets &&
4603 Right.MatchingParen->isOneOf(TT_ArraySubscriptLSquare,
4604 TT_StructuredBindingLSquare,
4605 TT_LambdaLSquare)));
4607 if (Right.is(tok::l_square) &&
4608 !Right.isOneOf(TT_ObjCMethodExpr, TT_LambdaLSquare,
4609 TT_DesignatedInitializerLSquare,
4610 TT_StructuredBindingLSquare, TT_AttributeSquare) &&
4611 !Left.isOneOf(tok::numeric_constant, TT_DictLiteral) &&
4612 !(Left.isNot(tok::r_square) && Style.SpaceBeforeSquareBrackets &&
4613 Right.is(TT_ArraySubscriptLSquare))) {
4616 if (Left.is(tok::l_brace) && Right.is(tok::r_brace))
4617 return !Left.Children.empty();
4618 if ((Left.is(tok::l_brace) && Left.isNot(
BK_Block)) ||
4619 (Right.is(tok::r_brace) && Right.MatchingParen &&
4620 Right.MatchingParen->isNot(
BK_Block))) {
4621 return !Style.Cpp11BracedListStyle || Style.SpacesInParensOptions.Other;
4623 if (Left.is(TT_BlockComment)) {
4625 return Style.isJavaScript() || !Left.TokenText.ends_with(
"=*/");
4630 if (Left.is(TT_TemplateCloser) && Right.is(TT_AttributeSquare))
4633 if (Right.is(tok::l_paren)) {
4634 if (Left.is(TT_TemplateCloser) && Right.isNot(TT_FunctionTypeLParen))
4635 return spaceRequiredBeforeParens(Right);
4636 if (Left.isOneOf(TT_RequiresClause,
4637 TT_RequiresClauseInARequiresExpression)) {
4638 return Style.SpaceBeforeParensOptions.AfterRequiresInClause ||
4639 spaceRequiredBeforeParens(Right);
4641 if (Left.is(TT_RequiresExpression)) {
4642 return Style.SpaceBeforeParensOptions.AfterRequiresInExpression ||
4643 spaceRequiredBeforeParens(Right);
4645 if (Left.is(TT_AttributeRParen) ||
4646 (Left.is(tok::r_square) && Left.is(TT_AttributeSquare))) {
4649 if (Left.is(TT_ForEachMacro)) {
4650 return Style.SpaceBeforeParensOptions.AfterForeachMacros ||
4651 spaceRequiredBeforeParens(Right);
4653 if (Left.is(TT_IfMacro)) {
4654 return Style.SpaceBeforeParensOptions.AfterIfMacros ||
4655 spaceRequiredBeforeParens(Right);
4658 Left.isOneOf(tok::kw_new, tok::kw_delete) &&
4659 Right.isNot(TT_OverloadedOperatorLParen) &&
4660 !(Line.MightBeFunctionDecl && Left.is(TT_FunctionDeclarationName))) {
4661 return Style.SpaceBeforeParensOptions.AfterPlacementOperator;
4665 if (Left.is(tok::semi))
4667 if (Left.isOneOf(
tok::pp_elif, tok::kw_for, tok::kw_while, tok::kw_switch,
4668 tok::kw_case, TT_ForEachMacro, TT_ObjCForIn) ||
4670 Right.is(TT_ConditionLParen)) {
4671 return Style.SpaceBeforeParensOptions.AfterControlStatements ||
4672 spaceRequiredBeforeParens(Right);
4677 if (Right.is(TT_OverloadedOperatorLParen))
4678 return spaceRequiredBeforeParens(Right);
4680 if (Line.MightBeFunctionDecl && (Left.is(TT_FunctionDeclarationName))) {
4681 if (Line.mightBeFunctionDefinition()) {
4682 return Style.SpaceBeforeParensOptions.AfterFunctionDefinitionName ||
4683 spaceRequiredBeforeParens(Right);
4685 return Style.SpaceBeforeParensOptions.AfterFunctionDeclarationName ||
4686 spaceRequiredBeforeParens(Right);
4691 Left.MatchingParen && Left.MatchingParen->is(TT_LambdaLSquare)) {
4692 return Style.SpaceBeforeParensOptions.AfterFunctionDefinitionName ||
4693 spaceRequiredBeforeParens(Right);
4695 if (!BeforeLeft || !BeforeLeft->isOneOf(tok::period, tok::arrow)) {
4696 if (Left.isOneOf(tok::kw_try, Keywords.kw___except, tok::kw_catch)) {
4697 return Style.SpaceBeforeParensOptions.AfterControlStatements ||
4698 spaceRequiredBeforeParens(Right);
4700 if (Left.isOneOf(tok::kw_new, tok::kw_delete)) {
4701 return ((!Line.MightBeFunctionDecl || !BeforeLeft) &&
4703 spaceRequiredBeforeParens(Right);
4706 if (Left.is(tok::r_square) && Left.MatchingParen &&
4707 Left.MatchingParen->Previous &&
4708 Left.MatchingParen->Previous->is(tok::kw_delete)) {
4710 spaceRequiredBeforeParens(Right);
4715 (Left.Tok.getIdentifierInfo() || Left.is(tok::r_paren))) {
4716 return spaceRequiredBeforeParens(Right);
4720 if (Left.is(tok::at) && Right.Tok.getObjCKeywordID() != tok::objc_not_keyword)
4722 if (Right.is(TT_UnaryOperator)) {
4723 return !Left.isOneOf(tok::l_paren, tok::l_square, tok::at) &&
4724 (Left.isNot(tok::colon) || Left.isNot(TT_ObjCMethodExpr));
4730 if (!Style.isVerilog() &&
4731 (Left.isOneOf(tok::identifier, tok::greater, tok::r_square,
4733 Left.isTypeName(LangOpts)) &&
4734 Right.is(tok::l_brace) && Right.getNextNonComment() &&
4738 if (Left.is(tok::period) || Right.is(tok::period))
4742 if (Right.is(tok::hash) && Left.is(tok::identifier) &&
4743 (Left.TokenText ==
"L" || Left.TokenText ==
"u" ||
4744 Left.TokenText ==
"U" || Left.TokenText ==
"u8" ||
4745 Left.TokenText ==
"LR" || Left.TokenText ==
"uR" ||
4746 Left.TokenText ==
"UR" || Left.TokenText ==
"u8R")) {
4749 if (Left.is(TT_TemplateCloser) && Left.MatchingParen &&
4750 Left.MatchingParen->Previous &&
4751 (Left.MatchingParen->Previous->is(tok::period) ||
4752 Left.MatchingParen->Previous->is(tok::coloncolon))) {
4758 if (Left.is(TT_TemplateCloser) && Right.is(tok::l_square))
4760 if (Left.is(tok::l_brace) && Left.endsSequence(TT_DictLiteral, tok::at)) {
4764 if (Right.is(tok::r_brace) && Right.MatchingParen &&
4765 Right.MatchingParen->endsSequence(TT_DictLiteral, tok::at)) {
4769 if (Right.is(TT_TrailingAnnotation) && Right.isOneOf(tok::amp, tok::ampamp) &&
4770 Left.isOneOf(tok::kw_const, tok::kw_volatile) &&
4771 (!Right.Next || Right.Next->is(tok::semi))) {
4781 bool TokenAnnotator::spaceRequiredBefore(
const AnnotatedLine &Line,
4782 const FormatToken &Right)
const {
4783 const FormatToken &Left = *Right.Previous;
4788 return Right.hasWhitespaceBefore();
4790 const bool IsVerilog = Style.isVerilog();
4791 assert(!IsVerilog || !IsCpp);
4794 if (Keywords.isWordLike(Right, IsVerilog) &&
4795 Keywords.isWordLike(Left, IsVerilog)) {
4801 if (Left.is(tok::star) && Right.is(tok::comment))
4805 if (Left.is(TT_OverloadedOperator) &&
4806 Right.isOneOf(TT_TemplateOpener, TT_TemplateCloser)) {
4810 if (Right.is(tok::period) && Left.is(tok::numeric_constant))
4814 if (Left.is(Keywords.kw_import) && Right.isOneOf(tok::less, tok::ellipsis))
4817 if (Left.isOneOf(Keywords.kw_module, Keywords.kw_import) &&
4818 Right.is(TT_ModulePartitionColon)) {
4822 if (Left.is(tok::identifier) && Right.is(TT_ModulePartitionColon))
4825 if (Left.is(TT_ModulePartitionColon) &&
4826 Right.isOneOf(tok::identifier, tok::kw_private)) {
4829 if (Left.is(tok::ellipsis) && Right.is(tok::identifier) &&
4830 Line.First->is(Keywords.kw_import)) {
4834 if (Left.isOneOf(TT_AttributeRParen, TT_AttributeMacro) &&
4835 Right.is(tok::coloncolon)) {
4839 if (Left.is(tok::kw_operator))
4840 return Right.is(tok::coloncolon);
4842 !Left.opensScope() && Style.SpaceBeforeCpp11BracedList) {
4845 if (Left.is(tok::less) && Left.is(TT_OverloadedOperator) &&
4846 Right.is(TT_TemplateOpener)) {
4849 if (Left.Tok.getIdentifierInfo() && Right.is(tok::numeric_constant))
4850 return Right.TokenText[0] !=
'.';
4851 }
else if (Style.isProto()) {
4852 if (Right.is(tok::period) &&
4853 Left.isOneOf(Keywords.kw_optional, Keywords.kw_required,
4854 Keywords.kw_repeated, Keywords.kw_extend)) {
4857 if (Right.is(tok::l_paren) &&
4858 Left.isOneOf(Keywords.kw_returns, Keywords.kw_option)) {
4861 if (Right.isOneOf(tok::l_brace, tok::less) && Left.is(TT_SelectorName))
4864 if (Left.is(tok::slash) || Right.is(tok::slash))
4866 if (Left.MatchingParen &&
4867 Left.MatchingParen->is(TT_ProtoExtensionLSquare) &&
4868 Right.isOneOf(tok::l_brace, tok::less)) {
4869 return !Style.Cpp11BracedListStyle;
4872 if (Left.is(tok::percent))
4876 if (Left.is(tok::numeric_constant) && Right.is(tok::percent))
4877 return Right.hasWhitespaceBefore();
4878 }
else if (Style.isJson()) {
4879 if (Right.is(tok::colon) && Left.is(tok::string_literal))
4880 return Style.SpaceBeforeJsonColon;
4881 }
else if (Style.isCSharp()) {
4887 if (Left.is(tok::kw_this) && Right.is(tok::l_square))
4891 if (Left.is(tok::kw_new) && Right.is(tok::l_paren))
4895 if (Right.is(tok::l_brace))
4899 if (Left.is(tok::l_brace) && Right.isNot(tok::r_brace))
4902 if (Left.isNot(tok::l_brace) && Right.is(tok::r_brace))
4906 if (Left.is(TT_FatArrow) || Right.is(TT_FatArrow))
4910 if (Left.is(TT_AttributeColon) || Right.is(TT_AttributeColon))
4914 if (Left.is(TT_TemplateCloser) && Right.is(TT_StartOfName))
4918 if (Left.is(tok::l_square) || Right.is(tok::r_square))
4919 return Style.SpacesInSquareBrackets;
4922 if (Right.is(TT_CSharpNullable))
4926 if (Right.is(TT_NonNullAssertion))
4930 if (Left.is(tok::comma) && Right.is(tok::comma))
4934 if (Left.is(Keywords.kw_var) && Right.is(tok::l_paren))
4938 if (Right.is(tok::l_paren)) {
4939 if (Left.isOneOf(tok::kw_using, Keywords.kw_async, Keywords.kw_when,
4940 Keywords.kw_lock)) {
4941 return Style.SpaceBeforeParensOptions.AfterControlStatements ||
4942 spaceRequiredBeforeParens(Right);
4948 if (Left.isOneOf(tok::kw_public, tok::kw_private, tok::kw_protected,
4949 tok::kw_virtual, tok::kw_extern, tok::kw_static,
4950 Keywords.kw_internal, Keywords.kw_abstract,
4951 Keywords.kw_sealed, Keywords.kw_override,
4952 Keywords.kw_async, Keywords.kw_unsafe) &&
4953 Right.is(tok::l_paren)) {
4956 }
else if (Style.isJavaScript()) {
4957 if (Left.is(TT_FatArrow))
4960 if (Right.is(tok::l_paren) && Left.is(Keywords.kw_await) && Left.Previous &&
4961 Left.Previous->is(tok::kw_for)) {
4964 if (Left.is(Keywords.kw_async) && Right.is(tok::l_paren) &&
4965 Right.MatchingParen) {
4966 const FormatToken *Next = Right.MatchingParen->getNextNonComment();
4969 if (Next && Next->is(TT_FatArrow))
4972 if ((Left.is(TT_TemplateString) && Left.TokenText.ends_with(
"${")) ||
4973 (Right.is(TT_TemplateString) && Right.TokenText.starts_with(
"}"))) {
4978 if (Keywords.IsJavaScriptIdentifier(Left,
4980 Right.is(TT_TemplateString)) {
4983 if (Right.is(tok::star) &&
4984 Left.isOneOf(Keywords.kw_function, Keywords.kw_yield)) {
4987 if (Right.isOneOf(tok::l_brace, tok::l_square) &&
4988 Left.isOneOf(Keywords.kw_function, Keywords.kw_yield,
4989 Keywords.kw_extends, Keywords.kw_implements)) {
4992 if (Right.is(tok::l_paren)) {
4994 if (Line.MustBeDeclaration && Left.Tok.getIdentifierInfo())
4998 if (Left.Previous && Left.Previous->is(tok::period) &&
4999 Left.Tok.getIdentifierInfo()) {
5003 if (Left.isOneOf(tok::kw_throw, Keywords.kw_await, Keywords.kw_typeof,
5009 if (Left.endsSequence(tok::kw_const, Keywords.kw_as))
5011 if ((Left.isOneOf(Keywords.kw_let, Keywords.kw_var, Keywords.kw_in,
5016 (Left.is(Keywords.kw_of) && Left.Previous &&
5017 (Left.Previous->is(tok::identifier) ||
5018 Left.Previous->isOneOf(tok::r_square, tok::r_brace)))) &&
5019 (!Left.Previous || Left.Previous->isNot(tok::period))) {
5022 if (Left.isOneOf(tok::kw_for, Keywords.kw_as) && Left.Previous &&
5023 Left.Previous->is(tok::period) && Right.is(tok::l_paren)) {
5026 if (Left.is(Keywords.kw_as) &&
5027 Right.isOneOf(tok::l_square, tok::l_brace, tok::l_paren)) {
5030 if (Left.is(tok::kw_default) && Left.Previous &&
5031 Left.Previous->is(tok::kw_export)) {
5034 if (Left.is(Keywords.kw_is) && Right.is(tok::l_brace))
5036 if (Right.isOneOf(TT_JsTypeColon, TT_JsTypeOptionalQuestion))
5038 if (Left.is(TT_JsTypeOperator) || Right.is(TT_JsTypeOperator))
5040 if ((Left.is(tok::l_brace) || Right.is(tok::r_brace)) &&
5041 Line.First->isOneOf(Keywords.kw_import, tok::kw_export)) {
5044 if (Left.is(tok::ellipsis))
5046 if (Left.is(TT_TemplateCloser) &&
5047 !Right.isOneOf(tok::equal, tok::l_brace, tok::comma, tok::l_square,
5048 Keywords.kw_implements, Keywords.kw_extends)) {
5054 if (Right.is(TT_NonNullAssertion))
5056 if (Left.is(TT_NonNullAssertion) &&
5057 Right.isOneOf(Keywords.kw_as, Keywords.kw_in)) {
5061 if (Left.is(TT_CaseLabelArrow) || Right.is(TT_CaseLabelArrow))
5063 if (Left.is(tok::r_square) && Right.is(tok::l_brace))
5066 if (Left.is(tok::l_square) || Right.is(tok::r_square))
5067 return Style.SpacesInSquareBrackets;
5069 if (Left.is(Keywords.kw_synchronized) && Right.is(tok::l_paren)) {
5070 return Style.SpaceBeforeParensOptions.AfterControlStatements ||
5071 spaceRequiredBeforeParens(Right);
5073 if ((Left.isOneOf(tok::kw_static, tok::kw_public, tok::kw_private,
5074 tok::kw_protected) ||
5075 Left.isOneOf(Keywords.kw_final, Keywords.kw_abstract,
5076 Keywords.kw_native)) &&
5077 Right.is(TT_TemplateOpener)) {
5080 }
else if (IsVerilog) {
5082 if (Left.is(tok::identifier) && Left.TokenText[0] ==
'\\')
5086 if ((Left.is(TT_VerilogTableItem) &&
5087 !Right.isOneOf(tok::r_paren, tok::semi)) ||
5088 (Right.is(TT_VerilogTableItem) && Left.isNot(tok::l_paren))) {
5089 const FormatToken *Next = Right.getNextNonComment();
5090 return !(Next && Next->is(tok::r_paren));
5093 if (Left.isNot(TT_BinaryOperator) &&
5094 Left.isOneOf(Keywords.kw_verilogHash, Keywords.kw_verilogHashHash)) {
5098 if (Right.isNot(tok::semi) &&
5099 (Left.endsSequence(tok::numeric_constant, Keywords.kw_verilogHash) ||
5100 Left.endsSequence(tok::numeric_constant,
5101 Keywords.kw_verilogHashHash) ||
5102 (Left.is(tok::r_paren) && Left.MatchingParen &&
5103 Left.MatchingParen->endsSequence(tok::l_paren, tok::at)))) {
5108 if (Left.is(Keywords.kw_apostrophe) ||
5109 (Left.is(TT_VerilogNumberBase) && Right.is(tok::numeric_constant))) {
5113 if (Left.is(tok::arrow) || Right.is(tok::arrow))
5118 if (Left.is(tok::at) && Right.isOneOf(tok::l_paren, tok::star, tok::at))
5121 if (Right.is(tok::l_square) &&
5122 Left.isOneOf(TT_VerilogDimensionedTypeName, Keywords.kw_function)) {
5126 if (Right.isOneOf(tok::period, Keywords.kw_apostrophe) &&
5127 Keywords.isVerilogIdentifier(Left) && Left.getPreviousNonComment() &&
5128 Left.getPreviousNonComment()->is(Keywords.kw_tagged)) {
5134 if ((Right.is(Keywords.kw_apostrophe) ||
5136 !(Left.isOneOf(Keywords.kw_assign, Keywords.kw_unique) ||
5137 Keywords.isVerilogWordOperator(Left)) &&
5138 (Left.isOneOf(tok::r_square, tok::r_paren, tok::r_brace,
5139 tok::numeric_constant) ||
5140 Keywords.isWordLike(Left))) {
5144 if ((Right.is(tok::star) && Left.is(tok::coloncolon)) ||
5145 (Left.is(tok::star) && Right.is(tok::semi))) {
5149 if (Left.endsSequence(tok::star, tok::l_paren) && Right.is(tok::identifier))
5152 if (Right.is(tok::l_paren) && Right.is(TT_VerilogStrength))
5155 if ((Left.is(tok::l_brace) &&
5156 Right.isOneOf(tok::lessless, tok::greatergreater)) ||
5157 (Left.endsSequence(tok::lessless, tok::l_brace) ||
5158 Left.endsSequence(tok::greatergreater, tok::l_brace))) {
5161 }
else if (Style.isTableGen()) {
5163 if (Left.is(tok::l_square) && Right.is(tok::l_brace))
5165 if (Left.is(tok::r_brace) && Right.is(tok::r_square))
5168 if (Right.isOneOf(TT_TableGenDAGArgListColon,
5169 TT_TableGenDAGArgListColonToAlign) ||
5170 Left.isOneOf(TT_TableGenDAGArgListColon,
5171 TT_TableGenDAGArgListColonToAlign)) {
5174 if (Right.is(TT_TableGenCondOperatorColon))
5176 if (Left.isOneOf(TT_TableGenDAGArgOperatorID,
5177 TT_TableGenDAGArgOperatorToBreak) &&
5178 Right.isNot(TT_TableGenDAGArgCloser)) {
5182 if (Right.isOneOf(tok::l_paren, tok::less) &&
5183 Left.isOneOf(TT_TableGenBangOperator, TT_TableGenCondOperator)) {
5188 if (Left.is(TT_TableGenTrailingPasteOperator) &&
5189 Right.isOneOf(tok::l_brace, tok::colon)) {
5193 if (Left.is(tok::hash) || Right.is(tok::hash))
5196 if (Keywords.isTableGenDefinition(Left))
5200 if (Left.is(TT_ImplicitStringLiteral))
5201 return Right.hasWhitespaceBefore();
5203 if (Left.is(TT_ObjCMethodSpecifier))
5205 if (Left.is(tok::r_paren) && Left.isNot(TT_AttributeRParen) &&
5206 canBeObjCSelectorComponent(Right)) {
5214 (Right.is(tok::equal) || Left.is(tok::equal))) {
5218 if (Right.is(TT_TrailingReturnArrow) || Left.is(TT_TrailingReturnArrow))
5221 if (Left.is(tok::comma) && Right.isNot(TT_OverloadedOperatorLParen) &&
5224 (Left.Children.empty() || !Left.MacroParent)) {
5227 if (Right.is(tok::comma))
5229 if (Right.is(TT_ObjCBlockLParen))
5231 if (Right.is(TT_CtorInitializerColon))
5232 return Style.SpaceBeforeCtorInitializerColon;
5233 if (Right.is(TT_InheritanceColon) && !Style.SpaceBeforeInheritanceColon)
5235 if (Right.is(TT_RangeBasedForLoopColon) &&
5236 !Style.SpaceBeforeRangeBasedForLoopColon) {
5239 if (Left.is(TT_BitFieldColon)) {
5243 if (Right.is(tok::colon)) {
5244 if (Right.is(TT_CaseLabelColon))
5245 return Style.SpaceBeforeCaseColon;
5246 if (Right.is(TT_GotoLabelColon))
5249 if (!Right.getNextNonComment())
5251 if (Right.is(TT_ObjCMethodExpr))
5253 if (Left.is(tok::question))
5255 if (Right.is(TT_InlineASMColon) && Left.is(tok::coloncolon))
5257 if (Right.is(TT_DictLiteral))
5258 return Style.SpacesInContainerLiterals;
5259 if (Right.is(TT_AttributeColon))
5261 if (Right.is(TT_CSharpNamedArgumentColon))
5263 if (Right.is(TT_GenericSelectionColon))
5265 if (Right.is(TT_BitFieldColon)) {
5272 if ((Left.isOneOf(tok::minus, tok::minusminus) &&
5273 Right.isOneOf(tok::minus, tok::minusminus)) ||
5274 (Left.isOneOf(tok::plus, tok::plusplus) &&
5275 Right.isOneOf(tok::plus, tok::plusplus))) {
5278 if (Left.is(TT_UnaryOperator)) {
5281 if (Left.is(tok::amp) && Right.is(tok::r_square))
5282 return Style.SpacesInSquareBrackets;
5283 return Style.SpaceAfterLogicalNot && Left.is(tok::exclaim);
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);
5396 bool 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 const auto *BeforeLeft = Left.Previous;
5409 const auto *AfterRight = Right.Next;
5411 if (Style.isCSharp()) {
5412 if (Left.is(TT_FatArrow) && Right.is(tok::l_brace) &&
5413 Style.BraceWrapping.AfterFunction) {
5416 if (Right.is(TT_CSharpNamedArgumentColon) ||
5417 Left.is(TT_CSharpNamedArgumentColon)) {
5420 if (Right.is(TT_CSharpGenericTypeConstraint))
5422 if (AfterRight && AfterRight->is(TT_FatArrow) &&
5423 (Right.is(tok::numeric_constant) ||
5424 (Right.is(tok::identifier) && Right.TokenText ==
"_"))) {
5429 if (Left.is(TT_AttributeSquare) && Left.is(tok::r_square) &&
5430 (Right.isAccessSpecifier(
false) ||
5431 Right.is(Keywords.kw_internal))) {
5435 if (Left.is(TT_AttributeSquare) && Right.is(TT_AttributeSquare) &&
5436 Left.is(tok::r_square) && Right.is(tok::l_square)) {
5439 }
else if (Style.isJavaScript()) {
5441 if (Right.is(tok::string_literal) && Left.is(tok::plus) && BeforeLeft &&
5442 BeforeLeft->is(tok::string_literal)) {
5445 if (Left.is(TT_DictLiteral) && Left.is(tok::l_brace) && Line.Level == 0 &&
5446 BeforeLeft && BeforeLeft->is(tok::equal) &&
5447 Line.First->isOneOf(tok::identifier, Keywords.kw_import, tok::kw_export,
5451 !Line.First->isOneOf(Keywords.kw_var, Keywords.kw_let)) {
5456 if (Left.is(tok::l_brace) && Line.Level == 0 &&
5457 (Line.startsWith(tok::kw_enum) ||
5458 Line.startsWith(tok::kw_const, tok::kw_enum) ||
5459 Line.startsWith(tok::kw_export, tok::kw_enum) ||
5460 Line.startsWith(tok::kw_export, tok::kw_const, tok::kw_enum))) {
5465 if (Right.is(tok::r_brace) && Left.is(tok::l_brace) && BeforeLeft &&
5466 BeforeLeft->is(TT_FatArrow)) {
5468 switch (Style.AllowShortLambdasOnASingleLine) {
5474 return !Left.Children.empty();
5478 return (Left.NestingLevel == 0 && Line.Level == 0) &&
5479 !Left.Children.empty();
5481 llvm_unreachable(
"Unknown FormatStyle::ShortLambdaStyle enum");
5484 if (Right.is(tok::r_brace) && Left.is(tok::l_brace) &&
5485 !Left.Children.empty()) {
5489 (Left.NestingLevel == 0 && Line.Level == 0 &&
5490 Style.AllowShortFunctionsOnASingleLine &
5494 if (Right.is(tok::plus) && Left.is(tok::string_literal) && AfterRight &&
5495 AfterRight->is(tok::string_literal)) {
5498 }
else if (Style.isVerilog()) {
5500 if (Left.is(TT_VerilogAssignComma))
5503 if (Left.is(TT_VerilogTypeComma))
5507 if (Style.VerilogBreakBetweenInstancePorts &&
5508 (Left.is(TT_VerilogInstancePortComma) ||
5509 (Left.is(tok::r_paren) && Keywords.isVerilogIdentifier(Right) &&
5510 Left.MatchingParen &&
5511 Left.MatchingParen->is(TT_VerilogInstancePortLParen)))) {
5516 if (!Keywords.isVerilogBegin(Right) && Keywords.isVerilogEndOfLabel(Left))
5518 }
else if (Style.BreakAdjacentStringLiterals &&
5519 (IsCpp || Style.isProto() ||
5521 if (Left.isStringLiteral() && Right.isStringLiteral())
5526 if (Style.isJson()) {
5530 if (Left.is(TT_DictLiteral) && Left.is(tok::l_brace))
5533 if ((Left.is(TT_ArrayInitializerLSquare) && Left.is(tok::l_square) &&
5534 Right.isNot(tok::r_square)) ||
5535 Left.is(tok::comma)) {
5536 if (Right.is(tok::l_brace))
5540 for (
const auto *Tok = &Right; Tok; Tok = Tok->Next) {
5541 if (Tok->isOneOf(tok::l_brace, tok::l_square))
5543 if (Tok->isOneOf(tok::r_brace, tok::r_square))
5546 return Style.BreakArrays;
5548 }
else if (Style.isTableGen()) {
5552 if (Left.is(TT_TableGenCondOperatorComma))
5554 if (Left.is(TT_TableGenDAGArgOperatorToBreak) &&
5555 Right.isNot(TT_TableGenDAGArgCloser)) {
5558 if (Left.is(TT_TableGenDAGArgListCommaToBreak))
5560 if (Right.is(TT_TableGenDAGArgCloser) && Right.MatchingParen &&
5561 Right.MatchingParen->is(TT_TableGenDAGArgOpenerToBreak) &&
5562 &Left != Right.MatchingParen->Next) {
5568 if (Line.startsWith(tok::kw_asm) && Right.is(TT_InlineASMColon) &&
5578 const FormatToken *BeforeClosingBrace =
nullptr;
5579 if ((Left.isOneOf(tok::l_brace, TT_ArrayInitializerLSquare) ||
5580 (Style.isJavaScript() && Left.is(tok::l_paren))) &&
5581 Left.isNot(
BK_Block) && Left.MatchingParen) {
5582 BeforeClosingBrace = Left.MatchingParen->Previous;
5583 }
else if (Right.MatchingParen &&
5584 (Right.MatchingParen->isOneOf(tok::l_brace,
5585 TT_ArrayInitializerLSquare) ||
5586 (Style.isJavaScript() &&
5587 Right.MatchingParen->is(tok::l_paren)))) {
5588 BeforeClosingBrace = &Left;
5590 if (BeforeClosingBrace && (BeforeClosingBrace->is(tok::comma) ||
5591 BeforeClosingBrace->isTrailingComment())) {
5596 if (Right.is(tok::comment)) {
5597 return Left.isNot(
BK_BracedInit) && Left.isNot(TT_CtorInitializerColon) &&
5598 (Right.NewlinesBefore > 0 && Right.HasUnescapedNewline);
5600 if (Left.isTrailingComment())
5602 if (Left.IsUnterminatedLiteral)
5605 if (BeforeLeft && BeforeLeft->is(tok::lessless) &&
5606 Left.is(tok::string_literal) && Right.is(tok::lessless) && AfterRight &&
5607 AfterRight->is(tok::string_literal)) {
5608 return Right.NewlinesBefore > 0;
5611 if (Right.is(TT_RequiresClause)) {
5612 switch (Style.RequiresClausePosition) {
5621 if (Left.ClosesTemplateDeclaration && Left.MatchingParen &&
5622 Left.MatchingParen->NestingLevel == 0) {
5626 if (Right.is(tok::kw_concept))
5630 Right.NewlinesBefore > 0);
5632 if (Left.ClosesRequiresClause && Right.isNot(tok::semi)) {
5633 switch (Style.RequiresClausePosition) {
5643 (Left.is(TT_CtorInitializerComma) ||
5644 Right.is(TT_CtorInitializerColon))) {
5649 Left.isOneOf(TT_CtorInitializerColon, TT_CtorInitializerComma)) {
5655 Right.isOneOf(TT_CtorInitializerComma, TT_CtorInitializerColon)) {
5661 Right.is(TT_CtorInitializerColon)) {
5666 Left.is(TT_CtorInitializerColon)) {
5672 Right.is(TT_InheritanceComma)) {
5676 Left.is(TT_InheritanceComma)) {
5679 if (Right.is(tok::string_literal) && Right.TokenText.starts_with(
"R\"")) {
5683 return Right.IsMultiline && Right.NewlinesBefore > 0;
5685 if ((Left.is(tok::l_brace) ||
5686 (Left.is(tok::less) && BeforeLeft && BeforeLeft->is(tok::equal))) &&
5692 if (Right.is(TT_InlineASMBrace))
5693 return Right.HasUnescapedNewline;
5696 auto *FirstNonComment = Line.getFirstNonComment();
5699 FirstNonComment->isOneOf(Keywords.kw_internal, tok::kw_public,
5700 tok::kw_private, tok::kw_protected);
5702 if (Style.BraceWrapping.AfterEnum) {
5703 if (Line.startsWith(tok::kw_enum) ||
5704 Line.startsWith(tok::kw_typedef, tok::kw_enum)) {
5709 FirstNonComment->Next->is(tok::kw_enum)) {
5715 if (Style.BraceWrapping.AfterClass &&
5717 FirstNonComment->Next->is(Keywords.kw_interface)) ||
5718 Line.startsWith(Keywords.kw_interface))) {
5723 if (Right.isNot(TT_FunctionLBrace)) {
5724 return (Line.startsWith(tok::kw_class) &&
5725 Style.BraceWrapping.AfterClass) ||
5726 (Line.startsWith(tok::kw_struct) &&
5727 Style.BraceWrapping.AfterStruct);
5731 if (Left.is(TT_ObjCBlockLBrace) &&
5737 if (Left.isOneOf(TT_AttributeRParen, TT_AttributeMacro) &&
5738 Right.is(TT_ObjCDecl)) {
5742 if (Left.is(TT_LambdaLBrace)) {
5750 (!Left.Children.empty() &&
5756 if (Style.BraceWrapping.BeforeLambdaBody && Right.is(TT_LambdaLBrace) &&
5757 (Left.isPointerOrReference() || Left.is(TT_TemplateCloser))) {
5763 Left.is(TT_LeadingJavaAnnotation) &&
5764 Right.isNot(TT_LeadingJavaAnnotation) && Right.isNot(tok::l_paren) &&
5765 (Line.Last->is(tok::l_brace) || Style.BreakAfterJavaFieldAnnotations)) {
5769 if (Right.is(TT_ProtoExtensionLSquare))
5799 if (Style.isProto() && Right.is(TT_SelectorName) &&
5800 Right.isNot(tok::r_square) && AfterRight) {
5803 if (Left.is(tok::at))
5809 const auto *LBrace = AfterRight;
5810 if (LBrace && LBrace->is(tok::colon)) {
5811 LBrace = LBrace->Next;
5812 if (LBrace && LBrace->is(tok::at)) {
5813 LBrace = LBrace->Next;
5815 LBrace = LBrace->Next;
5827 ((LBrace->is(tok::l_brace) &&
5828 (LBrace->is(TT_DictLiteral) ||
5829 (LBrace->Next && LBrace->Next->is(tok::r_brace)))) ||
5830 LBrace->is(TT_ArrayInitializerLSquare) || LBrace->is(tok::less))) {
5837 if (Left.ParameterCount == 0)
5852 if (Left.isOneOf(tok::r_brace, tok::greater, tok::r_square))
5859 bool TokenAnnotator::canBreakBefore(
const AnnotatedLine &Line,
5860 const FormatToken &Right)
const {
5861 const FormatToken &Left = *Right.Previous;
5863 if (Style.isCSharp()) {
5864 if (Left.isOneOf(TT_CSharpNamedArgumentColon, TT_AttributeColon) ||
5865 Right.isOneOf(TT_CSharpNamedArgumentColon, TT_AttributeColon)) {
5869 if (Line.First->is(TT_CSharpGenericTypeConstraint))
5870 return Left.is(TT_CSharpGenericTypeConstraintComma);
5872 if (Right.is(TT_CSharpNullable))
5875 if (Left.isOneOf(Keywords.kw_throws, Keywords.kw_extends,
5876 Keywords.kw_implements)) {
5879 if (Right.isOneOf(Keywords.kw_throws, Keywords.kw_extends,
5880 Keywords.kw_implements)) {
5883 }
else if (Style.isJavaScript()) {
5884 const FormatToken *NonComment = Right.getPreviousNonComment();
5886 NonComment->isOneOf(
5887 tok::kw_return, Keywords.kw_yield, tok::kw_continue, tok::kw_break,
5888 tok::kw_throw, Keywords.kw_interface, Keywords.kw_type,
5889 tok::kw_static, tok::kw_public, tok::kw_private, tok::kw_protected,
5890 Keywords.kw_readonly, Keywords.kw_override, Keywords.kw_abstract,
5891 Keywords.kw_get, Keywords.kw_set, Keywords.kw_async,
5892 Keywords.kw_await)) {
5895 if (Right.NestingLevel == 0 &&
5896 (Left.Tok.getIdentifierInfo() ||
5897 Left.isOneOf(tok::r_square, tok::r_paren)) &&
5898 Right.isOneOf(tok::l_square, tok::l_paren)) {
5901 if (NonComment && NonComment->is(tok::identifier) &&
5902 NonComment->TokenText ==
"asserts") {
5905 if (Left.is(TT_FatArrow) && Right.is(tok::l_brace))
5907 if (Left.is(TT_JsTypeColon))
5910 if (Left.is(tok::exclaim) && Right.is(tok::colon))
5915 if (Right.is(Keywords.kw_is)) {
5916 const FormatToken *Next = Right.getNextNonComment();
5924 if (!Next || Next->isNot(tok::colon))
5927 if (Left.is(Keywords.kw_in))
5929 if (Right.is(Keywords.kw_in))
5931 if (Right.is(Keywords.kw_as))
5933 if (Right.isOneOf(Keywords.kw_extends, Keywords.kw_infer)) {
5939 if (Left.is(Keywords.kw_as))
5941 if (Left.is(TT_NonNullAssertion))
5943 if (Left.is(Keywords.kw_declare) &&
5944 Right.isOneOf(Keywords.kw_module, tok::kw_namespace,
5945 Keywords.kw_function, tok::kw_class, tok::kw_enum,
5946 Keywords.kw_interface, Keywords.kw_type, Keywords.kw_var,
5947 Keywords.kw_let, tok::kw_const)) {
5952 if (Left.isOneOf(Keywords.kw_module, tok::kw_namespace) &&
5953 Right.isOneOf(tok::identifier, tok::string_literal)) {
5956 if (Right.is(TT_TemplateString) && Right.closesScope())
5960 if (Left.is(tok::identifier) && Right.is(TT_TemplateString))
5962 if (Left.is(TT_TemplateString) && Left.opensScope())
5964 }
else if (Style.isTableGen()) {
5966 if (Keywords.isTableGenDefinition(Left))
5969 if (Right.is(tok::l_paren)) {
5970 return !Left.isOneOf(TT_TableGenBangOperator, TT_TableGenCondOperator,
5974 if (Left.is(TT_TableGenValueSuffix))
5977 if (Left.is(tok::hash) || Right.is(tok::hash))
5979 if (Left.isOneOf(TT_TableGenBangOperator, TT_TableGenCondOperator))
5983 if (Left.is(tok::at))
5985 if (Left.Tok.getObjCKeywordID() == tok::objc_interface)
5987 if (Left.isOneOf(TT_JavaAnnotation, TT_LeadingJavaAnnotation))
5988 return Right.isNot(tok::l_paren);
5989 if (Right.is(TT_PointerOrReference)) {
5990 return Line.IsMultiVariableDeclStmt ||
5991 (getTokenPointerOrReferenceAlignment(Right) ==
5993 (!Right.Next || Right.Next->isNot(TT_FunctionDeclarationName)));
5995 if (Right.isOneOf(TT_StartOfName, TT_FunctionDeclarationName) ||
5996 Right.is(tok::kw_operator)) {
5999 if (Left.is(TT_PointerOrReference))
6001 if (Right.isTrailingComment()) {
6008 (Left.is(TT_CtorInitializerColon) && Right.NewlinesBefore > 0 &&
6011 if (Left.is(tok::question) && Right.is(tok::colon))
6013 if (Right.is(TT_ConditionalExpr) || Right.is(tok::question))
6014 return Style.BreakBeforeTernaryOperators;
6015 if (Left.is(TT_ConditionalExpr) || Left.is(tok::question))
6016 return !Style.BreakBeforeTernaryOperators;
6017 if (Left.is(TT_InheritanceColon))
6019 if (Right.is(TT_InheritanceColon))
6021 if (Right.is(TT_ObjCMethodExpr) && Right.isNot(tok::r_square) &&
6022 Left.isNot(TT_SelectorName)) {
6026 if (Right.is(tok::colon) &&
6027 !Right.isOneOf(TT_CtorInitializerColon, TT_InlineASMColon)) {
6030 if (Left.is(tok::colon) && Left.isOneOf(TT_DictLiteral, TT_ObjCMethodExpr)) {
6031 if (Style.isProto()) {
6032 if (!Style.AlwaysBreakBeforeMultilineStrings && Right.isStringLiteral())
6058 if (((Right.is(tok::l_brace) || Right.is(tok::less)) &&
6059 Right.is(TT_DictLiteral)) ||
6060 Right.is(TT_ArrayInitializerLSquare)) {
6066 if (Right.is(tok::r_square) && Right.MatchingParen &&
6067 Right.MatchingParen->is(TT_ProtoExtensionLSquare)) {
6070 if (Right.is(TT_SelectorName) || (Right.is(tok::identifier) && Right.Next &&
6071 Right.Next->is(TT_ObjCMethodExpr))) {
6072 return Left.isNot(tok::period);
6076 if (Right.is(tok::kw_concept))
6078 if (Right.is(TT_RequiresClause))
6080 if (Left.ClosesTemplateDeclaration) {
6082 Right.NewlinesBefore > 0;
6084 if (Left.is(TT_FunctionAnnotationRParen))
6086 if (Left.ClosesRequiresClause)
6088 if (Right.isOneOf(TT_RangeBasedForLoopColon, TT_OverloadedOperatorLParen,
6089 TT_OverloadedOperator)) {
6092 if (Left.is(TT_RangeBasedForLoopColon))
6094 if (Right.is(TT_RangeBasedForLoopColon))
6096 if (Left.is(TT_TemplateCloser) && Right.is(TT_TemplateOpener))
6098 if ((Left.is(tok::greater) && Right.is(tok::greater)) ||
6099 (Left.is(tok::less) && Right.is(tok::less))) {
6102 if (Right.is(TT_BinaryOperator) &&
6108 if (Left.isOneOf(TT_TemplateCloser, TT_UnaryOperator) ||
6109 Left.is(tok::kw_operator)) {
6112 if (Left.is(tok::equal) && !Right.isOneOf(tok::kw_default, tok::kw_delete) &&
6116 if (Left.is(tok::equal) && Right.is(tok::l_brace) &&
6117 !Style.Cpp11BracedListStyle) {
6120 if (Left.is(TT_AttributeLParen) ||
6121 (Left.is(tok::l_paren) && Left.is(TT_TypeDeclarationParen))) {
6124 if (Left.is(tok::l_paren) && Left.Previous &&
6125 (Left.Previous->isOneOf(TT_BinaryOperator, TT_CastRParen))) {
6128 if (Right.is(TT_ImplicitStringLiteral))
6131 if (Right.is(TT_TemplateCloser))
6133 if (Right.is(tok::r_square) && Right.MatchingParen &&
6134 Right.MatchingParen->is(TT_LambdaLSquare)) {
6140 if (Right.is(tok::r_brace)) {
6141 return Right.MatchingParen && (Right.MatchingParen->is(
BK_Block) ||
6142 (Right.isBlockIndentedInitRBrace(Style)));
6146 if (Right.is(tok::r_paren)) {
6148 !Right.MatchingParen) {
6151 auto Next = Right.Next;
6152 if (Next && Next->is(tok::r_paren))
6154 if (Next && Next->is(tok::l_paren))
6156 const FormatToken *
Previous = Right.MatchingParen->Previous;
6162 if (Left.is(TT_TrailingAnnotation)) {
6163 return !Right.isOneOf(tok::l_brace, tok::semi, tok::equal, tok::l_paren,
6164 tok::less, tok::coloncolon);
6167 if (Right.isAttribute())
6170 if (Right.is(tok::l_square) && Right.is(TT_AttributeSquare))
6171 return Left.isNot(TT_AttributeSquare);
6173 if (Left.is(tok::identifier) && Right.is(tok::string_literal))
6176 if (Right.is(tok::identifier) && Right.Next && Right.Next->is(TT_DictLiteral))
6179 if (Left.is(TT_CtorInitializerColon)) {
6181 (!Right.isTrailingComment() || Right.NewlinesBefore > 0);
6183 if (Right.is(TT_CtorInitializerColon))
6185 if (Left.is(TT_CtorInitializerComma) &&
6189 if (Right.is(TT_CtorInitializerComma) &&
6193 if (Left.is(TT_InheritanceComma) &&
6197 if (Right.is(TT_InheritanceComma) &&
6201 if (Left.is(TT_ArrayInitializerLSquare))
6203 if (Right.is(tok::kw_typename) && Left.isNot(tok::kw_const))
6205 if ((Left.isBinaryOperator() || Left.is(TT_BinaryOperator)) &&
6206 !Left.isOneOf(tok::arrowstar, tok::lessless) &&
6212 if ((Left.is(TT_AttributeSquare) && Right.is(tok::l_square)) ||
6213 (Left.is(tok::r_square) && Right.is(TT_AttributeSquare))) {
6217 auto ShortLambdaOption = Style.AllowShortLambdasOnASingleLine;
6218 if (Style.BraceWrapping.BeforeLambdaBody && Right.is(TT_LambdaLBrace)) {
6225 if (Right.is(tok::kw_noexcept) && Right.is(TT_TrailingAnnotation)) {
6226 switch (Style.AllowBreakBeforeNoexceptSpecifier) {
6232 return Right.Next && Right.Next->is(tok::l_paren);
6236 return Left.isOneOf(tok::comma, tok::coloncolon, tok::semi, tok::l_brace,
6237 tok::kw_class, tok::kw_struct, tok::comment) ||
6238 Right.isMemberAccess() ||
6239 Right.isOneOf(TT_TrailingReturnArrow, tok::lessless, tok::colon,
6240 tok::l_square, tok::at) ||
6241 (Left.is(tok::r_paren) &&
6242 Right.isOneOf(tok::identifier, tok::kw_const)) ||
6243 (Left.is(tok::l_paren) && Right.isNot(tok::r_paren)) ||
6244 (Left.is(TT_TemplateOpener) && Right.isNot(TT_TemplateCloser));
6247 void TokenAnnotator::printDebugInfo(
const AnnotatedLine &Line)
const {
6248 llvm::errs() <<
"AnnotatedTokens(L=" << Line.Level <<
", P=" << Line.PPLevel
6249 <<
", T=" << Line.Type <<
", C=" << Line.IsContinuation
6251 const FormatToken *Tok = Line.First;
6253 llvm::errs() <<
" M=" << Tok->MustBreakBefore
6254 <<
" C=" << Tok->CanBreakBefore
6256 <<
" S=" << Tok->SpacesRequiredBefore
6257 <<
" F=" << Tok->Finalized <<
" B=" << Tok->BlockParameterCount
6258 <<
" BK=" << Tok->getBlockKind() <<
" P=" << Tok->SplitPenalty
6259 <<
" Name=" << Tok->Tok.getName() <<
" L=" << Tok->TotalLength
6260 <<
" PPK=" << Tok->getPackingKind() <<
" FakeLParens=";
6262 llvm::errs() << LParen <<
"/";
6263 llvm::errs() <<
" FakeRParens=" << Tok->FakeRParens;
6264 llvm::errs() <<
" II=" << Tok->Tok.getIdentifierInfo();
6265 llvm::errs() <<
" Text='" << Tok->TokenText <<
"'\n";
6267 assert(Tok == Line.Last);
6270 llvm::errs() <<
"----\n";
6274 TokenAnnotator::getTokenReferenceAlignment(
const FormatToken &Reference)
const {
6275 assert(
Reference.isOneOf(tok::amp, tok::ampamp));
6276 switch (Style.ReferenceAlignment) {
6278 return Style.PointerAlignment;
6287 return Style.PointerAlignment;
6291 TokenAnnotator::getTokenPointerOrReferenceAlignment(
6292 const FormatToken &PointerOrReference)
const {
6293 if (PointerOrReference.isOneOf(tok::amp, tok::ampamp)) {
6294 switch (Style.ReferenceAlignment) {
6296 return Style.PointerAlignment;
6305 assert(PointerOrReference.is(tok::star));
6306 return Style.PointerAlignment;
Defines the SourceManager interface.
bool ColonIsObjCMethodExpr
FormatToken * FirstStartOfName
bool InCpp11AttributeSpecifier
enum clang::format::@1273::AnnotatingParser::Context::@343 ContextType
unsigned LongestObjCSelectorName
#define TRANSFORM_TYPE_TRAIT_DEF(_, Trait)
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.
__DEVICE__ int max(int __a, int __b)
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
Parser - This implements a parser for the C family of languages.
IdentifierInfo * getIdentifierInfo() const
bool Ret(InterpState &S, CodePtr &PC, 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.
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...