The following example throws false warnings, caused by the
branch predictor macro. (Obviously in this case the
performance of using UNLIKELY is dubious, but there's other
more performance critical examples I see getting warnings in
my application.)
Can this please get fixed in a future drop? Thanks much.
Thanks
//Cppcheck 1.82:
//
// cppcheck --enable=all bug.cpp
//[bug.cpp:10] -> [include/cppcheck_bug.cpp:12]: (warning) Either the condition '!fp' is redundant or there is possible null pointer dereference: fp.
//[bug.cpp:11]: (error) Resource leak: fp
#include <cstdio>
#define UNLIKELY(x) __builtin_expect(!!(x), 0)
void x() {
FILE* fp = fopen(filenamep, "w");
if (UNLIKELY(!fp)) { // but just "if (!fp)" works
return;
}
fprintf(fp, "...");
fclose(fp);
}
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
__builtin_expect is a GCC extension that is not known by default.
There is a define that "removes" it in gnu.cfg.
To use the gnu.cfg add --library=gnu to the Cppcheck arguments.
With Cppcheck 1.84 i still get the warnings if gnu.cfg is used. I guess the three exclamation marks are a bit confusing.
Telling Cppcheck that UNLIKELY(x) can be ignored (replaced just by x) could help to get rid of the warnings.
Maybe you can simply add a custom configuration file with an according define, but i have not tested it. If the definition of UNLIKELY is seen then the define in the configuration might be overwritten.
Last edit: versat 2018-06-15
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
The following example throws false warnings, caused by the
branch predictor macro. (Obviously in this case the
performance of using UNLIKELY is dubious, but there's other
more performance critical examples I see getting warnings in
my application.)
Can this please get fixed in a future drop? Thanks much.
Thanks
//Cppcheck 1.82:
//
// cppcheck --enable=all bug.cpp
//[bug.cpp:10] -> [include/cppcheck_bug.cpp:12]: (warning) Either the condition '!fp' is redundant or there is possible null pointer dereference: fp.
//[bug.cpp:11]: (error) Resource leak: fp
#include <cstdio>
#define UNLIKELY(x) __builtin_expect(!!(x), 0)
void x() {
FILE* fp = fopen(filenamep, "w");
if (UNLIKELY(!fp)) { // but just "if (!fp)" works
return;
}
fprintf(fp, "...");
fclose(fp);
}
Have you tested this bug in the recently released version 1.84?
__builtin_expect
is a GCC extension that is not known by default.There is a define that "removes" it in gnu.cfg.
To use the gnu.cfg add
--library=gnu
to the Cppcheck arguments.With Cppcheck 1.84 i still get the warnings if gnu.cfg is used. I guess the three exclamation marks are a bit confusing.
Telling Cppcheck that
UNLIKELY(x)
can be ignored (replaced just byx
) could help to get rid of the warnings.Maybe you can simply add a custom configuration file with an according define, but i have not tested it. If the definition of
UNLIKELY
is seen then the define in the configuration might be overwritten.Last edit: versat 2018-06-15