xref: /spdk/scripts/check_format.sh (revision 438b71d17ebaee1e2d53f4548e6cf91ef1623ce2)
1#!/usr/bin/env bash
2
3readonly BASEDIR=$(readlink -f $(dirname $0))/..
4cd $BASEDIR
5
6# exit on errors
7set -e
8
9rc=0
10
11if hash astyle; then
12	echo -n "Checking coding style..."
13	if [ "$(astyle -V)" \< "Artistic Style Version 3" ]
14	then
15		echo -n " Your astyle version is too old. This may cause failure on patch verification performed by CI. Please update astyle to at least 3.0.1 version..."
16	fi
17	rm -f astyle.log
18	touch astyle.log
19	# Exclude rte_vhost code imported from DPDK - we want to keep the original code
20	#  as-is to enable ongoing work to synch with a generic upstream DPDK vhost library,
21	#  rather than making diffs more complicated by a lot of changes to follow SPDK
22	#  coding standards.
23	git ls-files '*.[ch]' '*.cpp' '*.cc' '*.cxx' '*.hh' '*.hpp' | \
24		grep -v rte_vhost | grep -v cpp_headers | \
25		xargs astyle --options=.astylerc >> astyle.log
26	if grep -q "^Formatted" astyle.log; then
27		echo " errors detected"
28		git diff
29		sed -i -e 's/  / /g' astyle.log
30		grep --color=auto "^Formatted.*" astyle.log
31		echo "Incorrect code style detected in one or more files."
32		echo "The files have been automatically formatted."
33		echo "Remember to add the files to your commit."
34		rc=1
35	else
36		echo " OK"
37	fi
38	rm -f astyle.log
39else
40	echo "You do not have astyle installed so your code style is not being checked!"
41fi
42
43echo -n "Checking comment style..."
44
45git grep --line-number -e '/[*][^ *-]' -- '*.[ch]' > comment.log || true
46git grep --line-number -e '[^ ][*]/' -- '*.[ch]' ':!lib/vhost/rte_vhost*/*' >> comment.log || true
47git grep --line-number -e '^[*]' -- '*.[ch]' >> comment.log || true
48
49if [ -s comment.log ]; then
50	echo " Incorrect comment formatting detected"
51	cat comment.log
52	rc=1
53else
54	echo " OK"
55fi
56rm -f comment.log
57
58echo -n "Checking for spaces before tabs..."
59git grep --line-number $' \t' -- > whitespace.log || true
60if [ -s whitespace.log ]; then
61	echo " Spaces before tabs detected"
62	cat whitespace.log
63	rc=1
64else
65	echo " OK"
66fi
67rm -f whitespace.log
68
69echo -n "Checking trailing whitespace in output strings..."
70
71git grep --line-number -e ' \\n"' -- '*.[ch]' > whitespace.log || true
72
73if [ -s whitespace.log ]; then
74	echo " Incorrect trailing whitespace detected"
75	cat whitespace.log
76	rc=1
77else
78	echo " OK"
79fi
80rm -f whitespace.log
81
82echo -n "Checking for use of forbidden library functions..."
83
84git grep --line-number -w '\(strncpy\|strcpy\|strcat\|sprintf\|vsprintf\)' -- './*.c' ':!lib/vhost/rte_vhost*/**' > badfunc.log || true
85if [ -s badfunc.log ]; then
86	echo " Forbidden library functions detected"
87	cat badfunc.log
88	rc=1
89else
90	echo " OK"
91fi
92rm -f badfunc.log
93
94echo -n "Checking for use of forbidden CUnit macros..."
95
96git grep --line-number -w 'CU_ASSERT_FATAL' -- 'test/*' ':!test/spdk_cunit.h' > badcunit.log || true
97if [ -s badcunit.log ]; then
98	echo " Forbidden CU_ASSERT_FATAL usage detected - use SPDK_CU_ASSERT_FATAL instead"
99	cat badcunit.log
100	rc=1
101else
102	echo " OK"
103fi
104rm -f badcunit.log
105
106echo -n "Checking blank lines at end of file..."
107
108if ! git grep -I -l -e . -z | \
109	xargs -0 -P8 -n1 scripts/eofnl > eofnl.log; then
110	echo " Incorrect end-of-file formatting detected"
111	cat eofnl.log
112	rc=1
113else
114	echo " OK"
115fi
116rm -f eofnl.log
117
118echo -n "Checking for POSIX includes..."
119git grep -I -i -f scripts/posix.txt -- './*' ':!include/spdk/stdinc.h' ':!include/linux/**' ':!lib/vhost/rte_vhost*/**' ':!scripts/posix.txt' > scripts/posix.log || true
120if [ -s scripts/posix.log ]; then
121	echo "POSIX includes detected. Please include spdk/stdinc.h instead."
122	cat scripts/posix.log
123	rc=1
124else
125	echo " OK"
126fi
127rm -f scripts/posix.log
128
129if hash pycodestyle 2>/dev/null; then
130	PEP8=pycodestyle
131elif hash pep8 2>/dev/null; then
132	PEP8=pep8
133fi
134
135if [ ! -z ${PEP8} ]; then
136	echo -n "Checking Python style..."
137
138	PEP8_ARGS+=" --max-line-length=140"
139
140	error=0
141	git ls-files '*.py' | xargs -n1 $PEP8 $PEP8_ARGS > pep8.log || error=1
142	if [ $error -ne 0 ]; then
143		echo " Python formatting errors detected"
144		cat pep8.log
145		rc=1
146	else
147		echo " OK"
148	fi
149	rm -f pep8.log
150else
151	echo "You do not have pycodestyle or pep8 installed so your Python style is not being checked!"
152fi
153
154# Check if any of the public interfaces were modified by this patch.
155# Warn the user to consider updating the changelog any changes
156# are detected.
157echo -n "Checking whether CHANGELOG.md should be updated..."
158staged=$(git diff --name-only --cached .)
159working=$(git status -s --porcelain | grep -iv "??" | awk '{print $2}')
160files="$staged $working"
161if [[ "$files" = " " ]]; then
162	files=$(git diff-tree --no-commit-id --name-only -r HEAD)
163fi
164
165has_changelog=0
166for f in $files; do
167	if [[ $f == CHANGELOG.md ]]; then
168		# The user has a changelog entry, so exit.
169		has_changelog=1
170		break
171	fi
172done
173
174needs_changelog=0
175if [ $has_changelog -eq 0 ]; then
176	for f in $files; do
177		if [[ $f == include/spdk/* ]] || [[ $f == scripts/rpc.py ]] || [[ $f == etc/* ]]; then
178			echo ""
179			echo -n "$f was modified. Consider updating CHANGELOG.md."
180			needs_changelog=1
181		fi
182	done
183fi
184
185if [ $needs_changelog -eq 0 ]; then
186	echo " OK"
187else
188	echo ""
189fi
190
191exit $rc
192