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 CLANG_FORMAT_VER=${CLANG_FORMAT_VER:-10}
22 GIT_DIFF_ARGS="-U0 --no-color --relative HEAD~1"
23 CLANG_FORMAT_DIFF_ARGS="-style file -p1"
24 SUFFIX="-${CLANG_FORMAT_VER}"
26 # Attempt to find clang-format to confirm Clang version.
27 if command -v clang-format${SUFFIX} &> /dev/null;
29 CLANG_FORMAT=clang-format${SUFFIX}
30 elif command -v clang-format &> /dev/null;
32 CLANG_FORMAT=clang-format
35 CLANG_FORMAT_VERSION=$(${CLANG_FORMAT} --version)
36 echo $CLANG_FORMAT_VERSION
38 # Confirm that Clang is the expected version.
39 if [[ ! $CLANG_FORMAT_VERSION =~ $CLANG_FORMAT_VER_REGEX ]];
41 echo "*******************************************************************"
42 echo "* CHECKSTYLE VERSION REGEX CHECK FAILED"
43 echo "* $CLANG_FORMAT_VERSION"
44 echo "*******************************************************************"
48 if [[ ! $CLANG_FORMAT_VER == "${BASH_REMATCH[1]}" ]];
50 echo "*******************************************************************"
51 echo "* CHECKSTYLE VERSION CHECK FAILED"
52 echo "* Expected major version $CLANG_FORMAT_VER, found ${BASH_REMATCH[1]}"
53 echo "*******************************************************************"
57 # Attempt to find clang-format-diff.
58 if command -v clang-format-diff${SUFFIX} &> /dev/null;
60 CLANG_FORMAT_DIFF=clang-format-diff${SUFFIX}
61 elif command -v clang-format-diff &> /dev/null;
63 CLANG_FORMAT=clang-format-diff
64 elif [ ! -f $CLANG_FORMAT_DIFF ] ;
66 echo "*******************************************************************"
67 echo "* CHECKSTYLE FAILED"
68 echo "* Could not locate the clang-format-diff script"
69 echo "*******************************************************************"
74 git diff ${GIT_DIFF_ARGS} ':!*.patch' > ${in}
76 line_count=$(sed -n '/^+.*\*INDENT-O[NF][F]\{0,1\}\*/p' ${in} | wc -l)
77 if [ ${line_count} -gt 0 ] ; then
79 sed -n '/^+++ /{h}; /^+.*\*INDENT-O[NF][F]\{0,1\}\*/{x;p;x;p;}' ${in}
81 echo "*******************************************************************"
82 echo "* CHECKSTYLE FAILED"
83 echo "* Please remove INDENT-ON and INDENT-OFF from modified lines."
84 echo "*******************************************************************"
89 if [ "${1}" == "--fix" ]; then
90 cat ${in} | ${CLANG_FORMAT_DIFF} ${CLANG_FORMAT_DIFF_ARGS} -i
91 filelist=$(sed -n 's/^+++ b\/\(.*\.[ch]\)/\1/p' ${in})
92 git status ${filelist}
97 line_count=$(sed -n '/^+.*\s\+$/p' ${in} | wc -l)
98 if [ ${line_count} -gt 0 ] ; then
100 sed -n '/^+++/h; /^+.*\s\+$/{x;p;x;p;}' ${in}
102 echo "*******************************************************************"
103 echo "* CHECKSTYLE FAILED"
104 echo "* Trailing whitespace detected"
105 echo "*******************************************************************"
112 cat ${in} | ${CLANG_FORMAT_DIFF} ${CLANG_FORMAT_DIFF_ARGS} > ${out}
115 line_count=$(cat ${out} | wc -l)
117 if [ -t 1 ] && [ -n $(tput colors) ] && [ $(tput colors) -ge 1 ] && \
118 command -v highlight &> /dev/null ; then
119 highlight --syntax diff -O ansi ${out}
126 if [ ${line_count} -gt 0 ] ; then
127 echo "*******************************************************************"
128 echo "* CHECKSTYLE FAILED"
129 echo "* CONSULT DIFF ABOVE"
130 echo "* NOTE: Running 'extras/scripts/checkstyle.sh --fix' *MAY* fix the issue"
131 echo "*******************************************************************"
134 echo "*******************************************************************"
135 echo "* CHECKSTYLE SUCCESSFULLY COMPLETED"
136 echo "*******************************************************************"