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