Sparse is a computer software tool designed to find possible coding faults in the Linux kernel. Unlike other such tools, this static analysis tool was initially designed to only flag constructs that were likely to be of interest to kernel developers, such as the mixing of pointers to user and kernel address spaces.
Sparse checks for known problems and allows the developer to include annotations in the code that convey information about data types, such as the address space that pointers point to and the locks that a function acquires or releases.
Linus Torvalds started writing Sparse in 2003. Josh Triplett was its maintainer from 2006, a role taken over by Christopher Li in 2009 and by Luc Van Oostenryck in November 2018. Sparse is released under the MIT License.
Some of the checks performed by Sparse require annotating the source code using the <code>__attribute__</code> GCC extension, or the Sparse-specific <code>__context__</code> specifier. Sparse defines the following list of attributes:
When an API is defined with a macro, the specifier <code>__attribute__((context(...)))</code> can be replaced by <code>__context__(...)</code>.
The Linux kernel defines the following short forms as pre-processor macros in files linux/compiler.h and linux/types.h (when building without the <code>__CHECKER__</code> flag, all these annotations are removed from the code):
The types <code>__le32</code> and <code>__be32</code> represent 32-bit integer types with different endianness. However, the C language does not allow to specify that variables of these types should not be mixed. The <code>bitwise</code> attribute is used to mark these types as restricted, so Sparse will give a warning if variables of these types or other integer variables are mixed:
To mark valid conversions between restricted types, a casting with the <code>force</code> attribute is used to avoid Sparse giving a warning.