kernel-doc-nano-HOWTO.txt 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. kernel-doc nano-HOWTO
  2. =====================
  3. How to format kernel-doc comments
  4. ---------------------------------
  5. In order to provide embedded, 'C' friendly, easy to maintain,
  6. but consistent and extractable documentation of the functions and
  7. data structures in the Linux kernel, the Linux kernel has adopted
  8. a consistent style for documenting functions and their parameters,
  9. and structures and their members.
  10. The format for this documentation is called the kernel-doc format.
  11. It is documented in this Documentation/kernel-doc-nano-HOWTO.txt file.
  12. This style embeds the documentation within the source files, using
  13. a few simple conventions. The scripts/kernel-doc perl script, some
  14. SGML templates in Documentation/DocBook, and other tools understand
  15. these conventions, and are used to extract this embedded documentation
  16. into various documents.
  17. In order to provide good documentation of kernel functions and data
  18. structures, please use the following conventions to format your
  19. kernel-doc comments in Linux kernel source.
  20. We definitely need kernel-doc formatted documentation for functions
  21. that are exported to loadable modules using EXPORT_SYMBOL.
  22. We also look to provide kernel-doc formatted documentation for
  23. functions externally visible to other kernel files (not marked
  24. "static").
  25. We also recommend providing kernel-doc formatted documentation
  26. for private (file "static") routines, for consistency of kernel
  27. source code layout. But this is lower priority and at the
  28. discretion of the MAINTAINER of that kernel source file.
  29. Data structures visible in kernel include files should also be
  30. documented using kernel-doc formatted comments.
  31. The opening comment mark "/**" is reserved for kernel-doc comments.
  32. Only comments so marked will be considered by the kernel-doc scripts,
  33. and any comment so marked must be in kernel-doc format. Do not use
  34. "/**" to be begin a comment block unless the comment block contains
  35. kernel-doc formatted comments. The closing comment marker for
  36. kernel-doc comments can be either "*/" or "**/", but "*/" is
  37. preferred in the Linux kernel tree.
  38. Kernel-doc comments should be placed just before the function
  39. or data structure being described.
  40. Example kernel-doc function comment:
  41. /**
  42. * foobar() - short function description of foobar
  43. * @arg1: Describe the first argument to foobar.
  44. * @arg2: Describe the second argument to foobar.
  45. * One can provide multiple line descriptions
  46. * for arguments.
  47. *
  48. * A longer description, with more discussion of the function foobar()
  49. * that might be useful to those using or modifying it. Begins with
  50. * empty comment line, and may include additional embedded empty
  51. * comment lines.
  52. *
  53. * The longer description can have multiple paragraphs.
  54. */
  55. The short description following the subject can span multiple lines
  56. and ends with an @argument description, an empty line or the end of
  57. the comment block.
  58. The @argument descriptions must begin on the very next line following
  59. this opening short function description line, with no intervening
  60. empty comment lines.
  61. If a function parameter is "..." (varargs), it should be listed in
  62. kernel-doc notation as:
  63. * @...: description
  64. Example kernel-doc data structure comment.
  65. /**
  66. * struct blah - the basic blah structure
  67. * @mem1: describe the first member of struct blah
  68. * @mem2: describe the second member of struct blah,
  69. * perhaps with more lines and words.
  70. *
  71. * Longer description of this structure.
  72. */
  73. The kernel-doc function comments describe each parameter to the
  74. function, in order, with the @name lines.
  75. The kernel-doc data structure comments describe each structure member
  76. in the data structure, with the @name lines.
  77. The longer description formatting is "reflowed", losing your line
  78. breaks. So presenting carefully formatted lists within these
  79. descriptions won't work so well; derived documentation will lose
  80. the formatting.
  81. See the section below "How to add extractable documentation to your
  82. source files" for more details and notes on how to format kernel-doc
  83. comments.
  84. Components of the kernel-doc system
  85. -----------------------------------
  86. Many places in the source tree have extractable documentation in the
  87. form of block comments above functions. The components of this system
  88. are:
  89. - scripts/kernel-doc
  90. This is a perl script that hunts for the block comments and can mark
  91. them up directly into DocBook, man, text, and HTML. (No, not
  92. texinfo.)
  93. - Documentation/DocBook/*.tmpl
  94. These are SGML template files, which are normal SGML files with
  95. special place-holders for where the extracted documentation should
  96. go.
  97. - scripts/basic/docproc.c
  98. This is a program for converting SGML template files into SGML
  99. files. When a file is referenced it is searched for symbols
  100. exported (EXPORT_SYMBOL), to be able to distinguish between internal
  101. and external functions.
  102. It invokes kernel-doc, giving it the list of functions that
  103. are to be documented.
  104. Additionally it is used to scan the SGML template files to locate
  105. all the files referenced herein. This is used to generate dependency
  106. information as used by make.
  107. - Makefile
  108. The targets 'sgmldocs', 'psdocs', 'pdfdocs', and 'htmldocs' are used
  109. to build DocBook files, PostScript files, PDF files, and html files
  110. in Documentation/DocBook.
  111. - Documentation/DocBook/Makefile
  112. This is where C files are associated with SGML templates.
  113. How to extract the documentation
  114. --------------------------------
  115. If you just want to read the ready-made books on the various
  116. subsystems (see Documentation/DocBook/*.tmpl), just type 'make
  117. psdocs', or 'make pdfdocs', or 'make htmldocs', depending on your
  118. preference. If you would rather read a different format, you can type
  119. 'make sgmldocs' and then use DocBook tools to convert
  120. Documentation/DocBook/*.sgml to a format of your choice (for example,
  121. 'db2html ...' if 'make htmldocs' was not defined).
  122. If you want to see man pages instead, you can do this:
  123. $ cd linux
  124. $ scripts/kernel-doc -man $(find -name '*.c') | split-man.pl /tmp/man
  125. $ scripts/kernel-doc -man $(find -name '*.h') | split-man.pl /tmp/man
  126. Here is split-man.pl:
  127. -->
  128. #!/usr/bin/perl
  129. if ($#ARGV < 0) {
  130. die "where do I put the results?\n";
  131. }
  132. mkdir $ARGV[0],0777;
  133. $state = 0;
  134. while (<STDIN>) {
  135. if (/^\.TH \"[^\"]*\" 9 \"([^\"]*)\"/) {
  136. if ($state == 1) { close OUT }
  137. $state = 1;
  138. $fn = "$ARGV[0]/$1.9";
  139. print STDERR "Creating $fn\n";
  140. open OUT, ">$fn" or die "can't open $fn: $!\n";
  141. print OUT $_;
  142. } elsif ($state != 0) {
  143. print OUT $_;
  144. }
  145. }
  146. close OUT;
  147. <--
  148. If you just want to view the documentation for one function in one
  149. file, you can do this:
  150. $ scripts/kernel-doc -man -function fn file | nroff -man | less
  151. or this:
  152. $ scripts/kernel-doc -text -function fn file
  153. How to add extractable documentation to your source files
  154. ---------------------------------------------------------
  155. The format of the block comment is like this:
  156. /**
  157. * function_name(:)? (- short description)?
  158. (* @parameterx(space)*: (description of parameter x)?)*
  159. (* a blank line)?
  160. * (Description:)? (Description of function)?
  161. * (section header: (section description)? )*
  162. (*)?*/
  163. All "description" text can span multiple lines, although the
  164. function_name & its short description are traditionally on a single line.
  165. Description text may also contain blank lines (i.e., lines that contain
  166. only a "*").
  167. "section header:" names must be unique per function (or struct,
  168. union, typedef, enum).
  169. Avoid putting a spurious blank line after the function name, or else the
  170. description will be repeated!
  171. All descriptive text is further processed, scanning for the following special
  172. patterns, which are highlighted appropriately.
  173. 'funcname()' - function
  174. '$ENVVAR' - environment variable
  175. '&struct_name' - name of a structure (up to two words including 'struct')
  176. '@parameter' - name of a parameter
  177. '%CONST' - name of a constant.
  178. NOTE 1: The multi-line descriptive text you provide does *not* recognize
  179. line breaks, so if you try to format some text nicely, as in:
  180. Return codes
  181. 0 - cool
  182. 1 - invalid arg
  183. 2 - out of memory
  184. this will all run together and produce:
  185. Return codes 0 - cool 1 - invalid arg 2 - out of memory
  186. NOTE 2: If the descriptive text you provide has lines that begin with
  187. some phrase followed by a colon, each of those phrases will be taken as
  188. a new section heading, which means you should similarly try to avoid text
  189. like:
  190. Return codes:
  191. 0: cool
  192. 1: invalid arg
  193. 2: out of memory
  194. every line of which would start a new section. Again, probably not
  195. what you were after.
  196. Take a look around the source tree for examples.
  197. kernel-doc for structs, unions, enums, and typedefs
  198. ---------------------------------------------------
  199. Beside functions you can also write documentation for structs, unions,
  200. enums and typedefs. Instead of the function name you must write the name
  201. of the declaration; the struct/union/enum/typedef must always precede
  202. the name. Nesting of declarations is not supported.
  203. Use the argument mechanism to document members or constants.
  204. Inside a struct description, you can use the "private:" and "public:"
  205. comment tags. Structure fields that are inside a "private:" area
  206. are not listed in the generated output documentation. The "private:"
  207. and "public:" tags must begin immediately following a "/*" comment
  208. marker. They may optionally include comments between the ":" and the
  209. ending "*/" marker.
  210. Example:
  211. /**
  212. * struct my_struct - short description
  213. * @a: first member
  214. * @b: second member
  215. *
  216. * Longer description
  217. */
  218. struct my_struct {
  219. int a;
  220. int b;
  221. /* private: internal use only */
  222. int c;
  223. };
  224. Including documentation blocks in source files
  225. ----------------------------------------------
  226. To facilitate having source code and comments close together, you can
  227. include kernel-doc documentation blocks that are free-form comments
  228. instead of being kernel-doc for functions, structures, unions,
  229. enums, or typedefs. This could be used for something like a
  230. theory of operation for a driver or library code, for example.
  231. This is done by using a DOC: section keyword with a section title. E.g.:
  232. /**
  233. * DOC: Theory of Operation
  234. *
  235. * The whizbang foobar is a dilly of a gizmo. It can do whatever you
  236. * want it to do, at any time. It reads your mind. Here's how it works.
  237. *
  238. * foo bar splat
  239. *
  240. * The only drawback to this gizmo is that is can sometimes damage
  241. * hardware, software, or its subject(s).
  242. */
  243. DOC: sections are used in SGML templates files as indicated below.
  244. How to make new SGML template files
  245. -----------------------------------
  246. SGML template files (*.tmpl) are like normal SGML files, except that
  247. they can contain escape sequences where extracted documentation should
  248. be inserted.
  249. !E<filename> is replaced by the documentation, in <filename>, for
  250. functions that are exported using EXPORT_SYMBOL: the function list is
  251. collected from files listed in Documentation/DocBook/Makefile.
  252. !I<filename> is replaced by the documentation for functions that are
  253. _not_ exported using EXPORT_SYMBOL.
  254. !D<filename> is used to name additional files to search for functions
  255. exported using EXPORT_SYMBOL.
  256. !F<filename> <function [functions...]> is replaced by the
  257. documentation, in <filename>, for the functions listed.
  258. !P<filename> <section title> is replaced by the contents of the DOC:
  259. section titled <section title> from <filename>.
  260. Spaces are allowed in <section title>; do not quote the <section title>.
  261. !C<filename> is replaced by nothing, but makes the tools check that
  262. all DOC: sections and documented functions, symbols, etc. are used.
  263. This makes sense to use when you use !F/!P only and want to verify
  264. that all documentation is included.
  265. Tim.
  266. */ <twaugh@redhat.com>