[clang] b75896b - [Clang] Optimize tok::isLiteral with range-based condition (#153228)

via cfe-commits cfe-commits at lists.llvm.org
Wed Aug 13 05:18:15 PDT 2025


Author: Thibault Monnier
Date: 2025-08-13T08:18:11-04:00
New Revision: b75896bc4525e153aa9cdf77e0993aae0d637a28

URL: https://github.com/llvm/llvm-project/commit/b75896bc4525e153aa9cdf77e0993aae0d637a28
DIFF: https://github.com/llvm/llvm-project/commit/b75896bc4525e153aa9cdf77e0993aae0d637a28.diff

LOG: [Clang] Optimize tok::isLiteral with range-based condition (#153228)

This commit optimizes `tok::isLiteral` by replacing a succession of `13`
conditions with a range-based check.

I am not sure whether this is allowed. I believe it is done nowhere else
in the codebase ; however, I have seen range-based conditions being used
with other enums.

---------

Co-authored-by: Corentin Jabot <corentinjabot at gmail.com>

Added: 
    

Modified: 
    clang/include/clang/Basic/TokenKinds.h

Removed: 
    


################################################################################
diff  --git a/clang/include/clang/Basic/TokenKinds.h b/clang/include/clang/Basic/TokenKinds.h
index 1b133dde89587..d84f3598cbf33 100644
--- a/clang/include/clang/Basic/TokenKinds.h
+++ b/clang/include/clang/Basic/TokenKinds.h
@@ -95,10 +95,20 @@ inline bool isStringLiteral(TokenKind K) {
 /// Return true if this is a "literal" kind, like a numeric
 /// constant, string, etc.
 inline bool isLiteral(TokenKind K) {
-  return K == tok::numeric_constant || K == tok::char_constant ||
-         K == tok::wide_char_constant || K == tok::utf8_char_constant ||
-         K == tok::utf16_char_constant || K == tok::utf32_char_constant ||
-         isStringLiteral(K) || K == tok::header_name || K == tok::binary_data;
+  const bool isInLiteralRange =
+      K >= tok::numeric_constant && K <= tok::utf32_string_literal;
+
+#if !NDEBUG
+  const bool isLiteralExplicit =
+      K == tok::numeric_constant || K == tok::char_constant ||
+      K == tok::wide_char_constant || K == tok::utf8_char_constant ||
+      K == tok::utf16_char_constant || K == tok::utf32_char_constant ||
+      isStringLiteral(K) || K == tok::header_name || K == tok::binary_data;
+  assert(isInLiteralRange == isLiteralExplicit &&
+         "TokenKind literals should be contiguous");
+#endif
+
+  return isInLiteralRange;
 }
 
 /// Return true if this is any of tok::annot_* kinds.


        


More information about the cfe-commits mailing list