Skip to content
Closed
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
4 changes: 2 additions & 2 deletions lib/tokenize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9179,8 +9179,8 @@ void Tokenizer::findGarbageCode() const
syntaxError(tok);
if (Token::simpleMatch(tok->tokAt(-1), ")")) {
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.

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

if (prev->isUpperCaseName())
unknownMacroError(prev);
else
syntaxError(tok);
Expand Down
8 changes: 8 additions & 0 deletions test/testtokenize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7929,6 +7929,14 @@ class TestTokenizer : public TestFixture {
" }\n"
"}\n"), UNKNOWN_MACRO);

ASSERT_NO_THROW(tokenizeAndStringify("void f(void* h) {\n" // #15079
" (void)::CloseHandle(h);\n"
" int i = (int)::GetTickCount();\n"
"}\n"
"std::size_t* g(void* p) {\n"
" return new (p) ::std::size_t;\n"
"}\n"));

ASSERT_THROW_INTERNAL_EQUALS(tokenizeAndStringify("static void handle_toggle(void (*proc) PROTO_XT_CALLBACK_ARGS, int var) {}\n"), // #13198
UNKNOWN_MACRO,
"There is an unknown macro here somewhere. Configuration is required. If PROTO_XT_CALLBACK_ARGS is a macro then please configure it.");
Expand Down
Loading