Remove cycle suppression
[gromacs.git] / docs / dev-manual / formatting.rst
blobd742d97440b30c0ba2fb107ba6c699d1dd8649c8
1 .. _code-formatting:
3 Guidelines for code formatting
4 ==============================
6 The following list provides the general formatting/indentation rules for
7 |Gromacs| code (C/C++):
9 * Basic indentation is four spaces.
10 * Keep lines at a reasonable length.  There is no hard limit, but use 80
11   characters as a guideline.  If you end up indenting very deeply,
12   consider splitting the code into functions.
13 * Do not use tabs, only spaces.  Most editors can be configured to generate
14   spaces even when pressing tab.  Tabs (in particular when mixed with spaces)
15   easily break indentation in contexts where settings are not exactly equal
16   (e.g., in ``git diff`` output).
17 * No trailing whitespace.
18 * Use braces always for delimiting blocks, even when there is only a single
19   statement in an ``if`` block or similar.
20 * Put braces on their own lines.  The only exception is short one-line inline
21   functions in C++ classes, which can be put on a single line.
22 * Use spaces liberally.
23 * ``extern "C"`` and ``namespace`` blocks are not indented, but all others
24   (including ``class`` and ``switch`` bodies) are.
26 Additionally:
28 * All source files and other non-trivial scripts should contain a copyright
29   header with a predetermined format and license information (check existing
30   files).  Copyright holder should be "the |Gromacs| development team" for the
31   years where the code has been in the |Gromacs| source repository, but earlier
32   years can hold other copyrights.
33 * Whenever you update a file, you should check that the current year is listed
34   as a copyright year.
36 Most of the above guidelines are enforced using uncrustify, an automatic source
37 code formatting tool.  The copyright guidelines are enforced by a separate
38 Python script.  See :doc:`uncrustify` for details.  Note that due to the
39 nature of uncrustify (it only does all-or-nothing formatting), it enforces
40 several additional formatting rules in addition to those above.
42 Enforcing a consistent formatting has a few advantages:
44 * No one needs to manually review code for most of these formatting issues,
45   and people can focus on content.
46 * A separate automatic script (see below) can be applied to re-establish the
47   formatting after refactoring like renaming symbols or changing some
48   parameters, without needing to manually do it all.
50 A number of user provided set-ups are available for the correct settings of your
51 favourite text editor. They are provided for convenience only, and may not
52 exactly conform to the expectations of uncrustify.
54 Emacs formatting set-up
55 -----------------------
56 Insert the following into your .emacs configuration file::
58     (defun gromacs-c-mode-common-hook ()
59     ;; GROMACS customizations for c-mode
61     (c-set-offset 'substatement-open 0)
62     (c-set-offset 'innamespace 0)
63     ;; other customizations can go here
65     (setq c++-tab-always-indent t)
66     (setq c-basic-offset 4)                  ;; Default is 2
67     (setq c-indent-level 4)                  ;; Default is 2
68     (setq c-file-style "stroustrup")
69     (setq tab-stop-list '(4 8 12 16 20 24 28 32 36 40 44 48 52 56 60))
70     (setq tab-width 4)
71     (setq indent-tabs-mode nil)  ; use tabs if t
72     )
73     (add-hook 'c-mode-common-hook 'gromacs-c-mode-common-hook)
75     (defun gromacs-c++-mode-common-hook ()
76     ;; GROMACS customizations for c++-moe
78     (c++-set-offset 'substatement-open 0)
79     (c++-set-offset 'innamespace 0)
80     ;; other customizations can go here
82     (setq c++-tab-always-indent t)
83     (setq c++-basic-offset 4)                  ;; Default is 2
84     (setq c++-indent-level 4)                  ;; Default is 2
85     (setq c++-file-style "stroustrup")
86     
87     (setq tab-stop-list '(4 8 12 16 20 24 28 32 36 40 44 48 52 56 60))
88     (setq tab-width 4)
89     (setq indent-tabs-mode nil)  ; use tabs if t
90     )
91     
92     (add-hook 'c++-mode-common-hook 'gromacs-c++-mode-common-hook)
94 This configuration is based on content from `stackoverflow`_.
96 .. _stackoverflow: http://stackoverflow.com/questions/663588/emacs-c-mode-incorrect-indentation
98 Eclipse/cdt formatting set-up
99 -----------------------------
101 For correct formatting, please use `this profile`_.
103 .. _this profile: https://gist.github.com/rolandschulz/74f4fae8985d65f33ff6