xref: /netbsd-src/tests/usr.bin/xlint/lint1/t_integration.sh (revision ecb2cf6da722715f61ec55105a651b69a965f0e4)
1# $NetBSD: t_integration.sh,v 1.85 2025/01/03 02:14:52 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
31srcdir="$(atf_get_srcdir)"
32
33configure_test_case()
34{
35	local awk
36
37	# shellcheck disable=SC2016
38	awk='
39		BEGIN {
40			# see ./gen-platforms.sh
41			platform["aarch64"]	= "uchar lp64  long ldbl128"
42			platform["alpha"]	= "schar lp64  long ldbl64"
43			platform["arm"]		= "uchar ilp32 long ldbl64"
44			platform["coldfire"]	= "schar ilp32 int  ldbl64"
45			platform["hppa"]	= "schar ilp32 long ldbl64"
46			platform["i386"]	= "schar ilp32 int  ldbl96"
47			platform["ia64"]	= "schar lp64  long ldbl128"
48			platform["m68000"]	= "schar ilp32 int  ldbl64"
49			platform["m68k"]	= "schar ilp32 int  ldbl96"
50			platform["mips"]	= "schar ilp32 ???? ldbl64"
51			platform["mips64"]	= "schar ilp32 long ldbl128"
52			platform["mipsn64"]	= "schar lp64  long ldbl128"
53			platform["or1k"]	= "schar ilp32 int  ldbl64"
54			platform["powerpc"]	= "uchar ilp32 int  ldbl64"
55			platform["powerpc64"]	= "uchar lp64  long ldbl64"
56			platform["riscv32"]	= "schar ilp32 int  ldbl64"
57			platform["riscv64"]	= "schar lp64  long ldbl64"
58			platform["sh3"]		= "schar ilp32 int  ldbl64"
59			platform["sparc"]	= "schar ilp32 long ldbl64"
60			platform["sparc64"]	= "schar lp64  long ldbl128"
61			platform["vax"]		= "schar ilp32 long ldbl64"
62			platform["x86_64"]	= "schar lp64  long ldbl128"
63		}
64
65		function platform_has(prop) {
66			if (!match(prop, /^(schar|uchar|ilp32|lp64|int|long|ldbl64|ldbl96|ldbl128)$/)) {
67				printf("bad property '\''%s'\''\n", prop) > "/dev/stderr"
68				exit(1)
69			}
70			if (platform[archsubdir] == "") {
71				printf("bad archsubdir '\''%s'\''\n", archsubdir) > "/dev/stderr"
72				exit(1)
73			}
74			return match(" " platform[archsubdir] " ", " " prop " ")
75		}
76
77		BEGIN {
78			archsubdir = "'"$archsubdir"'"
79			flags = "-g -S -w"
80			skip = "no"
81		}
82		$1 == "/*" && $2 ~ /^lint1-/ && $NF == "*/" {
83			if ($2 == "lint1-flags:" || $2 == "lint1-extra-flags:") {
84				if ($2 == "lint1-flags:")
85					flags = ""
86				for (i = 3; i < NF; i++)
87					flags = flags " " $i
88			} else if ($2 == "lint1-only-if:") {
89				for (i = 3; i < NF; i++)
90					if (!platform_has($i))
91						skip = "yes"
92			} else {
93				printf("bad lint1 comment '\''%s'\''\n", $2) > "/dev/stderr"
94				exit(1)
95			}
96		}
97
98		END {
99			printf("flags='\''%s'\''\n", flags)
100			printf("skip=%s\n", skip)
101		}
102	'
103
104	local config
105	config="$(awk "$awk" "$1")" || exit 1
106	eval "$config"
107
108	case "_${1%.c}_" in
109	*_utf8_*)
110		LC_ALL=en_US.UTF-8;;
111	*)
112		LC_ALL=C;;
113	esac
114	export LC_ALL
115}
116
117tests_done=''
118check_lint1()
119{
120	local src="$1"
121	local base="${src##*/}"
122	local exp="${base%.c}.exp"
123	local exp_ln="${src%.c}.exp-ln"
124	local wrk_ln="${base%.c}.ln"
125	local flags=""
126	local skip=""
127
128	if [ ! -f "$exp_ln" ]; then
129		exp_ln='/dev/null'
130		wrk_ln='/dev/null'
131	fi
132
133	configure_test_case "$src"	# sets 'skip' and 'flags'
134
135	if [ "$skip" = "yes" ]; then
136		return
137	fi
138	tests_done="$tests_done $src"
139
140	# shellcheck disable=SC2086
141	atf_check -s 'exit' -o "save:$exp" \
142	    "$lint1" $flags "$src" "$wrk_ln"
143
144	if [ "$exp_ln" != '/dev/null' ]; then
145		# Remove comments and whitespace from the .exp-ln file.
146		sed \
147		    -e '/^#/d' \
148		    -e '/^$/d' \
149		    -e 's,^#.*,,' \
150		    -e 's,\([^%]\)[[:space:]],\1,g' \
151		    < "$exp_ln" > "./${exp_ln##*/}"
152
153		atf_check -o "file:${exp_ln##*/}" cat "$wrk_ln"
154	fi
155}
156
157atf_test_case lint1
158lint1_head() {
159	atf_set 'require.progs' "$lint1"
160}
161lint1_body() {
162	local src
163
164	for src in "$srcdir"/*.c; do
165		check_lint1 "$src"
166	done
167
168	# shellcheck disable=SC2086
169	atf_check lua "$srcdir/check-expect.lua" $tests_done
170}
171
172atf_init_test_cases()
173{
174	atf_add_test_case lint1
175}
176