开发者社区> 优惠码领取> 正文

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

版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。

相关文章
怎样获得正确的LINUX用户的文档音乐视频等目录?
怎样获得正确的LINUX用户的文档音乐视频等目录?
20 0
使用python将word文档和pdf电子书进行格式互转(兼容Windows/Linux)
一些重要文档格式之间的互转在目前显得尤为重要,pdf作为通用格式在现在各个平台上兼容性是最好的,所以写python脚本将这些word文档批量转换pdf是最好的解决方案。 由于windows系统对于word文档有天然的兼容性优势,所以转换起来很简单,普遍上是通过comtypes模块。
128 0
【Unix/Linux】文档标准
【Unix/Linux】文档标准
42 0
【Unix/Linux 系统管理】本地文档维护
【Unix/Linux 系统管理】本地文档维护
64 0
linux第三课:目录文档操作命令(内含绝对/相对路径+1.pwd+2.cd+3.mkdir(创建目录)+4. rmdir(删除目录)+5. ls+6. cp+7.rm+8cat+9touch命令)
linux第三课:目录文档操作命令(内含绝对/相对路径+1.pwd+2.cd+3.mkdir(创建目录)+4. rmdir(删除目录)+5. ls+6. cp+7.rm+8cat+9touch命令)
46 0
Linux 系统下 Man 文档的用法和技巧| 学习笔记
快速学习 Linux 系统下 Man 文档的用法和技巧
188 0
Linux学习笔记 14(使用Vim文档编辑器进行文档编辑)
(1) 复制/etc/passwd文件到/tmp目录下(2) 用Vim打开它,当前处于什么模式(3) 将光标移动到行尾:$(4) 将光标移动到行首:0(5) 将光标移动到21行:21G(6) 删除5行:5dd(7) 撤销刚才的操作:u(8) 将光标移动到11行(9) 复制10行(10) 将复制的内容粘贴到文章末尾: G P(11) 强制保存退出(12) 使用Vim新建Hello.php(13) 进入编辑模式,输入源代码(14) 保存退出:ZZ或(15) 查看Hello.php文件(7) 撤销刚才的操作:u(8) 将光标移动到11行(9) 复制10行(10) 将复制的内保存退出:ZZ或wq()
181 0
5.9 Linux Vim批量注释和自定义注释
使用 Vim 编辑 Shell 脚本,在进行调试时,需要进行多行的注释,每次都要先切换到输入模式,在行首输入注释符”#”再退回命令模式,非常麻烦。
116 0
Linux CentOS7 httpd 服务配置注释
Linux CentOS7 httpd 服务配置注释 apache 配置注释如果没看懂可以去看看官方发布的文档 apache官方文档 conf/httpd.conf CopyServerRoot "/etc/httpd" # 指定服务配置根目录Listen 80 # 监听端口Include conf.
2925 0
Linux grep取消空行和注释行
cat /etc/zabbix/zabbix_server.conf|grep -Ev '#|^$'
1122 0
+关注
优惠码领取
阿里云优惠码阿里云推荐券bieryun.com
文章
问答
文章排行榜
最热
最新
相关电子书
更多
Decian GNU/Linux安全合规之路
立即下载
从 Linux 系统内核层面来解决实际问题的实战经验
立即下载
冬季实战营第二期:Linux操作系统实战入门
立即下载