18 #include "llvm/ADT/SmallPtrSet.h"
19 #include "llvm/Support/Debug.h"
21 #define DEBUG_TYPE "format-token-annotator"
33 class AnnotatingParser {
35 AnnotatingParser(
const FormatStyle &
Style, AnnotatedLine &
Line,
48 Left->ParentBracket =
Contexts.back().ContextKind;
49 ScopedContextCreator ContextCreator(*
this, tok::less, 10);
53 bool InExprContext =
Contexts.back().IsExpression;
55 Contexts.back().IsExpression =
false;
59 Left->Previous && Left->Previous->Tok.isNot(tok::kw_template);
78 if (
CurrentToken->isOneOf(tok::r_paren, tok::r_square, tok::r_brace) ||
79 (
CurrentToken->isOneOf(tok::colon, tok::question) && InExprContext))
87 if (
CurrentToken->Previous->isOneOf(tok::pipepipe, tok::ampamp) &&
90 !Line.startsWith(tok::kw_template))
99 bool parseParens(
bool LookForDecls =
false) {
103 Left->ParentBracket =
Contexts.back().ContextKind;
104 ScopedContextCreator ContextCreator(*
this, tok::l_paren, 1);
107 Contexts.back().ColonIsForRangeExpr =
110 bool StartsObjCMethodExpr =
false;
113 Left->Type = TT_ObjCBlockLParen;
114 }
else if (FormatToken *MaybeSel = Left->Previous) {
116 if (MaybeSel->isObjCAtKeyword(tok::objc_selector) && MaybeSel->Previous &&
117 MaybeSel->Previous->is(tok::at)) {
118 StartsObjCMethodExpr =
true;
122 if (Left->Previous &&
123 (Left->Previous->isOneOf(tok::kw_static_assert, tok::kw_if,
124 tok::kw_while, tok::l_paren, tok::comma) ||
125 Left->Previous->is(TT_BinaryOperator))) {
127 Contexts.back().IsExpression =
true;
128 }
else if (Left->Previous && Left->Previous->is(tok::r_square) &&
129 Left->Previous->MatchingParen &&
130 Left->Previous->MatchingParen->is(TT_LambdaLSquare)) {
132 Contexts.back().IsExpression =
false;
133 }
else if (Line.InPPDirective &&
135 !Left->Previous->isOneOf(tok::identifier,
136 TT_OverloadedOperator))) {
137 Contexts.back().IsExpression =
true;
140 Contexts.back().IsExpression =
false;
141 }
else if (Left->Previous && Left->Previous->is(tok::kw___attribute)) {
142 Left->Type = TT_AttributeParen;
143 }
else if (Left->Previous && Left->Previous->is(TT_ForEachMacro)) {
145 Contexts.back().IsForEachMacro =
true;
146 Contexts.back().IsExpression =
false;
147 }
else if (Left->Previous && Left->Previous->MatchingParen &&
148 Left->Previous->MatchingParen->is(TT_ObjCBlockLParen)) {
149 Contexts.back().IsExpression =
false;
152 if (StartsObjCMethodExpr) {
153 Contexts.back().ColonIsObjCMethodExpr =
true;
154 Left->Type = TT_ObjCMethodExpr;
158 bool HasMultipleLines =
false;
159 bool HasMultipleParametersOnALine =
false;
160 bool MightBeObjCForRangeLoop =
161 Left->Previous && Left->Previous->is(tok::kw_for);
168 FormatToken *Prev =
CurrentToken->getPreviousNonComment();
170 FormatToken *PrevPrev = Prev->getPreviousNonComment();
172 if (PrevPrev && PrevPrev->is(tok::identifier) &&
173 Prev->isOneOf(tok::star, tok::amp, tok::ampamp) &&
174 CurrentToken->is(tok::identifier) && Next->isNot(tok::equal)) {
175 Prev->Type = TT_BinaryOperator;
176 LookForDecls =
false;
181 if (
CurrentToken->Previous->is(TT_PointerOrReference) &&
184 MightBeFunctionType =
true;
186 Contexts.back().IsExpression =
true;
192 Left->Type = TT_FunctionTypeLParen;
196 if (StartsObjCMethodExpr) {
198 if (
Contexts.back().FirstObjCSelectorName) {
199 Contexts.back().FirstObjCSelectorName->LongestObjCSelectorName =
200 Contexts.back().LongestObjCSelectorName;
204 if (Left->is(TT_AttributeParen))
206 if (Left->Previous && Left->Previous->is(TT_JavaAnnotation))
208 if (Left->Previous && Left->Previous->is(TT_LeadingJavaAnnotation))
211 if (!HasMultipleLines)
213 else if (HasMultipleParametersOnALine)
225 Left->Type = TT_Unknown;
229 HasMultipleParametersOnALine =
true;
230 if (
CurrentToken->isOneOf(tok::kw_const, tok::kw_auto) ||
232 Contexts.back().IsExpression =
false;
234 MightBeObjCForRangeLoop =
false;
235 if (MightBeObjCForRangeLoop &&
CurrentToken->is(Keywords.kw_in))
240 Contexts.back().CanBeExpression =
true;
245 updateParameterCount(Left, Tok);
247 HasMultipleLines =
true;
260 Left->ParentBracket =
Contexts.back().ContextKind;
261 FormatToken *Parent = Left->getPreviousNonComment();
262 bool StartsObjCMethodExpr =
264 Contexts.back().CanBeExpression && Left->isNot(TT_LambdaLSquare) &&
267 Parent->isOneOf(tok::colon, tok::l_square, tok::l_paren,
268 tok::kw_return, tok::kw_throw) ||
269 Parent->isUnaryOperator() ||
270 Parent->isOneOf(TT_ObjCForIn, TT_CastRParen) ||
272 bool ColonFound =
false;
274 unsigned BindingIncrease = 1;
275 if (Left->is(TT_Unknown)) {
276 if (StartsObjCMethodExpr) {
277 Left->Type = TT_ObjCMethodExpr;
279 Contexts.back().ContextKind == tok::l_brace &&
280 Parent->isOneOf(tok::l_brace, tok::comma)) {
281 Left->Type = TT_JsComputedPropertyName;
283 Parent->isOneOf(tok::at, tok::equal, tok::comma, tok::l_paren,
284 tok::l_square, tok::question, tok::colon,
286 Left->Type = TT_ArrayInitializerLSquare;
288 BindingIncrease = 10;
289 Left->Type = TT_ArraySubscriptLSquare;
293 ScopedContextCreator ContextCreator(*
this, tok::l_square, BindingIncrease);
294 Contexts.back().IsExpression =
true;
295 Contexts.back().ColonIsObjCMethodExpr = StartsObjCMethodExpr;
300 Left->is(TT_ObjCMethodExpr)) {
303 StartsObjCMethodExpr =
false;
304 Left->Type = TT_Unknown;
306 if (StartsObjCMethodExpr &&
CurrentToken->Previous != Left) {
311 if (Parent && Parent->is(TT_PointerOrReference))
312 Parent->Type = TT_BinaryOperator;
316 if (
Contexts.back().FirstObjCSelectorName) {
317 Contexts.back().FirstObjCSelectorName->LongestObjCSelectorName =
318 Contexts.back().LongestObjCSelectorName;
319 if (Left->BlockParameterCount > 1)
320 Contexts.back().FirstObjCSelectorName->LongestObjCSelectorName = 0;
328 if (Left->is(TT_ArraySubscriptLSquare)) {
329 Left->Type = TT_ObjCMethodExpr;
330 StartsObjCMethodExpr =
true;
331 Contexts.back().ColonIsObjCMethodExpr =
true;
332 if (Parent && Parent->is(tok::r_paren))
333 Parent->Type = TT_CastRParen;
337 if (
CurrentToken->is(tok::comma) && Left->is(TT_ObjCMethodExpr) &&
339 Left->Type = TT_ArrayInitializerLSquare;
343 updateParameterCount(Left, Tok);
351 Left->ParentBracket =
Contexts.back().ContextKind;
354 Left->Type = TT_ObjCBlockLBrace;
357 ScopedContextCreator ContextCreator(*
this, tok::l_brace, 1);
358 Contexts.back().ColonIsDictLiteral =
true;
360 Contexts.back().IsExpression =
true;
376 Previous->is(tok::identifier))
377 Previous->Type = TT_SelectorName;
380 Left->Type = TT_DictLiteral;
389 void updateParameterCount(FormatToken *Left, FormatToken *
Current) {
390 if (Current->is(TT_LambdaLSquare) ||
391 (Current->is(tok::caret) && Current->is(TT_UnaryOperator)) ||
393 Current->is(Keywords.kw_function))) {
394 ++Left->BlockParameterCount;
396 if (Current->is(tok::comma)) {
397 ++Left->ParameterCount;
399 Left->Role.reset(
new CommaSeparatedList(Style));
400 Left->Role->CommaFound(Current);
401 }
else if (Left->ParameterCount == 0 && Current->isNot(tok::comment)) {
402 Left->ParameterCount = 1;
406 bool parseConditional() {
419 bool parseTemplateDeclaration() {
426 CurrentToken->Previous->ClosesTemplateDeclaration =
true;
432 bool consumeToken() {
435 switch (Tok->Tok.getKind()) {
438 if (!Tok->Previous && Line.MustBeDeclaration)
439 Tok->Type = TT_ObjCMethodSpecifier;
446 if (
Contexts.back().ColonIsForRangeExpr ||
448 !Line.First->isOneOf(tok::kw_enum, tok::kw_case)) ||
449 Contexts.back().ContextKind == tok::l_paren ||
450 Contexts.back().ContextKind == tok::l_square ||
452 Line.MustBeDeclaration)) {
453 Tok->Type = TT_JsTypeColon;
457 if (
Contexts.back().ColonIsDictLiteral) {
458 Tok->Type = TT_DictLiteral;
459 }
else if (
Contexts.back().ColonIsObjCMethodExpr ||
460 Line.startsWith(TT_ObjCMethodSpecifier)) {
461 Tok->Type = TT_ObjCMethodExpr;
462 Tok->Previous->Type = TT_SelectorName;
463 if (Tok->Previous->ColumnWidth >
464 Contexts.back().LongestObjCSelectorName) {
465 Contexts.back().LongestObjCSelectorName = Tok->Previous->ColumnWidth;
467 if (!
Contexts.back().FirstObjCSelectorName)
468 Contexts.back().FirstObjCSelectorName = Tok->Previous;
469 }
else if (
Contexts.back().ColonIsForRangeExpr) {
470 Tok->Type = TT_RangeBasedForLoopColon;
472 Tok->Type = TT_BitFieldColon;
474 !Line.First->isOneOf(tok::kw_enum, tok::kw_case)) {
475 if (Tok->Previous->is(tok::r_paren))
476 Tok->Type = TT_CtorInitializerColon;
478 Tok->Type = TT_InheritanceColon;
479 }
else if (Tok->Previous->is(tok::identifier) && Tok->Next &&
480 Tok->Next->isOneOf(tok::r_paren, tok::comma)) {
483 Tok->Type = TT_ObjCMethodExpr;
484 }
else if (
Contexts.back().ContextKind == tok::l_paren) {
485 Tok->Type = TT_InlineASMColon;
492 if (!parseParens(
true))
497 Contexts.back().ColonIsForRangeExpr =
true;
505 if (Line.MustBeDeclaration &&
Contexts.size() == 1 &&
506 !
Contexts.back().IsExpression && !Line.startsWith(TT_ObjCProperty) &&
508 !Tok->Previous->isOneOf(tok::kw_decltype, tok::kw___attribute,
509 TT_LeadingJavaAnnotation)))
510 Line.MightBeFunctionDecl =
true;
523 (!Tok->Previous->Tok.isLiteral() &&
524 !(Tok->Previous->is(tok::r_paren) &&
Contexts.size() > 1))) &&
526 Tok->Type = TT_TemplateOpener;
528 Tok->Type = TT_BinaryOperator;
543 Tok->Type = TT_BinaryOperator;
545 case tok::kw_operator:
547 !
CurrentToken->isOneOf(tok::l_paren, tok::semi, tok::r_paren)) {
562 Tok->Next->isOneOf(tok::semi, tok::comma, tok::colon, tok::r_paren,
568 Tok->Type = TT_JsTypeOptionalQuestion;
573 if (Line.MustBeDeclaration &&
578 case tok::kw_template:
579 parseTemplateDeclaration();
582 if (
Contexts.back().InCtorInitializer)
583 Tok->Type = TT_CtorInitializerComma;
585 (
Contexts.size() == 1 || Line.startsWith(tok::kw_for))) {
586 Contexts.back().FirstStartOfName->PartOfMultiVariableDeclStmt =
true;
587 Line.IsMultiVariableDeclStmt =
true;
590 Contexts.back().IsExpression =
true;
598 void parseIncludeDirective() {
609 void parseWarningOrError() {
623 CurrentToken->isOneOf(Keywords.kw_mark, Keywords.kw_option)) {
628 if (IsMark ||
CurrentToken->Previous->is(TT_BinaryOperator))
635 LineType parsePreprocessorDirective() {
648 switch (
CurrentToken->Tok.getIdentifierInfo()->getPPKeywordID()) {
649 case tok::pp_include:
650 case tok::pp_include_next:
653 parseIncludeDirective();
657 case tok::pp_warning:
658 parseWarningOrError();
665 Contexts.back().IsExpression =
true;
680 return parsePreprocessorDirective();
685 IdentifierInfo *Info =
CurrentToken->Tok.getIdentifierInfo();
688 (Info && Info->getPPKeywordID() == tok::pp_import &&
690 CurrentToken->Next->isOneOf(tok::string_literal, tok::identifier,
693 parseIncludeDirective();
699 if (
CurrentToken->is(tok::less) && Line.Last->is(tok::greater)) {
700 parseIncludeDirective();
713 bool KeywordVirtualFound =
false;
714 bool ImportStatement =
false;
717 KeywordVirtualFound =
true;
719 ImportStatement =
true;
723 if (KeywordVirtualFound)
728 if (Line.startsWith(TT_ObjCMethodSpecifier)) {
729 if (
Contexts.back().FirstObjCSelectorName)
730 Contexts.back().FirstObjCSelectorName->LongestObjCSelectorName =
731 Contexts.back().LongestObjCSelectorName;
739 bool IsImportStatement(
const FormatToken &Tok) {
743 Tok.TokenText ==
"goog" && Tok.Next && Tok.Next->is(tok::period) &&
744 Tok.Next->Next && (Tok.Next->Next->TokenText ==
"module" ||
745 Tok.Next->Next->TokenText ==
"require" ||
746 Tok.Next->Next->TokenText ==
"provide") &&
747 Tok.Next->Next->Next && Tok.Next->Next->Next->is(tok::l_paren);
750 void resetTokenMetadata(FormatToken *
Token) {
756 if (!
CurrentToken->isOneOf(TT_LambdaLSquare, TT_ForEachMacro,
757 TT_FunctionLBrace, TT_ImplicitStringLiteral,
758 TT_InlineASMBrace, TT_JsFatArrow, TT_LambdaArrow,
805 struct ScopedContextCreator {
811 P.Contexts.push_back(
Context(ContextKind,
812 P.Contexts.back().BindingStrength + Increase,
813 P.Contexts.back().IsExpression));
816 ~ScopedContextCreator() {
P.Contexts.pop_back(); }
819 void modifyContext(
const FormatToken &Current) {
821 !Line.First->isOneOf(tok::kw_template, tok::kw_using) &&
822 (!Current.Previous || Current.Previous->isNot(tok::kw_operator))) {
823 Contexts.back().IsExpression =
true;
824 if (!Line.startsWith(TT_UnaryOperator)) {
825 for (FormatToken *
Previous = Current.Previous;
828 if (
Previous->isOneOf(tok::r_square, tok::r_paren)) {
835 if (
Previous->isOneOf(TT_BinaryOperator, TT_UnaryOperator) &&
836 Previous->isOneOf(tok::star, tok::amp, tok::ampamp) &&
838 Previous->Type = TT_PointerOrReference;
841 }
else if (Current.is(tok::lessless) &&
842 (!Current.Previous || !Current.Previous->is(tok::kw_operator))) {
843 Contexts.back().IsExpression =
true;
844 }
else if (Current.isOneOf(tok::kw_return, tok::kw_throw)) {
845 Contexts.back().IsExpression =
true;
846 }
else if (Current.is(TT_TrailingReturnArrow)) {
847 Contexts.back().IsExpression =
false;
848 }
else if (Current.is(TT_LambdaArrow)) {
850 }
else if (Current.is(tok::l_paren) && !Line.MustBeDeclaration &&
851 !Line.InPPDirective &&
852 (!Current.Previous ||
853 Current.Previous->isNot(tok::kw_decltype))) {
854 bool ParametersOfFunctionType =
855 Current.Previous && Current.Previous->is(tok::r_paren) &&
856 Current.Previous->MatchingParen &&
857 Current.Previous->MatchingParen->is(TT_FunctionTypeLParen);
858 bool IsForOrCatch = Current.Previous &&
859 Current.Previous->isOneOf(tok::kw_for, tok::kw_catch);
860 Contexts.back().IsExpression = !ParametersOfFunctionType && !IsForOrCatch;
861 }
else if (Current.isOneOf(tok::r_paren, tok::greater, tok::comma)) {
862 for (FormatToken *
Previous = Current.Previous;
865 Previous->Type = TT_PointerOrReference;
866 if (Line.MustBeDeclaration)
868 }
else if (Current.Previous &&
869 Current.Previous->is(TT_CtorInitializerColon)) {
870 Contexts.back().IsExpression =
true;
871 Contexts.back().InCtorInitializer =
true;
872 }
else if (Current.is(tok::kw_new)) {
873 Contexts.back().CanBeExpression =
false;
874 }
else if (Current.isOneOf(tok::semi, tok::exclaim)) {
876 Contexts.back().IsExpression =
true;
880 void determineTokenType(FormatToken &Current) {
881 if (!Current.is(TT_Unknown))
888 if (Current.is(Keywords.kw_instanceof)) {
889 Current.Type = TT_BinaryOperator;
890 }
else if (isStartOfName(Current) &&
891 (!Line.MightBeFunctionDecl || Current.NestingLevel != 0)) {
893 Current.Type = TT_StartOfName;
894 }
else if (Current.is(tok::kw_auto)) {
896 }
else if (Current.is(tok::arrow) &&
898 Current.Type = TT_LambdaArrow;
899 }
else if (Current.is(tok::arrow) &&
AutoFound && Line.MustBeDeclaration &&
900 Current.NestingLevel == 0) {
901 Current.Type = TT_TrailingReturnArrow;
902 }
else if (Current.isOneOf(tok::star, tok::amp, tok::ampamp)) {
904 determineStarAmpUsage(Current,
Contexts.back().CanBeExpression &&
906 Contexts.back().InTemplateArgument);
907 }
else if (Current.isOneOf(tok::minus, tok::plus, tok::caret)) {
908 Current.Type = determinePlusMinusCaretUsage(Current);
909 if (Current.is(TT_UnaryOperator) && Current.is(tok::caret))
911 }
else if (Current.isOneOf(tok::minusminus, tok::plusplus)) {
912 Current.Type = determineIncrementUsage(Current);
913 }
else if (Current.isOneOf(tok::exclaim, tok::tilde)) {
914 Current.Type = TT_UnaryOperator;
915 }
else if (Current.is(tok::question)) {
917 Line.MustBeDeclaration) {
920 Current.Type = TT_JsTypeOptionalQuestion;
922 Current.Type = TT_ConditionalExpr;
924 }
else if (Current.isBinaryOperator() &&
925 (!Current.Previous || Current.Previous->isNot(tok::l_square))) {
926 Current.Type = TT_BinaryOperator;
927 }
else if (Current.is(tok::comment)) {
928 if (Current.TokenText.startswith(
"/*")) {
929 if (Current.TokenText.endswith(
"*/"))
930 Current.Type = TT_BlockComment;
934 Current.Tok.setKind(tok::unknown);
936 Current.Type = TT_LineComment;
938 }
else if (Current.is(tok::r_paren)) {
939 if (rParenEndsCast(Current))
940 Current.Type = TT_CastRParen;
941 if (Current.MatchingParen && Current.Next &&
942 !Current.Next->isBinaryOperator() &&
943 !Current.Next->isOneOf(tok::semi, tok::colon, tok::l_brace))
944 if (FormatToken *BeforeParen = Current.MatchingParen->Previous)
945 if (BeforeParen->is(tok::identifier) &&
946 BeforeParen->TokenText == BeforeParen->TokenText.upper() &&
947 (!BeforeParen->Previous ||
948 BeforeParen->Previous->ClosesTemplateDeclaration))
949 Current.Type = TT_FunctionAnnotationRParen;
950 }
else if (Current.is(tok::at) && Current.Next) {
951 if (Current.Next->isStringLiteral()) {
952 Current.Type = TT_ObjCStringLiteral;
954 switch (Current.Next->Tok.getObjCKeywordID()) {
955 case tok::objc_interface:
956 case tok::objc_implementation:
957 case tok::objc_protocol:
958 Current.Type = TT_ObjCDecl;
960 case tok::objc_property:
961 Current.Type = TT_ObjCProperty;
967 }
else if (Current.is(tok::period)) {
968 FormatToken *PreviousNoComment = Current.getPreviousNonComment();
969 if (PreviousNoComment &&
970 PreviousNoComment->isOneOf(tok::comma, tok::l_brace))
971 Current.Type = TT_DesignatedInitializerPeriod;
973 Current.Previous->isOneOf(TT_JavaAnnotation,
974 TT_LeadingJavaAnnotation)) {
975 Current.Type = Current.Previous->Type;
977 }
else if (Current.isOneOf(tok::identifier, tok::kw_const) &&
979 !Current.Previous->isOneOf(tok::equal, tok::at) &&
980 Line.MightBeFunctionDecl &&
Contexts.size() == 1) {
983 Current.Type = TT_TrailingAnnotation;
987 if (Current.Previous->is(tok::at) &&
988 Current.isNot(Keywords.kw_interface)) {
989 const FormatToken &AtToken = *Current.Previous;
990 const FormatToken *
Previous = AtToken.getPreviousNonComment();
991 if (!Previous || Previous->is(TT_LeadingJavaAnnotation))
992 Current.Type = TT_LeadingJavaAnnotation;
994 Current.Type = TT_JavaAnnotation;
995 }
else if (Current.Previous->is(tok::period) &&
996 Current.Previous->isOneOf(TT_JavaAnnotation,
997 TT_LeadingJavaAnnotation)) {
998 Current.Type = Current.Previous->Type;
1008 bool isStartOfName(
const FormatToken &Tok) {
1009 if (Tok.isNot(tok::identifier) || !Tok.Previous)
1012 if (Tok.Previous->isOneOf(TT_LeadingJavaAnnotation, Keywords.kw_instanceof))
1016 FormatToken *PreviousNotConst = Tok.Previous;
1017 while (PreviousNotConst && PreviousNotConst->is(tok::kw_const))
1018 PreviousNotConst = PreviousNotConst->Previous;
1020 if (!PreviousNotConst)
1023 bool IsPPKeyword = PreviousNotConst->is(tok::identifier) &&
1024 PreviousNotConst->Previous &&
1025 PreviousNotConst->Previous->is(tok::hash);
1027 if (PreviousNotConst->is(TT_TemplateCloser))
1028 return PreviousNotConst && PreviousNotConst->MatchingParen &&
1029 PreviousNotConst->MatchingParen->Previous &&
1030 PreviousNotConst->MatchingParen->Previous->isNot(tok::period) &&
1031 PreviousNotConst->MatchingParen->Previous->isNot(tok::kw_template);
1033 if (PreviousNotConst->is(tok::r_paren) && PreviousNotConst->MatchingParen &&
1034 PreviousNotConst->MatchingParen->Previous &&
1035 PreviousNotConst->MatchingParen->Previous->is(tok::kw_decltype))
1038 return (!IsPPKeyword && PreviousNotConst->is(tok::identifier)) ||
1039 PreviousNotConst->is(TT_PointerOrReference) ||
1040 PreviousNotConst->isSimpleTypeSpecifier();
1044 bool rParenEndsCast(
const FormatToken &Tok) {
1045 FormatToken *LeftOfParens =
nullptr;
1046 if (Tok.MatchingParen)
1047 LeftOfParens = Tok.MatchingParen->getPreviousNonComment();
1048 if (LeftOfParens && LeftOfParens->is(tok::r_paren) &&
1049 LeftOfParens->MatchingParen)
1050 LeftOfParens = LeftOfParens->MatchingParen->Previous;
1051 if (LeftOfParens && LeftOfParens->is(tok::r_square) &&
1052 LeftOfParens->MatchingParen &&
1053 LeftOfParens->MatchingParen->is(TT_LambdaLSquare))
1056 if (Tok.Next->is(tok::question))
1059 Tok.Next->is(Keywords.kw_in))
1064 bool IsCast =
false;
1065 bool ParensAreEmpty = Tok.Previous == Tok.MatchingParen;
1066 bool ParensAreType =
1068 Tok.Previous->isOneOf(TT_PointerOrReference, TT_TemplateCloser) ||
1069 Tok.Previous->isSimpleTypeSpecifier();
1070 bool ParensCouldEndDecl =
1071 Tok.Next && Tok.Next->isOneOf(tok::equal, tok::semi, tok::l_brace);
1072 bool IsSizeOfOrAlignOf =
1073 LeftOfParens && LeftOfParens->isOneOf(tok::kw_sizeof, tok::kw_alignof);
1074 if (ParensAreType && !ParensCouldEndDecl && !IsSizeOfOrAlignOf &&
1077 else if (Tok.Next && Tok.Next->isNot(tok::string_literal) &&
1078 (Tok.Next->Tok.isLiteral() ||
1079 Tok.Next->isOneOf(tok::kw_sizeof, tok::kw_alignof)))
1083 else if (LeftOfParens && Tok.Next &&
1084 (LeftOfParens->Tok.getIdentifierInfo() ==
nullptr ||
1085 LeftOfParens->isOneOf(tok::kw_return, tok::kw_case)) &&
1086 !LeftOfParens->isOneOf(TT_OverloadedOperator, tok::at,
1087 TT_TemplateCloser)) {
1088 if (Tok.Next->isOneOf(tok::identifier, tok::numeric_constant)) {
1092 FormatToken *Prev = Tok.Previous;
1093 if (Prev && Prev->isOneOf(tok::amp, tok::star))
1094 Prev = Prev->Previous;
1096 if (Prev && Tok.Next && Tok.Next->Next) {
1097 bool NextIsUnary = Tok.Next->isUnaryOperator() ||
1098 Tok.Next->isOneOf(tok::amp, tok::star);
1100 NextIsUnary && !Tok.Next->is(tok::plus) &&
1101 Tok.Next->Next->isOneOf(tok::identifier, tok::numeric_constant);
1104 for (; Prev != Tok.MatchingParen; Prev = Prev->Previous) {
1106 !Prev->isOneOf(tok::kw_const, tok::identifier, tok::coloncolon)) {
1113 return IsCast && !ParensAreEmpty;
1120 return TT_BinaryOperator;
1122 const FormatToken *PrevToken = Tok.getPreviousNonComment();
1124 return TT_UnaryOperator;
1126 const FormatToken *NextToken = Tok.getNextNonComment();
1127 if (!NextToken || NextToken->is(tok::arrow) ||
1128 (NextToken->is(tok::l_brace) && !NextToken->getNextNonComment()))
1131 if (PrevToken->is(tok::coloncolon))
1132 return TT_PointerOrReference;
1134 if (PrevToken->isOneOf(tok::l_paren, tok::l_square, tok::l_brace,
1135 tok::comma, tok::semi, tok::kw_return, tok::colon,
1136 tok::equal, tok::kw_delete, tok::kw_sizeof) ||
1137 PrevToken->isOneOf(TT_BinaryOperator, TT_ConditionalExpr,
1138 TT_UnaryOperator, TT_CastRParen))
1139 return TT_UnaryOperator;
1141 if (NextToken->is(tok::l_square) && NextToken->isNot(TT_LambdaLSquare))
1142 return TT_PointerOrReference;
1143 if (NextToken->isOneOf(tok::kw_operator, tok::comma, tok::semi))
1144 return TT_PointerOrReference;
1146 if (PrevToken->is(tok::r_paren) && PrevToken->MatchingParen &&
1147 PrevToken->MatchingParen->Previous &&
1148 PrevToken->MatchingParen->Previous->isOneOf(tok::kw_typeof,
1150 return TT_PointerOrReference;
1152 if (PrevToken->Tok.isLiteral() ||
1153 PrevToken->isOneOf(tok::r_paren, tok::r_square, tok::kw_true,
1154 tok::kw_false, tok::r_brace) ||
1155 NextToken->Tok.isLiteral() ||
1156 NextToken->isOneOf(tok::kw_true, tok::kw_false) ||
1157 NextToken->isUnaryOperator() ||
1161 (InTemplateArgument && NextToken->Tok.isAnyIdentifier()))
1162 return TT_BinaryOperator;
1165 if (Tok.is(tok::ampamp) && NextToken && NextToken->is(tok::l_paren))
1166 return TT_BinaryOperator;
1170 const FormatToken *NextNextToken = NextToken->getNextNonComment();
1171 if (NextNextToken && NextNextToken->is(tok::arrow))
1172 return TT_BinaryOperator;
1176 if (IsExpression && !
Contexts.back().CaretFound)
1177 return TT_BinaryOperator;
1179 return TT_PointerOrReference;
1182 TokenType determinePlusMinusCaretUsage(
const FormatToken &Tok) {
1183 const FormatToken *PrevToken = Tok.getPreviousNonComment();
1184 if (!PrevToken || PrevToken->is(TT_CastRParen))
1185 return TT_UnaryOperator;
1188 if (PrevToken->isOneOf(tok::equal, tok::l_paren, tok::comma, tok::l_square,
1189 tok::question, tok::colon, tok::kw_return,
1190 tok::kw_case, tok::at, tok::l_brace))
1191 return TT_UnaryOperator;
1194 if (PrevToken->is(TT_BinaryOperator))
1195 return TT_UnaryOperator;
1198 return TT_BinaryOperator;
1202 TokenType determineIncrementUsage(
const FormatToken &Tok) {
1203 const FormatToken *PrevToken = Tok.getPreviousNonComment();
1204 if (!PrevToken || PrevToken->is(TT_CastRParen))
1205 return TT_UnaryOperator;
1206 if (PrevToken->isOneOf(tok::r_paren, tok::r_square, tok::identifier))
1207 return TT_TrailingUnaryOperator;
1209 return TT_UnaryOperator;
1232 class ExpressionParser {
1234 ExpressionParser(
const FormatStyle &Style,
const AdditionalKeywords &Keywords,
1235 AnnotatedLine &Line)
1239 void parse(
int Precedence = 0) {
1242 while (Current && (Current->is(tok::kw_return) ||
1243 (Current->is(tok::colon) &&
1244 Current->isOneOf(TT_ObjCMethodExpr, TT_DictLiteral))))
1247 if (!Current || Precedence > PrecedenceArrowAndPeriod)
1252 parseConditionalExpr();
1258 if (Precedence == PrecedenceUnaryOperator) {
1259 parseUnaryOperator();
1264 FormatToken *LatestOperator =
nullptr;
1265 unsigned OperatorIndex = 0;
1269 parse(Precedence + 1);
1271 int CurrentPrecedence = getCurrentPrecedence();
1273 if (Current && Current->is(TT_SelectorName) &&
1274 Precedence == CurrentPrecedence) {
1276 addFakeParenthesis(Start,
prec::Level(Precedence));
1282 if (!Current || (Current->closesScope() && Current->MatchingParen) ||
1283 (CurrentPrecedence != -1 && CurrentPrecedence < Precedence) ||
1290 if (Current->opensScope()) {
1291 while (Current && !Current->closesScope()) {
1298 if (CurrentPrecedence == Precedence) {
1300 Current->OperatorIndex = OperatorIndex;
1303 next(Precedence > 0);
1307 if (LatestOperator && (Current || Precedence > 0)) {
1308 LatestOperator->LastOperator =
true;
1309 if (Precedence == PrecedenceArrowAndPeriod) {
1313 addFakeParenthesis(Start,
prec::Level(Precedence));
1321 int getCurrentPrecedence() {
1323 const FormatToken *NextNonComment = Current->getNextNonComment();
1324 if (Current->is(TT_ConditionalExpr))
1326 if (NextNonComment && NextNonComment->is(tok::colon) &&
1327 NextNonComment->is(TT_DictLiteral))
1329 if (Current->is(TT_LambdaArrow))
1331 if (Current->is(TT_JsFatArrow))
1333 if (Current->isOneOf(tok::semi, TT_InlineASMColon, TT_SelectorName,
1334 TT_JsComputedPropertyName) ||
1335 (Current->is(tok::comment) && NextNonComment &&
1336 NextNonComment->is(TT_SelectorName)))
1338 if (Current->is(TT_RangeBasedForLoopColon))
1342 Current->is(Keywords.kw_instanceof))
1344 if (Current->is(TT_BinaryOperator) || Current->is(tok::comma))
1345 return Current->getPrecedence();
1346 if (Current->isOneOf(tok::period, tok::arrow))
1347 return PrecedenceArrowAndPeriod;
1349 Current->isOneOf(Keywords.kw_extends, Keywords.kw_implements,
1350 Keywords.kw_throws))
1356 void addFakeParenthesis(FormatToken *Start,
prec::Level Precedence) {
1357 Start->FakeLParens.push_back(Precedence);
1359 Start->StartsBinaryExpression =
true;
1361 FormatToken *Previous = Current->Previous;
1362 while (Previous->is(tok::comment) && Previous->Previous)
1363 Previous = Previous->Previous;
1364 ++Previous->FakeRParens;
1366 Previous->EndsBinaryExpression =
true;
1372 void parseUnaryOperator() {
1373 if (!Current || Current->isNot(TT_UnaryOperator)) {
1374 parse(PrecedenceArrowAndPeriod);
1380 parseUnaryOperator();
1386 void parseConditionalExpr() {
1387 while (Current && Current->isTrailingComment()) {
1392 if (!Current || !Current->is(tok::question))
1396 if (!Current || Current->isNot(TT_ConditionalExpr))
1403 void next(
bool SkipPastLeadingComments =
true) {
1405 Current = Current->Next;
1407 (Current->NewlinesBefore == 0 || SkipPastLeadingComments) &&
1408 Current->isTrailingComment())
1409 Current = Current->Next;
1412 const FormatStyle &
Style;
1413 const AdditionalKeywords &
Keywords;
1425 if (NextNonCommentLine && (*I)->
First->
is(tok::comment) &&
1426 (*I)->First->Next ==
nullptr)
1429 NextNonCommentLine = (*I)->
First->
isNot(tok::r_brace) ? (*I) :
nullptr;
1441 AnnotatingParser
Parser(Style, Line, Keywords);
1442 Line.
Type = Parser.parseLine();
1446 ExpressionParser ExprParser(Style, Keywords, Line);
1467 if (Next->
is(TT_TemplateOpener)) {
1469 }
else if (Next->
is(tok::coloncolon)) {
1471 if (!Next || !Next->
is(tok::identifier))
1473 }
else if (Next->
is(tok::l_paren)) {
1481 assert(Next->
is(tok::l_paren));
1486 if (Tok->is(tok::kw_const) || Tok->isSimpleTypeSpecifier() ||
1487 Tok->isOneOf(TT_PointerOrReference, TT_StartOfName))
1489 if (Tok->isOneOf(tok::l_brace, tok::string_literal, TT_ObjCMethodExpr) ||
1490 Tok->Tok.isLiteral())
1511 Current->
Type = TT_FunctionDeclarationName;
1512 if (Current->
is(TT_LineComment)) {
1529 if (
Parameter->isOneOf(tok::comment, tok::r_brace))
1532 if (!
Parameter->Previous->is(TT_CtorInitializerComma) &&
1540 spaceRequiredBefore(Line, *Current)) {
1548 (Style.AlwaysBreakAfterDefinitionReturnType ==
1550 Line.
Level == 0)) &&
1551 InFunctionDecl && Current->
is(TT_FunctionDeclarationName) &&
1559 unsigned ChildSize = 0;
1568 Prev->
Children[0]->First->MustBreakBefore) ||
1575 if (Current->
is(TT_CtorInitializerColon))
1576 InFunctionDecl =
false;
1582 splitPenalty(Line, *Current, InFunctionDecl);
1584 Current = Current->
Next;
1587 calculateUnbreakableTailLengths(Line);
1588 for (Current = Line.
First; Current !=
nullptr; Current = Current->
Next) {
1590 Current->
Role->precomputeFormattingInfos(Current);
1593 DEBUG({ printDebugInfo(Line); });
1596 void TokenAnnotator::calculateUnbreakableTailLengths(
AnnotatedLine &Line) {
1597 unsigned UnbreakableTailLength = 0;
1602 Current->
isOneOf(tok::comment, tok::string_literal)) {
1603 UnbreakableTailLength = 0;
1605 UnbreakableTailLength +=
1612 unsigned TokenAnnotator::splitPenalty(
const AnnotatedLine &Line,
1613 const FormatToken &Tok,
1614 bool InFunctionDecl) {
1615 const FormatToken &Left = *Tok.
Previous;
1616 const FormatToken &Right = Tok;
1618 if (Left.is(tok::semi))
1622 if (Right.isOneOf(Keywords.kw_extends, Keywords.kw_throws))
1624 if (Right.is(Keywords.kw_implements))
1626 if (Left.is(tok::comma) && Left.NestingLevel == 0)
1629 if (Right.is(Keywords.kw_function) && Left.isNot(tok::comma))
1631 if (Left.is(TT_JsTypeColon))
1635 if (Left.is(tok::comma) || (Right.is(tok::identifier) && Right.Next &&
1636 Right.Next->is(TT_DictLiteral)))
1638 if (Right.is(tok::l_square)) {
1642 if (Right.is(TT_LambdaLSquare) && Left.is(tok::equal))
1644 if (!Right.isOneOf(TT_ObjCMethodExpr, TT_LambdaLSquare,
1645 TT_ArrayInitializerLSquare))
1649 if (Right.isOneOf(TT_StartOfName, TT_FunctionDeclarationName) ||
1650 Right.is(tok::kw_operator)) {
1651 if (Line.startsWith(tok::kw_for) && Right.PartOfMultiVariableDeclStmt)
1653 if (Left.is(TT_StartOfName))
1655 if (InFunctionDecl && Right.NestingLevel == 0)
1656 return Style.PenaltyReturnTypeOnItsOwnLine;
1659 if (Right.is(TT_PointerOrReference))
1661 if (Right.is(TT_LambdaArrow))
1663 if (Left.is(tok::equal) && Right.is(tok::l_brace))
1665 if (Left.is(TT_CastRParen))
1667 if (Left.is(tok::coloncolon) ||
1670 if (Left.isOneOf(tok::kw_class, tok::kw_struct))
1673 if (Left.isOneOf(TT_RangeBasedForLoopColon, TT_InheritanceColon))
1676 if (Right.isMemberAccess()) {
1677 if (Left.is(tok::r_paren) && Left.MatchingParen &&
1678 Left.MatchingParen->ParameterCount > 0)
1683 if (Right.is(TT_TrailingAnnotation) &&
1684 (!Right.Next || Right.Next->isNot(tok::l_paren))) {
1687 if (Line.startsWith(TT_ObjCMethodSpecifier))
1694 bool is_short_annotation = Right.
TokenText.size() < 10;
1695 return (Left.is(tok::r_paren) ? 100 : 120) + (is_short_annotation ? 50 : 0);
1699 if (Line.startsWith(tok::kw_for) && Left.is(tok::equal))
1704 if (Right.is(TT_SelectorName))
1706 if (Left.is(tok::colon) && Left.is(TT_ObjCMethodExpr))
1707 return Line.MightBeFunctionDecl ? 50 : 500;
1709 if (Left.is(tok::l_paren) && InFunctionDecl && Style.AlignAfterOpenBracket)
1711 if (Left.is(tok::l_paren) && Left.Previous &&
1712 Left.Previous->isOneOf(tok::kw_if, tok::kw_for))
1714 if (Left.is(tok::equal) && InFunctionDecl)
1716 if (Right.is(tok::r_brace))
1718 if (Left.is(TT_TemplateOpener))
1720 if (Left.opensScope()) {
1721 if (!Style.AlignAfterOpenBracket)
1723 return Left.ParameterCount > 1 ? Style.PenaltyBreakBeforeFirstCallParameter
1726 if (Left.is(TT_JavaAnnotation))
1729 if (Right.is(tok::lessless)) {
1730 if (Left.is(tok::string_literal) &&
1731 (!Right.LastOperator || Right.OperatorIndex != 1)) {
1732 StringRef Content = Left.TokenText;
1733 if (Content.startswith(
"\""))
1734 Content = Content.drop_front(1);
1735 if (Content.endswith(
"\""))
1736 Content = Content.drop_back(1);
1737 Content = Content.trim();
1738 if (Content.size() > 1 &&
1739 (Content.back() ==
':' || Content.back() ==
'='))
1744 if (Left.is(TT_ConditionalExpr))
1749 Level = Right.getPrecedence();
1756 bool TokenAnnotator::spaceRequiredBetween(
const AnnotatedLine &Line,
1757 const FormatToken &Left,
1758 const FormatToken &Right) {
1759 if (Left.is(tok::kw_return) && Right.isNot(tok::semi))
1762 Left.Tok.getObjCKeywordID() == tok::objc_property)
1764 if (Right.is(tok::hashhash))
1765 return Left.is(tok::hash);
1766 if (Left.isOneOf(tok::hashhash, tok::hash))
1767 return Right.is(tok::hash);
1768 if (Left.is(tok::l_paren) && Right.is(tok::r_paren))
1769 return Style.SpaceInEmptyParentheses;
1770 if (Left.is(tok::l_paren) || Right.is(tok::r_paren))
1771 return (Right.is(TT_CastRParen) ||
1772 (Left.MatchingParen && Left.MatchingParen->is(TT_CastRParen)))
1773 ? Style.SpacesInCStyleCastParentheses
1774 : Style.SpacesInParentheses;
1775 if (Right.isOneOf(tok::semi, tok::comma))
1777 if (Right.is(tok::less) &&
1778 (Left.is(tok::kw_template) ||
1779 (Line.Type ==
LT_ObjCDecl && Style.ObjCSpaceBeforeProtocolList)))
1781 if (Left.isOneOf(tok::exclaim, tok::tilde))
1783 if (Left.is(tok::at) &&
1784 Right.isOneOf(tok::identifier, tok::string_literal, tok::char_constant,
1785 tok::numeric_constant, tok::l_paren, tok::l_brace,
1786 tok::kw_true, tok::kw_false))
1788 if (Left.is(tok::coloncolon))
1790 if (Left.is(tok::less) || Right.isOneOf(tok::greater, tok::less))
1792 if (Right.is(tok::ellipsis))
1793 return Left.Tok.isLiteral();
1794 if (Left.is(tok::l_square) && Right.is(tok::amp))
1796 if (Right.is(TT_PointerOrReference))
1797 return !(Left.is(tok::r_paren) && Left.MatchingParen &&
1798 (Left.MatchingParen->is(TT_OverloadedOperatorLParen) ||
1799 (Left.MatchingParen->Previous &&
1800 Left.MatchingParen->Previous->is(
1801 TT_FunctionDeclarationName)))) &&
1802 (Left.Tok.isLiteral() ||
1803 (!Left.isOneOf(TT_PointerOrReference, tok::l_paren) &&
1805 Line.IsMultiVariableDeclStmt)));
1806 if (Right.is(TT_FunctionTypeLParen) && Left.isNot(tok::l_paren) &&
1807 (!Left.is(TT_PointerOrReference) ||
1809 !Line.IsMultiVariableDeclStmt)))
1811 if (Left.is(TT_PointerOrReference))
1812 return Right.Tok.isLiteral() || Right.is(TT_BlockComment) ||
1813 (Right.is(tok::l_brace) && Right.BlockKind ==
BK_Block) ||
1814 (!Right.isOneOf(TT_PointerOrReference, TT_ArraySubscriptLSquare,
1817 !Line.IsMultiVariableDeclStmt) &&
1819 !Left.Previous->isOneOf(tok::l_paren, tok::coloncolon));
1820 if (Right.is(tok::star) && Left.is(tok::l_paren))
1822 if (Left.is(tok::l_square))
1823 return (Left.is(TT_ArrayInitializerLSquare) &&
1824 Style.SpacesInContainerLiterals && Right.isNot(tok::r_square)) ||
1825 (Left.is(TT_ArraySubscriptLSquare) && Style.SpacesInSquareBrackets &&
1826 Right.isNot(tok::r_square));
1827 if (Right.is(tok::r_square))
1828 return Right.MatchingParen &&
1829 ((Style.SpacesInContainerLiterals &&
1830 Right.MatchingParen->is(TT_ArrayInitializerLSquare)) ||
1831 (Style.SpacesInSquareBrackets &&
1832 Right.MatchingParen->is(TT_ArraySubscriptLSquare)));
1833 if (Right.is(tok::l_square) &&
1834 !Right.isOneOf(TT_ObjCMethodExpr, TT_LambdaLSquare) &&
1835 !Left.isOneOf(tok::numeric_constant, TT_DictLiteral))
1837 if (Left.is(tok::colon))
1838 return !Left.is(TT_ObjCMethodExpr);
1839 if (Left.is(tok::l_brace) && Right.is(tok::r_brace))
1840 return !Left.Children.empty();
1841 if ((Left.is(tok::l_brace) && Left.BlockKind !=
BK_Block) ||
1842 (Right.is(tok::r_brace) && Right.MatchingParen &&
1843 Right.MatchingParen->BlockKind !=
BK_Block))
1844 return !Style.Cpp11BracedListStyle;
1845 if (Left.is(TT_BlockComment))
1846 return !Left.TokenText.endswith(
"=*/");
1847 if (Right.is(tok::l_paren)) {
1848 if (Left.is(tok::r_paren) && Left.is(TT_AttributeParen))
1850 return Line.Type ==
LT_ObjCDecl || Left.is(tok::semi) ||
1852 (Left.isOneOf(tok::kw_if, tok::kw_for, tok::kw_while,
1853 tok::kw_switch, tok::kw_case, TT_ForEachMacro) ||
1854 (Left.isOneOf(tok::kw_try, Keywords.kw___except, tok::kw_catch,
1855 tok::kw_new, tok::kw_delete) &&
1856 (!Left.Previous || Left.Previous->isNot(tok::period))))) ||
1858 (Left.is(tok::identifier) || Left.isFunctionLikeKeyword() ||
1859 Left.is(tok::r_paren)) &&
1862 if (Left.is(tok::at) && Right.Tok.getObjCKeywordID() != tok::objc_not_keyword)
1864 if (Right.is(TT_UnaryOperator))
1865 return !Left.isOneOf(tok::l_paren, tok::l_square, tok::at) &&
1866 (Left.isNot(tok::colon) || Left.isNot(TT_ObjCMethodExpr));
1867 if ((Left.isOneOf(tok::identifier, tok::greater, tok::r_square,
1869 Left.isSimpleTypeSpecifier()) &&
1870 Right.is(tok::l_brace) && Right.getNextNonComment() &&
1873 if (Left.is(tok::period) || Right.is(tok::period))
1875 if (Right.is(tok::hash) && Left.is(tok::identifier) && Left.TokenText ==
"L")
1877 if (Left.is(TT_TemplateCloser) && Left.MatchingParen &&
1878 Left.MatchingParen->Previous &&
1879 Left.MatchingParen->Previous->is(tok::period))
1882 if (Left.is(TT_TemplateCloser) && Right.is(tok::l_square))
1887 bool TokenAnnotator::spaceRequiredBefore(
const AnnotatedLine &Line,
1888 const FormatToken &Right) {
1889 const FormatToken &Left = *Right.Previous;
1890 if (Right.Tok.getIdentifierInfo() && Left.Tok.getIdentifierInfo())
1893 if (Left.is(tok::kw_operator))
1894 return Right.is(tok::coloncolon);
1896 if (Right.is(tok::period) &&
1897 Left.isOneOf(Keywords.kw_optional, Keywords.kw_required,
1898 Keywords.kw_repeated))
1900 if (Right.is(tok::l_paren) &&
1901 Left.isOneOf(Keywords.kw_returns, Keywords.kw_option))
1904 if (Left.isOneOf(Keywords.kw_var, TT_JsFatArrow))
1906 if (Right.isOneOf(TT_JsTypeColon, TT_JsTypeOptionalQuestion))
1908 if ((Left.is(tok::l_brace) || Right.is(tok::r_brace)) &&
1909 Line.First->isOneOf(Keywords.kw_import, tok::kw_export))
1911 if (Left.is(tok::ellipsis))
1913 if (Left.is(TT_TemplateCloser) &&
1914 !Right.isOneOf(tok::equal, tok::l_brace, tok::comma, tok::l_square,
1915 Keywords.kw_implements, Keywords.kw_extends))
1921 if (Left.is(tok::r_square) && Right.is(tok::l_brace))
1923 if (Left.is(Keywords.kw_synchronized) && Right.is(tok::l_paren))
1925 if ((Left.isOneOf(tok::kw_static, tok::kw_public, tok::kw_private,
1926 tok::kw_protected) ||
1927 Left.isOneOf(Keywords.kw_final, Keywords.kw_abstract,
1928 Keywords.kw_native)) &&
1929 Right.is(TT_TemplateOpener))
1932 if (Left.is(TT_ImplicitStringLiteral))
1933 return Right.WhitespaceRange.getBegin() != Right.WhitespaceRange.getEnd();
1935 if (Left.is(TT_ObjCMethodSpecifier))
1937 if (Left.is(tok::r_paren) && Right.is(tok::identifier))
1942 (Right.is(tok::equal) || Left.is(tok::equal)))
1945 if (Right.isOneOf(TT_TrailingReturnArrow, TT_LambdaArrow) ||
1946 Left.isOneOf(TT_TrailingReturnArrow, TT_LambdaArrow))
1948 if (Left.is(tok::comma))
1950 if (Right.is(tok::comma))
1952 if (Right.isOneOf(TT_CtorInitializerColon, TT_ObjCBlockLParen))
1954 if (Right.is(TT_OverloadedOperatorLParen))
1956 if (Right.is(tok::colon)) {
1957 if (Line.First->isOneOf(tok::kw_case, tok::kw_default) ||
1958 !Right.getNextNonComment() || Right.getNextNonComment()->is(tok::semi))
1960 if (Right.is(TT_ObjCMethodExpr))
1962 if (Left.is(tok::question))
1964 if (Right.is(TT_InlineASMColon) && Left.is(tok::coloncolon))
1966 if (Right.is(TT_DictLiteral))
1967 return Style.SpacesInContainerLiterals;
1970 if (Left.is(TT_UnaryOperator))
1971 return Right.is(TT_BinaryOperator);
1975 if (Left.is(TT_CastRParen))
1976 return Style.SpaceAfterCStyleCast ||
1977 Right.isOneOf(TT_BinaryOperator, TT_SelectorName);
1979 if (Left.is(tok::greater) && Right.is(tok::greater))
1980 return Right.is(TT_TemplateCloser) && Left.is(TT_TemplateCloser) &&
1982 if (Right.isOneOf(tok::arrow, tok::period, tok::arrowstar, tok::periodstar) ||
1983 Left.isOneOf(tok::arrow, tok::period, tok::arrowstar, tok::periodstar))
1985 if (!Style.SpaceBeforeAssignmentOperators &&
1988 if (Right.is(tok::coloncolon) && Left.isNot(tok::l_brace))
1989 return (Left.is(TT_TemplateOpener) &&
1991 !(Left.isOneOf(tok::identifier, tok::l_paren, tok::r_paren) ||
1992 Left.isOneOf(TT_TemplateCloser, TT_TemplateOpener));
1993 if ((Left.is(TT_TemplateOpener)) != (Right.is(TT_TemplateCloser)))
1994 return Style.SpacesInAngles;
1995 if ((Right.is(TT_BinaryOperator) && !Left.is(tok::l_paren)) ||
1996 Left.isOneOf(TT_BinaryOperator, TT_ConditionalExpr))
1998 if (Left.is(TT_TemplateCloser) && Right.is(tok::l_paren) &&
1999 Right.isNot(TT_FunctionTypeLParen))
2001 if (Right.is(TT_TemplateOpener) && Left.is(tok::r_paren) &&
2002 Left.MatchingParen && Left.MatchingParen->is(TT_OverloadedOperatorLParen))
2004 if (Right.is(tok::less) && Left.isNot(tok::l_paren) &&
2005 Line.startsWith(tok::hash))
2007 if (Right.is(TT_TrailingUnaryOperator))
2009 if (Left.is(TT_RegexLiteral))
2011 return spaceRequiredBetween(Line, Left, Right);
2017 !Tok.
isOneOf(TT_ObjCBlockLBrace, TT_DictLiteral);
2020 bool TokenAnnotator::mustBreakBefore(
const AnnotatedLine &Line,
2021 const FormatToken &Right) {
2022 const FormatToken &Left = *Right.Previous;
2023 if (Right.NewlinesBefore > 1)
2028 if (Right.is(tok::char_constant) && Left.is(tok::plus) && Left.Previous &&
2029 Left.Previous->is(tok::char_constant))
2031 if (Left.is(TT_DictLiteral) && Left.is(tok::l_brace) && Line.Level == 0 &&
2032 Left.Previous && Left.Previous->is(tok::equal) &&
2033 Line.First->isOneOf(tok::identifier, Keywords.kw_import, tok::kw_export,
2036 !Line.startsWith(Keywords.kw_var))
2040 if (Left.is(tok::l_brace) && Line.Level == 0 &&
2041 (Line.startsWith(tok::kw_enum) ||
2042 Line.startsWith(tok::kw_export, tok::kw_enum)))
2046 if (Right.is(tok::r_brace) && Left.is(tok::l_brace) &&
2047 !Left.Children.empty())
2050 (Left.NestingLevel == 0 && Line.Level == 0 &&
2051 Style.AllowShortFunctionsOnASingleLine ==
2054 if (Right.is(tok::plus) && Left.is(tok::string_literal) && Right.Next &&
2055 Right.Next->is(tok::string_literal))
2062 const FormatToken *BeforeClosingBrace =
nullptr;
2063 if (Left.isOneOf(tok::l_brace, TT_ArrayInitializerLSquare) &&
2064 Left.BlockKind !=
BK_Block && Left.MatchingParen)
2065 BeforeClosingBrace = Left.MatchingParen->Previous;
2066 else if (Right.MatchingParen &&
2067 Right.MatchingParen->isOneOf(tok::l_brace,
2068 TT_ArrayInitializerLSquare))
2069 BeforeClosingBrace = &Left;
2070 if (BeforeClosingBrace && (BeforeClosingBrace->is(tok::comma) ||
2071 BeforeClosingBrace->isTrailingComment()))
2074 if (Right.is(tok::comment))
2076 Left.isNot(TT_CtorInitializerColon) &&
2077 (Right.NewlinesBefore > 0 && Right.HasUnescapedNewline);
2078 if (Left.isTrailingComment())
2080 if (Left.isStringLiteral() &&
2081 (Right.isStringLiteral() || Right.is(TT_ObjCStringLiteral)))
2083 if (Right.Previous->IsUnterminatedLiteral)
2085 if (Right.is(tok::lessless) && Right.Next &&
2086 Right.Previous->is(tok::string_literal) &&
2087 Right.Next->is(tok::string_literal))
2089 if (Right.Previous->ClosesTemplateDeclaration &&
2090 Right.Previous->MatchingParen &&
2091 Right.Previous->MatchingParen->NestingLevel == 0 &&
2092 Style.AlwaysBreakTemplateDeclarations)
2094 if ((Right.isOneOf(TT_CtorInitializerComma, TT_CtorInitializerColon)) &&
2095 Style.BreakConstructorInitializersBeforeComma &&
2096 !Style.ConstructorInitializerAllOnOneLineOrOnePerLine)
2098 if (Right.is(tok::string_literal) && Right.TokenText.startswith(
"R\""))
2102 return Right.NewlinesBefore > 0;
2103 if (Right.Previous->is(tok::l_brace) && Right.NestingLevel == 1 &&
2107 if (Right.is(TT_InlineASMBrace))
2108 return Right.HasUnescapedNewline;
2113 Line.startsWith(tok::kw_enum));
2115 Right.is(TT_SelectorName))
2117 if (Left.is(TT_ObjCBlockLBrace) && !Style.AllowShortBlocksOnASingleLine)
2122 Left.is(TT_LeadingJavaAnnotation) &&
2123 Right.isNot(TT_LeadingJavaAnnotation) && Right.isNot(tok::l_paren) &&
2124 Line.Last->is(tok::l_brace))
2130 bool TokenAnnotator::canBreakBefore(
const AnnotatedLine &Line,
2131 const FormatToken &Right) {
2132 const FormatToken &Left = *Right.Previous;
2136 if (Left.isOneOf(Keywords.kw_throws, Keywords.kw_extends,
2137 Keywords.kw_implements))
2139 if (Right.isOneOf(Keywords.kw_throws, Keywords.kw_extends,
2140 Keywords.kw_implements))
2143 if (Left.is(TT_JsFatArrow) && Right.is(tok::l_brace))
2145 if (Left.is(TT_JsTypeColon))
2149 if (Left.is(tok::at))
2151 if (Left.Tok.getObjCKeywordID() == tok::objc_interface)
2153 if (Left.isOneOf(TT_JavaAnnotation, TT_LeadingJavaAnnotation))
2154 return !Right.is(tok::l_paren);
2155 if (Right.is(TT_PointerOrReference))
2156 return Line.IsMultiVariableDeclStmt ||
2158 (!Right.Next || Right.Next->isNot(TT_FunctionDeclarationName)));
2159 if (Right.isOneOf(TT_StartOfName, TT_FunctionDeclarationName) ||
2160 Right.is(tok::kw_operator))
2162 if (Left.is(TT_PointerOrReference))
2164 if (Right.isTrailingComment())
2171 if (Left.is(tok::question) && Right.is(tok::colon))
2173 if (Right.is(TT_ConditionalExpr) || Right.is(tok::question))
2174 return Style.BreakBeforeTernaryOperators;
2175 if (Left.is(TT_ConditionalExpr) || Left.is(tok::question))
2176 return !Style.BreakBeforeTernaryOperators;
2177 if (Right.is(TT_InheritanceColon))
2179 if (Right.is(tok::colon) &&
2180 !Right.isOneOf(TT_CtorInitializerColon, TT_InlineASMColon))
2182 if (Left.is(tok::colon) && (Left.isOneOf(TT_DictLiteral, TT_ObjCMethodExpr)))
2184 if (Right.is(TT_SelectorName) || (Right.is(tok::identifier) && Right.Next &&
2185 Right.Next->is(TT_ObjCMethodExpr)))
2186 return Left.isNot(tok::period);
2189 if (Left.ClosesTemplateDeclaration)
2191 if (Right.isOneOf(TT_RangeBasedForLoopColon, TT_OverloadedOperatorLParen,
2192 TT_OverloadedOperator))
2194 if (Left.is(TT_RangeBasedForLoopColon))
2196 if (Right.is(TT_RangeBasedForLoopColon))
2198 if (Left.isOneOf(TT_TemplateCloser, TT_UnaryOperator) ||
2199 Left.is(tok::kw_operator))
2201 if (Left.is(tok::equal) && !Right.isOneOf(tok::kw_default, tok::kw_delete) &&
2204 if (Left.is(tok::l_paren) && Left.is(TT_AttributeParen))
2206 if (Left.is(tok::l_paren) && Left.Previous &&
2207 (Left.Previous->isOneOf(TT_BinaryOperator, TT_CastRParen)))
2209 if (Right.is(TT_ImplicitStringLiteral))
2212 if (Right.is(tok::r_paren) || Right.is(TT_TemplateCloser))
2214 if (Right.is(tok::r_square) && Right.MatchingParen &&
2215 Right.MatchingParen->is(TT_LambdaLSquare))
2220 if (Right.is(tok::r_brace))
2221 return Right.MatchingParen && Right.MatchingParen->BlockKind ==
BK_Block;
2225 if (Left.is(TT_TrailingAnnotation))
2226 return !Right.isOneOf(tok::l_brace, tok::semi, tok::equal, tok::l_paren,
2227 tok::less, tok::coloncolon);
2229 if (Right.is(tok::kw___attribute))
2232 if (Left.is(tok::identifier) && Right.is(tok::string_literal))
2235 if (Right.is(tok::identifier) && Right.Next && Right.Next->is(TT_DictLiteral))
2238 if (Left.is(TT_CtorInitializerComma) &&
2239 Style.BreakConstructorInitializersBeforeComma)
2241 if (Right.is(TT_CtorInitializerComma) &&
2242 Style.BreakConstructorInitializersBeforeComma)
2244 if ((Left.is(tok::greater) && Right.is(tok::greater)) ||
2245 (Left.is(tok::less) && Right.is(tok::less)))
2247 if (Right.is(TT_BinaryOperator) &&
2252 if (Left.is(TT_ArrayInitializerLSquare))
2254 if (Right.is(tok::kw_typename) && Left.isNot(tok::kw_const))
2256 if ((Left.isBinaryOperator() || Left.is(TT_BinaryOperator)) &&
2257 !Left.isOneOf(tok::arrowstar, tok::lessless) &&
2262 return Left.isOneOf(tok::comma, tok::coloncolon, tok::semi, tok::l_brace,
2263 tok::kw_class, tok::kw_struct) ||
2264 Right.isMemberAccess() ||
2265 Right.isOneOf(TT_TrailingReturnArrow, TT_LambdaArrow, tok::lessless,
2266 tok::colon, tok::l_square, tok::at) ||
2267 (Left.is(tok::r_paren) &&
2268 Right.isOneOf(tok::identifier, tok::kw_const)) ||
2269 (Left.is(tok::l_paren) && !Right.is(tok::r_paren));
2272 void TokenAnnotator::printDebugInfo(
const AnnotatedLine &Line) {
2273 llvm::errs() <<
"AnnotatedTokens:\n";
2274 const FormatToken *Tok = Line.First;
2276 llvm::errs() <<
" M=" << Tok->MustBreakBefore
2277 <<
" C=" << Tok->CanBreakBefore
2279 <<
" S=" << Tok->SpacesRequiredBefore
2280 <<
" B=" << Tok->BlockParameterCount
2281 <<
" P=" << Tok->SplitPenalty <<
" Name=" << Tok->Tok.getName()
2282 <<
" L=" << Tok->TotalLength <<
" PPK=" << Tok->PackingKind
2284 for (
unsigned i = 0, e = Tok->FakeLParens.size(); i != e; ++i)
2285 llvm::errs() << Tok->FakeLParens[i] <<
"/";
2286 llvm::errs() <<
" FakeRParens=" << Tok->FakeRParens <<
"\n";
2288 assert(Tok == Line.Last);
2291 llvm::errs() <<
"----\n";
Defines the SourceManager interface.
FormatToken * CurrentToken
tok::TokenKind ContextKind
This file implements a token annotator, i.e. creates AnnotatedTokens out of FormatTokens with require...
llvm::SmallPtrSet< FormatToken *, 16 > NonTemplateLess
FormatToken * FirstStartOfName
const FormatStyle & Style
TokenKind
Provides a simple uniform namespace for tokens from all C languages.
if(T->getSizeExpr()) TRY_TO(TraverseStmt(T-> getSizeExpr()))
bool ColonIsObjCMethodExpr
const AdditionalKeywords & Keywords
SmallVector< Context, 8 > Contexts
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.