Static code analysis is the process of detecting flaws in software’s source code. The static analysis tools are useful to detect common coding mistakes; here are some benefits from using them:
- Make the code source more readable and maintainable.
- Prevent unexpected behavior in execution.
- Optimize the execution.
- Make the code more secure.
Many C/C++ static analysis tools exist right there, each one focus on a specific area and has its advantages, we can enumerate:
- Clang Analyzer
- Visual C++ Analyzer
Many ways exist to explore the results of these tools:
- XML format: XML files could be generated from each of these tools, and it can be used to create an HTML report or used by another tool to explore the analysis result.
- HTML format: HTML report is the prefered way to generate reports and share them between the team, and you can create your custom report by using an xsl stylesheet.
- IDE Plugins: almost all known IDE provides plugins for these tools, which gives the possibility to discover all violations from the source code.
One of the problems with code quality tools is that they tend to overwhelm developers with problems that aren’t really problems — that is, false positives. When false positives occur, developers learn to ignore the output of the tool or abandon it altogether.
To explore better their result, it’s interesting to have a way to focus only on what we want and gives to developers a useful view.
CppDepend and CQLinq
CppDepend is another static analysis tool which complements the other ones, it uses a code query langage based on Linq ( CQLinq) to query the code base like a database.
CppDepend embedd by default cppcheck, Vera++ and clang analyzer and It could be easily extensible to support other static analysis tools using its API. The visual Studio Analyzer plugin code source is available to show how to integrate other tools.
Let’s take as example the source code of Clang and discover how we can explore the analysis result of these tools from CppDepend.
Get all issues:
The request to get all issues is very simple. However, as you can see it’s not very interesting, indeed it’s a challenge to treat a result with 331 417 issues.
To treat better the result of these tools we can filter it and focus only on what we want.
Request by tool
We can modify the first request and add a criteria about the tool concerned.
Most recurrent issues
It’s interesting to know which issues are the most reported by these tools.
Classes having most issues
It’s very interesting to know the classes which contains many violations
The previous query is interesting, but it’s not give us exactly the classes with lack of quality, another useful metric to take into account is the NBLinesOfCode. We can modify the previous request and calculate the ratio between the Issues count and the NBLinesofCode.
Most popular methods having issues
When the static analysis tools report the issues, it’s useful to locate which the prioritary issues to resolve? specially if it concerns bugs.
A bug could exist in a specific method, but what interesting to know is how many methods are impacted by the bug. The popular methods are the most used ones and it’s better to resolve them quickly.
Using CQLinq we can combine the result of all these tools and also the result of CppDepend to create more elaborated queries, and add these checks to the build process.
Having issues in a project is not an exception; any project could have many problems to resolve. However, we have to check the quality trend of the project. Indeed it’s a bad indicator if the number of issues grows after changes and evolutions. CppDepend provides the Trend Monitoring feature to create trend charts.
Trend charts are made of trend metrics values logged over time at analysis time. More than 50 trend metrics are available per default and it is easy to create your own trend metrics.
With this trend chart we can monitor the evolution of the Cppcheck issues:
Integrate static analysis tools result into the HTML report
CppDepend makes possible appending extra report sections in the HTML report that lists some CQLinq queries.
In the CQLinq Query Explorer panel, a particular CQLinq reported group is bordered with an orange rectangle.
And in the HTML report these added sections are accessible from the menu:
Integrate static analysis tools result into the build process
CppDepend comes with the notion of Critical CQLinq Rule. Critical rules represent a mean to define high priority rules that must never be violated. With critical rules, it is possible to break the Build Process when a critical rule violation occurs.
A critical rule is just a CQLinq rule with the flag Critical Rule checked:
At Build Process time, when a critical rule is violated the process CppDepend.Console.exe returns a non-zero exit code. This behavior can be used to break the Build Process if a critical rule is violated.
We can easily define a cppcheck critical rule to break the build if kinds of cppcheck issues are found.
CppDepend is open to other static analysis tools, and you can also plug your customized tool easily . This way you can use all the CppDepend features to explore better the result from the known java static analysis tools.