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