sparse.1 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583
  1. .\" Sparse manpage by Josh Triplett
  2. .TH sparse "1"
  3. .
  4. .SH NAME
  5. sparse \- Semantic Parser for C
  6. .
  7. .SH SYNOPSIS
  8. .B sparse
  9. [\fIWARNING OPTIONS\fR]... \fIfile.c\fR
  10. .
  11. .SH DESCRIPTION
  12. Sparse parses C source and looks for errors, producing warnings on standard
  13. error.
  14. .P
  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.
  20. .
  21. .SH WARNING OPTIONS
  22. .TP
  23. .B \-fmax-errors=COUNT
  24. Set the maximum number of displayed errors to COUNT, which should be
  25. a numerical value or 'unlimited'.
  26. The default limit is 100.
  27. .
  28. .TP
  29. .B \-fmax-warnings=COUNT
  30. Set the maximum number of displayed warnings to COUNT, which should be
  31. a numerical value or 'unlimited'.
  32. The default limit is 100.
  33. .
  34. .TP
  35. .B \-Wsparse\-all
  36. Turn on all sparse warnings, except for those explicitly disabled via
  37. \fB\-Wno\-something\fR.
  38. .TP
  39. .B \-Wsparse\-error
  40. Turn all sparse warnings into errors.
  41. .TP
  42. .B \-Waddress\-space
  43. Warn about code which mixes pointers to different address spaces.
  44. Sparse allows an extended attribute
  45. .BI __attribute__((address_space( id )))
  46. on pointers, which designates a pointer target in address space \fIid\fR (an
  47. identifier or a constant integer).
  48. With \fB\-Waddress\-space\fR, Sparse treats pointers with
  49. identical target types but different address spaces as distinct types and
  50. will warn accordingly.
  51. Sparse will also warn on casts which remove the address space (casts to an
  52. integer type or to a plain pointer type). An exception to this is when the
  53. destination type is \fBuintptr_t\fR (or \fBunsigned long\fR) since such casts
  54. are often used to "get a pointer value representation in an integer type" and
  55. such values are independent of the address space.
  56. To override these warnings, use a type that includes \fB__attribute__((force))\fR.
  57. Sparse issues these warnings by default. To turn them off, use
  58. \fB\-Wno\-address\-space\fR.
  59. .
  60. .TP
  61. .B \-Wbitwise
  62. Warn about unsupported operations or type mismatches with restricted integer
  63. types.
  64. Sparse supports an extended attribute, \fB__attribute__((bitwise))\fR, which
  65. creates a new restricted integer type from a base integer type, distinct from
  66. the base integer type and from any other restricted integer type not declared
  67. in the same declaration or \fBtypedef\fR. For example, this allows programs
  68. to create \fBtypedef\fRs for integer types with specific endianness. With
  69. \fB-Wbitwise\fR, Sparse will warn on any use of a restricted type in
  70. arithmetic operations other than bitwise operations, and on any conversion of
  71. one restricted type into another, except via a cast that includes
  72. \fB__attribute__((force))\fR.
  73. __bitwise ends up being a "stronger integer separation", one that
  74. doesn't allow you to mix with non-bitwise integers, so now it's much
  75. harder to lose the type by mistake.
  76. __bitwise is for *unique types* that cannot be mixed with other
  77. types, and that you'd never want to just use as a random integer (the
  78. integer 0 is special, though, and gets silently accepted iirc - it's
  79. kind of like "NULL" for pointers). So "gfp_t" or the "safe endianness"
  80. types would be __bitwise: you can only operate on them by doing
  81. specific operations that know about *that* particular type.
  82. Sparse issues these warnings by default. To turn them off, use
  83. \fB\-Wno\-bitwise\fR.
  84. .
  85. .TP
  86. .B \-Wbitwise\-pointer
  87. Same as \fB\-Wbitwise\fR but for casts to or from pointers to bitwise types.
  88. Sparse does not issue these warnings by default.
  89. .
  90. .TP
  91. .B \-Wcast\-from\-as
  92. Warn about casts which remove an address space from a pointer type.
  93. This is similar to \fB\-Waddress\-space\fR but will also warn
  94. on casts to \fBunsigned long\fR.
  95. Sparse does not issues these warnings by default.
  96. .
  97. .TP
  98. .B \-Wcast\-to\-as
  99. Warn about casts which add an address space to a pointer type.
  100. A cast that includes \fB__attribute__((force))\fR will suppress this warning.
  101. No warning is generated if the original type is \fBuintptr_t\fR
  102. (or \fBunsigned long\fR).
  103. Sparse does not issue these warnings by default.
  104. .
  105. .TP
  106. .B \-Wcast\-truncate
  107. Warn about casts that truncate constant values.
  108. Sparse issues these warnings by default. To turn them off, use
  109. \fB\-Wno\-cast\-truncate\fR.
  110. .
  111. .TP
  112. .B \-Wconstant\-suffix
  113. Warn if an integer constant is larger than the maximum representable value
  114. of the type indicated by its type suffix (if any). For example, on a
  115. system where ints are 32-bit and longs 64-bit, the constant \fB0x100000000U\fR
  116. is larger than can be represented by an \fBunsigned int\fR but fits in an
  117. \fBunsigned long\fR. So its type is \fBunsigned long\fR but this is not
  118. indicated by its suffix. In this case, the warning could be suppressed by
  119. using the suffix \fBUL\fR: \fB0x100000000UL\fR.
  120. Sparse does not issue these warnings by default.
  121. .
  122. .TP
  123. .B \-Wconstexpr-not-const
  124. Warn if a non-constant expression is encountered when really expecting a
  125. constant expression instead.
  126. Currently, this warns when initializing an object of static storage duration
  127. with an initializer which is not a constant expression.
  128. Sparse does not issue these warnings by default.
  129. .
  130. .TP
  131. .B \-Wcontext
  132. Warn about potential errors in synchronization or other delimited contexts.
  133. Sparse supports several means of designating functions or statements that
  134. delimit contexts, such as synchronization. Functions with the extended
  135. attribute
  136. .BI __attribute__((context( expression , in_context , out_context ))
  137. require the context \fIexpression\fR (for instance, a lock) to have the value
  138. \fIin_context\fR (a constant nonnegative integer) when called, and return with
  139. the value \fIout_context\fR (a constant nonnegative integer). For APIs
  140. defined via macros, use the statement form
  141. .BI __context__( expression , in_value , out_value )
  142. in the body of the macro.
  143. With \fB-Wcontext\fR Sparse will warn when it sees a function change the
  144. context without indicating this with a \fBcontext\fR attribute, either by
  145. decreasing a context below zero (such as by releasing a lock without acquiring
  146. it), or returning with a changed context (such as by acquiring a lock without
  147. releasing it). Sparse will also warn about blocks of code which may
  148. potentially execute with different contexts.
  149. Sparse issues these warnings by default. To turn them off, use
  150. \fB\-Wno\-context\fR.
  151. .
  152. .TP
  153. .B \-Wdecl
  154. Warn about any non-\fBstatic\fR variable or function definition that has no
  155. previous declaration.
  156. Private symbols (functions and variables) internal to a given source file
  157. should use \fBstatic\fR, to allow additional compiler optimizations, allow
  158. detection of unused symbols, and prevent other code from relying on these
  159. internal symbols. Public symbols used by other source files will need
  160. declarations visible to those other source files, such as in a header file.
  161. All declarations should fall into one of these two categories. Thus, with
  162. \fB-Wdecl\fR, Sparse warns about any symbol definition with neither
  163. \fBstatic\fR nor a declaration. To fix this warning, declare private symbols
  164. \fBstatic\fR, and ensure that the files defining public symbols have the
  165. symbol declarations available first (such as by including the appropriate
  166. header file).
  167. Sparse issues these warnings by default. To turn them off, use
  168. \fB\-Wno\-decl\fR.
  169. .
  170. .TP
  171. .B \-Wdeclaration-after-statement
  172. Warn about declarations that are not at the start of a block.
  173. These declarations are permitted in C99 but not in C89.
  174. Sparse issues these warnings by default only when the C dialect is
  175. C89 (i.e. -ansi or -std=c89). To turn them off, use
  176. \fB\-Wno\-declaration\-after\-statement\fR.
  177. .
  178. .TP
  179. .B \-Wdefault\-bitfield\-sign
  180. Warn about any bitfield with no explicit signedness.
  181. Bitfields have no standard-specified default signedness. (C99 6.7.2) A
  182. bitfield without an explicit \fBsigned\fR or \fBunsigned\fR creates a
  183. portability problem for software that relies on the available range of values.
  184. To fix this, specify the bitfield type as \fBsigned\fR or \fBunsigned\fR
  185. explicitly.
  186. Sparse does not issue these warnings by default.
  187. .
  188. .TP
  189. .B \-Wdesignated\-init
  190. Warn about positional initialization of structs marked as requiring designated
  191. initializers.
  192. Sparse allows an attribute
  193. .BI __attribute__((designated_init))
  194. which marks a struct as requiring designated initializers. Sparse will warn
  195. about positional initialization of a struct variable or struct literal of a
  196. type that has this attribute.
  197. Requiring designated initializers for a particular struct type will insulate
  198. code using that struct type from changes to the layout of the type, avoiding
  199. the need to change initializers for that type unless they initialize a removed
  200. or incompatibly changed field.
  201. Common examples of this type of struct include collections of function pointers
  202. for the implementations of a class of related operations, for which the default
  203. NULL for an unmentioned field in a designated initializer will correctly
  204. indicate the absence of that operation.
  205. Sparse issues these warnings by default. To turn them off, use
  206. \fB\-Wno\-designated\-init\fR.
  207. .
  208. .TP
  209. .B \-Wdo\-while
  210. Warn about do-while loops that do not delimit the loop body with braces.
  211. Sparse does not issue these warnings by default.
  212. .
  213. .TP
  214. .B \-Wenum\-mismatch
  215. Warn about the use of an expression of an incorrect \fBenum\fR type when
  216. initializing another \fBenum\fR type, assigning to another \fBenum\fR type, or
  217. passing an argument to a function which expects another \fBenum\fR type.
  218. Sparse issues these warnings by default. To turn them off, use
  219. \fB\-Wno\-enum\-mismatch\fR.
  220. .
  221. .TP
  222. .B \-Wexternal\-function\-has\-definition
  223. Warn about function definitions that are declared with external linkage.
  224. Sparse issues these warnings by default. To turn them off, use
  225. \fB\-Wno\-external\-function\-has\-definition\fR.
  226. .
  227. .TP
  228. .B -Wflexible-array-array
  229. Warn about arrays of structures containing a flexible array.
  230. Sparse issues these warnings by default. To turn them off, use
  231. \fB-Wno-flexible-array-array\fR.
  232. .
  233. .TP
  234. .B -Wflexible-array-nested
  235. Warn about structures containing a flexible array being contained into
  236. another structure, union or array.
  237. Sparse does not issue these warnings by default.
  238. .
  239. .TP
  240. .B -Wflexible-array-sizeof
  241. Warn about using the sizeof operator on a structure containing a flexible array,
  242. possibly recursively.
  243. Sparse does not issue these warnings by default.
  244. .
  245. .TP
  246. .B -Wflexible-array-union
  247. Enable the warnings regarding flexible arrays and unions.
  248. To have any effect, at least one of \fB-Wflexible-array-array\fR,
  249. \fB-Wflexible-array-nested\fR or \fB-Wflexible-array-sizeof\fR must also
  250. be enabled.
  251. Sparse does issue these warnings by default.
  252. .
  253. .TP
  254. .B \-Winit\-cstring
  255. Warn about initialization of a char array with a too long constant C string.
  256. If the size of the char array and the length of the string are the same,
  257. there is no space for the last nul char of the string in the array:
  258. .nf
  259. char s[3] = "abc";
  260. .fi
  261. If the array is used as a byte array, not as C string, this
  262. warning is just noise. However, if the array is passed to functions
  263. dealing with C string like printf(%s) and strcmp, it may cause a
  264. trouble.
  265. Sparse does not issue these warnings by default.
  266. .
  267. .TP
  268. .B \-Wmemcpy\-max\-count
  269. Warn about call of \fBmemcpy()\fR, \fBmemset()\fR, \fBcopy_from_user()\fR, or
  270. \fBcopy_to_user()\fR with a large compile-time byte count.
  271. Sparse issues these warnings by default. To turn them off, use
  272. \fB\-Wno\-memcpy\-max\-count\fR.
  273. The limit can be changed with \fB\-fmemcpy\-max\-count=COUNT\fR,
  274. the default being \fB100000\fR.
  275. .
  276. .TP
  277. .B \-Wnewline\-eof
  278. Warn if the input file doesn't end with a newline.
  279. Sparse issues these warnings by default. To turn them off, use
  280. \fB\-Wno\-newline\-eof\fR.
  281. .
  282. .TP
  283. .B \-Wnon\-pointer\-null
  284. Warn about the use of 0 as a NULL pointer.
  285. 0 has integer type. NULL has pointer type.
  286. Sparse issues these warnings by default. To turn them off, use
  287. \fB\-Wno\-non\-pointer\-null\fR.
  288. .
  289. .TP
  290. .B \-Wold\-initializer
  291. Warn about the use of the pre-C99 GCC syntax for designated initializers.
  292. C99 provides a standard syntax for designated fields in \fBstruct\fR or
  293. \fBunion\fR initializers:
  294. .nf
  295. struct structname var = { .field = value };
  296. .fi
  297. GCC also has an old, non-standard syntax for designated initializers which
  298. predates C99:
  299. .nf
  300. struct structname var = { field: value };
  301. .fi
  302. Sparse will warn about the use of GCC's non-standard syntax for designated
  303. initializers. To fix this warning, convert designated initializers to use the
  304. standard C99 syntax.
  305. Sparse issues these warnings by default. To turn them off, use
  306. \fB\-Wno\-old\-initializer\fR.
  307. .
  308. .TP
  309. .B \-Wone\-bit\-signed\-bitfield
  310. Warn about any one-bit \fBsigned\fR bitfields.
  311. A one-bit \fBsigned\fR bitfield can only have the values 0 and -1, or with
  312. some compilers only 0; this results in unexpected behavior for programs which
  313. expected the ability to store 0 and 1.
  314. Sparse issues these warnings by default. To turn them off, use
  315. \fB\-Wno\-one\-bit\-signed\-bitfield\fR.
  316. .
  317. .TP
  318. .B \-Wparen\-string
  319. Warn about the use of a parenthesized string to initialize an array.
  320. Standard C syntax does not permit a parenthesized string as an array
  321. initializer. GCC allows this syntax as an extension. With
  322. \fB\-Wparen\-string\fR, Sparse will warn about this syntax.
  323. Sparse does not issue these warnings by default.
  324. .
  325. .TP
  326. .B -Wpast-deep-designator
  327. Warn when, in a initializer-list, a initializer with a deep (nested)
  328. designator is followed by a non-designated one.
  329. Sparse does not issue these warnings by default.
  330. .
  331. .TP
  332. .B \-Wpointer\-arith
  333. Warn about anything that depends on the \fBsizeof\fR a void or function type.
  334. C99 does not allow the \fBsizeof\fR operator to be applied to function types
  335. or to incomplete types such as void. GCC allows \fBsizeof\fR to be applied to
  336. these types as an extension and assigns these types a size of \fI1\fR. With
  337. \fB\-pointer\-arith\fR, Sparse will warn about pointer arithmetic on void
  338. or function pointers, as well as expressions which directly apply the
  339. \fBsizeof\fR operator to void or function types.
  340. Sparse does not issue these warnings by default.
  341. .
  342. .TP
  343. .B \-Wptr\-subtraction\-blows
  344. Warn when subtracting two pointers to a type with a non-power-of-two size.
  345. Subtracting two pointers to a given type gives a difference in terms of the
  346. number of items of that type. To generate this value, compilers will usually
  347. need to divide the difference by the size of the type, an potentially
  348. expensive operation for sizes other than powers of two.
  349. Code written using pointer subtraction can often use another approach instead,
  350. such as array indexing with an explicit array index variable, which may allow
  351. compilers to generate more efficient code.
  352. Sparse does not issue these warnings by default.
  353. .
  354. .TP
  355. .B \-Wreturn\-void
  356. Warn if a function with return type void returns a void expression.
  357. C99 permits this, and in some cases this allows for more generic code in
  358. macros that use typeof or take a type as a macro argument. However, some
  359. programs consider this poor style, and those programs can use
  360. \fB\-Wreturn\-void\fR to get warnings about it.
  361. Sparse does not issue these warnings by default.
  362. .
  363. .TP
  364. .B \-Wshadow
  365. Warn when declaring a symbol which shadows a declaration with the same name in
  366. an outer scope.
  367. Such declarations can lead to error-prone code.
  368. Sparse does not issue these warnings by default.
  369. .
  370. .TP
  371. .B \-Wshift-count-negative
  372. Warn if a shift count is negative.
  373. Sparse issues these warnings by default.
  374. .
  375. .TP
  376. .B \-Wshift-count-overflow
  377. Warn if a shift count is bigger than the operand's width.
  378. Sparse issues these warnings by default.
  379. .
  380. .TP
  381. .B \-Wsizeof-bool
  382. Warn when checking the sizeof a _Bool.
  383. C99 does not specify the size of a _Bool. GCC, by default, uses \fI1\fR.
  384. Sparse does not issue these warnings by default.
  385. .
  386. .TP
  387. .B \-Wtransparent\-union
  388. Warn about any declaration using the GCC extension
  389. \fB__attribute__((transparent_union))\fR.
  390. Sparse issues these warnings by default. To turn them off, use
  391. \fB\-Wno\-transparent\-union\fR.
  392. .
  393. .TP
  394. .B \-Wtypesign
  395. Warn when converting a pointer to an integer type into a pointer to an integer
  396. type with different signedness.
  397. Sparse does not issue these warnings by default.
  398. .
  399. .TP
  400. .B \-Wundef
  401. Warn about preprocessor conditionals that use the value of an undefined
  402. preprocessor symbol.
  403. Standard C (C99 6.10.1) permits using the value of an undefined preprocessor
  404. symbol in preprocessor conditionals, and specifies it has a value of 0.
  405. However, this behavior can lead to subtle errors.
  406. Sparse does not issue these warnings by default.
  407. .
  408. .TP
  409. .B \-Wuniversal\-initializer
  410. Do not suppress warnings caused by using '{\ 0\ }' instead of '{\ }' on
  411. aggregate types, ignoring its special status as universal initializer.
  412. The concerned warnings are, for example, those triggered by
  413. \fB\-Wdesignated\-init\fR or \fB\-Wnon\-pointer\-null\fR.
  414. Sparse does not issue these warnings by default, processing '{\ 0\ }'
  415. the same as '{\ }'.
  416. .
  417. .TP
  418. .B -Wunion-cast
  419. Warn on casts to union types.
  420. Sparse does not issues these warnings by default.
  421. .
  422. .SH MISC OPTIONS
  423. .TP
  424. .B \-\-arch=\fIARCH\fR
  425. Specify the target architecture.
  426. For architectures having both a 32-bit and a 64-bit variant (mips, powerpc,
  427. riscv and sparc) the architecture name can be suffixed with \fI32\fR or \fI64\fR.
  428. The default architecture and size is the one of the machine used to build Sparse.
  429. .
  430. .TP
  431. .B \-gcc-base-dir \fIdir\fR
  432. Look for compiler-provided system headers in \fIdir\fR/include/ and \fIdir\fR/include-fixed/.
  433. .
  434. .TP
  435. .B \-multiarch-dir \fIdir\fR
  436. Look for system headers in the multiarch subdirectory \fIdir\fR.
  437. The \fIdir\fR name would normally take the form of the target's
  438. normalized GNU triplet. (e.g. i386-linux-gnu).
  439. .
  440. .TP
  441. .B --os=\fIOS\fR
  442. Specify the target Operating System.
  443. This only makes a few differences with the predefined types.
  444. The accepted values are: linux, unix, freebsd, netbsd, opensd, sunos, darwin
  445. and cygwin.
  446. The default OS is the one of the machine used to build Sparse if it can be
  447. detected, otherwise some generic settings are used.
  448. .
  449. .SH DEBUG OPTIONS
  450. .TP
  451. .B \-fmem-report
  452. Report some statistics about memory allocation used by the tool.
  453. .
  454. .SH OTHER OPTIONS
  455. .TP
  456. .B \-fdiagnostic-prefix[=PREFIX]
  457. Prefix all diagnostics by the given PREFIX, followed by ": ".
  458. If no one is given "sparse" is used.
  459. The default is to not use a prefix at all.
  460. .
  461. .TP
  462. .B \-fmemcpy-max-count=COUNT
  463. Set the limit for the warnings given by \fB-Wmemcpy-max-count\fR.
  464. A COUNT of 'unlimited' or '0' will effectively disable the warning.
  465. The default limit is 100000.
  466. .
  467. .TP
  468. .B \-ftabstop=WIDTH
  469. Set the distance between tab stops. This helps sparse report correct
  470. column numbers in warnings or errors. If the value is less than 1 or
  471. greater than 100, the option is ignored. The default is 8.
  472. .
  473. .TP
  474. .B \-f[no-]unsigned-char, \-f[no-]signed-char
  475. Let plain 'char' be unsigned or signed.
  476. By default chars are signed.
  477. .
  478. .SH SEE ALSO
  479. .BR cgcc (1)
  480. .
  481. .SH HOMEPAGE
  482. https://sparse.docs.kernel.org
  483. .
  484. .SH MAILING LIST
  485. linux-sparse@vger.kernel.org
  486. .
  487. .SH CONTRIBUTING AND REPORTING BUGS
  488. Submission of patches and reporting of bugs, as well as discussions
  489. related to Sparse, should be done via the mailing list (linux-sparse@vger.kernel.org)
  490. where the development and maintenance is primarily done.
  491. You do not have to be subscribed to the list to send a message there.
  492. Bugs can also be reported and tracked via the Linux kernel's bugzilla:
  493. http://bugzilla.kernel.org/enter_bug.cgi?component=Sparse&product=Tools .
  494. .
  495. .SH DOCUMENTATION
  496. More documentation about Sparse can be found at
  497. https://sparse.docs.kernel.org
  498. .
  499. .SH AUTHORS
  500. Sparse was started by Linus Torvalds.
  501. The complete list of contributors can be find at
  502. https://www.openhub.net/p/sparse/contributors .
  503. Luc Van Oostenryck is Sparse's current maintainer.