ci(compiler): Add script for C++ linting

Add script `.github/workflows/scripts/lint_cpp.sh` that runs
`clang-tidy` with the right parameters to limit checks to source files
not generated automatically.

The script must be run from within the build directory as it relies on
`compiler_commands.json` and `CMakeFiles/CMakeDirectoryInformation.cmake`.
This commit is contained in:
Andi Drebes
2021-06-07 11:57:15 +02:00
parent 5ef78f37f3
commit bf101e5147

32
.github/workflows/scripts/lint_cpp.sh vendored Executable file
View File

@@ -0,0 +1,32 @@
#!/bin/bash
die() {
echo "$@" >&2
exit 1
}
check_buildfile() {
local FILE="$1"
[ -f "$FILE" ] ||
die "$FILE not found. Please run this script from within your build " \
"directory."
}
check_buildfile "CMakeFiles/CMakeDirectoryInformation.cmake"
check_buildfile "compile_commands.json"
# Extract toplevel source directory from CMakeDirectoryInformation.cmake
# containing a line:
#
# set(CMAKE_RELATIVE_PATH_TOP_SOURCE "...")
TOP_SRCDIR=$(grep -o 'set\s*(\s*CMAKE_RELATIVE_PATH_TOP_SOURCE\s\+"[^"]\+")' \
CMakeFiles/CMakeDirectoryInformation.cmake | \
sed 's/set\s*(\s*CMAKE_RELATIVE_PATH_TOP_SOURCE\s\+"\([^"]\+\)")/\1/g')
[ $? -eq 0 -a ! -z "$TOP_SRCDIR" ] ||
die "Could not extract CMAKE_RELATIVE_PATH_TOP_SOURCE from CMake files."
find "$TOP_SRCDIR/"{include,lib,src} \
\( -iname "*.h" -o -iname "*.cpp" -o -iname "*.cc" \) | \
xargs clang-tidy -p . -header-filter="$TOP_SRCDIR/include/.*\.h"