blob: 78f69cdc9b3fbcec6f32beb179eb4c8732883d5a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001kernel-doc nano-HOWTO
2=====================
3
Paul Jackson0842b242008-06-05 22:46:45 -07004How to format kernel-doc comments
5---------------------------------
6
7In order to provide embedded, 'C' friendly, easy to maintain,
8but consistent and extractable documentation of the functions and
9data structures in the Linux kernel, the Linux kernel has adopted
10a consistent style for documenting functions and their parameters,
11and structures and their members.
12
13The format for this documentation is called the kernel-doc format.
14It is documented in this Documentation/kernel-doc-nano-HOWTO.txt file.
15
16This style embeds the documentation within the source files, using
17a few simple conventions. The scripts/kernel-doc perl script, some
18SGML templates in Documentation/DocBook, and other tools understand
19these conventions, and are used to extract this embedded documentation
20into various documents.
21
22In order to provide good documentation of kernel functions and data
23structures, please use the following conventions to format your
24kernel-doc comments in Linux kernel source.
25
26We definitely need kernel-doc formatted documentation for functions
27that are exported to loadable modules using EXPORT_SYMBOL.
28
29We also look to provide kernel-doc formatted documentation for
30functions externally visible to other kernel files (not marked
31"static").
32
33We also recommend providing kernel-doc formatted documentation
34for private (file "static") routines, for consistency of kernel
35source code layout. But this is lower priority and at the
36discretion of the MAINTAINER of that kernel source file.
37
38Data structures visible in kernel include files should also be
39documented using kernel-doc formatted comments.
40
41The opening comment mark "/**" is reserved for kernel-doc comments.
42Only comments so marked will be considered by the kernel-doc scripts,
43and any comment so marked must be in kernel-doc format. Do not use
44"/**" to be begin a comment block unless the comment block contains
45kernel-doc formatted comments. The closing comment marker for
Randy Dunlapf40b45a2009-02-11 13:04:31 -080046kernel-doc comments can be either "*/" or "**/", but "*/" is
47preferred in the Linux kernel tree.
Paul Jackson0842b242008-06-05 22:46:45 -070048
49Kernel-doc comments should be placed just before the function
50or data structure being described.
51
52Example kernel-doc function comment:
53
54/**
55 * foobar() - short function description of foobar
56 * @arg1: Describe the first argument to foobar.
57 * @arg2: Describe the second argument to foobar.
58 * One can provide multiple line descriptions
59 * for arguments.
60 *
61 * A longer description, with more discussion of the function foobar()
62 * that might be useful to those using or modifying it. Begins with
63 * empty comment line, and may include additional embedded empty
64 * comment lines.
65 *
66 * The longer description can have multiple paragraphs.
Yacine Belkadie65fe5a2012-11-26 22:21:23 +010067 *
68 * Return: Describe the return value of foobar.
Randy Dunlapf40b45a2009-02-11 13:04:31 -080069 */
Paul Jackson0842b242008-06-05 22:46:45 -070070
Johannes Weiner64231332009-09-17 19:26:53 -070071The short description following the subject can span multiple lines
72and ends with an @argument description, an empty line or the end of
73the comment block.
Paul Jackson0842b242008-06-05 22:46:45 -070074
75The @argument descriptions must begin on the very next line following
76this opening short function description line, with no intervening
77empty comment lines.
78
Randy Dunlapd78dd072009-01-06 14:42:40 -080079If a function parameter is "..." (varargs), it should be listed in
80kernel-doc notation as:
81 * @...: description
82
Yacine Belkadie65fe5a2012-11-26 22:21:23 +010083The return value, if any, should be described in a dedicated section
84named "Return".
Randy Dunlapd78dd072009-01-06 14:42:40 -080085
Paul Jackson0842b242008-06-05 22:46:45 -070086Example kernel-doc data structure comment.
87
88/**
89 * struct blah - the basic blah structure
90 * @mem1: describe the first member of struct blah
91 * @mem2: describe the second member of struct blah,
92 * perhaps with more lines and words.
93 *
94 * Longer description of this structure.
Randy Dunlapf40b45a2009-02-11 13:04:31 -080095 */
Paul Jackson0842b242008-06-05 22:46:45 -070096
97The kernel-doc function comments describe each parameter to the
98function, in order, with the @name lines.
99
100The kernel-doc data structure comments describe each structure member
101in the data structure, with the @name lines.
102
103The longer description formatting is "reflowed", losing your line
104breaks. So presenting carefully formatted lists within these
105descriptions won't work so well; derived documentation will lose
106the formatting.
107
108See the section below "How to add extractable documentation to your
109source files" for more details and notes on how to format kernel-doc
110comments.
111
112Components of the kernel-doc system
113-----------------------------------
114
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115Many places in the source tree have extractable documentation in the
116form of block comments above functions. The components of this system
117are:
118
119- scripts/kernel-doc
120
121 This is a perl script that hunts for the block comments and can mark
122 them up directly into DocBook, man, text, and HTML. (No, not
123 texinfo.)
124
125- Documentation/DocBook/*.tmpl
126
127 These are SGML template files, which are normal SGML files with
128 special place-holders for where the extracted documentation should
129 go.
130
Peter Hueweacc068d2015-07-13 22:02:48 +0200131- scripts/docproc.c
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
133 This is a program for converting SGML template files into SGML
134 files. When a file is referenced it is searched for symbols
135 exported (EXPORT_SYMBOL), to be able to distinguish between internal
136 and external functions.
137 It invokes kernel-doc, giving it the list of functions that
138 are to be documented.
139 Additionally it is used to scan the SGML template files to locate
140 all the files referenced herein. This is used to generate dependency
141 information as used by make.
142
143- Makefile
144
Robert P. J. Day01462c22013-05-02 11:27:03 -0400145 The targets 'xmldocs', 'psdocs', 'pdfdocs', and 'htmldocs' are used
146 to build XML DocBook files, PostScript files, PDF files, and html files
147 in Documentation/DocBook. The older target 'sgmldocs' is equivalent
148 to 'xmldocs'.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
150- Documentation/DocBook/Makefile
151
152 This is where C files are associated with SGML templates.
153
154
155How to extract the documentation
156--------------------------------
157
158If you just want to read the ready-made books on the various
159subsystems (see Documentation/DocBook/*.tmpl), just type 'make
Randy Dunlapd28bee02006-02-01 03:06:57 -0800160psdocs', or 'make pdfdocs', or 'make htmldocs', depending on your
161preference. If you would rather read a different format, you can type
Robert P. J. Day01462c22013-05-02 11:27:03 -0400162'make xmldocs' and then use DocBook tools to convert
163Documentation/DocBook/*.xml to a format of your choice (for example,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164'db2html ...' if 'make htmldocs' was not defined).
165
166If you want to see man pages instead, you can do this:
167
168$ cd linux
169$ scripts/kernel-doc -man $(find -name '*.c') | split-man.pl /tmp/man
170$ scripts/kernel-doc -man $(find -name '*.h') | split-man.pl /tmp/man
171
172Here is split-man.pl:
173
174-->
175#!/usr/bin/perl
176
177if ($#ARGV < 0) {
178 die "where do I put the results?\n";
179}
180
181mkdir $ARGV[0],0777;
182$state = 0;
183while (<STDIN>) {
Kevin Diggs65eb3dc2008-08-26 10:26:54 +0200184 if (/^\.TH \"[^\"]*\" 9 \"([^\"]*)\"/) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 if ($state == 1) { close OUT }
186 $state = 1;
Kevin Diggs65eb3dc2008-08-26 10:26:54 +0200187 $fn = "$ARGV[0]/$1.9";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 print STDERR "Creating $fn\n";
189 open OUT, ">$fn" or die "can't open $fn: $!\n";
190 print OUT $_;
191 } elsif ($state != 0) {
192 print OUT $_;
193 }
194}
195
196close OUT;
197<--
198
199If you just want to view the documentation for one function in one
200file, you can do this:
201
202$ scripts/kernel-doc -man -function fn file | nroff -man | less
203
204or this:
205
206$ scripts/kernel-doc -text -function fn file
207
208
209How to add extractable documentation to your source files
210---------------------------------------------------------
211
212The format of the block comment is like this:
213
214/**
215 * function_name(:)? (- short description)?
Randy Dunlap891dcd22007-02-10 01:45:53 -0800216(* @parameterx(space)*: (description of parameter x)?)*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217(* a blank line)?
218 * (Description:)? (Description of function)?
219 * (section header: (section description)? )*
220(*)?*/
221
Randy Dunlapd2b34e22010-01-08 14:43:09 -0800222All "description" text can span multiple lines, although the
223function_name & its short description are traditionally on a single line.
224Description text may also contain blank lines (i.e., lines that contain
225only a "*").
226
227"section header:" names must be unique per function (or struct,
228union, typedef, enum).
Robert P. J. Day262086c2007-02-10 01:45:58 -0800229
Yacine Belkadie65fe5a2012-11-26 22:21:23 +0100230Use the section header "Return" for sections describing the return value
231of a function.
232
Robert P. J. Day262086c2007-02-10 01:45:58 -0800233Avoid putting a spurious blank line after the function name, or else the
234description will be repeated!
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
236All descriptive text is further processed, scanning for the following special
237patterns, which are highlighted appropriately.
238
239'funcname()' - function
240'$ENVVAR' - environment variable
241'&struct_name' - name of a structure (up to two words including 'struct')
242'@parameter' - name of a parameter
243'%CONST' - name of a constant.
244
Robert P. J. Day262086c2007-02-10 01:45:58 -0800245NOTE 1: The multi-line descriptive text you provide does *not* recognize
246line breaks, so if you try to format some text nicely, as in:
247
Yacine Belkadie65fe5a2012-11-26 22:21:23 +0100248 Return:
Robert P. J. Day262086c2007-02-10 01:45:58 -0800249 0 - cool
250 1 - invalid arg
251 2 - out of memory
252
253this will all run together and produce:
254
Yacine Belkadie65fe5a2012-11-26 22:21:23 +0100255 Return: 0 - cool 1 - invalid arg 2 - out of memory
Robert P. J. Day262086c2007-02-10 01:45:58 -0800256
257NOTE 2: If the descriptive text you provide has lines that begin with
258some phrase followed by a colon, each of those phrases will be taken as
259a new section heading, which means you should similarly try to avoid text
260like:
261
Yacine Belkadie65fe5a2012-11-26 22:21:23 +0100262 Return:
Robert P. J. Day262086c2007-02-10 01:45:58 -0800263 0: cool
264 1: invalid arg
265 2: out of memory
266
267every line of which would start a new section. Again, probably not
268what you were after.
269
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270Take a look around the source tree for examples.
271
272
Randy Dunlapd28bee02006-02-01 03:06:57 -0800273kernel-doc for structs, unions, enums, and typedefs
274---------------------------------------------------
275
276Beside functions you can also write documentation for structs, unions,
277enums and typedefs. Instead of the function name you must write the name
278of the declaration; the struct/union/enum/typedef must always precede
279the name. Nesting of declarations is not supported.
280Use the argument mechanism to document members or constants.
281
282Inside a struct description, you can use the "private:" and "public:"
283comment tags. Structure fields that are inside a "private:" area
Randy Dunlap52dc5ae2009-04-30 15:08:53 -0700284are not listed in the generated output documentation. The "private:"
285and "public:" tags must begin immediately following a "/*" comment
286marker. They may optionally include comments between the ":" and the
287ending "*/" marker.
Randy Dunlapd28bee02006-02-01 03:06:57 -0800288
289Example:
290
291/**
292 * struct my_struct - short description
293 * @a: first member
294 * @b: second member
295 *
296 * Longer description
297 */
298struct my_struct {
299 int a;
300 int b;
Randy Dunlap52dc5ae2009-04-30 15:08:53 -0700301/* private: internal use only */
Randy Dunlapd28bee02006-02-01 03:06:57 -0800302 int c;
303};
304
305
Randy Dunlap28f4d752009-01-06 14:42:43 -0800306Including documentation blocks in source files
307----------------------------------------------
308
309To facilitate having source code and comments close together, you can
310include kernel-doc documentation blocks that are free-form comments
311instead of being kernel-doc for functions, structures, unions,
312enums, or typedefs. This could be used for something like a
313theory of operation for a driver or library code, for example.
314
315This is done by using a DOC: section keyword with a section title. E.g.:
316
317/**
318 * DOC: Theory of Operation
319 *
320 * The whizbang foobar is a dilly of a gizmo. It can do whatever you
321 * want it to do, at any time. It reads your mind. Here's how it works.
322 *
323 * foo bar splat
324 *
325 * The only drawback to this gizmo is that is can sometimes damage
326 * hardware, software, or its subject(s).
327 */
328
329DOC: sections are used in SGML templates files as indicated below.
330
331
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332How to make new SGML template files
333-----------------------------------
334
335SGML template files (*.tmpl) are like normal SGML files, except that
336they can contain escape sequences where extracted documentation should
337be inserted.
338
339!E<filename> is replaced by the documentation, in <filename>, for
340functions that are exported using EXPORT_SYMBOL: the function list is
341collected from files listed in Documentation/DocBook/Makefile.
342
343!I<filename> is replaced by the documentation for functions that are
344_not_ exported using EXPORT_SYMBOL.
345
346!D<filename> is used to name additional files to search for functions
347exported using EXPORT_SYMBOL.
348
349!F<filename> <function [functions...]> is replaced by the
350documentation, in <filename>, for the functions listed.
351
Randy Dunlap28f4d752009-01-06 14:42:43 -0800352!P<filename> <section title> is replaced by the contents of the DOC:
353section titled <section title> from <filename>.
354Spaces are allowed in <section title>; do not quote the <section title>.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355
Johannes Bergeda603f2010-09-11 15:55:22 -0700356!C<filename> is replaced by nothing, but makes the tools check that
357all DOC: sections and documented functions, symbols, etc. are used.
358This makes sense to use when you use !F/!P only and want to verify
359that all documentation is included.
360
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361Tim.
362*/ <twaugh@redhat.com>