kernel-doc-nano-HOWTO.txt 11 KB

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