Skip to content

Commit 71de237

Browse files
shellcheck: extras/clang-checker.sh
This series aim to reduce the shellcheck linter build logs. This commit ddresses many many linter complaints and mistakes. Signed-off-by: Thales Antunes de Oliveira Barretto <[email protected]>
1 parent 255889b commit 71de237

File tree

1 file changed

+89
-91
lines changed

1 file changed

+89
-91
lines changed

extras/clang-checker.sh

Lines changed: 89 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -31,43 +31,43 @@
3131
# *
3232
#*******************************************************************************
3333

34-
REPORTS_DIR=$(pwd)
35-
BASELINE_DIR=${REPORTS_DIR}/baseline
36-
BRESULTS_DIR=${BASELINE_DIR}/results
37-
BBACKUP_DIR=${BASELINE_DIR}/backup
38-
TARGET_DIR=${REPORTS_DIR}/target
39-
TRESULTS_DIR=${TARGET_DIR}/results
40-
TBACKUP_DIR=${TARGET_DIR}/backup
34+
REPORTS_DIR="$(pwd)"
35+
BASELINE_DIR="${REPORTS_DIR}/baseline"
36+
BRESULTS_DIR="${BASELINE_DIR}/results"
37+
BBACKUP_DIR="${BASELINE_DIR}/backup"
38+
TARGET_DIR="${REPORTS_DIR}/target"
39+
TRESULTS_DIR="${TARGET_DIR}/results"
40+
TBACKUP_DIR="${TARGET_DIR}/backup"
4141

4242
declare -A DICT_B
4343
declare -A DICT_T
44-
declare -A ARR
45-
declare -A FILES
44+
declare -a ARR
45+
declare -a FILES
4646

4747
function identify_changes () {
4848
MODIFIED_DATA=$(git show --name-status --oneline | tail -n +2)
4949
FLAG=0
5050
for i in ${MODIFIED_DATA}; do
5151
if [ $FLAG -eq 1 ]; then
52-
ARR+="$(dirname $i) ";
52+
ARR+=( "$(dirname "${i}") ");
5353
FLAG=0;
5454
fi
55-
if [ $i = 'M' ] || [ $i = 'A' ]; then
55+
if [ "${i}" = 'M' ] || [ "${i}" = 'A' ]; then
5656
FLAG=1;
5757
fi
5858
done
5959

6060
MODIFIED_DIR=$(echo "${ARR[@]}" | tr ' ' '\n' | sort -u | tr '\n' ' ')
6161
for i in $MODIFIED_DIR; do
6262
# run only in directories which has Makefile
63-
if [ $(find ./$i -iname "makefile*" | wc -c) -gt 0 ]; then
63+
if [ "$(find ./"${i}" -iname "makefile*" | wc -c)" -gt 0 ]; then
6464
# skip 'doc' and '.'(top) directory
65-
if [ "xx$i" != "xxdoc" ] && [ "xx$i" != "xx." ]; then
66-
FILES+="$i "
65+
if [ "xx${i}" != "xxdoc" ] && [ "xx${i}" != "xx." ]; then
66+
FILES+=( "${i} " )
6767
fi
6868
fi
6969
done
70-
if [ -z $FILES ]; then
70+
if [ -z "${FILES[*]}" ]; then
7171
echo "Probably no changes made to 'c' files"
7272
exit;
7373
fi
@@ -76,12 +76,12 @@ function identify_changes () {
7676
function check_prerequisites () {
7777
if ! type "clang" 2> /dev/null; then
7878
echo -e "\ntry after installing clang and scan-build..."
79-
echo "useful info at http://clang-analyzer.llvm.org/installation.html\n"
79+
echo -e "useful info at http://clang-analyzer.llvm.org/installation.html\n"
8080
echo -e "hint: 'dnf -y install clang-analyzer.noarch'\n"
8181
exit 1;
8282
elif ! type "scan-build" 2> /dev/null; then
8383
echo -e "\ntry after installing scan-build..."
84-
echo "useful info at http://clang-analyzer.llvm.org/installation.html"
84+
echo -e "useful info at http://clang-analyzer.llvm.org/installation.html"
8585
echo -e "hint: 'dnf -y install clang-analyzer.noarch'\n"
8686
exit 1;
8787
fi
@@ -90,34 +90,34 @@ function check_prerequisites () {
9090
function force_terminate () {
9191
echo -e "\nreceived a signal to force terminate ..\n"
9292
git am --abort 2> /dev/null
93-
git am ${PATCH_NAME}
94-
rm -f ${REPORTS_DIR}/${PATCH_NAME}
93+
git am "${PATCH_NAME}"
94+
rm -f "${REPORTS_DIR}"/"${PATCH_NAME}"
9595
exit 1;
9696
}
9797

9898
function run_scanbuild () {
99-
local CLANG=$(which clang)
100-
local SCAN_BUILD=$(which scan-build)
101-
local ORIG_COMMIT=$(git rev-parse --verify HEAD^)
102-
PATCH_NAME=$(git format-patch $ORIG_COMMIT)
99+
local CLANG="$(which clang)"
100+
local SCAN_BUILD="$(which scan-build)"
101+
local ORIG_COMMIT="$(git rev-parse --verify HEAD^)"
102+
PATCH_NAME="$(git format-patch "${ORIG_COMMIT}")"
103103

104104
echo -e "\n| Performing clang analysis on:" \
105105
"$(git log --pretty=format:"%h - '%s' by %an" -1) ... |\n"
106-
echo -e "Changes are identified in '${FILES[@]}' directorie[s]\n"
106+
echo -e "Changes are identified in '${FILES[*]}' directorie[s]\n"
107107

108108
if [ -d "${BRESULTS_DIR}" ]; then
109-
mkdir -p ${BBACKUP_DIR} ${TBACKUP_DIR}
110-
mv ${BRESULTS_DIR} \
111-
${BBACKUP_DIR}/results_$(ls -l ${BBACKUP_DIR} | wc -l)
112-
mv ${TRESULTS_DIR} \
113-
${TBACKUP_DIR}/results_$(ls -l ${TBACKUP_DIR} | wc -l)
109+
mkdir -p "${BBACKUP_DIR}" "${TBACKUP_DIR}"
110+
mv "${BRESULTS_DIR}" \
111+
"${BBACKUP_DIR}/results_$(ls -l "${BBACKUP_DIR}" | wc -l)"
112+
mv "${TRESULTS_DIR}" \
113+
"${TBACKUP_DIR}/results_$(ls -l "${TBACKUP_DIR}" | wc -l)"
114114
fi
115-
for DIR in ${FILES[@]}; do
116-
mkdir -p ${BRESULTS_DIR}/$(echo ${DIR} | sed 's/\//_/g')
117-
mkdir -p ${TRESULTS_DIR}/$(echo ${DIR} | sed 's/\//_/g')
115+
for DIR in "${FILES[@]}" ; do
116+
mkdir -p "${BRESULTS_DIR}"/"$(echo "${DIR}" | sed 's/\//_/g')"
117+
mkdir -p "${TRESULTS_DIR}"/"$(echo "${DIR}" | sed 's/\//_/g')"
118118
done
119119
# get nproc info
120-
case $(uname -s) in
120+
case "$(uname -s)" in
121121
'Linux')
122122
local NPROC=$(getconf _NPROCESSORS_ONLN)
123123
;;
@@ -132,110 +132,110 @@ function run_scanbuild () {
132132

133133
# build complete source code for sake of dependencies
134134
echo -e "\n# make -j${NPROC} ..."
135-
make -j${NPROC} 1>/dev/null
135+
make -j"${NPROC}" 1>/dev/null
136136

137-
for DIR in ${FILES[@]}; do
138-
if [ $(find ./$i -iname "makefile*" | wc -c) -gt 0 ]; then
139-
make clean -C ${DIR} 1>/dev/null
137+
for DIR in "${FILES[@]}" ; do
138+
if [ "$(find ./"${i}" -iname "makefile*" | wc -c)" -gt 0 ]; then
139+
make clean -C "${DIR}" 1>/dev/null
140140
echo -e "\n| Analyzing ${DIR} without commit ... |\n"
141141
# run only in directory where changes are made
142-
${SCAN_BUILD} -o ${BRESULTS_DIR}/$(echo ${DIR} | sed 's/\//_/g') \
143-
--use-analyzer=${CLANG} make -j${NPROC} -C ${DIR}
142+
${SCAN_BUILD} -o "${BRESULTS_DIR}/$(echo "${DIR}" | sed 's/\//_/g')" \
143+
--use-analyzer="${CLANG}" make -j"${NPROC}" -C "${DIR}"
144144
fi
145145
done
146146

147147
echo -e "\n| Analyzing without commit complete ... |\n"
148148

149-
git am ${PATCH_NAME}
149+
git am "${PATCH_NAME}"
150150
trap - INT TERM QUIT EXIT
151151

152152
# In case commit has changes to configure stuff ?
153153
echo -e "\n# make clean ..."
154154
make clean 1>/dev/null
155155
echo -e "\n# ./autogen.sh && ./configure --with-previous-options ..."
156-
${REPORTS_DIR}/autogen.sh 2>/dev/null
157-
${REPORTS_DIR}/configure --with-previous-options 1>/dev/null
156+
"${REPORTS_DIR}"/autogen.sh 2>/dev/null
157+
"${REPORTS_DIR}"/configure --with-previous-options 1>/dev/null
158158
echo -e "\n# make -j${NPROC} ..."
159-
make -j${NPROC} 1>/dev/null
159+
make -j"${NPROC}" 1>/dev/null
160160

161-
for DIR in ${FILES[@]}; do
162-
if [ $(find ./$i -iname "makefile*" | wc -c) -gt 0 ]; then
163-
make clean -C ${DIR} 1>/dev/null
161+
for DIR in "${FILES[@]}" ; do
162+
if [ "$(find ./"${i}" -iname "makefile*" | wc -c)" -gt 0 ]; then
163+
make clean -C "${DIR}" 1>/dev/null
164164
echo -e "\n| Analyzing ${DIR} with commit ... |\n"
165165
# run only in directory where changes are made
166-
${SCAN_BUILD} -o ${TRESULTS_DIR}/$(echo ${DIR} | sed 's/\//_/g') \
167-
--use-analyzer=${CLANG} make -j${NPROC} -C ${DIR}
166+
${SCAN_BUILD} -o "${TRESULTS_DIR}/$(echo "${DIR}" | sed 's/\//_/g')" \
167+
--use-analyzer="${CLANG}" make -j"${NPROC}" -C "${DIR}"
168168
fi
169169
done
170170

171171
echo -e "\n| Analyzing with commit complete ... |\n"
172172

173-
rm -f ${REPORTS_DIR}/${PATCH_NAME}
173+
rm -f "${REPORTS_DIR}/${PATCH_NAME}"
174174
}
175175

176176
function count_for_baseline () {
177-
for DIR in ${FILES[@]}; do
178-
HTMLS_DIR=${BRESULTS_DIR}/$(echo ${DIR} |
179-
sed 's/\//_/g')/$(ls ${BRESULTS_DIR}/$(echo ${DIR} |
180-
sed 's/\//_/g')/);
177+
for DIR in "${FILES[@]}" ; do
178+
HTMLS_DIR="${BRESULTS_DIR}/$(echo "${DIR}" |
179+
sed 's/\//_/g')/$(ls "${BRESULTS_DIR}/$(echo "${DIR}" |
180+
sed 's/\//_/g')"/)";
181181

182-
local NAMES_OF_BUGS_B=$(grep -n "SUMM_DESC" ${HTMLS_DIR}/index.html |
182+
local NAMES_OF_BUGS_B="$(grep -n "SUMM_DESC" "${HTMLS_DIR}"/index.html |
183183
cut -d"<" -f3 | cut -d">" -f2 |
184-
sed 's/[^a-zA-Z0]/_/g' | tr '\n' ' ')
185-
local NO_OF_BUGS_B=$(grep -n "SUMM_DESC" ${HTMLS_DIR}/index.html |
186-
cut -d"<" -f5 | cut -d">" -f2 | tr '\n' ' ')
184+
sed 's/[^a-zA-Z0]/_/g' | tr '\n' ' ')"
185+
local NO_OF_BUGS_B="$(grep -n "SUMM_DESC" "${HTMLS_DIR}"/index.html |
186+
cut -d"<" -f5 | cut -d">" -f2 | tr '\n' ' ')"
187187
local count_B=0;
188188

189-
read -a BUG_NAME_B <<<$NAMES_OF_BUGS_B
190-
read -a BUG_COUNT_B <<<$NO_OF_BUGS_B
191-
for i in ${BUG_NAME_B[@]};
189+
read -r -a BUG_NAME_B <<<"${NAMES_OF_BUGS_B}"
190+
read -r -a BUG_COUNT_B <<<"${NO_OF_BUGS_B}"
191+
for i in "${BUG_NAME_B[@]}" ;
192192
do
193-
if [ ! -z ${DICT_B[$i]} ]; then
194-
DICT_B[$i]=$(expr ${BUG_COUNT_B[count_B]} + ${DICT_B[$i]});
193+
if [ -z "${DICT_B[${i}]}" ]; then
194+
DICT_B[${i}]="$(( BUG_COUNT_B[count_B] + DICT_B[${i}]))";
195195
else
196196
DICT_B+=([$i]=${BUG_COUNT_B[count_B]});
197197
fi
198-
count_B=$(expr $count_B + 1)
198+
count_B=$(( count_B + 1))
199199
done
200200
done
201201

202202
echo -e "\nBASELINE BUGS LIST (before applying patch):"
203203
echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
204-
for key_B in ${!DICT_B[@]}; do
204+
for key_B in "${!DICT_B[@]}" ; do
205205
echo "${key_B} --> ${DICT_B[${key_B}]}" | sed 's/_/ /g' | tr -s ' '
206206
done
207207
}
208208

209209
function count_for_target () {
210-
for DIR in ${FILES[@]}; do
211-
HTMLS_DIR=${TRESULTS_DIR}/$(echo ${DIR} |
212-
sed 's/\//_/g')/$(ls ${TRESULTS_DIR}/$(echo ${DIR} |
213-
sed 's/\//_/g')/);
210+
for DIR in "${FILES[@]}" ; do
211+
HTMLS_DIR="${TRESULTS_DIR}"/"$(echo "${DIR}" |
212+
sed 's/\//_/g')"/"$(ls "${TRESULTS_DIR}"/"$(echo "${DIR}" |
213+
sed 's/\//_/g')"/)";
214214

215-
local NAME_OF_BUGS_T=$(grep -n "SUMM_DESC" ${HTMLS_DIR}/index.html |
215+
local NAME_OF_BUGS_T="$(grep -n "SUMM_DESC" "${HTMLS_DIR}"/index.html |
216216
cut -d"<" -f3 | cut -d">" -f2 |
217-
sed 's/[^a-zA-Z0]/_/g'| tr '\n' ' ')
218-
local NO_OF_BUGS_T=$(grep -n "SUMM_DESC" ${HTMLS_DIR}/index.html |
219-
cut -d"<" -f5 | cut -d">" -f2 | tr '\n' ' ')
217+
sed 's/[^a-zA-Z0]/_/g'| tr '\n' ' ')"
218+
local NO_OF_BUGS_T="$(grep -n "SUMM_DESC" "${HTMLS_DIR}"/index.html |
219+
cut -d"<" -f5 | cut -d">" -f2 | tr '\n' ' ')"
220220
local count_T=0;
221221

222-
read -a BUG_NAME_T <<<$NAME_OF_BUGS_T
223-
read -a BUG_COUNT_T <<<$NO_OF_BUGS_T
222+
read -r -a BUG_NAME_T <<<"${NAME_OF_BUGS_T}"
223+
read -r -a BUG_COUNT_T <<<"${NO_OF_BUGS_T}"
224224

225-
for i in ${BUG_NAME_T[@]};
225+
for i in "${BUG_NAME_T[@]}" ;
226226
do
227-
if [ ! -z ${DICT_T[$i]} ]; then
228-
DICT_T[$i]=$(expr ${BUG_COUNT_T[count_T]} + ${DICT_T[$i]});
227+
if [ -n "${DICT_T[$i]}" ]; then
228+
DICT_T[$i]=$(( BUG_COUNT_T[count_T] + DICT_T[$i]));
229229
else
230230
DICT_T+=([$i]=${BUG_COUNT_T[count_T]});
231231
fi
232-
count_T=$(expr $count_T + 1)
232+
count_T=$(( count_T + 1))
233233
done
234234
done
235235

236236
echo -e "\nTARGET BUGS LIST (after applying patch):"
237237
echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
238-
for key_T in ${!DICT_T[@]}; do
238+
for key_T in "${!DICT_T[@]}"; do
239239
echo "${key_T} --> ${DICT_T[${key_T}]}" | sed 's/_/ /g' | tr -s ' '
240240
done
241241
}
@@ -244,7 +244,7 @@ function array_contains () {
244244
local SEEKING=$1; shift
245245
local IN=1
246246
for ELEMENT; do
247-
if [[ $ELEMENT == $SEEKING ]]; then
247+
if [[ $ELEMENT == "${SEEKING}" ]]; then
248248
IN=0
249249
break
250250
fi
@@ -264,20 +264,18 @@ function main () {
264264
echo "~~~~~~~~~~~~~~~~~~~~~~~~~~"
265265

266266
FLAG=0
267-
for BUG in ${!DICT_T[@]}; do
268-
array_contains $BUG "${!DICT_B[@]}"
267+
for BUG in "${!DICT_T[@]}" ; do
268+
array_contains "${BUG}" "${!DICT_B[@]}"
269269
if [ $? -eq 1 ]; then
270-
echo "New ${DICT_T[${BUG}]} Bug[s] introduced: $(echo $BUG |
270+
echo "New ${DICT_T[${BUG}]} Bug[s] introduced: $(echo "${BUG}" |
271271
sed 's/_/ /g' |
272272
tr -s ' ')"
273273
FLAG=1
274274
else
275-
if [ ${BUG} != "All_Bugs" ]; then
276-
if [ ${DICT_B[${BUG}]} -lt \
277-
${DICT_T[${BUG}]} ]; then
278-
echo "Extra $(expr ${DICT_T[${BUG}]} - \
279-
${DICT_B[${BUG}]}) Bug[s] Introduced in: $(echo $BUG |
280-
sed 's/_/ /g' | tr -s ' ')"
275+
if [ "${BUG}" != "All_Bugs" ]; then
276+
if [ "${DICT_B[${BUG}]}" -lt \
277+
"${DICT_T[${BUG}]}" ]; then
278+
echo "Extra $(( DICT_T[${BUG}] - DICT_B[${BUG}])) Bug[s] Introduced in: $(echo "${BUG}" | sed 's/_/ /g' | tr -s ' ')"
281279
FLAG=1
282280
fi
283281
fi
@@ -291,8 +289,8 @@ function main () {
291289
echo -e "Patch Value given by Clang analyzer '-1'\n"
292290
fi
293291
echo -e "\nExplore complete results at:"
294-
find ${BRESULTS_DIR}/ -iname "index.html"
295-
find ${TRESULTS_DIR}/ -iname "index.html"
292+
find "${BRESULTS_DIR}/" -iname "index.html"
293+
find "${TRESULTS_DIR}/" -iname "index.html"
296294
echo -e "\n================= Done with Clang Analysis =================\n"
297295

298296
exit ${FLAG}

0 commit comments

Comments
 (0)