1 .\" Sparse manpage by Josh Triplett
5 sparse \- Semantic Parser for C
9 [\fIWARNING OPTIONS\fR]... \fIfile.c\fR
12 Sparse parses C source and looks for errors, producing warnings on standard
15 Sparse accepts options controlling the set of warnings to generate. To turn
16 on warnings Sparse does not issue by default, use the corresponding warning
17 option \fB\-Wsomething\fR. Sparse issues some warnings by default; to turn
18 off those warnings, pass the negation of the associated warning option,
19 \fB\-Wno\-something\fR.
24 Turn on all sparse warnings, except for those explicitly disabled via
25 \fB\-Wno\-something\fR.
28 Warn about code which mixes pointers to different address spaces.
30 Sparse allows an extended attribute
31 .BI __attribute__((address_space( num )))
32 on pointers, which designates a pointer target in address space \fInum\fR (a
33 constant integer). With \fB\-Waddress\-space\fR, Sparse treats pointers with
34 identical target types but different address spaces as distinct types. To
35 override this warning, such as for functions which convert pointers between
36 address spaces, use a type that includes \fB__attribute__((force))\fR.
38 Sparse issues these warnings by default. To turn them off, use
39 \fB\-Wno\-address\-space\fR.
43 Warn about unsupported operations or type mismatches with restricted integer
46 Sparse supports an extended attribute, \fB__attribute__((bitwise))\fR, which
47 creates a new restricted integer type from a base integer type, distinct from
48 the base integer type and from any other restricted integer type not declared
49 in the same declaration or \fBtypedef\fR. For example, this allows programs
50 to create \fBtypedef\fRs for integer types with specific endianness. With
51 \fB-Wbitwise\fR, Sparse will warn on any use of a restricted type in
52 arithmetic operations other than bitwise operations, and on any conversion of
53 one restricted type into another, except via a cast that includes
54 \fB__attribute__((force))\fR.
56 __bitwise ends up being a "stronger integer separation". That one
57 doesn't allow you to mix with non-bitwise integers, so now it's much
58 harder to lose the type by mistake.
60 __bitwise is for *unique types* that cannot be mixed with other
61 types, and that you'd never want to just use as a random integer (the
62 integer 0 is special, though, and gets silently accepted iirc - it's
63 kind of like "NULL" for pointers). So "gfp_t" or the "safe endianness"
64 types would be __bitwise: you can only operate on them by doing
65 specific operations that know about *that* particular type.
67 Generally, you want bitwise if you are looking for type safety. Sparse
68 does not issue these warnings by default.
72 Warn about casts which add an address space to a pointer type.
74 A cast that includes \fB__attribute__((force))\fR will suppress this warning.
76 Sparse does not issue these warnings by default.
80 Warn about casts that truncate constant values.
82 Sparse issues these warnings by default. To turn them off, use
83 \fB\-Wno\-cast\-truncate\fR.
87 Warn about potential errors in synchronization or other delimited contexts.
89 Sparse supports several means of designating functions or statements that
90 delimit contexts, such as synchronization. Functions with the extended
92 .BI __attribute__((context( expression , in_context , out_context ))
93 require the context \fIexpression\fR (for instance, a lock) to have the value
94 \fIin_context\fR (a constant nonnegative integer) when called, and return with
95 the value \fIout_context\fR (a constant nonnegative integer). For APIs
96 defined via macros, use the statement form
97 .BI __context__( expression , in_value , out_value )
98 in the body of the macro.
100 With \fB-Wcontext\fR Sparse will warn when it sees a function change the
101 context without indicating this with a \fBcontext\fR attribute, either by
102 decreasing a context below zero (such as by releasing a lock without acquiring
103 it), or returning with a changed context (such as by acquiring a lock without
104 releasing it). Sparse will also warn about blocks of code which may
105 potentially execute with different contexts.
107 Sparse issues these warnings by default. To turn them off, use
108 \fB\-Wno\-context\fR.
112 Warn about any non-\fBstatic\fR variable or function definition that has no
113 previous declaration.
115 Private symbols (functions and variables) internal to a given source file
116 should use \fBstatic\fR, to allow additional compiler optimizations, allow
117 detection of unused symbols, and prevent other code from relying on these
118 internal symbols. Public symbols used by other source files will need
119 declarations visible to those other source files, such as in a header file.
120 All declarations should fall into one of these two categories. Thus, with
121 \fB-Wdecl\fR, Sparse warns about any symbol definition with neither
122 \fBstatic\fR nor a declaration. To fix this warning, declare private symbols
123 \fBstatic\fR, and ensure that the files defining public symbols have the
124 symbol declarations available first (such as by including the appropriate
127 Sparse issues these warnings by default. To turn them off, use
131 .B \-Wdeclaration-after-statement
132 Warn about declarations that are not at the start of a block.
134 These declarations are permitted in C99 but not in C89.
136 Sparse issues these warnings by default only when the C dialect is
137 C89 (i.e. -ansi or -std=c89). To turn them off, use
138 \fB\-Wno\-declaration\-after\-statement\fR.
141 .B \-Wdefault\-bitfield\-sign
142 Warn about any bitfield with no explicit signedness.
144 Bitfields have no standard-specified default signedness. (C99 6.7.2) A
145 bitfield without an explicit \fBsigned\fR or \fBunsigned\fR creates a
146 portability problem for software that relies on the available range of values.
147 To fix this, specify the bitfield type as \fBsigned\fR or \fBunsigned\fR
150 Sparse does not issue these warnings by default.
153 .B \-Wdesignated\-init
154 Warn about positional initialization of structs marked as requiring designated
157 Sparse allows an attribute
158 .BI __attribute__((designated_init))
159 which marks a struct as requiring designated initializers. Sparse will warn
160 about positional initialization of a struct variable or struct literal of a
161 type that has this attribute.
163 Requiring designated initializers for a particular struct type will insulate
164 code using that struct type from changes to the layout of the type, avoiding
165 the need to change initializers for that type unless they initialize a removed
166 or incompatibly changed field.
168 Common examples of this type of struct include collections of function pointers
169 for the implementations of a class of related operations, for which the default
170 NULL for an unmentioned field in a designated initializer will correctly
171 indicate the absence of that operation.
173 Sparse issues these warnings by default. To turn them off, use
174 \fB\-Wno\-designated\-init\fR.
178 Warn about do-while loops that do not delimit the loop body with braces.
180 Sparse does not issue these warnings by default.
184 Warn about the use of an expression of an incorrect \fBenum\fR type when
185 initializing another \fBenum\fR type, assigning to another \fBenum\fR type, or
186 passing an argument to a function which expects another \fBenum\fR type.
188 Sparse issues these warnings by default. To turn them off, use
189 \fB\-Wno\-enum\-mismatch\fR.
193 Warn about initialization of a char array with a too long constant C string.
195 If the size of the char array and the length of the string is the same,
196 there is no space for the last nul char of the string in the array:
202 If the array is used as a byte array, not as C string, this
203 warning is just noise. However, if the array is passed to functions
204 dealing with C string like printf(%s) and strcmp, it may cause a
207 Sparse does not issue these warnings by default.
210 .B \-Wnon\-pointer\-null
211 Warn about the use of 0 as a NULL pointer.
213 0 has integer type. NULL has pointer type.
215 Sparse issues these warnings by default. To turn them off, use
216 \fB\-Wno\-non\-pointer\-null\fR.
219 .B \-Wold\-initializer
220 Warn about the use of the pre-C99 GCC syntax for designated initializers.
222 C99 provides a standard syntax for designated fields in \fBstruct\fR or
223 \fBunion\fR initializers:
226 struct structname var = { .field = value };
229 GCC also has an old, non-standard syntax for designated initializers which
233 struct structname var = { field: value };
236 Sparse will warn about the use of GCC's non-standard syntax for designated
237 initializers. To fix this warning, convert designated initializers to use the
240 Sparse issues these warnings by default. To turn them off, use
241 \fB\-Wno\-old\-initializer\fR.
244 .B \-Wone\-bit\-signed\-bitfield
245 Warn about any one-bit \fBsigned\fR bitfields.
247 A one-bit \fBsigned\fR bitfield can only have the values 0 and -1, or with
248 some compilers only 0; this results in unexpected behavior for programs which
249 expected the ability to store 0 and 1.
251 Sparse issues these warnings by default. To turn them off, use
252 \fB\-Wno\-one\-bit\-signed\-bitfield\fR.
256 Warn about the use of a parenthesized string to initialize an array.
258 Standard C syntax does not permit a parenthesized string as an array
259 initializer. GCC allows this syntax as an extension. With
260 \fB\-Wparen\-string\fR, Sparse will warn about this syntax.
262 Sparse does not issue these warnings by default.
265 .B \-Wptr\-subtraction\-blows
266 Warn when subtracting two pointers to a type with a non-power-of-two size.
268 Subtracting two pointers to a given type gives a difference in terms of the
269 number of items of that type. To generate this value, compilers will usually
270 need to divide the difference by the size of the type, an potentially
271 expensive operation for sizes other than powers of two.
273 Code written using pointer subtraction can often use another approach instead,
274 such as array indexing with an explicit array index variable, which may allow
275 compilers to generate more efficient code.
277 Sparse does not issue these warnings by default.
281 Warn if a function with return type void returns a void expression.
283 C99 permits this, and in some cases this allows for more generic code in
284 macros that use typeof or take a type as a macro argument. However, some
285 programs consider this poor style, and those programs can use
286 \fB\-Wreturn\-void\fR to get warnings about it.
288 Sparse does not issue these warnings by default.
292 Warn when declaring a symbol which shadows a declaration with the same name in
295 Such declarations can lead to error-prone code.
297 Sparse does not issue these warnings by default.
300 .B \-Wtransparent\-union
301 Warn about any declaration using the GCC extension
302 \fB__attribute__((transparent_union))\fR.
304 Sparse issues these warnings by default. To turn them off, use
305 \fB\-Wno\-transparent\-union\fR.
309 Warn when converting a pointer to an integer type into a pointer to an integer
310 type with different signedness.
312 Sparse does not issue these warnings by default.
316 Warn about preprocessor conditionals that use the value of an undefined
319 Standard C (C99 6.10.1) permits using the value of an undefined preprocessor
320 symbol in preprocessor conditionals, and specifies it has have a value of 0.
321 However, this behavior can lead to subtle errors.
323 Sparse does not issue these warnings by default.
327 .B \-gcc-base-dir \fIdir\fR
328 Look for compiler-provided system headers in \fIdir\fR/include/ and \fIdir\fR/include-fixed/.
333 Set the distance between tab stops. This helps sparse report correct
334 column numbers in warnings or errors. If the value is less than 1 or
335 greater than 100, the option is ignored. The default is 8.
341 http://www.kernel.org/pub/software/devel/sparse/
344 linux-sparse@vger.kernel.org
347 Josh Triplett <josh@kernel.org>