sparse.txt 2.2 KB

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