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