Back to home page

LXR

 
 

    


0001 NOTE: this document is outdated and will eventually be removed.  See
0002 Documentation/doc-guide/ for current information.
0003 
0004 kernel-doc nano-HOWTO
0005 =====================
0006 
0007 How to format kernel-doc comments
0008 ---------------------------------
0009 
0010 In order to provide embedded, 'C' friendly, easy to maintain,
0011 but consistent and extractable documentation of the functions and
0012 data structures in the Linux kernel, the Linux kernel has adopted
0013 a consistent style for documenting functions and their parameters,
0014 and structures and their members.
0015 
0016 The format for this documentation is called the kernel-doc format.
0017 It is documented in this Documentation/kernel-doc-nano-HOWTO.txt file.
0018 
0019 This style embeds the documentation within the source files, using
0020 a few simple conventions.  The scripts/kernel-doc perl script, some
0021 SGML templates in Documentation/DocBook, and other tools understand
0022 these conventions, and are used to extract this embedded documentation
0023 into various documents.
0024 
0025 In order to provide good documentation of kernel functions and data
0026 structures, please use the following conventions to format your
0027 kernel-doc comments in Linux kernel source.
0028 
0029 We definitely need kernel-doc formatted documentation for functions
0030 that are exported to loadable modules using EXPORT_SYMBOL.
0031 
0032 We also look to provide kernel-doc formatted documentation for
0033 functions externally visible to other kernel files (not marked
0034 "static").
0035 
0036 We also recommend providing kernel-doc formatted documentation
0037 for private (file "static") routines, for consistency of kernel
0038 source code layout.  But this is lower priority and at the
0039 discretion of the MAINTAINER of that kernel source file.
0040 
0041 Data structures visible in kernel include files should also be
0042 documented using kernel-doc formatted comments.
0043 
0044 The opening comment mark "/**" is reserved for kernel-doc comments.
0045 Only comments so marked will be considered by the kernel-doc scripts,
0046 and any comment so marked must be in kernel-doc format.  Do not use
0047 "/**" to be begin a comment block unless the comment block contains
0048 kernel-doc formatted comments.  The closing comment marker for
0049 kernel-doc comments can be either "*/" or "**/", but "*/" is
0050 preferred in the Linux kernel tree.
0051 
0052 Kernel-doc comments should be placed just before the function
0053 or data structure being described.
0054 
0055 Example kernel-doc function comment:
0056 
0057 /**
0058  * foobar() - short function description of foobar
0059  * @arg1:       Describe the first argument to foobar.
0060  * @arg2:       Describe the second argument to foobar.
0061  *              One can provide multiple line descriptions
0062  *              for arguments.
0063  *
0064  * A longer description, with more discussion of the function foobar()
0065  * that might be useful to those using or modifying it.  Begins with
0066  * empty comment line, and may include additional embedded empty
0067  * comment lines.
0068  *
0069  * The longer description can have multiple paragraphs.
0070  *
0071  * Return: Describe the return value of foobar.
0072  */
0073 
0074 The short description following the subject can span multiple lines
0075 and ends with an @argument description, an empty line or the end of
0076 the comment block.
0077 
0078 The @argument descriptions must begin on the very next line following
0079 this opening short function description line, with no intervening
0080 empty comment lines.
0081 
0082 If a function parameter is "..." (varargs), it should be listed in
0083 kernel-doc notation as:
0084  * @...: description
0085 
0086 The return value, if any, should be described in a dedicated section
0087 named "Return".
0088 
0089 Example kernel-doc data structure comment.
0090 
0091 /**
0092  * struct blah - the basic blah structure
0093  * @mem1:       describe the first member of struct blah
0094  * @mem2:       describe the second member of struct blah,
0095  *              perhaps with more lines and words.
0096  *
0097  * Longer description of this structure.
0098  */
0099 
0100 The kernel-doc function comments describe each parameter to the
0101 function, in order, with the @name lines.
0102 
0103 The kernel-doc data structure comments describe each structure member
0104 in the data structure, with the @name lines.
0105 
0106 The longer description formatting is "reflowed", losing your line
0107 breaks.  So presenting carefully formatted lists within these
0108 descriptions won't work so well; derived documentation will lose
0109 the formatting.
0110 
0111 See the section below "How to add extractable documentation to your
0112 source files" for more details and notes on how to format kernel-doc
0113 comments.
0114 
0115 Components of the kernel-doc system
0116 -----------------------------------
0117 
0118 Many places in the source tree have extractable documentation in the
0119 form of block comments above functions.  The components of this system
0120 are:
0121 
0122 - scripts/kernel-doc
0123 
0124   This is a perl script that hunts for the block comments and can mark
0125   them up directly into DocBook, man, text, and HTML. (No, not
0126   texinfo.)
0127 
0128 - Documentation/DocBook/*.tmpl
0129 
0130   These are SGML template files, which are normal SGML files with
0131   special place-holders for where the extracted documentation should
0132   go.
0133 
0134 - scripts/docproc.c
0135 
0136   This is a program for converting SGML template files into SGML
0137   files. When a file is referenced it is searched for symbols
0138   exported (EXPORT_SYMBOL), to be able to distinguish between internal
0139   and external functions.
0140   It invokes kernel-doc, giving it the list of functions that
0141   are to be documented.
0142   Additionally it is used to scan the SGML template files to locate
0143   all the files referenced herein. This is used to generate dependency
0144   information as used by make.
0145 
0146 - Makefile
0147 
0148   The targets 'xmldocs', 'psdocs', 'pdfdocs', and 'htmldocs' are used
0149   to build XML DocBook files, PostScript files, PDF files, and html files
0150   in Documentation/DocBook. The older target 'sgmldocs' is equivalent
0151   to 'xmldocs'.
0152 
0153 - Documentation/DocBook/Makefile
0154 
0155   This is where C files are associated with SGML templates.
0156 
0157 
0158 How to extract the documentation
0159 --------------------------------
0160 
0161 If you just want to read the ready-made books on the various
0162 subsystems (see Documentation/DocBook/*.tmpl), just type 'make
0163 psdocs', or 'make pdfdocs', or 'make htmldocs', depending on your
0164 preference.  If you would rather read a different format, you can type
0165 'make xmldocs' and then use DocBook tools to convert
0166 Documentation/DocBook/*.xml to a format of your choice (for example,
0167 'db2html ...' if 'make htmldocs' was not defined).
0168 
0169 If you want to see man pages instead, you can do this:
0170 
0171 $ cd linux
0172 $ scripts/kernel-doc -man $(find -name '*.c') | split-man.pl /tmp/man
0173 $ scripts/kernel-doc -man $(find -name '*.h') | split-man.pl /tmp/man
0174 
0175 Here is split-man.pl:
0176 
0177 -->
0178 #!/usr/bin/perl
0179 
0180 if ($#ARGV < 0) {
0181    die "where do I put the results?\n";
0182 }
0183 
0184 mkdir $ARGV[0],0777;
0185 $state = 0;
0186 while (<STDIN>) {
0187     if (/^\.TH \"[^\"]*\" 9 \"([^\"]*)\"/) {
0188         if ($state == 1) { close OUT }
0189         $state = 1;
0190         $fn = "$ARGV[0]/$1.9";
0191         print STDERR "Creating $fn\n";
0192         open OUT, ">$fn" or die "can't open $fn: $!\n";
0193         print OUT $_;
0194     } elsif ($state != 0) {
0195         print OUT $_;
0196     }
0197 }
0198 
0199 close OUT;
0200 <--
0201 
0202 If you just want to view the documentation for one function in one
0203 file, you can do this:
0204 
0205 $ scripts/kernel-doc -man -function fn file | nroff -man | less
0206 
0207 or this:
0208 
0209 $ scripts/kernel-doc -text -function fn file
0210 
0211 
0212 How to add extractable documentation to your source files
0213 ---------------------------------------------------------
0214 
0215 The format of the block comment is like this:
0216 
0217 /**
0218  * function_name(:)? (- short description)?
0219 (* @parameterx(space)*: (description of parameter x)?)*
0220 (* a blank line)?
0221  * (Description:)? (Description of function)?
0222  * (section header: (section description)? )*
0223 (*)?*/
0224 
0225 All "description" text can span multiple lines, although the
0226 function_name & its short description are traditionally on a single line.
0227 Description text may also contain blank lines (i.e., lines that contain
0228 only a "*").
0229 
0230 "section header:" names must be unique per function (or struct,
0231 union, typedef, enum).
0232 
0233 Use the section header "Return" for sections describing the return value
0234 of a function.
0235 
0236 Avoid putting a spurious blank line after the function name, or else the
0237 description will be repeated!
0238 
0239 All descriptive text is further processed, scanning for the following special
0240 patterns, which are highlighted appropriately.
0241 
0242 'funcname()' - function
0243 '$ENVVAR' - environment variable
0244 '&struct_name' - name of a structure (up to two words including 'struct')
0245 '@parameter' - name of a parameter
0246 '%CONST' - name of a constant.
0247 
0248 NOTE 1:  The multi-line descriptive text you provide does *not* recognize
0249 line breaks, so if you try to format some text nicely, as in:
0250 
0251   Return:
0252     0 - cool
0253     1 - invalid arg
0254     2 - out of memory
0255 
0256 this will all run together and produce:
0257 
0258   Return: 0 - cool 1 - invalid arg 2 - out of memory
0259 
0260 NOTE 2:  If the descriptive text you provide has lines that begin with
0261 some phrase followed by a colon, each of those phrases will be taken as
0262 a new section heading, which means you should similarly try to avoid text
0263 like:
0264 
0265   Return:
0266     0: cool
0267     1: invalid arg
0268     2: out of memory
0269 
0270 every line of which would start a new section.  Again, probably not
0271 what you were after.
0272 
0273 Take a look around the source tree for examples.
0274 
0275 
0276 kernel-doc for structs, unions, enums, and typedefs
0277 ---------------------------------------------------
0278 
0279 Beside functions you can also write documentation for structs, unions,
0280 enums and typedefs. Instead of the function name you must write the name
0281 of the declaration;  the struct/union/enum/typedef must always precede
0282 the name. Nesting of declarations is not supported.
0283 Use the argument mechanism to document members or constants.
0284 
0285 Inside a struct description, you can use the "private:" and "public:"
0286 comment tags.  Structure fields that are inside a "private:" area
0287 are not listed in the generated output documentation.  The "private:"
0288 and "public:" tags must begin immediately following a "/*" comment
0289 marker.  They may optionally include comments between the ":" and the
0290 ending "*/" marker.
0291 
0292 Example:
0293 
0294 /**
0295  * struct my_struct - short description
0296  * @a: first member
0297  * @b: second member
0298  *
0299  * Longer description
0300  */
0301 struct my_struct {
0302     int a;
0303     int b;
0304 /* private: internal use only */
0305     int c;
0306 };
0307 
0308 
0309 Including documentation blocks in source files
0310 ----------------------------------------------
0311 
0312 To facilitate having source code and comments close together, you can
0313 include kernel-doc documentation blocks that are free-form comments
0314 instead of being kernel-doc for functions, structures, unions,
0315 enums, or typedefs.  This could be used for something like a
0316 theory of operation for a driver or library code, for example.
0317 
0318 This is done by using a DOC: section keyword with a section title.  E.g.:
0319 
0320 /**
0321  * DOC: Theory of Operation
0322  *
0323  * The whizbang foobar is a dilly of a gizmo.  It can do whatever you
0324  * want it to do, at any time.  It reads your mind.  Here's how it works.
0325  *
0326  * foo bar splat
0327  *
0328  * The only drawback to this gizmo is that is can sometimes damage
0329  * hardware, software, or its subject(s).
0330  */
0331 
0332 DOC: sections are used in SGML templates files as indicated below.
0333 
0334 
0335 How to make new SGML template files
0336 -----------------------------------
0337 
0338 SGML template files (*.tmpl) are like normal SGML files, except that
0339 they can contain escape sequences where extracted documentation should
0340 be inserted.
0341 
0342 !E<filename> is replaced by the documentation, in <filename>, for
0343 functions that are exported using EXPORT_SYMBOL: the function list is
0344 collected from files listed in Documentation/DocBook/Makefile.
0345 
0346 !I<filename> is replaced by the documentation for functions that are
0347 _not_ exported using EXPORT_SYMBOL.
0348 
0349 !D<filename> is used to name additional files to search for functions
0350 exported using EXPORT_SYMBOL.
0351 
0352 !F<filename> <function [functions...]> is replaced by the
0353 documentation, in <filename>, for the functions listed.
0354 
0355 !P<filename> <section title> is replaced by the contents of the DOC:
0356 section titled <section title> from <filename>.
0357 Spaces are allowed in <section title>; do not quote the <section title>.
0358 
0359 !C<filename> is replaced by nothing, but makes the tools check that
0360 all DOC: sections and documented functions, symbols, etc. are used.
0361 This makes sense to use when you use !F/!P only and want to verify
0362 that all documentation is included.
0363 
0364 Tim.
0365 */ <twaugh@redhat.com>