-
Notifications
You must be signed in to change notification settings - Fork 362
/
generate_clangtidy_resources.sh
executable file
·180 lines (163 loc) · 5.13 KB
/
generate_clangtidy_resources.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#!/usr/bin/env bash
# vim: set tabstop=2 shiftwidth=2 expandtab:
__script_name=${0##*/}
__name="${__script_name//.*}"
__log_file="${__name}.log"
__script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd)"
set -euo pipefail
IFS=$'\n\t '
exec 2>&1
mkdir -p "$(dirname "${__log_file}")" || {
>&2 echo "Failed to create log directory: $(dirname "${__log_file}")"; return 1;
}
exec &> >(tee "${__log_file}")
__llvm_dir="(none)"
function __usage_main() {
echo "${__script_name}: Build the Sonar rules from Clang Tidy documentation and Clang diagnostics."
echo "[ OPTIONS ]"
echo " -h|--help " "Print this helper."
echo " --llvm-dir " "The LLVM source directory. Default: ${__llvm_dir}."
return 0
}
function __print_args() {
local args
for arg in "${@:-}"; do args="${args:-} ${arg:-}"; done
echo "[$(pwd)] ${BASH_SOURCE[0]}${args:-}"
return 0
}
function __parse_args() {
__print_args "${@}"|| return 1
while :
do
case "${1:-}" in
-h|--help)
__usage_main
exit 0
;;
--llvm-dir)
__llvm_dir="${2:?Missing LLVM source directory path as parameter value.}"
shift 2
;;
*) # Unknown or no more options
if [[ -n ${1+x} ]] ; then
>&2 "Unknown option: ${1}"; return 1;
shift
else
break
fi
;;
esac
done
if [[ ! -d "${__llvm_dir}" ]]; then
>&2 echo "The LLVM source directory does not exist: ${__llvm_dir}"; return 1;
fi
return 0
}
function __has_python3() {
command -v python3 >/dev/null 2>&1 || {
>&2 echo "[ERROR] python3 is not available in the PATH!"
return 1
}
python3 --version
return 0
}
function __has_llvm_tblgen() {
command -v llvm-tblgen >/dev/null 2>&1 || {
>&2 echo "[ERROR] llvm-tblgen is not available in the PATH!"
return 1
}
llvm-tblgen --version
return 0
}
function __has_pandoc() {
command -v pandoc >/dev/null 2>&1 || {
>&2 echo "[ERROR] pandoc is not available in the PATH!"
return 1
}
pandoc --version
return 0
}
function __check_requirements() {
__has_python3 || return 1
__has_llvm_tblgen || return 1
__has_pandoc || return 1
return 0
}
function __generate_rules_from_clang_tidy_docs() {
local rules_new rules_old rules_comparison
local clang_tidy_checks_dir
rules_new="${__script_dir}/clangtidy_new.xml"
rules_old="${__script_dir}/../main/resources/clangtidy.xml"
rules_comparison="${__script_dir}/clangtidy-comparison.md"
clang_tidy_checks_dir="${__llvm_dir}/clang-tools-extra/docs/clang-tidy/checks"
(
cd "${__script_dir}" || {
>&2 echo "[ERROR] Failed to change directory: ${__script_dir}"; return 1;
}
echo "[INFO] generate the new version of the rules file..."
python3 clangtidy_createrules.py \
rules "${clang_tidy_checks_dir}" \
> "${rules_new}" || {
>&2 echo "[ERROR] Failed to generate the new version of the rules file: ${rules_new} !"; return 1;
}
echo "[INFO] compare the new version with the old one, extend the old XML..."
python3 utils_createrules.py \
comparerules \
"${rules_old}" "${rules_new}" \
> "${rules_comparison}" || {
>&2 echo "[ERROR] Failed to compare the new version with the old one !"; return 1;
}
) || return 1
return 0
}
function __generate_rules_from_clang_diagnostics() {
local rules_new rules_old rules_comparison
local diagnostic_json
local clang_basic_dir
rules_new="${__script_dir}/diagnostic_new.xml"
rules_old="${__script_dir}/../main/resources/clangtidy.xml"
rules_comparison="${__script_dir}/diagnostic-comparison.md"
diagnostic_json="${__script_dir}/diagnostic.json"
clang_basic_dir="${__llvm_dir}/clang/include/clang/Basic"
(
cd "${__script_dir}" || {
>&2 echo "[ERROR] Failed to change directory: ${__script_dir}"; return 1;
}
echo "[INFO] generate the list of diagnostics..."
(
cd "${clang_basic_dir}" || {
>&2 echo "[ERROR] Failed to change directory: ${clang_basic_dir}"; return 1;
}
llvm-tblgen \
-dump-json Diagnostic.td \
> "${diagnostic_json}" || {
>&2 echo "[ERROR] Failed to generate the list of diagnostics !"; return 1;
}
) || return 1
echo "[INFO] generate the new version of the diagnostics file..."
python3 clangtidy_createrules.py \
diagnostics "${diagnostic_json}" \
> "${rules_new}" || {
>&2 echo "[ERROR] Failed to generate the new version of the diagnostics file !"; return 1;
}
echo "[INFO] compare the new version with the old one, extend the old XML..."
python3 utils_createrules.py \
comparerules \
"${rules_old}" "${rules_new}" \
> "${rules_comparison}" || {
>&2 echo "[ERROR] Failed to compare the new version with the old one !"; return 1;
}
) || return 1
return 0
}
function __main() {
__parse_args "${@}" || return 1
__check_requirements || {
>&2 echo "[ERROR] Some required tools are missing."; return 1;
}
__generate_rules_from_clang_tidy_docs || return 1
__generate_rules_from_clang_diagnostics || return 1
return 0
}
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
__main "${@}"