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!


Apologies for reviving this from the dead, but I noticed GradleRIO has a new generateCompileCommands task. I didn’t see this announced anywhere but it’s super useful. I was able to enable desktop support, generate a compile_commands.json, and plug it into clang-tidy without any modifications. It seems like there’s now no reason not to do this if you’re a C++ team! Here’s a video about clang-tidy for the uninitiated.

That’s actually been there I think since 2019. I might have added it for 2020 though, I don’t remember. I know some other devs use it to get intellisense in Vim and their favorite operating system/editor.

1 Like

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.