Applying Clang-Tidy to ROS packages#
Clang-Tidy is a powerful C++ linter.
Prerequisites#
Install fd-find
to make it easier to feed file lists to run-clang-tidy
.
More info can be found in the fd documentation.
sudo apt-get install fd-find
Install clang-tidy
and clang-apply-replacements
.
sudo apt-get install clang-tidy clang-tools
If you encounter clang-diagnostic-error
, try installing libomp-dev
.
Related: https://github.com/autowarefoundation/autoware-github-actions/pull/172
sudo apt-get install libomp-dev
Preparation#
You need to generate build/compile_commands.json
before using Clang-Tidy.
colcon build --cmake-args -DCMAKE_EXPORT_COMPILE_COMMANDS=1
Usage#
If you are on Ubuntu 22.04, run the following command only once to help run-clang-tidy
find the correct include paths.
Reference: autoware/pull/5543.
sed -i '/- -Wno-c11-extensions/a\ - -I/usr/include/c++/11\n - -I/usr/include/x86_64-linux-gnu/c++/11' .clang-tidy-ci
To run Clang-Tidy with all files in a package and export fixes to a YAML file, run the following command from the workspace root.
Here we use autoware_utils
as an example package name.
cd autoware/
run-clang-tidy -p build/ -config="$(cat .clang-tidy-ci)" -export-fixes clang-tidy-fixes.yaml -j $(nproc) $(fdfind -e cpp -e hpp --full-path "/autoware_utils/") > clang-tidy-report.log
To apply fixes directly, run the following command.
run-clang-tidy -p build/ -config="$(cat .clang-tidy-ci)" -j $(nproc) -fix $(fdfind -e cpp -e hpp --full-path "/autoware_utils/")
clang-apply-replacements
command can also be used to apply fixes from a YAML file.
IDE integration#
CLion#
Refer to the CLion Documentation.
Visual Studio Code#
Use either one of the following extensions: