1f08c3bdfSopenharmony_ci#!/bin/sh
2f08c3bdfSopenharmony_ci
3f08c3bdfSopenharmony_ci#set -x
4f08c3bdfSopenharmony_ci
5f08c3bdfSopenharmony_cicd $(dirname "$0")
6f08c3bdfSopenharmony_ci
7f08c3bdfSopenharmony_cidefault_path=".."
8f08c3bdfSopenharmony_cidefault_cmd="sparse \$file"
9f08c3bdfSopenharmony_cidefault_args="$SPARSE_TEST_ARGS"
10f08c3bdfSopenharmony_citests_list=""
11f08c3bdfSopenharmony_ciprog_name=`basename $0`
12f08c3bdfSopenharmony_ci
13f08c3bdfSopenharmony_ciif [ ! -x "$default_path/sparse-llvm" ]; then
14f08c3bdfSopenharmony_ci	disabled_cmds="sparsec sparsei sparse-llvm sparse-llvm-dis"
15f08c3bdfSopenharmony_cifi
16f08c3bdfSopenharmony_ciif [ ! -x "$default_path/scheck" ]; then
17f08c3bdfSopenharmony_ci	disabled_cmds="$disabled_cmds scheck"
18f08c3bdfSopenharmony_cifi
19f08c3bdfSopenharmony_ci
20f08c3bdfSopenharmony_ci# flags:
21f08c3bdfSopenharmony_ci#	- some tests gave an unexpected result
22f08c3bdfSopenharmony_cifailed=0
23f08c3bdfSopenharmony_ci
24f08c3bdfSopenharmony_ci# counts:
25f08c3bdfSopenharmony_ci#	- tests that have not been converted to test-suite format
26f08c3bdfSopenharmony_ci#	- tests that are disabled
27f08c3bdfSopenharmony_ci#	- tests that passed
28f08c3bdfSopenharmony_ci#	- tests that failed
29f08c3bdfSopenharmony_ci#	- tests that failed but are known to fail
30f08c3bdfSopenharmony_ciunhandled_tests=0
31f08c3bdfSopenharmony_cidisabled_tests=0
32f08c3bdfSopenharmony_ciok_tests=0
33f08c3bdfSopenharmony_ciko_tests=0
34f08c3bdfSopenharmony_ciknown_ko_tests=0
35f08c3bdfSopenharmony_ci
36f08c3bdfSopenharmony_ci# defaults to not verbose
37f08c3bdfSopenharmony_ci[ -z "$V" ] && V=0
38f08c3bdfSopenharmony_civquiet=""
39f08c3bdfSopenharmony_ciquiet=0
40f08c3bdfSopenharmony_ciabort=0
41f08c3bdfSopenharmony_ci
42f08c3bdfSopenharmony_ci
43f08c3bdfSopenharmony_ci##
44f08c3bdfSopenharmony_ci# verbose(string) - prints string if we are in verbose mode
45f08c3bdfSopenharmony_civerbose()
46f08c3bdfSopenharmony_ci{
47f08c3bdfSopenharmony_ci	[ "$V" -eq "1" ] && echo "        $1"
48f08c3bdfSopenharmony_ci	return 0
49f08c3bdfSopenharmony_ci}
50f08c3bdfSopenharmony_ci
51f08c3bdfSopenharmony_ci##
52f08c3bdfSopenharmony_ci# warning(string) - prints a warning
53f08c3bdfSopenharmony_ciwarning()
54f08c3bdfSopenharmony_ci{
55f08c3bdfSopenharmony_ci	[ "$quiet" -ne 1 ] && echo "warning: $1"
56f08c3bdfSopenharmony_ci	return 0
57f08c3bdfSopenharmony_ci}
58f08c3bdfSopenharmony_ci
59f08c3bdfSopenharmony_ci##
60f08c3bdfSopenharmony_ci# error(string[, die]) - prints an error and exits with value die if given
61f08c3bdfSopenharmony_cierror()
62f08c3bdfSopenharmony_ci{
63f08c3bdfSopenharmony_ci	[ "$quiet" -ne 1 ] && echo "error: $1"
64f08c3bdfSopenharmony_ci	[ -n "$2" ] && exit $2
65f08c3bdfSopenharmony_ci	return 0
66f08c3bdfSopenharmony_ci}
67f08c3bdfSopenharmony_ci
68f08c3bdfSopenharmony_ci
69f08c3bdfSopenharmony_ci##
70f08c3bdfSopenharmony_ci# get_tag_value(file) - get the 'check-<...>' tags & values
71f08c3bdfSopenharmony_ciget_tag_value()
72f08c3bdfSopenharmony_ci{
73f08c3bdfSopenharmony_ci	check_name=""
74f08c3bdfSopenharmony_ci	check_command="$default_cmd"
75f08c3bdfSopenharmony_ci	check_exit_value=0
76f08c3bdfSopenharmony_ci	check_timeout=0
77f08c3bdfSopenharmony_ci	check_known_to_fail=0
78f08c3bdfSopenharmony_ci	check_error_ignore=0
79f08c3bdfSopenharmony_ci	check_output_ignore=0
80f08c3bdfSopenharmony_ci	check_output_contains=0
81f08c3bdfSopenharmony_ci	check_output_excludes=0
82f08c3bdfSopenharmony_ci	check_output_pattern=0
83f08c3bdfSopenharmony_ci	check_output_match=0
84f08c3bdfSopenharmony_ci	check_output_returns=0
85f08c3bdfSopenharmony_ci	check_arch_ignore=""
86f08c3bdfSopenharmony_ci	check_arch_only=""
87f08c3bdfSopenharmony_ci	check_assert=""
88f08c3bdfSopenharmony_ci	check_cpp_if=""
89f08c3bdfSopenharmony_ci
90f08c3bdfSopenharmony_ci	lines=$(grep '^ \* check-[a-z-]*' $1 | \
91f08c3bdfSopenharmony_ci		sed -e 's/^ \* \(check-[a-z-]*:*\) *\(.*\)$/\1 \2/')
92f08c3bdfSopenharmony_ci
93f08c3bdfSopenharmony_ci	while read tag val; do
94f08c3bdfSopenharmony_ci		#echo "-> tag: '$tag'"
95f08c3bdfSopenharmony_ci		#echo "-> val: '$val'"
96f08c3bdfSopenharmony_ci		case $tag in
97f08c3bdfSopenharmony_ci		check-name:)		check_name="$val" ;;
98f08c3bdfSopenharmony_ci		check-command:)		check_command="$val" ;;
99f08c3bdfSopenharmony_ci		check-exit-value:)	check_exit_value="$val" ;;
100f08c3bdfSopenharmony_ci		check-timeout:)		[ -z "$val" ] && val=1
101f08c3bdfSopenharmony_ci					check_timeout="$val" ;;
102f08c3bdfSopenharmony_ci		check-known-to-fail)	check_known_to_fail=1 ;;
103f08c3bdfSopenharmony_ci		check-error-ignore)	check_error_ignore=1 ;;
104f08c3bdfSopenharmony_ci		check-output-ignore)	check_output_ignore=1 ;;
105f08c3bdfSopenharmony_ci		check-output-contains:)	check_output_contains=1 ;;
106f08c3bdfSopenharmony_ci		check-output-excludes:)	check_output_excludes=1 ;;
107f08c3bdfSopenharmony_ci		check-output-pattern)	check_output_pattern=1 ;;
108f08c3bdfSopenharmony_ci		check-output-match)	check_output_match=1 ;;
109f08c3bdfSopenharmony_ci		check-output-returns:)	check_output_returns=1 ;;
110f08c3bdfSopenharmony_ci		check-arch-ignore:)	arch=$(uname -m)
111f08c3bdfSopenharmony_ci					check_arch_ignore="$val" ;;
112f08c3bdfSopenharmony_ci		check-arch-only:)	arch=$(uname -m)
113f08c3bdfSopenharmony_ci					check_arch_only="$val" ;;
114f08c3bdfSopenharmony_ci		check-assert:)		check_assert="$val" ;;
115f08c3bdfSopenharmony_ci		check-cpp-if:)		check_cpp_if="$val" ;;
116f08c3bdfSopenharmony_ci
117f08c3bdfSopenharmony_ci		check-description:)	;;	# ignore
118f08c3bdfSopenharmony_ci		check-note:)		;;	# ignore
119f08c3bdfSopenharmony_ci		check-warning:)		;;	# ignore
120f08c3bdfSopenharmony_ci		check-error-start)	;;	# ignore
121f08c3bdfSopenharmony_ci		check-error-end)	;;	# ignore
122f08c3bdfSopenharmony_ci		check-output-start)	;;	# ignore
123f08c3bdfSopenharmony_ci		check-output-end)	;;	# ignore
124f08c3bdfSopenharmony_ci		check-should-pass)	;;	# ignore, unused annotation
125f08c3bdfSopenharmony_ci		check-should-fail)	;;	# ignore, unused annotation
126f08c3bdfSopenharmony_ci		check-should-warn)	;;	# ignore, unused annotation
127f08c3bdfSopenharmony_ci		check-*)		error "$1: unknown tag '$tag'" 1 ;;
128f08c3bdfSopenharmony_ci		esac
129f08c3bdfSopenharmony_ci	done << EOT
130f08c3bdfSopenharmony_ci	$lines
131f08c3bdfSopenharmony_ciEOT
132f08c3bdfSopenharmony_ci}
133f08c3bdfSopenharmony_ci
134f08c3bdfSopenharmony_ci##
135f08c3bdfSopenharmony_ci# helper for has_(each|none)_patterns()
136f08c3bdfSopenharmony_cihas_patterns()
137f08c3bdfSopenharmony_ci{
138f08c3bdfSopenharmony_ci	ifile="$1"
139f08c3bdfSopenharmony_ci	patt="$2"
140f08c3bdfSopenharmony_ci	ofile="$3"
141f08c3bdfSopenharmony_ci	cmp="$4"
142f08c3bdfSopenharmony_ci	msg="$5"
143f08c3bdfSopenharmony_ci	grep "$patt:" "$ifile" | \
144f08c3bdfSopenharmony_ci	sed -e "s/^.*$patt: *\(.*\)$/\1/" | \
145f08c3bdfSopenharmony_ci	while read val; do
146f08c3bdfSopenharmony_ci		grep -s -q "$val" "$ofile"
147f08c3bdfSopenharmony_ci		if [ "$?" $cmp 0 ]; then
148f08c3bdfSopenharmony_ci			error "	Pattern '$val' unexpectedly $msg"
149f08c3bdfSopenharmony_ci			return 1
150f08c3bdfSopenharmony_ci		fi
151f08c3bdfSopenharmony_ci	done
152f08c3bdfSopenharmony_ci
153f08c3bdfSopenharmony_ci	return $?
154f08c3bdfSopenharmony_ci}
155f08c3bdfSopenharmony_ci
156f08c3bdfSopenharmony_ci##
157f08c3bdfSopenharmony_ci# has_each_patterns(ifile tag ofile) - does ofile contains some
158f08c3bdfSopenharmony_ci#                        of the patterns given by ifile's tags?
159f08c3bdfSopenharmony_ci#
160f08c3bdfSopenharmony_ci# returns 0 if all present, 1 otherwise
161f08c3bdfSopenharmony_cihas_each_patterns()
162f08c3bdfSopenharmony_ci{
163f08c3bdfSopenharmony_ci	has_patterns "$1" "$2" "$4" -ne "$3"
164f08c3bdfSopenharmony_ci}
165f08c3bdfSopenharmony_ci
166f08c3bdfSopenharmony_ci##
167f08c3bdfSopenharmony_ci# has_none_patterns(ifile tag ofile) - does ofile contains some
168f08c3bdfSopenharmony_ci#                        of the patterns given by ifile's tags?
169f08c3bdfSopenharmony_ci#
170f08c3bdfSopenharmony_ci# returns 1 if any present, 0 otherwise
171f08c3bdfSopenharmony_cihas_none_patterns()
172f08c3bdfSopenharmony_ci{
173f08c3bdfSopenharmony_ci	has_patterns "$1" "$2" "$4" -eq "$3"
174f08c3bdfSopenharmony_ci}
175f08c3bdfSopenharmony_ci
176f08c3bdfSopenharmony_ci##
177f08c3bdfSopenharmony_ci# minmax_patterns(ifile tag ofile) - does ofile contains the
178f08c3bdfSopenharmony_ci#                        the patterns given by ifile's tags
179f08c3bdfSopenharmony_ci#                        the right number of time?
180f08c3bdfSopenharmony_ciminmax_patterns()
181f08c3bdfSopenharmony_ci{
182f08c3bdfSopenharmony_ci	ifile="$1"
183f08c3bdfSopenharmony_ci	patt="$2"
184f08c3bdfSopenharmony_ci	ofile="$3"
185f08c3bdfSopenharmony_ci	grep "$patt([0-9-]*\(, *\)*[0-9-]*):" "$ifile" | \
186f08c3bdfSopenharmony_ci	sed -e "s/^.*$patt(\([0-9]*\)): *\(.*\)/\1 eq \2/" \
187f08c3bdfSopenharmony_ci	    -e "s/^.*$patt(\([0-9-]*\), *\([0-9-]*\)): *\(.*\)/\1 \2 \3/" | \
188f08c3bdfSopenharmony_ci	while read min max pat; do
189f08c3bdfSopenharmony_ci		n=$(grep -s "$pat" "$ofile" | wc -l)
190f08c3bdfSopenharmony_ci		if [ "$max" = "eq" ]; then
191f08c3bdfSopenharmony_ci		    if [ "$n" -ne "$min" ]; then
192f08c3bdfSopenharmony_ci			error "	Pattern '$pat' expected $min times but got $n times"
193f08c3bdfSopenharmony_ci			return 1
194f08c3bdfSopenharmony_ci		    fi
195f08c3bdfSopenharmony_ci		    continue
196f08c3bdfSopenharmony_ci		fi
197f08c3bdfSopenharmony_ci		if [ "$min" != '-' ]; then
198f08c3bdfSopenharmony_ci		    if [ "$n" -lt "$min" ]; then
199f08c3bdfSopenharmony_ci			error "	Pattern '$pat' expected min $min times but got $n times"
200f08c3bdfSopenharmony_ci			return 1
201f08c3bdfSopenharmony_ci		    fi
202f08c3bdfSopenharmony_ci		fi
203f08c3bdfSopenharmony_ci		if [ "$max" != '-' ]; then
204f08c3bdfSopenharmony_ci		    if [ "$n" -gt "$max" ]; then
205f08c3bdfSopenharmony_ci			error "	Pattern '$pat' expected max $max times but got $n times"
206f08c3bdfSopenharmony_ci			return 1
207f08c3bdfSopenharmony_ci		    fi
208f08c3bdfSopenharmony_ci		fi
209f08c3bdfSopenharmony_ci	done
210f08c3bdfSopenharmony_ci
211f08c3bdfSopenharmony_ci	return $?
212f08c3bdfSopenharmony_ci}
213f08c3bdfSopenharmony_ci
214f08c3bdfSopenharmony_ci##
215f08c3bdfSopenharmony_cimatch_patterns()
216f08c3bdfSopenharmony_ci{
217f08c3bdfSopenharmony_ci	ifile="$1"
218f08c3bdfSopenharmony_ci	patt="$2"
219f08c3bdfSopenharmony_ci	ofile="$3"
220f08c3bdfSopenharmony_ci	grep "$patt" "$ifile" | sed -e "s/^.*$patt(\(.*\)): *\(.*\)$/\1 \2/" | \
221f08c3bdfSopenharmony_ci	while read ins pat; do
222f08c3bdfSopenharmony_ci		grep -s "^	$ins\\.*[0-9]* " "$ofile" | grep -v -s -q "$pat"
223f08c3bdfSopenharmony_ci		if [ "$?" -ne 1 ]; then
224f08c3bdfSopenharmony_ci			error "	IR doesn't match '$pat'"
225f08c3bdfSopenharmony_ci			return 1
226f08c3bdfSopenharmony_ci		fi
227f08c3bdfSopenharmony_ci	done
228f08c3bdfSopenharmony_ci
229f08c3bdfSopenharmony_ci	return $?
230f08c3bdfSopenharmony_ci}
231f08c3bdfSopenharmony_ci
232f08c3bdfSopenharmony_ci##
233f08c3bdfSopenharmony_cireturn_patterns()
234f08c3bdfSopenharmony_ci{
235f08c3bdfSopenharmony_ci	ifile="$1"
236f08c3bdfSopenharmony_ci	patt="$2"
237f08c3bdfSopenharmony_ci	ofile="$3"
238f08c3bdfSopenharmony_ci	grep "$patt:" "$ifile" | sed -e "s/^.*$patt: *\(.*\)$/\1/" | \
239f08c3bdfSopenharmony_ci	while read ret; do
240f08c3bdfSopenharmony_ci		grep -s "^	ret\\.[0-9]" "$ofile" | grep -v -s -q "[ \$]${ret}\$"
241f08c3bdfSopenharmony_ci		if [ "$?" -ne 1 ]; then
242f08c3bdfSopenharmony_ci			error "	Return doesn't match '$ret'"
243f08c3bdfSopenharmony_ci			return 1
244f08c3bdfSopenharmony_ci		fi
245f08c3bdfSopenharmony_ci	done
246f08c3bdfSopenharmony_ci
247f08c3bdfSopenharmony_ci	return $?
248f08c3bdfSopenharmony_ci}
249f08c3bdfSopenharmony_ci
250f08c3bdfSopenharmony_ci##
251f08c3bdfSopenharmony_ci# arg_file(filename) - checks if filename exists
252f08c3bdfSopenharmony_ciarg_file()
253f08c3bdfSopenharmony_ci{
254f08c3bdfSopenharmony_ci	[ -z "$1" ] && {
255f08c3bdfSopenharmony_ci		do_usage
256f08c3bdfSopenharmony_ci		exit 1
257f08c3bdfSopenharmony_ci	}
258f08c3bdfSopenharmony_ci	[ -e "$1" ] || {
259f08c3bdfSopenharmony_ci		error "Can't open file $1"
260f08c3bdfSopenharmony_ci		exit 1
261f08c3bdfSopenharmony_ci	}
262f08c3bdfSopenharmony_ci	return 0
263f08c3bdfSopenharmony_ci}
264f08c3bdfSopenharmony_ci
265f08c3bdfSopenharmony_ci
266f08c3bdfSopenharmony_ci##
267f08c3bdfSopenharmony_cido_usage()
268f08c3bdfSopenharmony_ci{
269f08c3bdfSopenharmony_ciecho "$prog_name - a tiny automatic testing script"
270f08c3bdfSopenharmony_ciecho "Usage: $prog_name [option(s)] [command] [arguments]"
271f08c3bdfSopenharmony_ciecho
272f08c3bdfSopenharmony_ciecho "options:"
273f08c3bdfSopenharmony_ciecho "    -a|--abort                 Abort the tests as soon as one fails."
274f08c3bdfSopenharmony_ciecho "    -q|--quiet                 Be extra quiet while running the tests."
275f08c3bdfSopenharmony_ciecho "    --args='...'               Add these options to the test command."
276f08c3bdfSopenharmony_ciecho
277f08c3bdfSopenharmony_ciecho "commands:"
278f08c3bdfSopenharmony_ciecho "    [file ...]                 Runs the test suite on the given file(s)."
279f08c3bdfSopenharmony_ciecho "                               If a directory is given, run only those files."
280f08c3bdfSopenharmony_ciecho "                               If no file is given, run the whole testsuite."
281f08c3bdfSopenharmony_ciecho "    single file                Run the test in 'file'."
282f08c3bdfSopenharmony_ciecho "    format file [name [cmd]]   Help writing a new test case using cmd."
283f08c3bdfSopenharmony_ciecho
284f08c3bdfSopenharmony_ciecho "    [command] help             Print usage."
285f08c3bdfSopenharmony_ci}
286f08c3bdfSopenharmony_ci
287f08c3bdfSopenharmony_cidisable()
288f08c3bdfSopenharmony_ci{
289f08c3bdfSopenharmony_ci	disabled_tests=$(($disabled_tests + 1))
290f08c3bdfSopenharmony_ci	if [ -z "$vquiet" ]; then
291f08c3bdfSopenharmony_ci		echo "  SKIP    $1 ($2)"
292f08c3bdfSopenharmony_ci	fi
293f08c3bdfSopenharmony_ci}
294f08c3bdfSopenharmony_ci
295f08c3bdfSopenharmony_ci##
296f08c3bdfSopenharmony_ci# do_test(file) - tries to validate a test case
297f08c3bdfSopenharmony_ci#
298f08c3bdfSopenharmony_ci# it "parses" file, looking for check-* tags and tries to validate
299f08c3bdfSopenharmony_ci# the test against an expected result
300f08c3bdfSopenharmony_ci# returns:
301f08c3bdfSopenharmony_ci#	- 0 if the test passed,
302f08c3bdfSopenharmony_ci#	- 1 if it failed,
303f08c3bdfSopenharmony_ci#	- 2 if it is not a "test-suite" test.
304f08c3bdfSopenharmony_ci#	- 3 if the test is disabled.
305f08c3bdfSopenharmony_cido_test()
306f08c3bdfSopenharmony_ci{
307f08c3bdfSopenharmony_ci	test_failed=0
308f08c3bdfSopenharmony_ci	file="$1"
309f08c3bdfSopenharmony_ci	quiet=0
310f08c3bdfSopenharmony_ci
311f08c3bdfSopenharmony_ci	get_tag_value $file
312f08c3bdfSopenharmony_ci
313f08c3bdfSopenharmony_ci	# can this test be handled by test-suite ?
314f08c3bdfSopenharmony_ci	# (it has to have a check-name key in it)
315f08c3bdfSopenharmony_ci	if [ "$check_name" = "" ]; then
316f08c3bdfSopenharmony_ci		warning "$file: test unhandled"
317f08c3bdfSopenharmony_ci		unhandled_tests=$(($unhandled_tests + 1))
318f08c3bdfSopenharmony_ci		return 2
319f08c3bdfSopenharmony_ci	fi
320f08c3bdfSopenharmony_ci	test_name="$check_name"
321f08c3bdfSopenharmony_ci
322f08c3bdfSopenharmony_ci	# does the test provide a specific command ?
323f08c3bdfSopenharmony_ci	if [ "$check_command" = "" ]; then
324f08c3bdfSopenharmony_ci		check_command="$defaut_command"
325f08c3bdfSopenharmony_ci	fi
326f08c3bdfSopenharmony_ci
327f08c3bdfSopenharmony_ci	# check for disabled commands
328f08c3bdfSopenharmony_ci	set -- $check_command
329f08c3bdfSopenharmony_ci	base_cmd=$1
330f08c3bdfSopenharmony_ci	for i in $disabled_cmds; do
331f08c3bdfSopenharmony_ci		if [ "$i" = "$base_cmd" ] ; then
332f08c3bdfSopenharmony_ci			disable "$test_name" "$file"
333f08c3bdfSopenharmony_ci			return 3
334f08c3bdfSopenharmony_ci		fi
335f08c3bdfSopenharmony_ci	done
336f08c3bdfSopenharmony_ci	if [ "$check_arch_ignore" != "" ]; then
337f08c3bdfSopenharmony_ci		if echo $arch | egrep -q -w "$check_arch_ignore"; then
338f08c3bdfSopenharmony_ci			disable "$test_name" "$file"
339f08c3bdfSopenharmony_ci			return 3
340f08c3bdfSopenharmony_ci		fi
341f08c3bdfSopenharmony_ci	fi
342f08c3bdfSopenharmony_ci	if [ "$check_arch_only" != "" ]; then
343f08c3bdfSopenharmony_ci		if ! (echo $arch | egrep -q -w "$check_arch_only"); then
344f08c3bdfSopenharmony_ci			disable "$test_name" "$file"
345f08c3bdfSopenharmony_ci			return 3
346f08c3bdfSopenharmony_ci		fi
347f08c3bdfSopenharmony_ci	fi
348f08c3bdfSopenharmony_ci	if [ "$check_assert" != "" ]; then
349f08c3bdfSopenharmony_ci		res=$(../sparse - 2>&1 >/dev/null <<- EOF
350f08c3bdfSopenharmony_ci			_Static_assert($check_assert, "$check_assert");
351f08c3bdfSopenharmony_ci			EOF
352f08c3bdfSopenharmony_ci		)
353f08c3bdfSopenharmony_ci		if [ "$res" != "" ]; then
354f08c3bdfSopenharmony_ci			disable "$test_name" "$file"
355f08c3bdfSopenharmony_ci			return 3
356f08c3bdfSopenharmony_ci		fi
357f08c3bdfSopenharmony_ci	fi
358f08c3bdfSopenharmony_ci	if [ "$check_cpp_if" != "" ]; then
359f08c3bdfSopenharmony_ci		res=$(../sparse -E - 2>/dev/null <<- EOF
360f08c3bdfSopenharmony_ci			#if !($check_cpp_if)
361f08c3bdfSopenharmony_ci			fail
362f08c3bdfSopenharmony_ci			#endif
363f08c3bdfSopenharmony_ci			EOF
364f08c3bdfSopenharmony_ci		)
365f08c3bdfSopenharmony_ci		if [ "$res" != "" ]; then
366f08c3bdfSopenharmony_ci			disable "$test_name" "$file"
367f08c3bdfSopenharmony_ci			return 3
368f08c3bdfSopenharmony_ci		fi
369f08c3bdfSopenharmony_ci	fi
370f08c3bdfSopenharmony_ci
371f08c3bdfSopenharmony_ci	if [ -z "$vquiet" ]; then
372f08c3bdfSopenharmony_ci		echo "  TEST    $test_name ($file)"
373f08c3bdfSopenharmony_ci	fi
374f08c3bdfSopenharmony_ci
375f08c3bdfSopenharmony_ci	verbose "Using command       : $(echo "$@")"
376f08c3bdfSopenharmony_ci
377f08c3bdfSopenharmony_ci	# grab the expected exit value
378f08c3bdfSopenharmony_ci	expected_exit_value=$check_exit_value
379f08c3bdfSopenharmony_ci	verbose "Expecting exit value: $expected_exit_value"
380f08c3bdfSopenharmony_ci
381f08c3bdfSopenharmony_ci	# do we want a timeout?
382f08c3bdfSopenharmony_ci	pre_cmd=""
383f08c3bdfSopenharmony_ci	if [ $check_timeout -ne 0 ]; then
384f08c3bdfSopenharmony_ci		pre_cmd="timeout $check_timeout"
385f08c3bdfSopenharmony_ci	fi
386f08c3bdfSopenharmony_ci
387f08c3bdfSopenharmony_ci	shift
388f08c3bdfSopenharmony_ci	# launch the test command and
389f08c3bdfSopenharmony_ci	# grab the actual output & exit value
390f08c3bdfSopenharmony_ci	eval $pre_cmd $default_path/$base_cmd $default_args "$@" \
391f08c3bdfSopenharmony_ci		1> $file.output.got 2> $file.error.got
392f08c3bdfSopenharmony_ci	actual_exit_value=$?
393f08c3bdfSopenharmony_ci
394f08c3bdfSopenharmony_ci	must_fail=$check_known_to_fail
395f08c3bdfSopenharmony_ci	[ $must_fail -eq 1 ] && [ $V -eq 0 ] && quiet=1
396f08c3bdfSopenharmony_ci	known_ko_tests=$(($known_ko_tests + $must_fail))
397f08c3bdfSopenharmony_ci
398f08c3bdfSopenharmony_ci	for stream in error output; do
399f08c3bdfSopenharmony_ci		eval ignore=\$check_${stream}_ignore
400f08c3bdfSopenharmony_ci		[ $ignore -eq 1 ] && continue
401f08c3bdfSopenharmony_ci
402f08c3bdfSopenharmony_ci		# grab the expected output
403f08c3bdfSopenharmony_ci		sed -n "/check-$stream-start/,/check-$stream-end/p" $file \
404f08c3bdfSopenharmony_ci		| grep -v check-$stream > "$file".$stream.expected
405f08c3bdfSopenharmony_ci
406f08c3bdfSopenharmony_ci		diff -u "$file".$stream.expected "$file".$stream.got > "$file".$stream.diff
407f08c3bdfSopenharmony_ci		if [ "$?" -ne "0" ]; then
408f08c3bdfSopenharmony_ci			error "actual $stream text does not match expected $stream text."
409f08c3bdfSopenharmony_ci			error  "see $file.$stream.* for further investigation."
410f08c3bdfSopenharmony_ci			[ $quiet -ne 1 ] && cat "$file".$stream.diff
411f08c3bdfSopenharmony_ci			test_failed=1
412f08c3bdfSopenharmony_ci		fi
413f08c3bdfSopenharmony_ci	done
414f08c3bdfSopenharmony_ci
415f08c3bdfSopenharmony_ci	if [ "$actual_exit_value" -ne "$expected_exit_value" ]; then
416f08c3bdfSopenharmony_ci		error "Actual exit value does not match the expected one."
417f08c3bdfSopenharmony_ci		error "expected $expected_exit_value, got $actual_exit_value."
418f08c3bdfSopenharmony_ci		test_failed=1
419f08c3bdfSopenharmony_ci	fi
420f08c3bdfSopenharmony_ci
421f08c3bdfSopenharmony_ci	# verify the 'check-output-contains/excludes' tags
422f08c3bdfSopenharmony_ci	if [ $check_output_contains -eq 1 ]; then
423f08c3bdfSopenharmony_ci		has_each_patterns "$file" 'check-output-contains' absent $file.output.got
424f08c3bdfSopenharmony_ci		if [ "$?" -ne "0" ]; then
425f08c3bdfSopenharmony_ci			test_failed=1
426f08c3bdfSopenharmony_ci		fi
427f08c3bdfSopenharmony_ci	fi
428f08c3bdfSopenharmony_ci	if [ $check_output_excludes -eq 1 ]; then
429f08c3bdfSopenharmony_ci		has_none_patterns "$file" 'check-output-excludes' present $file.output.got
430f08c3bdfSopenharmony_ci		if [ "$?" -ne "0" ]; then
431f08c3bdfSopenharmony_ci			test_failed=1
432f08c3bdfSopenharmony_ci		fi
433f08c3bdfSopenharmony_ci	fi
434f08c3bdfSopenharmony_ci	if [ $check_output_pattern -eq 1 ]; then
435f08c3bdfSopenharmony_ci		# verify the 'check-output-pattern(...)' tags
436f08c3bdfSopenharmony_ci		minmax_patterns "$file" 'check-output-pattern' $file.output.got
437f08c3bdfSopenharmony_ci		if [ "$?" -ne "0" ]; then
438f08c3bdfSopenharmony_ci			test_failed=1
439f08c3bdfSopenharmony_ci		fi
440f08c3bdfSopenharmony_ci	fi
441f08c3bdfSopenharmony_ci	if [ $check_output_match -eq 1 ]; then
442f08c3bdfSopenharmony_ci		# verify the 'check-output-match($insn): $patt' tags
443f08c3bdfSopenharmony_ci		match_patterns "$file" 'check-output-match' $file.output.got
444f08c3bdfSopenharmony_ci		if [ "$?" -ne "0" ]; then
445f08c3bdfSopenharmony_ci			test_failed=1
446f08c3bdfSopenharmony_ci		fi
447f08c3bdfSopenharmony_ci	fi
448f08c3bdfSopenharmony_ci	if [ $check_output_returns -eq 1 ]; then
449f08c3bdfSopenharmony_ci		# verify the 'check-output-return: $value' tags
450f08c3bdfSopenharmony_ci		return_patterns "$file" 'check-output-returns' $file.output.got
451f08c3bdfSopenharmony_ci		if [ "$?" -ne "0" ]; then
452f08c3bdfSopenharmony_ci			test_failed=1
453f08c3bdfSopenharmony_ci		fi
454f08c3bdfSopenharmony_ci	fi
455f08c3bdfSopenharmony_ci
456f08c3bdfSopenharmony_ci	if [ "$must_fail" -eq "1" ]; then
457f08c3bdfSopenharmony_ci		if [ "$test_failed" -eq "1" ]; then
458f08c3bdfSopenharmony_ci			[ -z "$vquiet" ] && \
459f08c3bdfSopenharmony_ci			echo "info: XFAIL: test '$file' is known to fail"
460f08c3bdfSopenharmony_ci		else
461f08c3bdfSopenharmony_ci			echo "error: XPASS: test '$file' is known to fail but succeed!"
462f08c3bdfSopenharmony_ci		fi
463f08c3bdfSopenharmony_ci	else
464f08c3bdfSopenharmony_ci		if [ "$test_failed" -eq "1" ]; then
465f08c3bdfSopenharmony_ci			echo "error: FAIL: test '$file' failed"
466f08c3bdfSopenharmony_ci		else
467f08c3bdfSopenharmony_ci			[ "$V" -ne "0" ] && \
468f08c3bdfSopenharmony_ci			echo "info: PASS: test '$file' passed"
469f08c3bdfSopenharmony_ci		fi
470f08c3bdfSopenharmony_ci	fi
471f08c3bdfSopenharmony_ci
472f08c3bdfSopenharmony_ci	if [ "$test_failed" -ne "$must_fail" ]; then
473f08c3bdfSopenharmony_ci		[ $abort -eq 1 ] && exit 1
474f08c3bdfSopenharmony_ci		test_failed=1
475f08c3bdfSopenharmony_ci		failed=1
476f08c3bdfSopenharmony_ci	fi
477f08c3bdfSopenharmony_ci
478f08c3bdfSopenharmony_ci	if [ "$test_failed" -eq "1" ]; then
479f08c3bdfSopenharmony_ci		ko_tests=$(($ko_tests + 1))
480f08c3bdfSopenharmony_ci	else
481f08c3bdfSopenharmony_ci		ok_tests=$(($ok_tests + 1))
482f08c3bdfSopenharmony_ci		rm -f $file.{error,output}.{expected,got,diff}
483f08c3bdfSopenharmony_ci	fi
484f08c3bdfSopenharmony_ci	return $test_failed
485f08c3bdfSopenharmony_ci}
486f08c3bdfSopenharmony_ci
487f08c3bdfSopenharmony_cido_test_suite()
488f08c3bdfSopenharmony_ci{
489f08c3bdfSopenharmony_ci	for i in $tests_list; do
490f08c3bdfSopenharmony_ci		do_test "$i"
491f08c3bdfSopenharmony_ci	done
492f08c3bdfSopenharmony_ci
493f08c3bdfSopenharmony_ci	OK=OK
494f08c3bdfSopenharmony_ci	[ $failed -eq 0 ] || OK=KO
495f08c3bdfSopenharmony_ci
496f08c3bdfSopenharmony_ci	# prints some numbers
497f08c3bdfSopenharmony_ci	tests_nr=$(($ok_tests + $ko_tests))
498f08c3bdfSopenharmony_ci	echo "$OK: out of $tests_nr tests, $ok_tests passed, $ko_tests failed"
499f08c3bdfSopenharmony_ci	if [ "$known_ko_tests" -ne 0 ]; then
500f08c3bdfSopenharmony_ci		echo "	$known_ko_tests of them are known to fail"
501f08c3bdfSopenharmony_ci	fi
502f08c3bdfSopenharmony_ci	if [ "$unhandled_tests" -ne "0" ]; then
503f08c3bdfSopenharmony_ci		echo "	$unhandled_tests tests could not be handled by $prog_name"
504f08c3bdfSopenharmony_ci	fi
505f08c3bdfSopenharmony_ci	if [ "$disabled_tests" -ne "0" ]; then
506f08c3bdfSopenharmony_ci		echo "	$disabled_tests tests were disabled"
507f08c3bdfSopenharmony_ci	fi
508f08c3bdfSopenharmony_ci}
509f08c3bdfSopenharmony_ci
510f08c3bdfSopenharmony_ci##
511f08c3bdfSopenharmony_cido_format_help() {
512f08c3bdfSopenharmony_ciecho "Usage: $prog_name [option(s)] [--]format file [name [cmd]]"
513f08c3bdfSopenharmony_ciecho
514f08c3bdfSopenharmony_ciecho "options:"
515f08c3bdfSopenharmony_ciecho "    -a                         append the created test to the input file"
516f08c3bdfSopenharmony_ciecho "    -f                         write a test known to fail"
517f08c3bdfSopenharmony_ciecho "    -l                         write a test for linearized code"
518f08c3bdfSopenharmony_ciecho "    -r                         write a test for linearized code returning 1"
519f08c3bdfSopenharmony_ciecho "    -p                         write a test for pre-processing"
520f08c3bdfSopenharmony_ciecho "    -s                         write a test for symbolic checking"
521f08c3bdfSopenharmony_ciecho
522f08c3bdfSopenharmony_ciecho "argument(s):"
523f08c3bdfSopenharmony_ciecho "    file                       file containing the test case(s)"
524f08c3bdfSopenharmony_ciecho "    name                       name for the test case (defaults to file)"
525f08c3bdfSopenharmony_ciecho "    cmd                        command to be used (defaults to 'sparse \$file')"
526f08c3bdfSopenharmony_ci}
527f08c3bdfSopenharmony_ci
528f08c3bdfSopenharmony_ci##
529f08c3bdfSopenharmony_ci# do_format([options,] file[, name[, cmd]]) - helps a test writer to format test-suite tags
530f08c3bdfSopenharmony_cido_format()
531f08c3bdfSopenharmony_ci{
532f08c3bdfSopenharmony_ci	def_cmd="$default_cmd"
533f08c3bdfSopenharmony_ci	append=0
534f08c3bdfSopenharmony_ci	linear=0
535f08c3bdfSopenharmony_ci	fail=0
536f08c3bdfSopenharmony_ci	ret=''
537f08c3bdfSopenharmony_ci
538f08c3bdfSopenharmony_ci	while [ $# -gt 0 ] ; do
539f08c3bdfSopenharmony_ci		case "$1" in
540f08c3bdfSopenharmony_ci		-a)
541f08c3bdfSopenharmony_ci			append=1 ;;
542f08c3bdfSopenharmony_ci		-f)
543f08c3bdfSopenharmony_ci			fail=1 ;;
544f08c3bdfSopenharmony_ci		-l)
545f08c3bdfSopenharmony_ci			def_cmd='test-linearize -Wno-decl $file'
546f08c3bdfSopenharmony_ci			linear=1 ;;
547f08c3bdfSopenharmony_ci		-r)
548f08c3bdfSopenharmony_ci			def_cmd='test-linearize -Wno-decl $file'
549f08c3bdfSopenharmony_ci			ret=1 ;;
550f08c3bdfSopenharmony_ci		-p)
551f08c3bdfSopenharmony_ci			def_cmd='sparse -E $file' ;;
552f08c3bdfSopenharmony_ci		-s)
553f08c3bdfSopenharmony_ci			def_cmd='scheck $file' ;;
554f08c3bdfSopenharmony_ci
555f08c3bdfSopenharmony_ci		help|-*)
556f08c3bdfSopenharmony_ci			do_format_help
557f08c3bdfSopenharmony_ci			return 0
558f08c3bdfSopenharmony_ci			;;
559f08c3bdfSopenharmony_ci		*)	break ;;
560f08c3bdfSopenharmony_ci		esac
561f08c3bdfSopenharmony_ci		shift
562f08c3bdfSopenharmony_ci		continue
563f08c3bdfSopenharmony_ci	done
564f08c3bdfSopenharmony_ci
565f08c3bdfSopenharmony_ci	if [ $# -lt 1 -o $# -gt 3 ]; then
566f08c3bdfSopenharmony_ci		do_format_help
567f08c3bdfSopenharmony_ci		return 0
568f08c3bdfSopenharmony_ci	fi
569f08c3bdfSopenharmony_ci
570f08c3bdfSopenharmony_ci	arg_file "$1" || return 1
571f08c3bdfSopenharmony_ci
572f08c3bdfSopenharmony_ci	file="$1"
573f08c3bdfSopenharmony_ci	fname="$2"
574f08c3bdfSopenharmony_ci	[ -z "$fname" ] && fname="$(basename "$1" .c)"
575f08c3bdfSopenharmony_ci	fcmd="$3"
576f08c3bdfSopenharmony_ci	[ -z "$fcmd" ] && fcmd="$def_cmd"
577f08c3bdfSopenharmony_ci
578f08c3bdfSopenharmony_ci	cmd=`eval echo $default_path/$fcmd`
579f08c3bdfSopenharmony_ci	$cmd 1> $file.output.got 2> $file.error.got
580f08c3bdfSopenharmony_ci	fexit_value=$?
581f08c3bdfSopenharmony_ci	[ $append != 0 ] && exec >> $file
582f08c3bdfSopenharmony_ci	cat <<_EOF
583f08c3bdfSopenharmony_ci
584f08c3bdfSopenharmony_ci/*
585f08c3bdfSopenharmony_ci * check-name: $fname
586f08c3bdfSopenharmony_ci_EOF
587f08c3bdfSopenharmony_ci	if [ "$fcmd" != "$default_cmd" ]; then
588f08c3bdfSopenharmony_ci		echo " * check-command: $fcmd"
589f08c3bdfSopenharmony_ci	fi
590f08c3bdfSopenharmony_ci	if [ "$fexit_value" -ne "0" ]; then
591f08c3bdfSopenharmony_ci		echo " * check-exit-value: $fexit_value"
592f08c3bdfSopenharmony_ci	fi
593f08c3bdfSopenharmony_ci	if [ $fail != 0 ]; then
594f08c3bdfSopenharmony_ci		echo " * check-known-to-fail"
595f08c3bdfSopenharmony_ci	fi
596f08c3bdfSopenharmony_ci	if [ "$ret" != '' ]; then
597f08c3bdfSopenharmony_ci		echo ' *'
598f08c3bdfSopenharmony_ci		echo ' * check-output-ignore'
599f08c3bdfSopenharmony_ci		echo " * check-output-returns: $ret"
600f08c3bdfSopenharmony_ci		rm -f "$file.output.got"
601f08c3bdfSopenharmony_ci	fi
602f08c3bdfSopenharmony_ci	if [ $linear != 0 ]; then
603f08c3bdfSopenharmony_ci		echo ' *'
604f08c3bdfSopenharmony_ci		echo ' * check-output-ignore'
605f08c3bdfSopenharmony_ci		echo ' * check-output-contains: xyz\\\\.'
606f08c3bdfSopenharmony_ci		echo ' * check-output-excludes: \\\\.'
607f08c3bdfSopenharmony_ci	fi
608f08c3bdfSopenharmony_ci	for stream in output error; do
609f08c3bdfSopenharmony_ci		if [ -s "$file.$stream.got" ]; then
610f08c3bdfSopenharmony_ci			echo " *"
611f08c3bdfSopenharmony_ci			echo " * check-$stream-start"
612f08c3bdfSopenharmony_ci			cat "$file.$stream.got"
613f08c3bdfSopenharmony_ci			echo " * check-$stream-end"
614f08c3bdfSopenharmony_ci		fi
615f08c3bdfSopenharmony_ci	done
616f08c3bdfSopenharmony_ci	echo " */"
617f08c3bdfSopenharmony_ci	return 0
618f08c3bdfSopenharmony_ci}
619f08c3bdfSopenharmony_ci
620f08c3bdfSopenharmony_ci## allow flags from environment
621f08c3bdfSopenharmony_ciset -- $SPARSE_TEST_FLAGS "$@"
622f08c3bdfSopenharmony_ci
623f08c3bdfSopenharmony_ci## process the flags
624f08c3bdfSopenharmony_ciwhile [ "$#" -gt "0" ]; do
625f08c3bdfSopenharmony_ci	case "$1" in
626f08c3bdfSopenharmony_ci	-a|--abort)
627f08c3bdfSopenharmony_ci		abort=1
628f08c3bdfSopenharmony_ci		;;
629f08c3bdfSopenharmony_ci	-q|--quiet)
630f08c3bdfSopenharmony_ci		vquiet=1
631f08c3bdfSopenharmony_ci		;;
632f08c3bdfSopenharmony_ci	--args=*)
633f08c3bdfSopenharmony_ci		default_args="${1#--args=}";
634f08c3bdfSopenharmony_ci		;;
635f08c3bdfSopenharmony_ci
636f08c3bdfSopenharmony_ci	single|--single)
637f08c3bdfSopenharmony_ci		arg_file "$2"
638f08c3bdfSopenharmony_ci		do_test "$2"
639f08c3bdfSopenharmony_ci		case "$?" in
640f08c3bdfSopenharmony_ci			0) echo "$2 passed !";;
641f08c3bdfSopenharmony_ci			1) echo "$2 failed !";;
642f08c3bdfSopenharmony_ci			2) echo "$2 can't be handled by $prog_name";;
643f08c3bdfSopenharmony_ci		esac
644f08c3bdfSopenharmony_ci		exit $failed
645f08c3bdfSopenharmony_ci		;;
646f08c3bdfSopenharmony_ci	format|--format)
647f08c3bdfSopenharmony_ci		shift
648f08c3bdfSopenharmony_ci		do_format "$@"
649f08c3bdfSopenharmony_ci		exit 0
650f08c3bdfSopenharmony_ci		;;
651f08c3bdfSopenharmony_ci	help)
652f08c3bdfSopenharmony_ci		do_usage
653f08c3bdfSopenharmony_ci		exit 1
654f08c3bdfSopenharmony_ci		;;
655f08c3bdfSopenharmony_ci
656f08c3bdfSopenharmony_ci	*.c|*.cdoc)
657f08c3bdfSopenharmony_ci		tests_list="$tests_list $1"
658f08c3bdfSopenharmony_ci		;;
659f08c3bdfSopenharmony_ci	*)
660f08c3bdfSopenharmony_ci		if [ ! -d "$1" ]; then
661f08c3bdfSopenharmony_ci			do_usage
662f08c3bdfSopenharmony_ci			exit 1
663f08c3bdfSopenharmony_ci		fi
664f08c3bdfSopenharmony_ci		tests_list="$tests_list $(find "$1" -name '*.c' | sort)"
665f08c3bdfSopenharmony_ci		;;
666f08c3bdfSopenharmony_ci	esac
667f08c3bdfSopenharmony_ci	shift
668f08c3bdfSopenharmony_cidone
669f08c3bdfSopenharmony_ci
670f08c3bdfSopenharmony_ciif [ -z "$tests_list" ]; then
671f08c3bdfSopenharmony_ci	tests_list=`find . -name '*.c' | sed -e 's#^\./\(.*\)#\1#' | sort`
672f08c3bdfSopenharmony_cifi
673f08c3bdfSopenharmony_ci
674f08c3bdfSopenharmony_cido_test_suite
675f08c3bdfSopenharmony_ciexit $failed
676f08c3bdfSopenharmony_ci
677