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);
1300 if (!consumeToken())
1306 bool parseTemplateDeclaration() {
1307 if (!CurrentToken || CurrentToken->isNot(tok::less))
1310 CurrentToken->setType(TT_TemplateOpener);
1313 TemplateDeclarationDepth++;
1314 const bool WellFormed = parseAngle();
1315 TemplateDeclarationDepth--;
1319 if (CurrentToken && TemplateDeclarationDepth == 0)
1320 CurrentToken->Previous->ClosesTemplateDeclaration =
true;
1325 bool consumeToken() {
1327 const auto *Prev = CurrentToken->getPreviousNonComment();
1328 if (Prev && Prev->is(TT_AttributeRSquare) &&
1329 CurrentToken->isOneOf(tok::kw_if, tok::kw_switch, tok::kw_case,
1330 tok::kw_default, tok::kw_for, tok::kw_while) &&
1332 CurrentToken->MustBreakBefore =
true;
1339 if (
Tok->is(TT_VerilogTableItem))
1342 if (
Tok->is(TT_TableGenMultiLineString))
1344 auto *Prev =
Tok->getPreviousNonComment();
1345 auto *
Next =
Tok->getNextNonComment();
1346 switch (
bool IsIf =
false;
Tok->Tok.getKind()) {
1349 if (!Prev && Line.MustBeDeclaration)
1350 Tok->setType(TT_ObjCMethodSpecifier);
1357 if (
Tok->isTypeFinalized())
1360 if (Style.isJavaScript()) {
1361 if (Contexts.back().ColonIsForRangeExpr ||
1362 (Contexts.size() == 1 &&
1363 Line.First->isNoneOf(tok::kw_enum, tok::kw_case)) ||
1364 Contexts.back().ContextKind == tok::l_paren ||
1365 Contexts.back().ContextKind == tok::l_square ||
1366 (!Contexts.back().IsExpression &&
1367 Contexts.back().ContextKind == tok::l_brace) ||
1368 (Contexts.size() == 1 &&
1369 Line.MustBeDeclaration)) {
1370 Contexts.back().IsExpression =
false;
1371 Tok->setType(TT_JsTypeColon);
1374 }
else if (Style.isCSharp()) {
1375 if (Contexts.back().InCSharpAttributeSpecifier) {
1376 Tok->setType(TT_AttributeColon);
1379 if (Contexts.back().ContextKind == tok::l_paren) {
1380 Tok->setType(TT_CSharpNamedArgumentColon);
1383 }
else if (Style.isVerilog() &&
Tok->isNot(TT_BinaryOperator)) {
1386 if (Keywords.isVerilogEnd(*Prev) || Keywords.isVerilogBegin(*Prev)) {
1387 Tok->setType(TT_VerilogBlockLabelColon);
1388 }
else if (Contexts.back().ContextKind == tok::l_square) {
1389 Tok->setType(TT_BitFieldColon);
1390 }
else if (Contexts.back().ColonIsDictLiteral) {
1391 Tok->setType(TT_DictLiteral);
1392 }
else if (Contexts.size() == 1) {
1396 Tok->setType(TT_CaseLabelColon);
1397 if (Line.Level > 1 || (!Line.InPPDirective && Line.Level > 0))
1402 if (Line.First->isOneOf(Keywords.kw_module, Keywords.kw_import) ||
1403 Line.First->startsSequence(tok::kw_export, Keywords.kw_module) ||
1404 Line.First->startsSequence(tok::kw_export, Keywords.kw_import)) {
1405 Tok->setType(TT_ModulePartitionColon);
1406 }
else if (Line.First->is(tok::kw_asm)) {
1407 Tok->setType(TT_InlineASMColon);
1408 }
else if (Contexts.back().ColonIsDictLiteral || Style.isProto()) {
1409 Tok->setType(TT_DictLiteral);
1410 if (Style.isTextProto())
1411 Prev->setType(TT_SelectorName);
1412 }
else if (Contexts.back().ColonIsObjCMethodExpr ||
1413 Line.startsWith(TT_ObjCMethodSpecifier)) {
1414 Tok->setType(TT_ObjCMethodExpr);
1415 const auto *PrevPrev = Prev->Previous;
1418 bool UnknownIdentifierInMethodDeclaration =
1419 Line.startsWith(TT_ObjCMethodSpecifier) &&
1420 Prev->is(tok::identifier) && Prev->is(TT_Unknown);
1423 !(PrevPrev->is(TT_CastRParen) ||
1424 (PrevPrev->is(TT_ObjCMethodExpr) && PrevPrev->is(tok::colon))) ||
1425 PrevPrev->is(tok::r_square) ||
1426 Contexts.back().LongestObjCSelectorName == 0 ||
1427 UnknownIdentifierInMethodDeclaration) {
1428 Prev->setType(TT_SelectorName);
1429 if (!Contexts.back().FirstObjCSelectorName)
1430 Contexts.back().FirstObjCSelectorName = Prev;
1431 else if (Prev->ColumnWidth > Contexts.back().LongestObjCSelectorName)
1432 Contexts.back().LongestObjCSelectorName = Prev->ColumnWidth;
1433 Prev->ParameterIndex =
1434 Contexts.back().FirstObjCSelectorName->ObjCSelectorNameParts;
1435 ++Contexts.back().FirstObjCSelectorName->ObjCSelectorNameParts;
1437 }
else if (Contexts.back().ColonIsForRangeExpr) {
1438 Tok->setType(TT_RangeBasedForLoopColon);
1439 for (
auto *Token = Prev;
1440 Token && Token->isNoneOf(tok::semi, tok::l_paren);
1441 Token = Token->Previous) {
1442 if (Token->isPointerOrReference())
1443 Token->setFinalizedType(TT_PointerOrReference);
1445 }
else if (Contexts.back().ContextType == Context::C11GenericSelection) {
1446 Tok->setType(TT_GenericSelectionColon);
1447 if (Prev->isPointerOrReference())
1448 Prev->setFinalizedType(TT_PointerOrReference);
1449 }
else if ((CurrentToken && CurrentToken->is(tok::numeric_constant)) ||
1450 (Prev->is(TT_StartOfName) && !Scopes.empty() &&
1452 Tok->setType(TT_BitFieldColon);
1453 }
else if (Contexts.size() == 1 &&
1454 Line.getFirstNonComment()->isNoneOf(tok::kw_enum, tok::kw_case,
1456 !Line.startsWith(tok::kw_typedef, tok::kw_enum)) {
1457 if (Prev->isOneOf(tok::r_paren, tok::kw_noexcept) ||
1458 Prev->ClosesRequiresClause) {
1459 Tok->setType(TT_CtorInitializerColon);
1460 }
else if (Prev->is(tok::kw_try)) {
1462 FormatToken *PrevPrev = Prev->getPreviousNonComment();
1465 if (PrevPrev && PrevPrev->isOneOf(tok::r_paren, tok::kw_noexcept))
1466 Tok->setType(TT_CtorInitializerColon);
1468 Tok->setType(TT_InheritanceColon);
1469 if (Prev->isAccessSpecifierKeyword())
1472 }
else if (canBeObjCSelectorComponent(*Prev) &&
Next &&
1473 (
Next->isOneOf(tok::r_paren, tok::comma) ||
1474 (canBeObjCSelectorComponent(*
Next) &&
Next->Next &&
1475 Next->Next->is(tok::colon)))) {
1478 Tok->setType(TT_ObjCSelector);
1485 if (Style.isJavaScript() && !Contexts.back().IsExpression)
1486 Tok->setType(TT_JsTypeOperator);
1489 if (Style.isTableGen()) {
1491 if (!parseTableGenValue())
1493 if (CurrentToken && CurrentToken->is(Keywords.kw_then))
1498 CurrentToken->isOneOf(tok::kw_constexpr, tok::identifier)) {
1504 if (CurrentToken && CurrentToken->is(tok::l_paren)) {
1506 if (!parseParens(IsIf))
1511 if (Style.isJavaScript()) {
1513 if ((Prev && Prev->is(tok::period)) || (
Next &&
Next->is(tok::colon)))
1516 if (CurrentToken && CurrentToken->is(Keywords.kw_await))
1519 if (IsCpp && CurrentToken && CurrentToken->is(tok::kw_co_await))
1521 Contexts.back().ColonIsForRangeExpr =
true;
1522 if (!CurrentToken || CurrentToken->isNot(tok::l_paren))
1533 if (Prev && Prev->is(tok::r_paren) && Prev->MatchingParen &&
1534 Prev->MatchingParen->is(TT_OverloadedOperatorLParen)) {
1535 Prev->setType(TT_OverloadedOperator);
1536 Prev->MatchingParen->setType(TT_OverloadedOperator);
1537 Tok->setType(TT_OverloadedOperatorLParen);
1540 if (Style.isVerilog()) {
1546 auto IsInstancePort = [&]() {
1555 if (!Prev || !(PrevPrev = Prev->getPreviousNonComment()))
1558 if (Keywords.isVerilogIdentifier(*Prev) &&
1559 Keywords.isVerilogIdentifier(*PrevPrev)) {
1563 if (Prev->is(Keywords.kw_verilogHash) &&
1564 Keywords.isVerilogIdentifier(*PrevPrev)) {
1568 if (Keywords.isVerilogIdentifier(*Prev) && PrevPrev->is(tok::r_paren))
1571 if (Keywords.isVerilogIdentifier(*Prev) && PrevPrev->is(tok::comma)) {
1572 const FormatToken *PrevParen = PrevPrev->getPreviousNonComment();
1573 if (PrevParen && PrevParen->is(tok::r_paren) &&
1574 PrevParen->MatchingParen &&
1575 PrevParen->MatchingParen->is(TT_VerilogInstancePortLParen)) {
1582 if (IsInstancePort())
1583 Tok->setType(TT_VerilogInstancePortLParen);
1588 if (Line.MustBeDeclaration && Contexts.size() == 1 &&
1589 !Contexts.back().IsExpression && !Line.startsWith(TT_ObjCProperty) &&
1590 !Line.startsWith(tok::l_paren) &&
1591 Tok->isNoneOf(TT_TypeDeclarationParen, TT_RequiresExpressionLParen)) {
1593 (!Prev->isAttribute() &&
1594 Prev->isNoneOf(TT_RequiresClause, TT_LeadingJavaAnnotation,
1595 TT_BinaryOperator))) {
1596 Line.MightBeFunctionDecl =
true;
1597 Tok->MightBeFunctionDeclParen =
true;
1602 if (Style.isTableGen())
1603 Tok->setType(TT_TableGenListOpener);
1609 if (
Tok->is(TT_RequiresExpressionLBrace))
1611 }
else if (Style.isTextProto()) {
1612 if (Prev && Prev->isNot(TT_DictLiteral))
1613 Prev->setType(TT_SelectorName);
1615 Scopes.push_back(getScopeType(*
Tok));
1621 Tok->setType(TT_TemplateOpener);
1627 if (Style.isTextProto() ||
1628 (Style.Language == FormatStyle::LK_Proto && Prev &&
1629 Prev->isOneOf(TT_SelectorName, TT_DictLiteral))) {
1630 Tok->setType(TT_DictLiteral);
1631 if (Prev && Prev->isNot(TT_DictLiteral))
1632 Prev->setType(TT_SelectorName);
1634 if (Style.isTableGen())
1635 Tok->setType(TT_TemplateOpener);
1637 Tok->setType(TT_BinaryOperator);
1638 NonTemplateLess.insert(
Tok);
1648 if (!Scopes.empty())
1655 if (!Style.isTextProto() &&
Tok->is(TT_Unknown))
1656 Tok->setType(TT_BinaryOperator);
1657 if (Prev && Prev->is(TT_TemplateCloser))
1658 Tok->SpacesRequiredBefore = 1;
1660 case tok::kw_operator:
1661 if (Style.isProto())
1664 if (IsCpp && CurrentToken) {
1665 const auto *Info = CurrentToken->Tok.getIdentifierInfo();
1667 if (Info && !(CurrentToken->isPlacementOperator() ||
1668 CurrentToken->is(tok::kw_co_await) ||
1669 Info->isCPlusPlusOperatorKeyword())) {
1671 if (CurrentToken->startsSequence(tok::kw_decltype, tok::l_paren,
1672 tok::kw_auto, tok::r_paren)) {
1674 LParen = CurrentToken->Next->Next->Next->Next;
1677 for (LParen = CurrentToken->Next;
1678 LParen && LParen->isNot(tok::l_paren); LParen = LParen->Next) {
1679 if (LParen->isPointerOrReference())
1680 LParen->setFinalizedType(TT_PointerOrReference);
1683 if (LParen && LParen->is(tok::l_paren)) {
1684 if (!Contexts.back().IsExpression) {
1685 Tok->setFinalizedType(TT_FunctionDeclarationName);
1686 LParen->setFinalizedType(TT_FunctionDeclarationLParen);
1692 while (CurrentToken &&
1693 CurrentToken->isNoneOf(tok::l_paren, tok::semi, tok::r_paren)) {
1694 if (CurrentToken->isOneOf(tok::star, tok::amp))
1695 CurrentToken->setType(TT_PointerOrReference);
1696 auto Next = CurrentToken->getNextNonComment();
1699 if (
Next->is(tok::less))
1705 auto Previous = CurrentToken->getPreviousNonComment();
1707 if (CurrentToken->is(tok::comma) &&
Previous->isNot(tok::kw_operator))
1709 if (
Previous->isOneOf(TT_BinaryOperator, TT_UnaryOperator, tok::comma,
1712 Previous->isPointerOrReference()) ||
1714 Previous->TokenText.starts_with(
"\"\"")) {
1715 Previous->setType(TT_OverloadedOperator);
1716 if (CurrentToken->isOneOf(tok::less, tok::greater))
1720 if (CurrentToken && CurrentToken->is(tok::l_paren))
1721 CurrentToken->setType(TT_OverloadedOperatorLParen);
1722 if (CurrentToken && CurrentToken->Previous->is(TT_BinaryOperator))
1723 CurrentToken->Previous->setType(TT_OverloadedOperator);
1726 if (Style.isJavaScript() &&
Next &&
1727 Next->isOneOf(tok::semi, tok::comma, tok::colon, tok::r_paren,
1728 tok::r_brace, tok::r_square)) {
1733 Tok->setType(TT_JsTypeOptionalQuestion);
1738 if (Line.MustBeDeclaration && !Contexts.back().IsExpression &&
1739 Style.isJavaScript()) {
1742 if (Style.isCSharp()) {
1745 if (
Next && (
Next->isOneOf(tok::r_paren, tok::greater) ||
1746 Next->startsSequence(tok::identifier, tok::semi) ||
1747 Next->startsSequence(tok::identifier, tok::equal))) {
1748 Tok->setType(TT_CSharpNullable);
1757 if (!Contexts.back().IsExpression && Line.MustBeDeclaration &&
1758 (!
Next ||
Next->isNoneOf(tok::identifier, tok::string_literal) ||
1759 !
Next->Next ||
Next->Next->isNoneOf(tok::colon, tok::question))) {
1760 Tok->setType(TT_CSharpNullable);
1766 case tok::kw_template:
1767 parseTemplateDeclaration();
1770 switch (Contexts.back().ContextType) {
1771 case Context::CtorInitializer:
1772 Tok->setType(TT_CtorInitializerComma);
1774 case Context::InheritanceList:
1775 Tok->setType(TT_InheritanceComma);
1777 case Context::VerilogInstancePortList:
1778 Tok->setType(TT_VerilogInstancePortComma);
1781 if (Style.isVerilog() && Contexts.size() == 1 &&
1782 Line.startsWith(Keywords.kw_assign)) {
1783 Tok->setFinalizedType(TT_VerilogAssignComma);
1784 }
else if (Contexts.back().FirstStartOfName &&
1785 (Contexts.size() == 1 || startsWithInitStatement(Line))) {
1786 Contexts.back().FirstStartOfName->PartOfMultiVariableDeclStmt =
true;
1787 Line.IsMultiVariableDeclStmt =
true;
1791 if (Contexts.back().ContextType == Context::ForEachMacro)
1792 Contexts.back().IsExpression =
true;
1794 case tok::kw_default:
1796 if (Style.isVerilog() && Keywords.isVerilogEndOfLabel(*
Tok) &&
1797 (Line.Level > 1 || (!Line.InPPDirective && Line.Level > 0))) {
1801 case tok::identifier:
1802 if (
Tok->isOneOf(Keywords.kw___has_include,
1803 Keywords.kw___has_include_next)) {
1807 if (
Next &&
Next->is(tok::l_paren) && Prev &&
1808 Prev->isOneOf(tok::kw___cdecl, tok::kw___stdcall,
1809 tok::kw___fastcall, tok::kw___thiscall,
1810 tok::kw___regcall, tok::kw___vectorcall)) {
1811 Tok->setFinalizedType(TT_FunctionDeclarationName);
1812 Next->setFinalizedType(TT_FunctionDeclarationLParen);
1814 }
else if (Style.isCSharp()) {
1815 if (
Tok->is(Keywords.kw_where) &&
Next &&
Next->isNot(tok::l_paren)) {
1816 Tok->setType(TT_CSharpGenericTypeConstraint);
1817 parseCSharpGenericTypeConstraint();
1819 Line.IsContinuation =
true;
1821 }
else if (Style.isTableGen()) {
1822 if (
Tok->is(Keywords.kw_assert)) {
1823 if (!parseTableGenValue())
1825 }
else if (
Tok->isOneOf(Keywords.kw_def, Keywords.kw_defm) &&
1826 (!
Next ||
Next->isNoneOf(tok::colon, tok::l_brace))) {
1828 if (!parseTableGenValue(
true))
1832 if (Style.AllowBreakBeforeQtProperty &&
1833 Contexts.back().ContextType == Context::QtProperty &&
1834 Tok->isQtProperty()) {
1835 Tok->setFinalizedType(TT_QtProperty);
1839 if (
Tok->isNot(TT_LambdaArrow) && Prev && Prev->is(tok::kw_noexcept))
1840 Tok->setType(TT_TrailingReturnArrow);
1844 if (Style.isTableGen() && !parseTableGenValue())
1846 if (!Scopes.empty() && Scopes.back() ==
ST_Enum)
1847 Tok->setFinalizedType(TT_EnumEqual);
1855 void parseCSharpGenericTypeConstraint() {
1856 int OpenAngleBracketsCount = 0;
1857 while (CurrentToken) {
1858 if (CurrentToken->is(tok::less)) {
1860 CurrentToken->setType(TT_TemplateOpener);
1861 ++OpenAngleBracketsCount;
1863 }
else if (CurrentToken->is(tok::greater)) {
1864 CurrentToken->setType(TT_TemplateCloser);
1865 --OpenAngleBracketsCount;
1867 }
else if (CurrentToken->is(tok::comma) && OpenAngleBracketsCount == 0) {
1870 CurrentToken->setType(TT_CSharpGenericTypeConstraintComma);
1872 }
else if (CurrentToken->is(Keywords.kw_where)) {
1873 CurrentToken->setType(TT_CSharpGenericTypeConstraint);
1875 }
else if (CurrentToken->is(tok::colon)) {
1876 CurrentToken->setType(TT_CSharpGenericTypeConstraintColon);
1884 void parseIncludeDirective() {
1885 if (CurrentToken && CurrentToken->is(tok::less)) {
1887 while (CurrentToken) {
1890 if (CurrentToken->isNot(tok::comment) &&
1891 !CurrentToken->TokenText.starts_with(
"//")) {
1892 CurrentToken->setType(TT_ImplicitStringLiteral);
1899 void parseWarningOrError() {
1904 while (CurrentToken) {
1905 CurrentToken->setType(TT_ImplicitStringLiteral);
1910 void parsePragma() {
1913 CurrentToken->isOneOf(Keywords.kw_mark, Keywords.kw_option,
1914 Keywords.kw_region)) {
1915 bool IsMarkOrRegion =
1916 CurrentToken->isOneOf(Keywords.kw_mark, Keywords.kw_region);
1919 while (CurrentToken) {
1920 if (IsMarkOrRegion || CurrentToken->Previous->is(TT_BinaryOperator))
1921 CurrentToken->setType(TT_ImplicitStringLiteral);
1927 void parseHasInclude() {
1928 if (!CurrentToken || CurrentToken->isNot(tok::l_paren))
1931 parseIncludeDirective();
1935 LineType parsePreprocessorDirective() {
1936 bool IsFirstToken = CurrentToken->IsFirst;
1942 if (Style.isJavaScript() && IsFirstToken) {
1946 while (CurrentToken) {
1948 CurrentToken->setType(TT_ImplicitStringLiteral);
1954 if (CurrentToken->is(tok::numeric_constant)) {
1955 CurrentToken->SpacesRequiredBefore = 1;
1960 if (!CurrentToken->Tok.getIdentifierInfo())
1964 if (Style.isVerilog() && !Keywords.isVerilogPPDirective(*CurrentToken))
1966 switch (CurrentToken->Tok.getIdentifierInfo()->getPPKeywordID()) {
1967 case tok::pp_include:
1968 case tok::pp_include_next:
1969 case tok::pp_import:
1971 parseIncludeDirective();
1975 case tok::pp_warning:
1976 parseWarningOrError();
1978 case tok::pp_pragma:
1983 Contexts.back().IsExpression =
true;
1986 CurrentToken->SpacesRequiredBefore = 1;
1992 while (CurrentToken) {
1995 if (
Tok->is(tok::l_paren)) {
1997 }
else if (
Tok->isOneOf(Keywords.kw___has_include,
1998 Keywords.kw___has_include_next)) {
2009 NonTemplateLess.clear();
2010 if (!Line.InMacroBody && CurrentToken->is(tok::hash)) {
2014 auto Type = parsePreprocessorDirective();
2022 IdentifierInfo *Info = CurrentToken->Tok.getIdentifierInfo();
2023 if ((Style.isJava() && CurrentToken->is(Keywords.kw_package)) ||
2024 (!Style.isVerilog() && Info &&
2025 Info->getPPKeywordID() == tok::pp_import && CurrentToken->Next &&
2026 CurrentToken->Next->isOneOf(tok::string_literal, tok::identifier,
2029 parseIncludeDirective();
2035 if (CurrentToken->is(tok::less) && Line.Last->is(tok::greater)) {
2036 parseIncludeDirective();
2042 if (Style.Language == FormatStyle::LK_Proto && Line.Level == 0 &&
2043 CurrentToken->isOneOf(Keywords.kw_option, Keywords.kw_package)) {
2045 if (CurrentToken && CurrentToken->is(tok::identifier)) {
2046 while (CurrentToken)
2052 bool KeywordVirtualFound =
false;
2053 bool ImportStatement =
false;
2056 if (Style.isJavaScript() && CurrentToken->is(Keywords.kw_import))
2057 ImportStatement =
true;
2059 while (CurrentToken) {
2060 if (CurrentToken->is(tok::kw_virtual))
2061 KeywordVirtualFound =
true;
2062 if (Style.isJavaScript()) {
2069 if (Line.First->is(tok::kw_export) &&
2070 CurrentToken->is(Keywords.kw_from) && CurrentToken->Next &&
2071 CurrentToken->Next->isStringLiteral()) {
2072 ImportStatement =
true;
2074 if (isClosureImportStatement(*CurrentToken))
2075 ImportStatement =
true;
2077 if (!consumeToken())
2085 if (KeywordVirtualFound)
2087 if (ImportStatement)
2090 if (Line.startsWith(TT_ObjCMethodSpecifier)) {
2091 if (Contexts.back().FirstObjCSelectorName) {
2092 Contexts.back().FirstObjCSelectorName->LongestObjCSelectorName =
2093 Contexts.back().LongestObjCSelectorName;
2098 for (
const auto &ctx : Contexts)
2099 if (ctx.ContextType == Context::StructArrayInitializer)
2109 return Tok.TokenText ==
"goog" &&
Tok.Next &&
Tok.Next->is(tok::period) &&
2111 (
Tok.Next->Next->TokenText ==
"module" ||
2112 Tok.Next->Next->TokenText ==
"provide" ||
2113 Tok.Next->Next->TokenText ==
"require" ||
2114 Tok.Next->Next->TokenText ==
"requireType" ||
2115 Tok.Next->Next->TokenText ==
"forwardDeclare") &&
2116 Tok.Next->Next->Next &&
Tok.Next->Next->Next->is(tok::l_paren);
2119 void resetTokenMetadata() {
2125 if (!CurrentToken->isTypeFinalized() &&
2126 CurrentToken->isNoneOf(
2127 TT_LambdaLSquare, TT_LambdaLBrace, TT_AttributeMacro, TT_IfMacro,
2128 TT_ForEachMacro, TT_TypenameMacro, TT_FunctionLBrace,
2129 TT_ImplicitStringLiteral, TT_InlineASMBrace, TT_FatArrow,
2130 TT_LambdaArrow, TT_NamespaceMacro, TT_OverloadedOperator,
2131 TT_RegexLiteral, TT_TemplateString, TT_ObjCStringLiteral,
2132 TT_UntouchableMacroFunc, TT_StatementAttributeLikeMacro,
2133 TT_FunctionLikeOrFreestandingMacro, TT_ClassLBrace, TT_EnumLBrace,
2134 TT_RecordLBrace, TT_StructLBrace, TT_UnionLBrace, TT_RequiresClause,
2135 TT_RequiresClauseInARequiresExpression, TT_RequiresExpression,
2136 TT_RequiresExpressionLParen, TT_RequiresExpressionLBrace,
2137 TT_CompoundRequirementLBrace, TT_BracedListLBrace,
2138 TT_FunctionLikeMacro)) {
2139 CurrentToken->setType(TT_Unknown);
2141 CurrentToken->Role.reset();
2142 CurrentToken->MatchingParen =
nullptr;
2143 CurrentToken->FakeLParens.clear();
2144 CurrentToken->FakeRParens = 0;
2151 CurrentToken->NestingLevel = Contexts.size() - 1;
2152 CurrentToken->BindingStrength = Contexts.back().BindingStrength;
2153 modifyContext(*CurrentToken);
2154 determineTokenType(*CurrentToken);
2155 CurrentToken = CurrentToken->Next;
2157 resetTokenMetadata();
2165 : ContextKind(ContextKind), BindingStrength(BindingStrength),
2166 IsExpression(IsExpression) {}
2169 unsigned BindingStrength;
2171 unsigned LongestObjCSelectorName = 0;
2172 bool ColonIsForRangeExpr =
false;
2173 bool ColonIsDictLiteral =
false;
2174 bool ColonIsObjCMethodExpr =
false;
2177 bool CanBeExpression =
true;
2178 bool CaretFound =
false;
2179 bool InCpp11AttributeSpecifier =
false;
2180 bool InCSharpAttributeSpecifier =
false;
2181 bool InStaticAssertFirstArgument =
false;
2182 bool VerilogAssignmentFound =
false;
2185 bool VerilogMayBeConcatenation =
false;
2186 bool IsTableGenDAGArgList =
false;
2187 bool IsTableGenBangOpe =
false;
2188 bool IsTableGenCondOpe =
false;
2201 StructArrayInitializer,
2205 C11GenericSelection,
2208 VerilogInstancePortList,
2209 } ContextType = Unknown;
2214 struct ScopedContextCreator {
2215 AnnotatingParser &P;
2217 ScopedContextCreator(AnnotatingParser &P,
tok::TokenKind ContextKind,
2220 P.Contexts.push_back(Context(ContextKind,
2221 P.Contexts.back().BindingStrength + Increase,
2222 P.Contexts.back().IsExpression));
2225 ~ScopedContextCreator() {
2226 if (P.Style.AlignArrayOfStructures != FormatStyle::AIAS_None) {
2227 if (P.Contexts.back().ContextType == Context::StructArrayInitializer) {
2228 P.Contexts.pop_back();
2229 P.Contexts.back().ContextType = Context::StructArrayInitializer;
2233 P.Contexts.pop_back();
2238 auto AssignmentStartsExpression = [&]() {
2242 if (Line.First->isOneOf(tok::kw_using, tok::kw_return))
2244 if (Line.First->is(tok::kw_template)) {
2245 assert(Current.Previous);
2246 if (Current.Previous->is(tok::kw_operator)) {
2254 if (
Tok->isNot(TT_TemplateOpener)) {
2261 if (Contexts.back().ContextKind == tok::less) {
2262 assert(Current.Previous->Previous);
2263 return Current.Previous->Previous->isNoneOf(tok::kw_typename,
2267 Tok =
Tok->MatchingParen;
2270 Tok =
Tok->getNextNonComment();
2274 if (
Tok->isOneOf(tok::kw_class, tok::kw_enum, tok::kw_struct,
2284 if (Style.isJavaScript() &&
2285 (Line.startsWith(Keywords.kw_type, tok::identifier) ||
2286 Line.startsWith(tok::kw_export, Keywords.kw_type,
2287 tok::identifier))) {
2291 return !Current.Previous || Current.Previous->isNot(tok::kw_operator);
2294 if (AssignmentStartsExpression()) {
2295 Contexts.back().IsExpression =
true;
2296 if (!Line.startsWith(TT_UnaryOperator)) {
2299 Previous->Previous->isNoneOf(tok::comma, tok::semi);
2301 if (
Previous->isOneOf(tok::r_square, tok::r_paren, tok::greater)) {
2308 if (
Previous->isOneOf(TT_BinaryOperator, TT_UnaryOperator) &&
2310 Previous->Previous->isNot(tok::equal)) {
2311 Previous->setType(TT_PointerOrReference);
2315 }
else if (Current.is(tok::lessless) &&
2316 (!Current.Previous ||
2317 Current.Previous->isNot(tok::kw_operator))) {
2318 Contexts.back().IsExpression =
true;
2319 }
else if (Current.isOneOf(tok::kw_return, tok::kw_throw)) {
2320 Contexts.back().IsExpression =
true;
2321 }
else if (Current.is(TT_TrailingReturnArrow)) {
2322 Contexts.back().IsExpression =
false;
2323 }
else if (Current.isOneOf(TT_LambdaArrow, Keywords.kw_assert)) {
2324 Contexts.back().IsExpression = Style.isJava();
2325 }
else if (Current.Previous &&
2326 Current.Previous->is(TT_CtorInitializerColon)) {
2327 Contexts.back().IsExpression =
true;
2328 Contexts.back().ContextType = Context::CtorInitializer;
2329 }
else if (Current.Previous && Current.Previous->is(TT_InheritanceColon)) {
2330 Contexts.back().ContextType = Context::InheritanceList;
2331 }
else if (Current.isOneOf(tok::r_paren, tok::greater, tok::comma)) {
2335 Previous->setType(TT_PointerOrReference);
2337 if (Line.MustBeDeclaration &&
2338 Contexts.front().ContextType != Context::CtorInitializer) {
2339 Contexts.back().IsExpression =
false;
2341 }
else if (Current.is(tok::kw_new)) {
2342 Contexts.back().CanBeExpression =
false;
2343 }
else if (Current.is(tok::semi) ||
2344 (Current.is(tok::exclaim) && Current.Previous &&
2345 Current.Previous->isNot(tok::kw_operator))) {
2349 Contexts.back().IsExpression =
true;
2357 if (Current->is(tok::l_paren))
2359 if (Current->is(tok::r_paren))
2363 Current = Current->Next;
2368 static bool isDeductionGuide(
FormatToken &Current) {
2370 if (Current.Previous && Current.Previous->is(tok::r_paren) &&
2371 Current.startsSequence(tok::arrow, tok::identifier, tok::less)) {
2375 while (TemplateCloser) {
2377 if (TemplateCloser->is(tok::l_paren)) {
2379 TemplateCloser = untilMatchingParen(TemplateCloser);
2380 if (!TemplateCloser)
2383 if (TemplateCloser->is(tok::less))
2385 if (TemplateCloser->is(tok::greater))
2389 TemplateCloser = TemplateCloser->Next;
2393 if (TemplateCloser && TemplateCloser->Next &&
2394 TemplateCloser->Next->is(tok::semi) &&
2395 Current.Previous->MatchingParen) {
2399 Current.Previous->MatchingParen->Previous;
2401 return LeadingIdentifier &&
2402 LeadingIdentifier->TokenText == Current.Next->TokenText;
2409 if (Current.isNot(TT_Unknown)) {
2414 if ((Style.isJavaScript() || Style.isCSharp()) &&
2415 Current.is(tok::exclaim)) {
2416 if (Current.Previous) {
2418 Style.isJavaScript()
2419 ? Keywords.isJavaScriptIdentifier(
2420 *Current.Previous,
true)
2421 : Current.Previous->is(tok::identifier);
2423 Current.Previous->isOneOf(
2424 tok::kw_default, tok::kw_namespace, tok::r_paren, tok::r_square,
2425 tok::r_brace, tok::kw_false, tok::kw_true, Keywords.kw_type,
2426 Keywords.kw_get, Keywords.kw_init, Keywords.kw_set) ||
2427 Current.Previous->Tok.isLiteral()) {
2428 Current.setType(TT_NonNullAssertion);
2433 Current.Next->isOneOf(TT_BinaryOperator, Keywords.kw_as)) {
2434 Current.setType(TT_NonNullAssertion);
2442 if ((Style.isJavaScript() || Style.isJava()) &&
2443 Current.is(Keywords.kw_instanceof)) {
2444 Current.setType(TT_BinaryOperator);
2445 }
else if (isStartOfName(Current) &&
2446 (!Line.MightBeFunctionDecl || Current.NestingLevel != 0)) {
2447 Contexts.back().FirstStartOfName = &Current;
2448 Current.setType(TT_StartOfName);
2449 }
else if (Current.is(tok::semi)) {
2453 Contexts.back().FirstStartOfName =
nullptr;
2454 }
else if (Current.isOneOf(tok::kw_auto, tok::kw___auto_type)) {
2456 }
else if (Current.is(tok::arrow) && Style.isJava()) {
2457 Current.setType(TT_LambdaArrow);
2458 }
else if (Current.is(tok::arrow) && Style.isVerilog()) {
2460 Current.setType(TT_BinaryOperator);
2461 }
else if (Current.is(tok::arrow) && AutoFound &&
2462 Line.MightBeFunctionDecl && Current.NestingLevel == 0 &&
2463 Current.Previous->isNoneOf(tok::kw_operator, tok::identifier)) {
2465 Current.setType(TT_TrailingReturnArrow);
2466 }
else if (Current.is(tok::arrow) && Current.Previous &&
2467 Current.Previous->is(tok::r_brace) &&
2471 Current.setType(TT_TrailingReturnArrow);
2472 }
else if (isDeductionGuide(Current)) {
2474 Current.setType(TT_TrailingReturnArrow);
2475 }
else if (Current.isPointerOrReference()) {
2476 Current.setType(determineStarAmpUsage(
2478 (Contexts.back().CanBeExpression && Contexts.back().IsExpression) ||
2479 Contexts.back().InStaticAssertFirstArgument,
2480 Contexts.back().ContextType == Context::TemplateArgument));
2481 }
else if (Current.isOneOf(tok::minus, tok::plus, tok::caret) ||
2482 (Style.isVerilog() && Current.is(tok::pipe))) {
2483 Current.setType(determinePlusMinusCaretUsage(Current));
2484 if (Current.is(TT_UnaryOperator) && Current.is(tok::caret))
2485 Contexts.back().CaretFound =
true;
2486 }
else if (Current.isOneOf(tok::minusminus, tok::plusplus)) {
2487 Current.setType(determineIncrementUsage(Current));
2488 }
else if (Current.isOneOf(tok::exclaim, tok::tilde)) {
2489 Current.setType(TT_UnaryOperator);
2490 }
else if (Current.is(tok::question)) {
2491 if (Style.isJavaScript() && Line.MustBeDeclaration &&
2492 !Contexts.back().IsExpression) {
2495 Current.setType(TT_JsTypeOptionalQuestion);
2496 }
else if (Style.isTableGen()) {
2498 Current.setType(TT_Unknown);
2500 Current.setType(TT_ConditionalExpr);
2502 }
else if (Current.isBinaryOperator() &&
2503 (!Current.Previous || Current.Previous->isNot(tok::l_square)) &&
2504 (Current.isNot(tok::greater) && !Style.isTextProto())) {
2505 if (Style.isVerilog()) {
2506 if (Current.is(tok::lessequal) && Contexts.size() == 1 &&
2507 !Contexts.back().VerilogAssignmentFound) {
2511 Current.setFinalizedType(TT_BinaryOperator);
2514 Contexts.back().VerilogAssignmentFound =
true;
2516 Current.setType(TT_BinaryOperator);
2517 }
else if (Current.is(tok::comment)) {
2518 if (Current.TokenText.starts_with(
"/*")) {
2519 if (Current.TokenText.ends_with(
"*/")) {
2520 Current.setType(TT_BlockComment);
2524 Current.Tok.setKind(tok::unknown);
2527 Current.setType(TT_LineComment);
2529 }
else if (Current.is(tok::string_literal)) {
2530 if (Style.isVerilog() && Contexts.back().VerilogMayBeConcatenation &&
2531 Current.getPreviousNonComment() &&
2532 Current.getPreviousNonComment()->isOneOf(tok::comma, tok::l_brace) &&
2533 Current.getNextNonComment() &&
2534 Current.getNextNonComment()->isOneOf(tok::comma, tok::r_brace)) {
2535 Current.setType(TT_StringInConcatenation);
2537 }
else if (Current.is(tok::l_paren)) {
2538 if (lParenStartsCppCast(Current))
2539 Current.setType(TT_CppCastLParen);
2540 }
else if (Current.is(tok::r_paren)) {
2541 if (rParenEndsCast(Current))
2542 Current.setType(TT_CastRParen);
2543 if (Current.MatchingParen && Current.Next &&
2544 !Current.Next->isBinaryOperator() &&
2545 Current.Next->isNoneOf(
2546 tok::semi, tok::colon, tok::l_brace, tok::l_paren, tok::comma,
2547 tok::period, tok::arrow, tok::coloncolon, tok::kw_noexcept)) {
2548 if (
FormatToken *AfterParen = Current.MatchingParen->Next;
2549 AfterParen && AfterParen->isNot(tok::caret)) {
2551 if (
FormatToken *BeforeParen = Current.MatchingParen->Previous;
2552 BeforeParen && BeforeParen->is(tok::identifier) &&
2553 BeforeParen->isNot(TT_TypenameMacro) &&
2554 BeforeParen->TokenText == BeforeParen->TokenText.upper() &&
2555 (!BeforeParen->Previous ||
2556 BeforeParen->Previous->ClosesTemplateDeclaration ||
2557 BeforeParen->Previous->ClosesRequiresClause)) {
2558 Current.setType(TT_FunctionAnnotationRParen);
2562 }
else if (Current.is(tok::at) && Current.Next && !Style.isJavaScript() &&
2566 switch (Current.Next->Tok.getObjCKeywordID()) {
2567 case tok::objc_interface:
2568 case tok::objc_implementation:
2569 case tok::objc_protocol:
2570 Current.setType(TT_ObjCDecl);
2572 case tok::objc_property:
2573 Current.setType(TT_ObjCProperty);
2578 }
else if (Current.is(tok::period)) {
2579 FormatToken *PreviousNoComment = Current.getPreviousNonComment();
2580 if (PreviousNoComment &&
2581 PreviousNoComment->isOneOf(tok::comma, tok::l_brace)) {
2582 Current.setType(TT_DesignatedInitializerPeriod);
2583 }
else if (Style.isJava() && Current.Previous &&
2584 Current.Previous->isOneOf(TT_JavaAnnotation,
2585 TT_LeadingJavaAnnotation)) {
2586 Current.setType(Current.Previous->getType());
2588 }
else if (canBeObjCSelectorComponent(Current) &&
2591 Current.Previous && Current.Previous->is(TT_CastRParen) &&
2592 Current.Previous->MatchingParen &&
2593 Current.Previous->MatchingParen->Previous &&
2594 Current.Previous->MatchingParen->Previous->is(
2595 TT_ObjCMethodSpecifier)) {
2599 Current.setType(TT_SelectorName);
2600 }
else if (Current.isOneOf(tok::identifier, tok::kw_const, tok::kw_noexcept,
2601 tok::kw_requires) &&
2603 Current.Previous->isNoneOf(tok::equal, tok::at,
2604 TT_CtorInitializerComma,
2605 TT_CtorInitializerColon) &&
2606 Line.MightBeFunctionDecl && Contexts.size() == 1) {
2609 Current.setType(TT_TrailingAnnotation);
2610 }
else if ((Style.isJava() || Style.isJavaScript()) && Current.Previous) {
2611 if (Current.Previous->is(tok::at) &&
2612 Current.isNot(Keywords.kw_interface)) {
2616 Current.setType(TT_LeadingJavaAnnotation);
2618 Current.setType(TT_JavaAnnotation);
2619 }
else if (Current.Previous->is(tok::period) &&
2620 Current.Previous->isOneOf(TT_JavaAnnotation,
2621 TT_LeadingJavaAnnotation)) {
2622 Current.setType(Current.Previous->getType());
2634 if (Style.isVerilog())
2637 if (!
Tok.Previous ||
Tok.isNot(tok::identifier) ||
Tok.is(TT_ClassHeadName))
2640 if (
Tok.endsSequence(Keywords.kw_final, TT_ClassHeadName))
2643 if ((Style.isJavaScript() || Style.isJava()) &&
Tok.is(Keywords.kw_extends))
2646 if (
const auto *NextNonComment =
Tok.getNextNonComment();
2647 (!NextNonComment && !Line.InMacroBody) ||
2649 (NextNonComment->isPointerOrReference() ||
2650 NextNonComment->isOneOf(TT_ClassHeadName, tok::string_literal) ||
2651 (Line.InPragmaDirective && NextNonComment->is(tok::identifier))))) {
2655 if (
Tok.Previous->isOneOf(TT_LeadingJavaAnnotation, Keywords.kw_instanceof,
2659 if (Style.isJavaScript() &&
Tok.Previous->is(Keywords.kw_in))
2666 if (!Style.isJavaScript())
2667 while (PreviousNotConst && PreviousNotConst->is(tok::kw_const))
2668 PreviousNotConst = PreviousNotConst->getPreviousNonComment();
2670 if (!PreviousNotConst)
2673 if (PreviousNotConst->ClosesRequiresClause)
2676 if (Style.isTableGen()) {
2678 if (Keywords.isTableGenDefinition(*PreviousNotConst))
2681 if (Contexts.back().ContextKind != tok::l_brace)
2685 bool IsPPKeyword = PreviousNotConst->is(tok::identifier) &&
2686 PreviousNotConst->Previous &&
2687 PreviousNotConst->Previous->is(tok::hash);
2689 if (PreviousNotConst->is(TT_TemplateCloser)) {
2690 return PreviousNotConst && PreviousNotConst->MatchingParen &&
2691 PreviousNotConst->MatchingParen->Previous &&
2692 PreviousNotConst->MatchingParen->Previous->isNoneOf(
2693 tok::period, tok::kw_template);
2696 if ((PreviousNotConst->is(tok::r_paren) &&
2697 PreviousNotConst->is(TT_TypeDeclarationParen)) ||
2698 PreviousNotConst->is(TT_AttributeRParen)) {
2707 if (PreviousNotConst->isOneOf(tok::identifier, tok::kw_auto) &&
2708 PreviousNotConst->isNot(TT_StatementAttributeLikeMacro)) {
2713 if (PreviousNotConst->is(TT_PointerOrReference) ||
2714 PreviousNotConst->endsSequence(tok::coloncolon,
2715 TT_PointerOrReference)) {
2720 if (PreviousNotConst->isTypeName(LangOpts))
2724 if (Style.isJava() && PreviousNotConst->is(tok::r_square))
2728 return Style.isJavaScript() && PreviousNotConst->is(tok::kw_const);
2738 if (LeftOfParens && LeftOfParens->is(TT_TemplateCloser) &&
2739 LeftOfParens->MatchingParen) {
2740 auto *Prev = LeftOfParens->MatchingParen->getPreviousNonComment();
2742 Prev->isOneOf(tok::kw_const_cast, tok::kw_dynamic_cast,
2743 tok::kw_reinterpret_cast, tok::kw_static_cast)) {
2754 assert(
Tok.is(tok::r_paren));
2756 if (!
Tok.MatchingParen || !
Tok.Previous)
2760 if (!IsCpp && !Style.isCSharp() && !Style.isJava())
2763 const auto *LParen =
Tok.MatchingParen;
2764 const auto *BeforeRParen =
Tok.Previous;
2765 const auto *AfterRParen =
Tok.Next;
2768 if (BeforeRParen == LParen || !AfterRParen)
2771 if (LParen->isOneOf(TT_OverloadedOperatorLParen, TT_FunctionTypeLParen))
2774 auto *LeftOfParens = LParen->getPreviousNonComment();
2778 if (LeftOfParens->is(tok::r_paren) &&
2779 LeftOfParens->isNot(TT_CastRParen)) {
2780 if (!LeftOfParens->MatchingParen ||
2781 !LeftOfParens->MatchingParen->Previous) {
2784 LeftOfParens = LeftOfParens->MatchingParen->Previous;
2787 if (LeftOfParens->is(tok::r_square)) {
2790 if (
Tok->isNot(tok::r_square))
2793 Tok =
Tok->getPreviousNonComment();
2794 if (!
Tok ||
Tok->isNot(tok::l_square))
2797 Tok =
Tok->getPreviousNonComment();
2798 if (!
Tok ||
Tok->isNot(tok::kw_delete))
2802 if (
FormatToken *MaybeDelete = MayBeArrayDelete(LeftOfParens))
2803 LeftOfParens = MaybeDelete;
2809 if (LeftOfParens->Tok.getIdentifierInfo() && LeftOfParens->Previous &&
2810 LeftOfParens->Previous->is(tok::kw_operator)) {
2816 if (LeftOfParens->Tok.getIdentifierInfo() &&
2817 LeftOfParens->isNoneOf(Keywords.kw_in, tok::kw_return, tok::kw_case,
2818 tok::kw_delete, tok::kw_throw)) {
2824 if (LeftOfParens->isOneOf(tok::at, tok::r_square, TT_OverloadedOperator,
2825 TT_TemplateCloser, tok::ellipsis)) {
2830 if (AfterRParen->is(tok::question) ||
2831 (AfterRParen->is(tok::ampamp) && !BeforeRParen->isTypeName(LangOpts))) {
2836 if (AfterRParen->is(Keywords.kw_in) && Style.isCSharp())
2841 if (AfterRParen->isOneOf(tok::kw_noexcept, tok::kw_volatile, tok::kw_const,
2842 tok::kw_requires, tok::kw_throw, tok::arrow,
2843 Keywords.kw_override, Keywords.kw_final) ||
2844 isCppAttribute(IsCpp, *AfterRParen)) {
2850 if (Style.isJava() && AfterRParen->is(tok::l_paren))
2854 if (AfterRParen->isOneOf(tok::kw_sizeof, tok::kw_alignof) ||
2855 (AfterRParen->Tok.isLiteral() &&
2856 AfterRParen->isNot(tok::string_literal))) {
2861 if (
Tok.isNot(TT_TemplateCloser))
2863 const auto *
Less =
Tok.MatchingParen;
2866 const auto *BeforeLess =
Less->getPreviousNonComment();
2867 return BeforeLess && BeforeLess->isNot(TT_VariableTemplate);
2871 auto IsQualifiedPointerOrReference = [](
const FormatToken *T,
2872 const LangOptions &LangOpts) {
2874 assert(!T->isTypeName(LangOpts) &&
"Should have already been checked");
2878 if (T->is(TT_AttributeRParen)) {
2880 assert(T->is(tok::r_paren));
2881 assert(T->MatchingParen);
2882 assert(T->MatchingParen->is(tok::l_paren));
2883 assert(T->MatchingParen->is(TT_AttributeLParen));
2884 if (
const auto *
Tok = T->MatchingParen->Previous;
2885 Tok &&
Tok->isAttribute()) {
2889 }
else if (T->is(TT_AttributeRSquare)) {
2891 if (T->MatchingParen && T->MatchingParen->Previous) {
2892 T = T->MatchingParen->Previous;
2895 }
else if (T->canBePointerOrReferenceQualifier()) {
2901 return T && T->is(TT_PointerOrReference);
2904 bool ParensAreType = IsNonVariableTemplate(*BeforeRParen) ||
2905 BeforeRParen->is(TT_TypeDeclarationParen) ||
2906 BeforeRParen->isTypeName(LangOpts) ||
2907 IsQualifiedPointerOrReference(BeforeRParen, LangOpts);
2908 bool ParensCouldEndDecl =
2909 AfterRParen->isOneOf(tok::equal, tok::semi, tok::l_brace, tok::greater);
2910 if (ParensAreType && !ParensCouldEndDecl)
2921 for (
const auto *Token = LParen->Next; Token != &
Tok; Token = Token->Next)
2922 if (Token->is(TT_BinaryOperator))
2927 if (AfterRParen->isOneOf(tok::identifier, tok::kw_this))
2931 if (AfterRParen->is(tok::l_paren)) {
2932 for (
const auto *Prev = BeforeRParen; Prev->is(tok::identifier);) {
2933 Prev = Prev->Previous;
2934 if (Prev->is(tok::coloncolon))
2935 Prev = Prev->Previous;
2941 if (!AfterRParen->Next)
2946 if (Style.Language != FormatStyle::LK_C && AfterRParen->is(tok::l_brace) &&
2954 const bool NextIsAmpOrStar = AfterRParen->isOneOf(tok::amp, tok::star);
2955 if (!(AfterRParen->isUnaryOperator() || NextIsAmpOrStar) ||
2956 AfterRParen->is(tok::plus) ||
2957 AfterRParen->Next->isNoneOf(tok::identifier, tok::numeric_constant)) {
2961 if (NextIsAmpOrStar &&
2962 (AfterRParen->Next->is(tok::numeric_constant) || Line.InPPDirective)) {
2966 if (Line.InPPDirective && AfterRParen->is(tok::minus))
2969 const auto *Prev = BeforeRParen;
2972 if (Prev->is(tok::r_paren)) {
2973 if (Prev->is(TT_CastRParen))
2975 Prev = Prev->MatchingParen;
2978 Prev = Prev->Previous;
2979 if (!Prev || Prev->isNot(tok::r_paren))
2981 Prev = Prev->MatchingParen;
2982 return Prev && Prev->is(TT_FunctionTypeLParen);
2986 for (Prev = BeforeRParen; Prev != LParen; Prev = Prev->Previous)
2987 if (Prev->isNoneOf(tok::kw_const, tok::identifier, tok::coloncolon))
3005 if (PrevToken->isOneOf(
3006 TT_ConditionalExpr, tok::l_paren, tok::comma, tok::colon, tok::semi,
3007 tok::equal, tok::question, tok::l_square, tok::l_brace,
3008 tok::kw_case, tok::kw_co_await, tok::kw_co_return, tok::kw_co_yield,
3009 tok::kw_delete, tok::kw_return, tok::kw_throw)) {
3016 if (PrevToken->is(tok::kw_sizeof))
3020 if (PrevToken->isOneOf(TT_CastRParen, TT_UnaryOperator))
3024 if (PrevToken->is(TT_BinaryOperator))
3032 bool InTemplateArgument) {
3033 if (Style.isJavaScript())
3034 return TT_BinaryOperator;
3037 if (Style.isCSharp() &&
Tok.is(tok::ampamp))
3038 return TT_BinaryOperator;
3040 if (Style.isVerilog()) {
3045 if (
Tok.is(tok::star))
3046 return TT_BinaryOperator;
3047 return determineUnaryOperatorByUsage(
Tok) ? TT_UnaryOperator
3048 : TT_BinaryOperator;
3053 return TT_UnaryOperator;
3054 if (PrevToken->isTypeName(LangOpts))
3055 return TT_PointerOrReference;
3056 if (PrevToken->isPlacementOperator() &&
Tok.is(tok::ampamp))
3057 return TT_BinaryOperator;
3059 auto *NextToken =
Tok.getNextNonComment();
3061 return TT_PointerOrReference;
3062 if (NextToken->is(tok::greater))
3063 return TT_PointerOrReference;
3065 if (InTemplateArgument && NextToken->is(tok::kw_noexcept))
3066 return TT_BinaryOperator;
3068 if (NextToken->isOneOf(tok::arrow, tok::equal, tok::comma, tok::r_paren,
3069 tok::semi, TT_RequiresClause) ||
3070 (NextToken->is(tok::kw_noexcept) && !IsExpression) ||
3071 NextToken->canBePointerOrReferenceQualifier() ||
3072 (NextToken->is(tok::l_brace) && !NextToken->getNextNonComment())) {
3073 return TT_PointerOrReference;
3076 if (PrevToken->is(tok::coloncolon))
3077 return TT_PointerOrReference;
3079 if (PrevToken->is(tok::r_paren) && PrevToken->is(TT_TypeDeclarationParen))
3080 return TT_PointerOrReference;
3082 if (determineUnaryOperatorByUsage(
Tok))
3083 return TT_UnaryOperator;
3085 if (NextToken->is(tok::l_square) && NextToken->isNot(TT_LambdaLSquare))
3086 return TT_PointerOrReference;
3087 if (NextToken->is(tok::kw_operator) && !IsExpression)
3088 return TT_PointerOrReference;
3100 if (PrevToken->is(tok::r_brace) &&
Tok.is(tok::star) &&
3101 !PrevToken->MatchingParen) {
3102 return TT_PointerOrReference;
3105 if (PrevToken->endsSequence(tok::r_square, tok::l_square, tok::kw_delete))
3106 return TT_UnaryOperator;
3108 if (PrevToken->Tok.isLiteral() ||
3109 PrevToken->isOneOf(tok::r_paren, tok::r_square, tok::kw_true,
3110 tok::kw_false, tok::r_brace)) {
3111 return TT_BinaryOperator;
3115 while (NextNonParen && NextNonParen->is(tok::l_paren))
3116 NextNonParen = NextNonParen->getNextNonComment();
3117 if (NextNonParen && (NextNonParen->Tok.isLiteral() ||
3118 NextNonParen->isOneOf(tok::kw_true, tok::kw_false) ||
3119 NextNonParen->isUnaryOperator())) {
3120 return TT_BinaryOperator;
3126 if (InTemplateArgument && NextToken->Tok.isAnyIdentifier())
3127 return TT_BinaryOperator;
3131 if (
Tok.is(tok::ampamp) &&
3132 NextToken->isOneOf(tok::l_paren, tok::star, tok::amp)) {
3133 return TT_BinaryOperator;
3140 if (NextToken->Tok.isAnyIdentifier()) {
3141 auto *NextNextToken = NextToken->getNextNonComment();
3142 if (NextNextToken) {
3143 if (NextNextToken->is(tok::arrow))
3144 return TT_BinaryOperator;
3145 if (NextNextToken->isPointerOrReference() &&
3146 !NextToken->isObjCLifetimeQualifier(Style)) {
3147 NextNextToken->setFinalizedType(TT_BinaryOperator);
3148 return TT_BinaryOperator;
3155 if (IsExpression && !Contexts.back().CaretFound &&
3156 Line.getFirstNonComment()->isNot(
3157 TT_RequiresClauseInARequiresExpression)) {
3158 return TT_BinaryOperator;
3162 if (!Scopes.empty() && Scopes.back() ==
ST_Class)
3163 return TT_PointerOrReference;
3166 auto IsChainedOperatorAmpOrMember = [](
const FormatToken *token) {
3167 return !token || token->isOneOf(tok::amp, tok::period, tok::arrow,
3168 tok::arrowstar, tok::periodstar);
3173 if (
Tok.is(tok::amp) && PrevToken->Tok.isAnyIdentifier() &&
3174 IsChainedOperatorAmpOrMember(PrevToken->getPreviousNonComment()) &&
3175 NextToken && NextToken->Tok.isAnyIdentifier()) {
3176 if (
auto NextNext = NextToken->getNextNonComment();
3178 (IsChainedOperatorAmpOrMember(NextNext) || NextNext->is(tok::semi))) {
3179 return TT_BinaryOperator;
3185 return TT_BinaryOperator;
3188 return TT_PointerOrReference;
3192 if (determineUnaryOperatorByUsage(
Tok))
3193 return TT_UnaryOperator;
3197 return TT_UnaryOperator;
3199 if (PrevToken->is(tok::at))
3200 return TT_UnaryOperator;
3203 return TT_BinaryOperator;
3209 if (!PrevToken || PrevToken->is(TT_CastRParen))
3210 return TT_UnaryOperator;
3211 if (PrevToken->isOneOf(tok::r_paren, tok::r_square, tok::identifier))
3212 return TT_TrailingUnaryOperator;
3214 return TT_UnaryOperator;
3217 SmallVector<Context, 8> Contexts;
3219 const FormatStyle &Style;
3220 AnnotatedLine &Line;
3224 LangOptions LangOpts;
3225 const AdditionalKeywords &Keywords;
3227 SmallVector<ScopeType> &Scopes;
3233 llvm::SmallPtrSet<FormatToken *, 16> NonTemplateLess;
3235 int TemplateDeclarationDepth;
3243class ExpressionParser {
3245 ExpressionParser(
const FormatStyle &Style,
const AdditionalKeywords &Keywords,
3246 AnnotatedLine &Line)
3247 : Style(Style), Keywords(Keywords), Line(Line), Current(Line.
First) {}
3250 void parse(
int Precedence = 0) {
3253 while (Current && (Current->is(tok::kw_return) ||
3254 (Current->is(tok::colon) &&
3255 Current->isOneOf(TT_ObjCMethodExpr, TT_DictLiteral)))) {
3259 if (!Current || Precedence > PrecedenceArrowAndPeriod)
3264 parseConditionalExpr();
3270 if (Precedence == PrecedenceUnaryOperator) {
3271 parseUnaryOperator();
3288 if (Style.isVerilog() && Precedence ==
prec::Comma) {
3289 VerilogFirstOfType =
3290 verilogGroupDecl(VerilogFirstOfType, LatestOperator);
3294 parse(Precedence + 1);
3296 int CurrentPrecedence = getCurrentPrecedence();
3305 if (Style.BreakBinaryOperations.PerOperator.empty() &&
3306 Style.BreakBinaryOperations.Default ==
3307 FormatStyle::BBO_OnePerLine) {
3312 if (Precedence == CurrentPrecedence && Current &&
3313 Current->is(TT_SelectorName)) {
3315 addFakeParenthesis(Start,
prec::Level(Precedence));
3319 if ((Style.isCSharp() || Style.isJavaScript() || Style.isJava()) &&
3323 FormatToken *Prev = Current->getPreviousNonComment();
3324 if (Prev && Prev->is(tok::string_literal) &&
3325 (Prev == Start || Prev->endsSequence(tok::string_literal, tok::plus,
3326 TT_StringInConcatenation))) {
3327 Prev->setType(TT_StringInConcatenation);
3334 (Current->closesScope() &&
3335 (Current->MatchingParen || Current->is(TT_TemplateString))) ||
3336 (CurrentPrecedence != -1 && CurrentPrecedence < Precedence) ||
3345 if (Current->opensScope() ||
3346 Current->isOneOf(TT_RequiresClause,
3347 TT_RequiresClauseInARequiresExpression)) {
3350 while (Current && (!Current->closesScope() || Current->opensScope())) {
3357 if (CurrentPrecedence == Precedence) {
3359 LatestOperator->NextOperator = Current;
3360 LatestOperator = Current;
3364 next(Precedence > 0);
3369 if (Style.isVerilog() && Precedence ==
prec::Comma && VerilogFirstOfType)
3370 addFakeParenthesis(VerilogFirstOfType,
prec::Comma);
3372 if (LatestOperator && (Current || Precedence > 0)) {
3378 Start->Previous->isOneOf(TT_RequiresClause,
3379 TT_RequiresClauseInARequiresExpression))
3381 auto Ret = Current ? Current : Line.Last;
3382 while (!
Ret->ClosesRequiresClause &&
Ret->Previous)
3388 if (Precedence == PrecedenceArrowAndPeriod) {
3392 addFakeParenthesis(Start,
prec::Level(Precedence), End);
3400 int getCurrentPrecedence() {
3402 const FormatToken *NextNonComment = Current->getNextNonComment();
3403 if (Current->is(TT_ConditionalExpr))
3405 if (NextNonComment && Current->is(TT_SelectorName) &&
3406 (NextNonComment->isOneOf(TT_DictLiteral, TT_JsTypeColon) ||
3407 (Style.isProto() && NextNonComment->is(tok::less)))) {
3410 if (Current->is(TT_JsComputedPropertyName))
3412 if (Current->is(TT_LambdaArrow))
3414 if (Current->is(TT_FatArrow))
3416 if (Current->isOneOf(tok::semi, TT_InlineASMColon, TT_SelectorName) ||
3417 (Current->is(tok::comment) && NextNonComment &&
3418 NextNonComment->is(TT_SelectorName))) {
3421 if (Current->is(TT_RangeBasedForLoopColon))
3423 if ((Style.isJava() || Style.isJavaScript()) &&
3424 Current->is(Keywords.kw_instanceof)) {
3427 if (Style.isJavaScript() &&
3428 Current->isOneOf(Keywords.kw_in, Keywords.kw_as)) {
3431 if (Current->isOneOf(TT_BinaryOperator, tok::comma))
3432 return Current->getPrecedence();
3433 if (Current->isOneOf(tok::period, tok::arrow) &&
3434 Current->isNot(TT_TrailingReturnArrow)) {
3435 return PrecedenceArrowAndPeriod;
3437 if ((Style.isJava() || Style.isJavaScript()) &&
3438 Current->isOneOf(Keywords.kw_extends, Keywords.kw_implements,
3439 Keywords.kw_throws)) {
3444 if (Style.isVerilog() && Current->is(tok::colon))
3456 if (Start->MacroParent)
3459 Start->FakeLParens.push_back(Precedence);
3461 Start->StartsBinaryExpression =
true;
3462 if (!End && Current)
3463 End = Current->getPreviousNonComment();
3467 End->EndsBinaryExpression =
true;
3473 void parseUnaryOperator() {
3474 SmallVector<FormatToken *, 2> Tokens;
3475 while (Current && Current->is(TT_UnaryOperator)) {
3476 Tokens.push_back(Current);
3479 parse(PrecedenceArrowAndPeriod);
3486 void parseConditionalExpr() {
3487 while (Current && Current->isTrailingComment())
3491 if (!Current || Current->isNot(tok::question))
3495 if (!Current || Current->isNot(TT_ConditionalExpr))
3502 void next(
bool SkipPastLeadingComments =
true) {
3504 Current = Current->Next;
3506 (Current->NewlinesBefore == 0 || SkipPastLeadingComments) &&
3507 Current->isTrailingComment()) {
3508 Current = Current->Next;
3522 while (Start->startsSequence(tok::l_paren, tok::star)) {
3523 if (!(Start = Start->MatchingParen) ||
3524 !(Start = Start->getNextNonComment())) {
3531 if (
Tok->is(Keywords.kw_assign))
3532 Tok =
Tok->getNextNonComment();
3544 if (
Tok->is(tok::hash)) {
3549 Tok =
Tok->getNextNonComment();
3550 }
else if (
Tok->is(tok::hashhash)) {
3554 Tok =
Tok->getNextNonComment();
3555 }
else if (Keywords.isVerilogQualifier(*
Tok) ||
3556 Keywords.isVerilogIdentifier(*
Tok)) {
3560 while (
Tok &&
Tok->isOneOf(tok::period, tok::coloncolon) &&
3561 (
Tok =
Tok->getNextNonComment())) {
3562 if (Keywords.isVerilogIdentifier(*
Tok))
3563 Tok =
Tok->getNextNonComment();
3567 }
else if (
Tok->is(tok::l_paren)) {
3572 Keywords.kw_highz0, Keywords.kw_highz1, Keywords.kw_large,
3573 Keywords.kw_medium, Keywords.kw_pull0, Keywords.kw_pull1,
3574 Keywords.kw_small, Keywords.kw_strong0, Keywords.kw_strong1,
3575 Keywords.kw_supply0, Keywords.kw_supply1, Keywords.kw_weak0,
3576 Keywords.kw_weak1)) {
3577 Tok->setType(TT_VerilogStrength);
3578 Tok =
Tok->MatchingParen;
3580 Tok->setType(TT_VerilogStrength);
3581 Tok =
Tok->getNextNonComment();
3586 }
else if (
Tok->is(Keywords.kw_verilogHash)) {
3588 if (
Next->is(tok::l_paren))
3591 Tok =
Next->getNextNonComment();
3600 while (
Tok &&
Tok->is(tok::l_square) && (
Tok =
Tok->MatchingParen))
3601 Tok =
Tok->getNextNonComment();
3602 if (
Tok && (
Tok->is(tok::hash) || Keywords.isVerilogIdentifier(*
Tok)))
3611 First->setType(TT_VerilogDimensionedTypeName);
3612 }
else if (
First != Start) {
3620 if (TypedName->is(TT_Unknown))
3621 TypedName->setType(TT_StartOfName);
3623 if (FirstOfType && PreviousComma) {
3624 PreviousComma->setType(TT_VerilogTypeComma);
3625 addFakeParenthesis(FirstOfType,
prec::Comma, PreviousComma->Previous);
3628 FirstOfType = TypedName;
3635 while (Current && Current != FirstOfType) {
3636 if (Current->opensScope()) {
3647 const FormatStyle &Style;
3648 const AdditionalKeywords &Keywords;
3649 const AnnotatedLine &Line;
3659 assert(
Line->First);
3666 Line->First->OriginalColumn) {
3667 const bool PPDirectiveOrImportStmt =
3670 if (PPDirectiveOrImportStmt)
3675 Line->Level = Style.IndentPPDirectives < FormatStyle::PPDIS_BeforeHash &&
3676 PPDirectiveOrImportStmt
3678 : NextNonCommentLine->
Level;
3680 NextNonCommentLine =
Line->First->isNot(tok::r_brace) ?
Line :
nullptr;
3700 if (
Tok->isNot(tok::identifier))
3703 Tok =
Tok->getNextNonComment();
3709 if (
Tok->is(tok::coloncolon))
3710 return Tok->getNextNonComment();
3714 if (
Tok->is(TT_TemplateOpener)) {
3715 Tok =
Tok->MatchingParen;
3719 Tok =
Tok->getNextNonComment();
3724 return Tok->is(tok::coloncolon) ?
Tok->getNextNonComment() :
nullptr;
3732 Tok =
Tok->getNextNonComment()) {
3734 if (
Tok->is(TT_AttributeLSquare)) {
3735 Tok =
Tok->MatchingParen;
3743 if (
Tok->is(tok::l_paren) &&
Tok->is(TT_Unknown) &&
Tok->MatchingParen) {
3751 if (
Tok->isOneOf(tok::kw_friend, tok::kw_inline, tok::kw_virtual,
3752 tok::kw_constexpr, tok::kw_consteval, tok::kw_explicit)) {
3758 if (
Tok->is(tok::kw_template)) {
3759 Tok =
Tok->getNextNonComment();
3765 if (
Tok->isNot(TT_TemplateOpener))
3768 Tok =
Tok->MatchingParen;
3776 if (
Tok->is(tok::coloncolon)) {
3787 if (
Tok->is(tok::tilde)) {
3794 if (
Tok->isNot(tok::identifier) ||
Tok->isNot(TT_Unknown))
3805 assert(
Tok &&
Tok->is(tok::identifier));
3806 const auto *Prev =
Tok->Previous;
3808 if (Prev && Prev->is(tok::tilde))
3809 Prev = Prev->Previous;
3812 if (!Prev || (!Prev->endsSequence(tok::coloncolon, tok::identifier) &&
3813 !Prev->endsSequence(tok::coloncolon, TT_TemplateCloser))) {
3817 assert(Prev->Previous);
3818 if (Prev->Previous->is(TT_TemplateCloser) && Prev->Previous->MatchingParen) {
3819 Prev = Prev->Previous->MatchingParen;
3820 assert(Prev->Previous);
3823 return Prev->Previous->TokenText ==
Tok->TokenText;
3827 if (!
Line.InMacroBody)
3828 MacroBodyScopes.clear();
3830 auto &ScopeStack =
Line.InMacroBody ? MacroBodyScopes : Scopes;
3831 AnnotatingParser
Parser(Style,
Line, Keywords, ScopeStack);
3834 if (!
Line.Children.empty()) {
3837 for (
auto &Child :
Line.Children) {
3838 if (InRequiresExpression &&
3839 Child->First->isNoneOf(tok::kw_typename, tok::kw_requires,
3840 TT_CompoundRequirementLBrace)) {
3846 if (!ScopeStack.empty())
3847 ScopeStack.pop_back();
3860 ExpressionParser ExprParser(Style, Keywords,
Line);
3866 if (
Tok && ((!ScopeStack.empty() && ScopeStack.back() ==
ST_Class) ||
3868 Tok->setFinalizedType(TT_CtorDtorDeclName);
3869 assert(OpeningParen);
3874 if (
Line.startsWith(TT_ObjCMethodSpecifier))
3876 else if (
Line.startsWith(TT_ObjCDecl))
3878 else if (
Line.startsWith(TT_ObjCProperty))
3882 First->SpacesRequiredBefore = 1;
3883 First->CanBreakBefore =
First->MustBreakBefore;
3892 if (Current.
is(TT_FunctionDeclarationName))
3895 if (Current.
isNoneOf(tok::identifier, tok::kw_operator))
3898 const auto *Prev = Current.getPreviousNonComment();
3903 if (
const auto *PrevPrev =
Previous.getPreviousNonComment();
3904 PrevPrev && PrevPrev->is(TT_ObjCDecl)) {
3908 auto skipOperatorName =
3911 if (
Next->is(TT_OverloadedOperatorLParen))
3913 if (
Next->is(TT_OverloadedOperator))
3915 if (
Next->isPlacementOperator() ||
Next->is(tok::kw_co_await)) {
3918 Next->Next->startsSequence(tok::l_square, tok::r_square)) {
3923 if (
Next->startsSequence(tok::l_square, tok::r_square)) {
3928 if ((
Next->isTypeName(LangOpts) ||
Next->is(tok::identifier)) &&
3929 Next->Next &&
Next->Next->isPointerOrReference()) {
3934 if (
Next->is(TT_TemplateOpener) &&
Next->MatchingParen) {
3945 const bool IsCpp = LangOpts.CXXOperatorNames || LangOpts.C11;
3948 if (Current.
is(tok::kw_operator)) {
3949 if (
Line.startsWith(tok::kw_friend))
3951 if (
Previous.Tok.getIdentifierInfo() &&
3952 Previous.isNoneOf(tok::kw_return, tok::kw_co_return)) {
3957 assert(
Previous.MatchingParen->is(tok::l_paren));
3958 assert(
Previous.MatchingParen->is(TT_TypeDeclarationParen));
3967 while (
Next &&
Next->startsSequence(tok::hashhash, tok::identifier))
3970 if (
Next->is(TT_TemplateOpener) &&
Next->MatchingParen) {
3972 }
else if (
Next->is(tok::coloncolon)) {
3976 if (
Next->is(tok::kw_operator)) {
3977 Next = skipOperatorName(
Next->Next);
3980 if (
Next->isNot(tok::identifier))
3982 }
else if (isCppAttribute(IsCpp, *
Next)) {
3986 }
else if (
Next->is(tok::l_paren)) {
3995 if (!
Next ||
Next->isNot(tok::l_paren) || !
Next->MatchingParen)
3997 ClosingParen =
Next->MatchingParen;
3998 assert(ClosingParen->
is(tok::r_paren));
4000 if (
Line.Last->is(tok::l_brace))
4002 if (
Next->Next == ClosingParen)
4005 if (ClosingParen->
Next && ClosingParen->
Next->
is(TT_PointerOrReference))
4018 if (IsCpp &&
Next->Next &&
Next->Next->is(tok::identifier) &&
4019 !
Line.endsWith(tok::semi)) {
4025 if (
Tok->is(TT_TypeDeclarationParen))
4027 if (
Tok->isOneOf(tok::l_paren, TT_TemplateOpener) &&
Tok->MatchingParen) {
4028 Tok =
Tok->MatchingParen;
4031 if (
Tok->is(tok::kw_const) ||
Tok->isTypeName(LangOpts) ||
4032 Tok->isOneOf(TT_PointerOrReference, TT_StartOfName, tok::ellipsis)) {
4035 if (
Tok->isOneOf(tok::l_brace, TT_ObjCMethodExpr) ||
Tok->Tok.isLiteral())
4041bool TokenAnnotator::mustBreakForReturnType(
const AnnotatedLine &
Line)
const {
4042 assert(
Line.MightBeFunctionDecl);
4044 if ((Style.BreakAfterReturnType == FormatStyle::RTBS_TopLevel ||
4045 Style.BreakAfterReturnType == FormatStyle::RTBS_TopLevelDefinitions) &&
4050 switch (Style.BreakAfterReturnType) {
4051 case FormatStyle::RTBS_None:
4052 case FormatStyle::RTBS_Automatic:
4053 case FormatStyle::RTBS_ExceptShortType:
4055 case FormatStyle::RTBS_All:
4056 case FormatStyle::RTBS_TopLevel:
4058 case FormatStyle::RTBS_AllDefinitions:
4059 case FormatStyle::RTBS_TopLevelDefinitions:
4060 return Line.mightBeFunctionDefinition();
4066bool TokenAnnotator::mustBreakBeforeReturnType(
4068 assert(
Line.MightBeFunctionDecl);
4070 switch (Style.BreakBeforeReturnType) {
4071 case FormatStyle::BBRTS_None:
4073 case FormatStyle::BBRTS_All:
4075 case FormatStyle::BBRTS_TopLevel:
4076 return Line.Level == 0;
4077 case FormatStyle::BBRTS_AllDefinitions:
4078 return Line.mightBeFunctionDefinition();
4079 case FormatStyle::BBRTS_TopLevelDefinitions:
4080 return Line.Level == 0 &&
Line.mightBeFunctionDefinition();
4087 auto *
Tok =
Line.getFirstNonComment();
4091 if (
Tok->is(tok::kw_template)) {
4092 auto *Opener =
Tok->Next;
4093 while (Opener && Opener->isNot(TT_TemplateOpener))
4094 Opener = Opener->Next;
4095 if (!Opener || !Opener->MatchingParen)
4097 Tok = Opener->MatchingParen->Next;
4100 if (
Tok &&
Tok->is(TT_RequiresClause)) {
4101 while (
Tok && !
Tok->ClosesRequiresClause)
4109 Tok->isOneOf(tok::kw___attribute, tok::kw___declspec,
4110 TT_AttributeMacro)) {
4112 if (
Next &&
Next->is(tok::l_paren) &&
Next->MatchingParen)
4113 Tok =
Next->MatchingParen->Next;
4118 if (
Tok->is(TT_AttributeLSquare) &&
Tok->MatchingParen) {
4119 Tok =
Tok->MatchingParen->Next;
4131 Line.Computed =
true;
4139 :
Line.FirstStartColumn +
First->ColumnWidth;
4140 bool AlignArrayOfStructures =
4141 (Style.AlignArrayOfStructures != FormatStyle::AIAS_None &&
4143 if (AlignArrayOfStructures)
4144 calculateArrayInitializerColumnList(
Line);
4146 const auto *FirstNonComment =
Line.getFirstNonComment();
4147 bool SeenName =
false;
4148 bool LineIsFunctionDeclaration =
false;
4152 for (
auto *
Tok = FirstNonComment && FirstNonComment->isNot(tok::kw_using)
4153 ? FirstNonComment->Next
4156 if (
Tok->is(TT_StartOfName))
4158 if (
Tok->Previous->EndsCppAttributeGroup)
4159 AfterLastAttribute =
Tok;
4160 if (
const bool IsCtorOrDtor =
Tok->is(TT_CtorDtorDeclName);
4164 Tok->setFinalizedType(TT_FunctionDeclarationName);
4165 LineIsFunctionDeclaration =
true;
4169 assert(OpeningParen);
4170 if (OpeningParen->is(TT_Unknown))
4171 OpeningParen->setType(TT_FunctionDeclarationLParen);
4178 if ((LineIsFunctionDeclaration ||
4179 (FirstNonComment && FirstNonComment->is(TT_CtorDtorDeclName))) &&
4180 Line.endsWith(tok::semi, tok::r_brace)) {
4181 auto *
Tok =
Line.Last->Previous;
4182 while (
Tok->isNot(tok::r_brace))
4184 if (
auto *LBrace =
Tok->MatchingParen; LBrace && LBrace->is(TT_Unknown)) {
4185 assert(LBrace->is(tok::l_brace));
4188 LBrace->setFinalizedType(TT_FunctionLBrace);
4192 if (SeenName && AfterLastAttribute &&
4195 if (LineIsFunctionDeclaration)
4196 Line.ReturnTypeWrapped =
true;
4199 if (!LineIsFunctionDeclaration) {
4200 Line.ReturnTypeWrapped =
false;
4202 for (
const auto *
Tok = FirstNonComment;
Tok;
Tok =
Tok->Next) {
4203 if (
Tok->isNot(tok::kw_operator))
4207 }
while (
Tok &&
Tok->isNot(TT_OverloadedOperatorLParen));
4208 if (!
Tok || !
Tok->MatchingParen)
4210 const auto *LeftParen =
Tok;
4211 for (
Tok =
Tok->Next;
Tok &&
Tok != LeftParen->MatchingParen;
4213 if (
Tok->isNot(tok::identifier))
4216 const bool NextIsBinaryOperator =
4218 Next->Next->is(tok::identifier);
4219 if (!NextIsBinaryOperator)
4221 Next->setType(TT_BinaryOperator);
4225 }
else if (ClosingParen) {
4227 if (
Tok->is(TT_CtorInitializerColon))
4229 if (
Tok->is(tok::arrow)) {
4230 Tok->overwriteFixedType(TT_TrailingReturnArrow);
4233 if (
Tok->isNot(TT_TrailingAnnotation))
4236 if (!
Next ||
Next->isNot(tok::l_paren))
4245 if (
Line.MightBeFunctionDecl && LineIsFunctionDeclaration &&
4246 mustBreakBeforeReturnType(
Line)) {
4248 ReturnTypeStart && ReturnTypeStart != FirstNonComment &&
4249 ReturnTypeStart->
isNoneOf(TT_FunctionDeclarationName,
4250 TT_CtorDtorDeclName, tok::tilde)) {
4251 ReturnTypeStart->MustBreakBefore =
true;
4252 Line.ReturnTypeWrapped =
true;
4256 if (
First->is(TT_ElseLBrace)) {
4257 First->CanBreakBefore =
true;
4258 First->MustBreakBefore =
true;
4261 bool InFunctionDecl =
Line.MightBeFunctionDecl;
4262 bool InParameterList =
false;
4263 for (
auto *Current =
First->Next; Current; Current = Current->Next) {
4265 if (Current->is(TT_LineComment)) {
4267 Current->SpacesRequiredBefore =
4268 (Style.Cpp11BracedListStyle == FormatStyle::BLS_AlignFirstComment &&
4269 !Style.SpacesInParensOptions.Other)
4272 }
else if (Prev->
is(TT_VerilogMultiLineListLParen)) {
4273 Current->SpacesRequiredBefore = 0;
4275 Current->SpacesRequiredBefore = Style.SpacesBeforeTrailingComments;
4285 if (!Current->HasUnescapedNewline) {
4288 if (
Parameter->isOneOf(tok::comment, tok::r_brace))
4291 if (
Parameter->Previous->isNot(TT_CtorInitializerComma) &&
4299 }
else if (!Current->Finalized && Current->SpacesRequiredBefore == 0 &&
4300 spaceRequiredBefore(
Line, *Current)) {
4301 Current->SpacesRequiredBefore = 1;
4306 Current->MustBreakBefore =
true;
4308 Current->MustBreakBefore =
4309 Current->MustBreakBefore || mustBreakBefore(
Line, *Current);
4310 if (!Current->MustBreakBefore && InFunctionDecl &&
4311 Current->is(TT_FunctionDeclarationName)) {
4312 Current->MustBreakBefore = mustBreakForReturnType(
Line);
4316 Current->CanBreakBefore =
4317 Current->MustBreakBefore || canBreakBefore(
Line, *Current);
4319 if (Current->is(TT_FunctionDeclarationLParen)) {
4320 InParameterList =
true;
4321 }
else if (Current->is(tok::r_paren)) {
4322 const auto *LParen = Current->MatchingParen;
4323 if (LParen && LParen->is(TT_FunctionDeclarationLParen))
4324 InParameterList =
false;
4325 }
else if (InParameterList &&
4326 Current->endsSequence(TT_AttributeMacro,
4327 TT_PointerOrReference)) {
4328 Current->CanBreakBefore =
false;
4331 unsigned ChildSize = 0;
4334 ChildSize = LastOfChild.isTrailingComment() ? Style.ColumnLimit
4337 if (Current->MustBreakBefore || Prev->
Children.size() > 1 ||
4339 Prev->
Children[0]->First->MustBreakBefore) ||
4340 Current->IsMultiline) {
4341 Current->TotalLength = Prev->
TotalLength + Style.ColumnLimit;
4343 Current->TotalLength = Prev->
TotalLength + Current->ColumnWidth +
4344 ChildSize + Current->SpacesRequiredBefore;
4347 if ((Style.PackParameters.BinPack == FormatStyle::BPPS_UseBreakAfter &&
4350 (Style.PackArguments.BinPack == FormatStyle::BPAS_UseBreakAfter &&
4352 Prev->
isOneOf(tok::l_paren, tok::l_brace,
4353 TT_ArrayInitializerLSquare) &&
4356 for (
auto *ParamTok = Current; ParamTok && ParamTok != RParen;
4357 ParamTok = ParamTok->Next) {
4358 if (ParamTok->opensScope()) {
4359 ParamTok = ParamTok->MatchingParen;
4364 ParamTok->MustBreakBefore =
true;
4365 ParamTok->CanBreakBefore =
true;
4370 if (Current->is(TT_ControlStatementLBrace)) {
4371 if (Style.ColumnLimit > 0 &&
4372 Style.BraceWrapping.AfterControlStatement ==
4373 FormatStyle::BWACS_MultiLine &&
4374 Line.Level * Style.IndentWidth +
Line.Last->TotalLength >
4375 Style.ColumnLimit) {
4376 Current->CanBreakBefore =
true;
4377 Current->MustBreakBefore =
true;
4379 }
else if (Current->is(TT_CtorInitializerColon)) {
4380 InFunctionDecl =
false;
4392 Current->SplitPenalty = splitPenalty(
Line, *Current, InFunctionDecl);
4393 if (Style.Language == FormatStyle::LK_ObjC &&
4394 Current->is(TT_SelectorName) && Current->ParameterIndex > 0) {
4395 if (Current->ParameterIndex == 1)
4396 Current->SplitPenalty += 5 * Current->BindingStrength;
4398 Current->SplitPenalty += 20 * Current->BindingStrength;
4402 calculateUnbreakableTailLengths(
Line);
4404 for (
auto *Current =
First; Current; Current = Current->Next) {
4406 Current->Role->precomputeFormattingInfos(Current);
4407 if (Current->MatchingParen &&
4408 Current->MatchingParen->opensBlockOrBlockTypeList(Style) &&
4413 if (Current->opensBlockOrBlockTypeList(Style))
4417 LLVM_DEBUG({ printDebugInfo(
Line); });
4420void TokenAnnotator::calculateUnbreakableTailLengths(
4427 Current->
isOneOf(tok::comment, tok::string_literal)) {
4437void TokenAnnotator::calculateArrayInitializerColumnList(
4441 auto *CurrentToken =
Line.First;
4442 CurrentToken->ArrayInitializerLineStart =
true;
4444 while (CurrentToken && CurrentToken !=
Line.Last) {
4445 if (CurrentToken->is(tok::l_brace)) {
4446 CurrentToken->IsArrayInitializer =
true;
4447 if (CurrentToken->Next)
4448 CurrentToken->Next->MustBreakBefore =
true;
4450 calculateInitializerColumnList(
Line, CurrentToken->Next, Depth + 1);
4452 CurrentToken = CurrentToken->Next;
4457FormatToken *TokenAnnotator::calculateInitializerColumnList(
4459 while (CurrentToken && CurrentToken !=
Line.Last) {
4460 if (CurrentToken->is(tok::l_brace))
4462 else if (CurrentToken->is(tok::r_brace))
4464 if (Depth == 2 && CurrentToken->isOneOf(tok::l_brace, tok::comma)) {
4465 CurrentToken = CurrentToken->Next;
4468 CurrentToken->StartsColumn =
true;
4469 CurrentToken = CurrentToken->Previous;
4471 CurrentToken = CurrentToken->Next;
4473 return CurrentToken;
4478 bool InFunctionDecl)
const {
4482 if (
Left.is(tok::semi))
4486 if (Style.isJava()) {
4487 if (
Right.isOneOf(Keywords.kw_extends, Keywords.kw_throws))
4489 if (
Right.is(Keywords.kw_implements))
4491 if (
Left.is(tok::comma) &&
Left.NestingLevel == 0)
4493 }
else if (Style.isJavaScript()) {
4494 if (
Right.is(Keywords.kw_function) &&
Left.isNot(tok::comma))
4496 if (
Left.is(TT_JsTypeColon))
4498 if ((
Left.is(TT_TemplateString) &&
Left.TokenText.ends_with(
"${")) ||
4499 (
Right.is(TT_TemplateString) &&
Right.TokenText.starts_with(
"}"))) {
4503 if (
Left.opensScope() &&
Right.closesScope())
4505 }
else if (Style.Language == FormatStyle::LK_Proto) {
4506 if (
Right.is(tok::l_square))
4508 if (
Right.is(tok::period))
4512 if (
Right.is(tok::identifier) &&
Right.Next &&
Right.Next->is(TT_DictLiteral))
4514 if (
Right.is(tok::l_square)) {
4515 if (
Left.is(tok::r_square))
4518 if (
Right.is(TT_LambdaLSquare) &&
Left.is(tok::equal))
4520 if (
Right.isNoneOf(TT_ObjCMethodExpr, TT_LambdaLSquare,
4521 TT_ArrayInitializerLSquare,
4522 TT_DesignatedInitializerLSquare, TT_AttributeLSquare)) {
4527 if (
Left.is(tok::coloncolon))
4528 return Style.PenaltyBreakScopeResolution;
4529 if (
Right.isOneOf(TT_StartOfName, TT_FunctionDeclarationName,
4530 tok::kw_operator)) {
4531 if (
Line.startsWith(tok::kw_for) &&
Right.PartOfMultiVariableDeclStmt)
4533 if (
Left.is(TT_StartOfName))
4535 if (InFunctionDecl &&
Right.NestingLevel == 0)
4536 return Style.PenaltyReturnTypeOnItsOwnLine;
4539 if (
Right.is(TT_PointerOrReference))
4541 if (
Right.is(TT_LambdaArrow))
4543 if (
Left.is(tok::equal) &&
Right.is(tok::l_brace))
4545 if (
Left.is(TT_CastRParen))
4547 if (
Left.isOneOf(tok::kw_class, tok::kw_struct, tok::kw_union))
4549 if (
Left.is(tok::comment))
4552 if (
Left.isOneOf(TT_RangeBasedForLoopColon, TT_InheritanceColon,
4553 TT_CtorInitializerColon)) {
4557 if (
Right.isMemberAccess()) {
4578 const auto Penalty = Style.PenaltyBreakBeforeMemberAccess;
4580 ? std::min(Penalty, 35u)
4584 if (
Right.is(TT_TrailingAnnotation) &&
4585 (!
Right.Next ||
Right.Next->isNot(tok::l_paren))) {
4588 if (
Line.startsWith(TT_ObjCMethodSpecifier))
4595 bool is_short_annotation =
Right.TokenText.size() < 10;
4596 return (
Left.is(tok::r_paren) ? 100 : 120) + (is_short_annotation ? 50 : 0);
4600 if (
Line.startsWith(tok::kw_for) &&
Left.is(tok::equal))
4605 if (
Right.is(TT_SelectorName))
4607 if (
Left.is(tok::colon)) {
4608 if (
Left.is(TT_ObjCMethodExpr))
4609 return Line.MightBeFunctionDecl ? 50 : 500;
4610 if (
Left.is(TT_ObjCSelector))
4618 Left.Previous->isOneOf(tok::identifier, tok::greater)) {
4622 if (
Left.is(tok::l_paren) && Style.PenaltyBreakOpenParenthesis != 0)
4623 return Style.PenaltyBreakOpenParenthesis;
4624 if (
Left.is(tok::l_paren) && InFunctionDecl && Style.AlignAfterOpenBracket)
4626 if (
Left.is(tok::l_paren) &&
Left.Previous &&
4627 (
Left.Previous->isOneOf(tok::kw_for, tok::kw__Generic) ||
4628 Left.Previous->isIf())) {
4631 if (
Left.is(tok::equal) && InFunctionDecl)
4633 if (
Right.is(tok::r_brace))
4635 if (
Left.is(TT_TemplateOpener))
4637 if (
Left.opensScope()) {
4641 if (!Style.AlignAfterOpenBracket &&
4642 (
Left.ParameterCount <= 1 || Style.AllowAllArgumentsOnNextLine)) {
4645 if (
Left.is(tok::l_brace) &&
4646 Style.Cpp11BracedListStyle == FormatStyle::BLS_Block) {
4649 return Left.ParameterCount > 1 ? Style.PenaltyBreakBeforeFirstCallParameter
4652 if (
Left.is(TT_JavaAnnotation))
4655 if (
Left.is(TT_UnaryOperator))
4657 if (
Left.isOneOf(tok::plus, tok::comma) &&
Left.Previous &&
4658 Left.Previous->isLabelString() &&
4659 (
Left.NextOperator ||
Left.OperatorIndex != 0)) {
4662 if (
Right.is(tok::plus) &&
Left.isLabelString() &&
4663 (
Right.NextOperator ||
Right.OperatorIndex != 0)) {
4666 if (
Left.is(tok::comma))
4668 if (
Right.is(tok::lessless) &&
Left.isLabelString() &&
4669 (
Right.NextOperator ||
Right.OperatorIndex != 1)) {
4672 if (
Right.is(tok::lessless)) {
4674 if (
Left.isNot(tok::r_paren) ||
Right.OperatorIndex > 0) {
4680 if (
Left.ClosesTemplateDeclaration)
4681 return Style.PenaltyBreakTemplateDeclaration;
4682 if (
Left.ClosesRequiresClause)
4684 if (
Left.is(TT_ConditionalExpr))
4690 return Style.PenaltyBreakAssignment;
4697bool TokenAnnotator::spaceRequiredBeforeParens(
const FormatToken &Right)
const {
4698 if (Style.SpaceBeforeParens == FormatStyle::SBPO_Always)
4700 if (
Right.is(TT_OverloadedOperatorLParen) &&
4701 Style.SpaceBeforeParensOptions.AfterOverloadedOperator) {
4704 if (Style.SpaceBeforeParensOptions.BeforeNonEmptyParentheses &&
4705 Right.ParameterCount > 0) {
4714 if (
Left.is(tok::kw_return) &&
4715 Right.isNoneOf(tok::semi, tok::r_paren, tok::hashhash)) {
4718 if (
Left.is(tok::kw_throw) &&
Right.is(tok::l_paren) &&
Right.MatchingParen &&
4719 Right.MatchingParen->is(TT_CastRParen)) {
4722 if (
Left.is(Keywords.kw_assert) && Style.isJava())
4725 Left.is(tok::objc_property)) {
4728 if (
Right.is(tok::hashhash))
4729 return Left.is(tok::hash);
4730 if (
Left.isOneOf(tok::hashhash, tok::hash))
4731 return Right.is(tok::hash);
4732 if (Style.SpacesInParens == FormatStyle::SIPO_Custom) {
4733 if (
Left.is(tok::l_paren) &&
Right.is(tok::r_paren))
4734 return Style.SpacesInParensOptions.InEmptyParentheses;
4735 if (Style.SpacesInParensOptions.ExceptDoubleParentheses &&
4736 Left.is(tok::r_paren) &&
Right.is(tok::r_paren)) {
4737 auto *InnerLParen =
Left.MatchingParen;
4738 if (InnerLParen && InnerLParen->Previous ==
Right.MatchingParen) {
4739 InnerLParen->SpacesRequiredBefore = 0;
4744 if (
Left.is(tok::l_paren))
4746 else if (
Right.is(tok::r_paren) &&
Right.MatchingParen)
4747 LeftParen =
Right.MatchingParen;
4748 if (LeftParen && (LeftParen->is(TT_ConditionLParen) ||
4749 (LeftParen->Previous &&
4750 isKeywordWithCondition(*LeftParen->Previous)))) {
4751 return Style.SpacesInParensOptions.InConditionalStatements;
4756 if (
Left.is(tok::kw_auto) &&
Right.isOneOf(TT_LambdaLBrace, TT_FunctionLBrace,
4758 TT_FunctionTypeLParen)) {
4763 if (
Left.is(tok::kw_auto) &&
Right.isOneOf(tok::l_paren, tok::l_brace))
4766 const auto *BeforeLeft =
Left.Previous;
4769 if (
Right.is(tok::l_paren) &&
Left.is(tok::kw_co_await) && BeforeLeft &&
4770 BeforeLeft->is(tok::kw_operator)) {
4774 if (
Left.isOneOf(tok::kw_co_await, tok::kw_co_yield, tok::kw_co_return) &&
4775 Right.isNoneOf(tok::semi, tok::r_paren)) {
4779 if (
Left.is(tok::l_paren) ||
Right.is(tok::r_paren)) {
4780 return (
Right.is(TT_CastRParen) ||
4781 (
Left.MatchingParen &&
Left.MatchingParen->is(TT_CastRParen)))
4782 ? Style.SpacesInParensOptions.InCStyleCasts
4783 : Style.SpacesInParensOptions.Other;
4785 if (
Right.isOneOf(tok::semi, tok::comma))
4788 bool IsLightweightGeneric =
Right.MatchingParen &&
4789 Right.MatchingParen->Next &&
4790 Right.MatchingParen->Next->is(tok::colon);
4791 return !IsLightweightGeneric && Style.ObjCSpaceBeforeProtocolList;
4793 if (
Right.is(tok::less) &&
Left.is(tok::kw_template))
4794 return Style.SpaceAfterTemplateKeyword;
4795 if (
Left.isOneOf(tok::exclaim, tok::tilde))
4797 if (
Left.is(tok::at) &&
4798 Right.isOneOf(tok::identifier, tok::string_literal, tok::char_constant,
4799 tok::numeric_constant, tok::l_paren, tok::l_brace,
4800 tok::kw_true, tok::kw_false)) {
4803 if (
Left.is(tok::colon))
4804 return Left.isNoneOf(TT_ObjCSelector, TT_ObjCMethodExpr);
4805 if (
Left.is(tok::coloncolon))
4807 if (
Left.is(tok::less) ||
Right.isOneOf(tok::greater, tok::less)) {
4808 if (Style.isTextProto() ||
4809 (Style.Language == FormatStyle::LK_Proto &&
4810 (
Left.is(TT_DictLiteral) ||
Right.is(TT_DictLiteral)))) {
4812 if (
Left.is(tok::less) &&
Right.is(tok::greater))
4814 return Style.Cpp11BracedListStyle == FormatStyle::BLS_Block;
4817 if (
Right.isNot(TT_OverloadedOperatorLParen))
4820 if (
Right.is(tok::ellipsis)) {
4821 return Left.Tok.isLiteral() || (
Left.is(tok::identifier) && BeforeLeft &&
4822 BeforeLeft->is(tok::kw_case));
4824 if (
Left.is(tok::l_square) &&
Right.is(tok::amp))
4825 return Style.SpacesInSquareBrackets;
4826 if (
Right.is(TT_PointerOrReference)) {
4827 if (
Left.is(tok::r_paren) &&
Line.MightBeFunctionDecl) {
4828 if (!
Left.MatchingParen)
4831 Left.MatchingParen->getPreviousNonComment();
4832 if (!TokenBeforeMatchingParen ||
Left.isNot(TT_TypeDeclarationParen))
4838 if ((Style.SpaceAroundPointerQualifiers == FormatStyle::SAPQ_After ||
4839 Style.SpaceAroundPointerQualifiers == FormatStyle::SAPQ_Both) &&
4840 (
Left.is(TT_AttributeRParen) ||
4841 Left.canBePointerOrReferenceQualifier())) {
4844 if (
Left.Tok.isLiteral())
4847 if (
Left.isTypeOrIdentifier(LangOpts) &&
Right.Next &&
Right.Next->Next &&
4848 Right.Next->Next->is(TT_RangeBasedForLoopColon)) {
4849 return getTokenPointerOrReferenceAlignment(Right) !=
4850 FormatStyle::PAS_Left;
4852 return Left.isNoneOf(TT_PointerOrReference, tok::l_paren) &&
4853 (getTokenPointerOrReferenceAlignment(Right) !=
4854 FormatStyle::PAS_Left ||
4855 (
Line.IsMultiVariableDeclStmt &&
4856 (
Left.NestingLevel == 0 ||
4857 (
Left.NestingLevel == 1 && startsWithInitStatement(
Line)))));
4859 if (
Right.is(TT_FunctionTypeLParen) &&
Left.isNot(tok::l_paren) &&
4860 (
Left.isNot(TT_PointerOrReference) ||
4861 (getTokenPointerOrReferenceAlignment(Left) != FormatStyle::PAS_Right &&
4862 !
Line.IsMultiVariableDeclStmt))) {
4865 if (
Left.is(TT_PointerOrReference)) {
4868 if ((Style.SpaceAroundPointerQualifiers == FormatStyle::SAPQ_Before ||
4869 Style.SpaceAroundPointerQualifiers == FormatStyle::SAPQ_Both) &&
4870 Right.canBePointerOrReferenceQualifier()) {
4874 if (
Right.Tok.isLiteral())
4877 if (
Right.is(TT_BlockComment))
4881 if (
Right.isOneOf(Keywords.kw_override, Keywords.kw_final, tok::kw_noexcept,
4882 TT_RequiresClause) &&
4883 Right.isNot(TT_StartOfName)) {
4890 if (BeforeLeft && BeforeLeft->isTypeOrIdentifier(LangOpts) &&
Right.Next &&
4891 Right.Next->is(TT_RangeBasedForLoopColon)) {
4892 return getTokenPointerOrReferenceAlignment(Left) !=
4893 FormatStyle::PAS_Right;
4895 if (
Right.isOneOf(TT_PointerOrReference, TT_ArraySubscriptLSquare,
4899 if (getTokenPointerOrReferenceAlignment(Left) == FormatStyle::PAS_Right)
4907 if (
Line.IsMultiVariableDeclStmt &&
4908 (
Left.NestingLevel ==
Line.First->NestingLevel ||
4909 ((
Left.NestingLevel ==
Line.First->NestingLevel + 1) &&
4910 startsWithInitStatement(
Line)))) {
4915 if (BeforeLeft->is(tok::coloncolon)) {
4916 if (
Left.isNot(tok::star))
4918 assert(Style.PointerAlignment != FormatStyle::PAS_Right);
4919 if (!
Right.startsSequence(tok::identifier, tok::r_paren))
4922 const auto *LParen =
Right.Next->MatchingParen;
4923 return !LParen || LParen->isNot(TT_FunctionTypeLParen);
4925 return BeforeLeft->isNoneOf(tok::l_paren, tok::l_square);
4928 if (
Left.is(tok::ellipsis) && BeforeLeft &&
4929 BeforeLeft->isPointerOrReference()) {
4930 return Style.PointerAlignment != FormatStyle::PAS_Right;
4933 if (
Right.is(tok::star) &&
Left.is(tok::l_paren))
4935 if (
Left.is(tok::star) &&
Right.isPointerOrReference())
4937 if (
Right.isPointerOrReference()) {
4948 if (
Previous->is(tok::coloncolon)) {
4967 if (
Previous->endsSequence(tok::kw_operator))
4968 return Style.PointerAlignment != FormatStyle::PAS_Left;
4969 if (
Previous->isOneOf(tok::kw_const, tok::kw_volatile)) {
4970 return (Style.PointerAlignment != FormatStyle::PAS_Left) ||
4971 (Style.SpaceAroundPointerQualifiers ==
4972 FormatStyle::SAPQ_After) ||
4973 (Style.SpaceAroundPointerQualifiers == FormatStyle::SAPQ_Both);
4977 if (Style.isCSharp() &&
Left.is(Keywords.kw_is) &&
Right.is(tok::l_square))
4979 const auto SpaceRequiredForArrayInitializerLSquare =
4980 [](
const FormatToken &LSquareTok,
const FormatStyle &Style) {
4981 return Style.SpacesInContainerLiterals ||
4983 Style.Cpp11BracedListStyle == FormatStyle::BLS_Block &&
4984 LSquareTok.endsSequence(tok::l_square, tok::colon,
4987 if (
Left.is(tok::l_square)) {
4988 return (
Left.is(TT_ArrayInitializerLSquare) &&
Right.isNot(tok::r_square) &&
4989 SpaceRequiredForArrayInitializerLSquare(Left, Style)) ||
4990 (
Left.isOneOf(TT_ArraySubscriptLSquare, TT_StructuredBindingLSquare,
4991 TT_LambdaLSquare) &&
4992 Style.SpacesInSquareBrackets &&
Right.isNot(tok::r_square));
4994 if (
Right.is(tok::r_square)) {
4995 return Right.MatchingParen &&
4996 ((
Right.MatchingParen->is(TT_ArrayInitializerLSquare) &&
4997 SpaceRequiredForArrayInitializerLSquare(*
Right.MatchingParen,
4999 (Style.SpacesInSquareBrackets &&
5000 Right.MatchingParen->isOneOf(TT_ArraySubscriptLSquare,
5001 TT_StructuredBindingLSquare,
5002 TT_LambdaLSquare)));
5004 if (
Right.is(tok::l_square) &&
5005 Right.isNoneOf(TT_ObjCMethodExpr, TT_LambdaLSquare,
5006 TT_DesignatedInitializerLSquare,
5007 TT_StructuredBindingLSquare, TT_AttributeLSquare) &&
5008 Left.isNoneOf(tok::numeric_constant, TT_DictLiteral) &&
5009 !(
Left.isNot(tok::r_square) && Style.SpaceBeforeSquareBrackets &&
5010 Right.is(TT_ArraySubscriptLSquare))) {
5014 (
Right.is(tok::r_brace) &&
Right.MatchingParen &&
5016 return Style.Cpp11BracedListStyle == FormatStyle::BLS_Block ||
5017 Style.SpacesInParensOptions.Other;
5019 if (
Left.is(TT_BlockComment)) {
5021 return Style.isJavaScript() || !
Left.TokenText.ends_with(
"=*/");
5026 if (
Left.is(TT_TemplateCloser) &&
Right.is(TT_AttributeLSquare))
5029 if (
Right.is(tok::l_paren)) {
5031 if (
Line.MightBeFunctionDecl &&
Right.is(TT_FunctionDeclarationLParen)) {
5032 if (spaceRequiredBeforeParens(Right))
5034 const auto &Options = Style.SpaceBeforeParensOptions;
5035 return Line.mightBeFunctionDefinition()
5036 ? Options.AfterFunctionDefinitionName
5037 : Options.AfterFunctionDeclarationName;
5039 if (
Left.is(TT_TemplateCloser) &&
Right.isNot(TT_FunctionTypeLParen))
5040 return spaceRequiredBeforeParens(Right);
5041 if (
Left.isOneOf(TT_RequiresClause,
5042 TT_RequiresClauseInARequiresExpression)) {
5043 return Style.SpaceBeforeParensOptions.AfterRequiresInClause ||
5044 spaceRequiredBeforeParens(Right);
5046 if (
Left.is(TT_RequiresExpression)) {
5047 return Style.SpaceBeforeParensOptions.AfterRequiresInExpression ||
5048 spaceRequiredBeforeParens(Right);
5050 if (
Left.isOneOf(TT_AttributeRParen, TT_AttributeRSquare))
5052 if (
Left.is(TT_ForEachMacro)) {
5053 return Style.SpaceBeforeParensOptions.AfterForeachMacros ||
5054 spaceRequiredBeforeParens(Right);
5056 if (
Left.is(TT_IfMacro)) {
5057 return Style.SpaceBeforeParensOptions.AfterIfMacros ||
5058 spaceRequiredBeforeParens(Right);
5060 if (Style.SpaceBeforeParens == FormatStyle::SBPO_Custom &&
5061 Left.isPlacementOperator() &&
5062 Right.isNot(TT_OverloadedOperatorLParen) &&
5063 !(
Line.MightBeFunctionDecl &&
Left.is(TT_FunctionDeclarationName))) {
5064 const auto *RParen =
Right.MatchingParen;
5065 return Style.SpaceBeforeParensOptions.AfterPlacementOperator ||
5066 (RParen && RParen->is(TT_CastRParen));
5070 if (
Left.is(tok::semi))
5072 if (
Left.isOneOf(tok::pp_elif, tok::kw_for, tok::kw_while, tok::kw_switch,
5073 tok::kw_case, TT_ForEachMacro, TT_ObjCForIn) ||
5075 Right.is(TT_ConditionLParen)) {
5076 return Style.SpaceBeforeParensOptions.AfterControlStatements ||
5077 spaceRequiredBeforeParens(Right);
5082 if (
Right.is(TT_OverloadedOperatorLParen))
5083 return spaceRequiredBeforeParens(Right);
5087 Left.MatchingParen &&
Left.MatchingParen->is(TT_LambdaLSquare)) {
5088 return Style.SpaceBeforeParensOptions.AfterFunctionDefinitionName ||
5089 spaceRequiredBeforeParens(Right);
5091 if (!BeforeLeft || BeforeLeft->isNoneOf(tok::period, tok::arrow)) {
5092 if (
Left.isOneOf(tok::kw_try, Keywords.kw___except, tok::kw_catch)) {
5093 return Style.SpaceBeforeParensOptions.AfterControlStatements ||
5094 spaceRequiredBeforeParens(Right);
5096 if (
Left.isPlacementOperator() ||
5097 (
Left.is(tok::r_square) &&
Left.MatchingParen &&
5098 Left.MatchingParen->Previous &&
5099 Left.MatchingParen->Previous->is(tok::kw_delete))) {
5100 return Style.SpaceBeforeParens != FormatStyle::SBPO_Never ||
5101 spaceRequiredBeforeParens(Right);
5105 if (
Tok.isNot(tok::l_paren))
5107 const auto *RParen =
Tok.MatchingParen;
5110 const auto *
Next = RParen->Next;
5117 (
Left.Tok.getIdentifierInfo() ||
Left.is(tok::r_paren))) {
5118 return spaceRequiredBeforeParens(Right);
5122 if (
Left.is(tok::at) &&
Right.isNot(tok::objc_not_keyword))
5124 if (
Right.is(TT_UnaryOperator)) {
5125 return Left.isNoneOf(tok::l_paren, tok::l_square, tok::at) &&
5126 (
Left.isNot(tok::colon) ||
Left.isNot(TT_ObjCMethodExpr));
5132 if (!Style.isVerilog() &&
5133 (
Left.isOneOf(tok::identifier, tok::greater, tok::r_square,
5135 Left.isTypeName(LangOpts)) &&
5136 Right.is(tok::l_brace) &&
Right.getNextNonComment() &&
5140 if (
Left.is(tok::period) ||
Right.is(tok::period))
5144 if (
Right.is(tok::hash) &&
Left.is(tok::identifier) &&
5145 (
Left.TokenText ==
"L" ||
Left.TokenText ==
"u" ||
5146 Left.TokenText ==
"U" ||
Left.TokenText ==
"u8" ||
5147 Left.TokenText ==
"LR" ||
Left.TokenText ==
"uR" ||
5148 Left.TokenText ==
"UR" ||
Left.TokenText ==
"u8R")) {
5151 if (
Left.is(TT_TemplateCloser) &&
Left.MatchingParen &&
5152 Left.MatchingParen->Previous &&
5153 Left.MatchingParen->Previous->isOneOf(tok::period, tok::coloncolon)) {
5159 if (
Left.is(TT_TemplateCloser) &&
Right.is(tok::l_square))
5161 if (
Left.is(tok::l_brace) &&
Left.endsSequence(TT_DictLiteral, tok::at)) {
5165 if (
Right.is(tok::r_brace) &&
Right.MatchingParen &&
5166 Right.MatchingParen->endsSequence(TT_DictLiteral, tok::at)) {
5170 if (
Right.is(TT_TrailingAnnotation) &&
Right.isOneOf(tok::amp, tok::ampamp) &&
5171 Left.isOneOf(tok::kw_const, tok::kw_volatile) &&
5172 (!
Right.Next ||
Right.Next->is(tok::semi))) {
5176 return getTokenReferenceAlignment(Right) != FormatStyle::PAS_Left;
5189 return Right.hasWhitespaceBefore();
5191 const bool IsVerilog = Style.isVerilog();
5192 assert(!IsVerilog || !IsCpp);
5195 if (Keywords.isWordLike(Right, IsVerilog) &&
5196 Keywords.isWordLike(Left, IsVerilog)) {
5202 if (
Left.is(tok::star) &&
Right.is(tok::comment))
5205 if (
Left.is(tok::l_brace) &&
Right.is(tok::r_brace) &&
5206 Left.Children.empty()) {
5208 return Style.SpaceInEmptyBraces != FormatStyle::SIEB_Never;
5209 if (Style.Cpp11BracedListStyle != FormatStyle::BLS_Block) {
5210 return Style.SpacesInParens == FormatStyle::SIPO_Custom &&
5211 Style.SpacesInParensOptions.InEmptyParentheses;
5213 return Style.SpaceInEmptyBraces == FormatStyle::SIEB_Always;
5216 const auto *BeforeLeft =
Left.Previous;
5219 if (
Left.is(TT_OverloadedOperator) &&
5220 Right.isOneOf(TT_TemplateOpener, TT_TemplateCloser)) {
5224 if (
Right.is(tok::period) &&
Left.is(tok::numeric_constant))
5228 if (
Left.is(Keywords.kw_import) &&
5229 Right.isOneOf(tok::less, tok::ellipsis) &&
5230 (!BeforeLeft || BeforeLeft->is(tok::kw_export))) {
5234 if (
Left.isOneOf(Keywords.kw_module, Keywords.kw_import) &&
5235 Right.is(TT_ModulePartitionColon)) {
5239 if (
Right.is(TT_AfterPPDirective))
5243 if (
Left.is(tok::identifier) &&
Right.is(TT_ModulePartitionColon))
5246 if (
Left.is(TT_ModulePartitionColon) &&
5247 Right.isOneOf(tok::identifier, tok::kw_private)) {
5250 if (
Left.is(tok::ellipsis) &&
Right.is(tok::identifier) &&
5251 Line.First->is(Keywords.kw_import)) {
5255 if (
Left.isOneOf(TT_AttributeRParen, TT_AttributeMacro) &&
5256 Right.is(tok::coloncolon)) {
5260 if (
Left.is(tok::kw_operator))
5261 return Right.is(tok::coloncolon) || Style.SpaceAfterOperatorKeyword;
5263 !
Left.opensScope() && Style.SpaceBeforeCpp11BracedList) {
5266 if (
Left.is(tok::less) &&
Left.is(TT_OverloadedOperator) &&
5267 Right.is(TT_TemplateOpener)) {
5271 if (
Left.is(tok::identifier) &&
Right.is(tok::numeric_constant))
5272 return Right.TokenText[0] !=
'.';
5274 if (
Left.Tok.getIdentifierInfo() &&
Right.Tok.isLiteral())
5276 }
else if (Style.isProto()) {
5277 if (
Right.is(tok::period) && !(BeforeLeft && BeforeLeft->is(tok::period)) &&
5278 Left.isOneOf(Keywords.kw_optional, Keywords.kw_required,
5279 Keywords.kw_repeated, Keywords.kw_extend)) {
5282 if (
Right.is(tok::l_paren) &&
5283 Left.isOneOf(Keywords.kw_returns, Keywords.kw_option)) {
5286 if (
Right.isOneOf(tok::l_brace, tok::less) &&
Left.is(TT_SelectorName))
5289 if (
Left.is(tok::slash) ||
Right.is(tok::slash))
5291 if (
Left.MatchingParen &&
5292 Left.MatchingParen->is(TT_ProtoExtensionLSquare) &&
5293 Right.isOneOf(tok::l_brace, tok::less)) {
5294 return Style.Cpp11BracedListStyle == FormatStyle::BLS_Block;
5297 if (
Left.is(tok::percent))
5301 if (
Left.is(tok::numeric_constant) &&
Right.is(tok::percent))
5302 return Right.hasWhitespaceBefore();
5303 }
else if (Style.isJson()) {
5304 if (
Right.is(tok::colon) &&
Left.is(tok::string_literal))
5305 return Style.SpaceBeforeJsonColon;
5306 }
else if (Style.isCSharp()) {
5312 if (
Left.is(tok::kw_this) &&
Right.is(tok::l_square))
5316 if (
Left.is(tok::kw_new) &&
Right.is(tok::l_paren))
5320 if (
Right.is(tok::l_brace))
5324 if (
Left.is(tok::l_brace) &&
Right.isNot(tok::r_brace))
5327 if (
Left.isNot(tok::l_brace) &&
Right.is(tok::r_brace))
5331 if (
Left.is(TT_FatArrow) ||
Right.is(TT_FatArrow))
5335 if (
Left.is(TT_AttributeColon) ||
Right.is(TT_AttributeColon))
5339 if (
Left.is(TT_TemplateCloser) &&
Right.is(TT_StartOfName))
5343 if (
Left.is(tok::l_square) ||
Right.is(tok::r_square))
5344 return Style.SpacesInSquareBrackets;
5347 if (
Right.is(TT_CSharpNullable))
5351 if (
Right.is(TT_NonNullAssertion))
5355 if (
Left.is(tok::comma) &&
Right.is(tok::comma))
5359 if (
Left.is(Keywords.kw_var) &&
Right.is(tok::l_paren))
5363 if (
Right.is(tok::l_paren)) {
5364 if (
Left.isOneOf(tok::kw_using, Keywords.kw_async, Keywords.kw_when,
5365 Keywords.kw_lock)) {
5366 return Style.SpaceBeforeParensOptions.AfterControlStatements ||
5367 spaceRequiredBeforeParens(Right);
5373 if ((
Left.isAccessSpecifierKeyword() ||
5374 Left.isOneOf(tok::kw_virtual, tok::kw_extern, tok::kw_static,
5375 Keywords.kw_internal, Keywords.kw_abstract,
5376 Keywords.kw_sealed, Keywords.kw_override,
5377 Keywords.kw_async, Keywords.kw_unsafe)) &&
5378 Right.is(tok::l_paren)) {
5381 }
else if (Style.isJavaScript()) {
5382 if (
Left.is(TT_FatArrow))
5385 if (
Right.is(tok::l_paren) &&
Left.is(Keywords.kw_await) && BeforeLeft &&
5386 BeforeLeft->is(tok::kw_for)) {
5389 if (
Left.is(Keywords.kw_async) &&
Right.is(tok::l_paren) &&
5390 Right.MatchingParen) {
5397 if ((
Left.is(TT_TemplateString) &&
Left.TokenText.ends_with(
"${")) ||
5398 (
Right.is(TT_TemplateString) &&
Right.TokenText.starts_with(
"}"))) {
5403 if (Keywords.isJavaScriptIdentifier(Left,
5405 Right.is(TT_TemplateString)) {
5408 if (
Right.is(tok::star) &&
5409 Left.isOneOf(Keywords.kw_function, Keywords.kw_yield)) {
5412 if (
Right.isOneOf(tok::l_brace, tok::l_square) &&
5413 Left.isOneOf(Keywords.kw_function, Keywords.kw_yield,
5414 Keywords.kw_extends, Keywords.kw_implements)) {
5417 if (
Right.is(tok::l_paren)) {
5419 if (
Line.MustBeDeclaration &&
Left.Tok.getIdentifierInfo())
5423 if (BeforeLeft && BeforeLeft->is(tok::period) &&
5424 Left.Tok.getIdentifierInfo()) {
5428 if (
Left.isOneOf(tok::kw_throw, Keywords.kw_await, Keywords.kw_typeof,
5434 if (
Left.endsSequence(tok::kw_const, Keywords.kw_as))
5436 if ((
Left.isOneOf(Keywords.kw_let, Keywords.kw_var, Keywords.kw_in,
5441 (
Left.is(Keywords.kw_of) && BeforeLeft &&
5442 BeforeLeft->isOneOf(tok::identifier, tok::r_square, tok::r_brace))) &&
5443 (!BeforeLeft || BeforeLeft->isNot(tok::period))) {
5446 if (
Left.isOneOf(tok::kw_for, Keywords.kw_as) && BeforeLeft &&
5447 BeforeLeft->is(tok::period) &&
Right.is(tok::l_paren)) {
5450 if (
Left.is(Keywords.kw_as) &&
5451 Right.isOneOf(tok::l_square, tok::l_brace, tok::l_paren)) {
5454 if (
Left.is(tok::kw_default) && BeforeLeft &&
5455 BeforeLeft->is(tok::kw_export)) {
5458 if (
Left.is(Keywords.kw_is) &&
Right.is(tok::l_brace))
5460 if (
Right.isOneOf(TT_JsTypeColon, TT_JsTypeOptionalQuestion))
5462 if (
Left.is(TT_JsTypeOperator) ||
Right.is(TT_JsTypeOperator))
5464 if ((
Left.is(tok::l_brace) ||
Right.is(tok::r_brace)) &&
5465 Line.First->isOneOf(Keywords.kw_import, tok::kw_export)) {
5468 if (
Left.is(tok::ellipsis))
5470 if (
Left.is(TT_TemplateCloser) &&
5471 Right.isNoneOf(tok::equal, tok::l_brace, tok::comma, tok::l_square,
5472 Keywords.kw_implements, Keywords.kw_extends)) {
5478 if (
Right.is(TT_NonNullAssertion))
5480 if (
Left.is(TT_NonNullAssertion) &&
5481 Right.isOneOf(Keywords.kw_as, Keywords.kw_in)) {
5484 }
else if (Style.isJava()) {
5485 if (
Left.is(TT_CaseLabelArrow) ||
Right.is(TT_CaseLabelArrow))
5487 if (
Left.is(tok::r_square) &&
Right.is(tok::l_brace))
5490 if (
Left.is(tok::l_square) ||
Right.is(tok::r_square))
5491 return Style.SpacesInSquareBrackets;
5493 if (
Left.is(Keywords.kw_synchronized) &&
Right.is(tok::l_paren)) {
5494 return Style.SpaceBeforeParensOptions.AfterControlStatements ||
5495 spaceRequiredBeforeParens(Right);
5497 if ((
Left.isAccessSpecifierKeyword() ||
5498 Left.isOneOf(tok::kw_static, Keywords.kw_final, Keywords.kw_abstract,
5499 Keywords.kw_native)) &&
5500 Right.is(TT_TemplateOpener)) {
5503 }
else if (IsVerilog) {
5505 if (
Left.is(tok::identifier) &&
Left.TokenText[0] ==
'\\')
5509 if ((
Left.is(TT_VerilogTableItem) &&
5510 Right.isNoneOf(tok::r_paren, tok::semi)) ||
5511 (
Right.is(TT_VerilogTableItem) &&
Left.isNot(tok::l_paren))) {
5513 return !(
Next &&
Next->is(tok::r_paren));
5516 if (
Left.isNot(TT_BinaryOperator) &&
5517 Left.isOneOf(Keywords.kw_verilogHash, Keywords.kw_verilogHashHash)) {
5521 if (
Right.isNot(tok::semi) &&
5522 (
Left.endsSequence(tok::numeric_constant, Keywords.kw_verilogHash) ||
5523 Left.endsSequence(tok::numeric_constant,
5524 Keywords.kw_verilogHashHash) ||
5525 (
Left.is(tok::r_paren) &&
Left.MatchingParen &&
5526 Left.MatchingParen->endsSequence(tok::l_paren, tok::at)))) {
5531 if (
Left.is(Keywords.kw_apostrophe) ||
5532 (
Left.is(TT_VerilogNumberBase) &&
Right.is(tok::numeric_constant))) {
5536 if (
Left.is(tok::arrow) ||
Right.is(tok::arrow))
5541 if (
Left.is(tok::at) &&
Right.isOneOf(tok::l_paren, tok::star, tok::at))
5544 if (
Right.is(tok::l_square) &&
5545 Left.isOneOf(TT_VerilogDimensionedTypeName, Keywords.kw_function)) {
5549 if (
Right.isOneOf(tok::period, Keywords.kw_apostrophe) &&
5550 Keywords.isVerilogIdentifier(Left) &&
Left.getPreviousNonComment() &&
5551 Left.getPreviousNonComment()->is(Keywords.kw_tagged)) {
5557 if ((
Right.is(Keywords.kw_apostrophe) ||
5559 Left.isNoneOf(Keywords.kw_assign, Keywords.kw_unique) &&
5560 !Keywords.isVerilogWordOperator(Left) &&
5561 (
Left.isOneOf(tok::r_square, tok::r_paren, tok::r_brace,
5562 tok::numeric_constant) ||
5563 Keywords.isWordLike(Left))) {
5567 if ((
Right.is(tok::star) &&
Left.is(tok::coloncolon)) ||
5568 (
Left.is(tok::star) &&
Right.is(tok::semi))) {
5572 if (
Left.endsSequence(tok::star, tok::l_paren) &&
Right.is(tok::identifier))
5575 if (
Right.is(tok::l_paren) &&
Right.is(TT_VerilogStrength))
5578 if ((
Left.is(tok::l_brace) &&
5579 Right.isOneOf(tok::lessless, tok::greatergreater)) ||
5580 (
Left.endsSequence(tok::lessless, tok::l_brace) ||
5581 Left.endsSequence(tok::greatergreater, tok::l_brace))) {
5584 }
else if (Style.isTableGen()) {
5586 if (
Left.is(tok::l_square) &&
Right.is(tok::l_brace))
5588 if (
Left.is(tok::r_brace) &&
Right.is(tok::r_square))
5591 if (
Right.isOneOf(TT_TableGenDAGArgListColon,
5592 TT_TableGenDAGArgListColonToAlign) ||
5593 Left.isOneOf(TT_TableGenDAGArgListColon,
5594 TT_TableGenDAGArgListColonToAlign)) {
5597 if (
Right.is(TT_TableGenCondOperatorColon))
5599 if (
Left.isOneOf(TT_TableGenDAGArgOperatorID,
5600 TT_TableGenDAGArgOperatorToBreak) &&
5601 Right.isNot(TT_TableGenDAGArgCloser)) {
5605 if (
Right.isOneOf(tok::l_paren, tok::less) &&
5606 Left.isOneOf(TT_TableGenBangOperator, TT_TableGenCondOperator)) {
5611 if (
Left.is(TT_TableGenTrailingPasteOperator) &&
5612 Right.isOneOf(tok::l_brace, tok::colon)) {
5616 if (
Left.is(tok::hash) ||
Right.is(tok::hash))
5619 if (Keywords.isTableGenDefinition(Left))
5623 if (
Left.is(TT_ImplicitStringLiteral))
5624 return Right.hasWhitespaceBefore();
5626 if (
Left.is(TT_ObjCMethodSpecifier))
5627 return Style.ObjCSpaceAfterMethodDeclarationPrefix;
5628 if (
Left.is(tok::r_paren) &&
Left.isNot(TT_AttributeRParen) &&
5629 canBeObjCSelectorComponent(Right)) {
5637 (
Right.is(tok::equal) ||
Left.is(tok::equal))) {
5641 if (
Right.isOneOf(TT_TrailingReturnArrow, TT_LambdaArrow) ||
5642 Left.isOneOf(TT_TrailingReturnArrow, TT_LambdaArrow)) {
5645 if (
Left.is(tok::comma) &&
Right.isNot(TT_OverloadedOperatorLParen) &&
5648 (
Left.Children.empty() || !
Left.MacroParent)) {
5651 if (
Right.is(tok::comma))
5653 if (
Right.is(TT_ObjCBlockLParen))
5655 if (
Right.is(TT_CtorInitializerColon))
5656 return Style.SpaceBeforeCtorInitializerColon;
5657 if (
Right.is(TT_InheritanceColon) && !Style.SpaceBeforeInheritanceColon)
5659 if (
Right.is(TT_EnumUnderlyingTypeColon) &&
5660 !Style.SpaceBeforeEnumUnderlyingTypeColon) {
5663 if (
Right.is(TT_RangeBasedForLoopColon) &&
5664 !Style.SpaceBeforeRangeBasedForLoopColon) {
5667 if (
Left.is(TT_BitFieldColon)) {
5668 return Style.BitFieldColonSpacing == FormatStyle::BFCS_Both ||
5669 Style.BitFieldColonSpacing == FormatStyle::BFCS_After;
5671 if (
Right.is(tok::colon)) {
5672 if (
Right.is(TT_CaseLabelColon))
5673 return Style.SpaceBeforeCaseColon;
5674 if (
Right.is(TT_GotoLabelColon))
5677 if (!
Right.getNextNonComment())
5679 if (
Right.isOneOf(TT_ObjCSelector, TT_ObjCMethodExpr))
5681 if (
Left.is(tok::question))
5683 if (
Right.is(TT_InlineASMColon) &&
Left.is(tok::coloncolon))
5685 if (
Right.is(TT_DictLiteral))
5686 return Style.SpacesInContainerLiterals;
5687 if (
Right.is(TT_AttributeColon))
5689 if (
Right.is(TT_CSharpNamedArgumentColon))
5691 if (
Right.is(TT_GenericSelectionColon))
5693 if (
Right.is(TT_BitFieldColon)) {
5694 return Style.BitFieldColonSpacing == FormatStyle::BFCS_Both ||
5695 Style.BitFieldColonSpacing == FormatStyle::BFCS_Before;
5700 if ((
Left.isOneOf(tok::minus, tok::minusminus) &&
5701 Right.isOneOf(tok::minus, tok::minusminus)) ||
5702 (
Left.isOneOf(tok::plus, tok::plusplus) &&
5703 Right.isOneOf(tok::plus, tok::plusplus))) {
5706 if (
Left.is(TT_UnaryOperator)) {
5709 if (
Left.is(tok::amp) &&
Right.is(tok::r_square))
5710 return Style.SpacesInSquareBrackets;
5711 if (
Left.isNot(tok::exclaim))
5713 if (
Left.TokenText ==
"!")
5714 return Style.SpaceAfterLogicalNot;
5715 assert(
Left.TokenText ==
"not");
5716 return Right.isOneOf(tok::coloncolon, TT_UnaryOperator) ||
5717 (
Right.is(tok::l_paren) && Style.SpaceBeforeParensOptions.AfterNot);
5722 if (
Left.is(TT_CastRParen)) {
5723 return Style.SpaceAfterCStyleCast ||
5724 Right.isOneOf(TT_BinaryOperator, TT_SelectorName);
5727 auto ShouldAddSpacesInAngles = [
this, &
Right]() {
5728 if (this->Style.SpacesInAngles == FormatStyle::SIAS_Always)
5730 if (this->Style.SpacesInAngles == FormatStyle::SIAS_Leave)
5731 return Right.hasWhitespaceBefore();
5735 if (
Left.is(tok::greater) &&
Right.is(tok::greater)) {
5736 if (Style.isTextProto() ||
5737 (Style.Language == FormatStyle::LK_Proto &&
Left.is(TT_DictLiteral))) {
5738 return Style.Cpp11BracedListStyle == FormatStyle::BLS_Block;
5740 return Right.is(TT_TemplateCloser) &&
Left.is(TT_TemplateCloser) &&
5741 ((Style.Standard < FormatStyle::LS_Cpp11) ||
5742 ShouldAddSpacesInAngles());
5744 if (
Right.isOneOf(tok::arrow, tok::arrowstar, tok::periodstar) ||
5745 Left.isOneOf(tok::arrow, tok::period, tok::arrowstar, tok::periodstar) ||
5746 (
Right.is(tok::period) &&
Right.isNot(TT_DesignatedInitializerPeriod))) {
5749 if (!Style.SpaceBeforeAssignmentOperators &&
Left.isNot(TT_TemplateCloser) &&
5753 if (Style.isJava() &&
Right.is(tok::coloncolon) &&
5754 Left.isOneOf(tok::identifier, tok::kw_this)) {
5757 if (
Right.is(tok::coloncolon) &&
Left.is(tok::identifier)) {
5759 return Left.isPossibleMacro(
true) &&
5760 Right.hasWhitespaceBefore();
5762 if (
Right.is(tok::coloncolon) &&
5763 Left.isNoneOf(tok::l_brace, tok::comment, tok::l_paren)) {
5765 return (
Left.is(TT_TemplateOpener) &&
5766 ((Style.Standard < FormatStyle::LS_Cpp11) ||
5767 ShouldAddSpacesInAngles())) ||
5768 Left.isNoneOf(tok::l_paren, tok::r_paren, tok::l_square,
5769 tok::kw___super, TT_TemplateOpener,
5770 TT_TemplateCloser) ||
5771 (
Left.is(tok::l_paren) && Style.SpacesInParensOptions.Other);
5773 if ((
Left.is(TT_TemplateOpener)) != (
Right.is(TT_TemplateCloser)))
5774 return ShouldAddSpacesInAngles();
5775 if (
Left.is(tok::r_paren) &&
Left.isNot(TT_TypeDeclarationParen) &&
5776 Right.is(TT_PointerOrReference) &&
Right.isOneOf(tok::amp, tok::ampamp)) {
5780 if (
Right.is(TT_StructuredBindingLSquare)) {
5781 return Left.isNoneOf(tok::amp, tok::ampamp) ||
5782 getTokenReferenceAlignment(Left) != FormatStyle::PAS_Right;
5785 if (
Right.Next &&
Right.Next->is(TT_StructuredBindingLSquare) &&
5786 Right.isOneOf(tok::amp, tok::ampamp)) {
5787 return getTokenReferenceAlignment(Right) != FormatStyle::PAS_Left;
5789 if ((
Right.is(TT_BinaryOperator) &&
Left.isNot(tok::l_paren)) ||
5790 (
Left.isOneOf(TT_BinaryOperator, TT_EnumEqual, TT_ConditionalExpr) &&
5791 Right.isNot(tok::r_paren))) {
5794 if (
Right.is(TT_TemplateOpener) &&
Left.is(tok::r_paren) &&
5795 Left.MatchingParen &&
5796 Left.MatchingParen->is(TT_OverloadedOperatorLParen)) {
5799 if (
Right.is(tok::less) &&
Left.isNot(tok::l_paren) &&
5803 if (
Right.is(TT_TrailingUnaryOperator))
5805 if (
Left.is(TT_RegexLiteral))
5807 return spaceRequiredBetween(
Line, Left, Right);
5813 Tok.isNoneOf(TT_ObjCBlockLBrace, TT_LambdaLBrace, TT_DictLiteral);
5818 return Tok.MatchingParen &&
Tok.MatchingParen->Next &&
5819 Tok.MatchingParen->Next->isOneOf(tok::comma, tok::r_paren,
5825 FormatStyle::ShortLambdaStyle ShortLambdaOption) {
5826 return Tok.Children.empty() && ShortLambdaOption != FormatStyle::SLS_None;
5831 Tok.isNoneOf(TT_ObjCBlockLBrace, TT_DictLiteral);
5834bool TokenAnnotator::mustBreakBefore(AnnotatedLine &
Line,
5836 if (Right.NewlinesBefore > 1 && Style.MaxEmptyLinesToKeep > 0 &&
5837 (!Style.RemoveEmptyLinesInUnwrappedLines || &Right ==
Line.First)) {
5843 if (Style.BreakFunctionDeclarationParameters &&
Line.MightBeFunctionDecl &&
5844 !
Line.mightBeFunctionDefinition() && Left.MightBeFunctionDeclParen &&
5845 Left.ParameterCount > 0) {
5849 if (Style.BreakFunctionDefinitionParameters &&
Line.MightBeFunctionDecl &&
5850 Line.mightBeFunctionDefinition() &&
Left.MightBeFunctionDeclParen &&
5851 Left.ParameterCount > 0) {
5857 if (Style.PackParameters.BinPack == FormatStyle::BPPS_AlwaysOnePerLine &&
5858 Line.MightBeFunctionDecl && !
Left.opensScope() &&
5863 const auto *BeforeLeft =
Left.Previous;
5864 const auto *AfterRight =
Right.Next;
5866 if (Style.isCSharp()) {
5867 if (
Left.is(TT_FatArrow) &&
Right.is(tok::l_brace) &&
5868 Style.BraceWrapping.AfterFunction) {
5871 if (
Right.is(TT_CSharpNamedArgumentColon) ||
5872 Left.is(TT_CSharpNamedArgumentColon)) {
5875 if (
Right.is(TT_CSharpGenericTypeConstraint))
5877 if (AfterRight && AfterRight->is(TT_FatArrow) &&
5878 (
Right.is(tok::numeric_constant) ||
5879 (
Right.is(tok::identifier) &&
Right.TokenText ==
"_"))) {
5884 if (
Left.is(TT_AttributeRSquare) &&
5885 (
Right.isAccessSpecifier(
false) ||
5886 Right.is(Keywords.kw_internal))) {
5890 if (
Left.is(TT_AttributeRSquare) &&
Right.is(TT_AttributeLSquare))
5892 }
else if (Style.isJavaScript()) {
5894 if (
Right.is(tok::string_literal) &&
Left.is(tok::plus) && BeforeLeft &&
5895 BeforeLeft->is(tok::string_literal)) {
5898 if (
Left.is(TT_DictLiteral) &&
Left.is(tok::l_brace) &&
Line.Level == 0 &&
5899 BeforeLeft && BeforeLeft->is(tok::equal) &&
5900 Line.First->isOneOf(tok::identifier, Keywords.kw_import, tok::kw_export,
5904 Line.First->isNoneOf(Keywords.kw_var, Keywords.kw_let)) {
5909 if (
Left.is(tok::l_brace) &&
Line.Level == 0 &&
5910 (
Line.startsWith(tok::kw_enum) ||
5911 Line.startsWith(tok::kw_const, tok::kw_enum) ||
5912 Line.startsWith(tok::kw_export, tok::kw_enum) ||
5913 Line.startsWith(tok::kw_export, tok::kw_const, tok::kw_enum))) {
5918 if (
Right.is(tok::r_brace) &&
Left.is(tok::l_brace) && BeforeLeft &&
5919 BeforeLeft->is(TT_FatArrow)) {
5921 switch (Style.AllowShortLambdasOnASingleLine) {
5922 case FormatStyle::SLS_All:
5924 case FormatStyle::SLS_None:
5926 case FormatStyle::SLS_Empty:
5927 return !
Left.Children.empty();
5928 case FormatStyle::SLS_Inline:
5931 return (
Left.NestingLevel == 0 &&
Line.Level == 0) &&
5932 !
Left.Children.empty();
5934 llvm_unreachable(
"Unknown FormatStyle::ShortLambdaStyle enum");
5937 if (
Right.is(tok::r_brace) &&
Left.is(tok::l_brace) &&
5938 !
Left.Children.empty()) {
5940 if (
Left.NestingLevel == 0 &&
Line.Level == 0)
5941 return !Style.AllowShortFunctionsOnASingleLine.Other;
5943 return !Style.AllowShortFunctionsOnASingleLine.Inline;
5945 }
else if (Style.isJava()) {
5946 if (
Right.is(tok::plus) &&
Left.is(tok::string_literal) && AfterRight &&
5947 AfterRight->is(tok::string_literal)) {
5950 }
else if (Style.isVerilog()) {
5952 if (
Left.is(TT_VerilogAssignComma))
5955 if (
Left.is(TT_VerilogTypeComma))
5959 if (Style.VerilogBreakBetweenInstancePorts &&
5960 (
Left.is(TT_VerilogInstancePortComma) ||
5961 (
Left.is(tok::r_paren) && Keywords.isVerilogIdentifier(Right) &&
5962 Left.MatchingParen &&
5963 Left.MatchingParen->is(TT_VerilogInstancePortLParen)))) {
5968 if (!Keywords.isVerilogBegin(Right) && Keywords.isVerilogEndOfLabel(Left))
5970 }
else if (Style.BreakAdjacentStringLiterals &&
5971 (IsCpp || Style.isProto() || Style.isTableGen())) {
5972 if (
Left.isStringLiteral() &&
Right.isStringLiteral())
5977 if (Style.isJson()) {
5981 if (
Left.is(TT_DictLiteral) &&
Left.is(tok::l_brace))
5984 if ((
Left.is(TT_ArrayInitializerLSquare) &&
Left.is(tok::l_square) &&
5985 Right.isNot(tok::r_square)) ||
5986 Left.is(tok::comma)) {
5987 if (
Right.is(tok::l_brace))
5992 if (
Tok->isOneOf(tok::l_brace, tok::l_square))
5994 if (
Tok->isOneOf(tok::r_brace, tok::r_square))
5997 return Style.BreakArrays;
5999 }
else if (Style.isTableGen()) {
6003 if (
Left.is(TT_TableGenCondOperatorComma))
6005 if (
Left.is(TT_TableGenDAGArgOperatorToBreak) &&
6006 Right.isNot(TT_TableGenDAGArgCloser)) {
6009 if (
Left.is(TT_TableGenDAGArgListCommaToBreak))
6011 if (
Right.is(TT_TableGenDAGArgCloser) &&
Right.MatchingParen &&
6012 Right.MatchingParen->is(TT_TableGenDAGArgOpenerToBreak) &&
6013 &Left !=
Right.MatchingParen->Next) {
6015 return Style.TableGenBreakInsideDAGArg == FormatStyle::DAS_BreakAll;
6019 if (
Line.startsWith(tok::kw_asm) &&
Right.is(TT_InlineASMColon) &&
6020 Style.BreakBeforeInlineASMColon == FormatStyle::BBIAS_Always) {
6030 if ((
Left.isOneOf(tok::l_brace, TT_ArrayInitializerLSquare) ||
6031 (Style.isJavaScript() &&
Left.is(tok::l_paren))) &&
6033 BeforeClosingBrace =
Left.MatchingParen->Previous;
6034 }
else if (
Right.MatchingParen &&
6035 (
Right.MatchingParen->isOneOf(tok::l_brace,
6036 TT_ArrayInitializerLSquare) ||
6037 (Style.isJavaScript() &&
6038 Right.MatchingParen->is(tok::l_paren)))) {
6039 BeforeClosingBrace = &
Left;
6041 if (BeforeClosingBrace && (BeforeClosingBrace->is(tok::comma) ||
6042 BeforeClosingBrace->isTrailingComment())) {
6047 if (
Right.is(tok::comment)) {
6049 Right.NewlinesBefore > 0 &&
Right.HasUnescapedNewline;
6051 if (
Left.isTrailingComment())
6053 if (
Left.IsUnterminatedLiteral)
6056 if (BeforeLeft && BeforeLeft->is(tok::lessless) &&
6057 Left.is(tok::string_literal) &&
Right.is(tok::lessless) && AfterRight &&
6058 AfterRight->is(tok::string_literal)) {
6059 return Right.NewlinesBefore > 0;
6062 if (
Right.is(TT_RequiresClause)) {
6063 switch (Style.RequiresClausePosition) {
6064 case FormatStyle::RCPS_OwnLine:
6065 case FormatStyle::RCPS_OwnLineWithBrace:
6066 case FormatStyle::RCPS_WithFollowing:
6073 if (
Left.ClosesTemplateDeclaration &&
Left.MatchingParen &&
6074 Left.MatchingParen->NestingLevel == 0) {
6078 if (
Right.is(tok::kw_concept))
6079 return Style.BreakBeforeConceptDeclarations == FormatStyle::BBCDS_Always;
6080 return Style.BreakTemplateDeclarations == FormatStyle::BTDS_Yes ||
6081 (Style.BreakTemplateDeclarations == FormatStyle::BTDS_Leave &&
6082 Right.NewlinesBefore > 0);
6084 if (
Left.ClosesRequiresClause) {
6085 switch (Style.RequiresClausePosition) {
6086 case FormatStyle::RCPS_OwnLine:
6087 case FormatStyle::RCPS_WithPreceding:
6088 return Right.isNot(tok::semi);
6089 case FormatStyle::RCPS_OwnLineWithBrace:
6090 return Right.isNoneOf(tok::semi, tok::l_brace);
6095 if (Style.PackConstructorInitializers == FormatStyle::PCIS_Never) {
6096 if (Style.BreakConstructorInitializers == FormatStyle::BCIS_BeforeColon &&
6097 (
Left.is(TT_CtorInitializerComma) ||
6098 Right.is(TT_CtorInitializerColon))) {
6102 if (Style.BreakConstructorInitializers == FormatStyle::BCIS_AfterColon &&
6103 Left.isOneOf(TT_CtorInitializerColon, TT_CtorInitializerComma)) {
6107 if (Style.BreakConstructorInitializers == FormatStyle::BCIS_AfterComma &&
6108 Left.is(TT_CtorInitializerComma)) {
6112 if (Style.PackConstructorInitializers < FormatStyle::PCIS_CurrentLine &&
6113 Style.BreakConstructorInitializers == FormatStyle::BCIS_BeforeComma &&
6114 Right.isOneOf(TT_CtorInitializerComma, TT_CtorInitializerColon)) {
6117 if (Style.PackConstructorInitializers == FormatStyle::PCIS_NextLineOnly) {
6118 if ((Style.BreakConstructorInitializers == FormatStyle::BCIS_BeforeColon ||
6119 Style.BreakConstructorInitializers == FormatStyle::BCIS_BeforeComma) &&
6120 Right.is(TT_CtorInitializerColon)) {
6124 if (Style.BreakConstructorInitializers == FormatStyle::BCIS_AfterColon &&
6125 Left.is(TT_CtorInitializerColon)) {
6130 if (Style.BreakInheritanceList == FormatStyle::BILS_BeforeComma &&
6131 Right.is(TT_InheritanceComma)) {
6134 if (Style.BreakInheritanceList == FormatStyle::BILS_AfterComma &&
6135 Left.is(TT_InheritanceComma)) {
6138 if (
Right.is(tok::string_literal) &&
Right.TokenText.starts_with(
"R\"")) {
6142 return Right.IsMultiline &&
Right.NewlinesBefore > 0;
6144 if ((
Left.is(tok::l_brace) ||
6145 (
Left.is(tok::less) && BeforeLeft && BeforeLeft->is(tok::equal))) &&
6146 Right.NestingLevel == 1 && Style.Language == FormatStyle::LK_Proto) {
6151 if (
Right.is(TT_InlineASMBrace))
6152 return Right.HasUnescapedNewline;
6155 auto *FirstNonComment =
Line.getFirstNonComment();
6157 FirstNonComment && (FirstNonComment->is(Keywords.kw_internal) ||
6158 FirstNonComment->isAccessSpecifierKeyword());
6160 if (Style.BraceWrapping.AfterEnum) {
6161 if (
Line.startsWith(tok::kw_enum) ||
6162 Line.startsWith(tok::kw_typedef, tok::kw_enum) ||
6163 Line.startsWith(tok::kw_export, tok::kw_enum)) {
6168 FirstNonComment->Next->is(tok::kw_enum)) {
6174 if (Style.BraceWrapping.AfterClass &&
6176 FirstNonComment->Next->is(Keywords.kw_interface)) ||
6177 Line.startsWith(Keywords.kw_interface))) {
6182 if (
Right.isNot(TT_FunctionLBrace)) {
6183 return Style.AllowShortRecordOnASingleLine == FormatStyle::SRS_Never &&
6184 ((
Line.startsWith(tok::kw_class) &&
6185 Style.BraceWrapping.AfterClass) ||
6186 (
Line.startsWith(tok::kw_struct) &&
6187 Style.BraceWrapping.AfterStruct) ||
6188 (
Line.startsWith(tok::kw_union) &&
6189 Style.BraceWrapping.AfterUnion));
6193 if (
Left.is(TT_ObjCBlockLBrace) &&
6194 Style.AllowShortBlocksOnASingleLine == FormatStyle::SBS_Never) {
6199 if (
Left.isOneOf(TT_AttributeRParen, TT_AttributeMacro) &&
6200 Right.is(TT_ObjCDecl)) {
6204 if (
Left.is(TT_LambdaLBrace)) {
6206 Style.AllowShortLambdasOnASingleLine == FormatStyle::SLS_Inline) {
6210 if (Style.AllowShortLambdasOnASingleLine == FormatStyle::SLS_None ||
6211 Style.AllowShortLambdasOnASingleLine == FormatStyle::SLS_Inline ||
6212 (!
Left.Children.empty() &&
6213 Style.AllowShortLambdasOnASingleLine == FormatStyle::SLS_Empty)) {
6218 if (Style.BraceWrapping.BeforeLambdaBody &&
Right.is(TT_LambdaLBrace) &&
6219 (
Left.isPointerOrReference() ||
Left.is(TT_TemplateCloser))) {
6224 if ((Style.isJava() || Style.isJavaScript()) &&
6225 Left.is(TT_LeadingJavaAnnotation) &&
6226 Right.isNoneOf(TT_LeadingJavaAnnotation, tok::l_paren) &&
6227 (
Line.Last->is(tok::l_brace) || Style.BreakAfterJavaFieldAnnotations)) {
6231 if (
Right.is(TT_ProtoExtensionLSquare))
6261 if (Style.isProto() &&
Right.is(TT_SelectorName) &&
6262 Right.isNot(tok::r_square) && AfterRight) {
6265 if (
Left.is(tok::at))
6271 const auto *LBrace = AfterRight;
6272 if (LBrace && LBrace->is(tok::colon)) {
6273 LBrace = LBrace->Next;
6274 if (LBrace && LBrace->is(tok::at)) {
6275 LBrace = LBrace->Next;
6277 LBrace = LBrace->Next;
6289 ((LBrace->is(tok::l_brace) &&
6290 (LBrace->is(TT_DictLiteral) ||
6291 (LBrace->Next && LBrace->Next->is(tok::r_brace)))) ||
6292 LBrace->isOneOf(TT_ArrayInitializerLSquare, tok::less))) {
6299 if (
Left.ParameterCount == 0)
6314 if (
Left.isOneOf(tok::r_brace, tok::greater, tok::r_square))
6318 if (Style.BreakAfterAttributes == FormatStyle::ABS_LeaveAll &&
6319 Left.is(TT_AttributeRSquare) &&
Right.NewlinesBefore > 0) {
6320 Line.ReturnTypeWrapped =
true;
6331 if (Style.isCSharp()) {
6332 if (
Left.isOneOf(TT_CSharpNamedArgumentColon, TT_AttributeColon) ||
6333 Right.isOneOf(TT_CSharpNamedArgumentColon, TT_AttributeColon)) {
6337 if (
Line.First->is(TT_CSharpGenericTypeConstraint))
6338 return Left.is(TT_CSharpGenericTypeConstraintComma);
6340 if (
Right.is(TT_CSharpNullable))
6342 }
else if (Style.isJava()) {
6343 if (
Left.isOneOf(Keywords.kw_throws, Keywords.kw_extends,
6344 Keywords.kw_implements)) {
6347 if (
Right.isOneOf(Keywords.kw_throws, Keywords.kw_extends,
6348 Keywords.kw_implements)) {
6351 }
else if (Style.isJavaScript()) {
6354 (NonComment->isAccessSpecifierKeyword() ||
6355 NonComment->isOneOf(
6356 tok::kw_return, Keywords.kw_yield, tok::kw_continue, tok::kw_break,
6357 tok::kw_throw, Keywords.kw_interface, Keywords.kw_type,
6358 tok::kw_static, Keywords.kw_readonly, Keywords.kw_override,
6359 Keywords.kw_abstract, Keywords.kw_get, Keywords.kw_set,
6360 Keywords.kw_async, Keywords.kw_await))) {
6363 if (
Right.NestingLevel == 0 &&
6364 (
Left.Tok.getIdentifierInfo() ||
6365 Left.isOneOf(tok::r_square, tok::r_paren)) &&
6366 Right.isOneOf(tok::l_square, tok::l_paren)) {
6369 if (NonComment && NonComment->is(tok::identifier) &&
6370 NonComment->TokenText ==
"asserts") {
6373 if (
Left.is(TT_FatArrow) &&
Right.is(tok::l_brace))
6375 if (
Left.is(TT_JsTypeColon))
6378 if (
Left.is(tok::exclaim) &&
Right.is(tok::colon))
6383 if (
Right.is(Keywords.kw_is)) {
6392 if (!
Next ||
Next->isNot(tok::colon))
6395 if (
Left.is(Keywords.kw_in))
6396 return Style.BreakBeforeBinaryOperators == FormatStyle::BOS_None;
6397 if (
Right.is(Keywords.kw_in))
6398 return Style.BreakBeforeBinaryOperators != FormatStyle::BOS_None;
6399 if (
Right.is(Keywords.kw_as))
6401 if (
Right.isOneOf(Keywords.kw_extends, Keywords.kw_infer)) {
6407 if (
Left.is(Keywords.kw_as))
6409 if (
Left.is(TT_NonNullAssertion))
6411 if (
Left.is(Keywords.kw_declare) &&
6412 Right.isOneOf(Keywords.kw_module, tok::kw_namespace,
6413 Keywords.kw_function, tok::kw_class, tok::kw_enum,
6414 Keywords.kw_interface, Keywords.kw_type, Keywords.kw_var,
6415 Keywords.kw_let, tok::kw_const)) {
6420 if (
Left.isOneOf(Keywords.kw_module, tok::kw_namespace) &&
6421 Right.isOneOf(tok::identifier, tok::string_literal)) {
6424 if (
Right.is(TT_TemplateString) &&
Right.closesScope())
6428 if (
Left.is(tok::identifier) &&
Right.is(TT_TemplateString))
6430 if (
Left.is(TT_TemplateString) &&
Left.opensScope())
6432 }
else if (Style.isTableGen()) {
6434 if (Keywords.isTableGenDefinition(Left))
6437 if (
Right.is(tok::l_paren)) {
6438 return Left.isNoneOf(TT_TableGenBangOperator, TT_TableGenCondOperator,
6442 if (
Left.is(TT_TableGenValueSuffix))
6445 if (
Left.is(tok::hash) ||
Right.is(tok::hash))
6447 if (
Left.isOneOf(TT_TableGenBangOperator, TT_TableGenCondOperator))
6454 if (
Right.is(tok::r_brace)) {
6456 (
Right.isBlockIndentedInitRBrace(Style)));
6461 if (
Right.is(tok::r_paren)) {
6462 if (!
Right.MatchingParen)
6465 if (
Next &&
Next->is(tok::r_paren))
6467 if (
Next &&
Next->is(tok::l_paren))
6473 return Style.BreakBeforeCloseBracketIf;
6475 return Style.BreakBeforeCloseBracketLoop;
6477 return Style.BreakBeforeCloseBracketSwitch;
6478 return Style.BreakBeforeCloseBracketFunction;
6481 if (
Left.isOneOf(tok::r_paren, TT_TrailingAnnotation) &&
6482 Right.is(TT_TrailingAnnotation) &&
6483 Style.BreakBeforeCloseBracketFunction) {
6487 if (
Right.is(TT_TemplateCloser))
6488 return Style.BreakBeforeTemplateCloser;
6490 if (
Left.isOneOf(tok::at, tok::objc_interface))
6492 if (
Left.isOneOf(TT_JavaAnnotation, TT_LeadingJavaAnnotation))
6493 return Right.isNot(tok::l_paren);
6494 if (
Right.is(TT_PointerOrReference)) {
6495 return Line.IsMultiVariableDeclStmt ||
6496 (getTokenPointerOrReferenceAlignment(Right) ==
6497 FormatStyle::PAS_Right &&
6499 Right.Next->isOneOf(TT_FunctionDeclarationName, tok::kw_const)));
6501 if (
Right.isOneOf(TT_StartOfName, TT_FunctionDeclarationName,
6502 TT_ClassHeadName, TT_QtProperty, tok::kw_operator)) {
6505 if (
Left.is(TT_PointerOrReference))
6507 if (
Right.isTrailingComment()) {
6514 (
Left.is(TT_CtorInitializerColon) &&
Right.NewlinesBefore > 0 &&
6515 Style.BreakConstructorInitializers == FormatStyle::BCIS_AfterColon);
6517 if (
Left.is(tok::question) &&
Right.is(tok::colon))
6519 if (
Right.isOneOf(TT_ConditionalExpr, tok::question))
6520 return Style.BreakBeforeTernaryOperators;
6521 if (
Left.isOneOf(TT_ConditionalExpr, tok::question))
6522 return !Style.BreakBeforeTernaryOperators;
6523 if (
Left.is(TT_InheritanceColon))
6524 return Style.BreakInheritanceList == FormatStyle::BILS_AfterColon;
6525 if (
Right.is(TT_InheritanceColon))
6526 return Style.BreakInheritanceList != FormatStyle::BILS_AfterColon;
6528 if (
Right.is(TT_ObjCMethodExpr) &&
Right.isNot(tok::r_square) &&
6529 Left.isNot(TT_SelectorName)) {
6533 if (
Right.is(tok::colon) &&
6534 Right.isNoneOf(TT_CtorInitializerColon, TT_InlineASMColon,
6535 TT_BitFieldColon)) {
6538 if (
Left.is(tok::colon) &&
Left.isOneOf(TT_ObjCSelector, TT_ObjCMethodExpr))
6540 if (
Left.is(tok::colon) &&
Left.is(TT_DictLiteral)) {
6541 if (Style.isProto()) {
6542 if (!Style.AlwaysBreakBeforeMultilineStrings &&
Right.isStringLiteral())
6568 if ((
Right.isOneOf(tok::l_brace, tok::less) &&
6569 Right.is(TT_DictLiteral)) ||
6570 Right.is(TT_ArrayInitializerLSquare)) {
6576 if (
Right.is(tok::r_square) &&
Right.MatchingParen &&
6577 Right.MatchingParen->is(TT_ProtoExtensionLSquare)) {
6580 if (
Right.is(TT_SelectorName) || (
Right.is(tok::identifier) &&
Right.Next &&
6581 Right.Next->is(TT_ObjCMethodExpr))) {
6582 return Left.isNot(tok::period);
6586 if (
Right.is(tok::kw_concept))
6587 return Style.BreakBeforeConceptDeclarations != FormatStyle::BBCDS_Never;
6588 if (
Right.is(TT_RequiresClause))
6590 if (
Left.ClosesTemplateDeclaration) {
6591 return Style.BreakTemplateDeclarations != FormatStyle::BTDS_Leave ||
6592 Right.NewlinesBefore > 0;
6594 if (
Left.is(TT_FunctionAnnotationRParen))
6596 if (
Left.ClosesRequiresClause)
6598 if (
Right.isOneOf(TT_RangeBasedForLoopColon, TT_OverloadedOperatorLParen,
6599 TT_OverloadedOperator)) {
6602 if (
Left.is(TT_RangeBasedForLoopColon))
6604 if (
Right.is(TT_RangeBasedForLoopColon))
6606 if (
Left.is(TT_TemplateCloser) &&
Right.is(TT_TemplateOpener))
6608 if ((
Left.is(tok::greater) &&
Right.is(tok::greater)) ||
6609 (
Left.is(tok::less) &&
Right.is(tok::less))) {
6612 if (
Right.is(TT_BinaryOperator) &&
6613 Style.BreakBeforeBinaryOperators != FormatStyle::BOS_None &&
6614 (Style.BreakBeforeBinaryOperators == FormatStyle::BOS_All ||
6618 if (
Left.isOneOf(TT_TemplateCloser, TT_UnaryOperator, tok::kw_operator))
6620 if (
Left.is(tok::equal) &&
Right.isNoneOf(tok::kw_default, tok::kw_delete) &&
6624 if (
Left.is(tok::equal) &&
Right.is(tok::l_brace) &&
6625 Style.Cpp11BracedListStyle == FormatStyle::BLS_Block) {
6628 if (
Left.is(TT_AttributeLParen) ||
6629 (
Left.is(tok::l_paren) &&
Left.is(TT_TypeDeclarationParen))) {
6632 if (
Left.is(tok::l_paren) &&
Left.Previous &&
6633 (
Left.Previous->isOneOf(TT_BinaryOperator, TT_CastRParen))) {
6636 if (
Right.is(TT_ImplicitStringLiteral))
6639 if (
Right.is(tok::r_square) &&
Right.MatchingParen &&
6640 Right.MatchingParen->is(TT_LambdaLSquare)) {
6646 if (
Left.is(TT_TrailingAnnotation)) {
6647 return Right.isNoneOf(tok::l_brace, tok::semi, tok::equal, tok::l_paren,
6648 tok::less, tok::coloncolon);
6651 if (
Right.isAttribute())
6654 if (
Right.is(TT_AttributeLSquare)) {
6655 assert(
Left.isNot(tok::l_square));
6659 if (
Left.is(tok::identifier) &&
Right.is(tok::string_literal))
6662 if (
Right.is(tok::identifier) &&
Right.Next &&
Right.Next->is(TT_DictLiteral))
6665 if (
Left.is(TT_CtorInitializerColon)) {
6666 return (Style.BreakConstructorInitializers ==
6667 FormatStyle::BCIS_AfterColon ||
6668 Style.BreakConstructorInitializers ==
6669 FormatStyle::BCIS_AfterComma) &&
6670 (!
Right.isTrailingComment() ||
Right.NewlinesBefore > 0);
6672 if (
Right.is(TT_CtorInitializerColon)) {
6673 return Style.BreakConstructorInitializers != FormatStyle::BCIS_AfterColon &&
6674 Style.BreakConstructorInitializers != FormatStyle::BCIS_AfterComma;
6676 if (
Left.is(TT_CtorInitializerComma) &&
6677 Style.BreakConstructorInitializers == FormatStyle::BCIS_BeforeComma) {
6680 if (
Right.is(TT_CtorInitializerComma) &&
6681 Style.BreakConstructorInitializers == FormatStyle::BCIS_BeforeComma) {
6684 if (
Left.is(TT_InheritanceComma) &&
6685 Style.BreakInheritanceList == FormatStyle::BILS_BeforeComma) {
6688 if (
Right.is(TT_InheritanceComma) &&
6689 Style.BreakInheritanceList == FormatStyle::BILS_BeforeComma) {
6692 if (
Left.is(TT_ArrayInitializerLSquare))
6694 if (
Right.is(tok::kw_typename) &&
Left.isNot(tok::kw_const))
6696 if ((
Left.isBinaryOperator() ||
Left.is(TT_BinaryOperator)) &&
6697 Left.isNoneOf(tok::arrowstar, tok::lessless) &&
6698 Style.BreakBeforeBinaryOperators != FormatStyle::BOS_All &&
6699 (Style.BreakBeforeBinaryOperators == FormatStyle::BOS_None ||
6703 if (
Left.is(TT_AttributeLSquare) &&
Right.is(tok::l_square)) {
6704 assert(
Right.isNot(TT_AttributeLSquare));
6707 if (
Left.is(tok::r_square) &&
Right.is(TT_AttributeRSquare)) {
6708 assert(
Left.isNot(TT_AttributeRSquare));
6712 auto ShortLambdaOption = Style.AllowShortLambdasOnASingleLine;
6713 if (Style.BraceWrapping.BeforeLambdaBody &&
Right.is(TT_LambdaLBrace)) {
6720 if (
Right.is(tok::kw_noexcept) &&
Right.is(TT_TrailingAnnotation)) {
6721 switch (Style.AllowBreakBeforeNoexceptSpecifier) {
6722 case FormatStyle::BBNSS_Never:
6724 case FormatStyle::BBNSS_Always:
6726 case FormatStyle::BBNSS_OnlyWithParen:
6727 return Right.Next &&
Right.Next->is(tok::l_paren);
6731 return Left.isOneOf(tok::comma, tok::coloncolon, tok::semi, tok::l_brace,
6732 tok::kw_class, tok::kw_struct, tok::comment) ||
6733 Right.isMemberAccess() ||
6734 Right.isOneOf(TT_TrailingReturnArrow, TT_LambdaArrow, tok::lessless,
6735 tok::colon, tok::l_square, tok::at) ||
6736 (
Left.is(tok::r_paren) &&
6737 Right.isOneOf(tok::identifier, tok::kw_const)) ||
6738 (
Left.is(tok::l_paren) &&
Right.isNot(tok::r_paren)) ||
6739 (
Left.is(TT_TemplateOpener) &&
Right.isNot(TT_TemplateCloser));
6743 llvm::errs() <<
"AnnotatedTokens(L=" <<
Line.Level <<
", P=" <<
Line.PPLevel
6744 <<
", T=" <<
Line.Type <<
", C=" <<
Line.IsContinuation
6748 llvm::errs() <<
" I=" <<
Tok->IndentLevel <<
" M=" <<
Tok->MustBreakBefore
6749 <<
" C=" <<
Tok->CanBreakBefore
6751 <<
" S=" <<
Tok->SpacesRequiredBefore
6752 <<
" F=" <<
Tok->Finalized <<
" B=" <<
Tok->BlockParameterCount
6753 <<
" BK=" <<
Tok->getBlockKind() <<
" P=" <<
Tok->SplitPenalty
6754 <<
" Name=" <<
Tok->Tok.getName() <<
" N=" <<
Tok->NestingLevel
6755 <<
" L=" <<
Tok->TotalLength
6756 <<
" PPK=" <<
Tok->getPackingKind() <<
" FakeLParens=";
6758 llvm::errs() << LParen <<
"/";
6759 llvm::errs() <<
" FakeRParens=" <<
Tok->FakeRParens;
6760 llvm::errs() <<
" II=" <<
Tok->Tok.getIdentifierInfo();
6761 llvm::errs() <<
" Text='" <<
Tok->TokenText <<
"'\n";
6766 llvm::errs() <<
"----\n";
6769FormatStyle::PointerAlignmentStyle
6771 assert(
Reference.isOneOf(tok::amp, tok::ampamp));
6772 switch (Style.ReferenceAlignment) {
6773 case FormatStyle::RAS_Pointer:
6774 return Style.PointerAlignment;
6775 case FormatStyle::RAS_Left:
6776 return FormatStyle::PAS_Left;
6777 case FormatStyle::RAS_Right:
6778 return FormatStyle::PAS_Right;
6779 case FormatStyle::RAS_Middle:
6780 return FormatStyle::PAS_Middle;
6783 return Style.PointerAlignment;
6786FormatStyle::PointerAlignmentStyle
6787TokenAnnotator::getTokenPointerOrReferenceAlignment(
6789 if (PointerOrReference.isOneOf(tok::amp, tok::ampamp))
6790 return getTokenReferenceAlignment(PointerOrReference);
6791 assert(PointerOrReference.is(tok::star));
6792 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.