4 How to format kernel-doc comments
5 ---------------------------------
7 In order to provide embedded, 'C' friendly, easy to maintain,
8 but consistent and extractable documentation of the functions and
9 data structures in the Linux kernel, the Linux kernel has adopted
10 a consistent style for documenting functions and their parameters,
11 and structures and their members.
13 The format for this documentation is called the kernel-doc format.
14 It is documented in this Documentation/kernel-doc-nano-HOWTO.txt file.
16 This style embeds the documentation within the source files, using
17 a few simple conventions. The scripts/kernel-doc perl script, some
18 SGML templates in Documentation/DocBook, and other tools understand
19 these conventions, and are used to extract this embedded documentation
20 into various documents.
22 In order to provide good documentation of kernel functions and data
23 structures, please use the following conventions to format your
24 kernel-doc comments in Linux kernel source.
26 We definitely need kernel-doc formatted documentation for functions
27 that are exported to loadable modules using EXPORT_SYMBOL.
29 We also look to provide kernel-doc formatted documentation for
30 functions externally visible to other kernel files (not marked
33 We also recommend providing kernel-doc formatted documentation
34 for private (file "static") routines, for consistency of kernel
35 source code layout. But this is lower priority and at the
36 discretion of the MAINTAINER of that kernel source file.
38 Data structures visible in kernel include files should also be
39 documented using kernel-doc formatted comments.
41 The opening comment mark "/**" is reserved for kernel-doc comments.
42 Only comments so marked will be considered by the kernel-doc scripts,
43 and 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
45 kernel-doc formatted comments. The closing comment marker for
46 kernel-doc comments can be either "*/" or "**/", but "*/" is
47 preferred in the Linux kernel tree.
49 Kernel-doc comments should be placed just before the function
50 or data structure being described.
52 Example kernel-doc function comment:
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
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
66 * The longer description can have multiple paragraphs.
68 * Return: Describe the return value of foobar.
71 The short description following the subject can span multiple lines
72 and ends with an @argument description, an empty line or the end of
75 The @argument descriptions must begin on the very next line following
76 this opening short function description line, with no intervening
79 If a function parameter is "..." (varargs), it should be listed in
80 kernel-doc notation as:
83 The return value, if any, should be described in a dedicated section
86 Example kernel-doc data structure comment.
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.
94 * Longer description of this structure.
97 The kernel-doc function comments describe each parameter to the
98 function, in order, with the @name lines.
100 The kernel-doc data structure comments describe each structure member
101 in the data structure, with the @name lines.
103 The longer description formatting is "reflowed", losing your line
104 breaks. So presenting carefully formatted lists within these
105 descriptions won't work so well; derived documentation will lose
108 See the section below "How to add extractable documentation to your
109 source files" for more details and notes on how to format kernel-doc
112 Components of the kernel-doc system
113 -----------------------------------
115 Many places in the source tree have extractable documentation in the
116 form of block comments above functions. The components of this system
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
125 - Documentation/DocBook/*.tmpl
127 These are SGML template files, which are normal SGML files with
128 special place-holders for where the extracted documentation should
131 - scripts/basic/docproc.c
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.
145 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
150 - Documentation/DocBook/Makefile
152 This is where C files are associated with SGML templates.
155 How to extract the documentation
156 --------------------------------
158 If you just want to read the ready-made books on the various
159 subsystems (see Documentation/DocBook/*.tmpl), just type 'make
160 psdocs', or 'make pdfdocs', or 'make htmldocs', depending on your
161 preference. If you would rather read a different format, you can type
162 'make xmldocs' and then use DocBook tools to convert
163 Documentation/DocBook/*.xml to a format of your choice (for example,
164 'db2html ...' if 'make htmldocs' was not defined).
166 If you want to see man pages instead, you can do this:
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
172 Here is split-man.pl:
178 die "where do I put the results?\n";
184 if (/^\.TH \"[^\"]*\" 9 \"([^\"]*)\"/) {
185 if ($state == 1) { close OUT }
187 $fn = "$ARGV[0]/$1.9";
188 print STDERR "Creating $fn\n";
189 open OUT, ">$fn" or die "can't open $fn: $!\n";
191 } elsif ($state != 0) {
199 If you just want to view the documentation for one function in one
200 file, you can do this:
202 $ scripts/kernel-doc -man -function fn file | nroff -man | less
206 $ scripts/kernel-doc -text -function fn file
209 How to add extractable documentation to your source files
210 ---------------------------------------------------------
212 The format of the block comment is like this:
215 * function_name(:)? (- short description)?
216 (* @parameterx(space)*: (description of parameter x)?)*
218 * (Description:)? (Description of function)?
219 * (section header: (section description)? )*
222 All "description" text can span multiple lines, although the
223 function_name & its short description are traditionally on a single line.
224 Description text may also contain blank lines (i.e., lines that contain
227 "section header:" names must be unique per function (or struct,
228 union, typedef, enum).
230 Use the section header "Return" for sections describing the return value
233 Avoid putting a spurious blank line after the function name, or else the
234 description will be repeated!
236 All descriptive text is further processed, scanning for the following special
237 patterns, which are highlighted appropriately.
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.
245 NOTE 1: The multi-line descriptive text you provide does *not* recognize
246 line breaks, so if you try to format some text nicely, as in:
253 this will all run together and produce:
255 Return: 0 - cool 1 - invalid arg 2 - out of memory
257 NOTE 2: If the descriptive text you provide has lines that begin with
258 some phrase followed by a colon, each of those phrases will be taken as
259 a new section heading, which means you should similarly try to avoid text
267 every line of which would start a new section. Again, probably not
270 Take a look around the source tree for examples.
273 kernel-doc for structs, unions, enums, and typedefs
274 ---------------------------------------------------
276 Beside functions you can also write documentation for structs, unions,
277 enums and typedefs. Instead of the function name you must write the name
278 of the declaration; the struct/union/enum/typedef must always precede
279 the name. Nesting of declarations is not supported.
280 Use the argument mechanism to document members or constants.
282 Inside a struct description, you can use the "private:" and "public:"
283 comment tags. Structure fields that are inside a "private:" area
284 are not listed in the generated output documentation. The "private:"
285 and "public:" tags must begin immediately following a "/*" comment
286 marker. They may optionally include comments between the ":" and the
292 * struct my_struct - short description
301 /* private: internal use only */
306 Including documentation blocks in source files
307 ----------------------------------------------
309 To facilitate having source code and comments close together, you can
310 include kernel-doc documentation blocks that are free-form comments
311 instead of being kernel-doc for functions, structures, unions,
312 enums, or typedefs. This could be used for something like a
313 theory of operation for a driver or library code, for example.
315 This is done by using a DOC: section keyword with a section title. E.g.:
318 * DOC: Theory of Operation
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.
325 * The only drawback to this gizmo is that is can sometimes damage
326 * hardware, software, or its subject(s).
329 DOC: sections are used in SGML templates files as indicated below.
332 How to make new SGML template files
333 -----------------------------------
335 SGML template files (*.tmpl) are like normal SGML files, except that
336 they can contain escape sequences where extracted documentation should
339 !E<filename> is replaced by the documentation, in <filename>, for
340 functions that are exported using EXPORT_SYMBOL: the function list is
341 collected from files listed in Documentation/DocBook/Makefile.
343 !I<filename> is replaced by the documentation for functions that are
344 _not_ exported using EXPORT_SYMBOL.
346 !D<filename> is used to name additional files to search for functions
347 exported using EXPORT_SYMBOL.
349 !F<filename> <function [functions...]> is replaced by the
350 documentation, in <filename>, for the functions listed.
352 !P<filename> <section title> is replaced by the contents of the DOC:
353 section titled <section title> from <filename>.
354 Spaces are allowed in <section title>; do not quote the <section title>.
356 !C<filename> is replaced by nothing, but makes the tools check that
357 all DOC: sections and documented functions, symbols, etc. are used.
358 This makes sense to use when you use !F/!P only and want to verify
359 that all documentation is included.
362 */ <twaugh@redhat.com>