Skip to content

Commit ce87bbb

Browse files
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.
1 parent 9366581 commit ce87bbb

File tree

2 files changed

+92
-2
lines changed

2 files changed

+92
-2
lines changed
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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}

compilation-database/generate_compilation_database.sh renamed to clang-tidy-review/generate_compilation_database.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/bin/bash
22

3-
INSTALL_DIR="/dev-tools"
3+
INSTALL_DIR="$(bazel info workspace)/dev-tools"
44
VERSION="0.5.2"
55

66
# Check if `generate.py` is already present.
@@ -14,4 +14,4 @@ which ${INSTALL_DIR}/bazel-compilation-database-${VERSION}/generate.py &> /dev/n
1414
# We should add `--action_env=CC=clang` cause we need clang
1515
# for using clang-tidy. This script should be executed in
1616
# the root directory containing `WORKSPACE.bazel`.
17-
${INSTALL_DIR}/bazel-compilation-database-${VERSION}/generate.py -- --action_env=CC=clang
17+
${INSTALL_DIR}/bazel-compilation-database-${VERSION}/generate.py -s -- --action_env=CC=clang

0 commit comments

Comments
 (0)