Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix #13452: iterator for library container not always detected #7248

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

ludviggunne
Copy link
Contributor

In reference to #6595.

@ludviggunne
Copy link
Contributor Author

@firewave does this look OK?

@firewave
Copy link
Collaborator

Given the results of the unit tests it is.

The code duplication and additional Token::Match() are not. This is the most expensive call in the "core" (i.e. no ValueFlow, no checking, ...) so that is probably the reason we get timeouts in the CI. There is also a ticket about it. Also see the "callgrind" step in https://github.com/danmar/cppcheck/actions/runs/12913957401/job/36012395159. It now takes more than twice the Ir.

I wrecked my brain multiple times already in making this function just slightly faster in the past so figuring this out might take a bit. Please give me a few days since I am not feeling well at the moment.

if (isIterator)
*isIterator = false;
return &container;
}

if (!firstLinkedTok)
if (!firstLinkedTok) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

In this case you never match the start pattern.

Copy link
Collaborator

Choose a reason for hiding this comment

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

That needs a negative test case.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hm, sorry I'm not sure if I understand. The start pattern is implicitly matched in the "combined" pattern below as far as I can tell.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Okay, I missed that but that causes that pattern to be matched twice if the first match fails.

The start match should be cached.

You can just modify the existing code with passing typeStart instead of firstLinkedTok->link() . Just store it in a variable.

The negative tests also already exist.

I will provide a branch with some WIP code (which does not work right now).

Copy link
Collaborator

Choose a reason for hiding this comment

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

I think we might be missing std::F<int>::iterator f_it; as a test case.

Copy link
Collaborator

Choose a reason for hiding this comment

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

https://github.com/firewave/cppcheck/tree/lib-it

This is broken because I misunderstood the matching in your new code. We need to adjust the end matching for that case.

continue;

if (detect != IteratorOnly && container.endPattern.empty() &&
Token::Match(typeStart, container.startPattern2.c_str() + offset)) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

The Token::Match() might be performed twice and should be cached.

@ludviggunne
Copy link
Contributor Author

I wrecked my brain multiple times already in making this function just slightly faster in the past so figuring this out might take a bit.

Just off the top of my head I'm thinking we could store the result of this function in Token, I did a quick run through and it was called six times for the same token.

Get well soon!

@firewave firewave marked this pull request as draft January 26, 2025 22: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.

2 participants