kernel-doc.rst 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  1. .. title:: Kernel-doc comments
  2. ===========================
  3. Writing kernel-doc comments
  4. ===========================
  5. The Linux kernel source files may contain structured documentation
  6. comments in the kernel-doc format to describe the functions, types
  7. and design of the code. It is easier to keep documentation up-to-date
  8. when it is embedded in source files.
  9. .. note:: The kernel-doc format is deceptively similar to javadoc,
  10. gtk-doc or Doxygen, yet distinctively different, for historical
  11. reasons. The kernel source contains tens of thousands of kernel-doc
  12. comments. Please stick to the style described here.
  13. .. note:: kernel-doc does not cover Rust code: please see
  14. Documentation/rust/general-information.rst instead.
  15. The kernel-doc structure is extracted from the comments, and proper
  16. `Sphinx C Domain`_ function and type descriptions with anchors are
  17. generated from them. The descriptions are filtered for special kernel-doc
  18. highlights and cross-references. See below for details.
  19. .. _Sphinx C Domain: http://www.sphinx-doc.org/en/stable/domains.html
  20. Every function that is exported to loadable modules using
  21. ``EXPORT_SYMBOL`` or ``EXPORT_SYMBOL_GPL`` should have a kernel-doc
  22. comment. Functions and data structures in header files which are intended
  23. to be used by modules should also have kernel-doc comments.
  24. It is good practice to also provide kernel-doc formatted documentation
  25. for functions externally visible to other kernel files (not marked
  26. ``static``). We also recommend providing kernel-doc formatted
  27. documentation for private (file ``static``) routines, for consistency of
  28. kernel source code layout. This is lower priority and at the discretion
  29. of the maintainer of that kernel source file.
  30. How to format kernel-doc comments
  31. ---------------------------------
  32. The opening comment mark ``/**`` is used for kernel-doc comments. The
  33. ``kernel-doc`` tool will extract comments marked this way. The rest of
  34. the comment is formatted like a normal multi-line comment with a column
  35. of asterisks on the left side, closing with ``*/`` on a line by itself.
  36. The function and type kernel-doc comments should be placed just before
  37. the function or type being described in order to maximise the chance
  38. that somebody changing the code will also change the documentation. The
  39. overview kernel-doc comments may be placed anywhere at the top indentation
  40. level.
  41. Running the ``kernel-doc`` tool with increased verbosity and without actual
  42. output generation may be used to verify proper formatting of the
  43. documentation comments. For example::
  44. scripts/kernel-doc -v -none drivers/foo/bar.c
  45. The documentation format is verified by the kernel build when it is
  46. requested to perform extra gcc checks::
  47. make W=n
  48. Function documentation
  49. ----------------------
  50. The general format of a function and function-like macro kernel-doc comment is::
  51. /**
  52. * function_name() - Brief description of function.
  53. * @arg1: Describe the first argument.
  54. * @arg2: Describe the second argument.
  55. * One can provide multiple line descriptions
  56. * for arguments.
  57. *
  58. * A longer description, with more discussion of the function function_name()
  59. * that might be useful to those using or modifying it. Begins with an
  60. * empty comment line, and may include additional embedded empty
  61. * comment lines.
  62. *
  63. * The longer description may have multiple paragraphs.
  64. *
  65. * Context: Describes whether the function can sleep, what locks it takes,
  66. * releases, or expects to be held. It can extend over multiple
  67. * lines.
  68. * Return: Describe the return value of function_name.
  69. *
  70. * The return value description can also have multiple paragraphs, and should
  71. * be placed at the end of the comment block.
  72. */
  73. The brief description following the function name may span multiple lines, and
  74. ends with an argument description, a blank comment line, or the end of the
  75. comment block.
  76. Function parameters
  77. ~~~~~~~~~~~~~~~~~~~
  78. Each function argument should be described in order, immediately following
  79. the short function description. Do not leave a blank line between the
  80. function description and the arguments, nor between the arguments.
  81. Each ``@argument:`` description may span multiple lines.
  82. .. note::
  83. If the ``@argument`` description has multiple lines, the continuation
  84. of the description should start at the same column as the previous line::
  85. * @argument: some long description
  86. * that continues on next lines
  87. or::
  88. * @argument:
  89. * some long description
  90. * that continues on next lines
  91. If a function has a variable number of arguments, its description should
  92. be written in kernel-doc notation as::
  93. * @...: description
  94. Function context
  95. ~~~~~~~~~~~~~~~~
  96. The context in which a function can be called should be described in a
  97. section named ``Context``. This should include whether the function
  98. sleeps or can be called from interrupt context, as well as what locks
  99. it takes, releases and expects to be held by its caller.
  100. Examples::
  101. * Context: Any context.
  102. * Context: Any context. Takes and releases the RCU lock.
  103. * Context: Any context. Expects <lock> to be held by caller.
  104. * Context: Process context. May sleep if @gfp flags permit.
  105. * Context: Process context. Takes and releases <mutex>.
  106. * Context: Softirq or process context. Takes and releases <lock>, BH-safe.
  107. * Context: Interrupt context.
  108. Return values
  109. ~~~~~~~~~~~~~
  110. The return value, if any, should be described in a dedicated section
  111. named ``Return``.
  112. .. note::
  113. #) The multi-line descriptive text you provide does *not* recognize
  114. line breaks, so if you try to format some text nicely, as in::
  115. * Return:
  116. * 0 - OK
  117. * -EINVAL - invalid argument
  118. * -ENOMEM - out of memory
  119. this will all run together and produce::
  120. Return: 0 - OK -EINVAL - invalid argument -ENOMEM - out of memory
  121. So, in order to produce the desired line breaks, you need to use a
  122. ReST list, e. g.::
  123. * Return:
  124. * * 0 - OK to runtime suspend the device
  125. * * -EBUSY - Device should not be runtime suspended
  126. #) If the descriptive text you provide has lines that begin with
  127. some phrase followed by a colon, each of those phrases will be taken
  128. as a new section heading, which probably won't produce the desired
  129. effect.
  130. Structure, union, and enumeration documentation
  131. -----------------------------------------------
  132. The general format of a struct, union, and enum kernel-doc comment is::
  133. /**
  134. * struct struct_name - Brief description.
  135. * @member1: Description of member1.
  136. * @member2: Description of member2.
  137. * One can provide multiple line descriptions
  138. * for members.
  139. *
  140. * Description of the structure.
  141. */
  142. You can replace the ``struct`` in the above example with ``union`` or
  143. ``enum`` to describe unions or enums. ``member`` is used to mean struct
  144. and union member names as well as enumerations in an enum.
  145. The brief description following the structure name may span multiple
  146. lines, and ends with a member description, a blank comment line, or the
  147. end of the comment block.
  148. Members
  149. ~~~~~~~
  150. Members of structs, unions and enums should be documented the same way
  151. as function parameters; they immediately succeed the short description
  152. and may be multi-line.
  153. Inside a struct or union description, you can use the ``private:`` and
  154. ``public:`` comment tags. Structure fields that are inside a ``private:``
  155. area are not listed in the generated output documentation.
  156. The ``private:`` and ``public:`` tags must begin immediately following a
  157. ``/*`` comment marker. They may optionally include comments between the
  158. ``:`` and the ending ``*/`` marker.
  159. Example::
  160. /**
  161. * struct my_struct - short description
  162. * @a: first member
  163. * @b: second member
  164. * @d: fourth member
  165. *
  166. * Longer description
  167. */
  168. struct my_struct {
  169. int a;
  170. int b;
  171. /* private: internal use only */
  172. int c;
  173. /* public: the next one is public */
  174. int d;
  175. };
  176. Nested structs/unions
  177. ~~~~~~~~~~~~~~~~~~~~~
  178. It is possible to document nested structs and unions, like::
  179. /**
  180. * struct nested_foobar - a struct with nested unions and structs
  181. * @memb1: first member of anonymous union/anonymous struct
  182. * @memb2: second member of anonymous union/anonymous struct
  183. * @memb3: third member of anonymous union/anonymous struct
  184. * @memb4: fourth member of anonymous union/anonymous struct
  185. * @bar: non-anonymous union
  186. * @bar.st1: struct st1 inside @bar
  187. * @bar.st2: struct st2 inside @bar
  188. * @bar.st1.memb1: first member of struct st1 on union bar
  189. * @bar.st1.memb2: second member of struct st1 on union bar
  190. * @bar.st2.memb1: first member of struct st2 on union bar
  191. * @bar.st2.memb2: second member of struct st2 on union bar
  192. */
  193. struct nested_foobar {
  194. /* Anonymous union/struct*/
  195. union {
  196. struct {
  197. int memb1;
  198. int memb2;
  199. };
  200. struct {
  201. void *memb3;
  202. int memb4;
  203. };
  204. };
  205. union {
  206. struct {
  207. int memb1;
  208. int memb2;
  209. } st1;
  210. struct {
  211. void *memb1;
  212. int memb2;
  213. } st2;
  214. } bar;
  215. };
  216. .. note::
  217. #) When documenting nested structs or unions, if the struct/union ``foo``
  218. is named, the member ``bar`` inside it should be documented as
  219. ``@foo.bar:``
  220. #) When the nested struct/union is anonymous, the member ``bar`` in it
  221. should be documented as ``@bar:``
  222. In-line member documentation comments
  223. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  224. The structure members may also be documented in-line within the definition.
  225. There are two styles, single-line comments where both the opening ``/**`` and
  226. closing ``*/`` are on the same line, and multi-line comments where they are each
  227. on a line of their own, like all other kernel-doc comments::
  228. /**
  229. * struct foo - Brief description.
  230. * @foo: The Foo member.
  231. */
  232. struct foo {
  233. int foo;
  234. /**
  235. * @bar: The Bar member.
  236. */
  237. int bar;
  238. /**
  239. * @baz: The Baz member.
  240. *
  241. * Here, the member description may contain several paragraphs.
  242. */
  243. int baz;
  244. union {
  245. /** @foobar: Single line description. */
  246. int foobar;
  247. };
  248. /** @bar2: Description for struct @bar2 inside @foo */
  249. struct {
  250. /**
  251. * @bar2.barbar: Description for @barbar inside @foo.bar2
  252. */
  253. int barbar;
  254. } bar2;
  255. };
  256. Typedef documentation
  257. ---------------------
  258. The general format of a typedef kernel-doc comment is::
  259. /**
  260. * typedef type_name - Brief description.
  261. *
  262. * Description of the type.
  263. */
  264. Typedefs with function prototypes can also be documented::
  265. /**
  266. * typedef type_name - Brief description.
  267. * @arg1: description of arg1
  268. * @arg2: description of arg2
  269. *
  270. * Description of the type.
  271. *
  272. * Context: Locking context.
  273. * Return: Meaning of the return value.
  274. */
  275. typedef void (*type_name)(struct v4l2_ctrl *arg1, void *arg2);
  276. Highlights and cross-references
  277. -------------------------------
  278. The following special patterns are recognized in the kernel-doc comment
  279. descriptive text and converted to proper reStructuredText markup and `Sphinx C
  280. Domain`_ references.
  281. .. attention:: The below are **only** recognized within kernel-doc comments,
  282. **not** within normal reStructuredText documents.
  283. ``funcname()``
  284. Function reference.
  285. ``@parameter``
  286. Name of a function parameter. (No cross-referencing, just formatting.)
  287. ``%CONST``
  288. Name of a constant. (No cross-referencing, just formatting.)
  289. ````literal````
  290. A literal block that should be handled as-is. The output will use a
  291. ``monospaced font``.
  292. Useful if you need to use special characters that would otherwise have some
  293. meaning either by kernel-doc script or by reStructuredText.
  294. This is particularly useful if you need to use things like ``%ph`` inside
  295. a function description.
  296. ``$ENVVAR``
  297. Name of an environment variable. (No cross-referencing, just formatting.)
  298. ``&struct name``
  299. Structure reference.
  300. ``&enum name``
  301. Enum reference.
  302. ``&typedef name``
  303. Typedef reference.
  304. ``&struct_name->member`` or ``&struct_name.member``
  305. Structure or union member reference. The cross-reference will be to the struct
  306. or union definition, not the member directly.
  307. ``&name``
  308. A generic type reference. Prefer using the full reference described above
  309. instead. This is mostly for legacy comments.
  310. Cross-referencing from reStructuredText
  311. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  312. No additional syntax is needed to cross-reference the functions and types
  313. defined in the kernel-doc comments from reStructuredText documents.
  314. Just end function names with ``()`` and write ``struct``, ``union``, ``enum``
  315. or ``typedef`` before types.
  316. For example::
  317. See foo().
  318. See struct foo.
  319. See union bar.
  320. See enum baz.
  321. See typedef meh.
  322. However, if you want custom text in the cross-reference link, that can be done
  323. through the following syntax::
  324. See :c:func:`my custom link text for function foo <foo>`.
  325. See :c:type:`my custom link text for struct bar <bar>`.
  326. For further details, please refer to the `Sphinx C Domain`_ documentation.
  327. Overview documentation comments
  328. -------------------------------
  329. To facilitate having source code and comments close together, you can include
  330. kernel-doc documentation blocks that are free-form comments instead of being
  331. kernel-doc for functions, structures, unions, enums, or typedefs. This could be
  332. used for something like a theory of operation for a driver or library code, for
  333. example.
  334. This is done by using a ``DOC:`` section keyword with a section title.
  335. The general format of an overview or high-level documentation comment is::
  336. /**
  337. * DOC: Theory of Operation
  338. *
  339. * The whizbang foobar is a dilly of a gizmo. It can do whatever you
  340. * want it to do, at any time. It reads your mind. Here's how it works.
  341. *
  342. * foo bar splat
  343. *
  344. * The only drawback to this gizmo is that is can sometimes damage
  345. * hardware, software, or its subject(s).
  346. */
  347. The title following ``DOC:`` acts as a heading within the source file, but also
  348. as an identifier for extracting the documentation comment. Thus, the title must
  349. be unique within the file.
  350. =============================
  351. Including kernel-doc comments
  352. =============================
  353. The documentation comments may be included in any of the reStructuredText
  354. documents using a dedicated kernel-doc Sphinx directive extension.
  355. The kernel-doc directive is of the format::
  356. .. kernel-doc:: source
  357. :option:
  358. The *source* is the path to a source file, relative to the kernel source
  359. tree. The following directive options are supported:
  360. export: *[source-pattern ...]*
  361. Include documentation for all functions in *source* that have been exported
  362. using ``EXPORT_SYMBOL`` or ``EXPORT_SYMBOL_GPL`` either in *source* or in any
  363. of the files specified by *source-pattern*.
  364. The *source-pattern* is useful when the kernel-doc comments have been placed
  365. in header files, while ``EXPORT_SYMBOL`` and ``EXPORT_SYMBOL_GPL`` are next to
  366. the function definitions.
  367. Examples::
  368. .. kernel-doc:: lib/bitmap.c
  369. :export:
  370. .. kernel-doc:: include/net/mac80211.h
  371. :export: net/mac80211/*.c
  372. internal: *[source-pattern ...]*
  373. Include documentation for all functions and types in *source* that have
  374. **not** been exported using ``EXPORT_SYMBOL`` or ``EXPORT_SYMBOL_GPL`` either
  375. in *source* or in any of the files specified by *source-pattern*.
  376. Example::
  377. .. kernel-doc:: drivers/gpu/drm/i915/intel_audio.c
  378. :internal:
  379. identifiers: *[ function/type ...]*
  380. Include documentation for each *function* and *type* in *source*.
  381. If no *function* is specified, the documentation for all functions
  382. and types in the *source* will be included.
  383. Examples::
  384. .. kernel-doc:: lib/bitmap.c
  385. :identifiers: bitmap_parselist bitmap_parselist_user
  386. .. kernel-doc:: lib/idr.c
  387. :identifiers:
  388. no-identifiers: *[ function/type ...]*
  389. Exclude documentation for each *function* and *type* in *source*.
  390. Example::
  391. .. kernel-doc:: lib/bitmap.c
  392. :no-identifiers: bitmap_parselist
  393. functions: *[ function/type ...]*
  394. This is an alias of the 'identifiers' directive and deprecated.
  395. doc: *title*
  396. Include documentation for the ``DOC:`` paragraph identified by *title* in
  397. *source*. Spaces are allowed in *title*; do not quote the *title*. The *title*
  398. is only used as an identifier for the paragraph, and is not included in the
  399. output. Please make sure to have an appropriate heading in the enclosing
  400. reStructuredText document.
  401. Example::
  402. .. kernel-doc:: drivers/gpu/drm/i915/intel_audio.c
  403. :doc: High Definition Audio over HDMI and Display Port
  404. Without options, the kernel-doc directive includes all documentation comments
  405. from the source file.
  406. The kernel-doc extension is included in the kernel source tree, at
  407. ``Documentation/sphinx/kerneldoc.py``. Internally, it uses the
  408. ``scripts/kernel-doc`` script to extract the documentation comments from the
  409. source.
  410. .. _kernel_doc:
  411. How to use kernel-doc to generate man pages
  412. -------------------------------------------
  413. If you just want to use kernel-doc to generate man pages you can do this
  414. from the kernel git tree::
  415. $ scripts/kernel-doc -man \
  416. $(git grep -l '/\*\*' -- :^Documentation :^tools) \
  417. | scripts/split-man.pl /tmp/man
  418. Some older versions of git do not support some of the variants of syntax for
  419. path exclusion. One of the following commands may work for those versions::
  420. $ scripts/kernel-doc -man \
  421. $(git grep -l '/\*\*' -- . ':!Documentation' ':!tools') \
  422. | scripts/split-man.pl /tmp/man
  423. $ scripts/kernel-doc -man \
  424. $(git grep -l '/\*\*' -- . ":(exclude)Documentation" ":(exclude)tools") \
  425. | scripts/split-man.pl /tmp/man