Skip to content

Commit 8c31b68

Browse files
committed
[clang-format] Use an enum for context types. NFC
We currently have all those fields in AnnotatingParser::Context. They are not inherited from the Context object for the parent scope. They are exclusive. Now they are replaced with an enum. `InCpp11AttributeSpecifier` and `InCSharpAttributeSpecifier` are not handled like the rest in ContextType because they are not exclusive. Reviewed By: curdeius, MyDeveloperDay, HazardyKnusperkeks, owenpan Differential Revision: https://reviews.llvm.org/D121907
1 parent d4aeb50 commit 8c31b68

File tree

1 file changed

+42
-24
lines changed

1 file changed

+42
-24
lines changed

clang/lib/Format/TokenAnnotator.cpp

Lines changed: 42 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,8 @@ class AnnotatingParser {
113113
Contexts.back().IsExpression = false;
114114
// If there's a template keyword before the opening angle bracket, this is a
115115
// 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;
118118

119119
if (Style.Language == FormatStyle::LK_Java &&
120120
CurrentToken->is(tok::question))
@@ -288,7 +288,7 @@ class AnnotatingParser {
288288
} else if (OpeningParen.Previous &&
289289
OpeningParen.Previous->is(TT_ForEachMacro)) {
290290
// The first argument to a foreach macro is a declaration.
291-
Contexts.back().IsForEachMacro = true;
291+
Contexts.back().ContextType = Context::ForEachMacro;
292292
Contexts.back().IsExpression = false;
293293
} else if (OpeningParen.Previous && OpeningParen.Previous->MatchingParen &&
294294
OpeningParen.Previous->MatchingParen->is(TT_ObjCBlockLParen)) {
@@ -558,7 +558,7 @@ class AnnotatingParser {
558558
bool CppArrayTemplates =
559559
Style.isCpp() && Parent && Parent->is(TT_TemplateCloser) &&
560560
(Contexts.back().CanBeExpression || Contexts.back().IsExpression ||
561-
Contexts.back().InTemplateArgument);
561+
Contexts.back().ContextType == Context::TemplateArgument);
562562

563563
bool IsCpp11AttributeSpecifier = isCpp11AttributeSpecifier(*Left) ||
564564
Contexts.back().InCpp11AttributeSpecifier;
@@ -803,7 +803,7 @@ class AnnotatingParser {
803803
if (Style.AlignArrayOfStructures != FormatStyle::AIAS_None) {
804804
if (OpeningBrace.ParentBracket == tok::l_brace &&
805805
couldBeInStructArrayInitializer() && CommaCount > 0)
806-
Contexts.back().InStructArrayInitializer = true;
806+
Contexts.back().ContextType = Context::StructArrayInitializer;
807807
}
808808
next();
809809
return true;
@@ -1157,16 +1157,22 @@ class AnnotatingParser {
11571157
parseTemplateDeclaration();
11581158
break;
11591159
case tok::comma:
1160-
if (Contexts.back().InCtorInitializer)
1160+
switch (Contexts.back().ContextType) {
1161+
case Context::CtorInitializer:
11611162
Tok->setType(TT_CtorInitializerComma);
1162-
else if (Contexts.back().InInheritanceList)
1163+
break;
1164+
case Context::InheritanceList:
11631165
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;
11681174
}
1169-
if (Contexts.back().IsForEachMacro)
1175+
if (Contexts.back().ContextType == Context::ForEachMacro)
11701176
Contexts.back().IsExpression = true;
11711177
break;
11721178
case tok::identifier:
@@ -1411,7 +1417,7 @@ class AnnotatingParser {
14111417
}
14121418

14131419
for (const auto &ctx : Contexts)
1414-
if (ctx.InStructArrayInitializer)
1420+
if (ctx.ContextType == Context::StructArrayInitializer)
14151421
return LT_ArrayOfStructInitializer;
14161422

14171423
return LT_Other;
@@ -1488,14 +1494,25 @@ class AnnotatingParser {
14881494
FormatToken *FirstObjCSelectorName = nullptr;
14891495
FormatToken *FirstStartOfName = nullptr;
14901496
bool CanBeExpression = true;
1491-
bool InTemplateArgument = false;
1492-
bool InCtorInitializer = false;
1493-
bool InInheritanceList = false;
14941497
bool CaretFound = false;
1495-
bool IsForEachMacro = false;
14961498
bool InCpp11AttributeSpecifier = false;
14971499
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;
14991516
};
15001517

15011518
/// Puts a new \c Context onto the stack \c Contexts for the lifetime
@@ -1513,9 +1530,9 @@ class AnnotatingParser {
15131530

15141531
~ScopedContextCreator() {
15151532
if (P.Style.AlignArrayOfStructures != FormatStyle::AIAS_None) {
1516-
if (P.Contexts.back().InStructArrayInitializer) {
1533+
if (P.Contexts.back().ContextType == Context::StructArrayInitializer) {
15171534
P.Contexts.pop_back();
1518-
P.Contexts.back().InStructArrayInitializer = true;
1535+
P.Contexts.back().ContextType = Context::StructArrayInitializer;
15191536
return;
15201537
}
15211538
}
@@ -1601,15 +1618,16 @@ class AnnotatingParser {
16011618
} else if (Current.Previous &&
16021619
Current.Previous->is(TT_CtorInitializerColon)) {
16031620
Contexts.back().IsExpression = true;
1604-
Contexts.back().InCtorInitializer = true;
1621+
Contexts.back().ContextType = Context::CtorInitializer;
16051622
} else if (Current.Previous && Current.Previous->is(TT_InheritanceColon)) {
1606-
Contexts.back().InInheritanceList = true;
1623+
Contexts.back().ContextType = Context::InheritanceList;
16071624
} else if (Current.isOneOf(tok::r_paren, tok::greater, tok::comma)) {
16081625
for (FormatToken *Previous = Current.Previous;
16091626
Previous && Previous->isOneOf(tok::star, tok::amp);
16101627
Previous = Previous->Previous)
16111628
Previous->setType(TT_PointerOrReference);
1612-
if (Line.MustBeDeclaration && !Contexts.front().InCtorInitializer)
1629+
if (Line.MustBeDeclaration &&
1630+
Contexts.front().ContextType != Context::CtorInitializer)
16131631
Contexts.back().IsExpression = false;
16141632
} else if (Current.is(tok::kw_new)) {
16151633
Contexts.back().CanBeExpression = false;
@@ -1756,7 +1774,7 @@ class AnnotatingParser {
17561774
Current.setType(determineStarAmpUsage(
17571775
Current,
17581776
Contexts.back().CanBeExpression && Contexts.back().IsExpression,
1759-
Contexts.back().InTemplateArgument));
1777+
Contexts.back().ContextType == Context::TemplateArgument));
17601778
} else if (Current.isOneOf(tok::minus, tok::plus, tok::caret)) {
17611779
Current.setType(determinePlusMinusCaretUsage(Current));
17621780
if (Current.is(TT_UnaryOperator) && Current.is(tok::caret))

0 commit comments

Comments
 (0)