accept.sh revision 1.13
1#! /bin/sh
2# $NetBSD: accept.sh,v 1.13 2023/07/08 08:02:45 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 <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
40done_tests=''
41for pattern in "$@"; do
42	# shellcheck disable=SC2231
43	for cfile in *$pattern*.c; do
44		base=${cfile%.*}
45		exp_tmp_file="$base.exp.tmp"
46		exp_file="$base.exp"
47		ln_tmp_file="$base.exp-ln.tmp"
48		ln_file="$base.exp-ln"
49
50		configure_test_case "$cfile"
51		# shellcheck disable=SC2154
52		if [ "$skip" = yes ]; then
53			continue
54		fi
55
56		if [ ! -f "$ln_file" ]; then
57			ln_file='/dev/null'
58		fi
59
60		# shellcheck disable=SC2154
61		# shellcheck disable=SC2086
62		if "$lint1" $flags "$base.c" "$ln_tmp_file" > "$exp_tmp_file"; then
63			if [ -s "$exp_tmp_file" ]; then
64				echo "$base produces output but exits successfully"
65				sed 's,^,| ,' "$exp_tmp_file"
66			fi
67		elif [ $? -ge 128 ]; then
68			echo "$base crashed"
69			continue
70		fi
71
72		if [ -f "$exp_file" ] && cmp -s "$exp_tmp_file"  "$exp_file"; then
73			rm "$exp_tmp_file"
74		else
75			mv "$exp_tmp_file" "$exp_file"
76		fi
77
78		if [ ! -f "$ln_tmp_file" ]; then
79			: 'No cleanup necessary.'
80		elif [ "$ln_file" = '/dev/null' ]; then
81			rm "$ln_tmp_file"
82		else
83			if tr -d ' \t' < "$ln_file" > "$ln_file.trimmed.tmp" &&
84			    tr -d ' \t' < "$ln_tmp_file" > "$ln_tmp_file.trimmed.tmp" &&
85			    cmp -s "$ln_file.trimmed.tmp" "$ln_tmp_file.trimmed.tmp"; then
86				rm "$ln_tmp_file"
87			else
88				echo "Replacing $ln_file"
89				mv "$ln_tmp_file" "$ln_file"
90			fi
91			rm -f "$ln_file.trimmed.tmp" "$ln_tmp_file.trimmed.tmp"
92		fi
93
94		case "$base" in (msg_*)
95			if grep 'This message is not used\.' "$cfile" >/dev/null; then
96				: 'Skip further checks.'
97			elif [ ! -s "$exp_file" ]; then
98				echo "$base should produce warnings"
99			elif grep '^TODO: "Add example code' "$cfile" >/dev/null; then
100				: 'ok, this test is not yet written'
101			else
102				msgid=${base}
103				msgid=${msgid#msg_00}
104				msgid=${msgid#msg_0}
105				msgid=${msgid#msg_}
106				msgid=${msgid%%_*}
107				if ! grep "\\[$msgid\\]\$" "$exp_file" >/dev/null; then
108					echo "$base should trigger the message '$msgid'"
109				fi
110			fi
111		esac
112
113		done_tests="$done_tests $cfile"
114	done
115done
116
117# shellcheck disable=SC2086
118lua './check-expect.lua' $done_tests
119