kernel-doc-nano-HOWTO.txt 12 KB

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