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))
92 if (
Tok.Previous &&
Tok.Previous->isOneOf(tok::at, tok::l_square))
99 if (AttrTok->startsSequence(tok::kw_using, tok::identifier, tok::colon))
101 if (AttrTok->isNot(tok::identifier))
103 while (AttrTok && !AttrTok->startsSequence(tok::r_square, tok::r_square)) {
107 if (AttrTok->is(tok::colon) ||
108 AttrTok->startsSequence(tok::identifier, tok::identifier) ||
109 AttrTok->startsSequence(tok::r_paren, tok::identifier)) {
112 if (AttrTok->is(tok::ellipsis))
114 AttrTok = AttrTok->Next;
116 return AttrTok && AttrTok->startsSequence(tok::r_square, tok::r_square);
124class AnnotatingParser {
126 AnnotatingParser(
const FormatStyle &Style, AnnotatedLine &Line,
127 const AdditionalKeywords &Keywords,
128 SmallVector<ScopeType> &Scopes)
129 : Style(Style), Line(Line), CurrentToken(Line.
First), AutoFound(
false),
131 Keywords(Keywords), Scopes(Scopes), TemplateDeclarationDepth(0) {
132 Contexts.push_back(Context(tok::unknown, 1,
false));
133 resetTokenMetadata();
138 switch (Token.getType()) {
140 case TT_StructLBrace:
145 case TT_CompoundRequirementLBrace:
156 auto *
Left = CurrentToken->Previous;
160 if (NonTemplateLess.count(Left) > 0)
163 const auto *BeforeLess =
Left->Previous;
166 if (BeforeLess->Tok.isLiteral())
168 if (BeforeLess->is(tok::r_brace))
170 if (BeforeLess->is(tok::r_paren) && Contexts.size() > 1 &&
171 !(BeforeLess->MatchingParen &&
172 BeforeLess->MatchingParen->is(TT_OverloadedOperatorLParen))) {
175 if (BeforeLess->is(tok::kw_operator) && CurrentToken->is(tok::l_paren))
179 Left->ParentBracket = Contexts.back().ContextKind;
180 ScopedContextCreator ContextCreator(*
this, tok::less, 12);
181 Contexts.back().IsExpression =
false;
185 if (BeforeLess && BeforeLess->isNot(tok::kw_template))
186 Contexts.back().ContextType = Context::TemplateArgument;
188 if (Style.isJava() && CurrentToken->is(tok::question))
191 for (
bool SeenTernaryOperator =
false, MaybeAngles =
true; CurrentToken;) {
192 const auto &ParentContext = Contexts[Contexts.size() - 2];
193 const bool InExpr = ParentContext.IsExpression;
194 if (CurrentToken->is(tok::greater)) {
195 const auto *
Next = CurrentToken->Next;
196 if (CurrentToken->isNot(TT_TemplateCloser)) {
203 if (
Next &&
Next->is(tok::greater) &&
204 Left->ParentBracket != tok::less &&
205 CurrentToken->getStartOfNonWhitespace() ==
206 Next->getStartOfNonWhitespace().getLocWithOffset(-1)) {
209 if (InExpr && SeenTernaryOperator &&
210 (!
Next ||
Next->isNoneOf(tok::l_paren, tok::l_brace))) {
215 if (ParentContext.InStaticAssertFirstArgument &&
Next &&
216 Next->isOneOf(tok::minus, tok::identifier)) {
220 Left->MatchingParen = CurrentToken;
221 CurrentToken->MatchingParen =
Left;
227 if (Style.isTextProto() ||
228 (Style.Language == FormatStyle::LK_Proto && BeforeLess &&
229 BeforeLess->isOneOf(TT_SelectorName, TT_DictLiteral))) {
230 CurrentToken->setType(TT_DictLiteral);
232 CurrentToken->setType(TT_TemplateCloser);
233 CurrentToken->Tok.setLength(1);
240 if (BeforeLess && BeforeLess->is(TT_TemplateName)) {
244 if (CurrentToken->is(tok::question) && Style.isJava()) {
248 if (CurrentToken->isOneOf(tok::r_paren, tok::r_square, tok::r_brace))
250 const auto &Prev = *CurrentToken->Previous;
257 if (MaybeAngles && InExpr && !Line.startsWith(tok::kw_template) &&
258 Prev.is(TT_BinaryOperator) &&
259 Prev.isOneOf(tok::pipepipe, tok::ampamp)) {
262 if (Prev.isOneOf(tok::question, tok::colon) && !Style.isProto())
263 SeenTernaryOperator =
true;
264 updateParameterCount(Left, CurrentToken);
265 if (Style.Language == FormatStyle::LK_Proto) {
267 if (CurrentToken->is(tok::colon) ||
268 (CurrentToken->isOneOf(tok::l_brace, tok::less) &&
273 }
else if (Style.isTableGen()) {
274 if (CurrentToken->isOneOf(tok::comma, tok::equal)) {
281 if (!parseTableGenValue())
291 bool parseUntouchableParens() {
292 while (CurrentToken) {
293 CurrentToken->Finalized =
true;
294 switch (CurrentToken->Tok.getKind()) {
297 if (!parseUntouchableParens())
312 bool parseParens(
bool IsIf =
false) {
315 assert(CurrentToken->Previous &&
"Unknown previous token");
316 FormatToken &OpeningParen = *CurrentToken->Previous;
317 assert(OpeningParen.is(tok::l_paren));
318 FormatToken *PrevNonComment = OpeningParen.getPreviousNonComment();
319 OpeningParen.ParentBracket = Contexts.back().ContextKind;
320 ScopedContextCreator ContextCreator(*
this, tok::l_paren, 1);
323 Contexts.back().ColonIsForRangeExpr =
324 Contexts.size() == 2 && Contexts[0].ColonIsForRangeExpr;
326 if (OpeningParen.Previous &&
327 OpeningParen.Previous->is(TT_UntouchableMacroFunc)) {
328 OpeningParen.Finalized =
true;
329 return parseUntouchableParens();
332 bool StartsObjCSelector =
false;
333 if (!Style.isVerilog()) {
334 if (
FormatToken *MaybeSel = OpeningParen.Previous) {
336 if (MaybeSel->is(tok::objc_selector) && MaybeSel->Previous &&
337 MaybeSel->Previous->is(tok::at)) {
338 StartsObjCSelector =
true;
343 if (OpeningParen.is(TT_OverloadedOperatorLParen)) {
346 while (Prev->isNot(tok::kw_operator)) {
347 Prev = Prev->Previous;
348 assert(Prev &&
"Expect a kw_operator prior to the OperatorLParen!");
354 bool OperatorCalledAsMemberFunction =
355 Prev->Previous && Prev->Previous->isOneOf(tok::period, tok::arrow);
356 Contexts.back().IsExpression = OperatorCalledAsMemberFunction;
357 }
else if (OpeningParen.is(TT_VerilogInstancePortLParen)) {
358 Contexts.back().IsExpression =
true;
359 Contexts.back().ContextType = Context::VerilogInstancePortList;
360 }
else if (Style.isJavaScript() &&
361 (Line.startsWith(Keywords.kw_type, tok::identifier) ||
362 Line.startsWith(tok::kw_export, Keywords.kw_type,
366 Contexts.back().IsExpression =
false;
367 }
else if (OpeningParen.Previous &&
368 (OpeningParen.Previous->isOneOf(
369 tok::kw_noexcept, tok::kw_explicit, tok::kw_while,
370 tok::l_paren, tok::comma, TT_CastRParen,
371 TT_BinaryOperator) ||
372 OpeningParen.Previous->isIf())) {
374 Contexts.back().IsExpression =
true;
375 }
else if (Style.isJavaScript() && OpeningParen.Previous &&
376 (OpeningParen.Previous->is(Keywords.kw_function) ||
377 (OpeningParen.Previous->endsSequence(tok::identifier,
378 Keywords.kw_function)))) {
380 Contexts.back().IsExpression =
false;
381 }
else if (Style.isJavaScript() && OpeningParen.Previous &&
382 OpeningParen.Previous->is(TT_JsTypeColon)) {
384 Contexts.back().IsExpression =
false;
385 }
else if (isLambdaParameterList(&OpeningParen)) {
387 OpeningParen.setType(TT_LambdaDefinitionLParen);
388 Contexts.back().IsExpression =
false;
389 }
else if (OpeningParen.is(TT_RequiresExpressionLParen)) {
390 Contexts.back().IsExpression =
false;
391 }
else if (OpeningParen.Previous &&
392 OpeningParen.Previous->is(tok::kw__Generic)) {
393 Contexts.back().ContextType = Context::C11GenericSelection;
394 Contexts.back().IsExpression =
true;
395 }
else if (OpeningParen.Previous &&
396 OpeningParen.Previous->TokenText ==
"Q_PROPERTY") {
397 Contexts.back().ContextType = Context::QtProperty;
398 Contexts.back().IsExpression =
false;
399 }
else if (Line.InPPDirective &&
400 (!OpeningParen.Previous ||
401 OpeningParen.Previous->isNot(tok::identifier))) {
402 Contexts.back().IsExpression =
true;
403 }
else if (Contexts[Contexts.size() - 2].CaretFound) {
405 Contexts.back().IsExpression =
false;
406 }
else if (OpeningParen.Previous &&
407 OpeningParen.Previous->is(TT_ForEachMacro)) {
409 Contexts.back().ContextType = Context::ForEachMacro;
410 Contexts.back().IsExpression =
false;
411 }
else if (OpeningParen.Previous && OpeningParen.Previous->MatchingParen &&
412 OpeningParen.Previous->MatchingParen->isOneOf(
413 TT_ObjCBlockLParen, TT_FunctionTypeLParen)) {
414 Contexts.back().IsExpression =
false;
415 }
else if (!Line.MustBeDeclaration &&
416 (!Line.InPPDirective || (Line.InMacroBody && !Scopes.empty()))) {
418 OpeningParen.Previous &&
419 OpeningParen.Previous->isOneOf(tok::kw_for, tok::kw_catch);
420 Contexts.back().IsExpression = !IsForOrCatch;
423 if (Style.isTableGen()) {
425 if (Prev->is(TT_TableGenCondOperator)) {
426 Contexts.back().IsTableGenCondOpe =
true;
427 Contexts.back().IsExpression =
true;
428 }
else if (Contexts.size() > 1 &&
429 Contexts[Contexts.size() - 2].IsTableGenBangOpe) {
434 Contexts.back().IsTableGenBangOpe =
true;
435 Contexts.back().IsExpression =
true;
438 if (!parseTableGenDAGArg())
440 return parseTableGenDAGArgAndList(&OpeningParen);
447 if (PrevNonComment && OpeningParen.is(TT_Unknown)) {
448 if (PrevNonComment->isAttribute()) {
449 OpeningParen.setType(TT_AttributeLParen);
450 }
else if (PrevNonComment->isOneOf(TT_TypenameMacro, tok::kw_decltype,
453#include
"clang/Basic/Traits.inc"
455 OpeningParen.setType(TT_TypeDeclarationParen);
457 if (PrevNonComment->isOneOf(tok::kw_decltype, tok::kw_typeof))
458 Contexts.back().IsExpression =
true;
462 if (StartsObjCSelector)
463 OpeningParen.setType(TT_ObjCSelector);
465 const bool IsStaticAssert =
466 PrevNonComment && PrevNonComment->is(tok::kw_static_assert);
468 Contexts.back().InStaticAssertFirstArgument =
true;
478 bool MightBeFunctionType = !Contexts[Contexts.size() - 2].IsExpression;
479 bool ProbablyFunctionType =
480 CurrentToken->isPointerOrReference() || CurrentToken->is(tok::caret);
481 bool HasMultipleLines =
false;
482 bool HasMultipleParametersOnALine =
false;
483 bool MightBeObjCForRangeLoop =
484 OpeningParen.Previous && OpeningParen.Previous->is(tok::kw_for);
486 while (CurrentToken) {
487 const auto &Prev = *CurrentToken->Previous;
488 const auto *PrevPrev = Prev.Previous;
489 if (Prev.is(TT_PointerOrReference) &&
490 PrevPrev->isOneOf(tok::l_paren, tok::coloncolon)) {
491 ProbablyFunctionType =
true;
493 if (CurrentToken->is(tok::comma))
494 MightBeFunctionType =
false;
495 if (Prev.is(TT_BinaryOperator))
496 Contexts.back().IsExpression =
true;
497 if (CurrentToken->is(tok::r_paren)) {
498 if (Prev.is(TT_PointerOrReference) &&
499 (PrevPrev == &OpeningParen || PrevPrev->is(tok::coloncolon))) {
500 MightBeFunctionType =
true;
502 if (OpeningParen.isNot(TT_CppCastLParen) && MightBeFunctionType &&
503 ProbablyFunctionType && CurrentToken->Next &&
504 (CurrentToken->Next->is(tok::l_paren) ||
505 (CurrentToken->Next->is(tok::l_square) &&
506 (Line.MustBeDeclaration ||
507 (PrevNonComment && PrevNonComment->isTypeName(LangOpts)))))) {
508 OpeningParen.setType(OpeningParen.Next->is(tok::caret)
510 : TT_FunctionTypeLParen);
512 OpeningParen.MatchingParen = CurrentToken;
513 CurrentToken->MatchingParen = &OpeningParen;
515 if (CurrentToken->Next && CurrentToken->Next->is(tok::l_brace) &&
516 OpeningParen.Previous && OpeningParen.Previous->is(tok::l_paren)) {
522 if (
Tok->is(TT_BinaryOperator) &&
Tok->isPointerOrReference())
523 Tok->setType(TT_PointerOrReference);
527 if (StartsObjCSelector) {
528 CurrentToken->setType(TT_ObjCSelector);
529 if (Contexts.back().FirstObjCSelectorName) {
530 Contexts.back().FirstObjCSelectorName->LongestObjCSelectorName =
531 Contexts.back().LongestObjCSelectorName;
535 if (OpeningParen.is(TT_AttributeLParen))
536 CurrentToken->setType(TT_AttributeRParen);
537 if (OpeningParen.is(TT_TypeDeclarationParen))
538 CurrentToken->setType(TT_TypeDeclarationParen);
539 if (OpeningParen.Previous &&
540 OpeningParen.Previous->is(TT_JavaAnnotation)) {
541 CurrentToken->setType(TT_JavaAnnotation);
543 if (OpeningParen.Previous &&
544 OpeningParen.Previous->is(TT_LeadingJavaAnnotation)) {
545 CurrentToken->setType(TT_LeadingJavaAnnotation);
548 if (!HasMultipleLines)
550 else if (HasMultipleParametersOnALine)
558 if (CurrentToken->isOneOf(tok::r_square, tok::r_brace))
561 if (CurrentToken->is(tok::l_brace) && OpeningParen.is(TT_ObjCBlockLParen))
562 OpeningParen.setType(TT_Unknown);
563 if (CurrentToken->is(tok::comma) && CurrentToken->Next &&
564 !CurrentToken->Next->HasUnescapedNewline &&
565 !CurrentToken->Next->isTrailingComment()) {
566 HasMultipleParametersOnALine =
true;
568 bool ProbablyFunctionTypeLParen =
569 (CurrentToken->is(tok::l_paren) && CurrentToken->Next &&
570 CurrentToken->Next->isOneOf(tok::star, tok::amp, tok::caret));
571 if ((Prev.isOneOf(tok::kw_const, tok::kw_auto) ||
572 Prev.isTypeName(LangOpts)) &&
573 !(CurrentToken->is(tok::l_brace) ||
574 (CurrentToken->is(tok::l_paren) && !ProbablyFunctionTypeLParen))) {
575 Contexts.back().IsExpression =
false;
577 if (CurrentToken->isOneOf(tok::semi, tok::colon)) {
578 MightBeObjCForRangeLoop =
false;
579 if (PossibleObjCForInToken) {
580 PossibleObjCForInToken->setType(TT_Unknown);
581 PossibleObjCForInToken =
nullptr;
584 if (IsIf && CurrentToken->is(tok::semi)) {
585 for (
auto *
Tok = OpeningParen.Next;
586 Tok != CurrentToken &&
587 Tok->isNoneOf(tok::equal, tok::l_paren, tok::l_brace);
589 if (
Tok->isPointerOrReference())
590 Tok->setFinalizedType(TT_PointerOrReference);
593 if (MightBeObjCForRangeLoop && CurrentToken->is(Keywords.kw_in)) {
594 PossibleObjCForInToken = CurrentToken;
595 PossibleObjCForInToken->setType(TT_ObjCForIn);
599 if (CurrentToken->is(tok::comma)) {
601 Contexts.back().InStaticAssertFirstArgument =
false;
603 Contexts.back().CanBeExpression =
true;
606 if (Style.isTableGen()) {
607 if (CurrentToken->is(tok::comma)) {
608 if (Contexts.back().IsTableGenCondOpe)
609 CurrentToken->setType(TT_TableGenCondOperatorComma);
611 }
else if (CurrentToken->is(tok::colon)) {
612 if (Contexts.back().IsTableGenCondOpe)
613 CurrentToken->setType(TT_TableGenCondOperatorColon);
617 if (!parseTableGenValue())
625 updateParameterCount(&OpeningParen,
Tok);
626 if (CurrentToken && CurrentToken->HasUnescapedNewline)
627 HasMultipleLines =
true;
633 if (!Style.isCSharp())
637 if (
Tok.Previous &&
Tok.Previous->is(tok::identifier))
641 if (
Tok.Previous &&
Tok.Previous->is(tok::r_square)) {
652 if (AttrTok->is(tok::r_square))
656 while (AttrTok && AttrTok->isNot(tok::r_square))
657 AttrTok = AttrTok->Next;
663 AttrTok = AttrTok->Next;
668 if (AttrTok->isAccessSpecifierKeyword() ||
669 AttrTok->isOneOf(tok::comment, tok::kw_class, tok::kw_static,
670 tok::l_square, Keywords.kw_internal)) {
676 AttrTok->Next->startsSequence(tok::identifier, tok::l_paren)) {
692 Left->ParentBracket = Contexts.back().ContextKind;
698 bool CppArrayTemplates =
699 IsCpp && Parent && Parent->is(TT_TemplateCloser) &&
700 (Contexts.back().CanBeExpression || Contexts.back().IsExpression ||
701 Contexts.back().ContextType == Context::TemplateArgument);
703 const bool IsInnerSquare = Contexts.back().InCpp11AttributeSpecifier;
704 const bool IsCpp11AttributeSpecifier =
705 isCppAttribute(IsCpp, *Left) || IsInnerSquare;
708 bool IsCSharpAttributeSpecifier =
709 isCSharpAttributeSpecifier(*Left) ||
710 Contexts.back().InCSharpAttributeSpecifier;
712 bool InsideInlineASM = Line.startsWith(tok::kw_asm);
713 bool IsCppStructuredBinding =
Left->isCppStructuredBinding(IsCpp);
714 bool StartsObjCMethodExpr =
715 !IsCppStructuredBinding && !InsideInlineASM && !CppArrayTemplates &&
716 IsCpp && !IsCpp11AttributeSpecifier && !IsCSharpAttributeSpecifier &&
717 Contexts.back().CanBeExpression &&
Left->isNot(TT_LambdaLSquare) &&
718 CurrentToken->isNoneOf(tok::l_brace, tok::r_square) &&
722 (!Parent || !Parent->is(tok::comma) ||
723 Contexts.back().ContextKind != tok::l_brace) &&
725 Parent->isOneOf(tok::colon, tok::l_square, tok::l_paren,
726 tok::kw_return, tok::kw_throw) ||
727 Parent->isUnaryOperator() ||
729 Parent->isOneOf(TT_ObjCForIn, TT_CastRParen) ||
732 bool ColonFound =
false;
734 unsigned BindingIncrease = 1;
735 if (IsCppStructuredBinding) {
736 Left->setType(TT_StructuredBindingLSquare);
737 }
else if (
Left->is(TT_Unknown)) {
738 if (StartsObjCMethodExpr) {
739 Left->setType(TT_ObjCMethodExpr);
740 }
else if (InsideInlineASM) {
741 Left->setType(TT_InlineASMSymbolicNameLSquare);
742 }
else if (IsCpp11AttributeSpecifier) {
743 if (!IsInnerSquare) {
744 Left->setType(TT_AttributeLSquare);
746 Left->Previous->EndsCppAttributeGroup =
false;
748 }
else if (Style.isJavaScript() && Parent &&
749 Contexts.back().ContextKind == tok::l_brace &&
750 Parent->isOneOf(tok::l_brace, tok::comma)) {
751 Left->setType(TT_JsComputedPropertyName);
752 }
else if (IsCpp && Contexts.back().ContextKind == tok::l_brace &&
753 Parent && Parent->isOneOf(tok::l_brace, tok::comma)) {
754 Left->setType(TT_DesignatedInitializerLSquare);
755 }
else if (IsCSharpAttributeSpecifier) {
756 Left->setType(TT_AttributeLSquare);
757 }
else if (CurrentToken->is(tok::r_square) && Parent &&
758 Parent->is(TT_TemplateCloser)) {
759 Left->setType(TT_ArraySubscriptLSquare);
760 }
else if (Style.isProto()) {
787 Left->setType(TT_ArrayInitializerLSquare);
788 if (!
Left->endsSequence(tok::l_square, tok::numeric_constant,
790 !
Left->endsSequence(tok::l_square, tok::numeric_constant,
792 !
Left->endsSequence(tok::l_square, tok::colon, TT_SelectorName)) {
793 Left->setType(TT_ProtoExtensionLSquare);
794 BindingIncrease = 10;
796 }
else if (!CppArrayTemplates && Parent &&
797 Parent->isOneOf(TT_BinaryOperator, TT_TemplateCloser, tok::at,
798 tok::comma, tok::l_paren, tok::l_square,
799 tok::question, tok::colon, tok::kw_return,
802 Left->setType(TT_ArrayInitializerLSquare);
804 BindingIncrease = 10;
805 Left->setType(TT_ArraySubscriptLSquare);
809 ScopedContextCreator ContextCreator(*
this, tok::l_square, BindingIncrease);
810 Contexts.back().IsExpression =
true;
811 if (Style.isJavaScript() && Parent && Parent->is(TT_JsTypeColon))
812 Contexts.back().IsExpression =
false;
814 Contexts.back().ColonIsObjCMethodExpr = StartsObjCMethodExpr;
815 Contexts.back().InCpp11AttributeSpecifier = IsCpp11AttributeSpecifier;
816 Contexts.back().InCSharpAttributeSpecifier = IsCSharpAttributeSpecifier;
818 while (CurrentToken) {
819 if (CurrentToken->is(tok::r_square)) {
820 if (IsCpp11AttributeSpecifier && !IsInnerSquare) {
821 CurrentToken->setType(TT_AttributeRSquare);
822 CurrentToken->EndsCppAttributeGroup =
true;
824 if (IsCSharpAttributeSpecifier) {
825 CurrentToken->setType(TT_AttributeRSquare);
826 }
else if (((CurrentToken->Next &&
827 CurrentToken->Next->is(tok::l_paren)) ||
828 (CurrentToken->Previous &&
829 CurrentToken->Previous->Previous == Left)) &&
830 Left->is(TT_ObjCMethodExpr)) {
835 StartsObjCMethodExpr =
false;
836 Left->setType(TT_Unknown);
838 if (StartsObjCMethodExpr && CurrentToken->Previous != Left) {
839 CurrentToken->setType(TT_ObjCMethodExpr);
842 if (!ColonFound && CurrentToken->Previous &&
843 CurrentToken->Previous->is(TT_Unknown) &&
844 canBeObjCSelectorComponent(*CurrentToken->Previous)) {
845 CurrentToken->Previous->setType(TT_SelectorName);
850 if (Parent && Parent->is(TT_PointerOrReference))
851 Parent->overwriteFixedType(TT_BinaryOperator);
853 Left->MatchingParen = CurrentToken;
854 CurrentToken->MatchingParen =
Left;
859 if (!Contexts.back().FirstObjCSelectorName) {
862 Previous->ObjCSelectorNameParts = 1;
863 Contexts.back().FirstObjCSelectorName =
Previous;
866 Left->ParameterCount =
867 Contexts.back().FirstObjCSelectorName->ObjCSelectorNameParts;
869 if (Contexts.back().FirstObjCSelectorName) {
870 Contexts.back().FirstObjCSelectorName->LongestObjCSelectorName =
871 Contexts.back().LongestObjCSelectorName;
872 if (
Left->BlockParameterCount > 1)
873 Contexts.back().FirstObjCSelectorName->LongestObjCSelectorName = 0;
875 if (Style.isTableGen() &&
Left->is(TT_TableGenListOpener))
876 CurrentToken->setType(TT_TableGenListCloser);
880 if (CurrentToken->isOneOf(tok::r_paren, tok::r_brace))
882 if (CurrentToken->is(tok::colon)) {
883 if (IsCpp11AttributeSpecifier &&
884 CurrentToken->endsSequence(tok::colon, tok::identifier,
888 CurrentToken->setType(TT_AttributeColon);
889 }
else if (!Style.isVerilog() && !Line.InPragmaDirective &&
890 Left->isOneOf(TT_ArraySubscriptLSquare,
891 TT_DesignatedInitializerLSquare)) {
892 Left->setType(TT_ObjCMethodExpr);
893 StartsObjCMethodExpr =
true;
894 Contexts.back().ColonIsObjCMethodExpr =
true;
895 if (Parent && Parent->is(tok::r_paren)) {
897 Parent->setType(TT_CastRParen);
902 if (CurrentToken->is(tok::comma) &&
Left->is(TT_ObjCMethodExpr) &&
904 Left->setType(TT_ArrayInitializerLSquare);
907 if (Style.isTableGen()) {
908 if (CurrentToken->isOneOf(tok::comma, tok::minus, tok::ellipsis)) {
914 if (!parseTableGenValue())
917 updateParameterCount(Left,
Tok);
922 updateParameterCount(Left,
Tok);
927 void skipToNextNonComment() {
929 while (CurrentToken && CurrentToken->is(tok::comment))
938 bool parseTableGenValue(
bool ParseNameMode =
false) {
941 while (CurrentToken->is(tok::comment))
943 if (!parseTableGenSimpleValue())
948 if (CurrentToken->is(tok::hash)) {
949 if (CurrentToken->Next &&
950 CurrentToken->Next->isOneOf(tok::colon, tok::semi, tok::l_brace)) {
953 CurrentToken->setType(TT_TableGenTrailingPasteOperator);
958 skipToNextNonComment();
959 HashTok->setType(TT_Unknown);
960 if (!parseTableGenValue(ParseNameMode))
967 if (ParseNameMode && CurrentToken->is(tok::l_brace))
970 if (CurrentToken->isOneOf(tok::l_brace, tok::l_square, tok::period)) {
971 CurrentToken->setType(TT_TableGenValueSuffix);
973 skipToNextNonComment();
974 if (Suffix->is(tok::l_square))
975 return parseSquare();
976 if (Suffix->is(tok::l_brace)) {
977 Scopes.push_back(getScopeType(*Suffix));
987 bool tryToParseTableGenTokVar() {
990 if (CurrentToken->is(tok::identifier) &&
991 CurrentToken->TokenText.front() ==
'$') {
992 skipToNextNonComment();
1000 bool parseTableGenDAGArg(
bool AlignColon =
false) {
1001 if (tryToParseTableGenTokVar())
1003 if (parseTableGenValue()) {
1004 if (CurrentToken && CurrentToken->is(tok::colon)) {
1006 CurrentToken->setType(TT_TableGenDAGArgListColonToAlign);
1008 CurrentToken->setType(TT_TableGenDAGArgListColon);
1009 skipToNextNonComment();
1010 return tryToParseTableGenTokVar();
1021 auto &Opes = Style.TableGenBreakingDAGArgOperators;
1026 if (
Tok.isNot(tok::identifier) ||
1027 Tok.isOneOf(TT_TableGenBangOperator, TT_TableGenCondOperator)) {
1031 if (!
Tok.Next ||
Tok.Next->is(tok::colon))
1033 return llvm::is_contained(Opes,
Tok.TokenText.str());
1038 bool parseTableGenDAGArgAndList(
FormatToken *Opener) {
1040 if (!parseTableGenDAGArg())
1042 bool BreakInside =
false;
1043 if (Style.TableGenBreakInsideDAGArg != FormatStyle::DAS_DontBreak) {
1046 if (isTableGenDAGArgBreakingOperator(*FirstTok)) {
1049 Opener->setType(TT_TableGenDAGArgOpenerToBreak);
1050 if (FirstTok->isOneOf(TT_TableGenBangOperator,
1051 TT_TableGenCondOperator)) {
1054 CurrentToken->Previous->setType(TT_TableGenDAGArgOperatorToBreak);
1055 }
else if (FirstTok->is(tok::identifier)) {
1056 if (Style.TableGenBreakInsideDAGArg == FormatStyle::DAS_BreakAll)
1057 FirstTok->setType(TT_TableGenDAGArgOperatorToBreak);
1059 FirstTok->setType(TT_TableGenDAGArgOperatorID);
1064 return parseTableGenDAGArgList(Opener, BreakInside);
1069 bool parseTableGenDAGArgList(
FormatToken *Opener,
bool BreakInside) {
1070 ScopedContextCreator ContextCreator(*
this, tok::l_paren, 0);
1071 Contexts.back().IsTableGenDAGArgList =
true;
1072 bool FirstDAGArgListElm =
true;
1073 while (CurrentToken) {
1074 if (!FirstDAGArgListElm && CurrentToken->is(tok::comma)) {
1075 CurrentToken->setType(BreakInside ? TT_TableGenDAGArgListCommaToBreak
1076 : TT_TableGenDAGArgListComma);
1077 skipToNextNonComment();
1079 if (CurrentToken && CurrentToken->is(tok::r_paren)) {
1080 CurrentToken->setType(TT_TableGenDAGArgCloser);
1081 Opener->MatchingParen = CurrentToken;
1082 CurrentToken->MatchingParen = Opener;
1083 skipToNextNonComment();
1086 if (!parseTableGenDAGArg(
1088 Style.AlignConsecutiveTableGenBreakingDAGArgColons.Enabled)) {
1091 FirstDAGArgListElm =
false;
1096 bool parseTableGenSimpleValue() {
1097 assert(Style.isTableGen());
1101 skipToNextNonComment();
1103 if (
Tok->isOneOf(tok::numeric_constant, tok::string_literal,
1104 TT_TableGenMultiLineString, tok::kw_true, tok::kw_false,
1105 tok::question, tok::kw_int)) {
1109 if (
Tok->is(tok::l_brace)) {
1110 Scopes.push_back(getScopeType(*
Tok));
1111 return parseBrace();
1114 if (
Tok->is(tok::l_square)) {
1115 Tok->setType(TT_TableGenListOpener);
1118 if (
Tok->is(tok::less)) {
1119 CurrentToken->setType(TT_TemplateOpener);
1120 return parseAngle();
1126 if (
Tok->is(tok::l_paren)) {
1127 Tok->setType(TT_TableGenDAGArgOpener);
1129 if (Contexts.back().IsTableGenDAGArgList)
1130 Tok->SpacesRequiredBefore = 1;
1131 return parseTableGenDAGArgAndList(
Tok);
1134 if (
Tok->is(TT_TableGenBangOperator)) {
1135 if (CurrentToken && CurrentToken->is(tok::less)) {
1136 CurrentToken->setType(TT_TemplateOpener);
1137 skipToNextNonComment();
1141 if (!CurrentToken || CurrentToken->isNot(tok::l_paren))
1145 Contexts.back().IsTableGenBangOpe =
true;
1146 bool Result = parseParens();
1147 Contexts.back().IsTableGenBangOpe =
false;
1151 if (
Tok->is(TT_TableGenCondOperator)) {
1152 if (!CurrentToken || CurrentToken->isNot(tok::l_paren))
1155 return parseParens();
1160 if (
Tok->is(tok::identifier)) {
1162 if (CurrentToken && CurrentToken->is(tok::less)) {
1163 CurrentToken->setType(TT_TemplateOpener);
1164 skipToNextNonComment();
1165 return parseAngle();
1173 bool couldBeInStructArrayInitializer()
const {
1174 if (Contexts.size() < 2)
1178 const auto End = std::next(Contexts.rbegin(), 2);
1179 auto Last = Contexts.rbegin();
1182 if (
Last->ContextKind == tok::l_brace)
1184 return Depth == 2 &&
Last->ContextKind != tok::l_brace;
1191 assert(CurrentToken->Previous);
1192 FormatToken &OpeningBrace = *CurrentToken->Previous;
1193 assert(OpeningBrace.is(tok::l_brace));
1194 OpeningBrace.ParentBracket = Contexts.back().ContextKind;
1196 if (Contexts.back().CaretFound)
1197 OpeningBrace.overwriteFixedType(TT_ObjCBlockLBrace);
1198 Contexts.back().CaretFound =
false;
1200 ScopedContextCreator ContextCreator(*
this, tok::l_brace, 1);
1201 Contexts.back().ColonIsDictLiteral =
true;
1203 Contexts.back().IsExpression =
true;
1204 if (Style.isJavaScript() && OpeningBrace.Previous &&
1205 OpeningBrace.Previous->is(TT_JsTypeColon)) {
1206 Contexts.back().IsExpression =
false;
1208 if (Style.isVerilog() &&
1209 (!OpeningBrace.getPreviousNonComment() ||
1210 OpeningBrace.getPreviousNonComment()->isNot(Keywords.kw_apostrophe))) {
1211 Contexts.back().VerilogMayBeConcatenation =
true;
1213 if (Style.isTableGen())
1214 Contexts.back().ColonIsDictLiteral =
false;
1216 unsigned CommaCount = 0;
1217 while (CurrentToken) {
1218 assert(!Scopes.empty());
1219 if (CurrentToken->is(tok::r_brace)) {
1220 assert(Scopes.back() == getScopeType(OpeningBrace));
1222 assert(OpeningBrace.Optional == CurrentToken->Optional);
1223 OpeningBrace.MatchingParen = CurrentToken;
1224 CurrentToken->MatchingParen = &OpeningBrace;
1225 if (Style.AlignArrayOfStructures != FormatStyle::AIAS_None) {
1226 if (OpeningBrace.ParentBracket == tok::l_brace &&
1227 couldBeInStructArrayInitializer() && CommaCount > 0) {
1228 Contexts.back().ContextType = Context::StructArrayInitializer;
1234 if (CurrentToken->isOneOf(tok::r_paren, tok::r_square))
1236 updateParameterCount(&OpeningBrace, CurrentToken);
1237 if (CurrentToken->isOneOf(tok::colon, tok::l_brace, tok::less)) {
1239 if (
Previous->is(TT_JsTypeOptionalQuestion))
1241 if ((CurrentToken->is(tok::colon) && !Style.isTableGen() &&
1242 (!Contexts.back().ColonIsDictLiteral || !IsCpp)) ||
1244 OpeningBrace.setType(TT_DictLiteral);
1245 Scopes.back() = getScopeType(OpeningBrace);
1246 if (
Previous->Tok.getIdentifierInfo() ||
1247 Previous->is(tok::string_literal)) {
1248 Previous->setType(TT_SelectorName);
1251 if (CurrentToken->is(tok::colon) && OpeningBrace.is(TT_Unknown) &&
1252 !Style.isTableGen()) {
1253 OpeningBrace.setType(TT_DictLiteral);
1254 Scopes.back() = getScopeType(OpeningBrace);
1255 }
else if (Style.isJavaScript()) {
1256 OpeningBrace.overwriteFixedType(TT_DictLiteral);
1257 Scopes.back() = getScopeType(OpeningBrace);
1260 bool IsBracedListComma =
false;
1261 if (CurrentToken->is(tok::comma)) {
1262 if (Style.isJavaScript()) {
1263 OpeningBrace.overwriteFixedType(TT_DictLiteral);
1264 Scopes.back() = getScopeType(OpeningBrace);
1270 if (!consumeToken())
1272 if (IsBracedListComma)
1273 Contexts.back().IsExpression =
true;
1282 if (Current->is(tok::l_brace) && Current->is(
BK_Block))
1283 ++
Left->BlockParameterCount;
1284 if (Current->is(tok::comma)) {
1285 ++
Left->ParameterCount;
1287 Left->Role.reset(
new CommaSeparatedList(Style));
1288 Left->Role->CommaFound(Current);
1289 }
else if (
Left->ParameterCount == 0 && Current->isNot(tok::comment)) {
1290 Left->ParameterCount = 1;
1294 bool parseConditional() {
1295 while (CurrentToken) {
1296 if (CurrentToken->is(tok::colon) && CurrentToken->is(TT_Unknown)) {
1297 CurrentToken->setType(TT_ConditionalExpr);
1304 if (CurrentToken->is(tok::r_brace))
1306 if (!consumeToken())
1312 bool parseTemplateDeclaration() {
1313 if (!CurrentToken || CurrentToken->isNot(tok::less))
1316 CurrentToken->setType(TT_TemplateOpener);
1319 TemplateDeclarationDepth++;
1320 const bool WellFormed = parseAngle();
1321 TemplateDeclarationDepth--;
1325 if (CurrentToken && TemplateDeclarationDepth == 0)
1326 CurrentToken->Previous->ClosesTemplateDeclaration =
true;
1331 bool consumeToken() {
1333 const auto *Prev = CurrentToken->getPreviousNonComment();
1334 if (Prev && Prev->is(TT_AttributeRSquare) &&
1335 CurrentToken->isOneOf(tok::kw_if, tok::kw_switch, tok::kw_case,
1336 tok::kw_default, tok::kw_for, tok::kw_while) &&
1338 CurrentToken->MustBreakBefore =
true;
1345 if (
Tok->is(TT_VerilogTableItem))
1348 if (
Tok->is(TT_TableGenMultiLineString))
1350 auto *Prev =
Tok->getPreviousNonComment();
1351 auto *
Next =
Tok->getNextNonComment();
1352 switch (
bool IsIf =
false;
Tok->Tok.getKind()) {
1355 if (!Prev && Line.MustBeDeclaration)
1356 Tok->setType(TT_ObjCMethodSpecifier);
1363 if (
Tok->isTypeFinalized())
1366 if (Style.isJavaScript()) {
1367 if (Contexts.back().ColonIsForRangeExpr ||
1368 (Contexts.size() == 1 &&
1369 Line.First->isNoneOf(tok::kw_enum, tok::kw_case)) ||
1370 Contexts.back().ContextKind == tok::l_paren ||
1371 Contexts.back().ContextKind == tok::l_square ||
1372 (!Contexts.back().IsExpression &&
1373 Contexts.back().ContextKind == tok::l_brace) ||
1374 (Contexts.size() == 1 &&
1375 Line.MustBeDeclaration)) {
1376 Contexts.back().IsExpression =
false;
1377 Tok->setType(TT_JsTypeColon);
1380 }
else if (Style.isCSharp()) {
1381 if (Contexts.back().InCSharpAttributeSpecifier) {
1382 Tok->setType(TT_AttributeColon);
1385 if (Contexts.back().ContextKind == tok::l_paren) {
1386 Tok->setType(TT_CSharpNamedArgumentColon);
1389 }
else if (Style.isVerilog() &&
Tok->isNot(TT_BinaryOperator)) {
1392 if (Keywords.isVerilogEnd(*Prev) || Keywords.isVerilogBegin(*Prev)) {
1393 Tok->setType(TT_VerilogBlockLabelColon);
1394 }
else if (Contexts.back().ContextKind == tok::l_square) {
1395 Tok->setType(TT_BitFieldColon);
1396 }
else if (Contexts.back().ColonIsDictLiteral) {
1397 Tok->setType(TT_DictLiteral);
1398 }
else if (Contexts.size() == 1) {
1402 Tok->setType(TT_CaseLabelColon);
1403 if (Line.Level > 1 || (!Line.InPPDirective && Line.Level > 0))
1408 if (Line.First->is(tok::kw_asm)) {
1409 Tok->setType(TT_InlineASMColon);
1410 }
else if (Contexts.back().ColonIsDictLiteral || Style.isProto()) {
1411 Tok->setType(TT_DictLiteral);
1412 if (Style.isTextProto())
1413 Prev->setType(TT_SelectorName);
1414 }
else if (Contexts.back().ColonIsObjCMethodExpr ||
1415 Line.startsWith(TT_ObjCMethodSpecifier)) {
1416 Tok->setType(TT_ObjCMethodExpr);
1417 const auto *PrevPrev = Prev->Previous;
1420 bool UnknownIdentifierInMethodDeclaration =
1421 Line.startsWith(TT_ObjCMethodSpecifier) &&
1422 Prev->is(tok::identifier) && Prev->is(TT_Unknown);
1425 !(PrevPrev->is(TT_CastRParen) ||
1426 (PrevPrev->is(TT_ObjCMethodExpr) && PrevPrev->is(tok::colon))) ||
1427 PrevPrev->is(tok::r_square) ||
1428 Contexts.back().LongestObjCSelectorName == 0 ||
1429 UnknownIdentifierInMethodDeclaration) {
1430 Prev->setType(TT_SelectorName);
1431 if (!Contexts.back().FirstObjCSelectorName)
1432 Contexts.back().FirstObjCSelectorName = Prev;
1433 else if (Prev->ColumnWidth > Contexts.back().LongestObjCSelectorName)
1434 Contexts.back().LongestObjCSelectorName = Prev->ColumnWidth;
1435 Prev->ParameterIndex =
1436 Contexts.back().FirstObjCSelectorName->ObjCSelectorNameParts;
1437 ++Contexts.back().FirstObjCSelectorName->ObjCSelectorNameParts;
1439 }
else if (Contexts.back().ColonIsForRangeExpr) {
1440 Tok->setType(TT_RangeBasedForLoopColon);
1441 for (
auto *Token = Prev;
1442 Token && Token->isNoneOf(tok::semi, tok::l_paren);
1443 Token = Token->Previous) {
1444 if (Token->isPointerOrReference())
1445 Token->setFinalizedType(TT_PointerOrReference);
1447 }
else if (Contexts.back().ContextType == Context::C11GenericSelection) {
1448 Tok->setType(TT_GenericSelectionColon);
1449 if (Prev->isPointerOrReference())
1450 Prev->setFinalizedType(TT_PointerOrReference);
1451 }
else if ((CurrentToken && CurrentToken->is(tok::numeric_constant)) ||
1452 (Prev->is(TT_StartOfName) && !Scopes.empty() &&
1454 Tok->setType(TT_BitFieldColon);
1455 }
else if (Contexts.size() == 1 &&
1456 Line.getFirstNonComment()->isNoneOf(tok::kw_enum, tok::kw_case,
1458 !Line.startsWith(tok::kw_typedef, tok::kw_enum)) {
1459 if (Prev->isOneOf(tok::r_paren, tok::kw_noexcept) ||
1460 Prev->ClosesRequiresClause) {
1461 Tok->setType(TT_CtorInitializerColon);
1462 }
else if (Prev->is(tok::kw_try)) {
1464 FormatToken *PrevPrev = Prev->getPreviousNonComment();
1467 if (PrevPrev && PrevPrev->isOneOf(tok::r_paren, tok::kw_noexcept))
1468 Tok->setType(TT_CtorInitializerColon);
1470 Tok->setType(TT_InheritanceColon);
1471 if (Prev->isAccessSpecifierKeyword())
1474 }
else if (canBeObjCSelectorComponent(*Prev) &&
Next &&
1475 (
Next->isOneOf(tok::r_paren, tok::comma) ||
1476 (canBeObjCSelectorComponent(*
Next) &&
Next->Next &&
1477 Next->Next->is(tok::colon)))) {
1480 Tok->setType(TT_ObjCSelector);
1487 if (Style.isJavaScript() && !Contexts.back().IsExpression)
1488 Tok->setType(TT_JsTypeOperator);
1491 if (Style.isTableGen()) {
1493 if (!parseTableGenValue())
1495 if (CurrentToken && CurrentToken->is(Keywords.kw_then))
1500 CurrentToken->isOneOf(tok::kw_constexpr, tok::identifier)) {
1506 if (CurrentToken && CurrentToken->is(tok::l_paren)) {
1508 if (!parseParens(IsIf))
1513 if (Style.isJavaScript()) {
1515 if ((Prev && Prev->is(tok::period)) || (
Next &&
Next->is(tok::colon)))
1518 if (CurrentToken && CurrentToken->is(Keywords.kw_await))
1521 if (IsCpp && CurrentToken && CurrentToken->is(tok::kw_co_await))
1523 Contexts.back().ColonIsForRangeExpr =
true;
1524 if (!CurrentToken || CurrentToken->isNot(tok::l_paren))
1535 if (Prev && Prev->is(tok::r_paren) && Prev->MatchingParen &&
1536 Prev->MatchingParen->is(TT_OverloadedOperatorLParen)) {
1537 Prev->setType(TT_OverloadedOperator);
1538 Prev->MatchingParen->setType(TT_OverloadedOperator);
1539 Tok->setType(TT_OverloadedOperatorLParen);
1542 if (Style.isVerilog()) {
1548 auto IsInstancePort = [&]() {
1557 if (!Prev || !(PrevPrev = Prev->getPreviousNonComment()))
1560 if (Keywords.isVerilogIdentifier(*Prev) &&
1561 Keywords.isVerilogIdentifier(*PrevPrev)) {
1565 if (Prev->is(Keywords.kw_verilogHash) &&
1566 Keywords.isVerilogIdentifier(*PrevPrev)) {
1570 if (Keywords.isVerilogIdentifier(*Prev) && PrevPrev->is(tok::r_paren))
1573 if (Keywords.isVerilogIdentifier(*Prev) && PrevPrev->is(tok::comma)) {
1574 const FormatToken *PrevParen = PrevPrev->getPreviousNonComment();
1575 if (PrevParen && PrevParen->is(tok::r_paren) &&
1576 PrevParen->MatchingParen &&
1577 PrevParen->MatchingParen->is(TT_VerilogInstancePortLParen)) {
1584 if (IsInstancePort())
1585 Tok->setType(TT_VerilogInstancePortLParen);
1590 if (Line.MustBeDeclaration && Contexts.size() == 1 &&
1591 !Contexts.back().IsExpression && !Line.startsWith(TT_ObjCProperty) &&
1592 !Line.startsWith(tok::l_paren) &&
1593 Tok->isNoneOf(TT_TypeDeclarationParen, TT_RequiresExpressionLParen)) {
1595 (!Prev->isAttribute() &&
1596 Prev->isNoneOf(TT_RequiresClause, TT_LeadingJavaAnnotation,
1597 TT_BinaryOperator))) {
1598 Line.MightBeFunctionDecl =
true;
1599 Tok->MightBeFunctionDeclParen =
true;
1604 if (Style.isTableGen())
1605 Tok->setType(TT_TableGenListOpener);
1611 if (
Tok->is(TT_RequiresExpressionLBrace))
1613 }
else if (Style.isTextProto()) {
1614 if (Prev && Prev->isNot(TT_DictLiteral))
1615 Prev->setType(TT_SelectorName);
1617 Scopes.push_back(getScopeType(*
Tok));
1623 Tok->setType(TT_TemplateOpener);
1629 if (Style.isTextProto() ||
1630 (Style.Language == FormatStyle::LK_Proto && Prev &&
1631 Prev->isOneOf(TT_SelectorName, TT_DictLiteral))) {
1632 Tok->setType(TT_DictLiteral);
1633 if (Prev && Prev->isNot(TT_DictLiteral))
1634 Prev->setType(TT_SelectorName);
1636 if (Style.isTableGen())
1637 Tok->setType(TT_TemplateOpener);
1639 Tok->setType(TT_BinaryOperator);
1640 NonTemplateLess.insert(
Tok);
1650 if (!Scopes.empty())
1657 if (!Style.isTextProto() &&
Tok->is(TT_Unknown))
1658 Tok->setType(TT_BinaryOperator);
1659 if (Prev && Prev->is(TT_TemplateCloser))
1660 Tok->SpacesRequiredBefore = 1;
1662 case tok::kw_operator:
1663 if (Style.isProto())
1666 if (IsCpp && CurrentToken) {
1667 const auto *Info = CurrentToken->Tok.getIdentifierInfo();
1669 if (Info && !(CurrentToken->isPlacementOperator() ||
1670 CurrentToken->is(tok::kw_co_await) ||
1671 Info->isCPlusPlusOperatorKeyword())) {
1673 if (CurrentToken->startsSequence(tok::kw_decltype, tok::l_paren,
1674 tok::kw_auto, tok::r_paren)) {
1676 LParen = CurrentToken->Next->Next->Next->Next;
1679 for (LParen = CurrentToken->Next;
1680 LParen && LParen->isNot(tok::l_paren); LParen = LParen->Next) {
1681 if (LParen->isPointerOrReference())
1682 LParen->setFinalizedType(TT_PointerOrReference);
1685 if (LParen && LParen->is(tok::l_paren)) {
1686 if (!Contexts.back().IsExpression) {
1687 Tok->setFinalizedType(TT_FunctionDeclarationName);
1688 LParen->setFinalizedType(TT_FunctionDeclarationLParen);
1694 while (CurrentToken &&
1695 CurrentToken->isNoneOf(tok::l_paren, tok::semi, tok::r_paren,
1697 if (CurrentToken->isOneOf(tok::star, tok::amp))
1698 CurrentToken->setType(TT_PointerOrReference);
1699 auto Next = CurrentToken->getNextNonComment();
1702 if (
Next->is(tok::less))
1708 auto Previous = CurrentToken->getPreviousNonComment();
1710 if (CurrentToken->is(tok::comma) &&
Previous->isNot(tok::kw_operator))
1712 if (
Previous->isOneOf(TT_BinaryOperator, TT_UnaryOperator, tok::comma,
1715 Previous->isPointerOrReference()) ||
1717 Previous->TokenText.starts_with(
"\"\"")) {
1718 Previous->setType(TT_OverloadedOperator);
1719 if (CurrentToken->isOneOf(tok::less, tok::greater))
1723 if (CurrentToken && CurrentToken->is(tok::l_paren))
1724 CurrentToken->setType(TT_OverloadedOperatorLParen);
1725 if (CurrentToken && CurrentToken->Previous->is(TT_BinaryOperator))
1726 CurrentToken->Previous->setType(TT_OverloadedOperator);
1729 if (Style.isJavaScript() &&
Next &&
1730 Next->isOneOf(tok::semi, tok::comma, tok::colon, tok::r_paren,
1731 tok::r_brace, tok::r_square)) {
1736 Tok->setType(TT_JsTypeOptionalQuestion);
1741 if (Line.MustBeDeclaration && !Contexts.back().IsExpression &&
1742 Style.isJavaScript()) {
1745 if (Style.isCSharp()) {
1748 if (
Next && (
Next->isOneOf(tok::r_paren, tok::greater) ||
1749 Next->startsSequence(tok::identifier, tok::semi) ||
1750 Next->startsSequence(tok::identifier, tok::equal))) {
1751 Tok->setType(TT_CSharpNullable);
1760 if (!Contexts.back().IsExpression && Line.MustBeDeclaration &&
1761 (!
Next ||
Next->isNoneOf(tok::identifier, tok::string_literal) ||
1762 !
Next->Next ||
Next->Next->isNoneOf(tok::colon, tok::question))) {
1763 Tok->setType(TT_CSharpNullable);
1769 case tok::kw_template:
1770 parseTemplateDeclaration();
1773 switch (Contexts.back().ContextType) {
1774 case Context::CtorInitializer:
1775 Tok->setType(TT_CtorInitializerComma);
1777 case Context::InheritanceList:
1778 Tok->setType(TT_InheritanceComma);
1780 case Context::VerilogInstancePortList:
1781 Tok->setType(TT_VerilogInstancePortComma);
1784 if (Style.isVerilog() && Contexts.size() == 1 &&
1785 Line.startsWith(Keywords.kw_assign)) {
1786 Tok->setFinalizedType(TT_VerilogAssignComma);
1787 }
else if (Contexts.back().FirstStartOfName &&
1788 (Contexts.size() == 1 || startsWithInitStatement(Line))) {
1789 Contexts.back().FirstStartOfName->PartOfMultiVariableDeclStmt =
true;
1790 Line.IsMultiVariableDeclStmt =
true;
1794 if (Contexts.back().ContextType == Context::ForEachMacro)
1795 Contexts.back().IsExpression =
true;
1797 case tok::kw_default:
1799 if (Style.isVerilog() && Keywords.isVerilogEndOfLabel(*
Tok) &&
1800 (Line.Level > 1 || (!Line.InPPDirective && Line.Level > 0))) {
1804 case tok::identifier:
1805 if (
Tok->isOneOf(Keywords.kw___has_include,
1806 Keywords.kw___has_include_next)) {
1810 if (
Next &&
Next->is(tok::l_paren) && Prev &&
1811 Prev->isOneOf(tok::kw___cdecl, tok::kw___stdcall,
1812 tok::kw___fastcall, tok::kw___thiscall,
1813 tok::kw___regcall, tok::kw___vectorcall)) {
1814 Tok->setFinalizedType(TT_FunctionDeclarationName);
1815 Next->setFinalizedType(TT_FunctionDeclarationLParen);
1817 }
else if (Style.isCSharp()) {
1818 if (
Tok->is(Keywords.kw_where) &&
Next &&
Next->isNot(tok::l_paren)) {
1819 Tok->setType(TT_CSharpGenericTypeConstraint);
1820 parseCSharpGenericTypeConstraint();
1822 Line.IsContinuation =
true;
1824 }
else if (Style.isTableGen()) {
1825 if (
Tok->is(Keywords.kw_assert)) {
1826 if (!parseTableGenValue())
1828 }
else if (
Tok->isOneOf(Keywords.kw_def, Keywords.kw_defm) &&
1829 (!
Next ||
Next->isNoneOf(tok::colon, tok::l_brace))) {
1831 if (!parseTableGenValue(
true))
1835 if (Style.AllowBreakBeforeQtProperty &&
1836 Contexts.back().ContextType == Context::QtProperty &&
1837 Tok->isQtProperty()) {
1838 Tok->setFinalizedType(TT_QtProperty);
1842 if (
Tok->isNot(TT_LambdaArrow) && Prev && Prev->is(tok::kw_noexcept))
1843 Tok->setType(TT_TrailingReturnArrow);
1847 if (Style.isTableGen() && !parseTableGenValue())
1849 if (!Scopes.empty() && Scopes.back() ==
ST_Enum)
1850 Tok->setFinalizedType(TT_EnumEqual);
1858 void parseCSharpGenericTypeConstraint() {
1859 int OpenAngleBracketsCount = 0;
1860 while (CurrentToken) {
1861 if (CurrentToken->is(tok::less)) {
1863 CurrentToken->setType(TT_TemplateOpener);
1864 ++OpenAngleBracketsCount;
1866 }
else if (CurrentToken->is(tok::greater)) {
1867 CurrentToken->setType(TT_TemplateCloser);
1868 --OpenAngleBracketsCount;
1870 }
else if (CurrentToken->is(tok::comma) && OpenAngleBracketsCount == 0) {
1873 CurrentToken->setType(TT_CSharpGenericTypeConstraintComma);
1875 }
else if (CurrentToken->is(Keywords.kw_where)) {
1876 CurrentToken->setType(TT_CSharpGenericTypeConstraint);
1878 }
else if (CurrentToken->is(tok::colon)) {
1879 CurrentToken->setType(TT_CSharpGenericTypeConstraintColon);
1887 void parseIncludeDirective() {
1888 if (CurrentToken && CurrentToken->is(tok::less)) {
1890 while (CurrentToken) {
1893 if (CurrentToken->isNot(tok::comment) &&
1894 !CurrentToken->TokenText.starts_with(
"//")) {
1895 CurrentToken->setType(TT_ImplicitStringLiteral);
1902 void parseWarningOrError() {
1907 while (CurrentToken) {
1908 CurrentToken->setType(TT_ImplicitStringLiteral);
1913 void parsePragma() {
1916 CurrentToken->isOneOf(Keywords.kw_mark, Keywords.kw_option,
1917 Keywords.kw_region)) {
1918 bool IsMarkOrRegion =
1919 CurrentToken->isOneOf(Keywords.kw_mark, Keywords.kw_region);
1922 while (CurrentToken) {
1923 if (IsMarkOrRegion || CurrentToken->Previous->is(TT_BinaryOperator))
1924 CurrentToken->setType(TT_ImplicitStringLiteral);
1930 void parseHasInclude() {
1931 if (!CurrentToken || CurrentToken->isNot(tok::l_paren))
1934 parseIncludeDirective();
1938 LineType parsePreprocessorDirective() {
1939 bool IsFirstToken = CurrentToken->IsFirst;
1945 if (Style.isJavaScript() && IsFirstToken) {
1949 while (CurrentToken) {
1951 CurrentToken->setType(TT_ImplicitStringLiteral);
1957 if (CurrentToken->is(tok::numeric_constant)) {
1958 CurrentToken->SpacesRequiredBefore = 1;
1963 if (!CurrentToken->Tok.getIdentifierInfo())
1967 if (Style.isVerilog() && !Keywords.isVerilogPPDirective(*CurrentToken))
1969 switch (CurrentToken->Tok.getIdentifierInfo()->getPPKeywordID()) {
1970 case tok::pp_include:
1971 case tok::pp_include_next:
1972 case tok::pp_import:
1974 parseIncludeDirective();
1978 case tok::pp_warning:
1979 parseWarningOrError();
1981 case tok::pp_pragma:
1986 Contexts.back().IsExpression =
true;
1989 CurrentToken->SpacesRequiredBefore = 1;
1995 while (CurrentToken) {
1998 if (
Tok->is(tok::l_paren)) {
2000 }
else if (
Tok->isOneOf(Keywords.kw___has_include,
2001 Keywords.kw___has_include_next)) {
2012 NonTemplateLess.clear();
2013 if (!Line.InMacroBody && CurrentToken->is(tok::hash)) {
2017 auto Type = parsePreprocessorDirective();
2025 IdentifierInfo *Info = CurrentToken->Tok.getIdentifierInfo();
2026 if ((Style.isJava() && CurrentToken->is(Keywords.kw_package)) ||
2027 (!Style.isVerilog() && Info &&
2028 Info->getPPKeywordID() == tok::pp_import && CurrentToken->Next &&
2029 CurrentToken->Next->isOneOf(tok::string_literal, tok::identifier,
2032 parseIncludeDirective();
2038 if (CurrentToken->is(tok::less) && Line.Last->is(tok::greater)) {
2039 parseIncludeDirective();
2045 if (Style.Language == FormatStyle::LK_Proto && Line.Level == 0 &&
2046 CurrentToken->isOneOf(Keywords.kw_option, Keywords.kw_package)) {
2048 if (CurrentToken && CurrentToken->is(tok::identifier)) {
2049 while (CurrentToken)
2055 bool KeywordVirtualFound =
false;
2056 bool ImportStatement =
false;
2059 if (Style.isJavaScript() && CurrentToken->is(Keywords.kw_import))
2060 ImportStatement =
true;
2062 while (CurrentToken) {
2063 if (CurrentToken->is(tok::kw_virtual))
2064 KeywordVirtualFound =
true;
2065 if (Style.isJavaScript()) {
2072 if (Line.First->is(tok::kw_export) &&
2073 CurrentToken->is(Keywords.kw_from) && CurrentToken->Next &&
2074 CurrentToken->Next->isStringLiteral()) {
2075 ImportStatement =
true;
2077 if (isClosureImportStatement(*CurrentToken))
2078 ImportStatement =
true;
2080 if (!consumeToken())
2088 if (KeywordVirtualFound)
2090 if (ImportStatement)
2093 if (Line.startsWith(TT_ObjCMethodSpecifier)) {
2094 if (Contexts.back().FirstObjCSelectorName) {
2095 Contexts.back().FirstObjCSelectorName->LongestObjCSelectorName =
2096 Contexts.back().LongestObjCSelectorName;
2101 for (
const auto &ctx : Contexts)
2102 if (ctx.ContextType == Context::StructArrayInitializer)
2112 return Tok.TokenText ==
"goog" &&
Tok.Next &&
Tok.Next->is(tok::period) &&
2114 (
Tok.Next->Next->TokenText ==
"module" ||
2115 Tok.Next->Next->TokenText ==
"provide" ||
2116 Tok.Next->Next->TokenText ==
"require" ||
2117 Tok.Next->Next->TokenText ==
"requireType" ||
2118 Tok.Next->Next->TokenText ==
"forwardDeclare") &&
2119 Tok.Next->Next->Next &&
Tok.Next->Next->Next->is(tok::l_paren);
2122 void resetTokenMetadata() {
2128 if (!CurrentToken->isTypeFinalized() &&
2129 CurrentToken->isNoneOf(
2130 TT_LambdaLSquare, TT_LambdaLBrace, TT_AttributeMacro, TT_IfMacro,
2131 TT_ForEachMacro, TT_TypenameMacro, TT_FunctionLBrace,
2132 TT_ImplicitStringLiteral, TT_InlineASMBrace, TT_FatArrow,
2133 TT_LambdaArrow, TT_NamespaceMacro, TT_OverloadedOperator,
2134 TT_RegexLiteral, TT_TemplateString, TT_ObjCStringLiteral,
2135 TT_UntouchableMacroFunc, TT_StatementAttributeLikeMacro,
2136 TT_FunctionLikeOrFreestandingMacro, TT_ClassLBrace, TT_EnumLBrace,
2137 TT_RecordLBrace, TT_StructLBrace, TT_UnionLBrace, TT_RequiresClause,
2138 TT_RequiresClauseInARequiresExpression, TT_RequiresExpression,
2139 TT_RequiresExpressionLParen, TT_RequiresExpressionLBrace,
2140 TT_CompoundRequirementLBrace, TT_BracedListLBrace,
2141 TT_FunctionLikeMacro)) {
2142 CurrentToken->setType(TT_Unknown);
2144 CurrentToken->Role.reset();
2145 CurrentToken->MatchingParen =
nullptr;
2146 CurrentToken->FakeLParens.clear();
2147 CurrentToken->FakeRParens = 0;
2154 CurrentToken->NestingLevel = Contexts.size() - 1;
2155 CurrentToken->BindingStrength = Contexts.back().BindingStrength;
2156 modifyContext(*CurrentToken);
2157 determineTokenType(*CurrentToken);
2158 CurrentToken = CurrentToken->Next;
2160 resetTokenMetadata();
2168 : ContextKind(ContextKind), BindingStrength(BindingStrength),
2169 IsExpression(IsExpression) {}
2172 unsigned BindingStrength;
2174 unsigned LongestObjCSelectorName = 0;
2175 bool ColonIsForRangeExpr =
false;
2176 bool ColonIsDictLiteral =
false;
2177 bool ColonIsObjCMethodExpr =
false;
2180 bool CanBeExpression =
true;
2181 bool CaretFound =
false;
2182 bool InCpp11AttributeSpecifier =
false;
2183 bool InCSharpAttributeSpecifier =
false;
2184 bool InStaticAssertFirstArgument =
false;
2185 bool VerilogAssignmentFound =
false;
2188 bool VerilogMayBeConcatenation =
false;
2189 bool IsTableGenDAGArgList =
false;
2190 bool IsTableGenBangOpe =
false;
2191 bool IsTableGenCondOpe =
false;
2204 StructArrayInitializer,
2208 C11GenericSelection,
2211 VerilogInstancePortList,
2212 } ContextType = Unknown;
2217 struct ScopedContextCreator {
2218 AnnotatingParser &P;
2220 ScopedContextCreator(AnnotatingParser &P,
tok::TokenKind ContextKind,
2223 P.Contexts.push_back(Context(ContextKind,
2224 P.Contexts.back().BindingStrength + Increase,
2225 P.Contexts.back().IsExpression));
2228 ~ScopedContextCreator() {
2229 if (P.Style.AlignArrayOfStructures != FormatStyle::AIAS_None) {
2230 if (P.Contexts.back().ContextType == Context::StructArrayInitializer) {
2231 P.Contexts.pop_back();
2232 P.Contexts.back().ContextType = Context::StructArrayInitializer;
2236 P.Contexts.pop_back();
2241 auto AssignmentStartsExpression = [&]() {
2245 if (Line.First->isOneOf(tok::kw_using, tok::kw_return))
2247 if (Line.First->is(tok::kw_template)) {
2248 assert(Current.Previous);
2249 if (Current.Previous->is(tok::kw_operator)) {
2257 if (
Tok->isNot(TT_TemplateOpener)) {
2264 if (Contexts.back().ContextKind == tok::less) {
2265 assert(Current.Previous->Previous);
2266 return Current.Previous->Previous->isNoneOf(tok::kw_typename,
2270 Tok =
Tok->MatchingParen;
2273 Tok =
Tok->getNextNonComment();
2277 if (
Tok->isOneOf(tok::kw_class, tok::kw_enum, tok::kw_struct,
2287 if (Style.isJavaScript() &&
2288 (Line.startsWith(Keywords.kw_type, tok::identifier) ||
2289 Line.startsWith(tok::kw_export, Keywords.kw_type,
2290 tok::identifier))) {
2294 return !Current.Previous || Current.Previous->isNot(tok::kw_operator);
2297 if (AssignmentStartsExpression()) {
2298 Contexts.back().IsExpression =
true;
2299 if (!Line.startsWith(TT_UnaryOperator)) {
2302 Previous->Previous->isNoneOf(tok::comma, tok::semi);
2304 if (
Previous->isOneOf(tok::r_square, tok::r_paren, tok::greater)) {
2311 if (
Previous->isOneOf(TT_BinaryOperator, TT_UnaryOperator) &&
2313 Previous->Previous->isNot(tok::equal)) {
2314 Previous->setType(TT_PointerOrReference);
2318 }
else if (Current.is(tok::lessless) &&
2319 (!Current.Previous ||
2320 Current.Previous->isNot(tok::kw_operator))) {
2321 Contexts.back().IsExpression =
true;
2322 }
else if (Current.isOneOf(tok::kw_return, tok::kw_throw)) {
2323 Contexts.back().IsExpression =
true;
2324 }
else if (Current.is(TT_TrailingReturnArrow)) {
2325 Contexts.back().IsExpression =
false;
2326 }
else if (Current.isOneOf(TT_LambdaArrow, Keywords.kw_assert)) {
2327 Contexts.back().IsExpression = Style.isJava();
2328 }
else if (Current.Previous &&
2329 Current.Previous->is(TT_CtorInitializerColon)) {
2330 Contexts.back().IsExpression =
true;
2331 Contexts.back().ContextType = Context::CtorInitializer;
2332 }
else if (Current.Previous && Current.Previous->is(TT_InheritanceColon)) {
2333 Contexts.back().ContextType = Context::InheritanceList;
2334 }
else if (Current.isOneOf(tok::r_paren, tok::greater, tok::comma)) {
2338 Previous->setType(TT_PointerOrReference);
2340 if (Line.MustBeDeclaration &&
2341 Contexts.front().ContextType != Context::CtorInitializer) {
2342 Contexts.back().IsExpression =
false;
2344 }
else if (Current.is(tok::kw_new)) {
2345 Contexts.back().CanBeExpression =
false;
2346 }
else if (Current.is(tok::semi) ||
2347 (Current.is(tok::exclaim) && Current.Previous &&
2348 Current.Previous->isNot(tok::kw_operator))) {
2352 Contexts.back().IsExpression =
true;
2360 if (Current->is(tok::l_paren))
2362 if (Current->is(tok::r_paren))
2366 Current = Current->Next;
2371 static bool isDeductionGuide(
FormatToken &Current) {
2373 if (Current.Previous && Current.Previous->is(tok::r_paren) &&
2374 Current.startsSequence(tok::arrow, tok::identifier, tok::less)) {
2378 while (TemplateCloser) {
2380 if (TemplateCloser->is(tok::l_paren)) {
2382 TemplateCloser = untilMatchingParen(TemplateCloser);
2383 if (!TemplateCloser)
2386 if (TemplateCloser->is(tok::less))
2388 if (TemplateCloser->is(tok::greater))
2392 TemplateCloser = TemplateCloser->Next;
2396 if (TemplateCloser && TemplateCloser->Next &&
2397 TemplateCloser->Next->is(tok::semi) &&
2398 Current.Previous->MatchingParen) {
2402 Current.Previous->MatchingParen->Previous;
2404 return LeadingIdentifier &&
2405 LeadingIdentifier->TokenText == Current.Next->TokenText;
2412 if (Current.isNot(TT_Unknown)) {
2417 if ((Style.isJavaScript() || Style.isCSharp()) &&
2418 Current.is(tok::exclaim)) {
2419 if (Current.Previous) {
2421 Style.isJavaScript()
2422 ? Keywords.isJavaScriptIdentifier(
2423 *Current.Previous,
true)
2424 : Current.Previous->is(tok::identifier);
2426 Current.Previous->isOneOf(
2427 tok::kw_default, tok::kw_namespace, tok::r_paren, tok::r_square,
2428 tok::r_brace, tok::kw_false, tok::kw_true, Keywords.kw_type,
2429 Keywords.kw_get, Keywords.kw_init, Keywords.kw_set) ||
2430 Current.Previous->Tok.isLiteral()) {
2431 Current.setType(TT_NonNullAssertion);
2436 Current.Next->isOneOf(TT_BinaryOperator, Keywords.kw_as)) {
2437 Current.setType(TT_NonNullAssertion);
2445 if ((Style.isJavaScript() || Style.isJava()) &&
2446 Current.is(Keywords.kw_instanceof)) {
2447 Current.setType(TT_BinaryOperator);
2448 }
else if (isStartOfName(Current) &&
2449 (!Line.MightBeFunctionDecl || Current.NestingLevel != 0)) {
2450 Contexts.back().FirstStartOfName = &Current;
2451 Current.setType(TT_StartOfName);
2452 }
else if (Current.is(tok::semi)) {
2456 Contexts.back().FirstStartOfName =
nullptr;
2457 }
else if (Current.isOneOf(tok::kw_auto, tok::kw___auto_type)) {
2459 }
else if (Current.is(tok::arrow) && Style.isJava()) {
2460 Current.setType(TT_LambdaArrow);
2461 }
else if (Current.is(tok::arrow) && Style.isVerilog()) {
2463 Current.setType(TT_BinaryOperator);
2464 }
else if (Current.is(tok::arrow) && AutoFound &&
2465 Line.MightBeFunctionDecl && Current.NestingLevel == 0 &&
2466 Current.Previous->isNoneOf(tok::kw_operator, tok::identifier)) {
2468 Current.setType(TT_TrailingReturnArrow);
2469 }
else if (Current.is(tok::arrow) && Current.Previous &&
2470 Current.Previous->is(tok::r_brace) &&
2474 Current.setType(TT_TrailingReturnArrow);
2475 }
else if (isDeductionGuide(Current)) {
2477 Current.setType(TT_TrailingReturnArrow);
2478 }
else if (Current.isPointerOrReference()) {
2479 Current.setType(determineStarAmpUsage(
2481 (Contexts.back().CanBeExpression && Contexts.back().IsExpression) ||
2482 Contexts.back().InStaticAssertFirstArgument,
2483 Contexts.back().ContextType == Context::TemplateArgument));
2484 }
else if (Current.isOneOf(tok::minus, tok::plus, tok::caret) ||
2485 (Style.isVerilog() && Current.is(tok::pipe))) {
2486 Current.setType(determinePlusMinusCaretUsage(Current));
2487 if (Current.is(TT_UnaryOperator) && Current.is(tok::caret))
2488 Contexts.back().CaretFound =
true;
2489 }
else if (Current.isOneOf(tok::minusminus, tok::plusplus)) {
2490 Current.setType(determineIncrementUsage(Current));
2491 }
else if (Current.isOneOf(tok::exclaim, tok::tilde)) {
2492 Current.setType(TT_UnaryOperator);
2493 }
else if (Current.is(tok::question)) {
2494 if (Style.isJavaScript() && Line.MustBeDeclaration &&
2495 !Contexts.back().IsExpression) {
2498 Current.setType(TT_JsTypeOptionalQuestion);
2499 }
else if (Style.isTableGen()) {
2501 Current.setType(TT_Unknown);
2503 Current.setType(TT_ConditionalExpr);
2505 Contexts.back().IsExpression =
true;
2507 }
else if (Current.isBinaryOperator() &&
2508 (!Current.Previous || Current.Previous->isNot(tok::l_square)) &&
2509 (Current.isNot(tok::greater) && !Style.isTextProto())) {
2510 if (Style.isVerilog()) {
2511 if (Current.is(tok::lessequal) && Contexts.size() == 1 &&
2512 !Contexts.back().VerilogAssignmentFound) {
2516 Current.setFinalizedType(TT_BinaryOperator);
2519 Contexts.back().VerilogAssignmentFound =
true;
2521 Current.setType(TT_BinaryOperator);
2522 }
else if (Current.is(tok::comment)) {
2523 if (Current.TokenText.starts_with(
"/*")) {
2524 if (Current.TokenText.ends_with(
"*/")) {
2525 Current.setType(TT_BlockComment);
2529 Current.Tok.setKind(tok::unknown);
2532 Current.setType(TT_LineComment);
2534 }
else if (Current.is(tok::string_literal)) {
2535 if (Style.isVerilog() && Contexts.back().VerilogMayBeConcatenation &&
2536 Current.getPreviousNonComment() &&
2537 Current.getPreviousNonComment()->isOneOf(tok::comma, tok::l_brace) &&
2538 Current.getNextNonComment() &&
2539 Current.getNextNonComment()->isOneOf(tok::comma, tok::r_brace)) {
2540 Current.setType(TT_StringInConcatenation);
2542 }
else if (Current.is(tok::l_paren)) {
2543 if (lParenStartsCppCast(Current))
2544 Current.setType(TT_CppCastLParen);
2545 }
else if (Current.is(tok::r_paren)) {
2546 if (rParenEndsCast(Current))
2547 Current.setType(TT_CastRParen);
2548 if (Current.MatchingParen && Current.MatchingParen->is(TT_InlineASMParen))
2549 Current.setType(TT_InlineASMParen);
2550 if (Current.MatchingParen && Current.Next &&
2551 !Current.Next->isBinaryOperator() &&
2552 Current.Next->isNoneOf(
2553 tok::semi, tok::colon, tok::l_brace, tok::l_paren, tok::comma,
2554 tok::period, tok::arrow, tok::coloncolon, tok::kw_noexcept)) {
2555 if (
FormatToken *AfterParen = Current.MatchingParen->Next;
2556 AfterParen && AfterParen->isNot(tok::caret)) {
2558 if (
FormatToken *BeforeParen = Current.MatchingParen->Previous;
2559 BeforeParen && BeforeParen->is(tok::identifier) &&
2560 BeforeParen->isNot(TT_TypenameMacro) &&
2561 BeforeParen->TokenText == BeforeParen->TokenText.upper() &&
2562 (!BeforeParen->Previous ||
2563 BeforeParen->Previous->ClosesTemplateDeclaration ||
2564 BeforeParen->Previous->ClosesRequiresClause)) {
2565 Current.setType(TT_FunctionAnnotationRParen);
2569 }
else if (Current.is(tok::at) && Current.Next && !Style.isJavaScript() &&
2573 switch (Current.Next->Tok.getObjCKeywordID()) {
2574 case tok::objc_interface:
2575 case tok::objc_implementation:
2576 case tok::objc_protocol:
2577 Current.setType(TT_ObjCDecl);
2579 case tok::objc_property:
2580 Current.setType(TT_ObjCProperty);
2585 }
else if (Current.is(tok::period)) {
2586 FormatToken *PreviousNoComment = Current.getPreviousNonComment();
2587 if (PreviousNoComment &&
2588 PreviousNoComment->isOneOf(tok::comma, tok::l_brace)) {
2589 Current.setType(TT_DesignatedInitializerPeriod);
2590 }
else if (Style.isJava() && Current.Previous &&
2591 Current.Previous->isOneOf(TT_JavaAnnotation,
2592 TT_LeadingJavaAnnotation)) {
2593 Current.setType(Current.Previous->getType());
2595 }
else if (canBeObjCSelectorComponent(Current) &&
2598 Current.Previous && Current.Previous->is(TT_CastRParen) &&
2599 Current.Previous->MatchingParen &&
2600 Current.Previous->MatchingParen->Previous &&
2601 Current.Previous->MatchingParen->Previous->is(
2602 TT_ObjCMethodSpecifier)) {
2606 Current.setType(TT_SelectorName);
2607 }
else if (Current.isOneOf(tok::identifier, tok::kw_const, tok::kw_noexcept,
2608 tok::kw_requires) &&
2610 Current.Previous->isNoneOf(tok::equal, tok::at,
2611 TT_CtorInitializerComma,
2612 TT_CtorInitializerColon) &&
2613 Line.MightBeFunctionDecl && Contexts.size() == 1) {
2616 Current.setType(TT_TrailingAnnotation);
2617 }
else if ((Style.isJava() || Style.isJavaScript()) && Current.Previous) {
2618 if (Current.Previous->is(tok::at) &&
2619 Current.isNot(Keywords.kw_interface)) {
2623 Current.setType(TT_LeadingJavaAnnotation);
2625 Current.setType(TT_JavaAnnotation);
2626 }
else if (Current.Previous->is(tok::period) &&
2627 Current.Previous->isOneOf(TT_JavaAnnotation,
2628 TT_LeadingJavaAnnotation)) {
2629 Current.setType(Current.Previous->getType());
2641 if (Style.isVerilog())
2644 if (!
Tok.Previous ||
Tok.isNot(tok::identifier) ||
Tok.is(TT_ClassHeadName))
2647 if (
Tok.endsSequence(Keywords.kw_final, TT_ClassHeadName))
2650 if ((Style.isJavaScript() || Style.isJava()) &&
Tok.is(Keywords.kw_extends))
2653 if (
const auto *NextNonComment =
Tok.getNextNonComment();
2654 (!NextNonComment && !Line.InMacroBody) ||
2656 (NextNonComment->isPointerOrReference() ||
2657 NextNonComment->isOneOf(TT_ClassHeadName, tok::string_literal) ||
2658 (Line.InPragmaDirective && NextNonComment->is(tok::identifier))))) {
2662 if (
Tok.Previous->isOneOf(TT_LeadingJavaAnnotation, Keywords.kw_instanceof,
2666 if (Style.isJavaScript() &&
Tok.Previous->is(Keywords.kw_in))
2673 if (!Style.isJavaScript())
2674 while (PreviousNotConst && PreviousNotConst->is(tok::kw_const))
2675 PreviousNotConst = PreviousNotConst->getPreviousNonComment();
2677 if (!PreviousNotConst)
2680 if (PreviousNotConst->ClosesRequiresClause)
2683 if (Style.isTableGen()) {
2685 if (Keywords.isTableGenDefinition(*PreviousNotConst))
2688 if (Contexts.back().ContextKind != tok::l_brace)
2692 bool IsPPKeyword = PreviousNotConst->is(tok::identifier) &&
2693 PreviousNotConst->Previous &&
2694 PreviousNotConst->Previous->is(tok::hash);
2696 if (PreviousNotConst->is(TT_TemplateCloser)) {
2697 return PreviousNotConst && PreviousNotConst->MatchingParen &&
2698 PreviousNotConst->MatchingParen->Previous &&
2699 PreviousNotConst->MatchingParen->Previous->isNoneOf(
2700 tok::period, tok::kw_template);
2703 if ((PreviousNotConst->is(tok::r_paren) &&
2704 PreviousNotConst->is(TT_TypeDeclarationParen)) ||
2705 PreviousNotConst->is(TT_AttributeRParen)) {
2714 if (PreviousNotConst->isOneOf(tok::identifier, tok::kw_auto) &&
2715 !PreviousNotConst->endsSequence(Keywords.kw_import, tok::kw_export) &&
2716 PreviousNotConst->isNot(TT_StatementAttributeLikeMacro)) {
2721 if (PreviousNotConst->is(TT_PointerOrReference) ||
2722 PreviousNotConst->endsSequence(tok::coloncolon,
2723 TT_PointerOrReference)) {
2728 if (PreviousNotConst->isTypeName(LangOpts))
2732 if (Style.isJava() && PreviousNotConst->is(tok::r_square))
2736 return Style.isJavaScript() && PreviousNotConst->is(tok::kw_const);
2746 if (LeftOfParens && LeftOfParens->is(TT_TemplateCloser) &&
2747 LeftOfParens->MatchingParen) {
2748 auto *Prev = LeftOfParens->MatchingParen->getPreviousNonComment();
2750 Prev->isOneOf(tok::kw_const_cast, tok::kw_dynamic_cast,
2751 tok::kw_reinterpret_cast, tok::kw_static_cast)) {
2762 assert(
Tok.is(tok::r_paren));
2764 if (!
Tok.MatchingParen || !
Tok.Previous)
2768 if (!IsCpp && !Style.isCSharp() && !Style.isJava())
2771 const auto *LParen =
Tok.MatchingParen;
2772 const auto *BeforeRParen =
Tok.Previous;
2773 const auto *AfterRParen =
Tok.Next;
2776 if (BeforeRParen == LParen || !AfterRParen)
2779 if (LParen->isOneOf(TT_OverloadedOperatorLParen, TT_FunctionTypeLParen))
2782 auto *LeftOfParens = LParen->getPreviousNonComment();
2786 if (LeftOfParens->is(tok::r_paren) &&
2787 LeftOfParens->isNot(TT_CastRParen)) {
2788 if (!LeftOfParens->MatchingParen ||
2789 !LeftOfParens->MatchingParen->Previous) {
2792 LeftOfParens = LeftOfParens->MatchingParen->Previous;
2795 if (LeftOfParens->is(tok::r_square)) {
2798 if (
Tok->isNot(tok::r_square))
2801 Tok =
Tok->getPreviousNonComment();
2802 if (!
Tok ||
Tok->isNot(tok::l_square))
2805 Tok =
Tok->getPreviousNonComment();
2806 if (!
Tok ||
Tok->isNot(tok::kw_delete))
2810 if (
FormatToken *MaybeDelete = MayBeArrayDelete(LeftOfParens))
2811 LeftOfParens = MaybeDelete;
2817 if (LeftOfParens->Tok.getIdentifierInfo() && LeftOfParens->Previous &&
2818 LeftOfParens->Previous->is(tok::kw_operator)) {
2824 if (LeftOfParens->Tok.getIdentifierInfo() &&
2825 LeftOfParens->isNoneOf(TT_ObjCForIn, tok::kw_return, tok::kw_case,
2826 tok::kw_delete, tok::kw_throw)) {
2832 if (LeftOfParens->isOneOf(tok::at, tok::r_square, TT_OverloadedOperator,
2833 TT_TemplateCloser, tok::ellipsis)) {
2838 if (AfterRParen->is(tok::question) ||
2839 (AfterRParen->is(tok::ampamp) && !BeforeRParen->isTypeName(LangOpts))) {
2844 if (AfterRParen->is(Keywords.kw_in) && Style.isCSharp())
2849 if (AfterRParen->isOneOf(tok::kw_noexcept, tok::kw_volatile, tok::kw_const,
2850 tok::kw_requires, tok::kw_throw, tok::arrow,
2851 Keywords.kw_override, Keywords.kw_final) ||
2852 isCppAttribute(IsCpp, *AfterRParen)) {
2858 if (Style.isJava() && AfterRParen->is(tok::l_paren))
2862 if (AfterRParen->isOneOf(tok::kw_sizeof, tok::kw_alignof) ||
2863 (AfterRParen->Tok.isLiteral() &&
2864 AfterRParen->isNot(tok::string_literal))) {
2869 if (
Tok.isNot(TT_TemplateCloser))
2871 const auto *
Less =
Tok.MatchingParen;
2874 const auto *BeforeLess =
Less->getPreviousNonComment();
2875 return BeforeLess && BeforeLess->isNot(TT_VariableTemplate);
2879 auto IsQualifiedPointerOrReference = [](
const FormatToken *
T,
2880 const LangOptions &LangOpts) {
2882 assert(!
T->isTypeName(LangOpts) &&
"Should have already been checked");
2886 if (
T->is(TT_AttributeRParen)) {
2888 assert(
T->is(tok::r_paren));
2889 assert(
T->MatchingParen);
2890 assert(
T->MatchingParen->is(tok::l_paren));
2891 assert(
T->MatchingParen->is(TT_AttributeLParen));
2892 if (
const auto *
Tok =
T->MatchingParen->Previous;
2893 Tok &&
Tok->isAttribute()) {
2897 }
else if (
T->is(TT_AttributeRSquare)) {
2899 if (
T->MatchingParen &&
T->MatchingParen->Previous) {
2900 T =
T->MatchingParen->Previous;
2903 }
else if (
T->canBePointerOrReferenceQualifier()) {
2909 return T &&
T->is(TT_PointerOrReference);
2912 bool ParensAreType = IsNonVariableTemplate(*BeforeRParen) ||
2913 BeforeRParen->is(TT_TypeDeclarationParen) ||
2914 BeforeRParen->isTypeName(LangOpts) ||
2915 IsQualifiedPointerOrReference(BeforeRParen, LangOpts);
2916 bool ParensCouldEndDecl =
2917 AfterRParen->isOneOf(tok::equal, tok::semi, tok::l_brace, tok::greater);
2918 if (ParensAreType && !ParensCouldEndDecl)
2929 for (
const auto *Token = LParen->Next; Token != &
Tok; Token = Token->Next)
2930 if (Token->is(TT_BinaryOperator))
2935 if (AfterRParen->isOneOf(tok::identifier, tok::kw_this))
2939 if (AfterRParen->is(tok::l_paren)) {
2940 for (
const auto *Prev = BeforeRParen; Prev->is(tok::identifier);) {
2941 Prev = Prev->Previous;
2942 if (Prev->is(tok::coloncolon))
2943 Prev = Prev->Previous;
2949 if (!AfterRParen->Next)
2954 if (Style.Language != FormatStyle::LK_C && AfterRParen->is(tok::l_brace) &&
2962 const bool NextIsAmpOrStar = AfterRParen->isOneOf(tok::amp, tok::star);
2963 if (!(AfterRParen->isUnaryOperator() || NextIsAmpOrStar) ||
2964 AfterRParen->is(tok::plus) ||
2965 AfterRParen->Next->isNoneOf(tok::identifier, tok::numeric_constant)) {
2969 if (NextIsAmpOrStar &&
2970 (AfterRParen->Next->is(tok::numeric_constant) || Line.InPPDirective)) {
2974 if (Line.InPPDirective && AfterRParen->is(tok::minus))
2977 const auto *Prev = BeforeRParen;
2980 if (Prev->is(tok::r_paren)) {
2981 if (Prev->is(TT_CastRParen))
2983 Prev = Prev->MatchingParen;
2986 Prev = Prev->Previous;
2987 if (!Prev || Prev->isNot(tok::r_paren))
2989 Prev = Prev->MatchingParen;
2990 return Prev && Prev->is(TT_FunctionTypeLParen);
2994 for (Prev = BeforeRParen; Prev != LParen; Prev = Prev->Previous)
2995 if (Prev->isNoneOf(tok::kw_const, tok::identifier, tok::coloncolon))
3013 if (PrevToken->isOneOf(
3014 TT_ConditionalExpr, tok::l_paren, tok::comma, tok::colon, tok::semi,
3015 tok::equal, tok::question, tok::l_square, tok::l_brace,
3016 tok::kw_case, tok::kw_co_await, tok::kw_co_return, tok::kw_co_yield,
3017 tok::kw_delete, tok::kw_return, tok::kw_throw)) {
3024 if (PrevToken->is(tok::kw_sizeof))
3028 if (PrevToken->isOneOf(TT_CastRParen, TT_UnaryOperator))
3032 if (PrevToken->is(TT_BinaryOperator))
3040 bool InTemplateArgument) {
3041 if (Style.isJavaScript())
3042 return TT_BinaryOperator;
3045 if (Style.isCSharp() &&
Tok.is(tok::ampamp))
3046 return TT_BinaryOperator;
3048 if (Style.isVerilog()) {
3053 if (
Tok.is(tok::star))
3054 return TT_BinaryOperator;
3055 return determineUnaryOperatorByUsage(
Tok) ? TT_UnaryOperator
3056 : TT_BinaryOperator;
3061 return TT_UnaryOperator;
3062 if (PrevToken->isTypeName(LangOpts))
3063 return TT_PointerOrReference;
3064 if (PrevToken->isPlacementOperator() &&
Tok.is(tok::ampamp))
3065 return TT_BinaryOperator;
3067 auto *NextToken =
Tok.getNextNonComment();
3069 return TT_PointerOrReference;
3070 if (NextToken->is(tok::greater))
3071 return TT_PointerOrReference;
3073 if (InTemplateArgument && NextToken->is(tok::kw_noexcept))
3074 return TT_BinaryOperator;
3076 if (NextToken->isOneOf(tok::arrow, tok::equal, tok::comma, tok::r_paren,
3077 tok::semi, TT_RequiresClause) ||
3078 (NextToken->is(tok::kw_noexcept) && !IsExpression) ||
3079 NextToken->canBePointerOrReferenceQualifier() ||
3080 (NextToken->is(tok::l_brace) && !NextToken->getNextNonComment())) {
3081 return TT_PointerOrReference;
3084 if (PrevToken->is(tok::coloncolon))
3085 return TT_PointerOrReference;
3087 if (PrevToken->is(tok::r_paren) && PrevToken->is(TT_TypeDeclarationParen))
3088 return TT_PointerOrReference;
3090 if (determineUnaryOperatorByUsage(
Tok))
3091 return TT_UnaryOperator;
3093 if (NextToken->is(tok::l_square) && NextToken->isNot(TT_LambdaLSquare))
3094 return TT_PointerOrReference;
3095 if (NextToken->is(tok::kw_operator) && !IsExpression)
3096 return TT_PointerOrReference;
3108 if (PrevToken->is(tok::r_brace) &&
Tok.is(tok::star) &&
3109 !PrevToken->MatchingParen) {
3110 return TT_PointerOrReference;
3113 if (PrevToken->endsSequence(tok::r_square, tok::l_square, tok::kw_delete))
3114 return TT_UnaryOperator;
3116 if (PrevToken->Tok.isLiteral() ||
3117 PrevToken->isOneOf(tok::r_paren, tok::r_square, tok::kw_true,
3118 tok::kw_false, tok::r_brace)) {
3119 return TT_BinaryOperator;
3123 while (NextNonParen && NextNonParen->is(tok::l_paren))
3124 NextNonParen = NextNonParen->getNextNonComment();
3125 if (NextNonParen && (NextNonParen->Tok.isLiteral() ||
3126 NextNonParen->isOneOf(tok::kw_true, tok::kw_false) ||
3127 NextNonParen->isUnaryOperator())) {
3128 return TT_BinaryOperator;
3134 if (InTemplateArgument && NextToken->Tok.isAnyIdentifier())
3135 return TT_BinaryOperator;
3139 if (
Tok.is(tok::ampamp) &&
3140 NextToken->isOneOf(tok::l_paren, tok::star, tok::amp)) {
3141 return TT_BinaryOperator;
3148 if (NextToken->Tok.isAnyIdentifier()) {
3149 auto *NextNextToken = NextToken->getNextNonComment();
3150 if (NextNextToken) {
3151 if (NextNextToken->is(tok::arrow))
3152 return TT_BinaryOperator;
3153 if (NextNextToken->isPointerOrReference() &&
3154 !NextToken->isObjCLifetimeQualifier(Style)) {
3155 NextNextToken->setFinalizedType(TT_BinaryOperator);
3156 return TT_BinaryOperator;
3163 if (IsExpression && !Contexts.back().CaretFound &&
3164 Line.getFirstNonComment()->isNot(
3165 TT_RequiresClauseInARequiresExpression)) {
3166 return TT_BinaryOperator;
3170 if (!Scopes.empty() && Scopes.back() ==
ST_Class)
3171 return TT_PointerOrReference;
3174 auto IsChainedOperatorAmpOrMember = [](
const FormatToken *token) {
3175 return !token || token->isOneOf(tok::amp, tok::period, tok::arrow,
3176 tok::arrowstar, tok::periodstar);
3181 if (
Tok.is(tok::amp) && PrevToken->Tok.isAnyIdentifier() &&
3182 IsChainedOperatorAmpOrMember(PrevToken->getPreviousNonComment()) &&
3183 NextToken && NextToken->Tok.isAnyIdentifier()) {
3184 if (
auto NextNext = NextToken->getNextNonComment();
3186 (IsChainedOperatorAmpOrMember(NextNext) || NextNext->is(tok::semi))) {
3187 return TT_BinaryOperator;
3193 return TT_BinaryOperator;
3196 return TT_PointerOrReference;
3200 if (determineUnaryOperatorByUsage(
Tok))
3201 return TT_UnaryOperator;
3205 return TT_UnaryOperator;
3207 if (PrevToken->is(tok::at))
3208 return TT_UnaryOperator;
3211 return TT_BinaryOperator;
3217 if (!PrevToken || PrevToken->is(TT_CastRParen))
3218 return TT_UnaryOperator;
3219 if (PrevToken->isOneOf(tok::r_paren, tok::r_square, tok::identifier))
3220 return TT_TrailingUnaryOperator;
3222 return TT_UnaryOperator;
3225 SmallVector<Context, 8> Contexts;
3227 const FormatStyle &Style;
3228 AnnotatedLine &Line;
3232 LangOptions LangOpts;
3233 const AdditionalKeywords &Keywords;
3235 SmallVector<ScopeType> &Scopes;
3241 llvm::SmallPtrSet<FormatToken *, 16> NonTemplateLess;
3243 int TemplateDeclarationDepth;
3251class ExpressionParser {
3253 ExpressionParser(
const FormatStyle &Style,
const AdditionalKeywords &Keywords,
3254 AnnotatedLine &Line)
3255 : Style(Style), Keywords(Keywords), Line(Line), Current(Line.
First) {}
3258 void parse(
int Precedence = 0) {
3261 while (Current && (Current->is(tok::kw_return) ||
3262 (Current->is(tok::colon) &&
3263 Current->isOneOf(TT_ObjCMethodExpr, TT_DictLiteral)))) {
3267 if (!Current || Precedence > PrecedenceArrowAndPeriod)
3272 parseConditionalExpr();
3278 if (Precedence == PrecedenceUnaryOperator) {
3279 parseUnaryOperator();
3296 if (Style.isVerilog() && Precedence ==
prec::Comma) {
3297 VerilogFirstOfType =
3298 verilogGroupDecl(VerilogFirstOfType, LatestOperator);
3302 parse(Precedence + 1);
3304 int CurrentPrecedence = getCurrentPrecedence();
3313 if (Style.BreakBinaryOperations.PerOperator.empty() &&
3314 Style.BreakBinaryOperations.Default ==
3315 FormatStyle::BBO_OnePerLine) {
3320 if (Precedence == CurrentPrecedence && Current &&
3321 Current->is(TT_SelectorName)) {
3323 addFakeParenthesis(Start,
prec::Level(Precedence));
3327 if ((Style.isCSharp() || Style.isJavaScript() || Style.isJava()) &&
3331 FormatToken *Prev = Current->getPreviousNonComment();
3332 if (Prev && Prev->is(tok::string_literal) &&
3333 (Prev == Start || Prev->endsSequence(tok::string_literal, tok::plus,
3334 TT_StringInConcatenation))) {
3335 Prev->setType(TT_StringInConcatenation);
3342 (Current->closesScope() &&
3343 (Current->MatchingParen || Current->is(TT_TemplateString))) ||
3344 (CurrentPrecedence != -1 && CurrentPrecedence < Precedence) ||
3353 if (Current->opensScope() ||
3354 Current->isOneOf(TT_RequiresClause,
3355 TT_RequiresClauseInARequiresExpression)) {
3358 while (Current && (!Current->closesScope() || Current->opensScope())) {
3365 if (CurrentPrecedence == Precedence) {
3367 LatestOperator->NextOperator = Current;
3368 LatestOperator = Current;
3372 next(Precedence > 0);
3377 if (Style.isVerilog() && Precedence ==
prec::Comma && VerilogFirstOfType)
3378 addFakeParenthesis(VerilogFirstOfType,
prec::Comma);
3380 if (LatestOperator && (Current || Precedence > 0)) {
3386 Start->Previous->isOneOf(TT_RequiresClause,
3387 TT_RequiresClauseInARequiresExpression))
3389 auto Ret = Current ? Current : Line.Last;
3390 while (!
Ret->ClosesRequiresClause &&
Ret->Previous)
3396 if (Precedence == PrecedenceArrowAndPeriod) {
3400 addFakeParenthesis(Start,
prec::Level(Precedence), End);
3408 int getCurrentPrecedence() {
3410 const FormatToken *NextNonComment = Current->getNextNonComment();
3411 if (Current->is(TT_ConditionalExpr))
3413 if (NextNonComment && Current->is(TT_SelectorName) &&
3414 (NextNonComment->isOneOf(TT_DictLiteral, TT_JsTypeColon) ||
3415 (Style.isProto() && NextNonComment->is(tok::less)))) {
3418 if (Current->is(TT_JsComputedPropertyName))
3420 if (Current->is(TT_LambdaArrow))
3422 if (Current->is(TT_FatArrow))
3424 if (Current->isOneOf(tok::semi, TT_InlineASMColon, TT_SelectorName) ||
3425 (Current->is(tok::comment) && NextNonComment &&
3426 NextNonComment->is(TT_SelectorName))) {
3429 if (Current->is(TT_RangeBasedForLoopColon))
3431 if ((Style.isJava() || Style.isJavaScript()) &&
3432 Current->is(Keywords.kw_instanceof)) {
3435 if (Style.isJavaScript() &&
3436 Current->isOneOf(Keywords.kw_in, Keywords.kw_as)) {
3439 if (Current->isOneOf(TT_BinaryOperator, tok::comma))
3440 return Current->getPrecedence();
3441 if (Current->isOneOf(tok::period, tok::arrow) &&
3442 Current->isNot(TT_TrailingReturnArrow)) {
3443 return PrecedenceArrowAndPeriod;
3445 if ((Style.isJava() || Style.isJavaScript()) &&
3446 Current->isOneOf(Keywords.kw_extends, Keywords.kw_implements,
3447 Keywords.kw_throws)) {
3452 if (Style.isVerilog() && Current->is(tok::colon))
3464 if (Start->MacroParent)
3467 Start->FakeLParens.push_back(Precedence);
3469 Start->StartsBinaryExpression =
true;
3470 if (!End && Current)
3471 End = Current->getPreviousNonComment();
3475 End->EndsBinaryExpression =
true;
3481 void parseUnaryOperator() {
3482 SmallVector<FormatToken *, 2> Tokens;
3483 while (Current && Current->is(TT_UnaryOperator)) {
3484 Tokens.push_back(Current);
3487 parse(PrecedenceArrowAndPeriod);
3494 void parseConditionalExpr() {
3495 while (Current && Current->isTrailingComment())
3499 if (!Current || Current->isNot(tok::question))
3503 if (!Current || Current->isNot(TT_ConditionalExpr))
3510 void next(
bool SkipPastLeadingComments =
true) {
3512 Current = Current->Next;
3514 (Current->NewlinesBefore == 0 || SkipPastLeadingComments) &&
3515 Current->isTrailingComment()) {
3516 Current = Current->Next;
3530 while (Start->startsSequence(tok::l_paren, tok::star)) {
3531 if (!(Start = Start->MatchingParen) ||
3532 !(Start = Start->getNextNonComment())) {
3539 if (
Tok->is(Keywords.kw_assign))
3540 Tok =
Tok->getNextNonComment();
3552 if (
Tok->is(tok::hash)) {
3557 Tok =
Tok->getNextNonComment();
3558 }
else if (
Tok->is(tok::hashhash)) {
3562 Tok =
Tok->getNextNonComment();
3563 }
else if (Keywords.isVerilogQualifier(*
Tok) ||
3564 Keywords.isVerilogIdentifier(*
Tok)) {
3568 while (
Tok &&
Tok->isOneOf(tok::period, tok::coloncolon) &&
3569 (
Tok =
Tok->getNextNonComment())) {
3570 if (Keywords.isVerilogIdentifier(*
Tok))
3571 Tok =
Tok->getNextNonComment();
3575 }
else if (
Tok->is(tok::l_paren)) {
3580 Keywords.kw_highz0, Keywords.kw_highz1, Keywords.kw_large,
3581 Keywords.kw_medium, Keywords.kw_pull0, Keywords.kw_pull1,
3582 Keywords.kw_small, Keywords.kw_strong0, Keywords.kw_strong1,
3583 Keywords.kw_supply0, Keywords.kw_supply1, Keywords.kw_weak0,
3584 Keywords.kw_weak1)) {
3585 Tok->setType(TT_VerilogStrength);
3586 Tok =
Tok->MatchingParen;
3588 Tok->setType(TT_VerilogStrength);
3589 Tok =
Tok->getNextNonComment();
3594 }
else if (
Tok->is(Keywords.kw_verilogHash)) {
3596 if (
Next->is(tok::l_paren))
3599 Tok =
Next->getNextNonComment();
3608 while (
Tok &&
Tok->is(tok::l_square) && (
Tok =
Tok->MatchingParen))
3609 Tok =
Tok->getNextNonComment();
3610 if (
Tok && (
Tok->is(tok::hash) || Keywords.isVerilogIdentifier(*
Tok)))
3619 First->setType(TT_VerilogDimensionedTypeName);
3620 }
else if (
First != Start) {
3628 if (TypedName->is(TT_Unknown))
3629 TypedName->setType(TT_StartOfName);
3631 if (FirstOfType && PreviousComma) {
3632 PreviousComma->setType(TT_VerilogTypeComma);
3633 addFakeParenthesis(FirstOfType,
prec::Comma, PreviousComma->Previous);
3636 FirstOfType = TypedName;
3643 while (Current && Current != FirstOfType) {
3644 if (Current->opensScope()) {
3655 const FormatStyle &Style;
3656 const AdditionalKeywords &Keywords;
3657 const AnnotatedLine &Line;
3667 assert(
Line->First);
3671 if (
const auto Column =
Line->First->OriginalColumn;
3675 const bool PPDirectiveOrImportStmt =
3678 if (PPDirectiveOrImportStmt)
3680 if (
const auto IndentWidth = Style.IndentWidth;
3682 Column % IndentWidth == 0) {
3689 Style.IndentPPDirectives < FormatStyle::PPDIS_BeforeHash &&
3690 PPDirectiveOrImportStmt
3692 : NextNonCommentLine->
Level;
3695 NextNonCommentLine =
Line->First->isNot(tok::r_brace) ?
Line :
nullptr;
3715 if (
Tok->isNot(tok::identifier))
3718 Tok =
Tok->getNextNonComment();
3724 if (
Tok->is(tok::coloncolon))
3725 return Tok->getNextNonComment();
3729 if (
Tok->is(TT_TemplateOpener)) {
3730 Tok =
Tok->MatchingParen;
3734 Tok =
Tok->getNextNonComment();
3739 return Tok->is(tok::coloncolon) ?
Tok->getNextNonComment() :
nullptr;
3747 Tok =
Tok->getNextNonComment()) {
3749 if (
Tok->is(TT_AttributeLSquare)) {
3750 Tok =
Tok->MatchingParen;
3758 if (
Tok->is(tok::l_paren) &&
Tok->is(TT_Unknown) &&
Tok->MatchingParen) {
3766 if (
Tok->isOneOf(tok::kw_friend, tok::kw_inline, tok::kw_virtual,
3767 tok::kw_constexpr, tok::kw_consteval, tok::kw_explicit)) {
3773 if (
Tok->is(tok::kw_template)) {
3774 Tok =
Tok->getNextNonComment();
3780 if (
Tok->isNot(TT_TemplateOpener))
3783 Tok =
Tok->MatchingParen;
3791 if (
Tok->is(tok::coloncolon)) {
3802 if (
Tok->is(tok::tilde)) {
3809 if (
Tok->isNot(tok::identifier) ||
Tok->isNot(TT_Unknown))
3820 assert(
Tok &&
Tok->is(tok::identifier));
3821 const auto *Prev =
Tok->Previous;
3823 if (Prev && Prev->is(tok::tilde))
3824 Prev = Prev->Previous;
3827 if (!Prev || (!Prev->endsSequence(tok::coloncolon, tok::identifier) &&
3828 !Prev->endsSequence(tok::coloncolon, TT_TemplateCloser))) {
3832 assert(Prev->Previous);
3833 if (Prev->Previous->is(TT_TemplateCloser) && Prev->Previous->MatchingParen) {
3834 Prev = Prev->Previous->MatchingParen;
3835 assert(Prev->Previous);
3838 return Prev->Previous->TokenText ==
Tok->TokenText;
3842 if (!
Line.InMacroBody)
3843 MacroBodyScopes.clear();
3845 auto &ScopeStack =
Line.InMacroBody ? MacroBodyScopes : Scopes;
3846 AnnotatingParser
Parser(Style,
Line, Keywords, ScopeStack);
3849 if (!
Line.Children.empty()) {
3852 for (
auto &Child :
Line.Children) {
3853 if (InRequiresExpression &&
3854 Child->First->isNoneOf(tok::kw_typename, tok::kw_requires,
3855 TT_CompoundRequirementLBrace)) {
3861 if (!ScopeStack.empty())
3862 ScopeStack.pop_back();
3875 ExpressionParser ExprParser(Style, Keywords,
Line);
3881 if (
Tok && ((!ScopeStack.empty() && ScopeStack.back() ==
ST_Class) ||
3883 Tok->setFinalizedType(TT_CtorDtorDeclName);
3884 assert(OpeningParen);
3889 if (
Line.startsWith(TT_ObjCMethodSpecifier))
3891 else if (
Line.startsWith(TT_ObjCDecl))
3893 else if (
Line.startsWith(TT_ObjCProperty))
3897 First->SpacesRequiredBefore = 1;
3898 First->CanBreakBefore =
First->MustBreakBefore;
3907 if (Current.
is(TT_FunctionDeclarationName))
3910 if (Current.
isNoneOf(tok::identifier, tok::kw_operator))
3913 const auto *Prev = Current.getPreviousNonComment();
3918 if (
const auto *PrevPrev =
Previous.getPreviousNonComment();
3919 PrevPrev && PrevPrev->is(TT_ObjCDecl)) {
3923 auto skipOperatorName =
3926 if (
Next->is(TT_OverloadedOperatorLParen))
3928 if (
Next->is(TT_OverloadedOperator))
3930 if (
Next->isPlacementOperator() ||
Next->is(tok::kw_co_await)) {
3933 Next->Next->startsSequence(tok::l_square, tok::r_square)) {
3938 if (
Next->startsSequence(tok::l_square, tok::r_square)) {
3943 if ((
Next->isTypeName(LangOpts) ||
Next->is(tok::identifier)) &&
3944 Next->Next &&
Next->Next->isPointerOrReference()) {
3949 if (
Next->is(TT_TemplateOpener) &&
Next->MatchingParen) {
3960 const bool IsCpp = LangOpts.CXXOperatorNames || LangOpts.C11;
3963 if (Current.
is(tok::kw_operator)) {
3964 if (
Line.startsWith(tok::kw_friend))
3966 if (
Previous.Tok.getIdentifierInfo() &&
3967 Previous.isNoneOf(tok::kw_return, tok::kw_co_return)) {
3972 assert(
Previous.MatchingParen->is(tok::l_paren));
3973 assert(
Previous.MatchingParen->is(TT_TypeDeclarationParen));
3982 while (
Next &&
Next->startsSequence(tok::hashhash, tok::identifier))
3985 if (
Next->is(TT_TemplateOpener) &&
Next->MatchingParen) {
3987 }
else if (
Next->is(tok::coloncolon)) {
3991 if (
Next->is(tok::kw_operator)) {
3992 Next = skipOperatorName(
Next->Next);
3995 if (
Next->isNot(tok::identifier))
3997 }
else if (isCppAttribute(IsCpp, *
Next)) {
4001 }
else if (
Next->is(tok::l_paren)) {
4010 if (!
Next ||
Next->isNot(tok::l_paren) || !
Next->MatchingParen)
4012 ClosingParen =
Next->MatchingParen;
4013 assert(ClosingParen->
is(tok::r_paren));
4015 if (
Line.Last->is(tok::l_brace))
4017 if (
Next->Next == ClosingParen)
4020 if (ClosingParen->
Next && ClosingParen->
Next->
is(TT_PointerOrReference))
4033 if (IsCpp &&
Next->Next &&
Next->Next->is(tok::identifier) &&
4034 !
Line.endsWith(tok::semi)) {
4040 if (
Tok->is(TT_TypeDeclarationParen))
4042 if (
Tok->isOneOf(tok::l_paren, TT_TemplateOpener) &&
Tok->MatchingParen) {
4043 Tok =
Tok->MatchingParen;
4046 if (
Tok->is(tok::kw_const) ||
Tok->isTypeName(LangOpts) ||
4047 Tok->isOneOf(TT_PointerOrReference, TT_StartOfName, tok::ellipsis)) {
4050 if (
Tok->isOneOf(tok::l_brace, TT_ObjCMethodExpr) ||
Tok->Tok.isLiteral())
4056bool TokenAnnotator::mustBreakForReturnType(
const AnnotatedLine &
Line)
const {
4057 assert(
Line.MightBeFunctionDecl);
4059 if ((Style.BreakAfterReturnType == FormatStyle::RTBS_TopLevel ||
4060 Style.BreakAfterReturnType == FormatStyle::RTBS_TopLevelDefinitions) &&
4065 switch (Style.BreakAfterReturnType) {
4066 case FormatStyle::RTBS_None:
4067 case FormatStyle::RTBS_Automatic:
4068 case FormatStyle::RTBS_ExceptShortType:
4070 case FormatStyle::RTBS_All:
4071 case FormatStyle::RTBS_TopLevel:
4073 case FormatStyle::RTBS_AllDefinitions:
4074 case FormatStyle::RTBS_TopLevelDefinitions:
4075 return Line.mightBeFunctionDefinition();
4081bool TokenAnnotator::mustBreakBeforeReturnType(
4083 assert(
Line.MightBeFunctionDecl);
4085 switch (Style.BreakBeforeReturnType) {
4086 case FormatStyle::BBRTS_None:
4088 case FormatStyle::BBRTS_All:
4090 case FormatStyle::BBRTS_TopLevel:
4091 return Line.Level == 0;
4092 case FormatStyle::BBRTS_AllDefinitions:
4093 return Line.mightBeFunctionDefinition();
4094 case FormatStyle::BBRTS_TopLevelDefinitions:
4095 return Line.Level == 0 &&
Line.mightBeFunctionDefinition();
4102 auto *
Tok =
Line.getFirstNonComment();
4106 if (
Tok->is(tok::kw_template)) {
4107 auto *Opener =
Tok->Next;
4108 while (Opener && Opener->isNot(TT_TemplateOpener))
4109 Opener = Opener->Next;
4110 if (!Opener || !Opener->MatchingParen)
4112 Tok = Opener->MatchingParen->Next;
4115 if (
Tok &&
Tok->is(TT_RequiresClause)) {
4116 while (
Tok && !
Tok->ClosesRequiresClause)
4124 Tok->isOneOf(tok::kw___attribute, tok::kw___declspec,
4125 TT_AttributeMacro)) {
4127 if (
Next &&
Next->is(tok::l_paren) &&
Next->MatchingParen)
4128 Tok =
Next->MatchingParen->Next;
4133 if (
Tok->is(TT_AttributeLSquare) &&
Tok->MatchingParen) {
4134 Tok =
Tok->MatchingParen->Next;
4146 Line.Computed =
true;
4154 :
Line.FirstStartColumn +
First->ColumnWidth;
4155 bool AlignArrayOfStructures =
4156 (Style.AlignArrayOfStructures != FormatStyle::AIAS_None &&
4158 if (AlignArrayOfStructures)
4159 calculateArrayInitializerColumnList(
Line);
4161 const auto *FirstNonComment =
Line.getFirstNonComment();
4162 bool SeenName =
false;
4163 bool LineIsFunctionDeclaration =
false;
4167 for (
auto *
Tok = FirstNonComment && FirstNonComment->isNot(tok::kw_using)
4168 ? FirstNonComment->Next
4171 if (
Tok->is(TT_StartOfName))
4173 if (
Tok->Previous->EndsCppAttributeGroup)
4174 AfterLastAttribute =
Tok;
4175 if (
const bool IsCtorOrDtor =
Tok->is(TT_CtorDtorDeclName);
4179 Tok->setFinalizedType(TT_FunctionDeclarationName);
4180 LineIsFunctionDeclaration =
true;
4184 assert(OpeningParen);
4185 if (OpeningParen->is(TT_Unknown))
4186 OpeningParen->setType(TT_FunctionDeclarationLParen);
4193 if ((LineIsFunctionDeclaration ||
4194 (FirstNonComment && FirstNonComment->is(TT_CtorDtorDeclName))) &&
4195 Line.endsWith(tok::semi, tok::r_brace)) {
4196 auto *
Tok =
Line.Last->Previous;
4197 while (
Tok->isNot(tok::r_brace))
4199 if (
auto *LBrace =
Tok->MatchingParen; LBrace && LBrace->is(TT_Unknown)) {
4200 assert(LBrace->is(tok::l_brace));
4203 LBrace->setFinalizedType(TT_FunctionLBrace);
4207 if (SeenName && AfterLastAttribute &&
4210 if (LineIsFunctionDeclaration)
4211 Line.ReturnTypeWrapped =
true;
4214 if (!LineIsFunctionDeclaration) {
4215 Line.ReturnTypeWrapped =
false;
4217 for (
const auto *
Tok = FirstNonComment;
Tok;
Tok =
Tok->Next) {
4218 if (
Tok->isNot(tok::kw_operator))
4222 }
while (
Tok &&
Tok->isNot(TT_OverloadedOperatorLParen));
4223 if (!
Tok || !
Tok->MatchingParen)
4225 const auto *LeftParen =
Tok;
4226 for (
Tok =
Tok->Next;
Tok &&
Tok != LeftParen->MatchingParen;
4228 if (
Tok->isNot(tok::identifier))
4231 const bool NextIsBinaryOperator =
4233 Next->Next->is(tok::identifier);
4234 if (!NextIsBinaryOperator)
4236 Next->setType(TT_BinaryOperator);
4240 }
else if (ClosingParen) {
4242 if (
Tok->is(TT_CtorInitializerColon))
4244 if (
Tok->is(tok::arrow)) {
4245 Tok->overwriteFixedType(TT_TrailingReturnArrow);
4248 if (
Tok->isNot(TT_TrailingAnnotation))
4251 if (!
Next ||
Next->isNot(tok::l_paren))
4260 if (
Line.MightBeFunctionDecl && LineIsFunctionDeclaration &&
4261 mustBreakBeforeReturnType(
Line)) {
4263 ReturnTypeStart && ReturnTypeStart != FirstNonComment &&
4264 ReturnTypeStart->
isNoneOf(TT_FunctionDeclarationName,
4265 TT_CtorDtorDeclName, tok::tilde)) {
4266 ReturnTypeStart->MustBreakBefore =
true;
4267 Line.ReturnTypeWrapped =
true;
4271 if (
First->is(TT_ElseLBrace)) {
4272 First->CanBreakBefore =
true;
4273 First->MustBreakBefore =
true;
4276 bool InFunctionDecl =
Line.MightBeFunctionDecl;
4277 bool InParameterList =
false;
4278 for (
auto *Current =
First->Next; Current; Current = Current->Next) {
4280 if (Current->is(TT_LineComment)) {
4282 Current->SpacesRequiredBefore =
4283 (Style.Cpp11BracedListStyle == FormatStyle::BLS_AlignFirstComment &&
4284 !Style.SpacesInParensOptions.Other)
4287 }
else if (Prev->
is(TT_VerilogMultiLineListLParen)) {
4288 Current->SpacesRequiredBefore = 0;
4290 Current->SpacesRequiredBefore = Style.SpacesBeforeTrailingComments;
4300 if (!Current->HasUnescapedNewline) {
4303 if (
Parameter->isOneOf(tok::comment, tok::r_brace))
4306 if (
Parameter->Previous->isNot(TT_CtorInitializerComma) &&
4314 }
else if (!Current->Finalized && Current->SpacesRequiredBefore == 0 &&
4315 spaceRequiredBefore(
Line, *Current)) {
4316 Current->SpacesRequiredBefore = 1;
4321 Current->MustBreakBefore =
true;
4323 Current->MustBreakBefore =
4324 Current->MustBreakBefore || mustBreakBefore(
Line, *Current);
4325 if (!Current->MustBreakBefore && InFunctionDecl &&
4326 Current->is(TT_FunctionDeclarationName)) {
4327 Current->MustBreakBefore = mustBreakForReturnType(
Line);
4331 Current->CanBreakBefore =
4332 !
Line.IsModuleOrImportDecl &&
4333 (Current->MustBreakBefore || canBreakBefore(
Line, *Current));
4335 if (Current->is(TT_FunctionDeclarationLParen)) {
4336 InParameterList =
true;
4337 }
else if (Current->is(tok::r_paren)) {
4338 const auto *LParen = Current->MatchingParen;
4339 if (LParen && LParen->is(TT_FunctionDeclarationLParen))
4340 InParameterList =
false;
4341 }
else if (InParameterList &&
4342 Current->endsSequence(TT_AttributeMacro,
4343 TT_PointerOrReference)) {
4344 Current->CanBreakBefore =
false;
4347 unsigned ChildSize = 0;
4350 ChildSize = LastOfChild.isTrailingComment() ? Style.ColumnLimit
4353 if (Current->MustBreakBefore || Prev->
Children.size() > 1 ||
4355 Prev->
Children[0]->First->MustBreakBefore) ||
4356 Current->IsMultiline) {
4357 Current->TotalLength = Prev->
TotalLength + Style.ColumnLimit;
4359 Current->TotalLength = Prev->
TotalLength + Current->ColumnWidth +
4360 ChildSize + Current->SpacesRequiredBefore;
4363 if ((Style.PackParameters.BinPack == FormatStyle::BPPS_UseBreakAfter &&
4366 (Style.PackArguments.BinPack == FormatStyle::BPAS_UseBreakAfter &&
4368 Prev->
isOneOf(tok::l_paren, tok::l_brace,
4369 TT_ArrayInitializerLSquare) &&
4372 for (
auto *ParamTok = Current; ParamTok && ParamTok != RParen;
4373 ParamTok = ParamTok->Next) {
4374 if (ParamTok->opensScope()) {
4375 ParamTok = ParamTok->MatchingParen;
4380 ParamTok->MustBreakBefore =
true;
4381 ParamTok->CanBreakBefore =
true;
4386 if (Current->is(TT_ControlStatementLBrace)) {
4387 if (Style.ColumnLimit > 0 &&
4388 Style.BraceWrapping.AfterControlStatement ==
4389 FormatStyle::BWACS_MultiLine &&
4390 Line.Level * Style.IndentWidth +
Line.Last->TotalLength >
4391 Style.ColumnLimit) {
4392 Current->CanBreakBefore =
true;
4393 Current->MustBreakBefore =
true;
4395 }
else if (Current->is(TT_CtorInitializerColon)) {
4396 InFunctionDecl =
false;
4408 Current->SplitPenalty = splitPenalty(
Line, *Current, InFunctionDecl);
4409 if (Style.Language == FormatStyle::LK_ObjC &&
4410 Current->is(TT_SelectorName) && Current->ParameterIndex > 0) {
4411 if (Current->ParameterIndex == 1)
4412 Current->SplitPenalty += 5 * Current->BindingStrength;
4414 Current->SplitPenalty += 20 * Current->BindingStrength;
4418 calculateUnbreakableTailLengths(
Line);
4420 for (
auto *Current =
First; Current; Current = Current->Next) {
4422 Current->Role->precomputeFormattingInfos(Current);
4423 if (Current->MatchingParen &&
4424 Current->MatchingParen->opensBlockOrBlockTypeList(Style) &&
4429 if (Current->opensBlockOrBlockTypeList(Style))
4433 LLVM_DEBUG({ printDebugInfo(
Line); });
4436void TokenAnnotator::calculateUnbreakableTailLengths(
4443 Current->
isOneOf(tok::comment, tok::string_literal)) {
4453void TokenAnnotator::calculateArrayInitializerColumnList(
4457 auto *CurrentToken =
Line.First;
4458 CurrentToken->ArrayInitializerLineStart =
true;
4460 while (CurrentToken && CurrentToken !=
Line.Last) {
4461 if (CurrentToken->is(tok::l_brace)) {
4462 CurrentToken->IsArrayInitializer =
true;
4463 if (CurrentToken->Next)
4464 CurrentToken->Next->MustBreakBefore =
true;
4466 calculateInitializerColumnList(
Line, CurrentToken->Next, Depth + 1);
4468 CurrentToken = CurrentToken->Next;
4473FormatToken *TokenAnnotator::calculateInitializerColumnList(
4475 while (CurrentToken && CurrentToken !=
Line.Last) {
4476 if (CurrentToken->is(tok::l_brace))
4478 else if (CurrentToken->is(tok::r_brace))
4480 if (Depth == 2 && CurrentToken->isOneOf(tok::l_brace, tok::comma)) {
4481 CurrentToken = CurrentToken->Next;
4484 CurrentToken->StartsColumn =
true;
4485 CurrentToken = CurrentToken->Previous;
4487 CurrentToken = CurrentToken->Next;
4489 return CurrentToken;
4494 bool InFunctionDecl)
const {
4498 if (
Left.is(tok::semi))
4502 if (Style.isJava()) {
4503 if (
Right.isOneOf(Keywords.kw_extends, Keywords.kw_throws))
4505 if (
Right.is(Keywords.kw_implements))
4507 if (
Left.is(tok::comma) &&
Left.NestingLevel == 0)
4509 }
else if (Style.isJavaScript()) {
4510 if (
Right.is(Keywords.kw_function) &&
Left.isNot(tok::comma))
4512 if (
Left.is(TT_JsTypeColon))
4514 if ((
Left.is(TT_TemplateString) &&
Left.TokenText.ends_with(
"${")) ||
4515 (
Right.is(TT_TemplateString) &&
Right.TokenText.starts_with(
"}"))) {
4519 if (
Left.opensScope() &&
Right.closesScope())
4521 }
else if (Style.Language == FormatStyle::LK_Proto) {
4522 if (
Right.is(tok::l_square))
4524 if (
Right.is(tok::period))
4528 if (
Right.is(tok::identifier) &&
Right.Next &&
Right.Next->is(TT_DictLiteral))
4530 if (
Right.is(tok::l_square)) {
4531 if (
Left.is(tok::r_square))
4534 if (
Right.is(TT_LambdaLSquare) &&
Left.is(tok::equal))
4536 if (
Right.isNoneOf(TT_ObjCMethodExpr, TT_LambdaLSquare,
4537 TT_ArrayInitializerLSquare,
4538 TT_DesignatedInitializerLSquare, TT_AttributeLSquare)) {
4543 if (
Left.is(tok::coloncolon))
4544 return Style.PenaltyBreakScopeResolution;
4545 if (
Right.isOneOf(TT_StartOfName, TT_FunctionDeclarationName,
4546 tok::kw_operator)) {
4547 if (
Line.startsWith(tok::kw_for) &&
Right.PartOfMultiVariableDeclStmt)
4549 if (
Left.is(TT_StartOfName))
4551 if (InFunctionDecl &&
Right.NestingLevel == 0)
4552 return Style.PenaltyReturnTypeOnItsOwnLine;
4555 if (
Right.is(TT_PointerOrReference))
4557 if (
Right.is(TT_LambdaArrow))
4559 if (
Left.is(tok::equal) &&
Right.is(tok::l_brace))
4561 if (
Left.is(TT_CastRParen))
4563 if (
Left.isOneOf(tok::kw_class, tok::kw_struct, tok::kw_union))
4565 if (
Left.is(tok::comment))
4568 if (
Left.isOneOf(TT_RangeBasedForLoopColon, TT_InheritanceColon,
4569 TT_CtorInitializerColon)) {
4573 if (
Right.isMemberAccess()) {
4594 const auto Penalty = Style.PenaltyBreakBeforeMemberAccess;
4596 ? std::min(Penalty, 35u)
4600 if (
Right.is(TT_TrailingAnnotation) &&
4601 (!
Right.Next ||
Right.Next->isNot(tok::l_paren))) {
4604 if (
Line.startsWith(TT_ObjCMethodSpecifier))
4611 bool is_short_annotation =
Right.TokenText.size() < 10;
4612 return (
Left.is(tok::r_paren) ? 100 : 120) + (is_short_annotation ? 50 : 0);
4616 if (
Line.startsWith(tok::kw_for) &&
Left.is(tok::equal))
4621 if (
Right.is(TT_SelectorName))
4623 if (
Left.is(tok::colon)) {
4624 if (
Left.is(TT_ObjCMethodExpr))
4625 return Line.MightBeFunctionDecl ? 50 : 500;
4626 if (
Left.is(TT_ObjCSelector))
4634 Left.Previous->isOneOf(tok::identifier, tok::greater)) {
4638 if (
Left.is(tok::l_paren) && Style.PenaltyBreakOpenParenthesis != 0)
4639 return Style.PenaltyBreakOpenParenthesis;
4640 if (
Left.is(tok::l_paren) && InFunctionDecl && Style.AlignAfterOpenBracket)
4642 if (
Left.is(tok::l_paren) &&
Left.Previous &&
4643 (
Left.Previous->isOneOf(tok::kw_for, tok::kw__Generic) ||
4644 Left.Previous->isIf())) {
4647 if (
Left.is(tok::equal) && InFunctionDecl)
4649 if (
Right.is(tok::r_brace))
4651 if (
Left.is(TT_TemplateOpener))
4653 if (
Left.opensScope()) {
4657 if (!Style.AlignAfterOpenBracket &&
4658 (
Left.ParameterCount <= 1 || Style.AllowAllArgumentsOnNextLine)) {
4661 if (
Left.is(tok::l_brace) &&
4662 Style.Cpp11BracedListStyle == FormatStyle::BLS_Block) {
4665 return Left.ParameterCount > 1 ? Style.PenaltyBreakBeforeFirstCallParameter
4668 if (
Left.is(TT_JavaAnnotation))
4671 if (
Left.is(TT_UnaryOperator))
4673 if (
Left.isOneOf(tok::plus, tok::comma) &&
Left.Previous &&
4674 Left.Previous->isLabelString() &&
4675 (
Left.NextOperator ||
Left.OperatorIndex != 0)) {
4678 if (
Right.is(tok::plus) &&
Left.isLabelString() &&
4679 (
Right.NextOperator ||
Right.OperatorIndex != 0)) {
4682 if (
Left.is(tok::comma))
4684 if (
Right.is(tok::lessless) &&
Left.isLabelString() &&
4685 (
Right.NextOperator ||
Right.OperatorIndex != 1)) {
4688 if (
Right.is(tok::lessless)) {
4690 if (
Left.isNot(tok::r_paren) ||
Right.OperatorIndex > 0) {
4696 if (
Left.ClosesTemplateDeclaration)
4697 return Style.PenaltyBreakTemplateDeclaration;
4698 if (
Left.ClosesRequiresClause)
4700 if (
Left.is(TT_ConditionalExpr))
4706 return Style.PenaltyBreakAssignment;
4713bool TokenAnnotator::spaceRequiredBeforeParens(
const FormatToken &Right)
const {
4714 if (Style.SpaceBeforeParens == FormatStyle::SBPO_Always)
4716 if (
Right.is(TT_OverloadedOperatorLParen) &&
4717 Style.SpaceBeforeParensOptions.AfterOverloadedOperator) {
4720 if (Style.SpaceBeforeParensOptions.BeforeNonEmptyParentheses &&
4721 Right.ParameterCount > 0) {
4730 if (
Left.is(tok::kw_return) &&
4731 Right.isNoneOf(tok::semi, tok::r_paren, tok::hashhash)) {
4734 if (
Left.is(tok::kw_throw) &&
Right.is(tok::l_paren) &&
Right.MatchingParen &&
4735 Right.MatchingParen->is(TT_CastRParen)) {
4738 if (
Left.is(Keywords.kw_assert) && Style.isJava())
4741 Left.is(tok::objc_property)) {
4744 if (
Right.is(tok::hashhash))
4745 return Left.is(tok::hash);
4746 if (
Left.isOneOf(tok::hashhash, tok::hash))
4747 return Right.is(tok::hash);
4748 if (Style.SpacesInParens == FormatStyle::SIPO_Custom) {
4749 if (
Left.is(tok::l_paren) &&
Right.is(tok::r_paren))
4750 return Style.SpacesInParensOptions.InEmptyParentheses;
4751 if (Style.SpacesInParensOptions.ExceptDoubleParentheses &&
4752 Left.is(tok::r_paren) &&
Right.is(tok::r_paren)) {
4753 auto *InnerLParen =
Left.MatchingParen;
4754 if (InnerLParen && InnerLParen->Previous ==
Right.MatchingParen) {
4755 InnerLParen->SpacesRequiredBefore = 0;
4760 if (
Left.is(tok::l_paren))
4762 else if (
Right.is(tok::r_paren) &&
Right.MatchingParen)
4763 LeftParen =
Right.MatchingParen;
4764 if (LeftParen && (LeftParen->is(TT_ConditionLParen) ||
4765 (LeftParen->Previous &&
4766 isKeywordWithCondition(*LeftParen->Previous)))) {
4767 return Style.SpacesInParensOptions.InConditionalStatements;
4772 if (
Left.is(tok::kw_auto) &&
Right.isOneOf(TT_LambdaLBrace, TT_FunctionLBrace,
4774 TT_FunctionTypeLParen)) {
4779 if (
Left.is(tok::kw_auto) &&
Right.isOneOf(tok::l_paren, tok::l_brace))
4782 const auto *BeforeLeft =
Left.Previous;
4785 if (
Right.is(tok::l_paren) &&
Left.is(tok::kw_co_await) && BeforeLeft &&
4786 BeforeLeft->is(tok::kw_operator)) {
4790 if (
Left.isOneOf(tok::kw_co_await, tok::kw_co_yield, tok::kw_co_return) &&
4791 Right.isNoneOf(tok::semi, tok::r_paren)) {
4795 if (
Left.is(tok::l_paren) ||
Right.is(tok::r_paren)) {
4796 return (
Right.is(TT_CastRParen) ||
4797 (
Left.MatchingParen &&
Left.MatchingParen->is(TT_CastRParen)))
4798 ? Style.SpacesInParensOptions.InCStyleCasts
4799 : Style.SpacesInParensOptions.Other;
4801 if (
Right.isOneOf(tok::semi, tok::comma))
4804 bool IsLightweightGeneric =
Right.MatchingParen &&
4805 Right.MatchingParen->Next &&
4806 Right.MatchingParen->Next->is(tok::colon);
4807 return !IsLightweightGeneric && Style.ObjCSpaceBeforeProtocolList;
4809 if (
Right.is(tok::less) &&
Left.is(tok::kw_template))
4810 return Style.SpaceAfterTemplateKeyword;
4811 if (
Left.isOneOf(tok::exclaim, tok::tilde))
4813 if (
Left.is(tok::at) &&
4814 Right.isOneOf(tok::identifier, tok::string_literal, tok::char_constant,
4815 tok::numeric_constant, tok::l_paren, tok::l_brace,
4816 tok::kw_true, tok::kw_false)) {
4819 if (
Left.is(tok::colon))
4820 return Left.isNoneOf(TT_ObjCSelector, TT_ObjCMethodExpr);
4821 if (
Left.is(tok::coloncolon))
4823 if (
Left.is(tok::less) ||
Right.isOneOf(tok::greater, tok::less)) {
4824 if (Style.isTextProto() ||
4825 (Style.Language == FormatStyle::LK_Proto &&
4826 (
Left.is(TT_DictLiteral) ||
Right.is(TT_DictLiteral)))) {
4828 if (
Left.is(tok::less) &&
Right.is(tok::greater))
4830 return Style.Cpp11BracedListStyle == FormatStyle::BLS_Block;
4833 if (
Right.isNot(TT_OverloadedOperatorLParen))
4836 if (
Right.is(tok::ellipsis)) {
4837 return Left.Tok.isLiteral() || (
Left.is(tok::identifier) && BeforeLeft &&
4838 BeforeLeft->is(tok::kw_case));
4840 if (
Left.is(tok::l_square) &&
Right.is(tok::amp))
4841 return Style.SpacesInSquareBrackets;
4842 if (
Right.is(TT_PointerOrReference)) {
4843 if (
Left.is(tok::r_paren) &&
Line.MightBeFunctionDecl) {
4844 if (!
Left.MatchingParen)
4847 Left.MatchingParen->getPreviousNonComment();
4848 if (!TokenBeforeMatchingParen ||
Left.isNot(TT_TypeDeclarationParen))
4854 if ((Style.SpaceAroundPointerQualifiers == FormatStyle::SAPQ_After ||
4855 Style.SpaceAroundPointerQualifiers == FormatStyle::SAPQ_Both) &&
4856 (
Left.is(TT_AttributeRParen) ||
4857 Left.canBePointerOrReferenceQualifier())) {
4860 if (
Left.Tok.isLiteral())
4863 if (
Left.isTypeOrIdentifier(LangOpts) &&
Right.Next &&
Right.Next->Next &&
4864 Right.Next->Next->is(TT_RangeBasedForLoopColon)) {
4865 return getTokenPointerOrReferenceAlignment(Right) !=
4866 FormatStyle::PAS_Left;
4868 return Left.isNoneOf(TT_PointerOrReference, tok::l_paren) &&
4869 (getTokenPointerOrReferenceAlignment(Right) !=
4870 FormatStyle::PAS_Left ||
4871 (
Line.IsMultiVariableDeclStmt &&
4872 (
Left.NestingLevel == 0 ||
4873 (
Left.NestingLevel == 1 && startsWithInitStatement(
Line)))));
4875 if (
Right.is(TT_FunctionTypeLParen) &&
Left.isNot(tok::l_paren) &&
4876 (
Left.isNot(TT_PointerOrReference) ||
4877 (getTokenPointerOrReferenceAlignment(Left) != FormatStyle::PAS_Right &&
4878 !
Line.IsMultiVariableDeclStmt))) {
4881 if (
Left.is(TT_PointerOrReference)) {
4884 if ((Style.SpaceAroundPointerQualifiers == FormatStyle::SAPQ_Before ||
4885 Style.SpaceAroundPointerQualifiers == FormatStyle::SAPQ_Both) &&
4886 Right.canBePointerOrReferenceQualifier()) {
4890 if (
Right.Tok.isLiteral())
4893 if (
Right.is(TT_BlockComment))
4897 if (
Right.isOneOf(Keywords.kw_override, Keywords.kw_final, tok::kw_noexcept,
4898 TT_RequiresClause) &&
4899 Right.isNot(TT_StartOfName)) {
4906 if (BeforeLeft && BeforeLeft->isTypeOrIdentifier(LangOpts) &&
Right.Next &&
4907 Right.Next->is(TT_RangeBasedForLoopColon)) {
4908 return getTokenPointerOrReferenceAlignment(Left) !=
4909 FormatStyle::PAS_Right;
4911 if (
Right.isOneOf(TT_PointerOrReference, TT_ArraySubscriptLSquare,
4915 if (getTokenPointerOrReferenceAlignment(Left) == FormatStyle::PAS_Right)
4923 if (
Line.IsMultiVariableDeclStmt &&
4924 (
Left.NestingLevel ==
Line.First->NestingLevel ||
4925 ((
Left.NestingLevel ==
Line.First->NestingLevel + 1) &&
4926 startsWithInitStatement(
Line)))) {
4931 if (BeforeLeft->is(tok::coloncolon)) {
4932 if (
Left.isNot(tok::star))
4934 assert(Style.PointerAlignment != FormatStyle::PAS_Right);
4935 if (!
Right.startsSequence(tok::identifier, tok::r_paren))
4938 const auto *LParen =
Right.Next->MatchingParen;
4939 return !LParen || LParen->isNot(TT_FunctionTypeLParen);
4941 return BeforeLeft->isNoneOf(tok::l_paren, tok::l_square);
4944 if (
Left.is(tok::ellipsis) && BeforeLeft &&
4945 BeforeLeft->isPointerOrReference()) {
4946 return Style.PointerAlignment != FormatStyle::PAS_Right;
4949 if (
Right.is(tok::star) &&
Left.is(tok::l_paren))
4951 if (
Left.is(tok::star) &&
Right.isPointerOrReference())
4953 if (
Right.isPointerOrReference()) {
4964 if (
Previous->is(tok::coloncolon)) {
4983 if (
Previous->endsSequence(tok::kw_operator))
4984 return Style.PointerAlignment != FormatStyle::PAS_Left;
4985 if (
Previous->isOneOf(tok::kw_const, tok::kw_volatile)) {
4986 return (Style.PointerAlignment != FormatStyle::PAS_Left) ||
4987 (Style.SpaceAroundPointerQualifiers ==
4988 FormatStyle::SAPQ_After) ||
4989 (Style.SpaceAroundPointerQualifiers == FormatStyle::SAPQ_Both);
4993 if (Style.isCSharp() &&
Left.is(Keywords.kw_is) &&
Right.is(tok::l_square))
4995 const auto SpaceRequiredForArrayInitializerLSquare =
4996 [](
const FormatToken &LSquareTok,
const FormatStyle &Style) {
4997 return Style.SpacesInContainerLiterals ||
4999 Style.Cpp11BracedListStyle == FormatStyle::BLS_Block &&
5000 LSquareTok.endsSequence(tok::l_square, tok::colon,
5003 if (
Left.is(tok::l_square)) {
5004 return (
Left.is(TT_ArrayInitializerLSquare) &&
Right.isNot(tok::r_square) &&
5005 SpaceRequiredForArrayInitializerLSquare(Left, Style)) ||
5006 (
Left.isOneOf(TT_ArraySubscriptLSquare, TT_StructuredBindingLSquare,
5007 TT_LambdaLSquare) &&
5008 Style.SpacesInSquareBrackets &&
Right.isNot(tok::r_square));
5010 if (
Right.is(tok::r_square)) {
5011 return Right.MatchingParen &&
5012 ((
Right.MatchingParen->is(TT_ArrayInitializerLSquare) &&
5013 SpaceRequiredForArrayInitializerLSquare(*
Right.MatchingParen,
5015 (Style.SpacesInSquareBrackets &&
5016 Right.MatchingParen->isOneOf(TT_ArraySubscriptLSquare,
5017 TT_StructuredBindingLSquare,
5018 TT_LambdaLSquare)));
5020 if (
Right.is(tok::l_square) &&
5021 Right.isNoneOf(TT_ObjCMethodExpr, TT_LambdaLSquare,
5022 TT_DesignatedInitializerLSquare,
5023 TT_StructuredBindingLSquare, TT_AttributeLSquare) &&
5024 Left.isNoneOf(tok::numeric_constant, TT_DictLiteral) &&
5025 !(
Left.isNot(tok::r_square) && Style.SpaceBeforeSquareBrackets &&
5026 Right.is(TT_ArraySubscriptLSquare))) {
5030 (
Right.is(tok::r_brace) &&
Right.MatchingParen &&
5032 return Style.Cpp11BracedListStyle == FormatStyle::BLS_Block ||
5033 Style.SpacesInParensOptions.Other;
5035 if (
Left.is(TT_BlockComment)) {
5037 return Style.isJavaScript() || !
Left.TokenText.ends_with(
"=*/");
5042 if (
Left.is(TT_TemplateCloser) &&
Right.is(TT_AttributeLSquare))
5045 if (
Right.is(tok::l_paren)) {
5047 if (
Line.MightBeFunctionDecl &&
Right.is(TT_FunctionDeclarationLParen)) {
5048 if (spaceRequiredBeforeParens(Right))
5050 const auto &Options = Style.SpaceBeforeParensOptions;
5051 return Line.mightBeFunctionDefinition()
5052 ? Options.AfterFunctionDefinitionName
5053 : Options.AfterFunctionDeclarationName;
5055 if (
Left.is(TT_TemplateCloser) &&
Right.isNot(TT_FunctionTypeLParen))
5056 return spaceRequiredBeforeParens(Right);
5057 if (
Left.isOneOf(TT_RequiresClause,
5058 TT_RequiresClauseInARequiresExpression)) {
5059 return Style.SpaceBeforeParensOptions.AfterRequiresInClause ||
5060 spaceRequiredBeforeParens(Right);
5062 if (
Left.is(TT_RequiresExpression)) {
5063 return Style.SpaceBeforeParensOptions.AfterRequiresInExpression ||
5064 spaceRequiredBeforeParens(Right);
5066 if (
Left.isOneOf(TT_AttributeRParen, TT_AttributeRSquare))
5068 if (
Left.is(TT_ForEachMacro)) {
5069 return Style.SpaceBeforeParensOptions.AfterForeachMacros ||
5070 spaceRequiredBeforeParens(Right);
5072 if (
Left.is(TT_IfMacro)) {
5073 return Style.SpaceBeforeParensOptions.AfterIfMacros ||
5074 spaceRequiredBeforeParens(Right);
5076 if (Style.SpaceBeforeParens == FormatStyle::SBPO_Custom &&
5077 Left.isPlacementOperator() &&
5078 Right.isNot(TT_OverloadedOperatorLParen) &&
5079 !(
Line.MightBeFunctionDecl &&
Left.is(TT_FunctionDeclarationName))) {
5080 const auto *RParen =
Right.MatchingParen;
5081 return Style.SpaceBeforeParensOptions.AfterPlacementOperator ||
5082 (RParen && RParen->is(TT_CastRParen));
5086 if (
Left.is(tok::semi))
5088 if (
Left.isOneOf(tok::pp_elif, tok::kw_for, tok::kw_while, tok::kw_switch,
5089 tok::kw_case, TT_ForEachMacro, TT_ObjCForIn) ||
5091 Right.is(TT_ConditionLParen)) {
5092 return Style.SpaceBeforeParensOptions.AfterControlStatements ||
5093 spaceRequiredBeforeParens(Right);
5098 if (
Right.is(TT_OverloadedOperatorLParen))
5099 return spaceRequiredBeforeParens(Right);
5103 Left.MatchingParen &&
Left.MatchingParen->is(TT_LambdaLSquare)) {
5104 return Style.SpaceBeforeParensOptions.AfterFunctionDefinitionName ||
5105 spaceRequiredBeforeParens(Right);
5107 if (!BeforeLeft || BeforeLeft->isNoneOf(tok::period, tok::arrow)) {
5108 if (
Left.isOneOf(tok::kw_try, Keywords.kw___except, tok::kw_catch)) {
5109 return Style.SpaceBeforeParensOptions.AfterControlStatements ||
5110 spaceRequiredBeforeParens(Right);
5112 if (
Left.isPlacementOperator() ||
5113 (
Left.is(tok::r_square) &&
Left.MatchingParen &&
5114 Left.MatchingParen->Previous &&
5115 Left.MatchingParen->Previous->is(tok::kw_delete))) {
5116 return Style.SpaceBeforeParens != FormatStyle::SBPO_Never ||
5117 spaceRequiredBeforeParens(Right);
5121 if (
Tok.isNot(tok::l_paren))
5123 const auto *RParen =
Tok.MatchingParen;
5126 const auto *
Next = RParen->Next;
5133 (
Left.Tok.getIdentifierInfo() ||
Left.is(tok::r_paren))) {
5134 return spaceRequiredBeforeParens(Right);
5138 if (
Left.is(tok::at) &&
Right.isNot(tok::objc_not_keyword))
5140 if (
Right.is(TT_UnaryOperator)) {
5141 return Left.isNoneOf(tok::l_paren, tok::l_square, tok::at) &&
5142 (
Left.isNot(tok::colon) ||
Left.isNot(TT_ObjCMethodExpr));
5148 if (!Style.isVerilog() &&
5149 (
Left.isOneOf(tok::identifier, tok::greater, tok::r_square,
5151 Left.isTypeName(LangOpts)) &&
5152 Right.is(tok::l_brace) &&
Right.getNextNonComment() &&
5156 if (
Left.is(tok::period) ||
Right.is(tok::period))
5160 if (
Right.is(tok::hash) &&
Left.is(tok::identifier) &&
5161 (
Left.TokenText ==
"L" ||
Left.TokenText ==
"u" ||
5162 Left.TokenText ==
"U" ||
Left.TokenText ==
"u8" ||
5163 Left.TokenText ==
"LR" ||
Left.TokenText ==
"uR" ||
5164 Left.TokenText ==
"UR" ||
Left.TokenText ==
"u8R")) {
5167 if (
Left.is(TT_TemplateCloser) &&
Left.MatchingParen &&
5168 Left.MatchingParen->Previous &&
5169 Left.MatchingParen->Previous->isOneOf(tok::period, tok::coloncolon)) {
5175 if (
Left.is(TT_TemplateCloser) &&
Right.is(tok::l_square))
5177 if (
Left.is(tok::l_brace) &&
Left.endsSequence(TT_DictLiteral, tok::at)) {
5181 if (
Right.is(tok::r_brace) &&
Right.MatchingParen &&
5182 Right.MatchingParen->endsSequence(TT_DictLiteral, tok::at)) {
5186 if (
Right.is(TT_TrailingAnnotation) &&
Right.isOneOf(tok::amp, tok::ampamp) &&
5187 Left.isOneOf(tok::kw_const, tok::kw_volatile) &&
5188 (!
Right.Next ||
Right.Next->is(tok::semi))) {
5192 return getTokenReferenceAlignment(Right) != FormatStyle::PAS_Left;
5205 return Right.hasWhitespaceBefore();
5207 const bool IsVerilog = Style.isVerilog();
5208 assert(!IsVerilog || !IsCpp);
5211 if (Keywords.isWordLike(Right, IsVerilog) &&
5212 Keywords.isWordLike(Left, IsVerilog)) {
5218 if (
Left.is(tok::star) &&
Right.is(tok::comment))
5221 if (
Left.is(tok::l_brace) &&
Right.is(tok::r_brace) &&
5222 Left.Children.empty()) {
5224 return Style.SpaceInEmptyBraces != FormatStyle::SIEB_Never;
5225 if (Style.Cpp11BracedListStyle != FormatStyle::BLS_Block) {
5226 return Style.SpacesInParens == FormatStyle::SIPO_Custom &&
5227 Style.SpacesInParensOptions.InEmptyParentheses;
5229 return Style.SpaceInEmptyBraces == FormatStyle::SIEB_Always;
5232 const auto *BeforeLeft =
Left.Previous;
5235 if (
Left.is(TT_OverloadedOperator) &&
5236 Right.isOneOf(TT_TemplateOpener, TT_TemplateCloser)) {
5240 if (
Right.is(tok::period) &&
Left.is(tok::numeric_constant))
5244 if (
Left.is(Keywords.kw_import) &&
5245 Right.isOneOf(tok::less, tok::ellipsis) &&
5246 (!BeforeLeft || BeforeLeft->is(tok::kw_export))) {
5250 if (
Left.is(Keywords.kw_import) &&
Right.is(TT_ModulePartitionColon))
5253 if (
Right.is(TT_AfterPPDirective))
5257 if (
Left.is(tok::identifier) &&
Right.is(TT_ModulePartitionColon))
5260 if (
Left.is(TT_ModulePartitionColon) &&
Right.is(tok::identifier))
5262 if (
Left.is(tok::ellipsis) &&
Right.is(tok::identifier) &&
5263 Line.First->is(Keywords.kw_import)) {
5267 if (
Left.isOneOf(TT_AttributeRParen, TT_AttributeMacro) &&
5268 Right.is(tok::coloncolon)) {
5272 if (
Left.is(tok::kw_operator))
5273 return Right.is(tok::coloncolon) || Style.SpaceAfterOperatorKeyword;
5275 !
Left.opensScope() && Style.SpaceBeforeCpp11BracedList) {
5278 if (
Left.is(tok::less) &&
Left.is(TT_OverloadedOperator) &&
5279 Right.is(TT_TemplateOpener)) {
5283 if (
Left.is(tok::identifier) &&
Right.is(tok::numeric_constant))
5284 return Right.TokenText[0] !=
'.';
5286 if (
Left.Tok.getIdentifierInfo() &&
Right.Tok.isLiteral())
5288 }
else if (Style.isProto()) {
5289 if (
Right.is(tok::period) && !(BeforeLeft && BeforeLeft->is(tok::period)) &&
5290 Left.isOneOf(Keywords.kw_optional, Keywords.kw_required,
5291 Keywords.kw_repeated, Keywords.kw_extend)) {
5294 if (
Right.is(tok::l_paren) &&
5295 Left.isOneOf(Keywords.kw_returns, Keywords.kw_option)) {
5298 if (
Right.isOneOf(tok::l_brace, tok::less) &&
Left.is(TT_SelectorName))
5301 if (
Left.is(tok::slash) ||
Right.is(tok::slash))
5303 if (
Left.MatchingParen &&
5304 Left.MatchingParen->is(TT_ProtoExtensionLSquare) &&
5305 Right.isOneOf(tok::l_brace, tok::less)) {
5306 return Style.Cpp11BracedListStyle == FormatStyle::BLS_Block;
5309 if (
Left.is(tok::percent))
5313 if (
Left.is(tok::numeric_constant) &&
Right.is(tok::percent))
5314 return Right.hasWhitespaceBefore();
5315 }
else if (Style.isJson()) {
5316 if (
Right.is(tok::colon) &&
Left.is(tok::string_literal))
5317 return Style.SpaceBeforeJsonColon;
5318 }
else if (Style.isCSharp()) {
5324 if (
Left.is(tok::kw_this) &&
Right.is(tok::l_square))
5328 if (
Left.is(tok::kw_new) &&
Right.is(tok::l_paren))
5332 if (
Right.is(tok::l_brace))
5336 if (
Left.is(tok::l_brace) &&
Right.isNot(tok::r_brace))
5339 if (
Left.isNot(tok::l_brace) &&
Right.is(tok::r_brace))
5343 if (
Left.is(TT_FatArrow) ||
Right.is(TT_FatArrow))
5347 if (
Left.is(TT_AttributeColon) ||
Right.is(TT_AttributeColon))
5351 if (
Left.is(TT_TemplateCloser) &&
Right.is(TT_StartOfName))
5355 if (
Left.is(tok::l_square) ||
Right.is(tok::r_square))
5356 return Style.SpacesInSquareBrackets;
5359 if (
Right.is(TT_CSharpNullable))
5363 if (
Right.is(TT_NonNullAssertion))
5367 if (
Left.is(tok::comma) &&
Right.is(tok::comma))
5371 if (
Left.is(Keywords.kw_var) &&
Right.is(tok::l_paren))
5375 if (
Right.is(tok::l_paren)) {
5376 if (
Left.isOneOf(tok::kw_using, Keywords.kw_async, Keywords.kw_when,
5377 Keywords.kw_lock)) {
5378 return Style.SpaceBeforeParensOptions.AfterControlStatements ||
5379 spaceRequiredBeforeParens(Right);
5385 if ((
Left.isAccessSpecifierKeyword() ||
5386 Left.isOneOf(tok::kw_virtual, tok::kw_extern, tok::kw_static,
5387 Keywords.kw_internal, Keywords.kw_abstract,
5388 Keywords.kw_sealed, Keywords.kw_override,
5389 Keywords.kw_async, Keywords.kw_unsafe)) &&
5390 Right.is(tok::l_paren)) {
5393 }
else if (Style.isJavaScript()) {
5394 if (
Left.is(TT_FatArrow))
5397 if (
Right.is(tok::l_paren) &&
Left.is(Keywords.kw_await) && BeforeLeft &&
5398 BeforeLeft->is(tok::kw_for)) {
5401 if (
Left.is(Keywords.kw_async) &&
Right.is(tok::l_paren) &&
5402 Right.MatchingParen) {
5409 if ((
Left.is(TT_TemplateString) &&
Left.TokenText.ends_with(
"${")) ||
5410 (
Right.is(TT_TemplateString) &&
Right.TokenText.starts_with(
"}"))) {
5415 if (Keywords.isJavaScriptIdentifier(Left,
5417 Right.is(TT_TemplateString)) {
5420 if (
Right.is(tok::star) &&
5421 Left.isOneOf(Keywords.kw_function, Keywords.kw_yield)) {
5424 if (
Right.isOneOf(tok::l_brace, tok::l_square) &&
5425 Left.isOneOf(Keywords.kw_function, Keywords.kw_yield,
5426 Keywords.kw_extends, Keywords.kw_implements)) {
5429 if (
Right.is(tok::l_paren)) {
5431 if (
Line.MustBeDeclaration &&
Left.Tok.getIdentifierInfo())
5435 if (BeforeLeft && BeforeLeft->is(tok::period) &&
5436 Left.Tok.getIdentifierInfo()) {
5440 if (
Left.isOneOf(tok::kw_throw, Keywords.kw_await, Keywords.kw_typeof,
5446 if (
Left.endsSequence(tok::kw_const, Keywords.kw_as))
5448 if ((
Left.isOneOf(Keywords.kw_let, Keywords.kw_var, Keywords.kw_in,
5453 (
Left.is(Keywords.kw_of) && BeforeLeft &&
5454 BeforeLeft->isOneOf(tok::identifier, tok::r_square, tok::r_brace))) &&
5455 (!BeforeLeft || BeforeLeft->isNot(tok::period))) {
5458 if (
Left.isOneOf(tok::kw_for, Keywords.kw_as) && BeforeLeft &&
5459 BeforeLeft->is(tok::period) &&
Right.is(tok::l_paren)) {
5462 if (
Left.is(Keywords.kw_as) &&
5463 Right.isOneOf(tok::l_square, tok::l_brace, tok::l_paren)) {
5466 if (
Left.is(tok::kw_default) && BeforeLeft &&
5467 BeforeLeft->is(tok::kw_export)) {
5470 if (
Left.is(Keywords.kw_is) &&
Right.is(tok::l_brace))
5472 if (
Right.isOneOf(TT_JsTypeColon, TT_JsTypeOptionalQuestion))
5474 if (
Left.is(TT_JsTypeOperator) ||
Right.is(TT_JsTypeOperator))
5476 if ((
Left.is(tok::l_brace) ||
Right.is(tok::r_brace)) &&
5477 Line.First->isOneOf(Keywords.kw_import, tok::kw_export)) {
5480 if (
Left.is(tok::ellipsis))
5482 if (
Left.is(TT_TemplateCloser) &&
5483 Right.isNoneOf(tok::equal, tok::l_brace, tok::comma, tok::l_square,
5484 Keywords.kw_implements, Keywords.kw_extends)) {
5490 if (
Right.is(TT_NonNullAssertion))
5492 if (
Left.is(TT_NonNullAssertion) &&
5493 Right.isOneOf(Keywords.kw_as, Keywords.kw_in)) {
5496 }
else if (Style.isJava()) {
5497 if (
Left.is(TT_CaseLabelArrow) ||
Right.is(TT_CaseLabelArrow))
5499 if (
Left.is(tok::r_square) &&
Right.is(tok::l_brace))
5502 if (
Left.is(tok::l_square) ||
Right.is(tok::r_square))
5503 return Style.SpacesInSquareBrackets;
5505 if (
Left.is(Keywords.kw_synchronized) &&
Right.is(tok::l_paren)) {
5506 return Style.SpaceBeforeParensOptions.AfterControlStatements ||
5507 spaceRequiredBeforeParens(Right);
5509 if ((
Left.isAccessSpecifierKeyword() ||
5510 Left.isOneOf(tok::kw_static, Keywords.kw_final, Keywords.kw_abstract,
5511 Keywords.kw_native)) &&
5512 Right.is(TT_TemplateOpener)) {
5515 }
else if (IsVerilog) {
5517 if (
Left.is(tok::identifier) &&
Left.TokenText[0] ==
'\\')
5521 if ((
Left.is(TT_VerilogTableItem) &&
5522 Right.isNoneOf(tok::r_paren, tok::semi)) ||
5523 (
Right.is(TT_VerilogTableItem) &&
Left.isNot(tok::l_paren))) {
5525 return !(
Next &&
Next->is(tok::r_paren));
5528 if (
Left.isNot(TT_BinaryOperator) &&
5529 Left.isOneOf(Keywords.kw_verilogHash, Keywords.kw_verilogHashHash)) {
5533 if (
Right.isNot(tok::semi) &&
5534 (
Left.endsSequence(tok::numeric_constant, Keywords.kw_verilogHash) ||
5535 Left.endsSequence(tok::numeric_constant,
5536 Keywords.kw_verilogHashHash) ||
5537 (
Left.is(tok::r_paren) &&
Left.MatchingParen &&
5538 Left.MatchingParen->endsSequence(tok::l_paren, tok::at)))) {
5543 if (
Left.is(Keywords.kw_apostrophe) ||
5544 (
Left.is(TT_VerilogNumberBase) &&
Right.is(tok::numeric_constant))) {
5548 if (
Left.is(tok::arrow) ||
Right.is(tok::arrow))
5553 if (
Left.is(tok::at) &&
Right.isOneOf(tok::l_paren, tok::star, tok::at))
5556 if (
Right.is(tok::l_square) &&
5557 Left.isOneOf(TT_VerilogDimensionedTypeName, Keywords.kw_function)) {
5561 if (
Right.isOneOf(tok::period, Keywords.kw_apostrophe) &&
5562 Keywords.isVerilogIdentifier(Left) &&
Left.getPreviousNonComment() &&
5563 Left.getPreviousNonComment()->is(Keywords.kw_tagged)) {
5569 if ((
Right.is(Keywords.kw_apostrophe) ||
5571 Left.isNoneOf(Keywords.kw_assign, Keywords.kw_unique) &&
5572 !Keywords.isVerilogWordOperator(Left) &&
5573 (
Left.isOneOf(tok::r_square, tok::r_paren, tok::r_brace,
5574 tok::numeric_constant) ||
5575 Keywords.isWordLike(Left))) {
5579 if ((
Right.is(tok::star) &&
Left.is(tok::coloncolon)) ||
5580 (
Left.is(tok::star) &&
Right.is(tok::semi))) {
5584 if (
Left.endsSequence(tok::star, tok::l_paren) &&
Right.is(tok::identifier))
5587 if (
Right.is(tok::l_paren) &&
Right.is(TT_VerilogStrength))
5590 if ((
Left.is(tok::l_brace) &&
5591 Right.isOneOf(tok::lessless, tok::greatergreater)) ||
5592 (
Left.endsSequence(tok::lessless, tok::l_brace) ||
5593 Left.endsSequence(tok::greatergreater, tok::l_brace))) {
5596 }
else if (Style.isTableGen()) {
5598 if (
Left.is(tok::l_square) &&
Right.is(tok::l_brace))
5600 if (
Left.is(tok::r_brace) &&
Right.is(tok::r_square))
5603 if (
Right.isOneOf(TT_TableGenDAGArgListColon,
5604 TT_TableGenDAGArgListColonToAlign) ||
5605 Left.isOneOf(TT_TableGenDAGArgListColon,
5606 TT_TableGenDAGArgListColonToAlign)) {
5609 if (
Right.is(TT_TableGenCondOperatorColon))
5611 if (
Left.isOneOf(TT_TableGenDAGArgOperatorID,
5612 TT_TableGenDAGArgOperatorToBreak) &&
5613 Right.isNot(TT_TableGenDAGArgCloser)) {
5617 if (
Right.isOneOf(tok::l_paren, tok::less) &&
5618 Left.isOneOf(TT_TableGenBangOperator, TT_TableGenCondOperator)) {
5623 if (
Left.is(TT_TableGenTrailingPasteOperator) &&
5624 Right.isOneOf(tok::l_brace, tok::colon)) {
5628 if (
Left.is(tok::hash) ||
Right.is(tok::hash))
5631 if (Keywords.isTableGenDefinition(Left))
5635 if (
Left.is(TT_ImplicitStringLiteral))
5636 return Right.hasWhitespaceBefore();
5638 if (
Left.is(TT_ObjCMethodSpecifier))
5639 return Style.ObjCSpaceAfterMethodDeclarationPrefix;
5640 if (
Left.is(tok::r_paren) &&
Left.isNot(TT_AttributeRParen) &&
5641 canBeObjCSelectorComponent(Right)) {
5649 (
Right.is(tok::equal) ||
Left.is(tok::equal))) {
5653 if (
Right.isOneOf(TT_TrailingReturnArrow, TT_LambdaArrow) ||
5654 Left.isOneOf(TT_TrailingReturnArrow, TT_LambdaArrow)) {
5657 if (
Left.is(tok::comma) &&
Right.isNot(TT_OverloadedOperatorLParen) &&
5660 (
Left.Children.empty() || !
Left.MacroParent)) {
5663 if (
Right.is(tok::comma))
5665 if (
Right.is(TT_ObjCBlockLParen))
5667 if (
Right.is(TT_CtorInitializerColon))
5668 return Style.SpaceBeforeCtorInitializerColon;
5669 if (
Right.is(TT_InheritanceColon) && !Style.SpaceBeforeInheritanceColon)
5671 if (
Right.is(TT_EnumUnderlyingTypeColon) &&
5672 !Style.SpaceBeforeEnumUnderlyingTypeColon) {
5675 if (
Right.is(TT_RangeBasedForLoopColon) &&
5676 !Style.SpaceBeforeRangeBasedForLoopColon) {
5679 if (
Left.is(TT_BitFieldColon)) {
5680 return Style.BitFieldColonSpacing == FormatStyle::BFCS_Both ||
5681 Style.BitFieldColonSpacing == FormatStyle::BFCS_After;
5683 if (
Right.is(tok::colon)) {
5684 if (
Right.is(TT_CaseLabelColon))
5685 return Style.SpaceBeforeCaseColon;
5686 if (
Right.is(TT_GotoLabelColon))
5689 if (!
Right.getNextNonComment())
5691 if (
Right.isOneOf(TT_ObjCSelector, TT_ObjCMethodExpr))
5693 if (
Left.is(tok::question))
5695 if (
Right.is(TT_InlineASMColon) &&
Left.is(tok::coloncolon))
5697 if (
Right.is(TT_DictLiteral))
5698 return Style.SpacesInContainerLiterals;
5699 if (
Right.is(TT_AttributeColon))
5701 if (
Right.is(TT_CSharpNamedArgumentColon))
5703 if (
Right.is(TT_GenericSelectionColon))
5705 if (
Right.is(TT_BitFieldColon)) {
5706 return Style.BitFieldColonSpacing == FormatStyle::BFCS_Both ||
5707 Style.BitFieldColonSpacing == FormatStyle::BFCS_Before;
5712 if ((
Left.isOneOf(tok::minus, tok::minusminus) &&
5713 Right.isOneOf(tok::minus, tok::minusminus)) ||
5714 (
Left.isOneOf(tok::plus, tok::plusplus) &&
5715 Right.isOneOf(tok::plus, tok::plusplus))) {
5718 if (
Left.is(TT_UnaryOperator)) {
5721 if (
Left.is(tok::amp) &&
Right.is(tok::r_square))
5722 return Style.SpacesInSquareBrackets;
5723 if (
Left.isNot(tok::exclaim))
5725 if (
Left.TokenText ==
"!")
5726 return Style.SpaceAfterLogicalNot;
5727 assert(
Left.TokenText ==
"not");
5728 return Right.isOneOf(tok::coloncolon, TT_UnaryOperator) ||
5729 (
Right.is(tok::l_paren) && Style.SpaceBeforeParensOptions.AfterNot);
5734 if (
Left.is(TT_CastRParen)) {
5735 return Style.SpaceAfterCStyleCast ||
5736 Right.isOneOf(TT_BinaryOperator, TT_SelectorName);
5739 auto ShouldAddSpacesInAngles = [
this, &
Right]() {
5740 if (this->Style.SpacesInAngles == FormatStyle::SIAS_Always)
5742 if (this->Style.SpacesInAngles == FormatStyle::SIAS_Leave)
5743 return Right.hasWhitespaceBefore();
5747 if (
Left.is(tok::greater) &&
Right.is(tok::greater)) {
5748 if (Style.isTextProto() ||
5749 (Style.Language == FormatStyle::LK_Proto &&
Left.is(TT_DictLiteral))) {
5750 return Style.Cpp11BracedListStyle == FormatStyle::BLS_Block;
5752 return Right.is(TT_TemplateCloser) &&
Left.is(TT_TemplateCloser) &&
5753 ((Style.Standard < FormatStyle::LS_Cpp11) ||
5754 ShouldAddSpacesInAngles());
5756 if (
Right.isOneOf(tok::arrow, tok::arrowstar, tok::periodstar) ||
5757 Left.isOneOf(tok::arrow, tok::period, tok::arrowstar, tok::periodstar) ||
5758 (
Right.is(tok::period) &&
Right.isNot(TT_DesignatedInitializerPeriod))) {
5761 if (!Style.SpaceBeforeAssignmentOperators &&
Left.isNot(TT_TemplateCloser) &&
5765 if (Style.isJava() &&
Right.is(tok::coloncolon) &&
5766 Left.isOneOf(tok::identifier, tok::kw_this)) {
5769 if (
Right.is(tok::coloncolon) &&
Left.is(tok::identifier)) {
5771 return Left.isPossibleMacro(
true) &&
5772 Right.hasWhitespaceBefore();
5774 if (
Right.is(tok::coloncolon) &&
5775 Left.isNoneOf(tok::l_brace, tok::comment, tok::l_paren)) {
5777 return (
Left.is(TT_TemplateOpener) &&
5778 ((Style.Standard < FormatStyle::LS_Cpp11) ||
5779 ShouldAddSpacesInAngles())) ||
5780 Left.isNoneOf(tok::l_paren, tok::r_paren, tok::l_square,
5781 tok::kw___super, TT_TemplateOpener,
5782 TT_TemplateCloser) ||
5783 (
Left.is(tok::l_paren) && Style.SpacesInParensOptions.Other);
5785 if ((
Left.is(TT_TemplateOpener)) != (
Right.is(TT_TemplateCloser)))
5786 return ShouldAddSpacesInAngles();
5787 if (
Left.is(tok::r_paren) &&
Left.isNot(TT_TypeDeclarationParen) &&
5788 Right.is(TT_PointerOrReference) &&
Right.isOneOf(tok::amp, tok::ampamp)) {
5792 if (
Right.is(TT_StructuredBindingLSquare)) {
5793 return Left.isNoneOf(tok::amp, tok::ampamp) ||
5794 getTokenReferenceAlignment(Left) != FormatStyle::PAS_Right;
5797 if (
Right.Next &&
Right.Next->is(TT_StructuredBindingLSquare) &&
5798 Right.isOneOf(tok::amp, tok::ampamp)) {
5799 return getTokenReferenceAlignment(Right) != FormatStyle::PAS_Left;
5801 if ((
Right.is(TT_BinaryOperator) &&
Left.isNot(tok::l_paren)) ||
5802 (
Left.isOneOf(TT_BinaryOperator, TT_EnumEqual, TT_ConditionalExpr) &&
5803 Right.isNot(tok::r_paren))) {
5806 if (
Right.is(TT_TemplateOpener) &&
Left.is(tok::r_paren) &&
5807 Left.MatchingParen &&
5808 Left.MatchingParen->is(TT_OverloadedOperatorLParen)) {
5811 if (
Right.is(tok::less) &&
Left.isNot(tok::l_paren) &&
5815 if (
Right.is(TT_TrailingUnaryOperator))
5817 if (
Left.is(TT_RegexLiteral))
5819 return spaceRequiredBetween(
Line, Left, Right);
5825 Tok.isNoneOf(TT_ObjCBlockLBrace, TT_LambdaLBrace, TT_DictLiteral);
5830 return Tok.MatchingParen &&
Tok.MatchingParen->Next &&
5831 Tok.MatchingParen->Next->isOneOf(tok::comma, tok::r_paren,
5837 FormatStyle::ShortLambdaStyle ShortLambdaOption) {
5838 return Tok.Children.empty() && ShortLambdaOption != FormatStyle::SLS_None;
5843 Tok.isNoneOf(TT_ObjCBlockLBrace, TT_DictLiteral);
5846bool TokenAnnotator::mustBreakBefore(AnnotatedLine &
Line,
5848 if (Right.NewlinesBefore > 1 && Style.MaxEmptyLinesToKeep > 0 &&
5849 (!Style.RemoveEmptyLinesInUnwrappedLines || &Right ==
Line.First)) {
5855 if (Style.BreakFunctionDeclarationParameters &&
Line.MightBeFunctionDecl &&
5856 !
Line.mightBeFunctionDefinition() && Left.MightBeFunctionDeclParen &&
5857 Left.ParameterCount > 0) {
5861 if (Style.BreakFunctionDefinitionParameters &&
Line.MightBeFunctionDecl &&
5862 Line.mightBeFunctionDefinition() &&
Left.MightBeFunctionDeclParen &&
5863 Left.ParameterCount > 0) {
5869 if (Style.PackParameters.BinPack == FormatStyle::BPPS_AlwaysOnePerLine &&
5870 Line.MightBeFunctionDecl && !
Left.opensScope() &&
5875 const auto *BeforeLeft =
Left.Previous;
5876 const auto *AfterRight =
Right.Next;
5878 if (Style.isCSharp()) {
5879 if (
Left.is(TT_FatArrow) &&
Right.is(tok::l_brace) &&
5880 Style.BraceWrapping.AfterFunction) {
5883 if (
Right.is(TT_CSharpNamedArgumentColon) ||
5884 Left.is(TT_CSharpNamedArgumentColon)) {
5887 if (
Right.is(TT_CSharpGenericTypeConstraint))
5889 if (AfterRight && AfterRight->is(TT_FatArrow) &&
5890 (
Right.is(tok::numeric_constant) ||
5891 (
Right.is(tok::identifier) &&
Right.TokenText ==
"_"))) {
5896 if (
Left.is(TT_AttributeRSquare) &&
5897 (
Right.isAccessSpecifier(
false) ||
5898 Right.is(Keywords.kw_internal))) {
5902 if (
Left.is(TT_AttributeRSquare) &&
Right.is(TT_AttributeLSquare))
5904 }
else if (Style.isJavaScript()) {
5906 if (
Right.is(tok::string_literal) &&
Left.is(tok::plus) && BeforeLeft &&
5907 BeforeLeft->is(tok::string_literal)) {
5910 if (
Left.is(TT_DictLiteral) &&
Left.is(tok::l_brace) &&
Line.Level == 0 &&
5911 BeforeLeft && BeforeLeft->is(tok::equal) &&
5912 Line.First->isOneOf(tok::identifier, Keywords.kw_import, tok::kw_export,
5916 Line.First->isNoneOf(Keywords.kw_var, Keywords.kw_let)) {
5921 if (
Left.is(tok::l_brace) &&
Line.Level == 0 &&
5922 (
Line.startsWith(tok::kw_enum) ||
5923 Line.startsWith(tok::kw_const, tok::kw_enum) ||
5924 Line.startsWith(tok::kw_export, tok::kw_enum) ||
5925 Line.startsWith(tok::kw_export, tok::kw_const, tok::kw_enum))) {
5930 if (
Right.is(tok::r_brace) &&
Left.is(tok::l_brace) && BeforeLeft &&
5931 BeforeLeft->is(TT_FatArrow)) {
5933 switch (Style.AllowShortLambdasOnASingleLine) {
5934 case FormatStyle::SLS_All:
5936 case FormatStyle::SLS_None:
5938 case FormatStyle::SLS_Empty:
5939 return !
Left.Children.empty();
5940 case FormatStyle::SLS_Inline:
5943 return (
Left.NestingLevel == 0 &&
Line.Level == 0) &&
5944 !
Left.Children.empty();
5946 llvm_unreachable(
"Unknown FormatStyle::ShortLambdaStyle enum");
5949 if (
Right.is(tok::r_brace) &&
Left.is(tok::l_brace) &&
5950 !
Left.Children.empty()) {
5952 if (
Left.NestingLevel == 0 &&
Line.Level == 0)
5953 return !Style.AllowShortFunctionsOnASingleLine.Other;
5955 return !Style.AllowShortFunctionsOnASingleLine.Inline;
5957 }
else if (Style.isJava()) {
5958 if (
Right.is(tok::plus) &&
Left.is(tok::string_literal) && AfterRight &&
5959 AfterRight->is(tok::string_literal)) {
5962 }
else if (Style.isVerilog()) {
5964 if (
Left.is(TT_VerilogAssignComma))
5967 if (
Left.is(TT_VerilogTypeComma))
5971 if (Style.VerilogBreakBetweenInstancePorts &&
5972 (
Left.is(TT_VerilogInstancePortComma) ||
5973 (
Left.is(tok::r_paren) && Keywords.isVerilogIdentifier(Right) &&
5974 Left.MatchingParen &&
5975 Left.MatchingParen->is(TT_VerilogInstancePortLParen)))) {
5980 if (!Keywords.isVerilogBegin(Right) && Keywords.isVerilogEndOfLabel(Left))
5982 }
else if (Style.BreakAdjacentStringLiterals &&
5983 (IsCpp || Style.isProto() || Style.isTableGen())) {
5984 if (
Left.isStringLiteral() &&
Right.isStringLiteral())
5989 if (Style.isJson()) {
5993 if (
Left.is(TT_DictLiteral) &&
Left.is(tok::l_brace))
5996 if ((
Left.is(TT_ArrayInitializerLSquare) &&
Left.is(tok::l_square) &&
5997 Right.isNot(tok::r_square)) ||
5998 Left.is(tok::comma)) {
5999 if (
Right.is(tok::l_brace))
6004 if (
Tok->isOneOf(tok::l_brace, tok::l_square))
6006 if (
Tok->isOneOf(tok::r_brace, tok::r_square))
6009 return Style.BreakArrays;
6011 }
else if (Style.isTableGen()) {
6015 if (
Left.is(TT_TableGenCondOperatorComma))
6017 if (
Left.is(TT_TableGenDAGArgOperatorToBreak) &&
6018 Right.isNot(TT_TableGenDAGArgCloser)) {
6021 if (
Left.is(TT_TableGenDAGArgListCommaToBreak))
6023 if (
Right.is(TT_TableGenDAGArgCloser) &&
Right.MatchingParen &&
6024 Right.MatchingParen->is(TT_TableGenDAGArgOpenerToBreak) &&
6025 &Left !=
Right.MatchingParen->Next) {
6027 return Style.TableGenBreakInsideDAGArg == FormatStyle::DAS_BreakAll;
6031 if (
Line.startsWith(tok::kw_asm) &&
Right.is(TT_InlineASMColon) &&
6032 Style.BreakBeforeInlineASMColon == FormatStyle::BBIAS_Always) {
6042 if ((
Left.isOneOf(tok::l_brace, TT_ArrayInitializerLSquare) ||
6043 (Style.isJavaScript() &&
Left.is(tok::l_paren))) &&
6045 BeforeClosingBrace =
Left.MatchingParen->Previous;
6046 }
else if (
Right.MatchingParen &&
6047 (
Right.MatchingParen->isOneOf(tok::l_brace,
6048 TT_ArrayInitializerLSquare) ||
6049 (Style.isJavaScript() &&
6050 Right.MatchingParen->is(tok::l_paren)))) {
6051 BeforeClosingBrace = &
Left;
6053 if (BeforeClosingBrace && (BeforeClosingBrace->is(tok::comma) ||
6054 BeforeClosingBrace->isTrailingComment())) {
6059 if (
Right.is(tok::comment)) {
6061 Right.NewlinesBefore > 0 &&
Right.HasUnescapedNewline;
6063 if (
Left.isTrailingComment())
6065 if (
Left.IsUnterminatedLiteral)
6068 if (BeforeLeft && BeforeLeft->is(tok::lessless) &&
6069 Left.is(tok::string_literal) &&
Right.is(tok::lessless) && AfterRight &&
6070 AfterRight->is(tok::string_literal)) {
6071 return Right.NewlinesBefore > 0;
6074 if (
Right.is(TT_RequiresClause)) {
6075 switch (Style.RequiresClausePosition) {
6076 case FormatStyle::RCPS_OwnLine:
6077 case FormatStyle::RCPS_OwnLineWithBrace:
6078 case FormatStyle::RCPS_WithFollowing:
6085 if (
Left.ClosesTemplateDeclaration &&
Left.MatchingParen &&
6086 Left.MatchingParen->NestingLevel == 0) {
6090 if (
Right.is(tok::kw_concept))
6091 return Style.BreakBeforeConceptDeclarations == FormatStyle::BBCDS_Always;
6092 return Style.BreakTemplateDeclarations == FormatStyle::BTDS_Yes ||
6093 (Style.BreakTemplateDeclarations == FormatStyle::BTDS_Leave &&
6094 Right.NewlinesBefore > 0);
6096 if (
Left.ClosesRequiresClause) {
6097 switch (Style.RequiresClausePosition) {
6098 case FormatStyle::RCPS_OwnLine:
6099 case FormatStyle::RCPS_WithPreceding:
6100 return Right.isNot(tok::semi);
6101 case FormatStyle::RCPS_OwnLineWithBrace:
6102 return Right.isNoneOf(tok::semi, tok::l_brace);
6107 if (Style.PackConstructorInitializers == FormatStyle::PCIS_Never) {
6108 if (Style.BreakConstructorInitializers == FormatStyle::BCIS_BeforeColon &&
6109 (
Left.is(TT_CtorInitializerComma) ||
6110 Right.is(TT_CtorInitializerColon))) {
6114 if (Style.BreakConstructorInitializers == FormatStyle::BCIS_AfterColon &&
6115 Left.isOneOf(TT_CtorInitializerColon, TT_CtorInitializerComma)) {
6119 if (Style.BreakConstructorInitializers == FormatStyle::BCIS_AfterComma &&
6120 Left.is(TT_CtorInitializerComma)) {
6124 if (Style.PackConstructorInitializers < FormatStyle::PCIS_CurrentLine &&
6125 Style.BreakConstructorInitializers == FormatStyle::BCIS_BeforeComma &&
6126 Right.isOneOf(TT_CtorInitializerComma, TT_CtorInitializerColon)) {
6129 if (Style.PackConstructorInitializers == FormatStyle::PCIS_NextLineOnly) {
6130 if ((Style.BreakConstructorInitializers == FormatStyle::BCIS_BeforeColon ||
6131 Style.BreakConstructorInitializers == FormatStyle::BCIS_BeforeComma) &&
6132 Right.is(TT_CtorInitializerColon)) {
6136 if (Style.BreakConstructorInitializers == FormatStyle::BCIS_AfterColon &&
6137 Left.is(TT_CtorInitializerColon)) {
6142 if (Style.BreakInheritanceList == FormatStyle::BILS_BeforeComma &&
6143 Right.is(TT_InheritanceComma)) {
6146 if (Style.BreakInheritanceList == FormatStyle::BILS_AfterComma &&
6147 Left.is(TT_InheritanceComma)) {
6150 if (
Right.is(tok::string_literal) &&
Right.TokenText.starts_with(
"R\"")) {
6154 return Right.IsMultiline &&
Right.NewlinesBefore > 0;
6156 if ((
Left.is(tok::l_brace) ||
6157 (
Left.is(tok::less) && BeforeLeft && BeforeLeft->is(tok::equal))) &&
6158 Right.NestingLevel == 1 && Style.Language == FormatStyle::LK_Proto) {
6163 if (
Right.is(TT_InlineASMBrace))
6164 return Right.HasUnescapedNewline;
6167 auto *FirstNonComment =
Line.getFirstNonComment();
6169 FirstNonComment && (FirstNonComment->is(Keywords.kw_internal) ||
6170 FirstNonComment->isAccessSpecifierKeyword());
6172 if (Style.BraceWrapping.AfterEnum) {
6173 if (
Line.startsWith(tok::kw_enum) ||
6174 Line.startsWith(tok::kw_typedef, tok::kw_enum) ||
6175 Line.startsWith(tok::kw_export, tok::kw_enum)) {
6180 FirstNonComment->Next->is(tok::kw_enum)) {
6186 if (Style.BraceWrapping.AfterClass &&
6188 FirstNonComment->Next->is(Keywords.kw_interface)) ||
6189 Line.startsWith(Keywords.kw_interface))) {
6194 if (
Right.isNot(TT_FunctionLBrace)) {
6195 return Style.AllowShortRecordOnASingleLine == FormatStyle::SRS_Never &&
6196 ((
Line.startsWith(tok::kw_class) &&
6197 Style.BraceWrapping.AfterClass) ||
6198 (
Line.startsWith(tok::kw_struct) &&
6199 Style.BraceWrapping.AfterStruct) ||
6200 (
Line.startsWith(tok::kw_union) &&
6201 Style.BraceWrapping.AfterUnion));
6205 if (
Left.is(TT_ObjCBlockLBrace) &&
6206 Style.AllowShortBlocksOnASingleLine == FormatStyle::SBS_Never) {
6211 if (
Left.isOneOf(TT_AttributeRParen, TT_AttributeMacro) &&
6212 Right.is(TT_ObjCDecl)) {
6216 if (
Left.is(TT_LambdaLBrace)) {
6218 Style.AllowShortLambdasOnASingleLine == FormatStyle::SLS_Inline) {
6222 if (Style.AllowShortLambdasOnASingleLine == FormatStyle::SLS_None ||
6223 Style.AllowShortLambdasOnASingleLine == FormatStyle::SLS_Inline ||
6224 (!
Left.Children.empty() &&
6225 Style.AllowShortLambdasOnASingleLine == FormatStyle::SLS_Empty)) {
6230 if (Style.BraceWrapping.BeforeLambdaBody &&
Right.is(TT_LambdaLBrace) &&
6231 (
Left.isPointerOrReference() ||
Left.is(TT_TemplateCloser))) {
6236 if ((Style.isJava() || Style.isJavaScript()) &&
6237 Left.is(TT_LeadingJavaAnnotation) &&
6238 Right.isNoneOf(TT_LeadingJavaAnnotation, tok::l_paren) &&
6239 (
Line.Last->is(tok::l_brace) || Style.BreakAfterJavaFieldAnnotations)) {
6243 if (
Right.is(TT_ProtoExtensionLSquare))
6273 if (Style.isProto() &&
Right.is(TT_SelectorName) &&
6274 Right.isNot(tok::r_square) && AfterRight) {
6277 if (
Left.is(tok::at))
6283 const auto *LBrace = AfterRight;
6284 if (LBrace && LBrace->is(tok::colon)) {
6285 LBrace = LBrace->Next;
6286 if (LBrace && LBrace->is(tok::at)) {
6287 LBrace = LBrace->Next;
6289 LBrace = LBrace->Next;
6301 ((LBrace->is(tok::l_brace) &&
6302 (LBrace->is(TT_DictLiteral) ||
6303 (LBrace->Next && LBrace->Next->is(tok::r_brace)))) ||
6304 LBrace->isOneOf(TT_ArrayInitializerLSquare, tok::less))) {
6311 if (
Left.ParameterCount == 0)
6326 if (
Left.isOneOf(tok::r_brace, tok::greater, tok::r_square))
6330 if (Style.BreakAfterAttributes == FormatStyle::ABS_LeaveAll &&
6331 Left.is(TT_AttributeRSquare) &&
Right.NewlinesBefore > 0) {
6332 Line.ReturnTypeWrapped =
true;
6343 if (Style.isCSharp()) {
6344 if (
Left.isOneOf(TT_CSharpNamedArgumentColon, TT_AttributeColon) ||
6345 Right.isOneOf(TT_CSharpNamedArgumentColon, TT_AttributeColon)) {
6349 if (
Line.First->is(TT_CSharpGenericTypeConstraint))
6350 return Left.is(TT_CSharpGenericTypeConstraintComma);
6352 if (
Right.is(TT_CSharpNullable))
6354 }
else if (Style.isJava()) {
6355 if (
Left.isOneOf(Keywords.kw_throws, Keywords.kw_extends,
6356 Keywords.kw_implements)) {
6359 if (
Right.isOneOf(Keywords.kw_throws, Keywords.kw_extends,
6360 Keywords.kw_implements)) {
6363 }
else if (Style.isJavaScript()) {
6366 (NonComment->isAccessSpecifierKeyword() ||
6367 NonComment->isOneOf(
6368 tok::kw_return, Keywords.kw_yield, tok::kw_continue, tok::kw_break,
6369 tok::kw_throw, Keywords.kw_interface, Keywords.kw_type,
6370 tok::kw_static, Keywords.kw_readonly, Keywords.kw_override,
6371 Keywords.kw_abstract, Keywords.kw_get, Keywords.kw_set,
6372 Keywords.kw_async, Keywords.kw_await))) {
6375 if (
Right.NestingLevel == 0 &&
6376 (
Left.Tok.getIdentifierInfo() ||
6377 Left.isOneOf(tok::r_square, tok::r_paren)) &&
6378 Right.isOneOf(tok::l_square, tok::l_paren)) {
6381 if (NonComment && NonComment->is(tok::identifier) &&
6382 NonComment->TokenText ==
"asserts") {
6385 if (
Left.is(TT_FatArrow) &&
Right.is(tok::l_brace))
6387 if (
Left.is(TT_JsTypeColon))
6390 if (
Left.is(tok::exclaim) &&
Right.is(tok::colon))
6395 if (
Right.is(Keywords.kw_is)) {
6404 if (!
Next ||
Next->isNot(tok::colon))
6407 if (
Left.is(Keywords.kw_in))
6408 return Style.BreakBeforeBinaryOperators == FormatStyle::BOS_None;
6409 if (
Right.is(Keywords.kw_in))
6410 return Style.BreakBeforeBinaryOperators != FormatStyle::BOS_None;
6411 if (
Right.is(Keywords.kw_as))
6413 if (
Right.isOneOf(Keywords.kw_extends, Keywords.kw_infer)) {
6419 if (
Left.is(Keywords.kw_as))
6421 if (
Left.is(TT_NonNullAssertion))
6423 if (
Left.is(Keywords.kw_declare) &&
6424 Right.isOneOf(Keywords.kw_module, tok::kw_namespace,
6425 Keywords.kw_function, tok::kw_class, tok::kw_enum,
6426 Keywords.kw_interface, Keywords.kw_type, Keywords.kw_var,
6427 Keywords.kw_let, tok::kw_const)) {
6432 if (
Left.isOneOf(Keywords.kw_module, tok::kw_namespace) &&
6433 Right.isOneOf(tok::identifier, tok::string_literal)) {
6436 if (
Right.is(TT_TemplateString) &&
Right.closesScope())
6440 if (
Left.is(tok::identifier) &&
Right.is(TT_TemplateString))
6442 if (
Left.is(TT_TemplateString) &&
Left.opensScope())
6444 }
else if (Style.isTableGen()) {
6446 if (Keywords.isTableGenDefinition(Left))
6449 if (
Right.is(tok::l_paren)) {
6450 return Left.isNoneOf(TT_TableGenBangOperator, TT_TableGenCondOperator,
6454 if (
Left.is(TT_TableGenValueSuffix))
6457 if (
Left.is(tok::hash) ||
Right.is(tok::hash))
6459 if (
Left.isOneOf(TT_TableGenBangOperator, TT_TableGenCondOperator))
6466 if (
Right.is(tok::r_brace)) {
6468 (
Right.isBlockIndentedInitRBrace(Style)));
6473 if (
Right.is(tok::r_paren)) {
6474 if (!
Right.MatchingParen)
6477 if (
Next &&
Next->is(tok::r_paren))
6479 if (
Next &&
Next->is(tok::l_paren))
6485 return Style.BreakBeforeCloseBracketIf;
6487 return Style.BreakBeforeCloseBracketLoop;
6489 return Style.BreakBeforeCloseBracketSwitch;
6490 return Style.BreakBeforeCloseBracketFunction;
6493 if (
Left.isOneOf(tok::r_paren, TT_TrailingAnnotation) &&
6494 Right.is(TT_TrailingAnnotation) &&
6495 Style.BreakBeforeCloseBracketFunction) {
6499 if (
Right.is(TT_TemplateCloser))
6500 return Style.BreakBeforeTemplateCloser;
6502 if (
Left.isOneOf(tok::at, tok::objc_interface))
6504 if (
Left.isOneOf(TT_JavaAnnotation, TT_LeadingJavaAnnotation))
6505 return Right.isNot(tok::l_paren);
6506 if (
Right.is(TT_PointerOrReference)) {
6507 return Line.IsMultiVariableDeclStmt ||
6508 (getTokenPointerOrReferenceAlignment(Right) ==
6509 FormatStyle::PAS_Right &&
6511 Right.Next->isOneOf(TT_FunctionDeclarationName, tok::kw_const)));
6513 if (
Left.is(tok::hashhash) ||
Right.is(tok::hashhash))
6515 if (
Right.isOneOf(TT_StartOfName, TT_FunctionDeclarationName,
6516 TT_ClassHeadName, TT_QtProperty, tok::kw_operator)) {
6519 if (
Left.is(TT_PointerOrReference))
6521 if (
Right.isTrailingComment()) {
6528 (
Left.is(TT_CtorInitializerColon) &&
Right.NewlinesBefore > 0 &&
6529 Style.BreakConstructorInitializers == FormatStyle::BCIS_AfterColon);
6531 if (
Left.is(tok::question) &&
Right.is(tok::colon))
6533 if (
Right.isOneOf(TT_ConditionalExpr, tok::question))
6534 return Style.BreakBeforeTernaryOperators;
6535 if (
Left.isOneOf(TT_ConditionalExpr, tok::question))
6536 return !Style.BreakBeforeTernaryOperators;
6537 if (
Left.is(TT_InheritanceColon))
6538 return Style.BreakInheritanceList == FormatStyle::BILS_AfterColon;
6539 if (
Right.is(TT_InheritanceColon))
6540 return Style.BreakInheritanceList != FormatStyle::BILS_AfterColon;
6542 if (
Right.is(TT_ObjCMethodExpr) &&
Right.isNot(tok::r_square) &&
6543 Left.isNot(TT_SelectorName)) {
6547 if (
Right.is(tok::colon) &&
6548 Right.isNoneOf(TT_CtorInitializerColon, TT_InlineASMColon,
6549 TT_BitFieldColon)) {
6552 if (
Left.is(tok::colon) &&
Left.isOneOf(TT_ObjCSelector, TT_ObjCMethodExpr))
6554 if (
Left.is(tok::colon) &&
Left.is(TT_DictLiteral)) {
6555 if (Style.isProto()) {
6556 if (!Style.AlwaysBreakBeforeMultilineStrings &&
Right.isStringLiteral())
6582 if ((
Right.isOneOf(tok::l_brace, tok::less) &&
6583 Right.is(TT_DictLiteral)) ||
6584 Right.is(TT_ArrayInitializerLSquare)) {
6590 if (
Right.is(tok::r_square) &&
Right.MatchingParen &&
6591 Right.MatchingParen->is(TT_ProtoExtensionLSquare)) {
6594 if (
Right.is(TT_SelectorName) || (
Right.is(tok::identifier) &&
Right.Next &&
6595 Right.Next->is(TT_ObjCMethodExpr))) {
6596 return Left.isNot(tok::period);
6600 if (
Right.is(tok::kw_concept))
6601 return Style.BreakBeforeConceptDeclarations != FormatStyle::BBCDS_Never;
6602 if (
Right.is(TT_RequiresClause))
6604 if (
Left.ClosesTemplateDeclaration) {
6605 return Style.BreakTemplateDeclarations != FormatStyle::BTDS_Leave ||
6606 Right.NewlinesBefore > 0;
6608 if (
Left.is(TT_FunctionAnnotationRParen))
6610 if (
Left.ClosesRequiresClause)
6612 if (
Right.isOneOf(TT_RangeBasedForLoopColon, TT_OverloadedOperatorLParen,
6613 TT_OverloadedOperator)) {
6616 if (
Left.is(TT_RangeBasedForLoopColon))
6618 if (
Right.is(TT_RangeBasedForLoopColon))
6620 if (
Left.is(TT_TemplateCloser) &&
Right.is(TT_TemplateOpener))
6622 if ((
Left.is(tok::greater) &&
Right.is(tok::greater)) ||
6623 (
Left.is(tok::less) &&
Right.is(tok::less))) {
6626 if (
Right.is(TT_BinaryOperator) &&
6627 Style.BreakBeforeBinaryOperators != FormatStyle::BOS_None &&
6628 (Style.BreakBeforeBinaryOperators == FormatStyle::BOS_All ||
6632 if (
Left.isOneOf(TT_TemplateCloser, TT_UnaryOperator, tok::kw_operator))
6634 if (
Left.is(tok::equal) &&
Right.isNoneOf(tok::kw_default, tok::kw_delete) &&
6638 if (
Left.is(tok::equal) &&
Right.is(tok::l_brace) &&
6639 Style.Cpp11BracedListStyle == FormatStyle::BLS_Block) {
6642 if (
Left.is(TT_AttributeLParen) ||
6643 (
Left.is(tok::l_paren) &&
Left.is(TT_TypeDeclarationParen))) {
6646 if (
Left.is(tok::l_paren) &&
Left.Previous &&
6647 (
Left.Previous->isOneOf(TT_BinaryOperator, TT_CastRParen))) {
6650 if (
Right.is(TT_ImplicitStringLiteral))
6653 if (
Right.is(tok::r_square) &&
Right.MatchingParen &&
6654 Right.MatchingParen->is(TT_LambdaLSquare)) {
6660 if (
Left.is(TT_TrailingAnnotation)) {
6661 return Right.isNoneOf(tok::l_brace, tok::semi, tok::equal, tok::l_paren,
6662 tok::less, tok::coloncolon);
6665 if (
Right.isAttribute())
6668 if (
Right.is(TT_AttributeLSquare)) {
6669 assert(
Left.isNot(tok::l_square));
6673 if (
Left.is(tok::identifier) &&
Right.is(tok::string_literal))
6676 if (
Right.is(tok::identifier) &&
Right.Next &&
Right.Next->is(TT_DictLiteral))
6679 if (
Left.is(TT_CtorInitializerColon)) {
6680 return (Style.BreakConstructorInitializers ==
6681 FormatStyle::BCIS_AfterColon ||
6682 Style.BreakConstructorInitializers ==
6683 FormatStyle::BCIS_AfterComma) &&
6684 (!
Right.isTrailingComment() ||
Right.NewlinesBefore > 0);
6686 if (
Right.is(TT_CtorInitializerColon)) {
6687 return Style.BreakConstructorInitializers != FormatStyle::BCIS_AfterColon &&
6688 Style.BreakConstructorInitializers != FormatStyle::BCIS_AfterComma;
6690 if (
Left.is(TT_CtorInitializerComma) &&
6691 Style.BreakConstructorInitializers == FormatStyle::BCIS_BeforeComma) {
6694 if (
Right.is(TT_CtorInitializerComma) &&
6695 Style.BreakConstructorInitializers == FormatStyle::BCIS_BeforeComma) {
6698 if (
Left.is(TT_InheritanceComma) &&
6699 Style.BreakInheritanceList == FormatStyle::BILS_BeforeComma) {
6702 if (
Right.is(TT_InheritanceComma) &&
6703 Style.BreakInheritanceList == FormatStyle::BILS_BeforeComma) {
6706 if (
Left.is(TT_ArrayInitializerLSquare))
6708 if (
Right.is(tok::kw_typename) &&
Left.isNot(tok::kw_const))
6710 if ((
Left.isBinaryOperator() ||
Left.is(TT_BinaryOperator)) &&
6711 Left.isNoneOf(tok::arrowstar, tok::lessless) &&
6712 Style.BreakBeforeBinaryOperators != FormatStyle::BOS_All &&
6713 (Style.BreakBeforeBinaryOperators == FormatStyle::BOS_None ||
6717 if (
Left.is(TT_AttributeLSquare) &&
Right.is(tok::l_square)) {
6718 assert(
Right.isNot(TT_AttributeLSquare));
6721 if (
Left.is(tok::r_square) &&
Right.is(TT_AttributeRSquare)) {
6722 assert(
Left.isNot(TT_AttributeRSquare));
6726 auto ShortLambdaOption = Style.AllowShortLambdasOnASingleLine;
6727 if (Style.BraceWrapping.BeforeLambdaBody &&
Right.is(TT_LambdaLBrace)) {
6734 if (
Right.is(tok::kw_noexcept) &&
Right.is(TT_TrailingAnnotation)) {
6735 switch (Style.AllowBreakBeforeNoexceptSpecifier) {
6736 case FormatStyle::BBNSS_Never:
6738 case FormatStyle::BBNSS_Always:
6740 case FormatStyle::BBNSS_OnlyWithParen:
6741 return Right.Next &&
Right.Next->is(tok::l_paren);
6745 return Left.isOneOf(tok::comma, tok::coloncolon, tok::semi, tok::l_brace,
6746 tok::kw_class, tok::kw_struct, tok::comment) ||
6747 Right.isMemberAccess() ||
6748 Right.isOneOf(TT_TrailingReturnArrow, TT_LambdaArrow, tok::lessless,
6749 tok::colon, tok::l_square, tok::at) ||
6750 (
Left.is(tok::r_paren) &&
6751 Right.isOneOf(tok::identifier, tok::kw_const)) ||
6752 (
Left.is(tok::l_paren) &&
Right.isNot(tok::r_paren)) ||
6753 (
Left.is(TT_TemplateOpener) &&
Right.isNot(TT_TemplateCloser));
6757 llvm::errs() <<
"AnnotatedTokens(L=" <<
Line.Level <<
", P=" <<
Line.PPLevel
6758 <<
", T=" <<
Line.Type <<
", C=" <<
Line.IsContinuation
6762 llvm::errs() <<
" I=" <<
Tok->IndentLevel <<
" M=" <<
Tok->MustBreakBefore
6763 <<
" C=" <<
Tok->CanBreakBefore
6765 <<
" S=" <<
Tok->SpacesRequiredBefore
6766 <<
" F=" <<
Tok->Finalized <<
" B=" <<
Tok->BlockParameterCount
6767 <<
" BK=" <<
Tok->getBlockKind() <<
" P=" <<
Tok->SplitPenalty
6768 <<
" Name=" <<
Tok->Tok.getName() <<
" N=" <<
Tok->NestingLevel
6769 <<
" L=" <<
Tok->TotalLength
6770 <<
" PPK=" <<
Tok->getPackingKind() <<
" FakeLParens=";
6772 llvm::errs() << LParen <<
"/";
6773 llvm::errs() <<
" FakeRParens=" <<
Tok->FakeRParens;
6774 llvm::errs() <<
" II=" <<
Tok->Tok.getIdentifierInfo();
6775 llvm::errs() <<
" Text='" <<
Tok->TokenText <<
"'\n";
6780 llvm::errs() <<
"----\n";
6783FormatStyle::PointerAlignmentStyle
6785 assert(
Reference.isOneOf(tok::amp, tok::ampamp));
6786 switch (Style.ReferenceAlignment) {
6787 case FormatStyle::RAS_Pointer:
6788 return Style.PointerAlignment;
6789 case FormatStyle::RAS_Left:
6790 return FormatStyle::PAS_Left;
6791 case FormatStyle::RAS_Right:
6792 return FormatStyle::PAS_Right;
6793 case FormatStyle::RAS_Middle:
6794 return FormatStyle::PAS_Middle;
6797 return Style.PointerAlignment;
6800FormatStyle::PointerAlignmentStyle
6801TokenAnnotator::getTokenPointerOrReferenceAlignment(
6803 if (PointerOrReference.isOneOf(tok::amp, tok::ampamp))
6804 return getTokenReferenceAlignment(PointerOrReference);
6805 assert(PointerOrReference.is(tok::star));
6806 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)
TokenKind
Provides a simple uniform namespace for tokens from all C languages.
The JSON file list parser is used to communicate input to InstallAPI.
AccessSpecifier
A C++ access specifier (public, private, protected), plus the special value "none" which means differ...
nullptr
This class represents a compute construct, representing a 'Kind' of ‘parallel’, 'serial',...
@ Parameter
The parameter type of a method or function.
@ Result
The result type of a method or function.
const FunctionProtoType * T
bool isReturnTypePrefixSpecifier(const FormatToken &Tok)
@ 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.