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->is(TT_OverloadedOperatorLParen)) {
123 Contexts.back().IsExpression =
false;
124 }
else if (Left->Previous &&
125 (Left->Previous->isOneOf(tok::kw_static_assert, tok::kw_decltype,
126 tok::kw_if, tok::kw_while, tok::l_paren,
128 Left->Previous->is(TT_BinaryOperator))) {
130 Contexts.back().IsExpression =
true;
131 }
else if (Left->Previous && Left->Previous->is(tok::r_square) &&
132 Left->Previous->MatchingParen &&
133 Left->Previous->MatchingParen->is(TT_LambdaLSquare)) {
135 Contexts.back().IsExpression =
false;
136 }
else if (Line.InPPDirective &&
137 (!Left->Previous || !Left->Previous->is(tok::identifier))) {
138 Contexts.back().IsExpression =
true;
141 Contexts.back().IsExpression =
false;
142 }
else if (Left->Previous && Left->Previous->is(tok::kw___attribute)) {
143 Left->Type = TT_AttributeParen;
144 }
else if (Left->Previous && Left->Previous->is(TT_ForEachMacro)) {
146 Contexts.back().IsForEachMacro =
true;
147 Contexts.back().IsExpression =
false;
148 }
else if (Left->Previous && Left->Previous->MatchingParen &&
149 Left->Previous->MatchingParen->is(TT_ObjCBlockLParen)) {
150 Contexts.back().IsExpression =
false;
151 }
else if (!Line.MustBeDeclaration && !Line.InPPDirective) {
153 Left->Previous && Left->Previous->isOneOf(tok::kw_for, tok::kw_catch);
154 Contexts.back().IsExpression = !IsForOrCatch;
157 if (StartsObjCMethodExpr) {
158 Contexts.back().ColonIsObjCMethodExpr =
true;
159 Left->Type = TT_ObjCMethodExpr;
162 bool MightBeFunctionType =
CurrentToken->isOneOf(tok::star, tok::amp) &&
164 bool HasMultipleLines =
false;
165 bool HasMultipleParametersOnALine =
false;
166 bool MightBeObjCForRangeLoop =
167 Left->Previous && Left->Previous->is(tok::kw_for);
174 FormatToken *Prev =
CurrentToken->getPreviousNonComment();
176 FormatToken *PrevPrev = Prev->getPreviousNonComment();
178 if (PrevPrev && PrevPrev->is(tok::identifier) &&
179 Prev->isOneOf(tok::star, tok::amp, tok::ampamp) &&
180 CurrentToken->is(tok::identifier) && Next->isNot(tok::equal)) {
181 Prev->Type = TT_BinaryOperator;
182 LookForDecls =
false;
187 if (
CurrentToken->Previous->is(TT_PointerOrReference) &&
190 MightBeFunctionType =
true;
192 Contexts.back().IsExpression =
true;
197 Line.MustBeDeclaration)))
198 Left->Type = TT_FunctionTypeLParen;
203 Left->Previous && Left->Previous->is(tok::l_paren)) {
207 for (FormatToken *Tok = Left; Tok !=
CurrentToken; Tok = Tok->Next) {
208 if (Tok->is(TT_BinaryOperator) &&
209 Tok->isOneOf(tok::star, tok::amp, tok::ampamp))
210 Tok->Type = TT_PointerOrReference;
214 if (StartsObjCMethodExpr) {
216 if (
Contexts.back().FirstObjCSelectorName) {
217 Contexts.back().FirstObjCSelectorName->LongestObjCSelectorName =
218 Contexts.back().LongestObjCSelectorName;
222 if (Left->is(TT_AttributeParen))
224 if (Left->Previous && Left->Previous->is(TT_JavaAnnotation))
226 if (Left->Previous && Left->Previous->is(TT_LeadingJavaAnnotation))
229 if (!HasMultipleLines)
231 else if (HasMultipleParametersOnALine)
243 Left->Type = TT_Unknown;
247 HasMultipleParametersOnALine =
true;
248 if (
CurrentToken->isOneOf(tok::kw_const, tok::kw_auto) ||
250 Contexts.back().IsExpression =
false;
252 MightBeObjCForRangeLoop =
false;
253 if (MightBeObjCForRangeLoop &&
CurrentToken->is(Keywords.kw_in))
258 Contexts.back().CanBeExpression =
true;
263 updateParameterCount(Left, Tok);
265 HasMultipleLines =
true;
278 Left->ParentBracket =
Contexts.back().ContextKind;
279 FormatToken *Parent = Left->getPreviousNonComment();
280 bool StartsObjCMethodExpr =
282 Contexts.back().CanBeExpression && Left->isNot(TT_LambdaLSquare) &&
285 Parent->isOneOf(tok::colon, tok::l_square, tok::l_paren,
286 tok::kw_return, tok::kw_throw) ||
287 Parent->isUnaryOperator() ||
288 Parent->isOneOf(TT_ObjCForIn, TT_CastRParen) ||
290 bool ColonFound =
false;
292 unsigned BindingIncrease = 1;
293 if (Left->is(TT_Unknown)) {
294 if (StartsObjCMethodExpr) {
295 Left->Type = TT_ObjCMethodExpr;
297 Contexts.back().ContextKind == tok::l_brace &&
298 Parent->isOneOf(tok::l_brace, tok::comma)) {
299 Left->Type = TT_JsComputedPropertyName;
302 Parent->isOneOf(TT_BinaryOperator, tok::at, tok::comma,
303 tok::l_paren, tok::l_square, tok::question,
304 tok::colon, tok::kw_return,
307 Left->Type = TT_ArrayInitializerLSquare;
309 BindingIncrease = 10;
310 Left->Type = TT_ArraySubscriptLSquare;
314 ScopedContextCreator ContextCreator(*
this, tok::l_square, BindingIncrease);
315 Contexts.back().IsExpression =
true;
316 Contexts.back().ColonIsObjCMethodExpr = StartsObjCMethodExpr;
321 Left->is(TT_ObjCMethodExpr)) {
324 StartsObjCMethodExpr =
false;
325 Left->Type = TT_Unknown;
327 if (StartsObjCMethodExpr &&
CurrentToken->Previous != Left) {
332 if (Parent && Parent->is(TT_PointerOrReference))
333 Parent->Type = TT_BinaryOperator;
337 if (
Contexts.back().FirstObjCSelectorName) {
338 Contexts.back().FirstObjCSelectorName->LongestObjCSelectorName =
339 Contexts.back().LongestObjCSelectorName;
340 if (Left->BlockParameterCount > 1)
341 Contexts.back().FirstObjCSelectorName->LongestObjCSelectorName = 0;
349 if (Left->is(TT_ArraySubscriptLSquare)) {
350 Left->Type = TT_ObjCMethodExpr;
351 StartsObjCMethodExpr =
true;
352 Contexts.back().ColonIsObjCMethodExpr =
true;
353 if (Parent && Parent->is(tok::r_paren))
354 Parent->Type = TT_CastRParen;
358 if (
CurrentToken->is(tok::comma) && Left->is(TT_ObjCMethodExpr) &&
360 Left->Type = TT_ArrayInitializerLSquare;
364 updateParameterCount(Left, Tok);
372 Left->ParentBracket =
Contexts.back().ContextKind;
375 Left->Type = TT_ObjCBlockLBrace;
378 ScopedContextCreator ContextCreator(*
this, tok::l_brace, 1);
379 Contexts.back().ColonIsDictLiteral =
true;
381 Contexts.back().IsExpression =
true;
396 (!
Contexts.back().ColonIsDictLiteral ||
399 Previous->Tok.getIdentifierInfo())
400 Previous->Type = TT_SelectorName;
403 Left->Type = TT_DictLiteral;
412 void updateParameterCount(FormatToken *Left, FormatToken *
Current) {
413 if (Current->is(tok::l_brace) && !Current->is(TT_DictLiteral))
414 ++Left->BlockParameterCount;
415 if (Current->is(tok::comma)) {
416 ++Left->ParameterCount;
418 Left->Role.reset(
new CommaSeparatedList(Style));
419 Left->Role->CommaFound(Current);
420 }
else if (Left->ParameterCount == 0 && Current->isNot(tok::comment)) {
421 Left->ParameterCount = 1;
425 bool parseConditional() {
438 bool parseTemplateDeclaration() {
445 CurrentToken->Previous->ClosesTemplateDeclaration =
true;
451 bool consumeToken() {
454 switch (Tok->Tok.getKind()) {
457 if (!Tok->Previous && Line.MustBeDeclaration)
458 Tok->Type = TT_ObjCMethodSpecifier;
465 if (
Contexts.back().ColonIsForRangeExpr ||
467 !Line.First->isOneOf(tok::kw_enum, tok::kw_case)) ||
468 Contexts.back().ContextKind == tok::l_paren ||
469 Contexts.back().ContextKind == tok::l_square ||
471 Line.MustBeDeclaration)) {
472 Tok->Type = TT_JsTypeColon;
476 if (
Contexts.back().ColonIsDictLiteral ||
478 Tok->Type = TT_DictLiteral;
479 }
else if (
Contexts.back().ColonIsObjCMethodExpr ||
480 Line.startsWith(TT_ObjCMethodSpecifier)) {
481 Tok->Type = TT_ObjCMethodExpr;
482 Tok->Previous->Type = TT_SelectorName;
483 if (Tok->Previous->ColumnWidth >
484 Contexts.back().LongestObjCSelectorName)
485 Contexts.back().LongestObjCSelectorName = Tok->Previous->ColumnWidth;
486 if (!
Contexts.back().FirstObjCSelectorName)
487 Contexts.back().FirstObjCSelectorName = Tok->Previous;
488 }
else if (
Contexts.back().ColonIsForRangeExpr) {
489 Tok->Type = TT_RangeBasedForLoopColon;
491 Tok->Type = TT_BitFieldColon;
493 !Line.First->isOneOf(tok::kw_enum, tok::kw_case)) {
494 if (Tok->Previous->is(tok::r_paren))
495 Tok->Type = TT_CtorInitializerColon;
497 Tok->Type = TT_InheritanceColon;
498 }
else if (Tok->Previous->is(tok::identifier) && Tok->Next &&
499 Tok->Next->isOneOf(tok::r_paren, tok::comma)) {
502 Tok->Type = TT_ObjCMethodExpr;
503 }
else if (
Contexts.back().ContextKind == tok::l_paren) {
504 Tok->Type = TT_InlineASMColon;
511 if (!parseParens(
true))
516 Contexts.back().ColonIsForRangeExpr =
true;
527 Tok->Previous->is(tok::r_paren) &&
528 Tok->Previous->MatchingParen &&
529 Tok->Previous->MatchingParen->is(TT_OverloadedOperatorLParen)) {
530 Tok->Previous->Type = TT_OverloadedOperator;
531 Tok->Previous->MatchingParen->Type = TT_OverloadedOperator;
532 Tok->Type = TT_OverloadedOperatorLParen;
537 if (Line.MustBeDeclaration &&
Contexts.size() == 1 &&
538 !
Contexts.back().IsExpression && !Line.startsWith(TT_ObjCProperty) &&
540 !Tok->Previous->isOneOf(tok::kw_decltype, tok::kw___attribute,
541 TT_LeadingJavaAnnotation)))
542 Line.MightBeFunctionDecl =
true;
555 (!Tok->Previous->Tok.isLiteral() &&
556 !(Tok->Previous->is(tok::r_paren) &&
Contexts.size() > 1))) &&
558 Tok->Type = TT_TemplateOpener;
560 Tok->Type = TT_BinaryOperator;
575 Tok->Type = TT_BinaryOperator;
577 case tok::kw_operator:
579 !
CurrentToken->isOneOf(tok::l_paren, tok::semi, tok::r_paren)) {
584 CurrentToken->Previous->isOneOf(TT_BinaryOperator, tok::comma))
595 Tok->Next->isOneOf(tok::semi, tok::comma, tok::colon, tok::r_paren,
601 Tok->Type = TT_JsTypeOptionalQuestion;
606 if (Line.MustBeDeclaration &&
611 case tok::kw_template:
612 parseTemplateDeclaration();
615 if (
Contexts.back().InCtorInitializer)
616 Tok->Type = TT_CtorInitializerComma;
618 (
Contexts.size() == 1 || Line.startsWith(tok::kw_for))) {
619 Contexts.back().FirstStartOfName->PartOfMultiVariableDeclStmt =
true;
620 Line.IsMultiVariableDeclStmt =
true;
623 Contexts.back().IsExpression =
true;
631 void parseIncludeDirective() {
642 void parseWarningOrError() {
656 CurrentToken->isOneOf(Keywords.kw_mark, Keywords.kw_option)) {
661 if (IsMark ||
CurrentToken->Previous->is(TT_BinaryOperator))
668 LineType parsePreprocessorDirective() {
681 switch (
CurrentToken->Tok.getIdentifierInfo()->getPPKeywordID()) {
682 case tok::pp_include:
683 case tok::pp_include_next:
686 parseIncludeDirective();
690 case tok::pp_warning:
691 parseWarningOrError();
698 Contexts.back().IsExpression =
true;
713 return parsePreprocessorDirective();
718 IdentifierInfo *Info =
CurrentToken->Tok.getIdentifierInfo();
721 (Info && Info->getPPKeywordID() == tok::pp_import &&
723 CurrentToken->Next->isOneOf(tok::string_literal, tok::identifier,
726 parseIncludeDirective();
732 if (
CurrentToken->is(tok::less) && Line.Last->is(tok::greater)) {
733 parseIncludeDirective();
746 bool KeywordVirtualFound =
false;
747 bool ImportStatement =
false;
750 KeywordVirtualFound =
true;
752 ImportStatement =
true;
756 if (KeywordVirtualFound)
761 if (Line.startsWith(TT_ObjCMethodSpecifier)) {
762 if (
Contexts.back().FirstObjCSelectorName)
763 Contexts.back().FirstObjCSelectorName->LongestObjCSelectorName =
764 Contexts.back().LongestObjCSelectorName;
772 bool isImportStatement(
const FormatToken &Tok) {
776 Tok.TokenText ==
"goog" && Tok.Next && Tok.Next->is(tok::period) &&
777 Tok.Next->Next && (Tok.Next->Next->TokenText ==
"module" ||
778 Tok.Next->Next->TokenText ==
"provide" ||
779 Tok.Next->Next->TokenText ==
"require" ||
780 Tok.Next->Next->TokenText ==
"setTestOnly") &&
781 Tok.Next->Next->Next && Tok.Next->Next->Next->is(tok::l_paren);
784 void resetTokenMetadata(FormatToken *
Token) {
790 if (!
CurrentToken->isOneOf(TT_LambdaLSquare, TT_ForEachMacro,
791 TT_FunctionLBrace, TT_ImplicitStringLiteral,
792 TT_InlineASMBrace, TT_JsFatArrow, TT_LambdaArrow,
839 struct ScopedContextCreator {
845 P.Contexts.push_back(
Context(ContextKind,
846 P.Contexts.back().BindingStrength + Increase,
847 P.Contexts.back().IsExpression));
850 ~ScopedContextCreator() {
P.Contexts.pop_back(); }
853 void modifyContext(
const FormatToken &Current) {
855 !Line.First->isOneOf(tok::kw_template, tok::kw_using, tok::kw_return) &&
856 (!Current.Previous || Current.Previous->isNot(tok::kw_operator))) {
857 Contexts.back().IsExpression =
true;
858 if (!Line.startsWith(TT_UnaryOperator)) {
859 for (FormatToken *
Previous = Current.Previous;
861 !
Previous->Previous->isOneOf(tok::comma, tok::semi);
863 if (
Previous->isOneOf(tok::r_square, tok::r_paren)) {
870 if (
Previous->isOneOf(TT_BinaryOperator, TT_UnaryOperator) &&
871 Previous->isOneOf(tok::star, tok::amp, tok::ampamp) &&
873 Previous->Type = TT_PointerOrReference;
876 }
else if (Current.is(tok::lessless) &&
877 (!Current.Previous || !Current.Previous->is(tok::kw_operator))) {
878 Contexts.back().IsExpression =
true;
879 }
else if (Current.isOneOf(tok::kw_return, tok::kw_throw)) {
880 Contexts.back().IsExpression =
true;
881 }
else if (Current.is(TT_TrailingReturnArrow)) {
882 Contexts.back().IsExpression =
false;
883 }
else if (Current.is(TT_LambdaArrow) || Current.is(Keywords.kw_assert)) {
885 }
else if (Current.isOneOf(tok::r_paren, tok::greater, tok::comma)) {
886 for (FormatToken *
Previous = Current.Previous;
889 Previous->Type = TT_PointerOrReference;
890 if (Line.MustBeDeclaration)
892 }
else if (Current.Previous &&
893 Current.Previous->is(TT_CtorInitializerColon)) {
894 Contexts.back().IsExpression =
true;
895 Contexts.back().InCtorInitializer =
true;
896 }
else if (Current.is(tok::kw_new)) {
897 Contexts.back().CanBeExpression =
false;
898 }
else if (Current.isOneOf(tok::semi, tok::exclaim)) {
900 Contexts.back().IsExpression =
true;
904 void determineTokenType(FormatToken &Current) {
905 if (!Current.is(TT_Unknown))
912 if (Current.is(Keywords.kw_instanceof)) {
913 Current.Type = TT_BinaryOperator;
914 }
else if (isStartOfName(Current) &&
915 (!Line.MightBeFunctionDecl || Current.NestingLevel != 0)) {
917 Current.Type = TT_StartOfName;
918 }
else if (Current.isOneOf(tok::kw_auto, tok::kw___auto_type)) {
920 }
else if (Current.is(tok::arrow) &&
922 Current.Type = TT_LambdaArrow;
923 }
else if (Current.is(tok::arrow) &&
AutoFound && Line.MustBeDeclaration &&
924 Current.NestingLevel == 0) {
925 Current.Type = TT_TrailingReturnArrow;
926 }
else if (Current.isOneOf(tok::star, tok::amp, tok::ampamp)) {
928 determineStarAmpUsage(Current,
Contexts.back().CanBeExpression &&
930 Contexts.back().InTemplateArgument);
931 }
else if (Current.isOneOf(tok::minus, tok::plus, tok::caret)) {
932 Current.Type = determinePlusMinusCaretUsage(Current);
933 if (Current.is(TT_UnaryOperator) && Current.is(tok::caret))
935 }
else if (Current.isOneOf(tok::minusminus, tok::plusplus)) {
936 Current.Type = determineIncrementUsage(Current);
937 }
else if (Current.isOneOf(tok::exclaim, tok::tilde)) {
938 Current.Type = TT_UnaryOperator;
939 }
else if (Current.is(tok::question)) {
941 Line.MustBeDeclaration) {
944 Current.Type = TT_JsTypeOptionalQuestion;
946 Current.Type = TT_ConditionalExpr;
948 }
else if (Current.isBinaryOperator() &&
949 (!Current.Previous || Current.Previous->isNot(tok::l_square))) {
950 Current.Type = TT_BinaryOperator;
951 }
else if (Current.is(tok::comment)) {
952 if (Current.TokenText.startswith(
"/*")) {
953 if (Current.TokenText.endswith(
"*/"))
954 Current.Type = TT_BlockComment;
958 Current.Tok.setKind(tok::unknown);
960 Current.Type = TT_LineComment;
962 }
else if (Current.is(tok::r_paren)) {
963 if (rParenEndsCast(Current))
964 Current.Type = TT_CastRParen;
965 if (Current.MatchingParen && Current.Next &&
966 !Current.Next->isBinaryOperator() &&
967 !Current.Next->isOneOf(tok::semi, tok::colon, tok::l_brace))
968 if (FormatToken *BeforeParen = Current.MatchingParen->Previous)
969 if (BeforeParen->is(tok::identifier) &&
970 BeforeParen->TokenText == BeforeParen->TokenText.upper() &&
971 (!BeforeParen->Previous ||
972 BeforeParen->Previous->ClosesTemplateDeclaration))
973 Current.Type = TT_FunctionAnnotationRParen;
974 }
else if (Current.is(tok::at) && Current.Next) {
975 if (Current.Next->isStringLiteral()) {
976 Current.Type = TT_ObjCStringLiteral;
978 switch (Current.Next->Tok.getObjCKeywordID()) {
979 case tok::objc_interface:
980 case tok::objc_implementation:
981 case tok::objc_protocol:
982 Current.Type = TT_ObjCDecl;
984 case tok::objc_property:
985 Current.Type = TT_ObjCProperty;
991 }
else if (Current.is(tok::period)) {
992 FormatToken *PreviousNoComment = Current.getPreviousNonComment();
993 if (PreviousNoComment &&
994 PreviousNoComment->isOneOf(tok::comma, tok::l_brace))
995 Current.Type = TT_DesignatedInitializerPeriod;
997 Current.Previous->isOneOf(TT_JavaAnnotation,
998 TT_LeadingJavaAnnotation)) {
999 Current.Type = Current.Previous->Type;
1001 }
else if (Current.isOneOf(tok::identifier, tok::kw_const) &&
1003 !Current.Previous->isOneOf(tok::equal, tok::at) &&
1004 Line.MightBeFunctionDecl &&
Contexts.size() == 1) {
1007 Current.Type = TT_TrailingAnnotation;
1011 if (Current.Previous->is(tok::at) &&
1012 Current.isNot(Keywords.kw_interface)) {
1013 const FormatToken &AtToken = *Current.Previous;
1014 const FormatToken *
Previous = AtToken.getPreviousNonComment();
1015 if (!Previous || Previous->is(TT_LeadingJavaAnnotation))
1016 Current.Type = TT_LeadingJavaAnnotation;
1018 Current.Type = TT_JavaAnnotation;
1019 }
else if (Current.Previous->is(tok::period) &&
1020 Current.Previous->isOneOf(TT_JavaAnnotation,
1021 TT_LeadingJavaAnnotation)) {
1022 Current.Type = Current.Previous->Type;
1032 bool isStartOfName(
const FormatToken &Tok) {
1033 if (Tok.isNot(tok::identifier) || !Tok.Previous)
1036 if (Tok.Previous->isOneOf(TT_LeadingJavaAnnotation, Keywords.kw_instanceof))
1040 FormatToken *PreviousNotConst = Tok.Previous;
1041 while (PreviousNotConst && PreviousNotConst->is(tok::kw_const))
1042 PreviousNotConst = PreviousNotConst->Previous;
1044 if (!PreviousNotConst)
1047 bool IsPPKeyword = PreviousNotConst->is(tok::identifier) &&
1048 PreviousNotConst->Previous &&
1049 PreviousNotConst->Previous->is(tok::hash);
1051 if (PreviousNotConst->is(TT_TemplateCloser))
1052 return PreviousNotConst && PreviousNotConst->MatchingParen &&
1053 PreviousNotConst->MatchingParen->Previous &&
1054 PreviousNotConst->MatchingParen->Previous->isNot(tok::period) &&
1055 PreviousNotConst->MatchingParen->Previous->isNot(tok::kw_template);
1057 if (PreviousNotConst->is(tok::r_paren) && PreviousNotConst->MatchingParen &&
1058 PreviousNotConst->MatchingParen->Previous &&
1059 PreviousNotConst->MatchingParen->Previous->is(tok::kw_decltype))
1062 return (!IsPPKeyword &&
1063 PreviousNotConst->isOneOf(tok::identifier, tok::kw_auto)) ||
1064 PreviousNotConst->is(TT_PointerOrReference) ||
1065 PreviousNotConst->isSimpleTypeSpecifier();
1069 bool rParenEndsCast(
const FormatToken &Tok) {
1076 if (Tok.Previous == Tok.MatchingParen || !Tok.Next || !Tok.MatchingParen)
1079 FormatToken *LeftOfParens = Tok.MatchingParen->getPreviousNonComment();
1083 if (LeftOfParens->is(tok::r_paren)) {
1084 if (!LeftOfParens->MatchingParen ||
1085 !LeftOfParens->MatchingParen->Previous)
1087 LeftOfParens = LeftOfParens->MatchingParen->Previous;
1092 if (LeftOfParens->Tok.getIdentifierInfo() &&
1093 !LeftOfParens->isOneOf(Keywords.kw_in, tok::kw_return, tok::kw_case,
1099 if (LeftOfParens->isOneOf(tok::at, tok::r_square, TT_OverloadedOperator,
1104 if (Tok.Next->is(tok::question))
1113 if (Tok.Next->isNot(tok::string_literal) &&
1114 (Tok.Next->Tok.isLiteral() ||
1115 Tok.Next->isOneOf(tok::kw_sizeof, tok::kw_alignof)))
1119 bool ParensAreType =
1121 Tok.Previous->isOneOf(TT_PointerOrReference, TT_TemplateCloser) ||
1122 Tok.Previous->isSimpleTypeSpecifier();
1123 bool ParensCouldEndDecl =
1124 Tok.Next->isOneOf(tok::equal, tok::semi, tok::l_brace, tok::greater);
1125 if (ParensAreType && !ParensCouldEndDecl)
1136 if (Tok.Next->is(tok::identifier))
1139 if (!Tok.Next->Next)
1146 Tok.Next->isUnaryOperator() || Tok.Next->isOneOf(tok::amp, tok::star);
1147 if (!NextIsUnary || Tok.Next->is(tok::plus) ||
1148 !Tok.Next->Next->isOneOf(tok::identifier, tok::numeric_constant))
1151 for (FormatToken *Prev = Tok.Previous; Prev != Tok.MatchingParen;
1152 Prev = Prev->Previous) {
1153 if (!Prev->isOneOf(tok::kw_const, tok::identifier, tok::coloncolon))
1163 return TT_BinaryOperator;
1165 const FormatToken *PrevToken = Tok.getPreviousNonComment();
1167 return TT_UnaryOperator;
1169 const FormatToken *NextToken = Tok.getNextNonComment();
1171 NextToken->isOneOf(tok::arrow, Keywords.kw_final,
1172 Keywords.kw_override) ||
1173 (NextToken->is(tok::l_brace) && !NextToken->getNextNonComment()))
1174 return TT_PointerOrReference;
1176 if (PrevToken->is(tok::coloncolon))
1177 return TT_PointerOrReference;
1179 if (PrevToken->isOneOf(tok::l_paren, tok::l_square, tok::l_brace,
1180 tok::comma, tok::semi, tok::kw_return, tok::colon,
1181 tok::equal, tok::kw_delete, tok::kw_sizeof) ||
1182 PrevToken->isOneOf(TT_BinaryOperator, TT_ConditionalExpr,
1183 TT_UnaryOperator, TT_CastRParen))
1184 return TT_UnaryOperator;
1186 if (NextToken->is(tok::l_square) && NextToken->isNot(TT_LambdaLSquare))
1187 return TT_PointerOrReference;
1189 return TT_PointerOrReference;
1190 if (NextToken->isOneOf(tok::comma, tok::semi))
1191 return TT_PointerOrReference;
1193 if (PrevToken->is(tok::r_paren) && PrevToken->MatchingParen &&
1194 PrevToken->MatchingParen->Previous &&
1195 PrevToken->MatchingParen->Previous->isOneOf(tok::kw_typeof,
1197 return TT_PointerOrReference;
1199 if (PrevToken->Tok.isLiteral() ||
1200 PrevToken->isOneOf(tok::r_paren, tok::r_square, tok::kw_true,
1201 tok::kw_false, tok::r_brace) ||
1202 NextToken->Tok.isLiteral() ||
1203 NextToken->isOneOf(tok::kw_true, tok::kw_false) ||
1204 NextToken->isUnaryOperator() ||
1208 (InTemplateArgument && NextToken->Tok.isAnyIdentifier()))
1209 return TT_BinaryOperator;
1212 if (Tok.is(tok::ampamp) && NextToken && NextToken->is(tok::l_paren))
1213 return TT_BinaryOperator;
1217 const FormatToken *NextNextToken = NextToken->getNextNonComment();
1218 if (NextNextToken && NextNextToken->is(tok::arrow))
1219 return TT_BinaryOperator;
1223 if (IsExpression && !
Contexts.back().CaretFound)
1224 return TT_BinaryOperator;
1226 return TT_PointerOrReference;
1229 TokenType determinePlusMinusCaretUsage(
const FormatToken &Tok) {
1230 const FormatToken *PrevToken = Tok.getPreviousNonComment();
1231 if (!PrevToken || PrevToken->is(TT_CastRParen))
1232 return TT_UnaryOperator;
1235 if (PrevToken->isOneOf(tok::equal, tok::l_paren, tok::comma, tok::l_square,
1236 tok::question, tok::colon, tok::kw_return,
1237 tok::kw_case, tok::at, tok::l_brace))
1238 return TT_UnaryOperator;
1241 if (PrevToken->is(TT_BinaryOperator))
1242 return TT_UnaryOperator;
1245 return TT_BinaryOperator;
1249 TokenType determineIncrementUsage(
const FormatToken &Tok) {
1250 const FormatToken *PrevToken = Tok.getPreviousNonComment();
1251 if (!PrevToken || PrevToken->is(TT_CastRParen))
1252 return TT_UnaryOperator;
1253 if (PrevToken->isOneOf(tok::r_paren, tok::r_square, tok::identifier))
1254 return TT_TrailingUnaryOperator;
1256 return TT_UnaryOperator;
1279 class ExpressionParser {
1281 ExpressionParser(
const FormatStyle &Style,
const AdditionalKeywords &Keywords,
1282 AnnotatedLine &Line)
1286 void parse(
int Precedence = 0) {
1289 while (Current && (Current->is(tok::kw_return) ||
1290 (Current->is(tok::colon) &&
1291 Current->isOneOf(TT_ObjCMethodExpr, TT_DictLiteral))))
1294 if (!Current || Precedence > PrecedenceArrowAndPeriod)
1299 parseConditionalExpr();
1305 if (Precedence == PrecedenceUnaryOperator) {
1306 parseUnaryOperator();
1311 FormatToken *LatestOperator =
nullptr;
1312 unsigned OperatorIndex = 0;
1316 parse(Precedence + 1);
1318 int CurrentPrecedence = getCurrentPrecedence();
1320 if (Current && Current->is(TT_SelectorName) &&
1321 Precedence == CurrentPrecedence) {
1323 addFakeParenthesis(Start,
prec::Level(Precedence));
1329 if (!Current || (Current->closesScope() && Current->MatchingParen) ||
1330 (CurrentPrecedence != -1 && CurrentPrecedence < Precedence) ||
1337 if (Current->opensScope()) {
1338 while (Current && !Current->closesScope()) {
1345 if (CurrentPrecedence == Precedence) {
1347 LatestOperator->NextOperator =
Current;
1349 Current->OperatorIndex = OperatorIndex;
1352 next(Precedence > 0);
1356 if (LatestOperator && (Current || Precedence > 0)) {
1358 if (Precedence == PrecedenceArrowAndPeriod) {
1362 addFakeParenthesis(Start,
prec::Level(Precedence));
1370 int getCurrentPrecedence() {
1372 const FormatToken *NextNonComment = Current->getNextNonComment();
1373 if (Current->is(TT_ConditionalExpr))
1375 if (NextNonComment && NextNonComment->is(tok::colon) &&
1376 NextNonComment->is(TT_DictLiteral))
1378 if (Current->is(TT_LambdaArrow))
1380 if (Current->is(TT_JsFatArrow))
1382 if (Current->isOneOf(tok::semi, TT_InlineASMColon, TT_SelectorName,
1383 TT_JsComputedPropertyName) ||
1384 (Current->is(tok::comment) && NextNonComment &&
1385 NextNonComment->is(TT_SelectorName)))
1387 if (Current->is(TT_RangeBasedForLoopColon))
1391 Current->is(Keywords.kw_instanceof))
1393 if (Current->is(TT_BinaryOperator) || Current->is(tok::comma))
1394 return Current->getPrecedence();
1395 if (Current->isOneOf(tok::period, tok::arrow))
1396 return PrecedenceArrowAndPeriod;
1398 Current->isOneOf(Keywords.kw_extends, Keywords.kw_implements,
1399 Keywords.kw_throws))
1405 void addFakeParenthesis(FormatToken *Start,
prec::Level Precedence) {
1406 Start->FakeLParens.push_back(Precedence);
1408 Start->StartsBinaryExpression =
true;
1410 FormatToken *Previous = Current->Previous;
1411 while (Previous->is(tok::comment) && Previous->Previous)
1412 Previous = Previous->Previous;
1413 ++Previous->FakeRParens;
1415 Previous->EndsBinaryExpression =
true;
1421 void parseUnaryOperator() {
1422 if (!Current || Current->isNot(TT_UnaryOperator)) {
1423 parse(PrecedenceArrowAndPeriod);
1429 parseUnaryOperator();
1435 void parseConditionalExpr() {
1436 while (Current && Current->isTrailingComment()) {
1441 if (!Current || !Current->is(tok::question))
1445 if (!Current || Current->isNot(TT_ConditionalExpr))
1452 void next(
bool SkipPastLeadingComments =
true) {
1454 Current = Current->Next;
1456 (Current->NewlinesBefore == 0 || SkipPastLeadingComments) &&
1457 Current->isTrailingComment())
1458 Current = Current->Next;
1461 const FormatStyle &
Style;
1462 const AdditionalKeywords &
Keywords;
1474 if (NextNonCommentLine && (*I)->
First->
is(tok::comment) &&
1475 (*I)->First->Next ==
nullptr)
1478 NextNonCommentLine = (*I)->
First->
isNot(tok::r_brace) ? (*I) :
nullptr;
1490 AnnotatingParser
Parser(Style, Line, Keywords);
1491 Line.
Type = Parser.parseLine();
1495 ExpressionParser ExprParser(Style, Keywords, Line);
1514 if (
Next->is(TT_OverloadedOperatorLParen))
1516 if (
Next->is(TT_OverloadedOperator))
1518 if (
Next->isOneOf(tok::kw_new, tok::kw_delete)) {
1520 if (
Next->Next &&
Next->Next->is(tok::l_square) &&
1521 Next->Next->Next &&
Next->Next->Next->is(tok::r_square))
1532 if (Current.
is(tok::kw_operator)) {
1535 Next = skipOperatorName(Next);
1540 if (Next->
is(TT_TemplateOpener)) {
1542 }
else if (Next->
is(tok::coloncolon)) {
1546 if (Next->
is(tok::kw_operator)) {
1547 Next = skipOperatorName(Next->
Next);
1550 if (!Next->
is(tok::identifier))
1552 }
else if (Next->
is(tok::l_paren)) {
1560 if (!Next || !Next->
is(tok::l_paren))
1566 if (Tok->is(tok::kw_const) || Tok->isSimpleTypeSpecifier() ||
1567 Tok->isOneOf(TT_PointerOrReference, TT_StartOfName))
1569 if (Tok->isOneOf(tok::l_brace, tok::string_literal, TT_ObjCMethodExpr) ||
1570 Tok->Tok.isLiteral())
1576 bool TokenAnnotator::mustBreakForReturnType(
const AnnotatedLine &Line)
const {
1577 assert(Line.MightBeFunctionDecl);
1580 Style.AlwaysBreakAfterReturnType ==
1585 switch (Style.AlwaysBreakAfterReturnType) {
1593 return Line.mightBeFunctionDefinition();
1614 Current->
Type = TT_FunctionDeclarationName;
1615 if (Current->
is(TT_LineComment)) {
1632 if (
Parameter->isOneOf(tok::comment, tok::r_brace))
1635 if (!
Parameter->Previous->is(TT_CtorInitializerComma) &&
1643 spaceRequiredBefore(Line, *Current)) {
1651 Current->
is(TT_FunctionDeclarationName))
1656 unsigned ChildSize = 0;
1665 Prev->
Children[0]->First->MustBreakBefore) ||
1672 if (Current->
is(TT_CtorInitializerColon))
1673 InFunctionDecl =
false;
1679 splitPenalty(Line, *Current, InFunctionDecl);
1681 Current = Current->
Next;
1684 calculateUnbreakableTailLengths(Line);
1685 for (Current = Line.
First; Current !=
nullptr; Current = Current->
Next) {
1687 Current->
Role->precomputeFormattingInfos(Current);
1690 DEBUG({ printDebugInfo(Line); });
1693 void TokenAnnotator::calculateUnbreakableTailLengths(
AnnotatedLine &Line) {
1694 unsigned UnbreakableTailLength = 0;
1699 Current->
isOneOf(tok::comment, tok::string_literal)) {
1700 UnbreakableTailLength = 0;
1702 UnbreakableTailLength +=
1709 unsigned TokenAnnotator::splitPenalty(
const AnnotatedLine &Line,
1710 const FormatToken &Tok,
1711 bool InFunctionDecl) {
1712 const FormatToken &Left = *Tok.
Previous;
1713 const FormatToken &Right = Tok;
1715 if (Left.is(tok::semi))
1719 if (Right.isOneOf(Keywords.kw_extends, Keywords.kw_throws))
1721 if (Right.is(Keywords.kw_implements))
1723 if (Left.is(tok::comma) && Left.NestingLevel == 0)
1726 if (Right.is(Keywords.kw_function) && Left.isNot(tok::comma))
1728 if (Left.is(TT_JsTypeColon))
1732 if (Left.is(tok::comma) || (Right.is(tok::identifier) && Right.Next &&
1733 Right.Next->is(TT_DictLiteral)))
1735 if (Right.is(tok::l_square)) {
1738 if (Left.is(tok::r_square))
1741 if (Right.is(TT_LambdaLSquare) && Left.is(tok::equal))
1743 if (!Right.isOneOf(TT_ObjCMethodExpr, TT_LambdaLSquare,
1744 TT_ArrayInitializerLSquare))
1748 if (Right.isOneOf(TT_StartOfName, TT_FunctionDeclarationName) ||
1749 Right.is(tok::kw_operator)) {
1750 if (Line.startsWith(tok::kw_for) && Right.PartOfMultiVariableDeclStmt)
1752 if (Left.is(TT_StartOfName))
1754 if (InFunctionDecl && Right.NestingLevel == 0)
1755 return Style.PenaltyReturnTypeOnItsOwnLine;
1758 if (Right.is(TT_PointerOrReference))
1760 if (Right.is(TT_LambdaArrow))
1762 if (Left.is(tok::equal) && Right.is(tok::l_brace))
1764 if (Left.is(TT_CastRParen))
1766 if (Left.is(tok::coloncolon) ||
1769 if (Left.isOneOf(tok::kw_class, tok::kw_struct))
1772 if (Left.isOneOf(TT_RangeBasedForLoopColon, TT_InheritanceColon))
1775 if (Right.isMemberAccess()) {
1800 if (Right.is(TT_TrailingAnnotation) &&
1801 (!Right.Next || Right.Next->isNot(tok::l_paren))) {
1804 if (Line.startsWith(TT_ObjCMethodSpecifier))
1811 bool is_short_annotation = Right.TokenText.size() < 10;
1812 return (Left.is(tok::r_paren) ? 100 : 120) + (is_short_annotation ? 50 : 0);
1816 if (Line.startsWith(tok::kw_for) && Left.is(tok::equal))
1821 if (Right.is(TT_SelectorName))
1823 if (Left.is(tok::colon) && Left.is(TT_ObjCMethodExpr))
1824 return Line.MightBeFunctionDecl ? 50 : 500;
1826 if (Left.is(tok::l_paren) && InFunctionDecl &&
1829 if (Left.is(tok::l_paren) && Left.Previous &&
1830 Left.Previous->isOneOf(tok::kw_if, tok::kw_for))
1832 if (Left.is(tok::equal) && InFunctionDecl)
1834 if (Right.is(tok::r_brace))
1836 if (Left.is(TT_TemplateOpener))
1838 if (Left.opensScope()) {
1841 return Left.ParameterCount > 1 ? Style.PenaltyBreakBeforeFirstCallParameter
1844 if (Left.is(TT_JavaAnnotation))
1847 if (Right.is(tok::lessless)) {
1848 if (Left.is(tok::string_literal) &&
1849 (Right.NextOperator || Right.OperatorIndex != 1)) {
1850 StringRef Content = Left.TokenText;
1851 if (Content.startswith(
"\""))
1852 Content = Content.drop_front(1);
1853 if (Content.endswith(
"\""))
1854 Content = Content.drop_back(1);
1855 Content = Content.trim();
1856 if (Content.size() > 1 &&
1857 (Content.back() ==
':' || Content.back() ==
'='))
1862 if (Left.is(TT_ConditionalExpr))
1867 Level = Right.getPrecedence();
1874 bool TokenAnnotator::spaceRequiredBetween(
const AnnotatedLine &Line,
1875 const FormatToken &Left,
1876 const FormatToken &Right) {
1877 if (Left.is(tok::kw_return) && Right.isNot(tok::semi))
1880 Left.Tok.getObjCKeywordID() == tok::objc_property)
1882 if (Right.is(tok::hashhash))
1883 return Left.is(tok::hash);
1884 if (Left.isOneOf(tok::hashhash, tok::hash))
1885 return Right.is(tok::hash);
1886 if (Left.is(tok::l_paren) && Right.is(tok::r_paren))
1887 return Style.SpaceInEmptyParentheses;
1888 if (Left.is(tok::l_paren) || Right.is(tok::r_paren))
1889 return (Right.is(TT_CastRParen) ||
1890 (Left.MatchingParen && Left.MatchingParen->is(TT_CastRParen)))
1891 ? Style.SpacesInCStyleCastParentheses
1892 : Style.SpacesInParentheses;
1893 if (Right.isOneOf(tok::semi, tok::comma))
1895 if (Right.is(tok::less) &&
1896 (Left.is(tok::kw_template) ||
1897 (Line.Type ==
LT_ObjCDecl && Style.ObjCSpaceBeforeProtocolList)))
1899 if (Left.isOneOf(tok::exclaim, tok::tilde))
1901 if (Left.is(tok::at) &&
1902 Right.isOneOf(tok::identifier, tok::string_literal, tok::char_constant,
1903 tok::numeric_constant, tok::l_paren, tok::l_brace,
1904 tok::kw_true, tok::kw_false))
1906 if (Left.is(tok::colon))
1907 return !Left.is(TT_ObjCMethodExpr);
1908 if (Left.is(tok::coloncolon))
1910 if (Left.is(tok::less) || Right.isOneOf(tok::greater, tok::less))
1912 if (Right.is(tok::ellipsis))
1913 return Left.Tok.isLiteral();
1914 if (Left.is(tok::l_square) && Right.is(tok::amp))
1916 if (Right.is(TT_PointerOrReference))
1917 return (Left.is(tok::r_paren) && Left.MatchingParen &&
1918 (Left.MatchingParen->is(TT_OverloadedOperatorLParen) ||
1919 (Left.MatchingParen->Previous &&
1920 Left.MatchingParen->Previous->is(TT_FunctionDeclarationName)))) ||
1921 (Left.Tok.isLiteral() ||
1922 (!Left.isOneOf(TT_PointerOrReference, tok::l_paren) &&
1924 Line.IsMultiVariableDeclStmt)));
1925 if (Right.is(TT_FunctionTypeLParen) && Left.isNot(tok::l_paren) &&
1926 (!Left.is(TT_PointerOrReference) ||
1928 !Line.IsMultiVariableDeclStmt)))
1930 if (Left.is(TT_PointerOrReference))
1931 return Right.Tok.isLiteral() ||
1932 Right.isOneOf(TT_BlockComment, Keywords.kw_final,
1933 Keywords.kw_override) ||
1934 (Right.is(tok::l_brace) && Right.BlockKind ==
BK_Block) ||
1935 (!Right.isOneOf(TT_PointerOrReference, TT_ArraySubscriptLSquare,
1938 !Line.IsMultiVariableDeclStmt) &&
1940 !Left.Previous->isOneOf(tok::l_paren, tok::coloncolon));
1941 if (Right.is(tok::star) && Left.is(tok::l_paren))
1943 if (Left.is(tok::l_square))
1944 return (Left.is(TT_ArrayInitializerLSquare) &&
1945 Style.SpacesInContainerLiterals && Right.isNot(tok::r_square)) ||
1946 (Left.is(TT_ArraySubscriptLSquare) && Style.SpacesInSquareBrackets &&
1947 Right.isNot(tok::r_square));
1948 if (Right.is(tok::r_square))
1949 return Right.MatchingParen &&
1950 ((Style.SpacesInContainerLiterals &&
1951 Right.MatchingParen->is(TT_ArrayInitializerLSquare)) ||
1952 (Style.SpacesInSquareBrackets &&
1953 Right.MatchingParen->is(TT_ArraySubscriptLSquare)));
1954 if (Right.is(tok::l_square) &&
1955 !Right.isOneOf(TT_ObjCMethodExpr, TT_LambdaLSquare) &&
1956 !Left.isOneOf(tok::numeric_constant, TT_DictLiteral))
1958 if (Left.is(tok::l_brace) && Right.is(tok::r_brace))
1959 return !Left.Children.empty();
1960 if ((Left.is(tok::l_brace) && Left.BlockKind !=
BK_Block) ||
1961 (Right.is(tok::r_brace) && Right.MatchingParen &&
1962 Right.MatchingParen->BlockKind !=
BK_Block))
1963 return !Style.Cpp11BracedListStyle;
1964 if (Left.is(TT_BlockComment))
1965 return !Left.TokenText.endswith(
"=*/");
1966 if (Right.is(tok::l_paren)) {
1967 if (Left.is(tok::r_paren) && Left.is(TT_AttributeParen))
1969 return Line.Type ==
LT_ObjCDecl || Left.is(tok::semi) ||
1971 (Left.isOneOf(tok::kw_if, tok::pp_elif, tok::kw_for, tok::kw_while,
1972 tok::kw_switch, tok::kw_case, TT_ForEachMacro,
1974 (Left.isOneOf(tok::kw_try, Keywords.kw___except, tok::kw_catch,
1975 tok::kw_new, tok::kw_delete) &&
1976 (!Left.Previous || Left.Previous->isNot(tok::period))))) ||
1978 (Left.is(tok::identifier) || Left.isFunctionLikeKeyword() ||
1979 Left.is(tok::r_paren)) &&
1982 if (Left.is(tok::at) && Right.Tok.getObjCKeywordID() != tok::objc_not_keyword)
1984 if (Right.is(TT_UnaryOperator))
1985 return !Left.isOneOf(tok::l_paren, tok::l_square, tok::at) &&
1986 (Left.isNot(tok::colon) || Left.isNot(TT_ObjCMethodExpr));
1987 if ((Left.isOneOf(tok::identifier, tok::greater, tok::r_square,
1989 Left.isSimpleTypeSpecifier()) &&
1990 Right.is(tok::l_brace) && Right.getNextNonComment() &&
1993 if (Left.is(tok::period) || Right.is(tok::period))
1995 if (Right.is(tok::hash) && Left.is(tok::identifier) && Left.TokenText ==
"L")
1997 if (Left.is(TT_TemplateCloser) && Left.MatchingParen &&
1998 Left.MatchingParen->Previous &&
1999 Left.MatchingParen->Previous->is(tok::period))
2002 if (Left.is(TT_TemplateCloser) && Right.is(tok::l_square))
2007 bool TokenAnnotator::spaceRequiredBefore(
const AnnotatedLine &Line,
2008 const FormatToken &Right) {
2009 const FormatToken &Left = *Right.Previous;
2010 if (Right.Tok.getIdentifierInfo() && Left.Tok.getIdentifierInfo())
2013 if (Left.is(tok::kw_operator))
2014 return Right.is(tok::coloncolon);
2016 if (Right.is(tok::period) &&
2017 Left.isOneOf(Keywords.kw_optional, Keywords.kw_required,
2018 Keywords.kw_repeated, Keywords.kw_extend))
2020 if (Right.is(tok::l_paren) &&
2021 Left.isOneOf(Keywords.kw_returns, Keywords.kw_option))
2024 if (Left.isOneOf(Keywords.kw_let, Keywords.kw_var, TT_JsFatArrow,
2027 if (Left.is(tok::kw_default) && Left.Previous &&
2028 Left.Previous->is(tok::kw_export))
2030 if (Left.is(Keywords.kw_is) && Right.is(tok::l_brace))
2032 if (Right.isOneOf(TT_JsTypeColon, TT_JsTypeOptionalQuestion))
2034 if ((Left.is(tok::l_brace) || Right.is(tok::r_brace)) &&
2035 Line.First->isOneOf(Keywords.kw_import, tok::kw_export))
2037 if (Left.is(tok::ellipsis))
2039 if (Left.is(TT_TemplateCloser) &&
2040 !Right.isOneOf(tok::equal, tok::l_brace, tok::comma, tok::l_square,
2041 Keywords.kw_implements, Keywords.kw_extends))
2047 if (Left.is(tok::r_square) && Right.is(tok::l_brace))
2049 if (Left.is(Keywords.kw_synchronized) && Right.is(tok::l_paren))
2051 if ((Left.isOneOf(tok::kw_static, tok::kw_public, tok::kw_private,
2052 tok::kw_protected) ||
2053 Left.isOneOf(Keywords.kw_final, Keywords.kw_abstract,
2054 Keywords.kw_native)) &&
2055 Right.is(TT_TemplateOpener))
2058 if (Left.is(TT_ImplicitStringLiteral))
2059 return Right.WhitespaceRange.getBegin() != Right.WhitespaceRange.getEnd();
2061 if (Left.is(TT_ObjCMethodSpecifier))
2063 if (Left.is(tok::r_paren) && Right.is(tok::identifier))
2068 (Right.is(tok::equal) || Left.is(tok::equal)))
2071 if (Right.isOneOf(TT_TrailingReturnArrow, TT_LambdaArrow) ||
2072 Left.isOneOf(TT_TrailingReturnArrow, TT_LambdaArrow))
2074 if (Right.is(TT_OverloadedOperatorLParen))
2076 if (Left.is(tok::comma))
2078 if (Right.is(tok::comma))
2080 if (Right.isOneOf(TT_CtorInitializerColon, TT_ObjCBlockLParen))
2082 if (Right.is(tok::colon)) {
2083 if (Line.First->isOneOf(tok::kw_case, tok::kw_default) ||
2084 !Right.getNextNonComment() || Right.getNextNonComment()->is(tok::semi))
2086 if (Right.is(TT_ObjCMethodExpr))
2088 if (Left.is(tok::question))
2090 if (Right.is(TT_InlineASMColon) && Left.is(tok::coloncolon))
2092 if (Right.is(TT_DictLiteral))
2093 return Style.SpacesInContainerLiterals;
2096 if (Left.is(TT_UnaryOperator))
2097 return Right.is(TT_BinaryOperator);
2101 if (Left.is(TT_CastRParen))
2102 return Style.SpaceAfterCStyleCast ||
2103 Right.isOneOf(TT_BinaryOperator, TT_SelectorName);
2105 if (Left.is(tok::greater) && Right.is(tok::greater))
2106 return Right.is(TT_TemplateCloser) && Left.is(TT_TemplateCloser) &&
2108 if (Right.isOneOf(tok::arrow, tok::period, tok::arrowstar, tok::periodstar) ||
2109 Left.isOneOf(tok::arrow, tok::period, tok::arrowstar, tok::periodstar))
2111 if (!Style.SpaceBeforeAssignmentOperators &&
2114 if (Right.is(tok::coloncolon) && Left.isNot(tok::l_brace))
2115 return (Left.is(TT_TemplateOpener) &&
2117 !(Left.isOneOf(tok::identifier, tok::l_paren, tok::r_paren) ||
2118 Left.isOneOf(TT_TemplateCloser, TT_TemplateOpener));
2119 if ((Left.is(TT_TemplateOpener)) != (Right.is(TT_TemplateCloser)))
2120 return Style.SpacesInAngles;
2121 if ((Right.is(TT_BinaryOperator) && !Left.is(tok::l_paren)) ||
2122 (Left.isOneOf(TT_BinaryOperator, TT_ConditionalExpr) &&
2123 !Right.is(tok::r_paren)))
2125 if (Left.is(TT_TemplateCloser) && Right.is(tok::l_paren) &&
2126 Right.isNot(TT_FunctionTypeLParen))
2128 if (Right.is(TT_TemplateOpener) && Left.is(tok::r_paren) &&
2129 Left.MatchingParen && Left.MatchingParen->is(TT_OverloadedOperatorLParen))
2131 if (Right.is(tok::less) && Left.isNot(tok::l_paren) &&
2132 Line.startsWith(tok::hash))
2134 if (Right.is(TT_TrailingUnaryOperator))
2136 if (Left.is(TT_RegexLiteral))
2138 return spaceRequiredBetween(Line, Left, Right);
2144 !Tok.
isOneOf(TT_ObjCBlockLBrace, TT_DictLiteral);
2147 bool TokenAnnotator::mustBreakBefore(
const AnnotatedLine &Line,
2148 const FormatToken &Right) {
2149 const FormatToken &Left = *Right.Previous;
2150 if (Right.NewlinesBefore > 1 && Style.MaxEmptyLinesToKeep > 0)
2155 if (Right.is(tok::char_constant) && Left.is(tok::plus) && Left.Previous &&
2156 Left.Previous->is(tok::char_constant))
2158 if (Left.is(TT_DictLiteral) && Left.is(tok::l_brace) && Line.Level == 0 &&
2159 Left.Previous && Left.Previous->is(tok::equal) &&
2160 Line.First->isOneOf(tok::identifier, Keywords.kw_import, tok::kw_export,
2164 !Line.First->isOneOf(Keywords.kw_var, Keywords.kw_let))
2168 if (Left.is(tok::l_brace) && Line.Level == 0 &&
2169 (Line.startsWith(tok::kw_enum) ||
2170 Line.startsWith(tok::kw_export, tok::kw_enum)))
2174 if (Right.is(tok::r_brace) && Left.is(tok::l_brace) &&
2175 !Left.Children.empty())
2179 (Left.NestingLevel == 0 && Line.Level == 0 &&
2180 Style.AllowShortFunctionsOnASingleLine ==
2183 if (Right.is(tok::plus) && Left.is(tok::string_literal) && Right.Next &&
2184 Right.Next->is(tok::string_literal))
2191 const FormatToken *BeforeClosingBrace =
nullptr;
2192 if (Left.isOneOf(tok::l_brace, TT_ArrayInitializerLSquare) &&
2193 Left.BlockKind !=
BK_Block && Left.MatchingParen)
2194 BeforeClosingBrace = Left.MatchingParen->Previous;
2195 else if (Right.MatchingParen &&
2196 Right.MatchingParen->isOneOf(tok::l_brace,
2197 TT_ArrayInitializerLSquare))
2198 BeforeClosingBrace = &Left;
2199 if (BeforeClosingBrace && (BeforeClosingBrace->is(tok::comma) ||
2200 BeforeClosingBrace->isTrailingComment()))
2203 if (Right.is(tok::comment))
2205 Left.isNot(TT_CtorInitializerColon) &&
2206 (Right.NewlinesBefore > 0 && Right.HasUnescapedNewline);
2207 if (Left.isTrailingComment())
2209 if (Left.isStringLiteral() &&
2210 (Right.isStringLiteral() || Right.is(TT_ObjCStringLiteral)))
2212 if (Right.Previous->IsUnterminatedLiteral)
2214 if (Right.is(tok::lessless) && Right.Next &&
2215 Right.Previous->is(tok::string_literal) &&
2216 Right.Next->is(tok::string_literal))
2218 if (Right.Previous->ClosesTemplateDeclaration &&
2219 Right.Previous->MatchingParen &&
2220 Right.Previous->MatchingParen->NestingLevel == 0 &&
2221 Style.AlwaysBreakTemplateDeclarations)
2223 if ((Right.isOneOf(TT_CtorInitializerComma, TT_CtorInitializerColon)) &&
2224 Style.BreakConstructorInitializersBeforeComma &&
2225 !Style.ConstructorInitializerAllOnOneLineOrOnePerLine)
2227 if (Right.is(tok::string_literal) && Right.TokenText.startswith(
"R\""))
2231 return Right.NewlinesBefore > 0;
2232 if (Right.Previous->is(tok::l_brace) && Right.NestingLevel == 1 &&
2236 if (Right.is(TT_InlineASMBrace))
2237 return Right.HasUnescapedNewline;
2239 return (Line.startsWith(tok::kw_enum) && Style.BraceWrapping.AfterEnum) ||
2240 (Line.startsWith(tok::kw_class) && Style.BraceWrapping.AfterClass) ||
2241 (Line.startsWith(tok::kw_struct) && Style.BraceWrapping.AfterStruct);
2243 Right.is(TT_SelectorName))
2245 if (Left.is(TT_ObjCBlockLBrace) && !Style.AllowShortBlocksOnASingleLine)
2250 Left.is(TT_LeadingJavaAnnotation) &&
2251 Right.isNot(TT_LeadingJavaAnnotation) && Right.isNot(tok::l_paren) &&
2252 (Line.Last->is(tok::l_brace) || Style.BreakAfterJavaFieldAnnotations))
2258 bool TokenAnnotator::canBreakBefore(
const AnnotatedLine &Line,
2259 const FormatToken &Right) {
2260 const FormatToken &Left = *Right.Previous;
2264 if (Left.isOneOf(Keywords.kw_throws, Keywords.kw_extends,
2265 Keywords.kw_implements))
2267 if (Right.isOneOf(Keywords.kw_throws, Keywords.kw_extends,
2268 Keywords.kw_implements))
2271 if (Left.is(TT_JsFatArrow) && Right.is(tok::l_brace))
2273 if (Left.is(TT_JsTypeColon))
2275 if (Right.NestingLevel == 0 && Right.is(Keywords.kw_is))
2279 if (Left.is(tok::at))
2281 if (Left.Tok.getObjCKeywordID() == tok::objc_interface)
2283 if (Left.isOneOf(TT_JavaAnnotation, TT_LeadingJavaAnnotation))
2284 return !Right.is(tok::l_paren);
2285 if (Right.is(TT_PointerOrReference))
2286 return Line.IsMultiVariableDeclStmt ||
2288 (!Right.Next || Right.Next->isNot(TT_FunctionDeclarationName)));
2289 if (Right.isOneOf(TT_StartOfName, TT_FunctionDeclarationName) ||
2290 Right.is(tok::kw_operator))
2292 if (Left.is(TT_PointerOrReference))
2294 if (Right.isTrailingComment())
2301 if (Left.is(tok::question) && Right.is(tok::colon))
2303 if (Right.is(TT_ConditionalExpr) || Right.is(tok::question))
2304 return Style.BreakBeforeTernaryOperators;
2305 if (Left.is(TT_ConditionalExpr) || Left.is(tok::question))
2306 return !Style.BreakBeforeTernaryOperators;
2307 if (Right.is(TT_InheritanceColon))
2309 if (Right.is(tok::colon) &&
2310 !Right.isOneOf(TT_CtorInitializerColon, TT_InlineASMColon))
2312 if (Left.is(tok::colon) && (Left.isOneOf(TT_DictLiteral, TT_ObjCMethodExpr)))
2314 if (Right.is(TT_SelectorName) || (Right.is(tok::identifier) && Right.Next &&
2315 Right.Next->is(TT_ObjCMethodExpr)))
2316 return Left.isNot(tok::period);
2319 if (Left.ClosesTemplateDeclaration || Left.is(TT_FunctionAnnotationRParen))
2321 if (Right.isOneOf(TT_RangeBasedForLoopColon, TT_OverloadedOperatorLParen,
2322 TT_OverloadedOperator))
2324 if (Left.is(TT_RangeBasedForLoopColon))
2326 if (Right.is(TT_RangeBasedForLoopColon))
2328 if (Left.isOneOf(TT_TemplateCloser, TT_UnaryOperator) ||
2329 Left.is(tok::kw_operator))
2331 if (Left.is(tok::equal) && !Right.isOneOf(tok::kw_default, tok::kw_delete) &&
2334 if (Left.is(tok::l_paren) && Left.is(TT_AttributeParen))
2336 if (Left.is(tok::l_paren) && Left.Previous &&
2337 (Left.Previous->isOneOf(TT_BinaryOperator, TT_CastRParen)))
2339 if (Right.is(TT_ImplicitStringLiteral))
2342 if (Right.is(tok::r_paren) || Right.is(TT_TemplateCloser))
2344 if (Right.is(tok::r_square) && Right.MatchingParen &&
2345 Right.MatchingParen->is(TT_LambdaLSquare))
2350 if (Right.is(tok::r_brace))
2351 return Right.MatchingParen && Right.MatchingParen->BlockKind ==
BK_Block;
2355 if (Left.is(TT_TrailingAnnotation))
2356 return !Right.isOneOf(tok::l_brace, tok::semi, tok::equal, tok::l_paren,
2357 tok::less, tok::coloncolon);
2359 if (Right.is(tok::kw___attribute))
2362 if (Left.is(tok::identifier) && Right.is(tok::string_literal))
2365 if (Right.is(tok::identifier) && Right.Next && Right.Next->is(TT_DictLiteral))
2368 if (Left.is(TT_CtorInitializerComma) &&
2369 Style.BreakConstructorInitializersBeforeComma)
2371 if (Right.is(TT_CtorInitializerComma) &&
2372 Style.BreakConstructorInitializersBeforeComma)
2374 if ((Left.is(tok::greater) && Right.is(tok::greater)) ||
2375 (Left.is(tok::less) && Right.is(tok::less)))
2377 if (Right.is(TT_BinaryOperator) &&
2382 if (Left.is(TT_ArrayInitializerLSquare))
2384 if (Right.is(tok::kw_typename) && Left.isNot(tok::kw_const))
2386 if ((Left.isBinaryOperator() || Left.is(TT_BinaryOperator)) &&
2387 !Left.isOneOf(tok::arrowstar, tok::lessless) &&
2392 return Left.isOneOf(tok::comma, tok::coloncolon, tok::semi, tok::l_brace,
2393 tok::kw_class, tok::kw_struct) ||
2394 Right.isMemberAccess() ||
2395 Right.isOneOf(TT_TrailingReturnArrow, TT_LambdaArrow, tok::lessless,
2396 tok::colon, tok::l_square, tok::at) ||
2397 (Left.is(tok::r_paren) &&
2398 Right.isOneOf(tok::identifier, tok::kw_const)) ||
2399 (Left.is(tok::l_paren) && !Right.is(tok::r_paren));
2402 void TokenAnnotator::printDebugInfo(
const AnnotatedLine &Line) {
2403 llvm::errs() <<
"AnnotatedTokens:\n";
2404 const FormatToken *Tok = Line.First;
2406 llvm::errs() <<
" M=" << Tok->MustBreakBefore
2407 <<
" C=" << Tok->CanBreakBefore
2409 <<
" S=" << Tok->SpacesRequiredBefore
2410 <<
" B=" << Tok->BlockParameterCount
2411 <<
" P=" << Tok->SplitPenalty <<
" Name=" << Tok->Tok.getName()
2412 <<
" L=" << Tok->TotalLength <<
" PPK=" << Tok->PackingKind
2414 for (
unsigned i = 0, e = Tok->FakeLParens.size(); i != e; ++i)
2415 llvm::errs() << Tok->FakeLParens[i] <<
"/";
2416 llvm::errs() <<
" FakeRParens=" << Tok->FakeRParens <<
"\n";
2418 assert(Tok == Line.Last);
2421 llvm::errs() <<
"----\n";
Defines the SourceManager interface.
Parser - This implements a parser for the C family of languages.
FormatToken * CurrentToken
tok::TokenKind ContextKind
This file implements a token annotator, i.e.
llvm::SmallPtrSet< FormatToken *, 16 > NonTemplateLess
detail::InMemoryDirectory::const_iterator I
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
detail::InMemoryDirectory::const_iterator E
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.