comparison: handle preops like "if (++a == b)"
[smatch.git] / sparse.1
blobacdce531bec000db72e28211adbbebc9ea7c71a2
1 .\" Sparse manpage by Josh Triplett
2 .TH sparse "1"
4 .SH NAME
5 sparse \- Semantic Parser for C
7 .SH SYNOPSIS
8 .B sparse
9 [\fIWARNING OPTIONS\fR]... \fIfile.c\fR
11 .SH DESCRIPTION
12 Sparse parses C source and looks for errors, producing warnings on standard
13 error.
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.
21 .SH WARNING OPTIONS
22 .TP
23 .B \-Wsparse\-all
24 Turn on all sparse warnings, except for those explicitly disabled via
25 \fB\-Wno\-something\fR.
26 .TP
27 .B \-Werror
28 Turn all sparse warnings into errors.
29 .TP
30 .B \-Waddress\-space
31 Warn about code which mixes pointers to different address spaces.
33 Sparse allows an extended attribute
34 .BI __attribute__((address_space( num )))
35 on pointers, which designates a pointer target in address space \fInum\fR (a
36 constant integer).  With \fB\-Waddress\-space\fR, Sparse treats pointers with
37 identical target types but different address spaces as distinct types.  To
38 override this warning, such as for functions which convert pointers between
39 address spaces, use a type that includes \fB__attribute__((force))\fR.
41 Sparse issues these warnings by default.  To turn them off, use
42 \fB\-Wno\-address\-space\fR.
44 .TP
45 .B \-Wbitwise
46 Warn about unsupported operations or type mismatches with restricted integer
47 types.
49 Sparse supports an extended attribute, \fB__attribute__((bitwise))\fR, which
50 creates a new restricted integer type from a base integer type, distinct from
51 the base integer type and from any other restricted integer type not declared
52 in the same declaration or \fBtypedef\fR.  For example, this allows programs
53 to create \fBtypedef\fRs for integer types with specific endianness.  With
54 \fB-Wbitwise\fR, Sparse will warn on any use of a restricted type in
55 arithmetic operations other than bitwise operations, and on any conversion of
56 one restricted type into another, except via a cast that includes
57 \fB__attribute__((force))\fR.
59 __bitwise ends up being a "stronger integer separation". That one
60 doesn't allow you to mix with non-bitwise integers, so now it's much
61 harder to lose the type by mistake.
63 __bitwise is for *unique types* that cannot be mixed with other
64 types, and that you'd never want to just use as a random integer (the
65 integer 0 is special, though, and gets silently accepted iirc - it's
66 kind of like "NULL" for pointers). So "gfp_t" or the "safe endianness"
67 types would be __bitwise: you can only operate on them by doing
68 specific operations that know about *that* particular type.
70 Generally, you want bitwise if you are looking for type safety. Sparse
71 does not issue these warnings by default.
73 .TP
74 .B \-Wcast\-to\-as
75 Warn about casts which add an address space to a pointer type.
77 A cast that includes \fB__attribute__((force))\fR will suppress this warning.
79 Sparse does not issue these warnings by default.
81 .TP
82 .B \-Wcast\-truncate
83 Warn about casts that truncate constant values.
85 Sparse issues these warnings by default.  To turn them off, use
86 \fB\-Wno\-cast\-truncate\fR.
88 .TP
89 .B \-Wcontext
90 Warn about potential errors in synchronization or other delimited contexts.
92 Sparse supports several means of designating functions or statements that
93 delimit contexts, such as synchronization.  Functions with the extended
94 attribute
95 .BI __attribute__((context( expression , in_context , out_context ))
96 require the context \fIexpression\fR (for instance, a lock) to have the value
97 \fIin_context\fR (a constant nonnegative integer) when called, and return with
98 the value \fIout_context\fR (a constant nonnegative integer).  For APIs
99 defined via macros, use the statement form
100 .BI __context__( expression , in_value , out_value )
101 in the body of the macro.
103 With \fB-Wcontext\fR Sparse will warn when it sees a function change the
104 context without indicating this with a \fBcontext\fR attribute, either by
105 decreasing a context below zero (such as by releasing a lock without acquiring
106 it), or returning with a changed context (such as by acquiring a lock without
107 releasing it).  Sparse will also warn about blocks of code which may
108 potentially execute with different contexts.
110 Sparse issues these warnings by default.  To turn them off, use
111 \fB\-Wno\-context\fR.
114 .B \-Wdecl
115 Warn about any non-\fBstatic\fR variable or function definition that has no
116 previous declaration.
118 Private symbols (functions and variables) internal to a given source file
119 should use \fBstatic\fR, to allow additional compiler optimizations, allow
120 detection of unused symbols, and prevent other code from relying on these
121 internal symbols.  Public symbols used by other source files will need
122 declarations visible to those other source files, such as in a header file.
123 All declarations should fall into one of these two categories.  Thus, with
124 \fB-Wdecl\fR, Sparse warns about any symbol definition with neither
125 \fBstatic\fR nor a declaration.  To fix this warning, declare private symbols
126 \fBstatic\fR, and ensure that the files defining public symbols have the
127 symbol declarations available first (such as by including the appropriate
128 header file).
130 Sparse issues these warnings by default.  To turn them off, use
131 \fB\-Wno\-decl\fR.
134 .B \-Wdeclaration-after-statement
135 Warn about declarations that are not at the start of a block.
137 These declarations are permitted in C99 but not in C89.
139 Sparse issues these warnings by default only when the C dialect is
140 C89 (i.e. -ansi or -std=c89).  To turn them off, use
141 \fB\-Wno\-declaration\-after\-statement\fR.
144 .B \-Wdefault\-bitfield\-sign
145 Warn about any bitfield with no explicit signedness.
147 Bitfields have no standard-specified default signedness. (C99 6.7.2) A
148 bitfield without an explicit \fBsigned\fR or \fBunsigned\fR creates a
149 portability problem for software that relies on the available range of values.
150 To fix this, specify the bitfield type as \fBsigned\fR or \fBunsigned\fR
151 explicitly.
153 Sparse does not issue these warnings by default.
156 .B \-Wdesignated\-init
157 Warn about positional initialization of structs marked as requiring designated
158 initializers.
160 Sparse allows an attribute
161 .BI __attribute__((designated_init))
162 which marks a struct as requiring designated initializers.  Sparse will warn
163 about positional initialization of a struct variable or struct literal of a
164 type that has this attribute.
166 Requiring designated initializers for a particular struct type will insulate
167 code using that struct type from changes to the layout of the type, avoiding
168 the need to change initializers for that type unless they initialize a removed
169 or incompatibly changed field.
171 Common examples of this type of struct include collections of function pointers
172 for the implementations of a class of related operations, for which the default
173 NULL for an unmentioned field in a designated initializer will correctly
174 indicate the absence of that operation.
176 Sparse issues these warnings by default.  To turn them off, use
177 \fB\-Wno\-designated\-init\fR.
180 .B \-Wdo\-while
181 Warn about do-while loops that do not delimit the loop body with braces.
183 Sparse does not issue these warnings by default.
186 .B \-Wenum\-mismatch
187 Warn about the use of an expression of an incorrect \fBenum\fR type when
188 initializing another \fBenum\fR type, assigning to another \fBenum\fR type, or
189 passing an argument to a function which expects another \fBenum\fR type.
191 Sparse issues these warnings by default.  To turn them off, use
192 \fB\-Wno\-enum\-mismatch\fR.
195 .B \-Winit\-cstring
196 Warn about initialization of a char array with a too long constant C string.
198 If the size of the char array and the length of the string is the same,
199 there is no space for the last nul char of the string in the array:
202 char s[3] = "abc";
205 If the array is used as a byte array, not as C string, this
206 warning is just noise. However, if the array is passed to functions
207 dealing with C string like printf(%s) and strcmp, it may cause a
208 trouble.
210 Sparse does not issue these warnings by default.
213 .B \-Wnon\-pointer\-null
214 Warn about the use of 0 as a NULL pointer.
216 0 has integer type.  NULL has pointer type.
218 Sparse issues these warnings by default.  To turn them off, use
219 \fB\-Wno\-non\-pointer\-null\fR.
222 .B \-Wold\-initializer
223 Warn about the use of the pre-C99 GCC syntax for designated initializers.
225 C99 provides a standard syntax for designated fields in \fBstruct\fR or
226 \fBunion\fR initializers:
229 struct structname var = { .field = value };
232 GCC also has an old, non-standard syntax for designated initializers which
233 predates C99:
236 struct structname var = { field: value };
239 Sparse will warn about the use of GCC's non-standard syntax for designated
240 initializers.  To fix this warning, convert designated initializers to use the
241 standard C99 syntax.
243 Sparse issues these warnings by default.  To turn them off, use
244 \fB\-Wno\-old\-initializer\fR.
247 .B \-Wone\-bit\-signed\-bitfield
248 Warn about any one-bit \fBsigned\fR bitfields.
250 A one-bit \fBsigned\fR bitfield can only have the values 0 and -1, or with
251 some compilers only 0; this results in unexpected behavior for programs which
252 expected the ability to store 0 and 1.
254 Sparse issues these warnings by default.  To turn them off, use
255 \fB\-Wno\-one\-bit\-signed\-bitfield\fR.
258 .B \-Wparen\-string
259 Warn about the use of a parenthesized string to initialize an array.
261 Standard C syntax does not permit a parenthesized string as an array
262 initializer.  GCC allows this syntax as an extension.  With
263 \fB\-Wparen\-string\fR, Sparse will warn about this syntax.
265 Sparse does not issue these warnings by default.
268 .B \-Wptr\-subtraction\-blows
269 Warn when subtracting two pointers to a type with a non-power-of-two size.
271 Subtracting two pointers to a given type gives a difference in terms of the
272 number of items of that type.  To generate this value, compilers will usually
273 need to divide the difference by the size of the type, an potentially
274 expensive operation for sizes other than powers of two.
276 Code written using pointer subtraction can often use another approach instead,
277 such as array indexing with an explicit array index variable, which may allow
278 compilers to generate more efficient code.
280 Sparse does not issue these warnings by default.
283 .B \-Wreturn\-void
284 Warn if a function with return type void returns a void expression.
286 C99 permits this, and in some cases this allows for more generic code in
287 macros that use typeof or take a type as a macro argument.  However, some
288 programs consider this poor style, and those programs can use
289 \fB\-Wreturn\-void\fR to get warnings about it.
291 Sparse does not issue these warnings by default.
294 .B \-Wshadow
295 Warn when declaring a symbol which shadows a declaration with the same name in
296 an outer scope.
298 Such declarations can lead to error-prone code.
300 Sparse does not issue these warnings by default.
303 .B \-Wsizeof-bool
304 Warn when checking the sizeof a _Bool.
306 C99 does not specify the sizeof a _Bool.  gcc uses 1.
308 Sparse does not issue these warnings by default.
311 .B \-Wtransparent\-union
312 Warn about any declaration using the GCC extension
313 \fB__attribute__((transparent_union))\fR.
315 Sparse issues these warnings by default.  To turn them off, use
316 \fB\-Wno\-transparent\-union\fR.
319 .B \-Wtypesign
320 Warn when converting a pointer to an integer type into a pointer to an integer
321 type with different signedness.
323 Sparse does not issue these warnings by default.
326 .B \-Wundef
327 Warn about preprocessor conditionals that use the value of an undefined
328 preprocessor symbol.
330 Standard C (C99 6.10.1) permits using the value of an undefined preprocessor
331 symbol in preprocessor conditionals, and specifies it has have a value of 0.
332 However, this behavior can lead to subtle errors.
334 Sparse does not issue these warnings by default.
336 .SH MISC OPTIONS
338 .B \-gcc-base-dir \fIdir\fR
339 Look for compiler-provided system headers in \fIdir\fR/include/ and \fIdir\fR/include-fixed/.
341 .SH OTHER OPTIONS
343 .B \-ftabstop=WIDTH
344 Set the distance between tab stops.  This helps sparse report correct
345 column numbers in warnings or errors.  If the value is less than 1 or
346 greater than 100, the option is ignored.  The default is 8.
348 .SH SEE ALSO
349 .BR cgcc (1)
351 .SH HOMEPAGE
352 http://www.kernel.org/pub/software/devel/sparse/
354 .SH MAILING LIST
355 linux-sparse@vger.kernel.org
357 .SH MAINTAINER
358 Josh Triplett <josh@kernel.org>