sparse.txt 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. Chinese translated version of Documentation/dev-tools/sparse.rst
  2. If you have any comment or update to the content, please contact the
  3. original document maintainer directly. However, if you have a problem
  4. communicating in English you can also ask the Chinese maintainer for
  5. help. Contact the Chinese maintainer if this translation is outdated
  6. or if there is a problem with the translation.
  7. Chinese maintainer: Li Yang <leoyang.li@nxp.com>
  8. ---------------------------------------------------------------------
  9. Documentation/dev-tools/sparse.rst 的中文翻译
  10. 如果想评论或更新本文的内容,请直接联系原文档的维护者。如果你使用英文
  11. 交流有困难的话,也可以向中文版维护者求助。如果本翻译更新不及时或者翻
  12. 译存在问题,请联系中文版维护者。
  13. 中文版维护者: 李阳 Li Yang <leoyang.li@nxp.com>
  14. 中文版翻译者: 李阳 Li Yang <leoyang.li@nxp.com>
  15. 以下为正文
  16. ---------------------------------------------------------------------
  17. Copyright 2004 Linus Torvalds
  18. Copyright 2004 Pavel Machek <pavel@ucw.cz>
  19. Copyright 2006 Bob Copeland <me@bobcopeland.com>
  20. 使用 sparse 工具做类型检查
  21. ~~~~~~~~~~~~~~~~~~~~~~~~~~
  22. "__bitwise" 是一种类型属性,所以你应该这样使用它:
  23. typedef int __bitwise pm_request_t;
  24. enum pm_request {
  25. PM_SUSPEND = (__force pm_request_t) 1,
  26. PM_RESUME = (__force pm_request_t) 2
  27. };
  28. 这样会使 PM_SUSPEND 和 PM_RESUME 成为位方式(bitwise)整数(使用"__force"
  29. 是因为 sparse 会抱怨改变位方式的类型转换,但是这里我们确实需要强制进行转
  30. 换)。而且因为所有枚举值都使用了相同的类型,这里的"enum pm_request"也将
  31. 会使用那个类型做为底层实现。
  32. 而且使用 gcc 编译的时候,所有的 __bitwise/__force 都会消失,最后在 gcc
  33. 看来它们只不过是普通的整数。
  34. 坦白来说,你并不需要使用枚举类型。上面那些实际都可以浓缩成一个特殊的"int
  35. __bitwise"类型。
  36. 所以更简单的办法只要这样做:
  37. typedef int __bitwise pm_request_t;
  38. #define PM_SUSPEND ((__force pm_request_t) 1)
  39. #define PM_RESUME ((__force pm_request_t) 2)
  40. 现在你就有了严格的类型检查所需要的所有基础架构。
  41. 一个小提醒:常数整数"0"是特殊的。你可以直接把常数零当作位方式整数使用而
  42. 不用担心 sparse 会抱怨。这是因为"bitwise"(恰如其名)是用来确保不同位方
  43. 式类型不会被弄混(小尾模式,大尾模式,cpu尾模式,或者其他),对他们来说
  44. 常数"0"确实是特殊的。
  45. 获取 sparse 工具
  46. ~~~~~~~~~~~~~~~~
  47. 你可以从 Sparse 的主页获取最新的发布版本:
  48. http://www.kernel.org/pub/linux/kernel/people/josh/sparse/
  49. 或者,你也可以使用 git 克隆最新的 sparse 开发版本:
  50. git://git.kernel.org/pub/scm/linux/kernel/git/josh/sparse.git
  51. 一旦你下载了源码,只要以普通用户身份运行:
  52. make
  53. make install
  54. 它将会被自动安装到你的 ~/bin 目录下。
  55. 使用 sparse 工具
  56. ~~~~~~~~~~~~~~~~
  57. 用"make C=1"命令来编译内核,会对所有重新编译的 C 文件使用 sparse 工具。
  58. 或者使用"make C=2"命令,无论文件是否被重新编译都会对其使用 sparse 工具。
  59. 如果你已经编译了内核,用后一种方式可以很快地检查整个源码树。
  60. make 的可选变量 CHECKFLAGS 可以用来向 sparse 工具传递参数。编译系统会自
  61. 动向 sparse 工具传递 -Wbitwise 参数。