Skip to content

[clang-format] More consumeToken() cleanup #143063

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 6, 2025
Merged

[clang-format] More consumeToken() cleanup #143063

merged 1 commit into from
Jun 6, 2025

Conversation

owenca
Copy link
Contributor

@owenca owenca commented Jun 6, 2025

Similar to #142104

@llvmbot
Copy link
Member

llvmbot commented Jun 6, 2025

@llvm/pr-subscribers-clang-format

Author: Owen Pan (owenca)

Changes

Full diff: https://github.com/llvm/llvm-project/pull/143063.diff

1 Files Affected:

  • (modified) clang/lib/Format/TokenAnnotator.cpp (+15-20)
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;

@owenca owenca changed the title [clang-format] More consumeToken() cleanup similar to #142104 [clang-format] More consumeToken() cleanup Jun 6, 2025
@owenca owenca merged commit eb71fdd into llvm:main Jun 6, 2025
7 checks passed
@owenca owenca deleted the nfc-ish branch June 6, 2025 06:36
rorth pushed a commit to rorth/llvm-project that referenced this pull request Jun 11, 2025
DhruvSrivastavaX pushed a commit to DhruvSrivastavaX/lldb-for-aix that referenced this pull request Jun 12, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants