1f563c0058cd8a941e0a1f34684f7d194b29585c
[alexxy/gromacs.git] / docs / doxygen / lib / doxygen.md
1 Using Doxygen {#page_doxygen}
2 =============
3
4 This page documents how Doxygen is set up in the \Gromacs source tree,
5 as well as guidelines for adding new Doxygen comments.  Examples are included,
6 as well as tips and tricks for avoiding Doxygen warnings.  The guidelines focus
7 on C++ code and other new code that follows the new module layout.
8 Parts of the guidelines are still applicable to documenting older code (e.g.,
9 within `gmxlib/` or `mdlib/`), in particular the guidelines about formatting
10 the Doxygen comments and the use of \c \\internal.
11 \ref page_codelayout documents the overall structure of the documentation.
12
13 To get started quickly, you only need to read the first two sections to
14 understand the overall structure of the documentation, and take a look at the
15 examples at the end.  The remaining sections provide the details for
16 understanding why the examples are the way they are, and for more complex
17 situations.  They are meant more as a reference to look up solutions for
18 particular problems, rather than single-time reading.  To understand or find
19 individual Doxygen commands, you should first look at Doxygen documentation
20 (http://www.stack.nl/~dimitri/doxygen/manual/index.html).
21
22
23 Documentation flavors
24 =====================
25
26 The \Gromacs source tree is set up to produce three different levels of Doxygen
27 documentation:
28
29 1. Public API documentation (suffix `-user`), which documents functions and
30    classes exported from the library and intended for use outside the \Gromacs
31    library.
32 2. Library API documentation (suffix `-lib`), which additionally includes
33    functions and classes that are designed to be used from other parts of
34    \Gromacs, as well as some guidelines that are mostly of interest to
35    developers.
36 3. Full documentation (suffix `-full`), which includes (nearly) all (documented)
37    functions and classes in the source tree.
38
39 Each subsequent level of documentation includes all the documentation from the
40 levels above it.  The suffixes above refer to the suffixes of Doxygen input and
41 output files, as well as the name of the output directory.  When all the
42 flavors have been built, the front pages of the documentation contain links to
43 the other flavors, and explain the differences in more detail.
44
45 As a general guideline, the public API documentation should be kept free of
46 anything that a user linking against an unmodified \Gromacs does not see.
47 In other words, the public API documentation should mainly document the
48 contents of installed headers, and provide the necessary overview of using
49 those.  Also, verbosity requirements for the public API documentation are
50 higher: ideally, readers of the documentation could immediately start using the
51 API based on the documentation, without any need to look at the implementation.
52
53 Similarly, the library API documentation should not contain things that other
54 modules in \Gromacs can or should never call.  In particular, anything declared
55 locally in source files should be only available in the full documentation.
56 Also, if something is documented, and is not identified to be in the library
57 API, then it should not be necessary to call that function from outside its
58 module.
59
60
61 Building the documentation
62 ==========================
63
64 If you simply want to see up-to-date documentation, you can go to
65 http://jenkins.gromacs.org/job/Documentation_Nightly_master/javadoc/html-lib/index.xhtml
66 to see the documentation for the current development version.
67 Jenkins also runs Doxygen for all changes pushed to Gerrit for
68 release-5-0 and master branches, and the
69 resulting documentation can be viewed from the link posted by Jenkins.  The
70 Doxygen build is marked as unstable if it introduces any Doxygen warnings.
71
72 You may need to build the documentation locally if you want to check the
73 results after adding/modifying a significant amount of comments.  This is
74 recommended in particular if you do not have much experience with Doxygen.
75 It is a good idea to build with all the different settings to see that the
76 result is what you want, and that you do not produce any warnings.
77 For local work, it is generally a good idea to set `GMX_COMPACT_DOXYGEN=ON`
78 CMake option, which removes some large generated graphs from the documentation
79 and speeds up the process significantly.
80
81 All files related to Doxygen reside in the `docs/doxygen/` subdirectory in the source
82 and build trees.  In a freshly checked out source tree, this directory contains
83 various `Doxyfile-*.cmakein` files.  When you run CMake, corresponding files
84 `Doxyfile-user`, `Doxyfile-lib`, and `Doxyfile-full` are generated at the
85 corresponding location in the build tree.  There is also a
86 `Doxyfile-common.cmakein`, which is used to produce `Doxyfile-common`.
87 This file contains settings that are shared between all the input files.
88 `Doxyfile-compact` provides the extra settings for `GMX_COMPACT_DOXYGEN=ON`.
89
90 You can run Doxygen directly with one of the generated files (all output will
91 be produced under the current working directory), or build one of the
92 `doxygen-user`, `doxygen-lib`, and `doxygen-full` targets.  The targets run
93 Doxygen in a quieter mode and only show the warnings if there were any, and put
94 the output under `docs/html/doxygen/` in the build tree, so that the Doxygen
95 build cooperates with the broader `webpage` target.
96 The `doxygen-all` target builds all three targets with less typing.
97
98 The generated documentation is put under `html-user/`, `html-lib/`, and/or
99 `html-full/`.  Open `index.xhtml` file from one of
100 these subdirectories to start browsing (for \Gromacs developers, the
101 `html-lib/` is a reasonable starting point).  Log files with all Doxygen
102 warnings are also produced as `docs/doxygen/doxygen-*.log`, so you can inspect them after
103 the run.
104
105 You will need Doxygen 1.8.5 to build the current documentation.  Other versions
106 may work, but likely also produce warnings.  Additionally,
107 [graphviz](http://www.graphviz.org) and
108 [mscgen](http://www.mcternan.me.uk/mscgen/) are required for some graphs in
109 the documentation, and `latex` for formulas.  Working versions are likely
110 available through most package managers.  It is possible to build the
111 documentation without these tools, but you will see some errors and the related
112 figures will be missing from the documentation.
113
114
115 General guidelines for Doxygen markup {#section_doxygen_guidelines}
116 =====================================
117
118 Doxygen provides quite a few different alternative styles for documenting the
119 source code.  There are subtleties in how Doxygen treats the different types of
120 comments, and this also depends somewhat on the Doxygen configuration.  It is
121 possible to change the meaning of a comment by just changing the style of
122 comment it is enclosed in.  To avoid such issues, and to avoid needing to
123 manage all the alternatives, a single style throughout the source tree is
124 preferable.  When it comes to treatment of styles, \Gromacs uses the default
125 Doxygen configuration with one exception: `JAVADOC_AUTOBRIEF` is set ON to
126 allow more convenient one-line brief descriptions in C code.
127
128 Majority of existing comments in \Gromacs uses Qt-style comments (`/*!` and
129 `//!` instead of `/*``*` and `///`, \c \\brief instead of \c \@brief etc.),
130 so these should be used also for new documentation.  There is a single
131 exception for brief comments in C code; see below.
132
133 Similarly, existing comments use `/*!` for multiline comments in both C and
134 C++ code, instead of using multiple `//!` lines for C++.  The rationale is that
135 since the code will be a mixture of both languages for a long time, it is more
136 uniform to use similar style in both.  Also, since files will likely transition
137 from C to C++ gradually, rewriting the comments because of different style
138 issues should not generally be necessary.  Finally, multi-line `//!` comments
139 can work differently depending on Doxygen configuration, so it is better to
140 avoid that ambiguity.
141
142 When adding comments, ensure that a short brief description is always produced.
143 This is used in various listings, and should briefly explain the purpose of the
144 method without unnecessarily expanding those lists.
145 The basic guideline is to start all comment blocks with \c \\brief (possibly
146 after some other Doxygen commands).
147 If you want to avoid the \c \\brief for one-liners, you can use `//!`, but the
148 description must fit on a single line; otherwise, it is not interpreted as a
149 brief comment.  Note in particular that a simple `/*!` without a \c \\brief
150 does not produce a brief description.
151 Also note that \c \\brief marks the whole following paragraph as a brief
152 description, so you should insert an empty line after the intended brief
153 description.
154
155 In C code, `//` comments must be avoided because some compilers do not like
156 them.  If you want to avoid the \c \\brief for one-liners in C code, use
157 `/*``*` instead of `//!`.  If you do this, the brief description should not
158 contain unescaped periods except at the end.  Because of this, you should
159 prefer `//!` in C++ code.
160
161 Put the documentation comments in the header file that contains the
162 declaration, if such a header exists.
163 Implementation-specific comments that do not influence how a method
164 is used can go into the source file, just before the method definition, with an
165 \c \\internal tag in the beginning of the comment block.  Doxygen-style comments
166 within functions are not generally usable.
167
168 At times, you may need to exclude some part of a header or a source file such
169 that Doxygen does not see it at all.  In general, you should try to avoid this,
170 but it may be necessary to remove some functions that you do not want to appear
171 in the public API documentation, and which would generate warnings if left
172 undocumented, or to avoid Doxygen warnings from code it does not understand.
173 Prefer \c \\cond and \c \\endcond to do this.  If \c \\cond does not work for
174 you, you can also use \c \#ifndef `DOXYGEN`.  If you exclude a class method in
175 a header, you also need to exclude it in the source code to avoid warnings.
176
177
178 \Gromacs specifics
179 =================
180
181 The general guidelines on the style of Doxygen comments were given above.
182 This section introduces \Gromacs specific constructs currently used in Doxygen
183 documentation, as well as how \Gromacs uses Doxygen groups to organize the
184 documentation.
185
186 Some consistency checks are done automatically using custom scripts.
187 See \ref page_dev_gmxtree for details.
188
189 Controlling documentation visibility
190 ------------------------------------
191
192 To control in which level of documentation a certain function appears, three
193 different mechanisms are used:
194 * Global Doxygen configuration.  This is mainly used to include
195   declarations local to source files only in the full documentation.
196   You can find the details from the `Doxyfile-*.cmakein` files, and some of
197   them are also mentioned below on individual code constructs.
198 * The standard Doxygen command \c \\internal marks the documentation to be only
199   extracted into the full documentation (`INTERNAL_DOCS` is `ON` only for the
200   full documentation).  This should be used as a first command in a comment
201   block to exclude all the documentation.  It is possible to use \c \\internal
202   and \c \\endinternal to exclude individual paragraphs, but \c \\if `internal`
203   is preferred (see below).
204   In addition, \Gromacs-specific custom Doxygen command \c \\libinternal is
205   provided, which should be used the same way to exclude the documentation from
206   the public API documentation.  This command expands to either \c \\internal
207   or to a no-op, depending on the documentation level.
208 * Doxygen commands \c \\if and \c \\cond can be used with section names
209   `libapi` and `internal` to only include the documentation in library API and
210   the full documentation, respectively.  `libapi` is also defined in the full
211   documentation.  These are declared using `ENABLED_SECTIONS` in the Doxygen
212   configuration files.
213
214 Examples of locations where it is necessary to use these explicit commands are
215 given below in the sections on individual code constructs.
216
217 Modules as Doxygen groups
218 -------------------------
219
220 As described in \ref page_codelayout, each subdirectory under `src/gromacs/`
221 represents a _module_, i.e., a somewhat coherent collection of routines.
222 Doxygen cannot automatically generate a list of routines in a module; it only
223 extracts various alphabetical indexes that contain more or less all documented
224 functions and classes.  To help reading the documentation, the routines for a
225 module should be visible in one place.
226
227 \Gromacs uses Doxygen groups to achieve this: for each documented module, there
228 is a \c \\defgroup definition for the module, and all the relevant classes and
229 functions need to be manually added to this group using \c \\ingroup and
230 \c \\addtogroup.
231 The group page also provides a natural place for overview documentation about
232 the module, and can be navigated to directly from the "Modules" tab in the
233 generated documentation.
234
235 Some notes about using \c \\addtogroup are in order:
236 * \c \\addtogroup only adds the elements that it directly contains into the
237   group.  If it contains a namespace declaration, only the namespace is added
238   to the group, but none of the namespace contents are.  For this reason,
239   \c \\addtogroup should go within the innermost scope, around the members that
240   should actually be added.
241 * If the module should not appear in the public API documentation, its
242   definition (\c \\defgroup) should be prefixed with a \c \\libinternal.
243   In this case, also all \c \\addtogroup commands for this module should be
244   similarly prefixed.  Otherwise, they create the group in the public API
245   documentation, but without any of the content from the \c \\defgroup
246   definition.  This may also cause the contents of the \c \\addtogroup section
247   to appear in the public API documentation, even if it otherwise would not.
248
249 Public API and library API groups
250 ---------------------------------
251
252 In addition to the module groups, two fixed groups are provided:
253 `group_publicapi` and `group_libraryapi`.  Classes and files can be added to
254 these groups using \Gromacs specific custom \c \\inpublicapi and
255 \c \\inlibraryapi commands.  The generated group documentation pages are not
256 very useful, but annotated classes and files show the API definition under the
257 name, making this information more easily accessible.  These commands in
258 file-level comments are also used for some automatic intermodule dependency
259 validation (see below).
260
261 Note that functions, enumerations, and other entities that do not have a
262 separate page in the generated documentation can only belong to one group;
263 in such a case, the module group is preferred over the API group.
264
265
266 Documenting specific code constructs
267 ====================================
268
269 This section describes the techical details and some tips and tricks for
270 documenting specific code constructs such that useful documentation is
271 produced.  If you are wondering where to document a certain piece of
272 information, see the documentation structure section on \ref page_codelayout.
273 The focus of the documentation should be on the overview content: Doxygen pages
274 and the module documentation.  An experienced developer can relatively easily
275 read and understand individual functions, but the documentation should help
276 in getting the big picture.
277
278 Doxygen pages
279 -------------
280
281 The pages that are accessible through navigation from the front page are
282 written using Markdown and are located under `docs/doxygen/`.  Each page should be
283 placed in the page hierarchy by making it a subpage of another page, i.e., it
284 should be referenced once using \c \\subpage.  `mainpage.md` is the root of the
285 hierarchy.
286
287 There are two subdirectories, `user/` and `lib/`, determining the highest
288 documentation level where the page appears.  If you add pages to `lib/`, ensure
289 that there are no references to the page from public API documentation.
290 \c \\if `libapi` can be used to add references in content that is otherwise
291 public.
292 Generally, the pages should be on a high enough level and provide overview
293 content that is useful enough such that it is not necessary to exclude them
294 from the library API documentation.
295
296 Modules
297 -------
298
299 For each module, decide on a header file that is the most important one for
300 that module (if there is no self-evident header, it may be better to designate,
301 e.g., module-doc.h for this purpose, but this is currently not done for any
302 module).  This header should contain the \c \\defgroup definition for the
303 module.  The name of the group should be `module_`<em>name</em>, where _name_
304 is the name of the subdirectory that hosts the module.
305
306 The module should be added to an appropriate group (see `docs/doxygen/misc.cpp` for
307 definitions) using \c \\ingroup to organize the "Modules" tab in the generated
308 documentation.
309
310 One or more contact persons who know about the contents of the module should be
311 listed using \c \\author commands.  This provides a point of contact if one
312 has questions.
313
314 Classes/structs
315 ---------------
316
317 Classes and structs in header files appear always in Doxygen documentation,
318 even if their enclosing file is not documented.  So start the documentation
319 blocks of classes that are not part of the public API with \c \\internal or
320 \c \\libinternal.  Classes declared locally in source files or in unnamed
321 namespaces only appear in the full documentation.
322
323 If a whole class is not documented, this does not currently generate any
324 warning.  The class is simply exluded from the documentation.  But if a member
325 of a documented class is not documented, a warning is generated.  Guidelines for
326 documenting free functions apply to methods of a class as well.
327
328 For base classes, the API classification (\c \\inpublicapi or
329 \c \\inlibraryapi) should be based on where the class is meant to be
330 subclassed.  The visibility (\c \\internal or \c \\libinternal), in contrast,
331 should reflect the API classification of derived classes such that the base
332 class documentation is always generated together with the derived classes.
333
334 For classes that are meant to be subclassed and have protected members, the
335 protected members should only appear at the documentation level where the class
336 is meant to be subclassed.  For example, if a class is meant to be subclassed
337 only within a module, the protected members should only appear in the
338 full documentation.  This can be accomplished using \c \\cond (note that you
339 will need to add the \c \\cond command also to the source files to hide the
340 same methods from Doxygen, otherwise you will get confusing warnings).
341
342 Methods/functions/enums/macros
343 ------------------------------
344
345 These items do not appear in the documentation unless their enclosing scope is
346 documented.  For class members, the scope is the class; otherwise, it is the
347 namespace if one exists, or the file.  An \c \\addtogroup can also define a
348 scope if the group has higher visibility than the scope outside it.
349 So if a function is not within a namespace (mostly applicable to C code) and
350 has the same visibility as its enclosing file, it is not necessary to add a
351 \c \\internal or \c \\libinternal.
352
353 Static functions are currently extracted for all documentation flavors to allow
354 headers to declare `static inline` functions (used in, for example, math code).
355 Functions in anonymous namespaces are only extracted into the full
356 documentation.  Together with the above rules, this means that you should avoid
357 putting a `static` function within a documented namespace, even within source
358 files, or it may inadvertently appear in the public API documentation.
359
360 If you want to exclude an item from the documentation, you need to put in
361 inside a \c \\cond block such that Doxygen does not see it.
362 Otherwise, a warning for an undocumented function is generated.  You need to
363 enclose both the declaration and the definition with \c \\cond.
364
365 Files
366 -----
367
368 Each documented file should start with a documentation block (right after the
369 copyright notice) that documents the file.  See the examples section for exact
370 formatting.  Things to note:
371 * Please do not specify the file name explicitly after \c \\file.  By default,
372   a file comment applies to the file it is contained in, and an explicit file
373   name only adds one more thing that can get out of date.
374 * \c \\brief cannot appear on the same line as the \c \\file, but should be on
375   the next line.
376 * \c \\internal or \c \\libinternal should indicate where the header is visible.
377   As a general guideline, all installed headers should appear in the public API
378   documentation, i.e., not contain these commands.  If nothing else, then to
379   document that it does not contain any public API functions.  Headers that
380   declare anything in the library API should be marked with \c \\libinternal,
381   and the rest with \c \\internal.
382 * All source files, as well as most test files, should be documented with
383   \c \\internal, since they do not provide anything to public or library API,
384   and this avoids unintentionally extracting things from the file into those
385   documentations.  Shared test files used in tests from other modules should be
386   marked with \c \\libinternal.
387 * \c \\inpublicapi or \c \\inlibraryapi should be used to indicate where the
388   header is meant to be directly included.
389 * As with files, one or more contact persons should be listed with \c \\author.
390   If you make significant modifications or additions to a file, consider adding
391   an \c \\author line for yourself.
392
393 Directories
394 -----------
395
396 Directory documentation does not typically contain useful information beyond a
397 possible brief description, since they correspond very closely to modules, and
398 the modules themselves are documented.  A brief description is still useful to
399 provide a high-level overview of the source tree on the generated "Files" page.
400 A reference to the module is typically sufficient as a brief description for a
401 directory.  All directories are currently documented in
402 `docs/doxygen/directories.cpp`.
403
404
405 Examples
406 ========
407
408 Basic C++
409 ---------
410
411 Here is an example of documenting a C++ class and its containing header file.
412 Comments in the code and the actual documentation explain the used Doxygen
413 constructs.
414
415 \includelineno doxygen-example.cpp
416
417 Basic C
418 -------
419
420 Here is another example of documenting a C header file (so avoiding all
421 C++-style comments), and including free functions.  It also demonstrates the use
422 of \c \\addtogroup to add multiple functions into a module group without repeated
423 \c \\ingroup tags.
424
425 \includelineno doxygen-example.c
426
427 Scoping and visibility rules
428 ----------------------------
429
430 The rules where Doxygen expects something to be documented, and when are
431 commands like \c \\internal needed, can be complex.  The examples below
432 describe some of the pitfalls.
433
434 \includelineno doxygen-example-scoping.cpp
435
436 Module documentation
437 --------------------
438
439 Documenting a new module should place a comment like this in a central header
440 for the module, such that the "Modules" tab in the generated documentation can
441 be used to navigate to the module.
442
443 \includelineno doxygen-example-module.cpp
444
445 Common mistakes
446 ---------------
447
448 The most common mistake, in particular in C code, is to forget to document the
449 file.  This causes Doxygen to ignore most comments in the file, so it
450 does not validate the contents of the comments either, nor is it possible to
451 actually check how the generated documentation looks like.
452
453 The following example shows some other common mistakes (and some less common)
454 that do not produce correct documentation, as well as Doxygen "features"/bugs
455 that can be confusing.
456 The issues are explained in normal comments above each code fragment.
457
458 \includelineno doxygen-example-issues.cpp
459
460 Existing code
461 -------------
462
463 More examples you can find by looking at existing code in the source tree.  In
464 particular new C++ code such as that in the `src/gromacs/analysisdata/` and
465 `src/gromacs/options/` subdirectories contains a large amount of code
466 documented mostly along these guidelines.  Some comments in
467 `src/gromacs/selection/` (in particular, any C-like code) predate the
468 introduction of these guidelines, so those are not the best examples.