18#include "llvm/ADT/SmallPtrSet.h"
19#include "llvm/Support/Debug.h"
21#define DEBUG_TYPE "format-token-annotator"
27 const FormatStyle &Style) {
28 switch (Style.BreakAfterAttributes) {
29 case FormatStyle::ABS_Always:
31 case FormatStyle::ABS_Never:
34 return Tok.NewlinesBefore > 0;
42static bool startsWithInitStatement(
const AnnotatedLine &
Line) {
43 return Line.startsWith(tok::kw_for) ||
Line.startsWith(tok::kw_if) ||
44 Line.startsWith(tok::kw_switch);
59 return Tok.Tok.getIdentifierInfo();
65static bool isLambdaParameterList(
const FormatToken *Left) {
67 if (
Left->Previous &&
Left->Previous->is(tok::greater) &&
68 Left->Previous->MatchingParen &&
69 Left->Previous->MatchingParen->is(TT_TemplateOpener)) {
70 Left =
Left->Previous->MatchingParen;
74 return Left->Previous &&
Left->Previous->is(tok::r_square) &&
75 Left->Previous->MatchingParen &&
76 Left->Previous->MatchingParen->is(TT_LambdaLSquare);
82 return Tok.isOneOf(tok::kw_if, tok::kw_for, tok::kw_while, tok::kw_switch,
83 tok::kw_constexpr, tok::kw_catch);
88 if (!IsCpp || !
Tok.startsSequence(tok::l_square, tok::l_square))
91 if (
Tok.Previous &&
Tok.Previous->is(tok::at))
98 if (AttrTok->startsSequence(tok::kw_using, tok::identifier, tok::colon))
100 if (AttrTok->isNot(tok::identifier))
102 while (AttrTok && !AttrTok->startsSequence(tok::r_square, tok::r_square)) {
106 if (AttrTok->is(tok::colon) ||
107 AttrTok->startsSequence(tok::identifier, tok::identifier) ||
108 AttrTok->startsSequence(tok::r_paren, tok::identifier)) {
111 if (AttrTok->is(tok::ellipsis))
113 AttrTok = AttrTok->Next;
115 return AttrTok && AttrTok->startsSequence(tok::r_square, tok::r_square);
123class AnnotatingParser {
125 AnnotatingParser(
const FormatStyle &Style, AnnotatedLine &Line,
126 const AdditionalKeywords &Keywords,
127 SmallVector<ScopeType> &Scopes)
128 : Style(Style), Line(Line), CurrentToken(Line.
First), AutoFound(
false),
130 Keywords(Keywords), Scopes(Scopes), TemplateDeclarationDepth(0) {
131 Contexts.push_back(Context(tok::unknown, 1,
false));
132 resetTokenMetadata();
137 switch (Token.getType()) {
139 case TT_StructLBrace:
144 case TT_CompoundRequirementLBrace:
155 auto *
Left = CurrentToken->Previous;
159 if (NonTemplateLess.count(Left) > 0)
162 const auto *BeforeLess =
Left->Previous;
165 if (BeforeLess->Tok.isLiteral())
167 if (BeforeLess->is(tok::r_brace))
169 if (BeforeLess->is(tok::r_paren) && Contexts.size() > 1 &&
170 !(BeforeLess->MatchingParen &&
171 BeforeLess->MatchingParen->is(TT_OverloadedOperatorLParen))) {
174 if (BeforeLess->is(tok::kw_operator) && CurrentToken->is(tok::l_paren))
178 Left->ParentBracket = Contexts.back().ContextKind;
179 ScopedContextCreator ContextCreator(*
this, tok::less, 12);
180 Contexts.back().IsExpression =
false;
184 if (BeforeLess && BeforeLess->isNot(tok::kw_template))
185 Contexts.back().ContextType = Context::TemplateArgument;
187 if (Style.isJava() && CurrentToken->is(tok::question))
190 for (
bool SeenTernaryOperator =
false, MaybeAngles =
true; CurrentToken;) {
191 const auto &ParentContext = Contexts[Contexts.size() - 2];
192 const bool InExpr = ParentContext.IsExpression;
193 if (CurrentToken->is(tok::greater)) {
194 const auto *
Next = CurrentToken->Next;
195 if (CurrentToken->isNot(TT_TemplateCloser)) {
202 if (
Next &&
Next->is(tok::greater) &&
203 Left->ParentBracket != tok::less &&
204 CurrentToken->getStartOfNonWhitespace() ==
205 Next->getStartOfNonWhitespace().getLocWithOffset(-1)) {
208 if (InExpr && SeenTernaryOperator &&
209 (!
Next ||
Next->isNoneOf(tok::l_paren, tok::l_brace))) {
214 if (ParentContext.InStaticAssertFirstArgument &&
Next &&
215 Next->isOneOf(tok::minus, tok::identifier)) {
219 Left->MatchingParen = CurrentToken;
220 CurrentToken->MatchingParen =
Left;
226 if (Style.isTextProto() ||
227 (Style.Language == FormatStyle::LK_Proto && BeforeLess &&
228 BeforeLess->isOneOf(TT_SelectorName, TT_DictLiteral))) {
229 CurrentToken->setType(TT_DictLiteral);
231 CurrentToken->setType(TT_TemplateCloser);
232 CurrentToken->Tok.setLength(1);
239 if (BeforeLess && BeforeLess->is(TT_TemplateName)) {
243 if (CurrentToken->is(tok::question) && Style.isJava()) {
247 if (CurrentToken->isOneOf(tok::r_paren, tok::r_square, tok::r_brace))
249 const auto &Prev = *CurrentToken->Previous;
256 if (MaybeAngles && InExpr && !Line.startsWith(tok::kw_template) &&
257 Prev.is(TT_BinaryOperator) &&
258 Prev.isOneOf(tok::pipepipe, tok::ampamp)) {
261 if (Prev.isOneOf(tok::question, tok::colon) && !Style.isProto())
262 SeenTernaryOperator =
true;
263 updateParameterCount(Left, CurrentToken);
264 if (Style.Language == FormatStyle::LK_Proto) {
266 if (CurrentToken->is(tok::colon) ||
267 (CurrentToken->isOneOf(tok::l_brace, tok::less) &&
272 }
else if (Style.isTableGen()) {
273 if (CurrentToken->isOneOf(tok::comma, tok::equal)) {
280 if (!parseTableGenValue())
290 bool parseUntouchableParens() {
291 while (CurrentToken) {
292 CurrentToken->Finalized =
true;
293 switch (CurrentToken->Tok.getKind()) {
296 if (!parseUntouchableParens())
311 bool parseParens(
bool IsIf =
false) {
314 assert(CurrentToken->Previous &&
"Unknown previous token");
315 FormatToken &OpeningParen = *CurrentToken->Previous;
316 assert(OpeningParen.is(tok::l_paren));
317 FormatToken *PrevNonComment = OpeningParen.getPreviousNonComment();
318 OpeningParen.ParentBracket = Contexts.back().ContextKind;
319 ScopedContextCreator ContextCreator(*
this, tok::l_paren, 1);
322 Contexts.back().ColonIsForRangeExpr =
323 Contexts.size() == 2 && Contexts[0].ColonIsForRangeExpr;
325 if (OpeningParen.Previous &&
326 OpeningParen.Previous->is(TT_UntouchableMacroFunc)) {
327 OpeningParen.Finalized =
true;
328 return parseUntouchableParens();
331 bool StartsObjCSelector =
false;
332 if (!Style.isVerilog()) {
333 if (
FormatToken *MaybeSel = OpeningParen.Previous) {
335 if (MaybeSel->is(tok::objc_selector) && MaybeSel->Previous &&
336 MaybeSel->Previous->is(tok::at)) {
337 StartsObjCSelector =
true;
342 if (OpeningParen.is(TT_OverloadedOperatorLParen)) {
345 while (Prev->isNot(tok::kw_operator)) {
346 Prev = Prev->Previous;
347 assert(Prev &&
"Expect a kw_operator prior to the OperatorLParen!");
353 bool OperatorCalledAsMemberFunction =
354 Prev->Previous && Prev->Previous->isOneOf(tok::period, tok::arrow);
355 Contexts.back().IsExpression = OperatorCalledAsMemberFunction;
356 }
else if (OpeningParen.is(TT_VerilogInstancePortLParen)) {
357 Contexts.back().IsExpression =
true;
358 Contexts.back().ContextType = Context::VerilogInstancePortList;
359 }
else if (Style.isJavaScript() &&
360 (Line.startsWith(Keywords.kw_type, tok::identifier) ||
361 Line.startsWith(tok::kw_export, Keywords.kw_type,
365 Contexts.back().IsExpression =
false;
366 }
else if (OpeningParen.Previous &&
367 (OpeningParen.Previous->isOneOf(
368 tok::kw_noexcept, tok::kw_explicit, tok::kw_while,
369 tok::l_paren, tok::comma, TT_CastRParen,
370 TT_BinaryOperator) ||
371 OpeningParen.Previous->isIf())) {
373 Contexts.back().IsExpression =
true;
374 }
else if (Style.isJavaScript() && OpeningParen.Previous &&
375 (OpeningParen.Previous->is(Keywords.kw_function) ||
376 (OpeningParen.Previous->endsSequence(tok::identifier,
377 Keywords.kw_function)))) {
379 Contexts.back().IsExpression =
false;
380 }
else if (Style.isJavaScript() && OpeningParen.Previous &&
381 OpeningParen.Previous->is(TT_JsTypeColon)) {
383 Contexts.back().IsExpression =
false;
384 }
else if (isLambdaParameterList(&OpeningParen)) {
386 OpeningParen.setType(TT_LambdaDefinitionLParen);
387 Contexts.back().IsExpression =
false;
388 }
else if (OpeningParen.is(TT_RequiresExpressionLParen)) {
389 Contexts.back().IsExpression =
false;
390 }
else if (OpeningParen.Previous &&
391 OpeningParen.Previous->is(tok::kw__Generic)) {
392 Contexts.back().ContextType = Context::C11GenericSelection;
393 Contexts.back().IsExpression =
true;
394 }
else if (OpeningParen.Previous &&
395 OpeningParen.Previous->TokenText ==
"Q_PROPERTY") {
396 Contexts.back().ContextType = Context::QtProperty;
397 Contexts.back().IsExpression =
false;
398 }
else if (Line.InPPDirective &&
399 (!OpeningParen.Previous ||
400 OpeningParen.Previous->isNot(tok::identifier))) {
401 Contexts.back().IsExpression =
true;
402 }
else if (Contexts[Contexts.size() - 2].CaretFound) {
404 Contexts.back().IsExpression =
false;
405 }
else if (OpeningParen.Previous &&
406 OpeningParen.Previous->is(TT_ForEachMacro)) {
408 Contexts.back().ContextType = Context::ForEachMacro;
409 Contexts.back().IsExpression =
false;
410 }
else if (OpeningParen.Previous && OpeningParen.Previous->MatchingParen &&
411 OpeningParen.Previous->MatchingParen->isOneOf(
412 TT_ObjCBlockLParen, TT_FunctionTypeLParen)) {
413 Contexts.back().IsExpression =
false;
414 }
else if (!Line.MustBeDeclaration &&
415 (!Line.InPPDirective || (Line.InMacroBody && !Scopes.empty()))) {
417 OpeningParen.Previous &&
418 OpeningParen.Previous->isOneOf(tok::kw_for, tok::kw_catch);
419 Contexts.back().IsExpression = !IsForOrCatch;
422 if (Style.isTableGen()) {
424 if (Prev->is(TT_TableGenCondOperator)) {
425 Contexts.back().IsTableGenCondOpe =
true;
426 Contexts.back().IsExpression =
true;
427 }
else if (Contexts.size() > 1 &&
428 Contexts[Contexts.size() - 2].IsTableGenBangOpe) {
433 Contexts.back().IsTableGenBangOpe =
true;
434 Contexts.back().IsExpression =
true;
437 if (!parseTableGenDAGArg())
439 return parseTableGenDAGArgAndList(&OpeningParen);
446 if (PrevNonComment && OpeningParen.is(TT_Unknown)) {
447 if (PrevNonComment->isAttribute()) {
448 OpeningParen.setType(TT_AttributeLParen);
449 }
else if (PrevNonComment->isOneOf(TT_TypenameMacro, tok::kw_decltype,
452#include
"clang/Basic/TransformTypeTraits.def"
454 OpeningParen.setType(TT_TypeDeclarationParen);
456 if (PrevNonComment->isOneOf(tok::kw_decltype, tok::kw_typeof))
457 Contexts.back().IsExpression =
true;
461 if (StartsObjCSelector)
462 OpeningParen.setType(TT_ObjCSelector);
464 const bool IsStaticAssert =
465 PrevNonComment && PrevNonComment->is(tok::kw_static_assert);
467 Contexts.back().InStaticAssertFirstArgument =
true;
477 bool MightBeFunctionType = !Contexts[Contexts.size() - 2].IsExpression;
478 bool ProbablyFunctionType =
479 CurrentToken->isPointerOrReference() || CurrentToken->is(tok::caret);
480 bool HasMultipleLines =
false;
481 bool HasMultipleParametersOnALine =
false;
482 bool MightBeObjCForRangeLoop =
483 OpeningParen.Previous && OpeningParen.Previous->is(tok::kw_for);
485 while (CurrentToken) {
486 const auto &Prev = *CurrentToken->Previous;
487 const auto *PrevPrev = Prev.Previous;
488 if (Prev.is(TT_PointerOrReference) &&
489 PrevPrev->isOneOf(tok::l_paren, tok::coloncolon)) {
490 ProbablyFunctionType =
true;
492 if (CurrentToken->is(tok::comma))
493 MightBeFunctionType =
false;
494 if (Prev.is(TT_BinaryOperator))
495 Contexts.back().IsExpression =
true;
496 if (CurrentToken->is(tok::r_paren)) {
497 if (Prev.is(TT_PointerOrReference) &&
498 (PrevPrev == &OpeningParen || PrevPrev->is(tok::coloncolon))) {
499 MightBeFunctionType =
true;
501 if (OpeningParen.isNot(TT_CppCastLParen) && MightBeFunctionType &&
502 ProbablyFunctionType && CurrentToken->Next &&
503 (CurrentToken->Next->is(tok::l_paren) ||
504 (CurrentToken->Next->is(tok::l_square) &&
505 (Line.MustBeDeclaration ||
506 (PrevNonComment && PrevNonComment->isTypeName(LangOpts)))))) {
507 OpeningParen.setType(OpeningParen.Next->is(tok::caret)
509 : TT_FunctionTypeLParen);
511 OpeningParen.MatchingParen = CurrentToken;
512 CurrentToken->MatchingParen = &OpeningParen;
514 if (CurrentToken->Next && CurrentToken->Next->is(tok::l_brace) &&
515 OpeningParen.Previous && OpeningParen.Previous->is(tok::l_paren)) {
521 if (
Tok->is(TT_BinaryOperator) &&
Tok->isPointerOrReference())
522 Tok->setType(TT_PointerOrReference);
526 if (StartsObjCSelector) {
527 CurrentToken->setType(TT_ObjCSelector);
528 if (Contexts.back().FirstObjCSelectorName) {
529 Contexts.back().FirstObjCSelectorName->LongestObjCSelectorName =
530 Contexts.back().LongestObjCSelectorName;
534 if (OpeningParen.is(TT_AttributeLParen))
535 CurrentToken->setType(TT_AttributeRParen);
536 if (OpeningParen.is(TT_TypeDeclarationParen))
537 CurrentToken->setType(TT_TypeDeclarationParen);
538 if (OpeningParen.Previous &&
539 OpeningParen.Previous->is(TT_JavaAnnotation)) {
540 CurrentToken->setType(TT_JavaAnnotation);
542 if (OpeningParen.Previous &&
543 OpeningParen.Previous->is(TT_LeadingJavaAnnotation)) {
544 CurrentToken->setType(TT_LeadingJavaAnnotation);
547 if (!HasMultipleLines)
549 else if (HasMultipleParametersOnALine)
557 if (CurrentToken->isOneOf(tok::r_square, tok::r_brace))
560 if (CurrentToken->is(tok::l_brace) && OpeningParen.is(TT_ObjCBlockLParen))
561 OpeningParen.setType(TT_Unknown);
562 if (CurrentToken->is(tok::comma) && CurrentToken->Next &&
563 !CurrentToken->Next->HasUnescapedNewline &&
564 !CurrentToken->Next->isTrailingComment()) {
565 HasMultipleParametersOnALine =
true;
567 bool ProbablyFunctionTypeLParen =
568 (CurrentToken->is(tok::l_paren) && CurrentToken->Next &&
569 CurrentToken->Next->isOneOf(tok::star, tok::amp, tok::caret));
570 if ((Prev.isOneOf(tok::kw_const, tok::kw_auto) ||
571 Prev.isTypeName(LangOpts)) &&
572 !(CurrentToken->is(tok::l_brace) ||
573 (CurrentToken->is(tok::l_paren) && !ProbablyFunctionTypeLParen))) {
574 Contexts.back().IsExpression =
false;
576 if (CurrentToken->isOneOf(tok::semi, tok::colon)) {
577 MightBeObjCForRangeLoop =
false;
578 if (PossibleObjCForInToken) {
579 PossibleObjCForInToken->setType(TT_Unknown);
580 PossibleObjCForInToken =
nullptr;
583 if (IsIf && CurrentToken->is(tok::semi)) {
584 for (
auto *
Tok = OpeningParen.Next;
585 Tok != CurrentToken &&
586 Tok->isNoneOf(tok::equal, tok::l_paren, tok::l_brace);
588 if (
Tok->isPointerOrReference())
589 Tok->setFinalizedType(TT_PointerOrReference);
592 if (MightBeObjCForRangeLoop && CurrentToken->is(Keywords.kw_in)) {
593 PossibleObjCForInToken = CurrentToken;
594 PossibleObjCForInToken->setType(TT_ObjCForIn);
598 if (CurrentToken->is(tok::comma)) {
600 Contexts.back().InStaticAssertFirstArgument =
false;
602 Contexts.back().CanBeExpression =
true;
605 if (Style.isTableGen()) {
606 if (CurrentToken->is(tok::comma)) {
607 if (Contexts.back().IsTableGenCondOpe)
608 CurrentToken->setType(TT_TableGenCondOperatorComma);
610 }
else if (CurrentToken->is(tok::colon)) {
611 if (Contexts.back().IsTableGenCondOpe)
612 CurrentToken->setType(TT_TableGenCondOperatorColon);
616 if (!parseTableGenValue())
624 updateParameterCount(&OpeningParen,
Tok);
625 if (CurrentToken && CurrentToken->HasUnescapedNewline)
626 HasMultipleLines =
true;
632 if (!Style.isCSharp())
636 if (
Tok.Previous &&
Tok.Previous->is(tok::identifier))
640 if (
Tok.Previous &&
Tok.Previous->is(tok::r_square)) {
651 if (AttrTok->is(tok::r_square))
655 while (AttrTok && AttrTok->isNot(tok::r_square))
656 AttrTok = AttrTok->Next;
662 AttrTok = AttrTok->Next;
667 if (AttrTok->isAccessSpecifierKeyword() ||
668 AttrTok->isOneOf(tok::comment, tok::kw_class, tok::kw_static,
669 tok::l_square, Keywords.kw_internal)) {
675 AttrTok->Next->startsSequence(tok::identifier, tok::l_paren)) {
691 Left->ParentBracket = Contexts.back().ContextKind;
697 bool CppArrayTemplates =
698 IsCpp && Parent && Parent->is(TT_TemplateCloser) &&
699 (Contexts.back().CanBeExpression || Contexts.back().IsExpression ||
700 Contexts.back().ContextType == Context::TemplateArgument);
702 const bool IsInnerSquare = Contexts.back().InCpp11AttributeSpecifier;
703 const bool IsCpp11AttributeSpecifier =
704 isCppAttribute(IsCpp, *Left) || IsInnerSquare;
707 bool IsCSharpAttributeSpecifier =
708 isCSharpAttributeSpecifier(*Left) ||
709 Contexts.back().InCSharpAttributeSpecifier;
711 bool InsideInlineASM = Line.startsWith(tok::kw_asm);
712 bool IsCppStructuredBinding =
Left->isCppStructuredBinding(IsCpp);
713 bool StartsObjCMethodExpr =
714 !IsCppStructuredBinding && !InsideInlineASM && !CppArrayTemplates &&
715 IsCpp && !IsCpp11AttributeSpecifier && !IsCSharpAttributeSpecifier &&
716 Contexts.back().CanBeExpression &&
Left->isNot(TT_LambdaLSquare) &&
717 CurrentToken->isNoneOf(tok::l_brace, tok::r_square) &&
721 (!Parent || !Parent->is(tok::comma) ||
722 Contexts.back().ContextKind != tok::l_brace) &&
724 Parent->isOneOf(tok::colon, tok::l_square, tok::l_paren,
725 tok::kw_return, tok::kw_throw) ||
726 Parent->isUnaryOperator() ||
728 Parent->isOneOf(TT_ObjCForIn, TT_CastRParen) ||
731 bool ColonFound =
false;
733 unsigned BindingIncrease = 1;
734 if (IsCppStructuredBinding) {
735 Left->setType(TT_StructuredBindingLSquare);
736 }
else if (
Left->is(TT_Unknown)) {
737 if (StartsObjCMethodExpr) {
738 Left->setType(TT_ObjCMethodExpr);
739 }
else if (InsideInlineASM) {
740 Left->setType(TT_InlineASMSymbolicNameLSquare);
741 }
else if (IsCpp11AttributeSpecifier) {
742 if (!IsInnerSquare) {
743 Left->setType(TT_AttributeLSquare);
745 Left->Previous->EndsCppAttributeGroup =
false;
747 }
else if (Style.isJavaScript() && Parent &&
748 Contexts.back().ContextKind == tok::l_brace &&
749 Parent->isOneOf(tok::l_brace, tok::comma)) {
750 Left->setType(TT_JsComputedPropertyName);
751 }
else if (IsCpp && Contexts.back().ContextKind == tok::l_brace &&
752 Parent && Parent->isOneOf(tok::l_brace, tok::comma)) {
753 Left->setType(TT_DesignatedInitializerLSquare);
754 }
else if (IsCSharpAttributeSpecifier) {
755 Left->setType(TT_AttributeLSquare);
756 }
else if (CurrentToken->is(tok::r_square) && Parent &&
757 Parent->is(TT_TemplateCloser)) {
758 Left->setType(TT_ArraySubscriptLSquare);
759 }
else if (Style.isProto()) {
786 Left->setType(TT_ArrayInitializerLSquare);
787 if (!
Left->endsSequence(tok::l_square, tok::numeric_constant,
789 !
Left->endsSequence(tok::l_square, tok::numeric_constant,
791 !
Left->endsSequence(tok::l_square, tok::colon, TT_SelectorName)) {
792 Left->setType(TT_ProtoExtensionLSquare);
793 BindingIncrease = 10;
795 }
else if (!CppArrayTemplates && Parent &&
796 Parent->isOneOf(TT_BinaryOperator, TT_TemplateCloser, tok::at,
797 tok::comma, tok::l_paren, tok::l_square,
798 tok::question, tok::colon, tok::kw_return,
801 Left->setType(TT_ArrayInitializerLSquare);
803 BindingIncrease = 10;
804 Left->setType(TT_ArraySubscriptLSquare);
808 ScopedContextCreator ContextCreator(*
this, tok::l_square, BindingIncrease);
809 Contexts.back().IsExpression =
true;
810 if (Style.isJavaScript() && Parent && Parent->is(TT_JsTypeColon))
811 Contexts.back().IsExpression =
false;
813 Contexts.back().ColonIsObjCMethodExpr = StartsObjCMethodExpr;
814 Contexts.back().InCpp11AttributeSpecifier = IsCpp11AttributeSpecifier;
815 Contexts.back().InCSharpAttributeSpecifier = IsCSharpAttributeSpecifier;
817 while (CurrentToken) {
818 if (CurrentToken->is(tok::r_square)) {
819 if (IsCpp11AttributeSpecifier && !IsInnerSquare) {
820 CurrentToken->setType(TT_AttributeRSquare);
821 CurrentToken->EndsCppAttributeGroup =
true;
823 if (IsCSharpAttributeSpecifier) {
824 CurrentToken->setType(TT_AttributeRSquare);
825 }
else if (((CurrentToken->Next &&
826 CurrentToken->Next->is(tok::l_paren)) ||
827 (CurrentToken->Previous &&
828 CurrentToken->Previous->Previous == Left)) &&
829 Left->is(TT_ObjCMethodExpr)) {
834 StartsObjCMethodExpr =
false;
835 Left->setType(TT_Unknown);
837 if (StartsObjCMethodExpr && CurrentToken->Previous != Left) {
838 CurrentToken->setType(TT_ObjCMethodExpr);
841 if (!ColonFound && CurrentToken->Previous &&
842 CurrentToken->Previous->is(TT_Unknown) &&
843 canBeObjCSelectorComponent(*CurrentToken->Previous)) {
844 CurrentToken->Previous->setType(TT_SelectorName);
849 if (Parent && Parent->is(TT_PointerOrReference))
850 Parent->overwriteFixedType(TT_BinaryOperator);
852 Left->MatchingParen = CurrentToken;
853 CurrentToken->MatchingParen =
Left;
858 if (!Contexts.back().FirstObjCSelectorName) {
861 Previous->ObjCSelectorNameParts = 1;
862 Contexts.back().FirstObjCSelectorName =
Previous;
865 Left->ParameterCount =
866 Contexts.back().FirstObjCSelectorName->ObjCSelectorNameParts;
868 if (Contexts.back().FirstObjCSelectorName) {
869 Contexts.back().FirstObjCSelectorName->LongestObjCSelectorName =
870 Contexts.back().LongestObjCSelectorName;
871 if (
Left->BlockParameterCount > 1)
872 Contexts.back().FirstObjCSelectorName->LongestObjCSelectorName = 0;
874 if (Style.isTableGen() &&
Left->is(TT_TableGenListOpener))
875 CurrentToken->setType(TT_TableGenListCloser);
879 if (CurrentToken->isOneOf(tok::r_paren, tok::r_brace))
881 if (CurrentToken->is(tok::colon)) {
882 if (IsCpp11AttributeSpecifier &&
883 CurrentToken->endsSequence(tok::colon, tok::identifier,
887 CurrentToken->setType(TT_AttributeColon);
888 }
else if (!Style.isVerilog() && !Line.InPragmaDirective &&
889 Left->isOneOf(TT_ArraySubscriptLSquare,
890 TT_DesignatedInitializerLSquare)) {
891 Left->setType(TT_ObjCMethodExpr);
892 StartsObjCMethodExpr =
true;
893 Contexts.back().ColonIsObjCMethodExpr =
true;
894 if (Parent && Parent->is(tok::r_paren)) {
896 Parent->setType(TT_CastRParen);
901 if (CurrentToken->is(tok::comma) &&
Left->is(TT_ObjCMethodExpr) &&
903 Left->setType(TT_ArrayInitializerLSquare);
906 if (Style.isTableGen()) {
907 if (CurrentToken->isOneOf(tok::comma, tok::minus, tok::ellipsis)) {
913 if (!parseTableGenValue())
916 updateParameterCount(Left,
Tok);
921 updateParameterCount(Left,
Tok);
926 void skipToNextNonComment() {
928 while (CurrentToken && CurrentToken->is(tok::comment))
937 bool parseTableGenValue(
bool ParseNameMode =
false) {
940 while (CurrentToken->is(tok::comment))
942 if (!parseTableGenSimpleValue())
947 if (CurrentToken->is(tok::hash)) {
948 if (CurrentToken->Next &&
949 CurrentToken->Next->isOneOf(tok::colon, tok::semi, tok::l_brace)) {
952 CurrentToken->setType(TT_TableGenTrailingPasteOperator);
957 skipToNextNonComment();
958 HashTok->setType(TT_Unknown);
959 if (!parseTableGenValue(ParseNameMode))
966 if (ParseNameMode && CurrentToken->is(tok::l_brace))
969 if (CurrentToken->isOneOf(tok::l_brace, tok::l_square, tok::period)) {
970 CurrentToken->setType(TT_TableGenValueSuffix);
972 skipToNextNonComment();
973 if (Suffix->is(tok::l_square))
974 return parseSquare();
975 if (Suffix->is(tok::l_brace)) {
976 Scopes.push_back(getScopeType(*Suffix));
986 bool tryToParseTableGenTokVar() {
989 if (CurrentToken->is(tok::identifier) &&
990 CurrentToken->TokenText.front() ==
'$') {
991 skipToNextNonComment();
999 bool parseTableGenDAGArg(
bool AlignColon =
false) {
1000 if (tryToParseTableGenTokVar())
1002 if (parseTableGenValue()) {
1003 if (CurrentToken && CurrentToken->is(tok::colon)) {
1005 CurrentToken->setType(TT_TableGenDAGArgListColonToAlign);
1007 CurrentToken->setType(TT_TableGenDAGArgListColon);
1008 skipToNextNonComment();
1009 return tryToParseTableGenTokVar();
1020 auto &Opes = Style.TableGenBreakingDAGArgOperators;
1025 if (
Tok.isNot(tok::identifier) ||
1026 Tok.isOneOf(TT_TableGenBangOperator, TT_TableGenCondOperator)) {
1030 if (!
Tok.Next ||
Tok.Next->is(tok::colon))
1032 return llvm::is_contained(Opes,
Tok.TokenText.str());
1037 bool parseTableGenDAGArgAndList(
FormatToken *Opener) {
1039 if (!parseTableGenDAGArg())
1041 bool BreakInside =
false;
1042 if (Style.TableGenBreakInsideDAGArg != FormatStyle::DAS_DontBreak) {
1045 if (isTableGenDAGArgBreakingOperator(*FirstTok)) {
1048 Opener->setType(TT_TableGenDAGArgOpenerToBreak);
1049 if (FirstTok->isOneOf(TT_TableGenBangOperator,
1050 TT_TableGenCondOperator)) {
1053 CurrentToken->Previous->setType(TT_TableGenDAGArgOperatorToBreak);
1054 }
else if (FirstTok->is(tok::identifier)) {
1055 if (Style.TableGenBreakInsideDAGArg == FormatStyle::DAS_BreakAll)
1056 FirstTok->setType(TT_TableGenDAGArgOperatorToBreak);
1058 FirstTok->setType(TT_TableGenDAGArgOperatorID);
1063 return parseTableGenDAGArgList(Opener, BreakInside);
1068 bool parseTableGenDAGArgList(
FormatToken *Opener,
bool BreakInside) {
1069 ScopedContextCreator ContextCreator(*
this, tok::l_paren, 0);
1070 Contexts.back().IsTableGenDAGArgList =
true;
1071 bool FirstDAGArgListElm =
true;
1072 while (CurrentToken) {
1073 if (!FirstDAGArgListElm && CurrentToken->is(tok::comma)) {
1074 CurrentToken->setType(BreakInside ? TT_TableGenDAGArgListCommaToBreak
1075 : TT_TableGenDAGArgListComma);
1076 skipToNextNonComment();
1078 if (CurrentToken && CurrentToken->is(tok::r_paren)) {
1079 CurrentToken->setType(TT_TableGenDAGArgCloser);
1080 Opener->MatchingParen = CurrentToken;
1081 CurrentToken->MatchingParen = Opener;
1082 skipToNextNonComment();
1085 if (!parseTableGenDAGArg(
1087 Style.AlignConsecutiveTableGenBreakingDAGArgColons.Enabled)) {
1090 FirstDAGArgListElm =
false;
1095 bool parseTableGenSimpleValue() {
1096 assert(Style.isTableGen());
1100 skipToNextNonComment();
1102 if (
Tok->isOneOf(tok::numeric_constant, tok::string_literal,
1103 TT_TableGenMultiLineString, tok::kw_true, tok::kw_false,
1104 tok::question, tok::kw_int)) {
1108 if (
Tok->is(tok::l_brace)) {
1109 Scopes.push_back(getScopeType(*
Tok));
1110 return parseBrace();
1113 if (
Tok->is(tok::l_square)) {
1114 Tok->setType(TT_TableGenListOpener);
1117 if (
Tok->is(tok::less)) {
1118 CurrentToken->setType(TT_TemplateOpener);
1119 return parseAngle();
1125 if (
Tok->is(tok::l_paren)) {
1126 Tok->setType(TT_TableGenDAGArgOpener);
1128 if (Contexts.back().IsTableGenDAGArgList)
1129 Tok->SpacesRequiredBefore = 1;
1130 return parseTableGenDAGArgAndList(
Tok);
1133 if (
Tok->is(TT_TableGenBangOperator)) {
1134 if (CurrentToken && CurrentToken->is(tok::less)) {
1135 CurrentToken->setType(TT_TemplateOpener);
1136 skipToNextNonComment();
1140 if (!CurrentToken || CurrentToken->isNot(tok::l_paren))
1144 Contexts.back().IsTableGenBangOpe =
true;
1145 bool Result = parseParens();
1146 Contexts.back().IsTableGenBangOpe =
false;
1150 if (
Tok->is(TT_TableGenCondOperator)) {
1151 if (!CurrentToken || CurrentToken->isNot(tok::l_paren))
1154 return parseParens();
1159 if (
Tok->is(tok::identifier)) {
1161 if (CurrentToken && CurrentToken->is(tok::less)) {
1162 CurrentToken->setType(TT_TemplateOpener);
1163 skipToNextNonComment();
1164 return parseAngle();
1172 bool couldBeInStructArrayInitializer()
const {
1173 if (Contexts.size() < 2)
1177 const auto End = std::next(Contexts.rbegin(), 2);
1178 auto Last = Contexts.rbegin();
1181 if (
Last->ContextKind == tok::l_brace)
1183 return Depth == 2 &&
Last->ContextKind != tok::l_brace;
1190 assert(CurrentToken->Previous);
1191 FormatToken &OpeningBrace = *CurrentToken->Previous;
1192 assert(OpeningBrace.is(tok::l_brace));
1193 OpeningBrace.ParentBracket = Contexts.back().ContextKind;
1195 if (Contexts.back().CaretFound)
1196 OpeningBrace.overwriteFixedType(TT_ObjCBlockLBrace);
1197 Contexts.back().CaretFound =
false;
1199 ScopedContextCreator ContextCreator(*
this, tok::l_brace, 1);
1200 Contexts.back().ColonIsDictLiteral =
true;
1202 Contexts.back().IsExpression =
true;
1203 if (Style.isJavaScript() && OpeningBrace.Previous &&
1204 OpeningBrace.Previous->is(TT_JsTypeColon)) {
1205 Contexts.back().IsExpression =
false;
1207 if (Style.isVerilog() &&
1208 (!OpeningBrace.getPreviousNonComment() ||
1209 OpeningBrace.getPreviousNonComment()->isNot(Keywords.kw_apostrophe))) {
1210 Contexts.back().VerilogMayBeConcatenation =
true;
1212 if (Style.isTableGen())
1213 Contexts.back().ColonIsDictLiteral =
false;
1215 unsigned CommaCount = 0;
1216 while (CurrentToken) {
1217 assert(!Scopes.empty());
1218 if (CurrentToken->is(tok::r_brace)) {
1219 assert(Scopes.back() == getScopeType(OpeningBrace));
1221 assert(OpeningBrace.Optional == CurrentToken->Optional);
1222 OpeningBrace.MatchingParen = CurrentToken;
1223 CurrentToken->MatchingParen = &OpeningBrace;
1224 if (Style.AlignArrayOfStructures != FormatStyle::AIAS_None) {
1225 if (OpeningBrace.ParentBracket == tok::l_brace &&
1226 couldBeInStructArrayInitializer() && CommaCount > 0) {
1227 Contexts.back().ContextType = Context::StructArrayInitializer;
1233 if (CurrentToken->isOneOf(tok::r_paren, tok::r_square))
1235 updateParameterCount(&OpeningBrace, CurrentToken);
1236 if (CurrentToken->isOneOf(tok::colon, tok::l_brace, tok::less)) {
1238 if (
Previous->is(TT_JsTypeOptionalQuestion))
1240 if ((CurrentToken->is(tok::colon) && !Style.isTableGen() &&
1241 (!Contexts.back().ColonIsDictLiteral || !IsCpp)) ||
1243 OpeningBrace.setType(TT_DictLiteral);
1244 Scopes.back() = getScopeType(OpeningBrace);
1245 if (
Previous->Tok.getIdentifierInfo() ||
1246 Previous->is(tok::string_literal)) {
1247 Previous->setType(TT_SelectorName);
1250 if (CurrentToken->is(tok::colon) && OpeningBrace.is(TT_Unknown) &&
1251 !Style.isTableGen()) {
1252 OpeningBrace.setType(TT_DictLiteral);
1253 Scopes.back() = getScopeType(OpeningBrace);
1254 }
else if (Style.isJavaScript()) {
1255 OpeningBrace.overwriteFixedType(TT_DictLiteral);
1256 Scopes.back() = getScopeType(OpeningBrace);
1259 bool IsBracedListComma =
false;
1260 if (CurrentToken->is(tok::comma)) {
1261 if (Style.isJavaScript()) {
1262 OpeningBrace.overwriteFixedType(TT_DictLiteral);
1263 Scopes.back() = getScopeType(OpeningBrace);
1269 if (!consumeToken())
1271 if (IsBracedListComma)
1272 Contexts.back().IsExpression =
true;
1281 if (Current->is(tok::l_brace) && Current->is(
BK_Block))
1282 ++
Left->BlockParameterCount;
1283 if (Current->is(tok::comma)) {
1284 ++
Left->ParameterCount;
1286 Left->Role.reset(
new CommaSeparatedList(Style));
1287 Left->Role->CommaFound(Current);
1288 }
else if (
Left->ParameterCount == 0 && Current->isNot(tok::comment)) {
1289 Left->ParameterCount = 1;
1293 bool parseConditional() {
1294 while (CurrentToken) {
1295 if (CurrentToken->is(tok::colon) && CurrentToken->is(TT_Unknown)) {
1296 CurrentToken->setType(TT_ConditionalExpr);
1303 if (CurrentToken->is(tok::r_brace))
1305 if (!consumeToken())
1311 bool parseTemplateDeclaration() {
1312 if (!CurrentToken || CurrentToken->isNot(tok::less))
1315 CurrentToken->setType(TT_TemplateOpener);
1318 TemplateDeclarationDepth++;
1319 const bool WellFormed = parseAngle();
1320 TemplateDeclarationDepth--;
1324 if (CurrentToken && TemplateDeclarationDepth == 0)
1325 CurrentToken->Previous->ClosesTemplateDeclaration =
true;
1330 bool consumeToken() {
1332 const auto *Prev = CurrentToken->getPreviousNonComment();
1333 if (Prev && Prev->is(TT_AttributeRSquare) &&
1334 CurrentToken->isOneOf(tok::kw_if, tok::kw_switch, tok::kw_case,
1335 tok::kw_default, tok::kw_for, tok::kw_while) &&
1337 CurrentToken->MustBreakBefore =
true;
1344 if (
Tok->is(TT_VerilogTableItem))
1347 if (
Tok->is(TT_TableGenMultiLineString))
1349 auto *Prev =
Tok->getPreviousNonComment();
1350 auto *
Next =
Tok->getNextNonComment();
1351 switch (
bool IsIf =
false;
Tok->Tok.getKind()) {
1354 if (!Prev && Line.MustBeDeclaration)
1355 Tok->setType(TT_ObjCMethodSpecifier);
1362 if (
Tok->isTypeFinalized())
1365 if (Style.isJavaScript()) {
1366 if (Contexts.back().ColonIsForRangeExpr ||
1367 (Contexts.size() == 1 &&
1368 Line.First->isNoneOf(tok::kw_enum, tok::kw_case)) ||
1369 Contexts.back().ContextKind == tok::l_paren ||
1370 Contexts.back().ContextKind == tok::l_square ||
1371 (!Contexts.back().IsExpression &&
1372 Contexts.back().ContextKind == tok::l_brace) ||
1373 (Contexts.size() == 1 &&
1374 Line.MustBeDeclaration)) {
1375 Contexts.back().IsExpression =
false;
1376 Tok->setType(TT_JsTypeColon);
1379 }
else if (Style.isCSharp()) {
1380 if (Contexts.back().InCSharpAttributeSpecifier) {
1381 Tok->setType(TT_AttributeColon);
1384 if (Contexts.back().ContextKind == tok::l_paren) {
1385 Tok->setType(TT_CSharpNamedArgumentColon);
1388 }
else if (Style.isVerilog() &&
Tok->isNot(TT_BinaryOperator)) {
1391 if (Keywords.isVerilogEnd(*Prev) || Keywords.isVerilogBegin(*Prev)) {
1392 Tok->setType(TT_VerilogBlockLabelColon);
1393 }
else if (Contexts.back().ContextKind == tok::l_square) {
1394 Tok->setType(TT_BitFieldColon);
1395 }
else if (Contexts.back().ColonIsDictLiteral) {
1396 Tok->setType(TT_DictLiteral);
1397 }
else if (Contexts.size() == 1) {
1401 Tok->setType(TT_CaseLabelColon);
1402 if (Line.Level > 1 || (!Line.InPPDirective && Line.Level > 0))
1407 if (Line.First->is(tok::kw_asm)) {
1408 Tok->setType(TT_InlineASMColon);
1409 }
else if (Contexts.back().ColonIsDictLiteral || Style.isProto()) {
1410 Tok->setType(TT_DictLiteral);
1411 if (Style.isTextProto())
1412 Prev->setType(TT_SelectorName);
1413 }
else if (Contexts.back().ColonIsObjCMethodExpr ||
1414 Line.startsWith(TT_ObjCMethodSpecifier)) {
1415 Tok->setType(TT_ObjCMethodExpr);
1416 const auto *PrevPrev = Prev->Previous;
1419 bool UnknownIdentifierInMethodDeclaration =
1420 Line.startsWith(TT_ObjCMethodSpecifier) &&
1421 Prev->is(tok::identifier) && Prev->is(TT_Unknown);
1424 !(PrevPrev->is(TT_CastRParen) ||
1425 (PrevPrev->is(TT_ObjCMethodExpr) && PrevPrev->is(tok::colon))) ||
1426 PrevPrev->is(tok::r_square) ||
1427 Contexts.back().LongestObjCSelectorName == 0 ||
1428 UnknownIdentifierInMethodDeclaration) {
1429 Prev->setType(TT_SelectorName);
1430 if (!Contexts.back().FirstObjCSelectorName)
1431 Contexts.back().FirstObjCSelectorName = Prev;
1432 else if (Prev->ColumnWidth > Contexts.back().LongestObjCSelectorName)
1433 Contexts.back().LongestObjCSelectorName = Prev->ColumnWidth;
1434 Prev->ParameterIndex =
1435 Contexts.back().FirstObjCSelectorName->ObjCSelectorNameParts;
1436 ++Contexts.back().FirstObjCSelectorName->ObjCSelectorNameParts;
1438 }
else if (Contexts.back().ColonIsForRangeExpr) {
1439 Tok->setType(TT_RangeBasedForLoopColon);
1440 for (
auto *Token = Prev;
1441 Token && Token->isNoneOf(tok::semi, tok::l_paren);
1442 Token = Token->Previous) {
1443 if (Token->isPointerOrReference())
1444 Token->setFinalizedType(TT_PointerOrReference);
1446 }
else if (Contexts.back().ContextType == Context::C11GenericSelection) {
1447 Tok->setType(TT_GenericSelectionColon);
1448 if (Prev->isPointerOrReference())
1449 Prev->setFinalizedType(TT_PointerOrReference);
1450 }
else if ((CurrentToken && CurrentToken->is(tok::numeric_constant)) ||
1451 (Prev->is(TT_StartOfName) && !Scopes.empty() &&
1453 Tok->setType(TT_BitFieldColon);
1454 }
else if (Contexts.size() == 1 &&
1455 Line.getFirstNonComment()->isNoneOf(tok::kw_enum, tok::kw_case,
1457 !Line.startsWith(tok::kw_typedef, tok::kw_enum)) {
1458 if (Prev->isOneOf(tok::r_paren, tok::kw_noexcept) ||
1459 Prev->ClosesRequiresClause) {
1460 Tok->setType(TT_CtorInitializerColon);
1461 }
else if (Prev->is(tok::kw_try)) {
1463 FormatToken *PrevPrev = Prev->getPreviousNonComment();
1466 if (PrevPrev && PrevPrev->isOneOf(tok::r_paren, tok::kw_noexcept))
1467 Tok->setType(TT_CtorInitializerColon);
1469 Tok->setType(TT_InheritanceColon);
1470 if (Prev->isAccessSpecifierKeyword())
1473 }
else if (canBeObjCSelectorComponent(*Prev) &&
Next &&
1474 (
Next->isOneOf(tok::r_paren, tok::comma) ||
1475 (canBeObjCSelectorComponent(*
Next) &&
Next->Next &&
1476 Next->Next->is(tok::colon)))) {
1479 Tok->setType(TT_ObjCSelector);
1486 if (Style.isJavaScript() && !Contexts.back().IsExpression)
1487 Tok->setType(TT_JsTypeOperator);
1490 if (Style.isTableGen()) {
1492 if (!parseTableGenValue())
1494 if (CurrentToken && CurrentToken->is(Keywords.kw_then))
1499 CurrentToken->isOneOf(tok::kw_constexpr, tok::identifier)) {
1505 if (CurrentToken && CurrentToken->is(tok::l_paren)) {
1507 if (!parseParens(IsIf))
1512 if (Style.isJavaScript()) {
1514 if ((Prev && Prev->is(tok::period)) || (
Next &&
Next->is(tok::colon)))
1517 if (CurrentToken && CurrentToken->is(Keywords.kw_await))
1520 if (IsCpp && CurrentToken && CurrentToken->is(tok::kw_co_await))
1522 Contexts.back().ColonIsForRangeExpr =
true;
1523 if (!CurrentToken || CurrentToken->isNot(tok::l_paren))
1534 if (Prev && Prev->is(tok::r_paren) && Prev->MatchingParen &&
1535 Prev->MatchingParen->is(TT_OverloadedOperatorLParen)) {
1536 Prev->setType(TT_OverloadedOperator);
1537 Prev->MatchingParen->setType(TT_OverloadedOperator);
1538 Tok->setType(TT_OverloadedOperatorLParen);
1541 if (Style.isVerilog()) {
1547 auto IsInstancePort = [&]() {
1556 if (!Prev || !(PrevPrev = Prev->getPreviousNonComment()))
1559 if (Keywords.isVerilogIdentifier(*Prev) &&
1560 Keywords.isVerilogIdentifier(*PrevPrev)) {
1564 if (Prev->is(Keywords.kw_verilogHash) &&
1565 Keywords.isVerilogIdentifier(*PrevPrev)) {
1569 if (Keywords.isVerilogIdentifier(*Prev) && PrevPrev->is(tok::r_paren))
1572 if (Keywords.isVerilogIdentifier(*Prev) && PrevPrev->is(tok::comma)) {
1573 const FormatToken *PrevParen = PrevPrev->getPreviousNonComment();
1574 if (PrevParen && PrevParen->is(tok::r_paren) &&
1575 PrevParen->MatchingParen &&
1576 PrevParen->MatchingParen->is(TT_VerilogInstancePortLParen)) {
1583 if (IsInstancePort())
1584 Tok->setType(TT_VerilogInstancePortLParen);
1589 if (Line.MustBeDeclaration && Contexts.size() == 1 &&
1590 !Contexts.back().IsExpression && !Line.startsWith(TT_ObjCProperty) &&
1591 !Line.startsWith(tok::l_paren) &&
1592 Tok->isNoneOf(TT_TypeDeclarationParen, TT_RequiresExpressionLParen)) {
1594 (!Prev->isAttribute() &&
1595 Prev->isNoneOf(TT_RequiresClause, TT_LeadingJavaAnnotation,
1596 TT_BinaryOperator))) {
1597 Line.MightBeFunctionDecl =
true;
1598 Tok->MightBeFunctionDeclParen =
true;
1603 if (Style.isTableGen())
1604 Tok->setType(TT_TableGenListOpener);
1610 if (
Tok->is(TT_RequiresExpressionLBrace))
1612 }
else if (Style.isTextProto()) {
1613 if (Prev && Prev->isNot(TT_DictLiteral))
1614 Prev->setType(TT_SelectorName);
1616 Scopes.push_back(getScopeType(*
Tok));
1622 Tok->setType(TT_TemplateOpener);
1628 if (Style.isTextProto() ||
1629 (Style.Language == FormatStyle::LK_Proto && Prev &&
1630 Prev->isOneOf(TT_SelectorName, TT_DictLiteral))) {
1631 Tok->setType(TT_DictLiteral);
1632 if (Prev && Prev->isNot(TT_DictLiteral))
1633 Prev->setType(TT_SelectorName);
1635 if (Style.isTableGen())
1636 Tok->setType(TT_TemplateOpener);
1638 Tok->setType(TT_BinaryOperator);
1639 NonTemplateLess.insert(
Tok);
1649 if (!Scopes.empty())
1656 if (!Style.isTextProto() &&
Tok->is(TT_Unknown))
1657 Tok->setType(TT_BinaryOperator);
1658 if (Prev && Prev->is(TT_TemplateCloser))
1659 Tok->SpacesRequiredBefore = 1;
1661 case tok::kw_operator:
1662 if (Style.isProto())
1665 if (IsCpp && CurrentToken) {
1666 const auto *Info = CurrentToken->Tok.getIdentifierInfo();
1668 if (Info && !(CurrentToken->isPlacementOperator() ||
1669 CurrentToken->is(tok::kw_co_await) ||
1670 Info->isCPlusPlusOperatorKeyword())) {
1672 if (CurrentToken->startsSequence(tok::kw_decltype, tok::l_paren,
1673 tok::kw_auto, tok::r_paren)) {
1675 LParen = CurrentToken->Next->Next->Next->Next;
1678 for (LParen = CurrentToken->Next;
1679 LParen && LParen->isNot(tok::l_paren); LParen = LParen->Next) {
1680 if (LParen->isPointerOrReference())
1681 LParen->setFinalizedType(TT_PointerOrReference);
1684 if (LParen && LParen->is(tok::l_paren)) {
1685 if (!Contexts.back().IsExpression) {
1686 Tok->setFinalizedType(TT_FunctionDeclarationName);
1687 LParen->setFinalizedType(TT_FunctionDeclarationLParen);
1693 while (CurrentToken &&
1694 CurrentToken->isNoneOf(tok::l_paren, tok::semi, tok::r_paren,
1696 if (CurrentToken->isOneOf(tok::star, tok::amp))
1697 CurrentToken->setType(TT_PointerOrReference);
1698 auto Next = CurrentToken->getNextNonComment();
1701 if (
Next->is(tok::less))
1707 auto Previous = CurrentToken->getPreviousNonComment();
1709 if (CurrentToken->is(tok::comma) &&
Previous->isNot(tok::kw_operator))
1711 if (
Previous->isOneOf(TT_BinaryOperator, TT_UnaryOperator, tok::comma,
1714 Previous->isPointerOrReference()) ||
1716 Previous->TokenText.starts_with(
"\"\"")) {
1717 Previous->setType(TT_OverloadedOperator);
1718 if (CurrentToken->isOneOf(tok::less, tok::greater))
1722 if (CurrentToken && CurrentToken->is(tok::l_paren))
1723 CurrentToken->setType(TT_OverloadedOperatorLParen);
1724 if (CurrentToken && CurrentToken->Previous->is(TT_BinaryOperator))
1725 CurrentToken->Previous->setType(TT_OverloadedOperator);
1728 if (Style.isJavaScript() &&
Next &&
1729 Next->isOneOf(tok::semi, tok::comma, tok::colon, tok::r_paren,
1730 tok::r_brace, tok::r_square)) {
1735 Tok->setType(TT_JsTypeOptionalQuestion);
1740 if (Line.MustBeDeclaration && !Contexts.back().IsExpression &&
1741 Style.isJavaScript()) {
1744 if (Style.isCSharp()) {
1747 if (
Next && (
Next->isOneOf(tok::r_paren, tok::greater) ||
1748 Next->startsSequence(tok::identifier, tok::semi) ||
1749 Next->startsSequence(tok::identifier, tok::equal))) {
1750 Tok->setType(TT_CSharpNullable);
1759 if (!Contexts.back().IsExpression && Line.MustBeDeclaration &&
1760 (!
Next ||
Next->isNoneOf(tok::identifier, tok::string_literal) ||
1761 !
Next->Next ||
Next->Next->isNoneOf(tok::colon, tok::question))) {
1762 Tok->setType(TT_CSharpNullable);
1768 case tok::kw_template:
1769 parseTemplateDeclaration();
1772 switch (Contexts.back().ContextType) {
1773 case Context::CtorInitializer:
1774 Tok->setType(TT_CtorInitializerComma);
1776 case Context::InheritanceList:
1777 Tok->setType(TT_InheritanceComma);
1779 case Context::VerilogInstancePortList:
1780 Tok->setType(TT_VerilogInstancePortComma);
1783 if (Style.isVerilog() && Contexts.size() == 1 &&
1784 Line.startsWith(Keywords.kw_assign)) {
1785 Tok->setFinalizedType(TT_VerilogAssignComma);
1786 }
else if (Contexts.back().FirstStartOfName &&
1787 (Contexts.size() == 1 || startsWithInitStatement(Line))) {
1788 Contexts.back().FirstStartOfName->PartOfMultiVariableDeclStmt =
true;
1789 Line.IsMultiVariableDeclStmt =
true;
1793 if (Contexts.back().ContextType == Context::ForEachMacro)
1794 Contexts.back().IsExpression =
true;
1796 case tok::kw_default:
1798 if (Style.isVerilog() && Keywords.isVerilogEndOfLabel(*
Tok) &&
1799 (Line.Level > 1 || (!Line.InPPDirective && Line.Level > 0))) {
1803 case tok::identifier:
1804 if (
Tok->isOneOf(Keywords.kw___has_include,
1805 Keywords.kw___has_include_next)) {
1809 if (
Next &&
Next->is(tok::l_paren) && Prev &&
1810 Prev->isOneOf(tok::kw___cdecl, tok::kw___stdcall,
1811 tok::kw___fastcall, tok::kw___thiscall,
1812 tok::kw___regcall, tok::kw___vectorcall)) {
1813 Tok->setFinalizedType(TT_FunctionDeclarationName);
1814 Next->setFinalizedType(TT_FunctionDeclarationLParen);
1816 }
else if (Style.isCSharp()) {
1817 if (
Tok->is(Keywords.kw_where) &&
Next &&
Next->isNot(tok::l_paren)) {
1818 Tok->setType(TT_CSharpGenericTypeConstraint);
1819 parseCSharpGenericTypeConstraint();
1821 Line.IsContinuation =
true;
1823 }
else if (Style.isTableGen()) {
1824 if (
Tok->is(Keywords.kw_assert)) {
1825 if (!parseTableGenValue())
1827 }
else if (
Tok->isOneOf(Keywords.kw_def, Keywords.kw_defm) &&
1828 (!
Next ||
Next->isNoneOf(tok::colon, tok::l_brace))) {
1830 if (!parseTableGenValue(
true))
1834 if (Style.AllowBreakBeforeQtProperty &&
1835 Contexts.back().ContextType == Context::QtProperty &&
1836 Tok->isQtProperty()) {
1837 Tok->setFinalizedType(TT_QtProperty);
1841 if (
Tok->isNot(TT_LambdaArrow) && Prev && Prev->is(tok::kw_noexcept))
1842 Tok->setType(TT_TrailingReturnArrow);
1846 if (Style.isTableGen() && !parseTableGenValue())
1848 if (!Scopes.empty() && Scopes.back() ==
ST_Enum)
1849 Tok->setFinalizedType(TT_EnumEqual);
1857 void parseCSharpGenericTypeConstraint() {
1858 int OpenAngleBracketsCount = 0;
1859 while (CurrentToken) {
1860 if (CurrentToken->is(tok::less)) {
1862 CurrentToken->setType(TT_TemplateOpener);
1863 ++OpenAngleBracketsCount;
1865 }
else if (CurrentToken->is(tok::greater)) {
1866 CurrentToken->setType(TT_TemplateCloser);
1867 --OpenAngleBracketsCount;
1869 }
else if (CurrentToken->is(tok::comma) && OpenAngleBracketsCount == 0) {
1872 CurrentToken->setType(TT_CSharpGenericTypeConstraintComma);
1874 }
else if (CurrentToken->is(Keywords.kw_where)) {
1875 CurrentToken->setType(TT_CSharpGenericTypeConstraint);
1877 }
else if (CurrentToken->is(tok::colon)) {
1878 CurrentToken->setType(TT_CSharpGenericTypeConstraintColon);
1886 void parseIncludeDirective() {
1887 if (CurrentToken && CurrentToken->is(tok::less)) {
1889 while (CurrentToken) {
1892 if (CurrentToken->isNot(tok::comment) &&
1893 !CurrentToken->TokenText.starts_with(
"//")) {
1894 CurrentToken->setType(TT_ImplicitStringLiteral);
1901 void parseWarningOrError() {
1906 while (CurrentToken) {
1907 CurrentToken->setType(TT_ImplicitStringLiteral);
1912 void parsePragma() {
1915 CurrentToken->isOneOf(Keywords.kw_mark, Keywords.kw_option,
1916 Keywords.kw_region)) {
1917 bool IsMarkOrRegion =
1918 CurrentToken->isOneOf(Keywords.kw_mark, Keywords.kw_region);
1921 while (CurrentToken) {
1922 if (IsMarkOrRegion || CurrentToken->Previous->is(TT_BinaryOperator))
1923 CurrentToken->setType(TT_ImplicitStringLiteral);
1929 void parseHasInclude() {
1930 if (!CurrentToken || CurrentToken->isNot(tok::l_paren))
1933 parseIncludeDirective();
1937 LineType parsePreprocessorDirective() {
1938 bool IsFirstToken = CurrentToken->IsFirst;
1944 if (Style.isJavaScript() && IsFirstToken) {
1948 while (CurrentToken) {
1950 CurrentToken->setType(TT_ImplicitStringLiteral);
1956 if (CurrentToken->is(tok::numeric_constant)) {
1957 CurrentToken->SpacesRequiredBefore = 1;
1962 if (!CurrentToken->Tok.getIdentifierInfo())
1966 if (Style.isVerilog() && !Keywords.isVerilogPPDirective(*CurrentToken))
1968 switch (CurrentToken->Tok.getIdentifierInfo()->getPPKeywordID()) {
1969 case tok::pp_include:
1970 case tok::pp_include_next:
1971 case tok::pp_import:
1973 parseIncludeDirective();
1977 case tok::pp_warning:
1978 parseWarningOrError();
1980 case tok::pp_pragma:
1985 Contexts.back().IsExpression =
true;
1988 CurrentToken->SpacesRequiredBefore = 1;
1994 while (CurrentToken) {
1997 if (
Tok->is(tok::l_paren)) {
1999 }
else if (
Tok->isOneOf(Keywords.kw___has_include,
2000 Keywords.kw___has_include_next)) {
2011 NonTemplateLess.clear();
2012 if (!Line.InMacroBody && CurrentToken->is(tok::hash)) {
2016 auto Type = parsePreprocessorDirective();
2024 IdentifierInfo *Info = CurrentToken->Tok.getIdentifierInfo();
2025 if ((Style.isJava() && CurrentToken->is(Keywords.kw_package)) ||
2026 (!Style.isVerilog() && Info &&
2027 Info->getPPKeywordID() == tok::pp_import && CurrentToken->Next &&
2028 CurrentToken->Next->isOneOf(tok::string_literal, tok::identifier,
2031 parseIncludeDirective();
2037 if (CurrentToken->is(tok::less) && Line.Last->is(tok::greater)) {
2038 parseIncludeDirective();
2044 if (Style.Language == FormatStyle::LK_Proto && Line.Level == 0 &&
2045 CurrentToken->isOneOf(Keywords.kw_option, Keywords.kw_package)) {
2047 if (CurrentToken && CurrentToken->is(tok::identifier)) {
2048 while (CurrentToken)
2054 bool KeywordVirtualFound =
false;
2055 bool ImportStatement =
false;
2058 if (Style.isJavaScript() && CurrentToken->is(Keywords.kw_import))
2059 ImportStatement =
true;
2061 while (CurrentToken) {
2062 if (CurrentToken->is(tok::kw_virtual))
2063 KeywordVirtualFound =
true;
2064 if (Style.isJavaScript()) {
2071 if (Line.First->is(tok::kw_export) &&
2072 CurrentToken->is(Keywords.kw_from) && CurrentToken->Next &&
2073 CurrentToken->Next->isStringLiteral()) {
2074 ImportStatement =
true;
2076 if (isClosureImportStatement(*CurrentToken))
2077 ImportStatement =
true;
2079 if (!consumeToken())
2087 if (KeywordVirtualFound)
2089 if (ImportStatement)
2092 if (Line.startsWith(TT_ObjCMethodSpecifier)) {
2093 if (Contexts.back().FirstObjCSelectorName) {
2094 Contexts.back().FirstObjCSelectorName->LongestObjCSelectorName =
2095 Contexts.back().LongestObjCSelectorName;
2100 for (
const auto &ctx : Contexts)
2101 if (ctx.ContextType == Context::StructArrayInitializer)
2111 return Tok.TokenText ==
"goog" &&
Tok.Next &&
Tok.Next->is(tok::period) &&
2113 (
Tok.Next->Next->TokenText ==
"module" ||
2114 Tok.Next->Next->TokenText ==
"provide" ||
2115 Tok.Next->Next->TokenText ==
"require" ||
2116 Tok.Next->Next->TokenText ==
"requireType" ||
2117 Tok.Next->Next->TokenText ==
"forwardDeclare") &&
2118 Tok.Next->Next->Next &&
Tok.Next->Next->Next->is(tok::l_paren);
2121 void resetTokenMetadata() {
2127 if (!CurrentToken->isTypeFinalized() &&
2128 CurrentToken->isNoneOf(
2129 TT_LambdaLSquare, TT_LambdaLBrace, TT_AttributeMacro, TT_IfMacro,
2130 TT_ForEachMacro, TT_TypenameMacro, TT_FunctionLBrace,
2131 TT_ImplicitStringLiteral, TT_InlineASMBrace, TT_FatArrow,
2132 TT_LambdaArrow, TT_NamespaceMacro, TT_OverloadedOperator,
2133 TT_RegexLiteral, TT_TemplateString, TT_ObjCStringLiteral,
2134 TT_UntouchableMacroFunc, TT_StatementAttributeLikeMacro,
2135 TT_FunctionLikeOrFreestandingMacro, TT_ClassLBrace, TT_EnumLBrace,
2136 TT_RecordLBrace, TT_StructLBrace, TT_UnionLBrace, TT_RequiresClause,
2137 TT_RequiresClauseInARequiresExpression, TT_RequiresExpression,
2138 TT_RequiresExpressionLParen, TT_RequiresExpressionLBrace,
2139 TT_CompoundRequirementLBrace, TT_BracedListLBrace,
2140 TT_FunctionLikeMacro)) {
2141 CurrentToken->setType(TT_Unknown);
2143 CurrentToken->Role.reset();
2144 CurrentToken->MatchingParen =
nullptr;
2145 CurrentToken->FakeLParens.clear();
2146 CurrentToken->FakeRParens = 0;
2153 CurrentToken->NestingLevel = Contexts.size() - 1;
2154 CurrentToken->BindingStrength = Contexts.back().BindingStrength;
2155 modifyContext(*CurrentToken);
2156 determineTokenType(*CurrentToken);
2157 CurrentToken = CurrentToken->Next;
2159 resetTokenMetadata();
2167 : ContextKind(ContextKind), BindingStrength(BindingStrength),
2168 IsExpression(IsExpression) {}
2171 unsigned BindingStrength;
2173 unsigned LongestObjCSelectorName = 0;
2174 bool ColonIsForRangeExpr =
false;
2175 bool ColonIsDictLiteral =
false;
2176 bool ColonIsObjCMethodExpr =
false;
2179 bool CanBeExpression =
true;
2180 bool CaretFound =
false;
2181 bool InCpp11AttributeSpecifier =
false;
2182 bool InCSharpAttributeSpecifier =
false;
2183 bool InStaticAssertFirstArgument =
false;
2184 bool VerilogAssignmentFound =
false;
2187 bool VerilogMayBeConcatenation =
false;
2188 bool IsTableGenDAGArgList =
false;
2189 bool IsTableGenBangOpe =
false;
2190 bool IsTableGenCondOpe =
false;
2203 StructArrayInitializer,
2207 C11GenericSelection,
2210 VerilogInstancePortList,
2211 } ContextType = Unknown;
2216 struct ScopedContextCreator {
2217 AnnotatingParser &P;
2219 ScopedContextCreator(AnnotatingParser &P,
tok::TokenKind ContextKind,
2222 P.Contexts.push_back(Context(ContextKind,
2223 P.Contexts.back().BindingStrength + Increase,
2224 P.Contexts.back().IsExpression));
2227 ~ScopedContextCreator() {
2228 if (P.Style.AlignArrayOfStructures != FormatStyle::AIAS_None) {
2229 if (P.Contexts.back().ContextType == Context::StructArrayInitializer) {
2230 P.Contexts.pop_back();
2231 P.Contexts.back().ContextType = Context::StructArrayInitializer;
2235 P.Contexts.pop_back();
2240 auto AssignmentStartsExpression = [&]() {
2244 if (Line.First->isOneOf(tok::kw_using, tok::kw_return))
2246 if (Line.First->is(tok::kw_template)) {
2247 assert(Current.Previous);
2248 if (Current.Previous->is(tok::kw_operator)) {
2256 if (
Tok->isNot(TT_TemplateOpener)) {
2263 if (Contexts.back().ContextKind == tok::less) {
2264 assert(Current.Previous->Previous);
2265 return Current.Previous->Previous->isNoneOf(tok::kw_typename,
2269 Tok =
Tok->MatchingParen;
2272 Tok =
Tok->getNextNonComment();
2276 if (
Tok->isOneOf(tok::kw_class, tok::kw_enum, tok::kw_struct,
2286 if (Style.isJavaScript() &&
2287 (Line.startsWith(Keywords.kw_type, tok::identifier) ||
2288 Line.startsWith(tok::kw_export, Keywords.kw_type,
2289 tok::identifier))) {
2293 return !Current.Previous || Current.Previous->isNot(tok::kw_operator);
2296 if (AssignmentStartsExpression()) {
2297 Contexts.back().IsExpression =
true;
2298 if (!Line.startsWith(TT_UnaryOperator)) {
2301 Previous->Previous->isNoneOf(tok::comma, tok::semi);
2303 if (
Previous->isOneOf(tok::r_square, tok::r_paren, tok::greater)) {
2310 if (
Previous->isOneOf(TT_BinaryOperator, TT_UnaryOperator) &&
2312 Previous->Previous->isNot(tok::equal)) {
2313 Previous->setType(TT_PointerOrReference);
2317 }
else if (Current.is(tok::lessless) &&
2318 (!Current.Previous ||
2319 Current.Previous->isNot(tok::kw_operator))) {
2320 Contexts.back().IsExpression =
true;
2321 }
else if (Current.isOneOf(tok::kw_return, tok::kw_throw)) {
2322 Contexts.back().IsExpression =
true;
2323 }
else if (Current.is(TT_TrailingReturnArrow)) {
2324 Contexts.back().IsExpression =
false;
2325 }
else if (Current.isOneOf(TT_LambdaArrow, Keywords.kw_assert)) {
2326 Contexts.back().IsExpression = Style.isJava();
2327 }
else if (Current.Previous &&
2328 Current.Previous->is(TT_CtorInitializerColon)) {
2329 Contexts.back().IsExpression =
true;
2330 Contexts.back().ContextType = Context::CtorInitializer;
2331 }
else if (Current.Previous && Current.Previous->is(TT_InheritanceColon)) {
2332 Contexts.back().ContextType = Context::InheritanceList;
2333 }
else if (Current.isOneOf(tok::r_paren, tok::greater, tok::comma)) {
2337 Previous->setType(TT_PointerOrReference);
2339 if (Line.MustBeDeclaration &&
2340 Contexts.front().ContextType != Context::CtorInitializer) {
2341 Contexts.back().IsExpression =
false;
2343 }
else if (Current.is(tok::kw_new)) {
2344 Contexts.back().CanBeExpression =
false;
2345 }
else if (Current.is(tok::semi) ||
2346 (Current.is(tok::exclaim) && Current.Previous &&
2347 Current.Previous->isNot(tok::kw_operator))) {
2351 Contexts.back().IsExpression =
true;
2359 if (Current->is(tok::l_paren))
2361 if (Current->is(tok::r_paren))
2365 Current = Current->Next;
2370 static bool isDeductionGuide(
FormatToken &Current) {
2372 if (Current.Previous && Current.Previous->is(tok::r_paren) &&
2373 Current.startsSequence(tok::arrow, tok::identifier, tok::less)) {
2377 while (TemplateCloser) {
2379 if (TemplateCloser->is(tok::l_paren)) {
2381 TemplateCloser = untilMatchingParen(TemplateCloser);
2382 if (!TemplateCloser)
2385 if (TemplateCloser->is(tok::less))
2387 if (TemplateCloser->is(tok::greater))
2391 TemplateCloser = TemplateCloser->Next;
2395 if (TemplateCloser && TemplateCloser->Next &&
2396 TemplateCloser->Next->is(tok::semi) &&
2397 Current.Previous->MatchingParen) {
2401 Current.Previous->MatchingParen->Previous;
2403 return LeadingIdentifier &&
2404 LeadingIdentifier->TokenText == Current.Next->TokenText;
2411 if (Current.isNot(TT_Unknown)) {
2416 if ((Style.isJavaScript() || Style.isCSharp()) &&
2417 Current.is(tok::exclaim)) {
2418 if (Current.Previous) {
2420 Style.isJavaScript()
2421 ? Keywords.isJavaScriptIdentifier(
2422 *Current.Previous,
true)
2423 : Current.Previous->is(tok::identifier);
2425 Current.Previous->isOneOf(
2426 tok::kw_default, tok::kw_namespace, tok::r_paren, tok::r_square,
2427 tok::r_brace, tok::kw_false, tok::kw_true, Keywords.kw_type,
2428 Keywords.kw_get, Keywords.kw_init, Keywords.kw_set) ||
2429 Current.Previous->Tok.isLiteral()) {
2430 Current.setType(TT_NonNullAssertion);
2435 Current.Next->isOneOf(TT_BinaryOperator, Keywords.kw_as)) {
2436 Current.setType(TT_NonNullAssertion);
2444 if ((Style.isJavaScript() || Style.isJava()) &&
2445 Current.is(Keywords.kw_instanceof)) {
2446 Current.setType(TT_BinaryOperator);
2447 }
else if (isStartOfName(Current) &&
2448 (!Line.MightBeFunctionDecl || Current.NestingLevel != 0)) {
2449 Contexts.back().FirstStartOfName = &Current;
2450 Current.setType(TT_StartOfName);
2451 }
else if (Current.is(tok::semi)) {
2455 Contexts.back().FirstStartOfName =
nullptr;
2456 }
else if (Current.isOneOf(tok::kw_auto, tok::kw___auto_type)) {
2458 }
else if (Current.is(tok::arrow) && Style.isJava()) {
2459 Current.setType(TT_LambdaArrow);
2460 }
else if (Current.is(tok::arrow) && Style.isVerilog()) {
2462 Current.setType(TT_BinaryOperator);
2463 }
else if (Current.is(tok::arrow) && AutoFound &&
2464 Line.MightBeFunctionDecl && Current.NestingLevel == 0 &&
2465 Current.Previous->isNoneOf(tok::kw_operator, tok::identifier)) {
2467 Current.setType(TT_TrailingReturnArrow);
2468 }
else if (Current.is(tok::arrow) && Current.Previous &&
2469 Current.Previous->is(tok::r_brace) &&
2473 Current.setType(TT_TrailingReturnArrow);
2474 }
else if (isDeductionGuide(Current)) {
2476 Current.setType(TT_TrailingReturnArrow);
2477 }
else if (Current.isPointerOrReference()) {
2478 Current.setType(determineStarAmpUsage(
2480 (Contexts.back().CanBeExpression && Contexts.back().IsExpression) ||
2481 Contexts.back().InStaticAssertFirstArgument,
2482 Contexts.back().ContextType == Context::TemplateArgument));
2483 }
else if (Current.isOneOf(tok::minus, tok::plus, tok::caret) ||
2484 (Style.isVerilog() && Current.is(tok::pipe))) {
2485 Current.setType(determinePlusMinusCaretUsage(Current));
2486 if (Current.is(TT_UnaryOperator) && Current.is(tok::caret))
2487 Contexts.back().CaretFound =
true;
2488 }
else if (Current.isOneOf(tok::minusminus, tok::plusplus)) {
2489 Current.setType(determineIncrementUsage(Current));
2490 }
else if (Current.isOneOf(tok::exclaim, tok::tilde)) {
2491 Current.setType(TT_UnaryOperator);
2492 }
else if (Current.is(tok::question)) {
2493 if (Style.isJavaScript() && Line.MustBeDeclaration &&
2494 !Contexts.back().IsExpression) {
2497 Current.setType(TT_JsTypeOptionalQuestion);
2498 }
else if (Style.isTableGen()) {
2500 Current.setType(TT_Unknown);
2502 Current.setType(TT_ConditionalExpr);
2504 }
else if (Current.isBinaryOperator() &&
2505 (!Current.Previous || Current.Previous->isNot(tok::l_square)) &&
2506 (Current.isNot(tok::greater) && !Style.isTextProto())) {
2507 if (Style.isVerilog()) {
2508 if (Current.is(tok::lessequal) && Contexts.size() == 1 &&
2509 !Contexts.back().VerilogAssignmentFound) {
2513 Current.setFinalizedType(TT_BinaryOperator);
2516 Contexts.back().VerilogAssignmentFound =
true;
2518 Current.setType(TT_BinaryOperator);
2519 }
else if (Current.is(tok::comment)) {
2520 if (Current.TokenText.starts_with(
"/*")) {
2521 if (Current.TokenText.ends_with(
"*/")) {
2522 Current.setType(TT_BlockComment);
2526 Current.Tok.setKind(tok::unknown);
2529 Current.setType(TT_LineComment);
2531 }
else if (Current.is(tok::string_literal)) {
2532 if (Style.isVerilog() && Contexts.back().VerilogMayBeConcatenation &&
2533 Current.getPreviousNonComment() &&
2534 Current.getPreviousNonComment()->isOneOf(tok::comma, tok::l_brace) &&
2535 Current.getNextNonComment() &&
2536 Current.getNextNonComment()->isOneOf(tok::comma, tok::r_brace)) {
2537 Current.setType(TT_StringInConcatenation);
2539 }
else if (Current.is(tok::l_paren)) {
2540 if (lParenStartsCppCast(Current))
2541 Current.setType(TT_CppCastLParen);
2542 }
else if (Current.is(tok::r_paren)) {
2543 if (rParenEndsCast(Current))
2544 Current.setType(TT_CastRParen);
2545 if (Current.MatchingParen && Current.Next &&
2546 !Current.Next->isBinaryOperator() &&
2547 Current.Next->isNoneOf(
2548 tok::semi, tok::colon, tok::l_brace, tok::l_paren, tok::comma,
2549 tok::period, tok::arrow, tok::coloncolon, tok::kw_noexcept)) {
2550 if (
FormatToken *AfterParen = Current.MatchingParen->Next;
2551 AfterParen && AfterParen->isNot(tok::caret)) {
2553 if (
FormatToken *BeforeParen = Current.MatchingParen->Previous;
2554 BeforeParen && BeforeParen->is(tok::identifier) &&
2555 BeforeParen->isNot(TT_TypenameMacro) &&
2556 BeforeParen->TokenText == BeforeParen->TokenText.upper() &&
2557 (!BeforeParen->Previous ||
2558 BeforeParen->Previous->ClosesTemplateDeclaration ||
2559 BeforeParen->Previous->ClosesRequiresClause)) {
2560 Current.setType(TT_FunctionAnnotationRParen);
2564 }
else if (Current.is(tok::at) && Current.Next && !Style.isJavaScript() &&
2568 switch (Current.Next->Tok.getObjCKeywordID()) {
2569 case tok::objc_interface:
2570 case tok::objc_implementation:
2571 case tok::objc_protocol:
2572 Current.setType(TT_ObjCDecl);
2574 case tok::objc_property:
2575 Current.setType(TT_ObjCProperty);
2580 }
else if (Current.is(tok::period)) {
2581 FormatToken *PreviousNoComment = Current.getPreviousNonComment();
2582 if (PreviousNoComment &&
2583 PreviousNoComment->isOneOf(tok::comma, tok::l_brace)) {
2584 Current.setType(TT_DesignatedInitializerPeriod);
2585 }
else if (Style.isJava() && Current.Previous &&
2586 Current.Previous->isOneOf(TT_JavaAnnotation,
2587 TT_LeadingJavaAnnotation)) {
2588 Current.setType(Current.Previous->getType());
2590 }
else if (canBeObjCSelectorComponent(Current) &&
2593 Current.Previous && Current.Previous->is(TT_CastRParen) &&
2594 Current.Previous->MatchingParen &&
2595 Current.Previous->MatchingParen->Previous &&
2596 Current.Previous->MatchingParen->Previous->is(
2597 TT_ObjCMethodSpecifier)) {
2601 Current.setType(TT_SelectorName);
2602 }
else if (Current.isOneOf(tok::identifier, tok::kw_const, tok::kw_noexcept,
2603 tok::kw_requires) &&
2605 Current.Previous->isNoneOf(tok::equal, tok::at,
2606 TT_CtorInitializerComma,
2607 TT_CtorInitializerColon) &&
2608 Line.MightBeFunctionDecl && Contexts.size() == 1) {
2611 Current.setType(TT_TrailingAnnotation);
2612 }
else if ((Style.isJava() || Style.isJavaScript()) && Current.Previous) {
2613 if (Current.Previous->is(tok::at) &&
2614 Current.isNot(Keywords.kw_interface)) {
2618 Current.setType(TT_LeadingJavaAnnotation);
2620 Current.setType(TT_JavaAnnotation);
2621 }
else if (Current.Previous->is(tok::period) &&
2622 Current.Previous->isOneOf(TT_JavaAnnotation,
2623 TT_LeadingJavaAnnotation)) {
2624 Current.setType(Current.Previous->getType());
2636 if (Style.isVerilog())
2639 if (!
Tok.Previous ||
Tok.isNot(tok::identifier) ||
Tok.is(TT_ClassHeadName))
2642 if (
Tok.endsSequence(Keywords.kw_final, TT_ClassHeadName))
2645 if ((Style.isJavaScript() || Style.isJava()) &&
Tok.is(Keywords.kw_extends))
2648 if (
const auto *NextNonComment =
Tok.getNextNonComment();
2649 (!NextNonComment && !Line.InMacroBody) ||
2651 (NextNonComment->isPointerOrReference() ||
2652 NextNonComment->isOneOf(TT_ClassHeadName, tok::string_literal) ||
2653 (Line.InPragmaDirective && NextNonComment->is(tok::identifier))))) {
2657 if (
Tok.Previous->isOneOf(TT_LeadingJavaAnnotation, Keywords.kw_instanceof,
2661 if (Style.isJavaScript() &&
Tok.Previous->is(Keywords.kw_in))
2668 if (!Style.isJavaScript())
2669 while (PreviousNotConst && PreviousNotConst->is(tok::kw_const))
2670 PreviousNotConst = PreviousNotConst->getPreviousNonComment();
2672 if (!PreviousNotConst)
2675 if (PreviousNotConst->ClosesRequiresClause)
2678 if (Style.isTableGen()) {
2680 if (Keywords.isTableGenDefinition(*PreviousNotConst))
2683 if (Contexts.back().ContextKind != tok::l_brace)
2687 bool IsPPKeyword = PreviousNotConst->is(tok::identifier) &&
2688 PreviousNotConst->Previous &&
2689 PreviousNotConst->Previous->is(tok::hash);
2691 if (PreviousNotConst->is(TT_TemplateCloser)) {
2692 return PreviousNotConst && PreviousNotConst->MatchingParen &&
2693 PreviousNotConst->MatchingParen->Previous &&
2694 PreviousNotConst->MatchingParen->Previous->isNoneOf(
2695 tok::period, tok::kw_template);
2698 if ((PreviousNotConst->is(tok::r_paren) &&
2699 PreviousNotConst->is(TT_TypeDeclarationParen)) ||
2700 PreviousNotConst->is(TT_AttributeRParen)) {
2709 if (PreviousNotConst->isOneOf(tok::identifier, tok::kw_auto) &&
2710 !PreviousNotConst->endsSequence(Keywords.kw_import, tok::kw_export) &&
2711 PreviousNotConst->isNot(TT_StatementAttributeLikeMacro)) {
2716 if (PreviousNotConst->is(TT_PointerOrReference) ||
2717 PreviousNotConst->endsSequence(tok::coloncolon,
2718 TT_PointerOrReference)) {
2723 if (PreviousNotConst->isTypeName(LangOpts))
2727 if (Style.isJava() && PreviousNotConst->is(tok::r_square))
2731 return Style.isJavaScript() && PreviousNotConst->is(tok::kw_const);
2741 if (LeftOfParens && LeftOfParens->is(TT_TemplateCloser) &&
2742 LeftOfParens->MatchingParen) {
2743 auto *Prev = LeftOfParens->MatchingParen->getPreviousNonComment();
2745 Prev->isOneOf(tok::kw_const_cast, tok::kw_dynamic_cast,
2746 tok::kw_reinterpret_cast, tok::kw_static_cast)) {
2757 assert(
Tok.is(tok::r_paren));
2759 if (!
Tok.MatchingParen || !
Tok.Previous)
2763 if (!IsCpp && !Style.isCSharp() && !Style.isJava())
2766 const auto *LParen =
Tok.MatchingParen;
2767 const auto *BeforeRParen =
Tok.Previous;
2768 const auto *AfterRParen =
Tok.Next;
2771 if (BeforeRParen == LParen || !AfterRParen)
2774 if (LParen->isOneOf(TT_OverloadedOperatorLParen, TT_FunctionTypeLParen))
2777 auto *LeftOfParens = LParen->getPreviousNonComment();
2781 if (LeftOfParens->is(tok::r_paren) &&
2782 LeftOfParens->isNot(TT_CastRParen)) {
2783 if (!LeftOfParens->MatchingParen ||
2784 !LeftOfParens->MatchingParen->Previous) {
2787 LeftOfParens = LeftOfParens->MatchingParen->Previous;
2790 if (LeftOfParens->is(tok::r_square)) {
2793 if (
Tok->isNot(tok::r_square))
2796 Tok =
Tok->getPreviousNonComment();
2797 if (!
Tok ||
Tok->isNot(tok::l_square))
2800 Tok =
Tok->getPreviousNonComment();
2801 if (!
Tok ||
Tok->isNot(tok::kw_delete))
2805 if (
FormatToken *MaybeDelete = MayBeArrayDelete(LeftOfParens))
2806 LeftOfParens = MaybeDelete;
2812 if (LeftOfParens->Tok.getIdentifierInfo() && LeftOfParens->Previous &&
2813 LeftOfParens->Previous->is(tok::kw_operator)) {
2819 if (LeftOfParens->Tok.getIdentifierInfo() &&
2820 LeftOfParens->isNoneOf(Keywords.kw_in, tok::kw_return, tok::kw_case,
2821 tok::kw_delete, tok::kw_throw)) {
2827 if (LeftOfParens->isOneOf(tok::at, tok::r_square, TT_OverloadedOperator,
2828 TT_TemplateCloser, tok::ellipsis)) {
2833 if (AfterRParen->is(tok::question) ||
2834 (AfterRParen->is(tok::ampamp) && !BeforeRParen->isTypeName(LangOpts))) {
2839 if (AfterRParen->is(Keywords.kw_in) && Style.isCSharp())
2844 if (AfterRParen->isOneOf(tok::kw_noexcept, tok::kw_volatile, tok::kw_const,
2845 tok::kw_requires, tok::kw_throw, tok::arrow,
2846 Keywords.kw_override, Keywords.kw_final) ||
2847 isCppAttribute(IsCpp, *AfterRParen)) {
2853 if (Style.isJava() && AfterRParen->is(tok::l_paren))
2857 if (AfterRParen->isOneOf(tok::kw_sizeof, tok::kw_alignof) ||
2858 (AfterRParen->Tok.isLiteral() &&
2859 AfterRParen->isNot(tok::string_literal))) {
2864 if (
Tok.isNot(TT_TemplateCloser))
2866 const auto *
Less =
Tok.MatchingParen;
2869 const auto *BeforeLess =
Less->getPreviousNonComment();
2870 return BeforeLess && BeforeLess->isNot(TT_VariableTemplate);
2874 auto IsQualifiedPointerOrReference = [](
const FormatToken *T,
2875 const LangOptions &LangOpts) {
2877 assert(!T->isTypeName(LangOpts) &&
"Should have already been checked");
2881 if (T->is(TT_AttributeRParen)) {
2883 assert(T->is(tok::r_paren));
2884 assert(T->MatchingParen);
2885 assert(T->MatchingParen->is(tok::l_paren));
2886 assert(T->MatchingParen->is(TT_AttributeLParen));
2887 if (
const auto *
Tok = T->MatchingParen->Previous;
2888 Tok &&
Tok->isAttribute()) {
2892 }
else if (T->is(TT_AttributeRSquare)) {
2894 if (T->MatchingParen && T->MatchingParen->Previous) {
2895 T = T->MatchingParen->Previous;
2898 }
else if (T->canBePointerOrReferenceQualifier()) {
2904 return T && T->is(TT_PointerOrReference);
2907 bool ParensAreType = IsNonVariableTemplate(*BeforeRParen) ||
2908 BeforeRParen->is(TT_TypeDeclarationParen) ||
2909 BeforeRParen->isTypeName(LangOpts) ||
2910 IsQualifiedPointerOrReference(BeforeRParen, LangOpts);
2911 bool ParensCouldEndDecl =
2912 AfterRParen->isOneOf(tok::equal, tok::semi, tok::l_brace, tok::greater);
2913 if (ParensAreType && !ParensCouldEndDecl)
2924 for (
const auto *Token = LParen->Next; Token != &
Tok; Token = Token->Next)
2925 if (Token->is(TT_BinaryOperator))
2930 if (AfterRParen->isOneOf(tok::identifier, tok::kw_this))
2934 if (AfterRParen->is(tok::l_paren)) {
2935 for (
const auto *Prev = BeforeRParen; Prev->is(tok::identifier);) {
2936 Prev = Prev->Previous;
2937 if (Prev->is(tok::coloncolon))
2938 Prev = Prev->Previous;
2944 if (!AfterRParen->Next)
2949 if (Style.Language != FormatStyle::LK_C && AfterRParen->is(tok::l_brace) &&
2957 const bool NextIsAmpOrStar = AfterRParen->isOneOf(tok::amp, tok::star);
2958 if (!(AfterRParen->isUnaryOperator() || NextIsAmpOrStar) ||
2959 AfterRParen->is(tok::plus) ||
2960 AfterRParen->Next->isNoneOf(tok::identifier, tok::numeric_constant)) {
2964 if (NextIsAmpOrStar &&
2965 (AfterRParen->Next->is(tok::numeric_constant) || Line.InPPDirective)) {
2969 if (Line.InPPDirective && AfterRParen->is(tok::minus))
2972 const auto *Prev = BeforeRParen;
2975 if (Prev->is(tok::r_paren)) {
2976 if (Prev->is(TT_CastRParen))
2978 Prev = Prev->MatchingParen;
2981 Prev = Prev->Previous;
2982 if (!Prev || Prev->isNot(tok::r_paren))
2984 Prev = Prev->MatchingParen;
2985 return Prev && Prev->is(TT_FunctionTypeLParen);
2989 for (Prev = BeforeRParen; Prev != LParen; Prev = Prev->Previous)
2990 if (Prev->isNoneOf(tok::kw_const, tok::identifier, tok::coloncolon))
3008 if (PrevToken->isOneOf(
3009 TT_ConditionalExpr, tok::l_paren, tok::comma, tok::colon, tok::semi,
3010 tok::equal, tok::question, tok::l_square, tok::l_brace,
3011 tok::kw_case, tok::kw_co_await, tok::kw_co_return, tok::kw_co_yield,
3012 tok::kw_delete, tok::kw_return, tok::kw_throw)) {
3019 if (PrevToken->is(tok::kw_sizeof))
3023 if (PrevToken->isOneOf(TT_CastRParen, TT_UnaryOperator))
3027 if (PrevToken->is(TT_BinaryOperator))
3035 bool InTemplateArgument) {
3036 if (Style.isJavaScript())
3037 return TT_BinaryOperator;
3040 if (Style.isCSharp() &&
Tok.is(tok::ampamp))
3041 return TT_BinaryOperator;
3045 if (
Tok.is(tok::ampamp)) {
3046 const auto *Info =
Tok.Tok.getIdentifierInfo();
3047 if (Info && Info->isCPlusPlusOperatorKeyword())
3048 return TT_BinaryOperator;
3051 if (Style.isVerilog()) {
3056 if (
Tok.is(tok::star))
3057 return TT_BinaryOperator;
3058 return determineUnaryOperatorByUsage(
Tok) ? TT_UnaryOperator
3059 : TT_BinaryOperator;
3064 return TT_UnaryOperator;
3065 if (PrevToken->isTypeName(LangOpts))
3066 return TT_PointerOrReference;
3067 if (PrevToken->isPlacementOperator() &&
Tok.is(tok::ampamp))
3068 return TT_BinaryOperator;
3070 auto *NextToken =
Tok.getNextNonComment();
3072 return TT_PointerOrReference;
3073 if (NextToken->is(tok::greater))
3074 return TT_PointerOrReference;
3076 if (InTemplateArgument && NextToken->is(tok::kw_noexcept))
3077 return TT_BinaryOperator;
3079 if (NextToken->isOneOf(tok::arrow, tok::equal, tok::comma, tok::r_paren,
3080 tok::semi, TT_RequiresClause) ||
3081 (NextToken->is(tok::kw_noexcept) && !IsExpression) ||
3082 NextToken->canBePointerOrReferenceQualifier() ||
3083 (NextToken->is(tok::l_brace) && !NextToken->getNextNonComment())) {
3084 return TT_PointerOrReference;
3087 if (PrevToken->is(tok::coloncolon))
3088 return TT_PointerOrReference;
3090 if (PrevToken->is(tok::r_paren) && PrevToken->is(TT_TypeDeclarationParen))
3091 return TT_PointerOrReference;
3093 if (determineUnaryOperatorByUsage(
Tok))
3094 return TT_UnaryOperator;
3096 if (NextToken->is(tok::l_square) && NextToken->isNot(TT_LambdaLSquare))
3097 return TT_PointerOrReference;
3098 if (NextToken->is(tok::kw_operator) && !IsExpression)
3099 return TT_PointerOrReference;
3111 if (PrevToken->is(tok::r_brace) &&
Tok.is(tok::star) &&
3112 !PrevToken->MatchingParen) {
3113 return TT_PointerOrReference;
3116 if (PrevToken->endsSequence(tok::r_square, tok::l_square, tok::kw_delete))
3117 return TT_UnaryOperator;
3119 if (PrevToken->Tok.isLiteral() ||
3120 PrevToken->isOneOf(tok::r_paren, tok::r_square, tok::kw_true,
3121 tok::kw_false, tok::r_brace)) {
3122 return TT_BinaryOperator;
3126 while (NextNonParen && NextNonParen->is(tok::l_paren))
3127 NextNonParen = NextNonParen->getNextNonComment();
3128 if (NextNonParen && (NextNonParen->Tok.isLiteral() ||
3129 NextNonParen->isOneOf(tok::kw_true, tok::kw_false) ||
3130 NextNonParen->isUnaryOperator())) {
3131 return TT_BinaryOperator;
3137 if (InTemplateArgument && NextToken->Tok.isAnyIdentifier())
3138 return TT_BinaryOperator;
3142 if (
Tok.is(tok::ampamp) &&
3143 NextToken->isOneOf(tok::l_paren, tok::star, tok::amp)) {
3144 return TT_BinaryOperator;
3151 if (NextToken->Tok.isAnyIdentifier()) {
3152 auto *NextNextToken = NextToken->getNextNonComment();
3153 if (NextNextToken) {
3154 if (NextNextToken->is(tok::arrow))
3155 return TT_BinaryOperator;
3156 if (NextNextToken->isPointerOrReference() &&
3157 !NextToken->isObjCLifetimeQualifier(Style)) {
3158 NextNextToken->setFinalizedType(TT_BinaryOperator);
3159 return TT_BinaryOperator;
3166 if (IsExpression && !Contexts.back().CaretFound &&
3167 Line.getFirstNonComment()->isNot(
3168 TT_RequiresClauseInARequiresExpression)) {
3169 return TT_BinaryOperator;
3173 if (!Scopes.empty() && Scopes.back() ==
ST_Class)
3174 return TT_PointerOrReference;
3177 auto IsChainedOperatorAmpOrMember = [](
const FormatToken *token) {
3178 return !token || token->isOneOf(tok::amp, tok::period, tok::arrow,
3179 tok::arrowstar, tok::periodstar);
3184 if (
Tok.is(tok::amp) && PrevToken->Tok.isAnyIdentifier() &&
3185 IsChainedOperatorAmpOrMember(PrevToken->getPreviousNonComment()) &&
3186 NextToken && NextToken->Tok.isAnyIdentifier()) {
3187 if (
auto NextNext = NextToken->getNextNonComment();
3189 (IsChainedOperatorAmpOrMember(NextNext) || NextNext->is(tok::semi))) {
3190 return TT_BinaryOperator;
3196 return TT_BinaryOperator;
3199 return TT_PointerOrReference;
3203 if (determineUnaryOperatorByUsage(
Tok))
3204 return TT_UnaryOperator;
3208 return TT_UnaryOperator;
3210 if (PrevToken->is(tok::at))
3211 return TT_UnaryOperator;
3214 return TT_BinaryOperator;
3220 if (!PrevToken || PrevToken->is(TT_CastRParen))
3221 return TT_UnaryOperator;
3222 if (PrevToken->isOneOf(tok::r_paren, tok::r_square, tok::identifier))
3223 return TT_TrailingUnaryOperator;
3225 return TT_UnaryOperator;
3228 SmallVector<Context, 8> Contexts;
3230 const FormatStyle &Style;
3231 AnnotatedLine &Line;
3235 LangOptions LangOpts;
3236 const AdditionalKeywords &Keywords;
3238 SmallVector<ScopeType> &Scopes;
3244 llvm::SmallPtrSet<FormatToken *, 16> NonTemplateLess;
3246 int TemplateDeclarationDepth;
3254class ExpressionParser {
3256 ExpressionParser(
const FormatStyle &Style,
const AdditionalKeywords &Keywords,
3257 AnnotatedLine &Line)
3258 : Style(Style), Keywords(Keywords), Line(Line), Current(Line.
First) {}
3261 void parse(
int Precedence = 0) {
3264 while (Current && (Current->is(tok::kw_return) ||
3265 (Current->is(tok::colon) &&
3266 Current->isOneOf(TT_ObjCMethodExpr, TT_DictLiteral)))) {
3270 if (!Current || Precedence > PrecedenceArrowAndPeriod)
3275 parseConditionalExpr();
3281 if (Precedence == PrecedenceUnaryOperator) {
3282 parseUnaryOperator();
3299 if (Style.isVerilog() && Precedence ==
prec::Comma) {
3300 VerilogFirstOfType =
3301 verilogGroupDecl(VerilogFirstOfType, LatestOperator);
3305 parse(Precedence + 1);
3307 int CurrentPrecedence = getCurrentPrecedence();
3316 if (Style.BreakBinaryOperations.PerOperator.empty() &&
3317 Style.BreakBinaryOperations.Default ==
3318 FormatStyle::BBO_OnePerLine) {
3323 if (Precedence == CurrentPrecedence && Current &&
3324 Current->is(TT_SelectorName)) {
3326 addFakeParenthesis(Start,
prec::Level(Precedence));
3330 if ((Style.isCSharp() || Style.isJavaScript() || Style.isJava()) &&
3334 FormatToken *Prev = Current->getPreviousNonComment();
3335 if (Prev && Prev->is(tok::string_literal) &&
3336 (Prev == Start || Prev->endsSequence(tok::string_literal, tok::plus,
3337 TT_StringInConcatenation))) {
3338 Prev->setType(TT_StringInConcatenation);
3345 (Current->closesScope() &&
3346 (Current->MatchingParen || Current->is(TT_TemplateString))) ||
3347 (CurrentPrecedence != -1 && CurrentPrecedence < Precedence) ||
3356 if (Current->opensScope() ||
3357 Current->isOneOf(TT_RequiresClause,
3358 TT_RequiresClauseInARequiresExpression)) {
3361 while (Current && (!Current->closesScope() || Current->opensScope())) {
3368 if (CurrentPrecedence == Precedence) {
3370 LatestOperator->NextOperator = Current;
3371 LatestOperator = Current;
3375 next(Precedence > 0);
3380 if (Style.isVerilog() && Precedence ==
prec::Comma && VerilogFirstOfType)
3381 addFakeParenthesis(VerilogFirstOfType,
prec::Comma);
3383 if (LatestOperator && (Current || Precedence > 0)) {
3389 Start->Previous->isOneOf(TT_RequiresClause,
3390 TT_RequiresClauseInARequiresExpression))
3392 auto Ret = Current ? Current : Line.Last;
3393 while (!
Ret->ClosesRequiresClause &&
Ret->Previous)
3399 if (Precedence == PrecedenceArrowAndPeriod) {
3403 addFakeParenthesis(Start,
prec::Level(Precedence), End);
3411 int getCurrentPrecedence() {
3413 const FormatToken *NextNonComment = Current->getNextNonComment();
3414 if (Current->is(TT_ConditionalExpr))
3416 if (NextNonComment && Current->is(TT_SelectorName) &&
3417 (NextNonComment->isOneOf(TT_DictLiteral, TT_JsTypeColon) ||
3418 (Style.isProto() && NextNonComment->is(tok::less)))) {
3421 if (Current->is(TT_JsComputedPropertyName))
3423 if (Current->is(TT_LambdaArrow))
3425 if (Current->is(TT_FatArrow))
3427 if (Current->isOneOf(tok::semi, TT_InlineASMColon, TT_SelectorName) ||
3428 (Current->is(tok::comment) && NextNonComment &&
3429 NextNonComment->is(TT_SelectorName))) {
3432 if (Current->is(TT_RangeBasedForLoopColon))
3434 if ((Style.isJava() || Style.isJavaScript()) &&
3435 Current->is(Keywords.kw_instanceof)) {
3438 if (Style.isJavaScript() &&
3439 Current->isOneOf(Keywords.kw_in, Keywords.kw_as)) {
3442 if (Current->isOneOf(TT_BinaryOperator, tok::comma))
3443 return Current->getPrecedence();
3444 if (Current->isOneOf(tok::period, tok::arrow) &&
3445 Current->isNot(TT_TrailingReturnArrow)) {
3446 return PrecedenceArrowAndPeriod;
3448 if ((Style.isJava() || Style.isJavaScript()) &&
3449 Current->isOneOf(Keywords.kw_extends, Keywords.kw_implements,
3450 Keywords.kw_throws)) {
3455 if (Style.isVerilog() && Current->is(tok::colon))
3467 if (Start->MacroParent)
3470 Start->FakeLParens.push_back(Precedence);
3472 Start->StartsBinaryExpression =
true;
3473 if (!End && Current)
3474 End = Current->getPreviousNonComment();
3478 End->EndsBinaryExpression =
true;
3484 void parseUnaryOperator() {
3485 SmallVector<FormatToken *, 2> Tokens;
3486 while (Current && Current->is(TT_UnaryOperator)) {
3487 Tokens.push_back(Current);
3490 parse(PrecedenceArrowAndPeriod);
3497 void parseConditionalExpr() {
3498 while (Current && Current->isTrailingComment())
3502 if (!Current || Current->isNot(tok::question))
3506 if (!Current || Current->isNot(TT_ConditionalExpr))
3513 void next(
bool SkipPastLeadingComments =
true) {
3515 Current = Current->Next;
3517 (Current->NewlinesBefore == 0 || SkipPastLeadingComments) &&
3518 Current->isTrailingComment()) {
3519 Current = Current->Next;
3533 while (Start->startsSequence(tok::l_paren, tok::star)) {
3534 if (!(Start = Start->MatchingParen) ||
3535 !(Start = Start->getNextNonComment())) {
3542 if (
Tok->is(Keywords.kw_assign))
3543 Tok =
Tok->getNextNonComment();
3555 if (
Tok->is(tok::hash)) {
3560 Tok =
Tok->getNextNonComment();
3561 }
else if (
Tok->is(tok::hashhash)) {
3565 Tok =
Tok->getNextNonComment();
3566 }
else if (Keywords.isVerilogQualifier(*
Tok) ||
3567 Keywords.isVerilogIdentifier(*
Tok)) {
3571 while (
Tok &&
Tok->isOneOf(tok::period, tok::coloncolon) &&
3572 (
Tok =
Tok->getNextNonComment())) {
3573 if (Keywords.isVerilogIdentifier(*
Tok))
3574 Tok =
Tok->getNextNonComment();
3578 }
else if (
Tok->is(tok::l_paren)) {
3583 Keywords.kw_highz0, Keywords.kw_highz1, Keywords.kw_large,
3584 Keywords.kw_medium, Keywords.kw_pull0, Keywords.kw_pull1,
3585 Keywords.kw_small, Keywords.kw_strong0, Keywords.kw_strong1,
3586 Keywords.kw_supply0, Keywords.kw_supply1, Keywords.kw_weak0,
3587 Keywords.kw_weak1)) {
3588 Tok->setType(TT_VerilogStrength);
3589 Tok =
Tok->MatchingParen;
3591 Tok->setType(TT_VerilogStrength);
3592 Tok =
Tok->getNextNonComment();
3597 }
else if (
Tok->is(Keywords.kw_verilogHash)) {
3599 if (
Next->is(tok::l_paren))
3602 Tok =
Next->getNextNonComment();
3611 while (
Tok &&
Tok->is(tok::l_square) && (
Tok =
Tok->MatchingParen))
3612 Tok =
Tok->getNextNonComment();
3613 if (
Tok && (
Tok->is(tok::hash) || Keywords.isVerilogIdentifier(*
Tok)))
3622 First->setType(TT_VerilogDimensionedTypeName);
3623 }
else if (
First != Start) {
3631 if (TypedName->is(TT_Unknown))
3632 TypedName->setType(TT_StartOfName);
3634 if (FirstOfType && PreviousComma) {
3635 PreviousComma->setType(TT_VerilogTypeComma);
3636 addFakeParenthesis(FirstOfType,
prec::Comma, PreviousComma->Previous);
3639 FirstOfType = TypedName;
3646 while (Current && Current != FirstOfType) {
3647 if (Current->opensScope()) {
3658 const FormatStyle &Style;
3659 const AdditionalKeywords &Keywords;
3660 const AnnotatedLine &Line;
3670 assert(
Line->First);
3674 if (
const auto Column =
Line->First->OriginalColumn;
3678 const bool PPDirectiveOrImportStmt =
3681 if (PPDirectiveOrImportStmt)
3683 if (
const auto IndentWidth = Style.IndentWidth;
3685 Column % IndentWidth == 0) {
3692 Style.IndentPPDirectives < FormatStyle::PPDIS_BeforeHash &&
3693 PPDirectiveOrImportStmt
3695 : NextNonCommentLine->
Level;
3698 NextNonCommentLine =
Line->First->isNot(tok::r_brace) ?
Line :
nullptr;
3718 if (
Tok->isNot(tok::identifier))
3721 Tok =
Tok->getNextNonComment();
3727 if (
Tok->is(tok::coloncolon))
3728 return Tok->getNextNonComment();
3732 if (
Tok->is(TT_TemplateOpener)) {
3733 Tok =
Tok->MatchingParen;
3737 Tok =
Tok->getNextNonComment();
3742 return Tok->is(tok::coloncolon) ?
Tok->getNextNonComment() :
nullptr;
3750 Tok =
Tok->getNextNonComment()) {
3752 if (
Tok->is(TT_AttributeLSquare)) {
3753 Tok =
Tok->MatchingParen;
3761 if (
Tok->is(tok::l_paren) &&
Tok->is(TT_Unknown) &&
Tok->MatchingParen) {
3769 if (
Tok->isOneOf(tok::kw_friend, tok::kw_inline, tok::kw_virtual,
3770 tok::kw_constexpr, tok::kw_consteval, tok::kw_explicit)) {
3776 if (
Tok->is(tok::kw_template)) {
3777 Tok =
Tok->getNextNonComment();
3783 if (
Tok->isNot(TT_TemplateOpener))
3786 Tok =
Tok->MatchingParen;
3794 if (
Tok->is(tok::coloncolon)) {
3805 if (
Tok->is(tok::tilde)) {
3812 if (
Tok->isNot(tok::identifier) ||
Tok->isNot(TT_Unknown))
3823 assert(
Tok &&
Tok->is(tok::identifier));
3824 const auto *Prev =
Tok->Previous;
3826 if (Prev && Prev->is(tok::tilde))
3827 Prev = Prev->Previous;
3830 if (!Prev || (!Prev->endsSequence(tok::coloncolon, tok::identifier) &&
3831 !Prev->endsSequence(tok::coloncolon, TT_TemplateCloser))) {
3835 assert(Prev->Previous);
3836 if (Prev->Previous->is(TT_TemplateCloser) && Prev->Previous->MatchingParen) {
3837 Prev = Prev->Previous->MatchingParen;
3838 assert(Prev->Previous);
3841 return Prev->Previous->TokenText ==
Tok->TokenText;
3845 if (!
Line.InMacroBody)
3846 MacroBodyScopes.clear();
3848 auto &ScopeStack =
Line.InMacroBody ? MacroBodyScopes : Scopes;
3849 AnnotatingParser
Parser(Style,
Line, Keywords, ScopeStack);
3852 if (!
Line.Children.empty()) {
3855 for (
auto &Child :
Line.Children) {
3856 if (InRequiresExpression &&
3857 Child->First->isNoneOf(tok::kw_typename, tok::kw_requires,
3858 TT_CompoundRequirementLBrace)) {
3864 if (!ScopeStack.empty())
3865 ScopeStack.pop_back();
3878 ExpressionParser ExprParser(Style, Keywords,
Line);
3884 if (
Tok && ((!ScopeStack.empty() && ScopeStack.back() ==
ST_Class) ||
3886 Tok->setFinalizedType(TT_CtorDtorDeclName);
3887 assert(OpeningParen);
3892 if (
Line.startsWith(TT_ObjCMethodSpecifier))
3894 else if (
Line.startsWith(TT_ObjCDecl))
3896 else if (
Line.startsWith(TT_ObjCProperty))
3900 First->SpacesRequiredBefore = 1;
3901 First->CanBreakBefore =
First->MustBreakBefore;
3910 if (Current.
is(TT_FunctionDeclarationName))
3913 if (Current.
isNoneOf(tok::identifier, tok::kw_operator))
3916 const auto *Prev = Current.getPreviousNonComment();
3921 if (
const auto *PrevPrev =
Previous.getPreviousNonComment();
3922 PrevPrev && PrevPrev->is(TT_ObjCDecl)) {
3926 auto skipOperatorName =
3929 if (
Next->is(TT_OverloadedOperatorLParen))
3931 if (
Next->is(TT_OverloadedOperator))
3933 if (
Next->isPlacementOperator() ||
Next->is(tok::kw_co_await)) {
3936 Next->Next->startsSequence(tok::l_square, tok::r_square)) {
3941 if (
Next->startsSequence(tok::l_square, tok::r_square)) {
3946 if ((
Next->isTypeName(LangOpts) ||
Next->is(tok::identifier)) &&
3947 Next->Next &&
Next->Next->isPointerOrReference()) {
3952 if (
Next->is(TT_TemplateOpener) &&
Next->MatchingParen) {
3963 const bool IsCpp = LangOpts.CXXOperatorNames || LangOpts.C11;
3966 if (Current.
is(tok::kw_operator)) {
3967 if (
Line.startsWith(tok::kw_friend))
3969 if (
Previous.Tok.getIdentifierInfo() &&
3970 Previous.isNoneOf(tok::kw_return, tok::kw_co_return)) {
3975 assert(
Previous.MatchingParen->is(tok::l_paren));
3976 assert(
Previous.MatchingParen->is(TT_TypeDeclarationParen));
3985 while (
Next &&
Next->startsSequence(tok::hashhash, tok::identifier))
3988 if (
Next->is(TT_TemplateOpener) &&
Next->MatchingParen) {
3990 }
else if (
Next->is(tok::coloncolon)) {
3994 if (
Next->is(tok::kw_operator)) {
3995 Next = skipOperatorName(
Next->Next);
3998 if (
Next->isNot(tok::identifier))
4000 }
else if (isCppAttribute(IsCpp, *
Next)) {
4004 }
else if (
Next->is(tok::l_paren)) {
4013 if (!
Next ||
Next->isNot(tok::l_paren) || !
Next->MatchingParen)
4015 ClosingParen =
Next->MatchingParen;
4016 assert(ClosingParen->
is(tok::r_paren));
4018 if (
Line.Last->is(tok::l_brace))
4020 if (
Next->Next == ClosingParen)
4023 if (ClosingParen->
Next && ClosingParen->
Next->
is(TT_PointerOrReference))
4036 if (IsCpp &&
Next->Next &&
Next->Next->is(tok::identifier) &&
4037 !
Line.endsWith(tok::semi)) {
4043 if (
Tok->is(TT_TypeDeclarationParen))
4045 if (
Tok->isOneOf(tok::l_paren, TT_TemplateOpener) &&
Tok->MatchingParen) {
4046 Tok =
Tok->MatchingParen;
4049 if (
Tok->is(tok::kw_const) ||
Tok->isTypeName(LangOpts) ||
4050 Tok->isOneOf(TT_PointerOrReference, TT_StartOfName, tok::ellipsis)) {
4053 if (
Tok->isOneOf(tok::l_brace, TT_ObjCMethodExpr) ||
Tok->Tok.isLiteral())
4059bool TokenAnnotator::mustBreakForReturnType(
const AnnotatedLine &
Line)
const {
4060 assert(
Line.MightBeFunctionDecl);
4062 if ((Style.BreakAfterReturnType == FormatStyle::RTBS_TopLevel ||
4063 Style.BreakAfterReturnType == FormatStyle::RTBS_TopLevelDefinitions) &&
4068 switch (Style.BreakAfterReturnType) {
4069 case FormatStyle::RTBS_None:
4070 case FormatStyle::RTBS_Automatic:
4071 case FormatStyle::RTBS_ExceptShortType:
4073 case FormatStyle::RTBS_All:
4074 case FormatStyle::RTBS_TopLevel:
4076 case FormatStyle::RTBS_AllDefinitions:
4077 case FormatStyle::RTBS_TopLevelDefinitions:
4078 return Line.mightBeFunctionDefinition();
4084bool TokenAnnotator::mustBreakBeforeReturnType(
4086 assert(
Line.MightBeFunctionDecl);
4088 switch (Style.BreakBeforeReturnType) {
4089 case FormatStyle::BBRTS_None:
4091 case FormatStyle::BBRTS_All:
4093 case FormatStyle::BBRTS_TopLevel:
4094 return Line.Level == 0;
4095 case FormatStyle::BBRTS_AllDefinitions:
4096 return Line.mightBeFunctionDefinition();
4097 case FormatStyle::BBRTS_TopLevelDefinitions:
4098 return Line.Level == 0 &&
Line.mightBeFunctionDefinition();
4105 auto *
Tok =
Line.getFirstNonComment();
4109 if (
Tok->is(tok::kw_template)) {
4110 auto *Opener =
Tok->Next;
4111 while (Opener && Opener->isNot(TT_TemplateOpener))
4112 Opener = Opener->Next;
4113 if (!Opener || !Opener->MatchingParen)
4115 Tok = Opener->MatchingParen->Next;
4118 if (
Tok &&
Tok->is(TT_RequiresClause)) {
4119 while (
Tok && !
Tok->ClosesRequiresClause)
4127 Tok->isOneOf(tok::kw___attribute, tok::kw___declspec,
4128 TT_AttributeMacro)) {
4130 if (
Next &&
Next->is(tok::l_paren) &&
Next->MatchingParen)
4131 Tok =
Next->MatchingParen->Next;
4136 if (
Tok->is(TT_AttributeLSquare) &&
Tok->MatchingParen) {
4137 Tok =
Tok->MatchingParen->Next;
4149 Line.Computed =
true;
4157 :
Line.FirstStartColumn +
First->ColumnWidth;
4158 bool AlignArrayOfStructures =
4159 (Style.AlignArrayOfStructures != FormatStyle::AIAS_None &&
4161 if (AlignArrayOfStructures)
4162 calculateArrayInitializerColumnList(
Line);
4164 const auto *FirstNonComment =
Line.getFirstNonComment();
4165 bool SeenName =
false;
4166 bool LineIsFunctionDeclaration =
false;
4170 for (
auto *
Tok = FirstNonComment && FirstNonComment->isNot(tok::kw_using)
4171 ? FirstNonComment->Next
4174 if (
Tok->is(TT_StartOfName))
4176 if (
Tok->Previous->EndsCppAttributeGroup)
4177 AfterLastAttribute =
Tok;
4178 if (
const bool IsCtorOrDtor =
Tok->is(TT_CtorDtorDeclName);
4182 Tok->setFinalizedType(TT_FunctionDeclarationName);
4183 LineIsFunctionDeclaration =
true;
4187 assert(OpeningParen);
4188 if (OpeningParen->is(TT_Unknown))
4189 OpeningParen->setType(TT_FunctionDeclarationLParen);
4196 if ((LineIsFunctionDeclaration ||
4197 (FirstNonComment && FirstNonComment->is(TT_CtorDtorDeclName))) &&
4198 Line.endsWith(tok::semi, tok::r_brace)) {
4199 auto *
Tok =
Line.Last->Previous;
4200 while (
Tok->isNot(tok::r_brace))
4202 if (
auto *LBrace =
Tok->MatchingParen; LBrace && LBrace->is(TT_Unknown)) {
4203 assert(LBrace->is(tok::l_brace));
4206 LBrace->setFinalizedType(TT_FunctionLBrace);
4210 if (SeenName && AfterLastAttribute &&
4213 if (LineIsFunctionDeclaration)
4214 Line.ReturnTypeWrapped =
true;
4217 if (!LineIsFunctionDeclaration) {
4218 Line.ReturnTypeWrapped =
false;
4220 for (
const auto *
Tok = FirstNonComment;
Tok;
Tok =
Tok->Next) {
4221 if (
Tok->isNot(tok::kw_operator))
4225 }
while (
Tok &&
Tok->isNot(TT_OverloadedOperatorLParen));
4226 if (!
Tok || !
Tok->MatchingParen)
4228 const auto *LeftParen =
Tok;
4229 for (
Tok =
Tok->Next;
Tok &&
Tok != LeftParen->MatchingParen;
4231 if (
Tok->isNot(tok::identifier))
4234 const bool NextIsBinaryOperator =
4236 Next->Next->is(tok::identifier);
4237 if (!NextIsBinaryOperator)
4239 Next->setType(TT_BinaryOperator);
4243 }
else if (ClosingParen) {
4245 if (
Tok->is(TT_CtorInitializerColon))
4247 if (
Tok->is(tok::arrow)) {
4248 Tok->overwriteFixedType(TT_TrailingReturnArrow);
4251 if (
Tok->isNot(TT_TrailingAnnotation))
4254 if (!
Next ||
Next->isNot(tok::l_paren))
4263 if (
Line.MightBeFunctionDecl && LineIsFunctionDeclaration &&
4264 mustBreakBeforeReturnType(
Line)) {
4266 ReturnTypeStart && ReturnTypeStart != FirstNonComment &&
4267 ReturnTypeStart->
isNoneOf(TT_FunctionDeclarationName,
4268 TT_CtorDtorDeclName, tok::tilde)) {
4269 ReturnTypeStart->MustBreakBefore =
true;
4270 Line.ReturnTypeWrapped =
true;
4274 if (
First->is(TT_ElseLBrace)) {
4275 First->CanBreakBefore =
true;
4276 First->MustBreakBefore =
true;
4279 bool InFunctionDecl =
Line.MightBeFunctionDecl;
4280 bool InParameterList =
false;
4281 for (
auto *Current =
First->Next; Current; Current = Current->Next) {
4283 if (Current->is(TT_LineComment)) {
4285 Current->SpacesRequiredBefore =
4286 (Style.Cpp11BracedListStyle == FormatStyle::BLS_AlignFirstComment &&
4287 !Style.SpacesInParensOptions.Other)
4290 }
else if (Prev->
is(TT_VerilogMultiLineListLParen)) {
4291 Current->SpacesRequiredBefore = 0;
4293 Current->SpacesRequiredBefore = Style.SpacesBeforeTrailingComments;
4303 if (!Current->HasUnescapedNewline) {
4306 if (
Parameter->isOneOf(tok::comment, tok::r_brace))
4309 if (
Parameter->Previous->isNot(TT_CtorInitializerComma) &&
4317 }
else if (!Current->Finalized && Current->SpacesRequiredBefore == 0 &&
4318 spaceRequiredBefore(
Line, *Current)) {
4319 Current->SpacesRequiredBefore = 1;
4324 Current->MustBreakBefore =
true;
4326 Current->MustBreakBefore =
4327 Current->MustBreakBefore || mustBreakBefore(
Line, *Current);
4328 if (!Current->MustBreakBefore && InFunctionDecl &&
4329 Current->is(TT_FunctionDeclarationName)) {
4330 Current->MustBreakBefore = mustBreakForReturnType(
Line);
4334 Current->CanBreakBefore =
4335 !
Line.IsModuleOrImportDecl &&
4336 (Current->MustBreakBefore || canBreakBefore(
Line, *Current));
4338 if (Current->is(TT_FunctionDeclarationLParen)) {
4339 InParameterList =
true;
4340 }
else if (Current->is(tok::r_paren)) {
4341 const auto *LParen = Current->MatchingParen;
4342 if (LParen && LParen->is(TT_FunctionDeclarationLParen))
4343 InParameterList =
false;
4344 }
else if (InParameterList &&
4345 Current->endsSequence(TT_AttributeMacro,
4346 TT_PointerOrReference)) {
4347 Current->CanBreakBefore =
false;
4350 unsigned ChildSize = 0;
4353 ChildSize = LastOfChild.isTrailingComment() ? Style.ColumnLimit
4356 if (Current->MustBreakBefore || Prev->
Children.size() > 1 ||
4358 Prev->
Children[0]->First->MustBreakBefore) ||
4359 Current->IsMultiline) {
4360 Current->TotalLength = Prev->
TotalLength + Style.ColumnLimit;
4362 Current->TotalLength = Prev->
TotalLength + Current->ColumnWidth +
4363 ChildSize + Current->SpacesRequiredBefore;
4366 if ((Style.PackParameters.BinPack == FormatStyle::BPPS_UseBreakAfter &&
4369 (Style.PackArguments.BinPack == FormatStyle::BPAS_UseBreakAfter &&
4371 Prev->
isOneOf(tok::l_paren, tok::l_brace,
4372 TT_ArrayInitializerLSquare) &&
4375 for (
auto *ParamTok = Current; ParamTok && ParamTok != RParen;
4376 ParamTok = ParamTok->Next) {
4377 if (ParamTok->opensScope()) {
4378 ParamTok = ParamTok->MatchingParen;
4383 ParamTok->MustBreakBefore =
true;
4384 ParamTok->CanBreakBefore =
true;
4389 if (Current->is(TT_ControlStatementLBrace)) {
4390 if (Style.ColumnLimit > 0 &&
4391 Style.BraceWrapping.AfterControlStatement ==
4392 FormatStyle::BWACS_MultiLine &&
4393 Line.Level * Style.IndentWidth +
Line.Last->TotalLength >
4394 Style.ColumnLimit) {
4395 Current->CanBreakBefore =
true;
4396 Current->MustBreakBefore =
true;
4398 }
else if (Current->is(TT_CtorInitializerColon)) {
4399 InFunctionDecl =
false;
4411 Current->SplitPenalty = splitPenalty(
Line, *Current, InFunctionDecl);
4412 if (Style.Language == FormatStyle::LK_ObjC &&
4413 Current->is(TT_SelectorName) && Current->ParameterIndex > 0) {
4414 if (Current->ParameterIndex == 1)
4415 Current->SplitPenalty += 5 * Current->BindingStrength;
4417 Current->SplitPenalty += 20 * Current->BindingStrength;
4421 calculateUnbreakableTailLengths(
Line);
4423 for (
auto *Current =
First; Current; Current = Current->Next) {
4425 Current->Role->precomputeFormattingInfos(Current);
4426 if (Current->MatchingParen &&
4427 Current->MatchingParen->opensBlockOrBlockTypeList(Style) &&
4432 if (Current->opensBlockOrBlockTypeList(Style))
4436 LLVM_DEBUG({ printDebugInfo(
Line); });
4439void TokenAnnotator::calculateUnbreakableTailLengths(
4446 Current->
isOneOf(tok::comment, tok::string_literal)) {
4456void TokenAnnotator::calculateArrayInitializerColumnList(
4460 auto *CurrentToken =
Line.First;
4461 CurrentToken->ArrayInitializerLineStart =
true;
4463 while (CurrentToken && CurrentToken !=
Line.Last) {
4464 if (CurrentToken->is(tok::l_brace)) {
4465 CurrentToken->IsArrayInitializer =
true;
4466 if (CurrentToken->Next)
4467 CurrentToken->Next->MustBreakBefore =
true;
4469 calculateInitializerColumnList(
Line, CurrentToken->Next, Depth + 1);
4471 CurrentToken = CurrentToken->Next;
4476FormatToken *TokenAnnotator::calculateInitializerColumnList(
4478 while (CurrentToken && CurrentToken !=
Line.Last) {
4479 if (CurrentToken->is(tok::l_brace))
4481 else if (CurrentToken->is(tok::r_brace))
4483 if (Depth == 2 && CurrentToken->isOneOf(tok::l_brace, tok::comma)) {
4484 CurrentToken = CurrentToken->Next;
4487 CurrentToken->StartsColumn =
true;
4488 CurrentToken = CurrentToken->Previous;
4490 CurrentToken = CurrentToken->Next;
4492 return CurrentToken;
4497 bool InFunctionDecl)
const {
4501 if (
Left.is(tok::semi))
4505 if (Style.isJava()) {
4506 if (
Right.isOneOf(Keywords.kw_extends, Keywords.kw_throws))
4508 if (
Right.is(Keywords.kw_implements))
4510 if (
Left.is(tok::comma) &&
Left.NestingLevel == 0)
4512 }
else if (Style.isJavaScript()) {
4513 if (
Right.is(Keywords.kw_function) &&
Left.isNot(tok::comma))
4515 if (
Left.is(TT_JsTypeColon))
4517 if ((
Left.is(TT_TemplateString) &&
Left.TokenText.ends_with(
"${")) ||
4518 (
Right.is(TT_TemplateString) &&
Right.TokenText.starts_with(
"}"))) {
4522 if (
Left.opensScope() &&
Right.closesScope())
4524 }
else if (Style.Language == FormatStyle::LK_Proto) {
4525 if (
Right.is(tok::l_square))
4527 if (
Right.is(tok::period))
4531 if (
Right.is(tok::identifier) &&
Right.Next &&
Right.Next->is(TT_DictLiteral))
4533 if (
Right.is(tok::l_square)) {
4534 if (
Left.is(tok::r_square))
4537 if (
Right.is(TT_LambdaLSquare) &&
Left.is(tok::equal))
4539 if (
Right.isNoneOf(TT_ObjCMethodExpr, TT_LambdaLSquare,
4540 TT_ArrayInitializerLSquare,
4541 TT_DesignatedInitializerLSquare, TT_AttributeLSquare)) {
4546 if (
Left.is(tok::coloncolon))
4547 return Style.PenaltyBreakScopeResolution;
4548 if (
Right.isOneOf(TT_StartOfName, TT_FunctionDeclarationName,
4549 tok::kw_operator)) {
4550 if (
Line.startsWith(tok::kw_for) &&
Right.PartOfMultiVariableDeclStmt)
4552 if (
Left.is(TT_StartOfName))
4554 if (InFunctionDecl &&
Right.NestingLevel == 0)
4555 return Style.PenaltyReturnTypeOnItsOwnLine;
4558 if (
Right.is(TT_PointerOrReference))
4560 if (
Right.is(TT_LambdaArrow))
4562 if (
Left.is(tok::equal) &&
Right.is(tok::l_brace))
4564 if (
Left.is(TT_CastRParen))
4566 if (
Left.isOneOf(tok::kw_class, tok::kw_struct, tok::kw_union))
4568 if (
Left.is(tok::comment))
4571 if (
Left.isOneOf(TT_RangeBasedForLoopColon, TT_InheritanceColon,
4572 TT_CtorInitializerColon)) {
4576 if (
Right.isMemberAccess()) {
4597 const auto Penalty = Style.PenaltyBreakBeforeMemberAccess;
4599 ? std::min(Penalty, 35u)
4603 if (
Right.is(TT_TrailingAnnotation) &&
4604 (!
Right.Next ||
Right.Next->isNot(tok::l_paren))) {
4607 if (
Line.startsWith(TT_ObjCMethodSpecifier))
4614 bool is_short_annotation =
Right.TokenText.size() < 10;
4615 return (
Left.is(tok::r_paren) ? 100 : 120) + (is_short_annotation ? 50 : 0);
4619 if (
Line.startsWith(tok::kw_for) &&
Left.is(tok::equal))
4624 if (
Right.is(TT_SelectorName))
4626 if (
Left.is(tok::colon)) {
4627 if (
Left.is(TT_ObjCMethodExpr))
4628 return Line.MightBeFunctionDecl ? 50 : 500;
4629 if (
Left.is(TT_ObjCSelector))
4637 Left.Previous->isOneOf(tok::identifier, tok::greater)) {
4641 if (
Left.is(tok::l_paren) && Style.PenaltyBreakOpenParenthesis != 0)
4642 return Style.PenaltyBreakOpenParenthesis;
4643 if (
Left.is(tok::l_paren) && InFunctionDecl && Style.AlignAfterOpenBracket)
4645 if (
Left.is(tok::l_paren) &&
Left.Previous &&
4646 (
Left.Previous->isOneOf(tok::kw_for, tok::kw__Generic) ||
4647 Left.Previous->isIf())) {
4650 if (
Left.is(tok::equal) && InFunctionDecl)
4652 if (
Right.is(tok::r_brace))
4654 if (
Left.is(TT_TemplateOpener))
4656 if (
Left.opensScope()) {
4660 if (!Style.AlignAfterOpenBracket &&
4661 (
Left.ParameterCount <= 1 || Style.AllowAllArgumentsOnNextLine)) {
4664 if (
Left.is(tok::l_brace) &&
4665 Style.Cpp11BracedListStyle == FormatStyle::BLS_Block) {
4668 return Left.ParameterCount > 1 ? Style.PenaltyBreakBeforeFirstCallParameter
4671 if (
Left.is(TT_JavaAnnotation))
4674 if (
Left.is(TT_UnaryOperator))
4676 if (
Left.isOneOf(tok::plus, tok::comma) &&
Left.Previous &&
4677 Left.Previous->isLabelString() &&
4678 (
Left.NextOperator ||
Left.OperatorIndex != 0)) {
4681 if (
Right.is(tok::plus) &&
Left.isLabelString() &&
4682 (
Right.NextOperator ||
Right.OperatorIndex != 0)) {
4685 if (
Left.is(tok::comma))
4687 if (
Right.is(tok::lessless) &&
Left.isLabelString() &&
4688 (
Right.NextOperator ||
Right.OperatorIndex != 1)) {
4691 if (
Right.is(tok::lessless)) {
4693 if (
Left.isNot(tok::r_paren) ||
Right.OperatorIndex > 0) {
4699 if (
Left.ClosesTemplateDeclaration)
4700 return Style.PenaltyBreakTemplateDeclaration;
4701 if (
Left.ClosesRequiresClause)
4703 if (
Left.is(TT_ConditionalExpr))
4709 return Style.PenaltyBreakAssignment;
4716bool TokenAnnotator::spaceRequiredBeforeParens(
const FormatToken &Right)
const {
4717 if (Style.SpaceBeforeParens == FormatStyle::SBPO_Always)
4719 if (
Right.is(TT_OverloadedOperatorLParen) &&
4720 Style.SpaceBeforeParensOptions.AfterOverloadedOperator) {
4723 if (Style.SpaceBeforeParensOptions.BeforeNonEmptyParentheses &&
4724 Right.ParameterCount > 0) {
4733 if (
Left.is(tok::kw_return) &&
4734 Right.isNoneOf(tok::semi, tok::r_paren, tok::hashhash)) {
4737 if (
Left.is(tok::kw_throw) &&
Right.is(tok::l_paren) &&
Right.MatchingParen &&
4738 Right.MatchingParen->is(TT_CastRParen)) {
4741 if (
Left.is(Keywords.kw_assert) && Style.isJava())
4744 Left.is(tok::objc_property)) {
4747 if (
Right.is(tok::hashhash))
4748 return Left.is(tok::hash);
4749 if (
Left.isOneOf(tok::hashhash, tok::hash))
4750 return Right.is(tok::hash);
4751 if (Style.SpacesInParens == FormatStyle::SIPO_Custom) {
4752 if (
Left.is(tok::l_paren) &&
Right.is(tok::r_paren))
4753 return Style.SpacesInParensOptions.InEmptyParentheses;
4754 if (Style.SpacesInParensOptions.ExceptDoubleParentheses &&
4755 Left.is(tok::r_paren) &&
Right.is(tok::r_paren)) {
4756 auto *InnerLParen =
Left.MatchingParen;
4757 if (InnerLParen && InnerLParen->Previous ==
Right.MatchingParen) {
4758 InnerLParen->SpacesRequiredBefore = 0;
4763 if (
Left.is(tok::l_paren))
4765 else if (
Right.is(tok::r_paren) &&
Right.MatchingParen)
4766 LeftParen =
Right.MatchingParen;
4767 if (LeftParen && (LeftParen->is(TT_ConditionLParen) ||
4768 (LeftParen->Previous &&
4769 isKeywordWithCondition(*LeftParen->Previous)))) {
4770 return Style.SpacesInParensOptions.InConditionalStatements;
4775 if (
Left.is(tok::kw_auto) &&
Right.isOneOf(TT_LambdaLBrace, TT_FunctionLBrace,
4777 TT_FunctionTypeLParen)) {
4782 if (
Left.is(tok::kw_auto) &&
Right.isOneOf(tok::l_paren, tok::l_brace))
4785 const auto *BeforeLeft =
Left.Previous;
4788 if (
Right.is(tok::l_paren) &&
Left.is(tok::kw_co_await) && BeforeLeft &&
4789 BeforeLeft->is(tok::kw_operator)) {
4793 if (
Left.isOneOf(tok::kw_co_await, tok::kw_co_yield, tok::kw_co_return) &&
4794 Right.isNoneOf(tok::semi, tok::r_paren)) {
4798 if (
Left.is(tok::l_paren) ||
Right.is(tok::r_paren)) {
4799 return (
Right.is(TT_CastRParen) ||
4800 (
Left.MatchingParen &&
Left.MatchingParen->is(TT_CastRParen)))
4801 ? Style.SpacesInParensOptions.InCStyleCasts
4802 : Style.SpacesInParensOptions.Other;
4804 if (
Right.isOneOf(tok::semi, tok::comma))
4807 bool IsLightweightGeneric =
Right.MatchingParen &&
4808 Right.MatchingParen->Next &&
4809 Right.MatchingParen->Next->is(tok::colon);
4810 return !IsLightweightGeneric && Style.ObjCSpaceBeforeProtocolList;
4812 if (
Right.is(tok::less) &&
Left.is(tok::kw_template))
4813 return Style.SpaceAfterTemplateKeyword;
4814 if (
Left.isOneOf(tok::exclaim, tok::tilde))
4816 if (
Left.is(tok::at) &&
4817 Right.isOneOf(tok::identifier, tok::string_literal, tok::char_constant,
4818 tok::numeric_constant, tok::l_paren, tok::l_brace,
4819 tok::kw_true, tok::kw_false)) {
4822 if (
Left.is(tok::colon))
4823 return Left.isNoneOf(TT_ObjCSelector, TT_ObjCMethodExpr);
4824 if (
Left.is(tok::coloncolon))
4826 if (
Left.is(tok::less) ||
Right.isOneOf(tok::greater, tok::less)) {
4827 if (Style.isTextProto() ||
4828 (Style.Language == FormatStyle::LK_Proto &&
4829 (
Left.is(TT_DictLiteral) ||
Right.is(TT_DictLiteral)))) {
4831 if (
Left.is(tok::less) &&
Right.is(tok::greater))
4833 return Style.Cpp11BracedListStyle == FormatStyle::BLS_Block;
4836 if (
Right.isNot(TT_OverloadedOperatorLParen))
4839 if (
Right.is(tok::ellipsis)) {
4840 return Left.Tok.isLiteral() || (
Left.is(tok::identifier) && BeforeLeft &&
4841 BeforeLeft->is(tok::kw_case));
4843 if (
Left.is(tok::l_square) &&
Right.is(tok::amp))
4844 return Style.SpacesInSquareBrackets;
4845 if (
Right.is(TT_PointerOrReference)) {
4846 if (
Left.is(tok::r_paren) &&
Line.MightBeFunctionDecl) {
4847 if (!
Left.MatchingParen)
4850 Left.MatchingParen->getPreviousNonComment();
4851 if (!TokenBeforeMatchingParen ||
Left.isNot(TT_TypeDeclarationParen))
4857 if ((Style.SpaceAroundPointerQualifiers == FormatStyle::SAPQ_After ||
4858 Style.SpaceAroundPointerQualifiers == FormatStyle::SAPQ_Both) &&
4859 (
Left.is(TT_AttributeRParen) ||
4860 Left.canBePointerOrReferenceQualifier())) {
4863 if (
Left.Tok.isLiteral())
4866 if (
Left.isTypeOrIdentifier(LangOpts) &&
Right.Next &&
Right.Next->Next &&
4867 Right.Next->Next->is(TT_RangeBasedForLoopColon)) {
4868 return getTokenPointerOrReferenceAlignment(Right) !=
4869 FormatStyle::PAS_Left;
4871 return Left.isNoneOf(TT_PointerOrReference, tok::l_paren) &&
4872 (getTokenPointerOrReferenceAlignment(Right) !=
4873 FormatStyle::PAS_Left ||
4874 (
Line.IsMultiVariableDeclStmt &&
4875 (
Left.NestingLevel == 0 ||
4876 (
Left.NestingLevel == 1 && startsWithInitStatement(
Line)))));
4878 if (
Right.is(TT_FunctionTypeLParen) &&
Left.isNot(tok::l_paren) &&
4879 (
Left.isNot(TT_PointerOrReference) ||
4880 (getTokenPointerOrReferenceAlignment(Left) != FormatStyle::PAS_Right &&
4881 !
Line.IsMultiVariableDeclStmt))) {
4884 if (
Left.is(TT_PointerOrReference)) {
4887 if ((Style.SpaceAroundPointerQualifiers == FormatStyle::SAPQ_Before ||
4888 Style.SpaceAroundPointerQualifiers == FormatStyle::SAPQ_Both) &&
4889 Right.canBePointerOrReferenceQualifier()) {
4893 if (
Right.Tok.isLiteral())
4896 if (
Right.is(TT_BlockComment))
4900 if (
Right.isOneOf(Keywords.kw_override, Keywords.kw_final, tok::kw_noexcept,
4901 TT_RequiresClause) &&
4902 Right.isNot(TT_StartOfName)) {
4909 if (BeforeLeft && BeforeLeft->isTypeOrIdentifier(LangOpts) &&
Right.Next &&
4910 Right.Next->is(TT_RangeBasedForLoopColon)) {
4911 return getTokenPointerOrReferenceAlignment(Left) !=
4912 FormatStyle::PAS_Right;
4914 if (
Right.isOneOf(TT_PointerOrReference, TT_ArraySubscriptLSquare,
4918 if (getTokenPointerOrReferenceAlignment(Left) == FormatStyle::PAS_Right)
4926 if (
Line.IsMultiVariableDeclStmt &&
4927 (
Left.NestingLevel ==
Line.First->NestingLevel ||
4928 ((
Left.NestingLevel ==
Line.First->NestingLevel + 1) &&
4929 startsWithInitStatement(
Line)))) {
4934 if (BeforeLeft->is(tok::coloncolon)) {
4935 if (
Left.isNot(tok::star))
4937 assert(Style.PointerAlignment != FormatStyle::PAS_Right);
4938 if (!
Right.startsSequence(tok::identifier, tok::r_paren))
4941 const auto *LParen =
Right.Next->MatchingParen;
4942 return !LParen || LParen->isNot(TT_FunctionTypeLParen);
4944 return BeforeLeft->isNoneOf(tok::l_paren, tok::l_square);
4947 if (
Left.is(tok::ellipsis) && BeforeLeft &&
4948 BeforeLeft->isPointerOrReference()) {
4949 return Style.PointerAlignment != FormatStyle::PAS_Right;
4952 if (
Right.is(tok::star) &&
Left.is(tok::l_paren))
4954 if (
Left.is(tok::star) &&
Right.isPointerOrReference())
4956 if (
Right.isPointerOrReference()) {
4967 if (
Previous->is(tok::coloncolon)) {
4986 if (
Previous->endsSequence(tok::kw_operator))
4987 return Style.PointerAlignment != FormatStyle::PAS_Left;
4988 if (
Previous->isOneOf(tok::kw_const, tok::kw_volatile)) {
4989 return (Style.PointerAlignment != FormatStyle::PAS_Left) ||
4990 (Style.SpaceAroundPointerQualifiers ==
4991 FormatStyle::SAPQ_After) ||
4992 (Style.SpaceAroundPointerQualifiers == FormatStyle::SAPQ_Both);
4996 if (Style.isCSharp() &&
Left.is(Keywords.kw_is) &&
Right.is(tok::l_square))
4998 const auto SpaceRequiredForArrayInitializerLSquare =
4999 [](
const FormatToken &LSquareTok,
const FormatStyle &Style) {
5000 return Style.SpacesInContainerLiterals ||
5002 Style.Cpp11BracedListStyle == FormatStyle::BLS_Block &&
5003 LSquareTok.endsSequence(tok::l_square, tok::colon,
5006 if (
Left.is(tok::l_square)) {
5007 return (
Left.is(TT_ArrayInitializerLSquare) &&
Right.isNot(tok::r_square) &&
5008 SpaceRequiredForArrayInitializerLSquare(Left, Style)) ||
5009 (
Left.isOneOf(TT_ArraySubscriptLSquare, TT_StructuredBindingLSquare,
5010 TT_LambdaLSquare) &&
5011 Style.SpacesInSquareBrackets &&
Right.isNot(tok::r_square));
5013 if (
Right.is(tok::r_square)) {
5014 return Right.MatchingParen &&
5015 ((
Right.MatchingParen->is(TT_ArrayInitializerLSquare) &&
5016 SpaceRequiredForArrayInitializerLSquare(*
Right.MatchingParen,
5018 (Style.SpacesInSquareBrackets &&
5019 Right.MatchingParen->isOneOf(TT_ArraySubscriptLSquare,
5020 TT_StructuredBindingLSquare,
5021 TT_LambdaLSquare)));
5023 if (
Right.is(tok::l_square) &&
5024 Right.isNoneOf(TT_ObjCMethodExpr, TT_LambdaLSquare,
5025 TT_DesignatedInitializerLSquare,
5026 TT_StructuredBindingLSquare, TT_AttributeLSquare) &&
5027 Left.isNoneOf(tok::numeric_constant, TT_DictLiteral) &&
5028 !(
Left.isNot(tok::r_square) && Style.SpaceBeforeSquareBrackets &&
5029 Right.is(TT_ArraySubscriptLSquare))) {
5033 (
Right.is(tok::r_brace) &&
Right.MatchingParen &&
5035 return Style.Cpp11BracedListStyle == FormatStyle::BLS_Block ||
5036 Style.SpacesInParensOptions.Other;
5038 if (
Left.is(TT_BlockComment)) {
5040 return Style.isJavaScript() || !
Left.TokenText.ends_with(
"=*/");
5045 if (
Left.is(TT_TemplateCloser) &&
Right.is(TT_AttributeLSquare))
5048 if (
Right.is(tok::l_paren)) {
5050 if (
Line.MightBeFunctionDecl &&
Right.is(TT_FunctionDeclarationLParen)) {
5051 if (spaceRequiredBeforeParens(Right))
5053 const auto &Options = Style.SpaceBeforeParensOptions;
5054 return Line.mightBeFunctionDefinition()
5055 ? Options.AfterFunctionDefinitionName
5056 : Options.AfterFunctionDeclarationName;
5058 if (
Left.is(TT_TemplateCloser) &&
Right.isNot(TT_FunctionTypeLParen))
5059 return spaceRequiredBeforeParens(Right);
5060 if (
Left.isOneOf(TT_RequiresClause,
5061 TT_RequiresClauseInARequiresExpression)) {
5062 return Style.SpaceBeforeParensOptions.AfterRequiresInClause ||
5063 spaceRequiredBeforeParens(Right);
5065 if (
Left.is(TT_RequiresExpression)) {
5066 return Style.SpaceBeforeParensOptions.AfterRequiresInExpression ||
5067 spaceRequiredBeforeParens(Right);
5069 if (
Left.isOneOf(TT_AttributeRParen, TT_AttributeRSquare))
5071 if (
Left.is(TT_ForEachMacro)) {
5072 return Style.SpaceBeforeParensOptions.AfterForeachMacros ||
5073 spaceRequiredBeforeParens(Right);
5075 if (
Left.is(TT_IfMacro)) {
5076 return Style.SpaceBeforeParensOptions.AfterIfMacros ||
5077 spaceRequiredBeforeParens(Right);
5079 if (Style.SpaceBeforeParens == FormatStyle::SBPO_Custom &&
5080 Left.isPlacementOperator() &&
5081 Right.isNot(TT_OverloadedOperatorLParen) &&
5082 !(
Line.MightBeFunctionDecl &&
Left.is(TT_FunctionDeclarationName))) {
5083 const auto *RParen =
Right.MatchingParen;
5084 return Style.SpaceBeforeParensOptions.AfterPlacementOperator ||
5085 (RParen && RParen->is(TT_CastRParen));
5089 if (
Left.is(tok::semi))
5091 if (
Left.isOneOf(tok::pp_elif, tok::kw_for, tok::kw_while, tok::kw_switch,
5092 tok::kw_case, TT_ForEachMacro, TT_ObjCForIn) ||
5094 Right.is(TT_ConditionLParen)) {
5095 return Style.SpaceBeforeParensOptions.AfterControlStatements ||
5096 spaceRequiredBeforeParens(Right);
5101 if (
Right.is(TT_OverloadedOperatorLParen))
5102 return spaceRequiredBeforeParens(Right);
5106 Left.MatchingParen &&
Left.MatchingParen->is(TT_LambdaLSquare)) {
5107 return Style.SpaceBeforeParensOptions.AfterFunctionDefinitionName ||
5108 spaceRequiredBeforeParens(Right);
5110 if (!BeforeLeft || BeforeLeft->isNoneOf(tok::period, tok::arrow)) {
5111 if (
Left.isOneOf(tok::kw_try, Keywords.kw___except, tok::kw_catch)) {
5112 return Style.SpaceBeforeParensOptions.AfterControlStatements ||
5113 spaceRequiredBeforeParens(Right);
5115 if (
Left.isPlacementOperator() ||
5116 (
Left.is(tok::r_square) &&
Left.MatchingParen &&
5117 Left.MatchingParen->Previous &&
5118 Left.MatchingParen->Previous->is(tok::kw_delete))) {
5119 return Style.SpaceBeforeParens != FormatStyle::SBPO_Never ||
5120 spaceRequiredBeforeParens(Right);
5124 if (
Tok.isNot(tok::l_paren))
5126 const auto *RParen =
Tok.MatchingParen;
5129 const auto *
Next = RParen->Next;
5136 (
Left.Tok.getIdentifierInfo() ||
Left.is(tok::r_paren))) {
5137 return spaceRequiredBeforeParens(Right);
5141 if (
Left.is(tok::at) &&
Right.isNot(tok::objc_not_keyword))
5143 if (
Right.is(TT_UnaryOperator)) {
5144 return Left.isNoneOf(tok::l_paren, tok::l_square, tok::at) &&
5145 (
Left.isNot(tok::colon) ||
Left.isNot(TT_ObjCMethodExpr));
5151 if (!Style.isVerilog() &&
5152 (
Left.isOneOf(tok::identifier, tok::greater, tok::r_square,
5154 Left.isTypeName(LangOpts)) &&
5155 Right.is(tok::l_brace) &&
Right.getNextNonComment() &&
5159 if (
Left.is(tok::period) ||
Right.is(tok::period))
5163 if (
Right.is(tok::hash) &&
Left.is(tok::identifier) &&
5164 (
Left.TokenText ==
"L" ||
Left.TokenText ==
"u" ||
5165 Left.TokenText ==
"U" ||
Left.TokenText ==
"u8" ||
5166 Left.TokenText ==
"LR" ||
Left.TokenText ==
"uR" ||
5167 Left.TokenText ==
"UR" ||
Left.TokenText ==
"u8R")) {
5170 if (
Left.is(TT_TemplateCloser) &&
Left.MatchingParen &&
5171 Left.MatchingParen->Previous &&
5172 Left.MatchingParen->Previous->isOneOf(tok::period, tok::coloncolon)) {
5178 if (
Left.is(TT_TemplateCloser) &&
Right.is(tok::l_square))
5180 if (
Left.is(tok::l_brace) &&
Left.endsSequence(TT_DictLiteral, tok::at)) {
5184 if (
Right.is(tok::r_brace) &&
Right.MatchingParen &&
5185 Right.MatchingParen->endsSequence(TT_DictLiteral, tok::at)) {
5189 if (
Right.is(TT_TrailingAnnotation) &&
Right.isOneOf(tok::amp, tok::ampamp) &&
5190 Left.isOneOf(tok::kw_const, tok::kw_volatile) &&
5191 (!
Right.Next ||
Right.Next->is(tok::semi))) {
5195 return getTokenReferenceAlignment(Right) != FormatStyle::PAS_Left;
5208 return Right.hasWhitespaceBefore();
5210 const bool IsVerilog = Style.isVerilog();
5211 assert(!IsVerilog || !IsCpp);
5214 if (Keywords.isWordLike(Right, IsVerilog) &&
5215 Keywords.isWordLike(Left, IsVerilog)) {
5221 if (
Left.is(tok::star) &&
Right.is(tok::comment))
5224 if (
Left.is(tok::l_brace) &&
Right.is(tok::r_brace) &&
5225 Left.Children.empty()) {
5227 return Style.SpaceInEmptyBraces != FormatStyle::SIEB_Never;
5228 if (Style.Cpp11BracedListStyle != FormatStyle::BLS_Block) {
5229 return Style.SpacesInParens == FormatStyle::SIPO_Custom &&
5230 Style.SpacesInParensOptions.InEmptyParentheses;
5232 return Style.SpaceInEmptyBraces == FormatStyle::SIEB_Always;
5235 const auto *BeforeLeft =
Left.Previous;
5238 if (
Left.is(TT_OverloadedOperator) &&
5239 Right.isOneOf(TT_TemplateOpener, TT_TemplateCloser)) {
5243 if (
Right.is(tok::period) &&
Left.is(tok::numeric_constant))
5247 if (
Left.is(Keywords.kw_import) &&
5248 Right.isOneOf(tok::less, tok::ellipsis) &&
5249 (!BeforeLeft || BeforeLeft->is(tok::kw_export))) {
5253 if (
Left.is(Keywords.kw_import) &&
Right.is(TT_ModulePartitionColon))
5256 if (
Right.is(TT_AfterPPDirective))
5260 if (
Left.is(tok::identifier) &&
Right.is(TT_ModulePartitionColon))
5263 if (
Left.is(TT_ModulePartitionColon) &&
Right.is(tok::identifier))
5265 if (
Left.is(tok::ellipsis) &&
Right.is(tok::identifier) &&
5266 Line.First->is(Keywords.kw_import)) {
5270 if (
Left.isOneOf(TT_AttributeRParen, TT_AttributeMacro) &&
5271 Right.is(tok::coloncolon)) {
5275 if (
Left.is(tok::kw_operator))
5276 return Right.is(tok::coloncolon) || Style.SpaceAfterOperatorKeyword;
5278 !
Left.opensScope() && Style.SpaceBeforeCpp11BracedList) {
5281 if (
Left.is(tok::less) &&
Left.is(TT_OverloadedOperator) &&
5282 Right.is(TT_TemplateOpener)) {
5286 if (
Left.is(tok::identifier) &&
Right.is(tok::numeric_constant))
5287 return Right.TokenText[0] !=
'.';
5289 if (
Left.Tok.getIdentifierInfo() &&
Right.Tok.isLiteral())
5291 }
else if (Style.isProto()) {
5292 if (
Right.is(tok::period) && !(BeforeLeft && BeforeLeft->is(tok::period)) &&
5293 Left.isOneOf(Keywords.kw_optional, Keywords.kw_required,
5294 Keywords.kw_repeated, Keywords.kw_extend)) {
5297 if (
Right.is(tok::l_paren) &&
5298 Left.isOneOf(Keywords.kw_returns, Keywords.kw_option)) {
5301 if (
Right.isOneOf(tok::l_brace, tok::less) &&
Left.is(TT_SelectorName))
5304 if (
Left.is(tok::slash) ||
Right.is(tok::slash))
5306 if (
Left.MatchingParen &&
5307 Left.MatchingParen->is(TT_ProtoExtensionLSquare) &&
5308 Right.isOneOf(tok::l_brace, tok::less)) {
5309 return Style.Cpp11BracedListStyle == FormatStyle::BLS_Block;
5312 if (
Left.is(tok::percent))
5316 if (
Left.is(tok::numeric_constant) &&
Right.is(tok::percent))
5317 return Right.hasWhitespaceBefore();
5318 }
else if (Style.isJson()) {
5319 if (
Right.is(tok::colon) &&
Left.is(tok::string_literal))
5320 return Style.SpaceBeforeJsonColon;
5321 }
else if (Style.isCSharp()) {
5327 if (
Left.is(tok::kw_this) &&
Right.is(tok::l_square))
5331 if (
Left.is(tok::kw_new) &&
Right.is(tok::l_paren))
5335 if (
Right.is(tok::l_brace))
5339 if (
Left.is(tok::l_brace) &&
Right.isNot(tok::r_brace))
5342 if (
Left.isNot(tok::l_brace) &&
Right.is(tok::r_brace))
5346 if (
Left.is(TT_FatArrow) ||
Right.is(TT_FatArrow))
5350 if (
Left.is(TT_AttributeColon) ||
Right.is(TT_AttributeColon))
5354 if (
Left.is(TT_TemplateCloser) &&
Right.is(TT_StartOfName))
5358 if (
Left.is(tok::l_square) ||
Right.is(tok::r_square))
5359 return Style.SpacesInSquareBrackets;
5362 if (
Right.is(TT_CSharpNullable))
5366 if (
Right.is(TT_NonNullAssertion))
5370 if (
Left.is(tok::comma) &&
Right.is(tok::comma))
5374 if (
Left.is(Keywords.kw_var) &&
Right.is(tok::l_paren))
5378 if (
Right.is(tok::l_paren)) {
5379 if (
Left.isOneOf(tok::kw_using, Keywords.kw_async, Keywords.kw_when,
5380 Keywords.kw_lock)) {
5381 return Style.SpaceBeforeParensOptions.AfterControlStatements ||
5382 spaceRequiredBeforeParens(Right);
5388 if ((
Left.isAccessSpecifierKeyword() ||
5389 Left.isOneOf(tok::kw_virtual, tok::kw_extern, tok::kw_static,
5390 Keywords.kw_internal, Keywords.kw_abstract,
5391 Keywords.kw_sealed, Keywords.kw_override,
5392 Keywords.kw_async, Keywords.kw_unsafe)) &&
5393 Right.is(tok::l_paren)) {
5396 }
else if (Style.isJavaScript()) {
5397 if (
Left.is(TT_FatArrow))
5400 if (
Right.is(tok::l_paren) &&
Left.is(Keywords.kw_await) && BeforeLeft &&
5401 BeforeLeft->is(tok::kw_for)) {
5404 if (
Left.is(Keywords.kw_async) &&
Right.is(tok::l_paren) &&
5405 Right.MatchingParen) {
5412 if ((
Left.is(TT_TemplateString) &&
Left.TokenText.ends_with(
"${")) ||
5413 (
Right.is(TT_TemplateString) &&
Right.TokenText.starts_with(
"}"))) {
5418 if (Keywords.isJavaScriptIdentifier(Left,
5420 Right.is(TT_TemplateString)) {
5423 if (
Right.is(tok::star) &&
5424 Left.isOneOf(Keywords.kw_function, Keywords.kw_yield)) {
5427 if (
Right.isOneOf(tok::l_brace, tok::l_square) &&
5428 Left.isOneOf(Keywords.kw_function, Keywords.kw_yield,
5429 Keywords.kw_extends, Keywords.kw_implements)) {
5432 if (
Right.is(tok::l_paren)) {
5434 if (
Line.MustBeDeclaration &&
Left.Tok.getIdentifierInfo())
5438 if (BeforeLeft && BeforeLeft->is(tok::period) &&
5439 Left.Tok.getIdentifierInfo()) {
5443 if (
Left.isOneOf(tok::kw_throw, Keywords.kw_await, Keywords.kw_typeof,
5449 if (
Left.endsSequence(tok::kw_const, Keywords.kw_as))
5451 if ((
Left.isOneOf(Keywords.kw_let, Keywords.kw_var, Keywords.kw_in,
5456 (
Left.is(Keywords.kw_of) && BeforeLeft &&
5457 BeforeLeft->isOneOf(tok::identifier, tok::r_square, tok::r_brace))) &&
5458 (!BeforeLeft || BeforeLeft->isNot(tok::period))) {
5461 if (
Left.isOneOf(tok::kw_for, Keywords.kw_as) && BeforeLeft &&
5462 BeforeLeft->is(tok::period) &&
Right.is(tok::l_paren)) {
5465 if (
Left.is(Keywords.kw_as) &&
5466 Right.isOneOf(tok::l_square, tok::l_brace, tok::l_paren)) {
5469 if (
Left.is(tok::kw_default) && BeforeLeft &&
5470 BeforeLeft->is(tok::kw_export)) {
5473 if (
Left.is(Keywords.kw_is) &&
Right.is(tok::l_brace))
5475 if (
Right.isOneOf(TT_JsTypeColon, TT_JsTypeOptionalQuestion))
5477 if (
Left.is(TT_JsTypeOperator) ||
Right.is(TT_JsTypeOperator))
5479 if ((
Left.is(tok::l_brace) ||
Right.is(tok::r_brace)) &&
5480 Line.First->isOneOf(Keywords.kw_import, tok::kw_export)) {
5483 if (
Left.is(tok::ellipsis))
5485 if (
Left.is(TT_TemplateCloser) &&
5486 Right.isNoneOf(tok::equal, tok::l_brace, tok::comma, tok::l_square,
5487 Keywords.kw_implements, Keywords.kw_extends)) {
5493 if (
Right.is(TT_NonNullAssertion))
5495 if (
Left.is(TT_NonNullAssertion) &&
5496 Right.isOneOf(Keywords.kw_as, Keywords.kw_in)) {
5499 }
else if (Style.isJava()) {
5500 if (
Left.is(TT_CaseLabelArrow) ||
Right.is(TT_CaseLabelArrow))
5502 if (
Left.is(tok::r_square) &&
Right.is(tok::l_brace))
5505 if (
Left.is(tok::l_square) ||
Right.is(tok::r_square))
5506 return Style.SpacesInSquareBrackets;
5508 if (
Left.is(Keywords.kw_synchronized) &&
Right.is(tok::l_paren)) {
5509 return Style.SpaceBeforeParensOptions.AfterControlStatements ||
5510 spaceRequiredBeforeParens(Right);
5512 if ((
Left.isAccessSpecifierKeyword() ||
5513 Left.isOneOf(tok::kw_static, Keywords.kw_final, Keywords.kw_abstract,
5514 Keywords.kw_native)) &&
5515 Right.is(TT_TemplateOpener)) {
5518 }
else if (IsVerilog) {
5520 if (
Left.is(tok::identifier) &&
Left.TokenText[0] ==
'\\')
5524 if ((
Left.is(TT_VerilogTableItem) &&
5525 Right.isNoneOf(tok::r_paren, tok::semi)) ||
5526 (
Right.is(TT_VerilogTableItem) &&
Left.isNot(tok::l_paren))) {
5528 return !(
Next &&
Next->is(tok::r_paren));
5531 if (
Left.isNot(TT_BinaryOperator) &&
5532 Left.isOneOf(Keywords.kw_verilogHash, Keywords.kw_verilogHashHash)) {
5536 if (
Right.isNot(tok::semi) &&
5537 (
Left.endsSequence(tok::numeric_constant, Keywords.kw_verilogHash) ||
5538 Left.endsSequence(tok::numeric_constant,
5539 Keywords.kw_verilogHashHash) ||
5540 (
Left.is(tok::r_paren) &&
Left.MatchingParen &&
5541 Left.MatchingParen->endsSequence(tok::l_paren, tok::at)))) {
5546 if (
Left.is(Keywords.kw_apostrophe) ||
5547 (
Left.is(TT_VerilogNumberBase) &&
Right.is(tok::numeric_constant))) {
5551 if (
Left.is(tok::arrow) ||
Right.is(tok::arrow))
5556 if (
Left.is(tok::at) &&
Right.isOneOf(tok::l_paren, tok::star, tok::at))
5559 if (
Right.is(tok::l_square) &&
5560 Left.isOneOf(TT_VerilogDimensionedTypeName, Keywords.kw_function)) {
5564 if (
Right.isOneOf(tok::period, Keywords.kw_apostrophe) &&
5565 Keywords.isVerilogIdentifier(Left) &&
Left.getPreviousNonComment() &&
5566 Left.getPreviousNonComment()->is(Keywords.kw_tagged)) {
5572 if ((
Right.is(Keywords.kw_apostrophe) ||
5574 Left.isNoneOf(Keywords.kw_assign, Keywords.kw_unique) &&
5575 !Keywords.isVerilogWordOperator(Left) &&
5576 (
Left.isOneOf(tok::r_square, tok::r_paren, tok::r_brace,
5577 tok::numeric_constant) ||
5578 Keywords.isWordLike(Left))) {
5582 if ((
Right.is(tok::star) &&
Left.is(tok::coloncolon)) ||
5583 (
Left.is(tok::star) &&
Right.is(tok::semi))) {
5587 if (
Left.endsSequence(tok::star, tok::l_paren) &&
Right.is(tok::identifier))
5590 if (
Right.is(tok::l_paren) &&
Right.is(TT_VerilogStrength))
5593 if ((
Left.is(tok::l_brace) &&
5594 Right.isOneOf(tok::lessless, tok::greatergreater)) ||
5595 (
Left.endsSequence(tok::lessless, tok::l_brace) ||
5596 Left.endsSequence(tok::greatergreater, tok::l_brace))) {
5599 }
else if (Style.isTableGen()) {
5601 if (
Left.is(tok::l_square) &&
Right.is(tok::l_brace))
5603 if (
Left.is(tok::r_brace) &&
Right.is(tok::r_square))
5606 if (
Right.isOneOf(TT_TableGenDAGArgListColon,
5607 TT_TableGenDAGArgListColonToAlign) ||
5608 Left.isOneOf(TT_TableGenDAGArgListColon,
5609 TT_TableGenDAGArgListColonToAlign)) {
5612 if (
Right.is(TT_TableGenCondOperatorColon))
5614 if (
Left.isOneOf(TT_TableGenDAGArgOperatorID,
5615 TT_TableGenDAGArgOperatorToBreak) &&
5616 Right.isNot(TT_TableGenDAGArgCloser)) {
5620 if (
Right.isOneOf(tok::l_paren, tok::less) &&
5621 Left.isOneOf(TT_TableGenBangOperator, TT_TableGenCondOperator)) {
5626 if (
Left.is(TT_TableGenTrailingPasteOperator) &&
5627 Right.isOneOf(tok::l_brace, tok::colon)) {
5631 if (
Left.is(tok::hash) ||
Right.is(tok::hash))
5634 if (Keywords.isTableGenDefinition(Left))
5638 if (
Left.is(TT_ImplicitStringLiteral))
5639 return Right.hasWhitespaceBefore();
5641 if (
Left.is(TT_ObjCMethodSpecifier))
5642 return Style.ObjCSpaceAfterMethodDeclarationPrefix;
5643 if (
Left.is(tok::r_paren) &&
Left.isNot(TT_AttributeRParen) &&
5644 canBeObjCSelectorComponent(Right)) {
5652 (
Right.is(tok::equal) ||
Left.is(tok::equal))) {
5656 if (
Right.isOneOf(TT_TrailingReturnArrow, TT_LambdaArrow) ||
5657 Left.isOneOf(TT_TrailingReturnArrow, TT_LambdaArrow)) {
5660 if (
Left.is(tok::comma) &&
Right.isNot(TT_OverloadedOperatorLParen) &&
5663 (
Left.Children.empty() || !
Left.MacroParent)) {
5666 if (
Right.is(tok::comma))
5668 if (
Right.is(TT_ObjCBlockLParen))
5670 if (
Right.is(TT_CtorInitializerColon))
5671 return Style.SpaceBeforeCtorInitializerColon;
5672 if (
Right.is(TT_InheritanceColon) && !Style.SpaceBeforeInheritanceColon)
5674 if (
Right.is(TT_EnumUnderlyingTypeColon) &&
5675 !Style.SpaceBeforeEnumUnderlyingTypeColon) {
5678 if (
Right.is(TT_RangeBasedForLoopColon) &&
5679 !Style.SpaceBeforeRangeBasedForLoopColon) {
5682 if (
Left.is(TT_BitFieldColon)) {
5683 return Style.BitFieldColonSpacing == FormatStyle::BFCS_Both ||
5684 Style.BitFieldColonSpacing == FormatStyle::BFCS_After;
5686 if (
Right.is(tok::colon)) {
5687 if (
Right.is(TT_CaseLabelColon))
5688 return Style.SpaceBeforeCaseColon;
5689 if (
Right.is(TT_GotoLabelColon))
5692 if (!
Right.getNextNonComment())
5694 if (
Right.isOneOf(TT_ObjCSelector, TT_ObjCMethodExpr))
5696 if (
Left.is(tok::question))
5698 if (
Right.is(TT_InlineASMColon) &&
Left.is(tok::coloncolon))
5700 if (
Right.is(TT_DictLiteral))
5701 return Style.SpacesInContainerLiterals;
5702 if (
Right.is(TT_AttributeColon))
5704 if (
Right.is(TT_CSharpNamedArgumentColon))
5706 if (
Right.is(TT_GenericSelectionColon))
5708 if (
Right.is(TT_BitFieldColon)) {
5709 return Style.BitFieldColonSpacing == FormatStyle::BFCS_Both ||
5710 Style.BitFieldColonSpacing == FormatStyle::BFCS_Before;
5715 if ((
Left.isOneOf(tok::minus, tok::minusminus) &&
5716 Right.isOneOf(tok::minus, tok::minusminus)) ||
5717 (
Left.isOneOf(tok::plus, tok::plusplus) &&
5718 Right.isOneOf(tok::plus, tok::plusplus))) {
5721 if (
Left.is(TT_UnaryOperator)) {
5724 if (
Left.is(tok::amp) &&
Right.is(tok::r_square))
5725 return Style.SpacesInSquareBrackets;
5726 if (
Left.isNot(tok::exclaim))
5728 if (
Left.TokenText ==
"!")
5729 return Style.SpaceAfterLogicalNot;
5730 assert(
Left.TokenText ==
"not");
5731 return Right.isOneOf(tok::coloncolon, TT_UnaryOperator) ||
5732 (
Right.is(tok::l_paren) && Style.SpaceBeforeParensOptions.AfterNot);
5737 if (
Left.is(TT_CastRParen)) {
5738 return Style.SpaceAfterCStyleCast ||
5739 Right.isOneOf(TT_BinaryOperator, TT_SelectorName);
5742 auto ShouldAddSpacesInAngles = [
this, &
Right]() {
5743 if (this->Style.SpacesInAngles == FormatStyle::SIAS_Always)
5745 if (this->Style.SpacesInAngles == FormatStyle::SIAS_Leave)
5746 return Right.hasWhitespaceBefore();
5750 if (
Left.is(tok::greater) &&
Right.is(tok::greater)) {
5751 if (Style.isTextProto() ||
5752 (Style.Language == FormatStyle::LK_Proto &&
Left.is(TT_DictLiteral))) {
5753 return Style.Cpp11BracedListStyle == FormatStyle::BLS_Block;
5755 return Right.is(TT_TemplateCloser) &&
Left.is(TT_TemplateCloser) &&
5756 ((Style.Standard < FormatStyle::LS_Cpp11) ||
5757 ShouldAddSpacesInAngles());
5759 if (
Right.isOneOf(tok::arrow, tok::arrowstar, tok::periodstar) ||
5760 Left.isOneOf(tok::arrow, tok::period, tok::arrowstar, tok::periodstar) ||
5761 (
Right.is(tok::period) &&
Right.isNot(TT_DesignatedInitializerPeriod))) {
5764 if (!Style.SpaceBeforeAssignmentOperators &&
Left.isNot(TT_TemplateCloser) &&
5768 if (Style.isJava() &&
Right.is(tok::coloncolon) &&
5769 Left.isOneOf(tok::identifier, tok::kw_this)) {
5772 if (
Right.is(tok::coloncolon) &&
Left.is(tok::identifier)) {
5774 return Left.isPossibleMacro(
true) &&
5775 Right.hasWhitespaceBefore();
5777 if (
Right.is(tok::coloncolon) &&
5778 Left.isNoneOf(tok::l_brace, tok::comment, tok::l_paren)) {
5780 return (
Left.is(TT_TemplateOpener) &&
5781 ((Style.Standard < FormatStyle::LS_Cpp11) ||
5782 ShouldAddSpacesInAngles())) ||
5783 Left.isNoneOf(tok::l_paren, tok::r_paren, tok::l_square,
5784 tok::kw___super, TT_TemplateOpener,
5785 TT_TemplateCloser) ||
5786 (
Left.is(tok::l_paren) && Style.SpacesInParensOptions.Other);
5788 if ((
Left.is(TT_TemplateOpener)) != (
Right.is(TT_TemplateCloser)))
5789 return ShouldAddSpacesInAngles();
5790 if (
Left.is(tok::r_paren) &&
Left.isNot(TT_TypeDeclarationParen) &&
5791 Right.is(TT_PointerOrReference) &&
Right.isOneOf(tok::amp, tok::ampamp)) {
5795 if (
Right.is(TT_StructuredBindingLSquare)) {
5796 return Left.isNoneOf(tok::amp, tok::ampamp) ||
5797 getTokenReferenceAlignment(Left) != FormatStyle::PAS_Right;
5800 if (
Right.Next &&
Right.Next->is(TT_StructuredBindingLSquare) &&
5801 Right.isOneOf(tok::amp, tok::ampamp)) {
5802 return getTokenReferenceAlignment(Right) != FormatStyle::PAS_Left;
5804 if ((
Right.is(TT_BinaryOperator) &&
Left.isNot(tok::l_paren)) ||
5805 (
Left.isOneOf(TT_BinaryOperator, TT_EnumEqual, TT_ConditionalExpr) &&
5806 Right.isNot(tok::r_paren))) {
5809 if (
Right.is(TT_TemplateOpener) &&
Left.is(tok::r_paren) &&
5810 Left.MatchingParen &&
5811 Left.MatchingParen->is(TT_OverloadedOperatorLParen)) {
5814 if (
Right.is(tok::less) &&
Left.isNot(tok::l_paren) &&
5818 if (
Right.is(TT_TrailingUnaryOperator))
5820 if (
Left.is(TT_RegexLiteral))
5822 return spaceRequiredBetween(
Line, Left, Right);
5828 Tok.isNoneOf(TT_ObjCBlockLBrace, TT_LambdaLBrace, TT_DictLiteral);
5833 return Tok.MatchingParen &&
Tok.MatchingParen->Next &&
5834 Tok.MatchingParen->Next->isOneOf(tok::comma, tok::r_paren,
5840 FormatStyle::ShortLambdaStyle ShortLambdaOption) {
5841 return Tok.Children.empty() && ShortLambdaOption != FormatStyle::SLS_None;
5846 Tok.isNoneOf(TT_ObjCBlockLBrace, TT_DictLiteral);
5849bool TokenAnnotator::mustBreakBefore(AnnotatedLine &
Line,
5851 if (Right.NewlinesBefore > 1 && Style.MaxEmptyLinesToKeep > 0 &&
5852 (!Style.RemoveEmptyLinesInUnwrappedLines || &Right ==
Line.First)) {
5858 if (Style.BreakFunctionDeclarationParameters &&
Line.MightBeFunctionDecl &&
5859 !
Line.mightBeFunctionDefinition() && Left.MightBeFunctionDeclParen &&
5860 Left.ParameterCount > 0) {
5864 if (Style.BreakFunctionDefinitionParameters &&
Line.MightBeFunctionDecl &&
5865 Line.mightBeFunctionDefinition() &&
Left.MightBeFunctionDeclParen &&
5866 Left.ParameterCount > 0) {
5872 if (Style.PackParameters.BinPack == FormatStyle::BPPS_AlwaysOnePerLine &&
5873 Line.MightBeFunctionDecl && !
Left.opensScope() &&
5878 const auto *BeforeLeft =
Left.Previous;
5879 const auto *AfterRight =
Right.Next;
5881 if (Style.isCSharp()) {
5882 if (
Left.is(TT_FatArrow) &&
Right.is(tok::l_brace) &&
5883 Style.BraceWrapping.AfterFunction) {
5886 if (
Right.is(TT_CSharpNamedArgumentColon) ||
5887 Left.is(TT_CSharpNamedArgumentColon)) {
5890 if (
Right.is(TT_CSharpGenericTypeConstraint))
5892 if (AfterRight && AfterRight->is(TT_FatArrow) &&
5893 (
Right.is(tok::numeric_constant) ||
5894 (
Right.is(tok::identifier) &&
Right.TokenText ==
"_"))) {
5899 if (
Left.is(TT_AttributeRSquare) &&
5900 (
Right.isAccessSpecifier(
false) ||
5901 Right.is(Keywords.kw_internal))) {
5905 if (
Left.is(TT_AttributeRSquare) &&
Right.is(TT_AttributeLSquare))
5907 }
else if (Style.isJavaScript()) {
5909 if (
Right.is(tok::string_literal) &&
Left.is(tok::plus) && BeforeLeft &&
5910 BeforeLeft->is(tok::string_literal)) {
5913 if (
Left.is(TT_DictLiteral) &&
Left.is(tok::l_brace) &&
Line.Level == 0 &&
5914 BeforeLeft && BeforeLeft->is(tok::equal) &&
5915 Line.First->isOneOf(tok::identifier, Keywords.kw_import, tok::kw_export,
5919 Line.First->isNoneOf(Keywords.kw_var, Keywords.kw_let)) {
5924 if (
Left.is(tok::l_brace) &&
Line.Level == 0 &&
5925 (
Line.startsWith(tok::kw_enum) ||
5926 Line.startsWith(tok::kw_const, tok::kw_enum) ||
5927 Line.startsWith(tok::kw_export, tok::kw_enum) ||
5928 Line.startsWith(tok::kw_export, tok::kw_const, tok::kw_enum))) {
5933 if (
Right.is(tok::r_brace) &&
Left.is(tok::l_brace) && BeforeLeft &&
5934 BeforeLeft->is(TT_FatArrow)) {
5936 switch (Style.AllowShortLambdasOnASingleLine) {
5937 case FormatStyle::SLS_All:
5939 case FormatStyle::SLS_None:
5941 case FormatStyle::SLS_Empty:
5942 return !
Left.Children.empty();
5943 case FormatStyle::SLS_Inline:
5946 return (
Left.NestingLevel == 0 &&
Line.Level == 0) &&
5947 !
Left.Children.empty();
5949 llvm_unreachable(
"Unknown FormatStyle::ShortLambdaStyle enum");
5952 if (
Right.is(tok::r_brace) &&
Left.is(tok::l_brace) &&
5953 !
Left.Children.empty()) {
5955 if (
Left.NestingLevel == 0 &&
Line.Level == 0)
5956 return !Style.AllowShortFunctionsOnASingleLine.Other;
5958 return !Style.AllowShortFunctionsOnASingleLine.Inline;
5960 }
else if (Style.isJava()) {
5961 if (
Right.is(tok::plus) &&
Left.is(tok::string_literal) && AfterRight &&
5962 AfterRight->is(tok::string_literal)) {
5965 }
else if (Style.isVerilog()) {
5967 if (
Left.is(TT_VerilogAssignComma))
5970 if (
Left.is(TT_VerilogTypeComma))
5974 if (Style.VerilogBreakBetweenInstancePorts &&
5975 (
Left.is(TT_VerilogInstancePortComma) ||
5976 (
Left.is(tok::r_paren) && Keywords.isVerilogIdentifier(Right) &&
5977 Left.MatchingParen &&
5978 Left.MatchingParen->is(TT_VerilogInstancePortLParen)))) {
5983 if (!Keywords.isVerilogBegin(Right) && Keywords.isVerilogEndOfLabel(Left))
5985 }
else if (Style.BreakAdjacentStringLiterals &&
5986 (IsCpp || Style.isProto() || Style.isTableGen())) {
5987 if (
Left.isStringLiteral() &&
Right.isStringLiteral())
5992 if (Style.isJson()) {
5996 if (
Left.is(TT_DictLiteral) &&
Left.is(tok::l_brace))
5999 if ((
Left.is(TT_ArrayInitializerLSquare) &&
Left.is(tok::l_square) &&
6000 Right.isNot(tok::r_square)) ||
6001 Left.is(tok::comma)) {
6002 if (
Right.is(tok::l_brace))
6007 if (
Tok->isOneOf(tok::l_brace, tok::l_square))
6009 if (
Tok->isOneOf(tok::r_brace, tok::r_square))
6012 return Style.BreakArrays;
6014 }
else if (Style.isTableGen()) {
6018 if (
Left.is(TT_TableGenCondOperatorComma))
6020 if (
Left.is(TT_TableGenDAGArgOperatorToBreak) &&
6021 Right.isNot(TT_TableGenDAGArgCloser)) {
6024 if (
Left.is(TT_TableGenDAGArgListCommaToBreak))
6026 if (
Right.is(TT_TableGenDAGArgCloser) &&
Right.MatchingParen &&
6027 Right.MatchingParen->is(TT_TableGenDAGArgOpenerToBreak) &&
6028 &Left !=
Right.MatchingParen->Next) {
6030 return Style.TableGenBreakInsideDAGArg == FormatStyle::DAS_BreakAll;
6034 if (
Line.startsWith(tok::kw_asm) &&
Right.is(TT_InlineASMColon) &&
6035 Style.BreakBeforeInlineASMColon == FormatStyle::BBIAS_Always) {
6045 if ((
Left.isOneOf(tok::l_brace, TT_ArrayInitializerLSquare) ||
6046 (Style.isJavaScript() &&
Left.is(tok::l_paren))) &&
6048 BeforeClosingBrace =
Left.MatchingParen->Previous;
6049 }
else if (
Right.MatchingParen &&
6050 (
Right.MatchingParen->isOneOf(tok::l_brace,
6051 TT_ArrayInitializerLSquare) ||
6052 (Style.isJavaScript() &&
6053 Right.MatchingParen->is(tok::l_paren)))) {
6054 BeforeClosingBrace = &
Left;
6056 if (BeforeClosingBrace && (BeforeClosingBrace->is(tok::comma) ||
6057 BeforeClosingBrace->isTrailingComment())) {
6062 if (
Right.is(tok::comment)) {
6064 Right.NewlinesBefore > 0 &&
Right.HasUnescapedNewline;
6066 if (
Left.isTrailingComment())
6068 if (
Left.IsUnterminatedLiteral)
6071 if (BeforeLeft && BeforeLeft->is(tok::lessless) &&
6072 Left.is(tok::string_literal) &&
Right.is(tok::lessless) && AfterRight &&
6073 AfterRight->is(tok::string_literal)) {
6074 return Right.NewlinesBefore > 0;
6077 if (
Right.is(TT_RequiresClause)) {
6078 switch (Style.RequiresClausePosition) {
6079 case FormatStyle::RCPS_OwnLine:
6080 case FormatStyle::RCPS_OwnLineWithBrace:
6081 case FormatStyle::RCPS_WithFollowing:
6088 if (
Left.ClosesTemplateDeclaration &&
Left.MatchingParen &&
6089 Left.MatchingParen->NestingLevel == 0) {
6093 if (
Right.is(tok::kw_concept))
6094 return Style.BreakBeforeConceptDeclarations == FormatStyle::BBCDS_Always;
6095 return Style.BreakTemplateDeclarations == FormatStyle::BTDS_Yes ||
6096 (Style.BreakTemplateDeclarations == FormatStyle::BTDS_Leave &&
6097 Right.NewlinesBefore > 0);
6099 if (
Left.ClosesRequiresClause) {
6100 switch (Style.RequiresClausePosition) {
6101 case FormatStyle::RCPS_OwnLine:
6102 case FormatStyle::RCPS_WithPreceding:
6103 return Right.isNot(tok::semi);
6104 case FormatStyle::RCPS_OwnLineWithBrace:
6105 return Right.isNoneOf(tok::semi, tok::l_brace);
6110 if (Style.PackConstructorInitializers == FormatStyle::PCIS_Never) {
6111 if (Style.BreakConstructorInitializers == FormatStyle::BCIS_BeforeColon &&
6112 (
Left.is(TT_CtorInitializerComma) ||
6113 Right.is(TT_CtorInitializerColon))) {
6117 if (Style.BreakConstructorInitializers == FormatStyle::BCIS_AfterColon &&
6118 Left.isOneOf(TT_CtorInitializerColon, TT_CtorInitializerComma)) {
6122 if (Style.BreakConstructorInitializers == FormatStyle::BCIS_AfterComma &&
6123 Left.is(TT_CtorInitializerComma)) {
6127 if (Style.PackConstructorInitializers < FormatStyle::PCIS_CurrentLine &&
6128 Style.BreakConstructorInitializers == FormatStyle::BCIS_BeforeComma &&
6129 Right.isOneOf(TT_CtorInitializerComma, TT_CtorInitializerColon)) {
6132 if (Style.PackConstructorInitializers == FormatStyle::PCIS_NextLineOnly) {
6133 if ((Style.BreakConstructorInitializers == FormatStyle::BCIS_BeforeColon ||
6134 Style.BreakConstructorInitializers == FormatStyle::BCIS_BeforeComma) &&
6135 Right.is(TT_CtorInitializerColon)) {
6139 if (Style.BreakConstructorInitializers == FormatStyle::BCIS_AfterColon &&
6140 Left.is(TT_CtorInitializerColon)) {
6145 if (Style.BreakInheritanceList == FormatStyle::BILS_BeforeComma &&
6146 Right.is(TT_InheritanceComma)) {
6149 if (Style.BreakInheritanceList == FormatStyle::BILS_AfterComma &&
6150 Left.is(TT_InheritanceComma)) {
6153 if (
Right.is(tok::string_literal) &&
Right.TokenText.starts_with(
"R\"")) {
6157 return Right.IsMultiline &&
Right.NewlinesBefore > 0;
6159 if ((
Left.is(tok::l_brace) ||
6160 (
Left.is(tok::less) && BeforeLeft && BeforeLeft->is(tok::equal))) &&
6161 Right.NestingLevel == 1 && Style.Language == FormatStyle::LK_Proto) {
6166 if (
Right.is(TT_InlineASMBrace))
6167 return Right.HasUnescapedNewline;
6170 auto *FirstNonComment =
Line.getFirstNonComment();
6172 FirstNonComment && (FirstNonComment->is(Keywords.kw_internal) ||
6173 FirstNonComment->isAccessSpecifierKeyword());
6175 if (Style.BraceWrapping.AfterEnum) {
6176 if (
Line.startsWith(tok::kw_enum) ||
6177 Line.startsWith(tok::kw_typedef, tok::kw_enum) ||
6178 Line.startsWith(tok::kw_export, tok::kw_enum)) {
6183 FirstNonComment->Next->is(tok::kw_enum)) {
6189 if (Style.BraceWrapping.AfterClass &&
6191 FirstNonComment->Next->is(Keywords.kw_interface)) ||
6192 Line.startsWith(Keywords.kw_interface))) {
6197 if (
Right.isNot(TT_FunctionLBrace)) {
6198 return Style.AllowShortRecordOnASingleLine == FormatStyle::SRS_Never &&
6199 ((
Line.startsWith(tok::kw_class) &&
6200 Style.BraceWrapping.AfterClass) ||
6201 (
Line.startsWith(tok::kw_struct) &&
6202 Style.BraceWrapping.AfterStruct) ||
6203 (
Line.startsWith(tok::kw_union) &&
6204 Style.BraceWrapping.AfterUnion));
6208 if (
Left.is(TT_ObjCBlockLBrace) &&
6209 Style.AllowShortBlocksOnASingleLine == FormatStyle::SBS_Never) {
6214 if (
Left.isOneOf(TT_AttributeRParen, TT_AttributeMacro) &&
6215 Right.is(TT_ObjCDecl)) {
6219 if (
Left.is(TT_LambdaLBrace)) {
6221 Style.AllowShortLambdasOnASingleLine == FormatStyle::SLS_Inline) {
6225 if (Style.AllowShortLambdasOnASingleLine == FormatStyle::SLS_None ||
6226 Style.AllowShortLambdasOnASingleLine == FormatStyle::SLS_Inline ||
6227 (!
Left.Children.empty() &&
6228 Style.AllowShortLambdasOnASingleLine == FormatStyle::SLS_Empty)) {
6233 if (Style.BraceWrapping.BeforeLambdaBody &&
Right.is(TT_LambdaLBrace) &&
6234 (
Left.isPointerOrReference() ||
Left.is(TT_TemplateCloser))) {
6239 if ((Style.isJava() || Style.isJavaScript()) &&
6240 Left.is(TT_LeadingJavaAnnotation) &&
6241 Right.isNoneOf(TT_LeadingJavaAnnotation, tok::l_paren) &&
6242 (
Line.Last->is(tok::l_brace) || Style.BreakAfterJavaFieldAnnotations)) {
6246 if (
Right.is(TT_ProtoExtensionLSquare))
6276 if (Style.isProto() &&
Right.is(TT_SelectorName) &&
6277 Right.isNot(tok::r_square) && AfterRight) {
6280 if (
Left.is(tok::at))
6286 const auto *LBrace = AfterRight;
6287 if (LBrace && LBrace->is(tok::colon)) {
6288 LBrace = LBrace->Next;
6289 if (LBrace && LBrace->is(tok::at)) {
6290 LBrace = LBrace->Next;
6292 LBrace = LBrace->Next;
6304 ((LBrace->is(tok::l_brace) &&
6305 (LBrace->is(TT_DictLiteral) ||
6306 (LBrace->Next && LBrace->Next->is(tok::r_brace)))) ||
6307 LBrace->isOneOf(TT_ArrayInitializerLSquare, tok::less))) {
6314 if (
Left.ParameterCount == 0)
6329 if (
Left.isOneOf(tok::r_brace, tok::greater, tok::r_square))
6333 if (Style.BreakAfterAttributes == FormatStyle::ABS_LeaveAll &&
6334 Left.is(TT_AttributeRSquare) &&
Right.NewlinesBefore > 0) {
6335 Line.ReturnTypeWrapped =
true;
6346 if (Style.isCSharp()) {
6347 if (
Left.isOneOf(TT_CSharpNamedArgumentColon, TT_AttributeColon) ||
6348 Right.isOneOf(TT_CSharpNamedArgumentColon, TT_AttributeColon)) {
6352 if (
Line.First->is(TT_CSharpGenericTypeConstraint))
6353 return Left.is(TT_CSharpGenericTypeConstraintComma);
6355 if (
Right.is(TT_CSharpNullable))
6357 }
else if (Style.isJava()) {
6358 if (
Left.isOneOf(Keywords.kw_throws, Keywords.kw_extends,
6359 Keywords.kw_implements)) {
6362 if (
Right.isOneOf(Keywords.kw_throws, Keywords.kw_extends,
6363 Keywords.kw_implements)) {
6366 }
else if (Style.isJavaScript()) {
6369 (NonComment->isAccessSpecifierKeyword() ||
6370 NonComment->isOneOf(
6371 tok::kw_return, Keywords.kw_yield, tok::kw_continue, tok::kw_break,
6372 tok::kw_throw, Keywords.kw_interface, Keywords.kw_type,
6373 tok::kw_static, Keywords.kw_readonly, Keywords.kw_override,
6374 Keywords.kw_abstract, Keywords.kw_get, Keywords.kw_set,
6375 Keywords.kw_async, Keywords.kw_await))) {
6378 if (
Right.NestingLevel == 0 &&
6379 (
Left.Tok.getIdentifierInfo() ||
6380 Left.isOneOf(tok::r_square, tok::r_paren)) &&
6381 Right.isOneOf(tok::l_square, tok::l_paren)) {
6384 if (NonComment && NonComment->is(tok::identifier) &&
6385 NonComment->TokenText ==
"asserts") {
6388 if (
Left.is(TT_FatArrow) &&
Right.is(tok::l_brace))
6390 if (
Left.is(TT_JsTypeColon))
6393 if (
Left.is(tok::exclaim) &&
Right.is(tok::colon))
6398 if (
Right.is(Keywords.kw_is)) {
6407 if (!
Next ||
Next->isNot(tok::colon))
6410 if (
Left.is(Keywords.kw_in))
6411 return Style.BreakBeforeBinaryOperators == FormatStyle::BOS_None;
6412 if (
Right.is(Keywords.kw_in))
6413 return Style.BreakBeforeBinaryOperators != FormatStyle::BOS_None;
6414 if (
Right.is(Keywords.kw_as))
6416 if (
Right.isOneOf(Keywords.kw_extends, Keywords.kw_infer)) {
6422 if (
Left.is(Keywords.kw_as))
6424 if (
Left.is(TT_NonNullAssertion))
6426 if (
Left.is(Keywords.kw_declare) &&
6427 Right.isOneOf(Keywords.kw_module, tok::kw_namespace,
6428 Keywords.kw_function, tok::kw_class, tok::kw_enum,
6429 Keywords.kw_interface, Keywords.kw_type, Keywords.kw_var,
6430 Keywords.kw_let, tok::kw_const)) {
6435 if (
Left.isOneOf(Keywords.kw_module, tok::kw_namespace) &&
6436 Right.isOneOf(tok::identifier, tok::string_literal)) {
6439 if (
Right.is(TT_TemplateString) &&
Right.closesScope())
6443 if (
Left.is(tok::identifier) &&
Right.is(TT_TemplateString))
6445 if (
Left.is(TT_TemplateString) &&
Left.opensScope())
6447 }
else if (Style.isTableGen()) {
6449 if (Keywords.isTableGenDefinition(Left))
6452 if (
Right.is(tok::l_paren)) {
6453 return Left.isNoneOf(TT_TableGenBangOperator, TT_TableGenCondOperator,
6457 if (
Left.is(TT_TableGenValueSuffix))
6460 if (
Left.is(tok::hash) ||
Right.is(tok::hash))
6462 if (
Left.isOneOf(TT_TableGenBangOperator, TT_TableGenCondOperator))
6469 if (
Right.is(tok::r_brace)) {
6471 (
Right.isBlockIndentedInitRBrace(Style)));
6476 if (
Right.is(tok::r_paren)) {
6477 if (!
Right.MatchingParen)
6480 if (
Next &&
Next->is(tok::r_paren))
6482 if (
Next &&
Next->is(tok::l_paren))
6488 return Style.BreakBeforeCloseBracketIf;
6490 return Style.BreakBeforeCloseBracketLoop;
6492 return Style.BreakBeforeCloseBracketSwitch;
6493 return Style.BreakBeforeCloseBracketFunction;
6496 if (
Left.isOneOf(tok::r_paren, TT_TrailingAnnotation) &&
6497 Right.is(TT_TrailingAnnotation) &&
6498 Style.BreakBeforeCloseBracketFunction) {
6502 if (
Right.is(TT_TemplateCloser))
6503 return Style.BreakBeforeTemplateCloser;
6505 if (
Left.isOneOf(tok::at, tok::objc_interface))
6507 if (
Left.isOneOf(TT_JavaAnnotation, TT_LeadingJavaAnnotation))
6508 return Right.isNot(tok::l_paren);
6509 if (
Right.is(TT_PointerOrReference)) {
6510 return Line.IsMultiVariableDeclStmt ||
6511 (getTokenPointerOrReferenceAlignment(Right) ==
6512 FormatStyle::PAS_Right &&
6514 Right.Next->isOneOf(TT_FunctionDeclarationName, tok::kw_const)));
6516 if (
Left.is(tok::hashhash) ||
Right.is(tok::hashhash))
6518 if (
Right.isOneOf(TT_StartOfName, TT_FunctionDeclarationName,
6519 TT_ClassHeadName, TT_QtProperty, tok::kw_operator)) {
6522 if (
Left.is(TT_PointerOrReference))
6524 if (
Right.isTrailingComment()) {
6531 (
Left.is(TT_CtorInitializerColon) &&
Right.NewlinesBefore > 0 &&
6532 Style.BreakConstructorInitializers == FormatStyle::BCIS_AfterColon);
6534 if (
Left.is(tok::question) &&
Right.is(tok::colon))
6536 if (
Right.isOneOf(TT_ConditionalExpr, tok::question))
6537 return Style.BreakBeforeTernaryOperators;
6538 if (
Left.isOneOf(TT_ConditionalExpr, tok::question))
6539 return !Style.BreakBeforeTernaryOperators;
6540 if (
Left.is(TT_InheritanceColon))
6541 return Style.BreakInheritanceList == FormatStyle::BILS_AfterColon;
6542 if (
Right.is(TT_InheritanceColon))
6543 return Style.BreakInheritanceList != FormatStyle::BILS_AfterColon;
6545 if (
Right.is(TT_ObjCMethodExpr) &&
Right.isNot(tok::r_square) &&
6546 Left.isNot(TT_SelectorName)) {
6550 if (
Right.is(tok::colon) &&
6551 Right.isNoneOf(TT_CtorInitializerColon, TT_InlineASMColon,
6552 TT_BitFieldColon)) {
6555 if (
Left.is(tok::colon) &&
Left.isOneOf(TT_ObjCSelector, TT_ObjCMethodExpr))
6557 if (
Left.is(tok::colon) &&
Left.is(TT_DictLiteral)) {
6558 if (Style.isProto()) {
6559 if (!Style.AlwaysBreakBeforeMultilineStrings &&
Right.isStringLiteral())
6585 if ((
Right.isOneOf(tok::l_brace, tok::less) &&
6586 Right.is(TT_DictLiteral)) ||
6587 Right.is(TT_ArrayInitializerLSquare)) {
6593 if (
Right.is(tok::r_square) &&
Right.MatchingParen &&
6594 Right.MatchingParen->is(TT_ProtoExtensionLSquare)) {
6597 if (
Right.is(TT_SelectorName) || (
Right.is(tok::identifier) &&
Right.Next &&
6598 Right.Next->is(TT_ObjCMethodExpr))) {
6599 return Left.isNot(tok::period);
6603 if (
Right.is(tok::kw_concept))
6604 return Style.BreakBeforeConceptDeclarations != FormatStyle::BBCDS_Never;
6605 if (
Right.is(TT_RequiresClause))
6607 if (
Left.ClosesTemplateDeclaration) {
6608 return Style.BreakTemplateDeclarations != FormatStyle::BTDS_Leave ||
6609 Right.NewlinesBefore > 0;
6611 if (
Left.is(TT_FunctionAnnotationRParen))
6613 if (
Left.ClosesRequiresClause)
6615 if (
Right.isOneOf(TT_RangeBasedForLoopColon, TT_OverloadedOperatorLParen,
6616 TT_OverloadedOperator)) {
6619 if (
Left.is(TT_RangeBasedForLoopColon))
6621 if (
Right.is(TT_RangeBasedForLoopColon))
6623 if (
Left.is(TT_TemplateCloser) &&
Right.is(TT_TemplateOpener))
6625 if ((
Left.is(tok::greater) &&
Right.is(tok::greater)) ||
6626 (
Left.is(tok::less) &&
Right.is(tok::less))) {
6629 if (
Right.is(TT_BinaryOperator) &&
6630 Style.BreakBeforeBinaryOperators != FormatStyle::BOS_None &&
6631 (Style.BreakBeforeBinaryOperators == FormatStyle::BOS_All ||
6635 if (
Left.isOneOf(TT_TemplateCloser, TT_UnaryOperator, tok::kw_operator))
6637 if (
Left.is(tok::equal) &&
Right.isNoneOf(tok::kw_default, tok::kw_delete) &&
6641 if (
Left.is(tok::equal) &&
Right.is(tok::l_brace) &&
6642 Style.Cpp11BracedListStyle == FormatStyle::BLS_Block) {
6645 if (
Left.is(TT_AttributeLParen) ||
6646 (
Left.is(tok::l_paren) &&
Left.is(TT_TypeDeclarationParen))) {
6649 if (
Left.is(tok::l_paren) &&
Left.Previous &&
6650 (
Left.Previous->isOneOf(TT_BinaryOperator, TT_CastRParen))) {
6653 if (
Right.is(TT_ImplicitStringLiteral))
6656 if (
Right.is(tok::r_square) &&
Right.MatchingParen &&
6657 Right.MatchingParen->is(TT_LambdaLSquare)) {
6663 if (
Left.is(TT_TrailingAnnotation)) {
6664 return Right.isNoneOf(tok::l_brace, tok::semi, tok::equal, tok::l_paren,
6665 tok::less, tok::coloncolon);
6668 if (
Right.isAttribute())
6671 if (
Right.is(TT_AttributeLSquare)) {
6672 assert(
Left.isNot(tok::l_square));
6676 if (
Left.is(tok::identifier) &&
Right.is(tok::string_literal))
6679 if (
Right.is(tok::identifier) &&
Right.Next &&
Right.Next->is(TT_DictLiteral))
6682 if (
Left.is(TT_CtorInitializerColon)) {
6683 return (Style.BreakConstructorInitializers ==
6684 FormatStyle::BCIS_AfterColon ||
6685 Style.BreakConstructorInitializers ==
6686 FormatStyle::BCIS_AfterComma) &&
6687 (!
Right.isTrailingComment() ||
Right.NewlinesBefore > 0);
6689 if (
Right.is(TT_CtorInitializerColon)) {
6690 return Style.BreakConstructorInitializers != FormatStyle::BCIS_AfterColon &&
6691 Style.BreakConstructorInitializers != FormatStyle::BCIS_AfterComma;
6693 if (
Left.is(TT_CtorInitializerComma) &&
6694 Style.BreakConstructorInitializers == FormatStyle::BCIS_BeforeComma) {
6697 if (
Right.is(TT_CtorInitializerComma) &&
6698 Style.BreakConstructorInitializers == FormatStyle::BCIS_BeforeComma) {
6701 if (
Left.is(TT_InheritanceComma) &&
6702 Style.BreakInheritanceList == FormatStyle::BILS_BeforeComma) {
6705 if (
Right.is(TT_InheritanceComma) &&
6706 Style.BreakInheritanceList == FormatStyle::BILS_BeforeComma) {
6709 if (
Left.is(TT_ArrayInitializerLSquare))
6711 if (
Right.is(tok::kw_typename) &&
Left.isNot(tok::kw_const))
6713 if ((
Left.isBinaryOperator() ||
Left.is(TT_BinaryOperator)) &&
6714 Left.isNoneOf(tok::arrowstar, tok::lessless) &&
6715 Style.BreakBeforeBinaryOperators != FormatStyle::BOS_All &&
6716 (Style.BreakBeforeBinaryOperators == FormatStyle::BOS_None ||
6720 if (
Left.is(TT_AttributeLSquare) &&
Right.is(tok::l_square)) {
6721 assert(
Right.isNot(TT_AttributeLSquare));
6724 if (
Left.is(tok::r_square) &&
Right.is(TT_AttributeRSquare)) {
6725 assert(
Left.isNot(TT_AttributeRSquare));
6729 auto ShortLambdaOption = Style.AllowShortLambdasOnASingleLine;
6730 if (Style.BraceWrapping.BeforeLambdaBody &&
Right.is(TT_LambdaLBrace)) {
6737 if (
Right.is(tok::kw_noexcept) &&
Right.is(TT_TrailingAnnotation)) {
6738 switch (Style.AllowBreakBeforeNoexceptSpecifier) {
6739 case FormatStyle::BBNSS_Never:
6741 case FormatStyle::BBNSS_Always:
6743 case FormatStyle::BBNSS_OnlyWithParen:
6744 return Right.Next &&
Right.Next->is(tok::l_paren);
6748 return Left.isOneOf(tok::comma, tok::coloncolon, tok::semi, tok::l_brace,
6749 tok::kw_class, tok::kw_struct, tok::comment) ||
6750 Right.isMemberAccess() ||
6751 Right.isOneOf(TT_TrailingReturnArrow, TT_LambdaArrow, tok::lessless,
6752 tok::colon, tok::l_square, tok::at) ||
6753 (
Left.is(tok::r_paren) &&
6754 Right.isOneOf(tok::identifier, tok::kw_const)) ||
6755 (
Left.is(tok::l_paren) &&
Right.isNot(tok::r_paren)) ||
6756 (
Left.is(TT_TemplateOpener) &&
Right.isNot(TT_TemplateCloser));
6760 llvm::errs() <<
"AnnotatedTokens(L=" <<
Line.Level <<
", P=" <<
Line.PPLevel
6761 <<
", T=" <<
Line.Type <<
", C=" <<
Line.IsContinuation
6765 llvm::errs() <<
" I=" <<
Tok->IndentLevel <<
" M=" <<
Tok->MustBreakBefore
6766 <<
" C=" <<
Tok->CanBreakBefore
6768 <<
" S=" <<
Tok->SpacesRequiredBefore
6769 <<
" F=" <<
Tok->Finalized <<
" B=" <<
Tok->BlockParameterCount
6770 <<
" BK=" <<
Tok->getBlockKind() <<
" P=" <<
Tok->SplitPenalty
6771 <<
" Name=" <<
Tok->Tok.getName() <<
" N=" <<
Tok->NestingLevel
6772 <<
" L=" <<
Tok->TotalLength
6773 <<
" PPK=" <<
Tok->getPackingKind() <<
" FakeLParens=";
6775 llvm::errs() << LParen <<
"/";
6776 llvm::errs() <<
" FakeRParens=" <<
Tok->FakeRParens;
6777 llvm::errs() <<
" II=" <<
Tok->Tok.getIdentifierInfo();
6778 llvm::errs() <<
" Text='" <<
Tok->TokenText <<
"'\n";
6783 llvm::errs() <<
"----\n";
6786FormatStyle::PointerAlignmentStyle
6788 assert(
Reference.isOneOf(tok::amp, tok::ampamp));
6789 switch (Style.ReferenceAlignment) {
6790 case FormatStyle::RAS_Pointer:
6791 return Style.PointerAlignment;
6792 case FormatStyle::RAS_Left:
6793 return FormatStyle::PAS_Left;
6794 case FormatStyle::RAS_Right:
6795 return FormatStyle::PAS_Right;
6796 case FormatStyle::RAS_Middle:
6797 return FormatStyle::PAS_Middle;
6800 return Style.PointerAlignment;
6803FormatStyle::PointerAlignmentStyle
6804TokenAnnotator::getTokenPointerOrReferenceAlignment(
6806 if (PointerOrReference.isOneOf(tok::amp, tok::ampamp))
6807 return getTokenReferenceAlignment(PointerOrReference);
6808 assert(PointerOrReference.is(tok::star));
6809 return Style.PointerAlignment;
This file implements a token annotator, i.e.
Defines the clang::TokenKind enum and support functions.
#define TRANSFORM_TYPE_TRAIT_DEF(Enum, _)
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
Parser - This implements a parser for the C family of languages.
PRESERVE_NONE bool Ret(InterpState &S, CodePtr &PC)
TokenKind
Provides a simple uniform namespace for tokens from all C languages.
The JSON file list parser is used to communicate input to InstallAPI.
AccessSpecifier
A C++ access specifier (public, private, protected), plus the special value "none" which means differ...
nullptr
This class represents a compute construct, representing a 'Kind' of ‘parallel’, 'serial',...
@ Parameter
The parameter type of a method or function.
@ Result
The result type of a method or function.
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.