kernel-doc syntax¶
The format of a block comment is like this:
/**
* (data-type|DOC)? name(:)? (- short description)?
(* @x: (description of parameter/member x)?)*
(* a blank line)?
* (Description of function or data structure)?
(* a blank line)?
* (sub-section header:
* (sub-section description) )*
(*)?*/
(...)?
signifies optional structure and(...)*
signifies 0 or more structure elements
The name of the function, data-type respectively DOC is used as the section header. The names of section headers must be unique per source file.
functions¶
/**
* function_name(:)? (- short description)?
(* @parameterx: (description of parameter x)?)*
(* a blank line)?
* (Description of function)?
(* a blank line)?
* (sub-section header:
* (sub-section description) )*
(*)?*/
All description text can span multiple lines, although the function_name
&
its short description are traditionally on a single line. Description text may
also contain blank lines (i.e., lines that contain only a “*”).
So, the trivial example would be:
/**
* my_function
*/
If the Description: header tag is omitted, then there must be a blank line after the last parameter specification.:
/**
* my_function - does my stuff
* @my_arg: its mine damnit
*
* Does my stuff explained.
*/
or, could also use:
/**
* my_function - does my stuff
* @my_arg: its mine damnit
* Description:
* Does my stuff explained.
*/
You can also add additional sections. When documenting kernel functions you
should document the Context:
of the function, e.g. whether the functions can
be called form interrupts. Unlike other sections you can end it with an empty
line.
A non-void function should have a Return:
section describing the return
value(s). Example-sections should contain the string EXAMPLE
so that
they are marked appropriately in the output format.
1/**
2 * user_function() - function that can only be called in user context
3 * @a: some argument
4 * @...: ellipsis operator
5 *
6 * This function makes no sense, it's only a kernel-doc demonstration.
7 *
8 * Example:
9 * x = user_function(22);
10 *
11 * Return:
12 * Returns first argument
13 */
14int
15user_function(int a, ...)
16{
17 return a;
18}
Rendered example: user_function
macro¶
Documenting macros is similar to documenting functions.
1/**
2 * macro ADD - Function like macro to add two values
3 *
4 * @first: first value
5 * @second: second value
6 *
7 * Is replaced by a addition of @first and @second.
8 */
9#define ADD(first, second) (first) + (second)
Rendered example: macro ADD
structs, unions¶
Beside functions you can also write documentation for structs
,
unions
. Instead of the function name you must write the name of the
declaration; the struct
or union
must always precede the name. Nesting
of declarations is supported. Use the @argument
mechanism to document
members or constants.
Inside a struct
description, you can use the ‘private:’ and ‘public:’ comment
tags. Structure fields that are inside a ‘private:’ area are not listed in the
generated output documentation. The ‘private:’ and ‘public:’ tags must begin
immediately following a /*
comment marker. They may optionally include
comments between the :
and the ending */
marker.
1/**
2* struct my_struct - a struct with nested unions and structs
3* @arg1: first argument of anonymous union/anonymous struct
4* lorem ipsum ...
5* @arg2: second argument of anonymous union/anonymous struct
6* @arg1b: first argument of anonymous union/anonymous struct
7* @arg2b: second argument of anonymous union/anonymous struct
8* @arg3: third argument of anonymous union/anonymous struct
9* @arg4: fourth argument of anonymous union/anonymous struct
10* @bar: non-anonymous union
11* @bar.st1: struct st1 inside @bar
12* @bar.st1.arg1: first argument of struct st1 on union bar
13* @bar.st1.arg2: second argument of struct st1 on union bar
14* @bar.st2: struct st2 inside @bar
15* @bar.st2.arg1: first argument of struct st2 on union bar
16* @bar.st2.arg2: second argument of struct st2 on union bar
17* @bar.st3: struct st3 inside @bar
18* @bar.st3.arg2: second argument of struct st3 on union bar
19* @f1: nested function on anonimous union/struct
20* @bar.st2.f2: nested function on named union/struct
21*/
22struct my_struct {
23 /* Anonymous union/struct*/
24 union {
25 struct {
26 char arg1 : 1;
27 char arg2 : 3;
28 };
29 struct {
30 int arg1b;
31 int arg2b;
32 };
33 struct {
34 void *arg3;
35 int arg4;
36 int (*f1)(char foo, int bar);
37 };
38 };
39 union {
40 struct {
41 int arg1;
42 int arg2;
43 } st1;
44 struct {
45 void *arg1; /* bar.st3.arg1 is undocumented, cause a warning */
46 int arg2;
47 int (*f2)(char foo, int bar); /* bar.st3.fn2 is undocumented, cause a warning */
48 } st2, st3;
49 int (*f3)(char foo, int bar); /* f3 is undocumented, cause a warning */
50 } bar; /* bar is undocumented, cause a warning */
51
52 /* private: */
53 int undoc_privat; /* is undocumented but private, no warning */
54
55 /* public: */
56 enum {
57 FOO,
58 BAR,
59 } undoc_public; /* is undocumented, cause a warning */
60
61};
Rendered example: struct my_struct
All descriptions can be multi-line, except the short function description. For
really longs structs
, you can also describe arguments inside the body of the
struct
. There are two styles, single-line comments where both the opening
/**
and closing */
are on the same line, and multi-line comments where
they are each on a line of their own, like all other kernel-doc comments:
1/**
2 * struct my_long_struct - short description with &my_struct->a and &my_struct->b
3 * @foo: The Foo member.
4 *
5 * Longer description
6 */
7struct my_long_struct {
8 int foo;
9 /**
10 * @bar: The Bar member,
11 * lorem ipsum ..
12 */
13 int bar;
14 /**
15 * @baz: The Baz member,
16 * lorem ipsum ..
17 *
18 * Here, the member description may contain several paragraphs.
19 */
20 int baz;
21 union {
22 /** @foobar: Single line description. */
23 int foobar;
24 };
25 /** @bar2: Description for struct @bar2 inside @my_long_struct */
26 struct {
27 /**
28 * @bar2.barbar: Description for @barbar inside @my_long_struct.bar2
29 */
30 int barbar;
31 } bar2;
32};
Rendered example: struct my_long_struct
enums, typedefs¶
To write documentation for enums and typedefs, you must write the name of the
declaration; the enum
or typedef
must always precede the name.
1/**
2 * enum my_enum - log level
3 * @QUIET: logs nothing
4 * @INFO: logs info messages
5 * @WARN: logs warn and info messages
6 * @DEBUG: logs debug, warn and info messages
7 */
8
9enum my_enum {
10 QUIET,
11 INFO,
12 WARN,
13 DEBUG
14};
Rendered example: enum my_enum
1/**
2 * typedef my_typedef - useless typdef of int
3 *
4 */
5typedef int my_typedef;
Rendered example: typedef my_typedef
documentation blocks¶
To facilitate having source code and comments close together, you can include kernel-doc documentation blocks that are free-form comments instead of being kernel-doc for functions, structures, unions, enumerations, or typedefs. This could be used for something like a theory of operation for a driver or library code, for example.
This is done by using a DOC:
section keyword with a section title. A small
example:
1/**
2 * DOC: Theory of Operation
3 *
4 * The whizbang foobar is a dilly of a gizmo. It can do whatever you
5 * want it to do, at any time. It reads your mind. Here's how it works.
6 *
7 * foo bar splat
8 * -------------
9 *
10 * The only drawback to this gizmo is that it can sometimes damage hardware,
11 * software, or its subject(s).
12 *
13 * DOC: multiple DOC sections
14 *
15 * It's not recommended to place more than one "DOC:" section in the same
16 * comment block. To insert a new "DOC:" section, create a new comment block and
17 * to create a sub-section use the reST markup for headings, see documentation
18 * of function rst_mode()
19 */
Rendered example: Theory of Operation
highlight pattern¶
All kernel-doc markup is processed as described above, all descriptive text is further processed, scanning for the following special patterns, which are highlighted appropriately.
user_function()
: function@a
: name of a parameter&struct my_struct
: name of a structure (including the word struct)&union my_union
: name of a union&my_struct->a
or&my_struct.b
- member of a struct or union.&enum my_enum
: name of a enum&typedef my_typedef
: name of a typedef%CONST
: name of a constant.$ENVVAR
: environmental variable
The kernel-doc parser translates the pattern above to the corresponding reST markups (sphinx domains):
- :c:func:`user_function` : function
- ``a`` : name of a parameter
- :c:type:`struct my_struct <my_struct>` : name of a structure (including the word struct)
- :c:type:`union my_union <my_union>` : name of a union
- :c:type:`my_struct->a <my_struct>` or :c:type:`my_struct.b <my_struct>` - member of a struct or union.
- :c:type:`enum my_enum <my_enum>` : name of a enum
- :c:type:`typedef my_typedef <my_typedef>` : name of a typedef
- ``CONST`` : name of a constant.
- ``$ENVVAR`` : environmental variable
The sphinx-doc generator highlights these markups and tries to cross referencing to arbitrary locations (sphinx cross references). The result of a cross reference depends on the context of the document which includes the kernel-doc comment. You don’t have to use the highlight pattern, if you prefer pure reST, use the reST markup.
Since the prefixes $...
, &...
and @...
are used to markup the
highlight pattern, you have to escape them in other uses: \$...
, \&...
and \@...
.
Hint
The highlight pattern, are non regular reST markups. They are only available
within kernel-doc comments, helping C developers to write short and compact
documentation in source code comments. You can’t use them in plain reST files
(“.rst”). If you are editing “.rst” files (e.g. files under Documentation
)
please use the corresponding reST markups (sphinx domains).
Snippets¶
The kernel-doc Parser supports a comment-markup for snippets out of the source code. To start a region to snip insert:
/* parse-SNIP: <snippet-name> */
The snippet region stops with a new snippet region or at the next:
/* parse-SNAP: */
Jump to Snippets to see an example.