92 lines · plain
1Chinese translated version of Documentation/dev-tools/sparse.rst2 3If you have any comment or update to the content, please contact the4original document maintainer directly. However, if you have a problem5communicating in English you can also ask the Chinese maintainer for6help. Contact the Chinese maintainer if this translation is outdated7or if there is a problem with the translation.8 9Traditional Chinese maintainer: Hu Haowen <2023002089@link.tyut.edu.cn>10-------------------------------------------------------------------------11Documentation/dev-tools/sparse.rst 的繁體中文翻譯12 13如果想評論或更新本文的內容,請直接聯繫原文檔的維護者。如果你使用英文14交流有困難的話,也可以向繁體中文版維護者求助。如果本翻譯更新不及時或15者翻譯存在問題,請聯繫繁體中文版維護者。16 17繁體中文版維護者: 胡皓文 Hu Haowen <2023002089@link.tyut.edu.cn>18繁體中文版翻譯者: 胡皓文 Hu Haowen <2023002089@link.tyut.edu.cn>19 20以下爲正文21-------------------------------------------------------------------------22 23Copyright 2004 Linus Torvalds24Copyright 2004 Pavel Machek <pavel@ucw.cz>25Copyright 2006 Bob Copeland <me@bobcopeland.com>26 27使用 sparse 工具做類型檢查28~~~~~~~~~~~~~~~~~~~~~~~~~~29 30"__bitwise" 是一種類型屬性,所以你應該這樣使用它::31 32 typedef int __bitwise pm_request_t;33 34 enum pm_request {35 PM_SUSPEND = (__force pm_request_t) 1,36 PM_RESUME = (__force pm_request_t) 237 };38 39這樣會使 PM_SUSPEND 和 PM_RESUME 成爲位方式(bitwise)整數(使用"__force"40是因爲 sparse 會抱怨改變位方式的類型轉換,但是這裡我們確實需要強制進行轉41換)。而且因爲所有枚舉值都使用了相同的類型,這裡的"enum pm_request"也將42會使用那個類型做爲底層實現。43 44而且使用 gcc 編譯的時候,所有的 __bitwise/__force 都會消失,最後在 gcc45看來它們只不過是普通的整數。46 47坦白來說,你並不需要使用枚舉類型。上面那些實際都可以濃縮成一個特殊的"int48__bitwise"類型。49 50所以更簡單的辦法只要這樣做::51 52 typedef int __bitwise pm_request_t;53 54 #define PM_SUSPEND ((__force pm_request_t) 1)55 #define PM_RESUME ((__force pm_request_t) 2)56 57現在你就有了嚴格的類型檢查所需要的所有基礎架構。58 59一個小提醒:常數整數"0"是特殊的。你可以直接把常數零當作位方式整數使用而60不用擔心 sparse 會抱怨。這是因爲"bitwise"(恰如其名)是用來確保不同位方61式類型不會被弄混(小尾模式,大尾模式,cpu尾模式,或者其他),對他們來說62常數"0"確實是特殊的。63 64獲取 sparse 工具65~~~~~~~~~~~~~~~~66 67你可以從 Sparse 的主頁獲取最新的發布版本:68 69 https://www.kernel.org/pub/software/devel/sparse/dist/70 71或者,你也可以使用 git 克隆最新的 sparse 開發版本:72 73 git://git.kernel.org/pub/scm/devel/sparse/sparse.git74 75一旦你下載了源碼,只要以普通用戶身份運行:76 77 make78 make install79 80它將會被自動安裝到你的 ~/bin 目錄下。81 82使用 sparse 工具83~~~~~~~~~~~~~~~~84 85用"make C=1"命令來編譯內核,會對所有重新編譯的 C 文件使用 sparse 工具。86或者使用"make C=2"命令,無論文件是否被重新編譯都會對其使用 sparse 工具。87如果你已經編譯了內核,用後一種方式可以很快地檢查整個源碼樹。88 89make 的可選變量 CHECKFLAGS 可以用來向 sparse 工具傳遞參數。編譯系統會自90動向 sparse 工具傳遞 -Wbitwise 參數。91 92