Cleaning up C++ stack code with clang-tidy

Clang-tidy ( is a clang based C++ linter tool.
It can find (potential) bugs with static analysis, cleanup and/or modernize C++ code.
Recently I have been experimenting with running it on afw and jointcal.
If anybody else is keen to try it out, here are some brief instructions to get you started.

First install clang-tidy. I haven’t found a good way of installing it on MacOS, but it is easy to install with apt-get install clang-tidy on Debian or Ubuntu.

The clang-tidy tool can be run on a file-by-file basis, but this is difficult and error-prone (you need to include the proper header files and set the right compiler flags (the same as used for a regular build).
An easier way is to use the helper and a compile command database for an entire package.
This database is really a JSON file with all the commands used to compile the package.
A modified version of sconsUtils is available on branch u/pschella/clang_database that will generate the compile commands database during a regular build.
To use it:

  • git clone
  • cd sconsUtils
  • git checkout u/pschella/clang_database
  • setup -r .

Then setup and build your stack package as normal.
It should produce a compile_commands.json file in the current directory.

Because I was lazy, this file is not actually valid JSON yet.
So you will have to edit it and put an opening bracket [ on the first line and a closing bracket ] on the last line (remembering to also remove the last comma).

Then you can run clang-tidy, with your favorite checks, on all your files at once by executing e.g. -j 16 -checks="-*,modernize-*" -fix.
The -j16 processes 16 files in parallel, -* disables the default checks and modernize-* turns all modernization checks on (e.g. replace NULL by nullptr, use auto, etc.).
Finally the -fix applies fixes automatically (some checks just tell you about potential problems but don’t offer fixes, it is well worth looking at the output).

Please note that this post does not say anything about which checks are recommended or approved (we have no policy yet), so please observe moderation and always inspect the output as some checks may produce results that are in violation of our style guide.

For a list of possible checks see: (note that these are version dependent and often have different names in older/newer versions).

Have fun!