I have a code fragment that drives cppcheck nuts, because it doesn't see variables used in the log call as used. So I get unused variables and a scope reduction warning:
double start = GetTimeOfDayDoubleSec(), afterDb = 0;
if (LoadFromDatabase(serials)) {
afterDb = GetTimeOfDayDoubleSec();
Cache.ResetDebugFlags(serials);
}
double end = GetTimeOfDayDoubleSec();
ZLOG_INFO("DB time %f, total %f", afterDb ? afterDb - start : 0, end - start);
Cppcheck says:
The scope of the variable 'afterDb' can be reduced.
Variable 'afterDb' is assigned a value that is never used.
I can't work out the syntax to suppress both of those and the manual is unhelpful. Separate line, spaces, commas, colons and semicolons all fail. Separate lines gives me "suppression not matched", the rest are just invalid:
//cppcheck-suppress variableScope
//cppcheck-suppress unreadVariable
//cppcheck-suppress variableScope unreadVariable
//cppcheck-suppress variableScope,unreadVariable
//cppcheck-suppress variableScope;unreadVariable
double afterDb = 0;
Failed to add suppression. Invalid id "variableScope:unreadVariable"
Does cppcheck let me do this inline, or do I have to do it using XML on the command line?
Sorry, it turns out that there's a confounding issue: hitting "refresh" in cppcheck-gui doesn't refresh everything, I have to reload the cppcheck file to get changes in suppressions to update. Viz, the "open file" icon on the toolbar rather than the right hand "refresh" one.
It turns out that space separated works:
//cppcheck-suppress variableScope unreadVariable
To use multiple suppressions on the same line, use a comma-delimited list.
From the manual:
arr[10] = arr[10] / 0; // cppcheck-suppress[arrayIndexOutOfBounds,zerodiv]
You can also stack the suppressions:
// cppcheck-suppress arrayIndexOutOfBounds
// cppcheck-suppress zerodiv
arr[10] = arr[10] / 0;
First of all... as I read your code it seems to me that Cppcheck writes FP. We should not warn about that code, should we? I'd like that the FP is fixed. I copy and pasted your code, and fail to reproduce a FP for it though - so there is probably something in your real code that confuses Cppcheck.
The code for inline suppressions is in the cppcheck preprocessor (search for "cppcheck-suppress" in the lib/preprocessor.cpp file). As far as I see you can't use multiple inline suppressions currently.
If you have some time and want to help us, feel free to either:
I'd say space separated inline suppressions would be fine.
Does cppcheck let me do this inline, or do I have to do it using XML on the command line?
maybe that is what you meant.. but you can use --suppress or --suppressions-list.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With