Fix #11538: Fix syntax error on C++23 'if consteval' / 'if !consteval' - #8733
Fix #11538: Fix syntax error on C++23 'if consteval' / 'if !consteval'#8733autoantwort wants to merge 1 commit into
Conversation
danmar
left a comment
There was a problem hiding this comment.
Your suggested transformation seems valid to me. Feel free to finish this approach..
| // 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 {")) { |
There was a problem hiding this comment.
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; }
}There was a problem hiding this comment.
Or just match with "not" as well.
There was a problem hiding this comment.
it sounds good to me if it can be moved until after simplifyCAlternativeTokens.. is that a working solution?
There was a problem hiding this comment.
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: }
There was a problem hiding this comment.
I have kept the current implementation, or should I move it after simplifyCAlternativeTokens?
The tokenizer only recognized 'if constexpr (...)' and treated any
other 'if <name>' without parentheses as a syntax error, so C++23's
'if consteval { ... }' and 'if !consteval { ... }' were rejected.
Rewrite them to 'if ( __cppcheck_consteval__ ) { ... }' and
'if ( ! __cppcheck_consteval__ ) { ... }' respectively, using a
synthetic unresolved symbol rather than a boolean literal so that
valueflow can't treat the condition as always true/false.
2ebe4cc to
e5b16e1
Compare
The tokenizer only recognized
if constexpr (...)and treated any otherif <name>without parentheses as a syntax error, so C++23'sif consteval { ... }andif !consteval { ... }were rejected.Rewrite them to
if ( __cppcheck_consteval__ ) { ... }andif ( ! __cppcheck_consteval__ ) { ... }respectively, using a synthetic unresolved symbol rather than a boolean literal so that valueflow can't treat the condition as always true/false.