Skip to content

Commit f0d6d4b

Browse files
committed
[EGD-5238] Added tidy target to CI to inform about added tidy issues
* Small fixups based on shellceck * Tool now: can: * take diffs and pass it to clang-tidy cant: * set error on warnings/errors (errno) * copy compilation_database.json and sanitize it
1 parent 81adfe4 commit f0d6d4b

File tree

7 files changed

+198
-79
lines changed

7 files changed

+198
-79
lines changed

.clang-tidy

-2
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,6 @@ CheckOptions:
5252
value: '::realloc'
5353
- key: cppcoreguidelines-pro-bounds-constant-array-index.GslHeader
5454
value: ''
55-
- key: cppcoreguidelines-pro-bounds-constant-array-index.IncludeStyle
56-
value: '1'
5755
- key: cppcoreguidelines-pro-type-member-init.IgnoreArrays
5856
value: '0'
5957
- key: cppcoreguidelines-special-member-functions.AllowMissingMoveFunctions

.github/workflows/main.yml

+2
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ jobs:
3636
run: config/license_header_check.sh --ci --check-only
3737
- name: Style checking
3838
run: ./config/style_check_hook.sh --last
39+
- name: Clang tidy check
40+
run: ./config/clang_check.sh
3941

4042
build_rt1051_binary:
4143
name: build rt1051 binary

config/clang/clang-common.sh

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
#!/bin/env bash
2+
# Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved.
3+
# For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md
4+
5+
set -eo pipefail
6+
7+
verify_clang_format_version()
8+
{
9+
# check if clang-format in path is in proper version, version is 3rd column in `clang-format --version`
10+
local version
11+
version=$( [[ $(which clang-format) ]] && (clang-format --version | cut -d ' ' -f 3 | cut -d '.' -f 1) || echo "0")
12+
# check for either clang-format or clang-format-9
13+
if [[ $version -lt 10 && ! $(which clang-format-10) ]]; then
14+
cat << EOF >&1
15+
Either install:
16+
clang-format in at least version 10 and set as default"
17+
or
18+
clang-format-10
19+
20+
Your clang format version in path used:
21+
$(clang-format --version):
22+
$(clang-format-10 --version)
23+
git commit aborted"
24+
EOF
25+
exit 1
26+
fi
27+
}
28+
29+
## find `what` from list of directories `arr`
30+
get_tool() {
31+
local what="$1"; shift
32+
local arr=("$@")
33+
34+
local tool="";
35+
for el in ${arr[*]}; do
36+
if [[ -f "${el}" ]]; then
37+
tool=${el}
38+
break
39+
fi
40+
done
41+
if [[ ${tool} == "" ]]; then
42+
echo "$what not found in path and: ${arr[*]}" > /dev/stderr
43+
exit 2
44+
fi
45+
echo "${tool}"
46+
}
47+
48+
## Search for clang-format-diff.py
49+
get_clang_format() {
50+
local searchpaths=(
51+
"$(which "clang-format-diff.py" 2>/dev/null 1>/dev/null)" # clang-format-diff in path
52+
"/usr/share/clang/clang-format-*/clang-format-diff.py" # clang-format-diff location on Ubuntu/Debian
53+
"/usr/share/clang/clang-format-diff.py" # clang-format_diff location on Arch last resort
54+
)
55+
get_tool "clang-format-diff.py" "${searchpaths[@]}"
56+
}
57+
58+
## search for clang-tidy-diff
59+
get_clang_tidy()
60+
{
61+
local searchpaths=(
62+
"$(which "clang-tidy-diff.py" 2>/dev/null 1>/dev/null)" # clang-format-diff in path
63+
"/usr/bin/clang-tidy-diff-10.py" # clang-format-diff location on Ubuntu
64+
"/usr/share/clang/clang-tidy-*/clang-tidy-diff.py" # clang-format-diff location on Debian
65+
"/usr/share/clang/clang-tidy-diff.py" # clang-format_diff location on Arch last resort
66+
)
67+
get_tool "clang-tidy-diff.py" "${searchpaths[@]}"
68+
}

config/clang/colors.sh

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/bin/env bash
2+
# Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved.
3+
# For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md
4+
5+
export RED='\e[38;2;255;13;0m'
6+
export YELLOW="\e[38;2;255;232;0m"
7+
export GREEN="\e[38;2;0;222;27m"
8+
export ORANGE="\e[38;2;255;100;0m"
9+
10+
export OK="${GREEN}OK!\e[0m"
11+
export ERROR="${RED}Error!\e[0m"
12+
export FIXED="${YELLOW}Fixed!\e[0m"

config/clang_check.sh

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
#!/bin/env bash
2+
# Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved.
3+
# For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md
4+
5+
set -euo pipefail
6+
7+
# shellcheck source=../config/format-config.sh
8+
# shellcheck source=../config/clang/colors.sh
9+
# shellcheck source=../config/clang/common.sh
10+
11+
L_GIT_DIR=$(git rev-parse --show-toplevel)
12+
source $L_GIT_DIR/config/format-config.sh
13+
source $L_GIT_DIR/config/clang/clang-common.sh
14+
source $L_GIT_DIR/config/clang/colors.sh
15+
16+
help()
17+
{
18+
cat <<- EOF
19+
clang tidy diff tool used to perform check on our sources uses 2 defaults:
20+
.clang-tidy : configuration file, from the place where command is run
21+
compile_commands.json : compile data, as well from the place where command is run
22+
EOF
23+
}
24+
25+
get_files_to_check()
26+
{
27+
local files
28+
local endlist=()
29+
30+
files=$(git diff -U0 --name-only remotes/origin/master...HEAD)
31+
for file in ${files}; do
32+
if [[ ${file} =~ ^.*\.(cpp|hpp|c|h|cxx|gcc|cc)$ ]] && shouldnt_ignore "${file}"; then
33+
endlist+=("$file")
34+
fi
35+
done
36+
echo "${endlist[*]}"
37+
}
38+
39+
# if not exists create
40+
# could be in better place than tmp
41+
get_compile_commands()
42+
{
43+
[[ -f build-linux-Debug/compile_commands.json ]] || ./configure.sh linux debug -DCMAKE_EXPORT_COMPILE_COMMANDS=1 > /dev/null 2>&1
44+
cp build-linux-Debug/compile_commands.json /tmp/compile_commands.json
45+
sed -i 's|-static-libasan||g' /tmp/compile_commands.json
46+
}
47+
48+
main()
49+
{
50+
if [[ $# -ne 0 ]]; then
51+
help
52+
exit 0
53+
fi
54+
local tool
55+
tool=$(get_clang_tidy)
56+
57+
local files_to_check
58+
files_to_check=$(get_files_to_check)
59+
60+
if [[ -z $files_to_check ]]; then
61+
echo "no files to check"
62+
exit 0
63+
fi
64+
65+
# get the stage
66+
verify_clang_format_version
67+
get_compile_commands
68+
# run tidy
69+
git diff -U0 --no-color remotes/origin/master...HEAD "$files_to_check" | ${tool[*]} -p 1 -path=/tmp/
70+
}
71+
72+
main

config/format-config.sh

+16-3
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@
66
# ignore file for clang-format autoformating
77

88
# set this variable in your shell if you wish to disable autoformatting on commit for time being
9-
#DISABLE_AUTO_FORMATTING=1
9+
export DISABLE_AUTO_FORMATTING=0
1010

1111
# set this variable to get more verbose output
12-
VERBOSE=1
12+
export VERBOSE=1
1313

1414
# ignore_paths for formatter - these are regex matched with filenames to be formated
1515
# if you don't wish to format i.e one file - just pass whole path to this file from git root directory
16-
declare ignore_paths=(
16+
export declare ignore_paths=(
1717
'.*/catch.hpp'
1818
'.*/lib/'
1919
'build'
@@ -57,3 +57,16 @@ declare ignore_paths=(
5757
'module-vfs/drivers/include/thirdparty/fatfs/ffconf.h'
5858
'module-vfs/thirdparty/*'
5959
)
60+
61+
# bash function using above config function
62+
shouldnt_ignore() {
63+
# change full name path to path relative to root git dir
64+
local fname=${1/"$L_GIT_DIR"/"./"}
65+
for el in ${ignore_paths[@]}; do
66+
if [[ ${fname} =~ ^${el}.* ]]; then
67+
[[ $VERBOSE ]] && echo "Ignore: ${fname} formatting due to: $el match!"
68+
return 1
69+
fi
70+
done
71+
return 0
72+
}

config/style_check_hook.sh

+28-74
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
11
#!/bin/bash
2+
# Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved.
3+
# For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md
4+
5+
set -e
6+
set -o pipefail
7+
# trap 'echo ERROR on line: $LINENO running command: $BASH_COMMAND ; trap - EXIT; exit $?' EXIT
8+
29
# Copyright (c) 2017-2020, Mudita Sp. z.o.o. All rights reserved.
310
# For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md
411

@@ -7,86 +14,47 @@
714
# - with use of clang-format diff.py - for changed chunk changes only
815
# might be worth to consider: https://github.com/Sarcasm/run-clang-format
916

17+
# shellcheck source=../config/format-config.sh
18+
# shellcheck source=../config/clang/colors.sh
19+
# shellcheck source=../config/clang/common.sh
1020

1121
L_GIT_DIR=$(git rev-parse --show-toplevel)
1222
source $L_GIT_DIR/config/format-config.sh
13-
14-
RED='\e[38;2;255;13;0m'
15-
YELLOW="\e[38;2;255;232;0m"
16-
GREEN="\e[38;2;0;222;27m"
17-
ORANGE="\e[38;2;255;100;0m"
18-
19-
OK="${GREEN}OK!\e[0m"
20-
ERROR="${RED}Error!\e[0m"
21-
FIXED="${YELLOW}Fixed!\e[0m"
22-
23+
source $L_GIT_DIR/config/clang/colors.sh
24+
source $L_GIT_DIR/config/clang/clang-common.sh
2325

2426
# if autoformatting was disabled by user - then ignore this commit hook
25-
if [[ $DISABLE_AUTO_FORMATTING ]]; then
27+
if [ "$DISABLE_AUTO_FORMATTING" -eq 1 ]; then
2628
[[ $VERBOSE ]] && echo "auto formatting is disabled"
2729
exit 0
2830
fi
2931

30-
# check if clang-format in path is in proper version, version is 3rd column in `clang-format --version`
31-
CVER=$( [[ $(which clang-format) ]] && echo $(clang-format --version | cut -d ' ' -f 3 | cut -d '.' -f 1) || echo "0")
32-
# check for either clang-format or clang-format-9
33-
if [[ $CVER -lt 10 && ! $(which clang-format-10) ]]; then
34-
cat << EOF >&1
35-
Either install:
36-
clang-format in at least version 10 and set as default"
37-
or
38-
clang-format-10
39-
40-
Your clang format version in path used:
41-
$(clang-format --version):
42-
$(clang-format-10 --version)
43-
git commit aborted"
44-
EOF
45-
exit 1
46-
fi
32+
verify_clang_format_version
4733

48-
get_clang_format_script() {
49-
local declare searchpaths=(
50-
$(which "clang-format-diff.py") # clang-format-diff in path
51-
"/usr/share/clang/clang-format-10/clang-format-diff.py" # clang-format-diff location for clang format 9 Ubuntu/Debian 18.04
52-
"/usr/share/clang/clang-format-${CVER}/clang-format-diff.py" # clang-format-diff location on Ubuntu/Debian
53-
"/usr/share/clang/clang-format-diff.py" # clang-format_diff location on Arch last resort
54-
)
55-
local tool=""
56-
for el in ${searchpaths[@]}; do
57-
if [[ -f ${el} ]]; then
58-
tool=${el}
59-
break
60-
fi
61-
done
62-
if [[ ${tool} == "" ]]; then
63-
echo clang-format-diff not found in path and: ${sarchpaths[@]} >2
64-
fi
65-
echo "${tool}"
66-
}
67-
68-
L_CLANG_DIFF_TOOL=$(get_clang_format_script)
34+
L_CLANG_DIFF_TOOL=$(get_clang_format)
6935
if ! [[ $L_CLANG_DIFF_TOOL ]] || [[ $L_CLANG_DIFF_TOOL == "" ]]; then
36+
[[ $VERBOSE ]] && echo "clang tool not found"
7037
exit 1
7138
fi
7239

40+
7341
check_file() {
7442
file="${1}"
7543
last_commit="${2}"
76-
if [ -f $file ]; then
44+
if [ -f "${file}" ]; then
7745
#[[ $VERBOSE ]] && echo -en "Checking: \e[33m${file}\e[0m :\t"
7846
results["${file}"]="${OK}"
7947
case ${last_commit} in
8048
"True")
8149
if [[ ${FIX,,} == "true" ]]; then
82-
git diff -U0 --no-color remotes/origin/master...HEAD ${file} | ${L_CLANG_DIFF_TOOL} -style file -p1 -i
83-
STATUS=$(git status --short -- ${file})
50+
git diff -U0 --no-color remotes/origin/master...HEAD "${file}" | ${L_CLANG_DIFF_TOOL} -style file -p1 -i
51+
STATUS=$(git status --short -- "${file}")
8452
if [ -n "${STATUS}" ]; then
8553
git add "${file}"
8654
results["${file}"]="${FIXED}";
8755
fi
8856
else
89-
OUT=$(git diff -U0 --no-color remotes/origin/master...HEAD ${file} | ${L_CLANG_DIFF_TOOL} -style file -p1 )
57+
OUT=$(git diff -U0 --no-color remotes/origin/master...HEAD "${file}" | ${L_CLANG_DIFF_TOOL} -style file -p1 )
9058
if [ -n "${OUT}" ]; then
9159
results["${file}"]="${ERROR}"
9260
return 1
@@ -95,23 +63,23 @@ check_file() {
9563
;;
9664
"Stage")
9765
if [[ ${FIX,,} == "true" ]]; then
98-
git diff -U0 --no-color --cached ${file} | ${L_CLANG_DIFF_TOOL} -style file -p1 -i
99-
STATUS=$(git status --short -- ${file})
66+
git diff -U0 --no-color --cached "${file}" | ${L_CLANG_DIFF_TOOL} -style file -p1 -i
67+
STATUS=$(git status --short -- "${file}")
10068
if [ -n "${STATUS}" ]; then
10169
git add "${file}"
10270
results["${file}"]="${FIXED}";
10371
fi
10472
else
105-
OUT=$(git diff -U0 --no-color --cached ${file} | ${L_CLANG_DIFF_TOOL} -style file -p1)
73+
OUT=$(git diff -U0 --no-color --cached "${file}" | ${L_CLANG_DIFF_TOOL} -style file -p1)
10674
if [ -n "${OUT}" ]; then
10775
results["${file}"]="${ERROR}"
10876
return 1
10977
fi
11078
fi
11179
;;
11280
*)
113-
OUT=$(git diff -U0 --no-color --cached ${file} | ${L_CLANG_DIFF_TOOL} -style file -p1 )
114-
if [ -n ${OUT} ]; then
81+
OUT=$(git diff -U0 --no-color --cached "${file}" | ${L_CLANG_DIFF_TOOL} -style file -p1 )
82+
if [[ -n ${OUT} ]]; then
11583
results["${file}"]="${ERROR}"
11684
return 1
11785
fi
@@ -121,19 +89,6 @@ check_file() {
12189
return 0
12290
}
12391

124-
# bash function using above config function
125-
shouldnt_ignore() {
126-
# change full name path to path relative to root git dir
127-
local fname=${1/"$L_GIT_DIR"/"./"}
128-
for el in ${ignore_paths[@]}; do
129-
if [[ ${fname} =~ ^${el}.* ]]; then
130-
[[ $VERBOSE ]] && echo "Ignore: ${fname} formatting due to: $el match!"
131-
return 1
132-
fi
133-
done
134-
return 0
135-
}
136-
13792
function help() {
13893
echo "Runs clang-format on source files"
13994
cat <<- EOM
@@ -176,14 +131,13 @@ case "${1}" in
176131
;;
177132
*)
178133
if [[ $# -ne 0 ]]; then
179-
echo "unknown parameters: '$@'"
134+
echo "unknown parameters: '$*'"
180135
help
181136
exit 1
182137
fi
183138
FILES=$(git diff-index --cached --name-only HEAD)
184139
LAST="Stage"
185-
FIX=$(git config user.fixinstage)
186-
FIX=${FIX:-false}
140+
FIX=$([[ $(git config user.fixinstage) == "true" ]] && echo "true" || echo "false")
187141
;;
188142
esac
189143

0 commit comments

Comments
 (0)