Skip to content

[NFC][Clang][Preprocessor] Refine the implementation of isNextPPTokenOneOf #145546

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 2 commits into from
Jun 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions clang/include/clang/Lex/Preprocessor.h
Original file line number Diff line number Diff line change
Expand Up @@ -2304,7 +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 <tok::TokenKind K, tok::TokenKind... Ks> bool isNextPPTokenOneOf() {
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)
Expand Down Expand Up @@ -2335,7 +2337,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(Ks...);
}

private:
Expand Down
9 changes: 4 additions & 5 deletions clang/include/clang/Lex/Token.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +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; }
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...);
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
Expand Down
4 changes: 2 additions & 2 deletions clang/lib/Lex/PPDirectives.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions clang/lib/Lex/Preprocessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down