blob: 4d04572b6549edef13d29a514124371b72863d10 [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.
Randy Dunlapf40b45a2009-02-11 13:04:31 -080067 */
Paul Jackson0842b242008-06-05 22:46:45 -070068
69The first line, with the short description, must be on a single line.
70
71The @argument descriptions must begin on the very next line following
72this opening short function description line, with no intervening
73empty comment lines.
74
Randy Dunlapd78dd072009-01-06 14:42:40 -080075If a function parameter is "..." (varargs), it should be listed in
76kernel-doc notation as:
77 * @...: description
78
79
Paul Jackson0842b242008-06-05 22:46:45 -070080Example kernel-doc data structure comment.
81
82/**
83 * struct blah - the basic blah structure
84 * @mem1: describe the first member of struct blah
85 * @mem2: describe the second member of struct blah,
86 * perhaps with more lines and words.
87 *
88 * Longer description of this structure.
Randy Dunlapf40b45a2009-02-11 13:04:31 -080089 */
Paul Jackson0842b242008-06-05 22:46:45 -070090
91The kernel-doc function comments describe each parameter to the
92function, in order, with the @name lines.
93
94The kernel-doc data structure comments describe each structure member
95in the data structure, with the @name lines.
96
97The longer description formatting is "reflowed", losing your line
98breaks. So presenting carefully formatted lists within these
99descriptions won't work so well; derived documentation will lose
100the formatting.
101
102See the section below "How to add extractable documentation to your
103source files" for more details and notes on how to format kernel-doc
104comments.
105
106Components of the kernel-doc system
107-----------------------------------
108
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109Many places in the source tree have extractable documentation in the
110form of block comments above functions. The components of this system
111are:
112
113- scripts/kernel-doc
114
115 This is a perl script that hunts for the block comments and can mark
116 them up directly into DocBook, man, text, and HTML. (No, not
117 texinfo.)
118
119- Documentation/DocBook/*.tmpl
120
121 These are SGML template files, which are normal SGML files with
122 special place-holders for where the extracted documentation should
123 go.
124
Randy Dunlapc6120932006-11-02 22:07:01 -0800125- scripts/basic/docproc.c
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
127 This is a program for converting SGML template files into SGML
128 files. When a file is referenced it is searched for symbols
129 exported (EXPORT_SYMBOL), to be able to distinguish between internal
130 and external functions.
131 It invokes kernel-doc, giving it the list of functions that
132 are to be documented.
133 Additionally it is used to scan the SGML template files to locate
134 all the files referenced herein. This is used to generate dependency
135 information as used by make.
136
137- Makefile
138
139 The targets 'sgmldocs', 'psdocs', 'pdfdocs', and 'htmldocs' are used
140 to build DocBook files, PostScript files, PDF files, and html files
141 in Documentation/DocBook.
142
143- Documentation/DocBook/Makefile
144
145 This is where C files are associated with SGML templates.
146
147
148How to extract the documentation
149--------------------------------
150
151If you just want to read the ready-made books on the various
152subsystems (see Documentation/DocBook/*.tmpl), just type 'make
Randy Dunlapd28bee02006-02-01 03:06:57 -0800153psdocs', or 'make pdfdocs', or 'make htmldocs', depending on your
154preference. If you would rather read a different format, you can type
155'make sgmldocs' and then use DocBook tools to convert
156Documentation/DocBook/*.sgml to a format of your choice (for example,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157'db2html ...' if 'make htmldocs' was not defined).
158
159If you want to see man pages instead, you can do this:
160
161$ cd linux
162$ scripts/kernel-doc -man $(find -name '*.c') | split-man.pl /tmp/man
163$ scripts/kernel-doc -man $(find -name '*.h') | split-man.pl /tmp/man
164
165Here is split-man.pl:
166
167-->
168#!/usr/bin/perl
169
170if ($#ARGV < 0) {
171 die "where do I put the results?\n";
172}
173
174mkdir $ARGV[0],0777;
175$state = 0;
176while (<STDIN>) {
Kevin Diggs65eb3dc2008-08-26 10:26:54 +0200177 if (/^\.TH \"[^\"]*\" 9 \"([^\"]*)\"/) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 if ($state == 1) { close OUT }
179 $state = 1;
Kevin Diggs65eb3dc2008-08-26 10:26:54 +0200180 $fn = "$ARGV[0]/$1.9";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 print STDERR "Creating $fn\n";
182 open OUT, ">$fn" or die "can't open $fn: $!\n";
183 print OUT $_;
184 } elsif ($state != 0) {
185 print OUT $_;
186 }
187}
188
189close OUT;
190<--
191
192If you just want to view the documentation for one function in one
193file, you can do this:
194
195$ scripts/kernel-doc -man -function fn file | nroff -man | less
196
197or this:
198
199$ scripts/kernel-doc -text -function fn file
200
201
202How to add extractable documentation to your source files
203---------------------------------------------------------
204
205The format of the block comment is like this:
206
207/**
208 * function_name(:)? (- short description)?
Randy Dunlap891dcd22007-02-10 01:45:53 -0800209(* @parameterx(space)*: (description of parameter x)?)*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210(* a blank line)?
211 * (Description:)? (Description of function)?
212 * (section header: (section description)? )*
213(*)?*/
214
Robert P. J. Day262086c2007-02-10 01:45:58 -0800215The short function description ***cannot be multiline***, but the other
216descriptions can be (and they can contain blank lines). If you continue
217that initial short description onto a second line, that second line will
218appear further down at the beginning of the description section, which is
219almost certainly not what you had in mind.
220
221Avoid putting a spurious blank line after the function name, or else the
222description will be repeated!
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
224All descriptive text is further processed, scanning for the following special
225patterns, which are highlighted appropriately.
226
227'funcname()' - function
228'$ENVVAR' - environment variable
229'&struct_name' - name of a structure (up to two words including 'struct')
230'@parameter' - name of a parameter
231'%CONST' - name of a constant.
232
Robert P. J. Day262086c2007-02-10 01:45:58 -0800233NOTE 1: The multi-line descriptive text you provide does *not* recognize
234line breaks, so if you try to format some text nicely, as in:
235
236 Return codes
237 0 - cool
238 1 - invalid arg
239 2 - out of memory
240
241this will all run together and produce:
242
243 Return codes 0 - cool 1 - invalid arg 2 - out of memory
244
245NOTE 2: If the descriptive text you provide has lines that begin with
246some phrase followed by a colon, each of those phrases will be taken as
247a new section heading, which means you should similarly try to avoid text
248like:
249
250 Return codes:
251 0: cool
252 1: invalid arg
253 2: out of memory
254
255every line of which would start a new section. Again, probably not
256what you were after.
257
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258Take a look around the source tree for examples.
259
260
Randy Dunlapd28bee02006-02-01 03:06:57 -0800261kernel-doc for structs, unions, enums, and typedefs
262---------------------------------------------------
263
264Beside functions you can also write documentation for structs, unions,
265enums and typedefs. Instead of the function name you must write the name
266of the declaration; the struct/union/enum/typedef must always precede
267the name. Nesting of declarations is not supported.
268Use the argument mechanism to document members or constants.
269
270Inside a struct description, you can use the "private:" and "public:"
271comment tags. Structure fields that are inside a "private:" area
Randy Dunlap52dc5ae2009-04-30 15:08:53 -0700272are not listed in the generated output documentation. The "private:"
273and "public:" tags must begin immediately following a "/*" comment
274marker. They may optionally include comments between the ":" and the
275ending "*/" marker.
Randy Dunlapd28bee02006-02-01 03:06:57 -0800276
277Example:
278
279/**
280 * struct my_struct - short description
281 * @a: first member
282 * @b: second member
283 *
284 * Longer description
285 */
286struct my_struct {
287 int a;
288 int b;
Randy Dunlap52dc5ae2009-04-30 15:08:53 -0700289/* private: internal use only */
Randy Dunlapd28bee02006-02-01 03:06:57 -0800290 int c;
291};
292
293
Randy Dunlap28f4d752009-01-06 14:42:43 -0800294Including documentation blocks in source files
295----------------------------------------------
296
297To facilitate having source code and comments close together, you can
298include kernel-doc documentation blocks that are free-form comments
299instead of being kernel-doc for functions, structures, unions,
300enums, or typedefs. This could be used for something like a
301theory of operation for a driver or library code, for example.
302
303This is done by using a DOC: section keyword with a section title. E.g.:
304
305/**
306 * DOC: Theory of Operation
307 *
308 * The whizbang foobar is a dilly of a gizmo. It can do whatever you
309 * want it to do, at any time. It reads your mind. Here's how it works.
310 *
311 * foo bar splat
312 *
313 * The only drawback to this gizmo is that is can sometimes damage
314 * hardware, software, or its subject(s).
315 */
316
317DOC: sections are used in SGML templates files as indicated below.
318
319
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320How to make new SGML template files
321-----------------------------------
322
323SGML template files (*.tmpl) are like normal SGML files, except that
324they can contain escape sequences where extracted documentation should
325be inserted.
326
327!E<filename> is replaced by the documentation, in <filename>, for
328functions that are exported using EXPORT_SYMBOL: the function list is
329collected from files listed in Documentation/DocBook/Makefile.
330
331!I<filename> is replaced by the documentation for functions that are
332_not_ exported using EXPORT_SYMBOL.
333
334!D<filename> is used to name additional files to search for functions
335exported using EXPORT_SYMBOL.
336
337!F<filename> <function [functions...]> is replaced by the
338documentation, in <filename>, for the functions listed.
339
Randy Dunlap28f4d752009-01-06 14:42:43 -0800340!P<filename> <section title> is replaced by the contents of the DOC:
341section titled <section title> from <filename>.
342Spaces are allowed in <section title>; do not quote the <section title>.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343
344Tim.
345*/ <twaugh@redhat.com>