18 #include "llvm/ADT/SmallPtrSet.h" 19 #include "llvm/Support/Debug.h" 21 #define DEBUG_TYPE "format-token-annotator" 39 static bool canBeObjCSelectorComponent(
const FormatToken &
Tok) {
40 return Tok.Tok.getIdentifierInfo() !=
nullptr;
48 class AnnotatingParser {
50 AnnotatingParser(
const FormatStyle &
Style, AnnotatedLine &
Line,
51 const AdditionalKeywords &Keywords)
52 :
Style(Style),
Line(Line), CurrentToken(Line.First), AutoFound(
false),
54 Contexts.push_back(Context(tok::unknown, 1,
false));
55 resetTokenMetadata(CurrentToken);
60 if (!CurrentToken || !CurrentToken->Previous)
62 if (NonTemplateLess.count(CurrentToken->Previous))
65 const FormatToken &
Previous = *CurrentToken->Previous;
66 if (Previous.Previous) {
67 if (Previous.Previous->Tok.isLiteral())
69 if (Previous.Previous->is(tok::r_paren) && Contexts.size() > 1 &&
70 (!Previous.Previous->MatchingParen ||
71 !Previous.Previous->MatchingParen->is(TT_OverloadedOperatorLParen)))
75 FormatToken *Left = CurrentToken->Previous;
76 Left->ParentBracket = Contexts.back().ContextKind;
77 ScopedContextCreator ContextCreator(*
this, tok::less, 12);
81 bool InExprContext = Contexts.back().IsExpression;
83 Contexts.back().IsExpression =
false;
86 Contexts.back().InTemplateArgument =
87 Left->Previous && Left->Previous->Tok.isNot(tok::kw_template);
89 if (Style.Language == FormatStyle::LK_Java &&
90 CurrentToken->is(tok::question))
93 while (CurrentToken) {
94 if (CurrentToken->is(tok::greater)) {
95 Left->MatchingParen = CurrentToken;
96 CurrentToken->MatchingParen = Left;
102 if (Style.Language == FormatStyle::LK_TextProto ||
103 (Style.Language == FormatStyle::LK_Proto && Left->Previous &&
104 Left->Previous->isOneOf(TT_SelectorName, TT_DictLiteral)))
105 CurrentToken->Type = TT_DictLiteral;
107 CurrentToken->Type = TT_TemplateCloser;
111 if (CurrentToken->is(tok::question) &&
112 Style.Language == FormatStyle::LK_Java) {
116 if (CurrentToken->isOneOf(tok::r_paren, tok::r_square, tok::r_brace) ||
117 (CurrentToken->isOneOf(tok::colon, tok::question) && InExprContext &&
118 Style.Language != FormatStyle::LK_Proto &&
119 Style.Language != FormatStyle::LK_TextProto))
127 if (CurrentToken->Previous->isOneOf(tok::pipepipe, tok::ampamp) &&
128 CurrentToken->Previous->is(TT_BinaryOperator) &&
129 Contexts[Contexts.size() - 2].IsExpression &&
130 !Line.startsWith(tok::kw_template))
132 updateParameterCount(Left, CurrentToken);
133 if (Style.Language == FormatStyle::LK_Proto) {
134 if (FormatToken *Previous = CurrentToken->getPreviousNonComment()) {
135 if (CurrentToken->is(tok::colon) ||
136 (CurrentToken->isOneOf(tok::l_brace, tok::less) &&
137 Previous->isNot(tok::colon)))
138 Previous->Type = TT_SelectorName;
147 bool parseParens(
bool LookForDecls =
false) {
150 FormatToken *Left = CurrentToken->Previous;
151 Left->ParentBracket = Contexts.back().ContextKind;
152 ScopedContextCreator ContextCreator(*
this, tok::l_paren, 1);
155 Contexts.back().ColonIsForRangeExpr =
156 Contexts.size() == 2 && Contexts[0].ColonIsForRangeExpr;
158 bool StartsObjCMethodExpr =
false;
159 if (FormatToken *MaybeSel = Left->Previous) {
161 if (MaybeSel->isObjCAtKeyword(tok::objc_selector) && MaybeSel->Previous &&
162 MaybeSel->Previous->is(tok::at)) {
163 StartsObjCMethodExpr =
true;
167 if (Left->is(TT_OverloadedOperatorLParen)) {
168 Contexts.back().IsExpression =
false;
169 }
else if (Style.Language == FormatStyle::LK_JavaScript &&
170 (Line.startsWith(Keywords.kw_type, tok::identifier) ||
171 Line.startsWith(tok::kw_export, Keywords.kw_type,
175 Contexts.back().IsExpression =
false;
176 }
else if (Left->Previous &&
177 (Left->Previous->isOneOf(tok::kw_static_assert, tok::kw_decltype,
178 tok::kw_if, tok::kw_while, tok::l_paren,
180 Left->Previous->endsSequence(tok::kw_constexpr, tok::kw_if) ||
181 Left->Previous->is(TT_BinaryOperator))) {
183 Contexts.back().IsExpression =
true;
184 }
else if (Style.Language == FormatStyle::LK_JavaScript && Left->Previous &&
185 (Left->Previous->is(Keywords.kw_function) ||
186 (Left->Previous->endsSequence(tok::identifier,
187 Keywords.kw_function)))) {
189 Contexts.back().IsExpression =
false;
190 }
else if (Style.Language == FormatStyle::LK_JavaScript && Left->Previous &&
191 Left->Previous->is(TT_JsTypeColon)) {
193 Contexts.back().IsExpression =
false;
194 }
else if (Left->Previous && Left->Previous->is(tok::r_square) &&
195 Left->Previous->MatchingParen &&
196 Left->Previous->MatchingParen->is(TT_LambdaLSquare)) {
198 Contexts.back().IsExpression =
false;
199 }
else if (Line.InPPDirective &&
200 (!Left->Previous || !Left->Previous->is(tok::identifier))) {
201 Contexts.back().IsExpression =
true;
202 }
else if (Contexts[Contexts.size() - 2].CaretFound) {
204 Contexts.back().IsExpression =
false;
205 }
else if (Left->Previous && Left->Previous->is(tok::kw___attribute)) {
206 Left->Type = TT_AttributeParen;
207 }
else if (Left->Previous && Left->Previous->is(TT_ForEachMacro)) {
209 Contexts.back().IsForEachMacro =
true;
210 Contexts.back().IsExpression =
false;
211 }
else if (Left->Previous && Left->Previous->MatchingParen &&
212 Left->Previous->MatchingParen->is(TT_ObjCBlockLParen)) {
213 Contexts.back().IsExpression =
false;
214 }
else if (!Line.MustBeDeclaration && !Line.InPPDirective) {
216 Left->Previous && Left->Previous->isOneOf(tok::kw_for, tok::kw_catch);
217 Contexts.back().IsExpression = !IsForOrCatch;
220 if (StartsObjCMethodExpr) {
221 Contexts.back().ColonIsObjCMethodExpr =
true;
222 Left->Type = TT_ObjCMethodExpr;
232 bool MightBeFunctionType = !Contexts[Contexts.size() - 2].IsExpression;
233 bool ProbablyFunctionType =
234 CurrentToken->isOneOf(tok::star, tok::amp, tok::caret);
235 bool HasMultipleLines =
false;
236 bool HasMultipleParametersOnALine =
false;
237 bool MightBeObjCForRangeLoop =
238 Left->Previous && Left->Previous->is(tok::kw_for);
239 FormatToken *PossibleObjCForInToken =
nullptr;
240 while (CurrentToken) {
245 if (LookForDecls && CurrentToken->Next) {
246 FormatToken *Prev = CurrentToken->getPreviousNonComment();
248 FormatToken *PrevPrev = Prev->getPreviousNonComment();
249 FormatToken *Next = CurrentToken->Next;
250 if (PrevPrev && PrevPrev->is(tok::identifier) &&
251 Prev->isOneOf(tok::star, tok::amp, tok::ampamp) &&
252 CurrentToken->is(tok::identifier) && Next->isNot(tok::equal)) {
253 Prev->Type = TT_BinaryOperator;
254 LookForDecls =
false;
259 if (CurrentToken->Previous->is(TT_PointerOrReference) &&
260 CurrentToken->Previous->Previous->isOneOf(tok::l_paren,
262 ProbablyFunctionType =
true;
263 if (CurrentToken->is(tok::comma))
264 MightBeFunctionType =
false;
265 if (CurrentToken->Previous->is(TT_BinaryOperator))
266 Contexts.back().IsExpression =
true;
267 if (CurrentToken->is(tok::r_paren)) {
268 if (MightBeFunctionType && ProbablyFunctionType && CurrentToken->Next &&
269 (CurrentToken->Next->is(tok::l_paren) ||
270 (CurrentToken->Next->is(tok::l_square) && Line.MustBeDeclaration)))
271 Left->Type = Left->Next->is(tok::caret) ? TT_ObjCBlockLParen
272 : TT_FunctionTypeLParen;
273 Left->MatchingParen = CurrentToken;
274 CurrentToken->MatchingParen = Left;
276 if (CurrentToken->Next && CurrentToken->Next->is(tok::l_brace) &&
277 Left->Previous && Left->Previous->is(tok::l_paren)) {
281 for (FormatToken *Tok = Left; Tok != CurrentToken; Tok = Tok->Next) {
282 if (Tok->is(TT_BinaryOperator) &&
283 Tok->isOneOf(tok::star, tok::amp, tok::ampamp))
284 Tok->Type = TT_PointerOrReference;
288 if (StartsObjCMethodExpr) {
289 CurrentToken->Type = TT_ObjCMethodExpr;
290 if (Contexts.back().FirstObjCSelectorName) {
291 Contexts.back().FirstObjCSelectorName->LongestObjCSelectorName =
292 Contexts.back().LongestObjCSelectorName;
296 if (Left->is(TT_AttributeParen))
297 CurrentToken->Type = TT_AttributeParen;
298 if (Left->Previous && Left->Previous->is(TT_JavaAnnotation))
299 CurrentToken->Type = TT_JavaAnnotation;
300 if (Left->Previous && Left->Previous->is(TT_LeadingJavaAnnotation))
301 CurrentToken->Type = TT_LeadingJavaAnnotation;
302 if (Left->Previous && Left->Previous->is(TT_AttributeSquare))
303 CurrentToken->Type = TT_AttributeSquare;
305 if (!HasMultipleLines)
307 else if (HasMultipleParametersOnALine)
315 if (CurrentToken->isOneOf(tok::r_square, tok::r_brace))
318 if (CurrentToken->is(tok::l_brace))
319 Left->Type = TT_Unknown;
320 if (CurrentToken->is(tok::comma) && CurrentToken->Next &&
321 !CurrentToken->Next->HasUnescapedNewline &&
322 !CurrentToken->Next->isTrailingComment())
323 HasMultipleParametersOnALine =
true;
324 if ((CurrentToken->Previous->isOneOf(tok::kw_const, tok::kw_auto) ||
325 CurrentToken->Previous->isSimpleTypeSpecifier()) &&
326 !CurrentToken->is(tok::l_brace))
327 Contexts.back().IsExpression =
false;
328 if (CurrentToken->isOneOf(tok::semi, tok::colon)) {
329 MightBeObjCForRangeLoop =
false;
330 if (PossibleObjCForInToken) {
331 PossibleObjCForInToken->Type = TT_Unknown;
332 PossibleObjCForInToken =
nullptr;
335 if (MightBeObjCForRangeLoop && CurrentToken->is(Keywords.kw_in)) {
336 PossibleObjCForInToken = CurrentToken;
337 PossibleObjCForInToken->Type = TT_ObjCForIn;
341 if (CurrentToken->is(tok::comma))
342 Contexts.back().CanBeExpression =
true;
344 FormatToken *Tok = CurrentToken;
347 updateParameterCount(Left, Tok);
348 if (CurrentToken && CurrentToken->HasUnescapedNewline)
349 HasMultipleLines =
true;
354 bool isCSharpAttributeSpecifier(
const FormatToken &Tok) {
355 if (!Style.isCSharp())
358 const FormatToken *AttrTok = Tok.Next;
363 if (AttrTok->is(tok::r_square))
367 while (AttrTok && AttrTok->isNot(tok::r_square)) {
368 AttrTok = AttrTok->Next;
375 AttrTok = AttrTok->Next;
380 if (AttrTok->isOneOf(tok::kw_public, tok::kw_private, tok::kw_protected,
381 tok::kw_class, tok::kw_static, tok::l_square,
382 Keywords.kw_internal)) {
388 bool isCpp11AttributeSpecifier(
const FormatToken &Tok) {
389 if (!Style.isCpp() || !Tok.startsSequence(tok::l_square, tok::l_square))
392 if (Tok.Previous && Tok.Previous->is(tok::at)) {
395 const FormatToken *AttrTok = Tok.Next->Next;
400 if (AttrTok->startsSequence(tok::kw_using, tok::identifier, tok::colon))
402 if (AttrTok->isNot(tok::identifier))
404 while (AttrTok && !AttrTok->startsSequence(tok::r_square, tok::r_square)) {
408 if (AttrTok->is(tok::colon) ||
409 AttrTok->startsSequence(tok::identifier, tok::identifier) ||
410 AttrTok->startsSequence(tok::r_paren, tok::identifier))
412 if (AttrTok->is(tok::ellipsis))
414 AttrTok = AttrTok->Next;
416 return AttrTok && AttrTok->startsSequence(tok::r_square, tok::r_square);
427 FormatToken *Left = CurrentToken->Previous;
428 Left->ParentBracket = Contexts.back().ContextKind;
429 FormatToken *
Parent = Left->getPreviousNonComment();
434 bool CppArrayTemplates =
435 Style.isCpp() && Parent && Parent->is(TT_TemplateCloser) &&
436 (Contexts.back().CanBeExpression || Contexts.back().IsExpression ||
437 Contexts.back().InTemplateArgument);
439 bool IsCpp11AttributeSpecifier = isCpp11AttributeSpecifier(*Left) ||
440 Contexts.back().InCpp11AttributeSpecifier;
443 bool IsCSharp11AttributeSpecifier =
444 isCSharpAttributeSpecifier(*Left) ||
445 Contexts.back().InCSharpAttributeSpecifier;
447 bool InsideInlineASM = Line.startsWith(tok::kw_asm);
448 bool IsCppStructuredBinding = Left->isCppStructuredBinding(Style);
449 bool StartsObjCMethodExpr =
450 !IsCppStructuredBinding && !InsideInlineASM && !CppArrayTemplates &&
451 Style.isCpp() && !IsCpp11AttributeSpecifier &&
452 Contexts.back().CanBeExpression && Left->isNot(TT_LambdaLSquare) &&
453 !CurrentToken->isOneOf(tok::l_brace, tok::r_square) &&
455 Parent->isOneOf(tok::colon, tok::l_square, tok::l_paren,
456 tok::kw_return, tok::kw_throw) ||
457 Parent->isUnaryOperator() ||
459 Parent->isOneOf(TT_ObjCForIn, TT_CastRParen) ||
462 bool ColonFound =
false;
464 unsigned BindingIncrease = 1;
465 if (IsCppStructuredBinding) {
466 Left->Type = TT_StructuredBindingLSquare;
467 }
else if (Left->is(TT_Unknown)) {
468 if (StartsObjCMethodExpr) {
469 Left->Type = TT_ObjCMethodExpr;
470 }
else if (IsCpp11AttributeSpecifier) {
471 Left->Type = TT_AttributeSquare;
472 }
else if (Style.Language == FormatStyle::LK_JavaScript && Parent &&
473 Contexts.back().ContextKind == tok::l_brace &&
474 Parent->isOneOf(tok::l_brace, tok::comma)) {
475 Left->Type = TT_JsComputedPropertyName;
476 }
else if (Style.isCpp() && Contexts.back().ContextKind == tok::l_brace &&
477 Parent && Parent->isOneOf(tok::l_brace, tok::comma)) {
478 Left->Type = TT_DesignatedInitializerLSquare;
479 }
else if (CurrentToken->is(tok::r_square) && Parent &&
480 Parent->is(TT_TemplateCloser)) {
481 Left->Type = TT_ArraySubscriptLSquare;
482 }
else if (Style.Language == FormatStyle::LK_Proto ||
483 Style.Language == FormatStyle::LK_TextProto) {
510 Left->Type = TT_ArrayInitializerLSquare;
511 if (!Left->endsSequence(tok::l_square, tok::numeric_constant,
513 !Left->endsSequence(tok::l_square, tok::numeric_constant,
515 !Left->endsSequence(tok::l_square, tok::colon, TT_SelectorName)) {
516 Left->Type = TT_ProtoExtensionLSquare;
517 BindingIncrease = 10;
519 }
else if (!CppArrayTemplates && Parent &&
520 Parent->isOneOf(TT_BinaryOperator, TT_TemplateCloser, tok::at,
521 tok::comma, tok::l_paren, tok::l_square,
522 tok::question, tok::colon, tok::kw_return,
525 Left->Type = TT_ArrayInitializerLSquare;
526 }
else if (IsCSharp11AttributeSpecifier) {
527 Left->Type = TT_AttributeSquare;
529 BindingIncrease = 10;
530 Left->Type = TT_ArraySubscriptLSquare;
534 ScopedContextCreator ContextCreator(*
this, tok::l_square, BindingIncrease);
535 Contexts.back().IsExpression =
true;
536 if (Style.Language == FormatStyle::LK_JavaScript && Parent &&
537 Parent->is(TT_JsTypeColon))
538 Contexts.back().IsExpression =
false;
540 Contexts.back().ColonIsObjCMethodExpr = StartsObjCMethodExpr;
541 Contexts.back().InCpp11AttributeSpecifier = IsCpp11AttributeSpecifier;
542 Contexts.back().InCSharpAttributeSpecifier = IsCSharp11AttributeSpecifier;
544 while (CurrentToken) {
545 if (CurrentToken->is(tok::r_square)) {
546 if (IsCpp11AttributeSpecifier)
547 CurrentToken->Type = TT_AttributeSquare;
548 if (IsCSharp11AttributeSpecifier)
549 CurrentToken->Type = TT_AttributeSquare;
550 else if (((CurrentToken->Next &&
551 CurrentToken->Next->is(tok::l_paren)) ||
552 (CurrentToken->Previous &&
553 CurrentToken->Previous->Previous == Left)) &&
554 Left->is(TT_ObjCMethodExpr)) {
559 StartsObjCMethodExpr =
false;
560 Left->Type = TT_Unknown;
562 if (StartsObjCMethodExpr && CurrentToken->Previous != Left) {
563 CurrentToken->Type = TT_ObjCMethodExpr;
566 if (!ColonFound && CurrentToken->Previous &&
567 CurrentToken->Previous->is(TT_Unknown) &&
568 canBeObjCSelectorComponent(*CurrentToken->Previous))
569 CurrentToken->Previous->Type = TT_SelectorName;
573 if (Parent && Parent->is(TT_PointerOrReference))
574 Parent->Type = TT_BinaryOperator;
577 if (CurrentToken->Type == TT_ObjCMethodExpr && CurrentToken->Next &&
578 CurrentToken->Next->is(TT_LambdaArrow))
579 CurrentToken->Next->Type = TT_Unknown;
580 Left->MatchingParen = CurrentToken;
581 CurrentToken->MatchingParen = Left;
586 if (!Contexts.back().FirstObjCSelectorName) {
587 FormatToken *
Previous = CurrentToken->getPreviousNonComment();
588 if (Previous && Previous->is(TT_SelectorName)) {
589 Previous->ObjCSelectorNameParts = 1;
590 Contexts.back().FirstObjCSelectorName =
Previous;
593 Left->ParameterCount =
594 Contexts.back().FirstObjCSelectorName->ObjCSelectorNameParts;
596 if (Contexts.back().FirstObjCSelectorName) {
597 Contexts.back().FirstObjCSelectorName->LongestObjCSelectorName =
598 Contexts.back().LongestObjCSelectorName;
599 if (Left->BlockParameterCount > 1)
600 Contexts.back().FirstObjCSelectorName->LongestObjCSelectorName = 0;
605 if (CurrentToken->isOneOf(tok::r_paren, tok::r_brace))
607 if (CurrentToken->is(tok::colon)) {
608 if (IsCpp11AttributeSpecifier &&
609 CurrentToken->endsSequence(tok::colon, tok::identifier,
613 CurrentToken->Type = TT_AttributeColon;
614 }
else if (Left->isOneOf(TT_ArraySubscriptLSquare,
615 TT_DesignatedInitializerLSquare)) {
616 Left->Type = TT_ObjCMethodExpr;
617 StartsObjCMethodExpr =
true;
618 Contexts.back().ColonIsObjCMethodExpr =
true;
619 if (Parent && Parent->is(tok::r_paren))
621 Parent->Type = TT_CastRParen;
625 if (CurrentToken->is(tok::comma) && Left->is(TT_ObjCMethodExpr) &&
627 Left->Type = TT_ArrayInitializerLSquare;
628 FormatToken *Tok = CurrentToken;
631 updateParameterCount(Left, Tok);
638 FormatToken *Left = CurrentToken->Previous;
639 Left->ParentBracket = Contexts.back().ContextKind;
641 if (Contexts.back().CaretFound)
642 Left->Type = TT_ObjCBlockLBrace;
643 Contexts.back().CaretFound =
false;
645 ScopedContextCreator ContextCreator(*
this, tok::l_brace, 1);
646 Contexts.back().ColonIsDictLiteral =
true;
648 Contexts.back().IsExpression =
true;
649 if (Style.Language == FormatStyle::LK_JavaScript && Left->Previous &&
650 Left->Previous->is(TT_JsTypeColon))
651 Contexts.back().IsExpression =
false;
653 while (CurrentToken) {
654 if (CurrentToken->is(tok::r_brace)) {
655 Left->MatchingParen = CurrentToken;
656 CurrentToken->MatchingParen = Left;
660 if (CurrentToken->isOneOf(tok::r_paren, tok::r_square))
662 updateParameterCount(Left, CurrentToken);
663 if (CurrentToken->isOneOf(tok::colon, tok::l_brace, tok::less)) {
664 FormatToken *
Previous = CurrentToken->getPreviousNonComment();
665 if (Previous->is(TT_JsTypeOptionalQuestion))
666 Previous = Previous->getPreviousNonComment();
667 if ((CurrentToken->is(tok::colon) &&
668 (!Contexts.back().ColonIsDictLiteral || !Style.isCpp())) ||
669 Style.Language == FormatStyle::LK_Proto ||
670 Style.Language == FormatStyle::LK_TextProto) {
671 Left->Type = TT_DictLiteral;
672 if (Previous->Tok.getIdentifierInfo() ||
673 Previous->is(tok::string_literal))
674 Previous->Type = TT_SelectorName;
676 if (CurrentToken->is(tok::colon) ||
677 Style.Language == FormatStyle::LK_JavaScript)
678 Left->Type = TT_DictLiteral;
680 if (CurrentToken->is(tok::comma) &&
681 Style.Language == FormatStyle::LK_JavaScript)
682 Left->Type = TT_DictLiteral;
690 void updateParameterCount(FormatToken *Left, FormatToken *Current) {
694 if (Current->is(tok::l_brace) && Current->BlockKind ==
BK_Block)
695 ++Left->BlockParameterCount;
696 if (Current->is(tok::comma)) {
697 ++Left->ParameterCount;
699 Left->Role.reset(
new CommaSeparatedList(Style));
700 Left->Role->CommaFound(Current);
701 }
else if (Left->ParameterCount == 0 && Current->isNot(tok::comment)) {
702 Left->ParameterCount = 1;
706 bool parseConditional() {
707 while (CurrentToken) {
708 if (CurrentToken->is(tok::colon)) {
709 CurrentToken->Type = TT_ConditionalExpr;
719 bool parseTemplateDeclaration() {
720 if (CurrentToken && CurrentToken->is(tok::less)) {
721 CurrentToken->Type = TT_TemplateOpener;
726 CurrentToken->Previous->ClosesTemplateDeclaration =
true;
732 bool consumeToken() {
733 FormatToken *Tok = CurrentToken;
735 switch (Tok->Tok.getKind()) {
738 if (!Tok->Previous && Line.MustBeDeclaration)
739 Tok->Type = TT_ObjCMethodSpecifier;
745 if (Style.Language == FormatStyle::LK_JavaScript) {
746 if (Contexts.back().ColonIsForRangeExpr ||
747 (Contexts.size() == 1 &&
748 !Line.First->isOneOf(tok::kw_enum, tok::kw_case)) ||
749 Contexts.back().ContextKind == tok::l_paren ||
750 Contexts.back().ContextKind == tok::l_square ||
751 (!Contexts.back().IsExpression &&
752 Contexts.back().ContextKind == tok::l_brace) ||
753 (Contexts.size() == 1 &&
754 Line.MustBeDeclaration)) {
755 Contexts.back().IsExpression =
false;
756 Tok->Type = TT_JsTypeColon;
760 if (Contexts.back().ColonIsDictLiteral ||
761 Style.Language == FormatStyle::LK_Proto ||
762 Style.Language == FormatStyle::LK_TextProto) {
763 Tok->Type = TT_DictLiteral;
764 if (Style.Language == FormatStyle::LK_TextProto) {
765 if (FormatToken *
Previous = Tok->getPreviousNonComment())
768 }
else if (Contexts.back().ColonIsObjCMethodExpr ||
769 Line.startsWith(TT_ObjCMethodSpecifier)) {
770 Tok->Type = TT_ObjCMethodExpr;
771 const FormatToken *BeforePrevious = Tok->Previous->Previous;
774 bool UnknownIdentifierInMethodDeclaration =
775 Line.startsWith(TT_ObjCMethodSpecifier) &&
776 Tok->Previous->is(tok::identifier) && Tok->Previous->is(TT_Unknown);
777 if (!BeforePrevious ||
779 !(BeforePrevious->is(TT_CastRParen) ||
780 (BeforePrevious->is(TT_ObjCMethodExpr) &&
781 BeforePrevious->is(tok::colon))) ||
782 BeforePrevious->is(tok::r_square) ||
783 Contexts.back().LongestObjCSelectorName == 0 ||
784 UnknownIdentifierInMethodDeclaration) {
785 Tok->Previous->Type = TT_SelectorName;
786 if (!Contexts.back().FirstObjCSelectorName)
787 Contexts.back().FirstObjCSelectorName = Tok->Previous;
788 else if (Tok->Previous->ColumnWidth >
789 Contexts.back().LongestObjCSelectorName)
790 Contexts.back().LongestObjCSelectorName =
791 Tok->Previous->ColumnWidth;
792 Tok->Previous->ParameterIndex =
793 Contexts.back().FirstObjCSelectorName->ObjCSelectorNameParts;
794 ++Contexts.back().FirstObjCSelectorName->ObjCSelectorNameParts;
796 }
else if (Contexts.back().ColonIsForRangeExpr) {
797 Tok->Type = TT_RangeBasedForLoopColon;
798 }
else if (CurrentToken && CurrentToken->is(tok::numeric_constant)) {
799 Tok->Type = TT_BitFieldColon;
800 }
else if (Contexts.size() == 1 &&
801 !Line.First->isOneOf(tok::kw_enum, tok::kw_case)) {
802 if (Tok->getPreviousNonComment()->isOneOf(tok::r_paren,
804 Tok->Type = TT_CtorInitializerColon;
806 Tok->Type = TT_InheritanceColon;
807 }
else if (canBeObjCSelectorComponent(*Tok->Previous) && Tok->Next &&
808 (Tok->Next->isOneOf(tok::r_paren, tok::comma) ||
809 (canBeObjCSelectorComponent(*Tok->Next) && Tok->Next->Next &&
810 Tok->Next->Next->is(tok::colon)))) {
813 Tok->Type = TT_ObjCMethodExpr;
814 }
else if (Contexts.back().ContextKind == tok::l_paren) {
815 Tok->Type = TT_InlineASMColon;
822 if (Style.Language == FormatStyle::LK_JavaScript &&
823 !Contexts.back().IsExpression)
824 Tok->Type = TT_JsTypeOperator;
828 if (Tok->is(tok::kw_if) && CurrentToken &&
829 CurrentToken->is(tok::kw_constexpr))
831 if (CurrentToken && CurrentToken->is(tok::l_paren)) {
833 if (!parseParens(
true))
838 if (Style.Language == FormatStyle::LK_JavaScript) {
840 if ((Tok->Previous && Tok->Previous->is(tok::period)) ||
841 (Tok->Next && Tok->Next->is(tok::colon)))
844 if (CurrentToken && CurrentToken->is(Keywords.kw_await))
847 Contexts.back().ColonIsForRangeExpr =
true;
857 if (Tok->Previous && Tok->Previous->is(tok::r_paren) &&
858 Tok->Previous->MatchingParen &&
859 Tok->Previous->MatchingParen->is(TT_OverloadedOperatorLParen)) {
860 Tok->Previous->Type = TT_OverloadedOperator;
861 Tok->Previous->MatchingParen->Type = TT_OverloadedOperator;
862 Tok->Type = TT_OverloadedOperatorLParen;
867 if (Line.MustBeDeclaration && Contexts.size() == 1 &&
868 !Contexts.back().IsExpression && !Line.startsWith(TT_ObjCProperty) &&
870 !Tok->Previous->isOneOf(tok::kw_decltype, tok::kw___attribute,
871 TT_LeadingJavaAnnotation)))
872 Line.MightBeFunctionDecl =
true;
879 if (Style.Language == FormatStyle::LK_TextProto) {
880 FormatToken *
Previous = Tok->getPreviousNonComment();
881 if (Previous && Previous->Type != TT_DictLiteral)
882 Previous->Type = TT_SelectorName;
889 Tok->Type = TT_TemplateOpener;
895 if (Style.Language == FormatStyle::LK_TextProto ||
896 (Style.Language == FormatStyle::LK_Proto && Tok->Previous &&
897 Tok->Previous->isOneOf(TT_SelectorName, TT_DictLiteral))) {
898 Tok->Type = TT_DictLiteral;
899 FormatToken *
Previous = Tok->getPreviousNonComment();
900 if (Previous && Previous->Type != TT_DictLiteral)
901 Previous->Type = TT_SelectorName;
904 Tok->Type = TT_BinaryOperator;
905 NonTemplateLess.insert(Tok);
919 if (Style.Language != FormatStyle::LK_TextProto)
920 Tok->Type = TT_BinaryOperator;
922 case tok::kw_operator:
923 if (Style.Language == FormatStyle::LK_TextProto ||
924 Style.Language == FormatStyle::LK_Proto)
926 while (CurrentToken &&
927 !CurrentToken->isOneOf(tok::l_paren, tok::semi, tok::r_paren)) {
928 if (CurrentToken->isOneOf(tok::star, tok::amp))
929 CurrentToken->Type = TT_PointerOrReference;
932 CurrentToken->Previous->isOneOf(TT_BinaryOperator, TT_UnaryOperator,
934 CurrentToken->Previous->Type = TT_OverloadedOperator;
937 CurrentToken->Type = TT_OverloadedOperatorLParen;
938 if (CurrentToken->Previous->is(TT_BinaryOperator))
939 CurrentToken->Previous->Type = TT_OverloadedOperator;
943 if (Style.Language == FormatStyle::LK_JavaScript && Tok->Next &&
944 Tok->Next->isOneOf(tok::semi, tok::comma, tok::colon, tok::r_paren,
950 Tok->Type = TT_JsTypeOptionalQuestion;
955 if (Line.MustBeDeclaration && !Contexts.back().IsExpression &&
956 Style.Language == FormatStyle::LK_JavaScript)
960 case tok::kw_template:
961 parseTemplateDeclaration();
964 if (Contexts.back().InCtorInitializer)
965 Tok->Type = TT_CtorInitializerComma;
966 else if (Contexts.back().InInheritanceList)
967 Tok->Type = TT_InheritanceComma;
968 else if (Contexts.back().FirstStartOfName &&
969 (Contexts.size() == 1 || Line.startsWith(tok::kw_for))) {
970 Contexts.back().FirstStartOfName->PartOfMultiVariableDeclStmt =
true;
971 Line.IsMultiVariableDeclStmt =
true;
973 if (Contexts.back().IsForEachMacro)
974 Contexts.back().IsExpression =
true;
976 case tok::identifier:
977 if (Tok->isOneOf(Keywords.kw___has_include,
978 Keywords.kw___has_include_next)) {
988 void parseIncludeDirective() {
989 if (CurrentToken && CurrentToken->is(tok::less)) {
991 while (CurrentToken) {
994 if (CurrentToken->isNot(tok::comment) &&
995 !CurrentToken->TokenText.startswith(
"//"))
996 CurrentToken->Type = TT_ImplicitStringLiteral;
1002 void parseWarningOrError() {
1007 while (CurrentToken) {
1008 CurrentToken->Type = TT_ImplicitStringLiteral;
1013 void parsePragma() {
1016 CurrentToken->isOneOf(Keywords.kw_mark, Keywords.kw_option)) {
1017 bool IsMark = CurrentToken->is(Keywords.kw_mark);
1020 while (CurrentToken) {
1021 if (IsMark || CurrentToken->Previous->is(TT_BinaryOperator))
1022 CurrentToken->Type = TT_ImplicitStringLiteral;
1028 void parseHasInclude() {
1029 if (!CurrentToken || !CurrentToken->is(tok::l_paren))
1032 parseIncludeDirective();
1036 LineType parsePreprocessorDirective() {
1037 bool IsFirstToken = CurrentToken->IsFirst;
1043 if (Style.Language == FormatStyle::LK_JavaScript && IsFirstToken) {
1047 while (CurrentToken) {
1049 CurrentToken->Type = TT_ImplicitStringLiteral;
1055 if (CurrentToken->Tok.is(tok::numeric_constant)) {
1056 CurrentToken->SpacesRequiredBefore = 1;
1061 if (!CurrentToken->Tok.getIdentifierInfo())
1063 switch (CurrentToken->Tok.getIdentifierInfo()->getPPKeywordID()) {
1068 parseIncludeDirective();
1072 case tok::pp_warning:
1073 parseWarningOrError();
1075 case tok::pp_pragma:
1080 Contexts.back().IsExpression =
true;
1086 while (CurrentToken) {
1087 FormatToken *Tok = CurrentToken;
1089 if (Tok->is(tok::l_paren))
1091 else if (Tok->isOneOf(Keywords.kw___has_include,
1092 Keywords.kw___has_include_next))
1100 NonTemplateLess.clear();
1101 if (CurrentToken->is(tok::hash))
1102 return parsePreprocessorDirective();
1107 IdentifierInfo *Info = CurrentToken->Tok.getIdentifierInfo();
1108 if ((Style.Language == FormatStyle::LK_Java &&
1109 CurrentToken->is(Keywords.kw_package)) ||
1111 CurrentToken->Next &&
1112 CurrentToken->Next->isOneOf(tok::string_literal, tok::identifier,
1115 parseIncludeDirective();
1121 if (CurrentToken->is(tok::less) && Line.Last->is(tok::greater)) {
1122 parseIncludeDirective();
1128 if (Style.Language == FormatStyle::LK_Proto && Line.Level == 0 &&
1129 CurrentToken->isOneOf(Keywords.kw_option, Keywords.kw_package)) {
1131 if (CurrentToken && CurrentToken->is(tok::identifier)) {
1132 while (CurrentToken)
1138 bool KeywordVirtualFound =
false;
1139 bool ImportStatement =
false;
1142 if (Style.Language == FormatStyle::LK_JavaScript &&
1143 CurrentToken->is(Keywords.kw_import))
1144 ImportStatement =
true;
1146 while (CurrentToken) {
1147 if (CurrentToken->is(tok::kw_virtual))
1148 KeywordVirtualFound =
true;
1149 if (Style.Language == FormatStyle::LK_JavaScript) {
1156 if (Line.First->is(tok::kw_export) &&
1157 CurrentToken->is(Keywords.kw_from) && CurrentToken->Next &&
1158 CurrentToken->Next->isStringLiteral())
1159 ImportStatement =
true;
1160 if (isClosureImportStatement(*CurrentToken))
1161 ImportStatement =
true;
1163 if (!consumeToken())
1166 if (KeywordVirtualFound)
1168 if (ImportStatement)
1171 if (Line.startsWith(TT_ObjCMethodSpecifier)) {
1172 if (Contexts.back().FirstObjCSelectorName)
1173 Contexts.back().FirstObjCSelectorName->LongestObjCSelectorName =
1174 Contexts.back().LongestObjCSelectorName;
1182 bool isClosureImportStatement(
const FormatToken &Tok) {
1185 return Tok.TokenText ==
"goog" && Tok.Next && Tok.Next->is(tok::period) &&
1187 (Tok.Next->Next->TokenText ==
"module" ||
1188 Tok.Next->Next->TokenText ==
"provide" ||
1189 Tok.Next->Next->TokenText ==
"require" ||
1190 Tok.Next->Next->TokenText ==
"requireType" ||
1191 Tok.Next->Next->TokenText ==
"forwardDeclare") &&
1192 Tok.Next->Next->Next && Tok.Next->Next->Next->is(tok::l_paren);
1195 void resetTokenMetadata(FormatToken *Token) {
1201 if (!CurrentToken->isOneOf(
1202 TT_LambdaLSquare, TT_LambdaLBrace, TT_ForEachMacro,
1203 TT_TypenameMacro, TT_FunctionLBrace, TT_ImplicitStringLiteral,
1204 TT_InlineASMBrace, TT_JsFatArrow, TT_LambdaArrow, TT_NamespaceMacro,
1205 TT_OverloadedOperator, TT_RegexLiteral, TT_TemplateString,
1206 TT_ObjCStringLiteral))
1207 CurrentToken->Type = TT_Unknown;
1208 CurrentToken->Role.reset();
1209 CurrentToken->MatchingParen =
nullptr;
1210 CurrentToken->FakeLParens.clear();
1211 CurrentToken->FakeRParens = 0;
1216 CurrentToken->NestingLevel = Contexts.size() - 1;
1217 CurrentToken->BindingStrength = Contexts.back().BindingStrength;
1218 modifyContext(*CurrentToken);
1219 determineTokenType(*CurrentToken);
1220 CurrentToken = CurrentToken->Next;
1223 resetTokenMetadata(CurrentToken);
1255 struct ScopedContextCreator {
1256 AnnotatingParser &
P;
1261 P.Contexts.push_back(Context(ContextKind,
1262 P.Contexts.back().BindingStrength + Increase,
1263 P.Contexts.back().IsExpression));
1266 ~ScopedContextCreator() { P.Contexts.pop_back(); }
1269 void modifyContext(
const FormatToken &Current) {
1271 !Line.First->isOneOf(tok::kw_template, tok::kw_using, tok::kw_return) &&
1274 !(Style.Language == FormatStyle::LK_JavaScript &&
1275 (Line.startsWith(Keywords.kw_type, tok::identifier) ||
1276 Line.startsWith(tok::kw_export, Keywords.kw_type,
1277 tok::identifier))) &&
1278 (!Current.Previous || Current.Previous->isNot(tok::kw_operator))) {
1279 Contexts.back().IsExpression =
true;
1280 if (!Line.startsWith(TT_UnaryOperator)) {
1281 for (FormatToken *
Previous = Current.Previous;
1283 !
Previous->Previous->isOneOf(tok::comma, tok::semi);
1285 if (
Previous->isOneOf(tok::r_square, tok::r_paren)) {
1292 if (
Previous->isOneOf(TT_BinaryOperator, TT_UnaryOperator) &&
1293 Previous->isOneOf(tok::star, tok::amp, tok::ampamp) &&
1295 Previous->Type = TT_PointerOrReference;
1298 }
else if (Current.is(tok::lessless) &&
1299 (!Current.Previous || !Current.Previous->is(tok::kw_operator))) {
1300 Contexts.back().IsExpression =
true;
1301 }
else if (Current.isOneOf(tok::kw_return, tok::kw_throw)) {
1302 Contexts.back().IsExpression =
true;
1303 }
else if (Current.is(TT_TrailingReturnArrow)) {
1304 Contexts.back().IsExpression =
false;
1305 }
else if (Current.is(TT_LambdaArrow) || Current.is(Keywords.kw_assert)) {
1306 Contexts.back().IsExpression = Style.Language == FormatStyle::LK_Java;
1307 }
else if (Current.Previous &&
1308 Current.Previous->is(TT_CtorInitializerColon)) {
1309 Contexts.back().IsExpression =
true;
1310 Contexts.back().InCtorInitializer =
true;
1311 }
else if (Current.Previous && Current.Previous->is(TT_InheritanceColon)) {
1312 Contexts.back().InInheritanceList =
true;
1313 }
else if (Current.isOneOf(tok::r_paren, tok::greater, tok::comma)) {
1314 for (FormatToken *
Previous = Current.Previous;
1317 Previous->Type = TT_PointerOrReference;
1318 if (Line.MustBeDeclaration && !Contexts.front().InCtorInitializer)
1319 Contexts.back().IsExpression =
false;
1320 }
else if (Current.is(tok::kw_new)) {
1321 Contexts.back().CanBeExpression =
false;
1322 }
else if (Current.isOneOf(tok::semi, tok::exclaim)) {
1324 Contexts.back().IsExpression =
true;
1328 void determineTokenType(FormatToken &Current) {
1329 if (!Current.is(TT_Unknown))
1333 if (Style.Language == FormatStyle::LK_JavaScript) {
1334 if (Current.is(tok::exclaim)) {
1335 if (Current.Previous &&
1336 (Current.Previous->isOneOf(tok::identifier, tok::kw_namespace,
1337 tok::r_paren, tok::r_square,
1339 Current.Previous->Tok.isLiteral())) {
1340 Current.Type = TT_JsNonNullAssertion;
1344 Current.Next->isOneOf(TT_BinaryOperator, Keywords.kw_as)) {
1345 Current.Type = TT_JsNonNullAssertion;
1354 if (Current.is(Keywords.kw_instanceof)) {
1355 Current.Type = TT_BinaryOperator;
1356 }
else if (isStartOfName(Current) &&
1357 (!Line.MightBeFunctionDecl || Current.NestingLevel != 0)) {
1358 Contexts.back().FirstStartOfName = &Current;
1359 Current.Type = TT_StartOfName;
1360 }
else if (Current.is(tok::semi)) {
1364 Contexts.back().FirstStartOfName =
nullptr;
1365 }
else if (Current.isOneOf(tok::kw_auto, tok::kw___auto_type)) {
1367 }
else if (Current.is(tok::arrow) &&
1368 Style.Language == FormatStyle::LK_Java) {
1369 Current.Type = TT_LambdaArrow;
1370 }
else if (Current.is(tok::arrow) && AutoFound && Line.MustBeDeclaration &&
1371 Current.NestingLevel == 0) {
1372 Current.Type = TT_TrailingReturnArrow;
1373 }
else if (Current.isOneOf(tok::star, tok::amp, tok::ampamp)) {
1374 Current.Type = determineStarAmpUsage(Current,
1375 Contexts.back().CanBeExpression &&
1376 Contexts.back().IsExpression,
1377 Contexts.back().InTemplateArgument);
1378 }
else if (Current.isOneOf(tok::minus, tok::plus, tok::caret)) {
1379 Current.Type = determinePlusMinusCaretUsage(Current);
1380 if (Current.is(TT_UnaryOperator) && Current.is(tok::caret))
1381 Contexts.back().CaretFound =
true;
1382 }
else if (Current.isOneOf(tok::minusminus, tok::plusplus)) {
1383 Current.Type = determineIncrementUsage(Current);
1384 }
else if (Current.isOneOf(tok::exclaim, tok::tilde)) {
1385 Current.Type = TT_UnaryOperator;
1386 }
else if (Current.is(tok::question)) {
1387 if (Style.Language == FormatStyle::LK_JavaScript &&
1388 Line.MustBeDeclaration && !Contexts.back().IsExpression) {
1391 Current.Type = TT_JsTypeOptionalQuestion;
1393 Current.Type = TT_ConditionalExpr;
1395 }
else if (Current.isBinaryOperator() &&
1396 (!Current.Previous || Current.Previous->isNot(tok::l_square)) &&
1397 (!Current.is(tok::greater) &&
1398 Style.Language != FormatStyle::LK_TextProto)) {
1399 Current.Type = TT_BinaryOperator;
1400 }
else if (Current.is(tok::comment)) {
1401 if (Current.TokenText.startswith(
"/*")) {
1402 if (Current.TokenText.endswith(
"*/"))
1403 Current.Type = TT_BlockComment;
1407 Current.Tok.setKind(tok::unknown);
1409 Current.Type = TT_LineComment;
1411 }
else if (Current.is(tok::r_paren)) {
1412 if (rParenEndsCast(Current))
1413 Current.Type = TT_CastRParen;
1414 if (Current.MatchingParen && Current.Next &&
1415 !Current.Next->isBinaryOperator() &&
1416 !Current.Next->isOneOf(tok::semi, tok::colon, tok::l_brace,
1417 tok::comma, tok::period, tok::arrow,
1419 if (FormatToken *AfterParen = Current.MatchingParen->Next) {
1421 if (AfterParen->Tok.isNot(tok::caret)) {
1422 if (FormatToken *BeforeParen = Current.MatchingParen->Previous)
1423 if (BeforeParen->is(tok::identifier) &&
1424 !BeforeParen->is(TT_TypenameMacro) &&
1425 BeforeParen->TokenText == BeforeParen->TokenText.upper() &&
1426 (!BeforeParen->Previous ||
1427 BeforeParen->Previous->ClosesTemplateDeclaration))
1428 Current.Type = TT_FunctionAnnotationRParen;
1431 }
else if (Current.is(tok::at) && Current.Next &&
1432 Style.Language != FormatStyle::LK_JavaScript &&
1433 Style.Language != FormatStyle::LK_Java) {
1436 switch (Current.Next->Tok.getObjCKeywordID()) {
1437 case tok::objc_interface:
1438 case tok::objc_implementation:
1439 case tok::objc_protocol:
1440 Current.Type = TT_ObjCDecl;
1442 case tok::objc_property:
1443 Current.Type = TT_ObjCProperty;
1448 }
else if (Current.is(tok::period)) {
1449 FormatToken *PreviousNoComment = Current.getPreviousNonComment();
1450 if (PreviousNoComment &&
1451 PreviousNoComment->isOneOf(tok::comma, tok::l_brace))
1452 Current.Type = TT_DesignatedInitializerPeriod;
1453 else if (Style.Language == FormatStyle::LK_Java && Current.Previous &&
1454 Current.Previous->isOneOf(TT_JavaAnnotation,
1455 TT_LeadingJavaAnnotation)) {
1456 Current.Type = Current.Previous->Type;
1458 }
else if (canBeObjCSelectorComponent(Current) &&
1461 Current.Previous && Current.Previous->is(TT_CastRParen) &&
1462 Current.Previous->MatchingParen &&
1463 Current.Previous->MatchingParen->Previous &&
1464 Current.Previous->MatchingParen->Previous->is(
1465 TT_ObjCMethodSpecifier)) {
1469 Current.Type = TT_SelectorName;
1470 }
else if (Current.isOneOf(tok::identifier, tok::kw_const) &&
1472 !Current.Previous->isOneOf(tok::equal, tok::at) &&
1473 Line.MightBeFunctionDecl && Contexts.size() == 1) {
1476 Current.Type = TT_TrailingAnnotation;
1477 }
else if ((Style.Language == FormatStyle::LK_Java ||
1478 Style.Language == FormatStyle::LK_JavaScript) &&
1480 if (Current.Previous->is(tok::at) &&
1481 Current.isNot(Keywords.kw_interface)) {
1482 const FormatToken &AtToken = *Current.Previous;
1483 const FormatToken *
Previous = AtToken.getPreviousNonComment();
1484 if (!Previous || Previous->is(TT_LeadingJavaAnnotation))
1485 Current.Type = TT_LeadingJavaAnnotation;
1487 Current.Type = TT_JavaAnnotation;
1488 }
else if (Current.Previous->is(tok::period) &&
1489 Current.Previous->isOneOf(TT_JavaAnnotation,
1490 TT_LeadingJavaAnnotation)) {
1491 Current.Type = Current.Previous->Type;
1501 bool isStartOfName(
const FormatToken &Tok) {
1502 if (Tok.isNot(tok::identifier) || !Tok.Previous)
1505 if (Tok.Previous->isOneOf(TT_LeadingJavaAnnotation, Keywords.kw_instanceof,
1508 if (Style.Language == FormatStyle::LK_JavaScript &&
1509 Tok.Previous->is(Keywords.kw_in))
1513 FormatToken *PreviousNotConst = Tok.getPreviousNonComment();
1514 while (PreviousNotConst && PreviousNotConst->is(tok::kw_const))
1515 PreviousNotConst = PreviousNotConst->getPreviousNonComment();
1517 if (!PreviousNotConst)
1520 bool IsPPKeyword = PreviousNotConst->is(tok::identifier) &&
1521 PreviousNotConst->Previous &&
1522 PreviousNotConst->Previous->is(tok::hash);
1524 if (PreviousNotConst->is(TT_TemplateCloser))
1525 return PreviousNotConst && PreviousNotConst->MatchingParen &&
1526 PreviousNotConst->MatchingParen->Previous &&
1527 PreviousNotConst->MatchingParen->Previous->isNot(tok::period) &&
1528 PreviousNotConst->MatchingParen->Previous->isNot(tok::kw_template);
1530 if (PreviousNotConst->is(tok::r_paren) && PreviousNotConst->MatchingParen &&
1531 PreviousNotConst->MatchingParen->Previous &&
1532 PreviousNotConst->MatchingParen->Previous->is(tok::kw_decltype))
1535 return (!IsPPKeyword &&
1536 PreviousNotConst->isOneOf(tok::identifier, tok::kw_auto)) ||
1537 PreviousNotConst->is(TT_PointerOrReference) ||
1538 PreviousNotConst->isSimpleTypeSpecifier();
1542 bool rParenEndsCast(
const FormatToken &Tok) {
1544 if (!Style.isCpp() && Style.Language != FormatStyle::LK_Java)
1548 if (Tok.Previous == Tok.MatchingParen || !Tok.Next || !Tok.MatchingParen)
1551 FormatToken *LeftOfParens = Tok.MatchingParen->getPreviousNonComment();
1555 if (LeftOfParens->is(tok::r_paren)) {
1556 if (!LeftOfParens->MatchingParen ||
1557 !LeftOfParens->MatchingParen->Previous)
1559 LeftOfParens = LeftOfParens->MatchingParen->Previous;
1564 if (LeftOfParens->Tok.getIdentifierInfo() &&
1565 !LeftOfParens->isOneOf(Keywords.kw_in, tok::kw_return, tok::kw_case,
1571 if (LeftOfParens->isOneOf(tok::at, tok::r_square, TT_OverloadedOperator,
1572 TT_TemplateCloser, tok::ellipsis))
1576 if (Tok.Next->is(tok::question))
1581 if (Style.Language == FormatStyle::LK_Java && Tok.Next->is(tok::l_paren))
1585 if (Tok.Next->isNot(tok::string_literal) &&
1586 (Tok.Next->Tok.isLiteral() ||
1587 Tok.Next->isOneOf(tok::kw_sizeof, tok::kw_alignof)))
1591 bool ParensAreType =
1593 Tok.Previous->isOneOf(TT_PointerOrReference, TT_TemplateCloser) ||
1594 Tok.Previous->isSimpleTypeSpecifier();
1595 bool ParensCouldEndDecl =
1596 Tok.Next->isOneOf(tok::equal, tok::semi, tok::l_brace, tok::greater);
1597 if (ParensAreType && !ParensCouldEndDecl)
1608 for (
const FormatToken *Token = Tok.MatchingParen->Next; Token != &Tok;
1609 Token = Token->Next)
1610 if (Token->is(TT_BinaryOperator))
1615 if (Tok.Next->isOneOf(tok::identifier, tok::kw_this))
1618 if (!Tok.Next->Next)
1625 Tok.Next->isUnaryOperator() || Tok.Next->isOneOf(tok::amp, tok::star);
1626 if (!NextIsUnary || Tok.Next->is(tok::plus) ||
1627 !Tok.Next->Next->isOneOf(tok::identifier, tok::numeric_constant))
1630 for (FormatToken *Prev = Tok.Previous; Prev != Tok.MatchingParen;
1631 Prev = Prev->Previous) {
1632 if (!Prev->isOneOf(tok::kw_const, tok::identifier, tok::coloncolon))
1641 if (Style.Language == FormatStyle::LK_JavaScript)
1642 return TT_BinaryOperator;
1644 const FormatToken *PrevToken = Tok.getPreviousNonComment();
1646 return TT_UnaryOperator;
1648 const FormatToken *NextToken = Tok.getNextNonComment();
1650 NextToken->isOneOf(tok::arrow, tok::equal, tok::kw_const) ||
1651 (NextToken->is(tok::l_brace) && !NextToken->getNextNonComment()))
1652 return TT_PointerOrReference;
1654 if (PrevToken->is(tok::coloncolon))
1655 return TT_PointerOrReference;
1657 if (PrevToken->isOneOf(tok::l_paren, tok::l_square, tok::l_brace,
1658 tok::comma, tok::semi, tok::kw_return, tok::colon,
1659 tok::equal, tok::kw_delete, tok::kw_sizeof,
1661 PrevToken->isOneOf(TT_BinaryOperator, TT_ConditionalExpr,
1662 TT_UnaryOperator, TT_CastRParen))
1663 return TT_UnaryOperator;
1665 if (NextToken->is(tok::l_square) && NextToken->isNot(TT_LambdaLSquare))
1666 return TT_PointerOrReference;
1668 return TT_PointerOrReference;
1669 if (NextToken->isOneOf(tok::comma, tok::semi))
1670 return TT_PointerOrReference;
1672 if (PrevToken->is(tok::r_paren) && PrevToken->MatchingParen) {
1673 FormatToken *TokenBeforeMatchingParen =
1674 PrevToken->MatchingParen->getPreviousNonComment();
1675 if (TokenBeforeMatchingParen &&
1676 TokenBeforeMatchingParen->isOneOf(tok::kw_typeof, tok::kw_decltype,
1678 return TT_PointerOrReference;
1681 if (PrevToken->Tok.isLiteral() ||
1682 PrevToken->isOneOf(tok::r_paren, tok::r_square, tok::kw_true,
1683 tok::kw_false, tok::r_brace) ||
1684 NextToken->Tok.isLiteral() ||
1685 NextToken->isOneOf(tok::kw_true, tok::kw_false) ||
1686 NextToken->isUnaryOperator() ||
1690 (InTemplateArgument && NextToken->Tok.isAnyIdentifier()))
1691 return TT_BinaryOperator;
1694 if (Tok.is(tok::ampamp) && NextToken && NextToken->is(tok::l_paren))
1695 return TT_BinaryOperator;
1699 const FormatToken *NextNextToken = NextToken->getNextNonComment();
1700 if (NextNextToken && NextNextToken->is(tok::arrow))
1701 return TT_BinaryOperator;
1705 if (IsExpression && !Contexts.back().CaretFound)
1706 return TT_BinaryOperator;
1708 return TT_PointerOrReference;
1711 TokenType determinePlusMinusCaretUsage(
const FormatToken &Tok) {
1712 const FormatToken *PrevToken = Tok.getPreviousNonComment();
1714 return TT_UnaryOperator;
1716 if (PrevToken->isOneOf(TT_CastRParen, TT_UnaryOperator))
1718 return TT_UnaryOperator;
1721 if (PrevToken->isOneOf(tok::equal, tok::l_paren, tok::comma, tok::l_square,
1722 tok::question, tok::colon, tok::kw_return,
1723 tok::kw_case, tok::at, tok::l_brace))
1724 return TT_UnaryOperator;
1727 if (PrevToken->is(TT_BinaryOperator))
1728 return TT_UnaryOperator;
1731 return TT_BinaryOperator;
1735 TokenType determineIncrementUsage(
const FormatToken &Tok) {
1736 const FormatToken *PrevToken = Tok.getPreviousNonComment();
1737 if (!PrevToken || PrevToken->is(TT_CastRParen))
1738 return TT_UnaryOperator;
1739 if (PrevToken->isOneOf(tok::r_paren, tok::r_square, tok::identifier))
1740 return TT_TrailingUnaryOperator;
1742 return TT_UnaryOperator;
1745 SmallVector<Context, 8> Contexts;
1747 const FormatStyle &
Style;
1748 AnnotatedLine &
Line;
1749 FormatToken *CurrentToken;
1751 const AdditionalKeywords &Keywords;
1757 llvm::SmallPtrSet<FormatToken *, 16> NonTemplateLess;
1765 class ExpressionParser {
1767 ExpressionParser(
const FormatStyle &
Style,
const AdditionalKeywords &Keywords,
1768 AnnotatedLine &
Line)
1769 :
Style(Style), Keywords(Keywords), Current(Line.First) {}
1772 void parse(
int Precedence = 0) {
1775 while (Current && (Current->is(tok::kw_return) ||
1776 (Current->is(tok::colon) &&
1777 Current->isOneOf(TT_ObjCMethodExpr, TT_DictLiteral))))
1780 if (!Current || Precedence > PrecedenceArrowAndPeriod)
1785 parseConditionalExpr();
1791 if (Precedence == PrecedenceUnaryOperator) {
1792 parseUnaryOperator();
1796 FormatToken *Start = Current;
1797 FormatToken *LatestOperator =
nullptr;
1798 unsigned OperatorIndex = 0;
1802 parse(Precedence + 1);
1804 int CurrentPrecedence = getCurrentPrecedence();
1806 if (Current && Current->is(TT_SelectorName) &&
1807 Precedence == CurrentPrecedence) {
1809 addFakeParenthesis(Start,
prec::Level(Precedence));
1816 (Current->closesScope() &&
1817 (Current->MatchingParen || Current->is(TT_TemplateString))) ||
1818 (CurrentPrecedence != -1 && CurrentPrecedence < Precedence) ||
1825 if (Current->opensScope()) {
1828 while (Current && (!Current->closesScope() || Current->opensScope())) {
1835 if (CurrentPrecedence == Precedence) {
1837 LatestOperator->NextOperator = Current;
1838 LatestOperator = Current;
1839 Current->OperatorIndex = OperatorIndex;
1842 next(Precedence > 0);
1846 if (LatestOperator && (Current || Precedence > 0)) {
1848 if (Precedence == PrecedenceArrowAndPeriod) {
1852 addFakeParenthesis(Start,
prec::Level(Precedence));
1860 int getCurrentPrecedence() {
1862 const FormatToken *NextNonComment = Current->getNextNonComment();
1863 if (Current->is(TT_ConditionalExpr))
1865 if (NextNonComment && Current->is(TT_SelectorName) &&
1866 (NextNonComment->isOneOf(TT_DictLiteral, TT_JsTypeColon) ||
1867 ((Style.Language == FormatStyle::LK_Proto ||
1868 Style.Language == FormatStyle::LK_TextProto) &&
1869 NextNonComment->is(tok::less))))
1871 if (Current->is(TT_JsComputedPropertyName))
1873 if (Current->is(TT_LambdaArrow))
1875 if (Current->is(TT_JsFatArrow))
1877 if (Current->isOneOf(tok::semi, TT_InlineASMColon, TT_SelectorName) ||
1878 (Current->is(tok::comment) && NextNonComment &&
1879 NextNonComment->is(TT_SelectorName)))
1881 if (Current->is(TT_RangeBasedForLoopColon))
1883 if ((Style.Language == FormatStyle::LK_Java ||
1884 Style.Language == FormatStyle::LK_JavaScript) &&
1885 Current->is(Keywords.kw_instanceof))
1887 if (Style.Language == FormatStyle::LK_JavaScript &&
1888 Current->isOneOf(Keywords.kw_in, Keywords.kw_as))
1890 if (Current->is(TT_BinaryOperator) || Current->is(tok::comma))
1891 return Current->getPrecedence();
1892 if (Current->isOneOf(tok::period, tok::arrow))
1893 return PrecedenceArrowAndPeriod;
1894 if ((Style.Language == FormatStyle::LK_Java ||
1895 Style.Language == FormatStyle::LK_JavaScript) &&
1896 Current->isOneOf(Keywords.kw_extends, Keywords.kw_implements,
1897 Keywords.kw_throws))
1903 void addFakeParenthesis(FormatToken *Start,
prec::Level Precedence) {
1904 Start->FakeLParens.push_back(Precedence);
1906 Start->StartsBinaryExpression =
true;
1908 FormatToken *
Previous = Current->Previous;
1909 while (Previous->is(tok::comment) && Previous->Previous)
1910 Previous = Previous->Previous;
1911 ++Previous->FakeRParens;
1913 Previous->EndsBinaryExpression =
true;
1919 void parseUnaryOperator() {
1921 while (Current && Current->is(TT_UnaryOperator)) {
1922 Tokens.push_back(Current);
1925 parse(PrecedenceArrowAndPeriod);
1926 for (FormatToken *Token : llvm::reverse(Tokens))
1931 void parseConditionalExpr() {
1932 while (Current && Current->isTrailingComment()) {
1935 FormatToken *Start = Current;
1937 if (!Current || !Current->is(tok::question))
1941 if (!Current || Current->isNot(TT_ConditionalExpr))
1948 void next(
bool SkipPastLeadingComments =
true) {
1950 Current = Current->Next;
1952 (Current->NewlinesBefore == 0 || SkipPastLeadingComments) &&
1953 Current->isTrailingComment())
1954 Current = Current->Next;
1957 const FormatStyle &
Style;
1958 const AdditionalKeywords &Keywords;
1959 FormatToken *Current;
1970 bool CommentLine =
true;
1972 if (!
Tok->
is(tok::comment)) {
1973 CommentLine =
false;
1980 if (NextNonCommentLine && CommentLine &&
1983 (*I)->First->OriginalColumn) {
1988 (
Style.IndentPPDirectives != FormatStyle::PPDIS_BeforeHash &&
1992 : NextNonCommentLine->
Level;
1994 NextNonCommentLine = (*I)->
First->
isNot(tok::r_brace) ? (*I) :
nullptr;
1997 setCommentLineLevels((*I)->Children);
2002 unsigned Result = 0;
2015 Line.
Type = Parser.parseLine();
2027 ExpressionParser ExprParser(
Style, Keywords, Line);
2046 for (; Next; Next = Next->Next) {
2047 if (Next->is(TT_OverloadedOperatorLParen))
2049 if (Next->is(TT_OverloadedOperator))
2051 if (Next->isOneOf(tok::kw_new, tok::kw_delete)) {
2053 if (Next->Next && Next->Next->is(tok::l_square) && Next->Next->Next &&
2054 Next->Next->Next->is(tok::r_square))
2055 Next = Next->Next->Next;
2066 if (Current.
is(tok::kw_operator)) {
2069 Next = skipOperatorName(Next);
2073 for (; Next; Next = Next->
Next) {
2074 if (Next->
is(TT_TemplateOpener)) {
2076 }
else if (Next->
is(tok::coloncolon)) {
2080 if (Next->
is(tok::kw_operator)) {
2081 Next = skipOperatorName(Next->
Next);
2084 if (!Next->
is(tok::identifier))
2086 }
else if (Next->
is(tok::l_paren)) {
2098 if (Line.
Last->
is(tok::l_brace))
2113 Tok->
isOneOf(TT_PointerOrReference, TT_StartOfName, tok::ellipsis))
2115 if (
Tok->
isOneOf(tok::l_brace, tok::string_literal, TT_ObjCMethodExpr) ||
2125 if ((
Style.AlwaysBreakAfterReturnType == FormatStyle::RTBS_TopLevel ||
2126 Style.AlwaysBreakAfterReturnType ==
2127 FormatStyle::RTBS_TopLevelDefinitions) &&
2131 switch (
Style.AlwaysBreakAfterReturnType) {
2132 case FormatStyle::RTBS_None:
2134 case FormatStyle::RTBS_All:
2135 case FormatStyle::RTBS_TopLevel:
2137 case FormatStyle::RTBS_AllDefinitions:
2138 case FormatStyle::RTBS_TopLevelDefinitions:
2149 calculateFormattingInformation(**I);
2159 Current->
Type = TT_FunctionDeclarationName;
2160 if (Current->
is(TT_LineComment)) {
2177 if (
Parameter->isOneOf(tok::comment, tok::r_brace))
2180 if (!
Parameter->Previous->is(TT_CtorInitializerComma) &&
2188 spaceRequiredBefore(Line, *Current)) {
2196 Current->
is(TT_FunctionDeclarationName))
2201 unsigned ChildSize = 0;
2210 Prev->
Children[0]->First->MustBreakBefore) ||
2217 if (Current->
is(TT_CtorInitializerColon))
2218 InFunctionDecl =
false;
2229 Current->
SplitPenalty = splitPenalty(Line, *Current, InFunctionDecl);
2230 if (
Style.Language == FormatStyle::LK_ObjC &&
2238 Current = Current->
Next;
2241 calculateUnbreakableTailLengths(Line);
2242 unsigned IndentLevel = Line.
Level;
2243 for (Current = Line.
First; Current !=
nullptr; Current = Current->
Next) {
2245 Current->
Role->precomputeFormattingInfos(Current);
2248 assert(IndentLevel > 0);
2259 void TokenAnnotator::calculateUnbreakableTailLengths(
AnnotatedLine &Line) {
2260 unsigned UnbreakableTailLength = 0;
2265 Current->
isOneOf(tok::comment, tok::string_literal)) {
2266 UnbreakableTailLength = 0;
2268 UnbreakableTailLength +=
2275 unsigned TokenAnnotator::splitPenalty(
const AnnotatedLine &Line,
2277 bool InFunctionDecl) {
2281 if (Left.
is(tok::semi))
2284 if (
Style.Language == FormatStyle::LK_Java) {
2285 if (Right.
isOneOf(Keywords.kw_extends, Keywords.kw_throws))
2287 if (Right.
is(Keywords.kw_implements))
2291 }
else if (
Style.Language == FormatStyle::LK_JavaScript) {
2292 if (Right.
is(Keywords.kw_function) && Left.
isNot(tok::comma))
2294 if (Left.
is(TT_JsTypeColon))
2296 if ((Left.
is(TT_TemplateString) && Left.
TokenText.endswith(
"${")) ||
2297 (Right.
is(TT_TemplateString) && Right.
TokenText.startswith(
"}")))
2304 if (Right.
is(tok::identifier) && Right.
Next && Right.
Next->
is(TT_DictLiteral))
2306 if (Right.
is(tok::l_square)) {
2307 if (
Style.Language == FormatStyle::LK_Proto)
2309 if (Left.
is(tok::r_square))
2312 if (Right.
is(TT_LambdaLSquare) && Left.
is(tok::equal))
2314 if (!Right.
isOneOf(TT_ObjCMethodExpr, TT_LambdaLSquare,
2315 TT_ArrayInitializerLSquare,
2316 TT_DesignatedInitializerLSquare, TT_AttributeSquare))
2320 if (Left.
is(tok::coloncolon) ||
2321 (Right.
is(tok::period) &&
Style.Language == FormatStyle::LK_Proto))
2323 if (Right.
isOneOf(TT_StartOfName, TT_FunctionDeclarationName) ||
2324 Right.
is(tok::kw_operator)) {
2327 if (Left.
is(TT_StartOfName))
2330 return Style.PenaltyReturnTypeOnItsOwnLine;
2333 if (Right.
is(TT_PointerOrReference))
2335 if (Right.
is(TT_LambdaArrow))
2337 if (Left.
is(tok::equal) && Right.
is(tok::l_brace))
2339 if (Left.
is(TT_CastRParen))
2341 if (Left.
isOneOf(tok::kw_class, tok::kw_struct))
2343 if (Left.
is(tok::comment))
2346 if (Left.
isOneOf(TT_RangeBasedForLoopColon, TT_InheritanceColon,
2347 TT_CtorInitializerColon))
2375 if (Right.
is(TT_TrailingAnnotation) &&
2386 bool is_short_annotation = Right.
TokenText.size() < 10;
2387 return (Left.
is(tok::r_paren) ? 100 : 120) + (is_short_annotation ? 50 : 0);
2391 if (Line.
startsWith(tok::kw_for) && Left.
is(tok::equal))
2396 if (Right.
is(TT_SelectorName))
2398 if (Left.
is(tok::colon) && Left.
is(TT_ObjCMethodExpr))
2408 if (Left.
is(tok::l_paren) && InFunctionDecl &&
2411 if (Left.
is(tok::l_paren) && Left.
Previous &&
2415 if (Left.
is(tok::equal) && InFunctionDecl)
2417 if (Right.
is(tok::r_brace))
2419 if (Left.
is(TT_TemplateOpener))
2424 if (Left.
is(tok::l_brace) && !
Style.Cpp11BracedListStyle)
2429 if (Left.
is(TT_JavaAnnotation))
2439 if (Left.
is(tok::comma))
2444 if (Right.
is(tok::lessless)) {
2452 return Style.PenaltyBreakTemplateDeclaration;
2453 if (Left.
is(TT_ConditionalExpr))
2459 return Style.PenaltyBreakAssignment;
2466 bool TokenAnnotator::spaceRequiredBeforeParens(
const FormatToken &Right)
const {
2467 return Style.SpaceBeforeParens == FormatStyle::SBPO_Always ||
2468 (
Style.SpaceBeforeParens == FormatStyle::SBPO_NonEmptyParentheses &&
2472 bool TokenAnnotator::spaceRequiredBetween(
const AnnotatedLine &Line,
2475 if (Left.
is(tok::kw_return) && Right.
isNot(tok::semi))
2477 if (Left.
is(Keywords.kw_assert) &&
Style.Language == FormatStyle::LK_Java)
2482 if (Right.
is(tok::hashhash))
2483 return Left.
is(tok::hash);
2484 if (Left.
isOneOf(tok::hashhash, tok::hash))
2485 return Right.
is(tok::hash);
2486 if (Left.
is(tok::l_paren) && Right.
is(tok::r_paren))
2487 return Style.SpaceInEmptyParentheses;
2488 if (Left.
is(tok::l_paren) || Right.
is(tok::r_paren))
2489 return (Right.
is(TT_CastRParen) ||
2491 ?
Style.SpacesInCStyleCastParentheses
2492 :
Style.SpacesInParentheses;
2493 if (Right.
isOneOf(tok::semi, tok::comma))
2499 return !IsLightweightGeneric &&
Style.ObjCSpaceBeforeProtocolList;
2501 if (Right.
is(tok::less) && Left.
is(tok::kw_template))
2502 return Style.SpaceAfterTemplateKeyword;
2503 if (Left.
isOneOf(tok::exclaim, tok::tilde))
2505 if (Left.
is(tok::at) &&
2506 Right.
isOneOf(tok::identifier, tok::string_literal, tok::char_constant,
2507 tok::numeric_constant, tok::l_paren, tok::l_brace,
2508 tok::kw_true, tok::kw_false))
2510 if (Left.
is(tok::colon))
2511 return !Left.
is(TT_ObjCMethodExpr);
2512 if (Left.
is(tok::coloncolon))
2514 if (Left.
is(tok::less) || Right.
isOneOf(tok::greater, tok::less)) {
2515 if (
Style.Language == FormatStyle::LK_TextProto ||
2516 (
Style.Language == FormatStyle::LK_Proto &&
2517 (Left.
is(TT_DictLiteral) || Right.
is(TT_DictLiteral)))) {
2519 if (Left.
is(tok::less) && Right.
is(tok::greater))
2521 return !
Style.Cpp11BracedListStyle;
2525 if (Right.
is(tok::ellipsis))
2528 if (Left.
is(tok::l_square) && Right.
is(tok::amp))
2530 if (Right.
is(TT_PointerOrReference)) {
2536 if (!TokenBeforeMatchingParen ||
2537 !TokenBeforeMatchingParen->
isOneOf(tok::kw_typeof, tok::kw_decltype,
2542 (!Left.
isOneOf(TT_PointerOrReference, tok::l_paren) &&
2543 (
Style.PointerAlignment != FormatStyle::PAS_Left ||
2548 if (Right.
is(TT_FunctionTypeLParen) && Left.
isNot(tok::l_paren) &&
2549 (!Left.
is(TT_PointerOrReference) ||
2550 (
Style.PointerAlignment != FormatStyle::PAS_Right &&
2553 if (Left.
is(TT_PointerOrReference))
2555 (Right.
isOneOf(Keywords.kw_override, Keywords.kw_final) &&
2556 !Right.
is(TT_StartOfName)) ||
2558 (!Right.
isOneOf(TT_PointerOrReference, TT_ArraySubscriptLSquare,
2560 (
Style.PointerAlignment != FormatStyle::PAS_Right &&
2564 if (Right.
is(tok::star) && Left.
is(tok::l_paren))
2566 const auto SpaceRequiredForArrayInitializerLSquare =
2568 return Style.SpacesInContainerLiterals ||
2569 ((
Style.Language == FormatStyle::LK_Proto ||
2570 Style.Language == FormatStyle::LK_TextProto) &&
2571 !
Style.Cpp11BracedListStyle &&
2575 if (Left.
is(tok::l_square))
2576 return (Left.
is(TT_ArrayInitializerLSquare) && Right.
isNot(tok::r_square) &&
2577 SpaceRequiredForArrayInitializerLSquare(Left,
Style)) ||
2578 (Left.
isOneOf(TT_ArraySubscriptLSquare,
2579 TT_StructuredBindingLSquare) &&
2580 Style.SpacesInSquareBrackets && Right.
isNot(tok::r_square));
2581 if (Right.
is(tok::r_square))
2584 SpaceRequiredForArrayInitializerLSquare(*Right.
MatchingParen,
2586 (
Style.SpacesInSquareBrackets &&
2588 TT_StructuredBindingLSquare)) ||
2590 if (Right.
is(tok::l_square) &&
2591 !Right.
isOneOf(TT_ObjCMethodExpr, TT_LambdaLSquare,
2592 TT_DesignatedInitializerLSquare,
2593 TT_StructuredBindingLSquare, TT_AttributeSquare) &&
2594 !Left.
isOneOf(tok::numeric_constant, TT_DictLiteral))
2596 if (Left.
is(tok::l_brace) && Right.
is(tok::r_brace))
2601 return !
Style.Cpp11BracedListStyle;
2602 if (Left.
is(TT_BlockComment))
2604 return Style.Language == FormatStyle::LK_JavaScript ||
2606 if (Right.
is(tok::l_paren)) {
2607 if ((Left.
is(tok::r_paren) && Left.
is(TT_AttributeParen)) ||
2608 (Left.
is(tok::r_square) && Left.
is(TT_AttributeSquare)))
2611 (
Style.SpaceBeforeParens != FormatStyle::SBPO_Never &&
2613 tok::kw_switch, tok::kw_case, TT_ForEachMacro,
2616 (Left.
isOneOf(tok::kw_try, Keywords.kw___except, tok::kw_catch,
2617 tok::kw_new, tok::kw_delete) &&
2619 (spaceRequiredBeforeParens(Right) &&
2628 if (Right.
is(TT_UnaryOperator))
2629 return !Left.
isOneOf(tok::l_paren, tok::l_square, tok::at) &&
2630 (Left.
isNot(tok::colon) || Left.
isNot(TT_ObjCMethodExpr));
2631 if ((Left.
isOneOf(tok::identifier, tok::greater, tok::r_square,
2637 if (Left.
is(tok::period) || Right.
is(tok::period))
2639 if (Right.
is(tok::hash) && Left.
is(tok::identifier) && Left.
TokenText ==
"L")
2649 if (Left.
is(TT_TemplateCloser) && Right.
is(tok::l_square))
2651 if (Left.
is(tok::l_brace) && Left.
endsSequence(TT_DictLiteral, tok::at))
2661 bool TokenAnnotator::spaceRequiredBefore(
const AnnotatedLine &Line,
2666 if (
Style.isCpp()) {
2667 if (Left.
is(tok::kw_operator))
2668 return Right.
is(tok::coloncolon);
2672 }
else if (
Style.Language == FormatStyle::LK_Proto ||
2673 Style.Language == FormatStyle::LK_TextProto) {
2674 if (Right.
is(tok::period) &&
2675 Left.
isOneOf(Keywords.kw_optional, Keywords.kw_required,
2676 Keywords.kw_repeated, Keywords.kw_extend))
2678 if (Right.
is(tok::l_paren) &&
2679 Left.
isOneOf(Keywords.kw_returns, Keywords.kw_option))
2681 if (Right.
isOneOf(tok::l_brace, tok::less) && Left.
is(TT_SelectorName))
2684 if (Left.
is(tok::slash) || Right.
is(tok::slash))
2688 Right.
isOneOf(tok::l_brace, tok::less))
2689 return !
Style.Cpp11BracedListStyle;
2691 if (Left.
is(tok::percent))
2695 if (Left.
is(tok::numeric_constant) && Right.
is(tok::percent))
2697 }
else if (
Style.Language == FormatStyle::LK_JavaScript ||
Style.isCSharp()) {
2698 if (Left.
is(TT_JsFatArrow))
2701 if (Right.
is(tok::l_paren) && Left.
is(Keywords.kw_await) && Left.
Previous &&
2704 if (Left.
is(Keywords.kw_async) && Right.
is(tok::l_paren) &&
2709 if (Next && Next->
is(TT_JsFatArrow))
2712 if ((Left.
is(TT_TemplateString) && Left.
TokenText.endswith(
"${")) ||
2713 (Right.
is(TT_TemplateString) && Right.
TokenText.startswith(
"}")))
2718 if (Left.
is(tok::identifier) && Keywords.IsJavaScriptIdentifier(Left) &&
2719 Right.
is(TT_TemplateString))
2721 if (Right.
is(tok::star) &&
2722 Left.
isOneOf(Keywords.kw_function, Keywords.kw_yield))
2724 if (Right.
isOneOf(tok::l_brace, tok::l_square) &&
2725 Left.
isOneOf(Keywords.kw_function, Keywords.kw_yield,
2726 Keywords.kw_extends, Keywords.kw_implements))
2728 if (Right.
is(tok::l_paren)) {
2738 if (Left.
isOneOf(tok::kw_throw, Keywords.kw_await, Keywords.kw_typeof,
2742 if ((Left.
isOneOf(Keywords.kw_let, Keywords.kw_var, Keywords.kw_in,
2753 Left.
Previous->
is(tok::period) && Right.
is(tok::l_paren))
2755 if (Left.
is(Keywords.kw_as) &&
2756 Right.
isOneOf(tok::l_square, tok::l_brace, tok::l_paren))
2758 if (Left.
is(tok::kw_default) && Left.
Previous &&
2761 if (Left.
is(Keywords.kw_is) && Right.
is(tok::l_brace))
2763 if (Right.
isOneOf(TT_JsTypeColon, TT_JsTypeOptionalQuestion))
2765 if (Left.
is(TT_JsTypeOperator) || Right.
is(TT_JsTypeOperator))
2767 if ((Left.
is(tok::l_brace) || Right.
is(tok::r_brace)) &&
2768 Line.
First->
isOneOf(Keywords.kw_import, tok::kw_export))
2770 if (Left.
is(tok::ellipsis))
2772 if (Left.
is(TT_TemplateCloser) &&
2773 !Right.
isOneOf(tok::equal, tok::l_brace, tok::comma, tok::l_square,
2774 Keywords.kw_implements, Keywords.kw_extends))
2779 if (Right.
is(TT_JsNonNullAssertion))
2781 if (Left.
is(TT_JsNonNullAssertion) &&
2782 Right.
isOneOf(Keywords.kw_as, Keywords.kw_in))
2784 }
else if (
Style.Language == FormatStyle::LK_Java) {
2785 if (Left.
is(tok::r_square) && Right.
is(tok::l_brace))
2787 if (Left.
is(Keywords.kw_synchronized) && Right.
is(tok::l_paren))
2788 return Style.SpaceBeforeParens != FormatStyle::SBPO_Never;
2789 if ((Left.
isOneOf(tok::kw_static, tok::kw_public, tok::kw_private,
2790 tok::kw_protected) ||
2791 Left.
isOneOf(Keywords.kw_final, Keywords.kw_abstract,
2792 Keywords.kw_native)) &&
2793 Right.
is(TT_TemplateOpener))
2796 if (Left.
is(TT_ImplicitStringLiteral))
2799 if (Left.
is(TT_ObjCMethodSpecifier))
2801 if (Left.
is(tok::r_paren) && canBeObjCSelectorComponent(Right))
2808 (Right.
is(tok::equal) || Left.
is(tok::equal)))
2811 if (Right.
isOneOf(TT_TrailingReturnArrow, TT_LambdaArrow) ||
2812 Left.
isOneOf(TT_TrailingReturnArrow, TT_LambdaArrow))
2814 if (Right.
is(TT_OverloadedOperatorLParen))
2815 return spaceRequiredBeforeParens(Right);
2816 if (Left.
is(tok::comma))
2818 if (Right.
is(tok::comma))
2820 if (Right.
is(TT_ObjCBlockLParen))
2822 if (Right.
is(TT_CtorInitializerColon))
2823 return Style.SpaceBeforeCtorInitializerColon;
2824 if (Right.
is(TT_InheritanceColon) && !
Style.SpaceBeforeInheritanceColon)
2826 if (Right.
is(TT_RangeBasedForLoopColon) &&
2827 !
Style.SpaceBeforeRangeBasedForLoopColon)
2829 if (Right.
is(tok::colon)) {
2830 if (Line.
First->
isOneOf(tok::kw_case, tok::kw_default) ||
2833 if (Right.
is(TT_ObjCMethodExpr))
2835 if (Left.
is(tok::question))
2837 if (Right.
is(TT_InlineASMColon) && Left.
is(tok::coloncolon))
2839 if (Right.
is(TT_DictLiteral))
2840 return Style.SpacesInContainerLiterals;
2841 if (Right.
is(TT_AttributeColon))
2845 if (Left.
is(TT_UnaryOperator))
2846 return (
Style.SpaceAfterLogicalNot && Left.
is(tok::exclaim)) ||
2847 Right.
is(TT_BinaryOperator);
2851 if (Left.
is(TT_CastRParen))
2852 return Style.SpaceAfterCStyleCast ||
2853 Right.
isOneOf(TT_BinaryOperator, TT_SelectorName);
2855 if (Left.
is(tok::greater) && Right.
is(tok::greater)) {
2856 if (
Style.Language == FormatStyle::LK_TextProto ||
2857 (
Style.Language == FormatStyle::LK_Proto && Left.
is(TT_DictLiteral)))
2858 return !
Style.Cpp11BracedListStyle;
2859 return Right.
is(TT_TemplateCloser) && Left.
is(TT_TemplateCloser) &&
2860 (
Style.Standard != FormatStyle::LS_Cpp11 ||
Style.SpacesInAngles);
2862 if (Right.
isOneOf(tok::arrow, tok::arrowstar, tok::periodstar) ||
2863 Left.
isOneOf(tok::arrow, tok::period, tok::arrowstar, tok::periodstar) ||
2864 (Right.
is(tok::period) && Right.
isNot(TT_DesignatedInitializerPeriod)))
2866 if (!
Style.SpaceBeforeAssignmentOperators &&
2869 if (
Style.Language == FormatStyle::LK_Java && Right.
is(tok::coloncolon) &&
2870 (Left.
is(tok::identifier) || Left.
is(tok::kw_this)))
2872 if (Right.
is(tok::coloncolon) && Left.
is(tok::identifier))
2877 if (Right.
is(tok::coloncolon) && !Left.
isOneOf(tok::l_brace, tok::comment))
2878 return (Left.
is(TT_TemplateOpener) &&
2879 Style.Standard == FormatStyle::LS_Cpp03) ||
2880 !(Left.
isOneOf(tok::l_paren, tok::r_paren, tok::l_square,
2881 tok::kw___super, TT_TemplateCloser,
2882 TT_TemplateOpener)) ||
2883 (Left.
is(tok ::l_paren) &&
Style.SpacesInParentheses);
2884 if ((Left.
is(TT_TemplateOpener)) != (Right.
is(TT_TemplateCloser)))
2885 return Style.SpacesInAngles;
2887 if (Right.
is(TT_StructuredBindingLSquare))
2888 return !Left.
isOneOf(tok::amp, tok::ampamp) ||
2889 Style.PointerAlignment != FormatStyle::PAS_Right;
2891 if (Right.
Next && Right.
Next->
is(TT_StructuredBindingLSquare) &&
2892 Right.
isOneOf(tok::amp, tok::ampamp))
2893 return Style.PointerAlignment != FormatStyle::PAS_Left;
2894 if ((Right.
is(TT_BinaryOperator) && !Left.
is(tok::l_paren)) ||
2895 (Left.
isOneOf(TT_BinaryOperator, TT_ConditionalExpr) &&
2896 !Right.
is(tok::r_paren)))
2898 if (Left.
is(TT_TemplateCloser) && Right.
is(tok::l_paren) &&
2899 Right.
isNot(TT_FunctionTypeLParen))
2900 return spaceRequiredBeforeParens(Right);
2901 if (Right.
is(TT_TemplateOpener) && Left.
is(tok::r_paren) &&
2904 if (Right.
is(tok::less) && Left.
isNot(tok::l_paren) &&
2907 if (Right.
is(TT_TrailingUnaryOperator))
2909 if (Left.
is(TT_RegexLiteral))
2911 return spaceRequiredBetween(Line, Left, Right);
2917 !Tok.
isOneOf(TT_ObjCBlockLBrace, TT_LambdaLBrace, TT_DictLiteral);
2920 bool TokenAnnotator::mustBreakBefore(
const AnnotatedLine &Line,
2926 if (
Style.Language == FormatStyle::LK_JavaScript) {
2928 if (Right.
is(tok::string_literal) && Left.
is(tok::plus) && Left.
Previous &&
2931 if (Left.
is(TT_DictLiteral) && Left.
is(tok::l_brace) && Line.
Level == 0 &&
2933 Line.
First->
isOneOf(tok::identifier, Keywords.kw_import, tok::kw_export,
2937 !Line.
First->
isOneOf(Keywords.kw_var, Keywords.kw_let))
2941 if (Left.
is(tok::l_brace) && Line.
Level == 0 &&
2943 Line.
startsWith(tok::kw_const, tok::kw_enum) ||
2944 Line.
startsWith(tok::kw_export, tok::kw_enum) ||
2945 Line.
startsWith(tok::kw_export, tok::kw_const, tok::kw_enum)))
2949 if (Right.
is(tok::r_brace) && Left.
is(tok::l_brace) &&
2952 return Style.AllowShortFunctionsOnASingleLine == FormatStyle::SFS_None ||
2953 Style.AllowShortFunctionsOnASingleLine == FormatStyle::SFS_Empty ||
2955 Style.AllowShortFunctionsOnASingleLine &
2956 FormatStyle::SFS_InlineOnly);
2957 }
else if (
Style.Language == FormatStyle::LK_Java) {
2958 if (Right.
is(tok::plus) && Left.
is(tok::string_literal) && Right.
Next &&
2959 Right.
Next->
is(tok::string_literal))
2961 }
else if (
Style.Language == FormatStyle::LK_Cpp ||
2962 Style.Language == FormatStyle::LK_ObjC ||
2963 Style.Language == FormatStyle::LK_Proto ||
2964 Style.Language == FormatStyle::LK_TableGen ||
2965 Style.Language == FormatStyle::LK_TextProto) {
2976 if ((Left.
isOneOf(tok::l_brace, TT_ArrayInitializerLSquare) ||
2977 (
Style.Language == FormatStyle::LK_JavaScript &&
2978 Left.
is(tok::l_paren))) &&
2983 TT_ArrayInitializerLSquare) ||
2984 (
Style.Language == FormatStyle::LK_JavaScript &&
2986 BeforeClosingBrace = &Left;
2987 if (BeforeClosingBrace && (BeforeClosingBrace->
is(tok::comma) ||
2992 if (Right.
is(tok::comment))
2994 Left.
isNot(TT_CtorInitializerColon) &&
3000 if (Right.
is(tok::lessless) && Right.
Next &&
3002 Right.
Next->
is(tok::string_literal))
3007 Style.AlwaysBreakTemplateDeclarations == FormatStyle::BTDS_Yes)
3009 if (Right.
is(TT_CtorInitializerComma) &&
3010 Style.BreakConstructorInitializers == FormatStyle::BCIS_BeforeComma &&
3011 !
Style.ConstructorInitializerAllOnOneLineOrOnePerLine)
3013 if (Right.
is(TT_CtorInitializerColon) &&
3014 Style.BreakConstructorInitializers == FormatStyle::BCIS_BeforeComma &&
3015 !
Style.ConstructorInitializerAllOnOneLineOrOnePerLine)
3018 if (
Style.BreakInheritanceList == FormatStyle::BILS_BeforeComma &&
3019 Right.
is(TT_InheritanceComma))
3021 if (Right.
is(tok::string_literal) && Right.
TokenText.startswith(
"R\""))
3034 if (Right.
is(TT_InlineASMBrace))
3037 return (Line.
startsWith(tok::kw_enum) &&
Style.BraceWrapping.AfterEnum) ||
3038 (Line.
startsWith(tok::kw_typedef, tok::kw_enum) &&
3039 Style.BraceWrapping.AfterEnum) ||
3042 if (Left.
is(TT_ObjCBlockLBrace) && !
Style.AllowShortBlocksOnASingleLine)
3045 if (Left.
is(TT_LambdaLBrace)) {
3048 Style.AllowShortLambdasOnASingleLine == FormatStyle::SLS_Inline)
3051 if (
Style.AllowShortLambdasOnASingleLine == FormatStyle::SLS_None ||
3052 Style.AllowShortLambdasOnASingleLine == FormatStyle::SLS_Inline ||
3054 Style.AllowShortLambdasOnASingleLine == FormatStyle::SLS_Empty))
3059 if (
Style.isCSharp() &&
3060 ((Left.
is(TT_AttributeSquare) && Left.
is(tok::r_square)) ||
3061 (Left.
is(tok::r_square) && Right.
is(TT_AttributeSquare) &&
3062 Right.
is(tok::l_square))))
3066 if ((
Style.Language == FormatStyle::LK_Java ||
3067 Style.Language == FormatStyle::LK_JavaScript) &&
3068 Left.
is(TT_LeadingJavaAnnotation) &&
3069 Right.
isNot(TT_LeadingJavaAnnotation) && Right.
isNot(tok::l_paren) &&
3070 (Line.
Last->
is(tok::l_brace) ||
Style.BreakAfterJavaFieldAnnotations))
3073 if (Right.
is(TT_ProtoExtensionLSquare))
3103 if ((
Style.Language == FormatStyle::LK_Proto ||
3104 Style.Language == FormatStyle::LK_TextProto) &&
3105 Right.
is(TT_SelectorName) && !Right.
is(tok::r_square) && Right.
Next) {
3115 if (LBrace && LBrace->
is(tok::colon)) {
3116 LBrace = LBrace->
Next;
3117 if (LBrace && LBrace->
is(tok::at)) {
3118 LBrace = LBrace->
Next;
3120 LBrace = LBrace->
Next;
3132 ((LBrace->
is(tok::l_brace) &&
3133 (LBrace->
is(TT_DictLiteral) ||
3134 (LBrace->
Next && LBrace->
Next->
is(tok::r_brace)))) ||
3135 LBrace->
is(TT_ArrayInitializerLSquare) || LBrace->
is(tok::less))) {
3157 if (Left.
isOneOf(tok::r_brace, tok::greater, tok::r_square))
3164 if ((
Style.Language == FormatStyle::LK_Cpp ||
3165 Style.Language == FormatStyle::LK_ObjC) &&
3167 !Right.
isOneOf(tok::l_paren, TT_LambdaLSquare)) {
3178 auto Next =
Comma->getNextNonComment();
3181 if (!Next->isOneOf(TT_LambdaLSquare, tok::l_brace, tok::caret))
3188 bool TokenAnnotator::canBreakBefore(
const AnnotatedLine &Line,
3193 if (
Style.Language == FormatStyle::LK_Java) {
3194 if (Left.
isOneOf(Keywords.kw_throws, Keywords.kw_extends,
3195 Keywords.kw_implements))
3197 if (Right.
isOneOf(Keywords.kw_throws, Keywords.kw_extends,
3198 Keywords.kw_implements))
3200 }
else if (
Style.Language == FormatStyle::LK_JavaScript) {
3204 tok::kw_return, Keywords.kw_yield, tok::kw_continue, tok::kw_break,
3205 tok::kw_throw, Keywords.kw_interface, Keywords.kw_type,
3206 tok::kw_static, tok::kw_public, tok::kw_private, tok::kw_protected,
3207 Keywords.kw_readonly, Keywords.kw_abstract, Keywords.kw_get,
3208 Keywords.kw_set, Keywords.kw_async, Keywords.kw_await))
3212 Left.
isOneOf(tok::r_square, tok::r_paren)) &&
3213 Right.
isOneOf(tok::l_square, tok::l_paren))
3215 if (Left.
is(TT_JsFatArrow) && Right.
is(tok::l_brace))
3217 if (Left.
is(TT_JsTypeColon))
3220 if (Left.
is(tok::exclaim) && Right.
is(tok::colon))
3225 if (Right.
is(Keywords.kw_is)) {
3234 if (!Next || !Next->
is(tok::colon))
3237 if (Left.
is(Keywords.kw_in))
3238 return Style.BreakBeforeBinaryOperators == FormatStyle::BOS_None;
3239 if (Right.
is(Keywords.kw_in))
3240 return Style.BreakBeforeBinaryOperators != FormatStyle::BOS_None;
3241 if (Right.
is(Keywords.kw_as))
3243 if (Right.
isOneOf(Keywords.kw_extends, Keywords.kw_infer)) {
3249 if (Left.
is(Keywords.kw_as))
3251 if (Left.
is(TT_JsNonNullAssertion))
3253 if (Left.
is(Keywords.kw_declare) &&
3254 Right.
isOneOf(Keywords.kw_module, tok::kw_namespace,
3255 Keywords.kw_function, tok::kw_class, tok::kw_enum,
3256 Keywords.kw_interface, Keywords.kw_type, Keywords.kw_var,
3257 Keywords.kw_let, tok::kw_const))
3261 if (Left.
isOneOf(Keywords.kw_module, tok::kw_namespace) &&
3262 Right.
isOneOf(tok::identifier, tok::string_literal))
3268 if (Left.
is(tok::identifier) && Right.
is(TT_TemplateString)) {
3275 if (Left.
is(tok::at))
3279 if (Left.
isOneOf(TT_JavaAnnotation, TT_LeadingJavaAnnotation))
3280 return !Right.
is(tok::l_paren);
3281 if (Right.
is(TT_PointerOrReference))
3283 (
Style.PointerAlignment == FormatStyle::PAS_Right &&
3284 (!Right.
Next || Right.
Next->
isNot(TT_FunctionDeclarationName)));
3285 if (Right.
isOneOf(TT_StartOfName, TT_FunctionDeclarationName) ||
3286 Right.
is(tok::kw_operator))
3288 if (Left.
is(TT_PointerOrReference))
3297 (Left.
is(TT_CtorInitializerColon) &&
3298 Style.BreakConstructorInitializers == FormatStyle::BCIS_AfterColon);
3299 if (Left.
is(tok::question) && Right.
is(tok::colon))
3301 if (Right.
is(TT_ConditionalExpr) || Right.
is(tok::question))
3302 return Style.BreakBeforeTernaryOperators;
3303 if (Left.
is(TT_ConditionalExpr) || Left.
is(tok::question))
3304 return !
Style.BreakBeforeTernaryOperators;
3305 if (Left.
is(TT_InheritanceColon))
3306 return Style.BreakInheritanceList == FormatStyle::BILS_AfterColon;
3307 if (Right.
is(TT_InheritanceColon))
3308 return Style.BreakInheritanceList != FormatStyle::BILS_AfterColon;
3309 if (Right.
is(TT_ObjCMethodExpr) && !Right.
is(tok::r_square) &&
3310 Left.
isNot(TT_SelectorName))
3313 if (Right.
is(tok::colon) &&
3314 !Right.
isOneOf(TT_CtorInitializerColon, TT_InlineASMColon))
3316 if (Left.
is(tok::colon) && Left.
isOneOf(TT_DictLiteral, TT_ObjCMethodExpr)) {
3317 if (
Style.Language == FormatStyle::LK_Proto ||
3318 Style.Language == FormatStyle::LK_TextProto) {
3345 if (((Right.
is(tok::l_brace) || Right.
is(tok::less)) &&
3346 Right.
is(TT_DictLiteral)) ||
3347 Right.
is(TT_ArrayInitializerLSquare))
3355 if (Right.
is(TT_SelectorName) || (Right.
is(tok::identifier) && Right.
Next &&
3356 Right.
Next->
is(TT_ObjCMethodExpr)))
3357 return Left.
isNot(tok::period);
3362 if (Right.
isOneOf(TT_RangeBasedForLoopColon, TT_OverloadedOperatorLParen,
3363 TT_OverloadedOperator))
3365 if (Left.
is(TT_RangeBasedForLoopColon))
3367 if (Right.
is(TT_RangeBasedForLoopColon))
3369 if (Left.
is(TT_TemplateCloser) && Right.
is(TT_TemplateOpener))
3371 if (Left.
isOneOf(TT_TemplateCloser, TT_UnaryOperator) ||
3372 Left.
is(tok::kw_operator))
3374 if (Left.
is(tok::equal) && !Right.
isOneOf(tok::kw_default, tok::kw_delete) &&
3377 if (Left.
is(tok::equal) && Right.
is(tok::l_brace) &&
3378 !
Style.Cpp11BracedListStyle)
3380 if (Left.
is(tok::l_paren) && Left.
is(TT_AttributeParen))
3382 if (Left.
is(tok::l_paren) && Left.
Previous &&
3385 if (Right.
is(TT_ImplicitStringLiteral))
3388 if (Right.
is(tok::r_paren) || Right.
is(TT_TemplateCloser))
3396 if (Right.
is(tok::r_brace))
3401 if (Left.
is(TT_TrailingAnnotation))
3402 return !Right.
isOneOf(tok::l_brace, tok::semi, tok::equal, tok::l_paren,
3403 tok::less, tok::coloncolon);
3405 if (Right.
is(tok::kw___attribute) ||
3406 (Right.
is(tok::l_square) && Right.
is(TT_AttributeSquare)))
3409 if (Left.
is(tok::identifier) && Right.
is(tok::string_literal))
3412 if (Right.
is(tok::identifier) && Right.
Next && Right.
Next->
is(TT_DictLiteral))
3415 if (Left.
is(TT_CtorInitializerColon))
3416 return Style.BreakConstructorInitializers == FormatStyle::BCIS_AfterColon;
3417 if (Right.
is(TT_CtorInitializerColon))
3418 return Style.BreakConstructorInitializers != FormatStyle::BCIS_AfterColon;
3419 if (Left.
is(TT_CtorInitializerComma) &&
3420 Style.BreakConstructorInitializers == FormatStyle::BCIS_BeforeComma)
3422 if (Right.
is(TT_CtorInitializerComma) &&
3423 Style.BreakConstructorInitializers == FormatStyle::BCIS_BeforeComma)
3425 if (Left.
is(TT_InheritanceComma) &&
3426 Style.BreakInheritanceList == FormatStyle::BILS_BeforeComma)
3428 if (Right.
is(TT_InheritanceComma) &&
3429 Style.BreakInheritanceList == FormatStyle::BILS_BeforeComma)
3431 if ((Left.
is(tok::greater) && Right.
is(tok::greater)) ||
3432 (Left.
is(tok::less) && Right.
is(tok::less)))
3434 if (Right.
is(TT_BinaryOperator) &&
3435 Style.BreakBeforeBinaryOperators != FormatStyle::BOS_None &&
3436 (
Style.BreakBeforeBinaryOperators == FormatStyle::BOS_All ||
3439 if (Left.
is(TT_ArrayInitializerLSquare))
3441 if (Right.
is(tok::kw_typename) && Left.
isNot(tok::kw_const))
3444 !Left.
isOneOf(tok::arrowstar, tok::lessless) &&
3445 Style.BreakBeforeBinaryOperators != FormatStyle::BOS_All &&
3446 (
Style.BreakBeforeBinaryOperators == FormatStyle::BOS_None ||
3449 if ((Left.
is(TT_AttributeSquare) && Right.
is(tok::l_square)) ||
3450 (Left.
is(tok::r_square) && Right.
is(TT_AttributeSquare)))
3452 return Left.
isOneOf(tok::comma, tok::coloncolon, tok::semi, tok::l_brace,
3453 tok::kw_class, tok::kw_struct, tok::comment) ||
3455 Right.
isOneOf(TT_TrailingReturnArrow, TT_LambdaArrow, tok::lessless,
3456 tok::colon, tok::l_square, tok::at) ||
3457 (Left.
is(tok::r_paren) &&
3458 Right.
isOneOf(tok::identifier, tok::kw_const)) ||
3459 (Left.
is(tok::l_paren) && !Right.
is(tok::r_paren)) ||
3460 (Left.
is(TT_TemplateOpener) && !Right.
is(TT_TemplateCloser));
3463 void TokenAnnotator::printDebugInfo(
const AnnotatedLine &Line) {
3464 llvm::errs() <<
"AnnotatedTokens(L=" << Line.
Level <<
"):\n";
3474 <<
" PPK=" << Tok->
PackingKind <<
" FakeLParens=";
3477 llvm::errs() <<
" FakeRParens=" << Tok->
FakeRParens;
3479 llvm::errs() <<
" Text='" << Tok->
TokenText <<
"'\n";
3481 assert(Tok == Line.
Last);
3484 llvm::errs() <<
"----\n";
if(T->getSizeExpr()) TRY_TO(TraverseStmt(T -> getSizeExpr()))
bool is(tok::TokenKind K) const
is/isNot - Predicates to check if this token is a specific kind, as in "if (Tok.is(tok::l_brace)) {...
Defines the SourceManager interface.
Parser - This implements a parser for the C family of languages.
bool isLiteral() const
Return true if this is a "literal", like a numeric constant, string, etc.
tok::TokenKind ContextKind
This file implements a token annotator, i.e.
__DEVICE__ int max(int __a, int __b)
bool InCSharpAttributeSpecifier
const char * getName() const
const AnnotatedLine * Line
FormatToken * FirstStartOfName
SourceLocation getEnd() const
bool InCpp11AttributeSpecifier
IdentifierInfo * getIdentifierInfo() const
TokenKind
Provides a simple uniform namespace for tokens from all C languages.
tok::ObjCKeywordKind getObjCKeywordID() const
Return the ObjC keyword kind.
Optional< types::ID > Type
bool ColonIsObjCMethodExpr
Dataflow Directional Tag Classes.
FormatToken * FirstObjCSelectorName
The parameter type of a method or function.
unsigned LongestObjCSelectorName
prec::Level getBinOpPrecedence(tok::TokenKind Kind, bool GreaterThanIsOperator, bool CPlusPlus11)
Return the precedence of the specified binary operator token.
SourceLocation getBegin() const