kernel-doc-nano-HOWTO.txt 11.2 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3
kernel-doc nano-HOWTO
=====================

4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
How to format kernel-doc comments
---------------------------------

In order to provide embedded, 'C' friendly, easy to maintain,
but consistent and extractable documentation of the functions and
data structures in the Linux kernel, the Linux kernel has adopted
a consistent style for documenting functions and their parameters,
and structures and their members.

The format for this documentation is called the kernel-doc format.
It is documented in this Documentation/kernel-doc-nano-HOWTO.txt file.

This style embeds the documentation within the source files, using
a few simple conventions.  The scripts/kernel-doc perl script, some
SGML templates in Documentation/DocBook, and other tools understand
these conventions, and are used to extract this embedded documentation
into various documents.

In order to provide good documentation of kernel functions and data
structures, please use the following conventions to format your
kernel-doc comments in Linux kernel source.

We definitely need kernel-doc formatted documentation for functions
that are exported to loadable modules using EXPORT_SYMBOL.

We also look to provide kernel-doc formatted documentation for
functions externally visible to other kernel files (not marked
"static").

We also recommend providing kernel-doc formatted documentation
for private (file "static") routines, for consistency of kernel
source code layout.  But this is lower priority and at the
discretion of the MAINTAINER of that kernel source file.

Data structures visible in kernel include files should also be
documented using kernel-doc formatted comments.

The opening comment mark "/**" is reserved for kernel-doc comments.
Only comments so marked will be considered by the kernel-doc scripts,
and any comment so marked must be in kernel-doc format.  Do not use
"/**" to be begin a comment block unless the comment block contains
kernel-doc formatted comments.  The closing comment marker for
46 47
kernel-doc comments can be either "*/" or "**/", but "*/" is
preferred in the Linux kernel tree.
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66

Kernel-doc comments should be placed just before the function
or data structure being described.

Example kernel-doc function comment:

/**
 * foobar() - short function description of foobar
 * @arg1:	Describe the first argument to foobar.
 * @arg2:	Describe the second argument to foobar.
 *		One can provide multiple line descriptions
 *		for arguments.
 *
 * A longer description, with more discussion of the function foobar()
 * that might be useful to those using or modifying it.  Begins with
 * empty comment line, and may include additional embedded empty
 * comment lines.
 *
 * The longer description can have multiple paragraphs.
67
 */
68

69 70 71
The short description following the subject can span multiple lines
and ends with an @argument description, an empty line or the end of
the comment block.
72 73 74 75 76

The @argument descriptions must begin on the very next line following
this opening short function description line, with no intervening
empty comment lines.

77 78 79 80 81
If a function parameter is "..." (varargs), it should be listed in
kernel-doc notation as:
 * @...: description


82 83 84 85 86 87 88 89 90
Example kernel-doc data structure comment.

/**
 * struct blah - the basic blah structure
 * @mem1:	describe the first member of struct blah
 * @mem2:	describe the second member of struct blah,
 *		perhaps with more lines and words.
 *
 * Longer description of this structure.
91
 */
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110

The kernel-doc function comments describe each parameter to the
function, in order, with the @name lines.

The kernel-doc data structure comments describe each structure member
in the data structure, with the @name lines.

The longer description formatting is "reflowed", losing your line
breaks.  So presenting carefully formatted lists within these
descriptions won't work so well; derived documentation will lose
the formatting.

See the section below "How to add extractable documentation to your
source files" for more details and notes on how to format kernel-doc
comments.

Components of the kernel-doc system
-----------------------------------

L
Linus Torvalds 已提交
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
Many places in the source tree have extractable documentation in the
form of block comments above functions.  The components of this system
are:

- scripts/kernel-doc

  This is a perl script that hunts for the block comments and can mark
  them up directly into DocBook, man, text, and HTML. (No, not
  texinfo.)

- Documentation/DocBook/*.tmpl

  These are SGML template files, which are normal SGML files with
  special place-holders for where the extracted documentation should
  go.

127
- scripts/basic/docproc.c
L
Linus Torvalds 已提交
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154

  This is a program for converting SGML template files into SGML
  files. When a file is referenced it is searched for symbols
  exported (EXPORT_SYMBOL), to be able to distinguish between internal
  and external functions.
  It invokes kernel-doc, giving it the list of functions that
  are to be documented.
  Additionally it is used to scan the SGML template files to locate
  all the files referenced herein. This is used to generate dependency
  information as used by make.

- Makefile

  The targets 'sgmldocs', 'psdocs', 'pdfdocs', and 'htmldocs' are used
  to build DocBook files, PostScript files, PDF files, and html files
  in Documentation/DocBook.

- Documentation/DocBook/Makefile

  This is where C files are associated with SGML templates.


How to extract the documentation
--------------------------------

If you just want to read the ready-made books on the various
subsystems (see Documentation/DocBook/*.tmpl), just type 'make
155 156 157 158
psdocs', or 'make pdfdocs', or 'make htmldocs', depending on your
preference.  If you would rather read a different format, you can type
'make sgmldocs' and then use DocBook tools to convert
Documentation/DocBook/*.sgml to a format of your choice (for example,
L
Linus Torvalds 已提交
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
'db2html ...' if 'make htmldocs' was not defined).

If you want to see man pages instead, you can do this:

$ cd linux
$ scripts/kernel-doc -man $(find -name '*.c') | split-man.pl /tmp/man
$ scripts/kernel-doc -man $(find -name '*.h') | split-man.pl /tmp/man

Here is split-man.pl:

-->
#!/usr/bin/perl

if ($#ARGV < 0) {
   die "where do I put the results?\n";
}

mkdir $ARGV[0],0777;
$state = 0;
while (<STDIN>) {
179
    if (/^\.TH \"[^\"]*\" 9 \"([^\"]*)\"/) {
L
Linus Torvalds 已提交
180 181
	if ($state == 1) { close OUT }
	$state = 1;
182
	$fn = "$ARGV[0]/$1.9";
L
Linus Torvalds 已提交
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
	print STDERR "Creating $fn\n";
	open OUT, ">$fn" or die "can't open $fn: $!\n";
	print OUT $_;
    } elsif ($state != 0) {
	print OUT $_;
    }
}

close OUT;
<--

If you just want to view the documentation for one function in one
file, you can do this:

$ scripts/kernel-doc -man -function fn file | nroff -man | less

or this:

$ scripts/kernel-doc -text -function fn file


How to add extractable documentation to your source files
---------------------------------------------------------

The format of the block comment is like this:

/**
 * function_name(:)? (- short description)?
211
(* @parameterx(space)*: (description of parameter x)?)*
L
Linus Torvalds 已提交
212 213 214 215 216
(* a blank line)?
 * (Description:)? (Description of function)?
 * (section header: (section description)? )*
(*)?*/

217 218 219 220 221 222 223 224
The short function description ***cannot be multiline***, but the other
descriptions can be (and they can contain blank lines).  If you continue
that initial short description onto a second line, that second line will
appear further down at the beginning of the description section, which is
almost certainly not what you had in mind.

Avoid putting a spurious blank line after the function name, or else the
description will be repeated!
L
Linus Torvalds 已提交
225 226 227 228 229 230 231 232 233 234

All descriptive text is further processed, scanning for the following special
patterns, which are highlighted appropriately.

'funcname()' - function
'$ENVVAR' - environment variable
'&struct_name' - name of a structure (up to two words including 'struct')
'@parameter' - name of a parameter
'%CONST' - name of a constant.

235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
NOTE 1:  The multi-line descriptive text you provide does *not* recognize
line breaks, so if you try to format some text nicely, as in:

  Return codes
    0 - cool
    1 - invalid arg
    2 - out of memory

this will all run together and produce:

  Return codes 0 - cool 1 - invalid arg 2 - out of memory

NOTE 2:  If the descriptive text you provide has lines that begin with
some phrase followed by a colon, each of those phrases will be taken as
a new section heading, which means you should similarly try to avoid text
like:

  Return codes:
    0: cool
    1: invalid arg
    2: out of memory

every line of which would start a new section.  Again, probably not
what you were after.

L
Linus Torvalds 已提交
260 261 262
Take a look around the source tree for examples.


263 264 265 266 267 268 269 270 271 272 273
kernel-doc for structs, unions, enums, and typedefs
---------------------------------------------------

Beside functions you can also write documentation for structs, unions,
enums and typedefs. Instead of the function name you must write the name
of the declaration;  the struct/union/enum/typedef must always precede
the name. Nesting of declarations is not 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
274 275 276 277
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.
278 279 280 281 282 283 284 285 286 287 288 289 290

Example:

/**
 * struct my_struct - short description
 * @a: first member
 * @b: second member
 *
 * Longer description
 */
struct my_struct {
    int a;
    int b;
291
/* private: internal use only */
292 293 294 295
    int c;
};


296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321
Including documentation blocks in source files
----------------------------------------------

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,
enums, 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.  E.g.:

/**
 * DOC: Theory of Operation
 *
 * The whizbang foobar is a dilly of a gizmo.  It can do whatever you
 * want it to do, at any time.  It reads your mind.  Here's how it works.
 *
 * foo bar splat
 *
 * The only drawback to this gizmo is that is can sometimes damage
 * hardware, software, or its subject(s).
 */

DOC: sections are used in SGML templates files as indicated below.


L
Linus Torvalds 已提交
322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
How to make new SGML template files
-----------------------------------

SGML template files (*.tmpl) are like normal SGML files, except that
they can contain escape sequences where extracted documentation should
be inserted.

!E<filename> is replaced by the documentation, in <filename>, for
functions that are exported using EXPORT_SYMBOL: the function list is
collected from files listed in Documentation/DocBook/Makefile.

!I<filename> is replaced by the documentation for functions that are
_not_ exported using EXPORT_SYMBOL.

!D<filename> is used to name additional files to search for functions
exported using EXPORT_SYMBOL.

!F<filename> <function [functions...]> is replaced by the
documentation, in <filename>, for the functions listed.

342 343 344
!P<filename> <section title> is replaced by the contents of the DOC:
section titled <section title> from <filename>.
Spaces are allowed in <section title>; do not quote the <section title>.
L
Linus Torvalds 已提交
345 346 347

Tim.
*/ <twaugh@redhat.com>