Static analysis
With our library, one can check that the following code is valid but it may not be what the programmer intended:
Sphere s1(.1);
Sphere s2(.2);
std::pair<Sphere, Sphere> p(s1,2);
As the implicit conversion is allowed on the Sphere constructor, the pair of Sphere objects p is composed of the Sphere s1 and the Sphere(2) which is not the same as a pair of s1 and s2. In the case of a typo in the code source, this may certainly lead to bugs.
A static code analysis may help in the discovery of those potentials bugs.
cppcheck
cppcheck is a command-line tool dedicated to static C/C++ code analysis. It tries to detect bugs that your C/C++ compiler does not see. It is versatile, and can check non-standard code including various compiler extensions, inline assembly code, etc. Its internal preprocessor can handle includes, macros, and several preprocessor commands.
Applying this tool on our project, we get an analysis report using the command:
cppcheck <your source directory> -f -q --enable=style --output-file=<output file>
Exercise
graph LR;
subgraph build
B(Build);
end
subgraph test
C(Unit tests);
D(Warnings tests);
F(Static tests)
end
subgraph coverage
E(Coverage);
end
B-->C
B-->D
B-->F
C-->E
D-->E
F-->E
Add static tests with cppcheck, the same way it as been done for warning tests
Home | C++ Home | << C++ Previous - Exercise 4 | >> C++ Next - Compilation in a docker container