sparse(1) | General Commands Manual | sparse(1) |
sparse - Semantic Parser for C
sparse [WARNING OPTIONS]... file.c
Sparse parses C source and looks for errors, producing warnings on standard error.
Sparse accepts options controlling the set of warnings to generate. To turn on warnings Sparse does not issue by default, use the corresponding warning option -Wsomething. Sparse issues some warnings by default; to turn off those warnings, pass the negation of the associated warning option, -Wno-something.
Sparse allows an extended attribute __attribute__((address_space(id))) on pointers, which designates a pointer target in address space id (an identifier or a constant integer). With -Waddress-space, Sparse treats pointers with identical target types but different address spaces as distinct types and will warn accordingly.
Sparse will also warn on casts which remove the address space (casts to an integer type or to a plain pointer type). An exception to this is when the destination type is uintptr_t (or unsigned long) since such casts are often used to "get a pointer value representation in an integer type" and such values are independent of the address space.
To override these warnings, use a type that includes __attribute__((force)).
Sparse issues these warnings by default. To turn them off, use -Wno-address-space.
Sparse supports an extended attribute, __attribute__((bitwise)), which creates a new restricted integer type from a base integer type, distinct from the base integer type and from any other restricted integer type not declared in the same declaration or typedef. For example, this allows programs to create typedefs for integer types with specific endianness. With -Wbitwise, Sparse will warn on any use of a restricted type in arithmetic operations other than bitwise operations, and on any conversion of one restricted type into another, except via a cast that includes __attribute__((force)).
__bitwise ends up being a "stronger integer separation", one that doesn't allow you to mix with non-bitwise integers, so now it's much harder to lose the type by mistake.
__bitwise is for *unique types* that cannot be mixed with other types, and that you'd never want to just use as a random integer (the integer 0 is special, though, and gets silently accepted iirc - it's kind of like "NULL" for pointers). So "gfp_t" or the "safe endianness" types would be __bitwise: you can only operate on them by doing specific operations that know about *that* particular type.
Sparse issues these warnings by default. To turn them off, use -Wno-bitwise.
Sparse does not issue these warnings by default.
This is similar to -Waddress-space but will also warn on casts to unsigned long.
Sparse does not issues these warnings by default.
A cast that includes __attribute__((force)) will suppress this warning. No warning is generated if the original type is uintptr_t (or unsigned long).
Sparse does not issue these warnings by default.
Sparse issues these warnings by default. To turn them off, use -Wno-cast-truncate.
Sparse does not issue these warnings by default.
Sparse does not issue these warnings by default.
Sparse supports several means of designating functions or statements that delimit contexts, such as synchronization. Functions with the extended attribute __attribute__((context(expression,in_context,out_context)) require the context expression (for instance, a lock) to have the value in_context (a constant nonnegative integer) when called, and return with the value out_context (a constant nonnegative integer). For APIs defined via macros, use the statement form __context__(expression,in_value,out_value) in the body of the macro.
With -Wcontext Sparse will warn when it sees a function change the context without indicating this with a context attribute, either by decreasing a context below zero (such as by releasing a lock without acquiring it), or returning with a changed context (such as by acquiring a lock without releasing it). Sparse will also warn about blocks of code which may potentially execute with different contexts.
Sparse issues these warnings by default. To turn them off, use -Wno-context.
Private symbols (functions and variables) internal to a given source file should use static, to allow additional compiler optimizations, allow detection of unused symbols, and prevent other code from relying on these internal symbols. Public symbols used by other source files will need declarations visible to those other source files, such as in a header file. All declarations should fall into one of these two categories. Thus, with -Wdecl, Sparse warns about any symbol definition with neither static nor a declaration. To fix this warning, declare private symbols static, and ensure that the files defining public symbols have the symbol declarations available first (such as by including the appropriate header file).
Sparse issues these warnings by default. To turn them off, use -Wno-decl.
These declarations are permitted in C99 but not in C89.
Sparse issues these warnings by default only when the C dialect is C89 (i.e. -ansi or -std=c89). To turn them off, use -Wno-declaration-after-statement.
Bitfields have no standard-specified default signedness. (C99 6.7.2) A bitfield without an explicit signed or unsigned creates a portability problem for software that relies on the available range of values. To fix this, specify the bitfield type as signed or unsigned explicitly.
Sparse does not issue these warnings by default.
Sparse allows an attribute __attribute__((designated_init)) which marks a struct as requiring designated initializers. Sparse will warn about positional initialization of a struct variable or struct literal of a type that has this attribute.
Requiring designated initializers for a particular struct type will insulate code using that struct type from changes to the layout of the type, avoiding the need to change initializers for that type unless they initialize a removed or incompatibly changed field.
Common examples of this type of struct include collections of function pointers for the implementations of a class of related operations, for which the default NULL for an unmentioned field in a designated initializer will correctly indicate the absence of that operation.
Sparse issues these warnings by default. To turn them off, use -Wno-designated-init.
Sparse does not issue these warnings by default.
Sparse issues these warnings by default. To turn them off, use -Wno-enum-mismatch.
Sparse issues these warnings by default. To turn them off, use -Wno-external-function-has-definition.
Sparse issues these warnings by default. To turn them off, use -Wno-flexible-array-array.
Sparse does not issue these warnings by default.
Sparse does not issue these warnings by default.
Sparse does issue these warnings by default.
If the size of the char array and the length of the string are the same, there is no space for the last nul char of the string in the array:
char s[3] = "abc";
If the array is used as a byte array, not as C string, this warning is just noise. However, if the array is passed to functions dealing with C string like printf(%s) and strcmp, it may cause a trouble.
Sparse does not issue these warnings by default.
Sparse issues these warnings by default. To turn them off, use -Wno-memcpy-max-count.
The limit can be changed with -fmemcpy-max-count=COUNT, the default being 100000.
Sparse issues these warnings by default. To turn them off, use -Wno-newline-eof.
0 has integer type. NULL has pointer type.
Sparse issues these warnings by default. To turn them off, use -Wno-non-pointer-null.
C99 provides a standard syntax for designated fields in struct or union initializers:
struct structname var = { .field = value };
GCC also has an old, non-standard syntax for designated initializers which predates C99:
struct structname var = { field: value };
Sparse will warn about the use of GCC's non-standard syntax for designated initializers. To fix this warning, convert designated initializers to use the standard C99 syntax.
Sparse issues these warnings by default. To turn them off, use -Wno-old-initializer.
A one-bit signed bitfield can only have the values 0 and -1, or with some compilers only 0; this results in unexpected behavior for programs which expected the ability to store 0 and 1.
Sparse issues these warnings by default. To turn them off, use -Wno-one-bit-signed-bitfield.
Standard C syntax does not permit a parenthesized string as an array initializer. GCC allows this syntax as an extension. With -Wparen-string, Sparse will warn about this syntax.
Sparse does not issue these warnings by default.
Sparse does not issue these warnings by default.
C99 does not allow the sizeof operator to be applied to function types or to incomplete types such as void. GCC allows sizeof to be applied to these types as an extension and assigns these types a size of 1. With -pointer-arith, Sparse will warn about pointer arithmetic on void or function pointers, as well as expressions which directly apply the sizeof operator to void or function types.
Sparse does not issue these warnings by default.
Subtracting two pointers to a given type gives a difference in terms of the number of items of that type. To generate this value, compilers will usually need to divide the difference by the size of the type, an potentially expensive operation for sizes other than powers of two.
Code written using pointer subtraction can often use another approach instead, such as array indexing with an explicit array index variable, which may allow compilers to generate more efficient code.
Sparse does not issue these warnings by default.
C99 permits this, and in some cases this allows for more generic code in macros that use typeof or take a type as a macro argument. However, some programs consider this poor style, and those programs can use -Wreturn-void to get warnings about it.
Sparse does not issue these warnings by default.
Such declarations can lead to error-prone code.
Sparse does not issue these warnings by default.
Sparse issues these warnings by default.
Sparse issues these warnings by default.
C99 does not specify the size of a _Bool. GCC, by default, uses 1.
Sparse does not issue these warnings by default.
Sparse issues these warnings by default. To turn them off, use -Wno-transparent-union.
Sparse does not issue these warnings by default.
Standard C (C99 6.10.1) permits using the value of an undefined preprocessor symbol in preprocessor conditionals, and specifies it has a value of 0. However, this behavior can lead to subtle errors.
Sparse does not issue these warnings by default.
Sparse does not issue these warnings by default, processing '{ 0 }' the same as '{ }'.
Sparse does not issues these warnings by default.
The default architecture and size is the one of the machine used to build Sparse.
The default OS is the one of the machine used to build Sparse if it can be detected, otherwise some generic settings are used.
https://sparse.docs.kernel.org
linux-sparse@vger.kernel.org
Submission of patches and reporting of bugs, as well as discussions related to Sparse, should be done via the mailing list (linux-sparse@vger.kernel.org) where the development and maintenance is primarily done. You do not have to be subscribed to the list to send a message there.
Bugs can also be reported and tracked via the Linux kernel's bugzilla: http://bugzilla.kernel.org/enter_bug.cgi?component=Sparse&product=Tools .
More documentation about Sparse can be found at https://sparse.docs.kernel.org
Sparse was started by Linus Torvalds. The complete list of contributors can be find at https://www.openhub.net/p/sparse/contributors .
Luc Van Oostenryck is Sparse's current maintainer.