xref: /netbsd-src/tests/usr.bin/xlint/lint1/t_integration.sh (revision 3117ece4fc4a4ca4489ba793710b60b0d26bab6c)
1# $NetBSD: t_integration.sh,v 1.84 2024/06/08 06:42:59 rillig Exp $
2#
3# Copyright (c) 2008, 2010 The NetBSD Foundation, Inc.
4# All rights reserved.
5#
6# Redistribution and use in source and binary forms, with or without
7# modification, are permitted provided that the following conditions
8# are met:
9# 1. Redistributions of source code must retain the above copyright
10#    notice, this list of conditions and the following disclaimer.
11# 2. Redistributions in binary form must reproduce the above copyright
12#    notice, this list of conditions and the following disclaimer in the
13#    documentation and/or other materials provided with the distribution.
14#
15# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
16# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18# PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
19# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25# POSSIBILITY OF SUCH DAMAGE.
26#
27
28: "${lint1:=/usr/libexec/lint1}"
29: "${archsubdir:=archsubdir_must_be_set}"
30
31
32configure_test_case()
33{
34	local awk
35
36	# shellcheck disable=SC2016
37	awk='
38		BEGIN {
39			# see ./gen-platforms.sh
40			platform["aarch64"]	= "uchar lp64  long ldbl128"
41			platform["alpha"]	= "schar lp64  long ldbl64"
42			platform["arm"]		= "uchar ilp32 long ldbl64"
43			platform["coldfire"]	= "schar ilp32 int  ldbl64"
44			platform["hppa"]	= "schar ilp32 long ldbl64"
45			platform["i386"]	= "schar ilp32 int  ldbl96"
46			platform["ia64"]	= "schar lp64  long ldbl128"
47			platform["m68000"]	= "schar ilp32 int  ldbl64"
48			platform["m68k"]	= "schar ilp32 int  ldbl96"
49			platform["mips"]	= "schar ilp32 ???? ldbl64"
50			platform["mips64"]	= "schar ilp32 long ldbl128"
51			platform["mipsn64"]	= "schar lp64  long ldbl128"
52			platform["or1k"]	= "schar ilp32 int  ldbl64"
53			platform["powerpc"]	= "uchar ilp32 int  ldbl64"
54			platform["powerpc64"]	= "uchar lp64  long ldbl64"
55			platform["riscv32"]	= "schar ilp32 int  ldbl64"
56			platform["riscv64"]	= "schar lp64  long ldbl64"
57			platform["sh3"]		= "schar ilp32 int  ldbl64"
58			platform["sparc"]	= "schar ilp32 long ldbl64"
59			platform["sparc64"]	= "schar lp64  long ldbl128"
60			platform["vax"]		= "schar ilp32 long ldbl64"
61			platform["x86_64"]	= "schar lp64  long ldbl128"
62		}
63
64		function platform_has(prop) {
65			if (!match(prop, /^(schar|uchar|ilp32|lp64|int|long|ldbl64|ldbl96|ldbl128)$/)) {
66				printf("bad property '\''%s'\''\n", prop) > "/dev/stderr"
67				exit(1)
68			}
69			if (platform[archsubdir] == "") {
70				printf("bad archsubdir '\''%s'\''\n", archsubdir) > "/dev/stderr"
71				exit(1)
72			}
73			return match(" " platform[archsubdir] " ", " " prop " ")
74		}
75
76		BEGIN {
77			archsubdir = "'"$archsubdir"'"
78			flags = "-g -S -w"
79			skip = "no"
80		}
81		$1 == "/*" && $2 ~ /^lint1-/ && $NF == "*/" {
82			if ($2 == "lint1-flags:" || $2 == "lint1-extra-flags:") {
83				if ($2 == "lint1-flags:")
84					flags = ""
85				for (i = 3; i < NF; i++)
86					flags = flags " " $i
87			} else if ($2 == "lint1-only-if:") {
88				for (i = 3; i < NF; i++)
89					if (!platform_has($i))
90						skip = "yes"
91			} else {
92				printf("bad lint1 comment '\''%s'\''\n", $2) > "/dev/stderr"
93				exit(1)
94			}
95		}
96
97		END {
98			printf("flags='\''%s'\''\n", flags)
99			printf("skip=%s\n", skip)
100		}
101	'
102
103	local config
104	config="$(awk "$awk" "$1")" || exit 1
105	eval "$config"
106
107	case "_${1%.c}_" in
108	*_utf8_*)
109		LC_ALL=en_US.UTF-8;;
110	*)
111		LC_ALL=C;;
112	esac
113	export LC_ALL
114}
115
116# shellcheck disable=SC2155
117check_lint1()
118{
119	local src="$(atf_get_srcdir)/$1"
120	local exp="${1%.c}.exp"
121	local exp_ln="${src%.c}.exp-ln"
122	local wrk_ln="${1%.c}.ln"
123	local flags=""
124	local skip=""
125
126	if [ ! -f "$exp_ln" ]; then
127		exp_ln='/dev/null'
128		wrk_ln='/dev/null'
129	fi
130
131	configure_test_case "$src"	# sets 'skip' and 'flags'
132
133	if [ "$skip" = "yes" ]; then
134		atf_skip "unsuitable platform"
135	fi
136
137	# shellcheck disable=SC2086
138	atf_check -s 'exit' -o "save:$exp" \
139	    "$lint1" $flags "$src" "$wrk_ln"
140	atf_check lua "$(atf_get_srcdir)/check-expect.lua" "$src"
141
142	if [ "$exp_ln" != '/dev/null' ]; then
143		# Remove comments and whitespace from the .exp-ln file.
144		sed \
145		    -e '/^#/d' \
146		    -e '/^$/d' \
147		    -e 's,^#.*,,' \
148		    -e 's,\([^%]\)[[:space:]],\1,g' \
149		    < "$exp_ln" > "./${exp_ln##*/}"
150
151		atf_check -o "file:${exp_ln##*/}" cat "$wrk_ln"
152	fi
153}
154
155atf_init_test_cases()
156{
157	local src name
158
159	for src in "$(atf_get_srcdir)"/*.c; do
160		src=${src##*/}
161		name=${src%.c}
162
163		atf_test_case "$name"
164		eval "${name}_head() {
165			atf_set 'require.progs' '$lint1'
166		}"
167		eval "${name}_body() {
168			check_lint1 '$name.c'
169		}"
170		atf_add_test_case "$name"
171	done
172}
173