Skip to content

Add C++ code formatting

Daniel Joseph Antrim requested to merge add_formatting into main

What

This MR adds to the CI a new stage which checks the C++ code format using clang-format and cpplint. This is done in the script bash/check_formatting.sh, which gets called in the CI.

The cpplint rules and filters are defined in CPPLINT.cfg and those for clang-format are in .clang-format. The code style is a slightly modified version of google format.

When the CI runs, it essentially calls:

source bash/check_formatting.sh

This script will look at all *.cpp and *.h files in the src/ directory (recursively through all sub-directories in src/) and run cpplint over them. If the file fails the cpplint rules, the rules failing will be reported. When a file fails the cpplint formatting rules, the script run-clang-format from Sarcasm/run-clang-format is run on the file, which reports a diff between the file with the failing format and the file with the formatting errors fixed. This only shows the suggested fixes, it does not apply them. In this way, the output from the CI will show explicitly what the errors are and what the changes should be. An example of the CI running and reporting an error is shown here: ci_format_check

If a user finds formatting conflicts, they can run:

source bash/check_formatting.sh --apply

which will apply the fixes to the files in-place. To check that they are applied correctly, subsequent calls to source bash/check_formatting.sh should report success. They will then have to do git add over the changed files and push a new commit with the fixes applied.

CI Stages

There are now two stages to the CI: 1) format_check, and 2) build. The formatting check is done first and the build stage is not run if the committed code fails the formatting check.

Edited by Daniel Joseph Antrim

Merge request reports