This repository has been archived by the owner on Aug 17, 2023. It is now read-only.
forked from envoyproxy/envoy
-
Notifications
You must be signed in to change notification settings - Fork 2
/
run_clang_tidy.sh
executable file
·126 lines (105 loc) · 4.26 KB
/
run_clang_tidy.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#!/bin/bash
set -eo pipefail
# ENVOY_SRCDIR should point to where Envoy source lives, while SRCDIR could be a downstream build
# (for example envoy-filter-example).
[[ -z "${ENVOY_SRCDIR}" ]] && ENVOY_SRCDIR="${PWD}"
[[ -z "${SRCDIR}" ]] && SRCDIR="${ENVOY_SRCDIR}"
export LLVM_CONFIG=${LLVM_CONFIG:-llvm-config}
LLVM_PREFIX=${LLVM_PREFIX:-$(${LLVM_CONFIG} --prefix)}
CLANG_TIDY=${CLANG_TIDY:-$(${LLVM_CONFIG} --bindir)/clang-tidy}
CLANG_APPLY_REPLACEMENTS=${CLANG_APPLY_REPLACEMENTS:-$(${LLVM_CONFIG} --bindir)/clang-apply-replacements}
FIX_YAML=clang-tidy-fixes.yaml
# Quick syntax check of .clang-tidy.
${CLANG_TIDY} -dump-config > /dev/null 2> clang-tidy-config-errors.txt
if [[ -s clang-tidy-config-errors.txt ]]; then
cat clang-tidy-config-errors.txt
rm clang-tidy-config-errors.txt
exit 1
fi
rm clang-tidy-config-errors.txt
echo "Generating compilation database..."
# bazel build need to be run to setup virtual includes, generating files which are consumed
# by clang-tidy
read -ra COMP_DB_TARGETS <<< "$COMP_DB_TARGETS"
"${ENVOY_SRCDIR}/tools/gen_compilation_database.py" --include_headers "${COMP_DB_TARGETS[@]}"
# Do not run clang-tidy against win32 impl
# TODO(scw00): We should run clang-tidy against win32 impl once we have clang-cl support for Windows
function exclude_win32_impl() {
grep -v source/common/filesystem/win32/ | grep -v source/common/common/win32 | grep -v source/exe/win32 | grep -v source/common/api/win32 | grep -v source/common/event/win32
}
# Do not run clang-tidy against macOS impl
# TODO: We should run clang-tidy against macOS impl for completeness
function exclude_macos_impl() {
grep -v source/common/filesystem/kqueue/ | grep -v source/common/network/apple_dns_impl | grep -v test/common/network/apple_dns_impl_test
}
# Do not run incremental clang-tidy on check_format testdata files.
function exclude_check_format_testdata() {
grep -v tools/testdata/check_format/
}
# Do not run clang-tidy against Chromium URL import, this needs to largely
# reflect the upstream structure.
function exclude_chromium_url() {
grep -v source/common/chromium_url/
}
# Exclude files in third_party which are temporary forks from other OSS projects.
function exclude_third_party() {
grep -v third_party/
}
# Exclude files which are part of the Wasm emscripten environment
function exclude_wasm_emscripten() {
grep -v source/extensions/common/wasm/ext
}
# Exclude files which are part of the Wasm SDK
function exclude_wasm_sdk() {
grep -v proxy_wasm_cpp_sdk
}
# Exclude files which are part of the Wasm Host environment
function exclude_wasm_host() {
grep -v proxy_wasm_cpp_host
}
# Exclude proxy-wasm test_data.
function exclude_wasm_test_data() {
grep -v wasm/test_data
}
# Exclude files which are part of the Wasm examples
function exclude_wasm_examples() {
grep -v examples/wasm
}
function filter_excludes() {
exclude_check_format_testdata | exclude_chromium_url | exclude_win32_impl | exclude_macos_impl | exclude_third_party | exclude_wasm_emscripten | exclude_wasm_sdk | exclude_wasm_host | exclude_wasm_test_data | exclude_wasm_examples
}
function run_clang_tidy() {
python3 "${LLVM_PREFIX}/share/clang/run-clang-tidy.py" \
-clang-tidy-binary="${CLANG_TIDY}" \
-clang-apply-replacements-binary="${CLANG_APPLY_REPLACEMENTS}" \
-export-fixes=${FIX_YAML} -j "${NUM_CPUS:-0}" -p "${SRCDIR}" -quiet \
${APPLY_CLANG_TIDY_FIXES:+-fix} "$@"
}
function run_clang_tidy_diff() {
git diff "$1" | filter_excludes | \
python3 "${LLVM_PREFIX}/share/clang/clang-tidy-diff.py" \
-clang-tidy-binary="${CLANG_TIDY}" \
-export-fixes="${FIX_YAML}" -j "${NUM_CPUS:-0}" -p 1 -quiet
}
if [[ $# -gt 0 ]]; then
echo "Running clang-tidy on: $*"
run_clang_tidy "$@"
elif [[ "${RUN_FULL_CLANG_TIDY}" == 1 ]]; then
echo "Running a full clang-tidy"
run_clang_tidy
else
if [[ -z "${DIFF_REF}" ]]; then
if [[ "${BUILD_REASON}" == *CI ]]; then
DIFF_REF="HEAD^"
else
DIFF_REF=$("${ENVOY_SRCDIR}"/tools/git/last_github_commit.sh)
fi
fi
echo "Running clang-tidy-diff against ${DIFF_REF} ($(git rev-parse "${DIFF_REF}")), current HEAD ($(git rev-parse HEAD))"
run_clang_tidy_diff "${DIFF_REF}"
fi
if [[ -s "${FIX_YAML}" ]]; then
echo "clang-tidy check failed, potentially fixed by clang-apply-replacements:"
cat "${FIX_YAML}"
exit 1
fi