blob: f97841478459037c5627814f345c30c3d3ed6d21 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001Copyright 2004 Linus Torvalds
2Copyright 2004 Pavel Machek <pavel@suse.cz>
3
4Using sparse for typechecking
5~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6
7"__bitwise" is a type attribute, so you have to do something like this:
8
9 typedef int __bitwise pm_request_t;
10
11 enum pm_request {
12 PM_SUSPEND = (__force pm_request_t) 1,
13 PM_RESUME = (__force pm_request_t) 2
14 };
15
16which makes PM_SUSPEND and PM_RESUME "bitwise" integers (the "__force" is
17there because sparse will complain about casting to/from a bitwise type,
18but in this case we really _do_ want to force the conversion). And because
19the enum values are all the same type, now "enum pm_request" will be that
20type too.
21
22And with gcc, all the __bitwise/__force stuff goes away, and it all ends
23up looking just like integers to gcc.
24
25Quite frankly, you don't need the enum there. The above all really just
26boils down to one special "int __bitwise" type.
27
28So the simpler way is to just do
29
30 typedef int __bitwise pm_request_t;
31
32 #define PM_SUSPEND ((__force pm_request_t) 1)
33 #define PM_RESUME ((__force pm_request_t) 2)
34
35and you now have all the infrastructure needed for strict typechecking.
36
37One small note: the constant integer "0" is special. You can use a
38constant zero as a bitwise integer type without sparse ever complaining.
39This is because "bitwise" (as the name implies) was designed for making
40sure that bitwise types don't get mixed up (little-endian vs big-endian
41vs cpu-endian vs whatever), and there the constant "0" really _is_
42special.
43
44Modify top-level Makefile to say
45
46CHECK = sparse -Wbitwise
47
48or you don't get any checking at all.
49
50
51Where to get sparse
52~~~~~~~~~~~~~~~~~~~
53
54With BK, you can just get it from
55
56 bk://sparse.bkbits.net/sparse
57
58and DaveJ has tar-balls at
59
60 http://www.codemonkey.org.uk/projects/bitkeeper/sparse/
61
62
63Once you have it, just do
64
65 make
66 make install
67
68as your regular user, and it will install sparse in your ~/bin directory.
69After that, doing a kernel make with "make C=1" will run sparse on all the
70C files that get recompiled, or with "make C=2" will run sparse on the
71files whether they need to be recompiled or not (ie the latter is fast way
72to check the whole tree if you have already built it).