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 {")) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this has to be done after Tokenizer::simplifyCAlternativeTokens is called. Suggested test case (from https://en.cppreference.com/cpp/language/if#Consteval_if):

constexpr bool is_runtime_evaluated() noexcept
{
    if not consteval { return true; } else { return false; }
}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or just match with "not" as well.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it sounds good to me if it can be moved until after simplifyCAlternativeTokens.. is that a working solution?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's simpler to just match with not as well. It's replaced later anyway:

cppcheck $ cat tmp/consteval.cpp
void f(void)
{
    if consteval {} else {}
    if !consteval {} else {}
    if not consteval {} else {}
}
cppcheck $ ./cppcheck --debug tmp/consteval.cpp
Checking tmp/consteval.cpp ...


##file tmp/consteval.cpp
1: void f ( )
2: {
3: if ( __cppcheck_consteval__@expr2 ) { } else { }
4: if ( !@expr3 __cppcheck_consteval__@expr2 ) { } else { }
5: if ( !@expr3 __cppcheck_consteval__@expr2 ) { } else { }
6: }

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have kept the current implementation, or should I move it after simplifyCAlternativeTokens?

// '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::Match(tok, "if !|not consteval {")) {
// 'if !consteval { ... }' / 'if not 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
17 changes: 17 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,22 @@ 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());

ASSERT_EQUALS("constexpr bool is_runtime_evaluated ( ) noexcept ( true ) { if ( ! __cppcheck_consteval__ ) { return true ; } else { return false ; } }",
tokenizeAndStringify("constexpr bool is_runtime_evaluated() noexcept { if not consteval { return true; } else { return false; } }"));
filter_valueflow(errout_str());
}

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