kernel-doc-nano-HOWTO.txt 11 KB

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