blob: 304beff51d063fcf2537aaa0ce159b6fd724dd0b [file] [log] [blame]
Damjan Marion942542f2020-12-12 19:09:31 +01001#!/bin/bash
2
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:
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
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.
15
16set -eEo pipefail
17
Ray Kinsella71835982021-03-12 15:57:29 +000018CLANG_FORMAT_VER_REGEX='([0-9]+)\.[0-9]+\.[0-9]+'
19CLANG_FORMAT_DIFF="/usr/share/clang/clang-format-diff.py"
20
Dave Wallacefb1135d2021-11-09 13:58:12 -050021# TODO: Remove clang-format-${CLANG_FORMAT_VER} from 'make install-deps' when
Dave Wallacecf9356d2024-07-23 01:28:19 -040022# CLANG_FORMAT_VER default value is upgraded to 15 after Ubuntu-20.04 is deprecated
Dave Wallace4a332312022-03-17 17:44:35 -040023CLANG_FORMAT_VER=${CLANG_FORMAT_VER:-11}
Damjan Marion942542f2020-12-12 19:09:31 +010024GIT_DIFF_ARGS="-U0 --no-color --relative HEAD~1"
Dave Wallace5d0fa2f2022-03-23 21:29:54 -040025GIT_DIFF_EXCLUDE_LIST=(
26 ':!*.patch'
27 ':(exclude)*src/vppinfra/dlmalloc.*'
28)
Damjan Marion942542f2020-12-12 19:09:31 +010029CLANG_FORMAT_DIFF_ARGS="-style file -p1"
30SUFFIX="-${CLANG_FORMAT_VER}"
31
Ray Kinsella71835982021-03-12 15:57:29 +000032# Attempt to find clang-format to confirm Clang version.
Dave Wallacecf9356d2024-07-23 01:28:19 -040033if command -v "clang-format${SUFFIX}" &> /dev/null;
Ray Kinsella71835982021-03-12 15:57:29 +000034then
Dave Wallacecf9356d2024-07-23 01:28:19 -040035 CLANG_FORMAT="clang-format${SUFFIX}"
36else
37 echo "*******************************************************************"
38 echo "* CHECKSTYLE FAILED"
39 echo "* Could not locate the clang-format${SUFFIX} script"
40 echo "* Run 'make install-deps' to install it"
41 echo "*******************************************************************"
42 exit 1
Ray Kinsella71835982021-03-12 15:57:29 +000043fi
44
45CLANG_FORMAT_VERSION=$(${CLANG_FORMAT} --version)
Dave Wallacecf9356d2024-07-23 01:28:19 -040046echo "$CLANG_FORMAT_VERSION"
Ray Kinsella71835982021-03-12 15:57:29 +000047
48# Confirm that Clang is the expected version.
49if [[ ! $CLANG_FORMAT_VERSION =~ $CLANG_FORMAT_VER_REGEX ]];
50then
51 echo "*******************************************************************"
52 echo "* CHECKSTYLE VERSION REGEX CHECK FAILED"
53 echo "* $CLANG_FORMAT_VERSION"
54 echo "*******************************************************************"
55 exit 1
56fi
57
58if [[ ! $CLANG_FORMAT_VER == "${BASH_REMATCH[1]}" ]];
59then
60 echo "*******************************************************************"
61 echo "* CHECKSTYLE VERSION CHECK FAILED"
62 echo "* Expected major version $CLANG_FORMAT_VER, found ${BASH_REMATCH[1]}"
63 echo "*******************************************************************"
64 exit 1
65fi
66
67# Attempt to find clang-format-diff.
68if command -v clang-format-diff${SUFFIX} &> /dev/null;
69then
70 CLANG_FORMAT_DIFF=clang-format-diff${SUFFIX}
Klement Sekerab9ff03c2022-02-18 16:23:33 +000071elif command -v clang-format-diff.py &> /dev/null;
72then
73 CLANG_FORMAT_DIFF=clang-format-diff.py
Ray Kinsella71835982021-03-12 15:57:29 +000074elif command -v clang-format-diff &> /dev/null;
75then
Klement Sekerab9ff03c2022-02-18 16:23:33 +000076 CLANG_FORMAT_DIFF=clang-format-diff
Ray Kinsella71835982021-03-12 15:57:29 +000077elif [ ! -f $CLANG_FORMAT_DIFF ] ;
78then
79 echo "*******************************************************************"
80 echo "* CHECKSTYLE FAILED"
81 echo "* Could not locate the clang-format-diff script"
Dave Wallacecf9356d2024-07-23 01:28:19 -040082 echo "* Run 'make install-deps' to install it"
Ray Kinsella71835982021-03-12 15:57:29 +000083 echo "*******************************************************************"
84 exit 1
85fi
Damjan Marion942542f2020-12-12 19:09:31 +010086
87in=$(mktemp)
Dave Wallace5d0fa2f2022-03-23 21:29:54 -040088git diff ${GIT_DIFF_ARGS} ${GIT_DIFF_EXCLUDE_LIST[@]} > ${in}
Damjan Marion942542f2020-12-12 19:09:31 +010089
90line_count=$(sed -n '/^+.*\*INDENT-O[NF][F]\{0,1\}\*/p' ${in} | wc -l)
91if [ ${line_count} -gt 0 ] ; then
92 echo
93 sed -n '/^+++ /{h}; /^+.*\*INDENT-O[NF][F]\{0,1\}\*/{x;p;x;p;}' ${in}
94 echo
95 echo "*******************************************************************"
96 echo "* CHECKSTYLE FAILED"
97 echo "* Please remove INDENT-ON and INDENT-OFF from modified lines."
98 echo "*******************************************************************"
99 rm ${in}
100 exit 1
101fi
102
103if [ "${1}" == "--fix" ]; then
Ray Kinsella71835982021-03-12 15:57:29 +0000104 cat ${in} | ${CLANG_FORMAT_DIFF} ${CLANG_FORMAT_DIFF_ARGS} -i
Damjan Marion942542f2020-12-12 19:09:31 +0100105 filelist=$(sed -n 's/^+++ b\/\(.*\.[ch]\)/\1/p' ${in})
106 git status ${filelist}
107 rm ${in}
108 exit 0
109fi
110
111line_count=$(sed -n '/^+.*\s\+$/p' ${in} | wc -l)
112if [ ${line_count} -gt 0 ] ; then
113 echo
114 sed -n '/^+++/h; /^+.*\s\+$/{x;p;x;p;}' ${in}
115 echo
116 echo "*******************************************************************"
117 echo "* CHECKSTYLE FAILED"
118 echo "* Trailing whitespace detected"
119 echo "*******************************************************************"
120 rm ${in}
121 exit 1
122fi
123
124out=$(mktemp)
125
Ray Kinsella71835982021-03-12 15:57:29 +0000126cat ${in} | ${CLANG_FORMAT_DIFF} ${CLANG_FORMAT_DIFF_ARGS} > ${out}
Damjan Marion942542f2020-12-12 19:09:31 +0100127rm ${in}
128
129line_count=$(cat ${out} | wc -l)
130
131if [ -t 1 ] && [ -n $(tput colors) ] && [ $(tput colors) -ge 1 ] && \
132 command -v highlight &> /dev/null ; then
133 highlight --syntax diff -O ansi ${out}
134else
135 cat ${out}
136fi
137
138rm ${out}
139
140if [ ${line_count} -gt 0 ] ; then
141 echo "*******************************************************************"
142 echo "* CHECKSTYLE FAILED"
143 echo "* CONSULT DIFF ABOVE"
144 echo "* NOTE: Running 'extras/scripts/checkstyle.sh --fix' *MAY* fix the issue"
145 echo "*******************************************************************"
146 exit 1
147else
148 echo "*******************************************************************"
149 echo "* CHECKSTYLE SUCCESSFULLY COMPLETED"
150 echo "*******************************************************************"
151 exit 0
152fi