18c2ecf20Sopenharmony_ciChinese translated version of Documentation/dev-tools/sparse.rst 28c2ecf20Sopenharmony_ci 38c2ecf20Sopenharmony_ciIf you have any comment or update to the content, please contact the 48c2ecf20Sopenharmony_cioriginal document maintainer directly. However, if you have a problem 58c2ecf20Sopenharmony_cicommunicating in English you can also ask the Chinese maintainer for 68c2ecf20Sopenharmony_cihelp. Contact the Chinese maintainer if this translation is outdated 78c2ecf20Sopenharmony_cior if there is a problem with the translation. 88c2ecf20Sopenharmony_ci 98c2ecf20Sopenharmony_ciChinese maintainer: Li Yang <leoyang.li@nxp.com> 108c2ecf20Sopenharmony_ci--------------------------------------------------------------------- 118c2ecf20Sopenharmony_ciDocumentation/dev-tools/sparse.rst 的中文翻译 128c2ecf20Sopenharmony_ci 138c2ecf20Sopenharmony_ci如果想评论或更新本文的内容,请直接联系原文档的维护者。如果你使用英文 148c2ecf20Sopenharmony_ci交流有困难的话,也可以向中文版维护者求助。如果本翻译更新不及时或者翻 158c2ecf20Sopenharmony_ci译存在问题,请联系中文版维护者。 168c2ecf20Sopenharmony_ci 178c2ecf20Sopenharmony_ci中文版维护者: 李阳 Li Yang <leoyang.li@nxp.com> 188c2ecf20Sopenharmony_ci中文版翻译者: 李阳 Li Yang <leoyang.li@nxp.com> 198c2ecf20Sopenharmony_ci 208c2ecf20Sopenharmony_ci 218c2ecf20Sopenharmony_ci以下为正文 228c2ecf20Sopenharmony_ci--------------------------------------------------------------------- 238c2ecf20Sopenharmony_ci 248c2ecf20Sopenharmony_ciCopyright 2004 Linus Torvalds 258c2ecf20Sopenharmony_ciCopyright 2004 Pavel Machek <pavel@ucw.cz> 268c2ecf20Sopenharmony_ciCopyright 2006 Bob Copeland <me@bobcopeland.com> 278c2ecf20Sopenharmony_ci 288c2ecf20Sopenharmony_ci使用 sparse 工具做类型检查 298c2ecf20Sopenharmony_ci~~~~~~~~~~~~~~~~~~~~~~~~~~ 308c2ecf20Sopenharmony_ci 318c2ecf20Sopenharmony_ci"__bitwise" 是一种类型属性,所以你应该这样使用它: 328c2ecf20Sopenharmony_ci 338c2ecf20Sopenharmony_ci typedef int __bitwise pm_request_t; 348c2ecf20Sopenharmony_ci 358c2ecf20Sopenharmony_ci enum pm_request { 368c2ecf20Sopenharmony_ci PM_SUSPEND = (__force pm_request_t) 1, 378c2ecf20Sopenharmony_ci PM_RESUME = (__force pm_request_t) 2 388c2ecf20Sopenharmony_ci }; 398c2ecf20Sopenharmony_ci 408c2ecf20Sopenharmony_ci这样会使 PM_SUSPEND 和 PM_RESUME 成为位方式(bitwise)整数(使用"__force" 418c2ecf20Sopenharmony_ci是因为 sparse 会抱怨改变位方式的类型转换,但是这里我们确实需要强制进行转 428c2ecf20Sopenharmony_ci换)。而且因为所有枚举值都使用了相同的类型,这里的"enum pm_request"也将 438c2ecf20Sopenharmony_ci会使用那个类型做为底层实现。 448c2ecf20Sopenharmony_ci 458c2ecf20Sopenharmony_ci而且使用 gcc 编译的时候,所有的 __bitwise/__force 都会消失,最后在 gcc 468c2ecf20Sopenharmony_ci看来它们只不过是普通的整数。 478c2ecf20Sopenharmony_ci 488c2ecf20Sopenharmony_ci坦白来说,你并不需要使用枚举类型。上面那些实际都可以浓缩成一个特殊的"int 498c2ecf20Sopenharmony_ci__bitwise"类型。 508c2ecf20Sopenharmony_ci 518c2ecf20Sopenharmony_ci所以更简单的办法只要这样做: 528c2ecf20Sopenharmony_ci 538c2ecf20Sopenharmony_ci typedef int __bitwise pm_request_t; 548c2ecf20Sopenharmony_ci 558c2ecf20Sopenharmony_ci #define PM_SUSPEND ((__force pm_request_t) 1) 568c2ecf20Sopenharmony_ci #define PM_RESUME ((__force pm_request_t) 2) 578c2ecf20Sopenharmony_ci 588c2ecf20Sopenharmony_ci现在你就有了严格的类型检查所需要的所有基础架构。 598c2ecf20Sopenharmony_ci 608c2ecf20Sopenharmony_ci一个小提醒:常数整数"0"是特殊的。你可以直接把常数零当作位方式整数使用而 618c2ecf20Sopenharmony_ci不用担心 sparse 会抱怨。这是因为"bitwise"(恰如其名)是用来确保不同位方 628c2ecf20Sopenharmony_ci式类型不会被弄混(小尾模式,大尾模式,cpu尾模式,或者其他),对他们来说 638c2ecf20Sopenharmony_ci常数"0"确实是特殊的。 648c2ecf20Sopenharmony_ci 658c2ecf20Sopenharmony_ci获取 sparse 工具 668c2ecf20Sopenharmony_ci~~~~~~~~~~~~~~~~ 678c2ecf20Sopenharmony_ci 688c2ecf20Sopenharmony_ci你可以从 Sparse 的主页获取最新的发布版本: 698c2ecf20Sopenharmony_ci 708c2ecf20Sopenharmony_ci http://www.kernel.org/pub/linux/kernel/people/josh/sparse/ 718c2ecf20Sopenharmony_ci 728c2ecf20Sopenharmony_ci或者,你也可以使用 git 克隆最新的 sparse 开发版本: 738c2ecf20Sopenharmony_ci 748c2ecf20Sopenharmony_ci git://git.kernel.org/pub/scm/linux/kernel/git/josh/sparse.git 758c2ecf20Sopenharmony_ci 768c2ecf20Sopenharmony_ci一旦你下载了源码,只要以普通用户身份运行: 778c2ecf20Sopenharmony_ci 788c2ecf20Sopenharmony_ci make 798c2ecf20Sopenharmony_ci make install 808c2ecf20Sopenharmony_ci 818c2ecf20Sopenharmony_ci它将会被自动安装到你的 ~/bin 目录下。 828c2ecf20Sopenharmony_ci 838c2ecf20Sopenharmony_ci使用 sparse 工具 848c2ecf20Sopenharmony_ci~~~~~~~~~~~~~~~~ 858c2ecf20Sopenharmony_ci 868c2ecf20Sopenharmony_ci用"make C=1"命令来编译内核,会对所有重新编译的 C 文件使用 sparse 工具。 878c2ecf20Sopenharmony_ci或者使用"make C=2"命令,无论文件是否被重新编译都会对其使用 sparse 工具。 888c2ecf20Sopenharmony_ci如果你已经编译了内核,用后一种方式可以很快地检查整个源码树。 898c2ecf20Sopenharmony_ci 908c2ecf20Sopenharmony_cimake 的可选变量 CHECKFLAGS 可以用来向 sparse 工具传递参数。编译系统会自 918c2ecf20Sopenharmony_ci动向 sparse 工具传递 -Wbitwise 参数。 92