Skip to content
Open
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
17 changes: 16 additions & 1 deletion lib/tokenize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5721,7 +5721,22 @@ bool Tokenizer::simplifyTokenList1(const char FileName[])

// if MACRO
for (Token *tok = list.front(); tok; tok = tok->next()) {
if (Token::Match(tok, "if|for|while %name% (")) {
if (Token::simpleMatch(tok, "if consteval {")) {
// 'if consteval { ... }' -> 'if ( __cppcheck_consteval__ ) { ... }'
Token *parTok = tok->next();
parTok->str("(");
Token *evalTok = parTok->insertToken("__cppcheck_consteval__");
evalTok->originalName("consteval");
evalTok->insertToken(")");
} else if (Token::simpleMatch(tok, "if ! consteval {")) {
// 'if !consteval { ... }' -> 'if ( ! __cppcheck_consteval__ ) { ... }'
Token *notTok = tok->next();
notTok->insertTokenBefore("(");
Token *evalTok = notTok->next();
evalTok->str("__cppcheck_consteval__");
evalTok->originalName("consteval");
evalTok->insertToken(")");
} else if (Token::Match(tok, "if|for|while %name% (")) {
if (Token::simpleMatch(tok, "for each")) {
// 'for each (x in y )' -> 'for (x : y)'
if (Token* in = Token::findsimplematch(tok->tokAt(2), "in", tok->linkAt(2)))
Expand Down
13 changes: 13 additions & 0 deletions test/testtokenize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ class TestTokenizer : public TestFixture {

TEST_CASE(foreach); // #3690
TEST_CASE(ifconstexpr);
TEST_CASE(ifconsteval);

TEST_CASE(combineOperators);

Expand Down Expand Up @@ -1042,6 +1043,18 @@ class TestTokenizer : public TestFixture {
filter_valueflow(errout_str()));
}

void ifconsteval() {
ASSERT_EQUALS("void f ( ) { if ( __cppcheck_consteval__ ) { bar ( ) ; } }", tokenizeAndStringify("void f() { if consteval { bar(); } }"));
filter_valueflow(errout_str());

ASSERT_EQUALS("void f ( ) { if ( ! __cppcheck_consteval__ ) { bar ( ) ; } }", tokenizeAndStringify("void f() { if !consteval { bar(); } }"));
filter_valueflow(errout_str());

ASSERT_EQUALS("void f ( ) { if ( __cppcheck_consteval__ ) { bar ( ) ; } else { baz ( ) ; } }",
tokenizeAndStringify("void f() { if consteval { bar(); } else { baz(); } }"));
filter_valueflow(errout_str());
}

void combineOperators() {
ASSERT_EQUALS("; private: ;", tokenizeAndStringify(";private:;"));
ASSERT_EQUALS("; protected: ;", tokenizeAndStringify(";protected:;"));
Expand Down
Loading