t_xlint.sh revision 1.5
1# $NetBSD: t_xlint.sh,v 1.5 2024/11/30 18:17:12 rillig Exp $
2#
3# Copyright (c) 2023 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: "${lint:=/usr/bin/lint}"
29
30atf_test_case 'run_lint1_error'
31run_lint1_error_body()
32{
33	cat <<-EOF >input.c || atf_fail 'prepare input.c'
34		#include <stdbool.h>
35
36		bool
37		return_true(void)
38		{
39			return 1;
40		}
41	EOF
42	echo 'previous content' > input.ln
43
44	cat <<-EOF > expected.out
45		input.c(5): warning: missing header declaration for 'return_true' [351]
46		input.c(6): error: function has return type '_Bool' but returns 'int' [211]
47	EOF
48
49	atf_check \
50	    -s 'exit:1' \
51	    -o 'file:expected.out' \
52	    "$lint" -aabceghiprSTxz input.c
53
54	# In case of an error, any previous output file is overwritten, and the
55	# (possibly unfinished) output file is removed.
56	atf_check \
57	    -s 'exit:1' \
58	    test -f input.ln
59}
60
61
62atf_test_case 'run_lint1_warning'
63run_lint1_warning_body()
64{
65	cat <<-EOF >input.c || atf_fail 'prepare input.c'
66		static int number;
67
68		const void *
69		function(int a, const char *s)
70		{
71			return s + a;
72		}
73	EOF
74	cat <<-EOF >input.exp || atf_fail 'prepare input.exp'
75		0sinput.c
76		Sinput.c
77		1s<built-in>
78		2s<command-line>
79		4d0.4dr8functionF2IPcCPcV
80	EOF
81	cat <<-EOF >expected.out
82		input.c(5): warning: missing header declaration for 'function' [351]
83		input.c(1): warning: static variable 'number' unused [226]
84	EOF
85
86	atf_check \
87	    -o 'file:expected.out' \
88	    "$lint" -aabceghiprSTxz input.c
89	atf_check \
90	    -o 'file:input.exp' \
91	    cat input.ln
92}
93
94
95atf_test_case 'run_lint2'
96run_lint2_body()
97{
98	cat <<-EOF >input.ln || atf_fail 'prepare input.ln'
99		0sinput.c
100		Sinput.c
101		1s<built-in>
102		2s<command-line>
103		4d0.4dr8functionF2IPcCPcV
104	EOF
105
106	# Most of the command line options are not relevant for lint2,
107	# so they are effectively ignored.  The option '-i' is absent.
108	#
109	# Depending on whether the lint libraries are installed or not, there
110	# may be a warning 'cannot find llib-lc.ln' on stderr.
111	atf_check \
112	    -o 'inline:function is defined in input.c(4) but never used [lint2:001]\n' \
113	    -e 'ignore' \
114	    "$lint" -aabceghprSTxz input.ln
115}
116
117
118atf_init_test_cases()
119{
120	atf_add_test_case 'run_lint1_error'
121	atf_add_test_case 'run_lint1_warning'
122	atf_add_test_case 'run_lint2'
123}
124