From 9366581ffde4bbc494d0c60d7a945816df70d6c0 Mon Sep 17 00:00:00 2001 From: archi Date: Wed, 19 Jan 2022 15:57:23 +0300 Subject: [PATCH 1/2] Generate compilation database for clang-tidy tool --- .../generate_compilation_database.sh | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 compilation-database/generate_compilation_database.sh diff --git a/compilation-database/generate_compilation_database.sh b/compilation-database/generate_compilation_database.sh new file mode 100644 index 0000000..c101a8f --- /dev/null +++ b/compilation-database/generate_compilation_database.sh @@ -0,0 +1,17 @@ +#!/bin/bash + +INSTALL_DIR="/dev-tools" +VERSION="0.5.2" + +# Check if `generate.py` is already present. +which ${INSTALL_DIR}/bazel-compilation-database-${VERSION}/generate.py &> /dev/null || ( +# Download all source files for generating compilation database. + cd "${INSTALL_DIR}" \ + && curl -L "https://github.com/grailbio/bazel-compilation-database/archive/${VERSION}.tar.gz" | tar -xz +) + +# Run python script which generates compilation database. +# We should add `--action_env=CC=clang` cause we need clang +# for using clang-tidy. This script should be executed in +# the root directory containing `WORKSPACE.bazel`. +${INSTALL_DIR}/bazel-compilation-database-${VERSION}/generate.py -- --action_env=CC=clang From ce87bbbb783b28e3fadda179bfddf3412522ec6c Mon Sep 17 00:00:00 2001 From: archi Date: Wed, 19 Jan 2022 18:01:17 +0300 Subject: [PATCH 2/2] Adding 'clang_tidy_review.sh' and update compdb Added bash script 'clang_tidy_review.sh' for running clang-tidy on source files and update install directory for compilation database. --- clang-tidy-review/clang_tidy_review.sh | 90 +++++++++++++++++++ .../generate_compilation_database.sh | 4 +- 2 files changed, 92 insertions(+), 2 deletions(-) create mode 100644 clang-tidy-review/clang_tidy_review.sh rename {compilation-database => clang-tidy-review}/generate_compilation_database.sh (89%) mode change 100644 => 100755 diff --git a/clang-tidy-review/clang_tidy_review.sh b/clang-tidy-review/clang_tidy_review.sh new file mode 100644 index 0000000..0f13bc8 --- /dev/null +++ b/clang-tidy-review/clang_tidy_review.sh @@ -0,0 +1,90 @@ +#!/bin/bash + +# We need this export to enable color output to the terminal using GitHub +# Actions. If no, we will get the error while using commands in bash such +# as `tput`. +export TERM=xterm-color + +# Check if there is clang-tidy installed. +clang-tidy --version >/dev/null +if [[ ${?} != 0 ]]; then + tput setaf 1 # Red font in terminal. + printf "Error: failed to find 'clang-tidy'.\n" + tput sgr0 # Make font be default in terminal. + printf "Make sure you have clang-tidy installed!\n" + exit 1 +fi + +# Check for existence of compile_commands.json. +IFS=: +compilation_database=$(find . -name 'compile_commands.json') +unset IFS + +if [[ ${#compilation_database} == 0 ]]; then + tput setaf 1 # Red font in terminal. + printf "Error: there is no compilation database " + printf "(compile_commands.json) in your workspace!\n" + tput sgr0 # Make font be default in terminal. + exit 1 +fi + +# Check for existence of .clang-tidy. +IFS=: +clang_tidy_config_file=$(find . -name '\.clang-tidy') +unset IFS + +if [[ ${#clang_tidy_config_file} == 0 ]]; then + tput setaf 1 # Red font in terminal. + printf "Error: there is no .clang-tidy config file in your workspace!\n" + tput sgr0 # Make font be default in terminal. + exit 1 +fi + +# Find all source files (.cc|.cxx|.cpp|.c) we want to check with clang-tidy. +# We do not include headers since clang-tidy has `--header-filter` option or +# `HeaderFilterRegex` option (in .clang-tidy). With the help of this option +# we can easily grab all warnings|errors from headers included in the source +# files. +IFS=: +source_files=$(find . -name '*.cc' -o -name '*.cpp' -o -name '*.cxx') +unset IFS + +# Exit with success if there is no work to do. +if [[ ${#source_files} == 0 ]]; then + tput setaf 2 # Green font in terminal. + printf "There are no source files to check with clang-tidy!\n" + tput sgr0 # Reset terminal. + exit 0 +fi + +status_exit=0 + +# Run clang-tidy checks for every file. +for file in ${source_files} +do + printf "Run clang-tidy on ${file} ...\n" + + clang-tidy --config-file="${clang_tidy_config_file}" \ + -p ${compilation_database} ${file} -- -std=c++17 \ + -I$(bazel info workspace) \ + -I$(bazel info workspace)/bazel-bin/external/com_github_google_glog/src \ + -I$(bazel info workspace)/bazel-bin/external/com_github_google_glog/_virtual_includes/glog \ + -I$(bazel info workspace)/bazel-bin/external/com_github_gflags_gflags/_virtual_includes/gflags \ + -I$(bazel info workspace)/bazel-bin/external/com_github_libuv_libuv/libuv/include \ + -I$(bazel info workspace)/external/com_github_google_googletest/googlemock/include \ + -I$(bazel info workspace)/external/com_github_google_googletest/googletest/include \ + -I$(bazel info workspace)/external/com_github_curl_curl/include \ + -I$(bazel info workspace)/external/com_github_chriskohlhoff_asio/asio/include \ + -I$(bazel info workspace)/external/boringssl/src/include + + clang_tidy_status=$(echo $?) + if [[ ${clang_tidy_status} != 0 ]] + then + tput setaf 1 # Red font in terminal. + printf "Error: ${file} needs to be fixed from clang-tidy warnings.\n" + tput sgr0 # Reset terminal. + status_exit=1 + fi +done + +exit ${status_exit} diff --git a/compilation-database/generate_compilation_database.sh b/clang-tidy-review/generate_compilation_database.sh old mode 100644 new mode 100755 similarity index 89% rename from compilation-database/generate_compilation_database.sh rename to clang-tidy-review/generate_compilation_database.sh index c101a8f..2e03d19 --- a/compilation-database/generate_compilation_database.sh +++ b/clang-tidy-review/generate_compilation_database.sh @@ -1,6 +1,6 @@ #!/bin/bash -INSTALL_DIR="/dev-tools" +INSTALL_DIR="$(bazel info workspace)/dev-tools" VERSION="0.5.2" # Check if `generate.py` is already present. @@ -14,4 +14,4 @@ which ${INSTALL_DIR}/bazel-compilation-database-${VERSION}/generate.py &> /dev/n # We should add `--action_env=CC=clang` cause we need clang # for using clang-tidy. This script should be executed in # the root directory containing `WORKSPACE.bazel`. -${INSTALL_DIR}/bazel-compilation-database-${VERSION}/generate.py -- --action_env=CC=clang +${INSTALL_DIR}/bazel-compilation-database-${VERSION}/generate.py -s -- --action_env=CC=clang