Code Formatting

C/C++

After resolving Bug 1118 and Bug 2357 the sources have a uniform coding formatting based on the Google Style.

The style is also defined in the .clang-format file which when used by clang-format produces a correctly formatted source file.

Some short notes:

  • Indentation: two spaces per level, no hard tabs.

  • The two-spaces indentation also applies to headers despite standard Google style here is one space.

  • 80 characters max line length.

  • Unix-style lf line endings are used in the git repository.

  • No trailing space or hard tabs in sources.

  • Sources should use 7-bit ascii. Use for example "\x00b0" rather than "°"

CMake

Cmake file uses formatting defined by the .cmake-format.yaml which when used with cmake-format produces correctly formatted files. This has been applied to all cmake/*.cmake files and CMakeLists.txt.

Using clang-format

clang is part of the llvm tools. These are usually installed using package managers like choco (Windows), brew (MacOS) or apt (Debian/Ubuntu).

To format a C, C++ or header file with clang-format with the project’s default configuration use:

clang-format -i  <filename>

Using cmake-format

Install cmake-format as described in https://github.com/cheshirekow/cmake_format. To format a single file use

cmake-format -i  <filename>

Using the git pre-commit hook

The standard git pre-commit hook avoids committing trailing whitespace, bogus line-endings etc. It needs to be activated by

cp .git/hooks/pre-commit.sample .git/hooks/pre-commit

Windows users uses copy instead of cp. Using the hook is strongly recommended.

Editor and IDE configuration

There is an open bug for creating an .editorconfig file. This file could be used by most editors to define basic settings.

There are no standard configurations available for IDEs like Visual Studio Code, Eclipse or CLion.