source: rtems-docs/eng/coding-doxygen.rst @ 4886d60

5
Last change on this file since 4886d60 was 4886d60, checked in by Sebastian Huber <sebastian.huber@…>, on 01/09/19 at 15:14:04

Use standard format for copyright lines

  • Property mode set to 100644
File size: 11.8 KB
Line 
1.. comment SPDX-License-Identifier: CC-BY-SA-4.0
2
3.. Copyright (C) 2018.
4.. COMMENT: RTEMS Foundation, The RTEMS Documentation Project
5
6General Doxygen Recommentations
7===============================
8
9.. COMMENT: TBD - Convert the following to Rest and insert into this file
10.. COMMENT: TBD - https://devel.rtems.org/wiki/Developer/Coding/Doxygen
11
12.. sidebar:: *History*
13
14  RTEMS is much older than Doxygen (http://www.doxygen.org/) and
15  the documentation in the .h files was obviously not written with
16  Doxygen markup in mind. In 2007, Joel Sherrill undertook to convert the
17  documentation in the .h and .inl files in the RTEMS SuperCore? to Doxygen
18  format. As a result of this effort, the Doxygen for the development
19  version of the RTEMS is now built automatically periodically and made
20  available on the RTEMS Website. In April 2008, Joel Sherrill began to
21  update the Classic API (e.g. cpukit/rtems) .h files to include Doxygen
22  markup. Now, RTEMS has nice Doxygen markup including state and sequence
23  diagrams.
24
25Doxygen Best Practices
26----------------------
27
28* Do not use ``@a``. Instead use ``@param`` to document function parameters.
29* Do not use ``@return``. Instead use ``@retval`` to document return status
30  codes.
31* Do not write documentation for trivial functions.
32* Do not repeat documentation, use ``@see`` for example.
33* Do not use ``@note``.
34* Use groups and arrange them in a hierarchy. Put every file into at least
35  one group.
36* Use dot comments for state diagrams.
37* Use one whitespace character after an asterisk.
38
39Special Notes for Google Code-in Students
40-----------------------------------------
41
42Follow the directions given by the `Google Code-in
43<https://devel.rtems.org/wiki/GCI>`_ task and this should take
44care of itself if in doubt ask a mentor and/or tell a mentor the decision you
45made.
46
47Header File Example
48-------------------
49
50`thread.h
51<https://git.rtems.org/rtems/tree/cpukit/include/rtems/score/thread.h>`_ and
52`threadimpl.h
53<https://git.rtems.org/rtems/tree/cpukit/include/rtems/score/threadimpl.h>`_
54should be a good example of how a header file shouldbe written. The following
55gives details in bits and pieces.
56
57Header blocks
58-------------
59
60Header files should contain the similar comment blocks as other source files,
61described at :ref:`coding-file-hdr`.
62
63.. code-block:: c
64
65  /**
66   * @file
67   *
68   * @ingroup FlipFlop
69   *
70   * @brief Flip-Flop API
71   */
72
73  /*
74   * Copyright (c) YYYY Author.
75   *
76   * The license and distribution terms for this file may be
77   * found in the file LICENSE in this distribution or at
78   * http://www.rtems.com/license/LICENSE.
79   */
80
81Header guard
82------------
83
84After the comment blocks, use a header guard that assembles at least the
85include path of the file. For example, if ``flipflop.h`` is in
86``<rtems/lib/flipflop.h>`` then
87
88.. code-block:: c
89
90  #ifndef RTEMS_LIB_FLIP_FLOP_H
91  #define RTEMS_LIB_FLIP_FLOP_H
92
93Includes
94--------
95
96Then add your include files before protecting C declarations from C++.
97
98.. code-block:: c
99
100  #include <rtems.h>
101
102  #ifdef __cplusplus
103  extern "C" {
104  #endif /* __cplusplus */
105
106Using @defgroup for group definitions
107-------------------------------------
108
109Add any group definitions surrounding the function declarations that belong
110in that group. Rarely, a header may define more than one group. Here we use
111a dot diagram.
112
113.. code-block:: c
114
115  /**
116   * @defgroup FlipFlop Flip-Flop
117   *
118   * @brief Simple Flip-Flop state machine.
119   *
120   * @dot
121   *   digraph {
122   *     start [label="START"];
123   *     flip [label="FLIP"];
124   *     flop [label="FLOP"];
125   *     flip -> flop [label="flop()", URL="\ref flop"];
126   *     flop -> flip [label="flip()", URL="\ref flip"];
127   *     start -> flip
128   *       [label="flip_flop_initialize(FLIP)", URL="\ref flip_flop_initialize"];
129   *     start -> flop
130   *       [label="flip_flop_initialize(FLOP)", URL="\ref flip_flop_initialize"];
131   *     flip -> start
132   *       [label="flip_flop_restart()", URL="\ref flip_flop_restart"];
133   *   }
134   * @enddot
135   *
136   * @{
137   */
138
139enum and struct
140---------------
141
142Provide documentation for declarations of enumerated types and structs.
143Use typedefs for structs, and do not use ``_t`` as a typename suffix.
144
145.. code-block:: c
146
147  /**
148   * @brief The set of possible flip-flop states.
149   *
150   * Enumerated type to define the set of states for a flip-flop.
151   */
152  typedef enum {
153    START = 0,
154    FLIP,
155    FLOP
156  } flip_flop_state;
157
158  /**
159   * @brief Object containing multiple flip-flops.
160   *
161   * Encapsulates multiple flip-flops.
162   */
163  typedef struct {
164    /**
165     * @brief Primary flip-flop.
166     */
167    flip_flop_state primary;
168    /**
169     * @brief Secondary flip-flop.
170     */
171    flip_flop_state secondary;
172  } flip_flop_multiple;
173
174Using @name for organization
175----------------------------
176
177Complicated groups can be given additional organization by using ``@name``, or
178by declaring additional groups within the hierarchy of the header file's
179top-level group.
180
181.. code-block:: c
182
183  /**
184   * @name Flip-Flop Maintenance
185   *
186   * @{
187   */
188
189Declaring functions
190-------------------
191
192Function declarations should have an @brief that states what the function does
193in a single topic sentence starting with a descriptive verb in the present
194tense.
195
196.. code-block:: c
197
198  /**
199   * @brief Initializes the flip-flop state.
200   *
201   * @param[in] state The initial state to set the flip-flop.
202   *
203   * @retval RTEMS_SUCCESSFUL Successfully initialized.
204   * @retval RTEMS_INCORRECT_STATE Flip-flop state is not valid.
205   */
206  rtems_status_code flip_flop_initialize(flip_flop_state state);
207
208  /**
209   * @brief Restarts the flip-flop.
210   *
211   * @retval RTEMS_SUCCESSFUL Successfully restarted.
212   * @retval RTEMS_INCORRECT_STATE Flip-flop not in flip state.
213   */
214  rtems_status_code flip_flop_restart(void);
215
216Do not document trivial functions, such as getter/setter methods.
217
218.. code-block:: c
219
220  flip_flop_state flip_flop_current_state(void);
221
222Close the documentation name definition and open a new name definition.
223
224.. code-block:: c
225
226  /** @} */
227
228  /**
229   * @name Flip-Flop Usage
230   *
231   * @{
232   */
233
234  /**
235   * @brief Flip operation.
236   *
237   * @retval RTEMS_SUCCESSFUL Flipped successfully.
238   * @retval RTEMS_INCORRECT_STATE Incorrect state for flip operation.
239   */
240  rtems_status_code flip( void );
241
242  /**
243   * @brief Flop operation.
244   *
245   * @retval RTEMS_SUCCESSFUL Flopped successfully.
246   * @retval RTEMS_INCORRECT_STATE Incorrect state for flop operation.
247   */
248  rtems_status_code flop( void );
249
250  /** @} */
251
252Ending the file
253---------------
254
255Close the documentation group definition, then the extern C declarations,
256then the header guard.
257
258.. code-block:: c
259
260  /** @} */
261
262  #ifdef __cplusplus
263  }
264  #endif /* __cplusplus */
265
266  #endif /* RTEMS_LIB_FLIP_FLOP_H */
267
268 No newline at the end of the file.
269
270Source File Example
271-------------------
272
273.. code-block:: c
274
275  /**
276   * @file
277   *
278   * @ingroup FlipFlop
279   *
280   * @brief Flip-Flop implementation.
281   */
282
283  /*
284   * Copyright (c) YYYY Author.
285   *
286   * The license and distribution terms for this file may be
287   * found in the file LICENSE in this distribution or at
288   * http://www.rtems.com/license/LICENSE.
289   */
290
291  #include <rtems/lib/flipflop.h>
292
293  static flip_flop_state current_state;
294
295  rtems_status_code flip_flop_initialize(flip_flop_state state)
296  {
297    if (current_state == START) {
298      current_state = state;
299
300      return RTEMS_SUCCESSFUL;
301    } else {
302      return RTEMS_INCORRECT_STATE;
303    }
304  }
305
306  rtems_status_code flip_flop_restart(void)
307  {
308    if (current_state == FLIP) {
309      current_state = START;
310
311      return RTEMS_SUCCESSFUL;
312    } else {
313      return RTEMS_INCORRECT_STATE;
314    }
315  }
316
317  flip_flop_state flip_flop_current_state(void)
318  {
319    return current_state;
320  }
321
322  rtems_status_code flip(void)
323  {
324    if (current_state == FLOP) {
325      current_state = FLIP;
326
327      return RTEMS_SUCCESSFUL;
328    } else {
329      return RTEMS_INCORRECT_STATE;
330    }
331  }
332
333  rtems_status_code flop(void)
334  {
335    if (current_state == FLIP) {
336      current_state = FLOP;
337
338      return RTEMS_SUCCESSFUL;
339    } else {
340      return RTEMS_INCORRECT_STATE;
341    }
342  }
343
344Files
345-----
346Document files with the ``@file`` directive omitting the optional filename
347argument. Doxygen will infer the filename from the actual name of the file.
348Within one Doxygen run all files are unique and specified by the current
349Doxyfile. We can define how the generated output of path and filenames looks
350like in the Doxyfile via the ``FULL_PATH_NAMES``, ``STRIP_FROM_PATH`` and
351``STRIP_FROM_INC_PATH`` options.
352
353Functions
354---------
355
356For documentation of function arguments there are basically to ways:
357
358The first one uses ``@param``:
359
360.. code-block:: c
361
362  /**
363   * @brief Copies from a source to a destination memory area.
364   *
365   * The source and destination areas may not overlap.
366   *
367   * @param[out] dest The destination memory area to copy to.
368   * @param[in] src The source memory area to copy from.
369   * @param[in] n The number of bytes to copy.
370   */
371
372The second is to use ``@a`` param in descriptive text, for example:
373
374.. code-block:: c
375
376  /**
377  * Copies @a n bytes from a source memory area @a src to a destination memory
378  * area @a dest, where both areas may not overlap.
379  */
380
381The ``@a`` indicates that the next word is a function argument and deserves
382some kind of highlighting. However, we feel that ``@a`` buries the usage of
383function arguments within description text. In RTEMS sources, we prefer to
384use ``@param`` instead of ``@a``.
385
386Doxyfile Hints
387--------------
388
389Header Files
390~~~~~~~~~~~
391
392It is an RTEMS build feature that header files need to be installed in order to
393be useful. One workaround to generate documentation which allows automatic
394link generation is to use the installed header files as documentation input.
395Assume that we have the RTEMS sources in the rtems directory and the build of
396our BSP in build/powerpc-rtems5/mybsp relative to a common top-level directory.
397Then you can configure Doxygen like:
398
399.. code-block::
400
401  INPUT           = rtems/bsps/powerpc/mybsp \
402                    rtems/c/src/lib/libcpu/powerpc/mycpu \
403                    rtems/make/custom/mybsp.cfg \
404                    build/powerpc-rtems5/mybsp/lib/include/myincludes
405
406  RECURSIVE       = YES
407
408  EXCLUDE         = rtems/bsps/powerpc/mybsp/include \
409                    rtems/c/src/lib/libcpu/powerpc/mycpu/include
410
411  FULL_PATH_NAMES = YES
412
413  STRIP_FROM_PATH = build/powerpc-rtems5/mybsp/lib/include \
414                    rtems
415
416Script and Assembly Files
417~~~~~~~~~~~~~~~~~~~~~~~~~
418
419Doxygen cannot cope with script (= files with #-like comments) or assembly
420files. But you can add filter programs for them
421
422.. COMMENT: TBD - Add source code for filter programs somewhere
423
424.. code-block::
425
426  FILTER_PATTERNS = *.S=c-comments-only \
427                    *.s=c-comments-only \
428                    *.cfg=script-comments-only \
429                    *.am=script-comments-only \
430                    *.ac=script-comments-only
431
432Assembly Example
433~~~~~~~~~~~~~~~~
434
435.. code-block:: c
436
437  /**
438   * @fn void mpc55xx_fmpll_reset_config()
439   *
440   * @brief Configure FMPLL after reset.
441   *
442   * Sets the system clock from 12 MHz in two steps up to 128 MHz.
443   */
444  GLOBAL_FUNCTION mpc55xx_fmpll_reset_config
445      /* Save link register */
446      mflr r3
447
448      LA r4, FMPLL_SYNCR
449
450You have to put a declaration of this function somewhere in a header file.
451
452Script Example
453~~~~~~~~~~~~~~
454
455.. code-block:: shell
456
457  ##
458  #
459  # @file
460  #
461  # @ingroup mpc55xx_config
462  #
463  # @brief Configure script of LibBSP for the MPC55xx evaluation boards.
464  #
465
466  AC_PREREQ([2.69])
467  AC_INIT([rtems-c-src-lib-libbsp-powerpc-mpc55xxevb],[_RTEMS_VERSION],[https://devel.rtems.org/newticket])
468
469
470GCC Attributes
471--------------
472
473The Doxygen C/C++ parser cannot cope with the GCC ``attribute((something))``
474stuff. But you can discard such features with pre-defined preprocessor macros
475
476.. code-block:: shell
477
478  ENABLE_PREPROCESSING = YES
479  MACRO_EXPANSION      = YES
480  EXPAND_ONLY_PREDEF   = YES
481  PREDEFINED           = __attribute__(x)=
Note: See TracBrowser for help on using the repository browser.