-
Notifications
You must be signed in to change notification settings - Fork 14.8k
[Clang] Optimize tok::isLiteral with range-based condition #153228
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
Conversation
@llvm/pr-subscribers-clang Author: Thibault Monnier (Thibault-Monnier) ChangesThis commit optimizes 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. Full diff: https://github.com/llvm/llvm-project/pull/153228.diff 1 Files Affected:
diff --git a/clang/include/clang/Basic/TokenKinds.h b/clang/include/clang/Basic/TokenKinds.h
index 1b133dde89587..f9927182d3484 100644
--- a/clang/include/clang/Basic/TokenKinds.h
+++ b/clang/include/clang/Basic/TokenKinds.h
@@ -95,10 +95,7 @@ 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;
+ return K >= tok::numeric_constant && K <= tok::utf32_string_literal;
}
/// Return true if this is any of tok::annot_* kinds.
|
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; | ||
return K >= tok::numeric_constant && K <= tok::utf32_string_literal; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we add a comment in TokenKinds.def
(before numeric_constant) such that we guarantee someone does not change the order or add an assert that tests for equality?
I don't think we currently rely on, or guarantee an order here @AaronBallman
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay, I will add an assert.
66a0761
to
6ce02ca
Compare
@cor3ntin I am done. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
Co-authored-by: Corentin Jabot <[email protected]>
✅ With the latest revision this PR passed the C/C++ code formatter. |
b43d232
to
afe00a3
Compare
Thanks @cor3ntin! I do not have commit access, could you please commit for me? |
This commit optimizes
tok::isLiteral
by replacing a succession of13
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.