# Cleaning up C++ stack code with clang-tidy

**URL:** <https://www.rubin.community/t/cleaning-up-c-stack-code-with-clang-tidy/2788>\
**Category:** Data Management\
**Tags:** tooling\
**Created:** [March 24, 2018, 3:27pm UTC](https://www.rubin.community/t/cleaning-up-c-stack-code-with-clang-tidy/2788 "2018-03-24T15:27:40Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![pschella](https://sea2.discourse-cdn.com/flex002/user_avatar/www.rubin.community/pschella/32/655_2.png) [@pschella](https://www.rubin.community/u/pschella)\
**Post date:** [March 24, 2018, 3:27pm UTC](https://www.rubin.community/t/cleaning-up-c-stack-code-with-clang-tidy/2788/1 "2018-03-24T15:27:41Z")

</div>

Clang-tidy ([http://clang.llvm.org/extra/clang-tidy](http://clang.llvm.org/extra/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 `run-clang-tidy-5.0.py` 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 https://github.com/lsst/sconsUtils.git`
- `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. `run-clang-tidy-5.0.py -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: [http://clang.llvm.org/extra/clang-tidy/checks/list.html](http://clang.llvm.org/extra/clang-tidy/checks/list.html) (note that these are version dependent and often have different names in older/newer versions).

Have fun!
