Remove some false positives and enable the check.
[smatch.git] / sparse.1
blobd242dc755abe02f0500ff93c8341b8e65f9106c7
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 \-Waddress\-space
24 Warn about code which mixes pointers to different address spaces.
26 Sparse allows an extended attribute
27 .BI __attribute__((address_space( num )))
28 on pointers, which designates a pointer target in address space \fInum\fR (a
29 constant integer).  With \fB\-Waddress\-space\fR, Sparse treats pointers with
30 identical target types but different address spaces as distinct types.  To
31 override this warning, such as for functions which convert pointers between
32 address spaces, use a type that includes \fB__attribute__((force))\fR.
34 Sparse issues these warnings by default.  To turn them off, use
35 \fB\-Wno\-address\-space\fR.
37 .TP
38 .B \-Wbitwise
39 Warn about unsupported operations or type mismatches with restricted integer
40 types.
42 Sparse supports an extended attribute, \fB__attribute__((bitwise))\fR, which
43 creates a new restricted integer type from a base integer type, distinct from
44 the base integer type and from any other restricted integer type not declared
45 in the same declaration or \fBtypedef\fR.  For example, this allows programs
46 to create \fBtypedef\fRs for integer types with specific endianness.  With
47 \fB-Wbitwise\fR, Sparse will warn on any use of a restricted type in
48 arithmetic operations other than bitwise operations, and on any conversion of
49 one restricted type into another, except via a cast that includes
50 \fB__attribute__((force))\fR.
52 Sparse does not issue these warnings by default.
54 .TP
55 .B \-Wcast\-to\-as
56 Warn about casts which add an address space to a pointer type.
58 A cast that includes \fB__attribute__((force))\fR will suppress this warning.
60 Sparse does not issue these warnings by default.
62 .TP
63 .B \-Wcast\-truncate
64 Warn about casts that truncate constant values.
66 Sparse issues these warnings by default.  To turn them off, use
67 \fB\-Wno\-cast\-truncate\fR.
69 .TP
70 .B \-Wcontext
71 Warn about potential errors in synchronization or other delimited contexts.
73 Sparse supports several means of designating functions or statements that
74 delimit contexts, such as synchronization.  Functions with the extended
75 attribute
76 .BI __attribute__((context( [expression ,] in_context , out_context ))
77 require the context \fIexpression\fR (for instance, a lock) to have at least the value
78 \fIin_context\fR (a constant nonnegative integer) when called, and return with
79 the value adjusted by \fIout_context - in_context\fR (where
80 \fIout_context\fR is a constant nonnegative integer).  To change the value
81 of a context (for example in macros), use the statement
82 .BI __context__( [expression , ]adjust_value[ , required] )
83 where \fIadjust_value\fR is a constant integer and \fIrequired\fR is a
84 constant nonnegative integer. Not giving \fIrequired\fR is equivalent to
85 giving zero and means that the statement does not need the context as a
86 precondition, when given it means that the context must at least have the
87 value of \fIrequired\fR.
89 To indicate that a function requires
90 .BI exactly
91 a certain lock context (not "at least" as above), use the form
92 .BI __attribute__((exact_context( [expression ,] in_context , out_context ))
93 There currently is no corresponding
94 .BI __exact_context__( [expression , ]adjust_value[ , required] )
95 statement.
97 To indicate that a certain function acquires a context depending on its
98 return value, use
99 .BI __attribute__((conditional_context( [expression ,] in_context , out_success , out_failure ))
100 where \fIout_success\fR and \fIout_failure\fR indicate the context change
101 done depending on success (non-zero) or failure (zero) return of the
102 function. Note that currently, using this attribute on a function means that
103 the function itself won't be checked for context handling at all. See the
104 testsuite for examples.
106 Sparse will warn when it sees a function change a
107 context without indicating this with a \fBcontext\fR or \fBexact_context\fR attribute, either by
108 decreasing a context below zero (such as by releasing a lock without acquiring
109 it), or returning with a changed context (such as by acquiring a lock without
110 releasing it).  Sparse will also warn about blocks of code which may
111 potentially execute with different contexts and about functions that are
112 executed without a lock they require.
114 Sparse issues these warnings by default.  To turn them off, use
115 \fB\-Wno\-context\fR.
118 .B \-Wdecl
119 Warn about any non-\fBstatic\fR variable or function definition that has no
120 previous declaration.
122 Private symbols (functions and variables) internal to a given source file
123 should use \fBstatic\fR, to allow additional compiler optimizations, allow
124 detection of unused symbols, and prevent other code from relying on these
125 internal symbols.  Public symbols used by other source files will need
126 declarations visible to those other source files, such as in a header file.
127 All declarations should fall into one of these two categories.  Thus, with
128 \fB-Wdecl\fR, Sparse warns about any symbol definition with neither
129 \fBstatic\fR nor a declaration.  To fix this warning, declare private symbols
130 \fBstatic\fR, and ensure that the files defining public symbols have the
131 symbol declarations available first (such as by including the appropriate
132 header file).
134 Sparse issues these warnings by default.  To turn them off, use
135 \fB\-Wno\-decl\fR.
138 .B \-Wdeclaration-after-statement
139 Warn about declarations that are not at the start of a block.
141 These declarations are permitted in C99 but not in C89.
143 Sparse issues these warnings by default only when the C dialect is
144 C89 (i.e. -ansi or -std=c89).  To turn them off, use
145 \fB\-Wno\-declaration\-after\-statement\fR.
148 .B \-Wdefault\-bitfield\-sign
149 Warn about any bitfield with no explicit signedness.
151 Bitfields have no standard-specified default signedness. (C99 6.7.2) A
152 bitfield without an explicit \fBsigned\fR or \fBunsigned\fR creates a
153 portability problem for software that relies on the available range of values.
154 To fix this, specify the bitfield type as \fBsigned\fR or \fBunsigned\fR
155 explicitly.
157 Sparse does not issue these warnings by default.
160 .B \-Wdo\-while
161 Warn about do-while loops that do not delimit the loop body with braces.
163 Sparse does not issue these warnings by default.
166 .B \-Wenum\-mismatch
167 Warn about the use of an expression of an incorrect \fBenum\fR type when
168 initializing another \fBenum\fR type, assigning to another \fBenum\fR type, or
169 passing an argument to a function which expects another \fBenum\fR type.
171 Sparse issues these warnings by default.  To turn them off, use
172 \fB\-Wno\-enum\-mismatch\fR.
175 .B \-Wnon\-pointer\-null
176 Warn about the use of 0 as a NULL pointer.
178 0 has integer type.  NULL has pointer type.
180 Sparse issues these warnings by default.  To turn them off, use
181 \fB\-Wno\-non\-pointer\-null\fR.
184 .B \-Wold\-initializer
185 Warn about the use of the pre-C99 GCC syntax for designated initializers.
187 C99 provides a standard syntax for designated fields in \fBstruct\fR or
188 \fBunion\fR initializers:
191 struct structname var = { .field = value };
194 GCC also has an old, non-standard syntax for designated initializers which
195 predates C99:
198 struct structname var = { field: value };
201 Sparse will warn about the use of GCC's non-standard syntax for designated
202 initializers.  To fix this warning, convert designated initializers to use the
203 standard C99 syntax.
205 Sparse issues these warnings by default.  To turn them off, use
206 \fB\-Wno\-old\-initializer\fR.
209 .B \-Wone\-bit\-signed\-bitfield
210 Warn about any one-bit \fBsigned\fR bitfields.
212 A one-bit \fBsigned\fR bitfield can only have the values 0 and -1, or with
213 some compilers only 0; this results in unexpected behavior for programs which
214 expected the ability to store 0 and 1.
216 Sparse issues these warnings by default.  To turn them off, use
217 \fB\-Wno\-one\-bit\-signed\-bitfield\fR.
220 .B \-Wparen\-string
221 Warn about the use of a parenthesized string to initialize an array.
223 Standard C syntax does not permit a parenthesized string as an array
224 initializer.  GCC allows this syntax as an extension.  With
225 \fB\-Wparen\-string\fR, Sparse will warn about this syntax.
227 Sparse does not issue these warnings by default.
230 .B \-Wptr\-subtraction\-blows
231 Warn when subtracting two pointers to a type with a non-power-of-two size.
233 Subtracting two pointers to a given type gives a difference in terms of the
234 number of items of that type.  To generate this value, compilers will usually
235 need to divide the difference by the size of the type, an potentially
236 expensive operation for sizes other than powers of two.
238 Code written using pointer subtraction can often use another approach instead,
239 such as array indexing with an explicit array index variable, which may allow
240 compilers to generate more efficient code.
242 Sparse does not issue these warnings by default.
245 .B \-Wreturn\-void
246 Warn if a function with return type void returns a void expression.
248 C99 permits this, and in some cases this allows for more generic code in
249 macros that use typeof or take a type as a macro argument.  However, some
250 programs consider this poor style, and those programs can use
251 \fB\-Wreturn\-void\fR to get warnings about it.
253 Sparse does not issue these warnings by default.
256 .B \-Wshadow
257 Warn when declaring a symbol which shadows a declaration with the same name in
258 an outer scope.
260 Such declarations can lead to error-prone code.
262 Sparse does not issue these warnings by default.
265 .B \-Wtransparent\-union
266 Warn about any declaration using the GCC extension
267 \fB__attribute__((transparent_union))\fR.
269 Sparse issues these warnings by default.  To turn them off, use
270 \fB\-Wno\-transparent\-union\fR.
273 .B \-Wtypesign
274 Warn when converting a pointer to an integer type into a pointer to an integer
275 type with different signedness.
277 Sparse does not issue these warnings by default.
280 .B \-Wundef
281 Warn about preprocessor conditionals that use the value of an undefined
282 preprocessor symbol.
284 Standard C (C99 6.10.1) permits using the value of an undefined preprocessor
285 symbol in preprocessor conditionals, and specifies it has have a value of 0.
286 However, this behavior can lead to subtle errors.
288 Sparse does not issue these warnings by default.
290 .SH SEE ALSO
291 .BR cgcc (1)
293 .SH HOMEPAGE
294 http://www.kernel.org/pub/software/devel/sparse/
296 .SH MAILING LIST
297 linux-sparse@vger.kernel.org
299 .SH MAINTAINER
300 Josh Triplett <josh@kernel.org>