[clang] [clang-format] More consumeToken() cleanup similar to #142104 (PR #143063)
Owen Pan via cfe-commits
cfe-commits at lists.llvm.org
Thu Jun 5 21:46:22 PDT 2025
https://github.com/owenca created https://github.com/llvm/llvm-project/pull/143063
None
>From af43a40d33dd7af17b852cfd60df3c9cd1594935 Mon Sep 17 00:00:00 2001
From: Owen Pan <owenpiano at gmail.com>
Date: Thu, 5 Jun 2025 21:39:49 -0700
Subject: [PATCH] [clang-format] More consumeToken() cleanup similar to #142104
---
clang/lib/Format/TokenAnnotator.cpp | 35 +++++++++++++----------------
1 file changed, 15 insertions(+), 20 deletions(-)
diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp
index da279d07b5918..7c102ca2c6a61 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -1306,6 +1306,7 @@ class AnnotatingParser {
if (Tok->is(TT_TableGenMultiLineString))
return true;
auto *Prev = Tok->getPreviousNonComment();
+ auto *Next = Tok->getNextNonComment();
switch (bool IsIf = false; Tok->Tok.getKind()) {
case tok::plus:
case tok::minus:
@@ -1435,10 +1436,10 @@ class AnnotatingParser {
if (Prev->isAccessSpecifierKeyword())
Line.Type = LT_AccessModifier;
}
- } else if (canBeObjCSelectorComponent(*Prev) && Tok->Next &&
- (Tok->Next->isOneOf(tok::r_paren, tok::comma) ||
- (canBeObjCSelectorComponent(*Tok->Next) && Tok->Next->Next &&
- Tok->Next->Next->is(tok::colon)))) {
+ } else if (canBeObjCSelectorComponent(*Prev) && Next &&
+ (Next->isOneOf(tok::r_paren, tok::comma) ||
+ (canBeObjCSelectorComponent(*Next) && Next->Next &&
+ Next->Next->is(tok::colon)))) {
// This handles a special macro in ObjC code where selectors including
// the colon are passed as macro arguments.
Tok->setType(TT_ObjCMethodExpr);
@@ -1476,10 +1477,8 @@ class AnnotatingParser {
case tok::kw_for:
if (Style.isJavaScript()) {
// x.for and {for: ...}
- if ((Prev && Prev->is(tok::period)) ||
- (Tok->Next && Tok->Next->is(tok::colon))) {
+ if ((Prev && Prev->is(tok::period)) || (Next && Next->is(tok::colon)))
break;
- }
// JS' for await ( ...
if (CurrentToken && CurrentToken->is(Keywords.kw_await))
next();
@@ -1690,9 +1689,9 @@ class AnnotatingParser {
CurrentToken->Previous->setType(TT_OverloadedOperator);
break;
case tok::question:
- if (Style.isJavaScript() && Tok->Next &&
- Tok->Next->isOneOf(tok::semi, tok::comma, tok::colon, tok::r_paren,
- tok::r_brace, tok::r_square)) {
+ if (Style.isJavaScript() && Next &&
+ Next->isOneOf(tok::semi, tok::comma, tok::colon, tok::r_paren,
+ tok::r_brace, tok::r_square)) {
// Question marks before semicolons, colons, etc. indicate optional
// types (fields, parameters), e.g.
// function(x?: string, y?) {...}
@@ -1709,8 +1708,7 @@ class AnnotatingParser {
if (Style.isCSharp()) {
// `Type?)`, `Type?>`, `Type? name;`, and `Type? name =` can only be
// nullable types.
- if (const auto *Next = Tok->getNextNonComment();
- Next && (Next->isOneOf(tok::r_paren, tok::greater) ||
+ if (Next && (Next->isOneOf(tok::r_paren, tok::greater) ||
Next->startsSequence(tok::identifier, tok::semi) ||
Next->startsSequence(tok::identifier, tok::equal))) {
Tok->setType(TT_CSharpNullable);
@@ -1723,10 +1721,8 @@ class AnnotatingParser {
// cond ? id : "B";
// cond ? cond2 ? "A" : "B" : "C";
if (!Contexts.back().IsExpression && Line.MustBeDeclaration &&
- (!Tok->Next ||
- !Tok->Next->isOneOf(tok::identifier, tok::string_literal) ||
- !Tok->Next->Next ||
- !Tok->Next->Next->isOneOf(tok::colon, tok::question))) {
+ (!Next || !Next->isOneOf(tok::identifier, tok::string_literal) ||
+ !Next->Next || !Next->Next->isOneOf(tok::colon, tok::question))) {
Tok->setType(TT_CSharpNullable);
break;
}
@@ -1773,8 +1769,8 @@ class AnnotatingParser {
Keywords.kw___has_include_next)) {
parseHasInclude();
}
- if (Style.isCSharp() && Tok->is(Keywords.kw_where) && Tok->Next &&
- Tok->Next->isNot(tok::l_paren)) {
+ if (Style.isCSharp() && Tok->is(Keywords.kw_where) && Next &&
+ Next->isNot(tok::l_paren)) {
Tok->setType(TT_CSharpGenericTypeConstraint);
parseCSharpGenericTypeConstraint();
if (!Prev)
@@ -1785,8 +1781,7 @@ class AnnotatingParser {
if (!parseTableGenValue())
return false;
} else if (Tok->isOneOf(Keywords.kw_def, Keywords.kw_defm) &&
- (!Tok->Next ||
- !Tok->Next->isOneOf(tok::colon, tok::l_brace))) {
+ (!Next || !Next->isOneOf(tok::colon, tok::l_brace))) {
// The case NameValue appears.
if (!parseTableGenValue(true))
return false;
More information about the cfe-commits
mailing list