1#! /bin/sh
2# $NetBSD: accept.sh,v 1.15 2024/01/28 08:17:27 rillig Exp $
3#
4# Copyright (c) 2021 The NetBSD Foundation, Inc.
5# All rights reserved.
6#
7# Redistribution and use in source and binary forms, with or without
8# modification, are permitted provided that the following conditions
9# are met:
10# 1. Redistributions of source code must retain the above copyright
11#    notice, this list of conditions and the following disclaimer.
12# 2. Redistributions in binary form must reproduce the above copyright
13#    notice, this list of conditions and the following disclaimer in the
14#    documentation and/or other materials provided with the distribution.
15#
16# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19# PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26# POSSIBILITY OF SUCH DAMAGE.
27#
28
29# usage: accept.sh [-u] <pattern>...
30#
31#	Run one or more lint tests, saving their output in the corresponding
32#	.exp files, for incorporating the messages into the .c files as
33#	'expect' comments.
34
35set -eu
36
37: "${archsubdir:=$(make -v ARCHSUBDIR)}"
38. './t_integration.sh'		# for configure_test_case
39
40update_flags=''
41while getopts 'u' opt; do
42	case $opt in
43	u)	update_flags='-u';;
44	*)	echo "usage: $0 [-u] pattern..." 1>&2
45		exit 1;;
46	esac
47done
48shift $((OPTIND - 1))
49
50done_tests=''
51for pattern in "$@"; do
52	# shellcheck disable=SC2231
53	for cfile in *$pattern*.c; do
54		base=${cfile%.*}
55		exp_tmp_file="$base.exp.tmp"
56		exp_file="$base.exp"
57		ln_tmp_file="$base.exp-ln.tmp"
58		ln_file="$base.exp-ln"
59
60		configure_test_case "$cfile"
61		# shellcheck disable=SC2154
62		if [ "$skip" = yes ]; then
63			continue
64		fi
65
66		if [ ! -f "$ln_file" ]; then
67			ln_file='/dev/null'
68		fi
69
70		# shellcheck disable=SC2154
71		# shellcheck disable=SC2086
72		if "$lint1" $flags "$base.c" "$ln_tmp_file" > "$exp_tmp_file"; then
73			if [ -s "$exp_tmp_file" ]; then
74				echo "$base produces output but exits successfully"
75				sed 's,^,| ,' "$exp_tmp_file"
76			fi
77		elif [ $? -ge 128 ]; then
78			echo "$base crashed"
79			continue
80		fi
81
82		if [ -f "$exp_file" ] && cmp -s "$exp_tmp_file"  "$exp_file"; then
83			rm "$exp_tmp_file"
84		else
85			mv "$exp_tmp_file" "$exp_file"
86		fi
87
88		if [ ! -f "$ln_tmp_file" ]; then
89			: 'No cleanup necessary.'
90		elif [ "$ln_file" = '/dev/null' ]; then
91			rm "$ln_tmp_file"
92		else
93			if tr -d ' \t' < "$ln_file" > "$ln_file.trimmed.tmp" &&
94			    tr -d ' \t' < "$ln_tmp_file" > "$ln_tmp_file.trimmed.tmp" &&
95			    cmp -s "$ln_file.trimmed.tmp" "$ln_tmp_file.trimmed.tmp"; then
96				rm "$ln_tmp_file"
97			else
98				echo "Replacing $ln_file"
99				mv "$ln_tmp_file" "$ln_file"
100			fi
101			rm -f "$ln_file.trimmed.tmp" "$ln_tmp_file.trimmed.tmp"
102		fi
103
104		case "$base" in (msg_*)
105			if grep 'This message is not used\.' "$cfile" >/dev/null; then
106				: 'Skip further checks.'
107			elif [ ! -s "$exp_file" ]; then
108				echo "$base should produce warnings"
109			elif grep '^TODO: "Add example code' "$cfile" >/dev/null; then
110				: 'ok, this test is not yet written'
111			else
112				msgid=${base}
113				msgid=${msgid#msg_00}
114				msgid=${msgid#msg_0}
115				msgid=${msgid#msg_}
116				msgid=${msgid%%_*}
117				if ! grep "\\[$msgid\\]\$" "$exp_file" >/dev/null; then
118					echo "$base should trigger the message '$msgid'"
119				fi
120			fi
121		esac
122
123		done_tests="$done_tests $cfile"
124	done
125done
126
127# shellcheck disable=SC2086
128lua './check-expect.lua' $update_flags $done_tests
129