Skip to content

Commit 19b65ac

Browse files
committed
[NFC][Clang][Preprocessor] Optimize the implementation of isNextPPTokenOneOf
Signed-off-by: yronglin <[email protected]>
1 parent 6a8899c commit 19b65ac

File tree

4 files changed

+8
-10
lines changed

4 files changed

+8
-10
lines changed

clang/include/clang/Lex/Preprocessor.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2304,7 +2304,8 @@ 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>
2308+
bool isNextPPTokenOneOf(tok::TokenKind K, Ts... Ks) {
23082309
// Do some quick tests for rejection cases.
23092310
std::optional<Token> Val;
23102311
if (CurLexer)
@@ -2335,7 +2336,7 @@ class Preprocessor {
23352336

23362337
// Okay, we found the token and return. Otherwise we found the end of the
23372338
// translation unit.
2338-
return Val->is(K) || (... || Val->is(Ks));
2339+
return Val->isOneOf(K, Ks...);
23392340
}
23402341

23412342
private:

clang/include/clang/Lex/Token.h

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,11 +101,8 @@ 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-
}
107104
template <typename... Ts> bool isOneOf(tok::TokenKind K1, Ts... Ks) const {
108-
return is(K1) || isOneOf(Ks...);
105+
return is(K1) || (is(Ks) || ...);
109106
}
110107

111108
/// 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)