Check your code¶
Below, we provide tools and scripts that you can use to check the formatting of your code. Before you get started, please take a look at our detailed guidelines for C++ coding in NEST
Development environment¶
We have provided an environment.yml file that contains all packages to do development in NEST, including the tools to check coding style.
Tooling¶
The clang-format tool is built
on the clang compiler frontend. It prettyprints input files in a
configurable manner, and also has Vim and Emacs integration. We supply a
clang-format-file (<build_support/format_all_c_c++_files.sh>
to enforce some parts of the coding style. During
the code review process we check that there is no difference between the committed
files and the formatted version of the committed files.
Developers can benefit from the tool by formatting their changes before issuing a pull request. For fixing more files at once we provide a script that applies the formatting.
From the source directory call:
./build_support/format_all_c_c++_files.sh [start folder, defaults to '$PWD']
The code has to compile without warnings (in the default settings of the build infrastructure). We restrict ourselves to the C++11 standard for a larger support of compilers on various cluster systems and supercomputers.
We use clang-format version 13 in our CI. If your clang-format executable is not version 13, you need to specify an executable with version 13 explicitly with the –clang-format option to ensure consistency with the NEST CI.
Furthermore, we use Vera++, which
‘is a programmable tool for verification, analysis and transformation of C++
source code’. It enables further checks for the code complying to the coding
guidelines. We provide the vera-profile-nest (<build_support/vera++.profile>
) file in the
repository (which needs to be copied/symlinked into vera++home>/lib/vera++/profiles/
).
We then check that there are no messages generated by the execution of the following command:
vera++ -profile nest <committed file>
Finally, we let cppcheck statically analyse the committed files and check for severe errors. We require cppcheck version 1.69 or later.
cppcheck --enable=all <committed file>
Python¶
We enforce PEP8 formatting, using Black. You can automatically have your code reformatted before you commit using pre-commit hooks:
pip install pre-commit
pre-commit install
Now, whenever you commit, Black will check your code. If something was reformatted it will show up in your unstaged changes. Stage them and recommit to succesfully commit your code. Alternatively, you can run black manually:
pip install black
black .
Local static analysis¶
To run local static code checks, please refer to the “run” lines in the GitHub Actions CI definition at https://github.com/nest/nest-simulator/blob/master/.github/workflows/nestbuildmatrix.yml.