buf_size: fix handling of 1 element arrays
[smatch.git] / sparse.1
blob718bae6fbdfbfd15b2573872888f293d8c9374d0
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 \-Wsparse\-error
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 \-Wbig-constants
46 Warn about large integer constants that are not qualified by a size suffix.
48 Sparse issues these warnings by default.  To turn them off, use
49 \fB\-Wno\-big\-constants\fR.
51 .TP
52 .B \-Wbitwise
53 Warn about unsupported operations or type mismatches with restricted integer
54 types.
56 Sparse supports an extended attribute, \fB__attribute__((bitwise))\fR, which
57 creates a new restricted integer type from a base integer type, distinct from
58 the base integer type and from any other restricted integer type not declared
59 in the same declaration or \fBtypedef\fR.  For example, this allows programs
60 to create \fBtypedef\fRs for integer types with specific endianness.  With
61 \fB-Wbitwise\fR, Sparse will warn on any use of a restricted type in
62 arithmetic operations other than bitwise operations, and on any conversion of
63 one restricted type into another, except via a cast that includes
64 \fB__attribute__((force))\fR.
66 __bitwise ends up being a "stronger integer separation", one that
67 doesn't allow you to mix with non-bitwise integers, so now it's much
68 harder to lose the type by mistake.
70 __bitwise is for *unique types* that cannot be mixed with other
71 types, and that you'd never want to just use as a random integer (the
72 integer 0 is special, though, and gets silently accepted iirc - it's
73 kind of like "NULL" for pointers). So "gfp_t" or the "safe endianness"
74 types would be __bitwise: you can only operate on them by doing
75 specific operations that know about *that* particular type.
77 Sparse issues these warnings by default.  To turn them off, use
78 \fB\-Wno\-bitwise\fR.
80 .TP
81 .B \-Wcast\-to\-as
82 Warn about casts which add an address space to a pointer type.
84 A cast that includes \fB__attribute__((force))\fR will suppress this warning.
86 Sparse does not issue these warnings by default.
88 .TP
89 .B \-Wcast\-truncate
90 Warn about casts that truncate constant values.
92 Sparse issues these warnings by default.  To turn them off, use
93 \fB\-Wno\-cast\-truncate\fR.
95 .TP
96 .B \-Wconstexpr-not-const
97 Warn if a non-constant expression is encountered when really expecting a
98 constant expression instead.
99 Currently, this warns when initializing an object of static storage duration
100 with an initializer which is not a constant expression.
102 Sparse does not issue these warnings by default.
105 .B \-Wcontext
106 Warn about potential errors in synchronization or other delimited contexts.
108 Sparse supports several means of designating functions or statements that
109 delimit contexts, such as synchronization.  Functions with the extended
110 attribute
111 .BI __attribute__((context( expression , in_context , out_context ))
112 require the context \fIexpression\fR (for instance, a lock) to have the value
113 \fIin_context\fR (a constant nonnegative integer) when called, and return with
114 the value \fIout_context\fR (a constant nonnegative integer).  For APIs
115 defined via macros, use the statement form
116 .BI __context__( expression , in_value , out_value )
117 in the body of the macro.
119 With \fB-Wcontext\fR Sparse will warn when it sees a function change the
120 context without indicating this with a \fBcontext\fR attribute, either by
121 decreasing a context below zero (such as by releasing a lock without acquiring
122 it), or returning with a changed context (such as by acquiring a lock without
123 releasing it).  Sparse will also warn about blocks of code which may
124 potentially execute with different contexts.
126 Sparse issues these warnings by default.  To turn them off, use
127 \fB\-Wno\-context\fR.
130 .B \-Wdecl
131 Warn about any non-\fBstatic\fR variable or function definition that has no
132 previous declaration.
134 Private symbols (functions and variables) internal to a given source file
135 should use \fBstatic\fR, to allow additional compiler optimizations, allow
136 detection of unused symbols, and prevent other code from relying on these
137 internal symbols.  Public symbols used by other source files will need
138 declarations visible to those other source files, such as in a header file.
139 All declarations should fall into one of these two categories.  Thus, with
140 \fB-Wdecl\fR, Sparse warns about any symbol definition with neither
141 \fBstatic\fR nor a declaration.  To fix this warning, declare private symbols
142 \fBstatic\fR, and ensure that the files defining public symbols have the
143 symbol declarations available first (such as by including the appropriate
144 header file).
146 Sparse issues these warnings by default.  To turn them off, use
147 \fB\-Wno\-decl\fR.
150 .B \-Wdeclaration-after-statement
151 Warn about declarations that are not at the start of a block.
153 These declarations are permitted in C99 but not in C89.
155 Sparse issues these warnings by default only when the C dialect is
156 C89 (i.e. -ansi or -std=c89).  To turn them off, use
157 \fB\-Wno\-declaration\-after\-statement\fR.
160 .B \-Wdefault\-bitfield\-sign
161 Warn about any bitfield with no explicit signedness.
163 Bitfields have no standard-specified default signedness. (C99 6.7.2) A
164 bitfield without an explicit \fBsigned\fR or \fBunsigned\fR creates a
165 portability problem for software that relies on the available range of values.
166 To fix this, specify the bitfield type as \fBsigned\fR or \fBunsigned\fR
167 explicitly.
169 Sparse does not issue these warnings by default.
172 .B \-Wdesignated\-init
173 Warn about positional initialization of structs marked as requiring designated
174 initializers.
176 Sparse allows an attribute
177 .BI __attribute__((designated_init))
178 which marks a struct as requiring designated initializers.  Sparse will warn
179 about positional initialization of a struct variable or struct literal of a
180 type that has this attribute.
182 Requiring designated initializers for a particular struct type will insulate
183 code using that struct type from changes to the layout of the type, avoiding
184 the need to change initializers for that type unless they initialize a removed
185 or incompatibly changed field.
187 Common examples of this type of struct include collections of function pointers
188 for the implementations of a class of related operations, for which the default
189 NULL for an unmentioned field in a designated initializer will correctly
190 indicate the absence of that operation.
192 Sparse issues these warnings by default.  To turn them off, use
193 \fB\-Wno\-designated\-init\fR.
196 .B \-Wdo\-while
197 Warn about do-while loops that do not delimit the loop body with braces.
199 Sparse does not issue these warnings by default.
202 .B \-Wenum\-mismatch
203 Warn about the use of an expression of an incorrect \fBenum\fR type when
204 initializing another \fBenum\fR type, assigning to another \fBenum\fR type, or
205 passing an argument to a function which expects another \fBenum\fR type.
207 Sparse issues these warnings by default.  To turn them off, use
208 \fB\-Wno\-enum\-mismatch\fR.
211 .B \-Wempty\-character\-constant
212 Warn about a constant such as ''.
214 Sparse issues these warnings by default.  To turn them off, use
215 \fB\-Wno\-empty\-character\-constant\fR.
218 .B \-Winit\-cstring
219 Warn about initialization of a char array with a too long constant C string.
221 If the size of the char array and the length of the string are the same,
222 there is no space for the last nul char of the string in the array:
225 char s[3] = "abc";
228 If the array is used as a byte array, not as C string, this
229 warning is just noise. However, if the array is passed to functions
230 dealing with C string like printf(%s) and strcmp, it may cause a
231 trouble.
233 Sparse does not issue these warnings by default.
236 .B \-Wmemcpy\-max\-count
237 Warn about call of \fBmemcpy()\fR, \fBmemset()\fR, \fBcopy_from_user()\fR, or
238 \fBcopy_to_user()\fR with a large compile-time byte count.
240 Sparse issues these warnings by default. To turn them off, use
241 \fB\-Wno\-memcpy\-max\-count\fR.
243 The limit can be changed with \fB\-fmemcpy\-max\-count=COUNT\fR,
244 the default being \fB100000\fR.
247 .B \-Wnon\-ansi\-function\-declaration
248 Warn about non-ANSI function declarations.
250 Sparse issues these warnings by default.  To turn them off, use
251 \fB\-Wno\-non\-ansi\-function\-declaration\fR.
254 .B \-Wnon\-pointer\-null
255 Warn about the use of 0 as a NULL pointer.
257 0 has integer type.  NULL has pointer type.
259 Sparse issues these warnings by default.  To turn them off, use
260 \fB\-Wno\-non\-pointer\-null\fR.
263 .B \-Wold\-initializer
264 Warn about the use of the pre-C99 GCC syntax for designated initializers.
266 C99 provides a standard syntax for designated fields in \fBstruct\fR or
267 \fBunion\fR initializers:
270 struct structname var = { .field = value };
273 GCC also has an old, non-standard syntax for designated initializers which
274 predates C99:
277 struct structname var = { field: value };
280 Sparse will warn about the use of GCC's non-standard syntax for designated
281 initializers.  To fix this warning, convert designated initializers to use the
282 standard C99 syntax.
284 Sparse issues these warnings by default.  To turn them off, use
285 \fB\-Wno\-old\-initializer\fR.
288 .B \-Wone\-bit\-signed\-bitfield
289 Warn about any one-bit \fBsigned\fR bitfields.
291 A one-bit \fBsigned\fR bitfield can only have the values 0 and -1, or with
292 some compilers only 0; this results in unexpected behavior for programs which
293 expected the ability to store 0 and 1.
295 Sparse issues these warnings by default.  To turn them off, use
296 \fB\-Wno\-one\-bit\-signed\-bitfield\fR.
299 .B \-Wparen\-string
300 Warn about the use of a parenthesized string to initialize an array.
302 Standard C syntax does not permit a parenthesized string as an array
303 initializer.  GCC allows this syntax as an extension.  With
304 \fB\-Wparen\-string\fR, Sparse will warn about this syntax.
306 Sparse does not issue these warnings by default.
309 .B \-Wpointer\-arith
310 Warn about anything that depends on the \fBsizeof\fR a void or function type.
312 C99 does not allow the \fBsizeof\fR operator to be applied to function types
313 or to incomplete types such as void. GCC allows \fBsizeof\fR to be applied to
314 these types as an extension and assigns these types a size of \fI1\fR. With
315 \fB\-pointer\-arith\fR, Sparse will warn about pointer arithmetic on void
316 or function pointers, as well as expressions which directly apply the
317 \fBsizeof\fR operator to void or function types.
319 Sparse does not issue these warnings by default.
322 .B \-Wptr\-subtraction\-blows
323 Warn when subtracting two pointers to a type with a non-power-of-two size.
325 Subtracting two pointers to a given type gives a difference in terms of the
326 number of items of that type.  To generate this value, compilers will usually
327 need to divide the difference by the size of the type, an potentially
328 expensive operation for sizes other than powers of two.
330 Code written using pointer subtraction can often use another approach instead,
331 such as array indexing with an explicit array index variable, which may allow
332 compilers to generate more efficient code.
334 Sparse does not issue these warnings by default.
337 .B \-Wreturn\-void
338 Warn if a function with return type void returns a void expression.
340 C99 permits this, and in some cases this allows for more generic code in
341 macros that use typeof or take a type as a macro argument.  However, some
342 programs consider this poor style, and those programs can use
343 \fB\-Wreturn\-void\fR to get warnings about it.
345 Sparse does not issue these warnings by default.
348 .B \-Wshadow
349 Warn when declaring a symbol which shadows a declaration with the same name in
350 an outer scope.
352 Such declarations can lead to error-prone code.
354 Sparse does not issue these warnings by default.
357 .B \-Wsizeof-bool
358 Warn when checking the sizeof a _Bool.
360 C99 does not specify the sizeof a _Bool.  gcc uses 1.
362 Sparse does not issue these warnings by default.
365 .B \-Wtransparent\-union
366 Warn about any declaration using the GCC extension
367 \fB__attribute__((transparent_union))\fR.
369 Sparse issues these warnings by default.  To turn them off, use
370 \fB\-Wno\-transparent\-union\fR.
373 .B \-Wtypesign
374 Warn when converting a pointer to an integer type into a pointer to an integer
375 type with different signedness.
377 Sparse does not issue these warnings by default.
380 .B \-Wundef
381 Warn about preprocessor conditionals that use the value of an undefined
382 preprocessor symbol.
384 Standard C (C99 6.10.1) permits using the value of an undefined preprocessor
385 symbol in preprocessor conditionals, and specifies it has a value of 0.
386 However, this behavior can lead to subtle errors.
388 Sparse does not issue these warnings by default.
390 .SH MISC OPTIONS
392 .B \-gcc-base-dir \fIdir\fR
393 Look for compiler-provided system headers in \fIdir\fR/include/ and \fIdir\fR/include-fixed/.
396 .B \-multiarch-dir \fIdir\fR
397 Look for system headers in the multiarch subdirectory \fIdir\fR.
398 The \fIdir\fR name would normally take the form of the target's
399 normalized GNU triplet. (e.g. i386-linux-gnu).
401 .SH DEBUG OPTIONS
403 .B \-fdump-linearize[=only]
404 Dump the IR code of a function directly after its linearization,
405 before any simplifications are made. If the argument \fB=only\fR is
406 also given no further processing is done on the function.
408 .B \-fmem-report
409 Report some statistics about memory allocation used by the tool.
411 .SH OTHER OPTIONS
413 .B \-fmemcpy-max-count=COUNT
414 Set the limit for the warnings given by \fB-Wmemcpy-max-count\fR.
415 A COUNT of 0, useless in itself, will effectively disable the warning.
416 The default limit is 100000.
419 .B \-ftabstop=WIDTH
420 Set the distance between tab stops.  This helps sparse report correct
421 column numbers in warnings or errors.  If the value is less than 1 or
422 greater than 100, the option is ignored.  The default is 8.
424 .SH SEE ALSO
425 .BR cgcc (1)
427 .SH HOMEPAGE
428 http://www.kernel.org/pub/software/devel/sparse/
430 .SH MAILING LIST
431 linux-sparse@vger.kernel.org
433 .SH MAINTAINER
434 Christopher Li <sparse@chrisli.org>