Skip to content

Instantly share code, notes, and snippets.

@clalancette
Created January 6, 2020 16:04
Show Gist options
  • Save clalancette/e01b4771d941b9e5f35a9947c091b8f7 to your computer and use it in GitHub Desktop.
Save clalancette/e01b4771d941b9e5f35a9947c091b8f7 to your computer and use it in GitHub Desktop.
clang-tidy bash script to check ROS 2 packages
#!/bin/bash -xe
# FIXME: arguments to ignore certain files
usage() {
echo "Usage: $0 [OPTIONS]"
echo " OPTIONS:"
echo " -a <arg> Add any additional arguments to the clang-tidy command-line"
echo " -c <arg> Skip additional checks"
echo " -h Show this help message"
}
if [ "$ROS_DISTRO" = "" ]; then
echo "No ROS_DISTRO defined, did you source your ROS distribution?"
exit 1
fi
if [ ! -d install ]; then
echo "The packages must be built first"
exit 2
fi
LAYOUT=$( cat install/.colcon_install_layout )
if [ "$LAYOUT" != "isolated" ]; then
echo "The build must be an isolated one"
exit 3
fi
CHECKS_TO_SKIP="fuchsia-*
cert-err58-cpp
cppcoreguidelines-pro-bounds-array-to-pointer-decay
cppcoreguidelines-pro-type-const-cast
cppcoreguidelines-pro-type-reinterpret-cast
cppcoreguidelines-pro-type-vararg
hicpp-no-array-decay
hicpp-no-assembler
hicpp-signed-bitwise
hicpp-use-auto
hicpp-vararg
llvm-header-guard
llvm-include-order
modernize-pass-by-value
modernize-use-auto
performance-unnecessary-value-param
readability-avoid-const-params-in-decls
readability-container-size-empty
readability-implicit-bool-conversion "
OPTIND=1
ADDITIONAL_ARGS=""
while getopts "a:c:h" opt ; do
case "$opt" in
a)
ADDITIONAL_ARGS+=$OPTARG
;;
c)
CHECKS_TO_SKIP+=$OPTARG
;;
h)
usage
exit 0
;;
*)
usage
exit 4
esac
done
PACKAGE_PATHS=$( find install -maxdepth 1 -mindepth 1 -type d )
INCLUDEDIRS="-I/opt/ros/$ROS_DISTRO/include "
for pkgpath in $PACKAGE_PATHS ; do
INCLUDEDIRS+="-I$( realpath $pkgpath/include ) "
done
SKIP_CHECK_COMMAND=""
for check in $CHECKS_TO_SKIP; do
SKIP_CHECK_COMMAND+="-$check,"
done
SKIP_CHECK_COMMAND=${SKIP_CHECK_COMMAND::-1}
SRCDIR=$( pwd )/src
# Find if parallel is available. If so, use it.
PARALLEL=$( which parallel )
if [ "$PARALLEL" = "" ]; then
find $SRCDIR -type f -a \( -iname '*.cpp' -o -iname '*.c' -o -iname '*.hpp' -o -iname '*.h' \) | while read i ; do
clang-tidy -warnings-as-errors='*' -checks="*,$SKIP_CHECK_COMMAND" $i -- $INCLUDEDIRS $ADDITIONAL_ARGS
done
else
find $SRCDIR -type f -a \( -iname '*.cpp' -o -iname '*.c' -o -iname '*.hpp' -o -iname '*.h' \) | parallel clang-tidy -warnings-as-errors='*' -checks="*,$SKIP_CHECK_COMMAND" {} -- $INCLUDEDIRS $ADDITIONAL_ARGS
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment