18#include "llvm/ADT/SmallPtrSet.h"
19#include "llvm/Support/Debug.h"
21#define DEBUG_TYPE "format-token-annotator"
27 const FormatStyle &Style) {
28 switch (Style.BreakAfterAttributes) {
29 case FormatStyle::ABS_Always:
31 case FormatStyle::ABS_Never:
34 return Tok.NewlinesBefore > 0;
42static bool startsWithInitStatement(
const AnnotatedLine &
Line) {
43 return Line.startsWith(tok::kw_for) ||
Line.startsWith(tok::kw_if) ||
44 Line.startsWith(tok::kw_switch);
59 return Tok.Tok.getIdentifierInfo();
65static bool isLambdaParameterList(
const FormatToken *Left) {
67 if (
Left->Previous &&
Left->Previous->is(tok::greater) &&
68 Left->Previous->MatchingParen &&
69 Left->Previous->MatchingParen->is(TT_TemplateOpener)) {
70 Left =
Left->Previous->MatchingParen;
74 return Left->Previous &&
Left->Previous->is(tok::r_square) &&
75 Left->Previous->MatchingParen &&
76 Left->Previous->MatchingParen->is(TT_LambdaLSquare);
82 return Tok.isOneOf(tok::kw_if, tok::kw_for, tok::kw_while, tok::kw_switch,
83 tok::kw_constexpr, tok::kw_catch);
88 if (!IsCpp || !
Tok.startsSequence(tok::l_square, tok::l_square))
91 if (
Tok.Previous &&
Tok.Previous->is(tok::at))
98 if (AttrTok->startsSequence(tok::kw_using, tok::identifier, tok::colon))
100 if (AttrTok->isNot(tok::identifier))
102 while (AttrTok && !AttrTok->startsSequence(tok::r_square, tok::r_square)) {
106 if (AttrTok->is(tok::colon) ||
107 AttrTok->startsSequence(tok::identifier, tok::identifier) ||
108 AttrTok->startsSequence(tok::r_paren, tok::identifier)) {
111 if (AttrTok->is(tok::ellipsis))
113 AttrTok = AttrTok->Next;
115 return AttrTok && AttrTok->startsSequence(tok::r_square, tok::r_square);
123class AnnotatingParser {
125 AnnotatingParser(
const FormatStyle &Style, AnnotatedLine &Line,
126 const AdditionalKeywords &Keywords,
127 SmallVector<ScopeType> &Scopes)
128 : Style(Style), Line(Line), CurrentToken(Line.
First), AutoFound(
false),
130 Keywords(Keywords), Scopes(Scopes), TemplateDeclarationDepth(0) {
131 Contexts.push_back(Context(tok::unknown, 1,
false));
132 resetTokenMetadata();
137 switch (Token.getType()) {
139 case TT_StructLBrace:
144 case TT_CompoundRequirementLBrace:
155 auto *
Left = CurrentToken->Previous;
159 if (NonTemplateLess.count(Left) > 0)
162 const auto *BeforeLess =
Left->Previous;
165 if (BeforeLess->Tok.isLiteral())
167 if (BeforeLess->is(tok::r_brace))
169 if (BeforeLess->is(tok::r_paren) && Contexts.size() > 1 &&
170 !(BeforeLess->MatchingParen &&
171 BeforeLess->MatchingParen->is(TT_OverloadedOperatorLParen))) {
174 if (BeforeLess->is(tok::kw_operator) && CurrentToken->is(tok::l_paren))
178 Left->ParentBracket = Contexts.back().ContextKind;
179 ScopedContextCreator ContextCreator(*
this, tok::less, 12);
180 Contexts.back().IsExpression =
false;
184 if (BeforeLess && BeforeLess->isNot(tok::kw_template))
185 Contexts.back().ContextType = Context::TemplateArgument;
187 if (Style.isJava() && CurrentToken->is(tok::question))
190 for (
bool SeenTernaryOperator =
false, MaybeAngles =
true; CurrentToken;) {
191 const auto &ParentContext = Contexts[Contexts.size() - 2];
192 const bool InExpr = ParentContext.IsExpression;
193 if (CurrentToken->is(tok::greater)) {
194 const auto *
Next = CurrentToken->Next;
195 if (CurrentToken->isNot(TT_TemplateCloser)) {
202 if (
Next &&
Next->is(tok::greater) &&
203 Left->ParentBracket != tok::less &&
204 CurrentToken->getStartOfNonWhitespace() ==
205 Next->getStartOfNonWhitespace().getLocWithOffset(-1)) {
208 if (InExpr && SeenTernaryOperator &&
209 (!
Next ||
Next->isNoneOf(tok::l_paren, tok::l_brace))) {
214 if (ParentContext.InStaticAssertFirstArgument &&
Next &&
215 Next->isOneOf(tok::minus, tok::identifier)) {
219 Left->MatchingParen = CurrentToken;
220 CurrentToken->MatchingParen =
Left;
226 if (Style.isTextProto() ||
227 (Style.Language == FormatStyle::LK_Proto && BeforeLess &&
228 BeforeLess->isOneOf(TT_SelectorName, TT_DictLiteral))) {
229 CurrentToken->setType(TT_DictLiteral);
231 CurrentToken->setType(TT_TemplateCloser);
232 CurrentToken->Tok.setLength(1);
239 if (BeforeLess && BeforeLess->is(TT_TemplateName)) {
243 if (CurrentToken->is(tok::question) && Style.isJava()) {
247 if (CurrentToken->isOneOf(tok::r_paren, tok::r_square, tok::r_brace))
249 const auto &Prev = *CurrentToken->Previous;
256 if (MaybeAngles && InExpr && !Line.startsWith(tok::kw_template) &&
257 Prev.is(TT_BinaryOperator) &&
258 Prev.isOneOf(tok::pipepipe, tok::ampamp)) {
261 if (Prev.isOneOf(tok::question, tok::colon) && !Style.isProto())
262 SeenTernaryOperator =
true;
263 updateParameterCount(Left, CurrentToken);
264 if (Style.Language == FormatStyle::LK_Proto) {
266 if (CurrentToken->is(tok::colon) ||
267 (CurrentToken->isOneOf(tok::l_brace, tok::less) &&
272 }
else if (Style.isTableGen()) {
273 if (CurrentToken->isOneOf(tok::comma, tok::equal)) {
280 if (!parseTableGenValue())
290 bool parseUntouchableParens() {
291 while (CurrentToken) {
292 CurrentToken->Finalized =
true;
293 switch (CurrentToken->Tok.getKind()) {
296 if (!parseUntouchableParens())
311 bool parseParens(
bool IsIf =
false) {
314 assert(CurrentToken->Previous &&
"Unknown previous token");
315 FormatToken &OpeningParen = *CurrentToken->Previous;
316 assert(OpeningParen.is(tok::l_paren));
317 FormatToken *PrevNonComment = OpeningParen.getPreviousNonComment();
318 OpeningParen.ParentBracket = Contexts.back().ContextKind;
319 ScopedContextCreator ContextCreator(*
this, tok::l_paren, 1);
322 Contexts.back().ColonIsForRangeExpr =
323 Contexts.size() == 2 && Contexts[0].ColonIsForRangeExpr;
325 if (OpeningParen.Previous &&
326 OpeningParen.Previous->is(TT_UntouchableMacroFunc)) {
327 OpeningParen.Finalized =
true;
328 return parseUntouchableParens();
331 bool StartsObjCSelector =
false;
332 if (!Style.isVerilog()) {
333 if (
FormatToken *MaybeSel = OpeningParen.Previous) {
335 if (MaybeSel->is(tok::objc_selector) && MaybeSel->Previous &&
336 MaybeSel->Previous->is(tok::at)) {
337 StartsObjCSelector =
true;
342 if (OpeningParen.is(TT_OverloadedOperatorLParen)) {
345 while (Prev->isNot(tok::kw_operator)) {
346 Prev = Prev->Previous;
347 assert(Prev &&
"Expect a kw_operator prior to the OperatorLParen!");
353 bool OperatorCalledAsMemberFunction =
354 Prev->Previous && Prev->Previous->isOneOf(tok::period, tok::arrow);
355 Contexts.back().IsExpression = OperatorCalledAsMemberFunction;
356 }
else if (OpeningParen.is(TT_VerilogInstancePortLParen)) {
357 Contexts.back().IsExpression =
true;
358 Contexts.back().ContextType = Context::VerilogInstancePortList;
359 }
else if (Style.isJavaScript() &&
360 (Line.startsWith(Keywords.kw_type, tok::identifier) ||
361 Line.startsWith(tok::kw_export, Keywords.kw_type,
365 Contexts.back().IsExpression =
false;
366 }
else if (OpeningParen.Previous &&
367 (OpeningParen.Previous->isOneOf(
368 tok::kw_noexcept, tok::kw_explicit, tok::kw_while,
369 tok::l_paren, tok::comma, TT_CastRParen,
370 TT_BinaryOperator) ||
371 OpeningParen.Previous->isIf())) {
373 Contexts.back().IsExpression =
true;
374 }
else if (Style.isJavaScript() && OpeningParen.Previous &&
375 (OpeningParen.Previous->is(Keywords.kw_function) ||
376 (OpeningParen.Previous->endsSequence(tok::identifier,
377 Keywords.kw_function)))) {
379 Contexts.back().IsExpression =
false;
380 }
else if (Style.isJavaScript() && OpeningParen.Previous &&
381 OpeningParen.Previous->is(TT_JsTypeColon)) {
383 Contexts.back().IsExpression =
false;
384 }
else if (isLambdaParameterList(&OpeningParen)) {
386 OpeningParen.setType(TT_LambdaDefinitionLParen);
387 Contexts.back().IsExpression =
false;
388 }
else if (OpeningParen.is(TT_RequiresExpressionLParen)) {
389 Contexts.back().IsExpression =
false;
390 }
else if (OpeningParen.Previous &&
391 OpeningParen.Previous->is(tok::kw__Generic)) {
392 Contexts.back().ContextType = Context::C11GenericSelection;
393 Contexts.back().IsExpression =
true;
394 }
else if (OpeningParen.Previous &&
395 OpeningParen.Previous->TokenText ==
"Q_PROPERTY") {
396 Contexts.back().ContextType = Context::QtProperty;
397 Contexts.back().IsExpression =
false;
398 }
else if (Line.InPPDirective &&
399 (!OpeningParen.Previous ||
400 OpeningParen.Previous->isNot(tok::identifier))) {
401 Contexts.back().IsExpression =
true;
402 }
else if (Contexts[Contexts.size() - 2].CaretFound) {
404 Contexts.back().IsExpression =
false;
405 }
else if (OpeningParen.Previous &&
406 OpeningParen.Previous->is(TT_ForEachMacro)) {
408 Contexts.back().ContextType = Context::ForEachMacro;
409 Contexts.back().IsExpression =
false;
410 }
else if (OpeningParen.Previous && OpeningParen.Previous->MatchingParen &&
411 OpeningParen.Previous->MatchingParen->isOneOf(
412 TT_ObjCBlockLParen, TT_FunctionTypeLParen)) {
413 Contexts.back().IsExpression =
false;
414 }
else if (!Line.MustBeDeclaration &&
415 (!Line.InPPDirective || (Line.InMacroBody && !Scopes.empty()))) {
417 OpeningParen.Previous &&
418 OpeningParen.Previous->isOneOf(tok::kw_for, tok::kw_catch);
419 Contexts.back().IsExpression = !IsForOrCatch;
422 if (Style.isTableGen()) {
424 if (Prev->is(TT_TableGenCondOperator)) {
425 Contexts.back().IsTableGenCondOpe =
true;
426 Contexts.back().IsExpression =
true;
427 }
else if (Contexts.size() > 1 &&
428 Contexts[Contexts.size() - 2].IsTableGenBangOpe) {
433 Contexts.back().IsTableGenBangOpe =
true;
434 Contexts.back().IsExpression =
true;
437 if (!parseTableGenDAGArg())
439 return parseTableGenDAGArgAndList(&OpeningParen);
446 if (PrevNonComment && OpeningParen.is(TT_Unknown)) {
447 if (PrevNonComment->isAttribute()) {
448 OpeningParen.setType(TT_AttributeLParen);
449 }
else if (PrevNonComment->isOneOf(TT_TypenameMacro, tok::kw_decltype,
452#include
"clang/Basic/TransformTypeTraits.def"
454 OpeningParen.setType(TT_TypeDeclarationParen);
456 if (PrevNonComment->isOneOf(tok::kw_decltype, tok::kw_typeof))
457 Contexts.back().IsExpression =
true;
461 if (StartsObjCSelector)
462 OpeningParen.setType(TT_ObjCSelector);
464 const bool IsStaticAssert =
465 PrevNonComment && PrevNonComment->is(tok::kw_static_assert);
467 Contexts.back().InStaticAssertFirstArgument =
true;
477 bool MightBeFunctionType = !Contexts[Contexts.size() - 2].IsExpression;
478 bool ProbablyFunctionType =
479 CurrentToken->isPointerOrReference() || CurrentToken->is(tok::caret);
480 bool HasMultipleLines =
false;
481 bool HasMultipleParametersOnALine =
false;
482 bool MightBeObjCForRangeLoop =
483 OpeningParen.Previous && OpeningParen.Previous->is(tok::kw_for);
485 while (CurrentToken) {
486 const auto &Prev = *CurrentToken->Previous;
487 const auto *PrevPrev = Prev.Previous;
488 if (Prev.is(TT_PointerOrReference) &&
489 PrevPrev->isOneOf(tok::l_paren, tok::coloncolon)) {
490 ProbablyFunctionType =
true;
492 if (CurrentToken->is(tok::comma))
493 MightBeFunctionType =
false;
494 if (Prev.is(TT_BinaryOperator))
495 Contexts.back().IsExpression =
true;
496 if (CurrentToken->is(tok::r_paren)) {
497 if (Prev.is(TT_PointerOrReference) &&
498 (PrevPrev == &OpeningParen || PrevPrev->is(tok::coloncolon))) {
499 MightBeFunctionType =
true;
501 if (OpeningParen.isNot(TT_CppCastLParen) && MightBeFunctionType &&
502 ProbablyFunctionType && CurrentToken->Next &&
503 (CurrentToken->Next->is(tok::l_paren) ||
504 (CurrentToken->Next->is(tok::l_square) &&
505 (Line.MustBeDeclaration ||
506 (PrevNonComment && PrevNonComment->isTypeName(LangOpts)))))) {
507 OpeningParen.setType(OpeningParen.Next->is(tok::caret)
509 : TT_FunctionTypeLParen);
511 OpeningParen.MatchingParen = CurrentToken;
512 CurrentToken->MatchingParen = &OpeningParen;
514 if (CurrentToken->Next && CurrentToken->Next->is(tok::l_brace) &&
515 OpeningParen.Previous && OpeningParen.Previous->is(tok::l_paren)) {
521 if (
Tok->is(TT_BinaryOperator) &&
Tok->isPointerOrReference())
522 Tok->setType(TT_PointerOrReference);
526 if (StartsObjCSelector) {
527 CurrentToken->setType(TT_ObjCSelector);
528 if (Contexts.back().FirstObjCSelectorName) {
529 Contexts.back().FirstObjCSelectorName->LongestObjCSelectorName =
530 Contexts.back().LongestObjCSelectorName;
534 if (OpeningParen.is(TT_AttributeLParen))
535 CurrentToken->setType(TT_AttributeRParen);
536 if (OpeningParen.is(TT_TypeDeclarationParen))
537 CurrentToken->setType(TT_TypeDeclarationParen);
538 if (OpeningParen.Previous &&
539 OpeningParen.Previous->is(TT_JavaAnnotation)) {
540 CurrentToken->setType(TT_JavaAnnotation);
542 if (OpeningParen.Previous &&
543 OpeningParen.Previous->is(TT_LeadingJavaAnnotation)) {
544 CurrentToken->setType(TT_LeadingJavaAnnotation);
547 if (!HasMultipleLines)
549 else if (HasMultipleParametersOnALine)
557 if (CurrentToken->isOneOf(tok::r_square, tok::r_brace))
560 if (CurrentToken->is(tok::l_brace) && OpeningParen.is(TT_ObjCBlockLParen))
561 OpeningParen.setType(TT_Unknown);
562 if (CurrentToken->is(tok::comma) && CurrentToken->Next &&
563 !CurrentToken->Next->HasUnescapedNewline &&
564 !CurrentToken->Next->isTrailingComment()) {
565 HasMultipleParametersOnALine =
true;
567 bool ProbablyFunctionTypeLParen =
568 (CurrentToken->is(tok::l_paren) && CurrentToken->Next &&
569 CurrentToken->Next->isOneOf(tok::star, tok::amp, tok::caret));
570 if ((Prev.isOneOf(tok::kw_const, tok::kw_auto) ||
571 Prev.isTypeName(LangOpts)) &&
572 !(CurrentToken->is(tok::l_brace) ||
573 (CurrentToken->is(tok::l_paren) && !ProbablyFunctionTypeLParen))) {
574 Contexts.back().IsExpression =
false;
576 if (CurrentToken->isOneOf(tok::semi, tok::colon)) {
577 MightBeObjCForRangeLoop =
false;
578 if (PossibleObjCForInToken) {
579 PossibleObjCForInToken->setType(TT_Unknown);
580 PossibleObjCForInToken =
nullptr;
583 if (IsIf && CurrentToken->is(tok::semi)) {
584 for (
auto *
Tok = OpeningParen.Next;
585 Tok != CurrentToken &&
586 Tok->isNoneOf(tok::equal, tok::l_paren, tok::l_brace);
588 if (
Tok->isPointerOrReference())
589 Tok->setFinalizedType(TT_PointerOrReference);
592 if (MightBeObjCForRangeLoop && CurrentToken->is(Keywords.kw_in)) {
593 PossibleObjCForInToken = CurrentToken;
594 PossibleObjCForInToken->setType(TT_ObjCForIn);
598 if (CurrentToken->is(tok::comma)) {
600 Contexts.back().InStaticAssertFirstArgument =
false;
602 Contexts.back().CanBeExpression =
true;
605 if (Style.isTableGen()) {
606 if (CurrentToken->is(tok::comma)) {
607 if (Contexts.back().IsTableGenCondOpe)
608 CurrentToken->setType(TT_TableGenCondOperatorComma);
610 }
else if (CurrentToken->is(tok::colon)) {
611 if (Contexts.back().IsTableGenCondOpe)
612 CurrentToken->setType(TT_TableGenCondOperatorColon);
616 if (!parseTableGenValue())
624 updateParameterCount(&OpeningParen,
Tok);
625 if (CurrentToken && CurrentToken->HasUnescapedNewline)
626 HasMultipleLines =
true;
632 if (!Style.isCSharp())
636 if (
Tok.Previous &&
Tok.Previous->is(tok::identifier))
640 if (
Tok.Previous &&
Tok.Previous->is(tok::r_square)) {
651 if (AttrTok->is(tok::r_square))
655 while (AttrTok && AttrTok->isNot(tok::r_square))
656 AttrTok = AttrTok->Next;
662 AttrTok = AttrTok->Next;
667 if (AttrTok->isAccessSpecifierKeyword() ||
668 AttrTok->isOneOf(tok::comment, tok::kw_class, tok::kw_static,
669 tok::l_square, Keywords.kw_internal)) {
675 AttrTok->Next->startsSequence(tok::identifier, tok::l_paren)) {
691 Left->ParentBracket = Contexts.back().ContextKind;
697 bool CppArrayTemplates =
698 IsCpp && Parent && Parent->is(TT_TemplateCloser) &&
699 (Contexts.back().CanBeExpression || Contexts.back().IsExpression ||
700 Contexts.back().ContextType == Context::TemplateArgument);
702 const bool IsInnerSquare = Contexts.back().InCpp11AttributeSpecifier;
703 const bool IsCpp11AttributeSpecifier =
704 isCppAttribute(IsCpp, *Left) || IsInnerSquare;
707 bool IsCSharpAttributeSpecifier =
708 isCSharpAttributeSpecifier(*Left) ||
709 Contexts.back().InCSharpAttributeSpecifier;
711 bool InsideInlineASM = Line.startsWith(tok::kw_asm);
712 bool IsCppStructuredBinding =
Left->isCppStructuredBinding(IsCpp);
713 bool StartsObjCMethodExpr =
714 !IsCppStructuredBinding && !InsideInlineASM && !CppArrayTemplates &&
715 IsCpp && !IsCpp11AttributeSpecifier && !IsCSharpAttributeSpecifier &&
716 Contexts.back().CanBeExpression &&
Left->isNot(TT_LambdaLSquare) &&
717 CurrentToken->isNoneOf(tok::l_brace, tok::r_square) &&
721 (!Parent || !Parent->is(tok::comma) ||
722 Contexts.back().ContextKind != tok::l_brace) &&
724 Parent->isOneOf(tok::colon, tok::l_square, tok::l_paren,
725 tok::kw_return, tok::kw_throw) ||
726 Parent->isUnaryOperator() ||
728 Parent->isOneOf(TT_ObjCForIn, TT_CastRParen) ||
731 bool ColonFound =
false;
733 unsigned BindingIncrease = 1;
734 if (IsCppStructuredBinding) {
735 Left->setType(TT_StructuredBindingLSquare);
736 }
else if (
Left->is(TT_Unknown)) {
737 if (StartsObjCMethodExpr) {
738 Left->setType(TT_ObjCMethodExpr);
739 }
else if (InsideInlineASM) {
740 Left->setType(TT_InlineASMSymbolicNameLSquare);
741 }
else if (IsCpp11AttributeSpecifier) {
742 if (!IsInnerSquare) {
743 Left->setType(TT_AttributeLSquare);
745 Left->Previous->EndsCppAttributeGroup =
false;
747 }
else if (Style.isJavaScript() && Parent &&
748 Contexts.back().ContextKind == tok::l_brace &&
749 Parent->isOneOf(tok::l_brace, tok::comma)) {
750 Left->setType(TT_JsComputedPropertyName);
751 }
else if (IsCpp && Contexts.back().ContextKind == tok::l_brace &&
752 Parent && Parent->isOneOf(tok::l_brace, tok::comma)) {
753 Left->setType(TT_DesignatedInitializerLSquare);
754 }
else if (IsCSharpAttributeSpecifier) {
755 Left->setType(TT_AttributeLSquare);
756 }
else if (CurrentToken->is(tok::r_square) && Parent &&
757 Parent->is(TT_TemplateCloser)) {
758 Left->setType(TT_ArraySubscriptLSquare);
759 }
else if (Style.isProto()) {
786 Left->setType(TT_ArrayInitializerLSquare);
787 if (!
Left->endsSequence(tok::l_square, tok::numeric_constant,
789 !
Left->endsSequence(tok::l_square, tok::numeric_constant,
791 !
Left->endsSequence(tok::l_square, tok::colon, TT_SelectorName)) {
792 Left->setType(TT_ProtoExtensionLSquare);
793 BindingIncrease = 10;
795 }
else if (!CppArrayTemplates && Parent &&
796 Parent->isOneOf(TT_BinaryOperator, TT_TemplateCloser, tok::at,
797 tok::comma, tok::l_paren, tok::l_square,
798 tok::question, tok::colon, tok::kw_return,
801 Left->setType(TT_ArrayInitializerLSquare);
803 BindingIncrease = 10;
804 Left->setType(TT_ArraySubscriptLSquare);
808 ScopedContextCreator ContextCreator(*
this, tok::l_square, BindingIncrease);
809 Contexts.back().IsExpression =
true;
810 if (Style.isJavaScript() && Parent && Parent->is(TT_JsTypeColon))
811 Contexts.back().IsExpression =
false;
813 Contexts.back().ColonIsObjCMethodExpr = StartsObjCMethodExpr;
814 Contexts.back().InCpp11AttributeSpecifier = IsCpp11AttributeSpecifier;
815 Contexts.back().InCSharpAttributeSpecifier = IsCSharpAttributeSpecifier;
817 while (CurrentToken) {
818 if (CurrentToken->is(tok::r_square)) {
819 if (IsCpp11AttributeSpecifier && !IsInnerSquare) {
820 CurrentToken->setType(TT_AttributeRSquare);
821 CurrentToken->EndsCppAttributeGroup =
true;
823 if (IsCSharpAttributeSpecifier) {
824 CurrentToken->setType(TT_AttributeRSquare);
825 }
else if (((CurrentToken->Next &&
826 CurrentToken->Next->is(tok::l_paren)) ||
827 (CurrentToken->Previous &&
828 CurrentToken->Previous->Previous == Left)) &&
829 Left->is(TT_ObjCMethodExpr)) {
834 StartsObjCMethodExpr =
false;
835 Left->setType(TT_Unknown);
837 if (StartsObjCMethodExpr && CurrentToken->Previous != Left) {
838 CurrentToken->setType(TT_ObjCMethodExpr);
841 if (!ColonFound && CurrentToken->Previous &&
842 CurrentToken->Previous->is(TT_Unknown) &&
843 canBeObjCSelectorComponent(*CurrentToken->Previous)) {
844 CurrentToken->Previous->setType(TT_SelectorName);
849 if (Parent && Parent->is(TT_PointerOrReference))
850 Parent->overwriteFixedType(TT_BinaryOperator);
852 Left->MatchingParen = CurrentToken;
853 CurrentToken->MatchingParen =
Left;
858 if (!Contexts.back().FirstObjCSelectorName) {
861 Previous->ObjCSelectorNameParts = 1;
862 Contexts.back().FirstObjCSelectorName =
Previous;
865 Left->ParameterCount =
866 Contexts.back().FirstObjCSelectorName->ObjCSelectorNameParts;
868 if (Contexts.back().FirstObjCSelectorName) {
869 Contexts.back().FirstObjCSelectorName->LongestObjCSelectorName =
870 Contexts.back().LongestObjCSelectorName;
871 if (
Left->BlockParameterCount > 1)
872 Contexts.back().FirstObjCSelectorName->LongestObjCSelectorName = 0;
874 if (Style.isTableGen() &&
Left->is(TT_TableGenListOpener))
875 CurrentToken->setType(TT_TableGenListCloser);
879 if (CurrentToken->isOneOf(tok::r_paren, tok::r_brace))
881 if (CurrentToken->is(tok::colon)) {
882 if (IsCpp11AttributeSpecifier &&
883 CurrentToken->endsSequence(tok::colon, tok::identifier,
887 CurrentToken->setType(TT_AttributeColon);
888 }
else if (!Style.isVerilog() && !Line.InPragmaDirective &&
889 Left->isOneOf(TT_ArraySubscriptLSquare,
890 TT_DesignatedInitializerLSquare)) {
891 Left->setType(TT_ObjCMethodExpr);
892 StartsObjCMethodExpr =
true;
893 Contexts.back().ColonIsObjCMethodExpr =
true;
894 if (Parent && Parent->is(tok::r_paren)) {
896 Parent->setType(TT_CastRParen);
901 if (CurrentToken->is(tok::comma) &&
Left->is(TT_ObjCMethodExpr) &&
903 Left->setType(TT_ArrayInitializerLSquare);
906 if (Style.isTableGen()) {
907 if (CurrentToken->isOneOf(tok::comma, tok::minus, tok::ellipsis)) {
913 if (!parseTableGenValue())
916 updateParameterCount(Left,
Tok);
921 updateParameterCount(Left,
Tok);
926 void skipToNextNonComment() {
928 while (CurrentToken && CurrentToken->is(tok::comment))
937 bool parseTableGenValue(
bool ParseNameMode =
false) {
940 while (CurrentToken->is(tok::comment))
942 if (!parseTableGenSimpleValue())
947 if (CurrentToken->is(tok::hash)) {
948 if (CurrentToken->Next &&
949 CurrentToken->Next->isOneOf(tok::colon, tok::semi, tok::l_brace)) {
952 CurrentToken->setType(TT_TableGenTrailingPasteOperator);
957 skipToNextNonComment();
958 HashTok->setType(TT_Unknown);
959 if (!parseTableGenValue(ParseNameMode))
966 if (ParseNameMode && CurrentToken->is(tok::l_brace))
969 if (CurrentToken->isOneOf(tok::l_brace, tok::l_square, tok::period)) {
970 CurrentToken->setType(TT_TableGenValueSuffix);
972 skipToNextNonComment();
973 if (Suffix->is(tok::l_square))
974 return parseSquare();
975 if (Suffix->is(tok::l_brace)) {
976 Scopes.push_back(getScopeType(*Suffix));
986 bool tryToParseTableGenTokVar() {
989 if (CurrentToken->is(tok::identifier) &&
990 CurrentToken->TokenText.front() ==
'$') {
991 skipToNextNonComment();
999 bool parseTableGenDAGArg(
bool AlignColon =
false) {
1000 if (tryToParseTableGenTokVar())
1002 if (parseTableGenValue()) {
1003 if (CurrentToken && CurrentToken->is(tok::colon)) {
1005 CurrentToken->setType(TT_TableGenDAGArgListColonToAlign);
1007 CurrentToken->setType(TT_TableGenDAGArgListColon);
1008 skipToNextNonComment();
1009 return tryToParseTableGenTokVar();
1020 auto &Opes = Style.TableGenBreakingDAGArgOperators;
1025 if (
Tok.isNot(tok::identifier) ||
1026 Tok.isOneOf(TT_TableGenBangOperator, TT_TableGenCondOperator)) {
1030 if (!
Tok.Next ||
Tok.Next->is(tok::colon))
1032 return llvm::is_contained(Opes,
Tok.TokenText.str());
1037 bool parseTableGenDAGArgAndList(
FormatToken *Opener) {
1039 if (!parseTableGenDAGArg())
1041 bool BreakInside =
false;
1042 if (Style.TableGenBreakInsideDAGArg != FormatStyle::DAS_DontBreak) {
1045 if (isTableGenDAGArgBreakingOperator(*FirstTok)) {
1048 Opener->setType(TT_TableGenDAGArgOpenerToBreak);
1049 if (FirstTok->isOneOf(TT_TableGenBangOperator,
1050 TT_TableGenCondOperator)) {
1053 CurrentToken->Previous->setType(TT_TableGenDAGArgOperatorToBreak);
1054 }
else if (FirstTok->is(tok::identifier)) {
1055 if (Style.TableGenBreakInsideDAGArg == FormatStyle::DAS_BreakAll)
1056 FirstTok->setType(TT_TableGenDAGArgOperatorToBreak);
1058 FirstTok->setType(TT_TableGenDAGArgOperatorID);
1063 return parseTableGenDAGArgList(Opener, BreakInside);
1068 bool parseTableGenDAGArgList(
FormatToken *Opener,
bool BreakInside) {
1069 ScopedContextCreator ContextCreator(*
this, tok::l_paren, 0);
1070 Contexts.back().IsTableGenDAGArgList =
true;
1071 bool FirstDAGArgListElm =
true;
1072 while (CurrentToken) {
1073 if (!FirstDAGArgListElm && CurrentToken->is(tok::comma)) {
1074 CurrentToken->setType(BreakInside ? TT_TableGenDAGArgListCommaToBreak
1075 : TT_TableGenDAGArgListComma);
1076 skipToNextNonComment();
1078 if (CurrentToken && CurrentToken->is(tok::r_paren)) {
1079 CurrentToken->setType(TT_TableGenDAGArgCloser);
1080 Opener->MatchingParen = CurrentToken;
1081 CurrentToken->MatchingParen = Opener;
1082 skipToNextNonComment();
1085 if (!parseTableGenDAGArg(
1087 Style.AlignConsecutiveTableGenBreakingDAGArgColons.Enabled)) {
1090 FirstDAGArgListElm =
false;
1095 bool parseTableGenSimpleValue() {
1096 assert(Style.isTableGen());
1100 skipToNextNonComment();
1102 if (
Tok->isOneOf(tok::numeric_constant, tok::string_literal,
1103 TT_TableGenMultiLineString, tok::kw_true, tok::kw_false,
1104 tok::question, tok::kw_int)) {
1108 if (
Tok->is(tok::l_brace)) {
1109 Scopes.push_back(getScopeType(*
Tok));
1110 return parseBrace();
1113 if (
Tok->is(tok::l_square)) {
1114 Tok->setType(TT_TableGenListOpener);
1117 if (
Tok->is(tok::less)) {
1118 CurrentToken->setType(TT_TemplateOpener);
1119 return parseAngle();
1125 if (
Tok->is(tok::l_paren)) {
1126 Tok->setType(TT_TableGenDAGArgOpener);
1128 if (Contexts.back().IsTableGenDAGArgList)
1129 Tok->SpacesRequiredBefore = 1;
1130 return parseTableGenDAGArgAndList(
Tok);
1133 if (
Tok->is(TT_TableGenBangOperator)) {
1134 if (CurrentToken && CurrentToken->is(tok::less)) {
1135 CurrentToken->setType(TT_TemplateOpener);
1136 skipToNextNonComment();
1140 if (!CurrentToken || CurrentToken->isNot(tok::l_paren))
1144 Contexts.back().IsTableGenBangOpe =
true;
1145 bool Result = parseParens();
1146 Contexts.back().IsTableGenBangOpe =
false;
1150 if (
Tok->is(TT_TableGenCondOperator)) {
1151 if (!CurrentToken || CurrentToken->isNot(tok::l_paren))
1154 return parseParens();
1159 if (
Tok->is(tok::identifier)) {
1161 if (CurrentToken && CurrentToken->is(tok::less)) {
1162 CurrentToken->setType(TT_TemplateOpener);
1163 skipToNextNonComment();
1164 return parseAngle();
1172 bool couldBeInStructArrayInitializer()
const {
1173 if (Contexts.size() < 2)
1177 const auto End = std::next(Contexts.rbegin(), 2);
1178 auto Last = Contexts.rbegin();
1181 if (
Last->ContextKind == tok::l_brace)
1183 return Depth == 2 &&
Last->ContextKind != tok::l_brace;
1190 assert(CurrentToken->Previous);
1191 FormatToken &OpeningBrace = *CurrentToken->Previous;
1192 assert(OpeningBrace.is(tok::l_brace));
1193 OpeningBrace.ParentBracket = Contexts.back().ContextKind;
1195 if (Contexts.back().CaretFound)
1196 OpeningBrace.overwriteFixedType(TT_ObjCBlockLBrace);
1197 Contexts.back().CaretFound =
false;
1199 ScopedContextCreator ContextCreator(*
this, tok::l_brace, 1);
1200 Contexts.back().ColonIsDictLiteral =
true;
1202 Contexts.back().IsExpression =
true;
1203 if (Style.isJavaScript() && OpeningBrace.Previous &&
1204 OpeningBrace.Previous->is(TT_JsTypeColon)) {
1205 Contexts.back().IsExpression =
false;
1207 if (Style.isVerilog() &&
1208 (!OpeningBrace.getPreviousNonComment() ||
1209 OpeningBrace.getPreviousNonComment()->isNot(Keywords.kw_apostrophe))) {
1210 Contexts.back().VerilogMayBeConcatenation =
true;
1212 if (Style.isTableGen())
1213 Contexts.back().ColonIsDictLiteral =
false;
1215 unsigned CommaCount = 0;
1216 while (CurrentToken) {
1217 assert(!Scopes.empty());
1218 if (CurrentToken->is(tok::r_brace)) {
1219 assert(Scopes.back() == getScopeType(OpeningBrace));
1221 assert(OpeningBrace.Optional == CurrentToken->Optional);
1222 OpeningBrace.MatchingParen = CurrentToken;
1223 CurrentToken->MatchingParen = &OpeningBrace;
1224 if (Style.AlignArrayOfStructures != FormatStyle::AIAS_None) {
1225 if (OpeningBrace.ParentBracket == tok::l_brace &&
1226 couldBeInStructArrayInitializer() && CommaCount > 0) {
1227 Contexts.back().ContextType = Context::StructArrayInitializer;
1233 if (CurrentToken->isOneOf(tok::r_paren, tok::r_square))
1235 updateParameterCount(&OpeningBrace, CurrentToken);
1236 if (CurrentToken->isOneOf(tok::colon, tok::l_brace, tok::less)) {
1238 if (
Previous->is(TT_JsTypeOptionalQuestion))
1240 if ((CurrentToken->is(tok::colon) && !Style.isTableGen() &&
1241 (!Contexts.back().ColonIsDictLiteral || !IsCpp)) ||
1243 OpeningBrace.setType(TT_DictLiteral);
1244 Scopes.back() = getScopeType(OpeningBrace);
1245 if (
Previous->Tok.getIdentifierInfo() ||
1246 Previous->is(tok::string_literal)) {
1247 Previous->setType(TT_SelectorName);
1250 if (CurrentToken->is(tok::colon) && OpeningBrace.is(TT_Unknown) &&
1251 !Style.isTableGen()) {
1252 OpeningBrace.setType(TT_DictLiteral);
1253 Scopes.back() = getScopeType(OpeningBrace);
1254 }
else if (Style.isJavaScript()) {
1255 OpeningBrace.overwriteFixedType(TT_DictLiteral);
1256 Scopes.back() = getScopeType(OpeningBrace);
1259 bool IsBracedListComma =
false;
1260 if (CurrentToken->is(tok::comma)) {
1261 if (Style.isJavaScript()) {
1262 OpeningBrace.overwriteFixedType(TT_DictLiteral);
1263 Scopes.back() = getScopeType(OpeningBrace);
1269 if (!consumeToken())
1271 if (IsBracedListComma)
1272 Contexts.back().IsExpression =
true;
1281 if (Current->is(tok::l_brace) && Current->is(
BK_Block))
1282 ++
Left->BlockParameterCount;
1283 if (Current->is(tok::comma)) {
1284 ++
Left->ParameterCount;
1286 Left->Role.reset(
new CommaSeparatedList(Style));
1287 Left->Role->CommaFound(Current);
1288 }
else if (
Left->ParameterCount == 0 && Current->isNot(tok::comment)) {
1289 Left->ParameterCount = 1;
1293 bool parseConditional() {
1294 while (CurrentToken) {
1295 if (CurrentToken->is(tok::colon) && CurrentToken->is(TT_Unknown)) {
1296 CurrentToken->setType(TT_ConditionalExpr);
1303 if (CurrentToken->is(tok::r_brace))
1305 if (!consumeToken())
1311 bool parseTemplateDeclaration() {
1312 if (!CurrentToken || CurrentToken->isNot(tok::less))
1315 CurrentToken->setType(TT_TemplateOpener);
1318 TemplateDeclarationDepth++;
1319 const bool WellFormed = parseAngle();
1320 TemplateDeclarationDepth--;
1324 if (CurrentToken && TemplateDeclarationDepth == 0)
1325 CurrentToken->Previous->ClosesTemplateDeclaration =
true;
1330 bool consumeToken() {
1332 const auto *Prev = CurrentToken->getPreviousNonComment();
1333 if (Prev && Prev->is(TT_AttributeRSquare) &&
1334 CurrentToken->isOneOf(tok::kw_if, tok::kw_switch, tok::kw_case,
1335 tok::kw_default, tok::kw_for, tok::kw_while) &&
1337 CurrentToken->MustBreakBefore =
true;
1344 if (
Tok->is(TT_VerilogTableItem))
1347 if (
Tok->is(TT_TableGenMultiLineString))
1349 auto *Prev =
Tok->getPreviousNonComment();
1350 auto *
Next =
Tok->getNextNonComment();
1351 switch (
bool IsIf =
false;
Tok->Tok.getKind()) {
1354 if (!Prev && Line.MustBeDeclaration)
1355 Tok->setType(TT_ObjCMethodSpecifier);
1362 if (
Tok->isTypeFinalized())
1365 if (Style.isJavaScript()) {
1366 if (Contexts.back().ColonIsForRangeExpr ||
1367 (Contexts.size() == 1 &&
1368 Line.First->isNoneOf(tok::kw_enum, tok::kw_case)) ||
1369 Contexts.back().ContextKind == tok::l_paren ||
1370 Contexts.back().ContextKind == tok::l_square ||
1371 (!Contexts.back().IsExpression &&
1372 Contexts.back().ContextKind == tok::l_brace) ||
1373 (Contexts.size() == 1 &&
1374 Line.MustBeDeclaration)) {
1375 Contexts.back().IsExpression =
false;
1376 Tok->setType(TT_JsTypeColon);
1379 }
else if (Style.isCSharp()) {
1380 if (Contexts.back().InCSharpAttributeSpecifier) {
1381 Tok->setType(TT_AttributeColon);
1384 if (Contexts.back().ContextKind == tok::l_paren) {
1385 Tok->setType(TT_CSharpNamedArgumentColon);
1388 }
else if (Style.isVerilog() &&
Tok->isNot(TT_BinaryOperator)) {
1391 if (Keywords.isVerilogEnd(*Prev) || Keywords.isVerilogBegin(*Prev)) {
1392 Tok->setType(TT_VerilogBlockLabelColon);
1393 }
else if (Contexts.back().ContextKind == tok::l_square) {
1394 Tok->setType(TT_BitFieldColon);
1395 }
else if (Contexts.back().ColonIsDictLiteral) {
1396 Tok->setType(TT_DictLiteral);
1397 }
else if (Contexts.size() == 1) {
1401 Tok->setType(TT_CaseLabelColon);
1402 if (Line.Level > 1 || (!Line.InPPDirective && Line.Level > 0))
1407 if (Line.First->is(tok::kw_asm)) {
1408 Tok->setType(TT_InlineASMColon);
1409 }
else if (Contexts.back().ColonIsDictLiteral || Style.isProto()) {
1410 Tok->setType(TT_DictLiteral);
1411 if (Style.isTextProto())
1412 Prev->setType(TT_SelectorName);
1413 }
else if (Contexts.back().ColonIsObjCMethodExpr ||
1414 Line.startsWith(TT_ObjCMethodSpecifier)) {
1415 Tok->setType(TT_ObjCMethodExpr);
1416 const auto *PrevPrev = Prev->Previous;
1419 bool UnknownIdentifierInMethodDeclaration =
1420 Line.startsWith(TT_ObjCMethodSpecifier) &&
1421 Prev->is(tok::identifier) && Prev->is(TT_Unknown);
1424 !(PrevPrev->is(TT_CastRParen) ||
1425 (PrevPrev->is(TT_ObjCMethodExpr) && PrevPrev->is(tok::colon))) ||
1426 PrevPrev->is(tok::r_square) ||
1427 Contexts.back().LongestObjCSelectorName == 0 ||
1428 UnknownIdentifierInMethodDeclaration) {
1429 Prev->setType(TT_SelectorName);
1430 if (!Contexts.back().FirstObjCSelectorName)
1431 Contexts.back().FirstObjCSelectorName = Prev;
1432 else if (Prev->ColumnWidth > Contexts.back().LongestObjCSelectorName)
1433 Contexts.back().LongestObjCSelectorName = Prev->ColumnWidth;
1434 Prev->ParameterIndex =
1435 Contexts.back().FirstObjCSelectorName->ObjCSelectorNameParts;
1436 ++Contexts.back().FirstObjCSelectorName->ObjCSelectorNameParts;
1438 }
else if (Contexts.back().ColonIsForRangeExpr) {
1439 Tok->setType(TT_RangeBasedForLoopColon);
1440 for (
auto *Token = Prev;
1441 Token && Token->isNoneOf(tok::semi, tok::l_paren);
1442 Token = Token->Previous) {
1443 if (Token->isPointerOrReference())
1444 Token->setFinalizedType(TT_PointerOrReference);
1446 }
else if (Contexts.back().ContextType == Context::C11GenericSelection) {
1447 Tok->setType(TT_GenericSelectionColon);
1448 if (Prev->isPointerOrReference())
1449 Prev->setFinalizedType(TT_PointerOrReference);
1450 }
else if ((CurrentToken && CurrentToken->is(tok::numeric_constant)) ||
1451 (Prev->is(TT_StartOfName) && !Scopes.empty() &&
1453 Tok->setType(TT_BitFieldColon);
1454 }
else if (Contexts.size() == 1 &&
1455 Line.getFirstNonComment()->isNoneOf(tok::kw_enum, tok::kw_case,
1457 !Line.startsWith(tok::kw_typedef, tok::kw_enum)) {
1458 if (Prev->isOneOf(tok::r_paren, tok::kw_noexcept) ||
1459 Prev->ClosesRequiresClause) {
1460 Tok->setType(TT_CtorInitializerColon);
1461 }
else if (Prev->is(tok::kw_try)) {
1463 FormatToken *PrevPrev = Prev->getPreviousNonComment();
1466 if (PrevPrev && PrevPrev->isOneOf(tok::r_paren, tok::kw_noexcept))
1467 Tok->setType(TT_CtorInitializerColon);
1469 Tok->setType(TT_InheritanceColon);
1470 if (Prev->isAccessSpecifierKeyword())
1473 }
else if (canBeObjCSelectorComponent(*Prev) &&
Next &&
1474 (
Next->isOneOf(tok::r_paren, tok::comma) ||
1475 (canBeObjCSelectorComponent(*
Next) &&
Next->Next &&
1476 Next->Next->is(tok::colon)))) {
1479 Tok->setType(TT_ObjCSelector);
1486 if (Style.isJavaScript() && !Contexts.back().IsExpression)
1487 Tok->setType(TT_JsTypeOperator);
1490 if (Style.isTableGen()) {
1492 if (!parseTableGenValue())
1494 if (CurrentToken && CurrentToken->is(Keywords.kw_then))
1499 CurrentToken->isOneOf(tok::kw_constexpr, tok::identifier)) {
1505 if (CurrentToken && CurrentToken->is(tok::l_paren)) {
1507 if (!parseParens(IsIf))
1512 if (Style.isJavaScript()) {
1514 if ((Prev && Prev->is(tok::period)) || (
Next &&
Next->is(tok::colon)))
1517 if (CurrentToken && CurrentToken->is(Keywords.kw_await))
1520 if (IsCpp && CurrentToken && CurrentToken->is(tok::kw_co_await))
1522 Contexts.back().ColonIsForRangeExpr =
true;
1523 if (!CurrentToken || CurrentToken->isNot(tok::l_paren))
1534 if (Prev && Prev->is(tok::r_paren) && Prev->MatchingParen &&
1535 Prev->MatchingParen->is(TT_OverloadedOperatorLParen)) {
1536 Prev->setType(TT_OverloadedOperator);
1537 Prev->MatchingParen->setType(TT_OverloadedOperator);
1538 Tok->setType(TT_OverloadedOperatorLParen);
1541 if (Style.isVerilog()) {
1547 auto IsInstancePort = [&]() {
1556 if (!Prev || !(PrevPrev = Prev->getPreviousNonComment()))
1559 if (Keywords.isVerilogIdentifier(*Prev) &&
1560 Keywords.isVerilogIdentifier(*PrevPrev)) {
1564 if (Prev->is(Keywords.kw_verilogHash) &&
1565 Keywords.isVerilogIdentifier(*PrevPrev)) {
1569 if (Keywords.isVerilogIdentifier(*Prev) && PrevPrev->is(tok::r_paren))
1572 if (Keywords.isVerilogIdentifier(*Prev) && PrevPrev->is(tok::comma)) {
1573 const FormatToken *PrevParen = PrevPrev->getPreviousNonComment();
1574 if (PrevParen && PrevParen->is(tok::r_paren) &&
1575 PrevParen->MatchingParen &&
1576 PrevParen->MatchingParen->is(TT_VerilogInstancePortLParen)) {
1583 if (IsInstancePort())
1584 Tok->setType(TT_VerilogInstancePortLParen);
1589 if (Line.MustBeDeclaration && Contexts.size() == 1 &&
1590 !Contexts.back().IsExpression && !Line.startsWith(TT_ObjCProperty) &&
1591 !Line.startsWith(tok::l_paren) &&
1592 Tok->isNoneOf(TT_TypeDeclarationParen, TT_RequiresExpressionLParen)) {
1594 (!Prev->isAttribute() &&
1595 Prev->isNoneOf(TT_RequiresClause, TT_LeadingJavaAnnotation,
1596 TT_BinaryOperator))) {
1597 Line.MightBeFunctionDecl =
true;
1598 Tok->MightBeFunctionDeclParen =
true;
1603 if (Style.isTableGen())
1604 Tok->setType(TT_TableGenListOpener);
1610 if (
Tok->is(TT_RequiresExpressionLBrace))
1612 }
else if (Style.isTextProto()) {
1613 if (Prev && Prev->isNot(TT_DictLiteral))
1614 Prev->setType(TT_SelectorName);
1616 Scopes.push_back(getScopeType(*
Tok));
1622 Tok->setType(TT_TemplateOpener);
1628 if (Style.isTextProto() ||
1629 (Style.Language == FormatStyle::LK_Proto && Prev &&
1630 Prev->isOneOf(TT_SelectorName, TT_DictLiteral))) {
1631 Tok->setType(TT_DictLiteral);
1632 if (Prev && Prev->isNot(TT_DictLiteral))
1633 Prev->setType(TT_SelectorName);
1635 if (Style.isTableGen())
1636 Tok->setType(TT_TemplateOpener);
1638 Tok->setType(TT_BinaryOperator);
1639 NonTemplateLess.insert(
Tok);
1649 if (!Scopes.empty())
1656 if (!Style.isTextProto() &&
Tok->is(TT_Unknown))
1657 Tok->setType(TT_BinaryOperator);
1658 if (Prev && Prev->is(TT_TemplateCloser))
1659 Tok->SpacesRequiredBefore = 1;
1661 case tok::kw_operator:
1662 if (Style.isProto())
1665 if (IsCpp && CurrentToken) {
1666 const auto *Info = CurrentToken->Tok.getIdentifierInfo();
1668 if (Info && !(CurrentToken->isPlacementOperator() ||
1669 CurrentToken->is(tok::kw_co_await) ||
1670 Info->isCPlusPlusOperatorKeyword())) {
1672 if (CurrentToken->startsSequence(tok::kw_decltype, tok::l_paren,
1673 tok::kw_auto, tok::r_paren)) {
1675 LParen = CurrentToken->Next->Next->Next->Next;
1678 for (LParen = CurrentToken->Next;
1679 LParen && LParen->isNot(tok::l_paren); LParen = LParen->Next) {
1680 if (LParen->isPointerOrReference())
1681 LParen->setFinalizedType(TT_PointerOrReference);
1684 if (LParen && LParen->is(tok::l_paren)) {
1685 if (!Contexts.back().IsExpression) {
1686 Tok->setFinalizedType(TT_FunctionDeclarationName);
1687 LParen->setFinalizedType(TT_FunctionDeclarationLParen);
1693 while (CurrentToken &&
1694 CurrentToken->isNoneOf(tok::l_paren, tok::semi, tok::r_paren,
1696 if (CurrentToken->isOneOf(tok::star, tok::amp))
1697 CurrentToken->setType(TT_PointerOrReference);
1698 auto Next = CurrentToken->getNextNonComment();
1701 if (
Next->is(tok::less))
1707 auto Previous = CurrentToken->getPreviousNonComment();
1709 if (CurrentToken->is(tok::comma) &&
Previous->isNot(tok::kw_operator))
1711 if (
Previous->isOneOf(TT_BinaryOperator, TT_UnaryOperator, tok::comma,
1714 Previous->isPointerOrReference()) ||
1716 Previous->TokenText.starts_with(
"\"\"")) {
1717 Previous->setType(TT_OverloadedOperator);
1718 if (CurrentToken->isOneOf(tok::less, tok::greater))
1722 if (CurrentToken && CurrentToken->is(tok::l_paren))
1723 CurrentToken->setType(TT_OverloadedOperatorLParen);
1724 if (CurrentToken && CurrentToken->Previous->is(TT_BinaryOperator))
1725 CurrentToken->Previous->setType(TT_OverloadedOperator);
1728 if (Style.isJavaScript() &&
Next &&
1729 Next->isOneOf(tok::semi, tok::comma, tok::colon, tok::r_paren,
1730 tok::r_brace, tok::r_square)) {
1735 Tok->setType(TT_JsTypeOptionalQuestion);
1740 if (Line.MustBeDeclaration && !Contexts.back().IsExpression &&
1741 Style.isJavaScript()) {
1744 if (Style.isCSharp()) {
1747 if (
Next && (
Next->isOneOf(tok::r_paren, tok::greater) ||
1748 Next->startsSequence(tok::identifier, tok::semi) ||
1749 Next->startsSequence(tok::identifier, tok::equal))) {
1750 Tok->setType(TT_CSharpNullable);
1759 if (!Contexts.back().IsExpression && Line.MustBeDeclaration &&
1760 (!
Next ||
Next->isNoneOf(tok::identifier, tok::string_literal) ||
1761 !
Next->Next ||
Next->Next->isNoneOf(tok::colon, tok::question))) {
1762 Tok->setType(TT_CSharpNullable);
1768 case tok::kw_template:
1769 parseTemplateDeclaration();
1772 switch (Contexts.back().ContextType) {
1773 case Context::CtorInitializer:
1774 Tok->setType(TT_CtorInitializerComma);
1776 case Context::InheritanceList:
1777 Tok->setType(TT_InheritanceComma);
1779 case Context::VerilogInstancePortList:
1780 Tok->setType(TT_VerilogInstancePortComma);
1783 if (Style.isVerilog() && Contexts.size() == 1 &&
1784 Line.startsWith(Keywords.kw_assign)) {
1785 Tok->setFinalizedType(TT_VerilogAssignComma);
1786 }
else if (Contexts.back().FirstStartOfName &&
1787 (Contexts.size() == 1 || startsWithInitStatement(Line))) {
1788 Contexts.back().FirstStartOfName->PartOfMultiVariableDeclStmt =
true;
1789 Line.IsMultiVariableDeclStmt =
true;
1793 if (Contexts.back().ContextType == Context::ForEachMacro)
1794 Contexts.back().IsExpression =
true;
1796 case tok::kw_default:
1798 if (Style.isVerilog() && Keywords.isVerilogEndOfLabel(*
Tok) &&
1799 (Line.Level > 1 || (!Line.InPPDirective && Line.Level > 0))) {
1803 case tok::identifier:
1804 if (
Tok->isOneOf(Keywords.kw___has_include,
1805 Keywords.kw___has_include_next)) {
1809 if (
Next &&
Next->is(tok::l_paren) && Prev &&
1810 Prev->isOneOf(tok::kw___cdecl, tok::kw___stdcall,
1811 tok::kw___fastcall, tok::kw___thiscall,
1812 tok::kw___regcall, tok::kw___vectorcall)) {
1813 Tok->setFinalizedType(TT_FunctionDeclarationName);
1814 Next->setFinalizedType(TT_FunctionDeclarationLParen);
1816 }
else if (Style.isCSharp()) {
1817 if (
Tok->is(Keywords.kw_where) &&
Next &&
Next->isNot(tok::l_paren)) {
1818 Tok->setType(TT_CSharpGenericTypeConstraint);
1819 parseCSharpGenericTypeConstraint();
1821 Line.IsContinuation =
true;
1823 }
else if (Style.isTableGen()) {
1824 if (
Tok->is(Keywords.kw_assert)) {
1825 if (!parseTableGenValue())
1827 }
else if (
Tok->isOneOf(Keywords.kw_def, Keywords.kw_defm) &&
1828 (!
Next ||
Next->isNoneOf(tok::colon, tok::l_brace))) {
1830 if (!parseTableGenValue(
true))
1834 if (Style.AllowBreakBeforeQtProperty &&
1835 Contexts.back().ContextType == Context::QtProperty &&
1836 Tok->isQtProperty()) {
1837 Tok->setFinalizedType(TT_QtProperty);
1841 if (
Tok->isNot(TT_LambdaArrow) && Prev && Prev->is(tok::kw_noexcept))
1842 Tok->setType(TT_TrailingReturnArrow);
1846 if (Style.isTableGen() && !parseTableGenValue())
1848 if (!Scopes.empty() && Scopes.back() ==
ST_Enum)
1849 Tok->setFinalizedType(TT_EnumEqual);
1857 void parseCSharpGenericTypeConstraint() {
1858 int OpenAngleBracketsCount = 0;
1859 while (CurrentToken) {
1860 if (CurrentToken->is(tok::less)) {
1862 CurrentToken->setType(TT_TemplateOpener);
1863 ++OpenAngleBracketsCount;
1865 }
else if (CurrentToken->is(tok::greater)) {
1866 CurrentToken->setType(TT_TemplateCloser);
1867 --OpenAngleBracketsCount;
1869 }
else if (CurrentToken->is(tok::comma) && OpenAngleBracketsCount == 0) {
1872 CurrentToken->setType(TT_CSharpGenericTypeConstraintComma);
1874 }
else if (CurrentToken->is(Keywords.kw_where)) {
1875 CurrentToken->setType(TT_CSharpGenericTypeConstraint);
1877 }
else if (CurrentToken->is(tok::colon)) {
1878 CurrentToken->setType(TT_CSharpGenericTypeConstraintColon);
1886 void parseIncludeDirective() {
1887 if (CurrentToken && CurrentToken->is(tok::less)) {
1889 while (CurrentToken) {
1892 if (CurrentToken->isNot(tok::comment) &&
1893 !CurrentToken->TokenText.starts_with(
"//")) {
1894 CurrentToken->setType(TT_ImplicitStringLiteral);
1901 void parseWarningOrError() {
1906 while (CurrentToken) {
1907 CurrentToken->setType(TT_ImplicitStringLiteral);
1912 void parsePragma() {
1915 CurrentToken->isOneOf(Keywords.kw_mark, Keywords.kw_option,
1916 Keywords.kw_region)) {
1917 bool IsMarkOrRegion =
1918 CurrentToken->isOneOf(Keywords.kw_mark, Keywords.kw_region);
1921 while (CurrentToken) {
1922 if (IsMarkOrRegion || CurrentToken->Previous->is(TT_BinaryOperator))
1923 CurrentToken->setType(TT_ImplicitStringLiteral);
1929 void parseHasInclude() {
1930 if (!CurrentToken || CurrentToken->isNot(tok::l_paren))
1933 parseIncludeDirective();
1937 LineType parsePreprocessorDirective() {
1938 bool IsFirstToken = CurrentToken->IsFirst;
1944 if (Style.isJavaScript() && IsFirstToken) {
1948 while (CurrentToken) {
1950 CurrentToken->setType(TT_ImplicitStringLiteral);
1956 if (CurrentToken->is(tok::numeric_constant)) {
1957 CurrentToken->SpacesRequiredBefore = 1;
1962 if (!CurrentToken->Tok.getIdentifierInfo())
1966 if (Style.isVerilog() && !Keywords.isVerilogPPDirective(*CurrentToken))
1968 switch (CurrentToken->Tok.getIdentifierInfo()->getPPKeywordID()) {
1969 case tok::pp_include:
1970 case tok::pp_include_next:
1971 case tok::pp_import:
1973 parseIncludeDirective();
1977 case tok::pp_warning:
1978 parseWarningOrError();
1980 case tok::pp_pragma:
1985 Contexts.back().IsExpression =
true;
1988 CurrentToken->SpacesRequiredBefore = 1;
1994 while (CurrentToken) {
1997 if (
Tok->is(tok::l_paren)) {
1999 }
else if (
Tok->isOneOf(Keywords.kw___has_include,
2000 Keywords.kw___has_include_next)) {
2011 NonTemplateLess.clear();
2012 if (!Line.InMacroBody && CurrentToken->is(tok::hash)) {
2016 auto Type = parsePreprocessorDirective();
2024 IdentifierInfo *Info = CurrentToken->Tok.getIdentifierInfo();
2025 if ((Style.isJava() && CurrentToken->is(Keywords.kw_package)) ||
2026 (!Style.isVerilog() && Info &&
2027 Info->getPPKeywordID() == tok::pp_import && CurrentToken->Next &&
2028 CurrentToken->Next->isOneOf(tok::string_literal, tok::identifier,
2031 parseIncludeDirective();
2037 if (CurrentToken->is(tok::less) && Line.Last->is(tok::greater)) {
2038 parseIncludeDirective();
2044 if (Style.Language == FormatStyle::LK_Proto && Line.Level == 0 &&
2045 CurrentToken->isOneOf(Keywords.kw_option, Keywords.kw_package)) {
2047 if (CurrentToken && CurrentToken->is(tok::identifier)) {
2048 while (CurrentToken)
2054 bool KeywordVirtualFound =
false;
2055 bool ImportStatement =
false;
2058 if (Style.isJavaScript() && CurrentToken->is(Keywords.kw_import))
2059 ImportStatement =
true;
2061 while (CurrentToken) {
2062 if (CurrentToken->is(tok::kw_virtual))
2063 KeywordVirtualFound =
true;
2064 if (Style.isJavaScript()) {
2071 if (Line.First->is(tok::kw_export) &&
2072 CurrentToken->is(Keywords.kw_from) && CurrentToken->Next &&
2073 CurrentToken->Next->isStringLiteral()) {
2074 ImportStatement =
true;
2076 if (isClosureImportStatement(*CurrentToken))
2077 ImportStatement =
true;
2079 if (!consumeToken())
2087 if (KeywordVirtualFound)
2089 if (ImportStatement)
2092 if (Line.startsWith(TT_ObjCMethodSpecifier)) {
2093 if (Contexts.back().FirstObjCSelectorName) {
2094 Contexts.back().FirstObjCSelectorName->LongestObjCSelectorName =
2095 Contexts.back().LongestObjCSelectorName;
2100 for (
const auto &ctx : Contexts)
2101 if (ctx.ContextType == Context::StructArrayInitializer)
2111 return Tok.TokenText ==
"goog" &&
Tok.Next &&
Tok.Next->is(tok::period) &&
2113 (
Tok.Next->Next->TokenText ==
"module" ||
2114 Tok.Next->Next->TokenText ==
"provide" ||
2115 Tok.Next->Next->TokenText ==
"require" ||
2116 Tok.Next->Next->TokenText ==
"requireType" ||
2117 Tok.Next->Next->TokenText ==
"forwardDeclare") &&
2118 Tok.Next->Next->Next &&
Tok.Next->Next->Next->is(tok::l_paren);
2121 void resetTokenMetadata() {
2127 if (!CurrentToken->isTypeFinalized() &&
2128 CurrentToken->isNoneOf(
2129 TT_LambdaLSquare, TT_LambdaLBrace, TT_AttributeMacro, TT_IfMacro,
2130 TT_ForEachMacro, TT_TypenameMacro, TT_FunctionLBrace,
2131 TT_ImplicitStringLiteral, TT_InlineASMBrace, TT_FatArrow,
2132 TT_LambdaArrow, TT_NamespaceMacro, TT_OverloadedOperator,
2133 TT_RegexLiteral, TT_TemplateString, TT_ObjCStringLiteral,
2134 TT_UntouchableMacroFunc, TT_StatementAttributeLikeMacro,
2135 TT_FunctionLikeOrFreestandingMacro, TT_ClassLBrace, TT_EnumLBrace,
2136 TT_RecordLBrace, TT_StructLBrace, TT_UnionLBrace, TT_RequiresClause,
2137 TT_RequiresClauseInARequiresExpression, TT_RequiresExpression,
2138 TT_RequiresExpressionLParen, TT_RequiresExpressionLBrace,
2139 TT_CompoundRequirementLBrace, TT_BracedListLBrace,
2140 TT_FunctionLikeMacro)) {
2141 CurrentToken->setType(TT_Unknown);
2143 CurrentToken->Role.reset();
2144 CurrentToken->MatchingParen =
nullptr;
2145 CurrentToken->FakeLParens.clear();
2146 CurrentToken->FakeRParens = 0;
2153 CurrentToken->NestingLevel = Contexts.size() - 1;
2154 CurrentToken->BindingStrength = Contexts.back().BindingStrength;
2155 modifyContext(*CurrentToken);
2156 determineTokenType(*CurrentToken);
2157 CurrentToken = CurrentToken->Next;
2159 resetTokenMetadata();
2167 : ContextKind(ContextKind), BindingStrength(BindingStrength),
2168 IsExpression(IsExpression) {}
2171 unsigned BindingStrength;
2173 unsigned LongestObjCSelectorName = 0;
2174 bool ColonIsForRangeExpr =
false;
2175 bool ColonIsDictLiteral =
false;
2176 bool ColonIsObjCMethodExpr =
false;
2179 bool CanBeExpression =
true;
2180 bool CaretFound =
false;
2181 bool InCpp11AttributeSpecifier =
false;
2182 bool InCSharpAttributeSpecifier =
false;
2183 bool InStaticAssertFirstArgument =
false;
2184 bool VerilogAssignmentFound =
false;
2187 bool VerilogMayBeConcatenation =
false;
2188 bool IsTableGenDAGArgList =
false;
2189 bool IsTableGenBangOpe =
false;
2190 bool IsTableGenCondOpe =
false;
2203 StructArrayInitializer,
2207 C11GenericSelection,
2210 VerilogInstancePortList,
2211 } ContextType = Unknown;
2216 struct ScopedContextCreator {
2217 AnnotatingParser &P;
2219 ScopedContextCreator(AnnotatingParser &P,
tok::TokenKind ContextKind,
2222 P.Contexts.push_back(Context(ContextKind,
2223 P.Contexts.back().BindingStrength + Increase,
2224 P.Contexts.back().IsExpression));
2227 ~ScopedContextCreator() {
2228 if (P.Style.AlignArrayOfStructures != FormatStyle::AIAS_None) {
2229 if (P.Contexts.back().ContextType == Context::StructArrayInitializer) {
2230 P.Contexts.pop_back();
2231 P.Contexts.back().ContextType = Context::StructArrayInitializer;
2235 P.Contexts.pop_back();
2240 auto AssignmentStartsExpression = [&]() {
2244 if (Line.First->isOneOf(tok::kw_using, tok::kw_return))
2246 if (Line.First->is(tok::kw_template)) {
2247 assert(Current.Previous);
2248 if (Current.Previous->is(tok::kw_operator)) {
2256 if (
Tok->isNot(TT_TemplateOpener)) {
2263 if (Contexts.back().ContextKind == tok::less) {
2264 assert(Current.Previous->Previous);
2265 return Current.Previous->Previous->isNoneOf(tok::kw_typename,
2269 Tok =
Tok->MatchingParen;
2272 Tok =
Tok->getNextNonComment();
2276 if (
Tok->isOneOf(tok::kw_class, tok::kw_enum, tok::kw_struct,
2286 if (Style.isJavaScript() &&
2287 (Line.startsWith(Keywords.kw_type, tok::identifier) ||
2288 Line.startsWith(tok::kw_export, Keywords.kw_type,
2289 tok::identifier))) {
2293 return !Current.Previous || Current.Previous->isNot(tok::kw_operator);
2296 if (AssignmentStartsExpression()) {
2297 Contexts.back().IsExpression =
true;
2298 if (!Line.startsWith(TT_UnaryOperator)) {
2301 Previous->Previous->isNoneOf(tok::comma, tok::semi);
2303 if (
Previous->isOneOf(tok::r_square, tok::r_paren, tok::greater)) {
2310 if (
Previous->isOneOf(TT_BinaryOperator, TT_UnaryOperator) &&
2312 Previous->Previous->isNot(tok::equal)) {
2313 Previous->setType(TT_PointerOrReference);
2317 }
else if (Current.is(tok::lessless) &&
2318 (!Current.Previous ||
2319 Current.Previous->isNot(tok::kw_operator))) {
2320 Contexts.back().IsExpression =
true;
2321 }
else if (Current.isOneOf(tok::kw_return, tok::kw_throw)) {
2322 Contexts.back().IsExpression =
true;
2323 }
else if (Current.is(TT_TrailingReturnArrow)) {
2324 Contexts.back().IsExpression =
false;
2325 }
else if (Current.isOneOf(TT_LambdaArrow, Keywords.kw_assert)) {
2326 Contexts.back().IsExpression = Style.isJava();
2327 }
else if (Current.Previous &&
2328 Current.Previous->is(TT_CtorInitializerColon)) {
2329 Contexts.back().IsExpression =
true;
2330 Contexts.back().ContextType = Context::CtorInitializer;
2331 }
else if (Current.Previous && Current.Previous->is(TT_InheritanceColon)) {
2332 Contexts.back().ContextType = Context::InheritanceList;
2333 }
else if (Current.isOneOf(tok::r_paren, tok::greater, tok::comma)) {
2337 Previous->setType(TT_PointerOrReference);
2339 if (Line.MustBeDeclaration &&
2340 Contexts.front().ContextType != Context::CtorInitializer) {
2341 Contexts.back().IsExpression =
false;
2343 }
else if (Current.is(tok::kw_new)) {
2344 Contexts.back().CanBeExpression =
false;
2345 }
else if (Current.is(tok::semi) ||
2346 (Current.is(tok::exclaim) && Current.Previous &&
2347 Current.Previous->isNot(tok::kw_operator))) {
2351 Contexts.back().IsExpression =
true;
2359 if (Current->is(tok::l_paren))
2361 if (Current->is(tok::r_paren))
2365 Current = Current->Next;
2370 static bool isDeductionGuide(
FormatToken &Current) {
2372 if (Current.Previous && Current.Previous->is(tok::r_paren) &&
2373 Current.startsSequence(tok::arrow, tok::identifier, tok::less)) {
2377 while (TemplateCloser) {
2379 if (TemplateCloser->is(tok::l_paren)) {
2381 TemplateCloser = untilMatchingParen(TemplateCloser);
2382 if (!TemplateCloser)
2385 if (TemplateCloser->is(tok::less))
2387 if (TemplateCloser->is(tok::greater))
2391 TemplateCloser = TemplateCloser->Next;
2395 if (TemplateCloser && TemplateCloser->Next &&
2396 TemplateCloser->Next->is(tok::semi) &&
2397 Current.Previous->MatchingParen) {
2401 Current.Previous->MatchingParen->Previous;
2403 return LeadingIdentifier &&
2404 LeadingIdentifier->TokenText == Current.Next->TokenText;
2411 if (Current.isNot(TT_Unknown)) {
2416 if ((Style.isJavaScript() || Style.isCSharp()) &&
2417 Current.is(tok::exclaim)) {
2418 if (Current.Previous) {
2420 Style.isJavaScript()
2421 ? Keywords.isJavaScriptIdentifier(
2422 *Current.Previous,
true)
2423 : Current.Previous->is(tok::identifier);
2425 Current.Previous->isOneOf(
2426 tok::kw_default, tok::kw_namespace, tok::r_paren, tok::r_square,
2427 tok::r_brace, tok::kw_false, tok::kw_true, Keywords.kw_type,
2428 Keywords.kw_get, Keywords.kw_init, Keywords.kw_set) ||
2429 Current.Previous->Tok.isLiteral()) {
2430 Current.setType(TT_NonNullAssertion);
2435 Current.Next->isOneOf(TT_BinaryOperator, Keywords.kw_as)) {
2436 Current.setType(TT_NonNullAssertion);
2444 if ((Style.isJavaScript() || Style.isJava()) &&
2445 Current.is(Keywords.kw_instanceof)) {
2446 Current.setType(TT_BinaryOperator);
2447 }
else if (isStartOfName(Current) &&
2448 (!Line.MightBeFunctionDecl || Current.NestingLevel != 0)) {
2449 Contexts.back().FirstStartOfName = &Current;
2450 Current.setType(TT_StartOfName);
2451 }
else if (Current.is(tok::semi)) {
2455 Contexts.back().FirstStartOfName =
nullptr;
2456 }
else if (Current.isOneOf(tok::kw_auto, tok::kw___auto_type)) {
2458 }
else if (Current.is(tok::arrow) && Style.isJava()) {
2459 Current.setType(TT_LambdaArrow);
2460 }
else if (Current.is(tok::arrow) && Style.isVerilog()) {
2462 Current.setType(TT_BinaryOperator);
2463 }
else if (Current.is(tok::arrow) && AutoFound &&
2464 Line.MightBeFunctionDecl && Current.NestingLevel == 0 &&
2465 Current.Previous->isNoneOf(tok::kw_operator, tok::identifier)) {
2467 Current.setType(TT_TrailingReturnArrow);
2468 }
else if (Current.is(tok::arrow) && Current.Previous &&
2469 Current.Previous->is(tok::r_brace) &&
2473 Current.setType(TT_TrailingReturnArrow);
2474 }
else if (isDeductionGuide(Current)) {
2476 Current.setType(TT_TrailingReturnArrow);
2477 }
else if (Current.isPointerOrReference()) {
2478 Current.setType(determineStarAmpUsage(
2480 (Contexts.back().CanBeExpression && Contexts.back().IsExpression) ||
2481 Contexts.back().InStaticAssertFirstArgument,
2482 Contexts.back().ContextType == Context::TemplateArgument));
2483 }
else if (Current.isOneOf(tok::minus, tok::plus, tok::caret) ||
2484 (Style.isVerilog() && Current.is(tok::pipe))) {
2485 Current.setType(determinePlusMinusCaretUsage(Current));
2486 if (Current.is(TT_UnaryOperator) && Current.is(tok::caret))
2487 Contexts.back().CaretFound =
true;
2488 }
else if (Current.isOneOf(tok::minusminus, tok::plusplus)) {
2489 Current.setType(determineIncrementUsage(Current));
2490 }
else if (Current.isOneOf(tok::exclaim, tok::tilde)) {
2491 Current.setType(TT_UnaryOperator);
2492 }
else if (Current.is(tok::question)) {
2493 if (Style.isJavaScript() && Line.MustBeDeclaration &&
2494 !Contexts.back().IsExpression) {
2497 Current.setType(TT_JsTypeOptionalQuestion);
2498 }
else if (Style.isTableGen()) {
2500 Current.setType(TT_Unknown);
2502 Current.setType(TT_ConditionalExpr);
2504 Contexts.back().IsExpression =
true;
2506 }
else if (Current.isBinaryOperator() &&
2507 (!Current.Previous || Current.Previous->isNot(tok::l_square)) &&
2508 (Current.isNot(tok::greater) && !Style.isTextProto())) {
2509 if (Style.isVerilog()) {
2510 if (Current.is(tok::lessequal) && Contexts.size() == 1 &&
2511 !Contexts.back().VerilogAssignmentFound) {
2515 Current.setFinalizedType(TT_BinaryOperator);
2518 Contexts.back().VerilogAssignmentFound =
true;
2520 Current.setType(TT_BinaryOperator);
2521 }
else if (Current.is(tok::comment)) {
2522 if (Current.TokenText.starts_with(
"/*")) {
2523 if (Current.TokenText.ends_with(
"*/")) {
2524 Current.setType(TT_BlockComment);
2528 Current.Tok.setKind(tok::unknown);
2531 Current.setType(TT_LineComment);
2533 }
else if (Current.is(tok::string_literal)) {
2534 if (Style.isVerilog() && Contexts.back().VerilogMayBeConcatenation &&
2535 Current.getPreviousNonComment() &&
2536 Current.getPreviousNonComment()->isOneOf(tok::comma, tok::l_brace) &&
2537 Current.getNextNonComment() &&
2538 Current.getNextNonComment()->isOneOf(tok::comma, tok::r_brace)) {
2539 Current.setType(TT_StringInConcatenation);
2541 }
else if (Current.is(tok::l_paren)) {
2542 if (lParenStartsCppCast(Current))
2543 Current.setType(TT_CppCastLParen);
2544 }
else if (Current.is(tok::r_paren)) {
2545 if (rParenEndsCast(Current))
2546 Current.setType(TT_CastRParen);
2547 if (Current.MatchingParen && Current.MatchingParen->is(TT_InlineASMParen))
2548 Current.setType(TT_InlineASMParen);
2549 if (Current.MatchingParen && Current.Next &&
2550 !Current.Next->isBinaryOperator() &&
2551 Current.Next->isNoneOf(
2552 tok::semi, tok::colon, tok::l_brace, tok::l_paren, tok::comma,
2553 tok::period, tok::arrow, tok::coloncolon, tok::kw_noexcept)) {
2554 if (
FormatToken *AfterParen = Current.MatchingParen->Next;
2555 AfterParen && AfterParen->isNot(tok::caret)) {
2557 if (
FormatToken *BeforeParen = Current.MatchingParen->Previous;
2558 BeforeParen && BeforeParen->is(tok::identifier) &&
2559 BeforeParen->isNot(TT_TypenameMacro) &&
2560 BeforeParen->TokenText == BeforeParen->TokenText.upper() &&
2561 (!BeforeParen->Previous ||
2562 BeforeParen->Previous->ClosesTemplateDeclaration ||
2563 BeforeParen->Previous->ClosesRequiresClause)) {
2564 Current.setType(TT_FunctionAnnotationRParen);
2568 }
else if (Current.is(tok::at) && Current.Next && !Style.isJavaScript() &&
2572 switch (Current.Next->Tok.getObjCKeywordID()) {
2573 case tok::objc_interface:
2574 case tok::objc_implementation:
2575 case tok::objc_protocol:
2576 Current.setType(TT_ObjCDecl);
2578 case tok::objc_property:
2579 Current.setType(TT_ObjCProperty);
2584 }
else if (Current.is(tok::period)) {
2585 FormatToken *PreviousNoComment = Current.getPreviousNonComment();
2586 if (PreviousNoComment &&
2587 PreviousNoComment->isOneOf(tok::comma, tok::l_brace)) {
2588 Current.setType(TT_DesignatedInitializerPeriod);
2589 }
else if (Style.isJava() && Current.Previous &&
2590 Current.Previous->isOneOf(TT_JavaAnnotation,
2591 TT_LeadingJavaAnnotation)) {
2592 Current.setType(Current.Previous->getType());
2594 }
else if (canBeObjCSelectorComponent(Current) &&
2597 Current.Previous && Current.Previous->is(TT_CastRParen) &&
2598 Current.Previous->MatchingParen &&
2599 Current.Previous->MatchingParen->Previous &&
2600 Current.Previous->MatchingParen->Previous->is(
2601 TT_ObjCMethodSpecifier)) {
2605 Current.setType(TT_SelectorName);
2606 }
else if (Current.isOneOf(tok::identifier, tok::kw_const, tok::kw_noexcept,
2607 tok::kw_requires) &&
2609 Current.Previous->isNoneOf(tok::equal, tok::at,
2610 TT_CtorInitializerComma,
2611 TT_CtorInitializerColon) &&
2612 Line.MightBeFunctionDecl && Contexts.size() == 1) {
2615 Current.setType(TT_TrailingAnnotation);
2616 }
else if ((Style.isJava() || Style.isJavaScript()) && Current.Previous) {
2617 if (Current.Previous->is(tok::at) &&
2618 Current.isNot(Keywords.kw_interface)) {
2622 Current.setType(TT_LeadingJavaAnnotation);
2624 Current.setType(TT_JavaAnnotation);
2625 }
else if (Current.Previous->is(tok::period) &&
2626 Current.Previous->isOneOf(TT_JavaAnnotation,
2627 TT_LeadingJavaAnnotation)) {
2628 Current.setType(Current.Previous->getType());
2640 if (Style.isVerilog())
2643 if (!
Tok.Previous ||
Tok.isNot(tok::identifier) ||
Tok.is(TT_ClassHeadName))
2646 if (
Tok.endsSequence(Keywords.kw_final, TT_ClassHeadName))
2649 if ((Style.isJavaScript() || Style.isJava()) &&
Tok.is(Keywords.kw_extends))
2652 if (
const auto *NextNonComment =
Tok.getNextNonComment();
2653 (!NextNonComment && !Line.InMacroBody) ||
2655 (NextNonComment->isPointerOrReference() ||
2656 NextNonComment->isOneOf(TT_ClassHeadName, tok::string_literal) ||
2657 (Line.InPragmaDirective && NextNonComment->is(tok::identifier))))) {
2661 if (
Tok.Previous->isOneOf(TT_LeadingJavaAnnotation, Keywords.kw_instanceof,
2665 if (Style.isJavaScript() &&
Tok.Previous->is(Keywords.kw_in))
2672 if (!Style.isJavaScript())
2673 while (PreviousNotConst && PreviousNotConst->is(tok::kw_const))
2674 PreviousNotConst = PreviousNotConst->getPreviousNonComment();
2676 if (!PreviousNotConst)
2679 if (PreviousNotConst->ClosesRequiresClause)
2682 if (Style.isTableGen()) {
2684 if (Keywords.isTableGenDefinition(*PreviousNotConst))
2687 if (Contexts.back().ContextKind != tok::l_brace)
2691 bool IsPPKeyword = PreviousNotConst->is(tok::identifier) &&
2692 PreviousNotConst->Previous &&
2693 PreviousNotConst->Previous->is(tok::hash);
2695 if (PreviousNotConst->is(TT_TemplateCloser)) {
2696 return PreviousNotConst && PreviousNotConst->MatchingParen &&
2697 PreviousNotConst->MatchingParen->Previous &&
2698 PreviousNotConst->MatchingParen->Previous->isNoneOf(
2699 tok::period, tok::kw_template);
2702 if ((PreviousNotConst->is(tok::r_paren) &&
2703 PreviousNotConst->is(TT_TypeDeclarationParen)) ||
2704 PreviousNotConst->is(TT_AttributeRParen)) {
2713 if (PreviousNotConst->isOneOf(tok::identifier, tok::kw_auto) &&
2714 !PreviousNotConst->endsSequence(Keywords.kw_import, tok::kw_export) &&
2715 PreviousNotConst->isNot(TT_StatementAttributeLikeMacro)) {
2720 if (PreviousNotConst->is(TT_PointerOrReference) ||
2721 PreviousNotConst->endsSequence(tok::coloncolon,
2722 TT_PointerOrReference)) {
2727 if (PreviousNotConst->isTypeName(LangOpts))
2731 if (Style.isJava() && PreviousNotConst->is(tok::r_square))
2735 return Style.isJavaScript() && PreviousNotConst->is(tok::kw_const);
2745 if (LeftOfParens && LeftOfParens->is(TT_TemplateCloser) &&
2746 LeftOfParens->MatchingParen) {
2747 auto *Prev = LeftOfParens->MatchingParen->getPreviousNonComment();
2749 Prev->isOneOf(tok::kw_const_cast, tok::kw_dynamic_cast,
2750 tok::kw_reinterpret_cast, tok::kw_static_cast)) {
2761 assert(
Tok.is(tok::r_paren));
2763 if (!
Tok.MatchingParen || !
Tok.Previous)
2767 if (!IsCpp && !Style.isCSharp() && !Style.isJava())
2770 const auto *LParen =
Tok.MatchingParen;
2771 const auto *BeforeRParen =
Tok.Previous;
2772 const auto *AfterRParen =
Tok.Next;
2775 if (BeforeRParen == LParen || !AfterRParen)
2778 if (LParen->isOneOf(TT_OverloadedOperatorLParen, TT_FunctionTypeLParen))
2781 auto *LeftOfParens = LParen->getPreviousNonComment();
2785 if (LeftOfParens->is(tok::r_paren) &&
2786 LeftOfParens->isNot(TT_CastRParen)) {
2787 if (!LeftOfParens->MatchingParen ||
2788 !LeftOfParens->MatchingParen->Previous) {
2791 LeftOfParens = LeftOfParens->MatchingParen->Previous;
2794 if (LeftOfParens->is(tok::r_square)) {
2797 if (
Tok->isNot(tok::r_square))
2800 Tok =
Tok->getPreviousNonComment();
2801 if (!
Tok ||
Tok->isNot(tok::l_square))
2804 Tok =
Tok->getPreviousNonComment();
2805 if (!
Tok ||
Tok->isNot(tok::kw_delete))
2809 if (
FormatToken *MaybeDelete = MayBeArrayDelete(LeftOfParens))
2810 LeftOfParens = MaybeDelete;
2816 if (LeftOfParens->Tok.getIdentifierInfo() && LeftOfParens->Previous &&
2817 LeftOfParens->Previous->is(tok::kw_operator)) {
2823 if (LeftOfParens->Tok.getIdentifierInfo() &&
2824 LeftOfParens->isNoneOf(TT_ObjCForIn, tok::kw_return, tok::kw_case,
2825 tok::kw_delete, tok::kw_throw)) {
2831 if (LeftOfParens->isOneOf(tok::at, tok::r_square, TT_OverloadedOperator,
2832 TT_TemplateCloser, tok::ellipsis)) {
2837 if (AfterRParen->is(tok::question) ||
2838 (AfterRParen->is(tok::ampamp) && !BeforeRParen->isTypeName(LangOpts))) {
2843 if (AfterRParen->is(Keywords.kw_in) && Style.isCSharp())
2848 if (AfterRParen->isOneOf(tok::kw_noexcept, tok::kw_volatile, tok::kw_const,
2849 tok::kw_requires, tok::kw_throw, tok::arrow,
2850 Keywords.kw_override, Keywords.kw_final) ||
2851 isCppAttribute(IsCpp, *AfterRParen)) {
2857 if (Style.isJava() && AfterRParen->is(tok::l_paren))
2861 if (AfterRParen->isOneOf(tok::kw_sizeof, tok::kw_alignof) ||
2862 (AfterRParen->Tok.isLiteral() &&
2863 AfterRParen->isNot(tok::string_literal))) {
2868 if (
Tok.isNot(TT_TemplateCloser))
2870 const auto *
Less =
Tok.MatchingParen;
2873 const auto *BeforeLess =
Less->getPreviousNonComment();
2874 return BeforeLess && BeforeLess->isNot(TT_VariableTemplate);
2878 auto IsQualifiedPointerOrReference = [](
const FormatToken *T,
2879 const LangOptions &LangOpts) {
2881 assert(!T->isTypeName(LangOpts) &&
"Should have already been checked");
2885 if (T->is(TT_AttributeRParen)) {
2887 assert(T->is(tok::r_paren));
2888 assert(T->MatchingParen);
2889 assert(T->MatchingParen->is(tok::l_paren));
2890 assert(T->MatchingParen->is(TT_AttributeLParen));
2891 if (
const auto *
Tok = T->MatchingParen->Previous;
2892 Tok &&
Tok->isAttribute()) {
2896 }
else if (T->is(TT_AttributeRSquare)) {
2898 if (T->MatchingParen && T->MatchingParen->Previous) {
2899 T = T->MatchingParen->Previous;
2902 }
else if (T->canBePointerOrReferenceQualifier()) {
2908 return T && T->is(TT_PointerOrReference);
2911 bool ParensAreType = IsNonVariableTemplate(*BeforeRParen) ||
2912 BeforeRParen->is(TT_TypeDeclarationParen) ||
2913 BeforeRParen->isTypeName(LangOpts) ||
2914 IsQualifiedPointerOrReference(BeforeRParen, LangOpts);
2915 bool ParensCouldEndDecl =
2916 AfterRParen->isOneOf(tok::equal, tok::semi, tok::l_brace, tok::greater);
2917 if (ParensAreType && !ParensCouldEndDecl)
2928 for (
const auto *Token = LParen->Next; Token != &
Tok; Token = Token->Next)
2929 if (Token->is(TT_BinaryOperator))
2934 if (AfterRParen->isOneOf(tok::identifier, tok::kw_this))
2938 if (AfterRParen->is(tok::l_paren)) {
2939 for (
const auto *Prev = BeforeRParen; Prev->is(tok::identifier);) {
2940 Prev = Prev->Previous;
2941 if (Prev->is(tok::coloncolon))
2942 Prev = Prev->Previous;
2948 if (!AfterRParen->Next)
2953 if (Style.Language != FormatStyle::LK_C && AfterRParen->is(tok::l_brace) &&
2961 const bool NextIsAmpOrStar = AfterRParen->isOneOf(tok::amp, tok::star);
2962 if (!(AfterRParen->isUnaryOperator() || NextIsAmpOrStar) ||
2963 AfterRParen->is(tok::plus) ||
2964 AfterRParen->Next->isNoneOf(tok::identifier, tok::numeric_constant)) {
2968 if (NextIsAmpOrStar &&
2969 (AfterRParen->Next->is(tok::numeric_constant) || Line.InPPDirective)) {
2973 if (Line.InPPDirective && AfterRParen->is(tok::minus))
2976 const auto *Prev = BeforeRParen;
2979 if (Prev->is(tok::r_paren)) {
2980 if (Prev->is(TT_CastRParen))
2982 Prev = Prev->MatchingParen;
2985 Prev = Prev->Previous;
2986 if (!Prev || Prev->isNot(tok::r_paren))
2988 Prev = Prev->MatchingParen;
2989 return Prev && Prev->is(TT_FunctionTypeLParen);
2993 for (Prev = BeforeRParen; Prev != LParen; Prev = Prev->Previous)
2994 if (Prev->isNoneOf(tok::kw_const, tok::identifier, tok::coloncolon))
3012 if (PrevToken->isOneOf(
3013 TT_ConditionalExpr, tok::l_paren, tok::comma, tok::colon, tok::semi,
3014 tok::equal, tok::question, tok::l_square, tok::l_brace,
3015 tok::kw_case, tok::kw_co_await, tok::kw_co_return, tok::kw_co_yield,
3016 tok::kw_delete, tok::kw_return, tok::kw_throw)) {
3023 if (PrevToken->is(tok::kw_sizeof))
3027 if (PrevToken->isOneOf(TT_CastRParen, TT_UnaryOperator))
3031 if (PrevToken->is(TT_BinaryOperator))
3039 bool InTemplateArgument) {
3040 if (Style.isJavaScript())
3041 return TT_BinaryOperator;
3044 if (Style.isCSharp() &&
Tok.is(tok::ampamp))
3045 return TT_BinaryOperator;
3047 if (Style.isVerilog()) {
3052 if (
Tok.is(tok::star))
3053 return TT_BinaryOperator;
3054 return determineUnaryOperatorByUsage(
Tok) ? TT_UnaryOperator
3055 : TT_BinaryOperator;
3060 return TT_UnaryOperator;
3061 if (PrevToken->isTypeName(LangOpts))
3062 return TT_PointerOrReference;
3063 if (PrevToken->isPlacementOperator() &&
Tok.is(tok::ampamp))
3064 return TT_BinaryOperator;
3066 auto *NextToken =
Tok.getNextNonComment();
3068 return TT_PointerOrReference;
3069 if (NextToken->is(tok::greater))
3070 return TT_PointerOrReference;
3072 if (InTemplateArgument && NextToken->is(tok::kw_noexcept))
3073 return TT_BinaryOperator;
3075 if (NextToken->isOneOf(tok::arrow, tok::equal, tok::comma, tok::r_paren,
3076 tok::semi, TT_RequiresClause) ||
3077 (NextToken->is(tok::kw_noexcept) && !IsExpression) ||
3078 NextToken->canBePointerOrReferenceQualifier() ||
3079 (NextToken->is(tok::l_brace) && !NextToken->getNextNonComment())) {
3080 return TT_PointerOrReference;
3083 if (PrevToken->is(tok::coloncolon))
3084 return TT_PointerOrReference;
3086 if (PrevToken->is(tok::r_paren) && PrevToken->is(TT_TypeDeclarationParen))
3087 return TT_PointerOrReference;
3089 if (determineUnaryOperatorByUsage(
Tok))
3090 return TT_UnaryOperator;
3092 if (NextToken->is(tok::l_square) && NextToken->isNot(TT_LambdaLSquare))
3093 return TT_PointerOrReference;
3094 if (NextToken->is(tok::kw_operator) && !IsExpression)
3095 return TT_PointerOrReference;
3107 if (PrevToken->is(tok::r_brace) &&
Tok.is(tok::star) &&
3108 !PrevToken->MatchingParen) {
3109 return TT_PointerOrReference;
3112 if (PrevToken->endsSequence(tok::r_square, tok::l_square, tok::kw_delete))
3113 return TT_UnaryOperator;
3115 if (PrevToken->Tok.isLiteral() ||
3116 PrevToken->isOneOf(tok::r_paren, tok::r_square, tok::kw_true,
3117 tok::kw_false, tok::r_brace)) {
3118 return TT_BinaryOperator;
3122 while (NextNonParen && NextNonParen->is(tok::l_paren))
3123 NextNonParen = NextNonParen->getNextNonComment();
3124 if (NextNonParen && (NextNonParen->Tok.isLiteral() ||
3125 NextNonParen->isOneOf(tok::kw_true, tok::kw_false) ||
3126 NextNonParen->isUnaryOperator())) {
3127 return TT_BinaryOperator;
3133 if (InTemplateArgument && NextToken->Tok.isAnyIdentifier())
3134 return TT_BinaryOperator;
3138 if (
Tok.is(tok::ampamp) &&
3139 NextToken->isOneOf(tok::l_paren, tok::star, tok::amp)) {
3140 return TT_BinaryOperator;
3147 if (NextToken->Tok.isAnyIdentifier()) {
3148 auto *NextNextToken = NextToken->getNextNonComment();
3149 if (NextNextToken) {
3150 if (NextNextToken->is(tok::arrow))
3151 return TT_BinaryOperator;
3152 if (NextNextToken->isPointerOrReference() &&
3153 !NextToken->isObjCLifetimeQualifier(Style)) {
3154 NextNextToken->setFinalizedType(TT_BinaryOperator);
3155 return TT_BinaryOperator;
3162 if (IsExpression && !Contexts.back().CaretFound &&
3163 Line.getFirstNonComment()->isNot(
3164 TT_RequiresClauseInARequiresExpression)) {
3165 return TT_BinaryOperator;
3169 if (!Scopes.empty() && Scopes.back() ==
ST_Class)
3170 return TT_PointerOrReference;
3173 auto IsChainedOperatorAmpOrMember = [](
const FormatToken *token) {
3174 return !token || token->isOneOf(tok::amp, tok::period, tok::arrow,
3175 tok::arrowstar, tok::periodstar);
3180 if (
Tok.is(tok::amp) && PrevToken->Tok.isAnyIdentifier() &&
3181 IsChainedOperatorAmpOrMember(PrevToken->getPreviousNonComment()) &&
3182 NextToken && NextToken->Tok.isAnyIdentifier()) {
3183 if (
auto NextNext = NextToken->getNextNonComment();
3185 (IsChainedOperatorAmpOrMember(NextNext) || NextNext->is(tok::semi))) {
3186 return TT_BinaryOperator;
3192 return TT_BinaryOperator;
3195 return TT_PointerOrReference;
3199 if (determineUnaryOperatorByUsage(
Tok))
3200 return TT_UnaryOperator;
3204 return TT_UnaryOperator;
3206 if (PrevToken->is(tok::at))
3207 return TT_UnaryOperator;
3210 return TT_BinaryOperator;
3216 if (!PrevToken || PrevToken->is(TT_CastRParen))
3217 return TT_UnaryOperator;
3218 if (PrevToken->isOneOf(tok::r_paren, tok::r_square, tok::identifier))
3219 return TT_TrailingUnaryOperator;
3221 return TT_UnaryOperator;
3224 SmallVector<Context, 8> Contexts;
3226 const FormatStyle &Style;
3227 AnnotatedLine &Line;
3231 LangOptions LangOpts;
3232 const AdditionalKeywords &Keywords;
3234 SmallVector<ScopeType> &Scopes;
3240 llvm::SmallPtrSet<FormatToken *, 16> NonTemplateLess;
3242 int TemplateDeclarationDepth;
3250class ExpressionParser {
3252 ExpressionParser(
const FormatStyle &Style,
const AdditionalKeywords &Keywords,
3253 AnnotatedLine &Line)
3254 : Style(Style), Keywords(Keywords), Line(Line), Current(Line.
First) {}
3257 void parse(
int Precedence = 0) {
3260 while (Current && (Current->is(tok::kw_return) ||
3261 (Current->is(tok::colon) &&
3262 Current->isOneOf(TT_ObjCMethodExpr, TT_DictLiteral)))) {
3266 if (!Current || Precedence > PrecedenceArrowAndPeriod)
3271 parseConditionalExpr();
3277 if (Precedence == PrecedenceUnaryOperator) {
3278 parseUnaryOperator();
3295 if (Style.isVerilog() && Precedence ==
prec::Comma) {
3296 VerilogFirstOfType =
3297 verilogGroupDecl(VerilogFirstOfType, LatestOperator);
3301 parse(Precedence + 1);
3303 int CurrentPrecedence = getCurrentPrecedence();
3312 if (Style.BreakBinaryOperations.PerOperator.empty() &&
3313 Style.BreakBinaryOperations.Default ==
3314 FormatStyle::BBO_OnePerLine) {
3319 if (Precedence == CurrentPrecedence && Current &&
3320 Current->is(TT_SelectorName)) {
3322 addFakeParenthesis(Start,
prec::Level(Precedence));
3326 if ((Style.isCSharp() || Style.isJavaScript() || Style.isJava()) &&
3330 FormatToken *Prev = Current->getPreviousNonComment();
3331 if (Prev && Prev->is(tok::string_literal) &&
3332 (Prev == Start || Prev->endsSequence(tok::string_literal, tok::plus,
3333 TT_StringInConcatenation))) {
3334 Prev->setType(TT_StringInConcatenation);
3341 (Current->closesScope() &&
3342 (Current->MatchingParen || Current->is(TT_TemplateString))) ||
3343 (CurrentPrecedence != -1 && CurrentPrecedence < Precedence) ||
3352 if (Current->opensScope() ||
3353 Current->isOneOf(TT_RequiresClause,
3354 TT_RequiresClauseInARequiresExpression)) {
3357 while (Current && (!Current->closesScope() || Current->opensScope())) {
3364 if (CurrentPrecedence == Precedence) {
3366 LatestOperator->NextOperator = Current;
3367 LatestOperator = Current;
3371 next(Precedence > 0);
3376 if (Style.isVerilog() && Precedence ==
prec::Comma && VerilogFirstOfType)
3377 addFakeParenthesis(VerilogFirstOfType,
prec::Comma);
3379 if (LatestOperator && (Current || Precedence > 0)) {
3385 Start->Previous->isOneOf(TT_RequiresClause,
3386 TT_RequiresClauseInARequiresExpression))
3388 auto Ret = Current ? Current : Line.Last;
3389 while (!
Ret->ClosesRequiresClause &&
Ret->Previous)
3395 if (Precedence == PrecedenceArrowAndPeriod) {
3399 addFakeParenthesis(Start,
prec::Level(Precedence), End);
3407 int getCurrentPrecedence() {
3409 const FormatToken *NextNonComment = Current->getNextNonComment();
3410 if (Current->is(TT_ConditionalExpr))
3412 if (NextNonComment && Current->is(TT_SelectorName) &&
3413 (NextNonComment->isOneOf(TT_DictLiteral, TT_JsTypeColon) ||
3414 (Style.isProto() && NextNonComment->is(tok::less)))) {
3417 if (Current->is(TT_JsComputedPropertyName))
3419 if (Current->is(TT_LambdaArrow))
3421 if (Current->is(TT_FatArrow))
3423 if (Current->isOneOf(tok::semi, TT_InlineASMColon, TT_SelectorName) ||
3424 (Current->is(tok::comment) && NextNonComment &&
3425 NextNonComment->is(TT_SelectorName))) {
3428 if (Current->is(TT_RangeBasedForLoopColon))
3430 if ((Style.isJava() || Style.isJavaScript()) &&
3431 Current->is(Keywords.kw_instanceof)) {
3434 if (Style.isJavaScript() &&
3435 Current->isOneOf(Keywords.kw_in, Keywords.kw_as)) {
3438 if (Current->isOneOf(TT_BinaryOperator, tok::comma))
3439 return Current->getPrecedence();
3440 if (Current->isOneOf(tok::period, tok::arrow) &&
3441 Current->isNot(TT_TrailingReturnArrow)) {
3442 return PrecedenceArrowAndPeriod;
3444 if ((Style.isJava() || Style.isJavaScript()) &&
3445 Current->isOneOf(Keywords.kw_extends, Keywords.kw_implements,
3446 Keywords.kw_throws)) {
3451 if (Style.isVerilog() && Current->is(tok::colon))
3463 if (Start->MacroParent)
3466 Start->FakeLParens.push_back(Precedence);
3468 Start->StartsBinaryExpression =
true;
3469 if (!End && Current)
3470 End = Current->getPreviousNonComment();
3474 End->EndsBinaryExpression =
true;
3480 void parseUnaryOperator() {
3481 SmallVector<FormatToken *, 2> Tokens;
3482 while (Current && Current->is(TT_UnaryOperator)) {
3483 Tokens.push_back(Current);
3486 parse(PrecedenceArrowAndPeriod);
3493 void parseConditionalExpr() {
3494 while (Current && Current->isTrailingComment())
3498 if (!Current || Current->isNot(tok::question))
3502 if (!Current || Current->isNot(TT_ConditionalExpr))
3509 void next(
bool SkipPastLeadingComments =
true) {
3511 Current = Current->Next;
3513 (Current->NewlinesBefore == 0 || SkipPastLeadingComments) &&
3514 Current->isTrailingComment()) {
3515 Current = Current->Next;
3529 while (Start->startsSequence(tok::l_paren, tok::star)) {
3530 if (!(Start = Start->MatchingParen) ||
3531 !(Start = Start->getNextNonComment())) {
3538 if (
Tok->is(Keywords.kw_assign))
3539 Tok =
Tok->getNextNonComment();
3551 if (
Tok->is(tok::hash)) {
3556 Tok =
Tok->getNextNonComment();
3557 }
else if (
Tok->is(tok::hashhash)) {
3561 Tok =
Tok->getNextNonComment();
3562 }
else if (Keywords.isVerilogQualifier(*
Tok) ||
3563 Keywords.isVerilogIdentifier(*
Tok)) {
3567 while (
Tok &&
Tok->isOneOf(tok::period, tok::coloncolon) &&
3568 (
Tok =
Tok->getNextNonComment())) {
3569 if (Keywords.isVerilogIdentifier(*
Tok))
3570 Tok =
Tok->getNextNonComment();
3574 }
else if (
Tok->is(tok::l_paren)) {
3579 Keywords.kw_highz0, Keywords.kw_highz1, Keywords.kw_large,
3580 Keywords.kw_medium, Keywords.kw_pull0, Keywords.kw_pull1,
3581 Keywords.kw_small, Keywords.kw_strong0, Keywords.kw_strong1,
3582 Keywords.kw_supply0, Keywords.kw_supply1, Keywords.kw_weak0,
3583 Keywords.kw_weak1)) {
3584 Tok->setType(TT_VerilogStrength);
3585 Tok =
Tok->MatchingParen;
3587 Tok->setType(TT_VerilogStrength);
3588 Tok =
Tok->getNextNonComment();
3593 }
else if (
Tok->is(Keywords.kw_verilogHash)) {
3595 if (
Next->is(tok::l_paren))
3598 Tok =
Next->getNextNonComment();
3607 while (
Tok &&
Tok->is(tok::l_square) && (
Tok =
Tok->MatchingParen))
3608 Tok =
Tok->getNextNonComment();
3609 if (
Tok && (
Tok->is(tok::hash) || Keywords.isVerilogIdentifier(*
Tok)))
3618 First->setType(TT_VerilogDimensionedTypeName);
3619 }
else if (
First != Start) {
3627 if (TypedName->is(TT_Unknown))
3628 TypedName->setType(TT_StartOfName);
3630 if (FirstOfType && PreviousComma) {
3631 PreviousComma->setType(TT_VerilogTypeComma);
3632 addFakeParenthesis(FirstOfType,
prec::Comma, PreviousComma->Previous);
3635 FirstOfType = TypedName;
3642 while (Current && Current != FirstOfType) {
3643 if (Current->opensScope()) {
3654 const FormatStyle &Style;
3655 const AdditionalKeywords &Keywords;
3656 const AnnotatedLine &Line;
3666 assert(
Line->First);
3670 if (
const auto Column =
Line->First->OriginalColumn;
3674 const bool PPDirectiveOrImportStmt =
3677 if (PPDirectiveOrImportStmt)
3679 if (
const auto IndentWidth = Style.IndentWidth;
3681 Column % IndentWidth == 0) {
3688 Style.IndentPPDirectives < FormatStyle::PPDIS_BeforeHash &&
3689 PPDirectiveOrImportStmt
3691 : NextNonCommentLine->
Level;
3694 NextNonCommentLine =
Line->First->isNot(tok::r_brace) ?
Line :
nullptr;
3714 if (
Tok->isNot(tok::identifier))
3717 Tok =
Tok->getNextNonComment();
3723 if (
Tok->is(tok::coloncolon))
3724 return Tok->getNextNonComment();
3728 if (
Tok->is(TT_TemplateOpener)) {
3729 Tok =
Tok->MatchingParen;
3733 Tok =
Tok->getNextNonComment();
3738 return Tok->is(tok::coloncolon) ?
Tok->getNextNonComment() :
nullptr;
3746 Tok =
Tok->getNextNonComment()) {
3748 if (
Tok->is(TT_AttributeLSquare)) {
3749 Tok =
Tok->MatchingParen;
3757 if (
Tok->is(tok::l_paren) &&
Tok->is(TT_Unknown) &&
Tok->MatchingParen) {
3765 if (
Tok->isOneOf(tok::kw_friend, tok::kw_inline, tok::kw_virtual,
3766 tok::kw_constexpr, tok::kw_consteval, tok::kw_explicit)) {
3772 if (
Tok->is(tok::kw_template)) {
3773 Tok =
Tok->getNextNonComment();
3779 if (
Tok->isNot(TT_TemplateOpener))
3782 Tok =
Tok->MatchingParen;
3790 if (
Tok->is(tok::coloncolon)) {
3801 if (
Tok->is(tok::tilde)) {
3808 if (
Tok->isNot(tok::identifier) ||
Tok->isNot(TT_Unknown))
3819 assert(
Tok &&
Tok->is(tok::identifier));
3820 const auto *Prev =
Tok->Previous;
3822 if (Prev && Prev->is(tok::tilde))
3823 Prev = Prev->Previous;
3826 if (!Prev || (!Prev->endsSequence(tok::coloncolon, tok::identifier) &&
3827 !Prev->endsSequence(tok::coloncolon, TT_TemplateCloser))) {
3831 assert(Prev->Previous);
3832 if (Prev->Previous->is(TT_TemplateCloser) && Prev->Previous->MatchingParen) {
3833 Prev = Prev->Previous->MatchingParen;
3834 assert(Prev->Previous);
3837 return Prev->Previous->TokenText ==
Tok->TokenText;
3841 if (!
Line.InMacroBody)
3842 MacroBodyScopes.clear();
3844 auto &ScopeStack =
Line.InMacroBody ? MacroBodyScopes : Scopes;
3845 AnnotatingParser
Parser(Style,
Line, Keywords, ScopeStack);
3848 if (!
Line.Children.empty()) {
3851 for (
auto &Child :
Line.Children) {
3852 if (InRequiresExpression &&
3853 Child->First->isNoneOf(tok::kw_typename, tok::kw_requires,
3854 TT_CompoundRequirementLBrace)) {
3860 if (!ScopeStack.empty())
3861 ScopeStack.pop_back();
3874 ExpressionParser ExprParser(Style, Keywords,
Line);
3880 if (
Tok && ((!ScopeStack.empty() && ScopeStack.back() ==
ST_Class) ||
3882 Tok->setFinalizedType(TT_CtorDtorDeclName);
3883 assert(OpeningParen);
3888 if (
Line.startsWith(TT_ObjCMethodSpecifier))
3890 else if (
Line.startsWith(TT_ObjCDecl))
3892 else if (
Line.startsWith(TT_ObjCProperty))
3896 First->SpacesRequiredBefore = 1;
3897 First->CanBreakBefore =
First->MustBreakBefore;
3906 if (Current.
is(TT_FunctionDeclarationName))
3909 if (Current.
isNoneOf(tok::identifier, tok::kw_operator))
3912 const auto *Prev = Current.getPreviousNonComment();
3917 if (
const auto *PrevPrev =
Previous.getPreviousNonComment();
3918 PrevPrev && PrevPrev->is(TT_ObjCDecl)) {
3922 auto skipOperatorName =
3925 if (
Next->is(TT_OverloadedOperatorLParen))
3927 if (
Next->is(TT_OverloadedOperator))
3929 if (
Next->isPlacementOperator() ||
Next->is(tok::kw_co_await)) {
3932 Next->Next->startsSequence(tok::l_square, tok::r_square)) {
3937 if (
Next->startsSequence(tok::l_square, tok::r_square)) {
3942 if ((
Next->isTypeName(LangOpts) ||
Next->is(tok::identifier)) &&
3943 Next->Next &&
Next->Next->isPointerOrReference()) {
3948 if (
Next->is(TT_TemplateOpener) &&
Next->MatchingParen) {
3959 const bool IsCpp = LangOpts.CXXOperatorNames || LangOpts.C11;
3962 if (Current.
is(tok::kw_operator)) {
3963 if (
Line.startsWith(tok::kw_friend))
3965 if (
Previous.Tok.getIdentifierInfo() &&
3966 Previous.isNoneOf(tok::kw_return, tok::kw_co_return)) {
3971 assert(
Previous.MatchingParen->is(tok::l_paren));
3972 assert(
Previous.MatchingParen->is(TT_TypeDeclarationParen));
3981 while (
Next &&
Next->startsSequence(tok::hashhash, tok::identifier))
3984 if (
Next->is(TT_TemplateOpener) &&
Next->MatchingParen) {
3986 }
else if (
Next->is(tok::coloncolon)) {
3990 if (
Next->is(tok::kw_operator)) {
3991 Next = skipOperatorName(
Next->Next);
3994 if (
Next->isNot(tok::identifier))
3996 }
else if (isCppAttribute(IsCpp, *
Next)) {
4000 }
else if (
Next->is(tok::l_paren)) {
4009 if (!
Next ||
Next->isNot(tok::l_paren) || !
Next->MatchingParen)
4011 ClosingParen =
Next->MatchingParen;
4012 assert(ClosingParen->
is(tok::r_paren));
4014 if (
Line.Last->is(tok::l_brace))
4016 if (
Next->Next == ClosingParen)
4019 if (ClosingParen->
Next && ClosingParen->
Next->
is(TT_PointerOrReference))
4032 if (IsCpp &&
Next->Next &&
Next->Next->is(tok::identifier) &&
4033 !
Line.endsWith(tok::semi)) {
4039 if (
Tok->is(TT_TypeDeclarationParen))
4041 if (
Tok->isOneOf(tok::l_paren, TT_TemplateOpener) &&
Tok->MatchingParen) {
4042 Tok =
Tok->MatchingParen;
4045 if (
Tok->is(tok::kw_const) ||
Tok->isTypeName(LangOpts) ||
4046 Tok->isOneOf(TT_PointerOrReference, TT_StartOfName, tok::ellipsis)) {
4049 if (
Tok->isOneOf(tok::l_brace, TT_ObjCMethodExpr) ||
Tok->Tok.isLiteral())
4055bool TokenAnnotator::mustBreakForReturnType(
const AnnotatedLine &
Line)
const {
4056 assert(
Line.MightBeFunctionDecl);
4058 if ((Style.BreakAfterReturnType == FormatStyle::RTBS_TopLevel ||
4059 Style.BreakAfterReturnType == FormatStyle::RTBS_TopLevelDefinitions) &&
4064 switch (Style.BreakAfterReturnType) {
4065 case FormatStyle::RTBS_None:
4066 case FormatStyle::RTBS_Automatic:
4067 case FormatStyle::RTBS_ExceptShortType:
4069 case FormatStyle::RTBS_All:
4070 case FormatStyle::RTBS_TopLevel:
4072 case FormatStyle::RTBS_AllDefinitions:
4073 case FormatStyle::RTBS_TopLevelDefinitions:
4074 return Line.mightBeFunctionDefinition();
4080bool TokenAnnotator::mustBreakBeforeReturnType(
4082 assert(
Line.MightBeFunctionDecl);
4084 switch (Style.BreakBeforeReturnType) {
4085 case FormatStyle::BBRTS_None:
4087 case FormatStyle::BBRTS_All:
4089 case FormatStyle::BBRTS_TopLevel:
4090 return Line.Level == 0;
4091 case FormatStyle::BBRTS_AllDefinitions:
4092 return Line.mightBeFunctionDefinition();
4093 case FormatStyle::BBRTS_TopLevelDefinitions:
4094 return Line.Level == 0 &&
Line.mightBeFunctionDefinition();
4101 auto *
Tok =
Line.getFirstNonComment();
4105 if (
Tok->is(tok::kw_template)) {
4106 auto *Opener =
Tok->Next;
4107 while (Opener && Opener->isNot(TT_TemplateOpener))
4108 Opener = Opener->Next;
4109 if (!Opener || !Opener->MatchingParen)
4111 Tok = Opener->MatchingParen->Next;
4114 if (
Tok &&
Tok->is(TT_RequiresClause)) {
4115 while (
Tok && !
Tok->ClosesRequiresClause)
4123 Tok->isOneOf(tok::kw___attribute, tok::kw___declspec,
4124 TT_AttributeMacro)) {
4126 if (
Next &&
Next->is(tok::l_paren) &&
Next->MatchingParen)
4127 Tok =
Next->MatchingParen->Next;
4132 if (
Tok->is(TT_AttributeLSquare) &&
Tok->MatchingParen) {
4133 Tok =
Tok->MatchingParen->Next;
4145 Line.Computed =
true;
4153 :
Line.FirstStartColumn +
First->ColumnWidth;
4154 bool AlignArrayOfStructures =
4155 (Style.AlignArrayOfStructures != FormatStyle::AIAS_None &&
4157 if (AlignArrayOfStructures)
4158 calculateArrayInitializerColumnList(
Line);
4160 const auto *FirstNonComment =
Line.getFirstNonComment();
4161 bool SeenName =
false;
4162 bool LineIsFunctionDeclaration =
false;
4166 for (
auto *
Tok = FirstNonComment && FirstNonComment->isNot(tok::kw_using)
4167 ? FirstNonComment->Next
4170 if (
Tok->is(TT_StartOfName))
4172 if (
Tok->Previous->EndsCppAttributeGroup)
4173 AfterLastAttribute =
Tok;
4174 if (
const bool IsCtorOrDtor =
Tok->is(TT_CtorDtorDeclName);
4178 Tok->setFinalizedType(TT_FunctionDeclarationName);
4179 LineIsFunctionDeclaration =
true;
4183 assert(OpeningParen);
4184 if (OpeningParen->is(TT_Unknown))
4185 OpeningParen->setType(TT_FunctionDeclarationLParen);
4192 if ((LineIsFunctionDeclaration ||
4193 (FirstNonComment && FirstNonComment->is(TT_CtorDtorDeclName))) &&
4194 Line.endsWith(tok::semi, tok::r_brace)) {
4195 auto *
Tok =
Line.Last->Previous;
4196 while (
Tok->isNot(tok::r_brace))
4198 if (
auto *LBrace =
Tok->MatchingParen; LBrace && LBrace->is(TT_Unknown)) {
4199 assert(LBrace->is(tok::l_brace));
4202 LBrace->setFinalizedType(TT_FunctionLBrace);
4206 if (SeenName && AfterLastAttribute &&
4209 if (LineIsFunctionDeclaration)
4210 Line.ReturnTypeWrapped =
true;
4213 if (!LineIsFunctionDeclaration) {
4214 Line.ReturnTypeWrapped =
false;
4216 for (
const auto *
Tok = FirstNonComment;
Tok;
Tok =
Tok->Next) {
4217 if (
Tok->isNot(tok::kw_operator))
4221 }
while (
Tok &&
Tok->isNot(TT_OverloadedOperatorLParen));
4222 if (!
Tok || !
Tok->MatchingParen)
4224 const auto *LeftParen =
Tok;
4225 for (
Tok =
Tok->Next;
Tok &&
Tok != LeftParen->MatchingParen;
4227 if (
Tok->isNot(tok::identifier))
4230 const bool NextIsBinaryOperator =
4232 Next->Next->is(tok::identifier);
4233 if (!NextIsBinaryOperator)
4235 Next->setType(TT_BinaryOperator);
4239 }
else if (ClosingParen) {
4241 if (
Tok->is(TT_CtorInitializerColon))
4243 if (
Tok->is(tok::arrow)) {
4244 Tok->overwriteFixedType(TT_TrailingReturnArrow);
4247 if (
Tok->isNot(TT_TrailingAnnotation))
4250 if (!
Next ||
Next->isNot(tok::l_paren))
4259 if (
Line.MightBeFunctionDecl && LineIsFunctionDeclaration &&
4260 mustBreakBeforeReturnType(
Line)) {
4262 ReturnTypeStart && ReturnTypeStart != FirstNonComment &&
4263 ReturnTypeStart->
isNoneOf(TT_FunctionDeclarationName,
4264 TT_CtorDtorDeclName, tok::tilde)) {
4265 ReturnTypeStart->MustBreakBefore =
true;
4266 Line.ReturnTypeWrapped =
true;
4270 if (
First->is(TT_ElseLBrace)) {
4271 First->CanBreakBefore =
true;
4272 First->MustBreakBefore =
true;
4275 bool InFunctionDecl =
Line.MightBeFunctionDecl;
4276 bool InParameterList =
false;
4277 for (
auto *Current =
First->Next; Current; Current = Current->Next) {
4279 if (Current->is(TT_LineComment)) {
4281 Current->SpacesRequiredBefore =
4282 (Style.Cpp11BracedListStyle == FormatStyle::BLS_AlignFirstComment &&
4283 !Style.SpacesInParensOptions.Other)
4286 }
else if (Prev->
is(TT_VerilogMultiLineListLParen)) {
4287 Current->SpacesRequiredBefore = 0;
4289 Current->SpacesRequiredBefore = Style.SpacesBeforeTrailingComments;
4299 if (!Current->HasUnescapedNewline) {
4302 if (
Parameter->isOneOf(tok::comment, tok::r_brace))
4305 if (
Parameter->Previous->isNot(TT_CtorInitializerComma) &&
4313 }
else if (!Current->Finalized && Current->SpacesRequiredBefore == 0 &&
4314 spaceRequiredBefore(
Line, *Current)) {
4315 Current->SpacesRequiredBefore = 1;
4320 Current->MustBreakBefore =
true;
4322 Current->MustBreakBefore =
4323 Current->MustBreakBefore || mustBreakBefore(
Line, *Current);
4324 if (!Current->MustBreakBefore && InFunctionDecl &&
4325 Current->is(TT_FunctionDeclarationName)) {
4326 Current->MustBreakBefore = mustBreakForReturnType(
Line);
4330 Current->CanBreakBefore =
4331 !
Line.IsModuleOrImportDecl &&
4332 (Current->MustBreakBefore || canBreakBefore(
Line, *Current));
4334 if (Current->is(TT_FunctionDeclarationLParen)) {
4335 InParameterList =
true;
4336 }
else if (Current->is(tok::r_paren)) {
4337 const auto *LParen = Current->MatchingParen;
4338 if (LParen && LParen->is(TT_FunctionDeclarationLParen))
4339 InParameterList =
false;
4340 }
else if (InParameterList &&
4341 Current->endsSequence(TT_AttributeMacro,
4342 TT_PointerOrReference)) {
4343 Current->CanBreakBefore =
false;
4346 unsigned ChildSize = 0;
4349 ChildSize = LastOfChild.isTrailingComment() ? Style.ColumnLimit
4352 if (Current->MustBreakBefore || Prev->
Children.size() > 1 ||
4354 Prev->
Children[0]->First->MustBreakBefore) ||
4355 Current->IsMultiline) {
4356 Current->TotalLength = Prev->
TotalLength + Style.ColumnLimit;
4358 Current->TotalLength = Prev->
TotalLength + Current->ColumnWidth +
4359 ChildSize + Current->SpacesRequiredBefore;
4362 if ((Style.PackParameters.BinPack == FormatStyle::BPPS_UseBreakAfter &&
4365 (Style.PackArguments.BinPack == FormatStyle::BPAS_UseBreakAfter &&
4367 Prev->
isOneOf(tok::l_paren, tok::l_brace,
4368 TT_ArrayInitializerLSquare) &&
4371 for (
auto *ParamTok = Current; ParamTok && ParamTok != RParen;
4372 ParamTok = ParamTok->Next) {
4373 if (ParamTok->opensScope()) {
4374 ParamTok = ParamTok->MatchingParen;
4379 ParamTok->MustBreakBefore =
true;
4380 ParamTok->CanBreakBefore =
true;
4385 if (Current->is(TT_ControlStatementLBrace)) {
4386 if (Style.ColumnLimit > 0 &&
4387 Style.BraceWrapping.AfterControlStatement ==
4388 FormatStyle::BWACS_MultiLine &&
4389 Line.Level * Style.IndentWidth +
Line.Last->TotalLength >
4390 Style.ColumnLimit) {
4391 Current->CanBreakBefore =
true;
4392 Current->MustBreakBefore =
true;
4394 }
else if (Current->is(TT_CtorInitializerColon)) {
4395 InFunctionDecl =
false;
4407 Current->SplitPenalty = splitPenalty(
Line, *Current, InFunctionDecl);
4408 if (Style.Language == FormatStyle::LK_ObjC &&
4409 Current->is(TT_SelectorName) && Current->ParameterIndex > 0) {
4410 if (Current->ParameterIndex == 1)
4411 Current->SplitPenalty += 5 * Current->BindingStrength;
4413 Current->SplitPenalty += 20 * Current->BindingStrength;
4417 calculateUnbreakableTailLengths(
Line);
4419 for (
auto *Current =
First; Current; Current = Current->Next) {
4421 Current->Role->precomputeFormattingInfos(Current);
4422 if (Current->MatchingParen &&
4423 Current->MatchingParen->opensBlockOrBlockTypeList(Style) &&
4428 if (Current->opensBlockOrBlockTypeList(Style))
4432 LLVM_DEBUG({ printDebugInfo(
Line); });
4435void TokenAnnotator::calculateUnbreakableTailLengths(
4442 Current->
isOneOf(tok::comment, tok::string_literal)) {
4452void TokenAnnotator::calculateArrayInitializerColumnList(
4456 auto *CurrentToken =
Line.First;
4457 CurrentToken->ArrayInitializerLineStart =
true;
4459 while (CurrentToken && CurrentToken !=
Line.Last) {
4460 if (CurrentToken->is(tok::l_brace)) {
4461 CurrentToken->IsArrayInitializer =
true;
4462 if (CurrentToken->Next)
4463 CurrentToken->Next->MustBreakBefore =
true;
4465 calculateInitializerColumnList(
Line, CurrentToken->Next, Depth + 1);
4467 CurrentToken = CurrentToken->Next;
4472FormatToken *TokenAnnotator::calculateInitializerColumnList(
4474 while (CurrentToken && CurrentToken !=
Line.Last) {
4475 if (CurrentToken->is(tok::l_brace))
4477 else if (CurrentToken->is(tok::r_brace))
4479 if (Depth == 2 && CurrentToken->isOneOf(tok::l_brace, tok::comma)) {
4480 CurrentToken = CurrentToken->Next;
4483 CurrentToken->StartsColumn =
true;
4484 CurrentToken = CurrentToken->Previous;
4486 CurrentToken = CurrentToken->Next;
4488 return CurrentToken;
4493 bool InFunctionDecl)
const {
4497 if (
Left.is(tok::semi))
4501 if (Style.isJava()) {
4502 if (
Right.isOneOf(Keywords.kw_extends, Keywords.kw_throws))
4504 if (
Right.is(Keywords.kw_implements))
4506 if (
Left.is(tok::comma) &&
Left.NestingLevel == 0)
4508 }
else if (Style.isJavaScript()) {
4509 if (
Right.is(Keywords.kw_function) &&
Left.isNot(tok::comma))
4511 if (
Left.is(TT_JsTypeColon))
4513 if ((
Left.is(TT_TemplateString) &&
Left.TokenText.ends_with(
"${")) ||
4514 (
Right.is(TT_TemplateString) &&
Right.TokenText.starts_with(
"}"))) {
4518 if (
Left.opensScope() &&
Right.closesScope())
4520 }
else if (Style.Language == FormatStyle::LK_Proto) {
4521 if (
Right.is(tok::l_square))
4523 if (
Right.is(tok::period))
4527 if (
Right.is(tok::identifier) &&
Right.Next &&
Right.Next->is(TT_DictLiteral))
4529 if (
Right.is(tok::l_square)) {
4530 if (
Left.is(tok::r_square))
4533 if (
Right.is(TT_LambdaLSquare) &&
Left.is(tok::equal))
4535 if (
Right.isNoneOf(TT_ObjCMethodExpr, TT_LambdaLSquare,
4536 TT_ArrayInitializerLSquare,
4537 TT_DesignatedInitializerLSquare, TT_AttributeLSquare)) {
4542 if (
Left.is(tok::coloncolon))
4543 return Style.PenaltyBreakScopeResolution;
4544 if (
Right.isOneOf(TT_StartOfName, TT_FunctionDeclarationName,
4545 tok::kw_operator)) {
4546 if (
Line.startsWith(tok::kw_for) &&
Right.PartOfMultiVariableDeclStmt)
4548 if (
Left.is(TT_StartOfName))
4550 if (InFunctionDecl &&
Right.NestingLevel == 0)
4551 return Style.PenaltyReturnTypeOnItsOwnLine;
4554 if (
Right.is(TT_PointerOrReference))
4556 if (
Right.is(TT_LambdaArrow))
4558 if (
Left.is(tok::equal) &&
Right.is(tok::l_brace))
4560 if (
Left.is(TT_CastRParen))
4562 if (
Left.isOneOf(tok::kw_class, tok::kw_struct, tok::kw_union))
4564 if (
Left.is(tok::comment))
4567 if (
Left.isOneOf(TT_RangeBasedForLoopColon, TT_InheritanceColon,
4568 TT_CtorInitializerColon)) {
4572 if (
Right.isMemberAccess()) {
4593 const auto Penalty = Style.PenaltyBreakBeforeMemberAccess;
4595 ? std::min(Penalty, 35u)
4599 if (
Right.is(TT_TrailingAnnotation) &&
4600 (!
Right.Next ||
Right.Next->isNot(tok::l_paren))) {
4603 if (
Line.startsWith(TT_ObjCMethodSpecifier))
4610 bool is_short_annotation =
Right.TokenText.size() < 10;
4611 return (
Left.is(tok::r_paren) ? 100 : 120) + (is_short_annotation ? 50 : 0);
4615 if (
Line.startsWith(tok::kw_for) &&
Left.is(tok::equal))
4620 if (
Right.is(TT_SelectorName))
4622 if (
Left.is(tok::colon)) {
4623 if (
Left.is(TT_ObjCMethodExpr))
4624 return Line.MightBeFunctionDecl ? 50 : 500;
4625 if (
Left.is(TT_ObjCSelector))
4633 Left.Previous->isOneOf(tok::identifier, tok::greater)) {
4637 if (
Left.is(tok::l_paren) && Style.PenaltyBreakOpenParenthesis != 0)
4638 return Style.PenaltyBreakOpenParenthesis;
4639 if (
Left.is(tok::l_paren) && InFunctionDecl && Style.AlignAfterOpenBracket)
4641 if (
Left.is(tok::l_paren) &&
Left.Previous &&
4642 (
Left.Previous->isOneOf(tok::kw_for, tok::kw__Generic) ||
4643 Left.Previous->isIf())) {
4646 if (
Left.is(tok::equal) && InFunctionDecl)
4648 if (
Right.is(tok::r_brace))
4650 if (
Left.is(TT_TemplateOpener))
4652 if (
Left.opensScope()) {
4656 if (!Style.AlignAfterOpenBracket &&
4657 (
Left.ParameterCount <= 1 || Style.AllowAllArgumentsOnNextLine)) {
4660 if (
Left.is(tok::l_brace) &&
4661 Style.Cpp11BracedListStyle == FormatStyle::BLS_Block) {
4664 return Left.ParameterCount > 1 ? Style.PenaltyBreakBeforeFirstCallParameter
4667 if (
Left.is(TT_JavaAnnotation))
4670 if (
Left.is(TT_UnaryOperator))
4672 if (
Left.isOneOf(tok::plus, tok::comma) &&
Left.Previous &&
4673 Left.Previous->isLabelString() &&
4674 (
Left.NextOperator ||
Left.OperatorIndex != 0)) {
4677 if (
Right.is(tok::plus) &&
Left.isLabelString() &&
4678 (
Right.NextOperator ||
Right.OperatorIndex != 0)) {
4681 if (
Left.is(tok::comma))
4683 if (
Right.is(tok::lessless) &&
Left.isLabelString() &&
4684 (
Right.NextOperator ||
Right.OperatorIndex != 1)) {
4687 if (
Right.is(tok::lessless)) {
4689 if (
Left.isNot(tok::r_paren) ||
Right.OperatorIndex > 0) {
4695 if (
Left.ClosesTemplateDeclaration)
4696 return Style.PenaltyBreakTemplateDeclaration;
4697 if (
Left.ClosesRequiresClause)
4699 if (
Left.is(TT_ConditionalExpr))
4705 return Style.PenaltyBreakAssignment;
4712bool TokenAnnotator::spaceRequiredBeforeParens(
const FormatToken &Right)
const {
4713 if (Style.SpaceBeforeParens == FormatStyle::SBPO_Always)
4715 if (
Right.is(TT_OverloadedOperatorLParen) &&
4716 Style.SpaceBeforeParensOptions.AfterOverloadedOperator) {
4719 if (Style.SpaceBeforeParensOptions.BeforeNonEmptyParentheses &&
4720 Right.ParameterCount > 0) {
4729 if (
Left.is(tok::kw_return) &&
4730 Right.isNoneOf(tok::semi, tok::r_paren, tok::hashhash)) {
4733 if (
Left.is(tok::kw_throw) &&
Right.is(tok::l_paren) &&
Right.MatchingParen &&
4734 Right.MatchingParen->is(TT_CastRParen)) {
4737 if (
Left.is(Keywords.kw_assert) && Style.isJava())
4740 Left.is(tok::objc_property)) {
4743 if (
Right.is(tok::hashhash))
4744 return Left.is(tok::hash);
4745 if (
Left.isOneOf(tok::hashhash, tok::hash))
4746 return Right.is(tok::hash);
4747 if (Style.SpacesInParens == FormatStyle::SIPO_Custom) {
4748 if (
Left.is(tok::l_paren) &&
Right.is(tok::r_paren))
4749 return Style.SpacesInParensOptions.InEmptyParentheses;
4750 if (Style.SpacesInParensOptions.ExceptDoubleParentheses &&
4751 Left.is(tok::r_paren) &&
Right.is(tok::r_paren)) {
4752 auto *InnerLParen =
Left.MatchingParen;
4753 if (InnerLParen && InnerLParen->Previous ==
Right.MatchingParen) {
4754 InnerLParen->SpacesRequiredBefore = 0;
4759 if (
Left.is(tok::l_paren))
4761 else if (
Right.is(tok::r_paren) &&
Right.MatchingParen)
4762 LeftParen =
Right.MatchingParen;
4763 if (LeftParen && (LeftParen->is(TT_ConditionLParen) ||
4764 (LeftParen->Previous &&
4765 isKeywordWithCondition(*LeftParen->Previous)))) {
4766 return Style.SpacesInParensOptions.InConditionalStatements;
4771 if (
Left.is(tok::kw_auto) &&
Right.isOneOf(TT_LambdaLBrace, TT_FunctionLBrace,
4773 TT_FunctionTypeLParen)) {
4778 if (
Left.is(tok::kw_auto) &&
Right.isOneOf(tok::l_paren, tok::l_brace))
4781 const auto *BeforeLeft =
Left.Previous;
4784 if (
Right.is(tok::l_paren) &&
Left.is(tok::kw_co_await) && BeforeLeft &&
4785 BeforeLeft->is(tok::kw_operator)) {
4789 if (
Left.isOneOf(tok::kw_co_await, tok::kw_co_yield, tok::kw_co_return) &&
4790 Right.isNoneOf(tok::semi, tok::r_paren)) {
4794 if (
Left.is(tok::l_paren) ||
Right.is(tok::r_paren)) {
4795 return (
Right.is(TT_CastRParen) ||
4796 (
Left.MatchingParen &&
Left.MatchingParen->is(TT_CastRParen)))
4797 ? Style.SpacesInParensOptions.InCStyleCasts
4798 : Style.SpacesInParensOptions.Other;
4800 if (
Right.isOneOf(tok::semi, tok::comma))
4803 bool IsLightweightGeneric =
Right.MatchingParen &&
4804 Right.MatchingParen->Next &&
4805 Right.MatchingParen->Next->is(tok::colon);
4806 return !IsLightweightGeneric && Style.ObjCSpaceBeforeProtocolList;
4808 if (
Right.is(tok::less) &&
Left.is(tok::kw_template))
4809 return Style.SpaceAfterTemplateKeyword;
4810 if (
Left.isOneOf(tok::exclaim, tok::tilde))
4812 if (
Left.is(tok::at) &&
4813 Right.isOneOf(tok::identifier, tok::string_literal, tok::char_constant,
4814 tok::numeric_constant, tok::l_paren, tok::l_brace,
4815 tok::kw_true, tok::kw_false)) {
4818 if (
Left.is(tok::colon))
4819 return Left.isNoneOf(TT_ObjCSelector, TT_ObjCMethodExpr);
4820 if (
Left.is(tok::coloncolon))
4822 if (
Left.is(tok::less) ||
Right.isOneOf(tok::greater, tok::less)) {
4823 if (Style.isTextProto() ||
4824 (Style.Language == FormatStyle::LK_Proto &&
4825 (
Left.is(TT_DictLiteral) ||
Right.is(TT_DictLiteral)))) {
4827 if (
Left.is(tok::less) &&
Right.is(tok::greater))
4829 return Style.Cpp11BracedListStyle == FormatStyle::BLS_Block;
4832 if (
Right.isNot(TT_OverloadedOperatorLParen))
4835 if (
Right.is(tok::ellipsis)) {
4836 return Left.Tok.isLiteral() || (
Left.is(tok::identifier) && BeforeLeft &&
4837 BeforeLeft->is(tok::kw_case));
4839 if (
Left.is(tok::l_square) &&
Right.is(tok::amp))
4840 return Style.SpacesInSquareBrackets;
4841 if (
Right.is(TT_PointerOrReference)) {
4842 if (
Left.is(tok::r_paren) &&
Line.MightBeFunctionDecl) {
4843 if (!
Left.MatchingParen)
4846 Left.MatchingParen->getPreviousNonComment();
4847 if (!TokenBeforeMatchingParen ||
Left.isNot(TT_TypeDeclarationParen))
4853 if ((Style.SpaceAroundPointerQualifiers == FormatStyle::SAPQ_After ||
4854 Style.SpaceAroundPointerQualifiers == FormatStyle::SAPQ_Both) &&
4855 (
Left.is(TT_AttributeRParen) ||
4856 Left.canBePointerOrReferenceQualifier())) {
4859 if (
Left.Tok.isLiteral())
4862 if (
Left.isTypeOrIdentifier(LangOpts) &&
Right.Next &&
Right.Next->Next &&
4863 Right.Next->Next->is(TT_RangeBasedForLoopColon)) {
4864 return getTokenPointerOrReferenceAlignment(Right) !=
4865 FormatStyle::PAS_Left;
4867 return Left.isNoneOf(TT_PointerOrReference, tok::l_paren) &&
4868 (getTokenPointerOrReferenceAlignment(Right) !=
4869 FormatStyle::PAS_Left ||
4870 (
Line.IsMultiVariableDeclStmt &&
4871 (
Left.NestingLevel == 0 ||
4872 (
Left.NestingLevel == 1 && startsWithInitStatement(
Line)))));
4874 if (
Right.is(TT_FunctionTypeLParen) &&
Left.isNot(tok::l_paren) &&
4875 (
Left.isNot(TT_PointerOrReference) ||
4876 (getTokenPointerOrReferenceAlignment(Left) != FormatStyle::PAS_Right &&
4877 !
Line.IsMultiVariableDeclStmt))) {
4880 if (
Left.is(TT_PointerOrReference)) {
4883 if ((Style.SpaceAroundPointerQualifiers == FormatStyle::SAPQ_Before ||
4884 Style.SpaceAroundPointerQualifiers == FormatStyle::SAPQ_Both) &&
4885 Right.canBePointerOrReferenceQualifier()) {
4889 if (
Right.Tok.isLiteral())
4892 if (
Right.is(TT_BlockComment))
4896 if (
Right.isOneOf(Keywords.kw_override, Keywords.kw_final, tok::kw_noexcept,
4897 TT_RequiresClause) &&
4898 Right.isNot(TT_StartOfName)) {
4905 if (BeforeLeft && BeforeLeft->isTypeOrIdentifier(LangOpts) &&
Right.Next &&
4906 Right.Next->is(TT_RangeBasedForLoopColon)) {
4907 return getTokenPointerOrReferenceAlignment(Left) !=
4908 FormatStyle::PAS_Right;
4910 if (
Right.isOneOf(TT_PointerOrReference, TT_ArraySubscriptLSquare,
4914 if (getTokenPointerOrReferenceAlignment(Left) == FormatStyle::PAS_Right)
4922 if (
Line.IsMultiVariableDeclStmt &&
4923 (
Left.NestingLevel ==
Line.First->NestingLevel ||
4924 ((
Left.NestingLevel ==
Line.First->NestingLevel + 1) &&
4925 startsWithInitStatement(
Line)))) {
4930 if (BeforeLeft->is(tok::coloncolon)) {
4931 if (
Left.isNot(tok::star))
4933 assert(Style.PointerAlignment != FormatStyle::PAS_Right);
4934 if (!
Right.startsSequence(tok::identifier, tok::r_paren))
4937 const auto *LParen =
Right.Next->MatchingParen;
4938 return !LParen || LParen->isNot(TT_FunctionTypeLParen);
4940 return BeforeLeft->isNoneOf(tok::l_paren, tok::l_square);
4943 if (
Left.is(tok::ellipsis) && BeforeLeft &&
4944 BeforeLeft->isPointerOrReference()) {
4945 return Style.PointerAlignment != FormatStyle::PAS_Right;
4948 if (
Right.is(tok::star) &&
Left.is(tok::l_paren))
4950 if (
Left.is(tok::star) &&
Right.isPointerOrReference())
4952 if (
Right.isPointerOrReference()) {
4963 if (
Previous->is(tok::coloncolon)) {
4982 if (
Previous->endsSequence(tok::kw_operator))
4983 return Style.PointerAlignment != FormatStyle::PAS_Left;
4984 if (
Previous->isOneOf(tok::kw_const, tok::kw_volatile)) {
4985 return (Style.PointerAlignment != FormatStyle::PAS_Left) ||
4986 (Style.SpaceAroundPointerQualifiers ==
4987 FormatStyle::SAPQ_After) ||
4988 (Style.SpaceAroundPointerQualifiers == FormatStyle::SAPQ_Both);
4992 if (Style.isCSharp() &&
Left.is(Keywords.kw_is) &&
Right.is(tok::l_square))
4994 const auto SpaceRequiredForArrayInitializerLSquare =
4995 [](
const FormatToken &LSquareTok,
const FormatStyle &Style) {
4996 return Style.SpacesInContainerLiterals ||
4998 Style.Cpp11BracedListStyle == FormatStyle::BLS_Block &&
4999 LSquareTok.endsSequence(tok::l_square, tok::colon,
5002 if (
Left.is(tok::l_square)) {
5003 return (
Left.is(TT_ArrayInitializerLSquare) &&
Right.isNot(tok::r_square) &&
5004 SpaceRequiredForArrayInitializerLSquare(Left, Style)) ||
5005 (
Left.isOneOf(TT_ArraySubscriptLSquare, TT_StructuredBindingLSquare,
5006 TT_LambdaLSquare) &&
5007 Style.SpacesInSquareBrackets &&
Right.isNot(tok::r_square));
5009 if (
Right.is(tok::r_square)) {
5010 return Right.MatchingParen &&
5011 ((
Right.MatchingParen->is(TT_ArrayInitializerLSquare) &&
5012 SpaceRequiredForArrayInitializerLSquare(*
Right.MatchingParen,
5014 (Style.SpacesInSquareBrackets &&
5015 Right.MatchingParen->isOneOf(TT_ArraySubscriptLSquare,
5016 TT_StructuredBindingLSquare,
5017 TT_LambdaLSquare)));
5019 if (
Right.is(tok::l_square) &&
5020 Right.isNoneOf(TT_ObjCMethodExpr, TT_LambdaLSquare,
5021 TT_DesignatedInitializerLSquare,
5022 TT_StructuredBindingLSquare, TT_AttributeLSquare) &&
5023 Left.isNoneOf(tok::numeric_constant, TT_DictLiteral) &&
5024 !(
Left.isNot(tok::r_square) && Style.SpaceBeforeSquareBrackets &&
5025 Right.is(TT_ArraySubscriptLSquare))) {
5029 (
Right.is(tok::r_brace) &&
Right.MatchingParen &&
5031 return Style.Cpp11BracedListStyle == FormatStyle::BLS_Block ||
5032 Style.SpacesInParensOptions.Other;
5034 if (
Left.is(TT_BlockComment)) {
5036 return Style.isJavaScript() || !
Left.TokenText.ends_with(
"=*/");
5041 if (
Left.is(TT_TemplateCloser) &&
Right.is(TT_AttributeLSquare))
5044 if (
Right.is(tok::l_paren)) {
5046 if (
Line.MightBeFunctionDecl &&
Right.is(TT_FunctionDeclarationLParen)) {
5047 if (spaceRequiredBeforeParens(Right))
5049 const auto &Options = Style.SpaceBeforeParensOptions;
5050 return Line.mightBeFunctionDefinition()
5051 ? Options.AfterFunctionDefinitionName
5052 : Options.AfterFunctionDeclarationName;
5054 if (
Left.is(TT_TemplateCloser) &&
Right.isNot(TT_FunctionTypeLParen))
5055 return spaceRequiredBeforeParens(Right);
5056 if (
Left.isOneOf(TT_RequiresClause,
5057 TT_RequiresClauseInARequiresExpression)) {
5058 return Style.SpaceBeforeParensOptions.AfterRequiresInClause ||
5059 spaceRequiredBeforeParens(Right);
5061 if (
Left.is(TT_RequiresExpression)) {
5062 return Style.SpaceBeforeParensOptions.AfterRequiresInExpression ||
5063 spaceRequiredBeforeParens(Right);
5065 if (
Left.isOneOf(TT_AttributeRParen, TT_AttributeRSquare))
5067 if (
Left.is(TT_ForEachMacro)) {
5068 return Style.SpaceBeforeParensOptions.AfterForeachMacros ||
5069 spaceRequiredBeforeParens(Right);
5071 if (
Left.is(TT_IfMacro)) {
5072 return Style.SpaceBeforeParensOptions.AfterIfMacros ||
5073 spaceRequiredBeforeParens(Right);
5075 if (Style.SpaceBeforeParens == FormatStyle::SBPO_Custom &&
5076 Left.isPlacementOperator() &&
5077 Right.isNot(TT_OverloadedOperatorLParen) &&
5078 !(
Line.MightBeFunctionDecl &&
Left.is(TT_FunctionDeclarationName))) {
5079 const auto *RParen =
Right.MatchingParen;
5080 return Style.SpaceBeforeParensOptions.AfterPlacementOperator ||
5081 (RParen && RParen->is(TT_CastRParen));
5085 if (
Left.is(tok::semi))
5087 if (
Left.isOneOf(tok::pp_elif, tok::kw_for, tok::kw_while, tok::kw_switch,
5088 tok::kw_case, TT_ForEachMacro, TT_ObjCForIn) ||
5090 Right.is(TT_ConditionLParen)) {
5091 return Style.SpaceBeforeParensOptions.AfterControlStatements ||
5092 spaceRequiredBeforeParens(Right);
5097 if (
Right.is(TT_OverloadedOperatorLParen))
5098 return spaceRequiredBeforeParens(Right);
5102 Left.MatchingParen &&
Left.MatchingParen->is(TT_LambdaLSquare)) {
5103 return Style.SpaceBeforeParensOptions.AfterFunctionDefinitionName ||
5104 spaceRequiredBeforeParens(Right);
5106 if (!BeforeLeft || BeforeLeft->isNoneOf(tok::period, tok::arrow)) {
5107 if (
Left.isOneOf(tok::kw_try, Keywords.kw___except, tok::kw_catch)) {
5108 return Style.SpaceBeforeParensOptions.AfterControlStatements ||
5109 spaceRequiredBeforeParens(Right);
5111 if (
Left.isPlacementOperator() ||
5112 (
Left.is(tok::r_square) &&
Left.MatchingParen &&
5113 Left.MatchingParen->Previous &&
5114 Left.MatchingParen->Previous->is(tok::kw_delete))) {
5115 return Style.SpaceBeforeParens != FormatStyle::SBPO_Never ||
5116 spaceRequiredBeforeParens(Right);
5120 if (
Tok.isNot(tok::l_paren))
5122 const auto *RParen =
Tok.MatchingParen;
5125 const auto *
Next = RParen->Next;
5132 (
Left.Tok.getIdentifierInfo() ||
Left.is(tok::r_paren))) {
5133 return spaceRequiredBeforeParens(Right);
5137 if (
Left.is(tok::at) &&
Right.isNot(tok::objc_not_keyword))
5139 if (
Right.is(TT_UnaryOperator)) {
5140 return Left.isNoneOf(tok::l_paren, tok::l_square, tok::at) &&
5141 (
Left.isNot(tok::colon) ||
Left.isNot(TT_ObjCMethodExpr));
5147 if (!Style.isVerilog() &&
5148 (
Left.isOneOf(tok::identifier, tok::greater, tok::r_square,
5150 Left.isTypeName(LangOpts)) &&
5151 Right.is(tok::l_brace) &&
Right.getNextNonComment() &&
5155 if (
Left.is(tok::period) ||
Right.is(tok::period))
5159 if (
Right.is(tok::hash) &&
Left.is(tok::identifier) &&
5160 (
Left.TokenText ==
"L" ||
Left.TokenText ==
"u" ||
5161 Left.TokenText ==
"U" ||
Left.TokenText ==
"u8" ||
5162 Left.TokenText ==
"LR" ||
Left.TokenText ==
"uR" ||
5163 Left.TokenText ==
"UR" ||
Left.TokenText ==
"u8R")) {
5166 if (
Left.is(TT_TemplateCloser) &&
Left.MatchingParen &&
5167 Left.MatchingParen->Previous &&
5168 Left.MatchingParen->Previous->isOneOf(tok::period, tok::coloncolon)) {
5174 if (
Left.is(TT_TemplateCloser) &&
Right.is(tok::l_square))
5176 if (
Left.is(tok::l_brace) &&
Left.endsSequence(TT_DictLiteral, tok::at)) {
5180 if (
Right.is(tok::r_brace) &&
Right.MatchingParen &&
5181 Right.MatchingParen->endsSequence(TT_DictLiteral, tok::at)) {
5185 if (
Right.is(TT_TrailingAnnotation) &&
Right.isOneOf(tok::amp, tok::ampamp) &&
5186 Left.isOneOf(tok::kw_const, tok::kw_volatile) &&
5187 (!
Right.Next ||
Right.Next->is(tok::semi))) {
5191 return getTokenReferenceAlignment(Right) != FormatStyle::PAS_Left;
5204 return Right.hasWhitespaceBefore();
5206 const bool IsVerilog = Style.isVerilog();
5207 assert(!IsVerilog || !IsCpp);
5210 if (Keywords.isWordLike(Right, IsVerilog) &&
5211 Keywords.isWordLike(Left, IsVerilog)) {
5217 if (
Left.is(tok::star) &&
Right.is(tok::comment))
5220 if (
Left.is(tok::l_brace) &&
Right.is(tok::r_brace) &&
5221 Left.Children.empty()) {
5223 return Style.SpaceInEmptyBraces != FormatStyle::SIEB_Never;
5224 if (Style.Cpp11BracedListStyle != FormatStyle::BLS_Block) {
5225 return Style.SpacesInParens == FormatStyle::SIPO_Custom &&
5226 Style.SpacesInParensOptions.InEmptyParentheses;
5228 return Style.SpaceInEmptyBraces == FormatStyle::SIEB_Always;
5231 const auto *BeforeLeft =
Left.Previous;
5234 if (
Left.is(TT_OverloadedOperator) &&
5235 Right.isOneOf(TT_TemplateOpener, TT_TemplateCloser)) {
5239 if (
Right.is(tok::period) &&
Left.is(tok::numeric_constant))
5243 if (
Left.is(Keywords.kw_import) &&
5244 Right.isOneOf(tok::less, tok::ellipsis) &&
5245 (!BeforeLeft || BeforeLeft->is(tok::kw_export))) {
5249 if (
Left.is(Keywords.kw_import) &&
Right.is(TT_ModulePartitionColon))
5252 if (
Right.is(TT_AfterPPDirective))
5256 if (
Left.is(tok::identifier) &&
Right.is(TT_ModulePartitionColon))
5259 if (
Left.is(TT_ModulePartitionColon) &&
Right.is(tok::identifier))
5261 if (
Left.is(tok::ellipsis) &&
Right.is(tok::identifier) &&
5262 Line.First->is(Keywords.kw_import)) {
5266 if (
Left.isOneOf(TT_AttributeRParen, TT_AttributeMacro) &&
5267 Right.is(tok::coloncolon)) {
5271 if (
Left.is(tok::kw_operator))
5272 return Right.is(tok::coloncolon) || Style.SpaceAfterOperatorKeyword;
5274 !
Left.opensScope() && Style.SpaceBeforeCpp11BracedList) {
5277 if (
Left.is(tok::less) &&
Left.is(TT_OverloadedOperator) &&
5278 Right.is(TT_TemplateOpener)) {
5282 if (
Left.is(tok::identifier) &&
Right.is(tok::numeric_constant))
5283 return Right.TokenText[0] !=
'.';
5285 if (
Left.Tok.getIdentifierInfo() &&
Right.Tok.isLiteral())
5287 }
else if (Style.isProto()) {
5288 if (
Right.is(tok::period) && !(BeforeLeft && BeforeLeft->is(tok::period)) &&
5289 Left.isOneOf(Keywords.kw_optional, Keywords.kw_required,
5290 Keywords.kw_repeated, Keywords.kw_extend)) {
5293 if (
Right.is(tok::l_paren) &&
5294 Left.isOneOf(Keywords.kw_returns, Keywords.kw_option)) {
5297 if (
Right.isOneOf(tok::l_brace, tok::less) &&
Left.is(TT_SelectorName))
5300 if (
Left.is(tok::slash) ||
Right.is(tok::slash))
5302 if (
Left.MatchingParen &&
5303 Left.MatchingParen->is(TT_ProtoExtensionLSquare) &&
5304 Right.isOneOf(tok::l_brace, tok::less)) {
5305 return Style.Cpp11BracedListStyle == FormatStyle::BLS_Block;
5308 if (
Left.is(tok::percent))
5312 if (
Left.is(tok::numeric_constant) &&
Right.is(tok::percent))
5313 return Right.hasWhitespaceBefore();
5314 }
else if (Style.isJson()) {
5315 if (
Right.is(tok::colon) &&
Left.is(tok::string_literal))
5316 return Style.SpaceBeforeJsonColon;
5317 }
else if (Style.isCSharp()) {
5323 if (
Left.is(tok::kw_this) &&
Right.is(tok::l_square))
5327 if (
Left.is(tok::kw_new) &&
Right.is(tok::l_paren))
5331 if (
Right.is(tok::l_brace))
5335 if (
Left.is(tok::l_brace) &&
Right.isNot(tok::r_brace))
5338 if (
Left.isNot(tok::l_brace) &&
Right.is(tok::r_brace))
5342 if (
Left.is(TT_FatArrow) ||
Right.is(TT_FatArrow))
5346 if (
Left.is(TT_AttributeColon) ||
Right.is(TT_AttributeColon))
5350 if (
Left.is(TT_TemplateCloser) &&
Right.is(TT_StartOfName))
5354 if (
Left.is(tok::l_square) ||
Right.is(tok::r_square))
5355 return Style.SpacesInSquareBrackets;
5358 if (
Right.is(TT_CSharpNullable))
5362 if (
Right.is(TT_NonNullAssertion))
5366 if (
Left.is(tok::comma) &&
Right.is(tok::comma))
5370 if (
Left.is(Keywords.kw_var) &&
Right.is(tok::l_paren))
5374 if (
Right.is(tok::l_paren)) {
5375 if (
Left.isOneOf(tok::kw_using, Keywords.kw_async, Keywords.kw_when,
5376 Keywords.kw_lock)) {
5377 return Style.SpaceBeforeParensOptions.AfterControlStatements ||
5378 spaceRequiredBeforeParens(Right);
5384 if ((
Left.isAccessSpecifierKeyword() ||
5385 Left.isOneOf(tok::kw_virtual, tok::kw_extern, tok::kw_static,
5386 Keywords.kw_internal, Keywords.kw_abstract,
5387 Keywords.kw_sealed, Keywords.kw_override,
5388 Keywords.kw_async, Keywords.kw_unsafe)) &&
5389 Right.is(tok::l_paren)) {
5392 }
else if (Style.isJavaScript()) {
5393 if (
Left.is(TT_FatArrow))
5396 if (
Right.is(tok::l_paren) &&
Left.is(Keywords.kw_await) && BeforeLeft &&
5397 BeforeLeft->is(tok::kw_for)) {
5400 if (
Left.is(Keywords.kw_async) &&
Right.is(tok::l_paren) &&
5401 Right.MatchingParen) {
5408 if ((
Left.is(TT_TemplateString) &&
Left.TokenText.ends_with(
"${")) ||
5409 (
Right.is(TT_TemplateString) &&
Right.TokenText.starts_with(
"}"))) {
5414 if (Keywords.isJavaScriptIdentifier(Left,
5416 Right.is(TT_TemplateString)) {
5419 if (
Right.is(tok::star) &&
5420 Left.isOneOf(Keywords.kw_function, Keywords.kw_yield)) {
5423 if (
Right.isOneOf(tok::l_brace, tok::l_square) &&
5424 Left.isOneOf(Keywords.kw_function, Keywords.kw_yield,
5425 Keywords.kw_extends, Keywords.kw_implements)) {
5428 if (
Right.is(tok::l_paren)) {
5430 if (
Line.MustBeDeclaration &&
Left.Tok.getIdentifierInfo())
5434 if (BeforeLeft && BeforeLeft->is(tok::period) &&
5435 Left.Tok.getIdentifierInfo()) {
5439 if (
Left.isOneOf(tok::kw_throw, Keywords.kw_await, Keywords.kw_typeof,
5445 if (
Left.endsSequence(tok::kw_const, Keywords.kw_as))
5447 if ((
Left.isOneOf(Keywords.kw_let, Keywords.kw_var, Keywords.kw_in,
5452 (
Left.is(Keywords.kw_of) && BeforeLeft &&
5453 BeforeLeft->isOneOf(tok::identifier, tok::r_square, tok::r_brace))) &&
5454 (!BeforeLeft || BeforeLeft->isNot(tok::period))) {
5457 if (
Left.isOneOf(tok::kw_for, Keywords.kw_as) && BeforeLeft &&
5458 BeforeLeft->is(tok::period) &&
Right.is(tok::l_paren)) {
5461 if (
Left.is(Keywords.kw_as) &&
5462 Right.isOneOf(tok::l_square, tok::l_brace, tok::l_paren)) {
5465 if (
Left.is(tok::kw_default) && BeforeLeft &&
5466 BeforeLeft->is(tok::kw_export)) {
5469 if (
Left.is(Keywords.kw_is) &&
Right.is(tok::l_brace))
5471 if (
Right.isOneOf(TT_JsTypeColon, TT_JsTypeOptionalQuestion))
5473 if (
Left.is(TT_JsTypeOperator) ||
Right.is(TT_JsTypeOperator))
5475 if ((
Left.is(tok::l_brace) ||
Right.is(tok::r_brace)) &&
5476 Line.First->isOneOf(Keywords.kw_import, tok::kw_export)) {
5479 if (
Left.is(tok::ellipsis))
5481 if (
Left.is(TT_TemplateCloser) &&
5482 Right.isNoneOf(tok::equal, tok::l_brace, tok::comma, tok::l_square,
5483 Keywords.kw_implements, Keywords.kw_extends)) {
5489 if (
Right.is(TT_NonNullAssertion))
5491 if (
Left.is(TT_NonNullAssertion) &&
5492 Right.isOneOf(Keywords.kw_as, Keywords.kw_in)) {
5495 }
else if (Style.isJava()) {
5496 if (
Left.is(TT_CaseLabelArrow) ||
Right.is(TT_CaseLabelArrow))
5498 if (
Left.is(tok::r_square) &&
Right.is(tok::l_brace))
5501 if (
Left.is(tok::l_square) ||
Right.is(tok::r_square))
5502 return Style.SpacesInSquareBrackets;
5504 if (
Left.is(Keywords.kw_synchronized) &&
Right.is(tok::l_paren)) {
5505 return Style.SpaceBeforeParensOptions.AfterControlStatements ||
5506 spaceRequiredBeforeParens(Right);
5508 if ((
Left.isAccessSpecifierKeyword() ||
5509 Left.isOneOf(tok::kw_static, Keywords.kw_final, Keywords.kw_abstract,
5510 Keywords.kw_native)) &&
5511 Right.is(TT_TemplateOpener)) {
5514 }
else if (IsVerilog) {
5516 if (
Left.is(tok::identifier) &&
Left.TokenText[0] ==
'\\')
5520 if ((
Left.is(TT_VerilogTableItem) &&
5521 Right.isNoneOf(tok::r_paren, tok::semi)) ||
5522 (
Right.is(TT_VerilogTableItem) &&
Left.isNot(tok::l_paren))) {
5524 return !(
Next &&
Next->is(tok::r_paren));
5527 if (
Left.isNot(TT_BinaryOperator) &&
5528 Left.isOneOf(Keywords.kw_verilogHash, Keywords.kw_verilogHashHash)) {
5532 if (
Right.isNot(tok::semi) &&
5533 (
Left.endsSequence(tok::numeric_constant, Keywords.kw_verilogHash) ||
5534 Left.endsSequence(tok::numeric_constant,
5535 Keywords.kw_verilogHashHash) ||
5536 (
Left.is(tok::r_paren) &&
Left.MatchingParen &&
5537 Left.MatchingParen->endsSequence(tok::l_paren, tok::at)))) {
5542 if (
Left.is(Keywords.kw_apostrophe) ||
5543 (
Left.is(TT_VerilogNumberBase) &&
Right.is(tok::numeric_constant))) {
5547 if (
Left.is(tok::arrow) ||
Right.is(tok::arrow))
5552 if (
Left.is(tok::at) &&
Right.isOneOf(tok::l_paren, tok::star, tok::at))
5555 if (
Right.is(tok::l_square) &&
5556 Left.isOneOf(TT_VerilogDimensionedTypeName, Keywords.kw_function)) {
5560 if (
Right.isOneOf(tok::period, Keywords.kw_apostrophe) &&
5561 Keywords.isVerilogIdentifier(Left) &&
Left.getPreviousNonComment() &&
5562 Left.getPreviousNonComment()->is(Keywords.kw_tagged)) {
5568 if ((
Right.is(Keywords.kw_apostrophe) ||
5570 Left.isNoneOf(Keywords.kw_assign, Keywords.kw_unique) &&
5571 !Keywords.isVerilogWordOperator(Left) &&
5572 (
Left.isOneOf(tok::r_square, tok::r_paren, tok::r_brace,
5573 tok::numeric_constant) ||
5574 Keywords.isWordLike(Left))) {
5578 if ((
Right.is(tok::star) &&
Left.is(tok::coloncolon)) ||
5579 (
Left.is(tok::star) &&
Right.is(tok::semi))) {
5583 if (
Left.endsSequence(tok::star, tok::l_paren) &&
Right.is(tok::identifier))
5586 if (
Right.is(tok::l_paren) &&
Right.is(TT_VerilogStrength))
5589 if ((
Left.is(tok::l_brace) &&
5590 Right.isOneOf(tok::lessless, tok::greatergreater)) ||
5591 (
Left.endsSequence(tok::lessless, tok::l_brace) ||
5592 Left.endsSequence(tok::greatergreater, tok::l_brace))) {
5595 }
else if (Style.isTableGen()) {
5597 if (
Left.is(tok::l_square) &&
Right.is(tok::l_brace))
5599 if (
Left.is(tok::r_brace) &&
Right.is(tok::r_square))
5602 if (
Right.isOneOf(TT_TableGenDAGArgListColon,
5603 TT_TableGenDAGArgListColonToAlign) ||
5604 Left.isOneOf(TT_TableGenDAGArgListColon,
5605 TT_TableGenDAGArgListColonToAlign)) {
5608 if (
Right.is(TT_TableGenCondOperatorColon))
5610 if (
Left.isOneOf(TT_TableGenDAGArgOperatorID,
5611 TT_TableGenDAGArgOperatorToBreak) &&
5612 Right.isNot(TT_TableGenDAGArgCloser)) {
5616 if (
Right.isOneOf(tok::l_paren, tok::less) &&
5617 Left.isOneOf(TT_TableGenBangOperator, TT_TableGenCondOperator)) {
5622 if (
Left.is(TT_TableGenTrailingPasteOperator) &&
5623 Right.isOneOf(tok::l_brace, tok::colon)) {
5627 if (
Left.is(tok::hash) ||
Right.is(tok::hash))
5630 if (Keywords.isTableGenDefinition(Left))
5634 if (
Left.is(TT_ImplicitStringLiteral))
5635 return Right.hasWhitespaceBefore();
5637 if (
Left.is(TT_ObjCMethodSpecifier))
5638 return Style.ObjCSpaceAfterMethodDeclarationPrefix;
5639 if (
Left.is(tok::r_paren) &&
Left.isNot(TT_AttributeRParen) &&
5640 canBeObjCSelectorComponent(Right)) {
5648 (
Right.is(tok::equal) ||
Left.is(tok::equal))) {
5652 if (
Right.isOneOf(TT_TrailingReturnArrow, TT_LambdaArrow) ||
5653 Left.isOneOf(TT_TrailingReturnArrow, TT_LambdaArrow)) {
5656 if (
Left.is(tok::comma) &&
Right.isNot(TT_OverloadedOperatorLParen) &&
5659 (
Left.Children.empty() || !
Left.MacroParent)) {
5662 if (
Right.is(tok::comma))
5664 if (
Right.is(TT_ObjCBlockLParen))
5666 if (
Right.is(TT_CtorInitializerColon))
5667 return Style.SpaceBeforeCtorInitializerColon;
5668 if (
Right.is(TT_InheritanceColon) && !Style.SpaceBeforeInheritanceColon)
5670 if (
Right.is(TT_EnumUnderlyingTypeColon) &&
5671 !Style.SpaceBeforeEnumUnderlyingTypeColon) {
5674 if (
Right.is(TT_RangeBasedForLoopColon) &&
5675 !Style.SpaceBeforeRangeBasedForLoopColon) {
5678 if (
Left.is(TT_BitFieldColon)) {
5679 return Style.BitFieldColonSpacing == FormatStyle::BFCS_Both ||
5680 Style.BitFieldColonSpacing == FormatStyle::BFCS_After;
5682 if (
Right.is(tok::colon)) {
5683 if (
Right.is(TT_CaseLabelColon))
5684 return Style.SpaceBeforeCaseColon;
5685 if (
Right.is(TT_GotoLabelColon))
5688 if (!
Right.getNextNonComment())
5690 if (
Right.isOneOf(TT_ObjCSelector, TT_ObjCMethodExpr))
5692 if (
Left.is(tok::question))
5694 if (
Right.is(TT_InlineASMColon) &&
Left.is(tok::coloncolon))
5696 if (
Right.is(TT_DictLiteral))
5697 return Style.SpacesInContainerLiterals;
5698 if (
Right.is(TT_AttributeColon))
5700 if (
Right.is(TT_CSharpNamedArgumentColon))
5702 if (
Right.is(TT_GenericSelectionColon))
5704 if (
Right.is(TT_BitFieldColon)) {
5705 return Style.BitFieldColonSpacing == FormatStyle::BFCS_Both ||
5706 Style.BitFieldColonSpacing == FormatStyle::BFCS_Before;
5711 if ((
Left.isOneOf(tok::minus, tok::minusminus) &&
5712 Right.isOneOf(tok::minus, tok::minusminus)) ||
5713 (
Left.isOneOf(tok::plus, tok::plusplus) &&
5714 Right.isOneOf(tok::plus, tok::plusplus))) {
5717 if (
Left.is(TT_UnaryOperator)) {
5720 if (
Left.is(tok::amp) &&
Right.is(tok::r_square))
5721 return Style.SpacesInSquareBrackets;
5722 if (
Left.isNot(tok::exclaim))
5724 if (
Left.TokenText ==
"!")
5725 return Style.SpaceAfterLogicalNot;
5726 assert(
Left.TokenText ==
"not");
5727 return Right.isOneOf(tok::coloncolon, TT_UnaryOperator) ||
5728 (
Right.is(tok::l_paren) && Style.SpaceBeforeParensOptions.AfterNot);
5733 if (
Left.is(TT_CastRParen)) {
5734 return Style.SpaceAfterCStyleCast ||
5735 Right.isOneOf(TT_BinaryOperator, TT_SelectorName);
5738 auto ShouldAddSpacesInAngles = [
this, &
Right]() {
5739 if (this->Style.SpacesInAngles == FormatStyle::SIAS_Always)
5741 if (this->Style.SpacesInAngles == FormatStyle::SIAS_Leave)
5742 return Right.hasWhitespaceBefore();
5746 if (
Left.is(tok::greater) &&
Right.is(tok::greater)) {
5747 if (Style.isTextProto() ||
5748 (Style.Language == FormatStyle::LK_Proto &&
Left.is(TT_DictLiteral))) {
5749 return Style.Cpp11BracedListStyle == FormatStyle::BLS_Block;
5751 return Right.is(TT_TemplateCloser) &&
Left.is(TT_TemplateCloser) &&
5752 ((Style.Standard < FormatStyle::LS_Cpp11) ||
5753 ShouldAddSpacesInAngles());
5755 if (
Right.isOneOf(tok::arrow, tok::arrowstar, tok::periodstar) ||
5756 Left.isOneOf(tok::arrow, tok::period, tok::arrowstar, tok::periodstar) ||
5757 (
Right.is(tok::period) &&
Right.isNot(TT_DesignatedInitializerPeriod))) {
5760 if (!Style.SpaceBeforeAssignmentOperators &&
Left.isNot(TT_TemplateCloser) &&
5764 if (Style.isJava() &&
Right.is(tok::coloncolon) &&
5765 Left.isOneOf(tok::identifier, tok::kw_this)) {
5768 if (
Right.is(tok::coloncolon) &&
Left.is(tok::identifier)) {
5770 return Left.isPossibleMacro(
true) &&
5771 Right.hasWhitespaceBefore();
5773 if (
Right.is(tok::coloncolon) &&
5774 Left.isNoneOf(tok::l_brace, tok::comment, tok::l_paren)) {
5776 return (
Left.is(TT_TemplateOpener) &&
5777 ((Style.Standard < FormatStyle::LS_Cpp11) ||
5778 ShouldAddSpacesInAngles())) ||
5779 Left.isNoneOf(tok::l_paren, tok::r_paren, tok::l_square,
5780 tok::kw___super, TT_TemplateOpener,
5781 TT_TemplateCloser) ||
5782 (
Left.is(tok::l_paren) && Style.SpacesInParensOptions.Other);
5784 if ((
Left.is(TT_TemplateOpener)) != (
Right.is(TT_TemplateCloser)))
5785 return ShouldAddSpacesInAngles();
5786 if (
Left.is(tok::r_paren) &&
Left.isNot(TT_TypeDeclarationParen) &&
5787 Right.is(TT_PointerOrReference) &&
Right.isOneOf(tok::amp, tok::ampamp)) {
5791 if (
Right.is(TT_StructuredBindingLSquare)) {
5792 return Left.isNoneOf(tok::amp, tok::ampamp) ||
5793 getTokenReferenceAlignment(Left) != FormatStyle::PAS_Right;
5796 if (
Right.Next &&
Right.Next->is(TT_StructuredBindingLSquare) &&
5797 Right.isOneOf(tok::amp, tok::ampamp)) {
5798 return getTokenReferenceAlignment(Right) != FormatStyle::PAS_Left;
5800 if ((
Right.is(TT_BinaryOperator) &&
Left.isNot(tok::l_paren)) ||
5801 (
Left.isOneOf(TT_BinaryOperator, TT_EnumEqual, TT_ConditionalExpr) &&
5802 Right.isNot(tok::r_paren))) {
5805 if (
Right.is(TT_TemplateOpener) &&
Left.is(tok::r_paren) &&
5806 Left.MatchingParen &&
5807 Left.MatchingParen->is(TT_OverloadedOperatorLParen)) {
5810 if (
Right.is(tok::less) &&
Left.isNot(tok::l_paren) &&
5814 if (
Right.is(TT_TrailingUnaryOperator))
5816 if (
Left.is(TT_RegexLiteral))
5818 return spaceRequiredBetween(
Line, Left, Right);
5824 Tok.isNoneOf(TT_ObjCBlockLBrace, TT_LambdaLBrace, TT_DictLiteral);
5829 return Tok.MatchingParen &&
Tok.MatchingParen->Next &&
5830 Tok.MatchingParen->Next->isOneOf(tok::comma, tok::r_paren,
5836 FormatStyle::ShortLambdaStyle ShortLambdaOption) {
5837 return Tok.Children.empty() && ShortLambdaOption != FormatStyle::SLS_None;
5842 Tok.isNoneOf(TT_ObjCBlockLBrace, TT_DictLiteral);
5845bool TokenAnnotator::mustBreakBefore(AnnotatedLine &
Line,
5847 if (Right.NewlinesBefore > 1 && Style.MaxEmptyLinesToKeep > 0 &&
5848 (!Style.RemoveEmptyLinesInUnwrappedLines || &Right ==
Line.First)) {
5854 if (Style.BreakFunctionDeclarationParameters &&
Line.MightBeFunctionDecl &&
5855 !
Line.mightBeFunctionDefinition() && Left.MightBeFunctionDeclParen &&
5856 Left.ParameterCount > 0) {
5860 if (Style.BreakFunctionDefinitionParameters &&
Line.MightBeFunctionDecl &&
5861 Line.mightBeFunctionDefinition() &&
Left.MightBeFunctionDeclParen &&
5862 Left.ParameterCount > 0) {
5868 if (Style.PackParameters.BinPack == FormatStyle::BPPS_AlwaysOnePerLine &&
5869 Line.MightBeFunctionDecl && !
Left.opensScope() &&
5874 const auto *BeforeLeft =
Left.Previous;
5875 const auto *AfterRight =
Right.Next;
5877 if (Style.isCSharp()) {
5878 if (
Left.is(TT_FatArrow) &&
Right.is(tok::l_brace) &&
5879 Style.BraceWrapping.AfterFunction) {
5882 if (
Right.is(TT_CSharpNamedArgumentColon) ||
5883 Left.is(TT_CSharpNamedArgumentColon)) {
5886 if (
Right.is(TT_CSharpGenericTypeConstraint))
5888 if (AfterRight && AfterRight->is(TT_FatArrow) &&
5889 (
Right.is(tok::numeric_constant) ||
5890 (
Right.is(tok::identifier) &&
Right.TokenText ==
"_"))) {
5895 if (
Left.is(TT_AttributeRSquare) &&
5896 (
Right.isAccessSpecifier(
false) ||
5897 Right.is(Keywords.kw_internal))) {
5901 if (
Left.is(TT_AttributeRSquare) &&
Right.is(TT_AttributeLSquare))
5903 }
else if (Style.isJavaScript()) {
5905 if (
Right.is(tok::string_literal) &&
Left.is(tok::plus) && BeforeLeft &&
5906 BeforeLeft->is(tok::string_literal)) {
5909 if (
Left.is(TT_DictLiteral) &&
Left.is(tok::l_brace) &&
Line.Level == 0 &&
5910 BeforeLeft && BeforeLeft->is(tok::equal) &&
5911 Line.First->isOneOf(tok::identifier, Keywords.kw_import, tok::kw_export,
5915 Line.First->isNoneOf(Keywords.kw_var, Keywords.kw_let)) {
5920 if (
Left.is(tok::l_brace) &&
Line.Level == 0 &&
5921 (
Line.startsWith(tok::kw_enum) ||
5922 Line.startsWith(tok::kw_const, tok::kw_enum) ||
5923 Line.startsWith(tok::kw_export, tok::kw_enum) ||
5924 Line.startsWith(tok::kw_export, tok::kw_const, tok::kw_enum))) {
5929 if (
Right.is(tok::r_brace) &&
Left.is(tok::l_brace) && BeforeLeft &&
5930 BeforeLeft->is(TT_FatArrow)) {
5932 switch (Style.AllowShortLambdasOnASingleLine) {
5933 case FormatStyle::SLS_All:
5935 case FormatStyle::SLS_None:
5937 case FormatStyle::SLS_Empty:
5938 return !
Left.Children.empty();
5939 case FormatStyle::SLS_Inline:
5942 return (
Left.NestingLevel == 0 &&
Line.Level == 0) &&
5943 !
Left.Children.empty();
5945 llvm_unreachable(
"Unknown FormatStyle::ShortLambdaStyle enum");
5948 if (
Right.is(tok::r_brace) &&
Left.is(tok::l_brace) &&
5949 !
Left.Children.empty()) {
5951 if (
Left.NestingLevel == 0 &&
Line.Level == 0)
5952 return !Style.AllowShortFunctionsOnASingleLine.Other;
5954 return !Style.AllowShortFunctionsOnASingleLine.Inline;
5956 }
else if (Style.isJava()) {
5957 if (
Right.is(tok::plus) &&
Left.is(tok::string_literal) && AfterRight &&
5958 AfterRight->is(tok::string_literal)) {
5961 }
else if (Style.isVerilog()) {
5963 if (
Left.is(TT_VerilogAssignComma))
5966 if (
Left.is(TT_VerilogTypeComma))
5970 if (Style.VerilogBreakBetweenInstancePorts &&
5971 (
Left.is(TT_VerilogInstancePortComma) ||
5972 (
Left.is(tok::r_paren) && Keywords.isVerilogIdentifier(Right) &&
5973 Left.MatchingParen &&
5974 Left.MatchingParen->is(TT_VerilogInstancePortLParen)))) {
5979 if (!Keywords.isVerilogBegin(Right) && Keywords.isVerilogEndOfLabel(Left))
5981 }
else if (Style.BreakAdjacentStringLiterals &&
5982 (IsCpp || Style.isProto() || Style.isTableGen())) {
5983 if (
Left.isStringLiteral() &&
Right.isStringLiteral())
5988 if (Style.isJson()) {
5992 if (
Left.is(TT_DictLiteral) &&
Left.is(tok::l_brace))
5995 if ((
Left.is(TT_ArrayInitializerLSquare) &&
Left.is(tok::l_square) &&
5996 Right.isNot(tok::r_square)) ||
5997 Left.is(tok::comma)) {
5998 if (
Right.is(tok::l_brace))
6003 if (
Tok->isOneOf(tok::l_brace, tok::l_square))
6005 if (
Tok->isOneOf(tok::r_brace, tok::r_square))
6008 return Style.BreakArrays;
6010 }
else if (Style.isTableGen()) {
6014 if (
Left.is(TT_TableGenCondOperatorComma))
6016 if (
Left.is(TT_TableGenDAGArgOperatorToBreak) &&
6017 Right.isNot(TT_TableGenDAGArgCloser)) {
6020 if (
Left.is(TT_TableGenDAGArgListCommaToBreak))
6022 if (
Right.is(TT_TableGenDAGArgCloser) &&
Right.MatchingParen &&
6023 Right.MatchingParen->is(TT_TableGenDAGArgOpenerToBreak) &&
6024 &Left !=
Right.MatchingParen->Next) {
6026 return Style.TableGenBreakInsideDAGArg == FormatStyle::DAS_BreakAll;
6030 if (
Line.startsWith(tok::kw_asm) &&
Right.is(TT_InlineASMColon) &&
6031 Style.BreakBeforeInlineASMColon == FormatStyle::BBIAS_Always) {
6041 if ((
Left.isOneOf(tok::l_brace, TT_ArrayInitializerLSquare) ||
6042 (Style.isJavaScript() &&
Left.is(tok::l_paren))) &&
6044 BeforeClosingBrace =
Left.MatchingParen->Previous;
6045 }
else if (
Right.MatchingParen &&
6046 (
Right.MatchingParen->isOneOf(tok::l_brace,
6047 TT_ArrayInitializerLSquare) ||
6048 (Style.isJavaScript() &&
6049 Right.MatchingParen->is(tok::l_paren)))) {
6050 BeforeClosingBrace = &
Left;
6052 if (BeforeClosingBrace && (BeforeClosingBrace->is(tok::comma) ||
6053 BeforeClosingBrace->isTrailingComment())) {
6058 if (
Right.is(tok::comment)) {
6060 Right.NewlinesBefore > 0 &&
Right.HasUnescapedNewline;
6062 if (
Left.isTrailingComment())
6064 if (
Left.IsUnterminatedLiteral)
6067 if (BeforeLeft && BeforeLeft->is(tok::lessless) &&
6068 Left.is(tok::string_literal) &&
Right.is(tok::lessless) && AfterRight &&
6069 AfterRight->is(tok::string_literal)) {
6070 return Right.NewlinesBefore > 0;
6073 if (
Right.is(TT_RequiresClause)) {
6074 switch (Style.RequiresClausePosition) {
6075 case FormatStyle::RCPS_OwnLine:
6076 case FormatStyle::RCPS_OwnLineWithBrace:
6077 case FormatStyle::RCPS_WithFollowing:
6084 if (
Left.ClosesTemplateDeclaration &&
Left.MatchingParen &&
6085 Left.MatchingParen->NestingLevel == 0) {
6089 if (
Right.is(tok::kw_concept))
6090 return Style.BreakBeforeConceptDeclarations == FormatStyle::BBCDS_Always;
6091 return Style.BreakTemplateDeclarations == FormatStyle::BTDS_Yes ||
6092 (Style.BreakTemplateDeclarations == FormatStyle::BTDS_Leave &&
6093 Right.NewlinesBefore > 0);
6095 if (
Left.ClosesRequiresClause) {
6096 switch (Style.RequiresClausePosition) {
6097 case FormatStyle::RCPS_OwnLine:
6098 case FormatStyle::RCPS_WithPreceding:
6099 return Right.isNot(tok::semi);
6100 case FormatStyle::RCPS_OwnLineWithBrace:
6101 return Right.isNoneOf(tok::semi, tok::l_brace);
6106 if (Style.PackConstructorInitializers == FormatStyle::PCIS_Never) {
6107 if (Style.BreakConstructorInitializers == FormatStyle::BCIS_BeforeColon &&
6108 (
Left.is(TT_CtorInitializerComma) ||
6109 Right.is(TT_CtorInitializerColon))) {
6113 if (Style.BreakConstructorInitializers == FormatStyle::BCIS_AfterColon &&
6114 Left.isOneOf(TT_CtorInitializerColon, TT_CtorInitializerComma)) {
6118 if (Style.BreakConstructorInitializers == FormatStyle::BCIS_AfterComma &&
6119 Left.is(TT_CtorInitializerComma)) {
6123 if (Style.PackConstructorInitializers < FormatStyle::PCIS_CurrentLine &&
6124 Style.BreakConstructorInitializers == FormatStyle::BCIS_BeforeComma &&
6125 Right.isOneOf(TT_CtorInitializerComma, TT_CtorInitializerColon)) {
6128 if (Style.PackConstructorInitializers == FormatStyle::PCIS_NextLineOnly) {
6129 if ((Style.BreakConstructorInitializers == FormatStyle::BCIS_BeforeColon ||
6130 Style.BreakConstructorInitializers == FormatStyle::BCIS_BeforeComma) &&
6131 Right.is(TT_CtorInitializerColon)) {
6135 if (Style.BreakConstructorInitializers == FormatStyle::BCIS_AfterColon &&
6136 Left.is(TT_CtorInitializerColon)) {
6141 if (Style.BreakInheritanceList == FormatStyle::BILS_BeforeComma &&
6142 Right.is(TT_InheritanceComma)) {
6145 if (Style.BreakInheritanceList == FormatStyle::BILS_AfterComma &&
6146 Left.is(TT_InheritanceComma)) {
6149 if (
Right.is(tok::string_literal) &&
Right.TokenText.starts_with(
"R\"")) {
6153 return Right.IsMultiline &&
Right.NewlinesBefore > 0;
6155 if ((
Left.is(tok::l_brace) ||
6156 (
Left.is(tok::less) && BeforeLeft && BeforeLeft->is(tok::equal))) &&
6157 Right.NestingLevel == 1 && Style.Language == FormatStyle::LK_Proto) {
6162 if (
Right.is(TT_InlineASMBrace))
6163 return Right.HasUnescapedNewline;
6166 auto *FirstNonComment =
Line.getFirstNonComment();
6168 FirstNonComment && (FirstNonComment->is(Keywords.kw_internal) ||
6169 FirstNonComment->isAccessSpecifierKeyword());
6171 if (Style.BraceWrapping.AfterEnum) {
6172 if (
Line.startsWith(tok::kw_enum) ||
6173 Line.startsWith(tok::kw_typedef, tok::kw_enum) ||
6174 Line.startsWith(tok::kw_export, tok::kw_enum)) {
6179 FirstNonComment->Next->is(tok::kw_enum)) {
6185 if (Style.BraceWrapping.AfterClass &&
6187 FirstNonComment->Next->is(Keywords.kw_interface)) ||
6188 Line.startsWith(Keywords.kw_interface))) {
6193 if (
Right.isNot(TT_FunctionLBrace)) {
6194 return Style.AllowShortRecordOnASingleLine == FormatStyle::SRS_Never &&
6195 ((
Line.startsWith(tok::kw_class) &&
6196 Style.BraceWrapping.AfterClass) ||
6197 (
Line.startsWith(tok::kw_struct) &&
6198 Style.BraceWrapping.AfterStruct) ||
6199 (
Line.startsWith(tok::kw_union) &&
6200 Style.BraceWrapping.AfterUnion));
6204 if (
Left.is(TT_ObjCBlockLBrace) &&
6205 Style.AllowShortBlocksOnASingleLine == FormatStyle::SBS_Never) {
6210 if (
Left.isOneOf(TT_AttributeRParen, TT_AttributeMacro) &&
6211 Right.is(TT_ObjCDecl)) {
6215 if (
Left.is(TT_LambdaLBrace)) {
6217 Style.AllowShortLambdasOnASingleLine == FormatStyle::SLS_Inline) {
6221 if (Style.AllowShortLambdasOnASingleLine == FormatStyle::SLS_None ||
6222 Style.AllowShortLambdasOnASingleLine == FormatStyle::SLS_Inline ||
6223 (!
Left.Children.empty() &&
6224 Style.AllowShortLambdasOnASingleLine == FormatStyle::SLS_Empty)) {
6229 if (Style.BraceWrapping.BeforeLambdaBody &&
Right.is(TT_LambdaLBrace) &&
6230 (
Left.isPointerOrReference() ||
Left.is(TT_TemplateCloser))) {
6235 if ((Style.isJava() || Style.isJavaScript()) &&
6236 Left.is(TT_LeadingJavaAnnotation) &&
6237 Right.isNoneOf(TT_LeadingJavaAnnotation, tok::l_paren) &&
6238 (
Line.Last->is(tok::l_brace) || Style.BreakAfterJavaFieldAnnotations)) {
6242 if (
Right.is(TT_ProtoExtensionLSquare))
6272 if (Style.isProto() &&
Right.is(TT_SelectorName) &&
6273 Right.isNot(tok::r_square) && AfterRight) {
6276 if (
Left.is(tok::at))
6282 const auto *LBrace = AfterRight;
6283 if (LBrace && LBrace->is(tok::colon)) {
6284 LBrace = LBrace->Next;
6285 if (LBrace && LBrace->is(tok::at)) {
6286 LBrace = LBrace->Next;
6288 LBrace = LBrace->Next;
6300 ((LBrace->is(tok::l_brace) &&
6301 (LBrace->is(TT_DictLiteral) ||
6302 (LBrace->Next && LBrace->Next->is(tok::r_brace)))) ||
6303 LBrace->isOneOf(TT_ArrayInitializerLSquare, tok::less))) {
6310 if (
Left.ParameterCount == 0)
6325 if (
Left.isOneOf(tok::r_brace, tok::greater, tok::r_square))
6329 if (Style.BreakAfterAttributes == FormatStyle::ABS_LeaveAll &&
6330 Left.is(TT_AttributeRSquare) &&
Right.NewlinesBefore > 0) {
6331 Line.ReturnTypeWrapped =
true;
6342 if (Style.isCSharp()) {
6343 if (
Left.isOneOf(TT_CSharpNamedArgumentColon, TT_AttributeColon) ||
6344 Right.isOneOf(TT_CSharpNamedArgumentColon, TT_AttributeColon)) {
6348 if (
Line.First->is(TT_CSharpGenericTypeConstraint))
6349 return Left.is(TT_CSharpGenericTypeConstraintComma);
6351 if (
Right.is(TT_CSharpNullable))
6353 }
else if (Style.isJava()) {
6354 if (
Left.isOneOf(Keywords.kw_throws, Keywords.kw_extends,
6355 Keywords.kw_implements)) {
6358 if (
Right.isOneOf(Keywords.kw_throws, Keywords.kw_extends,
6359 Keywords.kw_implements)) {
6362 }
else if (Style.isJavaScript()) {
6365 (NonComment->isAccessSpecifierKeyword() ||
6366 NonComment->isOneOf(
6367 tok::kw_return, Keywords.kw_yield, tok::kw_continue, tok::kw_break,
6368 tok::kw_throw, Keywords.kw_interface, Keywords.kw_type,
6369 tok::kw_static, Keywords.kw_readonly, Keywords.kw_override,
6370 Keywords.kw_abstract, Keywords.kw_get, Keywords.kw_set,
6371 Keywords.kw_async, Keywords.kw_await))) {
6374 if (
Right.NestingLevel == 0 &&
6375 (
Left.Tok.getIdentifierInfo() ||
6376 Left.isOneOf(tok::r_square, tok::r_paren)) &&
6377 Right.isOneOf(tok::l_square, tok::l_paren)) {
6380 if (NonComment && NonComment->is(tok::identifier) &&
6381 NonComment->TokenText ==
"asserts") {
6384 if (
Left.is(TT_FatArrow) &&
Right.is(tok::l_brace))
6386 if (
Left.is(TT_JsTypeColon))
6389 if (
Left.is(tok::exclaim) &&
Right.is(tok::colon))
6394 if (
Right.is(Keywords.kw_is)) {
6403 if (!
Next ||
Next->isNot(tok::colon))
6406 if (
Left.is(Keywords.kw_in))
6407 return Style.BreakBeforeBinaryOperators == FormatStyle::BOS_None;
6408 if (
Right.is(Keywords.kw_in))
6409 return Style.BreakBeforeBinaryOperators != FormatStyle::BOS_None;
6410 if (
Right.is(Keywords.kw_as))
6412 if (
Right.isOneOf(Keywords.kw_extends, Keywords.kw_infer)) {
6418 if (
Left.is(Keywords.kw_as))
6420 if (
Left.is(TT_NonNullAssertion))
6422 if (
Left.is(Keywords.kw_declare) &&
6423 Right.isOneOf(Keywords.kw_module, tok::kw_namespace,
6424 Keywords.kw_function, tok::kw_class, tok::kw_enum,
6425 Keywords.kw_interface, Keywords.kw_type, Keywords.kw_var,
6426 Keywords.kw_let, tok::kw_const)) {
6431 if (
Left.isOneOf(Keywords.kw_module, tok::kw_namespace) &&
6432 Right.isOneOf(tok::identifier, tok::string_literal)) {
6435 if (
Right.is(TT_TemplateString) &&
Right.closesScope())
6439 if (
Left.is(tok::identifier) &&
Right.is(TT_TemplateString))
6441 if (
Left.is(TT_TemplateString) &&
Left.opensScope())
6443 }
else if (Style.isTableGen()) {
6445 if (Keywords.isTableGenDefinition(Left))
6448 if (
Right.is(tok::l_paren)) {
6449 return Left.isNoneOf(TT_TableGenBangOperator, TT_TableGenCondOperator,
6453 if (
Left.is(TT_TableGenValueSuffix))
6456 if (
Left.is(tok::hash) ||
Right.is(tok::hash))
6458 if (
Left.isOneOf(TT_TableGenBangOperator, TT_TableGenCondOperator))
6465 if (
Right.is(tok::r_brace)) {
6467 (
Right.isBlockIndentedInitRBrace(Style)));
6472 if (
Right.is(tok::r_paren)) {
6473 if (!
Right.MatchingParen)
6476 if (
Next &&
Next->is(tok::r_paren))
6478 if (
Next &&
Next->is(tok::l_paren))
6484 return Style.BreakBeforeCloseBracketIf;
6486 return Style.BreakBeforeCloseBracketLoop;
6488 return Style.BreakBeforeCloseBracketSwitch;
6489 return Style.BreakBeforeCloseBracketFunction;
6492 if (
Left.isOneOf(tok::r_paren, TT_TrailingAnnotation) &&
6493 Right.is(TT_TrailingAnnotation) &&
6494 Style.BreakBeforeCloseBracketFunction) {
6498 if (
Right.is(TT_TemplateCloser))
6499 return Style.BreakBeforeTemplateCloser;
6501 if (
Left.isOneOf(tok::at, tok::objc_interface))
6503 if (
Left.isOneOf(TT_JavaAnnotation, TT_LeadingJavaAnnotation))
6504 return Right.isNot(tok::l_paren);
6505 if (
Right.is(TT_PointerOrReference)) {
6506 return Line.IsMultiVariableDeclStmt ||
6507 (getTokenPointerOrReferenceAlignment(Right) ==
6508 FormatStyle::PAS_Right &&
6510 Right.Next->isOneOf(TT_FunctionDeclarationName, tok::kw_const)));
6512 if (
Left.is(tok::hashhash) ||
Right.is(tok::hashhash))
6514 if (
Right.isOneOf(TT_StartOfName, TT_FunctionDeclarationName,
6515 TT_ClassHeadName, TT_QtProperty, tok::kw_operator)) {
6518 if (
Left.is(TT_PointerOrReference))
6520 if (
Right.isTrailingComment()) {
6527 (
Left.is(TT_CtorInitializerColon) &&
Right.NewlinesBefore > 0 &&
6528 Style.BreakConstructorInitializers == FormatStyle::BCIS_AfterColon);
6530 if (
Left.is(tok::question) &&
Right.is(tok::colon))
6532 if (
Right.isOneOf(TT_ConditionalExpr, tok::question))
6533 return Style.BreakBeforeTernaryOperators;
6534 if (
Left.isOneOf(TT_ConditionalExpr, tok::question))
6535 return !Style.BreakBeforeTernaryOperators;
6536 if (
Left.is(TT_InheritanceColon))
6537 return Style.BreakInheritanceList == FormatStyle::BILS_AfterColon;
6538 if (
Right.is(TT_InheritanceColon))
6539 return Style.BreakInheritanceList != FormatStyle::BILS_AfterColon;
6541 if (
Right.is(TT_ObjCMethodExpr) &&
Right.isNot(tok::r_square) &&
6542 Left.isNot(TT_SelectorName)) {
6546 if (
Right.is(tok::colon) &&
6547 Right.isNoneOf(TT_CtorInitializerColon, TT_InlineASMColon,
6548 TT_BitFieldColon)) {
6551 if (
Left.is(tok::colon) &&
Left.isOneOf(TT_ObjCSelector, TT_ObjCMethodExpr))
6553 if (
Left.is(tok::colon) &&
Left.is(TT_DictLiteral)) {
6554 if (Style.isProto()) {
6555 if (!Style.AlwaysBreakBeforeMultilineStrings &&
Right.isStringLiteral())
6581 if ((
Right.isOneOf(tok::l_brace, tok::less) &&
6582 Right.is(TT_DictLiteral)) ||
6583 Right.is(TT_ArrayInitializerLSquare)) {
6589 if (
Right.is(tok::r_square) &&
Right.MatchingParen &&
6590 Right.MatchingParen->is(TT_ProtoExtensionLSquare)) {
6593 if (
Right.is(TT_SelectorName) || (
Right.is(tok::identifier) &&
Right.Next &&
6594 Right.Next->is(TT_ObjCMethodExpr))) {
6595 return Left.isNot(tok::period);
6599 if (
Right.is(tok::kw_concept))
6600 return Style.BreakBeforeConceptDeclarations != FormatStyle::BBCDS_Never;
6601 if (
Right.is(TT_RequiresClause))
6603 if (
Left.ClosesTemplateDeclaration) {
6604 return Style.BreakTemplateDeclarations != FormatStyle::BTDS_Leave ||
6605 Right.NewlinesBefore > 0;
6607 if (
Left.is(TT_FunctionAnnotationRParen))
6609 if (
Left.ClosesRequiresClause)
6611 if (
Right.isOneOf(TT_RangeBasedForLoopColon, TT_OverloadedOperatorLParen,
6612 TT_OverloadedOperator)) {
6615 if (
Left.is(TT_RangeBasedForLoopColon))
6617 if (
Right.is(TT_RangeBasedForLoopColon))
6619 if (
Left.is(TT_TemplateCloser) &&
Right.is(TT_TemplateOpener))
6621 if ((
Left.is(tok::greater) &&
Right.is(tok::greater)) ||
6622 (
Left.is(tok::less) &&
Right.is(tok::less))) {
6625 if (
Right.is(TT_BinaryOperator) &&
6626 Style.BreakBeforeBinaryOperators != FormatStyle::BOS_None &&
6627 (Style.BreakBeforeBinaryOperators == FormatStyle::BOS_All ||
6631 if (
Left.isOneOf(TT_TemplateCloser, TT_UnaryOperator, tok::kw_operator))
6633 if (
Left.is(tok::equal) &&
Right.isNoneOf(tok::kw_default, tok::kw_delete) &&
6637 if (
Left.is(tok::equal) &&
Right.is(tok::l_brace) &&
6638 Style.Cpp11BracedListStyle == FormatStyle::BLS_Block) {
6641 if (
Left.is(TT_AttributeLParen) ||
6642 (
Left.is(tok::l_paren) &&
Left.is(TT_TypeDeclarationParen))) {
6645 if (
Left.is(tok::l_paren) &&
Left.Previous &&
6646 (
Left.Previous->isOneOf(TT_BinaryOperator, TT_CastRParen))) {
6649 if (
Right.is(TT_ImplicitStringLiteral))
6652 if (
Right.is(tok::r_square) &&
Right.MatchingParen &&
6653 Right.MatchingParen->is(TT_LambdaLSquare)) {
6659 if (
Left.is(TT_TrailingAnnotation)) {
6660 return Right.isNoneOf(tok::l_brace, tok::semi, tok::equal, tok::l_paren,
6661 tok::less, tok::coloncolon);
6664 if (
Right.isAttribute())
6667 if (
Right.is(TT_AttributeLSquare)) {
6668 assert(
Left.isNot(tok::l_square));
6672 if (
Left.is(tok::identifier) &&
Right.is(tok::string_literal))
6675 if (
Right.is(tok::identifier) &&
Right.Next &&
Right.Next->is(TT_DictLiteral))
6678 if (
Left.is(TT_CtorInitializerColon)) {
6679 return (Style.BreakConstructorInitializers ==
6680 FormatStyle::BCIS_AfterColon ||
6681 Style.BreakConstructorInitializers ==
6682 FormatStyle::BCIS_AfterComma) &&
6683 (!
Right.isTrailingComment() ||
Right.NewlinesBefore > 0);
6685 if (
Right.is(TT_CtorInitializerColon)) {
6686 return Style.BreakConstructorInitializers != FormatStyle::BCIS_AfterColon &&
6687 Style.BreakConstructorInitializers != FormatStyle::BCIS_AfterComma;
6689 if (
Left.is(TT_CtorInitializerComma) &&
6690 Style.BreakConstructorInitializers == FormatStyle::BCIS_BeforeComma) {
6693 if (
Right.is(TT_CtorInitializerComma) &&
6694 Style.BreakConstructorInitializers == FormatStyle::BCIS_BeforeComma) {
6697 if (
Left.is(TT_InheritanceComma) &&
6698 Style.BreakInheritanceList == FormatStyle::BILS_BeforeComma) {
6701 if (
Right.is(TT_InheritanceComma) &&
6702 Style.BreakInheritanceList == FormatStyle::BILS_BeforeComma) {
6705 if (
Left.is(TT_ArrayInitializerLSquare))
6707 if (
Right.is(tok::kw_typename) &&
Left.isNot(tok::kw_const))
6709 if ((
Left.isBinaryOperator() ||
Left.is(TT_BinaryOperator)) &&
6710 Left.isNoneOf(tok::arrowstar, tok::lessless) &&
6711 Style.BreakBeforeBinaryOperators != FormatStyle::BOS_All &&
6712 (Style.BreakBeforeBinaryOperators == FormatStyle::BOS_None ||
6716 if (
Left.is(TT_AttributeLSquare) &&
Right.is(tok::l_square)) {
6717 assert(
Right.isNot(TT_AttributeLSquare));
6720 if (
Left.is(tok::r_square) &&
Right.is(TT_AttributeRSquare)) {
6721 assert(
Left.isNot(TT_AttributeRSquare));
6725 auto ShortLambdaOption = Style.AllowShortLambdasOnASingleLine;
6726 if (Style.BraceWrapping.BeforeLambdaBody &&
Right.is(TT_LambdaLBrace)) {
6733 if (
Right.is(tok::kw_noexcept) &&
Right.is(TT_TrailingAnnotation)) {
6734 switch (Style.AllowBreakBeforeNoexceptSpecifier) {
6735 case FormatStyle::BBNSS_Never:
6737 case FormatStyle::BBNSS_Always:
6739 case FormatStyle::BBNSS_OnlyWithParen:
6740 return Right.Next &&
Right.Next->is(tok::l_paren);
6744 return Left.isOneOf(tok::comma, tok::coloncolon, tok::semi, tok::l_brace,
6745 tok::kw_class, tok::kw_struct, tok::comment) ||
6746 Right.isMemberAccess() ||
6747 Right.isOneOf(TT_TrailingReturnArrow, TT_LambdaArrow, tok::lessless,
6748 tok::colon, tok::l_square, tok::at) ||
6749 (
Left.is(tok::r_paren) &&
6750 Right.isOneOf(tok::identifier, tok::kw_const)) ||
6751 (
Left.is(tok::l_paren) &&
Right.isNot(tok::r_paren)) ||
6752 (
Left.is(TT_TemplateOpener) &&
Right.isNot(TT_TemplateCloser));
6756 llvm::errs() <<
"AnnotatedTokens(L=" <<
Line.Level <<
", P=" <<
Line.PPLevel
6757 <<
", T=" <<
Line.Type <<
", C=" <<
Line.IsContinuation
6761 llvm::errs() <<
" I=" <<
Tok->IndentLevel <<
" M=" <<
Tok->MustBreakBefore
6762 <<
" C=" <<
Tok->CanBreakBefore
6764 <<
" S=" <<
Tok->SpacesRequiredBefore
6765 <<
" F=" <<
Tok->Finalized <<
" B=" <<
Tok->BlockParameterCount
6766 <<
" BK=" <<
Tok->getBlockKind() <<
" P=" <<
Tok->SplitPenalty
6767 <<
" Name=" <<
Tok->Tok.getName() <<
" N=" <<
Tok->NestingLevel
6768 <<
" L=" <<
Tok->TotalLength
6769 <<
" PPK=" <<
Tok->getPackingKind() <<
" FakeLParens=";
6771 llvm::errs() << LParen <<
"/";
6772 llvm::errs() <<
" FakeRParens=" <<
Tok->FakeRParens;
6773 llvm::errs() <<
" II=" <<
Tok->Tok.getIdentifierInfo();
6774 llvm::errs() <<
" Text='" <<
Tok->TokenText <<
"'\n";
6779 llvm::errs() <<
"----\n";
6782FormatStyle::PointerAlignmentStyle
6784 assert(
Reference.isOneOf(tok::amp, tok::ampamp));
6785 switch (Style.ReferenceAlignment) {
6786 case FormatStyle::RAS_Pointer:
6787 return Style.PointerAlignment;
6788 case FormatStyle::RAS_Left:
6789 return FormatStyle::PAS_Left;
6790 case FormatStyle::RAS_Right:
6791 return FormatStyle::PAS_Right;
6792 case FormatStyle::RAS_Middle:
6793 return FormatStyle::PAS_Middle;
6796 return Style.PointerAlignment;
6799FormatStyle::PointerAlignmentStyle
6800TokenAnnotator::getTokenPointerOrReferenceAlignment(
6802 if (PointerOrReference.isOneOf(tok::amp, tok::ampamp))
6803 return getTokenReferenceAlignment(PointerOrReference);
6804 assert(PointerOrReference.is(tok::star));
6805 return Style.PointerAlignment;
This file implements a token annotator, i.e.
Defines the clang::TokenKind enum and support functions.
#define TRANSFORM_TYPE_TRAIT_DEF(Enum, _)
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
Parser - This implements a parser for the C family of languages.
PRESERVE_NONE bool Ret(InterpState &S)
TokenKind
Provides a simple uniform namespace for tokens from all C languages.
The JSON file list parser is used to communicate input to InstallAPI.
AccessSpecifier
A C++ access specifier (public, private, protected), plus the special value "none" which means differ...
nullptr
This class represents a compute construct, representing a 'Kind' of ‘parallel’, 'serial',...
@ Parameter
The parameter type of a method or function.
@ Result
The result type of a method or function.
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.