Skip to content

Fix #15079 Regression: FP syntaxError for C-style cast before :: - #8912

Closed
chrchr-github wants to merge 1 commit into
cppcheck-opensource:mainfrom
chrchr-github:chr_15079
Closed

chrchr-github wants to merge 1 commit into
cppcheck-opensource:mainfrom
chrchr-github:chr_15079

Conversation

@chrchr-github

Copy link
Copy Markdown
Collaborator

No description provided.

@danmar danmar left a comment

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.

This is an AI review take it with a grain of salt. Please feel free to reject it by clicking on Resolve button

This fixes the same ticket as #8909 (autoantwort), which uses Token::Match(prev, "%name% (") && !prev->isKeyword() instead of prev->isNameOnly(). Both look correct. It might be good to agree on one and close the other. #8909 also has a few extra test cases that would be nice to have here: return (int)::abs(i); (a keyword before the cast), g((unsigned char)::toupper(i)); and decltype(s)::type i;.

Comment thread lib/tokenize.cpp
const Token* const prev = tok->linkAt(-1)->tokAt(-1);
if (!Token::Match(prev, "%name% (") || (!prev->isControlFlowKeyword() && prev->str() != "decltype")) {
if (prev && prev->isUpperCaseName())
if (prev && prev->isNameOnly()) {

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.

This is an AI review take it with a grain of salt. Please feel free to reject it by clicking on Resolve button

The old condition made the intent visible (%name% (, "something that looks like a macro call"). With isNameOnly() that's less obvious to a reader. A short comment would help, e.g. // NAME(...):: => NAME is probably an unknown macro; casts like (void)::f() and new (p) ::T are valid.

Note also that isNameOnly() is false for names with any extra flag set, so this is stricter than !prev->isKeyword(). That's fine from a no-false-positives point of view, just mentioning it in case it was unintentional.

Comment thread lib/tokenize.cpp
const Token* const prev = tok->linkAt(-1)->tokAt(-1);
if (!Token::Match(prev, "%name% (") || (!prev->isControlFlowKeyword() && prev->str() != "decltype")) {
if (prev && prev->isUpperCaseName())
if (prev && prev->isNameOnly()) {

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.

This is an AI review take it with a grain of salt. Please feel free to reject it by clicking on Resolve button

Whether a token is a keyword depends on --std (TokenList::isKeyword() uses Keywords::getAll(standards.cpp)). With --std=c++03, decltype is a plain name with no other flags, so isNameOnly() is true and this gives a false syntaxError (verified with the same condition in #8909):

$ cppcheck --std=c++03 dt.cpp
dt.cpp:3:12: error: syntax error [syntaxError]
decltype(s)::type i;

The old code avoided this with prev->str() != "decltype". Maybe keep that, e.g. if (prev && prev->isNameOnly() && prev->str() != "decltype").

@chrchr-github

Copy link
Copy Markdown
Collaborator Author

Closing in favor of #8909

@chrchr-github
chrchr-github deleted the chr_15079 branch October 2, 2026 11:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants