brintos

brintos / linux-shallow public Read only

0
0
Text · 19.0 KiB · e6ffd59 Raw
598 lines · plain
1.. title:: Kernel-doc comments2 3===========================4Writing kernel-doc comments5===========================6 7The Linux kernel source files may contain structured documentation8comments in the kernel-doc format to describe the functions, types9and design of the code. It is easier to keep documentation up-to-date10when it is embedded in source files.11 12.. note:: The kernel-doc format is deceptively similar to javadoc,13   gtk-doc or Doxygen, yet distinctively different, for historical14   reasons. The kernel source contains tens of thousands of kernel-doc15   comments. Please stick to the style described here.16 17.. note:: kernel-doc does not cover Rust code: please see18   Documentation/rust/general-information.rst instead.19 20The kernel-doc structure is extracted from the comments, and proper21`Sphinx C Domain`_ function and type descriptions with anchors are22generated from them. The descriptions are filtered for special kernel-doc23highlights and cross-references. See below for details.24 25.. _Sphinx C Domain: http://www.sphinx-doc.org/en/stable/domains.html26 27Every function that is exported to loadable modules using28``EXPORT_SYMBOL`` or ``EXPORT_SYMBOL_GPL`` should have a kernel-doc29comment. Functions and data structures in header files which are intended30to be used by modules should also have kernel-doc comments.31 32It is good practice to also provide kernel-doc formatted documentation33for functions externally visible to other kernel files (not marked34``static``). We also recommend providing kernel-doc formatted35documentation for private (file ``static``) routines, for consistency of36kernel source code layout. This is lower priority and at the discretion37of the maintainer of that kernel source file.38 39How to format kernel-doc comments40---------------------------------41 42The opening comment mark ``/**`` is used for kernel-doc comments. The43``kernel-doc`` tool will extract comments marked this way. The rest of44the comment is formatted like a normal multi-line comment with a column45of asterisks on the left side, closing with ``*/`` on a line by itself.46 47The function and type kernel-doc comments should be placed just before48the function or type being described in order to maximise the chance49that somebody changing the code will also change the documentation. The50overview kernel-doc comments may be placed anywhere at the top indentation51level.52 53Running the ``kernel-doc`` tool with increased verbosity and without actual54output generation may be used to verify proper formatting of the55documentation comments. For example::56 57	scripts/kernel-doc -v -none drivers/foo/bar.c58 59The documentation format is verified by the kernel build when it is60requested to perform extra gcc checks::61 62	make W=n63 64Function documentation65----------------------66 67The general format of a function and function-like macro kernel-doc comment is::68 69  /**70   * function_name() - Brief description of function.71   * @arg1: Describe the first argument.72   * @arg2: Describe the second argument.73   *        One can provide multiple line descriptions74   *        for arguments.75   *76   * A longer description, with more discussion of the function function_name()77   * that might be useful to those using or modifying it. Begins with an78   * empty comment line, and may include additional embedded empty79   * comment lines.80   *81   * The longer description may have multiple paragraphs.82   *83   * Context: Describes whether the function can sleep, what locks it takes,84   *          releases, or expects to be held. It can extend over multiple85   *          lines.86   * Return: Describe the return value of function_name.87   *88   * The return value description can also have multiple paragraphs, and should89   * be placed at the end of the comment block.90   */91 92The brief description following the function name may span multiple lines, and93ends with an argument description, a blank comment line, or the end of the94comment block.95 96Function parameters97~~~~~~~~~~~~~~~~~~~98 99Each function argument should be described in order, immediately following100the short function description.  Do not leave a blank line between the101function description and the arguments, nor between the arguments.102 103Each ``@argument:`` description may span multiple lines.104 105.. note::106 107   If the ``@argument`` description has multiple lines, the continuation108   of the description should start at the same column as the previous line::109 110      * @argument: some long description111      *            that continues on next lines112 113   or::114 115      * @argument:116      *		some long description117      *		that continues on next lines118 119If a function has a variable number of arguments, its description should120be written in kernel-doc notation as::121 122      * @...: description123 124Function context125~~~~~~~~~~~~~~~~126 127The context in which a function can be called should be described in a128section named ``Context``. This should include whether the function129sleeps or can be called from interrupt context, as well as what locks130it takes, releases and expects to be held by its caller.131 132Examples::133 134  * Context: Any context.135  * Context: Any context. Takes and releases the RCU lock.136  * Context: Any context. Expects <lock> to be held by caller.137  * Context: Process context. May sleep if @gfp flags permit.138  * Context: Process context. Takes and releases <mutex>.139  * Context: Softirq or process context. Takes and releases <lock>, BH-safe.140  * Context: Interrupt context.141 142Return values143~~~~~~~~~~~~~144 145The return value, if any, should be described in a dedicated section146named ``Return`` (or ``Returns``).147 148.. note::149 150  #) The multi-line descriptive text you provide does *not* recognize151     line breaks, so if you try to format some text nicely, as in::152 153	* Return:154	* %0 - OK155	* %-EINVAL - invalid argument156	* %-ENOMEM - out of memory157 158     this will all run together and produce::159 160	Return: 0 - OK -EINVAL - invalid argument -ENOMEM - out of memory161 162     So, in order to produce the desired line breaks, you need to use a163     ReST list, e. g.::164 165      * Return:166      * * %0		- OK to runtime suspend the device167      * * %-EBUSY	- Device should not be runtime suspended168 169  #) If the descriptive text you provide has lines that begin with170     some phrase followed by a colon, each of those phrases will be taken171     as a new section heading, which probably won't produce the desired172     effect.173 174Structure, union, and enumeration documentation175-----------------------------------------------176 177The general format of a struct, union, and enum kernel-doc comment is::178 179  /**180   * struct struct_name - Brief description.181   * @member1: Description of member1.182   * @member2: Description of member2.183   *           One can provide multiple line descriptions184   *           for members.185   *186   * Description of the structure.187   */188 189You can replace the ``struct`` in the above example with ``union`` or190``enum``  to describe unions or enums. ``member`` is used to mean struct191and union member names as well as enumerations in an enum.192 193The brief description following the structure name may span multiple194lines, and ends with a member description, a blank comment line, or the195end of the comment block.196 197Members198~~~~~~~199 200Members of structs, unions and enums should be documented the same way201as function parameters; they immediately succeed the short description202and may be multi-line.203 204Inside a struct or union description, you can use the ``private:`` and205``public:`` comment tags. Structure fields that are inside a ``private:``206area are not listed in the generated output documentation.207 208The ``private:`` and ``public:`` tags must begin immediately following a209``/*`` comment marker. They may optionally include comments between the210``:`` and the ending ``*/`` marker.211 212Example::213 214  /**215   * struct my_struct - short description216   * @a: first member217   * @b: second member218   * @d: fourth member219   *220   * Longer description221   */222  struct my_struct {223      int a;224      int b;225  /* private: internal use only */226      int c;227  /* public: the next one is public */228      int d;229  };230 231Nested structs/unions232~~~~~~~~~~~~~~~~~~~~~233 234It is possible to document nested structs and unions, like::235 236      /**237       * struct nested_foobar - a struct with nested unions and structs238       * @memb1: first member of anonymous union/anonymous struct239       * @memb2: second member of anonymous union/anonymous struct240       * @memb3: third member of anonymous union/anonymous struct241       * @memb4: fourth member of anonymous union/anonymous struct242       * @bar: non-anonymous union243       * @bar.st1: struct st1 inside @bar244       * @bar.st2: struct st2 inside @bar245       * @bar.st1.memb1: first member of struct st1 on union bar246       * @bar.st1.memb2: second member of struct st1 on union bar247       * @bar.st2.memb1: first member of struct st2 on union bar248       * @bar.st2.memb2: second member of struct st2 on union bar249       */250      struct nested_foobar {251        /* Anonymous union/struct*/252        union {253          struct {254            int memb1;255            int memb2;256          };257          struct {258            void *memb3;259            int memb4;260          };261        };262        union {263          struct {264            int memb1;265            int memb2;266          } st1;267          struct {268            void *memb1;269            int memb2;270          } st2;271        } bar;272      };273 274.. note::275 276   #) When documenting nested structs or unions, if the struct/union ``foo``277      is named, the member ``bar`` inside it should be documented as278      ``@foo.bar:``279   #) When the nested struct/union is anonymous, the member ``bar`` in it280      should be documented as ``@bar:``281 282In-line member documentation comments283~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~284 285The structure members may also be documented in-line within the definition.286There are two styles, single-line comments where both the opening ``/**`` and287closing ``*/`` are on the same line, and multi-line comments where they are each288on a line of their own, like all other kernel-doc comments::289 290  /**291   * struct foo - Brief description.292   * @foo: The Foo member.293   */294  struct foo {295        int foo;296        /**297         * @bar: The Bar member.298         */299        int bar;300        /**301         * @baz: The Baz member.302         *303         * Here, the member description may contain several paragraphs.304         */305        int baz;306        union {307                /** @foobar: Single line description. */308                int foobar;309        };310        /** @bar2: Description for struct @bar2 inside @foo */311        struct {312                /**313                 * @bar2.barbar: Description for @barbar inside @foo.bar2314                 */315                int barbar;316        } bar2;317  };318 319Typedef documentation320---------------------321 322The general format of a typedef kernel-doc comment is::323 324  /**325   * typedef type_name - Brief description.326   *327   * Description of the type.328   */329 330Typedefs with function prototypes can also be documented::331 332  /**333   * typedef type_name - Brief description.334   * @arg1: description of arg1335   * @arg2: description of arg2336   *337   * Description of the type.338   *339   * Context: Locking context.340   * Returns: Meaning of the return value.341   */342   typedef void (*type_name)(struct v4l2_ctrl *arg1, void *arg2);343 344Object-like macro documentation345-------------------------------346 347Object-like macros are distinct from function-like macros. They are348differentiated by whether the macro name is immediately followed by a349left parenthesis ('(') for function-like macros or not followed by one350for object-like macros.351 352Function-like macros are handled like functions by ``scripts/kernel-doc``.353They may have a parameter list. Object-like macros have do not have a354parameter list.355 356The general format of an object-like macro kernel-doc comment is::357 358  /**359   * define object_name - Brief description.360   *361   * Description of the object.362   */363 364Example::365 366  /**367   * define MAX_ERRNO - maximum errno value that is supported368   *369   * Kernel pointers have redundant information, so we can use a370   * scheme where we can return either an error code or a normal371   * pointer with the same return value.372   */373  #define MAX_ERRNO	4095374 375Example::376 377  /**378   * define DRM_GEM_VRAM_PLANE_HELPER_FUNCS - \379   *	Initializes struct drm_plane_helper_funcs for VRAM handling380   *381   * This macro initializes struct drm_plane_helper_funcs to use the382   * respective helper functions.383   */384  #define DRM_GEM_VRAM_PLANE_HELPER_FUNCS \385	.prepare_fb = drm_gem_vram_plane_helper_prepare_fb, \386	.cleanup_fb = drm_gem_vram_plane_helper_cleanup_fb387 388 389Highlights and cross-references390-------------------------------391 392The following special patterns are recognized in the kernel-doc comment393descriptive text and converted to proper reStructuredText markup and `Sphinx C394Domain`_ references.395 396.. attention:: The below are **only** recognized within kernel-doc comments,397	       **not** within normal reStructuredText documents.398 399``funcname()``400  Function reference.401 402``@parameter``403  Name of a function parameter. (No cross-referencing, just formatting.)404 405``%CONST``406  Name of a constant. (No cross-referencing, just formatting.)407 408````literal````409  A literal block that should be handled as-is. The output will use a410  ``monospaced font``.411 412  Useful if you need to use special characters that would otherwise have some413  meaning either by kernel-doc script or by reStructuredText.414 415  This is particularly useful if you need to use things like ``%ph`` inside416  a function description.417 418``$ENVVAR``419  Name of an environment variable. (No cross-referencing, just formatting.)420 421``&struct name``422  Structure reference.423 424``&enum name``425  Enum reference.426 427``&typedef name``428  Typedef reference.429 430``&struct_name->member`` or ``&struct_name.member``431  Structure or union member reference. The cross-reference will be to the struct432  or union definition, not the member directly.433 434``&name``435  A generic type reference. Prefer using the full reference described above436  instead. This is mostly for legacy comments.437 438Cross-referencing from reStructuredText439~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~440 441No additional syntax is needed to cross-reference the functions and types442defined in the kernel-doc comments from reStructuredText documents.443Just end function names with ``()`` and write ``struct``, ``union``, ``enum``444or ``typedef`` before types.445For example::446 447  See foo().448  See struct foo.449  See union bar.450  See enum baz.451  See typedef meh.452 453However, if you want custom text in the cross-reference link, that can be done454through the following syntax::455 456  See :c:func:`my custom link text for function foo <foo>`.457  See :c:type:`my custom link text for struct bar <bar>`.458 459For further details, please refer to the `Sphinx C Domain`_ documentation.460 461Overview documentation comments462-------------------------------463 464To facilitate having source code and comments close together, you can include465kernel-doc documentation blocks that are free-form comments instead of being466kernel-doc for functions, structures, unions, enums, or typedefs. This could be467used for something like a theory of operation for a driver or library code, for468example.469 470This is done by using a ``DOC:`` section keyword with a section title.471 472The general format of an overview or high-level documentation comment is::473 474  /**475   * DOC: Theory of Operation476   *477   * The whizbang foobar is a dilly of a gizmo. It can do whatever you478   * want it to do, at any time. It reads your mind. Here's how it works.479   *480   * foo bar splat481   *482   * The only drawback to this gizmo is that is can sometimes damage483   * hardware, software, or its subject(s).484   */485 486The title following ``DOC:`` acts as a heading within the source file, but also487as an identifier for extracting the documentation comment. Thus, the title must488be unique within the file.489 490=============================491Including kernel-doc comments492=============================493 494The documentation comments may be included in any of the reStructuredText495documents using a dedicated kernel-doc Sphinx directive extension.496 497The kernel-doc directive is of the format::498 499  .. kernel-doc:: source500     :option:501 502The *source* is the path to a source file, relative to the kernel source503tree. The following directive options are supported:504 505export: *[source-pattern ...]*506  Include documentation for all functions in *source* that have been exported507  using ``EXPORT_SYMBOL`` or ``EXPORT_SYMBOL_GPL`` either in *source* or in any508  of the files specified by *source-pattern*.509 510  The *source-pattern* is useful when the kernel-doc comments have been placed511  in header files, while ``EXPORT_SYMBOL`` and ``EXPORT_SYMBOL_GPL`` are next to512  the function definitions.513 514  Examples::515 516    .. kernel-doc:: lib/bitmap.c517       :export:518 519    .. kernel-doc:: include/net/mac80211.h520       :export: net/mac80211/*.c521 522internal: *[source-pattern ...]*523  Include documentation for all functions and types in *source* that have524  **not** been exported using ``EXPORT_SYMBOL`` or ``EXPORT_SYMBOL_GPL`` either525  in *source* or in any of the files specified by *source-pattern*.526 527  Example::528 529    .. kernel-doc:: drivers/gpu/drm/i915/intel_audio.c530       :internal:531 532identifiers: *[ function/type ...]*533  Include documentation for each *function* and *type* in *source*.534  If no *function* is specified, the documentation for all functions535  and types in the *source* will be included.536 537  Examples::538 539    .. kernel-doc:: lib/bitmap.c540       :identifiers: bitmap_parselist bitmap_parselist_user541 542    .. kernel-doc:: lib/idr.c543       :identifiers:544 545no-identifiers: *[ function/type ...]*546  Exclude documentation for each *function* and *type* in *source*.547 548  Example::549 550    .. kernel-doc:: lib/bitmap.c551       :no-identifiers: bitmap_parselist552 553functions: *[ function/type ...]*554  This is an alias of the 'identifiers' directive and deprecated.555 556doc: *title*557  Include documentation for the ``DOC:`` paragraph identified by *title* in558  *source*. Spaces are allowed in *title*; do not quote the *title*. The *title*559  is only used as an identifier for the paragraph, and is not included in the560  output. Please make sure to have an appropriate heading in the enclosing561  reStructuredText document.562 563  Example::564 565    .. kernel-doc:: drivers/gpu/drm/i915/intel_audio.c566       :doc: High Definition Audio over HDMI and Display Port567 568Without options, the kernel-doc directive includes all documentation comments569from the source file.570 571The kernel-doc extension is included in the kernel source tree, at572``Documentation/sphinx/kerneldoc.py``. Internally, it uses the573``scripts/kernel-doc`` script to extract the documentation comments from the574source.575 576.. _kernel_doc:577 578How to use kernel-doc to generate man pages579-------------------------------------------580 581If you just want to use kernel-doc to generate man pages you can do this582from the kernel git tree::583 584  $ scripts/kernel-doc -man \585    $(git grep -l '/\*\*' -- :^Documentation :^tools) \586    | scripts/split-man.pl /tmp/man587 588Some older versions of git do not support some of the variants of syntax for589path exclusion.  One of the following commands may work for those versions::590 591  $ scripts/kernel-doc -man \592    $(git grep -l '/\*\*' -- . ':!Documentation' ':!tools') \593    | scripts/split-man.pl /tmp/man594 595  $ scripts/kernel-doc -man \596    $(git grep -l '/\*\*' -- . ":(exclude)Documentation" ":(exclude)tools") \597    | scripts/split-man.pl /tmp/man598