[clang] [NFC][Clang][Preprocessor] Refine the implementation of isNextPPTokenOneOf (PR #145546)

via cfe-commits cfe-commits at lists.llvm.org
Tue Jun 24 10:12:58 PDT 2025


https://github.com/yronglin updated https://github.com/llvm/llvm-project/pull/145546

>From 19b65ac2c76e7b6610a2d135848f9a468fae1610 Mon Sep 17 00:00:00 2001
From: yronglin <yronglin777 at gmail.com>
Date: Wed, 25 Jun 2025 00:18:25 +0800
Subject: [PATCH 1/2] [NFC][Clang][Preprocessor] Optimize the implementation of
 isNextPPTokenOneOf

Signed-off-by: yronglin <yronglin777 at gmail.com>
---
 clang/include/clang/Lex/Preprocessor.h | 5 +++--
 clang/include/clang/Lex/Token.h        | 5 +----
 clang/lib/Lex/PPDirectives.cpp         | 4 ++--
 clang/lib/Lex/Preprocessor.cpp         | 4 ++--
 4 files changed, 8 insertions(+), 10 deletions(-)

diff --git a/clang/include/clang/Lex/Preprocessor.h b/clang/include/clang/Lex/Preprocessor.h
index 0ec1cb4d0c5d8..9e7734360dd2e 100644
--- a/clang/include/clang/Lex/Preprocessor.h
+++ b/clang/include/clang/Lex/Preprocessor.h
@@ -2304,7 +2304,8 @@ class Preprocessor {
 
   /// Check whether the next pp-token is one of the specificed token kind. this
   /// method should have no observable side-effect on the lexed tokens.
-  template <tok::TokenKind K, tok::TokenKind... Ks> bool isNextPPTokenOneOf() {
+  template <typename... Ts>
+  bool isNextPPTokenOneOf(tok::TokenKind K, Ts... Ks) {
     // Do some quick tests for rejection cases.
     std::optional<Token> Val;
     if (CurLexer)
@@ -2335,7 +2336,7 @@ class Preprocessor {
 
     // Okay, we found the token and return.  Otherwise we found the end of the
     // translation unit.
-    return Val->is(K) || (... || Val->is(Ks));
+    return Val->isOneOf(K, Ks...);
   }
 
 private:
diff --git a/clang/include/clang/Lex/Token.h b/clang/include/clang/Lex/Token.h
index d4dfd7b44d9af..9ae521c3c8f07 100644
--- a/clang/include/clang/Lex/Token.h
+++ b/clang/include/clang/Lex/Token.h
@@ -101,11 +101,8 @@ class Token {
   /// "if (Tok.is(tok::l_brace)) {...}".
   bool is(tok::TokenKind K) const { return Kind == K; }
   bool isNot(tok::TokenKind K) const { return Kind != K; }
-  bool isOneOf(tok::TokenKind K1, tok::TokenKind K2) const {
-    return is(K1) || is(K2);
-  }
   template <typename... Ts> bool isOneOf(tok::TokenKind K1, Ts... Ks) const {
-    return is(K1) || isOneOf(Ks...);
+    return is(K1) || (is(Ks) || ...);
   }
 
   /// Return true if this is a raw identifier (when lexing
diff --git a/clang/lib/Lex/PPDirectives.cpp b/clang/lib/Lex/PPDirectives.cpp
index c8974e5a3528c..b88624b22e622 100644
--- a/clang/lib/Lex/PPDirectives.cpp
+++ b/clang/lib/Lex/PPDirectives.cpp
@@ -183,9 +183,9 @@ static bool isReservedCXXAttributeName(Preprocessor &PP, IdentifierInfo *II) {
     AttributeCommonInfo::AttrArgsInfo AttrArgsInfo =
         AttributeCommonInfo::getCXX11AttrArgsInfo(II);
     if (AttrArgsInfo == AttributeCommonInfo::AttrArgsInfo::Required)
-      return PP.isNextPPTokenOneOf<tok::l_paren>();
+      return PP.isNextPPTokenOneOf(tok::l_paren);
 
-    return !PP.isNextPPTokenOneOf<tok::l_paren>() ||
+    return !PP.isNextPPTokenOneOf(tok::l_paren) ||
            AttrArgsInfo == AttributeCommonInfo::AttrArgsInfo::Optional;
   }
   return false;
diff --git a/clang/lib/Lex/Preprocessor.cpp b/clang/lib/Lex/Preprocessor.cpp
index 7fecbe9eee53c..500cf6f8400e0 100644
--- a/clang/lib/Lex/Preprocessor.cpp
+++ b/clang/lib/Lex/Preprocessor.cpp
@@ -813,14 +813,14 @@ bool Preprocessor::HandleIdentifier(Token &Identifier) {
       if (!Identifier.isExpandDisabled() && MI->isEnabled()) {
         // C99 6.10.3p10: If the preprocessing token immediately after the
         // macro name isn't a '(', this macro should not be expanded.
-        if (!MI->isFunctionLike() || isNextPPTokenOneOf<tok::l_paren>())
+        if (!MI->isFunctionLike() || isNextPPTokenOneOf(tok::l_paren))
           return HandleMacroExpandedIdentifier(Identifier, MD);
       } else {
         // C99 6.10.3.4p2 says that a disabled macro may never again be
         // expanded, even if it's in a context where it could be expanded in the
         // future.
         Identifier.setFlag(Token::DisableExpand);
-        if (MI->isObjectLike() || isNextPPTokenOneOf<tok::l_paren>())
+        if (MI->isObjectLike() || isNextPPTokenOneOf(tok::l_paren))
           Diag(Identifier, diag::pp_disabled_macro_expansion);
       }
     }

>From 3c3564fe106e7149e76d39fd73c1492f3b7a5d53 Mon Sep 17 00:00:00 2001
From: yronglin <yronglin777 at gmail.com>
Date: Wed, 25 Jun 2025 01:11:25 +0800
Subject: [PATCH 2/2] Use static_assert

Signed-off-by: yronglin <yronglin777 at gmail.com>
---
 clang/include/clang/Lex/Preprocessor.h | 7 ++++---
 clang/include/clang/Lex/Token.h        | 6 ++++--
 2 files changed, 8 insertions(+), 5 deletions(-)

diff --git a/clang/include/clang/Lex/Preprocessor.h b/clang/include/clang/Lex/Preprocessor.h
index 9e7734360dd2e..dae12a6015439 100644
--- a/clang/include/clang/Lex/Preprocessor.h
+++ b/clang/include/clang/Lex/Preprocessor.h
@@ -2304,8 +2304,9 @@ class Preprocessor {
 
   /// Check whether the next pp-token is one of the specificed token kind. this
   /// method should have no observable side-effect on the lexed tokens.
-  template <typename... Ts>
-  bool isNextPPTokenOneOf(tok::TokenKind K, Ts... Ks) {
+  template <typename... Ts> bool isNextPPTokenOneOf(Ts... Ks) {
+    static_assert(sizeof...(Ts) > 0,
+                  "requires at least one tok::TokenKind specified");
     // Do some quick tests for rejection cases.
     std::optional<Token> Val;
     if (CurLexer)
@@ -2336,7 +2337,7 @@ class Preprocessor {
 
     // Okay, we found the token and return.  Otherwise we found the end of the
     // translation unit.
-    return Val->isOneOf(K, Ks...);
+    return Val->isOneOf(Ks...);
   }
 
 private:
diff --git a/clang/include/clang/Lex/Token.h b/clang/include/clang/Lex/Token.h
index 9ae521c3c8f07..fc43e72593b94 100644
--- a/clang/include/clang/Lex/Token.h
+++ b/clang/include/clang/Lex/Token.h
@@ -101,8 +101,10 @@ class Token {
   /// "if (Tok.is(tok::l_brace)) {...}".
   bool is(tok::TokenKind K) const { return Kind == K; }
   bool isNot(tok::TokenKind K) const { return Kind != K; }
-  template <typename... Ts> bool isOneOf(tok::TokenKind K1, Ts... Ks) const {
-    return is(K1) || (is(Ks) || ...);
+  template <typename... Ts> bool isOneOf(Ts... Ks) const {
+    static_assert(sizeof...(Ts) > 0,
+                  "requires at least one tok::TokenKind specified");
+    return (is(Ks) || ...);
   }
 
   /// Return true if this is a raw identifier (when lexing



More information about the cfe-commits mailing list