|
| 1 | +Documenting VTR Code with Doxygen |
| 2 | +================================= |
| 3 | + |
| 4 | +VTR uses Doxygen and Sphinx for generating code documentation. Doxygen is used |
| 5 | +to parse a codebase, extract code comments, and save them into an XML file. |
| 6 | +The XML is then read by the Sphinx Breathe plugin, which converts it |
| 7 | +to an HTML available publicly in the project documentation. The documentation |
| 8 | +generated with Sphinx can be found in the API Reference section. |
| 9 | + |
| 10 | +This note presents how to document source code in the VTR project |
| 11 | +and check whether Doxygen can parse the created description. Code |
| 12 | +conventions presented below were chosen arbitrarily for the project, |
| 13 | +from many more options available in Doxygen. To read more about the tool, |
| 14 | +visit the official `Doxygen documentation`_. |
| 15 | + |
| 16 | +Documenting Code |
| 17 | +---------------- |
| 18 | + |
| 19 | +There are three basic types of Doxygen code comments used in the VTR documentation: |
| 20 | + |
| 21 | +- block comments |
| 22 | +- one-line comments before a code element |
| 23 | +- one-line comments after an element member |
| 24 | + |
| 25 | +In most cases, a piece of documentation should be placed before a code |
| 26 | +element. Comments after an element should be used only for documenting |
| 27 | +members of enumeration types, structures, and unions. |
| 28 | + |
| 29 | +Block Comments |
| 30 | +++++++++++++++ |
| 31 | + |
| 32 | +You should use Doxygen block comments with both brief and detailed |
| 33 | +descriptions to document code by default. As the name suggests, a brief |
| 34 | +description should be a one-liner with general information about |
| 35 | +the code element. A detailed description provides more specific |
| 36 | +information about the element, its usage, or implementation details. |
| 37 | +In the case of functions and methods, information about parameters and |
| 38 | +returned value comes after the detailed description. Note that brief |
| 39 | +and detailed descriptions have to be separated with at least one empty line. |
| 40 | + |
| 41 | +Here is an example of a Doxygen block comment: |
| 42 | + |
| 43 | +.. code-block:: c |
| 44 | +
|
| 45 | + /** |
| 46 | + * @brief This is a brief function description |
| 47 | + * |
| 48 | + * This is a detailed description. It should be separated from |
| 49 | + * the brief description with one blank line. |
| 50 | + * |
| 51 | + * @param a A description of a |
| 52 | + * @param b A description of b |
| 53 | + * |
| 54 | + * @return A return value description |
| 55 | + */ |
| 56 | + int my_func(int a, int b) { |
| 57 | + return a + b; |
| 58 | + } |
| 59 | +
|
| 60 | +General guidelines for using Doxygen block comments: |
| 61 | + |
| 62 | +#. A block-comment block **has to** start with the ``/**``, otherwise |
| 63 | + Doxygen will not recognize it. All the comment lines **have to** be |
| 64 | + preceded by an asterisk. All the asterisks **have to** create a straight |
| 65 | + vertical line. |
| 66 | + |
| 67 | +#. Brief and detailed descriptions **have to** be separated with one |
| 68 | + empty line. |
| 69 | + |
| 70 | +#. A detailed description and a parameter list **should be** separated with |
| 71 | + one empty line. |
| 72 | + |
| 73 | +#. A parameter list **should be** indented one level. All the parameter |
| 74 | + descriptions should be aligned together. |
| 75 | + |
| 76 | +#. A returned value description should be separated with one empty line |
| 77 | + from either a detailed or a parameter description. |
| 78 | + |
| 79 | +One-line Comments Before an Element |
| 80 | +++++++++++++++++++++++++++++++++++++ |
| 81 | + |
| 82 | +One-line comments can be used instead of the block comments described above, |
| 83 | +only if a brief description is sufficient for documenting the particular code |
| 84 | +element. Usually, this is the case with global variables and defines. |
| 85 | + |
| 86 | +Here is an example of a one-line Doxygen comment (before a code element): |
| 87 | + |
| 88 | +.. code-block:: c |
| 89 | +
|
| 90 | + /// @brief This is one-line documentation comment |
| 91 | + int var = 0; |
| 92 | +
|
| 93 | +General guidelines for using Doxygen one-line comments (before a code element): |
| 94 | + |
| 95 | +#. A one-line comment before an element **has to** start with ``///``, |
| 96 | + otherwise Doxygen will not recognize it. |
| 97 | + |
| 98 | +#. Since this style of code comments **should be** used only for |
| 99 | + brief descriptions, it **should** contain a ``@brief`` tag. |
| 100 | + |
| 101 | +#. One-line comments **should not** be overused. They are acceptable for |
| 102 | + single variables and defines, but more complicated elements like classes and |
| 103 | + structures should be documented more carefully with Doxygen block |
| 104 | + comments. |
| 105 | + |
| 106 | +One-line Comments After an Element Member |
| 107 | +++++++++++++++++++++++++++++++++++++++++++ |
| 108 | + |
| 109 | +There is another type of one-line code comments used to document members |
| 110 | +of enumeration types, structures, and unions. In those situations, the whole |
| 111 | +element should be documented in a standard way using a Doxygen block comment. |
| 112 | +However, the particular element members should be described after |
| 113 | +they appear in the code with the one-line comments. |
| 114 | + |
| 115 | +Here is an example of a one-line Doxygen comment (after an element member): |
| 116 | + |
| 117 | +.. code-block:: c |
| 118 | +
|
| 119 | + /** |
| 120 | + * @brief This is a brief enum description |
| 121 | + * |
| 122 | + * This is a detailed description. It should be separated from |
| 123 | + * the brief description with one blank line |
| 124 | + */ |
| 125 | + enum seasons { |
| 126 | + spring = 3, ///< Describes spring enum value |
| 127 | + summer, ///< Describes summer enum value |
| 128 | + autumn = 7, ///< Describes autumn enum value |
| 129 | + winter ///< Describes winter enum value |
| 130 | + }; |
| 131 | +
|
| 132 | +General guidelines for using Doxygen one-line comments (after an element member): |
| 133 | + |
| 134 | +#. One-line code comment after an element member **has to** start with |
| 135 | + ``///<``, otherwise Doxygen will not recognize it. |
| 136 | + |
| 137 | +#. This comment style **should be** used together with a Doxygen block |
| 138 | + comment for describing the whole element, before the members' description. |
| 139 | + |
| 140 | +Documenting Files |
| 141 | +----------------- |
| 142 | + |
| 143 | +All files that contain the source code should be documented with |
| 144 | +a Doxygen-style header. The file description in Doxygen is similar to |
| 145 | +code element description, and should be placed at the beginning of the file. |
| 146 | +The comment should contain information about an author, date of the document |
| 147 | +creation, and a description of functionalities introduced in the file. |
| 148 | + |
| 149 | +Here is an example of file documentation: |
| 150 | + |
| 151 | +.. code-block:: c |
| 152 | +
|
| 153 | + /** |
| 154 | + * @file |
| 155 | + * @author John Doe |
| 156 | + * @date 2020-09-03 |
| 157 | + * @brief This is a brief document description |
| 158 | + * |
| 159 | + * This is a detailed description. It should be separated from |
| 160 | + * the brief description with one blank line |
| 161 | + */ |
| 162 | +
|
| 163 | +General suggestions about a Doxygen file comments: |
| 164 | + |
| 165 | +#. A file comment **has to** start with the ``@file`` tag, |
| 166 | + otherwise it will not be recognized by Doxygen. |
| 167 | + |
| 168 | +#. The ``@file``, ``@author``, ``@date``, and ``@brief`` tags **should** form |
| 169 | + a single group of elements. A detailed description (if available) |
| 170 | + **has to** be placed one empty line after the brief description. |
| 171 | + |
| 172 | +#. A file comment **should** consist of at least the ``@file`` and ``@brief`` |
| 173 | + tags. |
| 174 | + |
| 175 | +Validation of Doxygen Comments (Updating API Reference) |
| 176 | +------------------------------------------------------- |
| 177 | + |
| 178 | +Validation of Doxygen code comments might be time-consuming since it |
| 179 | +requires setting the whole Doxygen project using Doxygen configuration |
| 180 | +files (doxyfiles). One solution to that problem is to use the configuration |
| 181 | +created for generating the official VTR documentation. The following steps |
| 182 | +will show you how to add new code comments to the Sphinx API Reference, |
| 183 | +available in the VTR documentation: |
| 184 | + |
| 185 | +#. Ensure that the documented project has a doxyfile, and it is added to |
| 186 | + breathe configuration. All the doxyfiles used by the Sphinx documentation |
| 187 | + are placed in ``<vtr_root>/doc/_doxygen`` (For details, check |
| 188 | + :doc:`Sphinx API Documentation for C/C++ Projects <c_api_doc>`) |
| 189 | + This will ensure that Doxygen XMLs will be created for that project |
| 190 | + during the Sphinx documentation building process. |
| 191 | + |
| 192 | +#. Check that the ``<vtr_root>/doc/src/api/<project_name>`` directory with |
| 193 | + a ``index.rst`` file exists. If not, create both the directory and the |
| 194 | + index file. Here is an example of the ``index.rst`` file for the VPR project. |
| 195 | + |
| 196 | + .. code-block:: rst |
| 197 | +
|
| 198 | + VPR API |
| 199 | + ======= |
| 200 | +
|
| 201 | + .. toctree:: |
| 202 | + :maxdepth: 1 |
| 203 | +
|
| 204 | + contexts |
| 205 | + netlist |
| 206 | +
|
| 207 | + .. note:: |
| 208 | + |
| 209 | + Do not forget about adding the index file title. The ``====`` marks |
| 210 | + should be of the same length as the title. |
| 211 | + |
| 212 | +#. Create a RST file, which will contain the references to the Doxygen |
| 213 | + code comments. Sphinx uses the Breathe plugin for extracting Doxygen |
| 214 | + comments from the generated XML files. The simplest check can be done by |
| 215 | + dumping all the Doxygen comments from the single file with |
| 216 | + a ``..doxygenfile ::`` directive. |
| 217 | + |
| 218 | + Assuming that your RST file name is ``myrst.rst``, and you created it to check |
| 219 | + the Doxygen comments in the ``mycode.cpp`` file within the ``vpr`` project, |
| 220 | + the contents of the file might be the following: |
| 221 | + |
| 222 | + .. code-block:: rst |
| 223 | +
|
| 224 | + ===== |
| 225 | + MyRST |
| 226 | + ===== |
| 227 | +
|
| 228 | + .. doxygenfile:: mycode.cpp |
| 229 | + :project: vpr |
| 230 | +
|
| 231 | + .. note:: |
| 232 | + |
| 233 | + A complete list of Breathe directives can be found in the |
| 234 | + `Breathe documentation`_ |
| 235 | + |
| 236 | +#. Add the newly created RST file to the ``index.rst``. In this example, that |
| 237 | + will lead to the following change in the ``index.rst``: |
| 238 | + |
| 239 | + .. code-block:: rst |
| 240 | +
|
| 241 | + VPR API |
| 242 | + ======= |
| 243 | +
|
| 244 | + .. toctree:: |
| 245 | + :maxdepth: 1 |
| 246 | +
|
| 247 | + contexts |
| 248 | + netlist |
| 249 | + myrst |
| 250 | +
|
| 251 | +#. Generate the Sphinx documentation by using ``make html`` command inside |
| 252 | + the ``<vtr_root>/doc/`` directory. |
| 253 | + |
| 254 | +#. The new section should be available in the API Reference. To verify that |
| 255 | + open the ``<vtr_root>/doc/_build/html/index.html`` with your browser and |
| 256 | + check the API Reference section. If the introduced code comments are |
| 257 | + unavailable, you can analyze the Sphinx build log. |
| 258 | + |
| 259 | +Additional Resources |
| 260 | +-------------------- |
| 261 | + |
| 262 | +- `Doxygen documentation`_ |
| 263 | +- `Breathe documentation`_ |
| 264 | + |
| 265 | +.. _Breathe documentation: https://breathe.readthedocs.io/en/latest/ |
| 266 | +.. _Doxygen documentation: https://www.doxygen.nl/index.html |
0 commit comments