Skip to content

Fix unmatched suppression (#5704)#3886

Merged
danmar merged 7 commits intocppcheck-opensource:mainfrom
jpyllman:fix_unmatchedSuppression
Mar 30, 2022
Merged

Fix unmatched suppression (#5704)#3886
danmar merged 7 commits intocppcheck-opensource:mainfrom
jpyllman:fix_unmatchedSuppression

Conversation

@jpyllman
Copy link
Copy Markdown
Contributor

I hope this is an OK way to solve it. The problem with adding this flag is that you more need to do full flow to get the result you want from functions like Suppressions::getUnmatchedLocalSuppressions().

Copy link
Copy Markdown
Collaborator

@danmar danmar left a comment

Choose a reason for hiding this comment

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

I think the approach seems reasonable overall. 👍

I am a bit skeptic technically about including the whole tokenizer in suppressions. It's a heavy dependency. I guess it could just settle with token.h.

Comment thread lib/suppressions.cpp Outdated
}

/**
* This function require that both tokens and suppressions are sorted in line
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.

That assumption will not work when files are included.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

OK? You mean that both suppression and token list can be multiple files in some 'list'??

Copy link
Copy Markdown
Collaborator

@danmar danmar Mar 13, 2022

Choose a reason for hiding this comment

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

As far as I know the suppressions are listed in arbitrary order. If you want to sort them feel free to do it.

The tokenizer.tokens() contains all tokens in a translation unit.

If you have a file foo.h with 100 lines of code.. then you put this code in foo.c:

#include "foo.h"
int x;

then the tokenizer.tokens() will first contain the tokens from "foo.h" and then the "int x;" tokens.. so if you just look at the line number it will decrease when the "int x;" code is reached.

Comment thread lib/suppressions.h Outdated

#include "config.h"
#include "errortypes.h"
#include "tokenize.h"
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.

it does not seem this include is necessary here. I would prefer a forward declaration in the header:

class Tokenizer;

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yes, and then include in the .cpp file? Or should we just only take a pointer to the first token? I just looked at some other functions that used the tokenizer and then took the token list from there.

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.

hmm.. I don't have a super strong opinion if you should pass Tokenizer or Token. Both will work fine. Well.. if you pass tokenizer it's more apparent that the whole token list is used. so feel free to keep the interface as is.

@jpyllman
Copy link
Copy Markdown
Contributor Author

Another thing. I do not really see what in that scriptcheck goes wrong. Am I blind or ... ?

@chrchr-github
Copy link
Copy Markdown
Collaborator

Another thing. I do not really see what in that scriptcheck goes wrong. Am I blind or ... ?

I think the diff tells you what to change in Makefile.

@jpyllman
Copy link
Copy Markdown
Contributor Author

Anyway, I have written a new batch of test-files to trigger all the things discussed in this PR. After I have something new we could talk about making a new PR or post in this one.

@jpyllman
Copy link
Copy Markdown
Contributor Author

I have a new loop to mark suppression's as checked. I looks good. But I do not know how slow it is going to be. On my tests it works fine, in project mode, on a directory or on a specific file.

Comment thread lib/suppressions.cpp Outdated
/**
* This function require that both tokens and suppressions are sorted in line
* number order.
* Loop on mSuppressions over and oer again might not be so fast.
Copy link
Copy Markdown
Collaborator

@danmar danmar Mar 22, 2022

Choose a reason for hiding this comment

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

The placement here indicates that this comment will describe what the function is doing. But the text does not say that.

It feels more like some TODO comment to speedup..

I don't feel very worried that this function will be a bottleneck since it's only called once on each file configuration right? But I suggest that you write TODO. maybe put it inside the function body to indicate that it doesn't describe the function.

Comment thread lib/suppressions.cpp Outdated
currLineNr = tok->linenr();
currFileIdx = tok->fileIndex();
for (auto &supp : mSuppressions) {
if (supp.fileName == tokenizer.list.file(tok) && supp.lineNumber == currLineNr) {
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.

Maybe you can put the lineNumber comparison before the filename comparison. A integer comparison should be faster than a string comparison.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

It is run one time per configuration for every file. Even if hash would skip other checks. I put it before that check cause I had problem with some suppression's not getting their check flag set. But that is mostly because putting 'same' test in different ifdef combination. In the 'real' world that maybe not happening so often.

Also, maybe some debug flag should print suppression's that does not have the checked flag set.

Comment thread test/testsuppressions.cpp Outdated
void inlinesuppress_unusedFunction() const { // #4210, #4946 - wrong report of "unmatchedSuppression" for "unusedFunction"
Suppressions suppressions;
suppressions.addSuppression(Suppressions::Suppression("unusedFunction", "test.c", 3));
auto spr = Suppressions::Suppression("unusedFunction", "test.c", 3);
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.

generally.. I think it's better to write out the full name. suppression is better that spr unless it's an abbreviation used everywhere..

@jpyllman
Copy link
Copy Markdown
Contributor Author

Question, how much do save by calculating the checksum and then skipping some configurations cause of same checksum?? I put the markUnmatchedInlineSuppressionsAsChecked() call before the check to ensure all relevant suppression's get checked. But maybe I am to cautious from having constructed a very strange test to trigger things.

@danmar
Copy link
Copy Markdown
Collaborator

danmar commented Mar 28, 2022

Question, how much do save by calculating the checksum and then skipping some configurations cause of same checksum??

When incremental analysis is used.. for an unchanged code it can save ~ 98% of time or something like that.

The calculation is quick. Analysis is very slow.

@danmar
Copy link
Copy Markdown
Collaborator

danmar commented Mar 28, 2022

sorry there is some conflict now.. could you look at that.

@jpyllman
Copy link
Copy Markdown
Contributor Author

sorry there is some conflict now.. could you look at that.

The conflict is the Makefile generated by dmake. But if I do not generate that file some CI will fail. I will put up my latest version. The reason why I talk about the checksum is that at the moment the check if suppression is being checked is done before. If we do after some of the work will already be done by the normal checks. The thing is just if we should have some extra check available to report if there are suppression's that are not marked as checked.

@danmar
Copy link
Copy Markdown
Collaborator

danmar commented Mar 28, 2022

The thing is just if we should have some extra check available to report if there are suppression's that are not marked as checked.

Yes that sounds reasonable. To avoid that unused inline suppressions are left by mistake in the code.
Use some new id.

@danmar danmar merged commit 34a11c1 into cppcheck-opensource:main Mar 30, 2022
@danmar
Copy link
Copy Markdown
Collaborator

danmar commented Mar 30, 2022

I merged your changes so far. feel free to look into the unmatchedsuppression.

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