Skip to content

Commit b878dc2

Browse files
authored
Fix #11538: Fix syntax error on C++23 'if consteval' / 'if !consteval' (#8733)
1 parent 4e62f0c commit b878dc2

2 files changed

Lines changed: 33 additions & 1 deletion

File tree

lib/tokenize.cpp

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5752,7 +5752,22 @@ bool Tokenizer::simplifyTokenList1(const char FileName[])
57525752

57535753
// if MACRO
57545754
for (Token *tok = list.front(); tok; tok = tok->next()) {
5755-
if (Token::Match(tok, "if|for|while %name% (")) {
5755+
if (Token::simpleMatch(tok, "if consteval {")) {
5756+
// 'if consteval { ... }' -> 'if ( __cppcheck_consteval__ ) { ... }'
5757+
Token *parTok = tok->next();
5758+
parTok->str("(");
5759+
Token *evalTok = parTok->insertToken("__cppcheck_consteval__");
5760+
evalTok->originalName("consteval");
5761+
evalTok->insertToken(")");
5762+
} else if (Token::Match(tok, "if !|not consteval {")) {
5763+
// 'if !consteval { ... }' / 'if not consteval { ... }' -> 'if ( ! __cppcheck_consteval__ ) { ... }'
5764+
Token *notTok = tok->next();
5765+
notTok->insertTokenBefore("(");
5766+
Token *evalTok = notTok->next();
5767+
evalTok->str("__cppcheck_consteval__");
5768+
evalTok->originalName("consteval");
5769+
evalTok->insertToken(")");
5770+
} else if (Token::Match(tok, "if|for|while %name% (")) {
57565771
if (Token::simpleMatch(tok, "for each")) {
57575772
// 'for each (x in y )' -> 'for (x : y)'
57585773
if (Token* in = Token::findsimplematch(tok->tokAt(2), "in", tok->linkAt(2)))

test/testtokenize.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ class TestTokenizer : public TestFixture {
105105

106106
TEST_CASE(foreach); // #3690
107107
TEST_CASE(ifconstexpr);
108+
TEST_CASE(ifconsteval);
108109

109110
TEST_CASE(combineOperators);
110111

@@ -1068,6 +1069,22 @@ class TestTokenizer : public TestFixture {
10681069
filter_valueflow(errout_str()));
10691070
}
10701071

1072+
void ifconsteval() {
1073+
ASSERT_EQUALS("void f ( ) { if ( __cppcheck_consteval__ ) { bar ( ) ; } }", tokenizeAndStringify("void f() { if consteval { bar(); } }"));
1074+
filter_valueflow(errout_str());
1075+
1076+
ASSERT_EQUALS("void f ( ) { if ( ! __cppcheck_consteval__ ) { bar ( ) ; } }", tokenizeAndStringify("void f() { if !consteval { bar(); } }"));
1077+
filter_valueflow(errout_str());
1078+
1079+
ASSERT_EQUALS("void f ( ) { if ( __cppcheck_consteval__ ) { bar ( ) ; } else { baz ( ) ; } }",
1080+
tokenizeAndStringify("void f() { if consteval { bar(); } else { baz(); } }"));
1081+
filter_valueflow(errout_str());
1082+
1083+
ASSERT_EQUALS("constexpr bool is_runtime_evaluated ( ) noexcept ( true ) { if ( ! __cppcheck_consteval__ ) { return true ; } else { return false ; } }",
1084+
tokenizeAndStringify("constexpr bool is_runtime_evaluated() noexcept { if not consteval { return true; } else { return false; } }"));
1085+
filter_valueflow(errout_str());
1086+
}
1087+
10711088
void combineOperators() {
10721089
ASSERT_EQUALS("; private: ;", tokenizeAndStringify(";private:;\n"));
10731090
ASSERT_EQUALS("; protected: ;", tokenizeAndStringify(";protected:;\n"));

0 commit comments

Comments
 (0)