Using clang-tidy with GradleRIO

I wanted to run some clang-tidy checks on some robot code and it wasn’t immediately obvious how to do this. Here’s the steps I had to take to get things somewhat-working.

  • Before getting started, make sure you have a pretty recent build of clang-tidy. I used a nightly build.
  • Build and install Bear from source.
  • Run ./gradlew clean in your project dir.
  • Run bear --use-c++ cc1plus ./gradlew build to generate a compilation database.
  • Clang doesn’t understand a couple of options that the build uses, so we need to manually clean up the database. Just run sed -i 's/-mtls-dialect=gnu//' compile_commands.json and sed -i 's/-quiet//' compile_commands.json to remove them.
  • We can run clang-tidy now, just make sure to pass -p . and --extra-arg=--target=arm when invoking it. For example: clang-tidy -p . --extra-arg=--target=arm --checks='performance-*' $(find . -name '*.cpp') will run performance checks on all cpp files in the current tree.

Note that Bear won’t work on macOS with SIP enabled or Linux with selinux enabled. Otherwise, I’ve gotten it to work on Linux and WSL.

Running the actual checks can be pretty slow. Any ideas to speed it up are appreciated - there’s probably some option for caching I’m not using.

Happy checking!