18#include "llvm/ADT/SmallPtrSet.h"
19#include "llvm/Support/Debug.h"
21#define DEBUG_TYPE "format-token-annotator"
27 const FormatStyle &Style) {
28 switch (Style.BreakAfterAttributes) {
29 case FormatStyle::ABS_Always:
31 case FormatStyle::ABS_Never:
34 return Tok.NewlinesBefore > 0;
42static bool startsWithInitStatement(
const AnnotatedLine &
Line) {
43 return Line.startsWith(tok::kw_for) ||
Line.startsWith(tok::kw_if) ||
44 Line.startsWith(tok::kw_switch);
59 return Tok.Tok.getIdentifierInfo();
65static bool isLambdaParameterList(
const FormatToken *Left) {
67 if (
Left->Previous &&
Left->Previous->is(tok::greater) &&
68 Left->Previous->MatchingParen &&
69 Left->Previous->MatchingParen->is(TT_TemplateOpener)) {
70 Left =
Left->Previous->MatchingParen;
74 return Left->Previous &&
Left->Previous->is(tok::r_square) &&
75 Left->Previous->MatchingParen &&
76 Left->Previous->MatchingParen->is(TT_LambdaLSquare);
82 return Tok.isOneOf(tok::kw_if, tok::kw_for, tok::kw_while, tok::kw_switch,
83 tok::kw_constexpr, tok::kw_catch);
88 if (!IsCpp || !
Tok.startsSequence(tok::l_square, tok::l_square))
91 if (
Tok.Previous &&
Tok.Previous->is(tok::at))
98 if (AttrTok->startsSequence(tok::kw_using, tok::identifier, tok::colon))
100 if (AttrTok->isNot(tok::identifier))
102 while (AttrTok && !AttrTok->startsSequence(tok::r_square, tok::r_square)) {
106 if (AttrTok->is(tok::colon) ||
107 AttrTok->startsSequence(tok::identifier, tok::identifier) ||
108 AttrTok->startsSequence(tok::r_paren, tok::identifier)) {
111 if (AttrTok->is(tok::ellipsis))
113 AttrTok = AttrTok->Next;
115 return AttrTok && AttrTok->startsSequence(tok::r_square, tok::r_square);
123class AnnotatingParser {
125 AnnotatingParser(
const FormatStyle &Style, AnnotatedLine &Line,
126 const AdditionalKeywords &Keywords,
127 SmallVector<ScopeType> &Scopes)
128 : Style(Style), Line(Line), CurrentToken(Line.
First), AutoFound(
false),
130 Keywords(Keywords), Scopes(Scopes), TemplateDeclarationDepth(0) {
131 Contexts.push_back(Context(tok::unknown, 1,
false));
132 resetTokenMetadata();
137 switch (Token.getType()) {
139 case TT_StructLBrace:
142 case TT_CompoundRequirementLBrace:
153 auto *
Left = CurrentToken->Previous;
157 if (NonTemplateLess.count(Left) > 0)
160 const auto *BeforeLess =
Left->Previous;
163 if (BeforeLess->Tok.isLiteral())
165 if (BeforeLess->is(tok::r_brace))
167 if (BeforeLess->is(tok::r_paren) && Contexts.size() > 1 &&
168 !(BeforeLess->MatchingParen &&
169 BeforeLess->MatchingParen->is(TT_OverloadedOperatorLParen))) {
172 if (BeforeLess->is(tok::kw_operator) && CurrentToken->is(tok::l_paren))
176 Left->ParentBracket = Contexts.back().ContextKind;
177 ScopedContextCreator ContextCreator(*
this, tok::less, 12);
178 Contexts.back().IsExpression =
false;
182 if (BeforeLess && BeforeLess->isNot(tok::kw_template))
183 Contexts.back().ContextType = Context::TemplateArgument;
185 if (Style.isJava() && CurrentToken->is(tok::question))
188 for (
bool SeenTernaryOperator =
false, MaybeAngles =
true; CurrentToken;) {
189 const auto &ParentContext = Contexts[Contexts.size() - 2];
190 const bool InExpr = ParentContext.IsExpression;
191 if (CurrentToken->is(tok::greater)) {
192 const auto *
Next = CurrentToken->Next;
193 if (CurrentToken->isNot(TT_TemplateCloser)) {
200 if (
Next &&
Next->is(tok::greater) &&
201 Left->ParentBracket != tok::less &&
202 CurrentToken->getStartOfNonWhitespace() ==
203 Next->getStartOfNonWhitespace().getLocWithOffset(-1)) {
206 if (InExpr && SeenTernaryOperator &&
207 (!
Next ||
Next->isNoneOf(tok::l_paren, tok::l_brace))) {
212 if (ParentContext.InStaticAssertFirstArgument &&
Next &&
213 Next->isOneOf(tok::minus, tok::identifier)) {
217 Left->MatchingParen = CurrentToken;
218 CurrentToken->MatchingParen =
Left;
224 if (Style.isTextProto() ||
225 (Style.Language == FormatStyle::LK_Proto && BeforeLess &&
226 BeforeLess->isOneOf(TT_SelectorName, TT_DictLiteral))) {
227 CurrentToken->setType(TT_DictLiteral);
229 CurrentToken->setType(TT_TemplateCloser);
230 CurrentToken->Tok.setLength(1);
237 if (BeforeLess && BeforeLess->is(TT_TemplateName)) {
241 if (CurrentToken->is(tok::question) && Style.isJava()) {
245 if (CurrentToken->isOneOf(tok::r_paren, tok::r_square, tok::r_brace))
247 const auto &Prev = *CurrentToken->Previous;
254 if (MaybeAngles && InExpr && !Line.startsWith(tok::kw_template) &&
255 Prev.is(TT_BinaryOperator) &&
256 Prev.isOneOf(tok::pipepipe, tok::ampamp)) {
259 if (Prev.isOneOf(tok::question, tok::colon) && !Style.isProto())
260 SeenTernaryOperator =
true;
261 updateParameterCount(Left, CurrentToken);
262 if (Style.Language == FormatStyle::LK_Proto) {
264 if (CurrentToken->is(tok::colon) ||
265 (CurrentToken->isOneOf(tok::l_brace, tok::less) &&
270 }
else if (Style.isTableGen()) {
271 if (CurrentToken->isOneOf(tok::comma, tok::equal)) {
278 if (!parseTableGenValue())
288 bool parseUntouchableParens() {
289 while (CurrentToken) {
290 CurrentToken->Finalized =
true;
291 switch (CurrentToken->Tok.getKind()) {
294 if (!parseUntouchableParens())
309 bool parseParens(
bool IsIf =
false) {
312 assert(CurrentToken->Previous &&
"Unknown previous token");
313 FormatToken &OpeningParen = *CurrentToken->Previous;
314 assert(OpeningParen.is(tok::l_paren));
315 FormatToken *PrevNonComment = OpeningParen.getPreviousNonComment();
316 OpeningParen.ParentBracket = Contexts.back().ContextKind;
317 ScopedContextCreator ContextCreator(*
this, tok::l_paren, 1);
320 Contexts.back().ColonIsForRangeExpr =
321 Contexts.size() == 2 && Contexts[0].ColonIsForRangeExpr;
323 if (OpeningParen.Previous &&
324 OpeningParen.Previous->is(TT_UntouchableMacroFunc)) {
325 OpeningParen.Finalized =
true;
326 return parseUntouchableParens();
329 bool StartsObjCSelector =
false;
330 if (!Style.isVerilog()) {
331 if (
FormatToken *MaybeSel = OpeningParen.Previous) {
333 if (MaybeSel->is(tok::objc_selector) && MaybeSel->Previous &&
334 MaybeSel->Previous->is(tok::at)) {
335 StartsObjCSelector =
true;
340 if (OpeningParen.is(TT_OverloadedOperatorLParen)) {
343 while (Prev->isNot(tok::kw_operator)) {
344 Prev = Prev->Previous;
345 assert(Prev &&
"Expect a kw_operator prior to the OperatorLParen!");
351 bool OperatorCalledAsMemberFunction =
352 Prev->Previous && Prev->Previous->isOneOf(tok::period, tok::arrow);
353 Contexts.back().IsExpression = OperatorCalledAsMemberFunction;
354 }
else if (OpeningParen.is(TT_VerilogInstancePortLParen)) {
355 Contexts.back().IsExpression =
true;
356 Contexts.back().ContextType = Context::VerilogInstancePortList;
357 }
else if (Style.isJavaScript() &&
358 (Line.startsWith(Keywords.kw_type, tok::identifier) ||
359 Line.startsWith(tok::kw_export, Keywords.kw_type,
363 Contexts.back().IsExpression =
false;
364 }
else if (OpeningParen.Previous &&
365 (OpeningParen.Previous->isOneOf(
366 tok::kw_noexcept, tok::kw_explicit, tok::kw_while,
367 tok::l_paren, tok::comma, TT_CastRParen,
368 TT_BinaryOperator) ||
369 OpeningParen.Previous->isIf())) {
371 Contexts.back().IsExpression =
true;
372 }
else if (Style.isJavaScript() && OpeningParen.Previous &&
373 (OpeningParen.Previous->is(Keywords.kw_function) ||
374 (OpeningParen.Previous->endsSequence(tok::identifier,
375 Keywords.kw_function)))) {
377 Contexts.back().IsExpression =
false;
378 }
else if (Style.isJavaScript() && OpeningParen.Previous &&
379 OpeningParen.Previous->is(TT_JsTypeColon)) {
381 Contexts.back().IsExpression =
false;
382 }
else if (isLambdaParameterList(&OpeningParen)) {
384 OpeningParen.setType(TT_LambdaDefinitionLParen);
385 Contexts.back().IsExpression =
false;
386 }
else if (OpeningParen.is(TT_RequiresExpressionLParen)) {
387 Contexts.back().IsExpression =
false;
388 }
else if (OpeningParen.Previous &&
389 OpeningParen.Previous->is(tok::kw__Generic)) {
390 Contexts.back().ContextType = Context::C11GenericSelection;
391 Contexts.back().IsExpression =
true;
392 }
else if (OpeningParen.Previous &&
393 OpeningParen.Previous->TokenText ==
"Q_PROPERTY") {
394 Contexts.back().ContextType = Context::QtProperty;
395 Contexts.back().IsExpression =
false;
396 }
else if (Line.InPPDirective &&
397 (!OpeningParen.Previous ||
398 OpeningParen.Previous->isNot(tok::identifier))) {
399 Contexts.back().IsExpression =
true;
400 }
else if (Contexts[Contexts.size() - 2].CaretFound) {
402 Contexts.back().IsExpression =
false;
403 }
else if (OpeningParen.Previous &&
404 OpeningParen.Previous->is(TT_ForEachMacro)) {
406 Contexts.back().ContextType = Context::ForEachMacro;
407 Contexts.back().IsExpression =
false;
408 }
else if (OpeningParen.Previous && OpeningParen.Previous->MatchingParen &&
409 OpeningParen.Previous->MatchingParen->isOneOf(
410 TT_ObjCBlockLParen, TT_FunctionTypeLParen)) {
411 Contexts.back().IsExpression =
false;
412 }
else if (!Line.MustBeDeclaration &&
413 (!Line.InPPDirective || (Line.InMacroBody && !Scopes.empty()))) {
415 OpeningParen.Previous &&
416 OpeningParen.Previous->isOneOf(tok::kw_for, tok::kw_catch);
417 Contexts.back().IsExpression = !IsForOrCatch;
420 if (Style.isTableGen()) {
422 if (Prev->is(TT_TableGenCondOperator)) {
423 Contexts.back().IsTableGenCondOpe =
true;
424 Contexts.back().IsExpression =
true;
425 }
else if (Contexts.size() > 1 &&
426 Contexts[Contexts.size() - 2].IsTableGenBangOpe) {
431 Contexts.back().IsTableGenBangOpe =
true;
432 Contexts.back().IsExpression =
true;
435 if (!parseTableGenDAGArg())
437 return parseTableGenDAGArgAndList(&OpeningParen);
444 if (PrevNonComment && OpeningParen.is(TT_Unknown)) {
445 if (PrevNonComment->isAttribute()) {
446 OpeningParen.setType(TT_AttributeLParen);
447 }
else if (PrevNonComment->isOneOf(TT_TypenameMacro, tok::kw_decltype,
450#include
"clang/Basic/TransformTypeTraits.def"
452 OpeningParen.setType(TT_TypeDeclarationParen);
454 if (PrevNonComment->isOneOf(tok::kw_decltype, tok::kw_typeof))
455 Contexts.back().IsExpression =
true;
459 if (StartsObjCSelector)
460 OpeningParen.setType(TT_ObjCSelector);
462 const bool IsStaticAssert =
463 PrevNonComment && PrevNonComment->is(tok::kw_static_assert);
465 Contexts.back().InStaticAssertFirstArgument =
true;
475 bool MightBeFunctionType = !Contexts[Contexts.size() - 2].IsExpression;
476 bool ProbablyFunctionType =
477 CurrentToken->isPointerOrReference() || CurrentToken->is(tok::caret);
478 bool HasMultipleLines =
false;
479 bool HasMultipleParametersOnALine =
false;
480 bool MightBeObjCForRangeLoop =
481 OpeningParen.Previous && OpeningParen.Previous->is(tok::kw_for);
483 while (CurrentToken) {
484 const auto &Prev = *CurrentToken->Previous;
485 const auto *PrevPrev = Prev.Previous;
486 if (Prev.is(TT_PointerOrReference) &&
487 PrevPrev->isOneOf(tok::l_paren, tok::coloncolon)) {
488 ProbablyFunctionType =
true;
490 if (CurrentToken->is(tok::comma))
491 MightBeFunctionType =
false;
492 if (Prev.is(TT_BinaryOperator))
493 Contexts.back().IsExpression =
true;
494 if (CurrentToken->is(tok::r_paren)) {
495 if (Prev.is(TT_PointerOrReference) &&
496 (PrevPrev == &OpeningParen || PrevPrev->is(tok::coloncolon))) {
497 MightBeFunctionType =
true;
499 if (OpeningParen.isNot(TT_CppCastLParen) && MightBeFunctionType &&
500 ProbablyFunctionType && CurrentToken->Next &&
501 (CurrentToken->Next->is(tok::l_paren) ||
502 (CurrentToken->Next->is(tok::l_square) &&
503 (Line.MustBeDeclaration ||
504 (PrevNonComment && PrevNonComment->isTypeName(LangOpts)))))) {
505 OpeningParen.setType(OpeningParen.Next->is(tok::caret)
507 : TT_FunctionTypeLParen);
509 OpeningParen.MatchingParen = CurrentToken;
510 CurrentToken->MatchingParen = &OpeningParen;
512 if (CurrentToken->Next && CurrentToken->Next->is(tok::l_brace) &&
513 OpeningParen.Previous && OpeningParen.Previous->is(tok::l_paren)) {
519 if (
Tok->is(TT_BinaryOperator) &&
Tok->isPointerOrReference())
520 Tok->setType(TT_PointerOrReference);
524 if (StartsObjCSelector) {
525 CurrentToken->setType(TT_ObjCSelector);
526 if (Contexts.back().FirstObjCSelectorName) {
527 Contexts.back().FirstObjCSelectorName->LongestObjCSelectorName =
528 Contexts.back().LongestObjCSelectorName;
532 if (OpeningParen.is(TT_AttributeLParen))
533 CurrentToken->setType(TT_AttributeRParen);
534 if (OpeningParen.is(TT_TypeDeclarationParen))
535 CurrentToken->setType(TT_TypeDeclarationParen);
536 if (OpeningParen.Previous &&
537 OpeningParen.Previous->is(TT_JavaAnnotation)) {
538 CurrentToken->setType(TT_JavaAnnotation);
540 if (OpeningParen.Previous &&
541 OpeningParen.Previous->is(TT_LeadingJavaAnnotation)) {
542 CurrentToken->setType(TT_LeadingJavaAnnotation);
545 if (!HasMultipleLines)
547 else if (HasMultipleParametersOnALine)
555 if (CurrentToken->isOneOf(tok::r_square, tok::r_brace))
558 if (CurrentToken->is(tok::l_brace) && OpeningParen.is(TT_ObjCBlockLParen))
559 OpeningParen.setType(TT_Unknown);
560 if (CurrentToken->is(tok::comma) && CurrentToken->Next &&
561 !CurrentToken->Next->HasUnescapedNewline &&
562 !CurrentToken->Next->isTrailingComment()) {
563 HasMultipleParametersOnALine =
true;
565 bool ProbablyFunctionTypeLParen =
566 (CurrentToken->is(tok::l_paren) && CurrentToken->Next &&
567 CurrentToken->Next->isOneOf(tok::star, tok::amp, tok::caret));
568 if ((Prev.isOneOf(tok::kw_const, tok::kw_auto) ||
569 Prev.isTypeName(LangOpts)) &&
570 !(CurrentToken->is(tok::l_brace) ||
571 (CurrentToken->is(tok::l_paren) && !ProbablyFunctionTypeLParen))) {
572 Contexts.back().IsExpression =
false;
574 if (CurrentToken->isOneOf(tok::semi, tok::colon)) {
575 MightBeObjCForRangeLoop =
false;
576 if (PossibleObjCForInToken) {
577 PossibleObjCForInToken->setType(TT_Unknown);
578 PossibleObjCForInToken =
nullptr;
581 if (IsIf && CurrentToken->is(tok::semi)) {
582 for (
auto *
Tok = OpeningParen.Next;
583 Tok != CurrentToken &&
584 Tok->isNoneOf(tok::equal, tok::l_paren, tok::l_brace);
586 if (
Tok->isPointerOrReference())
587 Tok->setFinalizedType(TT_PointerOrReference);
590 if (MightBeObjCForRangeLoop && CurrentToken->is(Keywords.kw_in)) {
591 PossibleObjCForInToken = CurrentToken;
592 PossibleObjCForInToken->setType(TT_ObjCForIn);
596 if (CurrentToken->is(tok::comma)) {
598 Contexts.back().InStaticAssertFirstArgument =
false;
600 Contexts.back().CanBeExpression =
true;
603 if (Style.isTableGen()) {
604 if (CurrentToken->is(tok::comma)) {
605 if (Contexts.back().IsTableGenCondOpe)
606 CurrentToken->setType(TT_TableGenCondOperatorComma);
608 }
else if (CurrentToken->is(tok::colon)) {
609 if (Contexts.back().IsTableGenCondOpe)
610 CurrentToken->setType(TT_TableGenCondOperatorColon);
614 if (!parseTableGenValue())
622 updateParameterCount(&OpeningParen,
Tok);
623 if (CurrentToken && CurrentToken->HasUnescapedNewline)
624 HasMultipleLines =
true;
630 if (!Style.isCSharp())
634 if (
Tok.Previous &&
Tok.Previous->is(tok::identifier))
638 if (
Tok.Previous &&
Tok.Previous->is(tok::r_square)) {
649 if (AttrTok->is(tok::r_square))
653 while (AttrTok && AttrTok->isNot(tok::r_square))
654 AttrTok = AttrTok->Next;
660 AttrTok = AttrTok->Next;
665 if (AttrTok->isAccessSpecifierKeyword() ||
666 AttrTok->isOneOf(tok::comment, tok::kw_class, tok::kw_static,
667 tok::l_square, Keywords.kw_internal)) {
673 AttrTok->Next->startsSequence(tok::identifier, tok::l_paren)) {
689 Left->ParentBracket = Contexts.back().ContextKind;
695 bool CppArrayTemplates =
696 IsCpp && Parent && Parent->is(TT_TemplateCloser) &&
697 (Contexts.back().CanBeExpression || Contexts.back().IsExpression ||
698 Contexts.back().ContextType == Context::TemplateArgument);
700 const bool IsInnerSquare = Contexts.back().InCpp11AttributeSpecifier;
701 const bool IsCpp11AttributeSpecifier =
702 isCppAttribute(IsCpp, *Left) || IsInnerSquare;
705 bool IsCSharpAttributeSpecifier =
706 isCSharpAttributeSpecifier(*Left) ||
707 Contexts.back().InCSharpAttributeSpecifier;
709 bool InsideInlineASM = Line.startsWith(tok::kw_asm);
710 bool IsCppStructuredBinding =
Left->isCppStructuredBinding(IsCpp);
711 bool StartsObjCMethodExpr =
712 !IsCppStructuredBinding && !InsideInlineASM && !CppArrayTemplates &&
713 IsCpp && !IsCpp11AttributeSpecifier && !IsCSharpAttributeSpecifier &&
714 Contexts.back().CanBeExpression &&
Left->isNot(TT_LambdaLSquare) &&
715 CurrentToken->isNoneOf(tok::l_brace, tok::r_square) &&
719 (!Parent || !Parent->is(tok::comma) ||
720 Contexts.back().ContextKind != tok::l_brace) &&
722 Parent->isOneOf(tok::colon, tok::l_square, tok::l_paren,
723 tok::kw_return, tok::kw_throw) ||
724 Parent->isUnaryOperator() ||
726 Parent->isOneOf(TT_ObjCForIn, TT_CastRParen) ||
729 bool ColonFound =
false;
731 unsigned BindingIncrease = 1;
732 if (IsCppStructuredBinding) {
733 Left->setType(TT_StructuredBindingLSquare);
734 }
else if (
Left->is(TT_Unknown)) {
735 if (StartsObjCMethodExpr) {
736 Left->setType(TT_ObjCMethodExpr);
737 }
else if (InsideInlineASM) {
738 Left->setType(TT_InlineASMSymbolicNameLSquare);
739 }
else if (IsCpp11AttributeSpecifier) {
740 if (!IsInnerSquare) {
741 Left->setType(TT_AttributeLSquare);
743 Left->Previous->EndsCppAttributeGroup =
false;
745 }
else if (Style.isJavaScript() && Parent &&
746 Contexts.back().ContextKind == tok::l_brace &&
747 Parent->isOneOf(tok::l_brace, tok::comma)) {
748 Left->setType(TT_JsComputedPropertyName);
749 }
else if (IsCpp && Contexts.back().ContextKind == tok::l_brace &&
750 Parent && Parent->isOneOf(tok::l_brace, tok::comma)) {
751 Left->setType(TT_DesignatedInitializerLSquare);
752 }
else if (IsCSharpAttributeSpecifier) {
753 Left->setType(TT_AttributeLSquare);
754 }
else if (CurrentToken->is(tok::r_square) && Parent &&
755 Parent->is(TT_TemplateCloser)) {
756 Left->setType(TT_ArraySubscriptLSquare);
757 }
else if (Style.isProto()) {
784 Left->setType(TT_ArrayInitializerLSquare);
785 if (!
Left->endsSequence(tok::l_square, tok::numeric_constant,
787 !
Left->endsSequence(tok::l_square, tok::numeric_constant,
789 !
Left->endsSequence(tok::l_square, tok::colon, TT_SelectorName)) {
790 Left->setType(TT_ProtoExtensionLSquare);
791 BindingIncrease = 10;
793 }
else if (!CppArrayTemplates && Parent &&
794 Parent->isOneOf(TT_BinaryOperator, TT_TemplateCloser, tok::at,
795 tok::comma, tok::l_paren, tok::l_square,
796 tok::question, tok::colon, tok::kw_return,
799 Left->setType(TT_ArrayInitializerLSquare);
801 BindingIncrease = 10;
802 Left->setType(TT_ArraySubscriptLSquare);
806 ScopedContextCreator ContextCreator(*
this, tok::l_square, BindingIncrease);
807 Contexts.back().IsExpression =
true;
808 if (Style.isJavaScript() && Parent && Parent->is(TT_JsTypeColon))
809 Contexts.back().IsExpression =
false;
811 Contexts.back().ColonIsObjCMethodExpr = StartsObjCMethodExpr;
812 Contexts.back().InCpp11AttributeSpecifier = IsCpp11AttributeSpecifier;
813 Contexts.back().InCSharpAttributeSpecifier = IsCSharpAttributeSpecifier;
815 while (CurrentToken) {
816 if (CurrentToken->is(tok::r_square)) {
817 if (IsCpp11AttributeSpecifier && !IsInnerSquare) {
818 CurrentToken->setType(TT_AttributeRSquare);
819 CurrentToken->EndsCppAttributeGroup =
true;
821 if (IsCSharpAttributeSpecifier) {
822 CurrentToken->setType(TT_AttributeRSquare);
823 }
else if (((CurrentToken->Next &&
824 CurrentToken->Next->is(tok::l_paren)) ||
825 (CurrentToken->Previous &&
826 CurrentToken->Previous->Previous == Left)) &&
827 Left->is(TT_ObjCMethodExpr)) {
832 StartsObjCMethodExpr =
false;
833 Left->setType(TT_Unknown);
835 if (StartsObjCMethodExpr && CurrentToken->Previous != Left) {
836 CurrentToken->setType(TT_ObjCMethodExpr);
839 if (!ColonFound && CurrentToken->Previous &&
840 CurrentToken->Previous->is(TT_Unknown) &&
841 canBeObjCSelectorComponent(*CurrentToken->Previous)) {
842 CurrentToken->Previous->setType(TT_SelectorName);
847 if (Parent && Parent->is(TT_PointerOrReference))
848 Parent->overwriteFixedType(TT_BinaryOperator);
850 Left->MatchingParen = CurrentToken;
851 CurrentToken->MatchingParen =
Left;
856 if (!Contexts.back().FirstObjCSelectorName) {
859 Previous->ObjCSelectorNameParts = 1;
860 Contexts.back().FirstObjCSelectorName =
Previous;
863 Left->ParameterCount =
864 Contexts.back().FirstObjCSelectorName->ObjCSelectorNameParts;
866 if (Contexts.back().FirstObjCSelectorName) {
867 Contexts.back().FirstObjCSelectorName->LongestObjCSelectorName =
868 Contexts.back().LongestObjCSelectorName;
869 if (
Left->BlockParameterCount > 1)
870 Contexts.back().FirstObjCSelectorName->LongestObjCSelectorName = 0;
872 if (Style.isTableGen() &&
Left->is(TT_TableGenListOpener))
873 CurrentToken->setType(TT_TableGenListCloser);
877 if (CurrentToken->isOneOf(tok::r_paren, tok::r_brace))
879 if (CurrentToken->is(tok::colon)) {
880 if (IsCpp11AttributeSpecifier &&
881 CurrentToken->endsSequence(tok::colon, tok::identifier,
885 CurrentToken->setType(TT_AttributeColon);
886 }
else if (!Style.isVerilog() && !Line.InPragmaDirective &&
887 Left->isOneOf(TT_ArraySubscriptLSquare,
888 TT_DesignatedInitializerLSquare)) {
889 Left->setType(TT_ObjCMethodExpr);
890 StartsObjCMethodExpr =
true;
891 Contexts.back().ColonIsObjCMethodExpr =
true;
892 if (Parent && Parent->is(tok::r_paren)) {
894 Parent->setType(TT_CastRParen);
899 if (CurrentToken->is(tok::comma) &&
Left->is(TT_ObjCMethodExpr) &&
901 Left->setType(TT_ArrayInitializerLSquare);
904 if (Style.isTableGen()) {
905 if (CurrentToken->isOneOf(tok::comma, tok::minus, tok::ellipsis)) {
911 if (!parseTableGenValue())
914 updateParameterCount(Left,
Tok);
919 updateParameterCount(Left,
Tok);
924 void skipToNextNonComment() {
926 while (CurrentToken && CurrentToken->is(tok::comment))
935 bool parseTableGenValue(
bool ParseNameMode =
false) {
938 while (CurrentToken->is(tok::comment))
940 if (!parseTableGenSimpleValue())
945 if (CurrentToken->is(tok::hash)) {
946 if (CurrentToken->Next &&
947 CurrentToken->Next->isOneOf(tok::colon, tok::semi, tok::l_brace)) {
950 CurrentToken->setType(TT_TableGenTrailingPasteOperator);
955 skipToNextNonComment();
956 HashTok->setType(TT_Unknown);
957 if (!parseTableGenValue(ParseNameMode))
964 if (ParseNameMode && CurrentToken->is(tok::l_brace))
967 if (CurrentToken->isOneOf(tok::l_brace, tok::l_square, tok::period)) {
968 CurrentToken->setType(TT_TableGenValueSuffix);
970 skipToNextNonComment();
971 if (Suffix->is(tok::l_square))
972 return parseSquare();
973 if (Suffix->is(tok::l_brace)) {
974 Scopes.push_back(getScopeType(*Suffix));
984 bool tryToParseTableGenTokVar() {
987 if (CurrentToken->is(tok::identifier) &&
988 CurrentToken->TokenText.front() ==
'$') {
989 skipToNextNonComment();
997 bool parseTableGenDAGArg(
bool AlignColon =
false) {
998 if (tryToParseTableGenTokVar())
1000 if (parseTableGenValue()) {
1001 if (CurrentToken && CurrentToken->is(tok::colon)) {
1003 CurrentToken->setType(TT_TableGenDAGArgListColonToAlign);
1005 CurrentToken->setType(TT_TableGenDAGArgListColon);
1006 skipToNextNonComment();
1007 return tryToParseTableGenTokVar();
1018 auto &Opes = Style.TableGenBreakingDAGArgOperators;
1023 if (
Tok.isNot(tok::identifier) ||
1024 Tok.isOneOf(TT_TableGenBangOperator, TT_TableGenCondOperator)) {
1028 if (!
Tok.Next ||
Tok.Next->is(tok::colon))
1030 return llvm::is_contained(Opes,
Tok.TokenText.str());
1035 bool parseTableGenDAGArgAndList(
FormatToken *Opener) {
1037 if (!parseTableGenDAGArg())
1039 bool BreakInside =
false;
1040 if (Style.TableGenBreakInsideDAGArg != FormatStyle::DAS_DontBreak) {
1043 if (isTableGenDAGArgBreakingOperator(*FirstTok)) {
1046 Opener->setType(TT_TableGenDAGArgOpenerToBreak);
1047 if (FirstTok->isOneOf(TT_TableGenBangOperator,
1048 TT_TableGenCondOperator)) {
1051 CurrentToken->Previous->setType(TT_TableGenDAGArgOperatorToBreak);
1052 }
else if (FirstTok->is(tok::identifier)) {
1053 if (Style.TableGenBreakInsideDAGArg == FormatStyle::DAS_BreakAll)
1054 FirstTok->setType(TT_TableGenDAGArgOperatorToBreak);
1056 FirstTok->setType(TT_TableGenDAGArgOperatorID);
1061 return parseTableGenDAGArgList(Opener, BreakInside);
1066 bool parseTableGenDAGArgList(
FormatToken *Opener,
bool BreakInside) {
1067 ScopedContextCreator ContextCreator(*
this, tok::l_paren, 0);
1068 Contexts.back().IsTableGenDAGArgList =
true;
1069 bool FirstDAGArgListElm =
true;
1070 while (CurrentToken) {
1071 if (!FirstDAGArgListElm && CurrentToken->is(tok::comma)) {
1072 CurrentToken->setType(BreakInside ? TT_TableGenDAGArgListCommaToBreak
1073 : TT_TableGenDAGArgListComma);
1074 skipToNextNonComment();
1076 if (CurrentToken && CurrentToken->is(tok::r_paren)) {
1077 CurrentToken->setType(TT_TableGenDAGArgCloser);
1078 Opener->MatchingParen = CurrentToken;
1079 CurrentToken->MatchingParen = Opener;
1080 skipToNextNonComment();
1083 if (!parseTableGenDAGArg(
1085 Style.AlignConsecutiveTableGenBreakingDAGArgColons.Enabled)) {
1088 FirstDAGArgListElm =
false;
1093 bool parseTableGenSimpleValue() {
1094 assert(Style.isTableGen());
1098 skipToNextNonComment();
1100 if (
Tok->isOneOf(tok::numeric_constant, tok::string_literal,
1101 TT_TableGenMultiLineString, tok::kw_true, tok::kw_false,
1102 tok::question, tok::kw_int)) {
1106 if (
Tok->is(tok::l_brace)) {
1107 Scopes.push_back(getScopeType(*
Tok));
1108 return parseBrace();
1111 if (
Tok->is(tok::l_square)) {
1112 Tok->setType(TT_TableGenListOpener);
1115 if (
Tok->is(tok::less)) {
1116 CurrentToken->setType(TT_TemplateOpener);
1117 return parseAngle();
1123 if (
Tok->is(tok::l_paren)) {
1124 Tok->setType(TT_TableGenDAGArgOpener);
1126 if (Contexts.back().IsTableGenDAGArgList)
1127 Tok->SpacesRequiredBefore = 1;
1128 return parseTableGenDAGArgAndList(
Tok);
1131 if (
Tok->is(TT_TableGenBangOperator)) {
1132 if (CurrentToken && CurrentToken->is(tok::less)) {
1133 CurrentToken->setType(TT_TemplateOpener);
1134 skipToNextNonComment();
1138 if (!CurrentToken || CurrentToken->isNot(tok::l_paren))
1142 Contexts.back().IsTableGenBangOpe =
true;
1143 bool Result = parseParens();
1144 Contexts.back().IsTableGenBangOpe =
false;
1148 if (
Tok->is(TT_TableGenCondOperator)) {
1149 if (!CurrentToken || CurrentToken->isNot(tok::l_paren))
1152 return parseParens();
1157 if (
Tok->is(tok::identifier)) {
1159 if (CurrentToken && CurrentToken->is(tok::less)) {
1160 CurrentToken->setType(TT_TemplateOpener);
1161 skipToNextNonComment();
1162 return parseAngle();
1170 bool couldBeInStructArrayInitializer()
const {
1171 if (Contexts.size() < 2)
1175 const auto End = std::next(Contexts.rbegin(), 2);
1176 auto Last = Contexts.rbegin();
1179 if (
Last->ContextKind == tok::l_brace)
1181 return Depth == 2 &&
Last->ContextKind != tok::l_brace;
1188 assert(CurrentToken->Previous);
1189 FormatToken &OpeningBrace = *CurrentToken->Previous;
1190 assert(OpeningBrace.is(tok::l_brace));
1191 OpeningBrace.ParentBracket = Contexts.back().ContextKind;
1193 if (Contexts.back().CaretFound)
1194 OpeningBrace.overwriteFixedType(TT_ObjCBlockLBrace);
1195 Contexts.back().CaretFound =
false;
1197 ScopedContextCreator ContextCreator(*
this, tok::l_brace, 1);
1198 Contexts.back().ColonIsDictLiteral =
true;
1200 Contexts.back().IsExpression =
true;
1201 if (Style.isJavaScript() && OpeningBrace.Previous &&
1202 OpeningBrace.Previous->is(TT_JsTypeColon)) {
1203 Contexts.back().IsExpression =
false;
1205 if (Style.isVerilog() &&
1206 (!OpeningBrace.getPreviousNonComment() ||
1207 OpeningBrace.getPreviousNonComment()->isNot(Keywords.kw_apostrophe))) {
1208 Contexts.back().VerilogMayBeConcatenation =
true;
1210 if (Style.isTableGen())
1211 Contexts.back().ColonIsDictLiteral =
false;
1213 unsigned CommaCount = 0;
1214 while (CurrentToken) {
1215 if (CurrentToken->is(tok::r_brace)) {
1216 assert(!Scopes.empty());
1217 assert(Scopes.back() == getScopeType(OpeningBrace));
1219 assert(OpeningBrace.Optional == CurrentToken->Optional);
1220 OpeningBrace.MatchingParen = CurrentToken;
1221 CurrentToken->MatchingParen = &OpeningBrace;
1222 if (Style.AlignArrayOfStructures != FormatStyle::AIAS_None) {
1223 if (OpeningBrace.ParentBracket == tok::l_brace &&
1224 couldBeInStructArrayInitializer() && CommaCount > 0) {
1225 Contexts.back().ContextType = Context::StructArrayInitializer;
1231 if (CurrentToken->isOneOf(tok::r_paren, tok::r_square))
1233 updateParameterCount(&OpeningBrace, CurrentToken);
1234 if (CurrentToken->isOneOf(tok::colon, tok::l_brace, tok::less)) {
1236 if (
Previous->is(TT_JsTypeOptionalQuestion))
1238 if ((CurrentToken->is(tok::colon) && !Style.isTableGen() &&
1239 (!Contexts.back().ColonIsDictLiteral || !IsCpp)) ||
1241 OpeningBrace.setType(TT_DictLiteral);
1242 if (
Previous->Tok.getIdentifierInfo() ||
1243 Previous->is(tok::string_literal)) {
1244 Previous->setType(TT_SelectorName);
1247 if (CurrentToken->is(tok::colon) && OpeningBrace.is(TT_Unknown) &&
1248 !Style.isTableGen()) {
1249 OpeningBrace.setType(TT_DictLiteral);
1250 }
else if (Style.isJavaScript()) {
1251 OpeningBrace.overwriteFixedType(TT_DictLiteral);
1254 bool IsBracedListComma =
false;
1255 if (CurrentToken->is(tok::comma)) {
1256 if (Style.isJavaScript())
1257 OpeningBrace.overwriteFixedType(TT_DictLiteral);
1262 if (!consumeToken())
1264 if (IsBracedListComma)
1265 Contexts.back().IsExpression =
true;
1274 if (Current->is(tok::l_brace) && Current->is(
BK_Block))
1275 ++
Left->BlockParameterCount;
1276 if (Current->is(tok::comma)) {
1277 ++
Left->ParameterCount;
1279 Left->Role.reset(
new CommaSeparatedList(Style));
1280 Left->Role->CommaFound(Current);
1281 }
else if (
Left->ParameterCount == 0 && Current->isNot(tok::comment)) {
1282 Left->ParameterCount = 1;
1286 bool parseConditional() {
1287 while (CurrentToken) {
1288 if (CurrentToken->is(tok::colon) && CurrentToken->is(TT_Unknown)) {
1289 CurrentToken->setType(TT_ConditionalExpr);
1293 if (!consumeToken())
1299 bool parseTemplateDeclaration() {
1300 if (!CurrentToken || CurrentToken->isNot(tok::less))
1303 CurrentToken->setType(TT_TemplateOpener);
1306 TemplateDeclarationDepth++;
1307 const bool WellFormed = parseAngle();
1308 TemplateDeclarationDepth--;
1312 if (CurrentToken && TemplateDeclarationDepth == 0)
1313 CurrentToken->Previous->ClosesTemplateDeclaration =
true;
1318 bool consumeToken() {
1320 const auto *Prev = CurrentToken->getPreviousNonComment();
1321 if (Prev && Prev->is(TT_AttributeRSquare) &&
1322 CurrentToken->isOneOf(tok::kw_if, tok::kw_switch, tok::kw_case,
1323 tok::kw_default, tok::kw_for, tok::kw_while) &&
1325 CurrentToken->MustBreakBefore =
true;
1332 if (
Tok->is(TT_VerilogTableItem))
1335 if (
Tok->is(TT_TableGenMultiLineString))
1337 auto *Prev =
Tok->getPreviousNonComment();
1338 auto *
Next =
Tok->getNextNonComment();
1339 switch (
bool IsIf =
false;
Tok->Tok.getKind()) {
1342 if (!Prev && Line.MustBeDeclaration)
1343 Tok->setType(TT_ObjCMethodSpecifier);
1350 if (
Tok->isTypeFinalized())
1353 if (Style.isJavaScript()) {
1354 if (Contexts.back().ColonIsForRangeExpr ||
1355 (Contexts.size() == 1 &&
1356 Line.First->isNoneOf(tok::kw_enum, tok::kw_case)) ||
1357 Contexts.back().ContextKind == tok::l_paren ||
1358 Contexts.back().ContextKind == tok::l_square ||
1359 (!Contexts.back().IsExpression &&
1360 Contexts.back().ContextKind == tok::l_brace) ||
1361 (Contexts.size() == 1 &&
1362 Line.MustBeDeclaration)) {
1363 Contexts.back().IsExpression =
false;
1364 Tok->setType(TT_JsTypeColon);
1367 }
else if (Style.isCSharp()) {
1368 if (Contexts.back().InCSharpAttributeSpecifier) {
1369 Tok->setType(TT_AttributeColon);
1372 if (Contexts.back().ContextKind == tok::l_paren) {
1373 Tok->setType(TT_CSharpNamedArgumentColon);
1376 }
else if (Style.isVerilog() &&
Tok->isNot(TT_BinaryOperator)) {
1379 if (Keywords.isVerilogEnd(*Prev) || Keywords.isVerilogBegin(*Prev)) {
1380 Tok->setType(TT_VerilogBlockLabelColon);
1381 }
else if (Contexts.back().ContextKind == tok::l_square) {
1382 Tok->setType(TT_BitFieldColon);
1383 }
else if (Contexts.back().ColonIsDictLiteral) {
1384 Tok->setType(TT_DictLiteral);
1385 }
else if (Contexts.size() == 1) {
1389 Tok->setType(TT_CaseLabelColon);
1390 if (Line.Level > 1 || (!Line.InPPDirective && Line.Level > 0))
1395 if (Line.First->isOneOf(Keywords.kw_module, Keywords.kw_import) ||
1396 Line.First->startsSequence(tok::kw_export, Keywords.kw_module) ||
1397 Line.First->startsSequence(tok::kw_export, Keywords.kw_import)) {
1398 Tok->setType(TT_ModulePartitionColon);
1399 }
else if (Line.First->is(tok::kw_asm)) {
1400 Tok->setType(TT_InlineASMColon);
1401 }
else if (Contexts.back().ColonIsDictLiteral || Style.isProto()) {
1402 Tok->setType(TT_DictLiteral);
1403 if (Style.isTextProto())
1404 Prev->setType(TT_SelectorName);
1405 }
else if (Contexts.back().ColonIsObjCMethodExpr ||
1406 Line.startsWith(TT_ObjCMethodSpecifier)) {
1407 Tok->setType(TT_ObjCMethodExpr);
1408 const auto *PrevPrev = Prev->Previous;
1411 bool UnknownIdentifierInMethodDeclaration =
1412 Line.startsWith(TT_ObjCMethodSpecifier) &&
1413 Prev->is(tok::identifier) && Prev->is(TT_Unknown);
1416 !(PrevPrev->is(TT_CastRParen) ||
1417 (PrevPrev->is(TT_ObjCMethodExpr) && PrevPrev->is(tok::colon))) ||
1418 PrevPrev->is(tok::r_square) ||
1419 Contexts.back().LongestObjCSelectorName == 0 ||
1420 UnknownIdentifierInMethodDeclaration) {
1421 Prev->setType(TT_SelectorName);
1422 if (!Contexts.back().FirstObjCSelectorName)
1423 Contexts.back().FirstObjCSelectorName = Prev;
1424 else if (Prev->ColumnWidth > Contexts.back().LongestObjCSelectorName)
1425 Contexts.back().LongestObjCSelectorName = Prev->ColumnWidth;
1426 Prev->ParameterIndex =
1427 Contexts.back().FirstObjCSelectorName->ObjCSelectorNameParts;
1428 ++Contexts.back().FirstObjCSelectorName->ObjCSelectorNameParts;
1430 }
else if (Contexts.back().ColonIsForRangeExpr) {
1431 Tok->setType(TT_RangeBasedForLoopColon);
1432 for (
auto *Token = Prev;
1433 Token && Token->isNoneOf(tok::semi, tok::l_paren);
1434 Token = Token->Previous) {
1435 if (Token->isPointerOrReference())
1436 Token->setFinalizedType(TT_PointerOrReference);
1438 }
else if (Contexts.back().ContextType == Context::C11GenericSelection) {
1439 Tok->setType(TT_GenericSelectionColon);
1440 if (Prev->isPointerOrReference())
1441 Prev->setFinalizedType(TT_PointerOrReference);
1442 }
else if ((CurrentToken && CurrentToken->is(tok::numeric_constant)) ||
1443 (Prev->is(TT_StartOfName) && !Scopes.empty() &&
1445 Tok->setType(TT_BitFieldColon);
1446 }
else if (Contexts.size() == 1 &&
1447 Line.getFirstNonComment()->isNoneOf(tok::kw_enum, tok::kw_case,
1449 !Line.startsWith(tok::kw_typedef, tok::kw_enum)) {
1450 if (Prev->isOneOf(tok::r_paren, tok::kw_noexcept) ||
1451 Prev->ClosesRequiresClause) {
1452 Tok->setType(TT_CtorInitializerColon);
1453 }
else if (Prev->is(tok::kw_try)) {
1455 FormatToken *PrevPrev = Prev->getPreviousNonComment();
1458 if (PrevPrev && PrevPrev->isOneOf(tok::r_paren, tok::kw_noexcept))
1459 Tok->setType(TT_CtorInitializerColon);
1461 Tok->setType(TT_InheritanceColon);
1462 if (Prev->isAccessSpecifierKeyword())
1465 }
else if (canBeObjCSelectorComponent(*Prev) &&
Next &&
1466 (
Next->isOneOf(tok::r_paren, tok::comma) ||
1467 (canBeObjCSelectorComponent(*
Next) &&
Next->Next &&
1468 Next->Next->is(tok::colon)))) {
1471 Tok->setType(TT_ObjCSelector);
1478 if (Style.isJavaScript() && !Contexts.back().IsExpression)
1479 Tok->setType(TT_JsTypeOperator);
1482 if (Style.isTableGen()) {
1484 if (!parseTableGenValue())
1486 if (CurrentToken && CurrentToken->is(Keywords.kw_then))
1491 CurrentToken->isOneOf(tok::kw_constexpr, tok::identifier)) {
1497 if (CurrentToken && CurrentToken->is(tok::l_paren)) {
1499 if (!parseParens(IsIf))
1504 if (Style.isJavaScript()) {
1506 if ((Prev && Prev->is(tok::period)) || (
Next &&
Next->is(tok::colon)))
1509 if (CurrentToken && CurrentToken->is(Keywords.kw_await))
1512 if (IsCpp && CurrentToken && CurrentToken->is(tok::kw_co_await))
1514 Contexts.back().ColonIsForRangeExpr =
true;
1515 if (!CurrentToken || CurrentToken->isNot(tok::l_paren))
1526 if (Prev && Prev->is(tok::r_paren) && Prev->MatchingParen &&
1527 Prev->MatchingParen->is(TT_OverloadedOperatorLParen)) {
1528 Prev->setType(TT_OverloadedOperator);
1529 Prev->MatchingParen->setType(TT_OverloadedOperator);
1530 Tok->setType(TT_OverloadedOperatorLParen);
1533 if (Style.isVerilog()) {
1539 auto IsInstancePort = [&]() {
1548 if (!Prev || !(PrevPrev = Prev->getPreviousNonComment()))
1551 if (Keywords.isVerilogIdentifier(*Prev) &&
1552 Keywords.isVerilogIdentifier(*PrevPrev)) {
1556 if (Prev->is(Keywords.kw_verilogHash) &&
1557 Keywords.isVerilogIdentifier(*PrevPrev)) {
1561 if (Keywords.isVerilogIdentifier(*Prev) && PrevPrev->is(tok::r_paren))
1564 if (Keywords.isVerilogIdentifier(*Prev) && PrevPrev->is(tok::comma)) {
1565 const FormatToken *PrevParen = PrevPrev->getPreviousNonComment();
1566 if (PrevParen && PrevParen->is(tok::r_paren) &&
1567 PrevParen->MatchingParen &&
1568 PrevParen->MatchingParen->is(TT_VerilogInstancePortLParen)) {
1575 if (IsInstancePort())
1576 Tok->setType(TT_VerilogInstancePortLParen);
1581 if (Line.MustBeDeclaration && Contexts.size() == 1 &&
1582 !Contexts.back().IsExpression && !Line.startsWith(TT_ObjCProperty) &&
1583 !Line.startsWith(tok::l_paren) &&
1584 Tok->isNoneOf(TT_TypeDeclarationParen, TT_RequiresExpressionLParen)) {
1586 (!Prev->isAttribute() &&
1587 Prev->isNoneOf(TT_RequiresClause, TT_LeadingJavaAnnotation,
1588 TT_BinaryOperator))) {
1589 Line.MightBeFunctionDecl =
true;
1590 Tok->MightBeFunctionDeclParen =
true;
1595 if (Style.isTableGen())
1596 Tok->setType(TT_TableGenListOpener);
1602 if (
Tok->is(TT_RequiresExpressionLBrace))
1604 }
else if (Style.isTextProto()) {
1605 if (Prev && Prev->isNot(TT_DictLiteral))
1606 Prev->setType(TT_SelectorName);
1608 Scopes.push_back(getScopeType(*
Tok));
1614 Tok->setType(TT_TemplateOpener);
1620 if (Style.isTextProto() ||
1621 (Style.Language == FormatStyle::LK_Proto && Prev &&
1622 Prev->isOneOf(TT_SelectorName, TT_DictLiteral))) {
1623 Tok->setType(TT_DictLiteral);
1624 if (Prev && Prev->isNot(TT_DictLiteral))
1625 Prev->setType(TT_SelectorName);
1627 if (Style.isTableGen())
1628 Tok->setType(TT_TemplateOpener);
1630 Tok->setType(TT_BinaryOperator);
1631 NonTemplateLess.insert(
Tok);
1641 if (!Scopes.empty())
1648 if (!Style.isTextProto() &&
Tok->is(TT_Unknown))
1649 Tok->setType(TT_BinaryOperator);
1650 if (Prev && Prev->is(TT_TemplateCloser))
1651 Tok->SpacesRequiredBefore = 1;
1653 case tok::kw_operator:
1654 if (Style.isProto())
1657 if (IsCpp && CurrentToken) {
1658 const auto *Info = CurrentToken->Tok.getIdentifierInfo();
1660 if (Info && !(CurrentToken->isPlacementOperator() ||
1661 CurrentToken->is(tok::kw_co_await) ||
1662 Info->isCPlusPlusOperatorKeyword())) {
1664 if (CurrentToken->startsSequence(tok::kw_decltype, tok::l_paren,
1665 tok::kw_auto, tok::r_paren)) {
1667 LParen = CurrentToken->Next->Next->Next->Next;
1670 for (LParen = CurrentToken->Next;
1671 LParen && LParen->isNot(tok::l_paren); LParen = LParen->Next) {
1672 if (LParen->isPointerOrReference())
1673 LParen->setFinalizedType(TT_PointerOrReference);
1676 if (LParen && LParen->is(tok::l_paren)) {
1677 if (!Contexts.back().IsExpression) {
1678 Tok->setFinalizedType(TT_FunctionDeclarationName);
1679 LParen->setFinalizedType(TT_FunctionDeclarationLParen);
1685 while (CurrentToken &&
1686 CurrentToken->isNoneOf(tok::l_paren, tok::semi, tok::r_paren)) {
1687 if (CurrentToken->isOneOf(tok::star, tok::amp))
1688 CurrentToken->setType(TT_PointerOrReference);
1689 auto Next = CurrentToken->getNextNonComment();
1692 if (
Next->is(tok::less))
1698 auto Previous = CurrentToken->getPreviousNonComment();
1700 if (CurrentToken->is(tok::comma) &&
Previous->isNot(tok::kw_operator))
1702 if (
Previous->isOneOf(TT_BinaryOperator, TT_UnaryOperator, tok::comma,
1704 Previous->isPointerOrReference() ||
1706 Previous->TokenText.starts_with(
"\"\"")) {
1707 Previous->setType(TT_OverloadedOperator);
1708 if (CurrentToken->isOneOf(tok::less, tok::greater))
1712 if (CurrentToken && CurrentToken->is(tok::l_paren))
1713 CurrentToken->setType(TT_OverloadedOperatorLParen);
1714 if (CurrentToken && CurrentToken->Previous->is(TT_BinaryOperator))
1715 CurrentToken->Previous->setType(TT_OverloadedOperator);
1718 if (Style.isJavaScript() &&
Next &&
1719 Next->isOneOf(tok::semi, tok::comma, tok::colon, tok::r_paren,
1720 tok::r_brace, tok::r_square)) {
1725 Tok->setType(TT_JsTypeOptionalQuestion);
1730 if (Line.MustBeDeclaration && !Contexts.back().IsExpression &&
1731 Style.isJavaScript()) {
1734 if (Style.isCSharp()) {
1737 if (
Next && (
Next->isOneOf(tok::r_paren, tok::greater) ||
1738 Next->startsSequence(tok::identifier, tok::semi) ||
1739 Next->startsSequence(tok::identifier, tok::equal))) {
1740 Tok->setType(TT_CSharpNullable);
1749 if (!Contexts.back().IsExpression && Line.MustBeDeclaration &&
1750 (!
Next ||
Next->isNoneOf(tok::identifier, tok::string_literal) ||
1751 !
Next->Next ||
Next->Next->isNoneOf(tok::colon, tok::question))) {
1752 Tok->setType(TT_CSharpNullable);
1758 case tok::kw_template:
1759 parseTemplateDeclaration();
1762 switch (Contexts.back().ContextType) {
1763 case Context::CtorInitializer:
1764 Tok->setType(TT_CtorInitializerComma);
1766 case Context::InheritanceList:
1767 Tok->setType(TT_InheritanceComma);
1769 case Context::VerilogInstancePortList:
1770 Tok->setType(TT_VerilogInstancePortComma);
1773 if (Style.isVerilog() && Contexts.size() == 1 &&
1774 Line.startsWith(Keywords.kw_assign)) {
1775 Tok->setFinalizedType(TT_VerilogAssignComma);
1776 }
else if (Contexts.back().FirstStartOfName &&
1777 (Contexts.size() == 1 || startsWithInitStatement(Line))) {
1778 Contexts.back().FirstStartOfName->PartOfMultiVariableDeclStmt =
true;
1779 Line.IsMultiVariableDeclStmt =
true;
1783 if (Contexts.back().ContextType == Context::ForEachMacro)
1784 Contexts.back().IsExpression =
true;
1786 case tok::kw_default:
1788 if (Style.isVerilog() && Keywords.isVerilogEndOfLabel(*
Tok) &&
1789 (Line.Level > 1 || (!Line.InPPDirective && Line.Level > 0))) {
1793 case tok::identifier:
1794 if (
Tok->isOneOf(Keywords.kw___has_include,
1795 Keywords.kw___has_include_next)) {
1799 if (
Next &&
Next->is(tok::l_paren) && Prev &&
1800 Prev->isOneOf(tok::kw___cdecl, tok::kw___stdcall,
1801 tok::kw___fastcall, tok::kw___thiscall,
1802 tok::kw___regcall, tok::kw___vectorcall)) {
1803 Tok->setFinalizedType(TT_FunctionDeclarationName);
1804 Next->setFinalizedType(TT_FunctionDeclarationLParen);
1806 }
else if (Style.isCSharp()) {
1807 if (
Tok->is(Keywords.kw_where) &&
Next &&
Next->isNot(tok::l_paren)) {
1808 Tok->setType(TT_CSharpGenericTypeConstraint);
1809 parseCSharpGenericTypeConstraint();
1811 Line.IsContinuation =
true;
1813 }
else if (Style.isTableGen()) {
1814 if (
Tok->is(Keywords.kw_assert)) {
1815 if (!parseTableGenValue())
1817 }
else if (
Tok->isOneOf(Keywords.kw_def, Keywords.kw_defm) &&
1818 (!
Next ||
Next->isNoneOf(tok::colon, tok::l_brace))) {
1820 if (!parseTableGenValue(
true))
1824 if (Style.AllowBreakBeforeQtProperty &&
1825 Contexts.back().ContextType == Context::QtProperty &&
1826 Tok->isQtProperty()) {
1827 Tok->setFinalizedType(TT_QtProperty);
1831 if (
Tok->isNot(TT_LambdaArrow) && Prev && Prev->is(tok::kw_noexcept))
1832 Tok->setType(TT_TrailingReturnArrow);
1836 if (Style.isTableGen() && !parseTableGenValue())
1845 void parseCSharpGenericTypeConstraint() {
1846 int OpenAngleBracketsCount = 0;
1847 while (CurrentToken) {
1848 if (CurrentToken->is(tok::less)) {
1850 CurrentToken->setType(TT_TemplateOpener);
1851 ++OpenAngleBracketsCount;
1853 }
else if (CurrentToken->is(tok::greater)) {
1854 CurrentToken->setType(TT_TemplateCloser);
1855 --OpenAngleBracketsCount;
1857 }
else if (CurrentToken->is(tok::comma) && OpenAngleBracketsCount == 0) {
1860 CurrentToken->setType(TT_CSharpGenericTypeConstraintComma);
1862 }
else if (CurrentToken->is(Keywords.kw_where)) {
1863 CurrentToken->setType(TT_CSharpGenericTypeConstraint);
1865 }
else if (CurrentToken->is(tok::colon)) {
1866 CurrentToken->setType(TT_CSharpGenericTypeConstraintColon);
1874 void parseIncludeDirective() {
1875 if (CurrentToken && CurrentToken->is(tok::less)) {
1877 while (CurrentToken) {
1880 if (CurrentToken->isNot(tok::comment) &&
1881 !CurrentToken->TokenText.starts_with(
"//")) {
1882 CurrentToken->setType(TT_ImplicitStringLiteral);
1889 void parseWarningOrError() {
1894 while (CurrentToken) {
1895 CurrentToken->setType(TT_ImplicitStringLiteral);
1900 void parsePragma() {
1903 CurrentToken->isOneOf(Keywords.kw_mark, Keywords.kw_option,
1904 Keywords.kw_region)) {
1905 bool IsMarkOrRegion =
1906 CurrentToken->isOneOf(Keywords.kw_mark, Keywords.kw_region);
1909 while (CurrentToken) {
1910 if (IsMarkOrRegion || CurrentToken->Previous->is(TT_BinaryOperator))
1911 CurrentToken->setType(TT_ImplicitStringLiteral);
1917 void parseHasInclude() {
1918 if (!CurrentToken || CurrentToken->isNot(tok::l_paren))
1921 parseIncludeDirective();
1925 LineType parsePreprocessorDirective() {
1926 bool IsFirstToken = CurrentToken->IsFirst;
1932 if (Style.isJavaScript() && IsFirstToken) {
1936 while (CurrentToken) {
1938 CurrentToken->setType(TT_ImplicitStringLiteral);
1944 if (CurrentToken->is(tok::numeric_constant)) {
1945 CurrentToken->SpacesRequiredBefore = 1;
1950 if (!CurrentToken->Tok.getIdentifierInfo())
1954 if (Style.isVerilog() && !Keywords.isVerilogPPDirective(*CurrentToken))
1956 switch (CurrentToken->Tok.getIdentifierInfo()->getPPKeywordID()) {
1957 case tok::pp_include:
1958 case tok::pp_include_next:
1959 case tok::pp_import:
1961 parseIncludeDirective();
1965 case tok::pp_warning:
1966 parseWarningOrError();
1968 case tok::pp_pragma:
1973 Contexts.back().IsExpression =
true;
1976 CurrentToken->SpacesRequiredBefore = 1;
1982 while (CurrentToken) {
1985 if (
Tok->is(tok::l_paren)) {
1987 }
else if (
Tok->isOneOf(Keywords.kw___has_include,
1988 Keywords.kw___has_include_next)) {
1999 NonTemplateLess.clear();
2000 if (!Line.InMacroBody && CurrentToken->is(tok::hash)) {
2004 auto Type = parsePreprocessorDirective();
2012 IdentifierInfo *Info = CurrentToken->Tok.getIdentifierInfo();
2013 if ((Style.isJava() && CurrentToken->is(Keywords.kw_package)) ||
2014 (!Style.isVerilog() && Info &&
2015 Info->getPPKeywordID() == tok::pp_import && CurrentToken->Next &&
2016 CurrentToken->Next->isOneOf(tok::string_literal, tok::identifier,
2019 parseIncludeDirective();
2025 if (CurrentToken->is(tok::less) && Line.Last->is(tok::greater)) {
2026 parseIncludeDirective();
2032 if (Style.Language == FormatStyle::LK_Proto && Line.Level == 0 &&
2033 CurrentToken->isOneOf(Keywords.kw_option, Keywords.kw_package)) {
2035 if (CurrentToken && CurrentToken->is(tok::identifier)) {
2036 while (CurrentToken)
2042 bool KeywordVirtualFound =
false;
2043 bool ImportStatement =
false;
2046 if (Style.isJavaScript() && CurrentToken->is(Keywords.kw_import))
2047 ImportStatement =
true;
2049 while (CurrentToken) {
2050 if (CurrentToken->is(tok::kw_virtual))
2051 KeywordVirtualFound =
true;
2052 if (Style.isJavaScript()) {
2059 if (Line.First->is(tok::kw_export) &&
2060 CurrentToken->is(Keywords.kw_from) && CurrentToken->Next &&
2061 CurrentToken->Next->isStringLiteral()) {
2062 ImportStatement =
true;
2064 if (isClosureImportStatement(*CurrentToken))
2065 ImportStatement =
true;
2067 if (!consumeToken())
2075 if (KeywordVirtualFound)
2077 if (ImportStatement)
2080 if (Line.startsWith(TT_ObjCMethodSpecifier)) {
2081 if (Contexts.back().FirstObjCSelectorName) {
2082 Contexts.back().FirstObjCSelectorName->LongestObjCSelectorName =
2083 Contexts.back().LongestObjCSelectorName;
2088 for (
const auto &ctx : Contexts)
2089 if (ctx.ContextType == Context::StructArrayInitializer)
2099 return Tok.TokenText ==
"goog" &&
Tok.Next &&
Tok.Next->is(tok::period) &&
2101 (
Tok.Next->Next->TokenText ==
"module" ||
2102 Tok.Next->Next->TokenText ==
"provide" ||
2103 Tok.Next->Next->TokenText ==
"require" ||
2104 Tok.Next->Next->TokenText ==
"requireType" ||
2105 Tok.Next->Next->TokenText ==
"forwardDeclare") &&
2106 Tok.Next->Next->Next &&
Tok.Next->Next->Next->is(tok::l_paren);
2109 void resetTokenMetadata() {
2115 if (!CurrentToken->isTypeFinalized() &&
2116 CurrentToken->isNoneOf(
2117 TT_LambdaLSquare, TT_LambdaLBrace, TT_AttributeMacro, TT_IfMacro,
2118 TT_ForEachMacro, TT_TypenameMacro, TT_FunctionLBrace,
2119 TT_ImplicitStringLiteral, TT_InlineASMBrace, TT_FatArrow,
2120 TT_LambdaArrow, TT_NamespaceMacro, TT_OverloadedOperator,
2121 TT_RegexLiteral, TT_TemplateString, TT_ObjCStringLiteral,
2122 TT_UntouchableMacroFunc, TT_StatementAttributeLikeMacro,
2123 TT_FunctionLikeOrFreestandingMacro, TT_ClassLBrace, TT_EnumLBrace,
2124 TT_RecordLBrace, TT_StructLBrace, TT_UnionLBrace, TT_RequiresClause,
2125 TT_RequiresClauseInARequiresExpression, TT_RequiresExpression,
2126 TT_RequiresExpressionLParen, TT_RequiresExpressionLBrace,
2127 TT_CompoundRequirementLBrace, TT_BracedListLBrace,
2128 TT_FunctionLikeMacro)) {
2129 CurrentToken->setType(TT_Unknown);
2131 CurrentToken->Role.reset();
2132 CurrentToken->MatchingParen =
nullptr;
2133 CurrentToken->FakeLParens.clear();
2134 CurrentToken->FakeRParens = 0;
2141 CurrentToken->NestingLevel = Contexts.size() - 1;
2142 CurrentToken->BindingStrength = Contexts.back().BindingStrength;
2143 modifyContext(*CurrentToken);
2144 determineTokenType(*CurrentToken);
2145 CurrentToken = CurrentToken->Next;
2147 resetTokenMetadata();
2155 : ContextKind(ContextKind), BindingStrength(BindingStrength),
2156 IsExpression(IsExpression) {}
2159 unsigned BindingStrength;
2161 unsigned LongestObjCSelectorName = 0;
2162 bool ColonIsForRangeExpr =
false;
2163 bool ColonIsDictLiteral =
false;
2164 bool ColonIsObjCMethodExpr =
false;
2167 bool CanBeExpression =
true;
2168 bool CaretFound =
false;
2169 bool InCpp11AttributeSpecifier =
false;
2170 bool InCSharpAttributeSpecifier =
false;
2171 bool InStaticAssertFirstArgument =
false;
2172 bool VerilogAssignmentFound =
false;
2175 bool VerilogMayBeConcatenation =
false;
2176 bool IsTableGenDAGArgList =
false;
2177 bool IsTableGenBangOpe =
false;
2178 bool IsTableGenCondOpe =
false;
2191 StructArrayInitializer,
2195 C11GenericSelection,
2198 VerilogInstancePortList,
2199 } ContextType = Unknown;
2204 struct ScopedContextCreator {
2205 AnnotatingParser &P;
2207 ScopedContextCreator(AnnotatingParser &P,
tok::TokenKind ContextKind,
2210 P.Contexts.push_back(Context(ContextKind,
2211 P.Contexts.back().BindingStrength + Increase,
2212 P.Contexts.back().IsExpression));
2215 ~ScopedContextCreator() {
2216 if (P.Style.AlignArrayOfStructures != FormatStyle::AIAS_None) {
2217 if (P.Contexts.back().ContextType == Context::StructArrayInitializer) {
2218 P.Contexts.pop_back();
2219 P.Contexts.back().ContextType = Context::StructArrayInitializer;
2223 P.Contexts.pop_back();
2228 auto AssignmentStartsExpression = [&]() {
2232 if (Line.First->isOneOf(tok::kw_using, tok::kw_return))
2234 if (Line.First->is(tok::kw_template)) {
2235 assert(Current.Previous);
2236 if (Current.Previous->is(tok::kw_operator)) {
2244 if (
Tok->isNot(TT_TemplateOpener)) {
2251 if (Contexts.back().ContextKind == tok::less) {
2252 assert(Current.Previous->Previous);
2253 return Current.Previous->Previous->isNoneOf(tok::kw_typename,
2257 Tok =
Tok->MatchingParen;
2260 Tok =
Tok->getNextNonComment();
2264 if (
Tok->isOneOf(tok::kw_class, tok::kw_enum, tok::kw_struct,
2274 if (Style.isJavaScript() &&
2275 (Line.startsWith(Keywords.kw_type, tok::identifier) ||
2276 Line.startsWith(tok::kw_export, Keywords.kw_type,
2277 tok::identifier))) {
2281 return !Current.Previous || Current.Previous->isNot(tok::kw_operator);
2284 if (AssignmentStartsExpression()) {
2285 Contexts.back().IsExpression =
true;
2286 if (!Line.startsWith(TT_UnaryOperator)) {
2289 Previous->Previous->isNoneOf(tok::comma, tok::semi);
2291 if (
Previous->isOneOf(tok::r_square, tok::r_paren, tok::greater)) {
2298 if (
Previous->isOneOf(TT_BinaryOperator, TT_UnaryOperator) &&
2300 Previous->Previous->isNot(tok::equal)) {
2301 Previous->setType(TT_PointerOrReference);
2305 }
else if (Current.is(tok::lessless) &&
2306 (!Current.Previous ||
2307 Current.Previous->isNot(tok::kw_operator))) {
2308 Contexts.back().IsExpression =
true;
2309 }
else if (Current.isOneOf(tok::kw_return, tok::kw_throw)) {
2310 Contexts.back().IsExpression =
true;
2311 }
else if (Current.is(TT_TrailingReturnArrow)) {
2312 Contexts.back().IsExpression =
false;
2313 }
else if (Current.isOneOf(TT_LambdaArrow, Keywords.kw_assert)) {
2314 Contexts.back().IsExpression = Style.isJava();
2315 }
else if (Current.Previous &&
2316 Current.Previous->is(TT_CtorInitializerColon)) {
2317 Contexts.back().IsExpression =
true;
2318 Contexts.back().ContextType = Context::CtorInitializer;
2319 }
else if (Current.Previous && Current.Previous->is(TT_InheritanceColon)) {
2320 Contexts.back().ContextType = Context::InheritanceList;
2321 }
else if (Current.isOneOf(tok::r_paren, tok::greater, tok::comma)) {
2325 Previous->setType(TT_PointerOrReference);
2327 if (Line.MustBeDeclaration &&
2328 Contexts.front().ContextType != Context::CtorInitializer) {
2329 Contexts.back().IsExpression =
false;
2331 }
else if (Current.is(tok::kw_new)) {
2332 Contexts.back().CanBeExpression =
false;
2333 }
else if (Current.is(tok::semi) ||
2334 (Current.is(tok::exclaim) && Current.Previous &&
2335 Current.Previous->isNot(tok::kw_operator))) {
2339 Contexts.back().IsExpression =
true;
2347 if (Current->is(tok::l_paren))
2349 if (Current->is(tok::r_paren))
2353 Current = Current->Next;
2358 static bool isDeductionGuide(
FormatToken &Current) {
2360 if (Current.Previous && Current.Previous->is(tok::r_paren) &&
2361 Current.startsSequence(tok::arrow, tok::identifier, tok::less)) {
2365 while (TemplateCloser) {
2367 if (TemplateCloser->is(tok::l_paren)) {
2369 TemplateCloser = untilMatchingParen(TemplateCloser);
2370 if (!TemplateCloser)
2373 if (TemplateCloser->is(tok::less))
2375 if (TemplateCloser->is(tok::greater))
2379 TemplateCloser = TemplateCloser->Next;
2383 if (TemplateCloser && TemplateCloser->Next &&
2384 TemplateCloser->Next->is(tok::semi) &&
2385 Current.Previous->MatchingParen) {
2389 Current.Previous->MatchingParen->Previous;
2391 return LeadingIdentifier &&
2392 LeadingIdentifier->TokenText == Current.Next->TokenText;
2399 if (Current.isNot(TT_Unknown)) {
2404 if ((Style.isJavaScript() || Style.isCSharp()) &&
2405 Current.is(tok::exclaim)) {
2406 if (Current.Previous) {
2408 Style.isJavaScript()
2409 ? Keywords.isJavaScriptIdentifier(
2410 *Current.Previous,
true)
2411 : Current.Previous->is(tok::identifier);
2413 Current.Previous->isOneOf(
2414 tok::kw_default, tok::kw_namespace, tok::r_paren, tok::r_square,
2415 tok::r_brace, tok::kw_false, tok::kw_true, Keywords.kw_type,
2416 Keywords.kw_get, Keywords.kw_init, Keywords.kw_set) ||
2417 Current.Previous->Tok.isLiteral()) {
2418 Current.setType(TT_NonNullAssertion);
2423 Current.Next->isOneOf(TT_BinaryOperator, Keywords.kw_as)) {
2424 Current.setType(TT_NonNullAssertion);
2432 if ((Style.isJavaScript() || Style.isJava()) &&
2433 Current.is(Keywords.kw_instanceof)) {
2434 Current.setType(TT_BinaryOperator);
2435 }
else if (isStartOfName(Current) &&
2436 (!Line.MightBeFunctionDecl || Current.NestingLevel != 0)) {
2437 Contexts.back().FirstStartOfName = &Current;
2438 Current.setType(TT_StartOfName);
2439 }
else if (Current.is(tok::semi)) {
2443 Contexts.back().FirstStartOfName =
nullptr;
2444 }
else if (Current.isOneOf(tok::kw_auto, tok::kw___auto_type)) {
2446 }
else if (Current.is(tok::arrow) && Style.isJava()) {
2447 Current.setType(TT_LambdaArrow);
2448 }
else if (Current.is(tok::arrow) && Style.isVerilog()) {
2450 Current.setType(TT_BinaryOperator);
2451 }
else if (Current.is(tok::arrow) && AutoFound &&
2452 Line.MightBeFunctionDecl && Current.NestingLevel == 0 &&
2453 Current.Previous->isNoneOf(tok::kw_operator, tok::identifier)) {
2455 Current.setType(TT_TrailingReturnArrow);
2456 }
else if (Current.is(tok::arrow) && Current.Previous &&
2457 Current.Previous->is(tok::r_brace) &&
2461 Current.setType(TT_TrailingReturnArrow);
2462 }
else if (isDeductionGuide(Current)) {
2464 Current.setType(TT_TrailingReturnArrow);
2465 }
else if (Current.isPointerOrReference()) {
2466 Current.setType(determineStarAmpUsage(
2468 (Contexts.back().CanBeExpression && Contexts.back().IsExpression) ||
2469 Contexts.back().InStaticAssertFirstArgument,
2470 Contexts.back().ContextType == Context::TemplateArgument));
2471 }
else if (Current.isOneOf(tok::minus, tok::plus, tok::caret) ||
2472 (Style.isVerilog() && Current.is(tok::pipe))) {
2473 Current.setType(determinePlusMinusCaretUsage(Current));
2474 if (Current.is(TT_UnaryOperator) && Current.is(tok::caret))
2475 Contexts.back().CaretFound =
true;
2476 }
else if (Current.isOneOf(tok::minusminus, tok::plusplus)) {
2477 Current.setType(determineIncrementUsage(Current));
2478 }
else if (Current.isOneOf(tok::exclaim, tok::tilde)) {
2479 Current.setType(TT_UnaryOperator);
2480 }
else if (Current.is(tok::question)) {
2481 if (Style.isJavaScript() && Line.MustBeDeclaration &&
2482 !Contexts.back().IsExpression) {
2485 Current.setType(TT_JsTypeOptionalQuestion);
2486 }
else if (Style.isTableGen()) {
2488 Current.setType(TT_Unknown);
2490 Current.setType(TT_ConditionalExpr);
2492 }
else if (Current.isBinaryOperator() &&
2493 (!Current.Previous || Current.Previous->isNot(tok::l_square)) &&
2494 (Current.isNot(tok::greater) && !Style.isTextProto())) {
2495 if (Style.isVerilog()) {
2496 if (Current.is(tok::lessequal) && Contexts.size() == 1 &&
2497 !Contexts.back().VerilogAssignmentFound) {
2501 Current.setFinalizedType(TT_BinaryOperator);
2504 Contexts.back().VerilogAssignmentFound =
true;
2506 Current.setType(TT_BinaryOperator);
2507 }
else if (Current.is(tok::comment)) {
2508 if (Current.TokenText.starts_with(
"/*")) {
2509 if (Current.TokenText.ends_with(
"*/")) {
2510 Current.setType(TT_BlockComment);
2514 Current.Tok.setKind(tok::unknown);
2517 Current.setType(TT_LineComment);
2519 }
else if (Current.is(tok::string_literal)) {
2520 if (Style.isVerilog() && Contexts.back().VerilogMayBeConcatenation &&
2521 Current.getPreviousNonComment() &&
2522 Current.getPreviousNonComment()->isOneOf(tok::comma, tok::l_brace) &&
2523 Current.getNextNonComment() &&
2524 Current.getNextNonComment()->isOneOf(tok::comma, tok::r_brace)) {
2525 Current.setType(TT_StringInConcatenation);
2527 }
else if (Current.is(tok::l_paren)) {
2528 if (lParenStartsCppCast(Current))
2529 Current.setType(TT_CppCastLParen);
2530 }
else if (Current.is(tok::r_paren)) {
2531 if (rParenEndsCast(Current))
2532 Current.setType(TT_CastRParen);
2533 if (Current.MatchingParen && Current.Next &&
2534 !Current.Next->isBinaryOperator() &&
2535 Current.Next->isNoneOf(
2536 tok::semi, tok::colon, tok::l_brace, tok::l_paren, tok::comma,
2537 tok::period, tok::arrow, tok::coloncolon, tok::kw_noexcept)) {
2538 if (
FormatToken *AfterParen = Current.MatchingParen->Next;
2539 AfterParen && AfterParen->isNot(tok::caret)) {
2541 if (
FormatToken *BeforeParen = Current.MatchingParen->Previous;
2542 BeforeParen && BeforeParen->is(tok::identifier) &&
2543 BeforeParen->isNot(TT_TypenameMacro) &&
2544 BeforeParen->TokenText == BeforeParen->TokenText.upper() &&
2545 (!BeforeParen->Previous ||
2546 BeforeParen->Previous->ClosesTemplateDeclaration ||
2547 BeforeParen->Previous->ClosesRequiresClause)) {
2548 Current.setType(TT_FunctionAnnotationRParen);
2552 }
else if (Current.is(tok::at) && Current.Next && !Style.isJavaScript() &&
2556 switch (Current.Next->Tok.getObjCKeywordID()) {
2557 case tok::objc_interface:
2558 case tok::objc_implementation:
2559 case tok::objc_protocol:
2560 Current.setType(TT_ObjCDecl);
2562 case tok::objc_property:
2563 Current.setType(TT_ObjCProperty);
2568 }
else if (Current.is(tok::period)) {
2569 FormatToken *PreviousNoComment = Current.getPreviousNonComment();
2570 if (PreviousNoComment &&
2571 PreviousNoComment->isOneOf(tok::comma, tok::l_brace)) {
2572 Current.setType(TT_DesignatedInitializerPeriod);
2573 }
else if (Style.isJava() && Current.Previous &&
2574 Current.Previous->isOneOf(TT_JavaAnnotation,
2575 TT_LeadingJavaAnnotation)) {
2576 Current.setType(Current.Previous->getType());
2578 }
else if (canBeObjCSelectorComponent(Current) &&
2581 Current.Previous && Current.Previous->is(TT_CastRParen) &&
2582 Current.Previous->MatchingParen &&
2583 Current.Previous->MatchingParen->Previous &&
2584 Current.Previous->MatchingParen->Previous->is(
2585 TT_ObjCMethodSpecifier)) {
2589 Current.setType(TT_SelectorName);
2590 }
else if (Current.isOneOf(tok::identifier, tok::kw_const, tok::kw_noexcept,
2591 tok::kw_requires) &&
2593 Current.Previous->isNoneOf(tok::equal, tok::at,
2594 TT_CtorInitializerComma,
2595 TT_CtorInitializerColon) &&
2596 Line.MightBeFunctionDecl && Contexts.size() == 1) {
2599 Current.setType(TT_TrailingAnnotation);
2600 }
else if ((Style.isJava() || Style.isJavaScript()) && Current.Previous) {
2601 if (Current.Previous->is(tok::at) &&
2602 Current.isNot(Keywords.kw_interface)) {
2606 Current.setType(TT_LeadingJavaAnnotation);
2608 Current.setType(TT_JavaAnnotation);
2609 }
else if (Current.Previous->is(tok::period) &&
2610 Current.Previous->isOneOf(TT_JavaAnnotation,
2611 TT_LeadingJavaAnnotation)) {
2612 Current.setType(Current.Previous->getType());
2624 if (Style.isVerilog())
2627 if (!
Tok.Previous ||
Tok.isNot(tok::identifier) ||
Tok.is(TT_ClassHeadName))
2630 if (
Tok.endsSequence(Keywords.kw_final, TT_ClassHeadName))
2633 if ((Style.isJavaScript() || Style.isJava()) &&
Tok.is(Keywords.kw_extends))
2636 if (
const auto *NextNonComment =
Tok.getNextNonComment();
2637 (!NextNonComment && !Line.InMacroBody) ||
2639 (NextNonComment->isPointerOrReference() ||
2640 NextNonComment->isOneOf(TT_ClassHeadName, tok::string_literal) ||
2641 (Line.InPragmaDirective && NextNonComment->is(tok::identifier))))) {
2645 if (
Tok.Previous->isOneOf(TT_LeadingJavaAnnotation, Keywords.kw_instanceof,
2649 if (Style.isJavaScript() &&
Tok.Previous->is(Keywords.kw_in))
2656 if (!Style.isJavaScript())
2657 while (PreviousNotConst && PreviousNotConst->is(tok::kw_const))
2658 PreviousNotConst = PreviousNotConst->getPreviousNonComment();
2660 if (!PreviousNotConst)
2663 if (PreviousNotConst->ClosesRequiresClause)
2666 if (Style.isTableGen()) {
2668 if (Keywords.isTableGenDefinition(*PreviousNotConst))
2671 if (Contexts.back().ContextKind != tok::l_brace)
2675 bool IsPPKeyword = PreviousNotConst->is(tok::identifier) &&
2676 PreviousNotConst->Previous &&
2677 PreviousNotConst->Previous->is(tok::hash);
2679 if (PreviousNotConst->is(TT_TemplateCloser)) {
2680 return PreviousNotConst && PreviousNotConst->MatchingParen &&
2681 PreviousNotConst->MatchingParen->Previous &&
2682 PreviousNotConst->MatchingParen->Previous->isNoneOf(
2683 tok::period, tok::kw_template);
2686 if ((PreviousNotConst->is(tok::r_paren) &&
2687 PreviousNotConst->is(TT_TypeDeclarationParen)) ||
2688 PreviousNotConst->is(TT_AttributeRParen)) {
2697 if (PreviousNotConst->isOneOf(tok::identifier, tok::kw_auto) &&
2698 PreviousNotConst->isNot(TT_StatementAttributeLikeMacro)) {
2703 if (PreviousNotConst->is(TT_PointerOrReference) ||
2704 PreviousNotConst->endsSequence(tok::coloncolon,
2705 TT_PointerOrReference)) {
2710 if (PreviousNotConst->isTypeName(LangOpts))
2714 if (Style.isJava() && PreviousNotConst->is(tok::r_square))
2718 return Style.isJavaScript() && PreviousNotConst->is(tok::kw_const);
2728 if (LeftOfParens && LeftOfParens->is(TT_TemplateCloser) &&
2729 LeftOfParens->MatchingParen) {
2730 auto *Prev = LeftOfParens->MatchingParen->getPreviousNonComment();
2732 Prev->isOneOf(tok::kw_const_cast, tok::kw_dynamic_cast,
2733 tok::kw_reinterpret_cast, tok::kw_static_cast)) {
2744 assert(
Tok.is(tok::r_paren));
2746 if (!
Tok.MatchingParen || !
Tok.Previous)
2750 if (!IsCpp && !Style.isCSharp() && !Style.isJava())
2753 const auto *LParen =
Tok.MatchingParen;
2754 const auto *BeforeRParen =
Tok.Previous;
2755 const auto *AfterRParen =
Tok.Next;
2758 if (BeforeRParen == LParen || !AfterRParen)
2761 if (LParen->isOneOf(TT_OverloadedOperatorLParen, TT_FunctionTypeLParen))
2764 auto *LeftOfParens = LParen->getPreviousNonComment();
2768 if (LeftOfParens->is(tok::r_paren) &&
2769 LeftOfParens->isNot(TT_CastRParen)) {
2770 if (!LeftOfParens->MatchingParen ||
2771 !LeftOfParens->MatchingParen->Previous) {
2774 LeftOfParens = LeftOfParens->MatchingParen->Previous;
2777 if (LeftOfParens->is(tok::r_square)) {
2780 if (
Tok->isNot(tok::r_square))
2783 Tok =
Tok->getPreviousNonComment();
2784 if (!
Tok ||
Tok->isNot(tok::l_square))
2787 Tok =
Tok->getPreviousNonComment();
2788 if (!
Tok ||
Tok->isNot(tok::kw_delete))
2792 if (
FormatToken *MaybeDelete = MayBeArrayDelete(LeftOfParens))
2793 LeftOfParens = MaybeDelete;
2799 if (LeftOfParens->Tok.getIdentifierInfo() && LeftOfParens->Previous &&
2800 LeftOfParens->Previous->is(tok::kw_operator)) {
2806 if (LeftOfParens->Tok.getIdentifierInfo() &&
2807 LeftOfParens->isNoneOf(Keywords.kw_in, tok::kw_return, tok::kw_case,
2808 tok::kw_delete, tok::kw_throw)) {
2814 if (LeftOfParens->isOneOf(tok::at, tok::r_square, TT_OverloadedOperator,
2815 TT_TemplateCloser, tok::ellipsis)) {
2820 if (AfterRParen->is(tok::question) ||
2821 (AfterRParen->is(tok::ampamp) && !BeforeRParen->isTypeName(LangOpts))) {
2826 if (AfterRParen->is(Keywords.kw_in) && Style.isCSharp())
2831 if (AfterRParen->isOneOf(tok::kw_noexcept, tok::kw_volatile, tok::kw_const,
2832 tok::kw_requires, tok::kw_throw, tok::arrow,
2833 Keywords.kw_override, Keywords.kw_final) ||
2834 isCppAttribute(IsCpp, *AfterRParen)) {
2840 if (Style.isJava() && AfterRParen->is(tok::l_paren))
2844 if (AfterRParen->isOneOf(tok::kw_sizeof, tok::kw_alignof) ||
2845 (AfterRParen->Tok.isLiteral() &&
2846 AfterRParen->isNot(tok::string_literal))) {
2851 if (
Tok.isNot(TT_TemplateCloser))
2853 const auto *
Less =
Tok.MatchingParen;
2856 const auto *BeforeLess =
Less->getPreviousNonComment();
2857 return BeforeLess && BeforeLess->isNot(TT_VariableTemplate);
2861 auto IsQualifiedPointerOrReference = [](
const FormatToken *T,
2862 const LangOptions &LangOpts) {
2864 assert(!T->isTypeName(LangOpts) &&
"Should have already been checked");
2868 if (T->is(TT_AttributeRParen)) {
2870 assert(T->is(tok::r_paren));
2871 assert(T->MatchingParen);
2872 assert(T->MatchingParen->is(tok::l_paren));
2873 assert(T->MatchingParen->is(TT_AttributeLParen));
2874 if (
const auto *
Tok = T->MatchingParen->Previous;
2875 Tok &&
Tok->isAttribute()) {
2879 }
else if (T->is(TT_AttributeRSquare)) {
2881 if (T->MatchingParen && T->MatchingParen->Previous) {
2882 T = T->MatchingParen->Previous;
2885 }
else if (T->canBePointerOrReferenceQualifier()) {
2891 return T && T->is(TT_PointerOrReference);
2894 bool ParensAreType = IsNonVariableTemplate(*BeforeRParen) ||
2895 BeforeRParen->is(TT_TypeDeclarationParen) ||
2896 BeforeRParen->isTypeName(LangOpts) ||
2897 IsQualifiedPointerOrReference(BeforeRParen, LangOpts);
2898 bool ParensCouldEndDecl =
2899 AfterRParen->isOneOf(tok::equal, tok::semi, tok::l_brace, tok::greater);
2900 if (ParensAreType && !ParensCouldEndDecl)
2911 for (
const auto *Token = LParen->Next; Token != &
Tok; Token = Token->Next)
2912 if (Token->is(TT_BinaryOperator))
2917 if (AfterRParen->isOneOf(tok::identifier, tok::kw_this))
2921 if (AfterRParen->is(tok::l_paren)) {
2922 for (
const auto *Prev = BeforeRParen; Prev->is(tok::identifier);) {
2923 Prev = Prev->Previous;
2924 if (Prev->is(tok::coloncolon))
2925 Prev = Prev->Previous;
2931 if (!AfterRParen->Next)
2936 if (Style.Language != FormatStyle::LK_C && AfterRParen->is(tok::l_brace) &&
2944 const bool NextIsAmpOrStar = AfterRParen->isOneOf(tok::amp, tok::star);
2945 if (!(AfterRParen->isUnaryOperator() || NextIsAmpOrStar) ||
2946 AfterRParen->is(tok::plus) ||
2947 AfterRParen->Next->isNoneOf(tok::identifier, tok::numeric_constant)) {
2951 if (NextIsAmpOrStar &&
2952 (AfterRParen->Next->is(tok::numeric_constant) || Line.InPPDirective)) {
2956 if (Line.InPPDirective && AfterRParen->is(tok::minus))
2959 const auto *Prev = BeforeRParen;
2962 if (Prev->is(tok::r_paren)) {
2963 if (Prev->is(TT_CastRParen))
2965 Prev = Prev->MatchingParen;
2968 Prev = Prev->Previous;
2969 if (!Prev || Prev->isNot(tok::r_paren))
2971 Prev = Prev->MatchingParen;
2972 return Prev && Prev->is(TT_FunctionTypeLParen);
2976 for (Prev = BeforeRParen; Prev != LParen; Prev = Prev->Previous)
2977 if (Prev->isNoneOf(tok::kw_const, tok::identifier, tok::coloncolon))
2995 if (PrevToken->isOneOf(
2996 TT_ConditionalExpr, tok::l_paren, tok::comma, tok::colon, tok::semi,
2997 tok::equal, tok::question, tok::l_square, tok::l_brace,
2998 tok::kw_case, tok::kw_co_await, tok::kw_co_return, tok::kw_co_yield,
2999 tok::kw_delete, tok::kw_return, tok::kw_throw)) {
3006 if (PrevToken->is(tok::kw_sizeof))
3010 if (PrevToken->isOneOf(TT_CastRParen, TT_UnaryOperator))
3014 if (PrevToken->is(TT_BinaryOperator))
3022 bool InTemplateArgument) {
3023 if (Style.isJavaScript())
3024 return TT_BinaryOperator;
3027 if (Style.isCSharp() &&
Tok.is(tok::ampamp))
3028 return TT_BinaryOperator;
3030 if (Style.isVerilog()) {
3035 if (
Tok.is(tok::star))
3036 return TT_BinaryOperator;
3037 return determineUnaryOperatorByUsage(
Tok) ? TT_UnaryOperator
3038 : TT_BinaryOperator;
3043 return TT_UnaryOperator;
3044 if (PrevToken->isTypeName(LangOpts))
3045 return TT_PointerOrReference;
3046 if (PrevToken->isPlacementOperator() &&
Tok.is(tok::ampamp))
3047 return TT_BinaryOperator;
3049 auto *NextToken =
Tok.getNextNonComment();
3051 return TT_PointerOrReference;
3052 if (NextToken->is(tok::greater))
3053 return TT_PointerOrReference;
3055 if (InTemplateArgument && NextToken->is(tok::kw_noexcept))
3056 return TT_BinaryOperator;
3058 if (NextToken->isOneOf(tok::arrow, tok::equal, tok::comma, tok::r_paren,
3059 tok::semi, TT_RequiresClause) ||
3060 (NextToken->is(tok::kw_noexcept) && !IsExpression) ||
3061 NextToken->canBePointerOrReferenceQualifier() ||
3062 (NextToken->is(tok::l_brace) && !NextToken->getNextNonComment())) {
3063 return TT_PointerOrReference;
3066 if (PrevToken->is(tok::coloncolon))
3067 return TT_PointerOrReference;
3069 if (PrevToken->is(tok::r_paren) && PrevToken->is(TT_TypeDeclarationParen))
3070 return TT_PointerOrReference;
3072 if (determineUnaryOperatorByUsage(
Tok))
3073 return TT_UnaryOperator;
3075 if (NextToken->is(tok::l_square) && NextToken->isNot(TT_LambdaLSquare))
3076 return TT_PointerOrReference;
3077 if (NextToken->is(tok::kw_operator) && !IsExpression)
3078 return TT_PointerOrReference;
3090 if (PrevToken->is(tok::r_brace) &&
Tok.is(tok::star) &&
3091 !PrevToken->MatchingParen) {
3092 return TT_PointerOrReference;
3095 if (PrevToken->endsSequence(tok::r_square, tok::l_square, tok::kw_delete))
3096 return TT_UnaryOperator;
3098 if (PrevToken->Tok.isLiteral() ||
3099 PrevToken->isOneOf(tok::r_paren, tok::r_square, tok::kw_true,
3100 tok::kw_false, tok::r_brace)) {
3101 return TT_BinaryOperator;
3105 while (NextNonParen && NextNonParen->is(tok::l_paren))
3106 NextNonParen = NextNonParen->getNextNonComment();
3107 if (NextNonParen && (NextNonParen->Tok.isLiteral() ||
3108 NextNonParen->isOneOf(tok::kw_true, tok::kw_false) ||
3109 NextNonParen->isUnaryOperator())) {
3110 return TT_BinaryOperator;
3116 if (InTemplateArgument && NextToken->Tok.isAnyIdentifier())
3117 return TT_BinaryOperator;
3121 if (
Tok.is(tok::ampamp) &&
3122 NextToken->isOneOf(tok::l_paren, tok::star, tok::amp)) {
3123 return TT_BinaryOperator;
3130 if (NextToken->Tok.isAnyIdentifier()) {
3131 auto *NextNextToken = NextToken->getNextNonComment();
3132 if (NextNextToken) {
3133 if (NextNextToken->is(tok::arrow))
3134 return TT_BinaryOperator;
3135 if (NextNextToken->isPointerOrReference() &&
3136 !NextToken->isObjCLifetimeQualifier(Style)) {
3137 NextNextToken->setFinalizedType(TT_BinaryOperator);
3138 return TT_BinaryOperator;
3145 if (IsExpression && !Contexts.back().CaretFound &&
3146 Line.getFirstNonComment()->isNot(
3147 TT_RequiresClauseInARequiresExpression)) {
3148 return TT_BinaryOperator;
3152 if (!Scopes.empty() && Scopes.back() ==
ST_Class)
3153 return TT_PointerOrReference;
3156 auto IsChainedOperatorAmpOrMember = [](
const FormatToken *token) {
3157 return !token || token->isOneOf(tok::amp, tok::period, tok::arrow,
3158 tok::arrowstar, tok::periodstar);
3163 if (
Tok.is(tok::amp) && PrevToken->Tok.isAnyIdentifier() &&
3164 IsChainedOperatorAmpOrMember(PrevToken->getPreviousNonComment()) &&
3165 NextToken && NextToken->Tok.isAnyIdentifier()) {
3166 if (
auto NextNext = NextToken->getNextNonComment();
3168 (IsChainedOperatorAmpOrMember(NextNext) || NextNext->is(tok::semi))) {
3169 return TT_BinaryOperator;
3175 return TT_BinaryOperator;
3178 return TT_PointerOrReference;
3182 if (determineUnaryOperatorByUsage(
Tok))
3183 return TT_UnaryOperator;
3187 return TT_UnaryOperator;
3189 if (PrevToken->is(tok::at))
3190 return TT_UnaryOperator;
3193 return TT_BinaryOperator;
3199 if (!PrevToken || PrevToken->is(TT_CastRParen))
3200 return TT_UnaryOperator;
3201 if (PrevToken->isOneOf(tok::r_paren, tok::r_square, tok::identifier))
3202 return TT_TrailingUnaryOperator;
3204 return TT_UnaryOperator;
3207 SmallVector<Context, 8> Contexts;
3209 const FormatStyle &Style;
3210 AnnotatedLine &Line;
3214 LangOptions LangOpts;
3215 const AdditionalKeywords &Keywords;
3217 SmallVector<ScopeType> &Scopes;
3223 llvm::SmallPtrSet<FormatToken *, 16> NonTemplateLess;
3225 int TemplateDeclarationDepth;
3233class ExpressionParser {
3235 ExpressionParser(
const FormatStyle &Style,
const AdditionalKeywords &Keywords,
3236 AnnotatedLine &Line)
3237 : Style(Style), Keywords(Keywords), Line(Line), Current(Line.
First) {}
3240 void parse(
int Precedence = 0) {
3243 while (Current && (Current->is(tok::kw_return) ||
3244 (Current->is(tok::colon) &&
3245 Current->isOneOf(TT_ObjCMethodExpr, TT_DictLiteral)))) {
3249 if (!Current || Precedence > PrecedenceArrowAndPeriod)
3254 parseConditionalExpr();
3260 if (Precedence == PrecedenceUnaryOperator) {
3261 parseUnaryOperator();
3278 if (Style.isVerilog() && Precedence ==
prec::Comma) {
3279 VerilogFirstOfType =
3280 verilogGroupDecl(VerilogFirstOfType, LatestOperator);
3284 parse(Precedence + 1);
3286 int CurrentPrecedence = getCurrentPrecedence();
3295 if (Style.BreakBinaryOperations.PerOperator.empty() &&
3296 Style.BreakBinaryOperations.Default ==
3297 FormatStyle::BBO_OnePerLine) {
3302 if (Precedence == CurrentPrecedence && Current &&
3303 Current->is(TT_SelectorName)) {
3305 addFakeParenthesis(Start,
prec::Level(Precedence));
3309 if ((Style.isCSharp() || Style.isJavaScript() || Style.isJava()) &&
3313 FormatToken *Prev = Current->getPreviousNonComment();
3314 if (Prev && Prev->is(tok::string_literal) &&
3315 (Prev == Start || Prev->endsSequence(tok::string_literal, tok::plus,
3316 TT_StringInConcatenation))) {
3317 Prev->setType(TT_StringInConcatenation);
3324 (Current->closesScope() &&
3325 (Current->MatchingParen || Current->is(TT_TemplateString))) ||
3326 (CurrentPrecedence != -1 && CurrentPrecedence < Precedence) ||
3335 if (Current->opensScope() ||
3336 Current->isOneOf(TT_RequiresClause,
3337 TT_RequiresClauseInARequiresExpression)) {
3340 while (Current && (!Current->closesScope() || Current->opensScope())) {
3347 if (CurrentPrecedence == Precedence) {
3349 LatestOperator->NextOperator = Current;
3350 LatestOperator = Current;
3354 next(Precedence > 0);
3359 if (Style.isVerilog() && Precedence ==
prec::Comma && VerilogFirstOfType)
3360 addFakeParenthesis(VerilogFirstOfType,
prec::Comma);
3362 if (LatestOperator && (Current || Precedence > 0)) {
3368 Start->Previous->isOneOf(TT_RequiresClause,
3369 TT_RequiresClauseInARequiresExpression))
3371 auto Ret = Current ? Current : Line.Last;
3372 while (!
Ret->ClosesRequiresClause &&
Ret->Previous)
3378 if (Precedence == PrecedenceArrowAndPeriod) {
3382 addFakeParenthesis(Start,
prec::Level(Precedence), End);
3390 int getCurrentPrecedence() {
3392 const FormatToken *NextNonComment = Current->getNextNonComment();
3393 if (Current->is(TT_ConditionalExpr))
3395 if (NextNonComment && Current->is(TT_SelectorName) &&
3396 (NextNonComment->isOneOf(TT_DictLiteral, TT_JsTypeColon) ||
3397 (Style.isProto() && NextNonComment->is(tok::less)))) {
3400 if (Current->is(TT_JsComputedPropertyName))
3402 if (Current->is(TT_LambdaArrow))
3404 if (Current->is(TT_FatArrow))
3406 if (Current->isOneOf(tok::semi, TT_InlineASMColon, TT_SelectorName) ||
3407 (Current->is(tok::comment) && NextNonComment &&
3408 NextNonComment->is(TT_SelectorName))) {
3411 if (Current->is(TT_RangeBasedForLoopColon))
3413 if ((Style.isJava() || Style.isJavaScript()) &&
3414 Current->is(Keywords.kw_instanceof)) {
3417 if (Style.isJavaScript() &&
3418 Current->isOneOf(Keywords.kw_in, Keywords.kw_as)) {
3421 if (Current->isOneOf(TT_BinaryOperator, tok::comma))
3422 return Current->getPrecedence();
3423 if (Current->isOneOf(tok::period, tok::arrow) &&
3424 Current->isNot(TT_TrailingReturnArrow)) {
3425 return PrecedenceArrowAndPeriod;
3427 if ((Style.isJava() || Style.isJavaScript()) &&
3428 Current->isOneOf(Keywords.kw_extends, Keywords.kw_implements,
3429 Keywords.kw_throws)) {
3434 if (Style.isVerilog() && Current->is(tok::colon))
3446 if (Start->MacroParent)
3449 Start->FakeLParens.push_back(Precedence);
3451 Start->StartsBinaryExpression =
true;
3452 if (!End && Current)
3453 End = Current->getPreviousNonComment();
3457 End->EndsBinaryExpression =
true;
3463 void parseUnaryOperator() {
3464 SmallVector<FormatToken *, 2> Tokens;
3465 while (Current && Current->is(TT_UnaryOperator)) {
3466 Tokens.push_back(Current);
3469 parse(PrecedenceArrowAndPeriod);
3476 void parseConditionalExpr() {
3477 while (Current && Current->isTrailingComment())
3481 if (!Current || Current->isNot(tok::question))
3485 if (!Current || Current->isNot(TT_ConditionalExpr))
3492 void next(
bool SkipPastLeadingComments =
true) {
3494 Current = Current->Next;
3496 (Current->NewlinesBefore == 0 || SkipPastLeadingComments) &&
3497 Current->isTrailingComment()) {
3498 Current = Current->Next;
3512 while (Start->startsSequence(tok::l_paren, tok::star)) {
3513 if (!(Start = Start->MatchingParen) ||
3514 !(Start = Start->getNextNonComment())) {
3521 if (
Tok->is(Keywords.kw_assign))
3522 Tok =
Tok->getNextNonComment();
3534 if (
Tok->is(tok::hash)) {
3539 Tok =
Tok->getNextNonComment();
3540 }
else if (
Tok->is(tok::hashhash)) {
3544 Tok =
Tok->getNextNonComment();
3545 }
else if (Keywords.isVerilogQualifier(*
Tok) ||
3546 Keywords.isVerilogIdentifier(*
Tok)) {
3550 while (
Tok &&
Tok->isOneOf(tok::period, tok::coloncolon) &&
3551 (
Tok =
Tok->getNextNonComment())) {
3552 if (Keywords.isVerilogIdentifier(*
Tok))
3553 Tok =
Tok->getNextNonComment();
3557 }
else if (
Tok->is(tok::l_paren)) {
3562 Keywords.kw_highz0, Keywords.kw_highz1, Keywords.kw_large,
3563 Keywords.kw_medium, Keywords.kw_pull0, Keywords.kw_pull1,
3564 Keywords.kw_small, Keywords.kw_strong0, Keywords.kw_strong1,
3565 Keywords.kw_supply0, Keywords.kw_supply1, Keywords.kw_weak0,
3566 Keywords.kw_weak1)) {
3567 Tok->setType(TT_VerilogStrength);
3568 Tok =
Tok->MatchingParen;
3570 Tok->setType(TT_VerilogStrength);
3571 Tok =
Tok->getNextNonComment();
3576 }
else if (
Tok->is(Keywords.kw_verilogHash)) {
3578 if (
Next->is(tok::l_paren))
3581 Tok =
Next->getNextNonComment();
3590 while (
Tok &&
Tok->is(tok::l_square) && (
Tok =
Tok->MatchingParen))
3591 Tok =
Tok->getNextNonComment();
3592 if (
Tok && (
Tok->is(tok::hash) || Keywords.isVerilogIdentifier(*
Tok)))
3601 First->setType(TT_VerilogDimensionedTypeName);
3602 }
else if (
First != Start) {
3610 if (TypedName->is(TT_Unknown))
3611 TypedName->setType(TT_StartOfName);
3613 if (FirstOfType && PreviousComma) {
3614 PreviousComma->setType(TT_VerilogTypeComma);
3615 addFakeParenthesis(FirstOfType,
prec::Comma, PreviousComma->Previous);
3618 FirstOfType = TypedName;
3625 while (Current && Current != FirstOfType) {
3626 if (Current->opensScope()) {
3637 const FormatStyle &Style;
3638 const AdditionalKeywords &Keywords;
3639 const AnnotatedLine &Line;
3649 assert(
Line->First);
3656 Line->First->OriginalColumn) {
3657 const bool PPDirectiveOrImportStmt =
3660 if (PPDirectiveOrImportStmt)
3665 Line->Level = Style.IndentPPDirectives < FormatStyle::PPDIS_BeforeHash &&
3666 PPDirectiveOrImportStmt
3668 : NextNonCommentLine->
Level;
3670 NextNonCommentLine =
Line->First->isNot(tok::r_brace) ?
Line :
nullptr;
3690 if (
Tok->isNot(tok::identifier))
3693 Tok =
Tok->getNextNonComment();
3699 if (
Tok->is(tok::coloncolon))
3700 return Tok->getNextNonComment();
3704 if (
Tok->is(TT_TemplateOpener)) {
3705 Tok =
Tok->MatchingParen;
3709 Tok =
Tok->getNextNonComment();
3714 return Tok->is(tok::coloncolon) ?
Tok->getNextNonComment() :
nullptr;
3722 Tok =
Tok->getNextNonComment()) {
3724 if (
Tok->is(TT_AttributeLSquare)) {
3725 Tok =
Tok->MatchingParen;
3733 if (
Tok->is(tok::l_paren) &&
Tok->is(TT_Unknown) &&
Tok->MatchingParen) {
3741 if (
Tok->isOneOf(tok::kw_friend, tok::kw_inline, tok::kw_virtual,
3742 tok::kw_constexpr, tok::kw_consteval, tok::kw_explicit)) {
3748 if (
Tok->is(tok::kw_template)) {
3749 Tok =
Tok->getNextNonComment();
3755 if (
Tok->isNot(TT_TemplateOpener))
3758 Tok =
Tok->MatchingParen;
3766 if (
Tok->is(tok::coloncolon)) {
3777 if (
Tok->is(tok::tilde)) {
3784 if (
Tok->isNot(tok::identifier) ||
Tok->isNot(TT_Unknown))
3795 assert(
Tok &&
Tok->is(tok::identifier));
3796 const auto *Prev =
Tok->Previous;
3798 if (Prev && Prev->is(tok::tilde))
3799 Prev = Prev->Previous;
3802 if (!Prev || (!Prev->endsSequence(tok::coloncolon, tok::identifier) &&
3803 !Prev->endsSequence(tok::coloncolon, TT_TemplateCloser))) {
3807 assert(Prev->Previous);
3808 if (Prev->Previous->is(TT_TemplateCloser) && Prev->Previous->MatchingParen) {
3809 Prev = Prev->Previous->MatchingParen;
3810 assert(Prev->Previous);
3813 return Prev->Previous->TokenText ==
Tok->TokenText;
3817 if (!
Line.InMacroBody)
3818 MacroBodyScopes.clear();
3820 auto &ScopeStack =
Line.InMacroBody ? MacroBodyScopes : Scopes;
3821 AnnotatingParser
Parser(Style,
Line, Keywords, ScopeStack);
3824 if (!
Line.Children.empty()) {
3827 for (
auto &Child :
Line.Children) {
3828 if (InRequiresExpression &&
3829 Child->First->isNoneOf(tok::kw_typename, tok::kw_requires,
3830 TT_CompoundRequirementLBrace)) {
3836 if (!ScopeStack.empty())
3837 ScopeStack.pop_back();
3850 ExpressionParser ExprParser(Style, Keywords,
Line);
3856 if (
Tok && ((!ScopeStack.empty() && ScopeStack.back() ==
ST_Class) ||
3858 Tok->setFinalizedType(TT_CtorDtorDeclName);
3859 assert(OpeningParen);
3864 if (
Line.startsWith(TT_ObjCMethodSpecifier))
3866 else if (
Line.startsWith(TT_ObjCDecl))
3868 else if (
Line.startsWith(TT_ObjCProperty))
3872 First->SpacesRequiredBefore = 1;
3873 First->CanBreakBefore =
First->MustBreakBefore;
3882 if (Current.
is(TT_FunctionDeclarationName))
3885 if (Current.
isNoneOf(tok::identifier, tok::kw_operator))
3888 const auto *Prev = Current.getPreviousNonComment();
3893 if (
const auto *PrevPrev =
Previous.getPreviousNonComment();
3894 PrevPrev && PrevPrev->is(TT_ObjCDecl)) {
3898 auto skipOperatorName =
3901 if (
Next->is(TT_OverloadedOperatorLParen))
3903 if (
Next->is(TT_OverloadedOperator))
3905 if (
Next->isPlacementOperator() ||
Next->is(tok::kw_co_await)) {
3908 Next->Next->startsSequence(tok::l_square, tok::r_square)) {
3913 if (
Next->startsSequence(tok::l_square, tok::r_square)) {
3918 if ((
Next->isTypeName(LangOpts) ||
Next->is(tok::identifier)) &&
3919 Next->Next &&
Next->Next->isPointerOrReference()) {
3924 if (
Next->is(TT_TemplateOpener) &&
Next->MatchingParen) {
3935 const bool IsCpp = LangOpts.CXXOperatorNames || LangOpts.C11;
3938 if (Current.
is(tok::kw_operator)) {
3939 if (
Line.startsWith(tok::kw_friend))
3941 if (
Previous.Tok.getIdentifierInfo() &&
3942 Previous.isNoneOf(tok::kw_return, tok::kw_co_return)) {
3947 assert(
Previous.MatchingParen->is(tok::l_paren));
3948 assert(
Previous.MatchingParen->is(TT_TypeDeclarationParen));
3957 while (
Next &&
Next->startsSequence(tok::hashhash, tok::identifier))
3960 if (
Next->is(TT_TemplateOpener) &&
Next->MatchingParen) {
3962 }
else if (
Next->is(tok::coloncolon)) {
3966 if (
Next->is(tok::kw_operator)) {
3967 Next = skipOperatorName(
Next->Next);
3970 if (
Next->isNot(tok::identifier))
3972 }
else if (isCppAttribute(IsCpp, *
Next)) {
3976 }
else if (
Next->is(tok::l_paren)) {
3985 if (!
Next ||
Next->isNot(tok::l_paren) || !
Next->MatchingParen)
3987 ClosingParen =
Next->MatchingParen;
3988 assert(ClosingParen->
is(tok::r_paren));
3990 if (
Line.Last->is(tok::l_brace))
3992 if (
Next->Next == ClosingParen)
3995 if (ClosingParen->
Next && ClosingParen->
Next->
is(TT_PointerOrReference))
4008 if (IsCpp &&
Next->Next &&
Next->Next->is(tok::identifier) &&
4009 !
Line.endsWith(tok::semi)) {
4015 if (
Tok->is(TT_TypeDeclarationParen))
4017 if (
Tok->isOneOf(tok::l_paren, TT_TemplateOpener) &&
Tok->MatchingParen) {
4018 Tok =
Tok->MatchingParen;
4021 if (
Tok->is(tok::kw_const) ||
Tok->isTypeName(LangOpts) ||
4022 Tok->isOneOf(TT_PointerOrReference, TT_StartOfName, tok::ellipsis)) {
4025 if (
Tok->isOneOf(tok::l_brace, TT_ObjCMethodExpr) ||
Tok->Tok.isLiteral())
4031bool TokenAnnotator::mustBreakForReturnType(
const AnnotatedLine &
Line)
const {
4032 assert(
Line.MightBeFunctionDecl);
4034 if ((Style.BreakAfterReturnType == FormatStyle::RTBS_TopLevel ||
4035 Style.BreakAfterReturnType == FormatStyle::RTBS_TopLevelDefinitions) &&
4040 switch (Style.BreakAfterReturnType) {
4041 case FormatStyle::RTBS_None:
4042 case FormatStyle::RTBS_Automatic:
4043 case FormatStyle::RTBS_ExceptShortType:
4045 case FormatStyle::RTBS_All:
4046 case FormatStyle::RTBS_TopLevel:
4048 case FormatStyle::RTBS_AllDefinitions:
4049 case FormatStyle::RTBS_TopLevelDefinitions:
4050 return Line.mightBeFunctionDefinition();
4060 Line.Computed =
true;
4068 :
Line.FirstStartColumn +
First->ColumnWidth;
4069 bool AlignArrayOfStructures =
4070 (Style.AlignArrayOfStructures != FormatStyle::AIAS_None &&
4072 if (AlignArrayOfStructures)
4073 calculateArrayInitializerColumnList(
Line);
4075 const auto *FirstNonComment =
Line.getFirstNonComment();
4076 bool SeenName =
false;
4077 bool LineIsFunctionDeclaration =
false;
4081 for (
auto *
Tok = FirstNonComment && FirstNonComment->isNot(tok::kw_using)
4082 ? FirstNonComment->Next
4085 if (
Tok->is(TT_StartOfName))
4087 if (
Tok->Previous->EndsCppAttributeGroup)
4088 AfterLastAttribute =
Tok;
4089 if (
const bool IsCtorOrDtor =
Tok->is(TT_CtorDtorDeclName);
4093 Tok->setFinalizedType(TT_FunctionDeclarationName);
4094 LineIsFunctionDeclaration =
true;
4098 assert(OpeningParen);
4099 if (OpeningParen->is(TT_Unknown))
4100 OpeningParen->setType(TT_FunctionDeclarationLParen);
4107 if ((LineIsFunctionDeclaration ||
4108 (FirstNonComment && FirstNonComment->is(TT_CtorDtorDeclName))) &&
4109 Line.endsWith(tok::semi, tok::r_brace)) {
4110 auto *
Tok =
Line.Last->Previous;
4111 while (
Tok->isNot(tok::r_brace))
4113 if (
auto *LBrace =
Tok->MatchingParen; LBrace && LBrace->is(TT_Unknown)) {
4114 assert(LBrace->is(tok::l_brace));
4117 LBrace->setFinalizedType(TT_FunctionLBrace);
4121 if (SeenName && AfterLastAttribute &&
4124 if (LineIsFunctionDeclaration)
4125 Line.ReturnTypeWrapped =
true;
4128 if (!LineIsFunctionDeclaration) {
4129 Line.ReturnTypeWrapped =
false;
4131 for (
const auto *
Tok = FirstNonComment;
Tok;
Tok =
Tok->Next) {
4132 if (
Tok->isNot(tok::kw_operator))
4136 }
while (
Tok &&
Tok->isNot(TT_OverloadedOperatorLParen));
4137 if (!
Tok || !
Tok->MatchingParen)
4139 const auto *LeftParen =
Tok;
4140 for (
Tok =
Tok->Next;
Tok &&
Tok != LeftParen->MatchingParen;
4142 if (
Tok->isNot(tok::identifier))
4145 const bool NextIsBinaryOperator =
4147 Next->Next->is(tok::identifier);
4148 if (!NextIsBinaryOperator)
4150 Next->setType(TT_BinaryOperator);
4154 }
else if (ClosingParen) {
4156 if (
Tok->is(TT_CtorInitializerColon))
4158 if (
Tok->is(tok::arrow)) {
4159 Tok->overwriteFixedType(TT_TrailingReturnArrow);
4162 if (
Tok->isNot(TT_TrailingAnnotation))
4165 if (!
Next ||
Next->isNot(tok::l_paren))
4174 if (
First->is(TT_ElseLBrace)) {
4175 First->CanBreakBefore =
true;
4176 First->MustBreakBefore =
true;
4179 bool InFunctionDecl =
Line.MightBeFunctionDecl;
4180 bool InParameterList =
false;
4181 for (
auto *Current =
First->Next; Current; Current = Current->Next) {
4183 if (Current->is(TT_LineComment)) {
4185 Current->SpacesRequiredBefore =
4186 (Style.Cpp11BracedListStyle == FormatStyle::BLS_AlignFirstComment &&
4187 !Style.SpacesInParensOptions.Other)
4190 }
else if (Prev->
is(TT_VerilogMultiLineListLParen)) {
4191 Current->SpacesRequiredBefore = 0;
4193 Current->SpacesRequiredBefore = Style.SpacesBeforeTrailingComments;
4203 if (!Current->HasUnescapedNewline) {
4206 if (
Parameter->isOneOf(tok::comment, tok::r_brace))
4209 if (
Parameter->Previous->isNot(TT_CtorInitializerComma) &&
4217 }
else if (!Current->Finalized && Current->SpacesRequiredBefore == 0 &&
4218 spaceRequiredBefore(
Line, *Current)) {
4219 Current->SpacesRequiredBefore = 1;
4224 Current->MustBreakBefore =
true;
4226 Current->MustBreakBefore =
4227 Current->MustBreakBefore || mustBreakBefore(
Line, *Current);
4228 if (!Current->MustBreakBefore && InFunctionDecl &&
4229 Current->is(TT_FunctionDeclarationName)) {
4230 Current->MustBreakBefore = mustBreakForReturnType(
Line);
4234 Current->CanBreakBefore =
4235 Current->MustBreakBefore || canBreakBefore(
Line, *Current);
4237 if (Current->is(TT_FunctionDeclarationLParen)) {
4238 InParameterList =
true;
4239 }
else if (Current->is(tok::r_paren)) {
4240 const auto *LParen = Current->MatchingParen;
4241 if (LParen && LParen->is(TT_FunctionDeclarationLParen))
4242 InParameterList =
false;
4243 }
else if (InParameterList &&
4244 Current->endsSequence(TT_AttributeMacro,
4245 TT_PointerOrReference)) {
4246 Current->CanBreakBefore =
false;
4249 unsigned ChildSize = 0;
4252 ChildSize = LastOfChild.isTrailingComment() ? Style.ColumnLimit
4255 if (Current->MustBreakBefore || Prev->
Children.size() > 1 ||
4257 Prev->
Children[0]->First->MustBreakBefore) ||
4258 Current->IsMultiline) {
4259 Current->TotalLength = Prev->
TotalLength + Style.ColumnLimit;
4261 Current->TotalLength = Prev->
TotalLength + Current->ColumnWidth +
4262 ChildSize + Current->SpacesRequiredBefore;
4265 if ((Style.PackParameters.BinPack == FormatStyle::BPPS_UseBreakAfter &&
4268 (Style.PackArguments.BinPack == FormatStyle::BPAS_UseBreakAfter &&
4270 Prev->
isOneOf(tok::l_paren, tok::l_brace,
4271 TT_ArrayInitializerLSquare) &&
4274 for (
auto *ParamTok = Current; ParamTok && ParamTok != RParen;
4275 ParamTok = ParamTok->Next) {
4276 if (ParamTok->opensScope()) {
4277 ParamTok = ParamTok->MatchingParen;
4282 ParamTok->MustBreakBefore =
true;
4283 ParamTok->CanBreakBefore =
true;
4288 if (Current->is(TT_ControlStatementLBrace)) {
4289 if (Style.ColumnLimit > 0 &&
4290 Style.BraceWrapping.AfterControlStatement ==
4291 FormatStyle::BWACS_MultiLine &&
4292 Line.Level * Style.IndentWidth +
Line.Last->TotalLength >
4293 Style.ColumnLimit) {
4294 Current->CanBreakBefore =
true;
4295 Current->MustBreakBefore =
true;
4297 }
else if (Current->is(TT_CtorInitializerColon)) {
4298 InFunctionDecl =
false;
4310 Current->SplitPenalty = splitPenalty(
Line, *Current, InFunctionDecl);
4311 if (Style.Language == FormatStyle::LK_ObjC &&
4312 Current->is(TT_SelectorName) && Current->ParameterIndex > 0) {
4313 if (Current->ParameterIndex == 1)
4314 Current->SplitPenalty += 5 * Current->BindingStrength;
4316 Current->SplitPenalty += 20 * Current->BindingStrength;
4320 calculateUnbreakableTailLengths(
Line);
4322 for (
auto *Current =
First; Current; Current = Current->Next) {
4324 Current->Role->precomputeFormattingInfos(Current);
4325 if (Current->MatchingParen &&
4326 Current->MatchingParen->opensBlockOrBlockTypeList(Style) &&
4331 if (Current->opensBlockOrBlockTypeList(Style))
4335 LLVM_DEBUG({ printDebugInfo(
Line); });
4338void TokenAnnotator::calculateUnbreakableTailLengths(
4345 Current->
isOneOf(tok::comment, tok::string_literal)) {
4355void TokenAnnotator::calculateArrayInitializerColumnList(
4359 auto *CurrentToken =
Line.First;
4360 CurrentToken->ArrayInitializerLineStart =
true;
4362 while (CurrentToken && CurrentToken !=
Line.Last) {
4363 if (CurrentToken->is(tok::l_brace)) {
4364 CurrentToken->IsArrayInitializer =
true;
4365 if (CurrentToken->Next)
4366 CurrentToken->Next->MustBreakBefore =
true;
4368 calculateInitializerColumnList(
Line, CurrentToken->Next, Depth + 1);
4370 CurrentToken = CurrentToken->Next;
4375FormatToken *TokenAnnotator::calculateInitializerColumnList(
4377 while (CurrentToken && CurrentToken !=
Line.Last) {
4378 if (CurrentToken->is(tok::l_brace))
4380 else if (CurrentToken->is(tok::r_brace))
4382 if (Depth == 2 && CurrentToken->isOneOf(tok::l_brace, tok::comma)) {
4383 CurrentToken = CurrentToken->Next;
4386 CurrentToken->StartsColumn =
true;
4387 CurrentToken = CurrentToken->Previous;
4389 CurrentToken = CurrentToken->Next;
4391 return CurrentToken;
4396 bool InFunctionDecl)
const {
4400 if (
Left.is(tok::semi))
4404 if (Style.isJava()) {
4405 if (
Right.isOneOf(Keywords.kw_extends, Keywords.kw_throws))
4407 if (
Right.is(Keywords.kw_implements))
4409 if (
Left.is(tok::comma) &&
Left.NestingLevel == 0)
4411 }
else if (Style.isJavaScript()) {
4412 if (
Right.is(Keywords.kw_function) &&
Left.isNot(tok::comma))
4414 if (
Left.is(TT_JsTypeColon))
4416 if ((
Left.is(TT_TemplateString) &&
Left.TokenText.ends_with(
"${")) ||
4417 (
Right.is(TT_TemplateString) &&
Right.TokenText.starts_with(
"}"))) {
4421 if (
Left.opensScope() &&
Right.closesScope())
4423 }
else if (Style.Language == FormatStyle::LK_Proto) {
4424 if (
Right.is(tok::l_square))
4426 if (
Right.is(tok::period))
4430 if (
Right.is(tok::identifier) &&
Right.Next &&
Right.Next->is(TT_DictLiteral))
4432 if (
Right.is(tok::l_square)) {
4433 if (
Left.is(tok::r_square))
4436 if (
Right.is(TT_LambdaLSquare) &&
Left.is(tok::equal))
4438 if (
Right.isNoneOf(TT_ObjCMethodExpr, TT_LambdaLSquare,
4439 TT_ArrayInitializerLSquare,
4440 TT_DesignatedInitializerLSquare, TT_AttributeLSquare)) {
4445 if (
Left.is(tok::coloncolon))
4446 return Style.PenaltyBreakScopeResolution;
4447 if (
Right.isOneOf(TT_StartOfName, TT_FunctionDeclarationName,
4448 tok::kw_operator)) {
4449 if (
Line.startsWith(tok::kw_for) &&
Right.PartOfMultiVariableDeclStmt)
4451 if (
Left.is(TT_StartOfName))
4453 if (InFunctionDecl &&
Right.NestingLevel == 0)
4454 return Style.PenaltyReturnTypeOnItsOwnLine;
4457 if (
Right.is(TT_PointerOrReference))
4459 if (
Right.is(TT_LambdaArrow))
4461 if (
Left.is(tok::equal) &&
Right.is(tok::l_brace))
4463 if (
Left.is(TT_CastRParen))
4465 if (
Left.isOneOf(tok::kw_class, tok::kw_struct, tok::kw_union))
4467 if (
Left.is(tok::comment))
4470 if (
Left.isOneOf(TT_RangeBasedForLoopColon, TT_InheritanceColon,
4471 TT_CtorInitializerColon)) {
4475 if (
Right.isMemberAccess()) {
4496 const auto Penalty = Style.PenaltyBreakBeforeMemberAccess;
4498 ? std::min(Penalty, 35u)
4502 if (
Right.is(TT_TrailingAnnotation) &&
4503 (!
Right.Next ||
Right.Next->isNot(tok::l_paren))) {
4506 if (
Line.startsWith(TT_ObjCMethodSpecifier))
4513 bool is_short_annotation =
Right.TokenText.size() < 10;
4514 return (
Left.is(tok::r_paren) ? 100 : 120) + (is_short_annotation ? 50 : 0);
4518 if (
Line.startsWith(tok::kw_for) &&
Left.is(tok::equal))
4523 if (
Right.is(TT_SelectorName))
4525 if (
Left.is(tok::colon)) {
4526 if (
Left.is(TT_ObjCMethodExpr))
4527 return Line.MightBeFunctionDecl ? 50 : 500;
4528 if (
Left.is(TT_ObjCSelector))
4536 Left.Previous->isOneOf(tok::identifier, tok::greater)) {
4540 if (
Left.is(tok::l_paren) && Style.PenaltyBreakOpenParenthesis != 0)
4541 return Style.PenaltyBreakOpenParenthesis;
4542 if (
Left.is(tok::l_paren) && InFunctionDecl && Style.AlignAfterOpenBracket)
4544 if (
Left.is(tok::l_paren) &&
Left.Previous &&
4545 (
Left.Previous->isOneOf(tok::kw_for, tok::kw__Generic) ||
4546 Left.Previous->isIf())) {
4549 if (
Left.is(tok::equal) && InFunctionDecl)
4551 if (
Right.is(tok::r_brace))
4553 if (
Left.is(TT_TemplateOpener))
4555 if (
Left.opensScope()) {
4559 if (!Style.AlignAfterOpenBracket &&
4560 (
Left.ParameterCount <= 1 || Style.AllowAllArgumentsOnNextLine)) {
4563 if (
Left.is(tok::l_brace) &&
4564 Style.Cpp11BracedListStyle == FormatStyle::BLS_Block) {
4567 return Left.ParameterCount > 1 ? Style.PenaltyBreakBeforeFirstCallParameter
4570 if (
Left.is(TT_JavaAnnotation))
4573 if (
Left.is(TT_UnaryOperator))
4575 if (
Left.isOneOf(tok::plus, tok::comma) &&
Left.Previous &&
4576 Left.Previous->isLabelString() &&
4577 (
Left.NextOperator ||
Left.OperatorIndex != 0)) {
4580 if (
Right.is(tok::plus) &&
Left.isLabelString() &&
4581 (
Right.NextOperator ||
Right.OperatorIndex != 0)) {
4584 if (
Left.is(tok::comma))
4586 if (
Right.is(tok::lessless) &&
Left.isLabelString() &&
4587 (
Right.NextOperator ||
Right.OperatorIndex != 1)) {
4590 if (
Right.is(tok::lessless)) {
4592 if (
Left.isNot(tok::r_paren) ||
Right.OperatorIndex > 0) {
4598 if (
Left.ClosesTemplateDeclaration)
4599 return Style.PenaltyBreakTemplateDeclaration;
4600 if (
Left.ClosesRequiresClause)
4602 if (
Left.is(TT_ConditionalExpr))
4608 return Style.PenaltyBreakAssignment;
4615bool TokenAnnotator::spaceRequiredBeforeParens(
const FormatToken &Right)
const {
4616 if (Style.SpaceBeforeParens == FormatStyle::SBPO_Always)
4618 if (
Right.is(TT_OverloadedOperatorLParen) &&
4619 Style.SpaceBeforeParensOptions.AfterOverloadedOperator) {
4622 if (Style.SpaceBeforeParensOptions.BeforeNonEmptyParentheses &&
4623 Right.ParameterCount > 0) {
4632 if (
Left.is(tok::kw_return) &&
4633 Right.isNoneOf(tok::semi, tok::r_paren, tok::hashhash)) {
4636 if (
Left.is(tok::kw_throw) &&
Right.is(tok::l_paren) &&
Right.MatchingParen &&
4637 Right.MatchingParen->is(TT_CastRParen)) {
4640 if (
Left.is(Keywords.kw_assert) && Style.isJava())
4643 Left.is(tok::objc_property)) {
4646 if (
Right.is(tok::hashhash))
4647 return Left.is(tok::hash);
4648 if (
Left.isOneOf(tok::hashhash, tok::hash))
4649 return Right.is(tok::hash);
4650 if (Style.SpacesInParens == FormatStyle::SIPO_Custom) {
4651 if (
Left.is(tok::l_paren) &&
Right.is(tok::r_paren))
4652 return Style.SpacesInParensOptions.InEmptyParentheses;
4653 if (Style.SpacesInParensOptions.ExceptDoubleParentheses &&
4654 Left.is(tok::r_paren) &&
Right.is(tok::r_paren)) {
4655 auto *InnerLParen =
Left.MatchingParen;
4656 if (InnerLParen && InnerLParen->Previous ==
Right.MatchingParen) {
4657 InnerLParen->SpacesRequiredBefore = 0;
4662 if (
Left.is(tok::l_paren))
4664 else if (
Right.is(tok::r_paren) &&
Right.MatchingParen)
4665 LeftParen =
Right.MatchingParen;
4666 if (LeftParen && (LeftParen->is(TT_ConditionLParen) ||
4667 (LeftParen->Previous &&
4668 isKeywordWithCondition(*LeftParen->Previous)))) {
4669 return Style.SpacesInParensOptions.InConditionalStatements;
4674 if (
Left.is(tok::kw_auto) &&
Right.isOneOf(TT_LambdaLBrace, TT_FunctionLBrace,
4676 TT_FunctionTypeLParen)) {
4681 if (
Left.is(tok::kw_auto) &&
Right.isOneOf(tok::l_paren, tok::l_brace))
4684 const auto *BeforeLeft =
Left.Previous;
4687 if (
Right.is(tok::l_paren) &&
Left.is(tok::kw_co_await) && BeforeLeft &&
4688 BeforeLeft->is(tok::kw_operator)) {
4692 if (
Left.isOneOf(tok::kw_co_await, tok::kw_co_yield, tok::kw_co_return) &&
4693 Right.isNoneOf(tok::semi, tok::r_paren)) {
4697 if (
Left.is(tok::l_paren) ||
Right.is(tok::r_paren)) {
4698 return (
Right.is(TT_CastRParen) ||
4699 (
Left.MatchingParen &&
Left.MatchingParen->is(TT_CastRParen)))
4700 ? Style.SpacesInParensOptions.InCStyleCasts
4701 : Style.SpacesInParensOptions.Other;
4703 if (
Right.isOneOf(tok::semi, tok::comma))
4706 bool IsLightweightGeneric =
Right.MatchingParen &&
4707 Right.MatchingParen->Next &&
4708 Right.MatchingParen->Next->is(tok::colon);
4709 return !IsLightweightGeneric && Style.ObjCSpaceBeforeProtocolList;
4711 if (
Right.is(tok::less) &&
Left.is(tok::kw_template))
4712 return Style.SpaceAfterTemplateKeyword;
4713 if (
Left.isOneOf(tok::exclaim, tok::tilde))
4715 if (
Left.is(tok::at) &&
4716 Right.isOneOf(tok::identifier, tok::string_literal, tok::char_constant,
4717 tok::numeric_constant, tok::l_paren, tok::l_brace,
4718 tok::kw_true, tok::kw_false)) {
4721 if (
Left.is(tok::colon))
4722 return Left.isNoneOf(TT_ObjCSelector, TT_ObjCMethodExpr);
4723 if (
Left.is(tok::coloncolon))
4725 if (
Left.is(tok::less) ||
Right.isOneOf(tok::greater, tok::less)) {
4726 if (Style.isTextProto() ||
4727 (Style.Language == FormatStyle::LK_Proto &&
4728 (
Left.is(TT_DictLiteral) ||
Right.is(TT_DictLiteral)))) {
4730 if (
Left.is(tok::less) &&
Right.is(tok::greater))
4732 return Style.Cpp11BracedListStyle == FormatStyle::BLS_Block;
4735 if (
Right.isNot(TT_OverloadedOperatorLParen))
4738 if (
Right.is(tok::ellipsis)) {
4739 return Left.Tok.isLiteral() || (
Left.is(tok::identifier) && BeforeLeft &&
4740 BeforeLeft->is(tok::kw_case));
4742 if (
Left.is(tok::l_square) &&
Right.is(tok::amp))
4743 return Style.SpacesInSquareBrackets;
4744 if (
Right.is(TT_PointerOrReference)) {
4745 if (
Left.is(tok::r_paren) &&
Line.MightBeFunctionDecl) {
4746 if (!
Left.MatchingParen)
4749 Left.MatchingParen->getPreviousNonComment();
4750 if (!TokenBeforeMatchingParen ||
Left.isNot(TT_TypeDeclarationParen))
4756 if ((Style.SpaceAroundPointerQualifiers == FormatStyle::SAPQ_After ||
4757 Style.SpaceAroundPointerQualifiers == FormatStyle::SAPQ_Both) &&
4758 (
Left.is(TT_AttributeRParen) ||
4759 Left.canBePointerOrReferenceQualifier())) {
4762 if (
Left.Tok.isLiteral())
4765 if (
Left.isTypeOrIdentifier(LangOpts) &&
Right.Next &&
Right.Next->Next &&
4766 Right.Next->Next->is(TT_RangeBasedForLoopColon)) {
4767 return getTokenPointerOrReferenceAlignment(Right) !=
4768 FormatStyle::PAS_Left;
4770 return Left.isNoneOf(TT_PointerOrReference, tok::l_paren) &&
4771 (getTokenPointerOrReferenceAlignment(Right) !=
4772 FormatStyle::PAS_Left ||
4773 (
Line.IsMultiVariableDeclStmt &&
4774 (
Left.NestingLevel == 0 ||
4775 (
Left.NestingLevel == 1 && startsWithInitStatement(
Line)))));
4777 if (
Right.is(TT_FunctionTypeLParen) &&
Left.isNot(tok::l_paren) &&
4778 (
Left.isNot(TT_PointerOrReference) ||
4779 (getTokenPointerOrReferenceAlignment(Left) != FormatStyle::PAS_Right &&
4780 !
Line.IsMultiVariableDeclStmt))) {
4783 if (
Left.is(TT_PointerOrReference)) {
4786 if ((Style.SpaceAroundPointerQualifiers == FormatStyle::SAPQ_Before ||
4787 Style.SpaceAroundPointerQualifiers == FormatStyle::SAPQ_Both) &&
4788 Right.canBePointerOrReferenceQualifier()) {
4792 if (
Right.Tok.isLiteral())
4795 if (
Right.is(TT_BlockComment))
4799 if (
Right.isOneOf(Keywords.kw_override, Keywords.kw_final, tok::kw_noexcept,
4800 TT_RequiresClause) &&
4801 Right.isNot(TT_StartOfName)) {
4808 if (BeforeLeft && BeforeLeft->isTypeOrIdentifier(LangOpts) &&
Right.Next &&
4809 Right.Next->is(TT_RangeBasedForLoopColon)) {
4810 return getTokenPointerOrReferenceAlignment(Left) !=
4811 FormatStyle::PAS_Right;
4813 if (
Right.isOneOf(TT_PointerOrReference, TT_ArraySubscriptLSquare,
4817 if (getTokenPointerOrReferenceAlignment(Left) == FormatStyle::PAS_Right)
4825 if (
Line.IsMultiVariableDeclStmt &&
4826 (
Left.NestingLevel ==
Line.First->NestingLevel ||
4827 ((
Left.NestingLevel ==
Line.First->NestingLevel + 1) &&
4828 startsWithInitStatement(
Line)))) {
4833 if (BeforeLeft->is(tok::coloncolon)) {
4834 if (
Left.isNot(tok::star))
4836 assert(Style.PointerAlignment != FormatStyle::PAS_Right);
4837 if (!
Right.startsSequence(tok::identifier, tok::r_paren))
4840 const auto *LParen =
Right.Next->MatchingParen;
4841 return !LParen || LParen->isNot(TT_FunctionTypeLParen);
4843 return BeforeLeft->isNoneOf(tok::l_paren, tok::l_square);
4846 if (
Left.is(tok::ellipsis) && BeforeLeft &&
4847 BeforeLeft->isPointerOrReference()) {
4848 return Style.PointerAlignment != FormatStyle::PAS_Right;
4851 if (
Right.is(tok::star) &&
Left.is(tok::l_paren))
4853 if (
Left.is(tok::star) &&
Right.isPointerOrReference())
4855 if (
Right.isPointerOrReference()) {
4866 if (
Previous->is(tok::coloncolon)) {
4885 if (
Previous->endsSequence(tok::kw_operator))
4886 return Style.PointerAlignment != FormatStyle::PAS_Left;
4887 if (
Previous->isOneOf(tok::kw_const, tok::kw_volatile)) {
4888 return (Style.PointerAlignment != FormatStyle::PAS_Left) ||
4889 (Style.SpaceAroundPointerQualifiers ==
4890 FormatStyle::SAPQ_After) ||
4891 (Style.SpaceAroundPointerQualifiers == FormatStyle::SAPQ_Both);
4895 if (Style.isCSharp() &&
Left.is(Keywords.kw_is) &&
Right.is(tok::l_square))
4897 const auto SpaceRequiredForArrayInitializerLSquare =
4898 [](
const FormatToken &LSquareTok,
const FormatStyle &Style) {
4899 return Style.SpacesInContainerLiterals ||
4901 Style.Cpp11BracedListStyle == FormatStyle::BLS_Block &&
4902 LSquareTok.endsSequence(tok::l_square, tok::colon,
4905 if (
Left.is(tok::l_square)) {
4906 return (
Left.is(TT_ArrayInitializerLSquare) &&
Right.isNot(tok::r_square) &&
4907 SpaceRequiredForArrayInitializerLSquare(Left, Style)) ||
4908 (
Left.isOneOf(TT_ArraySubscriptLSquare, TT_StructuredBindingLSquare,
4909 TT_LambdaLSquare) &&
4910 Style.SpacesInSquareBrackets &&
Right.isNot(tok::r_square));
4912 if (
Right.is(tok::r_square)) {
4913 return Right.MatchingParen &&
4914 ((
Right.MatchingParen->is(TT_ArrayInitializerLSquare) &&
4915 SpaceRequiredForArrayInitializerLSquare(*
Right.MatchingParen,
4917 (Style.SpacesInSquareBrackets &&
4918 Right.MatchingParen->isOneOf(TT_ArraySubscriptLSquare,
4919 TT_StructuredBindingLSquare,
4920 TT_LambdaLSquare)));
4922 if (
Right.is(tok::l_square) &&
4923 Right.isNoneOf(TT_ObjCMethodExpr, TT_LambdaLSquare,
4924 TT_DesignatedInitializerLSquare,
4925 TT_StructuredBindingLSquare, TT_AttributeLSquare) &&
4926 Left.isNoneOf(tok::numeric_constant, TT_DictLiteral) &&
4927 !(
Left.isNot(tok::r_square) && Style.SpaceBeforeSquareBrackets &&
4928 Right.is(TT_ArraySubscriptLSquare))) {
4932 (
Right.is(tok::r_brace) &&
Right.MatchingParen &&
4934 return Style.Cpp11BracedListStyle == FormatStyle::BLS_Block ||
4935 Style.SpacesInParensOptions.Other;
4937 if (
Left.is(TT_BlockComment)) {
4939 return Style.isJavaScript() || !
Left.TokenText.ends_with(
"=*/");
4944 if (
Left.is(TT_TemplateCloser) &&
Right.is(TT_AttributeLSquare))
4947 if (
Right.is(tok::l_paren)) {
4949 if (
Line.MightBeFunctionDecl &&
Right.is(TT_FunctionDeclarationLParen)) {
4950 if (spaceRequiredBeforeParens(Right))
4952 const auto &Options = Style.SpaceBeforeParensOptions;
4953 return Line.mightBeFunctionDefinition()
4954 ? Options.AfterFunctionDefinitionName
4955 : Options.AfterFunctionDeclarationName;
4957 if (
Left.is(TT_TemplateCloser) &&
Right.isNot(TT_FunctionTypeLParen))
4958 return spaceRequiredBeforeParens(Right);
4959 if (
Left.isOneOf(TT_RequiresClause,
4960 TT_RequiresClauseInARequiresExpression)) {
4961 return Style.SpaceBeforeParensOptions.AfterRequiresInClause ||
4962 spaceRequiredBeforeParens(Right);
4964 if (
Left.is(TT_RequiresExpression)) {
4965 return Style.SpaceBeforeParensOptions.AfterRequiresInExpression ||
4966 spaceRequiredBeforeParens(Right);
4968 if (
Left.isOneOf(TT_AttributeRParen, TT_AttributeRSquare))
4970 if (
Left.is(TT_ForEachMacro)) {
4971 return Style.SpaceBeforeParensOptions.AfterForeachMacros ||
4972 spaceRequiredBeforeParens(Right);
4974 if (
Left.is(TT_IfMacro)) {
4975 return Style.SpaceBeforeParensOptions.AfterIfMacros ||
4976 spaceRequiredBeforeParens(Right);
4978 if (Style.SpaceBeforeParens == FormatStyle::SBPO_Custom &&
4979 Left.isPlacementOperator() &&
4980 Right.isNot(TT_OverloadedOperatorLParen) &&
4981 !(
Line.MightBeFunctionDecl &&
Left.is(TT_FunctionDeclarationName))) {
4982 const auto *RParen =
Right.MatchingParen;
4983 return Style.SpaceBeforeParensOptions.AfterPlacementOperator ||
4984 (RParen && RParen->is(TT_CastRParen));
4988 if (
Left.is(tok::semi))
4990 if (
Left.isOneOf(tok::pp_elif, tok::kw_for, tok::kw_while, tok::kw_switch,
4991 tok::kw_case, TT_ForEachMacro, TT_ObjCForIn) ||
4993 Right.is(TT_ConditionLParen)) {
4994 return Style.SpaceBeforeParensOptions.AfterControlStatements ||
4995 spaceRequiredBeforeParens(Right);
5000 if (
Right.is(TT_OverloadedOperatorLParen))
5001 return spaceRequiredBeforeParens(Right);
5005 Left.MatchingParen &&
Left.MatchingParen->is(TT_LambdaLSquare)) {
5006 return Style.SpaceBeforeParensOptions.AfterFunctionDefinitionName ||
5007 spaceRequiredBeforeParens(Right);
5009 if (!BeforeLeft || BeforeLeft->isNoneOf(tok::period, tok::arrow)) {
5010 if (
Left.isOneOf(tok::kw_try, Keywords.kw___except, tok::kw_catch)) {
5011 return Style.SpaceBeforeParensOptions.AfterControlStatements ||
5012 spaceRequiredBeforeParens(Right);
5014 if (
Left.isPlacementOperator() ||
5015 (
Left.is(tok::r_square) &&
Left.MatchingParen &&
5016 Left.MatchingParen->Previous &&
5017 Left.MatchingParen->Previous->is(tok::kw_delete))) {
5018 return Style.SpaceBeforeParens != FormatStyle::SBPO_Never ||
5019 spaceRequiredBeforeParens(Right);
5023 if (
Tok.isNot(tok::l_paren))
5025 const auto *RParen =
Tok.MatchingParen;
5028 const auto *
Next = RParen->Next;
5035 (
Left.Tok.getIdentifierInfo() ||
Left.is(tok::r_paren))) {
5036 return spaceRequiredBeforeParens(Right);
5040 if (
Left.is(tok::at) &&
Right.isNot(tok::objc_not_keyword))
5042 if (
Right.is(TT_UnaryOperator)) {
5043 return Left.isNoneOf(tok::l_paren, tok::l_square, tok::at) &&
5044 (
Left.isNot(tok::colon) ||
Left.isNot(TT_ObjCMethodExpr));
5050 if (!Style.isVerilog() &&
5051 (
Left.isOneOf(tok::identifier, tok::greater, tok::r_square,
5053 Left.isTypeName(LangOpts)) &&
5054 Right.is(tok::l_brace) &&
Right.getNextNonComment() &&
5058 if (
Left.is(tok::period) ||
Right.is(tok::period))
5062 if (
Right.is(tok::hash) &&
Left.is(tok::identifier) &&
5063 (
Left.TokenText ==
"L" ||
Left.TokenText ==
"u" ||
5064 Left.TokenText ==
"U" ||
Left.TokenText ==
"u8" ||
5065 Left.TokenText ==
"LR" ||
Left.TokenText ==
"uR" ||
5066 Left.TokenText ==
"UR" ||
Left.TokenText ==
"u8R")) {
5069 if (
Left.is(TT_TemplateCloser) &&
Left.MatchingParen &&
5070 Left.MatchingParen->Previous &&
5071 Left.MatchingParen->Previous->isOneOf(tok::period, tok::coloncolon)) {
5077 if (
Left.is(TT_TemplateCloser) &&
Right.is(tok::l_square))
5079 if (
Left.is(tok::l_brace) &&
Left.endsSequence(TT_DictLiteral, tok::at)) {
5083 if (
Right.is(tok::r_brace) &&
Right.MatchingParen &&
5084 Right.MatchingParen->endsSequence(TT_DictLiteral, tok::at)) {
5088 if (
Right.is(TT_TrailingAnnotation) &&
Right.isOneOf(tok::amp, tok::ampamp) &&
5089 Left.isOneOf(tok::kw_const, tok::kw_volatile) &&
5090 (!
Right.Next ||
Right.Next->is(tok::semi))) {
5094 return getTokenReferenceAlignment(Right) != FormatStyle::PAS_Left;
5107 return Right.hasWhitespaceBefore();
5109 const bool IsVerilog = Style.isVerilog();
5110 assert(!IsVerilog || !IsCpp);
5113 if (Keywords.isWordLike(Right, IsVerilog) &&
5114 Keywords.isWordLike(Left, IsVerilog)) {
5120 if (
Left.is(tok::star) &&
Right.is(tok::comment))
5123 if (
Left.is(tok::l_brace) &&
Right.is(tok::r_brace) &&
5124 Left.Children.empty()) {
5126 return Style.SpaceInEmptyBraces != FormatStyle::SIEB_Never;
5127 if (Style.Cpp11BracedListStyle != FormatStyle::BLS_Block) {
5128 return Style.SpacesInParens == FormatStyle::SIPO_Custom &&
5129 Style.SpacesInParensOptions.InEmptyParentheses;
5131 return Style.SpaceInEmptyBraces == FormatStyle::SIEB_Always;
5134 const auto *BeforeLeft =
Left.Previous;
5137 if (
Left.is(TT_OverloadedOperator) &&
5138 Right.isOneOf(TT_TemplateOpener, TT_TemplateCloser)) {
5142 if (
Right.is(tok::period) &&
Left.is(tok::numeric_constant))
5146 if (
Left.is(Keywords.kw_import) &&
5147 Right.isOneOf(tok::less, tok::ellipsis) &&
5148 (!BeforeLeft || BeforeLeft->is(tok::kw_export))) {
5152 if (
Left.isOneOf(Keywords.kw_module, Keywords.kw_import) &&
5153 Right.is(TT_ModulePartitionColon)) {
5157 if (
Right.is(TT_AfterPPDirective))
5161 if (
Left.is(tok::identifier) &&
Right.is(TT_ModulePartitionColon))
5164 if (
Left.is(TT_ModulePartitionColon) &&
5165 Right.isOneOf(tok::identifier, tok::kw_private)) {
5168 if (
Left.is(tok::ellipsis) &&
Right.is(tok::identifier) &&
5169 Line.First->is(Keywords.kw_import)) {
5173 if (
Left.isOneOf(TT_AttributeRParen, TT_AttributeMacro) &&
5174 Right.is(tok::coloncolon)) {
5178 if (
Left.is(tok::kw_operator))
5179 return Right.is(tok::coloncolon) || Style.SpaceAfterOperatorKeyword;
5181 !
Left.opensScope() && Style.SpaceBeforeCpp11BracedList) {
5184 if (
Left.is(tok::less) &&
Left.is(TT_OverloadedOperator) &&
5185 Right.is(TT_TemplateOpener)) {
5189 if (
Left.is(tok::identifier) &&
Right.is(tok::numeric_constant))
5190 return Right.TokenText[0] !=
'.';
5192 if (
Left.Tok.getIdentifierInfo() &&
Right.Tok.isLiteral())
5194 }
else if (Style.isProto()) {
5195 if (
Right.is(tok::period) && !(BeforeLeft && BeforeLeft->is(tok::period)) &&
5196 Left.isOneOf(Keywords.kw_optional, Keywords.kw_required,
5197 Keywords.kw_repeated, Keywords.kw_extend)) {
5200 if (
Right.is(tok::l_paren) &&
5201 Left.isOneOf(Keywords.kw_returns, Keywords.kw_option)) {
5204 if (
Right.isOneOf(tok::l_brace, tok::less) &&
Left.is(TT_SelectorName))
5207 if (
Left.is(tok::slash) ||
Right.is(tok::slash))
5209 if (
Left.MatchingParen &&
5210 Left.MatchingParen->is(TT_ProtoExtensionLSquare) &&
5211 Right.isOneOf(tok::l_brace, tok::less)) {
5212 return Style.Cpp11BracedListStyle == FormatStyle::BLS_Block;
5215 if (
Left.is(tok::percent))
5219 if (
Left.is(tok::numeric_constant) &&
Right.is(tok::percent))
5220 return Right.hasWhitespaceBefore();
5221 }
else if (Style.isJson()) {
5222 if (
Right.is(tok::colon) &&
Left.is(tok::string_literal))
5223 return Style.SpaceBeforeJsonColon;
5224 }
else if (Style.isCSharp()) {
5230 if (
Left.is(tok::kw_this) &&
Right.is(tok::l_square))
5234 if (
Left.is(tok::kw_new) &&
Right.is(tok::l_paren))
5238 if (
Right.is(tok::l_brace))
5242 if (
Left.is(tok::l_brace) &&
Right.isNot(tok::r_brace))
5245 if (
Left.isNot(tok::l_brace) &&
Right.is(tok::r_brace))
5249 if (
Left.is(TT_FatArrow) ||
Right.is(TT_FatArrow))
5253 if (
Left.is(TT_AttributeColon) ||
Right.is(TT_AttributeColon))
5257 if (
Left.is(TT_TemplateCloser) &&
Right.is(TT_StartOfName))
5261 if (
Left.is(tok::l_square) ||
Right.is(tok::r_square))
5262 return Style.SpacesInSquareBrackets;
5265 if (
Right.is(TT_CSharpNullable))
5269 if (
Right.is(TT_NonNullAssertion))
5273 if (
Left.is(tok::comma) &&
Right.is(tok::comma))
5277 if (
Left.is(Keywords.kw_var) &&
Right.is(tok::l_paren))
5281 if (
Right.is(tok::l_paren)) {
5282 if (
Left.isOneOf(tok::kw_using, Keywords.kw_async, Keywords.kw_when,
5283 Keywords.kw_lock)) {
5284 return Style.SpaceBeforeParensOptions.AfterControlStatements ||
5285 spaceRequiredBeforeParens(Right);
5291 if ((
Left.isAccessSpecifierKeyword() ||
5292 Left.isOneOf(tok::kw_virtual, tok::kw_extern, tok::kw_static,
5293 Keywords.kw_internal, Keywords.kw_abstract,
5294 Keywords.kw_sealed, Keywords.kw_override,
5295 Keywords.kw_async, Keywords.kw_unsafe)) &&
5296 Right.is(tok::l_paren)) {
5299 }
else if (Style.isJavaScript()) {
5300 if (
Left.is(TT_FatArrow))
5303 if (
Right.is(tok::l_paren) &&
Left.is(Keywords.kw_await) && BeforeLeft &&
5304 BeforeLeft->is(tok::kw_for)) {
5307 if (
Left.is(Keywords.kw_async) &&
Right.is(tok::l_paren) &&
5308 Right.MatchingParen) {
5315 if ((
Left.is(TT_TemplateString) &&
Left.TokenText.ends_with(
"${")) ||
5316 (
Right.is(TT_TemplateString) &&
Right.TokenText.starts_with(
"}"))) {
5321 if (Keywords.isJavaScriptIdentifier(Left,
5323 Right.is(TT_TemplateString)) {
5326 if (
Right.is(tok::star) &&
5327 Left.isOneOf(Keywords.kw_function, Keywords.kw_yield)) {
5330 if (
Right.isOneOf(tok::l_brace, tok::l_square) &&
5331 Left.isOneOf(Keywords.kw_function, Keywords.kw_yield,
5332 Keywords.kw_extends, Keywords.kw_implements)) {
5335 if (
Right.is(tok::l_paren)) {
5337 if (
Line.MustBeDeclaration &&
Left.Tok.getIdentifierInfo())
5341 if (BeforeLeft && BeforeLeft->is(tok::period) &&
5342 Left.Tok.getIdentifierInfo()) {
5346 if (
Left.isOneOf(tok::kw_throw, Keywords.kw_await, Keywords.kw_typeof,
5352 if (
Left.endsSequence(tok::kw_const, Keywords.kw_as))
5354 if ((
Left.isOneOf(Keywords.kw_let, Keywords.kw_var, Keywords.kw_in,
5359 (
Left.is(Keywords.kw_of) && BeforeLeft &&
5360 BeforeLeft->isOneOf(tok::identifier, tok::r_square, tok::r_brace))) &&
5361 (!BeforeLeft || BeforeLeft->isNot(tok::period))) {
5364 if (
Left.isOneOf(tok::kw_for, Keywords.kw_as) && BeforeLeft &&
5365 BeforeLeft->is(tok::period) &&
Right.is(tok::l_paren)) {
5368 if (
Left.is(Keywords.kw_as) &&
5369 Right.isOneOf(tok::l_square, tok::l_brace, tok::l_paren)) {
5372 if (
Left.is(tok::kw_default) && BeforeLeft &&
5373 BeforeLeft->is(tok::kw_export)) {
5376 if (
Left.is(Keywords.kw_is) &&
Right.is(tok::l_brace))
5378 if (
Right.isOneOf(TT_JsTypeColon, TT_JsTypeOptionalQuestion))
5380 if (
Left.is(TT_JsTypeOperator) ||
Right.is(TT_JsTypeOperator))
5382 if ((
Left.is(tok::l_brace) ||
Right.is(tok::r_brace)) &&
5383 Line.First->isOneOf(Keywords.kw_import, tok::kw_export)) {
5386 if (
Left.is(tok::ellipsis))
5388 if (
Left.is(TT_TemplateCloser) &&
5389 Right.isNoneOf(tok::equal, tok::l_brace, tok::comma, tok::l_square,
5390 Keywords.kw_implements, Keywords.kw_extends)) {
5396 if (
Right.is(TT_NonNullAssertion))
5398 if (
Left.is(TT_NonNullAssertion) &&
5399 Right.isOneOf(Keywords.kw_as, Keywords.kw_in)) {
5402 }
else if (Style.isJava()) {
5403 if (
Left.is(TT_CaseLabelArrow) ||
Right.is(TT_CaseLabelArrow))
5405 if (
Left.is(tok::r_square) &&
Right.is(tok::l_brace))
5408 if (
Left.is(tok::l_square) ||
Right.is(tok::r_square))
5409 return Style.SpacesInSquareBrackets;
5411 if (
Left.is(Keywords.kw_synchronized) &&
Right.is(tok::l_paren)) {
5412 return Style.SpaceBeforeParensOptions.AfterControlStatements ||
5413 spaceRequiredBeforeParens(Right);
5415 if ((
Left.isAccessSpecifierKeyword() ||
5416 Left.isOneOf(tok::kw_static, Keywords.kw_final, Keywords.kw_abstract,
5417 Keywords.kw_native)) &&
5418 Right.is(TT_TemplateOpener)) {
5421 }
else if (IsVerilog) {
5423 if (
Left.is(tok::identifier) &&
Left.TokenText[0] ==
'\\')
5427 if ((
Left.is(TT_VerilogTableItem) &&
5428 Right.isNoneOf(tok::r_paren, tok::semi)) ||
5429 (
Right.is(TT_VerilogTableItem) &&
Left.isNot(tok::l_paren))) {
5431 return !(
Next &&
Next->is(tok::r_paren));
5434 if (
Left.isNot(TT_BinaryOperator) &&
5435 Left.isOneOf(Keywords.kw_verilogHash, Keywords.kw_verilogHashHash)) {
5439 if (
Right.isNot(tok::semi) &&
5440 (
Left.endsSequence(tok::numeric_constant, Keywords.kw_verilogHash) ||
5441 Left.endsSequence(tok::numeric_constant,
5442 Keywords.kw_verilogHashHash) ||
5443 (
Left.is(tok::r_paren) &&
Left.MatchingParen &&
5444 Left.MatchingParen->endsSequence(tok::l_paren, tok::at)))) {
5449 if (
Left.is(Keywords.kw_apostrophe) ||
5450 (
Left.is(TT_VerilogNumberBase) &&
Right.is(tok::numeric_constant))) {
5454 if (
Left.is(tok::arrow) ||
Right.is(tok::arrow))
5459 if (
Left.is(tok::at) &&
Right.isOneOf(tok::l_paren, tok::star, tok::at))
5462 if (
Right.is(tok::l_square) &&
5463 Left.isOneOf(TT_VerilogDimensionedTypeName, Keywords.kw_function)) {
5467 if (
Right.isOneOf(tok::period, Keywords.kw_apostrophe) &&
5468 Keywords.isVerilogIdentifier(Left) &&
Left.getPreviousNonComment() &&
5469 Left.getPreviousNonComment()->is(Keywords.kw_tagged)) {
5475 if ((
Right.is(Keywords.kw_apostrophe) ||
5477 Left.isNoneOf(Keywords.kw_assign, Keywords.kw_unique) &&
5478 !Keywords.isVerilogWordOperator(Left) &&
5479 (
Left.isOneOf(tok::r_square, tok::r_paren, tok::r_brace,
5480 tok::numeric_constant) ||
5481 Keywords.isWordLike(Left))) {
5485 if ((
Right.is(tok::star) &&
Left.is(tok::coloncolon)) ||
5486 (
Left.is(tok::star) &&
Right.is(tok::semi))) {
5490 if (
Left.endsSequence(tok::star, tok::l_paren) &&
Right.is(tok::identifier))
5493 if (
Right.is(tok::l_paren) &&
Right.is(TT_VerilogStrength))
5496 if ((
Left.is(tok::l_brace) &&
5497 Right.isOneOf(tok::lessless, tok::greatergreater)) ||
5498 (
Left.endsSequence(tok::lessless, tok::l_brace) ||
5499 Left.endsSequence(tok::greatergreater, tok::l_brace))) {
5502 }
else if (Style.isTableGen()) {
5504 if (
Left.is(tok::l_square) &&
Right.is(tok::l_brace))
5506 if (
Left.is(tok::r_brace) &&
Right.is(tok::r_square))
5509 if (
Right.isOneOf(TT_TableGenDAGArgListColon,
5510 TT_TableGenDAGArgListColonToAlign) ||
5511 Left.isOneOf(TT_TableGenDAGArgListColon,
5512 TT_TableGenDAGArgListColonToAlign)) {
5515 if (
Right.is(TT_TableGenCondOperatorColon))
5517 if (
Left.isOneOf(TT_TableGenDAGArgOperatorID,
5518 TT_TableGenDAGArgOperatorToBreak) &&
5519 Right.isNot(TT_TableGenDAGArgCloser)) {
5523 if (
Right.isOneOf(tok::l_paren, tok::less) &&
5524 Left.isOneOf(TT_TableGenBangOperator, TT_TableGenCondOperator)) {
5529 if (
Left.is(TT_TableGenTrailingPasteOperator) &&
5530 Right.isOneOf(tok::l_brace, tok::colon)) {
5534 if (
Left.is(tok::hash) ||
Right.is(tok::hash))
5537 if (Keywords.isTableGenDefinition(Left))
5541 if (
Left.is(TT_ImplicitStringLiteral))
5542 return Right.hasWhitespaceBefore();
5544 if (
Left.is(TT_ObjCMethodSpecifier))
5545 return Style.ObjCSpaceAfterMethodDeclarationPrefix;
5546 if (
Left.is(tok::r_paren) &&
Left.isNot(TT_AttributeRParen) &&
5547 canBeObjCSelectorComponent(Right)) {
5555 (
Right.is(tok::equal) ||
Left.is(tok::equal))) {
5559 if (
Right.isOneOf(TT_TrailingReturnArrow, TT_LambdaArrow) ||
5560 Left.isOneOf(TT_TrailingReturnArrow, TT_LambdaArrow)) {
5563 if (
Left.is(tok::comma) &&
Right.isNot(TT_OverloadedOperatorLParen) &&
5566 (
Left.Children.empty() || !
Left.MacroParent)) {
5569 if (
Right.is(tok::comma))
5571 if (
Right.is(TT_ObjCBlockLParen))
5573 if (
Right.is(TT_CtorInitializerColon))
5574 return Style.SpaceBeforeCtorInitializerColon;
5575 if (
Right.is(TT_InheritanceColon) && !Style.SpaceBeforeInheritanceColon)
5577 if (
Right.is(TT_EnumUnderlyingTypeColon) &&
5578 !Style.SpaceBeforeEnumUnderlyingTypeColon) {
5581 if (
Right.is(TT_RangeBasedForLoopColon) &&
5582 !Style.SpaceBeforeRangeBasedForLoopColon) {
5585 if (
Left.is(TT_BitFieldColon)) {
5586 return Style.BitFieldColonSpacing == FormatStyle::BFCS_Both ||
5587 Style.BitFieldColonSpacing == FormatStyle::BFCS_After;
5589 if (
Right.is(tok::colon)) {
5590 if (
Right.is(TT_CaseLabelColon))
5591 return Style.SpaceBeforeCaseColon;
5592 if (
Right.is(TT_GotoLabelColon))
5595 if (!
Right.getNextNonComment())
5597 if (
Right.isOneOf(TT_ObjCSelector, TT_ObjCMethodExpr))
5599 if (
Left.is(tok::question))
5601 if (
Right.is(TT_InlineASMColon) &&
Left.is(tok::coloncolon))
5603 if (
Right.is(TT_DictLiteral))
5604 return Style.SpacesInContainerLiterals;
5605 if (
Right.is(TT_AttributeColon))
5607 if (
Right.is(TT_CSharpNamedArgumentColon))
5609 if (
Right.is(TT_GenericSelectionColon))
5611 if (
Right.is(TT_BitFieldColon)) {
5612 return Style.BitFieldColonSpacing == FormatStyle::BFCS_Both ||
5613 Style.BitFieldColonSpacing == FormatStyle::BFCS_Before;
5618 if ((
Left.isOneOf(tok::minus, tok::minusminus) &&
5619 Right.isOneOf(tok::minus, tok::minusminus)) ||
5620 (
Left.isOneOf(tok::plus, tok::plusplus) &&
5621 Right.isOneOf(tok::plus, tok::plusplus))) {
5624 if (
Left.is(TT_UnaryOperator)) {
5627 if (
Left.is(tok::amp) &&
Right.is(tok::r_square))
5628 return Style.SpacesInSquareBrackets;
5629 if (
Left.isNot(tok::exclaim))
5631 if (
Left.TokenText ==
"!")
5632 return Style.SpaceAfterLogicalNot;
5633 assert(
Left.TokenText ==
"not");
5634 return Right.isOneOf(tok::coloncolon, TT_UnaryOperator) ||
5635 (
Right.is(tok::l_paren) && Style.SpaceBeforeParensOptions.AfterNot);
5640 if (
Left.is(TT_CastRParen)) {
5641 return Style.SpaceAfterCStyleCast ||
5642 Right.isOneOf(TT_BinaryOperator, TT_SelectorName);
5645 auto ShouldAddSpacesInAngles = [
this, &
Right]() {
5646 if (this->Style.SpacesInAngles == FormatStyle::SIAS_Always)
5648 if (this->Style.SpacesInAngles == FormatStyle::SIAS_Leave)
5649 return Right.hasWhitespaceBefore();
5653 if (
Left.is(tok::greater) &&
Right.is(tok::greater)) {
5654 if (Style.isTextProto() ||
5655 (Style.Language == FormatStyle::LK_Proto &&
Left.is(TT_DictLiteral))) {
5656 return Style.Cpp11BracedListStyle == FormatStyle::BLS_Block;
5658 return Right.is(TT_TemplateCloser) &&
Left.is(TT_TemplateCloser) &&
5659 ((Style.Standard < FormatStyle::LS_Cpp11) ||
5660 ShouldAddSpacesInAngles());
5662 if (
Right.isOneOf(tok::arrow, tok::arrowstar, tok::periodstar) ||
5663 Left.isOneOf(tok::arrow, tok::period, tok::arrowstar, tok::periodstar) ||
5664 (
Right.is(tok::period) &&
Right.isNot(TT_DesignatedInitializerPeriod))) {
5667 if (!Style.SpaceBeforeAssignmentOperators &&
Left.isNot(TT_TemplateCloser) &&
5671 if (Style.isJava() &&
Right.is(tok::coloncolon) &&
5672 Left.isOneOf(tok::identifier, tok::kw_this)) {
5675 if (
Right.is(tok::coloncolon) &&
Left.is(tok::identifier)) {
5677 return Left.isPossibleMacro(
true) &&
5678 Right.hasWhitespaceBefore();
5680 if (
Right.is(tok::coloncolon) &&
5681 Left.isNoneOf(tok::l_brace, tok::comment, tok::l_paren)) {
5683 return (
Left.is(TT_TemplateOpener) &&
5684 ((Style.Standard < FormatStyle::LS_Cpp11) ||
5685 ShouldAddSpacesInAngles())) ||
5686 Left.isNoneOf(tok::l_paren, tok::r_paren, tok::l_square,
5687 tok::kw___super, TT_TemplateOpener,
5688 TT_TemplateCloser) ||
5689 (
Left.is(tok::l_paren) && Style.SpacesInParensOptions.Other);
5691 if ((
Left.is(TT_TemplateOpener)) != (
Right.is(TT_TemplateCloser)))
5692 return ShouldAddSpacesInAngles();
5693 if (
Left.is(tok::r_paren) &&
Left.isNot(TT_TypeDeclarationParen) &&
5694 Right.is(TT_PointerOrReference) &&
Right.isOneOf(tok::amp, tok::ampamp)) {
5698 if (
Right.is(TT_StructuredBindingLSquare)) {
5699 return Left.isNoneOf(tok::amp, tok::ampamp) ||
5700 getTokenReferenceAlignment(Left) != FormatStyle::PAS_Right;
5703 if (
Right.Next &&
Right.Next->is(TT_StructuredBindingLSquare) &&
5704 Right.isOneOf(tok::amp, tok::ampamp)) {
5705 return getTokenReferenceAlignment(Right) != FormatStyle::PAS_Left;
5707 if ((
Right.is(TT_BinaryOperator) &&
Left.isNot(tok::l_paren)) ||
5708 (
Left.isOneOf(TT_BinaryOperator, TT_ConditionalExpr) &&
5709 Right.isNot(tok::r_paren))) {
5712 if (
Right.is(TT_TemplateOpener) &&
Left.is(tok::r_paren) &&
5713 Left.MatchingParen &&
5714 Left.MatchingParen->is(TT_OverloadedOperatorLParen)) {
5717 if (
Right.is(tok::less) &&
Left.isNot(tok::l_paren) &&
5721 if (
Right.is(TT_TrailingUnaryOperator))
5723 if (
Left.is(TT_RegexLiteral))
5725 return spaceRequiredBetween(
Line, Left, Right);
5731 Tok.isNoneOf(TT_ObjCBlockLBrace, TT_LambdaLBrace, TT_DictLiteral);
5736 return Tok.MatchingParen &&
Tok.MatchingParen->Next &&
5737 Tok.MatchingParen->Next->isOneOf(tok::comma, tok::r_paren,
5743 FormatStyle::ShortLambdaStyle ShortLambdaOption) {
5744 return Tok.Children.empty() && ShortLambdaOption != FormatStyle::SLS_None;
5749 Tok.isNoneOf(TT_ObjCBlockLBrace, TT_DictLiteral);
5752bool TokenAnnotator::mustBreakBefore(AnnotatedLine &
Line,
5754 if (Right.NewlinesBefore > 1 && Style.MaxEmptyLinesToKeep > 0 &&
5755 (!Style.RemoveEmptyLinesInUnwrappedLines || &Right ==
Line.First)) {
5761 if (Style.BreakFunctionDeclarationParameters &&
Line.MightBeFunctionDecl &&
5762 !
Line.mightBeFunctionDefinition() && Left.MightBeFunctionDeclParen &&
5763 Left.ParameterCount > 0) {
5767 if (Style.BreakFunctionDefinitionParameters &&
Line.MightBeFunctionDecl &&
5768 Line.mightBeFunctionDefinition() &&
Left.MightBeFunctionDeclParen &&
5769 Left.ParameterCount > 0) {
5775 if (Style.PackParameters.BinPack == FormatStyle::BPPS_AlwaysOnePerLine &&
5776 Line.MightBeFunctionDecl && !
Left.opensScope() &&
5781 const auto *BeforeLeft =
Left.Previous;
5782 const auto *AfterRight =
Right.Next;
5784 if (Style.isCSharp()) {
5785 if (
Left.is(TT_FatArrow) &&
Right.is(tok::l_brace) &&
5786 Style.BraceWrapping.AfterFunction) {
5789 if (
Right.is(TT_CSharpNamedArgumentColon) ||
5790 Left.is(TT_CSharpNamedArgumentColon)) {
5793 if (
Right.is(TT_CSharpGenericTypeConstraint))
5795 if (AfterRight && AfterRight->is(TT_FatArrow) &&
5796 (
Right.is(tok::numeric_constant) ||
5797 (
Right.is(tok::identifier) &&
Right.TokenText ==
"_"))) {
5802 if (
Left.is(TT_AttributeRSquare) &&
5803 (
Right.isAccessSpecifier(
false) ||
5804 Right.is(Keywords.kw_internal))) {
5808 if (
Left.is(TT_AttributeRSquare) &&
Right.is(TT_AttributeLSquare))
5810 }
else if (Style.isJavaScript()) {
5812 if (
Right.is(tok::string_literal) &&
Left.is(tok::plus) && BeforeLeft &&
5813 BeforeLeft->is(tok::string_literal)) {
5816 if (
Left.is(TT_DictLiteral) &&
Left.is(tok::l_brace) &&
Line.Level == 0 &&
5817 BeforeLeft && BeforeLeft->is(tok::equal) &&
5818 Line.First->isOneOf(tok::identifier, Keywords.kw_import, tok::kw_export,
5822 Line.First->isNoneOf(Keywords.kw_var, Keywords.kw_let)) {
5827 if (
Left.is(tok::l_brace) &&
Line.Level == 0 &&
5828 (
Line.startsWith(tok::kw_enum) ||
5829 Line.startsWith(tok::kw_const, tok::kw_enum) ||
5830 Line.startsWith(tok::kw_export, tok::kw_enum) ||
5831 Line.startsWith(tok::kw_export, tok::kw_const, tok::kw_enum))) {
5836 if (
Right.is(tok::r_brace) &&
Left.is(tok::l_brace) && BeforeLeft &&
5837 BeforeLeft->is(TT_FatArrow)) {
5839 switch (Style.AllowShortLambdasOnASingleLine) {
5840 case FormatStyle::SLS_All:
5842 case FormatStyle::SLS_None:
5844 case FormatStyle::SLS_Empty:
5845 return !
Left.Children.empty();
5846 case FormatStyle::SLS_Inline:
5849 return (
Left.NestingLevel == 0 &&
Line.Level == 0) &&
5850 !
Left.Children.empty();
5852 llvm_unreachable(
"Unknown FormatStyle::ShortLambdaStyle enum");
5855 if (
Right.is(tok::r_brace) &&
Left.is(tok::l_brace) &&
5856 !
Left.Children.empty()) {
5858 if (
Left.NestingLevel == 0 &&
Line.Level == 0)
5859 return !Style.AllowShortFunctionsOnASingleLine.Other;
5861 return !Style.AllowShortFunctionsOnASingleLine.Inline;
5863 }
else if (Style.isJava()) {
5864 if (
Right.is(tok::plus) &&
Left.is(tok::string_literal) && AfterRight &&
5865 AfterRight->is(tok::string_literal)) {
5868 }
else if (Style.isVerilog()) {
5870 if (
Left.is(TT_VerilogAssignComma))
5873 if (
Left.is(TT_VerilogTypeComma))
5877 if (Style.VerilogBreakBetweenInstancePorts &&
5878 (
Left.is(TT_VerilogInstancePortComma) ||
5879 (
Left.is(tok::r_paren) && Keywords.isVerilogIdentifier(Right) &&
5880 Left.MatchingParen &&
5881 Left.MatchingParen->is(TT_VerilogInstancePortLParen)))) {
5886 if (!Keywords.isVerilogBegin(Right) && Keywords.isVerilogEndOfLabel(Left))
5888 }
else if (Style.BreakAdjacentStringLiterals &&
5889 (IsCpp || Style.isProto() || Style.isTableGen())) {
5890 if (
Left.isStringLiteral() &&
Right.isStringLiteral())
5895 if (Style.isJson()) {
5899 if (
Left.is(TT_DictLiteral) &&
Left.is(tok::l_brace))
5902 if ((
Left.is(TT_ArrayInitializerLSquare) &&
Left.is(tok::l_square) &&
5903 Right.isNot(tok::r_square)) ||
5904 Left.is(tok::comma)) {
5905 if (
Right.is(tok::l_brace))
5910 if (
Tok->isOneOf(tok::l_brace, tok::l_square))
5912 if (
Tok->isOneOf(tok::r_brace, tok::r_square))
5915 return Style.BreakArrays;
5917 }
else if (Style.isTableGen()) {
5921 if (
Left.is(TT_TableGenCondOperatorComma))
5923 if (
Left.is(TT_TableGenDAGArgOperatorToBreak) &&
5924 Right.isNot(TT_TableGenDAGArgCloser)) {
5927 if (
Left.is(TT_TableGenDAGArgListCommaToBreak))
5929 if (
Right.is(TT_TableGenDAGArgCloser) &&
Right.MatchingParen &&
5930 Right.MatchingParen->is(TT_TableGenDAGArgOpenerToBreak) &&
5931 &Left !=
Right.MatchingParen->Next) {
5933 return Style.TableGenBreakInsideDAGArg == FormatStyle::DAS_BreakAll;
5937 if (
Line.startsWith(tok::kw_asm) &&
Right.is(TT_InlineASMColon) &&
5938 Style.BreakBeforeInlineASMColon == FormatStyle::BBIAS_Always) {
5948 if ((
Left.isOneOf(tok::l_brace, TT_ArrayInitializerLSquare) ||
5949 (Style.isJavaScript() &&
Left.is(tok::l_paren))) &&
5951 BeforeClosingBrace =
Left.MatchingParen->Previous;
5952 }
else if (
Right.MatchingParen &&
5953 (
Right.MatchingParen->isOneOf(tok::l_brace,
5954 TT_ArrayInitializerLSquare) ||
5955 (Style.isJavaScript() &&
5956 Right.MatchingParen->is(tok::l_paren)))) {
5957 BeforeClosingBrace = &
Left;
5959 if (BeforeClosingBrace && (BeforeClosingBrace->is(tok::comma) ||
5960 BeforeClosingBrace->isTrailingComment())) {
5965 if (
Right.is(tok::comment)) {
5967 Right.NewlinesBefore > 0 &&
Right.HasUnescapedNewline;
5969 if (
Left.isTrailingComment())
5971 if (
Left.IsUnterminatedLiteral)
5974 if (BeforeLeft && BeforeLeft->is(tok::lessless) &&
5975 Left.is(tok::string_literal) &&
Right.is(tok::lessless) && AfterRight &&
5976 AfterRight->is(tok::string_literal)) {
5977 return Right.NewlinesBefore > 0;
5980 if (
Right.is(TT_RequiresClause)) {
5981 switch (Style.RequiresClausePosition) {
5982 case FormatStyle::RCPS_OwnLine:
5983 case FormatStyle::RCPS_OwnLineWithBrace:
5984 case FormatStyle::RCPS_WithFollowing:
5991 if (
Left.ClosesTemplateDeclaration &&
Left.MatchingParen &&
5992 Left.MatchingParen->NestingLevel == 0) {
5996 if (
Right.is(tok::kw_concept))
5997 return Style.BreakBeforeConceptDeclarations == FormatStyle::BBCDS_Always;
5998 return Style.BreakTemplateDeclarations == FormatStyle::BTDS_Yes ||
5999 (Style.BreakTemplateDeclarations == FormatStyle::BTDS_Leave &&
6000 Right.NewlinesBefore > 0);
6002 if (
Left.ClosesRequiresClause) {
6003 switch (Style.RequiresClausePosition) {
6004 case FormatStyle::RCPS_OwnLine:
6005 case FormatStyle::RCPS_WithPreceding:
6006 return Right.isNot(tok::semi);
6007 case FormatStyle::RCPS_OwnLineWithBrace:
6008 return Right.isNoneOf(tok::semi, tok::l_brace);
6013 if (Style.PackConstructorInitializers == FormatStyle::PCIS_Never) {
6014 if (Style.BreakConstructorInitializers == FormatStyle::BCIS_BeforeColon &&
6015 (
Left.is(TT_CtorInitializerComma) ||
6016 Right.is(TT_CtorInitializerColon))) {
6020 if (Style.BreakConstructorInitializers == FormatStyle::BCIS_AfterColon &&
6021 Left.isOneOf(TT_CtorInitializerColon, TT_CtorInitializerComma)) {
6025 if (Style.BreakConstructorInitializers == FormatStyle::BCIS_AfterComma &&
6026 Left.is(TT_CtorInitializerComma)) {
6030 if (Style.PackConstructorInitializers < FormatStyle::PCIS_CurrentLine &&
6031 Style.BreakConstructorInitializers == FormatStyle::BCIS_BeforeComma &&
6032 Right.isOneOf(TT_CtorInitializerComma, TT_CtorInitializerColon)) {
6035 if (Style.PackConstructorInitializers == FormatStyle::PCIS_NextLineOnly) {
6036 if ((Style.BreakConstructorInitializers == FormatStyle::BCIS_BeforeColon ||
6037 Style.BreakConstructorInitializers == FormatStyle::BCIS_BeforeComma) &&
6038 Right.is(TT_CtorInitializerColon)) {
6042 if (Style.BreakConstructorInitializers == FormatStyle::BCIS_AfterColon &&
6043 Left.is(TT_CtorInitializerColon)) {
6048 if (Style.BreakInheritanceList == FormatStyle::BILS_BeforeComma &&
6049 Right.is(TT_InheritanceComma)) {
6052 if (Style.BreakInheritanceList == FormatStyle::BILS_AfterComma &&
6053 Left.is(TT_InheritanceComma)) {
6056 if (
Right.is(tok::string_literal) &&
Right.TokenText.starts_with(
"R\"")) {
6060 return Right.IsMultiline &&
Right.NewlinesBefore > 0;
6062 if ((
Left.is(tok::l_brace) ||
6063 (
Left.is(tok::less) && BeforeLeft && BeforeLeft->is(tok::equal))) &&
6064 Right.NestingLevel == 1 && Style.Language == FormatStyle::LK_Proto) {
6069 if (
Right.is(TT_InlineASMBrace))
6070 return Right.HasUnescapedNewline;
6073 auto *FirstNonComment =
Line.getFirstNonComment();
6075 FirstNonComment && (FirstNonComment->is(Keywords.kw_internal) ||
6076 FirstNonComment->isAccessSpecifierKeyword());
6078 if (Style.BraceWrapping.AfterEnum) {
6079 if (
Line.startsWith(tok::kw_enum) ||
6080 Line.startsWith(tok::kw_typedef, tok::kw_enum) ||
6081 Line.startsWith(tok::kw_export, tok::kw_enum)) {
6086 FirstNonComment->Next->is(tok::kw_enum)) {
6092 if (Style.BraceWrapping.AfterClass &&
6094 FirstNonComment->Next->is(Keywords.kw_interface)) ||
6095 Line.startsWith(Keywords.kw_interface))) {
6100 if (
Right.isNot(TT_FunctionLBrace)) {
6101 return Style.AllowShortRecordOnASingleLine == FormatStyle::SRS_Never &&
6102 ((
Line.startsWith(tok::kw_class) &&
6103 Style.BraceWrapping.AfterClass) ||
6104 (
Line.startsWith(tok::kw_struct) &&
6105 Style.BraceWrapping.AfterStruct) ||
6106 (
Line.startsWith(tok::kw_union) &&
6107 Style.BraceWrapping.AfterUnion));
6111 if (
Left.is(TT_ObjCBlockLBrace) &&
6112 Style.AllowShortBlocksOnASingleLine == FormatStyle::SBS_Never) {
6117 if (
Left.isOneOf(TT_AttributeRParen, TT_AttributeMacro) &&
6118 Right.is(TT_ObjCDecl)) {
6122 if (
Left.is(TT_LambdaLBrace)) {
6124 Style.AllowShortLambdasOnASingleLine == FormatStyle::SLS_Inline) {
6128 if (Style.AllowShortLambdasOnASingleLine == FormatStyle::SLS_None ||
6129 Style.AllowShortLambdasOnASingleLine == FormatStyle::SLS_Inline ||
6130 (!
Left.Children.empty() &&
6131 Style.AllowShortLambdasOnASingleLine == FormatStyle::SLS_Empty)) {
6136 if (Style.BraceWrapping.BeforeLambdaBody &&
Right.is(TT_LambdaLBrace) &&
6137 (
Left.isPointerOrReference() ||
Left.is(TT_TemplateCloser))) {
6142 if ((Style.isJava() || Style.isJavaScript()) &&
6143 Left.is(TT_LeadingJavaAnnotation) &&
6144 Right.isNoneOf(TT_LeadingJavaAnnotation, tok::l_paren) &&
6145 (
Line.Last->is(tok::l_brace) || Style.BreakAfterJavaFieldAnnotations)) {
6149 if (
Right.is(TT_ProtoExtensionLSquare))
6179 if (Style.isProto() &&
Right.is(TT_SelectorName) &&
6180 Right.isNot(tok::r_square) && AfterRight) {
6183 if (
Left.is(tok::at))
6189 const auto *LBrace = AfterRight;
6190 if (LBrace && LBrace->is(tok::colon)) {
6191 LBrace = LBrace->Next;
6192 if (LBrace && LBrace->is(tok::at)) {
6193 LBrace = LBrace->Next;
6195 LBrace = LBrace->Next;
6207 ((LBrace->is(tok::l_brace) &&
6208 (LBrace->is(TT_DictLiteral) ||
6209 (LBrace->Next && LBrace->Next->is(tok::r_brace)))) ||
6210 LBrace->isOneOf(TT_ArrayInitializerLSquare, tok::less))) {
6217 if (
Left.ParameterCount == 0)
6232 if (
Left.isOneOf(tok::r_brace, tok::greater, tok::r_square))
6236 if (Style.BreakAfterAttributes == FormatStyle::ABS_LeaveAll &&
6237 Left.is(TT_AttributeRSquare) &&
Right.NewlinesBefore > 0) {
6238 Line.ReturnTypeWrapped =
true;
6249 if (Style.isCSharp()) {
6250 if (
Left.isOneOf(TT_CSharpNamedArgumentColon, TT_AttributeColon) ||
6251 Right.isOneOf(TT_CSharpNamedArgumentColon, TT_AttributeColon)) {
6255 if (
Line.First->is(TT_CSharpGenericTypeConstraint))
6256 return Left.is(TT_CSharpGenericTypeConstraintComma);
6258 if (
Right.is(TT_CSharpNullable))
6260 }
else if (Style.isJava()) {
6261 if (
Left.isOneOf(Keywords.kw_throws, Keywords.kw_extends,
6262 Keywords.kw_implements)) {
6265 if (
Right.isOneOf(Keywords.kw_throws, Keywords.kw_extends,
6266 Keywords.kw_implements)) {
6269 }
else if (Style.isJavaScript()) {
6272 (NonComment->isAccessSpecifierKeyword() ||
6273 NonComment->isOneOf(
6274 tok::kw_return, Keywords.kw_yield, tok::kw_continue, tok::kw_break,
6275 tok::kw_throw, Keywords.kw_interface, Keywords.kw_type,
6276 tok::kw_static, Keywords.kw_readonly, Keywords.kw_override,
6277 Keywords.kw_abstract, Keywords.kw_get, Keywords.kw_set,
6278 Keywords.kw_async, Keywords.kw_await))) {
6281 if (
Right.NestingLevel == 0 &&
6282 (
Left.Tok.getIdentifierInfo() ||
6283 Left.isOneOf(tok::r_square, tok::r_paren)) &&
6284 Right.isOneOf(tok::l_square, tok::l_paren)) {
6287 if (NonComment && NonComment->is(tok::identifier) &&
6288 NonComment->TokenText ==
"asserts") {
6291 if (
Left.is(TT_FatArrow) &&
Right.is(tok::l_brace))
6293 if (
Left.is(TT_JsTypeColon))
6296 if (
Left.is(tok::exclaim) &&
Right.is(tok::colon))
6301 if (
Right.is(Keywords.kw_is)) {
6310 if (!
Next ||
Next->isNot(tok::colon))
6313 if (
Left.is(Keywords.kw_in))
6314 return Style.BreakBeforeBinaryOperators == FormatStyle::BOS_None;
6315 if (
Right.is(Keywords.kw_in))
6316 return Style.BreakBeforeBinaryOperators != FormatStyle::BOS_None;
6317 if (
Right.is(Keywords.kw_as))
6319 if (
Right.isOneOf(Keywords.kw_extends, Keywords.kw_infer)) {
6325 if (
Left.is(Keywords.kw_as))
6327 if (
Left.is(TT_NonNullAssertion))
6329 if (
Left.is(Keywords.kw_declare) &&
6330 Right.isOneOf(Keywords.kw_module, tok::kw_namespace,
6331 Keywords.kw_function, tok::kw_class, tok::kw_enum,
6332 Keywords.kw_interface, Keywords.kw_type, Keywords.kw_var,
6333 Keywords.kw_let, tok::kw_const)) {
6338 if (
Left.isOneOf(Keywords.kw_module, tok::kw_namespace) &&
6339 Right.isOneOf(tok::identifier, tok::string_literal)) {
6342 if (
Right.is(TT_TemplateString) &&
Right.closesScope())
6346 if (
Left.is(tok::identifier) &&
Right.is(TT_TemplateString))
6348 if (
Left.is(TT_TemplateString) &&
Left.opensScope())
6350 }
else if (Style.isTableGen()) {
6352 if (Keywords.isTableGenDefinition(Left))
6355 if (
Right.is(tok::l_paren)) {
6356 return Left.isNoneOf(TT_TableGenBangOperator, TT_TableGenCondOperator,
6360 if (
Left.is(TT_TableGenValueSuffix))
6363 if (
Left.is(tok::hash) ||
Right.is(tok::hash))
6365 if (
Left.isOneOf(TT_TableGenBangOperator, TT_TableGenCondOperator))
6372 if (
Right.is(tok::r_brace)) {
6374 (
Right.isBlockIndentedInitRBrace(Style)));
6379 if (
Right.is(tok::r_paren)) {
6380 if (!
Right.MatchingParen)
6383 if (
Next &&
Next->is(tok::r_paren))
6385 if (
Next &&
Next->is(tok::l_paren))
6391 return Style.BreakBeforeCloseBracketIf;
6393 return Style.BreakBeforeCloseBracketLoop;
6395 return Style.BreakBeforeCloseBracketSwitch;
6396 return Style.BreakBeforeCloseBracketFunction;
6399 if (
Left.isOneOf(tok::r_paren, TT_TrailingAnnotation) &&
6400 Right.is(TT_TrailingAnnotation) &&
6401 Style.BreakBeforeCloseBracketFunction) {
6405 if (
Right.is(TT_TemplateCloser))
6406 return Style.BreakBeforeTemplateCloser;
6408 if (
Left.isOneOf(tok::at, tok::objc_interface))
6410 if (
Left.isOneOf(TT_JavaAnnotation, TT_LeadingJavaAnnotation))
6411 return Right.isNot(tok::l_paren);
6412 if (
Right.is(TT_PointerOrReference)) {
6413 return Line.IsMultiVariableDeclStmt ||
6414 (getTokenPointerOrReferenceAlignment(Right) ==
6415 FormatStyle::PAS_Right &&
6417 Right.Next->isOneOf(TT_FunctionDeclarationName, tok::kw_const)));
6419 if (
Right.isOneOf(TT_StartOfName, TT_FunctionDeclarationName,
6420 TT_ClassHeadName, TT_QtProperty, tok::kw_operator)) {
6423 if (
Left.is(TT_PointerOrReference))
6425 if (
Right.isTrailingComment()) {
6432 (
Left.is(TT_CtorInitializerColon) &&
Right.NewlinesBefore > 0 &&
6433 Style.BreakConstructorInitializers == FormatStyle::BCIS_AfterColon);
6435 if (
Left.is(tok::question) &&
Right.is(tok::colon))
6437 if (
Right.isOneOf(TT_ConditionalExpr, tok::question))
6438 return Style.BreakBeforeTernaryOperators;
6439 if (
Left.isOneOf(TT_ConditionalExpr, tok::question))
6440 return !Style.BreakBeforeTernaryOperators;
6441 if (
Left.is(TT_InheritanceColon))
6442 return Style.BreakInheritanceList == FormatStyle::BILS_AfterColon;
6443 if (
Right.is(TT_InheritanceColon))
6444 return Style.BreakInheritanceList != FormatStyle::BILS_AfterColon;
6446 if (
Right.is(TT_ObjCMethodExpr) &&
Right.isNot(tok::r_square) &&
6447 Left.isNot(TT_SelectorName)) {
6451 if (
Right.is(tok::colon) &&
6452 Right.isNoneOf(TT_CtorInitializerColon, TT_InlineASMColon,
6453 TT_BitFieldColon)) {
6456 if (
Left.is(tok::colon) &&
Left.isOneOf(TT_ObjCSelector, TT_ObjCMethodExpr))
6458 if (
Left.is(tok::colon) &&
Left.is(TT_DictLiteral)) {
6459 if (Style.isProto()) {
6460 if (!Style.AlwaysBreakBeforeMultilineStrings &&
Right.isStringLiteral())
6486 if ((
Right.isOneOf(tok::l_brace, tok::less) &&
6487 Right.is(TT_DictLiteral)) ||
6488 Right.is(TT_ArrayInitializerLSquare)) {
6494 if (
Right.is(tok::r_square) &&
Right.MatchingParen &&
6495 Right.MatchingParen->is(TT_ProtoExtensionLSquare)) {
6498 if (
Right.is(TT_SelectorName) || (
Right.is(tok::identifier) &&
Right.Next &&
6499 Right.Next->is(TT_ObjCMethodExpr))) {
6500 return Left.isNot(tok::period);
6504 if (
Right.is(tok::kw_concept))
6505 return Style.BreakBeforeConceptDeclarations != FormatStyle::BBCDS_Never;
6506 if (
Right.is(TT_RequiresClause))
6508 if (
Left.ClosesTemplateDeclaration) {
6509 return Style.BreakTemplateDeclarations != FormatStyle::BTDS_Leave ||
6510 Right.NewlinesBefore > 0;
6512 if (
Left.is(TT_FunctionAnnotationRParen))
6514 if (
Left.ClosesRequiresClause)
6516 if (
Right.isOneOf(TT_RangeBasedForLoopColon, TT_OverloadedOperatorLParen,
6517 TT_OverloadedOperator)) {
6520 if (
Left.is(TT_RangeBasedForLoopColon))
6522 if (
Right.is(TT_RangeBasedForLoopColon))
6524 if (
Left.is(TT_TemplateCloser) &&
Right.is(TT_TemplateOpener))
6526 if ((
Left.is(tok::greater) &&
Right.is(tok::greater)) ||
6527 (
Left.is(tok::less) &&
Right.is(tok::less))) {
6530 if (
Right.is(TT_BinaryOperator) &&
6531 Style.BreakBeforeBinaryOperators != FormatStyle::BOS_None &&
6532 (Style.BreakBeforeBinaryOperators == FormatStyle::BOS_All ||
6536 if (
Left.isOneOf(TT_TemplateCloser, TT_UnaryOperator, tok::kw_operator))
6538 if (
Left.is(tok::equal) &&
Right.isNoneOf(tok::kw_default, tok::kw_delete) &&
6542 if (
Left.is(tok::equal) &&
Right.is(tok::l_brace) &&
6543 Style.Cpp11BracedListStyle == FormatStyle::BLS_Block) {
6546 if (
Left.is(TT_AttributeLParen) ||
6547 (
Left.is(tok::l_paren) &&
Left.is(TT_TypeDeclarationParen))) {
6550 if (
Left.is(tok::l_paren) &&
Left.Previous &&
6551 (
Left.Previous->isOneOf(TT_BinaryOperator, TT_CastRParen))) {
6554 if (
Right.is(TT_ImplicitStringLiteral))
6557 if (
Right.is(tok::r_square) &&
Right.MatchingParen &&
6558 Right.MatchingParen->is(TT_LambdaLSquare)) {
6564 if (
Left.is(TT_TrailingAnnotation)) {
6565 return Right.isNoneOf(tok::l_brace, tok::semi, tok::equal, tok::l_paren,
6566 tok::less, tok::coloncolon);
6569 if (
Right.isAttribute())
6572 if (
Right.is(TT_AttributeLSquare)) {
6573 assert(
Left.isNot(tok::l_square));
6577 if (
Left.is(tok::identifier) &&
Right.is(tok::string_literal))
6580 if (
Right.is(tok::identifier) &&
Right.Next &&
Right.Next->is(TT_DictLiteral))
6583 if (
Left.is(TT_CtorInitializerColon)) {
6584 return (Style.BreakConstructorInitializers ==
6585 FormatStyle::BCIS_AfterColon ||
6586 Style.BreakConstructorInitializers ==
6587 FormatStyle::BCIS_AfterComma) &&
6588 (!
Right.isTrailingComment() ||
Right.NewlinesBefore > 0);
6590 if (
Right.is(TT_CtorInitializerColon)) {
6591 return Style.BreakConstructorInitializers != FormatStyle::BCIS_AfterColon &&
6592 Style.BreakConstructorInitializers != FormatStyle::BCIS_AfterComma;
6594 if (
Left.is(TT_CtorInitializerComma) &&
6595 Style.BreakConstructorInitializers == FormatStyle::BCIS_BeforeComma) {
6598 if (
Right.is(TT_CtorInitializerComma) &&
6599 Style.BreakConstructorInitializers == FormatStyle::BCIS_BeforeComma) {
6602 if (
Left.is(TT_InheritanceComma) &&
6603 Style.BreakInheritanceList == FormatStyle::BILS_BeforeComma) {
6606 if (
Right.is(TT_InheritanceComma) &&
6607 Style.BreakInheritanceList == FormatStyle::BILS_BeforeComma) {
6610 if (
Left.is(TT_ArrayInitializerLSquare))
6612 if (
Right.is(tok::kw_typename) &&
Left.isNot(tok::kw_const))
6614 if ((
Left.isBinaryOperator() ||
Left.is(TT_BinaryOperator)) &&
6615 Left.isNoneOf(tok::arrowstar, tok::lessless) &&
6616 Style.BreakBeforeBinaryOperators != FormatStyle::BOS_All &&
6617 (Style.BreakBeforeBinaryOperators == FormatStyle::BOS_None ||
6621 if (
Left.is(TT_AttributeLSquare) &&
Right.is(tok::l_square)) {
6622 assert(
Right.isNot(TT_AttributeLSquare));
6625 if (
Left.is(tok::r_square) &&
Right.is(TT_AttributeRSquare)) {
6626 assert(
Left.isNot(TT_AttributeRSquare));
6630 auto ShortLambdaOption = Style.AllowShortLambdasOnASingleLine;
6631 if (Style.BraceWrapping.BeforeLambdaBody &&
Right.is(TT_LambdaLBrace)) {
6638 if (
Right.is(tok::kw_noexcept) &&
Right.is(TT_TrailingAnnotation)) {
6639 switch (Style.AllowBreakBeforeNoexceptSpecifier) {
6640 case FormatStyle::BBNSS_Never:
6642 case FormatStyle::BBNSS_Always:
6644 case FormatStyle::BBNSS_OnlyWithParen:
6645 return Right.Next &&
Right.Next->is(tok::l_paren);
6649 return Left.isOneOf(tok::comma, tok::coloncolon, tok::semi, tok::l_brace,
6650 tok::kw_class, tok::kw_struct, tok::comment) ||
6651 Right.isMemberAccess() ||
6652 Right.isOneOf(TT_TrailingReturnArrow, TT_LambdaArrow, tok::lessless,
6653 tok::colon, tok::l_square, tok::at) ||
6654 (
Left.is(tok::r_paren) &&
6655 Right.isOneOf(tok::identifier, tok::kw_const)) ||
6656 (
Left.is(tok::l_paren) &&
Right.isNot(tok::r_paren)) ||
6657 (
Left.is(TT_TemplateOpener) &&
Right.isNot(TT_TemplateCloser));
6661 llvm::errs() <<
"AnnotatedTokens(L=" <<
Line.Level <<
", P=" <<
Line.PPLevel
6662 <<
", T=" <<
Line.Type <<
", C=" <<
Line.IsContinuation
6666 llvm::errs() <<
" I=" <<
Tok->IndentLevel <<
" M=" <<
Tok->MustBreakBefore
6667 <<
" C=" <<
Tok->CanBreakBefore
6669 <<
" S=" <<
Tok->SpacesRequiredBefore
6670 <<
" F=" <<
Tok->Finalized <<
" B=" <<
Tok->BlockParameterCount
6671 <<
" BK=" <<
Tok->getBlockKind() <<
" P=" <<
Tok->SplitPenalty
6672 <<
" Name=" <<
Tok->Tok.getName() <<
" N=" <<
Tok->NestingLevel
6673 <<
" L=" <<
Tok->TotalLength
6674 <<
" PPK=" <<
Tok->getPackingKind() <<
" FakeLParens=";
6676 llvm::errs() << LParen <<
"/";
6677 llvm::errs() <<
" FakeRParens=" <<
Tok->FakeRParens;
6678 llvm::errs() <<
" II=" <<
Tok->Tok.getIdentifierInfo();
6679 llvm::errs() <<
" Text='" <<
Tok->TokenText <<
"'\n";
6684 llvm::errs() <<
"----\n";
6687FormatStyle::PointerAlignmentStyle
6689 assert(
Reference.isOneOf(tok::amp, tok::ampamp));
6690 switch (Style.ReferenceAlignment) {
6691 case FormatStyle::RAS_Pointer:
6692 return Style.PointerAlignment;
6693 case FormatStyle::RAS_Left:
6694 return FormatStyle::PAS_Left;
6695 case FormatStyle::RAS_Right:
6696 return FormatStyle::PAS_Right;
6697 case FormatStyle::RAS_Middle:
6698 return FormatStyle::PAS_Middle;
6701 return Style.PointerAlignment;
6704FormatStyle::PointerAlignmentStyle
6705TokenAnnotator::getTokenPointerOrReferenceAlignment(
6707 if (PointerOrReference.isOneOf(tok::amp, tok::ampamp))
6708 return getTokenReferenceAlignment(PointerOrReference);
6709 assert(PointerOrReference.is(tok::star));
6710 return Style.PointerAlignment;
This file implements a token annotator, i.e.
Defines the clang::TokenKind enum and support functions.
#define TRANSFORM_TYPE_TRAIT_DEF(Enum, _)
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
Parser - This implements a parser for the C family of languages.
PRESERVE_NONE bool Ret(InterpState &S, CodePtr &PC)
TokenKind
Provides a simple uniform namespace for tokens from all C languages.
The JSON file list parser is used to communicate input to InstallAPI.
AccessSpecifier
A C++ access specifier (public, private, protected), plus the special value "none" which means differ...
nullptr
This class represents a compute construct, representing a 'Kind' of ‘parallel’, 'serial',...
@ Parameter
The parameter type of a method or function.
@ Result
The result type of a method or function.
@ Type
The name was classified as a type.
prec::Level getBinOpPrecedence(tok::TokenKind Kind, bool GreaterThanIsOperator, bool CPlusPlus11)
Return the precedence of the specified binary operator token.