What is Sparse ?
Sparse or semantic Parser for C is a tool built by Linus Torvald and colleagues with the aim of enforcing type-checking. It is actually a static analysis tool. By static we mean it analyses code without executing it; Sparse converts a sequence of characters in the program into a sequence of tokens and analyses the C syntax used.
By token, we mean strings with an assigned or identified meaning. These can be for example keywords, an identifier, a constant, a string, a special symbol, or an operator. A token can also be seen as the smallest element of a program that is meaningful to the compiler.
Sparse is all about semantics (tokens or groupings of tokens) or context(whether a group of tokens belong to a release or acquires or holds or no group).
The semantics parsing process in Sparse involves figuring out which grouping the tokens are and what _types_ are this grouping implies.
Sparse can work as a front end of a compiler and figures out whether a grouping of tokens belongs to an acquire or release lock for example.
The parsing in Sparse is theoretically done in five phases:
- full-file tokenization - pre-processing - semantic parsing - lazy type evaluation - inline function expansion and tree simplification
The above five phases are well described in the link below
The tool is currently maintained by Luc Van Oostenryck.
Why Sparse?
There are many tools, out there in the wild, that Kernel developers and newbies use to find and fix problems in a source code. Each tool excels in its own way.
Typical examples are checkpatch; a coding style checker, Coverity; a commercial tool that fixes Linux code, Coccinelle; used for Linux code matching and transformation, and smatch, to mention but a few.
Why Sparse? Sparse summarises particular features of some codes, find particular possible troublesome patterns and makes it easier to understand or navigate around a large code set. The tool is structured as a library that other tools can easily incorporate. (eg make C=2)
If you are working on a project that requires fixing lock warning as I am or address warning. Sparse will be the most appropriate to find these types of warnings or errors.
Install Sparse
The latest released versions can be downloaded from the Sparse homepage at
https://sparse.wiki.kernel.org/index.php/Main_Page
Alternatively, you can get snapshots of the latest development version of sparse using git to clone:
git clone git://git.kernel.org/pub/scm/devel/sparse/sparse.git
DaveJ has hourly generated tarballs of the git tree available at:
http://www.codemonkey.org.uk/projects/git-snapshots/sparse/
Once you have it, just do:
make
make install
as a regular user, and it will install sparse in your ~/bin directory.
Distributions like Fedora or Ubuntu made things easier, a simple command on a terminal will install Sparse.
sudo dnf install sparse -y
sudo apt install sparse -y
How to use Sparse?
The tool was designed to be particularly small, simple, and easy to use.
Run Sparse on Kernel C files that would get re-compiled only saving our time if in a hurry:
make C=1 pwd_directory/
Run Sparse on all kernel C files regardless of whether they need to get re-compiled or not:
make C=2 pwd_directory/
pwd_directory/ can be any kernel directory where our target C source file(s) is (are) located.
Examples: make C=2 drivers/staging/; make C=1 drivers/staging/
When running make to detect possible coding faults, Sparse offers flexibility in which type of warning or errors to be displayed. For that, a variable CF can be used to pass arguments to sparse.
The build system passes -Wbitwise to sparse automatically.
There are many options : -Wsparse-all, -Waddress-space, -Wcast-to-as, -Wcast-truncate, etc.
Sparse can also be used as a stand-alone application;
if that is the case, just type the command
sparse $filename
$filename for any C files.
Sparse provides a set of annotations designed to convey semantic information about types, such as what address space pointers point to, or what locks a function acquires or releases.