Skip to content

Dev: Useful tools

stuart-knock edited this page Dec 2, 2016 · 6 revisions

To analyse a source file (in this example main.cpp) and all its associated non-system headers for issues:

clang-tidy src/main.cpp -checks=*,-clang-analyzer-alpha*,-llvm-header-guard,-google-global-names-in-headers -header-filter=.* -- -std=c++11

Here we enable all checks (*), then explicitly exclude a few we don't want (,-). And after the -- we specify compilation options we want considered.

You can read through the warnings and errors produced and fix them manually, alternatively you can automatically fix some issues with the clang-tidy '-fix' flag. NOTE: this does not work perfectly and will sometimes require manual correction of the fixes.
To automatically fix the identified issues that clang-tidy thinks it can automatically fix:

clang-tidy src/main.cpp -checks=*,-clang-analyzer-alpha*,-llvm-header-guard,-google-global-names-in-headers -header-filter=.* -fix -- -std=c++11

At this point you should run the analysis step again to see what issues were not automatically fixed or if the automatic fix actually broke something.

We are using the predefined 'google' style with 2 space indenting.
astyle --style=google --indent=spaces=2

To look at the changes before applying them:
astyle --style=google --indent=spaces=2 < src/main.cpp | less

To compare changed with original (assumes you're using BASH), again, before applying:

diff --side-by-side --width=200 --suppress-common-lines <(astyle --style=google --indent=spaces=2 < src/main.cpp) src/main.cpp

And finally, once you've checked and are satisfied with the results, to actually make the change:
astyle --style=google --indent=spaces=2 src/main.cpp

For a complete check, from the base neurofield directory run:
cppcheck --verbose --enable=all --language=c++ --std=c++11 ./src/