Skip to content

Commit 2e9034c

Browse files
yronglinAnthony Tran
authored andcommitted
[NFC][Clang][Preprocessor] Refine the implementation of isNextPPTokenOneOf (llvm#145546)
This PR follow the suggestion(llvm#143898 (comment)) to refine the implementation of `Preprocessor::isNextPPToken`, also use C++ fold expression to refine `Token::isOneOf`. We don't need `bool isOneOf(tok::TokenKind K1, tok::TokenKind K2) const` anymore. In order to reduce the impact, specificed `TokenKind` is still passed to `Token::isOneOf` and `Preprocessor::isNextPPTokenOneOf` as function parameters. --------- Signed-off-by: yronglin <yronglin777@gmail.com>
1 parent 30b15a8 commit 2e9034c

File tree

4 files changed

+12
-11
lines changed

4 files changed

+12
-11
lines changed

clang/include/clang/Lex/Preprocessor.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2304,7 +2304,9 @@ class Preprocessor {
23042304

23052305
/// Check whether the next pp-token is one of the specificed token kind. this
23062306
/// method should have no observable side-effect on the lexed tokens.
2307-
template <tok::TokenKind K, tok::TokenKind... Ks> bool isNextPPTokenOneOf() {
2307+
template <typename... Ts> bool isNextPPTokenOneOf(Ts... Ks) {
2308+
static_assert(sizeof...(Ts) > 0,
2309+
"requires at least one tok::TokenKind specified");
23082310
// Do some quick tests for rejection cases.
23092311
std::optional<Token> Val;
23102312
if (CurLexer)
@@ -2335,7 +2337,7 @@ class Preprocessor {
23352337

23362338
// Okay, we found the token and return. Otherwise we found the end of the
23372339
// translation unit.
2338-
return Val->is(K) || (... || Val->is(Ks));
2340+
return Val->isOneOf(Ks...);
23392341
}
23402342

23412343
private:

clang/include/clang/Lex/Token.h

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -101,11 +101,10 @@ class Token {
101101
/// "if (Tok.is(tok::l_brace)) {...}".
102102
bool is(tok::TokenKind K) const { return Kind == K; }
103103
bool isNot(tok::TokenKind K) const { return Kind != K; }
104-
bool isOneOf(tok::TokenKind K1, tok::TokenKind K2) const {
105-
return is(K1) || is(K2);
106-
}
107-
template <typename... Ts> bool isOneOf(tok::TokenKind K1, Ts... Ks) const {
108-
return is(K1) || isOneOf(Ks...);
104+
template <typename... Ts> bool isOneOf(Ts... Ks) const {
105+
static_assert(sizeof...(Ts) > 0,
106+
"requires at least one tok::TokenKind specified");
107+
return (is(Ks) || ...);
109108
}
110109

111110
/// Return true if this is a raw identifier (when lexing

clang/lib/Lex/PPDirectives.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,9 +183,9 @@ static bool isReservedCXXAttributeName(Preprocessor &PP, IdentifierInfo *II) {
183183
AttributeCommonInfo::AttrArgsInfo AttrArgsInfo =
184184
AttributeCommonInfo::getCXX11AttrArgsInfo(II);
185185
if (AttrArgsInfo == AttributeCommonInfo::AttrArgsInfo::Required)
186-
return PP.isNextPPTokenOneOf<tok::l_paren>();
186+
return PP.isNextPPTokenOneOf(tok::l_paren);
187187

188-
return !PP.isNextPPTokenOneOf<tok::l_paren>() ||
188+
return !PP.isNextPPTokenOneOf(tok::l_paren) ||
189189
AttrArgsInfo == AttributeCommonInfo::AttrArgsInfo::Optional;
190190
}
191191
return false;

clang/lib/Lex/Preprocessor.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -813,14 +813,14 @@ bool Preprocessor::HandleIdentifier(Token &Identifier) {
813813
if (!Identifier.isExpandDisabled() && MI->isEnabled()) {
814814
// C99 6.10.3p10: If the preprocessing token immediately after the
815815
// macro name isn't a '(', this macro should not be expanded.
816-
if (!MI->isFunctionLike() || isNextPPTokenOneOf<tok::l_paren>())
816+
if (!MI->isFunctionLike() || isNextPPTokenOneOf(tok::l_paren))
817817
return HandleMacroExpandedIdentifier(Identifier, MD);
818818
} else {
819819
// C99 6.10.3.4p2 says that a disabled macro may never again be
820820
// expanded, even if it's in a context where it could be expanded in the
821821
// future.
822822
Identifier.setFlag(Token::DisableExpand);
823-
if (MI->isObjectLike() || isNextPPTokenOneOf<tok::l_paren>())
823+
if (MI->isObjectLike() || isNextPPTokenOneOf(tok::l_paren))
824824
Diag(Identifier, diag::pp_disabled_macro_expansion);
825825
}
826826
}

0 commit comments

Comments
 (0)