Skip to content

Commit b75896b

Browse files
[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 <[email protected]>
1 parent 48beed5 commit b75896b

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

clang/include/clang/Basic/TokenKinds.h

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,20 @@ inline bool isStringLiteral(TokenKind K) {
9595
/// Return true if this is a "literal" kind, like a numeric
9696
/// constant, string, etc.
9797
inline bool isLiteral(TokenKind K) {
98-
return K == tok::numeric_constant || K == tok::char_constant ||
99-
K == tok::wide_char_constant || K == tok::utf8_char_constant ||
100-
K == tok::utf16_char_constant || K == tok::utf32_char_constant ||
101-
isStringLiteral(K) || K == tok::header_name || K == tok::binary_data;
98+
const bool isInLiteralRange =
99+
K >= tok::numeric_constant && K <= tok::utf32_string_literal;
100+
101+
#if !NDEBUG
102+
const bool isLiteralExplicit =
103+
K == tok::numeric_constant || K == tok::char_constant ||
104+
K == tok::wide_char_constant || K == tok::utf8_char_constant ||
105+
K == tok::utf16_char_constant || K == tok::utf32_char_constant ||
106+
isStringLiteral(K) || K == tok::header_name || K == tok::binary_data;
107+
assert(isInLiteralRange == isLiteralExplicit &&
108+
"TokenKind literals should be contiguous");
109+
#endif
110+
111+
return isInLiteralRange;
102112
}
103113

104114
/// Return true if this is any of tok::annot_* kinds.

0 commit comments

Comments
 (0)