Repository navigation
Fix #15143: Improve performance in the case of many template instantiations #8960
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
Open
Oipo
wants to merge
2
commits into
cppcheck-opensource:main
Choose a base branch
from
Oipo:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+181
−43
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2025,6 +2025,39 @@ void CheckClassImpl::virtualDestructor() | |
|
|
||
| logChecker("CheckClass::virtualDestructor"); | ||
|
|
||
| // pointer variables of type 'Base *', for each type 'Base' | ||
| std::unordered_map<const Type *, std::vector<int>> pointersByType; | ||
|
|
||
| // for each pointer variable, the classes 'Derived' of a "ptr = new Derived" that is followed by a "delete ptr;" | ||
| std::unordered_map<int, std::set<std::string>> deletedAfterNew; | ||
|
|
||
| // collect these when the first base class is checked | ||
| bool deletionsCollected = false; | ||
| const auto collectDeletions = [&]() { | ||
| if (deletionsCollected) | ||
| return; | ||
| deletionsCollected = true; | ||
|
|
||
| for (const Variable* var : mSymbolDatabase->variableList()) { | ||
| if (var && var->isPointer() && var->type()) | ||
| pointersByType[var->type()].push_back(var->declarationId()); | ||
| } | ||
|
|
||
| // the classes 'Derived' of the "ptr = new Derived" seen so far | ||
| std::unordered_map<int, std::set<std::string>> newClasses; | ||
|
|
||
| for (const Token *tok = mTokenizer->tokens(); tok; tok = tok->next()) { | ||
| if (Token::Match(tok, "[;{}] %var% = new %name%")) | ||
| newClasses[tok->next()->varId()].insert(tok->strAt(4)); | ||
|
|
||
| else if (Token::Match(tok, "delete %var% ;")) { | ||
| const auto it = newClasses.find(tok->next()->varId()); | ||
| if (it != newClasses.end()) | ||
| deletedAfterNew[it->first].insert(it->second.cbegin(), it->second.cend()); | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| for (const Scope * scope : mSymbolDatabase->classAndStructScopes) { | ||
|
|
||
| // Skip base classes (unless inconclusive) | ||
|
|
@@ -2073,40 +2106,18 @@ void CheckClassImpl::virtualDestructor() | |
| // | ||
| // If this pattern is not seen then bailout the checking of these base/derived classes | ||
| { | ||
| // pointer variables of type 'Base *' | ||
| std::set<int> baseClassPointers; | ||
| collectDeletions(); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it does not look right to me to call this in the loop. It will go through all variables in symboldatabase and add declaration ids to pointersByType over and over again? can't we just do that once? |
||
|
|
||
| for (const Variable* var : mSymbolDatabase->variableList()) { | ||
| if (var && var->isPointer() && var->type() == derivedFrom) | ||
| baseClassPointers.insert(var->declarationId()); | ||
| } | ||
|
|
||
| // pointer variables of type 'Base *' that should not be deleted | ||
| std::set<int> dontDelete; | ||
|
|
||
| // No deletion of derived class instance through base class pointer found => the code is ok | ||
| bool ok = true; | ||
|
|
||
| for (const Token *tok = mTokenizer->tokens(); tok; tok = tok->next()) { | ||
| if (Token::Match(tok, "[;{}] %var% =") && | ||
| baseClassPointers.find(tok->next()->varId()) != baseClassPointers.end()) { | ||
| // new derived class.. | ||
| const std::string tmp("new " + derivedClass->str()); | ||
| if (Token::simpleMatch(tok->tokAt(3), tmp.c_str(), tmp.size())) { | ||
| dontDelete.insert(tok->next()->varId()); | ||
| } | ||
| } | ||
|
|
||
| // Delete base class pointer that might point at derived class | ||
| else if (Token::Match(tok, "delete %var% ;") && | ||
| dontDelete.find(tok->next()->varId()) != dontDelete.end()) { | ||
| ok = false; | ||
| break; | ||
| } | ||
| } | ||
| // pointer variables of type 'Base *' | ||
| const auto pointers = pointersByType.find(derivedFrom); | ||
| if (pointers == pointersByType.end()) | ||
| continue; | ||
|
|
||
| // No base class pointer that points at a derived class is deleted | ||
| if (ok) | ||
| if (std::none_of(pointers->second.cbegin(), pointers->second.cend(), [&](int varId) { | ||
| const auto deleted = deletedAfterNew.find(varId); | ||
| return deleted != deletedAfterNew.end() && deleted->second.count(derivedClass->str()) > 0; | ||
| })) | ||
| continue; | ||
| } | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would suggest a
&& var->nameToken()also. unnamed parameters will not be added then.