@@ -113,8 +113,8 @@ class AnnotatingParser {
113
113
Contexts.back ().IsExpression = false ;
114
114
// If there's a template keyword before the opening angle bracket, this is a
115
115
// template parameter, not an argument.
116
- Contexts. back (). InTemplateArgument =
117
- Left-> Previous && Left-> Previous -> isNot (tok::kw_template) ;
116
+ if (Left-> Previous && Left-> Previous -> isNot (tok::kw_template))
117
+ Contexts. back (). ContextType = Context::TemplateArgument ;
118
118
119
119
if (Style.Language == FormatStyle::LK_Java &&
120
120
CurrentToken->is (tok::question))
@@ -288,7 +288,7 @@ class AnnotatingParser {
288
288
} else if (OpeningParen.Previous &&
289
289
OpeningParen.Previous ->is (TT_ForEachMacro)) {
290
290
// The first argument to a foreach macro is a declaration.
291
- Contexts.back ().IsForEachMacro = true ;
291
+ Contexts.back ().ContextType = Context::ForEachMacro ;
292
292
Contexts.back ().IsExpression = false ;
293
293
} else if (OpeningParen.Previous && OpeningParen.Previous ->MatchingParen &&
294
294
OpeningParen.Previous ->MatchingParen ->is (TT_ObjCBlockLParen)) {
@@ -558,7 +558,7 @@ class AnnotatingParser {
558
558
bool CppArrayTemplates =
559
559
Style.isCpp () && Parent && Parent->is (TT_TemplateCloser) &&
560
560
(Contexts.back ().CanBeExpression || Contexts.back ().IsExpression ||
561
- Contexts.back ().InTemplateArgument );
561
+ Contexts.back ().ContextType == Context::TemplateArgument );
562
562
563
563
bool IsCpp11AttributeSpecifier = isCpp11AttributeSpecifier (*Left) ||
564
564
Contexts.back ().InCpp11AttributeSpecifier ;
@@ -803,7 +803,7 @@ class AnnotatingParser {
803
803
if (Style.AlignArrayOfStructures != FormatStyle::AIAS_None) {
804
804
if (OpeningBrace.ParentBracket == tok::l_brace &&
805
805
couldBeInStructArrayInitializer () && CommaCount > 0 )
806
- Contexts.back ().InStructArrayInitializer = true ;
806
+ Contexts.back ().ContextType = Context::StructArrayInitializer ;
807
807
}
808
808
next ();
809
809
return true ;
@@ -1157,16 +1157,22 @@ class AnnotatingParser {
1157
1157
parseTemplateDeclaration ();
1158
1158
break ;
1159
1159
case tok::comma:
1160
- if (Contexts.back ().InCtorInitializer )
1160
+ switch (Contexts.back ().ContextType ) {
1161
+ case Context::CtorInitializer:
1161
1162
Tok->setType (TT_CtorInitializerComma);
1162
- else if (Contexts.back ().InInheritanceList )
1163
+ break ;
1164
+ case Context::InheritanceList:
1163
1165
Tok->setType (TT_InheritanceComma);
1164
- else if (Contexts.back ().FirstStartOfName &&
1165
- (Contexts.size () == 1 || startsWithInitStatement (Line))) {
1166
- Contexts.back ().FirstStartOfName ->PartOfMultiVariableDeclStmt = true ;
1167
- Line.IsMultiVariableDeclStmt = true ;
1166
+ break ;
1167
+ default :
1168
+ if (Contexts.back ().FirstStartOfName &&
1169
+ (Contexts.size () == 1 || startsWithInitStatement (Line))) {
1170
+ Contexts.back ().FirstStartOfName ->PartOfMultiVariableDeclStmt = true ;
1171
+ Line.IsMultiVariableDeclStmt = true ;
1172
+ }
1173
+ break ;
1168
1174
}
1169
- if (Contexts.back ().IsForEachMacro )
1175
+ if (Contexts.back ().ContextType == Context::ForEachMacro )
1170
1176
Contexts.back ().IsExpression = true ;
1171
1177
break ;
1172
1178
case tok::identifier:
@@ -1411,7 +1417,7 @@ class AnnotatingParser {
1411
1417
}
1412
1418
1413
1419
for (const auto &ctx : Contexts)
1414
- if (ctx.InStructArrayInitializer )
1420
+ if (ctx.ContextType == Context::StructArrayInitializer )
1415
1421
return LT_ArrayOfStructInitializer;
1416
1422
1417
1423
return LT_Other;
@@ -1488,14 +1494,25 @@ class AnnotatingParser {
1488
1494
FormatToken *FirstObjCSelectorName = nullptr ;
1489
1495
FormatToken *FirstStartOfName = nullptr ;
1490
1496
bool CanBeExpression = true ;
1491
- bool InTemplateArgument = false ;
1492
- bool InCtorInitializer = false ;
1493
- bool InInheritanceList = false ;
1494
1497
bool CaretFound = false ;
1495
- bool IsForEachMacro = false ;
1496
1498
bool InCpp11AttributeSpecifier = false ;
1497
1499
bool InCSharpAttributeSpecifier = false ;
1498
- bool InStructArrayInitializer = false ;
1500
+ enum {
1501
+ Unknown,
1502
+ // Like the part after `:` in a constructor.
1503
+ // Context(...) : IsExpression(IsExpression)
1504
+ CtorInitializer,
1505
+ // Like in the parentheses in a foreach.
1506
+ ForEachMacro,
1507
+ // Like the inheritance list in a class declaration.
1508
+ // class Input : public IO
1509
+ InheritanceList,
1510
+ // Like in the braced list.
1511
+ // int x[] = {};
1512
+ StructArrayInitializer,
1513
+ // Like in `static_cast<int>`.
1514
+ TemplateArgument,
1515
+ } ContextType = Unknown;
1499
1516
};
1500
1517
1501
1518
// / Puts a new \c Context onto the stack \c Contexts for the lifetime
@@ -1513,9 +1530,9 @@ class AnnotatingParser {
1513
1530
1514
1531
~ScopedContextCreator () {
1515
1532
if (P.Style .AlignArrayOfStructures != FormatStyle::AIAS_None) {
1516
- if (P.Contexts .back ().InStructArrayInitializer ) {
1533
+ if (P.Contexts .back ().ContextType == Context::StructArrayInitializer ) {
1517
1534
P.Contexts .pop_back ();
1518
- P.Contexts .back ().InStructArrayInitializer = true ;
1535
+ P.Contexts .back ().ContextType = Context::StructArrayInitializer ;
1519
1536
return ;
1520
1537
}
1521
1538
}
@@ -1601,15 +1618,16 @@ class AnnotatingParser {
1601
1618
} else if (Current.Previous &&
1602
1619
Current.Previous ->is (TT_CtorInitializerColon)) {
1603
1620
Contexts.back ().IsExpression = true ;
1604
- Contexts.back ().InCtorInitializer = true ;
1621
+ Contexts.back ().ContextType = Context::CtorInitializer ;
1605
1622
} else if (Current.Previous && Current.Previous ->is (TT_InheritanceColon)) {
1606
- Contexts.back ().InInheritanceList = true ;
1623
+ Contexts.back ().ContextType = Context::InheritanceList ;
1607
1624
} else if (Current.isOneOf (tok::r_paren, tok::greater, tok::comma)) {
1608
1625
for (FormatToken *Previous = Current.Previous ;
1609
1626
Previous && Previous->isOneOf (tok::star, tok::amp);
1610
1627
Previous = Previous->Previous )
1611
1628
Previous->setType (TT_PointerOrReference);
1612
- if (Line.MustBeDeclaration && !Contexts.front ().InCtorInitializer )
1629
+ if (Line.MustBeDeclaration &&
1630
+ Contexts.front ().ContextType != Context::CtorInitializer)
1613
1631
Contexts.back ().IsExpression = false ;
1614
1632
} else if (Current.is (tok::kw_new)) {
1615
1633
Contexts.back ().CanBeExpression = false ;
@@ -1756,7 +1774,7 @@ class AnnotatingParser {
1756
1774
Current.setType (determineStarAmpUsage (
1757
1775
Current,
1758
1776
Contexts.back ().CanBeExpression && Contexts.back ().IsExpression ,
1759
- Contexts.back ().InTemplateArgument ));
1777
+ Contexts.back ().ContextType == Context::TemplateArgument ));
1760
1778
} else if (Current.isOneOf (tok::minus, tok::plus, tok::caret)) {
1761
1779
Current.setType (determinePlusMinusCaretUsage (Current));
1762
1780
if (Current.is (TT_UnaryOperator) && Current.is (tok::caret))
0 commit comments