flow: don't print duplicate "unreachable code" warnings
[smatch.git] / sparse.1
blobae85b54a1cca09276fff38fab7cd4b9d1d0063d4
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 \-Waddress\-space
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.
41 .TP
42 .B \-Wbitwise
43 Warn about unsupported operations or type mismatches with restricted integer
44 types.
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.
70 .TP
71 .B \-Wcast\-to\-as
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.
78 .TP
79 .B \-Wcast\-truncate
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.
85 .TP
86 .B \-Wcontext
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
91 attribute
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.
111 .B \-Wdecl
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
125 header file).
127 Sparse issues these warnings by default.  To turn them off, use
128 \fB\-Wno\-decl\fR.
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
148 explicitly.
150 Sparse does not issue these warnings by default.
153 .B \-Wdesignated\-init
154 Warn about positional initialization of structs marked as requiring designated
155 initializers.
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.
177 .B \-Wdo\-while
178 Warn about do-while loops that do not delimit the loop body with braces.
180 Sparse does not issue these warnings by default.
183 .B \-Wenum\-mismatch
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.
192 .B \-Wnon\-pointer\-null
193 Warn about the use of 0 as a NULL pointer.
195 0 has integer type.  NULL has pointer type.
197 Sparse issues these warnings by default.  To turn them off, use
198 \fB\-Wno\-non\-pointer\-null\fR.
201 .B \-Wold\-initializer
202 Warn about the use of the pre-C99 GCC syntax for designated initializers.
204 C99 provides a standard syntax for designated fields in \fBstruct\fR or
205 \fBunion\fR initializers:
208 struct structname var = { .field = value };
211 GCC also has an old, non-standard syntax for designated initializers which
212 predates C99:
215 struct structname var = { field: value };
218 Sparse will warn about the use of GCC's non-standard syntax for designated
219 initializers.  To fix this warning, convert designated initializers to use the
220 standard C99 syntax.
222 Sparse issues these warnings by default.  To turn them off, use
223 \fB\-Wno\-old\-initializer\fR.
226 .B \-Wone\-bit\-signed\-bitfield
227 Warn about any one-bit \fBsigned\fR bitfields.
229 A one-bit \fBsigned\fR bitfield can only have the values 0 and -1, or with
230 some compilers only 0; this results in unexpected behavior for programs which
231 expected the ability to store 0 and 1.
233 Sparse issues these warnings by default.  To turn them off, use
234 \fB\-Wno\-one\-bit\-signed\-bitfield\fR.
237 .B \-Wparen\-string
238 Warn about the use of a parenthesized string to initialize an array.
240 Standard C syntax does not permit a parenthesized string as an array
241 initializer.  GCC allows this syntax as an extension.  With
242 \fB\-Wparen\-string\fR, Sparse will warn about this syntax.
244 Sparse does not issue these warnings by default.
247 .B \-Wptr\-subtraction\-blows
248 Warn when subtracting two pointers to a type with a non-power-of-two size.
250 Subtracting two pointers to a given type gives a difference in terms of the
251 number of items of that type.  To generate this value, compilers will usually
252 need to divide the difference by the size of the type, an potentially
253 expensive operation for sizes other than powers of two.
255 Code written using pointer subtraction can often use another approach instead,
256 such as array indexing with an explicit array index variable, which may allow
257 compilers to generate more efficient code.
259 Sparse does not issue these warnings by default.
262 .B \-Wreturn\-void
263 Warn if a function with return type void returns a void expression.
265 C99 permits this, and in some cases this allows for more generic code in
266 macros that use typeof or take a type as a macro argument.  However, some
267 programs consider this poor style, and those programs can use
268 \fB\-Wreturn\-void\fR to get warnings about it.
270 Sparse does not issue these warnings by default.
273 .B \-Wshadow
274 Warn when declaring a symbol which shadows a declaration with the same name in
275 an outer scope.
277 Such declarations can lead to error-prone code.
279 Sparse does not issue these warnings by default.
282 .B \-Wtransparent\-union
283 Warn about any declaration using the GCC extension
284 \fB__attribute__((transparent_union))\fR.
286 Sparse issues these warnings by default.  To turn them off, use
287 \fB\-Wno\-transparent\-union\fR.
290 .B \-Wtypesign
291 Warn when converting a pointer to an integer type into a pointer to an integer
292 type with different signedness.
294 Sparse does not issue these warnings by default.
297 .B \-Wundef
298 Warn about preprocessor conditionals that use the value of an undefined
299 preprocessor symbol.
301 Standard C (C99 6.10.1) permits using the value of an undefined preprocessor
302 symbol in preprocessor conditionals, and specifies it has have a value of 0.
303 However, this behavior can lead to subtle errors.
305 Sparse does not issue these warnings by default.
307 .SH MISC OPTIONS
309 .B \-gcc-base-dir \fIdir\fR
310 Look for compiler-provided system headers in \fIdir\fR/include/ and \fIdir\fR/include-fixed/.
312 .SH OTHER OPTIONS
314 .B \-ftabstop=WIDTH
315 Set the distance between tab stops.  This helps sparse report correct
316 column numbers in warnings or errors.  If the value is less than 1 or
317 greater than 100, the option is ignored.  The default is 8.
319 .SH SEE ALSO
320 .BR cgcc (1)
322 .SH HOMEPAGE
323 http://www.kernel.org/pub/software/devel/sparse/
325 .SH MAILING LIST
326 linux-sparse@vger.kernel.org
328 .SH MAINTAINER
329 Josh Triplett <josh@kernel.org>