Static analyzers are tools that, by examining the source code, attempt to identify potential errors within it. They can be tremendously useful to you as the developer, though you must learn how to "tame" them – in the sense that they can result in false positives.
Several useful static analysis tools exist. Among them, the ones that are more relevant for Linux kernel code analysis include the following:
- Sparse: https://sparse.wiki.kernel.org/index.php/Main_Page
- Coccinelle: http://coccinelle.lip6.fr/ (requires the ocaml package installed)
- Smatch: http://smatch.sourceforge.net/, http://repo.or.cz/w/smatch.git
- Flawfinder: https://dwheeler.com/flawfinder/
- Cppcheck: https://github.com/danmar/cppcheck
For example, to install and try Sparse, do the following:
sudo apt install sparse
cd <kernel-src-tree>
make C=1 CHECK="/usr/bin/sparse"
There are also several high-quality commercial static analysis tools available. Among them are the following:
- SonarQube: https://www.sonarqube.org/ (a free and open source community edition is available)
- Coverity Scan: https://scan.coverity.com/
- Klocwork: https://www.meteonic.com/klocwork
clang is a frontend to GCC that is becoming more popular even for kernel builds. You can install it on Ubuntu with sudo apt install clang clang-tools.
Static analysis tools can save the day. Time spent learning to use them effectively is time well spent!