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. Keep every line at least below 120
11 characters. If you end up indenting very deeply, consider splitting the code
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. Namespace blocks have
25 to have a closing comment with the name of it.
29 * All source files and other non-trivial scripts should contain a copyright
30 header with a predetermined format and license information (check existing
31 files). Copyright holder should be "the |Gromacs| development team" for the
32 years where the code has been in the |Gromacs| source repository, but earlier
33 years can hold other copyrights.
34 * Whenever you update a file, you should check that the current year is listed
37 Most of the above guidelines are enforced using clang-foramt or uncrustify,
38 which are both automatic source code formatting tool. The copyright guidelines
39 are enforced by a separate Python script. See :doc:`code-formatting` for details.
40 Note that due to the nature of those scripts (they only do all-or-nothing formatting),
41 all the noted formatting rules are enforced at the same time.
43 Enforcing a consistent formatting has a few advantages:
45 * No one needs to manually review code for most of these formatting issues,
46 and people can focus on content.
47 * A separate automatic script (see below) can be applied to re-establish the
48 formatting after refactoring like renaming symbols or changing some
49 parameters, without needing to manually do it all.
51 A number of user provided set-ups are available for the correct settings of your
52 favourite text editor. They are provided for convenience only, and may not
53 exactly conform to the expectations of either formatting tool.
55 Emacs formatting set-up
56 -----------------------
57 Insert the following into your .emacs configuration file::
59 (defun gromacs-c-mode-common-hook ()
60 ;; GROMACS customizations for c-mode
62 (c-set-offset 'substatement-open 0)
63 (c-set-offset 'innamespace 0)
64 ;; other customizations can go here
66 (setq c++-tab-always-indent t)
67 (setq c-basic-offset 4) ;; Default is 2
68 (setq c-indent-level 4) ;; Default is 2
69 (setq c-file-style "stroustrup")
70 (setq tab-stop-list '(4 8 12 16 20 24 28 32 36 40 44 48 52 56 60))
72 (setq indent-tabs-mode nil) ; use tabs if t
74 (add-hook 'c-mode-common-hook 'gromacs-c-mode-common-hook)
76 (defun gromacs-c++-mode-common-hook ()
77 ;; GROMACS customizations for c++-moe
79 (c++-set-offset 'substatement-open 0)
80 (c++-set-offset 'innamespace 0)
81 ;; other customizations can go here
83 (setq c++-tab-always-indent t)
84 (setq c++-basic-offset 4) ;; Default is 2
85 (setq c++-indent-level 4) ;; Default is 2
86 (setq c++-file-style "stroustrup")
88 (setq tab-stop-list '(4 8 12 16 20 24 28 32 36 40 44 48 52 56 60))
90 (setq indent-tabs-mode nil) ; use tabs if t
93 (add-hook 'c++-mode-common-hook 'gromacs-c++-mode-common-hook)
95 This configuration is based on content from `stackoverflow`_.
97 .. _stackoverflow: http://stackoverflow.com/questions/663588/emacs-c-mode-incorrect-indentation
99 Eclipse/cdt formatting set-up
100 -----------------------------
102 For correct formatting, please use `this profile`_.
104 .. _this profile: https://gist.github.com/rolandschulz/74f4fae8985d65f33ff6