Skip to content

Commit 3f0a530

Browse files
authored
[clang-format][NFC] Clean up AnnotatingParser::consumeToken() (#142104)
1 parent d9dc357 commit 3f0a530

File tree

1 file changed

+43
-58
lines changed

1 file changed

+43
-58
lines changed

clang/lib/Format/TokenAnnotator.cpp

Lines changed: 43 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1306,14 +1306,15 @@ class AnnotatingParser {
13061306
// Multi-line string itself is a single annotated token.
13071307
if (Tok->is(TT_TableGenMultiLineString))
13081308
return true;
1309+
auto *Prev = Tok->getPreviousNonComment();
13091310
switch (bool IsIf = false; Tok->Tok.getKind()) {
13101311
case tok::plus:
13111312
case tok::minus:
1312-
if (!Tok->getPreviousNonComment() && Line.MustBeDeclaration)
1313+
if (!Prev && Line.MustBeDeclaration)
13131314
Tok->setType(TT_ObjCMethodSpecifier);
13141315
break;
13151316
case tok::colon:
1316-
if (!Tok->Previous)
1317+
if (!Prev)
13171318
return false;
13181319
// Goto labels and case labels are already identified in
13191320
// UnwrappedLineParser.
@@ -1346,8 +1347,7 @@ class AnnotatingParser {
13461347
} else if (Style.isVerilog() && Tok->isNot(TT_BinaryOperator)) {
13471348
// The distribution weight operators are labeled
13481349
// TT_BinaryOperator by the lexer.
1349-
if (Keywords.isVerilogEnd(*Tok->Previous) ||
1350-
Keywords.isVerilogBegin(*Tok->Previous)) {
1350+
if (Keywords.isVerilogEnd(*Prev) || Keywords.isVerilogBegin(*Prev)) {
13511351
Tok->setType(TT_VerilogBlockLabelColon);
13521352
} else if (Contexts.back().ContextKind == tok::l_square) {
13531353
Tok->setType(TT_BitFieldColon);
@@ -1371,50 +1371,43 @@ class AnnotatingParser {
13711371
Tok->setType(TT_InlineASMColon);
13721372
} else if (Contexts.back().ColonIsDictLiteral || Style.isProto()) {
13731373
Tok->setType(TT_DictLiteral);
1374-
if (Style.isTextProto()) {
1375-
if (FormatToken *Previous = Tok->getPreviousNonComment())
1376-
Previous->setType(TT_SelectorName);
1377-
}
1374+
if (Prev && Style.isTextProto())
1375+
Prev->setType(TT_SelectorName);
13781376
} else if (Contexts.back().ColonIsObjCMethodExpr ||
13791377
Line.startsWith(TT_ObjCMethodSpecifier)) {
13801378
Tok->setType(TT_ObjCMethodExpr);
1381-
const FormatToken *BeforePrevious = Tok->Previous->Previous;
1379+
const auto *PrevPrev = Prev->Previous;
13821380
// Ensure we tag all identifiers in method declarations as
13831381
// TT_SelectorName.
13841382
bool UnknownIdentifierInMethodDeclaration =
13851383
Line.startsWith(TT_ObjCMethodSpecifier) &&
1386-
Tok->Previous->is(tok::identifier) && Tok->Previous->is(TT_Unknown);
1387-
if (!BeforePrevious ||
1384+
Prev->is(tok::identifier) && Prev->is(TT_Unknown);
1385+
if (!PrevPrev ||
13881386
// FIXME(bug 36976): ObjC return types shouldn't use TT_CastRParen.
1389-
!(BeforePrevious->is(TT_CastRParen) ||
1390-
(BeforePrevious->is(TT_ObjCMethodExpr) &&
1391-
BeforePrevious->is(tok::colon))) ||
1392-
BeforePrevious->is(tok::r_square) ||
1387+
!(PrevPrev->is(TT_CastRParen) ||
1388+
(PrevPrev->is(TT_ObjCMethodExpr) && PrevPrev->is(tok::colon))) ||
1389+
PrevPrev->is(tok::r_square) ||
13931390
Contexts.back().LongestObjCSelectorName == 0 ||
13941391
UnknownIdentifierInMethodDeclaration) {
1395-
Tok->Previous->setType(TT_SelectorName);
1396-
if (!Contexts.back().FirstObjCSelectorName) {
1397-
Contexts.back().FirstObjCSelectorName = Tok->Previous;
1398-
} else if (Tok->Previous->ColumnWidth >
1399-
Contexts.back().LongestObjCSelectorName) {
1400-
Contexts.back().LongestObjCSelectorName =
1401-
Tok->Previous->ColumnWidth;
1402-
}
1403-
Tok->Previous->ParameterIndex =
1392+
Prev->setType(TT_SelectorName);
1393+
if (!Contexts.back().FirstObjCSelectorName)
1394+
Contexts.back().FirstObjCSelectorName = Prev;
1395+
else if (Prev->ColumnWidth > Contexts.back().LongestObjCSelectorName)
1396+
Contexts.back().LongestObjCSelectorName = Prev->ColumnWidth;
1397+
Prev->ParameterIndex =
14041398
Contexts.back().FirstObjCSelectorName->ObjCSelectorNameParts;
14051399
++Contexts.back().FirstObjCSelectorName->ObjCSelectorNameParts;
14061400
}
14071401
} else if (Contexts.back().ColonIsForRangeExpr) {
14081402
Tok->setType(TT_RangeBasedForLoopColon);
1409-
for (auto *Prev = Tok->Previous;
1410-
Prev && !Prev->isOneOf(tok::semi, tok::l_paren);
1411-
Prev = Prev->Previous) {
1412-
if (Prev->isPointerOrReference())
1413-
Prev->setFinalizedType(TT_PointerOrReference);
1403+
for (auto *Token = Prev;
1404+
Token && !Token->isOneOf(tok::semi, tok::l_paren);
1405+
Token = Token->Previous) {
1406+
if (Token->isPointerOrReference())
1407+
Token->setFinalizedType(TT_PointerOrReference);
14141408
}
14151409
} else if (Contexts.back().ContextType == Context::C11GenericSelection) {
14161410
Tok->setType(TT_GenericSelectionColon);
1417-
auto *Prev = Tok->getPreviousNonComment();
14181411
assert(Prev);
14191412
if (Prev->isPointerOrReference())
14201413
Prev->setFinalizedType(TT_PointerOrReference);
@@ -1424,7 +1417,6 @@ class AnnotatingParser {
14241417
!Line.getFirstNonComment()->isOneOf(tok::kw_enum, tok::kw_case,
14251418
tok::kw_default) &&
14261419
!Line.startsWith(tok::kw_typedef, tok::kw_enum)) {
1427-
FormatToken *Prev = Tok->getPreviousNonComment();
14281420
if (!Prev)
14291421
break;
14301422
if (Prev->isOneOf(tok::r_paren, tok::kw_noexcept) ||
@@ -1442,7 +1434,7 @@ class AnnotatingParser {
14421434
if (Prev->isAccessSpecifierKeyword())
14431435
Line.Type = LT_AccessModifier;
14441436
}
1445-
} else if (canBeObjCSelectorComponent(*Tok->Previous) && Tok->Next &&
1437+
} else if (canBeObjCSelectorComponent(*Prev) && Tok->Next &&
14461438
(Tok->Next->isOneOf(tok::r_paren, tok::comma) ||
14471439
(canBeObjCSelectorComponent(*Tok->Next) && Tok->Next->Next &&
14481440
Tok->Next->Next->is(tok::colon)))) {
@@ -1483,7 +1475,7 @@ class AnnotatingParser {
14831475
case tok::kw_for:
14841476
if (Style.isJavaScript()) {
14851477
// x.for and {for: ...}
1486-
if ((Tok->Previous && Tok->Previous->is(tok::period)) ||
1478+
if ((Prev && Prev->is(tok::period)) ||
14871479
(Tok->Next && Tok->Next->is(tok::colon))) {
14881480
break;
14891481
}
@@ -1505,11 +1497,10 @@ class AnnotatingParser {
15051497
// marks the first l_paren as a OverloadedOperatorLParen. Here, we make
15061498
// the first two parens OverloadedOperators and the second l_paren an
15071499
// OverloadedOperatorLParen.
1508-
if (Tok->Previous && Tok->Previous->is(tok::r_paren) &&
1509-
Tok->Previous->MatchingParen &&
1510-
Tok->Previous->MatchingParen->is(TT_OverloadedOperatorLParen)) {
1511-
Tok->Previous->setType(TT_OverloadedOperator);
1512-
Tok->Previous->MatchingParen->setType(TT_OverloadedOperator);
1500+
if (Prev && Prev->is(tok::r_paren) && Prev->MatchingParen &&
1501+
Prev->MatchingParen->is(TT_OverloadedOperatorLParen)) {
1502+
Prev->setType(TT_OverloadedOperator);
1503+
Prev->MatchingParen->setType(TT_OverloadedOperator);
15131504
Tok->setType(TT_OverloadedOperatorLParen);
15141505
}
15151506

@@ -1520,7 +1511,6 @@ class AnnotatingParser {
15201511
// function is only responsible for the definition, not the
15211512
// instantiation.
15221513
auto IsInstancePort = [&]() {
1523-
const FormatToken *Prev = Tok->getPreviousNonComment();
15241514
const FormatToken *PrevPrev;
15251515
// In the following example all 4 left parentheses will be treated as
15261516
// 'TT_VerilogInstancePortLParen'.
@@ -1566,11 +1556,10 @@ class AnnotatingParser {
15661556
!Contexts.back().IsExpression && !Line.startsWith(TT_ObjCProperty) &&
15671557
!Line.startsWith(tok::l_paren) &&
15681558
!Tok->isOneOf(TT_TypeDeclarationParen, TT_RequiresExpressionLParen)) {
1569-
if (const auto *Previous = Tok->Previous;
1570-
!Previous ||
1571-
(!Previous->isAttribute() &&
1572-
!Previous->isOneOf(TT_RequiresClause, TT_LeadingJavaAnnotation,
1573-
TT_BinaryOperator))) {
1559+
if (!Prev ||
1560+
(!Prev->isAttribute() &&
1561+
!Prev->isOneOf(TT_RequiresClause, TT_LeadingJavaAnnotation,
1562+
TT_BinaryOperator))) {
15741563
Line.MightBeFunctionDecl = true;
15751564
Tok->MightBeFunctionDeclParen = true;
15761565
}
@@ -1587,9 +1576,8 @@ class AnnotatingParser {
15871576
if (Tok->is(TT_RequiresExpressionLBrace))
15881577
Line.Type = LT_RequiresExpression;
15891578
} else if (Style.isTextProto()) {
1590-
FormatToken *Previous = Tok->getPreviousNonComment();
1591-
if (Previous && Previous->isNot(TT_DictLiteral))
1592-
Previous->setType(TT_SelectorName);
1579+
if (Prev && Prev->isNot(TT_DictLiteral))
1580+
Prev->setType(TT_SelectorName);
15931581
}
15941582
Scopes.push_back(getScopeType(*Tok));
15951583
if (!parseBrace())
@@ -1604,12 +1592,11 @@ class AnnotatingParser {
16041592
// msg: < item: data >
16051593
// In TT_TextProto, map<key, value> does not occur.
16061594
if (Style.isTextProto() ||
1607-
(Style.Language == FormatStyle::LK_Proto && Tok->Previous &&
1608-
Tok->Previous->isOneOf(TT_SelectorName, TT_DictLiteral))) {
1595+
(Style.Language == FormatStyle::LK_Proto && Prev &&
1596+
Prev->isOneOf(TT_SelectorName, TT_DictLiteral))) {
16091597
Tok->setType(TT_DictLiteral);
1610-
FormatToken *Previous = Tok->getPreviousNonComment();
1611-
if (Previous && Previous->isNot(TT_DictLiteral))
1612-
Previous->setType(TT_SelectorName);
1598+
if (Prev && Prev->isNot(TT_DictLiteral))
1599+
Prev->setType(TT_SelectorName);
16131600
}
16141601
if (Style.isTableGen())
16151602
Tok->setType(TT_TemplateOpener);
@@ -1628,13 +1615,13 @@ class AnnotatingParser {
16281615
if (!Scopes.empty())
16291616
Scopes.pop_back();
16301617
// Lines can start with '}'.
1631-
if (Tok->Previous)
1618+
if (Prev)
16321619
return false;
16331620
break;
16341621
case tok::greater:
16351622
if (!Style.isTextProto() && Tok->is(TT_Unknown))
16361623
Tok->setType(TT_BinaryOperator);
1637-
if (Tok->Previous && Tok->Previous->is(TT_TemplateCloser))
1624+
if (Prev && Prev->is(TT_TemplateCloser))
16381625
Tok->SpacesRequiredBefore = 1;
16391626
break;
16401627
case tok::kw_operator:
@@ -1789,7 +1776,7 @@ class AnnotatingParser {
17891776
Tok->Next->isNot(tok::l_paren)) {
17901777
Tok->setType(TT_CSharpGenericTypeConstraint);
17911778
parseCSharpGenericTypeConstraint();
1792-
if (!Tok->getPreviousNonComment())
1779+
if (!Prev)
17931780
Line.IsContinuation = true;
17941781
}
17951782
if (Style.isTableGen()) {
@@ -1806,10 +1793,8 @@ class AnnotatingParser {
18061793
}
18071794
break;
18081795
case tok::arrow:
1809-
if (Tok->isNot(TT_LambdaArrow) && Tok->Previous &&
1810-
Tok->Previous->is(tok::kw_noexcept)) {
1796+
if (Tok->isNot(TT_LambdaArrow) && Prev && Prev->is(tok::kw_noexcept))
18111797
Tok->setType(TT_TrailingReturnArrow);
1812-
}
18131798
break;
18141799
case tok::equal:
18151800
// In TableGen, there must be a value after "=";

0 commit comments

Comments
 (0)