|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# We need this export to enable color output to the terminal using GitHub |
| 4 | +# Actions. If no, we will get the error while using commands in bash such |
| 5 | +# as `tput`. |
| 6 | +export TERM=xterm-color |
| 7 | + |
| 8 | +# Check if there is clang-tidy installed. |
| 9 | +clang-tidy --version >/dev/null |
| 10 | +if [[ ${?} != 0 ]]; then |
| 11 | + tput setaf 1 # Red font in terminal. |
| 12 | + printf "Error: failed to find 'clang-tidy'.\n" |
| 13 | + tput sgr0 # Make font be default in terminal. |
| 14 | + printf "Make sure you have clang-tidy installed!\n" |
| 15 | + exit 1 |
| 16 | +fi |
| 17 | + |
| 18 | +# Check for existence of compile_commands.json. |
| 19 | +IFS=: |
| 20 | +compilation_database=$(find . -name 'compile_commands.json') |
| 21 | +unset IFS |
| 22 | + |
| 23 | +if [[ ${#compilation_database} == 0 ]]; then |
| 24 | + tput setaf 1 # Red font in terminal. |
| 25 | + printf "Error: there is no compilation database " |
| 26 | + printf "(compile_commands.json) in your workspace!\n" |
| 27 | + tput sgr0 # Make font be default in terminal. |
| 28 | + exit 1 |
| 29 | +fi |
| 30 | + |
| 31 | +# Check for existence of .clang-tidy. |
| 32 | +IFS=: |
| 33 | +clang_tidy_config_file=$(find . -name '\.clang-tidy') |
| 34 | +unset IFS |
| 35 | + |
| 36 | +if [[ ${#clang_tidy_config_file} == 0 ]]; then |
| 37 | + tput setaf 1 # Red font in terminal. |
| 38 | + printf "Error: there is no .clang-tidy config file in your workspace!\n" |
| 39 | + tput sgr0 # Make font be default in terminal. |
| 40 | + exit 1 |
| 41 | +fi |
| 42 | + |
| 43 | +# Find all source files (.cc|.cxx|.cpp|.c) we want to check with clang-tidy. |
| 44 | +# We do not include headers since clang-tidy has `--header-filter` option or |
| 45 | +# `HeaderFilterRegex` option (in .clang-tidy). With the help of this option |
| 46 | +# we can easily grab all warnings|errors from headers included in the source |
| 47 | +# files. |
| 48 | +IFS=: |
| 49 | +source_files=$(find . -name '*.cc' -o -name '*.cpp' -o -name '*.cxx') |
| 50 | +unset IFS |
| 51 | + |
| 52 | +# Exit with success if there is no work to do. |
| 53 | +if [[ ${#source_files} == 0 ]]; then |
| 54 | + tput setaf 2 # Green font in terminal. |
| 55 | + printf "There are no source files to check with clang-tidy!\n" |
| 56 | + tput sgr0 # Reset terminal. |
| 57 | + exit 0 |
| 58 | +fi |
| 59 | + |
| 60 | +status_exit=0 |
| 61 | + |
| 62 | +# Run clang-tidy checks for every file. |
| 63 | +for file in ${source_files} |
| 64 | +do |
| 65 | + printf "Run clang-tidy on ${file} ...\n" |
| 66 | + |
| 67 | + clang-tidy --config-file="${clang_tidy_config_file}" \ |
| 68 | + -p ${compilation_database} ${file} -- -std=c++17 \ |
| 69 | + -I$(bazel info workspace) \ |
| 70 | + -I$(bazel info workspace)/bazel-bin/external/com_github_google_glog/src \ |
| 71 | + -I$(bazel info workspace)/bazel-bin/external/com_github_google_glog/_virtual_includes/glog \ |
| 72 | + -I$(bazel info workspace)/bazel-bin/external/com_github_gflags_gflags/_virtual_includes/gflags \ |
| 73 | + -I$(bazel info workspace)/bazel-bin/external/com_github_libuv_libuv/libuv/include \ |
| 74 | + -I$(bazel info workspace)/external/com_github_google_googletest/googlemock/include \ |
| 75 | + -I$(bazel info workspace)/external/com_github_google_googletest/googletest/include \ |
| 76 | + -I$(bazel info workspace)/external/com_github_curl_curl/include \ |
| 77 | + -I$(bazel info workspace)/external/com_github_chriskohlhoff_asio/asio/include \ |
| 78 | + -I$(bazel info workspace)/external/boringssl/src/include |
| 79 | + |
| 80 | + clang_tidy_status=$(echo $?) |
| 81 | + if [[ ${clang_tidy_status} != 0 ]] |
| 82 | + then |
| 83 | + tput setaf 1 # Red font in terminal. |
| 84 | + printf "Error: ${file} needs to be fixed from clang-tidy warnings.\n" |
| 85 | + tput sgr0 # Reset terminal. |
| 86 | + status_exit=1 |
| 87 | + fi |
| 88 | +done |
| 89 | + |
| 90 | +exit ${status_exit} |
0 commit comments