xref: /netbsd-src/usr.bin/xlint/lint1/ckbool.c (revision d16b7486a53dcb8072b60ec6fcb4373a2d0c27b7)
1 /* $NetBSD: ckbool.c,v 1.26 2023/08/02 18:51:25 rillig Exp $ */
2 
3 /*-
4  * Copyright (c) 2021 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Roland Illig <rillig@NetBSD.org>.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #if HAVE_NBTOOL_CONFIG_H
33 #include "nbtool_config.h"
34 #endif
35 
36 #include <sys/cdefs.h>
37 
38 #if defined(__RCSID)
39 __RCSID("$NetBSD: ckbool.c,v 1.26 2023/08/02 18:51:25 rillig Exp $");
40 #endif
41 
42 #include <string.h>
43 
44 #include "lint1.h"
45 
46 
47 /*
48  * The option -T treats _Bool as incompatible with all other scalar types.
49  * See d_c99_bool_strict.c for the exact rules and for examples.
50  */
51 
52 
53 /*
54  * See if in strict bool mode, the operator takes either two bool operands
55  * or two arbitrary other operands.
56  */
57 static bool
58 is_assignment_bool_or_other(op_t op)
59 {
60 	return op == ASSIGN ||
61 	       op == ANDASS || op == XORASS || op == ORASS ||
62 	       op == RETURN || op == INIT || op == FARG;
63 }
64 
65 static bool
66 is_symmetric_bool_or_other(op_t op)
67 {
68 	return op == EQ || op == NE ||
69 	       op == BITAND || op == BITXOR || op == BITOR ||
70 	       op == COLON;
71 }
72 
73 static bool
74 is_int_constant_zero(const tnode_t *tn, tspec_t t)
75 {
76 	return t == INT && tn->tn_op == CON && tn->tn_val.u.integer == 0;
77 }
78 
79 static bool
80 is_typeok_strict_bool_binary(op_t op,
81 			     const tnode_t *ln, tspec_t lt,
82 			     const tnode_t *rn, tspec_t rt)
83 {
84 	if ((lt == BOOL) == (rt == BOOL))
85 		return true;
86 
87 	if (op == FARG && rn->tn_sys)
88 		return false;
89 
90 	if ((ln->tn_sys || rn->tn_sys) &&
91 	    (is_int_constant_zero(ln, lt) || is_int_constant_zero(rn, rt)))
92 		return true;
93 
94 	if (is_assignment_bool_or_other(op))
95 		return lt != BOOL && (ln->tn_sys || rn->tn_sys);
96 
97 	return !is_symmetric_bool_or_other(op);
98 }
99 
100 /*
101  * Some operators require that either both operands are bool or both are
102  * scalar.
103  *
104  * Code that passes this check can be compiled in a pre-C99 environment that
105  * doesn't implement the special rule C99 6.3.1.2, without silent change in
106  * behavior.
107  */
108 static bool
109 typeok_strict_bool_binary_compatible(op_t op, int arg,
110 				     const tnode_t *ln, tspec_t lt,
111 				     const tnode_t *rn, tspec_t rt)
112 {
113 	if (is_typeok_strict_bool_binary(op, ln, lt, rn, rt))
114 		return true;
115 
116 	if (op == FARG) {
117 		/* parameter %d expects '%s', gets passed '%s' */
118 		error(334, arg, tspec_name(lt), tspec_name(rt));
119 	} else if (op == RETURN) {
120 		/* function has return type '%s' but returns '%s' */
121 		error(211, tspec_name(lt), tspec_name(rt));
122 	} else {
123 		/* operands of '%s' have incompatible types '%s' and '%s' */
124 		error(107, op_name(op), tspec_name(lt), tspec_name(rt));
125 	}
126 
127 	return false;
128 }
129 
130 /*
131  * In strict bool mode, check whether the types of the operands match the
132  * operator.
133  */
134 bool
135 typeok_scalar_strict_bool(op_t op, const mod_t *mp, int arg,
136 			  const tnode_t *ln,
137 			  const tnode_t *rn)
138 {
139 	tspec_t lt, rt;
140 
141 	ln = before_conversion(ln);
142 	lt = ln->tn_type->t_tspec;
143 
144 	if (rn != NULL) {
145 		rn = before_conversion(rn);
146 		rt = rn->tn_type->t_tspec;
147 	} else {
148 		rt = NO_TSPEC;
149 	}
150 
151 	if (rn != NULL &&
152 	    !typeok_strict_bool_binary_compatible(op, arg, ln, lt, rn, rt))
153 		return false;
154 
155 	if (mp->m_compares_with_zero) {
156 		bool binary = mp->m_binary;
157 		bool lbool = is_typeok_bool_compares_with_zero(ln);
158 		bool ok = true;
159 
160 		if (!binary && !lbool) {
161 			/* operand of '%s' must be bool, not '%s' */
162 			error(330, op_name(op), tspec_name(lt));
163 			ok = false;
164 		}
165 		if (binary && !lbool) {
166 			/* left operand of '%s' must be bool, not '%s' */
167 			error(331, op_name(op), tspec_name(lt));
168 			ok = false;
169 		}
170 		if (binary && op != QUEST &&
171 		    !is_typeok_bool_compares_with_zero(rn)) {
172 			/* right operand of '%s' must be bool, not '%s' */
173 			error(332, op_name(op), tspec_name(rt));
174 			ok = false;
175 		}
176 		return ok;
177 	}
178 
179 	if (!mp->m_takes_bool) {
180 		bool binary = mp->m_binary;
181 		bool lbool = lt == BOOL;
182 		bool ok = true;
183 
184 		if (!binary && lbool) {
185 			/* operand of '%s' must not be bool */
186 			error(335, op_name(op));
187 			ok = false;
188 		}
189 		if (binary && lbool) {
190 			/* left operand of '%s' must not be bool */
191 			error(336, op_name(op));
192 			ok = false;
193 		}
194 		if (binary && rt == BOOL) {
195 			/* right operand of '%s' must not be bool */
196 			error(337, op_name(op));
197 			ok = false;
198 		}
199 		return ok;
200 	}
201 
202 	return true;
203 }
204 
205 /*
206  * See if the node is valid as operand of an operator that compares its
207  * operand with 0.
208  */
209 bool
210 is_typeok_bool_compares_with_zero(const tnode_t *tn)
211 {
212 	tspec_t t;
213 
214 	while (tn->tn_op == COMMA)
215 		tn = tn->tn_right;
216 	tn = before_conversion(tn);
217 	t = tn->tn_type->t_tspec;
218 
219 	if (t == BOOL)
220 		return true;
221 
222 	if (tn->tn_sys && is_scalar(t))
223 		return true;
224 
225 	/* For enums that are used as bit sets, allow "flags & FLAG". */
226 	if (tn->tn_op == BITAND &&
227 	    tn->tn_left->tn_op == CVT &&
228 	    tn->tn_left->tn_type->t_is_enum &&
229 	    tn->tn_right->tn_type->t_is_enum)
230 		return true;
231 
232 	return false;
233 }
234 
235 bool
236 fallback_symbol_strict_bool(sym_t *sym)
237 {
238 	if (strcmp(sym->s_name, "__lint_false") == 0) {
239 		sym->s_scl = BOOL_CONST;
240 		sym->s_type = gettyp(BOOL);
241 		sym->u.s_bool_constant = false;
242 		return true;
243 	}
244 
245 	if (strcmp(sym->s_name, "__lint_true") == 0) {
246 		sym->s_scl = BOOL_CONST;
247 		sym->s_type = gettyp(BOOL);
248 		sym->u.s_bool_constant = true;
249 		return true;
250 	}
251 
252 	return false;
253 }
254