kernel-doc-nano-HOWTO.txt 9.7 KB

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