Documentation/translations/zh_CN/dev-tools/sparse.rst
Source file repositories/reference/linux-study-clean/Documentation/translations/zh_CN/dev-tools/sparse.rst
File Facts
- System
- Linux kernel
- Corpus path
Documentation/translations/zh_CN/dev-tools/sparse.rst- Extension
.rst- Size
- 3945 bytes
- Lines
- 110
- 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.
- Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.
Dependency Surface
- No C-style include directives detected by the generator.
Detected Declarations
enum pm_request
Annotated Snippet
Copyright 2004 Linus Torvalds
Copyright 2004 Pavel Machek <pavel@ucw.cz>
Copyright 2006 Bob Copeland <me@bobcopeland.com>
.. include:: ../disclaimer-zh_CN.rst
:Original: Documentation/dev-tools/sparse.rst
:翻译:
Li Yang <leoyang.li@nxp.com>
:校译:
司延腾 Yanteng Si <siyanteng@loongson.cn>
.. _cn_sparse:
Sparse
======
Sparse是一个C程序的语义检查器;它可以用来发现内核代码的一些潜在问题。 关
于sparse的概述,请参见https://lwn.net/Articles/689907/;本文档包含
一些针对内核的sparse信息。
关于sparse的更多信息,主要是关于它的内部结构,可以在它的官方网页上找到:
https://sparse.docs.kernel.org。
使用 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进行锁检查
--------------------
下面的宏对于 gcc 来说是未定义的,在 sparse 运行时定义,以使用sparse的“上下文”
跟踪功能,应用于锁定。 这些注释告诉 sparse 什么时候有锁,以及注释的函数的进入和
退出。
Annotation
- Detected declarations: `enum pm_request`.
- Atlas domain: Support Tooling And Documentation / Documentation.
- Implementation status: atlas-only.
Implementation Notes
- This generated page is the file-by-file coverage layer; curated subsystem chapters should link here when they synthesize a multi-file control flow.
- Core OS pages should be promoted from atlas-only to deep-reviewed when they explain data structures, invariants, locking, lifecycle, and C implementation snippets.
- Driver-family pages are intentionally pattern-oriented unless they are part of the selected PCIe/NVMe representative device path.