Linux内核文档:如何写符合 kernel-doc 规范的注释

简介:

Linux内核文档:如何写符合 kernel-doc 规范的注释
简介#
Linux内核使用 Sphinx 实现把 Documentation 目录下的 reStructuredText 文件转换为非常漂亮的文档。文档既可以通过 make htmldocs转换成 HTML 格式,也可以通过 make pdfdocs 转换为 PDF 格式。 转换生成的文档存放于 Documentation/output 目录下。

Linux内核强大的文档功能,除了直接转换 .rst文档之外,还能从源码中汲取API说明,结构体说明等信息。当然要做到这样,源码的注释是有一定要求的。而这篇文档,就是介绍如何写符合 kernel-doc 格式要求的注释。

英文版原文,根据个人理解做的翻译,如果有翻译错误的地方,请告知。

注释概述#
符合 kernel-doc 的注释,都需要从 /* 开始,其后每一行内容都以 开头,最后是 */ 表示结束。例如:

Copy
/**

  • This is a sample of comment
    */

对于函数和类型的注释,必须放在函数和类型之前,以便于别人在修改代码的时候,可以顺手把注释也改了。对概述类型的注释,可以放到文件顶部的位置。

Linux内核有提供一个工具用于对 kernel-doc 的格式进行检查,例如:

Copy
$ scripts/kernel-doc -v -none drivers/foo/bar.c
当然,在编译的时候,如果添加以下的选项,也会检查文档的格式:

Copy
make W=n
函数文档#
规范的格式示例如下:

Copy
/**

  • function_name() - Brief description of function.
  • @arg1: Describe the first argument.
  • @arg2: Describe the second argument.
  • One can provide multiple line descriptions
  • for arguments.
    *
  • A longer description, with more discussion of the function function_name()
  • that might be useful to those using or modifying it. Begins with an
  • empty comment line, and may include additional embedded empty
  • comment lines.
    *
  • The longer description may have multiple paragraphs.
    *
  • Context: Describes whether the function can sleep, what locks it takes,
  • releases, or expects to be held. It can extend over multiple
  • lines.
  • Return: Describe the return value of function_name.
    *
  • The return value description can also have multiple paragraphs, and should
  • be placed at the end of the comment block.
    */

函数名后的函数功能简介可以跨越多行,以函数参数描述、返回值描述或其他描述结束。

函数参数#
函数参数的描述,必须直接且按顺序放在函数简介之后。需要注意的是,函数参数与函数简介、函数参数与函数参数之间不能有任何空行。

每个函数参数的描述可以跨行,但要注意的是,必须保持缩进对齐,例如

Copy

  • @argument: some long description
  • that continues on next lines # that 必须与 some对齐(避免排版乱套,补充说明)
    或者

Copy

  • @argument:
  • some long description
  • that continues on next lines
    函数参数#

如果出现不定数量个参数,可以这么表示:

Copy

  • @...: description
    函数上下文#

描述这个函数会在什么场景下调用,就是函数上下文字段所要做的。函数上下文字段用Context:表示,应该包含函数是否会休眠,是否会在中断中调用,以及它会持有、释放什么锁,和其他所有调用这个函数需要注意的东西。

例如:

Copy

  • Context: Any context.
  • Context: Any context. Takes and releases the RCU lock.
  • Context: Any context. Expects to be held by caller.
  • Context: Process context. May sleep if @gfp flags permit.
  • Context: Process context. Takes and releases .
  • Context: Softirq or process context. Takes and releases , BH-safe.
  • Context: Interrupt context.
    函数返回值#

函数返回值相关的描述应该放在Return:字段。

由于不能识别换行,因此如果你这样写:

Copy

  • Return:
  • 0 - OK
  • -EINVAL - invalid argument
  • -ENOMEM - out of memory
    效果只会是:

Copy
Return: 0 - OK -EINVAL - invalid argument -ENOMEM - out of memory
所以呢,如果你要换行,你需要使用 ReST List,例如:

Copy

  • Return:
    • 0 - OK to runtime suspend the device
    • -EBUSY - Device should not be runtime suspended
      另外,如果字段出现类似key:这样短语加冒号的形式,会被识别为其他的字段。这点需要注意。

struct 和 union 和 enum 类型的注释#
通常 struct ,union 和 enum 类型的注释说明是这样的:

Copy
/**

  • struct struct_name - Brief description.
  • @member1: Description of member1.
  • @member2: Description of member2.
  • One can provide multiple line descriptions
  • for members.
    *
  • Description of the structure.
    */

紧随名字后面的 Brief description 可以跨度几行,它以 @member 字段、空行或者*/表示结束。

成员#
struct,union 和 enum 的成员注释格式与函数参数的格式一致,他们都需要简短的描述且支持换行。

在 struct 和 union 里面也支持注释。支持 private 和 public 两种标签。private 标签的成员不会呈现到编译出来的文档上,类似与 C++ 上的 private 成员。

需要注意的是,private 和 public 标签必须以*标识开始且不能有缩进,以/标识结束。如果有简介,可以写在:与结束标识/之间,例如:

Copy
/**

  • struct my_struct - short description
  • @a: first member
  • @b: second member
  • @d: fourth member
    *
  • Longer description
    */

struct my_struct {

int a;
int b;

/ private: internal use only /

int c;

/ public: the next one is public /

int d;

};
嵌套struct和union#
如果出现嵌套定义 struct和union,可以参考下面的做法:

Copy
/**

  • struct nested_foobar - a struct with nested unions and structs
  • @memb1: first member of anonymous union/anonymous struct
  • @memb2: second member of anonymous union/anonymous struct
  • @memb3: third member of anonymous union/anonymous struct
  • @memb4: fourth member of anonymous union/anonymous struct
  • @bar: non-anonymous union
  • @bar.st1: struct st1 inside @bar
  • @bar.st2: struct st2 inside @bar
  • @bar.st1.memb1: first member of struct st1 on union bar
  • @bar.st1.memb2: second member of struct st1 on union bar
  • @bar.st2.memb1: first member of struct st2 on union bar
  • @bar.st2.memb2: second member of struct st2 on union bar
    */

struct nested_foobar {
/ Anonymous union/struct/
union {

struct {
  int memb1;
  int memb2;
}
struct {
  void *memb3;
  int memb4;
}

}
union {

struct {
  int memb1;
  int memb2;
} st1;
struct {
  void *memb1;
  int memb2;
} st2;

} bar;
};
有两点要注意的:

如果嵌套的struct或者union有命名,那么应该使用@foo.bar的形式,例如上例的@bar.st1
如果是匿名的,那么需要直接使用@bar的形式,例如上例的@memb1
细心的小伙伴可能发现,这与C语言上结构体的调用方式非常相似。

内联的成员描述#
成员的描述除了放在开头,还可以放在 struct 和 union 里面。支持单行或者多行,以/*开头,以/结束。例如:

Copy
/*

  • struct foo - Brief description.
  • @foo: The Foo member.
    */

struct foo {

  int foo;
  /**
   * @bar: The Bar member.
   */
  int bar;
  /**
   * @baz: The Baz member.
   *
   * Here, the member description may contain several paragraphs.
   */
  int baz;
  union {
          /** @foobar: Single line description. */
          int foobar;
  };
  /** @bar2: Description for struct @bar2 inside @foo */
  struct {
          /**
           * @bar2.barbar: Description for @barbar inside @foo.bar2
           */
          int barbar;
  } bar2;

};
typedef注释#
通常格式如下:

Copy
/**

  • typedef type_name - Brief description.
    *
  • Description of the type.
    */

如果是函数的类型定义,也可以这样:

Copy
/**

  • typedef type_name - Brief description.
  • @arg1: description of arg1
  • @arg2: description of arg2
    *
  • Description of the type.
    *
  • Context: Locking context.
  • Return: Meaning of the return value.
    */

typedef void (type_name)(struct v4l2_ctrl arg1, void *arg2);
高亮和交叉索引#
在各种说明字段中,可以用以下的形式做成索引。

funcname()

Copy
索引到函数
@parameter

Copy
索引到函数参数(只在本函数内索引,不会交叉到其他函数)
%CONST

Copy
索引到指定名字的上下文段说明(只在本函数内索引,不会交叉到其他函数)
literal

Copy
字面意义,让 kernel-doc 不解析,做纯字符串处理。常用于出现与 kernel-doc 或者 reStructuredText 关键字冲突的情况
$ENVVAR

Copy
环境变量(只在本函数内索引,不会交叉到其他函数)
&struct name

Copy
索引到其他结构体
&enum name

Copy
索引到其他的枚举型
&typedef name

Copy
索引到typedef定义的类型
&struct_name->member or &struct_name.member

Copy
索引到制定结构体成员
&name

Copy
泛型类型引用。不建议使用,请使用其他方法
概述说明#
为了便于将源代码和注释紧密结合在一起,可以包含自由格式注释的内核文档块,而不是函数、结构、联合、枚举或typedef的内核文档块。例如,这可以用于驱动程序或库代码的操作理论。

这是通过使用带有节标题的DOC:section关键字来完成的。

通用的格式如下:

Copy
/**

  • DOC: Theory of Operation
    *
  • The whizbang foobar is a dilly of a gizmo. It can do whatever you
  • want it to do, at any time. It reads your mind. Here's how it works.
    *
  • foo bar splat
    *
  • The only drawback to this gizmo is that is can sometimes damage
  • hardware, software, or its subject(s).
    */

DOC:后面的标题用作源文件中的标题,也用作提取文档注释的标识符。因此,标题在文件中必须是唯一的。

作者: 广漠飘羽

出处:https://www.cnblogs.com/gmpy/p/12529456.html

相关文章
|
8月前
|
安全 网络协议 Linux
深入理解Linux内核模块:加载机制、参数传递与实战开发
本文深入解析了Linux内核模块的加载机制、参数传递方式及实战开发技巧。内容涵盖模块基础概念、加载与卸载流程、生命周期管理、参数配置方法,并通过“Hello World”模块和字符设备驱动实例,带领读者逐步掌握模块开发技能。同时,介绍了调试手段、常见问题排查、开发规范及高级特性,如内核线程、模块间通信与性能优化策略。适合希望深入理解Linux内核机制、提升系统编程能力的技术人员阅读与实践。
772 1
|
8月前
|
Ubuntu Linux
Ubuntu 23.04 用上 Linux 6.2 内核,预计下放到 22.04 LTS 版本
Linux 6.2 带来了多项内容更新,修复了 AMD 锐龙处理器设备在启用 fTPM 后的运行卡顿问题,还增强了文件系统。
|
8月前
|
Ubuntu Linux
Ubuntu 23.10 现在由Linux内核6.3提供支持
如果你想在你的个人电脑上测试一下Ubuntu 23.10的最新开发快照,你可以从官方下载服务器下载最新的每日构建ISO。然而,请记住,这是一个预发布版本,所以不要在生产机器上使用或安装它。
|
8月前
|
传感器 监控 Ubuntu
10 月发布,Ubuntu 23.10 已升级到 Linux Kernel 6.3 内核
硬件方面,Linux 6.3 引入了在 HID 中引入了原生的 Steam Deck 控制器接口,允许罗技 G923 Xbox 版赛车方向盘在 Linux 上运行;改善 8BitDo Pro 2 有线控制器的行为;并为一系列华硕 Ryzen 主板添加传感器监控。
|
8月前
|
Ubuntu Linux
Ubuntu24.04LTS默认采用Linux 6.8内核,实验性版本可通过PPA获得
IT之家提醒,当下的 Ubuntu 23.10 也是一个“短期支持版本”,该版本将在今年 7 月终止支持,而今年 4 月推出的 Ubuntu 24.04 LTS 长期支持版本将获得 5 年的更新支持。
|
8月前
|
监控 Ubuntu Linux
什么Linux,Linux内核及Linux操作系统
上面只是简单的介绍了一下Linux操作系统的几个核心组件,其实Linux的整体架构要复杂的多。单纯从Linux内核的角度,它要管理CPU、内存、网卡、硬盘和输入输出等设备,因此内核本身分为进程调度,内存管理,虚拟文件系统,网络接口等4个核心子系统。
959 0
|
8月前
|
Web App开发 缓存 Rust
|
8月前
|
Ubuntu 安全 Linux
Ubuntu 发行版更新 Linux 内核,修复 17 个安全漏洞
本地攻击者可以利用上述漏洞,攻击 Ubuntu 22.10、Ubuntu 22.04、Ubuntu 20.04 LTS 发行版,导致拒绝服务(系统崩溃)或执行任意代码。
|
8月前
|
Ubuntu 机器人 物联网
Linux Ubuntu 22.04 LTS 测试版实时内核已可申请
请注意,在启用实时内核后您需要手动配置 grub 以恢复到原始内核。更多内容请参考: