Documentation/translations/zh_TW/dev-tools/sparse.rst

Source file repositories/reference/linux-study-clean/Documentation/translations/zh_TW/dev-tools/sparse.rst

File Facts

System
Linux kernel
Corpus path
Documentation/translations/zh_TW/dev-tools/sparse.rst
Extension
.rst
Size
3657 bytes
Lines
92
Domain
Support Tooling And Documentation
Bucket
Documentation
Inferred role
Support Tooling And Documentation: documentation
Status
atlas-only

Why This File Exists

Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.

Dependency Surface

Detected Declarations

Annotated Snippet

Chinese translated version of Documentation/dev-tools/sparse.rst

If you have any comment or update to the content, please contact the
original document maintainer directly.  However, if you have a problem
communicating in English you can also ask the Chinese maintainer for
help.  Contact the Chinese maintainer if this translation is outdated
or if there is a problem with the translation.

Traditional Chinese maintainer: Hu Haowen <2023002089@link.tyut.edu.cn>
-------------------------------------------------------------------------
Documentation/dev-tools/sparse.rst 的繁體中文翻譯

如果想評論或更新本文的內容,請直接聯繫原文檔的維護者。如果你使用英文
交流有困難的話,也可以向繁體中文版維護者求助。如果本翻譯更新不及時或
者翻譯存在問題,請聯繫繁體中文版維護者。

繁體中文版維護者: 胡皓文 Hu Haowen <2023002089@link.tyut.edu.cn>
繁體中文版翻譯者: 胡皓文 Hu Haowen <2023002089@link.tyut.edu.cn>

以下爲正文
-------------------------------------------------------------------------

Copyright 2004 Linus Torvalds
Copyright 2004 Pavel Machek <pavel@ucw.cz>
Copyright 2006 Bob Copeland <me@bobcopeland.com>

使用 sparse 工具做類型檢查
~~~~~~~~~~~~~~~~~~~~~~~~~~

"__bitwise" 是一種類型屬性,所以你應該這樣使用它::

        typedef int __bitwise pm_request_t;

        enum pm_request {
                PM_SUSPEND = (__force pm_request_t) 1,
                PM_RESUME = (__force pm_request_t) 2
        };

這樣會使 PM_SUSPEND 和 PM_RESUME 成爲位方式(bitwise)整數(使用"__force"
是因爲 sparse 會抱怨改變位方式的類型轉換,但是這裡我們確實需要強制進行轉
換)。而且因爲所有枚舉值都使用了相同的類型,這裡的"enum pm_request"也將
會使用那個類型做爲底層實現。

而且使用 gcc 編譯的時候,所有的 __bitwise/__force 都會消失,最後在 gcc
看來它們只不過是普通的整數。

坦白來說,你並不需要使用枚舉類型。上面那些實際都可以濃縮成一個特殊的"int
__bitwise"類型。

所以更簡單的辦法只要這樣做::

	typedef int __bitwise pm_request_t;

	#define PM_SUSPEND ((__force pm_request_t) 1)
	#define PM_RESUME ((__force pm_request_t) 2)

現在你就有了嚴格的類型檢查所需要的所有基礎架構。

一個小提醒:常數整數"0"是特殊的。你可以直接把常數零當作位方式整數使用而
不用擔心 sparse 會抱怨。這是因爲"bitwise"(恰如其名)是用來確保不同位方
式類型不會被弄混(小尾模式,大尾模式,cpu尾模式,或者其他),對他們來說
常數"0"確實是特殊的。

獲取 sparse 工具
~~~~~~~~~~~~~~~~

你可以從 Sparse 的主頁獲取最新的發布版本:

	https://www.kernel.org/pub/software/devel/sparse/dist/

Annotation

Implementation Notes