Skip to content

Fix #15305 (SymbolDatabase: enum/struct tag name gets Token::function() when a function with the same name exists) - #9111

Merged
danmar merged 2 commits into
cppcheck-opensource:mainfrom
cppchecksolutions:fix-15305
Dec 3, 2026
Merged

danmar merged 2 commits into
cppcheck-opensource:mainfrom
cppchecksolutions:fix-15305

Conversation

@danmar

@danmar danmar commented Dec 3, 2026

Copy link
Copy Markdown
Collaborator

No description provided.

…() when a function with the same name exists)

@dmcppcheck dmcppcheck left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

AI review

The fix works for unqualified tag names. I found no regressions: in C, enum E, struct S and union U no longer get Token::function(), and the bogus !0 (function address) values on them are gone. One case is still not handled: a qualified elaborated name like struct N::S still links S to the function N::S, because the new check only looks at the token directly before the name.

Comments

# Comment By Where Status
1 Qualified tag names (struct N::S) still get Token::function() AI review lib/symboldatabase.cpp:1211 🆕 New (false negative)

@danmar, please fix, resolve or reply to the open and new comments: #1.
For #1 (false negative), it is enough to create a Trac ticket and link it here. It does not need to be fixed in this PR.

Testing

  • Built cppcheck and testrunner in pr/ and cppcheck in base/.
  • ./testrunner TestSymbolDatabase: 432 tests, 0 failed. The full ./testrunner: 5436 tests, 0 failed.
  • Compared --debug output of base and PR on scratch C and C++ files that use enum E, struct S and enum class E in casts, sizeof and declarations next to functions of the same name:
    • In C, base gave S always !0 / E always !0 (function address values) and wrong values on the expression. PR gives . always 4 etc.
    • In C++, the spurious E always !0 on (enum class E)x is gone.
  • Ran samples/ and test/cfg/*.c with --enable=all --inconclusive: output is the same in base and PR.
  • One case is still wrong:
namespace N { struct S { int a; }; void S(int); }
int f(void *p) {
    return ((struct N::S*)p)->a + sizeof(struct N::S);
}

pr/cppcheck --debug c.cpp still prints S always !0 for the S in sizeof(struct N::S), so the token is still linked to the function. Base prints the same, so this is not a regression.


Automated review of dbe4b2b by Claude. The pull request was built and tested locally.

Comment thread lib/symboldatabase.cpp Outdated
if (inTemplateArg == tok)
inTemplateArg = nullptr;
if (tok->isName() && !tok->function() && tok->varId() == 0 && ((tok->astParent() && tok->astParent()->isComparisonOp()) || Token::Match(tok, "%name% [{}(,)>;:]]")) && !isReservedName(tok)) {
if (tok->isName() && !tok->function() && tok->varId() == 0 && !Token::Match(tok->previous(), "class|struct|union|enum") && ((tok->astParent() && tok->astParent()->isComparisonOp()) || Token::Match(tok, "%name% [{}(,)>;:]]")) && !isReservedName(tok)) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

AI review

The check only looks at tok->previous(), so a qualified elaborated type specifier is not covered. With namespace N { struct S { int a; }; void S(int); } and sizeof(struct N::S), the S token still gets the function, and --debug shows S always !0. You could walk back over %name% :: chains before matching class|struct|union|enum.

@dmcppcheck dmcppcheck left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

AI review

I found no new problems. The qualified tag case from the earlier review is fixed: isElaboratedTypeName now walks back over %name% :: chains, so struct N::S and struct ::N::S are covered, and the PR adds a test for both. A function name used as a function pointer next to a struct with the same name (void (*fp)(int) = T;) is still linked to the function.

Comments

# Comment By Where Status
1 Qualified elaborated tag names (struct N::S) still get Token::function() AI review lib/symboldatabase.cpp:1211 ✅ Fixed

Testing

  • Built cppcheck and testrunner in pr/ with no warnings or errors.
  • ./testrunner TestSymbolDatabase: 432 tests, 0 failed. The full ./testrunner -q: 0 failed.
  • Ran pr/cppcheck --debug on a scratch file with struct N::S, struct ::N::S and enum class N::S in casts and sizeof, all next to a function N::S. None of the S tokens got the bogus always !0 value any more; the sizeof results came out as numbers (( always 4).
  • In the same file, struct T {...}; void T(int); void (*fp)(int) = T; still gives T always !0, so a function name used as a function pointer is still linked to the function.

Automated review of e26e91d by Claude. The pull request was built and tested locally.

@danmar
danmar merged commit 83ec29f into cppcheck-opensource:main Dec 3, 2026
74 checks passed
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.

2 participants