3 # Copyright (c) 2020 Cisco and/or its affiliates.
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at:
8 # http://www.apache.org/licenses/LICENSE-2.0
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
18 CLANG_FORMAT_VER_REGEX='([0-9]+)\.[0-9]+\.[0-9]+'
19 CLANG_FORMAT_DIFF="/usr/share/clang/clang-format-diff.py"
21 # TODO: Remove clang-format-${CLANG_FORMAT_VER} from 'make install-deps' when
22 # CLANG_FORMAT_VER default value is upgraded
23 CLANG_FORMAT_VER=${CLANG_FORMAT_VER:-11}
24 GIT_DIFF_ARGS="-U0 --no-color --relative HEAD~1"
25 GIT_DIFF_EXCLUDE_LIST=(
27 ':(exclude)*src/vppinfra/dlmalloc.*'
29 CLANG_FORMAT_DIFF_ARGS="-style file -p1"
30 SUFFIX="-${CLANG_FORMAT_VER}"
32 # Attempt to find clang-format to confirm Clang version.
33 if command -v clang-format${SUFFIX} &> /dev/null;
35 CLANG_FORMAT=clang-format${SUFFIX}
36 elif command -v clang-format &> /dev/null;
38 CLANG_FORMAT=clang-format
41 CLANG_FORMAT_VERSION=$(${CLANG_FORMAT} --version)
42 echo $CLANG_FORMAT_VERSION
44 # Confirm that Clang is the expected version.
45 if [[ ! $CLANG_FORMAT_VERSION =~ $CLANG_FORMAT_VER_REGEX ]];
47 echo "*******************************************************************"
48 echo "* CHECKSTYLE VERSION REGEX CHECK FAILED"
49 echo "* $CLANG_FORMAT_VERSION"
50 echo "*******************************************************************"
54 if [[ ! $CLANG_FORMAT_VER == "${BASH_REMATCH[1]}" ]];
56 echo "*******************************************************************"
57 echo "* CHECKSTYLE VERSION CHECK FAILED"
58 echo "* Expected major version $CLANG_FORMAT_VER, found ${BASH_REMATCH[1]}"
59 echo "*******************************************************************"
63 # Attempt to find clang-format-diff.
64 if command -v clang-format-diff${SUFFIX} &> /dev/null;
66 CLANG_FORMAT_DIFF=clang-format-diff${SUFFIX}
67 elif command -v clang-format-diff.py &> /dev/null;
69 CLANG_FORMAT_DIFF=clang-format-diff.py
70 elif command -v clang-format-diff &> /dev/null;
72 CLANG_FORMAT_DIFF=clang-format-diff
73 elif [ ! -f $CLANG_FORMAT_DIFF ] ;
75 echo "*******************************************************************"
76 echo "* CHECKSTYLE FAILED"
77 echo "* Could not locate the clang-format-diff script"
78 echo "*******************************************************************"
83 git diff ${GIT_DIFF_ARGS} ${GIT_DIFF_EXCLUDE_LIST[@]} > ${in}
85 line_count=$(sed -n '/^+.*\*INDENT-O[NF][F]\{0,1\}\*/p' ${in} | wc -l)
86 if [ ${line_count} -gt 0 ] ; then
88 sed -n '/^+++ /{h}; /^+.*\*INDENT-O[NF][F]\{0,1\}\*/{x;p;x;p;}' ${in}
90 echo "*******************************************************************"
91 echo "* CHECKSTYLE FAILED"
92 echo "* Please remove INDENT-ON and INDENT-OFF from modified lines."
93 echo "*******************************************************************"
98 if [ "${1}" == "--fix" ]; then
99 cat ${in} | ${CLANG_FORMAT_DIFF} ${CLANG_FORMAT_DIFF_ARGS} -i
100 filelist=$(sed -n 's/^+++ b\/\(.*\.[ch]\)/\1/p' ${in})
101 git status ${filelist}
106 line_count=$(sed -n '/^+.*\s\+$/p' ${in} | wc -l)
107 if [ ${line_count} -gt 0 ] ; then
109 sed -n '/^+++/h; /^+.*\s\+$/{x;p;x;p;}' ${in}
111 echo "*******************************************************************"
112 echo "* CHECKSTYLE FAILED"
113 echo "* Trailing whitespace detected"
114 echo "*******************************************************************"
121 cat ${in} | ${CLANG_FORMAT_DIFF} ${CLANG_FORMAT_DIFF_ARGS} > ${out}
124 line_count=$(cat ${out} | wc -l)
126 if [ -t 1 ] && [ -n $(tput colors) ] && [ $(tput colors) -ge 1 ] && \
127 command -v highlight &> /dev/null ; then
128 highlight --syntax diff -O ansi ${out}
135 if [ ${line_count} -gt 0 ] ; then
136 echo "*******************************************************************"
137 echo "* CHECKSTYLE FAILED"
138 echo "* CONSULT DIFF ABOVE"
139 echo "* NOTE: Running 'extras/scripts/checkstyle.sh --fix' *MAY* fix the issue"
140 echo "*******************************************************************"
143 echo "*******************************************************************"
144 echo "* CHECKSTYLE SUCCESSFULLY COMPLETED"
145 echo "*******************************************************************"