18#include "llvm/ADT/SmallPtrSet.h"
19#include "llvm/Support/Debug.h"
21#define DEBUG_TYPE "format-token-annotator"
27 const FormatStyle &Style) {
28 switch (Style.BreakAfterAttributes) {
29 case FormatStyle::ABS_Always:
31 case FormatStyle::ABS_Never:
34 return Tok.NewlinesBefore > 0;
42static bool startsWithInitStatement(
const AnnotatedLine &
Line) {
43 return Line.startsWith(tok::kw_for) ||
Line.startsWith(tok::kw_if) ||
44 Line.startsWith(tok::kw_switch);
59 return Tok.Tok.getIdentifierInfo();
65static bool isLambdaParameterList(
const FormatToken *Left) {
67 if (
Left->Previous &&
Left->Previous->is(tok::greater) &&
68 Left->Previous->MatchingParen &&
69 Left->Previous->MatchingParen->is(TT_TemplateOpener)) {
70 Left =
Left->Previous->MatchingParen;
74 return Left->Previous &&
Left->Previous->is(tok::r_square) &&
75 Left->Previous->MatchingParen &&
76 Left->Previous->MatchingParen->is(TT_LambdaLSquare);
82 return Tok.isOneOf(tok::kw_if, tok::kw_for, tok::kw_while, tok::kw_switch,
83 tok::kw_constexpr, tok::kw_catch);
88 if (!IsCpp || !
Tok.startsSequence(tok::l_square, tok::l_square))
91 if (
Tok.Previous &&
Tok.Previous->is(tok::at))
98 if (AttrTok->startsSequence(tok::kw_using, tok::identifier, tok::colon))
100 if (AttrTok->isNot(tok::identifier))
102 while (AttrTok && !AttrTok->startsSequence(tok::r_square, tok::r_square)) {
106 if (AttrTok->is(tok::colon) ||
107 AttrTok->startsSequence(tok::identifier, tok::identifier) ||
108 AttrTok->startsSequence(tok::r_paren, tok::identifier)) {
111 if (AttrTok->is(tok::ellipsis))
113 AttrTok = AttrTok->Next;
115 return AttrTok && AttrTok->startsSequence(tok::r_square, tok::r_square);
123class AnnotatingParser {
125 AnnotatingParser(
const FormatStyle &Style, AnnotatedLine &Line,
126 const AdditionalKeywords &Keywords,
127 SmallVector<ScopeType> &Scopes)
128 : Style(Style), Line(Line), CurrentToken(Line.
First), AutoFound(
false),
130 Keywords(Keywords), Scopes(Scopes), TemplateDeclarationDepth(0) {
131 Contexts.push_back(Context(tok::unknown, 1,
false));
132 resetTokenMetadata();
137 switch (Token.getType()) {
139 case TT_StructLBrace:
142 case TT_CompoundRequirementLBrace:
153 auto *
Left = CurrentToken->Previous;
157 if (NonTemplateLess.count(Left) > 0)
160 const auto *BeforeLess =
Left->Previous;
163 if (BeforeLess->Tok.isLiteral())
165 if (BeforeLess->is(tok::r_brace))
167 if (BeforeLess->is(tok::r_paren) && Contexts.size() > 1 &&
168 !(BeforeLess->MatchingParen &&
169 BeforeLess->MatchingParen->is(TT_OverloadedOperatorLParen))) {
172 if (BeforeLess->is(tok::kw_operator) && CurrentToken->is(tok::l_paren))
176 Left->ParentBracket = Contexts.back().ContextKind;
177 ScopedContextCreator ContextCreator(*
this, tok::less, 12);
178 Contexts.back().IsExpression =
false;
182 if (BeforeLess && BeforeLess->isNot(tok::kw_template))
183 Contexts.back().ContextType = Context::TemplateArgument;
185 if (Style.isJava() && CurrentToken->is(tok::question))
188 for (
bool SeenTernaryOperator =
false, MaybeAngles =
true; CurrentToken;) {
189 const auto &ParentContext = Contexts[Contexts.size() - 2];
190 const bool InExpr = ParentContext.IsExpression;
191 if (CurrentToken->is(tok::greater)) {
192 const auto *
Next = CurrentToken->Next;
193 if (CurrentToken->isNot(TT_TemplateCloser)) {
200 if (
Next &&
Next->is(tok::greater) &&
201 Left->ParentBracket != tok::less &&
202 CurrentToken->getStartOfNonWhitespace() ==
203 Next->getStartOfNonWhitespace().getLocWithOffset(-1)) {
206 if (InExpr && SeenTernaryOperator &&
207 (!
Next ||
Next->isNoneOf(tok::l_paren, tok::l_brace))) {
212 if (ParentContext.InStaticAssertFirstArgument &&
Next &&
213 Next->isOneOf(tok::minus, tok::identifier)) {
217 Left->MatchingParen = CurrentToken;
218 CurrentToken->MatchingParen =
Left;
224 if (Style.isTextProto() ||
225 (Style.Language == FormatStyle::LK_Proto && BeforeLess &&
226 BeforeLess->isOneOf(TT_SelectorName, TT_DictLiteral))) {
227 CurrentToken->setType(TT_DictLiteral);
229 CurrentToken->setType(TT_TemplateCloser);
230 CurrentToken->Tok.setLength(1);
237 if (BeforeLess && BeforeLess->is(TT_TemplateName)) {
241 if (CurrentToken->is(tok::question) && Style.isJava()) {
245 if (CurrentToken->isOneOf(tok::r_paren, tok::r_square, tok::r_brace))
247 const auto &Prev = *CurrentToken->Previous;
254 if (MaybeAngles && InExpr && !Line.startsWith(tok::kw_template) &&
255 Prev.is(TT_BinaryOperator) &&
256 Prev.isOneOf(tok::pipepipe, tok::ampamp)) {
259 if (Prev.isOneOf(tok::question, tok::colon) && !Style.isProto())
260 SeenTernaryOperator =
true;
261 updateParameterCount(Left, CurrentToken);
262 if (Style.Language == FormatStyle::LK_Proto) {
264 if (CurrentToken->is(tok::colon) ||
265 (CurrentToken->isOneOf(tok::l_brace, tok::less) &&
270 }
else if (Style.isTableGen()) {
271 if (CurrentToken->isOneOf(tok::comma, tok::equal)) {
278 if (!parseTableGenValue())
288 bool parseUntouchableParens() {
289 while (CurrentToken) {
290 CurrentToken->Finalized =
true;
291 switch (CurrentToken->Tok.getKind()) {
294 if (!parseUntouchableParens())
309 bool parseParens(
bool IsIf =
false) {
312 assert(CurrentToken->Previous &&
"Unknown previous token");
313 FormatToken &OpeningParen = *CurrentToken->Previous;
314 assert(OpeningParen.is(tok::l_paren));
315 FormatToken *PrevNonComment = OpeningParen.getPreviousNonComment();
316 OpeningParen.ParentBracket = Contexts.back().ContextKind;
317 ScopedContextCreator ContextCreator(*
this, tok::l_paren, 1);
320 Contexts.back().ColonIsForRangeExpr =
321 Contexts.size() == 2 && Contexts[0].ColonIsForRangeExpr;
323 if (OpeningParen.Previous &&
324 OpeningParen.Previous->is(TT_UntouchableMacroFunc)) {
325 OpeningParen.Finalized =
true;
326 return parseUntouchableParens();
329 bool StartsObjCSelector =
false;
330 if (!Style.isVerilog()) {
331 if (
FormatToken *MaybeSel = OpeningParen.Previous) {
333 if (MaybeSel->is(tok::objc_selector) && MaybeSel->Previous &&
334 MaybeSel->Previous->is(tok::at)) {
335 StartsObjCSelector =
true;
340 if (OpeningParen.is(TT_OverloadedOperatorLParen)) {
343 while (Prev->isNot(tok::kw_operator)) {
344 Prev = Prev->Previous;
345 assert(Prev &&
"Expect a kw_operator prior to the OperatorLParen!");
351 bool OperatorCalledAsMemberFunction =
352 Prev->Previous && Prev->Previous->isOneOf(tok::period, tok::arrow);
353 Contexts.back().IsExpression = OperatorCalledAsMemberFunction;
354 }
else if (OpeningParen.is(TT_VerilogInstancePortLParen)) {
355 Contexts.back().IsExpression =
true;
356 Contexts.back().ContextType = Context::VerilogInstancePortList;
357 }
else if (Style.isJavaScript() &&
358 (Line.startsWith(Keywords.kw_type, tok::identifier) ||
359 Line.startsWith(tok::kw_export, Keywords.kw_type,
363 Contexts.back().IsExpression =
false;
364 }
else if (OpeningParen.Previous &&
365 (OpeningParen.Previous->isOneOf(
366 tok::kw_noexcept, tok::kw_explicit, tok::kw_while,
367 tok::l_paren, tok::comma, TT_CastRParen,
368 TT_BinaryOperator) ||
369 OpeningParen.Previous->isIf())) {
371 Contexts.back().IsExpression =
true;
372 }
else if (Style.isJavaScript() && OpeningParen.Previous &&
373 (OpeningParen.Previous->is(Keywords.kw_function) ||
374 (OpeningParen.Previous->endsSequence(tok::identifier,
375 Keywords.kw_function)))) {
377 Contexts.back().IsExpression =
false;
378 }
else if (Style.isJavaScript() && OpeningParen.Previous &&
379 OpeningParen.Previous->is(TT_JsTypeColon)) {
381 Contexts.back().IsExpression =
false;
382 }
else if (isLambdaParameterList(&OpeningParen)) {
384 OpeningParen.setType(TT_LambdaDefinitionLParen);
385 Contexts.back().IsExpression =
false;
386 }
else if (OpeningParen.is(TT_RequiresExpressionLParen)) {
387 Contexts.back().IsExpression =
false;
388 }
else if (OpeningParen.Previous &&
389 OpeningParen.Previous->is(tok::kw__Generic)) {
390 Contexts.back().ContextType = Context::C11GenericSelection;
391 Contexts.back().IsExpression =
true;
392 }
else if (OpeningParen.Previous &&
393 OpeningParen.Previous->TokenText ==
"Q_PROPERTY") {
394 Contexts.back().ContextType = Context::QtProperty;
395 Contexts.back().IsExpression =
false;
396 }
else if (Line.InPPDirective &&
397 (!OpeningParen.Previous ||
398 OpeningParen.Previous->isNot(tok::identifier))) {
399 Contexts.back().IsExpression =
true;
400 }
else if (Contexts[Contexts.size() - 2].CaretFound) {
402 Contexts.back().IsExpression =
false;
403 }
else if (OpeningParen.Previous &&
404 OpeningParen.Previous->is(TT_ForEachMacro)) {
406 Contexts.back().ContextType = Context::ForEachMacro;
407 Contexts.back().IsExpression =
false;
408 }
else if (OpeningParen.Previous && OpeningParen.Previous->MatchingParen &&
409 OpeningParen.Previous->MatchingParen->isOneOf(
410 TT_ObjCBlockLParen, TT_FunctionTypeLParen)) {
411 Contexts.back().IsExpression =
false;
412 }
else if (!Line.MustBeDeclaration &&
413 (!Line.InPPDirective || (Line.InMacroBody && !Scopes.empty()))) {
415 OpeningParen.Previous &&
416 OpeningParen.Previous->isOneOf(tok::kw_for, tok::kw_catch);
417 Contexts.back().IsExpression = !IsForOrCatch;
420 if (Style.isTableGen()) {
422 if (Prev->is(TT_TableGenCondOperator)) {
423 Contexts.back().IsTableGenCondOpe =
true;
424 Contexts.back().IsExpression =
true;
425 }
else if (Contexts.size() > 1 &&
426 Contexts[Contexts.size() - 2].IsTableGenBangOpe) {
431 Contexts.back().IsTableGenBangOpe =
true;
432 Contexts.back().IsExpression =
true;
435 if (!parseTableGenDAGArg())
437 return parseTableGenDAGArgAndList(&OpeningParen);
444 if (PrevNonComment && OpeningParen.is(TT_Unknown)) {
445 if (PrevNonComment->isAttribute()) {
446 OpeningParen.setType(TT_AttributeLParen);
447 }
else if (PrevNonComment->isOneOf(TT_TypenameMacro, tok::kw_decltype,
450#include
"clang/Basic/TransformTypeTraits.def"
452 OpeningParen.setType(TT_TypeDeclarationParen);
454 if (PrevNonComment->isOneOf(tok::kw_decltype, tok::kw_typeof))
455 Contexts.back().IsExpression =
true;
459 if (StartsObjCSelector)
460 OpeningParen.setType(TT_ObjCSelector);
462 const bool IsStaticAssert =
463 PrevNonComment && PrevNonComment->is(tok::kw_static_assert);
465 Contexts.back().InStaticAssertFirstArgument =
true;
475 bool MightBeFunctionType = !Contexts[Contexts.size() - 2].IsExpression;
476 bool ProbablyFunctionType =
477 CurrentToken->isPointerOrReference() || CurrentToken->is(tok::caret);
478 bool HasMultipleLines =
false;
479 bool HasMultipleParametersOnALine =
false;
480 bool MightBeObjCForRangeLoop =
481 OpeningParen.Previous && OpeningParen.Previous->is(tok::kw_for);
483 while (CurrentToken) {
484 const auto &Prev = *CurrentToken->Previous;
485 const auto *PrevPrev = Prev.Previous;
486 if (Prev.is(TT_PointerOrReference) &&
487 PrevPrev->isOneOf(tok::l_paren, tok::coloncolon)) {
488 ProbablyFunctionType =
true;
490 if (CurrentToken->is(tok::comma))
491 MightBeFunctionType =
false;
492 if (Prev.is(TT_BinaryOperator))
493 Contexts.back().IsExpression =
true;
494 if (CurrentToken->is(tok::r_paren)) {
495 if (Prev.is(TT_PointerOrReference) &&
496 (PrevPrev == &OpeningParen || PrevPrev->is(tok::coloncolon))) {
497 MightBeFunctionType =
true;
499 if (OpeningParen.isNot(TT_CppCastLParen) && MightBeFunctionType &&
500 ProbablyFunctionType && CurrentToken->Next &&
501 (CurrentToken->Next->is(tok::l_paren) ||
502 (CurrentToken->Next->is(tok::l_square) &&
503 (Line.MustBeDeclaration ||
504 (PrevNonComment && PrevNonComment->isTypeName(LangOpts)))))) {
505 OpeningParen.setType(OpeningParen.Next->is(tok::caret)
507 : TT_FunctionTypeLParen);
509 OpeningParen.MatchingParen = CurrentToken;
510 CurrentToken->MatchingParen = &OpeningParen;
512 if (CurrentToken->Next && CurrentToken->Next->is(tok::l_brace) &&
513 OpeningParen.Previous && OpeningParen.Previous->is(tok::l_paren)) {
519 if (
Tok->is(TT_BinaryOperator) &&
Tok->isPointerOrReference())
520 Tok->setType(TT_PointerOrReference);
524 if (StartsObjCSelector) {
525 CurrentToken->setType(TT_ObjCSelector);
526 if (Contexts.back().FirstObjCSelectorName) {
527 Contexts.back().FirstObjCSelectorName->LongestObjCSelectorName =
528 Contexts.back().LongestObjCSelectorName;
532 if (OpeningParen.is(TT_AttributeLParen))
533 CurrentToken->setType(TT_AttributeRParen);
534 if (OpeningParen.is(TT_TypeDeclarationParen))
535 CurrentToken->setType(TT_TypeDeclarationParen);
536 if (OpeningParen.Previous &&
537 OpeningParen.Previous->is(TT_JavaAnnotation)) {
538 CurrentToken->setType(TT_JavaAnnotation);
540 if (OpeningParen.Previous &&
541 OpeningParen.Previous->is(TT_LeadingJavaAnnotation)) {
542 CurrentToken->setType(TT_LeadingJavaAnnotation);
545 if (!HasMultipleLines)
547 else if (HasMultipleParametersOnALine)
555 if (CurrentToken->isOneOf(tok::r_square, tok::r_brace))
558 if (CurrentToken->is(tok::l_brace) && OpeningParen.is(TT_ObjCBlockLParen))
559 OpeningParen.setType(TT_Unknown);
560 if (CurrentToken->is(tok::comma) && CurrentToken->Next &&
561 !CurrentToken->Next->HasUnescapedNewline &&
562 !CurrentToken->Next->isTrailingComment()) {
563 HasMultipleParametersOnALine =
true;
565 bool ProbablyFunctionTypeLParen =
566 (CurrentToken->is(tok::l_paren) && CurrentToken->Next &&
567 CurrentToken->Next->isOneOf(tok::star, tok::amp, tok::caret));
568 if ((Prev.isOneOf(tok::kw_const, tok::kw_auto) ||
569 Prev.isTypeName(LangOpts)) &&
570 !(CurrentToken->is(tok::l_brace) ||
571 (CurrentToken->is(tok::l_paren) && !ProbablyFunctionTypeLParen))) {
572 Contexts.back().IsExpression =
false;
574 if (CurrentToken->isOneOf(tok::semi, tok::colon)) {
575 MightBeObjCForRangeLoop =
false;
576 if (PossibleObjCForInToken) {
577 PossibleObjCForInToken->setType(TT_Unknown);
578 PossibleObjCForInToken =
nullptr;
581 if (IsIf && CurrentToken->is(tok::semi)) {
582 for (
auto *
Tok = OpeningParen.Next;
583 Tok != CurrentToken &&
584 Tok->isNoneOf(tok::equal, tok::l_paren, tok::l_brace);
586 if (
Tok->isPointerOrReference())
587 Tok->setFinalizedType(TT_PointerOrReference);
590 if (MightBeObjCForRangeLoop && CurrentToken->is(Keywords.kw_in)) {
591 PossibleObjCForInToken = CurrentToken;
592 PossibleObjCForInToken->setType(TT_ObjCForIn);
596 if (CurrentToken->is(tok::comma)) {
598 Contexts.back().InStaticAssertFirstArgument =
false;
600 Contexts.back().CanBeExpression =
true;
603 if (Style.isTableGen()) {
604 if (CurrentToken->is(tok::comma)) {
605 if (Contexts.back().IsTableGenCondOpe)
606 CurrentToken->setType(TT_TableGenCondOperatorComma);
608 }
else if (CurrentToken->is(tok::colon)) {
609 if (Contexts.back().IsTableGenCondOpe)
610 CurrentToken->setType(TT_TableGenCondOperatorColon);
614 if (!parseTableGenValue())
622 updateParameterCount(&OpeningParen,
Tok);
623 if (CurrentToken && CurrentToken->HasUnescapedNewline)
624 HasMultipleLines =
true;
630 if (!Style.isCSharp())
634 if (
Tok.Previous &&
Tok.Previous->is(tok::identifier))
638 if (
Tok.Previous &&
Tok.Previous->is(tok::r_square)) {
649 if (AttrTok->is(tok::r_square))
653 while (AttrTok && AttrTok->isNot(tok::r_square))
654 AttrTok = AttrTok->Next;
660 AttrTok = AttrTok->Next;
665 if (AttrTok->isAccessSpecifierKeyword() ||
666 AttrTok->isOneOf(tok::comment, tok::kw_class, tok::kw_static,
667 tok::l_square, Keywords.kw_internal)) {
673 AttrTok->Next->startsSequence(tok::identifier, tok::l_paren)) {
689 Left->ParentBracket = Contexts.back().ContextKind;
695 bool CppArrayTemplates =
696 IsCpp && Parent && Parent->is(TT_TemplateCloser) &&
697 (Contexts.back().CanBeExpression || Contexts.back().IsExpression ||
698 Contexts.back().ContextType == Context::TemplateArgument);
700 const bool IsInnerSquare = Contexts.back().InCpp11AttributeSpecifier;
701 const bool IsCpp11AttributeSpecifier =
702 isCppAttribute(IsCpp, *Left) || IsInnerSquare;
705 bool IsCSharpAttributeSpecifier =
706 isCSharpAttributeSpecifier(*Left) ||
707 Contexts.back().InCSharpAttributeSpecifier;
709 bool InsideInlineASM = Line.startsWith(tok::kw_asm);
710 bool IsCppStructuredBinding =
Left->isCppStructuredBinding(IsCpp);
711 bool StartsObjCMethodExpr =
712 !IsCppStructuredBinding && !InsideInlineASM && !CppArrayTemplates &&
713 IsCpp && !IsCpp11AttributeSpecifier && !IsCSharpAttributeSpecifier &&
714 Contexts.back().CanBeExpression &&
Left->isNot(TT_LambdaLSquare) &&
715 CurrentToken->isNoneOf(tok::l_brace, tok::r_square) &&
719 (!Parent || !Parent->is(tok::comma) ||
720 Contexts.back().ContextKind != tok::l_brace) &&
722 Parent->isOneOf(tok::colon, tok::l_square, tok::l_paren,
723 tok::kw_return, tok::kw_throw) ||
724 Parent->isUnaryOperator() ||
726 Parent->isOneOf(TT_ObjCForIn, TT_CastRParen) ||
729 bool ColonFound =
false;
731 unsigned BindingIncrease = 1;
732 if (IsCppStructuredBinding) {
733 Left->setType(TT_StructuredBindingLSquare);
734 }
else if (
Left->is(TT_Unknown)) {
735 if (StartsObjCMethodExpr) {
736 Left->setType(TT_ObjCMethodExpr);
737 }
else if (InsideInlineASM) {
738 Left->setType(TT_InlineASMSymbolicNameLSquare);
739 }
else if (IsCpp11AttributeSpecifier) {
740 if (!IsInnerSquare) {
741 Left->setType(TT_AttributeLSquare);
743 Left->Previous->EndsCppAttributeGroup =
false;
745 }
else if (Style.isJavaScript() && Parent &&
746 Contexts.back().ContextKind == tok::l_brace &&
747 Parent->isOneOf(tok::l_brace, tok::comma)) {
748 Left->setType(TT_JsComputedPropertyName);
749 }
else if (IsCpp && Contexts.back().ContextKind == tok::l_brace &&
750 Parent && Parent->isOneOf(tok::l_brace, tok::comma)) {
751 Left->setType(TT_DesignatedInitializerLSquare);
752 }
else if (IsCSharpAttributeSpecifier) {
753 Left->setType(TT_AttributeLSquare);
754 }
else if (CurrentToken->is(tok::r_square) && Parent &&
755 Parent->is(TT_TemplateCloser)) {
756 Left->setType(TT_ArraySubscriptLSquare);
757 }
else if (Style.isProto()) {
784 Left->setType(TT_ArrayInitializerLSquare);
785 if (!
Left->endsSequence(tok::l_square, tok::numeric_constant,
787 !
Left->endsSequence(tok::l_square, tok::numeric_constant,
789 !
Left->endsSequence(tok::l_square, tok::colon, TT_SelectorName)) {
790 Left->setType(TT_ProtoExtensionLSquare);
791 BindingIncrease = 10;
793 }
else if (!CppArrayTemplates && Parent &&
794 Parent->isOneOf(TT_BinaryOperator, TT_TemplateCloser, tok::at,
795 tok::comma, tok::l_paren, tok::l_square,
796 tok::question, tok::colon, tok::kw_return,
799 Left->setType(TT_ArrayInitializerLSquare);
801 BindingIncrease = 10;
802 Left->setType(TT_ArraySubscriptLSquare);
806 ScopedContextCreator ContextCreator(*
this, tok::l_square, BindingIncrease);
807 Contexts.back().IsExpression =
true;
808 if (Style.isJavaScript() && Parent && Parent->is(TT_JsTypeColon))
809 Contexts.back().IsExpression =
false;
811 Contexts.back().ColonIsObjCMethodExpr = StartsObjCMethodExpr;
812 Contexts.back().InCpp11AttributeSpecifier = IsCpp11AttributeSpecifier;
813 Contexts.back().InCSharpAttributeSpecifier = IsCSharpAttributeSpecifier;
815 while (CurrentToken) {
816 if (CurrentToken->is(tok::r_square)) {
817 if (IsCpp11AttributeSpecifier && !IsInnerSquare) {
818 CurrentToken->setType(TT_AttributeRSquare);
819 CurrentToken->EndsCppAttributeGroup =
true;
821 if (IsCSharpAttributeSpecifier) {
822 CurrentToken->setType(TT_AttributeRSquare);
823 }
else if (((CurrentToken->Next &&
824 CurrentToken->Next->is(tok::l_paren)) ||
825 (CurrentToken->Previous &&
826 CurrentToken->Previous->Previous == Left)) &&
827 Left->is(TT_ObjCMethodExpr)) {
832 StartsObjCMethodExpr =
false;
833 Left->setType(TT_Unknown);
835 if (StartsObjCMethodExpr && CurrentToken->Previous != Left) {
836 CurrentToken->setType(TT_ObjCMethodExpr);
839 if (!ColonFound && CurrentToken->Previous &&
840 CurrentToken->Previous->is(TT_Unknown) &&
841 canBeObjCSelectorComponent(*CurrentToken->Previous)) {
842 CurrentToken->Previous->setType(TT_SelectorName);
847 if (Parent && Parent->is(TT_PointerOrReference))
848 Parent->overwriteFixedType(TT_BinaryOperator);
850 Left->MatchingParen = CurrentToken;
851 CurrentToken->MatchingParen =
Left;
856 if (!Contexts.back().FirstObjCSelectorName) {
859 Previous->ObjCSelectorNameParts = 1;
860 Contexts.back().FirstObjCSelectorName =
Previous;
863 Left->ParameterCount =
864 Contexts.back().FirstObjCSelectorName->ObjCSelectorNameParts;
866 if (Contexts.back().FirstObjCSelectorName) {
867 Contexts.back().FirstObjCSelectorName->LongestObjCSelectorName =
868 Contexts.back().LongestObjCSelectorName;
869 if (
Left->BlockParameterCount > 1)
870 Contexts.back().FirstObjCSelectorName->LongestObjCSelectorName = 0;
872 if (Style.isTableGen() &&
Left->is(TT_TableGenListOpener))
873 CurrentToken->setType(TT_TableGenListCloser);
877 if (CurrentToken->isOneOf(tok::r_paren, tok::r_brace))
879 if (CurrentToken->is(tok::colon)) {
880 if (IsCpp11AttributeSpecifier &&
881 CurrentToken->endsSequence(tok::colon, tok::identifier,
885 CurrentToken->setType(TT_AttributeColon);
886 }
else if (!Style.isVerilog() && !Line.InPragmaDirective &&
887 Left->isOneOf(TT_ArraySubscriptLSquare,
888 TT_DesignatedInitializerLSquare)) {
889 Left->setType(TT_ObjCMethodExpr);
890 StartsObjCMethodExpr =
true;
891 Contexts.back().ColonIsObjCMethodExpr =
true;
892 if (Parent && Parent->is(tok::r_paren)) {
894 Parent->setType(TT_CastRParen);
899 if (CurrentToken->is(tok::comma) &&
Left->is(TT_ObjCMethodExpr) &&
901 Left->setType(TT_ArrayInitializerLSquare);
904 if (Style.isTableGen()) {
905 if (CurrentToken->isOneOf(tok::comma, tok::minus, tok::ellipsis)) {
911 if (!parseTableGenValue())
914 updateParameterCount(Left,
Tok);
919 updateParameterCount(Left,
Tok);
924 void skipToNextNonComment() {
926 while (CurrentToken && CurrentToken->is(tok::comment))
935 bool parseTableGenValue(
bool ParseNameMode =
false) {
938 while (CurrentToken->is(tok::comment))
940 if (!parseTableGenSimpleValue())
945 if (CurrentToken->is(tok::hash)) {
946 if (CurrentToken->Next &&
947 CurrentToken->Next->isOneOf(tok::colon, tok::semi, tok::l_brace)) {
950 CurrentToken->setType(TT_TableGenTrailingPasteOperator);
955 skipToNextNonComment();
956 HashTok->setType(TT_Unknown);
957 if (!parseTableGenValue(ParseNameMode))
964 if (ParseNameMode && CurrentToken->is(tok::l_brace))
967 if (CurrentToken->isOneOf(tok::l_brace, tok::l_square, tok::period)) {
968 CurrentToken->setType(TT_TableGenValueSuffix);
970 skipToNextNonComment();
971 if (Suffix->is(tok::l_square))
972 return parseSquare();
973 if (Suffix->is(tok::l_brace)) {
974 Scopes.push_back(getScopeType(*Suffix));
984 bool tryToParseTableGenTokVar() {
987 if (CurrentToken->is(tok::identifier) &&
988 CurrentToken->TokenText.front() ==
'$') {
989 skipToNextNonComment();
997 bool parseTableGenDAGArg(
bool AlignColon =
false) {
998 if (tryToParseTableGenTokVar())
1000 if (parseTableGenValue()) {
1001 if (CurrentToken && CurrentToken->is(tok::colon)) {
1003 CurrentToken->setType(TT_TableGenDAGArgListColonToAlign);
1005 CurrentToken->setType(TT_TableGenDAGArgListColon);
1006 skipToNextNonComment();
1007 return tryToParseTableGenTokVar();
1018 auto &Opes = Style.TableGenBreakingDAGArgOperators;
1023 if (
Tok.isNot(tok::identifier) ||
1024 Tok.isOneOf(TT_TableGenBangOperator, TT_TableGenCondOperator)) {
1028 if (!
Tok.Next ||
Tok.Next->is(tok::colon))
1030 return llvm::is_contained(Opes,
Tok.TokenText.str());
1035 bool parseTableGenDAGArgAndList(
FormatToken *Opener) {
1037 if (!parseTableGenDAGArg())
1039 bool BreakInside =
false;
1040 if (Style.TableGenBreakInsideDAGArg != FormatStyle::DAS_DontBreak) {
1043 if (isTableGenDAGArgBreakingOperator(*FirstTok)) {
1046 Opener->setType(TT_TableGenDAGArgOpenerToBreak);
1047 if (FirstTok->isOneOf(TT_TableGenBangOperator,
1048 TT_TableGenCondOperator)) {
1051 CurrentToken->Previous->setType(TT_TableGenDAGArgOperatorToBreak);
1052 }
else if (FirstTok->is(tok::identifier)) {
1053 if (Style.TableGenBreakInsideDAGArg == FormatStyle::DAS_BreakAll)
1054 FirstTok->setType(TT_TableGenDAGArgOperatorToBreak);
1056 FirstTok->setType(TT_TableGenDAGArgOperatorID);
1061 return parseTableGenDAGArgList(Opener, BreakInside);
1066 bool parseTableGenDAGArgList(
FormatToken *Opener,
bool BreakInside) {
1067 ScopedContextCreator ContextCreator(*
this, tok::l_paren, 0);
1068 Contexts.back().IsTableGenDAGArgList =
true;
1069 bool FirstDAGArgListElm =
true;
1070 while (CurrentToken) {
1071 if (!FirstDAGArgListElm && CurrentToken->is(tok::comma)) {
1072 CurrentToken->setType(BreakInside ? TT_TableGenDAGArgListCommaToBreak
1073 : TT_TableGenDAGArgListComma);
1074 skipToNextNonComment();
1076 if (CurrentToken && CurrentToken->is(tok::r_paren)) {
1077 CurrentToken->setType(TT_TableGenDAGArgCloser);
1078 Opener->MatchingParen = CurrentToken;
1079 CurrentToken->MatchingParen = Opener;
1080 skipToNextNonComment();
1083 if (!parseTableGenDAGArg(
1085 Style.AlignConsecutiveTableGenBreakingDAGArgColons.Enabled)) {
1088 FirstDAGArgListElm =
false;
1093 bool parseTableGenSimpleValue() {
1094 assert(Style.isTableGen());
1098 skipToNextNonComment();
1100 if (
Tok->isOneOf(tok::numeric_constant, tok::string_literal,
1101 TT_TableGenMultiLineString, tok::kw_true, tok::kw_false,
1102 tok::question, tok::kw_int)) {
1106 if (
Tok->is(tok::l_brace)) {
1107 Scopes.push_back(getScopeType(*
Tok));
1108 return parseBrace();
1111 if (
Tok->is(tok::l_square)) {
1112 Tok->setType(TT_TableGenListOpener);
1115 if (
Tok->is(tok::less)) {
1116 CurrentToken->setType(TT_TemplateOpener);
1117 return parseAngle();
1123 if (
Tok->is(tok::l_paren)) {
1124 Tok->setType(TT_TableGenDAGArgOpener);
1126 if (Contexts.back().IsTableGenDAGArgList)
1127 Tok->SpacesRequiredBefore = 1;
1128 return parseTableGenDAGArgAndList(
Tok);
1131 if (
Tok->is(TT_TableGenBangOperator)) {
1132 if (CurrentToken && CurrentToken->is(tok::less)) {
1133 CurrentToken->setType(TT_TemplateOpener);
1134 skipToNextNonComment();
1138 if (!CurrentToken || CurrentToken->isNot(tok::l_paren))
1142 Contexts.back().IsTableGenBangOpe =
true;
1143 bool Result = parseParens();
1144 Contexts.back().IsTableGenBangOpe =
false;
1148 if (
Tok->is(TT_TableGenCondOperator)) {
1149 if (!CurrentToken || CurrentToken->isNot(tok::l_paren))
1152 return parseParens();
1157 if (
Tok->is(tok::identifier)) {
1159 if (CurrentToken && CurrentToken->is(tok::less)) {
1160 CurrentToken->setType(TT_TemplateOpener);
1161 skipToNextNonComment();
1162 return parseAngle();
1170 bool couldBeInStructArrayInitializer()
const {
1171 if (Contexts.size() < 2)
1175 const auto End = std::next(Contexts.rbegin(), 2);
1176 auto Last = Contexts.rbegin();
1179 if (
Last->ContextKind == tok::l_brace)
1181 return Depth == 2 &&
Last->ContextKind != tok::l_brace;
1188 assert(CurrentToken->Previous);
1189 FormatToken &OpeningBrace = *CurrentToken->Previous;
1190 assert(OpeningBrace.is(tok::l_brace));
1191 OpeningBrace.ParentBracket = Contexts.back().ContextKind;
1193 if (Contexts.back().CaretFound)
1194 OpeningBrace.overwriteFixedType(TT_ObjCBlockLBrace);
1195 Contexts.back().CaretFound =
false;
1197 ScopedContextCreator ContextCreator(*
this, tok::l_brace, 1);
1198 Contexts.back().ColonIsDictLiteral =
true;
1200 Contexts.back().IsExpression =
true;
1201 if (Style.isJavaScript() && OpeningBrace.Previous &&
1202 OpeningBrace.Previous->is(TT_JsTypeColon)) {
1203 Contexts.back().IsExpression =
false;
1205 if (Style.isVerilog() &&
1206 (!OpeningBrace.getPreviousNonComment() ||
1207 OpeningBrace.getPreviousNonComment()->isNot(Keywords.kw_apostrophe))) {
1208 Contexts.back().VerilogMayBeConcatenation =
true;
1210 if (Style.isTableGen())
1211 Contexts.back().ColonIsDictLiteral =
false;
1213 unsigned CommaCount = 0;
1214 while (CurrentToken) {
1215 if (CurrentToken->is(tok::r_brace)) {
1216 assert(!Scopes.empty());
1217 assert(Scopes.back() == getScopeType(OpeningBrace));
1219 assert(OpeningBrace.Optional == CurrentToken->Optional);
1220 OpeningBrace.MatchingParen = CurrentToken;
1221 CurrentToken->MatchingParen = &OpeningBrace;
1222 if (Style.AlignArrayOfStructures != FormatStyle::AIAS_None) {
1223 if (OpeningBrace.ParentBracket == tok::l_brace &&
1224 couldBeInStructArrayInitializer() && CommaCount > 0) {
1225 Contexts.back().ContextType = Context::StructArrayInitializer;
1231 if (CurrentToken->isOneOf(tok::r_paren, tok::r_square))
1233 updateParameterCount(&OpeningBrace, CurrentToken);
1234 if (CurrentToken->isOneOf(tok::colon, tok::l_brace, tok::less)) {
1236 if (
Previous->is(TT_JsTypeOptionalQuestion))
1238 if ((CurrentToken->is(tok::colon) && !Style.isTableGen() &&
1239 (!Contexts.back().ColonIsDictLiteral || !IsCpp)) ||
1241 OpeningBrace.setType(TT_DictLiteral);
1242 if (
Previous->Tok.getIdentifierInfo() ||
1243 Previous->is(tok::string_literal)) {
1244 Previous->setType(TT_SelectorName);
1247 if (CurrentToken->is(tok::colon) && OpeningBrace.is(TT_Unknown) &&
1248 !Style.isTableGen()) {
1249 OpeningBrace.setType(TT_DictLiteral);
1250 }
else if (Style.isJavaScript()) {
1251 OpeningBrace.overwriteFixedType(TT_DictLiteral);
1254 bool IsBracedListComma =
false;
1255 if (CurrentToken->is(tok::comma)) {
1256 if (Style.isJavaScript())
1257 OpeningBrace.overwriteFixedType(TT_DictLiteral);
1262 if (!consumeToken())
1264 if (IsBracedListComma)
1265 Contexts.back().IsExpression =
true;
1274 if (Current->is(tok::l_brace) && Current->is(
BK_Block))
1275 ++
Left->BlockParameterCount;
1276 if (Current->is(tok::comma)) {
1277 ++
Left->ParameterCount;
1279 Left->Role.reset(
new CommaSeparatedList(Style));
1280 Left->Role->CommaFound(Current);
1281 }
else if (
Left->ParameterCount == 0 && Current->isNot(tok::comment)) {
1282 Left->ParameterCount = 1;
1286 bool parseConditional() {
1287 while (CurrentToken) {
1288 if (CurrentToken->is(tok::colon) && CurrentToken->is(TT_Unknown)) {
1289 CurrentToken->setType(TT_ConditionalExpr);
1293 if (!consumeToken())
1299 bool parseTemplateDeclaration() {
1300 if (!CurrentToken || CurrentToken->isNot(tok::less))
1303 CurrentToken->setType(TT_TemplateOpener);
1306 TemplateDeclarationDepth++;
1307 const bool WellFormed = parseAngle();
1308 TemplateDeclarationDepth--;
1312 if (CurrentToken && TemplateDeclarationDepth == 0)
1313 CurrentToken->Previous->ClosesTemplateDeclaration =
true;
1318 bool consumeToken() {
1320 const auto *Prev = CurrentToken->getPreviousNonComment();
1321 if (Prev && Prev->is(TT_AttributeRSquare) &&
1322 CurrentToken->isOneOf(tok::kw_if, tok::kw_switch, tok::kw_case,
1323 tok::kw_default, tok::kw_for, tok::kw_while) &&
1325 CurrentToken->MustBreakBefore =
true;
1332 if (
Tok->is(TT_VerilogTableItem))
1335 if (
Tok->is(TT_TableGenMultiLineString))
1337 auto *Prev =
Tok->getPreviousNonComment();
1338 auto *
Next =
Tok->getNextNonComment();
1339 switch (
bool IsIf =
false;
Tok->Tok.getKind()) {
1342 if (!Prev && Line.MustBeDeclaration)
1343 Tok->setType(TT_ObjCMethodSpecifier);
1350 if (
Tok->isTypeFinalized())
1353 if (Style.isJavaScript()) {
1354 if (Contexts.back().ColonIsForRangeExpr ||
1355 (Contexts.size() == 1 &&
1356 Line.First->isNoneOf(tok::kw_enum, tok::kw_case)) ||
1357 Contexts.back().ContextKind == tok::l_paren ||
1358 Contexts.back().ContextKind == tok::l_square ||
1359 (!Contexts.back().IsExpression &&
1360 Contexts.back().ContextKind == tok::l_brace) ||
1361 (Contexts.size() == 1 &&
1362 Line.MustBeDeclaration)) {
1363 Contexts.back().IsExpression =
false;
1364 Tok->setType(TT_JsTypeColon);
1367 }
else if (Style.isCSharp()) {
1368 if (Contexts.back().InCSharpAttributeSpecifier) {
1369 Tok->setType(TT_AttributeColon);
1372 if (Contexts.back().ContextKind == tok::l_paren) {
1373 Tok->setType(TT_CSharpNamedArgumentColon);
1376 }
else if (Style.isVerilog() &&
Tok->isNot(TT_BinaryOperator)) {
1379 if (Keywords.isVerilogEnd(*Prev) || Keywords.isVerilogBegin(*Prev)) {
1380 Tok->setType(TT_VerilogBlockLabelColon);
1381 }
else if (Contexts.back().ContextKind == tok::l_square) {
1382 Tok->setType(TT_BitFieldColon);
1383 }
else if (Contexts.back().ColonIsDictLiteral) {
1384 Tok->setType(TT_DictLiteral);
1385 }
else if (Contexts.size() == 1) {
1389 Tok->setType(TT_CaseLabelColon);
1390 if (Line.Level > 1 || (!Line.InPPDirective && Line.Level > 0))
1395 if (Line.First->isOneOf(Keywords.kw_module, Keywords.kw_import) ||
1396 Line.First->startsSequence(tok::kw_export, Keywords.kw_module) ||
1397 Line.First->startsSequence(tok::kw_export, Keywords.kw_import)) {
1398 Tok->setType(TT_ModulePartitionColon);
1399 }
else if (Line.First->is(tok::kw_asm)) {
1400 Tok->setType(TT_InlineASMColon);
1401 }
else if (Contexts.back().ColonIsDictLiteral || Style.isProto()) {
1402 Tok->setType(TT_DictLiteral);
1403 if (Style.isTextProto())
1404 Prev->setType(TT_SelectorName);
1405 }
else if (Contexts.back().ColonIsObjCMethodExpr ||
1406 Line.startsWith(TT_ObjCMethodSpecifier)) {
1407 Tok->setType(TT_ObjCMethodExpr);
1408 const auto *PrevPrev = Prev->Previous;
1411 bool UnknownIdentifierInMethodDeclaration =
1412 Line.startsWith(TT_ObjCMethodSpecifier) &&
1413 Prev->is(tok::identifier) && Prev->is(TT_Unknown);
1416 !(PrevPrev->is(TT_CastRParen) ||
1417 (PrevPrev->is(TT_ObjCMethodExpr) && PrevPrev->is(tok::colon))) ||
1418 PrevPrev->is(tok::r_square) ||
1419 Contexts.back().LongestObjCSelectorName == 0 ||
1420 UnknownIdentifierInMethodDeclaration) {
1421 Prev->setType(TT_SelectorName);
1422 if (!Contexts.back().FirstObjCSelectorName)
1423 Contexts.back().FirstObjCSelectorName = Prev;
1424 else if (Prev->ColumnWidth > Contexts.back().LongestObjCSelectorName)
1425 Contexts.back().LongestObjCSelectorName = Prev->ColumnWidth;
1426 Prev->ParameterIndex =
1427 Contexts.back().FirstObjCSelectorName->ObjCSelectorNameParts;
1428 ++Contexts.back().FirstObjCSelectorName->ObjCSelectorNameParts;
1430 }
else if (Contexts.back().ColonIsForRangeExpr) {
1431 Tok->setType(TT_RangeBasedForLoopColon);
1432 for (
auto *Token = Prev;
1433 Token && Token->isNoneOf(tok::semi, tok::l_paren);
1434 Token = Token->Previous) {
1435 if (Token->isPointerOrReference())
1436 Token->setFinalizedType(TT_PointerOrReference);
1438 }
else if (Contexts.back().ContextType == Context::C11GenericSelection) {
1439 Tok->setType(TT_GenericSelectionColon);
1440 if (Prev->isPointerOrReference())
1441 Prev->setFinalizedType(TT_PointerOrReference);
1442 }
else if ((CurrentToken && CurrentToken->is(tok::numeric_constant)) ||
1443 (Prev->is(TT_StartOfName) && !Scopes.empty() &&
1445 Tok->setType(TT_BitFieldColon);
1446 }
else if (Contexts.size() == 1 &&
1447 Line.getFirstNonComment()->isNoneOf(tok::kw_enum, tok::kw_case,
1449 !Line.startsWith(tok::kw_typedef, tok::kw_enum)) {
1450 if (Prev->isOneOf(tok::r_paren, tok::kw_noexcept) ||
1451 Prev->ClosesRequiresClause) {
1452 Tok->setType(TT_CtorInitializerColon);
1453 }
else if (Prev->is(tok::kw_try)) {
1455 FormatToken *PrevPrev = Prev->getPreviousNonComment();
1458 if (PrevPrev && PrevPrev->isOneOf(tok::r_paren, tok::kw_noexcept))
1459 Tok->setType(TT_CtorInitializerColon);
1461 Tok->setType(TT_InheritanceColon);
1462 if (Prev->isAccessSpecifierKeyword())
1465 }
else if (canBeObjCSelectorComponent(*Prev) &&
Next &&
1466 (
Next->isOneOf(tok::r_paren, tok::comma) ||
1467 (canBeObjCSelectorComponent(*
Next) &&
Next->Next &&
1468 Next->Next->is(tok::colon)))) {
1471 Tok->setType(TT_ObjCSelector);
1478 if (Style.isJavaScript() && !Contexts.back().IsExpression)
1479 Tok->setType(TT_JsTypeOperator);
1482 if (Style.isTableGen()) {
1484 if (!parseTableGenValue())
1486 if (CurrentToken && CurrentToken->is(Keywords.kw_then))
1491 CurrentToken->isOneOf(tok::kw_constexpr, tok::identifier)) {
1497 if (CurrentToken && CurrentToken->is(tok::l_paren)) {
1499 if (!parseParens(IsIf))
1504 if (Style.isJavaScript()) {
1506 if ((Prev && Prev->is(tok::period)) || (
Next &&
Next->is(tok::colon)))
1509 if (CurrentToken && CurrentToken->is(Keywords.kw_await))
1512 if (IsCpp && CurrentToken && CurrentToken->is(tok::kw_co_await))
1514 Contexts.back().ColonIsForRangeExpr =
true;
1515 if (!CurrentToken || CurrentToken->isNot(tok::l_paren))
1526 if (Prev && Prev->is(tok::r_paren) && Prev->MatchingParen &&
1527 Prev->MatchingParen->is(TT_OverloadedOperatorLParen)) {
1528 Prev->setType(TT_OverloadedOperator);
1529 Prev->MatchingParen->setType(TT_OverloadedOperator);
1530 Tok->setType(TT_OverloadedOperatorLParen);
1533 if (Style.isVerilog()) {
1539 auto IsInstancePort = [&]() {
1548 if (!Prev || !(PrevPrev = Prev->getPreviousNonComment()))
1551 if (Keywords.isVerilogIdentifier(*Prev) &&
1552 Keywords.isVerilogIdentifier(*PrevPrev)) {
1556 if (Prev->is(Keywords.kw_verilogHash) &&
1557 Keywords.isVerilogIdentifier(*PrevPrev)) {
1561 if (Keywords.isVerilogIdentifier(*Prev) && PrevPrev->is(tok::r_paren))
1564 if (Keywords.isVerilogIdentifier(*Prev) && PrevPrev->is(tok::comma)) {
1565 const FormatToken *PrevParen = PrevPrev->getPreviousNonComment();
1566 if (PrevParen && PrevParen->is(tok::r_paren) &&
1567 PrevParen->MatchingParen &&
1568 PrevParen->MatchingParen->is(TT_VerilogInstancePortLParen)) {
1575 if (IsInstancePort())
1576 Tok->setType(TT_VerilogInstancePortLParen);
1581 if (Line.MustBeDeclaration && Contexts.size() == 1 &&
1582 !Contexts.back().IsExpression && !Line.startsWith(TT_ObjCProperty) &&
1583 !Line.startsWith(tok::l_paren) &&
1584 Tok->isNoneOf(TT_TypeDeclarationParen, TT_RequiresExpressionLParen)) {
1586 (!Prev->isAttribute() &&
1587 Prev->isNoneOf(TT_RequiresClause, TT_LeadingJavaAnnotation,
1588 TT_BinaryOperator))) {
1589 Line.MightBeFunctionDecl =
true;
1590 Tok->MightBeFunctionDeclParen =
true;
1595 if (Style.isTableGen())
1596 Tok->setType(TT_TableGenListOpener);
1602 if (
Tok->is(TT_RequiresExpressionLBrace))
1604 }
else if (Style.isTextProto()) {
1605 if (Prev && Prev->isNot(TT_DictLiteral))
1606 Prev->setType(TT_SelectorName);
1608 Scopes.push_back(getScopeType(*
Tok));
1614 Tok->setType(TT_TemplateOpener);
1620 if (Style.isTextProto() ||
1621 (Style.Language == FormatStyle::LK_Proto && Prev &&
1622 Prev->isOneOf(TT_SelectorName, TT_DictLiteral))) {
1623 Tok->setType(TT_DictLiteral);
1624 if (Prev && Prev->isNot(TT_DictLiteral))
1625 Prev->setType(TT_SelectorName);
1627 if (Style.isTableGen())
1628 Tok->setType(TT_TemplateOpener);
1630 Tok->setType(TT_BinaryOperator);
1631 NonTemplateLess.insert(
Tok);
1641 if (!Scopes.empty())
1648 if (!Style.isTextProto() &&
Tok->is(TT_Unknown))
1649 Tok->setType(TT_BinaryOperator);
1650 if (Prev && Prev->is(TT_TemplateCloser))
1651 Tok->SpacesRequiredBefore = 1;
1653 case tok::kw_operator:
1654 if (Style.isProto())
1657 if (IsCpp && CurrentToken) {
1658 const auto *Info = CurrentToken->Tok.getIdentifierInfo();
1660 if (Info && !(CurrentToken->isPlacementOperator() ||
1661 CurrentToken->is(tok::kw_co_await) ||
1662 Info->isCPlusPlusOperatorKeyword())) {
1664 if (CurrentToken->startsSequence(tok::kw_decltype, tok::l_paren,
1665 tok::kw_auto, tok::r_paren)) {
1667 LParen = CurrentToken->Next->Next->Next->Next;
1670 for (LParen = CurrentToken->Next;
1671 LParen && LParen->isNot(tok::l_paren); LParen = LParen->Next) {
1672 if (LParen->isPointerOrReference())
1673 LParen->setFinalizedType(TT_PointerOrReference);
1676 if (LParen && LParen->is(tok::l_paren)) {
1677 if (!Contexts.back().IsExpression) {
1678 Tok->setFinalizedType(TT_FunctionDeclarationName);
1679 LParen->setFinalizedType(TT_FunctionDeclarationLParen);
1685 while (CurrentToken &&
1686 CurrentToken->isNoneOf(tok::l_paren, tok::semi, tok::r_paren)) {
1687 if (CurrentToken->isOneOf(tok::star, tok::amp))
1688 CurrentToken->setType(TT_PointerOrReference);
1689 auto Next = CurrentToken->getNextNonComment();
1692 if (
Next->is(tok::less))
1698 auto Previous = CurrentToken->getPreviousNonComment();
1700 if (CurrentToken->is(tok::comma) &&
Previous->isNot(tok::kw_operator))
1702 if (
Previous->isOneOf(TT_BinaryOperator, TT_UnaryOperator, tok::comma,
1704 Previous->isPointerOrReference() ||
1706 Previous->TokenText.starts_with(
"\"\"")) {
1707 Previous->setType(TT_OverloadedOperator);
1708 if (CurrentToken->isOneOf(tok::less, tok::greater))
1712 if (CurrentToken && CurrentToken->is(tok::l_paren))
1713 CurrentToken->setType(TT_OverloadedOperatorLParen);
1714 if (CurrentToken && CurrentToken->Previous->is(TT_BinaryOperator))
1715 CurrentToken->Previous->setType(TT_OverloadedOperator);
1718 if (Style.isJavaScript() &&
Next &&
1719 Next->isOneOf(tok::semi, tok::comma, tok::colon, tok::r_paren,
1720 tok::r_brace, tok::r_square)) {
1725 Tok->setType(TT_JsTypeOptionalQuestion);
1730 if (Line.MustBeDeclaration && !Contexts.back().IsExpression &&
1731 Style.isJavaScript()) {
1734 if (Style.isCSharp()) {
1737 if (
Next && (
Next->isOneOf(tok::r_paren, tok::greater) ||
1738 Next->startsSequence(tok::identifier, tok::semi) ||
1739 Next->startsSequence(tok::identifier, tok::equal))) {
1740 Tok->setType(TT_CSharpNullable);
1749 if (!Contexts.back().IsExpression && Line.MustBeDeclaration &&
1750 (!
Next ||
Next->isNoneOf(tok::identifier, tok::string_literal) ||
1751 !
Next->Next ||
Next->Next->isNoneOf(tok::colon, tok::question))) {
1752 Tok->setType(TT_CSharpNullable);
1758 case tok::kw_template:
1759 parseTemplateDeclaration();
1762 switch (Contexts.back().ContextType) {
1763 case Context::CtorInitializer:
1764 Tok->setType(TT_CtorInitializerComma);
1766 case Context::InheritanceList:
1767 Tok->setType(TT_InheritanceComma);
1769 case Context::VerilogInstancePortList:
1770 Tok->setType(TT_VerilogInstancePortComma);
1773 if (Style.isVerilog() && Contexts.size() == 1 &&
1774 Line.startsWith(Keywords.kw_assign)) {
1775 Tok->setFinalizedType(TT_VerilogAssignComma);
1776 }
else if (Contexts.back().FirstStartOfName &&
1777 (Contexts.size() == 1 || startsWithInitStatement(Line))) {
1778 Contexts.back().FirstStartOfName->PartOfMultiVariableDeclStmt =
true;
1779 Line.IsMultiVariableDeclStmt =
true;
1783 if (Contexts.back().ContextType == Context::ForEachMacro)
1784 Contexts.back().IsExpression =
true;
1786 case tok::kw_default:
1788 if (Style.isVerilog() && Keywords.isVerilogEndOfLabel(*
Tok) &&
1789 (Line.Level > 1 || (!Line.InPPDirective && Line.Level > 0))) {
1793 case tok::identifier:
1794 if (
Tok->isOneOf(Keywords.kw___has_include,
1795 Keywords.kw___has_include_next)) {
1799 if (
Next &&
Next->is(tok::l_paren) && Prev &&
1800 Prev->isOneOf(tok::kw___cdecl, tok::kw___stdcall,
1801 tok::kw___fastcall, tok::kw___thiscall,
1802 tok::kw___regcall, tok::kw___vectorcall)) {
1803 Tok->setFinalizedType(TT_FunctionDeclarationName);
1804 Next->setFinalizedType(TT_FunctionDeclarationLParen);
1806 }
else if (Style.isCSharp()) {
1807 if (
Tok->is(Keywords.kw_where) &&
Next &&
Next->isNot(tok::l_paren)) {
1808 Tok->setType(TT_CSharpGenericTypeConstraint);
1809 parseCSharpGenericTypeConstraint();
1811 Line.IsContinuation =
true;
1813 }
else if (Style.isTableGen()) {
1814 if (
Tok->is(Keywords.kw_assert)) {
1815 if (!parseTableGenValue())
1817 }
else if (
Tok->isOneOf(Keywords.kw_def, Keywords.kw_defm) &&
1818 (!
Next ||
Next->isNoneOf(tok::colon, tok::l_brace))) {
1820 if (!parseTableGenValue(
true))
1824 if (Style.AllowBreakBeforeQtProperty &&
1825 Contexts.back().ContextType == Context::QtProperty &&
1826 Tok->isQtProperty()) {
1827 Tok->setFinalizedType(TT_QtProperty);
1831 if (
Tok->isNot(TT_LambdaArrow) && Prev && Prev->is(tok::kw_noexcept))
1832 Tok->setType(TT_TrailingReturnArrow);
1836 if (Style.isTableGen() && !parseTableGenValue())
1845 void parseCSharpGenericTypeConstraint() {
1846 int OpenAngleBracketsCount = 0;
1847 while (CurrentToken) {
1848 if (CurrentToken->is(tok::less)) {
1850 CurrentToken->setType(TT_TemplateOpener);
1851 ++OpenAngleBracketsCount;
1853 }
else if (CurrentToken->is(tok::greater)) {
1854 CurrentToken->setType(TT_TemplateCloser);
1855 --OpenAngleBracketsCount;
1857 }
else if (CurrentToken->is(tok::comma) && OpenAngleBracketsCount == 0) {
1860 CurrentToken->setType(TT_CSharpGenericTypeConstraintComma);
1862 }
else if (CurrentToken->is(Keywords.kw_where)) {
1863 CurrentToken->setType(TT_CSharpGenericTypeConstraint);
1865 }
else if (CurrentToken->is(tok::colon)) {
1866 CurrentToken->setType(TT_CSharpGenericTypeConstraintColon);
1874 void parseIncludeDirective() {
1875 if (CurrentToken && CurrentToken->is(tok::less)) {
1877 while (CurrentToken) {
1880 if (CurrentToken->isNot(tok::comment) &&
1881 !CurrentToken->TokenText.starts_with(
"//")) {
1882 CurrentToken->setType(TT_ImplicitStringLiteral);
1889 void parseWarningOrError() {
1894 while (CurrentToken) {
1895 CurrentToken->setType(TT_ImplicitStringLiteral);
1900 void parsePragma() {
1903 CurrentToken->isOneOf(Keywords.kw_mark, Keywords.kw_option,
1904 Keywords.kw_region)) {
1905 bool IsMarkOrRegion =
1906 CurrentToken->isOneOf(Keywords.kw_mark, Keywords.kw_region);
1909 while (CurrentToken) {
1910 if (IsMarkOrRegion || CurrentToken->Previous->is(TT_BinaryOperator))
1911 CurrentToken->setType(TT_ImplicitStringLiteral);
1917 void parseHasInclude() {
1918 if (!CurrentToken || CurrentToken->isNot(tok::l_paren))
1921 parseIncludeDirective();
1925 LineType parsePreprocessorDirective() {
1926 bool IsFirstToken = CurrentToken->IsFirst;
1932 if (Style.isJavaScript() && IsFirstToken) {
1936 while (CurrentToken) {
1938 CurrentToken->setType(TT_ImplicitStringLiteral);
1944 if (CurrentToken->is(tok::numeric_constant)) {
1945 CurrentToken->SpacesRequiredBefore = 1;
1950 if (!CurrentToken->Tok.getIdentifierInfo())
1954 if (Style.isVerilog() && !Keywords.isVerilogPPDirective(*CurrentToken))
1956 switch (CurrentToken->Tok.getIdentifierInfo()->getPPKeywordID()) {
1957 case tok::pp_include:
1958 case tok::pp_include_next:
1959 case tok::pp_import:
1961 parseIncludeDirective();
1965 case tok::pp_warning:
1966 parseWarningOrError();
1968 case tok::pp_pragma:
1973 Contexts.back().IsExpression =
true;
1976 CurrentToken->SpacesRequiredBefore = 1;
1982 while (CurrentToken) {
1985 if (
Tok->is(tok::l_paren)) {
1987 }
else if (
Tok->isOneOf(Keywords.kw___has_include,
1988 Keywords.kw___has_include_next)) {
1999 NonTemplateLess.clear();
2000 if (!Line.InMacroBody && CurrentToken->is(tok::hash)) {
2004 auto Type = parsePreprocessorDirective();
2012 IdentifierInfo *Info = CurrentToken->Tok.getIdentifierInfo();
2013 if ((Style.isJava() && CurrentToken->is(Keywords.kw_package)) ||
2014 (!Style.isVerilog() && Info &&
2015 Info->getPPKeywordID() == tok::pp_import && CurrentToken->Next &&
2016 CurrentToken->Next->isOneOf(tok::string_literal, tok::identifier,
2019 parseIncludeDirective();
2025 if (CurrentToken->is(tok::less) && Line.Last->is(tok::greater)) {
2026 parseIncludeDirective();
2032 if (Style.Language == FormatStyle::LK_Proto && Line.Level == 0 &&
2033 CurrentToken->isOneOf(Keywords.kw_option, Keywords.kw_package)) {
2035 if (CurrentToken && CurrentToken->is(tok::identifier)) {
2036 while (CurrentToken)
2042 bool KeywordVirtualFound =
false;
2043 bool ImportStatement =
false;
2046 if (Style.isJavaScript() && CurrentToken->is(Keywords.kw_import))
2047 ImportStatement =
true;
2049 while (CurrentToken) {
2050 if (CurrentToken->is(tok::kw_virtual))
2051 KeywordVirtualFound =
true;
2052 if (Style.isJavaScript()) {
2059 if (Line.First->is(tok::kw_export) &&
2060 CurrentToken->is(Keywords.kw_from) && CurrentToken->Next &&
2061 CurrentToken->Next->isStringLiteral()) {
2062 ImportStatement =
true;
2064 if (isClosureImportStatement(*CurrentToken))
2065 ImportStatement =
true;
2067 if (!consumeToken())
2075 if (KeywordVirtualFound)
2077 if (ImportStatement)
2080 if (Line.startsWith(TT_ObjCMethodSpecifier)) {
2081 if (Contexts.back().FirstObjCSelectorName) {
2082 Contexts.back().FirstObjCSelectorName->LongestObjCSelectorName =
2083 Contexts.back().LongestObjCSelectorName;
2088 for (
const auto &ctx : Contexts)
2089 if (ctx.ContextType == Context::StructArrayInitializer)
2099 return Tok.TokenText ==
"goog" &&
Tok.Next &&
Tok.Next->is(tok::period) &&
2101 (
Tok.Next->Next->TokenText ==
"module" ||
2102 Tok.Next->Next->TokenText ==
"provide" ||
2103 Tok.Next->Next->TokenText ==
"require" ||
2104 Tok.Next->Next->TokenText ==
"requireType" ||
2105 Tok.Next->Next->TokenText ==
"forwardDeclare") &&
2106 Tok.Next->Next->Next &&
Tok.Next->Next->Next->is(tok::l_paren);
2109 void resetTokenMetadata() {
2115 if (!CurrentToken->isTypeFinalized() &&
2116 CurrentToken->isNoneOf(
2117 TT_LambdaLSquare, TT_LambdaLBrace, TT_AttributeMacro, TT_IfMacro,
2118 TT_ForEachMacro, TT_TypenameMacro, TT_FunctionLBrace,
2119 TT_ImplicitStringLiteral, TT_InlineASMBrace, TT_FatArrow,
2120 TT_LambdaArrow, TT_NamespaceMacro, TT_OverloadedOperator,
2121 TT_RegexLiteral, TT_TemplateString, TT_ObjCStringLiteral,
2122 TT_UntouchableMacroFunc, TT_StatementAttributeLikeMacro,
2123 TT_FunctionLikeOrFreestandingMacro, TT_ClassLBrace, TT_EnumLBrace,
2124 TT_RecordLBrace, TT_StructLBrace, TT_UnionLBrace, TT_RequiresClause,
2125 TT_RequiresClauseInARequiresExpression, TT_RequiresExpression,
2126 TT_RequiresExpressionLParen, TT_RequiresExpressionLBrace,
2127 TT_CompoundRequirementLBrace, TT_BracedListLBrace,
2128 TT_FunctionLikeMacro)) {
2129 CurrentToken->setType(TT_Unknown);
2131 CurrentToken->Role.reset();
2132 CurrentToken->MatchingParen =
nullptr;
2133 CurrentToken->FakeLParens.clear();
2134 CurrentToken->FakeRParens = 0;
2141 CurrentToken->NestingLevel = Contexts.size() - 1;
2142 CurrentToken->BindingStrength = Contexts.back().BindingStrength;
2143 modifyContext(*CurrentToken);
2144 determineTokenType(*CurrentToken);
2145 CurrentToken = CurrentToken->Next;
2147 resetTokenMetadata();
2155 : ContextKind(ContextKind), BindingStrength(BindingStrength),
2156 IsExpression(IsExpression) {}
2159 unsigned BindingStrength;
2161 unsigned LongestObjCSelectorName = 0;
2162 bool ColonIsForRangeExpr =
false;
2163 bool ColonIsDictLiteral =
false;
2164 bool ColonIsObjCMethodExpr =
false;
2167 bool CanBeExpression =
true;
2168 bool CaretFound =
false;
2169 bool InCpp11AttributeSpecifier =
false;
2170 bool InCSharpAttributeSpecifier =
false;
2171 bool InStaticAssertFirstArgument =
false;
2172 bool VerilogAssignmentFound =
false;
2175 bool VerilogMayBeConcatenation =
false;
2176 bool IsTableGenDAGArgList =
false;
2177 bool IsTableGenBangOpe =
false;
2178 bool IsTableGenCondOpe =
false;
2191 StructArrayInitializer,
2195 C11GenericSelection,
2198 VerilogInstancePortList,
2199 } ContextType = Unknown;
2204 struct ScopedContextCreator {
2205 AnnotatingParser &P;
2207 ScopedContextCreator(AnnotatingParser &P,
tok::TokenKind ContextKind,
2210 P.Contexts.push_back(Context(ContextKind,
2211 P.Contexts.back().BindingStrength + Increase,
2212 P.Contexts.back().IsExpression));
2215 ~ScopedContextCreator() {
2216 if (P.Style.AlignArrayOfStructures != FormatStyle::AIAS_None) {
2217 if (P.Contexts.back().ContextType == Context::StructArrayInitializer) {
2218 P.Contexts.pop_back();
2219 P.Contexts.back().ContextType = Context::StructArrayInitializer;
2223 P.Contexts.pop_back();
2228 auto AssignmentStartsExpression = [&]() {
2232 if (Line.First->isOneOf(tok::kw_using, tok::kw_return))
2234 if (Line.First->is(tok::kw_template)) {
2235 assert(Current.Previous);
2236 if (Current.Previous->is(tok::kw_operator)) {
2244 if (
Tok->isNot(TT_TemplateOpener)) {
2251 if (Contexts.back().ContextKind == tok::less) {
2252 assert(Current.Previous->Previous);
2253 return Current.Previous->Previous->isNoneOf(tok::kw_typename,
2257 Tok =
Tok->MatchingParen;
2260 Tok =
Tok->getNextNonComment();
2264 if (
Tok->isOneOf(tok::kw_class, tok::kw_enum, tok::kw_struct,
2274 if (Style.isJavaScript() &&
2275 (Line.startsWith(Keywords.kw_type, tok::identifier) ||
2276 Line.startsWith(tok::kw_export, Keywords.kw_type,
2277 tok::identifier))) {
2281 return !Current.Previous || Current.Previous->isNot(tok::kw_operator);
2284 if (AssignmentStartsExpression()) {
2285 Contexts.back().IsExpression =
true;
2286 if (!Line.startsWith(TT_UnaryOperator)) {
2289 Previous->Previous->isNoneOf(tok::comma, tok::semi);
2291 if (
Previous->isOneOf(tok::r_square, tok::r_paren, tok::greater)) {
2298 if (
Previous->isOneOf(TT_BinaryOperator, TT_UnaryOperator) &&
2300 Previous->Previous->isNot(tok::equal)) {
2301 Previous->setType(TT_PointerOrReference);
2305 }
else if (Current.is(tok::lessless) &&
2306 (!Current.Previous ||
2307 Current.Previous->isNot(tok::kw_operator))) {
2308 Contexts.back().IsExpression =
true;
2309 }
else if (Current.isOneOf(tok::kw_return, tok::kw_throw)) {
2310 Contexts.back().IsExpression =
true;
2311 }
else if (Current.is(TT_TrailingReturnArrow)) {
2312 Contexts.back().IsExpression =
false;
2313 }
else if (Current.isOneOf(TT_LambdaArrow, Keywords.kw_assert)) {
2314 Contexts.back().IsExpression = Style.isJava();
2315 }
else if (Current.Previous &&
2316 Current.Previous->is(TT_CtorInitializerColon)) {
2317 Contexts.back().IsExpression =
true;
2318 Contexts.back().ContextType = Context::CtorInitializer;
2319 }
else if (Current.Previous && Current.Previous->is(TT_InheritanceColon)) {
2320 Contexts.back().ContextType = Context::InheritanceList;
2321 }
else if (Current.isOneOf(tok::r_paren, tok::greater, tok::comma)) {
2325 Previous->setType(TT_PointerOrReference);
2327 if (Line.MustBeDeclaration &&
2328 Contexts.front().ContextType != Context::CtorInitializer) {
2329 Contexts.back().IsExpression =
false;
2331 }
else if (Current.is(tok::kw_new)) {
2332 Contexts.back().CanBeExpression =
false;
2333 }
else if (Current.is(tok::semi) ||
2334 (Current.is(tok::exclaim) && Current.Previous &&
2335 Current.Previous->isNot(tok::kw_operator))) {
2339 Contexts.back().IsExpression =
true;
2347 if (Current->is(tok::l_paren))
2349 if (Current->is(tok::r_paren))
2353 Current = Current->Next;
2358 static bool isDeductionGuide(
FormatToken &Current) {
2360 if (Current.Previous && Current.Previous->is(tok::r_paren) &&
2361 Current.startsSequence(tok::arrow, tok::identifier, tok::less)) {
2365 while (TemplateCloser) {
2367 if (TemplateCloser->is(tok::l_paren)) {
2369 TemplateCloser = untilMatchingParen(TemplateCloser);
2370 if (!TemplateCloser)
2373 if (TemplateCloser->is(tok::less))
2375 if (TemplateCloser->is(tok::greater))
2379 TemplateCloser = TemplateCloser->Next;
2383 if (TemplateCloser && TemplateCloser->Next &&
2384 TemplateCloser->Next->is(tok::semi) &&
2385 Current.Previous->MatchingParen) {
2389 Current.Previous->MatchingParen->Previous;
2391 return LeadingIdentifier &&
2392 LeadingIdentifier->TokenText == Current.Next->TokenText;
2399 if (Current.isNot(TT_Unknown)) {
2404 if ((Style.isJavaScript() || Style.isCSharp()) &&
2405 Current.is(tok::exclaim)) {
2406 if (Current.Previous) {
2408 Style.isJavaScript()
2409 ? Keywords.isJavaScriptIdentifier(
2410 *Current.Previous,
true)
2411 : Current.Previous->is(tok::identifier);
2413 Current.Previous->isOneOf(
2414 tok::kw_default, tok::kw_namespace, tok::r_paren, tok::r_square,
2415 tok::r_brace, tok::kw_false, tok::kw_true, Keywords.kw_type,
2416 Keywords.kw_get, Keywords.kw_init, Keywords.kw_set) ||
2417 Current.Previous->Tok.isLiteral()) {
2418 Current.setType(TT_NonNullAssertion);
2423 Current.Next->isOneOf(TT_BinaryOperator, Keywords.kw_as)) {
2424 Current.setType(TT_NonNullAssertion);
2432 if ((Style.isJavaScript() || Style.isJava()) &&
2433 Current.is(Keywords.kw_instanceof)) {
2434 Current.setType(TT_BinaryOperator);
2435 }
else if (isStartOfName(Current) &&
2436 (!Line.MightBeFunctionDecl || Current.NestingLevel != 0)) {
2437 Contexts.back().FirstStartOfName = &Current;
2438 Current.setType(TT_StartOfName);
2439 }
else if (Current.is(tok::semi)) {
2443 Contexts.back().FirstStartOfName =
nullptr;
2444 }
else if (Current.isOneOf(tok::kw_auto, tok::kw___auto_type)) {
2446 }
else if (Current.is(tok::arrow) && Style.isJava()) {
2447 Current.setType(TT_LambdaArrow);
2448 }
else if (Current.is(tok::arrow) && Style.isVerilog()) {
2450 Current.setType(TT_BinaryOperator);
2451 }
else if (Current.is(tok::arrow) && AutoFound &&
2452 Line.MightBeFunctionDecl && Current.NestingLevel == 0 &&
2453 Current.Previous->isNoneOf(tok::kw_operator, tok::identifier)) {
2455 Current.setType(TT_TrailingReturnArrow);
2456 }
else if (Current.is(tok::arrow) && Current.Previous &&
2457 Current.Previous->is(tok::r_brace) &&
2461 Current.setType(TT_TrailingReturnArrow);
2462 }
else if (isDeductionGuide(Current)) {
2464 Current.setType(TT_TrailingReturnArrow);
2465 }
else if (Current.isPointerOrReference()) {
2466 Current.setType(determineStarAmpUsage(
2468 (Contexts.back().CanBeExpression && Contexts.back().IsExpression) ||
2469 Contexts.back().InStaticAssertFirstArgument,
2470 Contexts.back().ContextType == Context::TemplateArgument));
2471 }
else if (Current.isOneOf(tok::minus, tok::plus, tok::caret) ||
2472 (Style.isVerilog() && Current.is(tok::pipe))) {
2473 Current.setType(determinePlusMinusCaretUsage(Current));
2474 if (Current.is(TT_UnaryOperator) && Current.is(tok::caret))
2475 Contexts.back().CaretFound =
true;
2476 }
else if (Current.isOneOf(tok::minusminus, tok::plusplus)) {
2477 Current.setType(determineIncrementUsage(Current));
2478 }
else if (Current.isOneOf(tok::exclaim, tok::tilde)) {
2479 Current.setType(TT_UnaryOperator);
2480 }
else if (Current.is(tok::question)) {
2481 if (Style.isJavaScript() && Line.MustBeDeclaration &&
2482 !Contexts.back().IsExpression) {
2485 Current.setType(TT_JsTypeOptionalQuestion);
2486 }
else if (Style.isTableGen()) {
2488 Current.setType(TT_Unknown);
2490 Current.setType(TT_ConditionalExpr);
2492 }
else if (Current.isBinaryOperator() &&
2493 (!Current.Previous || Current.Previous->isNot(tok::l_square)) &&
2494 (Current.isNot(tok::greater) && !Style.isTextProto())) {
2495 if (Style.isVerilog()) {
2496 if (Current.is(tok::lessequal) && Contexts.size() == 1 &&
2497 !Contexts.back().VerilogAssignmentFound) {
2501 Current.setFinalizedType(TT_BinaryOperator);
2504 Contexts.back().VerilogAssignmentFound =
true;
2506 Current.setType(TT_BinaryOperator);
2507 }
else if (Current.is(tok::comment)) {
2508 if (Current.TokenText.starts_with(
"/*")) {
2509 if (Current.TokenText.ends_with(
"*/")) {
2510 Current.setType(TT_BlockComment);
2514 Current.Tok.setKind(tok::unknown);
2517 Current.setType(TT_LineComment);
2519 }
else if (Current.is(tok::string_literal)) {
2520 if (Style.isVerilog() && Contexts.back().VerilogMayBeConcatenation &&
2521 Current.getPreviousNonComment() &&
2522 Current.getPreviousNonComment()->isOneOf(tok::comma, tok::l_brace) &&
2523 Current.getNextNonComment() &&
2524 Current.getNextNonComment()->isOneOf(tok::comma, tok::r_brace)) {
2525 Current.setType(TT_StringInConcatenation);
2527 }
else if (Current.is(tok::l_paren)) {
2528 if (lParenStartsCppCast(Current))
2529 Current.setType(TT_CppCastLParen);
2530 }
else if (Current.is(tok::r_paren)) {
2531 if (rParenEndsCast(Current))
2532 Current.setType(TT_CastRParen);
2533 if (Current.MatchingParen && Current.Next &&
2534 !Current.Next->isBinaryOperator() &&
2535 Current.Next->isNoneOf(
2536 tok::semi, tok::colon, tok::l_brace, tok::l_paren, tok::comma,
2537 tok::period, tok::arrow, tok::coloncolon, tok::kw_noexcept)) {
2538 if (
FormatToken *AfterParen = Current.MatchingParen->Next;
2539 AfterParen && AfterParen->isNot(tok::caret)) {
2541 if (
FormatToken *BeforeParen = Current.MatchingParen->Previous;
2542 BeforeParen && BeforeParen->is(tok::identifier) &&
2543 BeforeParen->isNot(TT_TypenameMacro) &&
2544 BeforeParen->TokenText == BeforeParen->TokenText.upper() &&
2545 (!BeforeParen->Previous ||
2546 BeforeParen->Previous->ClosesTemplateDeclaration ||
2547 BeforeParen->Previous->ClosesRequiresClause)) {
2548 Current.setType(TT_FunctionAnnotationRParen);
2552 }
else if (Current.is(tok::at) && Current.Next && !Style.isJavaScript() &&
2556 switch (Current.Next->Tok.getObjCKeywordID()) {
2557 case tok::objc_interface:
2558 case tok::objc_implementation:
2559 case tok::objc_protocol:
2560 Current.setType(TT_ObjCDecl);
2562 case tok::objc_property:
2563 Current.setType(TT_ObjCProperty);
2568 }
else if (Current.is(tok::period)) {
2569 FormatToken *PreviousNoComment = Current.getPreviousNonComment();
2570 if (PreviousNoComment &&
2571 PreviousNoComment->isOneOf(tok::comma, tok::l_brace)) {
2572 Current.setType(TT_DesignatedInitializerPeriod);
2573 }
else if (Style.isJava() && Current.Previous &&
2574 Current.Previous->isOneOf(TT_JavaAnnotation,
2575 TT_LeadingJavaAnnotation)) {
2576 Current.setType(Current.Previous->getType());
2578 }
else if (canBeObjCSelectorComponent(Current) &&
2581 Current.Previous && Current.Previous->is(TT_CastRParen) &&
2582 Current.Previous->MatchingParen &&
2583 Current.Previous->MatchingParen->Previous &&
2584 Current.Previous->MatchingParen->Previous->is(
2585 TT_ObjCMethodSpecifier)) {
2589 Current.setType(TT_SelectorName);
2590 }
else if (Current.isOneOf(tok::identifier, tok::kw_const, tok::kw_noexcept,
2591 tok::kw_requires) &&
2593 Current.Previous->isNoneOf(tok::equal, tok::at,
2594 TT_CtorInitializerComma,
2595 TT_CtorInitializerColon) &&
2596 Line.MightBeFunctionDecl && Contexts.size() == 1) {
2599 Current.setType(TT_TrailingAnnotation);
2600 }
else if ((Style.isJava() || Style.isJavaScript()) && Current.Previous) {
2601 if (Current.Previous->is(tok::at) &&
2602 Current.isNot(Keywords.kw_interface)) {
2606 Current.setType(TT_LeadingJavaAnnotation);
2608 Current.setType(TT_JavaAnnotation);
2609 }
else if (Current.Previous->is(tok::period) &&
2610 Current.Previous->isOneOf(TT_JavaAnnotation,
2611 TT_LeadingJavaAnnotation)) {
2612 Current.setType(Current.Previous->getType());
2624 if (Style.isVerilog())
2627 if (!
Tok.Previous ||
Tok.isNot(tok::identifier) ||
Tok.is(TT_ClassHeadName))
2630 if (
Tok.endsSequence(Keywords.kw_final, TT_ClassHeadName))
2633 if ((Style.isJavaScript() || Style.isJava()) &&
Tok.is(Keywords.kw_extends))
2636 if (
const auto *NextNonComment =
Tok.getNextNonComment();
2637 (!NextNonComment && !Line.InMacroBody) ||
2639 (NextNonComment->isPointerOrReference() ||
2640 NextNonComment->isOneOf(TT_ClassHeadName, tok::string_literal) ||
2641 (Line.InPragmaDirective && NextNonComment->is(tok::identifier))))) {
2645 if (
Tok.Previous->isOneOf(TT_LeadingJavaAnnotation, Keywords.kw_instanceof,
2649 if (Style.isJavaScript() &&
Tok.Previous->is(Keywords.kw_in))
2656 if (!Style.isJavaScript())
2657 while (PreviousNotConst && PreviousNotConst->is(tok::kw_const))
2658 PreviousNotConst = PreviousNotConst->getPreviousNonComment();
2660 if (!PreviousNotConst)
2663 if (PreviousNotConst->ClosesRequiresClause)
2666 if (Style.isTableGen()) {
2668 if (Keywords.isTableGenDefinition(*PreviousNotConst))
2671 if (Contexts.back().ContextKind != tok::l_brace)
2675 bool IsPPKeyword = PreviousNotConst->is(tok::identifier) &&
2676 PreviousNotConst->Previous &&
2677 PreviousNotConst->Previous->is(tok::hash);
2679 if (PreviousNotConst->is(TT_TemplateCloser)) {
2680 return PreviousNotConst && PreviousNotConst->MatchingParen &&
2681 PreviousNotConst->MatchingParen->Previous &&
2682 PreviousNotConst->MatchingParen->Previous->isNoneOf(
2683 tok::period, tok::kw_template);
2686 if ((PreviousNotConst->is(tok::r_paren) &&
2687 PreviousNotConst->is(TT_TypeDeclarationParen)) ||
2688 PreviousNotConst->is(TT_AttributeRParen)) {
2697 if (PreviousNotConst->isOneOf(tok::identifier, tok::kw_auto) &&
2698 PreviousNotConst->isNot(TT_StatementAttributeLikeMacro)) {
2703 if (PreviousNotConst->is(TT_PointerOrReference) ||
2704 PreviousNotConst->endsSequence(tok::coloncolon,
2705 TT_PointerOrReference)) {
2710 if (PreviousNotConst->isTypeName(LangOpts))
2714 if (Style.isJava() && PreviousNotConst->is(tok::r_square))
2718 return Style.isJavaScript() && PreviousNotConst->is(tok::kw_const);
2728 if (LeftOfParens && LeftOfParens->is(TT_TemplateCloser) &&
2729 LeftOfParens->MatchingParen) {
2730 auto *Prev = LeftOfParens->MatchingParen->getPreviousNonComment();
2732 Prev->isOneOf(tok::kw_const_cast, tok::kw_dynamic_cast,
2733 tok::kw_reinterpret_cast, tok::kw_static_cast)) {
2744 assert(
Tok.is(tok::r_paren));
2746 if (!
Tok.MatchingParen || !
Tok.Previous)
2750 if (!IsCpp && !Style.isCSharp() && !Style.isJava())
2753 const auto *LParen =
Tok.MatchingParen;
2754 const auto *BeforeRParen =
Tok.Previous;
2755 const auto *AfterRParen =
Tok.Next;
2758 if (BeforeRParen == LParen || !AfterRParen)
2761 if (LParen->isOneOf(TT_OverloadedOperatorLParen, TT_FunctionTypeLParen))
2764 auto *LeftOfParens = LParen->getPreviousNonComment();
2768 if (LeftOfParens->is(tok::r_paren) &&
2769 LeftOfParens->isNot(TT_CastRParen)) {
2770 if (!LeftOfParens->MatchingParen ||
2771 !LeftOfParens->MatchingParen->Previous) {
2774 LeftOfParens = LeftOfParens->MatchingParen->Previous;
2777 if (LeftOfParens->is(tok::r_square)) {
2780 if (
Tok->isNot(tok::r_square))
2783 Tok =
Tok->getPreviousNonComment();
2784 if (!
Tok ||
Tok->isNot(tok::l_square))
2787 Tok =
Tok->getPreviousNonComment();
2788 if (!
Tok ||
Tok->isNot(tok::kw_delete))
2792 if (
FormatToken *MaybeDelete = MayBeArrayDelete(LeftOfParens))
2793 LeftOfParens = MaybeDelete;
2799 if (LeftOfParens->Tok.getIdentifierInfo() && LeftOfParens->Previous &&
2800 LeftOfParens->Previous->is(tok::kw_operator)) {
2806 if (LeftOfParens->Tok.getIdentifierInfo() &&
2807 LeftOfParens->isNoneOf(Keywords.kw_in, tok::kw_return, tok::kw_case,
2808 tok::kw_delete, tok::kw_throw)) {
2814 if (LeftOfParens->isOneOf(tok::at, tok::r_square, TT_OverloadedOperator,
2815 TT_TemplateCloser, tok::ellipsis)) {
2820 if (AfterRParen->is(tok::question) ||
2821 (AfterRParen->is(tok::ampamp) && !BeforeRParen->isTypeName(LangOpts))) {
2826 if (AfterRParen->is(Keywords.kw_in) && Style.isCSharp())
2831 if (AfterRParen->isOneOf(tok::kw_noexcept, tok::kw_volatile, tok::kw_const,
2832 tok::kw_requires, tok::kw_throw, tok::arrow,
2833 Keywords.kw_override, Keywords.kw_final) ||
2834 isCppAttribute(IsCpp, *AfterRParen)) {
2840 if (Style.isJava() && AfterRParen->is(tok::l_paren))
2844 if (AfterRParen->isOneOf(tok::kw_sizeof, tok::kw_alignof) ||
2845 (AfterRParen->Tok.isLiteral() &&
2846 AfterRParen->isNot(tok::string_literal))) {
2851 if (
Tok.isNot(TT_TemplateCloser))
2853 const auto *
Less =
Tok.MatchingParen;
2856 const auto *BeforeLess =
Less->getPreviousNonComment();
2857 return BeforeLess && BeforeLess->isNot(TT_VariableTemplate);
2861 auto IsQualifiedPointerOrReference = [](
const FormatToken *T,
2862 const LangOptions &LangOpts) {
2864 assert(!T->isTypeName(LangOpts) &&
"Should have already been checked");
2868 if (T->is(TT_AttributeRParen)) {
2870 assert(T->is(tok::r_paren));
2871 assert(T->MatchingParen);
2872 assert(T->MatchingParen->is(tok::l_paren));
2873 assert(T->MatchingParen->is(TT_AttributeLParen));
2874 if (
const auto *
Tok = T->MatchingParen->Previous;
2875 Tok &&
Tok->isAttribute()) {
2879 }
else if (T->is(TT_AttributeRSquare)) {
2881 if (T->MatchingParen && T->MatchingParen->Previous) {
2882 T = T->MatchingParen->Previous;
2885 }
else if (T->canBePointerOrReferenceQualifier()) {
2891 return T && T->is(TT_PointerOrReference);
2894 bool ParensAreType = IsNonVariableTemplate(*BeforeRParen) ||
2895 BeforeRParen->is(TT_TypeDeclarationParen) ||
2896 BeforeRParen->isTypeName(LangOpts) ||
2897 IsQualifiedPointerOrReference(BeforeRParen, LangOpts);
2898 bool ParensCouldEndDecl =
2899 AfterRParen->isOneOf(tok::equal, tok::semi, tok::l_brace, tok::greater);
2900 if (ParensAreType && !ParensCouldEndDecl)
2911 for (
const auto *Token = LParen->Next; Token != &
Tok; Token = Token->Next)
2912 if (Token->is(TT_BinaryOperator))
2917 if (AfterRParen->isOneOf(tok::identifier, tok::kw_this))
2921 if (AfterRParen->is(tok::l_paren)) {
2922 for (
const auto *Prev = BeforeRParen; Prev->is(tok::identifier);) {
2923 Prev = Prev->Previous;
2924 if (Prev->is(tok::coloncolon))
2925 Prev = Prev->Previous;
2931 if (!AfterRParen->Next)
2936 if (Style.Language != FormatStyle::LK_C && AfterRParen->is(tok::l_brace) &&
2944 const bool NextIsAmpOrStar = AfterRParen->isOneOf(tok::amp, tok::star);
2945 if (!(AfterRParen->isUnaryOperator() || NextIsAmpOrStar) ||
2946 AfterRParen->is(tok::plus) ||
2947 AfterRParen->Next->isNoneOf(tok::identifier, tok::numeric_constant)) {
2951 if (NextIsAmpOrStar &&
2952 (AfterRParen->Next->is(tok::numeric_constant) || Line.InPPDirective)) {
2956 if (Line.InPPDirective && AfterRParen->is(tok::minus))
2959 const auto *Prev = BeforeRParen;
2962 if (Prev->is(tok::r_paren)) {
2963 if (Prev->is(TT_CastRParen))
2965 Prev = Prev->MatchingParen;
2968 Prev = Prev->Previous;
2969 if (!Prev || Prev->isNot(tok::r_paren))
2971 Prev = Prev->MatchingParen;
2972 return Prev && Prev->is(TT_FunctionTypeLParen);
2976 for (Prev = BeforeRParen; Prev != LParen; Prev = Prev->Previous)
2977 if (Prev->isNoneOf(tok::kw_const, tok::identifier, tok::coloncolon))
2995 if (PrevToken->isOneOf(
2996 TT_ConditionalExpr, tok::l_paren, tok::comma, tok::colon, tok::semi,
2997 tok::equal, tok::question, tok::l_square, tok::l_brace,
2998 tok::kw_case, tok::kw_co_await, tok::kw_co_return, tok::kw_co_yield,
2999 tok::kw_delete, tok::kw_return, tok::kw_throw)) {
3006 if (PrevToken->is(tok::kw_sizeof))
3010 if (PrevToken->isOneOf(TT_CastRParen, TT_UnaryOperator))
3014 if (PrevToken->is(TT_BinaryOperator))
3022 bool InTemplateArgument) {
3023 if (Style.isJavaScript())
3024 return TT_BinaryOperator;
3027 if (Style.isCSharp() &&
Tok.is(tok::ampamp))
3028 return TT_BinaryOperator;
3030 if (Style.isVerilog()) {
3035 if (
Tok.is(tok::star))
3036 return TT_BinaryOperator;
3037 return determineUnaryOperatorByUsage(
Tok) ? TT_UnaryOperator
3038 : TT_BinaryOperator;
3043 return TT_UnaryOperator;
3044 if (PrevToken->isTypeName(LangOpts))
3045 return TT_PointerOrReference;
3046 if (PrevToken->isPlacementOperator() &&
Tok.is(tok::ampamp))
3047 return TT_BinaryOperator;
3049 auto *NextToken =
Tok.getNextNonComment();
3051 return TT_PointerOrReference;
3052 if (NextToken->is(tok::greater))
3053 return TT_PointerOrReference;
3055 if (InTemplateArgument && NextToken->is(tok::kw_noexcept))
3056 return TT_BinaryOperator;
3058 if (NextToken->isOneOf(tok::arrow, tok::equal, tok::comma, tok::r_paren,
3059 tok::semi, TT_RequiresClause) ||
3060 (NextToken->is(tok::kw_noexcept) && !IsExpression) ||
3061 NextToken->canBePointerOrReferenceQualifier() ||
3062 (NextToken->is(tok::l_brace) && !NextToken->getNextNonComment())) {
3063 return TT_PointerOrReference;
3066 if (PrevToken->is(tok::coloncolon))
3067 return TT_PointerOrReference;
3069 if (PrevToken->is(tok::r_paren) && PrevToken->is(TT_TypeDeclarationParen))
3070 return TT_PointerOrReference;
3072 if (determineUnaryOperatorByUsage(
Tok))
3073 return TT_UnaryOperator;
3075 if (NextToken->is(tok::l_square) && NextToken->isNot(TT_LambdaLSquare))
3076 return TT_PointerOrReference;
3077 if (NextToken->is(tok::kw_operator) && !IsExpression)
3078 return TT_PointerOrReference;
3090 if (PrevToken->is(tok::r_brace) &&
Tok.is(tok::star) &&
3091 !PrevToken->MatchingParen) {
3092 return TT_PointerOrReference;
3095 if (PrevToken->endsSequence(tok::r_square, tok::l_square, tok::kw_delete))
3096 return TT_UnaryOperator;
3098 if (PrevToken->Tok.isLiteral() ||
3099 PrevToken->isOneOf(tok::r_paren, tok::r_square, tok::kw_true,
3100 tok::kw_false, tok::r_brace)) {
3101 return TT_BinaryOperator;
3105 while (NextNonParen && NextNonParen->is(tok::l_paren))
3106 NextNonParen = NextNonParen->getNextNonComment();
3107 if (NextNonParen && (NextNonParen->Tok.isLiteral() ||
3108 NextNonParen->isOneOf(tok::kw_true, tok::kw_false) ||
3109 NextNonParen->isUnaryOperator())) {
3110 return TT_BinaryOperator;
3116 if (InTemplateArgument && NextToken->Tok.isAnyIdentifier())
3117 return TT_BinaryOperator;
3121 if (
Tok.is(tok::ampamp) &&
3122 NextToken->isOneOf(tok::l_paren, tok::star, tok::amp)) {
3123 return TT_BinaryOperator;
3130 if (NextToken->Tok.isAnyIdentifier()) {
3131 auto *NextNextToken = NextToken->getNextNonComment();
3132 if (NextNextToken) {
3133 if (NextNextToken->is(tok::arrow))
3134 return TT_BinaryOperator;
3135 if (NextNextToken->isPointerOrReference() &&
3136 !NextToken->isObjCLifetimeQualifier(Style)) {
3137 NextNextToken->setFinalizedType(TT_BinaryOperator);
3138 return TT_BinaryOperator;
3145 if (IsExpression && !Contexts.back().CaretFound &&
3146 Line.getFirstNonComment()->isNot(
3147 TT_RequiresClauseInARequiresExpression)) {
3148 return TT_BinaryOperator;
3152 if (!Scopes.empty() && Scopes.back() ==
ST_Class)
3153 return TT_PointerOrReference;
3156 auto IsChainedOperatorAmpOrMember = [](
const FormatToken *token) {
3157 return !token || token->isOneOf(tok::amp, tok::period, tok::arrow,
3158 tok::arrowstar, tok::periodstar);
3163 if (
Tok.is(tok::amp) && PrevToken->Tok.isAnyIdentifier() &&
3164 IsChainedOperatorAmpOrMember(PrevToken->getPreviousNonComment()) &&
3165 NextToken && NextToken->Tok.isAnyIdentifier()) {
3166 if (
auto NextNext = NextToken->getNextNonComment();
3168 (IsChainedOperatorAmpOrMember(NextNext) || NextNext->is(tok::semi))) {
3169 return TT_BinaryOperator;
3175 return TT_BinaryOperator;
3178 return TT_PointerOrReference;
3182 if (determineUnaryOperatorByUsage(
Tok))
3183 return TT_UnaryOperator;
3187 return TT_UnaryOperator;
3189 if (PrevToken->is(tok::at))
3190 return TT_UnaryOperator;
3193 return TT_BinaryOperator;
3199 if (!PrevToken || PrevToken->is(TT_CastRParen))
3200 return TT_UnaryOperator;
3201 if (PrevToken->isOneOf(tok::r_paren, tok::r_square, tok::identifier))
3202 return TT_TrailingUnaryOperator;
3204 return TT_UnaryOperator;
3207 SmallVector<Context, 8> Contexts;
3209 const FormatStyle &Style;
3210 AnnotatedLine &Line;
3214 LangOptions LangOpts;
3215 const AdditionalKeywords &Keywords;
3217 SmallVector<ScopeType> &Scopes;
3223 llvm::SmallPtrSet<FormatToken *, 16> NonTemplateLess;
3225 int TemplateDeclarationDepth;
3233class ExpressionParser {
3235 ExpressionParser(
const FormatStyle &Style,
const AdditionalKeywords &Keywords,
3236 AnnotatedLine &Line)
3237 : Style(Style), Keywords(Keywords), Line(Line), Current(Line.
First) {}
3240 void parse(
int Precedence = 0) {
3243 while (Current && (Current->is(tok::kw_return) ||
3244 (Current->is(tok::colon) &&
3245 Current->isOneOf(TT_ObjCMethodExpr, TT_DictLiteral)))) {
3249 if (!Current || Precedence > PrecedenceArrowAndPeriod)
3254 parseConditionalExpr();
3260 if (Precedence == PrecedenceUnaryOperator) {
3261 parseUnaryOperator();
3278 if (Style.isVerilog() && Precedence ==
prec::Comma) {
3279 VerilogFirstOfType =
3280 verilogGroupDecl(VerilogFirstOfType, LatestOperator);
3284 parse(Precedence + 1);
3286 int CurrentPrecedence = getCurrentPrecedence();
3295 if (Style.BreakBinaryOperations.PerOperator.empty() &&
3296 Style.BreakBinaryOperations.Default ==
3297 FormatStyle::BBO_OnePerLine) {
3302 if (Precedence == CurrentPrecedence && Current &&
3303 Current->is(TT_SelectorName)) {
3305 addFakeParenthesis(Start,
prec::Level(Precedence));
3309 if ((Style.isCSharp() || Style.isJavaScript() || Style.isJava()) &&
3313 FormatToken *Prev = Current->getPreviousNonComment();
3314 if (Prev && Prev->is(tok::string_literal) &&
3315 (Prev == Start || Prev->endsSequence(tok::string_literal, tok::plus,
3316 TT_StringInConcatenation))) {
3317 Prev->setType(TT_StringInConcatenation);
3324 (Current->closesScope() &&
3325 (Current->MatchingParen || Current->is(TT_TemplateString))) ||
3326 (CurrentPrecedence != -1 && CurrentPrecedence < Precedence) ||
3335 if (Current->opensScope() ||
3336 Current->isOneOf(TT_RequiresClause,
3337 TT_RequiresClauseInARequiresExpression)) {
3340 while (Current && (!Current->closesScope() || Current->opensScope())) {
3347 if (CurrentPrecedence == Precedence) {
3349 LatestOperator->NextOperator = Current;
3350 LatestOperator = Current;
3354 next(Precedence > 0);
3359 if (Style.isVerilog() && Precedence ==
prec::Comma && VerilogFirstOfType)
3360 addFakeParenthesis(VerilogFirstOfType,
prec::Comma);
3362 if (LatestOperator && (Current || Precedence > 0)) {
3368 Start->Previous->isOneOf(TT_RequiresClause,
3369 TT_RequiresClauseInARequiresExpression))
3371 auto Ret = Current ? Current : Line.Last;
3372 while (!
Ret->ClosesRequiresClause &&
Ret->Previous)
3378 if (Precedence == PrecedenceArrowAndPeriod) {
3382 addFakeParenthesis(Start,
prec::Level(Precedence), End);
3390 int getCurrentPrecedence() {
3392 const FormatToken *NextNonComment = Current->getNextNonComment();
3393 if (Current->is(TT_ConditionalExpr))
3395 if (NextNonComment && Current->is(TT_SelectorName) &&
3396 (NextNonComment->isOneOf(TT_DictLiteral, TT_JsTypeColon) ||
3397 (Style.isProto() && NextNonComment->is(tok::less)))) {
3400 if (Current->is(TT_JsComputedPropertyName))
3402 if (Current->is(TT_LambdaArrow))
3404 if (Current->is(TT_FatArrow))
3406 if (Current->isOneOf(tok::semi, TT_InlineASMColon, TT_SelectorName) ||
3407 (Current->is(tok::comment) && NextNonComment &&
3408 NextNonComment->is(TT_SelectorName))) {
3411 if (Current->is(TT_RangeBasedForLoopColon))
3413 if ((Style.isJava() || Style.isJavaScript()) &&
3414 Current->is(Keywords.kw_instanceof)) {
3417 if (Style.isJavaScript() &&
3418 Current->isOneOf(Keywords.kw_in, Keywords.kw_as)) {
3421 if (Current->isOneOf(TT_BinaryOperator, tok::comma))
3422 return Current->getPrecedence();
3423 if (Current->isOneOf(tok::period, tok::arrow) &&
3424 Current->isNot(TT_TrailingReturnArrow)) {
3425 return PrecedenceArrowAndPeriod;
3427 if ((Style.isJava() || Style.isJavaScript()) &&
3428 Current->isOneOf(Keywords.kw_extends, Keywords.kw_implements,
3429 Keywords.kw_throws)) {
3434 if (Style.isVerilog() && Current->is(tok::colon))
3446 if (Start->MacroParent)
3449 Start->FakeLParens.push_back(Precedence);
3451 Start->StartsBinaryExpression =
true;
3452 if (!End && Current)
3453 End = Current->getPreviousNonComment();
3457 End->EndsBinaryExpression =
true;
3463 void parseUnaryOperator() {
3464 SmallVector<FormatToken *, 2> Tokens;
3465 while (Current && Current->is(TT_UnaryOperator)) {
3466 Tokens.push_back(Current);
3469 parse(PrecedenceArrowAndPeriod);
3476 void parseConditionalExpr() {
3477 while (Current && Current->isTrailingComment())
3481 if (!Current || Current->isNot(tok::question))
3485 if (!Current || Current->isNot(TT_ConditionalExpr))
3492 void next(
bool SkipPastLeadingComments =
true) {
3494 Current = Current->Next;
3496 (Current->NewlinesBefore == 0 || SkipPastLeadingComments) &&
3497 Current->isTrailingComment()) {
3498 Current = Current->Next;
3512 while (Start->startsSequence(tok::l_paren, tok::star)) {
3513 if (!(Start = Start->MatchingParen) ||
3514 !(Start = Start->getNextNonComment())) {
3521 if (
Tok->is(Keywords.kw_assign))
3522 Tok =
Tok->getNextNonComment();
3534 if (
Tok->is(tok::hash)) {
3539 Tok =
Tok->getNextNonComment();
3540 }
else if (
Tok->is(tok::hashhash)) {
3544 Tok =
Tok->getNextNonComment();
3545 }
else if (Keywords.isVerilogQualifier(*
Tok) ||
3546 Keywords.isVerilogIdentifier(*
Tok)) {
3550 while (
Tok &&
Tok->isOneOf(tok::period, tok::coloncolon) &&
3551 (
Tok =
Tok->getNextNonComment())) {
3552 if (Keywords.isVerilogIdentifier(*
Tok))
3553 Tok =
Tok->getNextNonComment();
3557 }
else if (
Tok->is(tok::l_paren)) {
3562 Keywords.kw_highz0, Keywords.kw_highz1, Keywords.kw_large,
3563 Keywords.kw_medium, Keywords.kw_pull0, Keywords.kw_pull1,
3564 Keywords.kw_small, Keywords.kw_strong0, Keywords.kw_strong1,
3565 Keywords.kw_supply0, Keywords.kw_supply1, Keywords.kw_weak0,
3566 Keywords.kw_weak1)) {
3567 Tok->setType(TT_VerilogStrength);
3568 Tok =
Tok->MatchingParen;
3570 Tok->setType(TT_VerilogStrength);
3571 Tok =
Tok->getNextNonComment();
3576 }
else if (
Tok->is(Keywords.kw_verilogHash)) {
3578 if (
Next->is(tok::l_paren))
3581 Tok =
Next->getNextNonComment();
3590 while (
Tok &&
Tok->is(tok::l_square) && (
Tok =
Tok->MatchingParen))
3591 Tok =
Tok->getNextNonComment();
3592 if (
Tok && (
Tok->is(tok::hash) || Keywords.isVerilogIdentifier(*
Tok)))
3601 First->setType(TT_VerilogDimensionedTypeName);
3602 }
else if (
First != Start) {
3610 if (TypedName->is(TT_Unknown))
3611 TypedName->setType(TT_StartOfName);
3613 if (FirstOfType && PreviousComma) {
3614 PreviousComma->setType(TT_VerilogTypeComma);
3615 addFakeParenthesis(FirstOfType,
prec::Comma, PreviousComma->Previous);
3618 FirstOfType = TypedName;
3625 while (Current && Current != FirstOfType) {
3626 if (Current->opensScope()) {
3637 const FormatStyle &Style;
3638 const AdditionalKeywords &Keywords;
3639 const AnnotatedLine &Line;
3649 assert(
Line->First);
3656 Line->First->OriginalColumn) {
3657 const bool PPDirectiveOrImportStmt =
3660 if (PPDirectiveOrImportStmt)
3665 Line->Level = Style.IndentPPDirectives < FormatStyle::PPDIS_BeforeHash &&
3666 PPDirectiveOrImportStmt
3668 : NextNonCommentLine->
Level;
3670 NextNonCommentLine =
Line->First->isNot(tok::r_brace) ?
Line :
nullptr;
3690 if (
Tok->isNot(tok::identifier))
3693 Tok =
Tok->getNextNonComment();
3699 if (
Tok->is(tok::coloncolon))
3700 return Tok->getNextNonComment();
3704 if (
Tok->is(TT_TemplateOpener)) {
3705 Tok =
Tok->MatchingParen;
3709 Tok =
Tok->getNextNonComment();
3714 return Tok->is(tok::coloncolon) ?
Tok->getNextNonComment() :
nullptr;
3722 Tok =
Tok->getNextNonComment()) {
3724 if (
Tok->is(TT_AttributeLSquare)) {
3725 Tok =
Tok->MatchingParen;
3733 if (
Tok->is(tok::l_paren) &&
Tok->is(TT_Unknown) &&
Tok->MatchingParen) {
3741 if (
Tok->isOneOf(tok::kw_friend, tok::kw_inline, tok::kw_virtual,
3742 tok::kw_constexpr, tok::kw_consteval, tok::kw_explicit)) {
3748 if (
Tok->is(tok::kw_template)) {
3749 Tok =
Tok->getNextNonComment();
3755 if (
Tok->isNot(TT_TemplateOpener))
3758 Tok =
Tok->MatchingParen;
3766 if (
Tok->is(tok::coloncolon)) {
3777 if (
Tok->is(tok::tilde)) {
3784 if (
Tok->isNot(tok::identifier) ||
Tok->isNot(TT_Unknown))
3795 assert(
Tok &&
Tok->is(tok::identifier));
3796 const auto *Prev =
Tok->Previous;
3798 if (Prev && Prev->is(tok::tilde))
3799 Prev = Prev->Previous;
3802 if (!Prev || (!Prev->endsSequence(tok::coloncolon, tok::identifier) &&
3803 !Prev->endsSequence(tok::coloncolon, TT_TemplateCloser))) {
3807 assert(Prev->Previous);
3808 if (Prev->Previous->is(TT_TemplateCloser) && Prev->Previous->MatchingParen) {
3809 Prev = Prev->Previous->MatchingParen;
3810 assert(Prev->Previous);
3813 return Prev->Previous->TokenText ==
Tok->TokenText;
3817 if (!
Line.InMacroBody)
3818 MacroBodyScopes.clear();
3820 auto &ScopeStack =
Line.InMacroBody ? MacroBodyScopes : Scopes;
3821 AnnotatingParser
Parser(Style,
Line, Keywords, ScopeStack);
3824 if (!
Line.Children.empty()) {
3827 for (
auto &Child :
Line.Children) {
3828 if (InRequiresExpression &&
3829 Child->First->isNoneOf(tok::kw_typename, tok::kw_requires,
3830 TT_CompoundRequirementLBrace)) {
3836 if (!ScopeStack.empty())
3837 ScopeStack.pop_back();
3850 ExpressionParser ExprParser(Style, Keywords,
Line);
3856 if (
Tok && ((!ScopeStack.empty() && ScopeStack.back() ==
ST_Class) ||
3858 Tok->setFinalizedType(TT_CtorDtorDeclName);
3859 assert(OpeningParen);
3864 if (
Line.startsWith(TT_ObjCMethodSpecifier))
3866 else if (
Line.startsWith(TT_ObjCDecl))
3868 else if (
Line.startsWith(TT_ObjCProperty))
3872 First->SpacesRequiredBefore = 1;
3873 First->CanBreakBefore =
First->MustBreakBefore;
3882 if (Current.
is(TT_FunctionDeclarationName))
3885 if (Current.
isNoneOf(tok::identifier, tok::kw_operator))
3888 const auto *Prev = Current.getPreviousNonComment();
3893 if (
const auto *PrevPrev =
Previous.getPreviousNonComment();
3894 PrevPrev && PrevPrev->is(TT_ObjCDecl)) {
3898 auto skipOperatorName =
3901 if (
Next->is(TT_OverloadedOperatorLParen))
3903 if (
Next->is(TT_OverloadedOperator))
3905 if (
Next->isPlacementOperator() ||
Next->is(tok::kw_co_await)) {
3908 Next->Next->startsSequence(tok::l_square, tok::r_square)) {
3913 if (
Next->startsSequence(tok::l_square, tok::r_square)) {
3918 if ((
Next->isTypeName(LangOpts) ||
Next->is(tok::identifier)) &&
3919 Next->Next &&
Next->Next->isPointerOrReference()) {
3924 if (
Next->is(TT_TemplateOpener) &&
Next->MatchingParen) {
3935 const bool IsCpp = LangOpts.CXXOperatorNames || LangOpts.C11;
3938 if (Current.
is(tok::kw_operator)) {
3939 if (
Line.startsWith(tok::kw_friend))
3941 if (
Previous.Tok.getIdentifierInfo() &&
3942 Previous.isNoneOf(tok::kw_return, tok::kw_co_return)) {
3947 assert(
Previous.MatchingParen->is(tok::l_paren));
3948 assert(
Previous.MatchingParen->is(TT_TypeDeclarationParen));
3957 while (
Next &&
Next->startsSequence(tok::hashhash, tok::identifier))
3960 if (
Next->is(TT_TemplateOpener) &&
Next->MatchingParen) {
3962 }
else if (
Next->is(tok::coloncolon)) {
3966 if (
Next->is(tok::kw_operator)) {
3967 Next = skipOperatorName(
Next->Next);
3970 if (
Next->isNot(tok::identifier))
3972 }
else if (isCppAttribute(IsCpp, *
Next)) {
3976 }
else if (
Next->is(tok::l_paren)) {
3985 if (!
Next ||
Next->isNot(tok::l_paren) || !
Next->MatchingParen)
3987 ClosingParen =
Next->MatchingParen;
3988 assert(ClosingParen->
is(tok::r_paren));
3990 if (
Line.Last->is(tok::l_brace))
3992 if (
Next->Next == ClosingParen)
3995 if (ClosingParen->
Next && ClosingParen->
Next->
is(TT_PointerOrReference))
4008 if (IsCpp &&
Next->Next &&
Next->Next->is(tok::identifier) &&
4009 !
Line.endsWith(tok::semi)) {
4015 if (
Tok->is(TT_TypeDeclarationParen))
4017 if (
Tok->isOneOf(tok::l_paren, TT_TemplateOpener) &&
Tok->MatchingParen) {
4018 Tok =
Tok->MatchingParen;
4021 if (
Tok->is(tok::kw_const) ||
Tok->isTypeName(LangOpts) ||
4022 Tok->isOneOf(TT_PointerOrReference, TT_StartOfName, tok::ellipsis)) {
4025 if (
Tok->isOneOf(tok::l_brace, TT_ObjCMethodExpr) ||
Tok->Tok.isLiteral())
4031bool TokenAnnotator::mustBreakForReturnType(
const AnnotatedLine &
Line)
const {
4032 assert(
Line.MightBeFunctionDecl);
4034 if ((Style.BreakAfterReturnType == FormatStyle::RTBS_TopLevel ||
4035 Style.BreakAfterReturnType == FormatStyle::RTBS_TopLevelDefinitions) &&
4040 switch (Style.BreakAfterReturnType) {
4041 case FormatStyle::RTBS_None:
4042 case FormatStyle::RTBS_Automatic:
4043 case FormatStyle::RTBS_ExceptShortType:
4045 case FormatStyle::RTBS_All:
4046 case FormatStyle::RTBS_TopLevel:
4048 case FormatStyle::RTBS_AllDefinitions:
4049 case FormatStyle::RTBS_TopLevelDefinitions:
4050 return Line.mightBeFunctionDefinition();
4060 Line.Computed =
true;
4068 :
Line.FirstStartColumn +
First->ColumnWidth;
4069 bool AlignArrayOfStructures =
4070 (Style.AlignArrayOfStructures != FormatStyle::AIAS_None &&
4072 if (AlignArrayOfStructures)
4073 calculateArrayInitializerColumnList(
Line);
4075 const auto *FirstNonComment =
Line.getFirstNonComment();
4076 bool SeenName =
false;
4077 bool LineIsFunctionDeclaration =
false;
4081 for (
auto *
Tok = FirstNonComment && FirstNonComment->isNot(tok::kw_using)
4082 ? FirstNonComment->Next
4085 if (
Tok->is(TT_StartOfName))
4087 if (
Tok->Previous->EndsCppAttributeGroup)
4088 AfterLastAttribute =
Tok;
4089 if (
const bool IsCtorOrDtor =
Tok->is(TT_CtorDtorDeclName);
4093 Tok->setFinalizedType(TT_FunctionDeclarationName);
4094 LineIsFunctionDeclaration =
true;
4098 assert(OpeningParen);
4099 if (OpeningParen->is(TT_Unknown))
4100 OpeningParen->setType(TT_FunctionDeclarationLParen);
4107 if ((LineIsFunctionDeclaration ||
4108 (FirstNonComment && FirstNonComment->is(TT_CtorDtorDeclName))) &&
4109 Line.endsWith(tok::semi, tok::r_brace)) {
4110 auto *
Tok =
Line.Last->Previous;
4111 while (
Tok->isNot(tok::r_brace))
4113 if (
auto *LBrace =
Tok->MatchingParen; LBrace && LBrace->is(TT_Unknown)) {
4114 assert(LBrace->is(tok::l_brace));
4117 LBrace->setFinalizedType(TT_FunctionLBrace);
4121 if (SeenName && AfterLastAttribute &&
4124 if (LineIsFunctionDeclaration)
4125 Line.ReturnTypeWrapped =
true;
4128 if (!LineIsFunctionDeclaration) {
4129 Line.ReturnTypeWrapped =
false;
4131 for (
const auto *
Tok = FirstNonComment;
Tok;
Tok =
Tok->Next) {
4132 if (
Tok->isNot(tok::kw_operator))
4136 }
while (
Tok &&
Tok->isNot(TT_OverloadedOperatorLParen));
4137 if (!
Tok || !
Tok->MatchingParen)
4139 const auto *LeftParen =
Tok;
4140 for (
Tok =
Tok->Next;
Tok &&
Tok != LeftParen->MatchingParen;
4142 if (
Tok->isNot(tok::identifier))
4145 const bool NextIsBinaryOperator =
4147 Next->Next->is(tok::identifier);
4148 if (!NextIsBinaryOperator)
4150 Next->setType(TT_BinaryOperator);
4154 }
else if (ClosingParen) {
4156 if (
Tok->is(TT_CtorInitializerColon))
4158 if (
Tok->is(tok::arrow)) {
4159 Tok->overwriteFixedType(TT_TrailingReturnArrow);
4162 if (
Tok->isNot(TT_TrailingAnnotation))
4165 if (!
Next ||
Next->isNot(tok::l_paren))
4174 if (
First->is(TT_ElseLBrace)) {
4175 First->CanBreakBefore =
true;
4176 First->MustBreakBefore =
true;
4179 bool InFunctionDecl =
Line.MightBeFunctionDecl;
4180 bool InParameterList =
false;
4181 for (
auto *Current =
First->Next; Current; Current = Current->Next) {
4183 if (Current->is(TT_LineComment)) {
4185 Current->SpacesRequiredBefore =
4186 (Style.Cpp11BracedListStyle == FormatStyle::BLS_AlignFirstComment &&
4187 !Style.SpacesInParensOptions.Other)
4190 }
else if (Prev->
is(TT_VerilogMultiLineListLParen)) {
4191 Current->SpacesRequiredBefore = 0;
4193 Current->SpacesRequiredBefore = Style.SpacesBeforeTrailingComments;
4203 if (!Current->HasUnescapedNewline) {
4206 if (
Parameter->isOneOf(tok::comment, tok::r_brace))
4209 if (
Parameter->Previous->isNot(TT_CtorInitializerComma) &&
4217 }
else if (!Current->Finalized && Current->SpacesRequiredBefore == 0 &&
4218 spaceRequiredBefore(
Line, *Current)) {
4219 Current->SpacesRequiredBefore = 1;
4224 Current->MustBreakBefore =
true;
4226 Current->MustBreakBefore =
4227 Current->MustBreakBefore || mustBreakBefore(
Line, *Current);
4228 if (!Current->MustBreakBefore && InFunctionDecl &&
4229 Current->is(TT_FunctionDeclarationName)) {
4230 Current->MustBreakBefore = mustBreakForReturnType(
Line);
4234 Current->CanBreakBefore =
4235 Current->MustBreakBefore || canBreakBefore(
Line, *Current);
4237 if (Current->is(TT_FunctionDeclarationLParen)) {
4238 InParameterList =
true;
4239 }
else if (Current->is(tok::r_paren)) {
4240 const auto *LParen = Current->MatchingParen;
4241 if (LParen && LParen->is(TT_FunctionDeclarationLParen))
4242 InParameterList =
false;
4243 }
else if (InParameterList &&
4244 Current->endsSequence(TT_AttributeMacro,
4245 TT_PointerOrReference)) {
4246 Current->CanBreakBefore =
false;
4249 unsigned ChildSize = 0;
4252 ChildSize = LastOfChild.isTrailingComment() ? Style.ColumnLimit
4255 if (Current->MustBreakBefore || Prev->
Children.size() > 1 ||
4257 Prev->
Children[0]->First->MustBreakBefore) ||
4258 Current->IsMultiline) {
4259 Current->TotalLength = Prev->
TotalLength + Style.ColumnLimit;
4261 Current->TotalLength = Prev->
TotalLength + Current->ColumnWidth +
4262 ChildSize + Current->SpacesRequiredBefore;
4265 if (Current->is(TT_ControlStatementLBrace)) {
4266 if (Style.ColumnLimit > 0 &&
4267 Style.BraceWrapping.AfterControlStatement ==
4268 FormatStyle::BWACS_MultiLine &&
4269 Line.Level * Style.IndentWidth +
Line.Last->TotalLength >
4270 Style.ColumnLimit) {
4271 Current->CanBreakBefore =
true;
4272 Current->MustBreakBefore =
true;
4274 }
else if (Current->is(TT_CtorInitializerColon)) {
4275 InFunctionDecl =
false;
4287 Current->SplitPenalty = splitPenalty(
Line, *Current, InFunctionDecl);
4288 if (Style.Language == FormatStyle::LK_ObjC &&
4289 Current->is(TT_SelectorName) && Current->ParameterIndex > 0) {
4290 if (Current->ParameterIndex == 1)
4291 Current->SplitPenalty += 5 * Current->BindingStrength;
4293 Current->SplitPenalty += 20 * Current->BindingStrength;
4297 calculateUnbreakableTailLengths(
Line);
4299 for (
auto *Current =
First; Current; Current = Current->Next) {
4301 Current->Role->precomputeFormattingInfos(Current);
4302 if (Current->MatchingParen &&
4303 Current->MatchingParen->opensBlockOrBlockTypeList(Style) &&
4308 if (Current->opensBlockOrBlockTypeList(Style))
4312 LLVM_DEBUG({ printDebugInfo(
Line); });
4315void TokenAnnotator::calculateUnbreakableTailLengths(
4322 Current->
isOneOf(tok::comment, tok::string_literal)) {
4332void TokenAnnotator::calculateArrayInitializerColumnList(
4336 auto *CurrentToken =
Line.First;
4337 CurrentToken->ArrayInitializerLineStart =
true;
4339 while (CurrentToken && CurrentToken !=
Line.Last) {
4340 if (CurrentToken->is(tok::l_brace)) {
4341 CurrentToken->IsArrayInitializer =
true;
4342 if (CurrentToken->Next)
4343 CurrentToken->Next->MustBreakBefore =
true;
4345 calculateInitializerColumnList(
Line, CurrentToken->Next, Depth + 1);
4347 CurrentToken = CurrentToken->Next;
4352FormatToken *TokenAnnotator::calculateInitializerColumnList(
4354 while (CurrentToken && CurrentToken !=
Line.Last) {
4355 if (CurrentToken->is(tok::l_brace))
4357 else if (CurrentToken->is(tok::r_brace))
4359 if (Depth == 2 && CurrentToken->isOneOf(tok::l_brace, tok::comma)) {
4360 CurrentToken = CurrentToken->Next;
4363 CurrentToken->StartsColumn =
true;
4364 CurrentToken = CurrentToken->Previous;
4366 CurrentToken = CurrentToken->Next;
4368 return CurrentToken;
4373 bool InFunctionDecl)
const {
4377 if (
Left.is(tok::semi))
4381 if (Style.isJava()) {
4382 if (
Right.isOneOf(Keywords.kw_extends, Keywords.kw_throws))
4384 if (
Right.is(Keywords.kw_implements))
4386 if (
Left.is(tok::comma) &&
Left.NestingLevel == 0)
4388 }
else if (Style.isJavaScript()) {
4389 if (
Right.is(Keywords.kw_function) &&
Left.isNot(tok::comma))
4391 if (
Left.is(TT_JsTypeColon))
4393 if ((
Left.is(TT_TemplateString) &&
Left.TokenText.ends_with(
"${")) ||
4394 (
Right.is(TT_TemplateString) &&
Right.TokenText.starts_with(
"}"))) {
4398 if (
Left.opensScope() &&
Right.closesScope())
4400 }
else if (Style.Language == FormatStyle::LK_Proto) {
4401 if (
Right.is(tok::l_square))
4403 if (
Right.is(tok::period))
4407 if (
Right.is(tok::identifier) &&
Right.Next &&
Right.Next->is(TT_DictLiteral))
4409 if (
Right.is(tok::l_square)) {
4410 if (
Left.is(tok::r_square))
4413 if (
Right.is(TT_LambdaLSquare) &&
Left.is(tok::equal))
4415 if (
Right.isNoneOf(TT_ObjCMethodExpr, TT_LambdaLSquare,
4416 TT_ArrayInitializerLSquare,
4417 TT_DesignatedInitializerLSquare, TT_AttributeLSquare)) {
4422 if (
Left.is(tok::coloncolon))
4423 return Style.PenaltyBreakScopeResolution;
4424 if (
Right.isOneOf(TT_StartOfName, TT_FunctionDeclarationName,
4425 tok::kw_operator)) {
4426 if (
Line.startsWith(tok::kw_for) &&
Right.PartOfMultiVariableDeclStmt)
4428 if (
Left.is(TT_StartOfName))
4430 if (InFunctionDecl &&
Right.NestingLevel == 0)
4431 return Style.PenaltyReturnTypeOnItsOwnLine;
4434 if (
Right.is(TT_PointerOrReference))
4436 if (
Right.is(TT_LambdaArrow))
4438 if (
Left.is(tok::equal) &&
Right.is(tok::l_brace))
4440 if (
Left.is(TT_CastRParen))
4442 if (
Left.isOneOf(tok::kw_class, tok::kw_struct, tok::kw_union))
4444 if (
Left.is(tok::comment))
4447 if (
Left.isOneOf(TT_RangeBasedForLoopColon, TT_InheritanceColon,
4448 TT_CtorInitializerColon)) {
4452 if (
Right.isMemberAccess()) {
4473 const auto Penalty = Style.PenaltyBreakBeforeMemberAccess;
4475 ? std::min(Penalty, 35u)
4479 if (
Right.is(TT_TrailingAnnotation) &&
4480 (!
Right.Next ||
Right.Next->isNot(tok::l_paren))) {
4483 if (
Line.startsWith(TT_ObjCMethodSpecifier))
4490 bool is_short_annotation =
Right.TokenText.size() < 10;
4491 return (
Left.is(tok::r_paren) ? 100 : 120) + (is_short_annotation ? 50 : 0);
4495 if (
Line.startsWith(tok::kw_for) &&
Left.is(tok::equal))
4500 if (
Right.is(TT_SelectorName))
4502 if (
Left.is(tok::colon)) {
4503 if (
Left.is(TT_ObjCMethodExpr))
4504 return Line.MightBeFunctionDecl ? 50 : 500;
4505 if (
Left.is(TT_ObjCSelector))
4513 Left.Previous->isOneOf(tok::identifier, tok::greater)) {
4517 if (
Left.is(tok::l_paren) && Style.PenaltyBreakOpenParenthesis != 0)
4518 return Style.PenaltyBreakOpenParenthesis;
4519 if (
Left.is(tok::l_paren) && InFunctionDecl && Style.AlignAfterOpenBracket)
4521 if (
Left.is(tok::l_paren) &&
Left.Previous &&
4522 (
Left.Previous->isOneOf(tok::kw_for, tok::kw__Generic) ||
4523 Left.Previous->isIf())) {
4526 if (
Left.is(tok::equal) && InFunctionDecl)
4528 if (
Right.is(tok::r_brace))
4530 if (
Left.is(TT_TemplateOpener))
4532 if (
Left.opensScope()) {
4536 if (!Style.AlignAfterOpenBracket &&
4537 (
Left.ParameterCount <= 1 || Style.AllowAllArgumentsOnNextLine)) {
4540 if (
Left.is(tok::l_brace) &&
4541 Style.Cpp11BracedListStyle == FormatStyle::BLS_Block) {
4544 return Left.ParameterCount > 1 ? Style.PenaltyBreakBeforeFirstCallParameter
4547 if (
Left.is(TT_JavaAnnotation))
4550 if (
Left.is(TT_UnaryOperator))
4552 if (
Left.isOneOf(tok::plus, tok::comma) &&
Left.Previous &&
4553 Left.Previous->isLabelString() &&
4554 (
Left.NextOperator ||
Left.OperatorIndex != 0)) {
4557 if (
Right.is(tok::plus) &&
Left.isLabelString() &&
4558 (
Right.NextOperator ||
Right.OperatorIndex != 0)) {
4561 if (
Left.is(tok::comma))
4563 if (
Right.is(tok::lessless) &&
Left.isLabelString() &&
4564 (
Right.NextOperator ||
Right.OperatorIndex != 1)) {
4567 if (
Right.is(tok::lessless)) {
4569 if (
Left.isNot(tok::r_paren) ||
Right.OperatorIndex > 0) {
4575 if (
Left.ClosesTemplateDeclaration)
4576 return Style.PenaltyBreakTemplateDeclaration;
4577 if (
Left.ClosesRequiresClause)
4579 if (
Left.is(TT_ConditionalExpr))
4585 return Style.PenaltyBreakAssignment;
4592bool TokenAnnotator::spaceRequiredBeforeParens(
const FormatToken &Right)
const {
4593 if (Style.SpaceBeforeParens == FormatStyle::SBPO_Always)
4595 if (
Right.is(TT_OverloadedOperatorLParen) &&
4596 Style.SpaceBeforeParensOptions.AfterOverloadedOperator) {
4599 if (Style.SpaceBeforeParensOptions.BeforeNonEmptyParentheses &&
4600 Right.ParameterCount > 0) {
4609 if (
Left.is(tok::kw_return) &&
4610 Right.isNoneOf(tok::semi, tok::r_paren, tok::hashhash)) {
4613 if (
Left.is(tok::kw_throw) &&
Right.is(tok::l_paren) &&
Right.MatchingParen &&
4614 Right.MatchingParen->is(TT_CastRParen)) {
4617 if (
Left.is(Keywords.kw_assert) && Style.isJava())
4620 Left.is(tok::objc_property)) {
4623 if (
Right.is(tok::hashhash))
4624 return Left.is(tok::hash);
4625 if (
Left.isOneOf(tok::hashhash, tok::hash))
4626 return Right.is(tok::hash);
4627 if (Style.SpacesInParens == FormatStyle::SIPO_Custom) {
4628 if (
Left.is(tok::l_paren) &&
Right.is(tok::r_paren))
4629 return Style.SpacesInParensOptions.InEmptyParentheses;
4630 if (Style.SpacesInParensOptions.ExceptDoubleParentheses &&
4631 Left.is(tok::r_paren) &&
Right.is(tok::r_paren)) {
4632 auto *InnerLParen =
Left.MatchingParen;
4633 if (InnerLParen && InnerLParen->Previous ==
Right.MatchingParen) {
4634 InnerLParen->SpacesRequiredBefore = 0;
4639 if (
Left.is(tok::l_paren))
4641 else if (
Right.is(tok::r_paren) &&
Right.MatchingParen)
4642 LeftParen =
Right.MatchingParen;
4643 if (LeftParen && (LeftParen->is(TT_ConditionLParen) ||
4644 (LeftParen->Previous &&
4645 isKeywordWithCondition(*LeftParen->Previous)))) {
4646 return Style.SpacesInParensOptions.InConditionalStatements;
4651 if (
Left.is(tok::kw_auto) &&
Right.isOneOf(TT_LambdaLBrace, TT_FunctionLBrace,
4653 TT_FunctionTypeLParen)) {
4658 if (
Left.is(tok::kw_auto) &&
Right.isOneOf(tok::l_paren, tok::l_brace))
4661 const auto *BeforeLeft =
Left.Previous;
4664 if (
Right.is(tok::l_paren) &&
Left.is(tok::kw_co_await) && BeforeLeft &&
4665 BeforeLeft->is(tok::kw_operator)) {
4669 if (
Left.isOneOf(tok::kw_co_await, tok::kw_co_yield, tok::kw_co_return) &&
4670 Right.isNoneOf(tok::semi, tok::r_paren)) {
4674 if (
Left.is(tok::l_paren) ||
Right.is(tok::r_paren)) {
4675 return (
Right.is(TT_CastRParen) ||
4676 (
Left.MatchingParen &&
Left.MatchingParen->is(TT_CastRParen)))
4677 ? Style.SpacesInParensOptions.InCStyleCasts
4678 : Style.SpacesInParensOptions.Other;
4680 if (
Right.isOneOf(tok::semi, tok::comma))
4683 bool IsLightweightGeneric =
Right.MatchingParen &&
4684 Right.MatchingParen->Next &&
4685 Right.MatchingParen->Next->is(tok::colon);
4686 return !IsLightweightGeneric && Style.ObjCSpaceBeforeProtocolList;
4688 if (
Right.is(tok::less) &&
Left.is(tok::kw_template))
4689 return Style.SpaceAfterTemplateKeyword;
4690 if (
Left.isOneOf(tok::exclaim, tok::tilde))
4692 if (
Left.is(tok::at) &&
4693 Right.isOneOf(tok::identifier, tok::string_literal, tok::char_constant,
4694 tok::numeric_constant, tok::l_paren, tok::l_brace,
4695 tok::kw_true, tok::kw_false)) {
4698 if (
Left.is(tok::colon))
4699 return Left.isNoneOf(TT_ObjCSelector, TT_ObjCMethodExpr);
4700 if (
Left.is(tok::coloncolon))
4702 if (
Left.is(tok::less) ||
Right.isOneOf(tok::greater, tok::less)) {
4703 if (Style.isTextProto() ||
4704 (Style.Language == FormatStyle::LK_Proto &&
4705 (
Left.is(TT_DictLiteral) ||
Right.is(TT_DictLiteral)))) {
4707 if (
Left.is(tok::less) &&
Right.is(tok::greater))
4709 return Style.Cpp11BracedListStyle == FormatStyle::BLS_Block;
4712 if (
Right.isNot(TT_OverloadedOperatorLParen))
4715 if (
Right.is(tok::ellipsis)) {
4716 return Left.Tok.isLiteral() || (
Left.is(tok::identifier) && BeforeLeft &&
4717 BeforeLeft->is(tok::kw_case));
4719 if (
Left.is(tok::l_square) &&
Right.is(tok::amp))
4720 return Style.SpacesInSquareBrackets;
4721 if (
Right.is(TT_PointerOrReference)) {
4722 if (
Left.is(tok::r_paren) &&
Line.MightBeFunctionDecl) {
4723 if (!
Left.MatchingParen)
4726 Left.MatchingParen->getPreviousNonComment();
4727 if (!TokenBeforeMatchingParen ||
Left.isNot(TT_TypeDeclarationParen))
4733 if ((Style.SpaceAroundPointerQualifiers == FormatStyle::SAPQ_After ||
4734 Style.SpaceAroundPointerQualifiers == FormatStyle::SAPQ_Both) &&
4735 (
Left.is(TT_AttributeRParen) ||
4736 Left.canBePointerOrReferenceQualifier())) {
4739 if (
Left.Tok.isLiteral())
4742 if (
Left.isTypeOrIdentifier(LangOpts) &&
Right.Next &&
Right.Next->Next &&
4743 Right.Next->Next->is(TT_RangeBasedForLoopColon)) {
4744 return getTokenPointerOrReferenceAlignment(Right) !=
4745 FormatStyle::PAS_Left;
4747 return Left.isNoneOf(TT_PointerOrReference, tok::l_paren) &&
4748 (getTokenPointerOrReferenceAlignment(Right) !=
4749 FormatStyle::PAS_Left ||
4750 (
Line.IsMultiVariableDeclStmt &&
4751 (
Left.NestingLevel == 0 ||
4752 (
Left.NestingLevel == 1 && startsWithInitStatement(
Line)))));
4754 if (
Right.is(TT_FunctionTypeLParen) &&
Left.isNot(tok::l_paren) &&
4755 (
Left.isNot(TT_PointerOrReference) ||
4756 (getTokenPointerOrReferenceAlignment(Left) != FormatStyle::PAS_Right &&
4757 !
Line.IsMultiVariableDeclStmt))) {
4760 if (
Left.is(TT_PointerOrReference)) {
4763 if ((Style.SpaceAroundPointerQualifiers == FormatStyle::SAPQ_Before ||
4764 Style.SpaceAroundPointerQualifiers == FormatStyle::SAPQ_Both) &&
4765 Right.canBePointerOrReferenceQualifier()) {
4769 if (
Right.Tok.isLiteral())
4772 if (
Right.is(TT_BlockComment))
4776 if (
Right.isOneOf(Keywords.kw_override, Keywords.kw_final, tok::kw_noexcept,
4777 TT_RequiresClause) &&
4778 Right.isNot(TT_StartOfName)) {
4785 if (BeforeLeft && BeforeLeft->isTypeOrIdentifier(LangOpts) &&
Right.Next &&
4786 Right.Next->is(TT_RangeBasedForLoopColon)) {
4787 return getTokenPointerOrReferenceAlignment(Left) !=
4788 FormatStyle::PAS_Right;
4790 if (
Right.isOneOf(TT_PointerOrReference, TT_ArraySubscriptLSquare,
4794 if (getTokenPointerOrReferenceAlignment(Left) == FormatStyle::PAS_Right)
4802 if (
Line.IsMultiVariableDeclStmt &&
4803 (
Left.NestingLevel ==
Line.First->NestingLevel ||
4804 ((
Left.NestingLevel ==
Line.First->NestingLevel + 1) &&
4805 startsWithInitStatement(
Line)))) {
4810 if (BeforeLeft->is(tok::coloncolon)) {
4811 if (
Left.isNot(tok::star))
4813 assert(Style.PointerAlignment != FormatStyle::PAS_Right);
4814 if (!
Right.startsSequence(tok::identifier, tok::r_paren))
4817 const auto *LParen =
Right.Next->MatchingParen;
4818 return !LParen || LParen->isNot(TT_FunctionTypeLParen);
4820 return BeforeLeft->isNoneOf(tok::l_paren, tok::l_square);
4823 if (
Left.is(tok::ellipsis) && BeforeLeft &&
4824 BeforeLeft->isPointerOrReference()) {
4825 return Style.PointerAlignment != FormatStyle::PAS_Right;
4828 if (
Right.is(tok::star) &&
Left.is(tok::l_paren))
4830 if (
Left.is(tok::star) &&
Right.isPointerOrReference())
4832 if (
Right.isPointerOrReference()) {
4843 if (
Previous->is(tok::coloncolon)) {
4862 if (
Previous->endsSequence(tok::kw_operator))
4863 return Style.PointerAlignment != FormatStyle::PAS_Left;
4864 if (
Previous->isOneOf(tok::kw_const, tok::kw_volatile)) {
4865 return (Style.PointerAlignment != FormatStyle::PAS_Left) ||
4866 (Style.SpaceAroundPointerQualifiers ==
4867 FormatStyle::SAPQ_After) ||
4868 (Style.SpaceAroundPointerQualifiers == FormatStyle::SAPQ_Both);
4872 if (Style.isCSharp() &&
Left.is(Keywords.kw_is) &&
Right.is(tok::l_square))
4874 const auto SpaceRequiredForArrayInitializerLSquare =
4875 [](
const FormatToken &LSquareTok,
const FormatStyle &Style) {
4876 return Style.SpacesInContainerLiterals ||
4878 Style.Cpp11BracedListStyle == FormatStyle::BLS_Block &&
4879 LSquareTok.endsSequence(tok::l_square, tok::colon,
4882 if (
Left.is(tok::l_square)) {
4883 return (
Left.is(TT_ArrayInitializerLSquare) &&
Right.isNot(tok::r_square) &&
4884 SpaceRequiredForArrayInitializerLSquare(Left, Style)) ||
4885 (
Left.isOneOf(TT_ArraySubscriptLSquare, TT_StructuredBindingLSquare,
4886 TT_LambdaLSquare) &&
4887 Style.SpacesInSquareBrackets &&
Right.isNot(tok::r_square));
4889 if (
Right.is(tok::r_square)) {
4890 return Right.MatchingParen &&
4891 ((
Right.MatchingParen->is(TT_ArrayInitializerLSquare) &&
4892 SpaceRequiredForArrayInitializerLSquare(*
Right.MatchingParen,
4894 (Style.SpacesInSquareBrackets &&
4895 Right.MatchingParen->isOneOf(TT_ArraySubscriptLSquare,
4896 TT_StructuredBindingLSquare,
4897 TT_LambdaLSquare)));
4899 if (
Right.is(tok::l_square) &&
4900 Right.isNoneOf(TT_ObjCMethodExpr, TT_LambdaLSquare,
4901 TT_DesignatedInitializerLSquare,
4902 TT_StructuredBindingLSquare, TT_AttributeLSquare) &&
4903 Left.isNoneOf(tok::numeric_constant, TT_DictLiteral) &&
4904 !(
Left.isNot(tok::r_square) && Style.SpaceBeforeSquareBrackets &&
4905 Right.is(TT_ArraySubscriptLSquare))) {
4909 (
Right.is(tok::r_brace) &&
Right.MatchingParen &&
4911 return Style.Cpp11BracedListStyle == FormatStyle::BLS_Block ||
4912 Style.SpacesInParensOptions.Other;
4914 if (
Left.is(TT_BlockComment)) {
4916 return Style.isJavaScript() || !
Left.TokenText.ends_with(
"=*/");
4921 if (
Left.is(TT_TemplateCloser) &&
Right.is(TT_AttributeLSquare))
4924 if (
Right.is(tok::l_paren)) {
4926 if (
Line.MightBeFunctionDecl &&
Right.is(TT_FunctionDeclarationLParen)) {
4927 if (spaceRequiredBeforeParens(Right))
4929 const auto &Options = Style.SpaceBeforeParensOptions;
4930 return Line.mightBeFunctionDefinition()
4931 ? Options.AfterFunctionDefinitionName
4932 : Options.AfterFunctionDeclarationName;
4934 if (
Left.is(TT_TemplateCloser) &&
Right.isNot(TT_FunctionTypeLParen))
4935 return spaceRequiredBeforeParens(Right);
4936 if (
Left.isOneOf(TT_RequiresClause,
4937 TT_RequiresClauseInARequiresExpression)) {
4938 return Style.SpaceBeforeParensOptions.AfterRequiresInClause ||
4939 spaceRequiredBeforeParens(Right);
4941 if (
Left.is(TT_RequiresExpression)) {
4942 return Style.SpaceBeforeParensOptions.AfterRequiresInExpression ||
4943 spaceRequiredBeforeParens(Right);
4945 if (
Left.isOneOf(TT_AttributeRParen, TT_AttributeRSquare))
4947 if (
Left.is(TT_ForEachMacro)) {
4948 return Style.SpaceBeforeParensOptions.AfterForeachMacros ||
4949 spaceRequiredBeforeParens(Right);
4951 if (
Left.is(TT_IfMacro)) {
4952 return Style.SpaceBeforeParensOptions.AfterIfMacros ||
4953 spaceRequiredBeforeParens(Right);
4955 if (Style.SpaceBeforeParens == FormatStyle::SBPO_Custom &&
4956 Left.isPlacementOperator() &&
4957 Right.isNot(TT_OverloadedOperatorLParen) &&
4958 !(
Line.MightBeFunctionDecl &&
Left.is(TT_FunctionDeclarationName))) {
4959 const auto *RParen =
Right.MatchingParen;
4960 return Style.SpaceBeforeParensOptions.AfterPlacementOperator ||
4961 (RParen && RParen->is(TT_CastRParen));
4965 if (
Left.is(tok::semi))
4967 if (
Left.isOneOf(tok::pp_elif, tok::kw_for, tok::kw_while, tok::kw_switch,
4968 tok::kw_case, TT_ForEachMacro, TT_ObjCForIn) ||
4970 Right.is(TT_ConditionLParen)) {
4971 return Style.SpaceBeforeParensOptions.AfterControlStatements ||
4972 spaceRequiredBeforeParens(Right);
4977 if (
Right.is(TT_OverloadedOperatorLParen))
4978 return spaceRequiredBeforeParens(Right);
4982 Left.MatchingParen &&
Left.MatchingParen->is(TT_LambdaLSquare)) {
4983 return Style.SpaceBeforeParensOptions.AfterFunctionDefinitionName ||
4984 spaceRequiredBeforeParens(Right);
4986 if (!BeforeLeft || BeforeLeft->isNoneOf(tok::period, tok::arrow)) {
4987 if (
Left.isOneOf(tok::kw_try, Keywords.kw___except, tok::kw_catch)) {
4988 return Style.SpaceBeforeParensOptions.AfterControlStatements ||
4989 spaceRequiredBeforeParens(Right);
4991 if (
Left.isPlacementOperator() ||
4992 (
Left.is(tok::r_square) &&
Left.MatchingParen &&
4993 Left.MatchingParen->Previous &&
4994 Left.MatchingParen->Previous->is(tok::kw_delete))) {
4995 return Style.SpaceBeforeParens != FormatStyle::SBPO_Never ||
4996 spaceRequiredBeforeParens(Right);
5000 if (
Tok.isNot(tok::l_paren))
5002 const auto *RParen =
Tok.MatchingParen;
5005 const auto *
Next = RParen->Next;
5012 (
Left.Tok.getIdentifierInfo() ||
Left.is(tok::r_paren))) {
5013 return spaceRequiredBeforeParens(Right);
5017 if (
Left.is(tok::at) &&
Right.isNot(tok::objc_not_keyword))
5019 if (
Right.is(TT_UnaryOperator)) {
5020 return Left.isNoneOf(tok::l_paren, tok::l_square, tok::at) &&
5021 (
Left.isNot(tok::colon) ||
Left.isNot(TT_ObjCMethodExpr));
5027 if (!Style.isVerilog() &&
5028 (
Left.isOneOf(tok::identifier, tok::greater, tok::r_square,
5030 Left.isTypeName(LangOpts)) &&
5031 Right.is(tok::l_brace) &&
Right.getNextNonComment() &&
5035 if (
Left.is(tok::period) ||
Right.is(tok::period))
5039 if (
Right.is(tok::hash) &&
Left.is(tok::identifier) &&
5040 (
Left.TokenText ==
"L" ||
Left.TokenText ==
"u" ||
5041 Left.TokenText ==
"U" ||
Left.TokenText ==
"u8" ||
5042 Left.TokenText ==
"LR" ||
Left.TokenText ==
"uR" ||
5043 Left.TokenText ==
"UR" ||
Left.TokenText ==
"u8R")) {
5046 if (
Left.is(TT_TemplateCloser) &&
Left.MatchingParen &&
5047 Left.MatchingParen->Previous &&
5048 Left.MatchingParen->Previous->isOneOf(tok::period, tok::coloncolon)) {
5054 if (
Left.is(TT_TemplateCloser) &&
Right.is(tok::l_square))
5056 if (
Left.is(tok::l_brace) &&
Left.endsSequence(TT_DictLiteral, tok::at)) {
5060 if (
Right.is(tok::r_brace) &&
Right.MatchingParen &&
5061 Right.MatchingParen->endsSequence(TT_DictLiteral, tok::at)) {
5065 if (
Right.is(TT_TrailingAnnotation) &&
Right.isOneOf(tok::amp, tok::ampamp) &&
5066 Left.isOneOf(tok::kw_const, tok::kw_volatile) &&
5067 (!
Right.Next ||
Right.Next->is(tok::semi))) {
5071 return getTokenReferenceAlignment(Right) != FormatStyle::PAS_Left;
5084 return Right.hasWhitespaceBefore();
5086 const bool IsVerilog = Style.isVerilog();
5087 assert(!IsVerilog || !IsCpp);
5090 if (Keywords.isWordLike(Right, IsVerilog) &&
5091 Keywords.isWordLike(Left, IsVerilog)) {
5097 if (
Left.is(tok::star) &&
Right.is(tok::comment))
5100 if (
Left.is(tok::l_brace) &&
Right.is(tok::r_brace) &&
5101 Left.Children.empty()) {
5103 return Style.SpaceInEmptyBraces != FormatStyle::SIEB_Never;
5104 if (Style.Cpp11BracedListStyle != FormatStyle::BLS_Block) {
5105 return Style.SpacesInParens == FormatStyle::SIPO_Custom &&
5106 Style.SpacesInParensOptions.InEmptyParentheses;
5108 return Style.SpaceInEmptyBraces == FormatStyle::SIEB_Always;
5111 const auto *BeforeLeft =
Left.Previous;
5114 if (
Left.is(TT_OverloadedOperator) &&
5115 Right.isOneOf(TT_TemplateOpener, TT_TemplateCloser)) {
5119 if (
Right.is(tok::period) &&
Left.is(tok::numeric_constant))
5123 if (
Left.is(Keywords.kw_import) &&
5124 Right.isOneOf(tok::less, tok::ellipsis) &&
5125 (!BeforeLeft || BeforeLeft->is(tok::kw_export))) {
5129 if (
Left.isOneOf(Keywords.kw_module, Keywords.kw_import) &&
5130 Right.is(TT_ModulePartitionColon)) {
5134 if (
Right.is(TT_AfterPPDirective))
5138 if (
Left.is(tok::identifier) &&
Right.is(TT_ModulePartitionColon))
5141 if (
Left.is(TT_ModulePartitionColon) &&
5142 Right.isOneOf(tok::identifier, tok::kw_private)) {
5145 if (
Left.is(tok::ellipsis) &&
Right.is(tok::identifier) &&
5146 Line.First->is(Keywords.kw_import)) {
5150 if (
Left.isOneOf(TT_AttributeRParen, TT_AttributeMacro) &&
5151 Right.is(tok::coloncolon)) {
5155 if (
Left.is(tok::kw_operator))
5156 return Right.is(tok::coloncolon) || Style.SpaceAfterOperatorKeyword;
5158 !
Left.opensScope() && Style.SpaceBeforeCpp11BracedList) {
5161 if (
Left.is(tok::less) &&
Left.is(TT_OverloadedOperator) &&
5162 Right.is(TT_TemplateOpener)) {
5166 if (
Left.is(tok::identifier) &&
Right.is(tok::numeric_constant))
5167 return Right.TokenText[0] !=
'.';
5169 if (
Left.Tok.getIdentifierInfo() &&
Right.Tok.isLiteral())
5171 }
else if (Style.isProto()) {
5172 if (
Right.is(tok::period) && !(BeforeLeft && BeforeLeft->is(tok::period)) &&
5173 Left.isOneOf(Keywords.kw_optional, Keywords.kw_required,
5174 Keywords.kw_repeated, Keywords.kw_extend)) {
5177 if (
Right.is(tok::l_paren) &&
5178 Left.isOneOf(Keywords.kw_returns, Keywords.kw_option)) {
5181 if (
Right.isOneOf(tok::l_brace, tok::less) &&
Left.is(TT_SelectorName))
5184 if (
Left.is(tok::slash) ||
Right.is(tok::slash))
5186 if (
Left.MatchingParen &&
5187 Left.MatchingParen->is(TT_ProtoExtensionLSquare) &&
5188 Right.isOneOf(tok::l_brace, tok::less)) {
5189 return Style.Cpp11BracedListStyle == FormatStyle::BLS_Block;
5192 if (
Left.is(tok::percent))
5196 if (
Left.is(tok::numeric_constant) &&
Right.is(tok::percent))
5197 return Right.hasWhitespaceBefore();
5198 }
else if (Style.isJson()) {
5199 if (
Right.is(tok::colon) &&
Left.is(tok::string_literal))
5200 return Style.SpaceBeforeJsonColon;
5201 }
else if (Style.isCSharp()) {
5207 if (
Left.is(tok::kw_this) &&
Right.is(tok::l_square))
5211 if (
Left.is(tok::kw_new) &&
Right.is(tok::l_paren))
5215 if (
Right.is(tok::l_brace))
5219 if (
Left.is(tok::l_brace) &&
Right.isNot(tok::r_brace))
5222 if (
Left.isNot(tok::l_brace) &&
Right.is(tok::r_brace))
5226 if (
Left.is(TT_FatArrow) ||
Right.is(TT_FatArrow))
5230 if (
Left.is(TT_AttributeColon) ||
Right.is(TT_AttributeColon))
5234 if (
Left.is(TT_TemplateCloser) &&
Right.is(TT_StartOfName))
5238 if (
Left.is(tok::l_square) ||
Right.is(tok::r_square))
5239 return Style.SpacesInSquareBrackets;
5242 if (
Right.is(TT_CSharpNullable))
5246 if (
Right.is(TT_NonNullAssertion))
5250 if (
Left.is(tok::comma) &&
Right.is(tok::comma))
5254 if (
Left.is(Keywords.kw_var) &&
Right.is(tok::l_paren))
5258 if (
Right.is(tok::l_paren)) {
5259 if (
Left.isOneOf(tok::kw_using, Keywords.kw_async, Keywords.kw_when,
5260 Keywords.kw_lock)) {
5261 return Style.SpaceBeforeParensOptions.AfterControlStatements ||
5262 spaceRequiredBeforeParens(Right);
5268 if ((
Left.isAccessSpecifierKeyword() ||
5269 Left.isOneOf(tok::kw_virtual, tok::kw_extern, tok::kw_static,
5270 Keywords.kw_internal, Keywords.kw_abstract,
5271 Keywords.kw_sealed, Keywords.kw_override,
5272 Keywords.kw_async, Keywords.kw_unsafe)) &&
5273 Right.is(tok::l_paren)) {
5276 }
else if (Style.isJavaScript()) {
5277 if (
Left.is(TT_FatArrow))
5280 if (
Right.is(tok::l_paren) &&
Left.is(Keywords.kw_await) && BeforeLeft &&
5281 BeforeLeft->is(tok::kw_for)) {
5284 if (
Left.is(Keywords.kw_async) &&
Right.is(tok::l_paren) &&
5285 Right.MatchingParen) {
5292 if ((
Left.is(TT_TemplateString) &&
Left.TokenText.ends_with(
"${")) ||
5293 (
Right.is(TT_TemplateString) &&
Right.TokenText.starts_with(
"}"))) {
5298 if (Keywords.isJavaScriptIdentifier(Left,
5300 Right.is(TT_TemplateString)) {
5303 if (
Right.is(tok::star) &&
5304 Left.isOneOf(Keywords.kw_function, Keywords.kw_yield)) {
5307 if (
Right.isOneOf(tok::l_brace, tok::l_square) &&
5308 Left.isOneOf(Keywords.kw_function, Keywords.kw_yield,
5309 Keywords.kw_extends, Keywords.kw_implements)) {
5312 if (
Right.is(tok::l_paren)) {
5314 if (
Line.MustBeDeclaration &&
Left.Tok.getIdentifierInfo())
5318 if (BeforeLeft && BeforeLeft->is(tok::period) &&
5319 Left.Tok.getIdentifierInfo()) {
5323 if (
Left.isOneOf(tok::kw_throw, Keywords.kw_await, Keywords.kw_typeof,
5329 if (
Left.endsSequence(tok::kw_const, Keywords.kw_as))
5331 if ((
Left.isOneOf(Keywords.kw_let, Keywords.kw_var, Keywords.kw_in,
5336 (
Left.is(Keywords.kw_of) && BeforeLeft &&
5337 BeforeLeft->isOneOf(tok::identifier, tok::r_square, tok::r_brace))) &&
5338 (!BeforeLeft || BeforeLeft->isNot(tok::period))) {
5341 if (
Left.isOneOf(tok::kw_for, Keywords.kw_as) && BeforeLeft &&
5342 BeforeLeft->is(tok::period) &&
Right.is(tok::l_paren)) {
5345 if (
Left.is(Keywords.kw_as) &&
5346 Right.isOneOf(tok::l_square, tok::l_brace, tok::l_paren)) {
5349 if (
Left.is(tok::kw_default) && BeforeLeft &&
5350 BeforeLeft->is(tok::kw_export)) {
5353 if (
Left.is(Keywords.kw_is) &&
Right.is(tok::l_brace))
5355 if (
Right.isOneOf(TT_JsTypeColon, TT_JsTypeOptionalQuestion))
5357 if (
Left.is(TT_JsTypeOperator) ||
Right.is(TT_JsTypeOperator))
5359 if ((
Left.is(tok::l_brace) ||
Right.is(tok::r_brace)) &&
5360 Line.First->isOneOf(Keywords.kw_import, tok::kw_export)) {
5363 if (
Left.is(tok::ellipsis))
5365 if (
Left.is(TT_TemplateCloser) &&
5366 Right.isNoneOf(tok::equal, tok::l_brace, tok::comma, tok::l_square,
5367 Keywords.kw_implements, Keywords.kw_extends)) {
5373 if (
Right.is(TT_NonNullAssertion))
5375 if (
Left.is(TT_NonNullAssertion) &&
5376 Right.isOneOf(Keywords.kw_as, Keywords.kw_in)) {
5379 }
else if (Style.isJava()) {
5380 if (
Left.is(TT_CaseLabelArrow) ||
Right.is(TT_CaseLabelArrow))
5382 if (
Left.is(tok::r_square) &&
Right.is(tok::l_brace))
5385 if (
Left.is(tok::l_square) ||
Right.is(tok::r_square))
5386 return Style.SpacesInSquareBrackets;
5388 if (
Left.is(Keywords.kw_synchronized) &&
Right.is(tok::l_paren)) {
5389 return Style.SpaceBeforeParensOptions.AfterControlStatements ||
5390 spaceRequiredBeforeParens(Right);
5392 if ((
Left.isAccessSpecifierKeyword() ||
5393 Left.isOneOf(tok::kw_static, Keywords.kw_final, Keywords.kw_abstract,
5394 Keywords.kw_native)) &&
5395 Right.is(TT_TemplateOpener)) {
5398 }
else if (IsVerilog) {
5400 if (
Left.is(tok::identifier) &&
Left.TokenText[0] ==
'\\')
5404 if ((
Left.is(TT_VerilogTableItem) &&
5405 Right.isNoneOf(tok::r_paren, tok::semi)) ||
5406 (
Right.is(TT_VerilogTableItem) &&
Left.isNot(tok::l_paren))) {
5408 return !(
Next &&
Next->is(tok::r_paren));
5411 if (
Left.isNot(TT_BinaryOperator) &&
5412 Left.isOneOf(Keywords.kw_verilogHash, Keywords.kw_verilogHashHash)) {
5416 if (
Right.isNot(tok::semi) &&
5417 (
Left.endsSequence(tok::numeric_constant, Keywords.kw_verilogHash) ||
5418 Left.endsSequence(tok::numeric_constant,
5419 Keywords.kw_verilogHashHash) ||
5420 (
Left.is(tok::r_paren) &&
Left.MatchingParen &&
5421 Left.MatchingParen->endsSequence(tok::l_paren, tok::at)))) {
5426 if (
Left.is(Keywords.kw_apostrophe) ||
5427 (
Left.is(TT_VerilogNumberBase) &&
Right.is(tok::numeric_constant))) {
5431 if (
Left.is(tok::arrow) ||
Right.is(tok::arrow))
5436 if (
Left.is(tok::at) &&
Right.isOneOf(tok::l_paren, tok::star, tok::at))
5439 if (
Right.is(tok::l_square) &&
5440 Left.isOneOf(TT_VerilogDimensionedTypeName, Keywords.kw_function)) {
5444 if (
Right.isOneOf(tok::period, Keywords.kw_apostrophe) &&
5445 Keywords.isVerilogIdentifier(Left) &&
Left.getPreviousNonComment() &&
5446 Left.getPreviousNonComment()->is(Keywords.kw_tagged)) {
5452 if ((
Right.is(Keywords.kw_apostrophe) ||
5454 Left.isNoneOf(Keywords.kw_assign, Keywords.kw_unique) &&
5455 !Keywords.isVerilogWordOperator(Left) &&
5456 (
Left.isOneOf(tok::r_square, tok::r_paren, tok::r_brace,
5457 tok::numeric_constant) ||
5458 Keywords.isWordLike(Left))) {
5462 if ((
Right.is(tok::star) &&
Left.is(tok::coloncolon)) ||
5463 (
Left.is(tok::star) &&
Right.is(tok::semi))) {
5467 if (
Left.endsSequence(tok::star, tok::l_paren) &&
Right.is(tok::identifier))
5470 if (
Right.is(tok::l_paren) &&
Right.is(TT_VerilogStrength))
5473 if ((
Left.is(tok::l_brace) &&
5474 Right.isOneOf(tok::lessless, tok::greatergreater)) ||
5475 (
Left.endsSequence(tok::lessless, tok::l_brace) ||
5476 Left.endsSequence(tok::greatergreater, tok::l_brace))) {
5479 }
else if (Style.isTableGen()) {
5481 if (
Left.is(tok::l_square) &&
Right.is(tok::l_brace))
5483 if (
Left.is(tok::r_brace) &&
Right.is(tok::r_square))
5486 if (
Right.isOneOf(TT_TableGenDAGArgListColon,
5487 TT_TableGenDAGArgListColonToAlign) ||
5488 Left.isOneOf(TT_TableGenDAGArgListColon,
5489 TT_TableGenDAGArgListColonToAlign)) {
5492 if (
Right.is(TT_TableGenCondOperatorColon))
5494 if (
Left.isOneOf(TT_TableGenDAGArgOperatorID,
5495 TT_TableGenDAGArgOperatorToBreak) &&
5496 Right.isNot(TT_TableGenDAGArgCloser)) {
5500 if (
Right.isOneOf(tok::l_paren, tok::less) &&
5501 Left.isOneOf(TT_TableGenBangOperator, TT_TableGenCondOperator)) {
5506 if (
Left.is(TT_TableGenTrailingPasteOperator) &&
5507 Right.isOneOf(tok::l_brace, tok::colon)) {
5511 if (
Left.is(tok::hash) ||
Right.is(tok::hash))
5514 if (Keywords.isTableGenDefinition(Left))
5518 if (
Left.is(TT_ImplicitStringLiteral))
5519 return Right.hasWhitespaceBefore();
5521 if (
Left.is(TT_ObjCMethodSpecifier))
5522 return Style.ObjCSpaceAfterMethodDeclarationPrefix;
5523 if (
Left.is(tok::r_paren) &&
Left.isNot(TT_AttributeRParen) &&
5524 canBeObjCSelectorComponent(Right)) {
5532 (
Right.is(tok::equal) ||
Left.is(tok::equal))) {
5536 if (
Right.isOneOf(TT_TrailingReturnArrow, TT_LambdaArrow) ||
5537 Left.isOneOf(TT_TrailingReturnArrow, TT_LambdaArrow)) {
5540 if (
Left.is(tok::comma) &&
Right.isNot(TT_OverloadedOperatorLParen) &&
5543 (
Left.Children.empty() || !
Left.MacroParent)) {
5546 if (
Right.is(tok::comma))
5548 if (
Right.is(TT_ObjCBlockLParen))
5550 if (
Right.is(TT_CtorInitializerColon))
5551 return Style.SpaceBeforeCtorInitializerColon;
5552 if (
Right.is(TT_InheritanceColon) && !Style.SpaceBeforeInheritanceColon)
5554 if (
Right.is(TT_RangeBasedForLoopColon) &&
5555 !Style.SpaceBeforeRangeBasedForLoopColon) {
5558 if (
Left.is(TT_BitFieldColon)) {
5559 return Style.BitFieldColonSpacing == FormatStyle::BFCS_Both ||
5560 Style.BitFieldColonSpacing == FormatStyle::BFCS_After;
5562 if (
Right.is(tok::colon)) {
5563 if (
Right.is(TT_CaseLabelColon))
5564 return Style.SpaceBeforeCaseColon;
5565 if (
Right.is(TT_GotoLabelColon))
5568 if (!
Right.getNextNonComment())
5570 if (
Right.isOneOf(TT_ObjCSelector, TT_ObjCMethodExpr))
5572 if (
Left.is(tok::question))
5574 if (
Right.is(TT_InlineASMColon) &&
Left.is(tok::coloncolon))
5576 if (
Right.is(TT_DictLiteral))
5577 return Style.SpacesInContainerLiterals;
5578 if (
Right.is(TT_AttributeColon))
5580 if (
Right.is(TT_CSharpNamedArgumentColon))
5582 if (
Right.is(TT_GenericSelectionColon))
5584 if (
Right.is(TT_BitFieldColon)) {
5585 return Style.BitFieldColonSpacing == FormatStyle::BFCS_Both ||
5586 Style.BitFieldColonSpacing == FormatStyle::BFCS_Before;
5591 if ((
Left.isOneOf(tok::minus, tok::minusminus) &&
5592 Right.isOneOf(tok::minus, tok::minusminus)) ||
5593 (
Left.isOneOf(tok::plus, tok::plusplus) &&
5594 Right.isOneOf(tok::plus, tok::plusplus))) {
5597 if (
Left.is(TT_UnaryOperator)) {
5600 if (
Left.is(tok::amp) &&
Right.is(tok::r_square))
5601 return Style.SpacesInSquareBrackets;
5602 if (
Left.isNot(tok::exclaim))
5604 if (
Left.TokenText ==
"!")
5605 return Style.SpaceAfterLogicalNot;
5606 assert(
Left.TokenText ==
"not");
5607 return Right.isOneOf(tok::coloncolon, TT_UnaryOperator) ||
5608 (
Right.is(tok::l_paren) && Style.SpaceBeforeParensOptions.AfterNot);
5613 if (
Left.is(TT_CastRParen)) {
5614 return Style.SpaceAfterCStyleCast ||
5615 Right.isOneOf(TT_BinaryOperator, TT_SelectorName);
5618 auto ShouldAddSpacesInAngles = [
this, &
Right]() {
5619 if (this->Style.SpacesInAngles == FormatStyle::SIAS_Always)
5621 if (this->Style.SpacesInAngles == FormatStyle::SIAS_Leave)
5622 return Right.hasWhitespaceBefore();
5626 if (
Left.is(tok::greater) &&
Right.is(tok::greater)) {
5627 if (Style.isTextProto() ||
5628 (Style.Language == FormatStyle::LK_Proto &&
Left.is(TT_DictLiteral))) {
5629 return Style.Cpp11BracedListStyle == FormatStyle::BLS_Block;
5631 return Right.is(TT_TemplateCloser) &&
Left.is(TT_TemplateCloser) &&
5632 ((Style.Standard < FormatStyle::LS_Cpp11) ||
5633 ShouldAddSpacesInAngles());
5635 if (
Right.isOneOf(tok::arrow, tok::arrowstar, tok::periodstar) ||
5636 Left.isOneOf(tok::arrow, tok::period, tok::arrowstar, tok::periodstar) ||
5637 (
Right.is(tok::period) &&
Right.isNot(TT_DesignatedInitializerPeriod))) {
5640 if (!Style.SpaceBeforeAssignmentOperators &&
Left.isNot(TT_TemplateCloser) &&
5644 if (Style.isJava() &&
Right.is(tok::coloncolon) &&
5645 Left.isOneOf(tok::identifier, tok::kw_this)) {
5648 if (
Right.is(tok::coloncolon) &&
Left.is(tok::identifier)) {
5650 return Left.isPossibleMacro(
true) &&
5651 Right.hasWhitespaceBefore();
5653 if (
Right.is(tok::coloncolon) &&
5654 Left.isNoneOf(tok::l_brace, tok::comment, tok::l_paren)) {
5656 return (
Left.is(TT_TemplateOpener) &&
5657 ((Style.Standard < FormatStyle::LS_Cpp11) ||
5658 ShouldAddSpacesInAngles())) ||
5659 Left.isNoneOf(tok::l_paren, tok::r_paren, tok::l_square,
5660 tok::kw___super, TT_TemplateOpener,
5661 TT_TemplateCloser) ||
5662 (
Left.is(tok::l_paren) && Style.SpacesInParensOptions.Other);
5664 if ((
Left.is(TT_TemplateOpener)) != (
Right.is(TT_TemplateCloser)))
5665 return ShouldAddSpacesInAngles();
5666 if (
Left.is(tok::r_paren) &&
Left.isNot(TT_TypeDeclarationParen) &&
5667 Right.is(TT_PointerOrReference) &&
Right.isOneOf(tok::amp, tok::ampamp)) {
5671 if (
Right.is(TT_StructuredBindingLSquare)) {
5672 return Left.isNoneOf(tok::amp, tok::ampamp) ||
5673 getTokenReferenceAlignment(Left) != FormatStyle::PAS_Right;
5676 if (
Right.Next &&
Right.Next->is(TT_StructuredBindingLSquare) &&
5677 Right.isOneOf(tok::amp, tok::ampamp)) {
5678 return getTokenReferenceAlignment(Right) != FormatStyle::PAS_Left;
5680 if ((
Right.is(TT_BinaryOperator) &&
Left.isNot(tok::l_paren)) ||
5681 (
Left.isOneOf(TT_BinaryOperator, TT_ConditionalExpr) &&
5682 Right.isNot(tok::r_paren))) {
5685 if (
Right.is(TT_TemplateOpener) &&
Left.is(tok::r_paren) &&
5686 Left.MatchingParen &&
5687 Left.MatchingParen->is(TT_OverloadedOperatorLParen)) {
5690 if (
Right.is(tok::less) &&
Left.isNot(tok::l_paren) &&
5694 if (
Right.is(TT_TrailingUnaryOperator))
5696 if (
Left.is(TT_RegexLiteral))
5698 return spaceRequiredBetween(
Line, Left, Right);
5704 Tok.isNoneOf(TT_ObjCBlockLBrace, TT_LambdaLBrace, TT_DictLiteral);
5709 return Tok.MatchingParen &&
Tok.MatchingParen->Next &&
5710 Tok.MatchingParen->Next->isOneOf(tok::comma, tok::r_paren,
5716 FormatStyle::ShortLambdaStyle ShortLambdaOption) {
5717 return Tok.Children.empty() && ShortLambdaOption != FormatStyle::SLS_None;
5722 Tok.isNoneOf(TT_ObjCBlockLBrace, TT_DictLiteral);
5725bool TokenAnnotator::mustBreakBefore(AnnotatedLine &
Line,
5727 if (Right.NewlinesBefore > 1 && Style.MaxEmptyLinesToKeep > 0 &&
5728 (!Style.RemoveEmptyLinesInUnwrappedLines || &Right ==
Line.First)) {
5734 if (Style.BreakFunctionDefinitionParameters &&
Line.MightBeFunctionDecl &&
5735 Line.mightBeFunctionDefinition() && Left.MightBeFunctionDeclParen &&
5736 Left.ParameterCount > 0) {
5742 if (Style.BinPackParameters == FormatStyle::BPPS_AlwaysOnePerLine &&
5743 Line.MightBeFunctionDecl && !
Left.opensScope() &&
5748 const auto *BeforeLeft =
Left.Previous;
5749 const auto *AfterRight =
Right.Next;
5751 if (Style.isCSharp()) {
5752 if (
Left.is(TT_FatArrow) &&
Right.is(tok::l_brace) &&
5753 Style.BraceWrapping.AfterFunction) {
5756 if (
Right.is(TT_CSharpNamedArgumentColon) ||
5757 Left.is(TT_CSharpNamedArgumentColon)) {
5760 if (
Right.is(TT_CSharpGenericTypeConstraint))
5762 if (AfterRight && AfterRight->is(TT_FatArrow) &&
5763 (
Right.is(tok::numeric_constant) ||
5764 (
Right.is(tok::identifier) &&
Right.TokenText ==
"_"))) {
5769 if (
Left.is(TT_AttributeRSquare) &&
5770 (
Right.isAccessSpecifier(
false) ||
5771 Right.is(Keywords.kw_internal))) {
5775 if (
Left.is(TT_AttributeRSquare) &&
Right.is(TT_AttributeLSquare))
5777 }
else if (Style.isJavaScript()) {
5779 if (
Right.is(tok::string_literal) &&
Left.is(tok::plus) && BeforeLeft &&
5780 BeforeLeft->is(tok::string_literal)) {
5783 if (
Left.is(TT_DictLiteral) &&
Left.is(tok::l_brace) &&
Line.Level == 0 &&
5784 BeforeLeft && BeforeLeft->is(tok::equal) &&
5785 Line.First->isOneOf(tok::identifier, Keywords.kw_import, tok::kw_export,
5789 Line.First->isNoneOf(Keywords.kw_var, Keywords.kw_let)) {
5794 if (
Left.is(tok::l_brace) &&
Line.Level == 0 &&
5795 (
Line.startsWith(tok::kw_enum) ||
5796 Line.startsWith(tok::kw_const, tok::kw_enum) ||
5797 Line.startsWith(tok::kw_export, tok::kw_enum) ||
5798 Line.startsWith(tok::kw_export, tok::kw_const, tok::kw_enum))) {
5803 if (
Right.is(tok::r_brace) &&
Left.is(tok::l_brace) && BeforeLeft &&
5804 BeforeLeft->is(TT_FatArrow)) {
5806 switch (Style.AllowShortLambdasOnASingleLine) {
5807 case FormatStyle::SLS_All:
5809 case FormatStyle::SLS_None:
5811 case FormatStyle::SLS_Empty:
5812 return !
Left.Children.empty();
5813 case FormatStyle::SLS_Inline:
5816 return (
Left.NestingLevel == 0 &&
Line.Level == 0) &&
5817 !
Left.Children.empty();
5819 llvm_unreachable(
"Unknown FormatStyle::ShortLambdaStyle enum");
5822 if (
Right.is(tok::r_brace) &&
Left.is(tok::l_brace) &&
5823 !
Left.Children.empty()) {
5825 if (
Left.NestingLevel == 0 &&
Line.Level == 0)
5826 return !Style.AllowShortFunctionsOnASingleLine.Other;
5828 return !Style.AllowShortFunctionsOnASingleLine.Inline;
5830 }
else if (Style.isJava()) {
5831 if (
Right.is(tok::plus) &&
Left.is(tok::string_literal) && AfterRight &&
5832 AfterRight->is(tok::string_literal)) {
5835 }
else if (Style.isVerilog()) {
5837 if (
Left.is(TT_VerilogAssignComma))
5840 if (
Left.is(TT_VerilogTypeComma))
5844 if (Style.VerilogBreakBetweenInstancePorts &&
5845 (
Left.is(TT_VerilogInstancePortComma) ||
5846 (
Left.is(tok::r_paren) && Keywords.isVerilogIdentifier(Right) &&
5847 Left.MatchingParen &&
5848 Left.MatchingParen->is(TT_VerilogInstancePortLParen)))) {
5853 if (!Keywords.isVerilogBegin(Right) && Keywords.isVerilogEndOfLabel(Left))
5855 }
else if (Style.BreakAdjacentStringLiterals &&
5856 (IsCpp || Style.isProto() || Style.isTableGen())) {
5857 if (
Left.isStringLiteral() &&
Right.isStringLiteral())
5862 if (Style.isJson()) {
5866 if (
Left.is(TT_DictLiteral) &&
Left.is(tok::l_brace))
5869 if ((
Left.is(TT_ArrayInitializerLSquare) &&
Left.is(tok::l_square) &&
5870 Right.isNot(tok::r_square)) ||
5871 Left.is(tok::comma)) {
5872 if (
Right.is(tok::l_brace))
5877 if (
Tok->isOneOf(tok::l_brace, tok::l_square))
5879 if (
Tok->isOneOf(tok::r_brace, tok::r_square))
5882 return Style.BreakArrays;
5884 }
else if (Style.isTableGen()) {
5888 if (
Left.is(TT_TableGenCondOperatorComma))
5890 if (
Left.is(TT_TableGenDAGArgOperatorToBreak) &&
5891 Right.isNot(TT_TableGenDAGArgCloser)) {
5894 if (
Left.is(TT_TableGenDAGArgListCommaToBreak))
5896 if (
Right.is(TT_TableGenDAGArgCloser) &&
Right.MatchingParen &&
5897 Right.MatchingParen->is(TT_TableGenDAGArgOpenerToBreak) &&
5898 &Left !=
Right.MatchingParen->Next) {
5900 return Style.TableGenBreakInsideDAGArg == FormatStyle::DAS_BreakAll;
5904 if (
Line.startsWith(tok::kw_asm) &&
Right.is(TT_InlineASMColon) &&
5905 Style.BreakBeforeInlineASMColon == FormatStyle::BBIAS_Always) {
5915 if ((
Left.isOneOf(tok::l_brace, TT_ArrayInitializerLSquare) ||
5916 (Style.isJavaScript() &&
Left.is(tok::l_paren))) &&
5918 BeforeClosingBrace =
Left.MatchingParen->Previous;
5919 }
else if (
Right.MatchingParen &&
5920 (
Right.MatchingParen->isOneOf(tok::l_brace,
5921 TT_ArrayInitializerLSquare) ||
5922 (Style.isJavaScript() &&
5923 Right.MatchingParen->is(tok::l_paren)))) {
5924 BeforeClosingBrace = &
Left;
5926 if (BeforeClosingBrace && (BeforeClosingBrace->is(tok::comma) ||
5927 BeforeClosingBrace->isTrailingComment())) {
5932 if (
Right.is(tok::comment)) {
5934 Right.NewlinesBefore > 0 &&
Right.HasUnescapedNewline;
5936 if (
Left.isTrailingComment())
5938 if (
Left.IsUnterminatedLiteral)
5941 if (BeforeLeft && BeforeLeft->is(tok::lessless) &&
5942 Left.is(tok::string_literal) &&
Right.is(tok::lessless) && AfterRight &&
5943 AfterRight->is(tok::string_literal)) {
5944 return Right.NewlinesBefore > 0;
5947 if (
Right.is(TT_RequiresClause)) {
5948 switch (Style.RequiresClausePosition) {
5949 case FormatStyle::RCPS_OwnLine:
5950 case FormatStyle::RCPS_OwnLineWithBrace:
5951 case FormatStyle::RCPS_WithFollowing:
5958 if (
Left.ClosesTemplateDeclaration &&
Left.MatchingParen &&
5959 Left.MatchingParen->NestingLevel == 0) {
5963 if (
Right.is(tok::kw_concept))
5964 return Style.BreakBeforeConceptDeclarations == FormatStyle::BBCDS_Always;
5965 return Style.BreakTemplateDeclarations == FormatStyle::BTDS_Yes ||
5966 (Style.BreakTemplateDeclarations == FormatStyle::BTDS_Leave &&
5967 Right.NewlinesBefore > 0);
5969 if (
Left.ClosesRequiresClause) {
5970 switch (Style.RequiresClausePosition) {
5971 case FormatStyle::RCPS_OwnLine:
5972 case FormatStyle::RCPS_WithPreceding:
5973 return Right.isNot(tok::semi);
5974 case FormatStyle::RCPS_OwnLineWithBrace:
5975 return Right.isNoneOf(tok::semi, tok::l_brace);
5980 if (Style.PackConstructorInitializers == FormatStyle::PCIS_Never) {
5981 if (Style.BreakConstructorInitializers == FormatStyle::BCIS_BeforeColon &&
5982 (
Left.is(TT_CtorInitializerComma) ||
5983 Right.is(TT_CtorInitializerColon))) {
5987 if (Style.BreakConstructorInitializers == FormatStyle::BCIS_AfterColon &&
5988 Left.isOneOf(TT_CtorInitializerColon, TT_CtorInitializerComma)) {
5992 if (Style.BreakConstructorInitializers == FormatStyle::BCIS_AfterComma &&
5993 Left.is(TT_CtorInitializerComma)) {
5997 if (Style.PackConstructorInitializers < FormatStyle::PCIS_CurrentLine &&
5998 Style.BreakConstructorInitializers == FormatStyle::BCIS_BeforeComma &&
5999 Right.isOneOf(TT_CtorInitializerComma, TT_CtorInitializerColon)) {
6002 if (Style.PackConstructorInitializers == FormatStyle::PCIS_NextLineOnly) {
6003 if ((Style.BreakConstructorInitializers == FormatStyle::BCIS_BeforeColon ||
6004 Style.BreakConstructorInitializers == FormatStyle::BCIS_BeforeComma) &&
6005 Right.is(TT_CtorInitializerColon)) {
6009 if (Style.BreakConstructorInitializers == FormatStyle::BCIS_AfterColon &&
6010 Left.is(TT_CtorInitializerColon)) {
6015 if (Style.BreakInheritanceList == FormatStyle::BILS_BeforeComma &&
6016 Right.is(TT_InheritanceComma)) {
6019 if (Style.BreakInheritanceList == FormatStyle::BILS_AfterComma &&
6020 Left.is(TT_InheritanceComma)) {
6023 if (
Right.is(tok::string_literal) &&
Right.TokenText.starts_with(
"R\"")) {
6027 return Right.IsMultiline &&
Right.NewlinesBefore > 0;
6029 if ((
Left.is(tok::l_brace) ||
6030 (
Left.is(tok::less) && BeforeLeft && BeforeLeft->is(tok::equal))) &&
6031 Right.NestingLevel == 1 && Style.Language == FormatStyle::LK_Proto) {
6036 if (
Right.is(TT_InlineASMBrace))
6037 return Right.HasUnescapedNewline;
6040 auto *FirstNonComment =
Line.getFirstNonComment();
6042 FirstNonComment && (FirstNonComment->is(Keywords.kw_internal) ||
6043 FirstNonComment->isAccessSpecifierKeyword());
6045 if (Style.BraceWrapping.AfterEnum) {
6046 if (
Line.startsWith(tok::kw_enum) ||
6047 Line.startsWith(tok::kw_typedef, tok::kw_enum) ||
6048 Line.startsWith(tok::kw_export, tok::kw_enum)) {
6053 FirstNonComment->Next->is(tok::kw_enum)) {
6059 if (Style.BraceWrapping.AfterClass &&
6061 FirstNonComment->Next->is(Keywords.kw_interface)) ||
6062 Line.startsWith(Keywords.kw_interface))) {
6067 if (
Right.isNot(TT_FunctionLBrace)) {
6068 return Style.AllowShortRecordOnASingleLine == FormatStyle::SRS_Never &&
6069 ((
Line.startsWith(tok::kw_class) &&
6070 Style.BraceWrapping.AfterClass) ||
6071 (
Line.startsWith(tok::kw_struct) &&
6072 Style.BraceWrapping.AfterStruct) ||
6073 (
Line.startsWith(tok::kw_union) &&
6074 Style.BraceWrapping.AfterUnion));
6078 if (
Left.is(TT_ObjCBlockLBrace) &&
6079 Style.AllowShortBlocksOnASingleLine == FormatStyle::SBS_Never) {
6084 if (
Left.isOneOf(TT_AttributeRParen, TT_AttributeMacro) &&
6085 Right.is(TT_ObjCDecl)) {
6089 if (
Left.is(TT_LambdaLBrace)) {
6091 Style.AllowShortLambdasOnASingleLine == FormatStyle::SLS_Inline) {
6095 if (Style.AllowShortLambdasOnASingleLine == FormatStyle::SLS_None ||
6096 Style.AllowShortLambdasOnASingleLine == FormatStyle::SLS_Inline ||
6097 (!
Left.Children.empty() &&
6098 Style.AllowShortLambdasOnASingleLine == FormatStyle::SLS_Empty)) {
6103 if (Style.BraceWrapping.BeforeLambdaBody &&
Right.is(TT_LambdaLBrace) &&
6104 (
Left.isPointerOrReference() ||
Left.is(TT_TemplateCloser))) {
6109 if ((Style.isJava() || Style.isJavaScript()) &&
6110 Left.is(TT_LeadingJavaAnnotation) &&
6111 Right.isNoneOf(TT_LeadingJavaAnnotation, tok::l_paren) &&
6112 (
Line.Last->is(tok::l_brace) || Style.BreakAfterJavaFieldAnnotations)) {
6116 if (
Right.is(TT_ProtoExtensionLSquare))
6146 if (Style.isProto() &&
Right.is(TT_SelectorName) &&
6147 Right.isNot(tok::r_square) && AfterRight) {
6150 if (
Left.is(tok::at))
6156 const auto *LBrace = AfterRight;
6157 if (LBrace && LBrace->is(tok::colon)) {
6158 LBrace = LBrace->Next;
6159 if (LBrace && LBrace->is(tok::at)) {
6160 LBrace = LBrace->Next;
6162 LBrace = LBrace->Next;
6174 ((LBrace->is(tok::l_brace) &&
6175 (LBrace->is(TT_DictLiteral) ||
6176 (LBrace->Next && LBrace->Next->is(tok::r_brace)))) ||
6177 LBrace->isOneOf(TT_ArrayInitializerLSquare, tok::less))) {
6184 if (
Left.ParameterCount == 0)
6199 if (
Left.isOneOf(tok::r_brace, tok::greater, tok::r_square))
6203 if (Style.BreakAfterAttributes == FormatStyle::ABS_LeaveAll &&
6204 Left.is(TT_AttributeRSquare) &&
Right.NewlinesBefore > 0) {
6205 Line.ReturnTypeWrapped =
true;
6216 if (Style.isCSharp()) {
6217 if (
Left.isOneOf(TT_CSharpNamedArgumentColon, TT_AttributeColon) ||
6218 Right.isOneOf(TT_CSharpNamedArgumentColon, TT_AttributeColon)) {
6222 if (
Line.First->is(TT_CSharpGenericTypeConstraint))
6223 return Left.is(TT_CSharpGenericTypeConstraintComma);
6225 if (
Right.is(TT_CSharpNullable))
6227 }
else if (Style.isJava()) {
6228 if (
Left.isOneOf(Keywords.kw_throws, Keywords.kw_extends,
6229 Keywords.kw_implements)) {
6232 if (
Right.isOneOf(Keywords.kw_throws, Keywords.kw_extends,
6233 Keywords.kw_implements)) {
6236 }
else if (Style.isJavaScript()) {
6239 (NonComment->isAccessSpecifierKeyword() ||
6240 NonComment->isOneOf(
6241 tok::kw_return, Keywords.kw_yield, tok::kw_continue, tok::kw_break,
6242 tok::kw_throw, Keywords.kw_interface, Keywords.kw_type,
6243 tok::kw_static, Keywords.kw_readonly, Keywords.kw_override,
6244 Keywords.kw_abstract, Keywords.kw_get, Keywords.kw_set,
6245 Keywords.kw_async, Keywords.kw_await))) {
6248 if (
Right.NestingLevel == 0 &&
6249 (
Left.Tok.getIdentifierInfo() ||
6250 Left.isOneOf(tok::r_square, tok::r_paren)) &&
6251 Right.isOneOf(tok::l_square, tok::l_paren)) {
6254 if (NonComment && NonComment->is(tok::identifier) &&
6255 NonComment->TokenText ==
"asserts") {
6258 if (
Left.is(TT_FatArrow) &&
Right.is(tok::l_brace))
6260 if (
Left.is(TT_JsTypeColon))
6263 if (
Left.is(tok::exclaim) &&
Right.is(tok::colon))
6268 if (
Right.is(Keywords.kw_is)) {
6277 if (!
Next ||
Next->isNot(tok::colon))
6280 if (
Left.is(Keywords.kw_in))
6281 return Style.BreakBeforeBinaryOperators == FormatStyle::BOS_None;
6282 if (
Right.is(Keywords.kw_in))
6283 return Style.BreakBeforeBinaryOperators != FormatStyle::BOS_None;
6284 if (
Right.is(Keywords.kw_as))
6286 if (
Right.isOneOf(Keywords.kw_extends, Keywords.kw_infer)) {
6292 if (
Left.is(Keywords.kw_as))
6294 if (
Left.is(TT_NonNullAssertion))
6296 if (
Left.is(Keywords.kw_declare) &&
6297 Right.isOneOf(Keywords.kw_module, tok::kw_namespace,
6298 Keywords.kw_function, tok::kw_class, tok::kw_enum,
6299 Keywords.kw_interface, Keywords.kw_type, Keywords.kw_var,
6300 Keywords.kw_let, tok::kw_const)) {
6305 if (
Left.isOneOf(Keywords.kw_module, tok::kw_namespace) &&
6306 Right.isOneOf(tok::identifier, tok::string_literal)) {
6309 if (
Right.is(TT_TemplateString) &&
Right.closesScope())
6313 if (
Left.is(tok::identifier) &&
Right.is(TT_TemplateString))
6315 if (
Left.is(TT_TemplateString) &&
Left.opensScope())
6317 }
else if (Style.isTableGen()) {
6319 if (Keywords.isTableGenDefinition(Left))
6322 if (
Right.is(tok::l_paren)) {
6323 return Left.isNoneOf(TT_TableGenBangOperator, TT_TableGenCondOperator,
6327 if (
Left.is(TT_TableGenValueSuffix))
6330 if (
Left.is(tok::hash) ||
Right.is(tok::hash))
6332 if (
Left.isOneOf(TT_TableGenBangOperator, TT_TableGenCondOperator))
6339 if (
Right.is(tok::r_brace)) {
6341 (
Right.isBlockIndentedInitRBrace(Style)));
6346 if (
Right.is(tok::r_paren)) {
6347 if (!
Right.MatchingParen)
6350 if (
Next &&
Next->is(tok::r_paren))
6352 if (
Next &&
Next->is(tok::l_paren))
6358 return Style.BreakBeforeCloseBracketIf;
6360 return Style.BreakBeforeCloseBracketLoop;
6362 return Style.BreakBeforeCloseBracketSwitch;
6363 return Style.BreakBeforeCloseBracketFunction;
6366 if (
Left.isOneOf(tok::r_paren, TT_TrailingAnnotation) &&
6367 Right.is(TT_TrailingAnnotation) &&
6368 Style.BreakBeforeCloseBracketFunction) {
6372 if (
Right.is(TT_TemplateCloser))
6373 return Style.BreakBeforeTemplateCloser;
6375 if (
Left.isOneOf(tok::at, tok::objc_interface))
6377 if (
Left.isOneOf(TT_JavaAnnotation, TT_LeadingJavaAnnotation))
6378 return Right.isNot(tok::l_paren);
6379 if (
Right.is(TT_PointerOrReference)) {
6380 return Line.IsMultiVariableDeclStmt ||
6381 (getTokenPointerOrReferenceAlignment(Right) ==
6382 FormatStyle::PAS_Right &&
6384 Right.Next->isOneOf(TT_FunctionDeclarationName, tok::kw_const)));
6386 if (
Right.isOneOf(TT_StartOfName, TT_FunctionDeclarationName,
6387 TT_ClassHeadName, TT_QtProperty, tok::kw_operator)) {
6390 if (
Left.is(TT_PointerOrReference))
6392 if (
Right.isTrailingComment()) {
6399 (
Left.is(TT_CtorInitializerColon) &&
Right.NewlinesBefore > 0 &&
6400 Style.BreakConstructorInitializers == FormatStyle::BCIS_AfterColon);
6402 if (
Left.is(tok::question) &&
Right.is(tok::colon))
6404 if (
Right.isOneOf(TT_ConditionalExpr, tok::question))
6405 return Style.BreakBeforeTernaryOperators;
6406 if (
Left.isOneOf(TT_ConditionalExpr, tok::question))
6407 return !Style.BreakBeforeTernaryOperators;
6408 if (
Left.is(TT_InheritanceColon))
6409 return Style.BreakInheritanceList == FormatStyle::BILS_AfterColon;
6410 if (
Right.is(TT_InheritanceColon))
6411 return Style.BreakInheritanceList != FormatStyle::BILS_AfterColon;
6413 if (
Right.is(TT_ObjCMethodExpr) &&
Right.isNot(tok::r_square) &&
6414 Left.isNot(TT_SelectorName)) {
6418 if (
Right.is(tok::colon) &&
6419 Right.isNoneOf(TT_CtorInitializerColon, TT_InlineASMColon,
6420 TT_BitFieldColon)) {
6423 if (
Left.is(tok::colon) &&
Left.isOneOf(TT_ObjCSelector, TT_ObjCMethodExpr))
6425 if (
Left.is(tok::colon) &&
Left.is(TT_DictLiteral)) {
6426 if (Style.isProto()) {
6427 if (!Style.AlwaysBreakBeforeMultilineStrings &&
Right.isStringLiteral())
6453 if ((
Right.isOneOf(tok::l_brace, tok::less) &&
6454 Right.is(TT_DictLiteral)) ||
6455 Right.is(TT_ArrayInitializerLSquare)) {
6461 if (
Right.is(tok::r_square) &&
Right.MatchingParen &&
6462 Right.MatchingParen->is(TT_ProtoExtensionLSquare)) {
6465 if (
Right.is(TT_SelectorName) || (
Right.is(tok::identifier) &&
Right.Next &&
6466 Right.Next->is(TT_ObjCMethodExpr))) {
6467 return Left.isNot(tok::period);
6471 if (
Right.is(tok::kw_concept))
6472 return Style.BreakBeforeConceptDeclarations != FormatStyle::BBCDS_Never;
6473 if (
Right.is(TT_RequiresClause))
6475 if (
Left.ClosesTemplateDeclaration) {
6476 return Style.BreakTemplateDeclarations != FormatStyle::BTDS_Leave ||
6477 Right.NewlinesBefore > 0;
6479 if (
Left.is(TT_FunctionAnnotationRParen))
6481 if (
Left.ClosesRequiresClause)
6483 if (
Right.isOneOf(TT_RangeBasedForLoopColon, TT_OverloadedOperatorLParen,
6484 TT_OverloadedOperator)) {
6487 if (
Left.is(TT_RangeBasedForLoopColon))
6489 if (
Right.is(TT_RangeBasedForLoopColon))
6491 if (
Left.is(TT_TemplateCloser) &&
Right.is(TT_TemplateOpener))
6493 if ((
Left.is(tok::greater) &&
Right.is(tok::greater)) ||
6494 (
Left.is(tok::less) &&
Right.is(tok::less))) {
6497 if (
Right.is(TT_BinaryOperator) &&
6498 Style.BreakBeforeBinaryOperators != FormatStyle::BOS_None &&
6499 (Style.BreakBeforeBinaryOperators == FormatStyle::BOS_All ||
6503 if (
Left.isOneOf(TT_TemplateCloser, TT_UnaryOperator, tok::kw_operator))
6505 if (
Left.is(tok::equal) &&
Right.isNoneOf(tok::kw_default, tok::kw_delete) &&
6509 if (
Left.is(tok::equal) &&
Right.is(tok::l_brace) &&
6510 Style.Cpp11BracedListStyle == FormatStyle::BLS_Block) {
6513 if (
Left.is(TT_AttributeLParen) ||
6514 (
Left.is(tok::l_paren) &&
Left.is(TT_TypeDeclarationParen))) {
6517 if (
Left.is(tok::l_paren) &&
Left.Previous &&
6518 (
Left.Previous->isOneOf(TT_BinaryOperator, TT_CastRParen))) {
6521 if (
Right.is(TT_ImplicitStringLiteral))
6524 if (
Right.is(tok::r_square) &&
Right.MatchingParen &&
6525 Right.MatchingParen->is(TT_LambdaLSquare)) {
6531 if (
Left.is(TT_TrailingAnnotation)) {
6532 return Right.isNoneOf(tok::l_brace, tok::semi, tok::equal, tok::l_paren,
6533 tok::less, tok::coloncolon);
6536 if (
Right.isAttribute())
6539 if (
Right.is(TT_AttributeLSquare)) {
6540 assert(
Left.isNot(tok::l_square));
6544 if (
Left.is(tok::identifier) &&
Right.is(tok::string_literal))
6547 if (
Right.is(tok::identifier) &&
Right.Next &&
Right.Next->is(TT_DictLiteral))
6550 if (
Left.is(TT_CtorInitializerColon)) {
6551 return (Style.BreakConstructorInitializers ==
6552 FormatStyle::BCIS_AfterColon ||
6553 Style.BreakConstructorInitializers ==
6554 FormatStyle::BCIS_AfterComma) &&
6555 (!
Right.isTrailingComment() ||
Right.NewlinesBefore > 0);
6557 if (
Right.is(TT_CtorInitializerColon)) {
6558 return Style.BreakConstructorInitializers != FormatStyle::BCIS_AfterColon &&
6559 Style.BreakConstructorInitializers != FormatStyle::BCIS_AfterComma;
6561 if (
Left.is(TT_CtorInitializerComma) &&
6562 Style.BreakConstructorInitializers == FormatStyle::BCIS_BeforeComma) {
6565 if (
Right.is(TT_CtorInitializerComma) &&
6566 Style.BreakConstructorInitializers == FormatStyle::BCIS_BeforeComma) {
6569 if (
Left.is(TT_InheritanceComma) &&
6570 Style.BreakInheritanceList == FormatStyle::BILS_BeforeComma) {
6573 if (
Right.is(TT_InheritanceComma) &&
6574 Style.BreakInheritanceList == FormatStyle::BILS_BeforeComma) {
6577 if (
Left.is(TT_ArrayInitializerLSquare))
6579 if (
Right.is(tok::kw_typename) &&
Left.isNot(tok::kw_const))
6581 if ((
Left.isBinaryOperator() ||
Left.is(TT_BinaryOperator)) &&
6582 Left.isNoneOf(tok::arrowstar, tok::lessless) &&
6583 Style.BreakBeforeBinaryOperators != FormatStyle::BOS_All &&
6584 (Style.BreakBeforeBinaryOperators == FormatStyle::BOS_None ||
6588 if (
Left.is(TT_AttributeLSquare) &&
Right.is(tok::l_square)) {
6589 assert(
Right.isNot(TT_AttributeLSquare));
6592 if (
Left.is(tok::r_square) &&
Right.is(TT_AttributeRSquare)) {
6593 assert(
Left.isNot(TT_AttributeRSquare));
6597 auto ShortLambdaOption = Style.AllowShortLambdasOnASingleLine;
6598 if (Style.BraceWrapping.BeforeLambdaBody &&
Right.is(TT_LambdaLBrace)) {
6605 if (
Right.is(tok::kw_noexcept) &&
Right.is(TT_TrailingAnnotation)) {
6606 switch (Style.AllowBreakBeforeNoexceptSpecifier) {
6607 case FormatStyle::BBNSS_Never:
6609 case FormatStyle::BBNSS_Always:
6611 case FormatStyle::BBNSS_OnlyWithParen:
6612 return Right.Next &&
Right.Next->is(tok::l_paren);
6616 return Left.isOneOf(tok::comma, tok::coloncolon, tok::semi, tok::l_brace,
6617 tok::kw_class, tok::kw_struct, tok::comment) ||
6618 Right.isMemberAccess() ||
6619 Right.isOneOf(TT_TrailingReturnArrow, TT_LambdaArrow, tok::lessless,
6620 tok::colon, tok::l_square, tok::at) ||
6621 (
Left.is(tok::r_paren) &&
6622 Right.isOneOf(tok::identifier, tok::kw_const)) ||
6623 (
Left.is(tok::l_paren) &&
Right.isNot(tok::r_paren)) ||
6624 (
Left.is(TT_TemplateOpener) &&
Right.isNot(TT_TemplateCloser));
6628 llvm::errs() <<
"AnnotatedTokens(L=" <<
Line.Level <<
", P=" <<
Line.PPLevel
6629 <<
", T=" <<
Line.Type <<
", C=" <<
Line.IsContinuation
6633 llvm::errs() <<
" I=" <<
Tok->IndentLevel <<
" M=" <<
Tok->MustBreakBefore
6634 <<
" C=" <<
Tok->CanBreakBefore
6636 <<
" S=" <<
Tok->SpacesRequiredBefore
6637 <<
" F=" <<
Tok->Finalized <<
" B=" <<
Tok->BlockParameterCount
6638 <<
" BK=" <<
Tok->getBlockKind() <<
" P=" <<
Tok->SplitPenalty
6639 <<
" Name=" <<
Tok->Tok.getName() <<
" N=" <<
Tok->NestingLevel
6640 <<
" L=" <<
Tok->TotalLength
6641 <<
" PPK=" <<
Tok->getPackingKind() <<
" FakeLParens=";
6643 llvm::errs() << LParen <<
"/";
6644 llvm::errs() <<
" FakeRParens=" <<
Tok->FakeRParens;
6645 llvm::errs() <<
" II=" <<
Tok->Tok.getIdentifierInfo();
6646 llvm::errs() <<
" Text='" <<
Tok->TokenText <<
"'\n";
6651 llvm::errs() <<
"----\n";
6654FormatStyle::PointerAlignmentStyle
6656 assert(
Reference.isOneOf(tok::amp, tok::ampamp));
6657 switch (Style.ReferenceAlignment) {
6658 case FormatStyle::RAS_Pointer:
6659 return Style.PointerAlignment;
6660 case FormatStyle::RAS_Left:
6661 return FormatStyle::PAS_Left;
6662 case FormatStyle::RAS_Right:
6663 return FormatStyle::PAS_Right;
6664 case FormatStyle::RAS_Middle:
6665 return FormatStyle::PAS_Middle;
6668 return Style.PointerAlignment;
6671FormatStyle::PointerAlignmentStyle
6672TokenAnnotator::getTokenPointerOrReferenceAlignment(
6674 if (PointerOrReference.isOneOf(tok::amp, tok::ampamp))
6675 return getTokenReferenceAlignment(PointerOrReference);
6676 assert(PointerOrReference.is(tok::star));
6677 return Style.PointerAlignment;
This file implements a token annotator, i.e.
Defines the clang::TokenKind enum and support functions.
#define TRANSFORM_TYPE_TRAIT_DEF(Enum, _)
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
Parser - This implements a parser for the C family of languages.
PRESERVE_NONE bool Ret(InterpState &S, CodePtr &PC)
TokenKind
Provides a simple uniform namespace for tokens from all C languages.
The JSON file list parser is used to communicate input to InstallAPI.
AccessSpecifier
A C++ access specifier (public, private, protected), plus the special value "none" which means differ...
nullptr
This class represents a compute construct, representing a 'Kind' of ‘parallel’, 'serial',...
@ Parameter
The parameter type of a method or function.
@ Result
The result type of a method or function.
@ Type
The name was classified as a type.
prec::Level getBinOpPrecedence(tok::TokenKind Kind, bool GreaterThanIsOperator, bool CPlusPlus11)
Return the precedence of the specified binary operator token.