Fix for UBSan build
[platform/upstream/doxygen.git] / doc / docblocks.doc
1 /******************************************************************************
2  *
3  * 
4  *
5  * Copyright (C) 1997-2012 by Dimitri van Heesch.
6  *
7  * Permission to use, copy, modify, and distribute this software and its
8  * documentation under the terms of the GNU General Public License is hereby 
9  * granted. No representations are made about the suitability of this software 
10  * for any purpose. It is provided "as is" without express or implied warranty.
11  * See the GNU General Public License for more details.
12  *
13  * Documents produced by Doxygen are derivative works derived from the
14  * input used in their production; they are not affected by this license.
15  *
16  */
17 /*! \page docblocks Documenting the code
18 \tableofcontents
19
20 This chapter covers two topics:
21 1. How to put comments in your code such that doxygen incorporates them in
22    the documentation it generates. 
23    This is further detailed in the \ref specialblock "next section".
24 2. Ways to structure the contents of a comment block such that the output
25    looks good, as explained in section \ref docstructure.
26
27 \section specialblock Special comment blocks 
28
29 A special comment block is a C or C++ style comment block with some 
30 additional markings, so doxygen knows it is a piece of structured text that
31 needs to end up in the generated documentation. The \ref cppblock "next" section
32 presents the various styles supported by doxygen.
33
34 For Python, VHDL, Fortran, and Tcl code there are different commenting 
35 conventions, which can be found in sections \ref pythonblocks, \ref vhdlblocks, 
36 \ref fortranblocks, and \ref tclblocks respectively.
37
38 \subsection cppblock Comment blocks for C-like languages (C/C++/C#/Objective-C/PHP/Java)
39
40 For each entity in the code there are two (or in some cases three) types of descriptions, 
41 which together form the documentation for that entity; a *brief* description and *detailed*
42 description, both are optional. For methods and functions there is also a third
43 type of description, the so called *in body* description, which consists of 
44 the concatenation of all comment blocks found within the body of the method or function.
45
46 Having more than one brief or detailed description is allowed (but not recommended,
47 as the order in which the descriptions will appear is not specified).
48
49 As the name suggest, a brief description is
50 a short one-liner, whereas the detailed description provides longer, 
51 more detailed documentation. An "in body" description can also act as a detailed
52 description or can describe a collection of implementation details.
53 For the HTML output brief descriptions are also
54 used to provide tooltips at places where an item is referenced.
55
56 There are several ways to mark a comment block as a detailed description:
57 <ol>
58 <li> You can use the JavaDoc style, which consist of a C-style comment
59 block starting with two *'s, like this:
60
61 \verbatim
62 /**
63  * ... text ...
64  */
65 \endverbatim
66
67 <li> or you can use the Qt style and add an exclamation mark (!) 
68 after the opening of a C-style comment block, as shown in this example:
69
70 \verbatim
71 /*!
72  * ... text ...
73  */
74 \endverbatim
75
76 In both cases the intermediate *'s are optional, so
77
78 \verbatim
79 /*!
80  ... text ...
81 */
82 \endverbatim
83
84 is also valid.
85
86 <li> A third alternative is to use a block of <i>at least two</i> C++ comment 
87 lines, where each line starts with an additional slash or an 
88 exclamation mark. Here are examples of the two cases:
89
90 \verbatim
91 ///
92 /// ... text ...
93 ///
94 \endverbatim
95
96 or
97
98 \verbatim
99 //!
100 //!... text ...
101 //!
102 \endverbatim
103
104 Note that a blank line ends a documentation block in this case.
105
106 <li>
107
108 Some people like to make their comment blocks more visible in the
109 documentation. For this purpose you can use the following:
110
111 \verbatim
112 /********************************************//**
113  *  ... text
114  ***********************************************/
115 \endverbatim
116 (note the 2 slashes to end the normal comment block and start a special comment block).
117
118 or
119
120 \verbatim
121 /////////////////////////////////////////////////
122 /// ... text ...
123 /////////////////////////////////////////////////
124 \endverbatim
125
126 </ol>
127
128 For the brief description there are also several possibilities:
129 <ol>
130 <li>One could use the \ref cmdbrief "\\brief" command with one of the 
131 above comment blocks. This command ends at the end of a paragraph, 
132 so the detailed description follows after an empty line.
133
134 Here is an example:
135
136 \verbatim
137 /*! \brief Brief description.
138  *         Brief description continued.
139  *
140  *  Detailed description starts here.
141  */
142 \endverbatim
143
144 <li>If \ref cfg_javadoc_autobrief "JAVADOC_AUTOBRIEF" is set to \c YES 
145     in the configuration file, 
146     then using JavaDoc style comment
147     blocks will automatically start a brief description which ends at the
148     first dot followed by a space or new line. Here is an example:
149
150 \verbatim
151 /** Brief description which ends at this dot. Details follow
152  *  here.
153  */
154 \endverbatim
155 The option has the same effect for multi-line special C++ comments:
156 \verbatim
157 /// Brief description which ends at this dot. Details follow
158 /// here.
159 \endverbatim
160
161 <li>A third option is to use a special C++ style comment which does not 
162     span more than one line. Here are two examples:
163 \verbatim
164 /// Brief description.
165 /** Detailed description. */
166 \endverbatim
167
168 or
169
170 \verbatim
171 //! Brief description.
172
173 //! Detailed description 
174 //! starts here.
175 \endverbatim
176
177 Note the blank line in the last example, which is required to separate the 
178 brief description from the block containing the detailed description. The
179 \ref cfg_javadoc_autobrief "JAVADOC_AUTOBRIEF" should also be set to \c NO
180 for this case.
181
182 </ol>
183
184 As you can see doxygen is quite flexible. If you have multiple
185 detailed descriptions, like in the following example:
186
187 \verbatim
188 //! Brief description, which is
189 //! really a detailed description since it spans multiple lines.
190 /*! Another detailed description!
191  */
192 \endverbatim
193
194 They will be joined. Note that this is also the case if the descriptions
195 are at different places in the code! In this case the order will depend
196 on the order in which doxygen parses the code.
197
198
199 Unlike most other documentation systems, doxygen also allows you to put
200 the documentation of members (including global functions) in front of 
201 the \e definition. This way the documentation can be placed in the source 
202 file instead of the header file. This keeps the header file compact, and allows the 
203 implementer of the members more direct access to the documentation.
204 As a compromise the brief description could be placed before the
205 declaration and the detailed description before the member definition.
206
207 \subsubsection memberdoc Putting documentation after members 
208
209 If you want to document the members of a file, struct, union, class, or enum,
210 it is sometimes desired to place the documentation block after the member 
211 instead of before. For this purpose you have to put an additional \< marker
212 in the comment block. Note that this also works for the parameters 
213 of a function.
214
215 Here are some examples:
216 \verbatim
217 int var; /*!< Detailed description after the member */
218 \endverbatim
219 This block can be used to put a Qt style detailed 
220 documentation block \e after a member. Other ways to do the
221 same are:
222 \verbatim
223 int var; /**< Detailed description after the member */
224 \endverbatim
225 or 
226 \verbatim
227 int var; //!< Detailed description after the member
228          //!< 
229 \endverbatim
230 or 
231 \verbatim
232 int var; ///< Detailed description after the member
233          ///< 
234 \endverbatim
235
236 Most often one only wants to put a brief description after a member.
237 This is done as follows:
238 \verbatim
239 int var; //!< Brief description after the member
240 \endverbatim
241 or
242 \verbatim
243 int var; ///< Brief description after the member
244 \endverbatim
245
246 For functions one can use the \ref cmdparam "\@param" command to document the parameters
247 and then use <code>[in]</code>, <code>[out]</code>, <code>[in,out]</code> 
248 to document the direction. For inline documentation this is also possible 
249 by starting with the direction attribute, e.g.
250 \verbatim
251 void foo(int v /**< [in] docs for input parameter v. */);
252 \endverbatim
253
254 Note that these blocks have the same structure and meaning as the 
255 special comment blocks in the previous section 
256 only the \< indicates that the member is 
257 located in front of the block instead of after the block.
258
259 Here is an example of the use of these comment blocks:
260 \include afterdoc.h
261  \htmlonly
262  Click <a href="$(DOXYGEN_DOCDIR)/examples/afterdoc/html/class_test.html">here</a>
263  for the corresponding HTML documentation that is generated by doxygen.
264  \endhtmlonly
265
266 \warning These blocks can only be used to document \e members and \e parameters.
267          They cannot be used to document files, classes, unions, structs,
268          groups, namespaces and enums themselves. Furthermore, the structural 
269          commands mentioned in the next section 
270          (like <code>\\class</code>) are not allowed 
271          inside these comment blocks.
272
273 \subsubsection docexamples Examples
274
275 Here is an example of a documented piece of C++ code using the Qt style:
276 \include qtstyle.cpp
277  \htmlonly
278  Click <a href="$(DOXYGEN_DOCDIR)/examples/qtstyle/html/class_test.html">here</a>
279  for the corresponding HTML documentation that is generated by doxygen.
280  \endhtmlonly
281
282 The brief descriptions are included in the member overview of a 
283 class, namespace or file and are printed using a small italic font 
284 (this description can be hidden by setting 
285 \ref cfg_brief_member_desc "BRIEF_MEMBER_DESC" to \c NO in 
286 the config file). By default the brief descriptions become the first 
287 sentence of the detailed descriptions 
288 (but this can be changed by setting the \ref cfg_repeat_brief "REPEAT_BRIEF" 
289 tag to \c NO). Both the brief and the detailed descriptions are optional 
290 for the Qt style. 
291
292 By default a JavaDoc style documentation block behaves the same way as a
293 Qt style documentation block. This is not according the JavaDoc specification
294 however, where the first sentence of the documentation block is automatically
295 treated as a brief description. To enable this behavior you should set
296 \ref cfg_javadoc_autobrief "JAVADOC_AUTOBRIEF" to YES in the configuration
297 file. If you enable this option and want to put a dot in the middle of a
298 sentence without ending it, you should put a backslash and a space after it.
299 Here is an example:
300 \verbatim
301   /** Brief description (e.g.\ using only a few words). Details follow. */
302 \endverbatim
303
304 Here is the same piece of code as shown above, this time documented using the 
305 JavaDoc style and \ref cfg_javadoc_autobrief "JAVADOC_AUTOBRIEF" set to YES:
306 \include jdstyle.cpp
307  \htmlonly
308  Click <a href="$(DOXYGEN_DOCDIR)/examples/jdstyle/html/class_test.html">here</a>
309  for the corresponding HTML documentation that is generated by doxygen.
310  \endhtmlonly
311
312 Similarly, if one wishes the first sentence of a Qt style documentation
313 block to automatically be treated as a brief description, one may set
314 \ref cfg_qt_autobrief "QT_AUTOBRIEF" to YES in the configuration file.
315
316 \subsubsection structuralcommands Documentation at other places
317
318 In the examples in the previous section the comment blocks were always located *in 
319 front* of the declaration or definition of a file, class or namespace or *in
320 front* or *after* one of its members. 
321 Although this is often comfortable, there may sometimes be reasons to put the 
322 documentation somewhere else. For documenting a file this is even 
323 required since there is no such thing as "in front of a file". 
324
325 Doxygen allows you to put your documentation blocks practically 
326 anywhere (the exception is inside the body of a function or inside a 
327 normal C style comment block). 
328
329 The price you pay for not putting the
330 documentation block directly before (or after) an item is the need to put a  
331 structural command inside the documentation block, which leads to some
332 duplication of information. So in practice you should \e avoid the use of
333 structural commands \e unless other requirements force you to do so.
334
335 Structural commands (like \ref cmd_intro "all other commands") start with a backslash 
336 (<tt>\\</tt>), or an at-sign (<tt>\@</tt>) if you prefer JavaDoc style, 
337 followed by a command name and one or more parameters.
338 For instance, if you want to document the class \c Test in the example
339 above, you could have also put the following documentation block somewhere
340 in the input that is read by doxygen:
341 \verbatim
342 /*! \class Test
343     \brief A test class.
344
345     A more detailed class description.
346 */
347 \endverbatim
348
349 Here the special command \c \\class is used to indicate that the
350 comment block contains documentation for the class \c Test.
351 Other structural commands are:
352 <ul>
353 <li>\c \\struct to document a C-struct.
354 <li>\c \\union to document a union.
355 <li>\c \\enum to document an enumeration type.
356 <li>\c \\fn to document a function.
357 <li>\c \\var to document a variable or typedef or enum value.
358 <li>\c \\def to document a \#define.
359 <li>\c \\typedef to document a type definition.
360 <li>\c \\file to document a file.
361 <li>\c \\namespace to document a namespace.
362 <li>\c \\package to document a Java package.
363 <li>\c \\interface to document an IDL interface.
364 </ul>
365 See section \ref commands for detailed information about these and many other 
366 commands. 
367
368 To document a member of a C++ class, you must also document the class 
369 itself. The same holds for namespaces. To document a global C function, 
370 typedef, enum or preprocessor definition you must first document the file 
371 that contains it (usually this will be a header file, because that file 
372 contains the information that is exported to other source files).
373
374 Let's repeat that, because it is often overlooked:
375 to document global objects (functions, typedefs, enum, macros, etc), you
376 <em>must</em> document the file in which they are defined. In other words, 
377 there <em>must</em> at least be a \verbatim /*! \file */ \endverbatim
378 or a \verbatim /** @file */ \endverbatim line in this file.
379
380 Here is an example of a C header named \c structcmd.h that is documented 
381 using structural commands:
382 \include structcmd.h
383  \htmlonly
384  Click <a href="$(DOXYGEN_DOCDIR)/examples/structcmd/html/structcmd_8h.html">here</a>
385  for the corresponding HTML documentation that is generated by doxygen.
386  \endhtmlonly
387
388  Because each comment block in the example above contains a structural command, all
389  the comment blocks could be moved to another location or input file 
390  (the source file for instance), without affecting the generated 
391  documentation. The disadvantage of this approach is that prototypes are
392  duplicated, so all changes have to be made twice! Because of this you
393  should first consider if this is really needed, and avoid structural
394  commands if possible. I often receive examples that contain \\fn command
395  in comment blocks which are place in front of a function. This is clearly
396  a case where the \\fn command is redundant and will only lead to problems.
397
398 \subsection pythonblocks Comment blocks in Python
399
400 For Python there is a standard way of documenting the code using 
401 so called documentation strings. Such strings are stored in \c __doc__
402 and can be retrieved at runtime. Doxygen will extract such comments
403 and assume they have to be represented in a preformatted way.
404
405 \include docstring.py
406  \htmlonly
407  Click <a href="$(DOXYGEN_DOCDIR)/examples/docstring/html/index.html">here</a>
408  for the corresponding HTML documentation that is generated by doxygen.
409  \endhtmlonly
410
411 Note that in this case none of doxygen's \ref cmd_intro "special commands" 
412 are supported.
413
414 There is also another way to document Python code using comments that 
415 start with "##". These type of comment blocks are more in line with the 
416 way documentation blocks work for the other languages supported by doxygen 
417 and this also allows the use of special commands. 
418
419 Here is the same example again but now using doxygen style comments:
420
421 \include pyexample.py
422  \htmlonly
423  Click <a href="$(DOXYGEN_DOCDIR)/examples/pyexample/html/index.html">here</a>
424  for the corresponding HTML documentation that is generated by doxygen.
425  \endhtmlonly
426
427 Since python looks more like Java than like C or C++, you should set 
428 \ref cfg_optimize_output_java "OPTIMIZE_OUTPUT_JAVA" to \c YES in the
429 config file. 
430
431
432 \htmlonly
433 Go to the <a href="lists.html">next</a> section or return to the
434  <a href="index.html">index</a>.
435 \endhtmlonly
436
437 \subsection vhdlblocks Comment blocks in VHDL
438
439 For VHDL a comment normally start with "--". Doxygen will extract comments
440 starting with "--!". There are only two types of comment blocks in VHDL;
441 a one line --! comment representing a brief description, and a multi-line
442 --! comment (where the --! prefix is repeated for each line) representing
443 a detailed description.
444
445 Comments are always located in front of the item that is being documented
446 with one exception: for ports the comment can also be after the item
447 and is then treated as a brief description for the port.
448
449 Here is an example VHDL file with doxygen comments:
450
451 \include  mux.vhdl
452  \htmlonly
453  Click <a href="$(DOXYGEN_DOCDIR)/examples/mux/html/index.html">here</a>
454  for the corresponding HTML documentation that is generated by doxygen.
455  \endhtmlonly
456
457 To get proper looking output you need to set
458 \ref cfg_optimize_output_vhdl "OPTIMIZE_OUTPUT_VHDL" to \c YES in the
459 config file. This will also affect a number of other settings. When they
460 were not already set correctly doxygen will produce a warning telling which
461 settings where overruled.
462
463 \subsection fortranblocks Comment blocks in Fortran
464
465 When using doxygen for Fortran code you should
466 set \ref cfg_optimize_for_fortran "OPTIMIZE_FOR_FORTRAN" to \c YES.
467
468 For Fortran "!>" or "!<" starts a comment and "!!" or "!>" can be used to 
469 continuate a one line comment into a multi-line comment.
470
471 Here is an example of a documented Fortran subroutine:
472 \verbatim
473   !> Build the restriction matrix for the aggregation 
474   !! method.
475   !! @param aggr information about the aggregates
476   !! @todo Handle special case
477   subroutine IntRestBuild(A,aggr,Restrict,A_ghost)
478     implicit none
479     Type(SpMtx), intent(in) :: A !< our fine level matrix
480     Type(Aggrs), intent(in) :: aggr
481     Type(SpMtx), intent(out) :: Restrict !< Our restriction matrix
482 \endverbatim
483
484 As a alternative you can also use comments in fixed format code:
485
486 \verbatim
487 C> Function comment
488 C> another line of comment
489       function A(i)
490 C> input parameter
491         integer i
492       end function A
493 \endverbatim
494
495 \subsection tclblocks Comment blocks in Tcl
496
497 Doxygen documentation can be included in normal Tcl comments.
498
499 To start a new documentation block start a line with \c ## (two hashes).
500 All following comment lines and continuation lines will be added to this
501 block. The block ends with a line not starting with a \c # (hash sign).
502
503 A brief documentation can be added with \c ;#< (semicolon, hash and
504 lower then sign). The brief documentation also ends at a line not starting
505 with a \c # (hash sign).
506
507 Inside doxygen comment blocks all normal doxygen markings are supported.
508 The only exceptions are described in the following two paragraphs.
509
510 If a doxygen comment block ends with a line containing only
511 \c #\\code or \c #\@code all code until a line only containing \c #\\endcode
512 or \c #\@endcode is added to the generated documentation as code block.
513
514 If a doxygen comment block ends with a line containing only
515 \c #\\verbatim or \c #\@verbatim all code until a line only containing
516 \c #\\endverbatim or \c #\@endverbatim is added verbatim to the generated
517 documentation.
518
519 To detect namespaces, classes, functions and variables the following
520 Tcl commands are recognized. Documentation blocks can be put on the lines
521 before the command.
522
523 <ul>
524 <li><tt>namespace eval ..</tt> Namespace
525 <li><tt>proc ..</tt> Function
526 <li><tt>variable ..</tt> Variable
527 <li><tt>common ..</tt> Common variable
528 <li><tt>itcl::class ..</tt> Class
529 <li><tt>itcl::body ..</tt> Class method body definition
530 <li><tt>oo::class create ..</tt> Class
531 <li><tt>oo::define ..</tt> OO Class definition
532 <li><tt>method ..</tt> Class method definitions
533 <li><tt>constructor ..</tt> Class constructor
534 <li><tt>destructor ..</tt> Class destructor
535 <li><tt>public ..</tt> Set protection level
536 <li><tt>protected ..</tt> Set protection level
537 <li><tt>private ..</tt> Set protection level
538 </ul>
539
540 <!--
541 To use your own keywords you an map these keyword to the recognized commands
542 using the \ref cfg_tcl_subs "TCL_SUBST" entry in the config file.
543 The entry contain a list of word-keyword mappings. To use the itcl::*
544 commands without the leading namespace use p.e.:
545
546 \verbatim TCL_SUBST = class itcl:class body itcl:body \endverbatim
547 -->
548
549 Following is a example using doxygen style comments:
550
551 \include tclexample.tcl
552  \htmlonly
553  Click <a href="$(DOXYGEN_DOCDIR)/examples/tclexample/html/index.html">here</a>
554  for the corresponding HTML documentation that is generated by doxygen.
555  \endhtmlonly
556
557 \htmlonly
558 Go to the <a href="lists.html">next</a> section or return to the
559  <a href="index.html">index</a>.
560 \endhtmlonly
561
562 \section docstructure Anatomy of a comment block
563
564 The previous section focused on how to make the comments in your code known
565 to doxygen, it explained the difference between a brief and a detailed description, and
566 the use of structural commands.
567
568 In this section we look at the contents of the comment block itself.
569
570 Doxygen supports various styles of formatting your comments.
571
572 The simplest form is to use plain text. This will appear as-is in the output 
573 and is ideal for a short description.
574
575 For longer descriptions you often will find the 
576 need for some more structure, like a block of verbatim text, a list, or a 
577 simple table. For this doxygen supports the 
578 <a href="http://daringfireball.net/projects/markdown/syntax">Markdown</a> 
579 syntax, including parts of the
580 <a href="http://michelf.com/projects/php-markdown/extra/">Markdown Extra</a>
581 extension. 
582
583 Markdown is designed to be very easy to read and write. 
584 It's formatting is inspired by plain text mail.
585 Markdown works great for simple, generic formatting, like an introduction
586 page for your project. Doxygen also supports reading of markdown files 
587 directly. See \ref markdown "here" for more details regards Markdown support.
588
589 For programming language specific formatting doxygen has two
590 forms of additional markup on top of Markdown formatting.
591
592 1. <a href="http://en.wikipedia.org/wiki/Javadoc">Javadoc</a> like markup.
593    See \ref cmd_intro "here" for a complete overview of all commands supported by doxygen.
594 2. <a href="http://en.wikipedia.org/wiki/C_Sharp_(programming_language)#XML_documentation_system">XML</a> markup
595    as specified in the C# standard. See \ref xmlcmds "here" for the XML commands supported by doxygen.
596
597 If this is still not enough doxygen also supports a \ref htmlcmds "subset" of 
598 the <a href="http://en.wikipedia.org/wiki/HTML">HTML</a> markup language.
599
600 \htmlonly
601 Go to the <a href="markdown.html">next</a> section or return to the
602  <a href="index.html">index</a>.
603 \endhtmlonly
604
605 */