kernel-doc-nano-HOWTO.txt 12 KB

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