ckbool.c revision 1.15 1 /* $NetBSD: ckbool.c,v 1.15 2022/05/20 21:18:55 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 (at) 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.15 2022/05/20 21:18:55 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 static const char *
54 op_name(op_t op)
55 {
56 return modtab[op].m_name;
57 }
58
59 /*
60 * See if in strict bool mode, the operator takes either two bool operands
61 * or two arbitrary other operands.
62 */
63 static bool
64 is_assignment_bool_or_other(op_t op)
65 {
66 return op == ASSIGN ||
67 op == ANDASS || op == XORASS || op == ORASS ||
68 op == RETURN || op == INIT || op == FARG;
69 }
70
71 static bool
72 is_symmetric_bool_or_other(op_t op)
73 {
74 return op == EQ || op == NE ||
75 op == BITAND || op == BITXOR || op == BITOR ||
76 op == COLON;
77 }
78
79 static bool
80 is_int_constant_zero(const tnode_t *tn, tspec_t t)
81 {
82 return t == INT && tn->tn_op == CON && tn->tn_val->v_quad == 0;
83 }
84
85 static bool
86 is_typeok_strict_bool_binary(op_t op,
87 const tnode_t *ln, tspec_t lt,
88 const tnode_t *rn, tspec_t rt)
89 {
90 if ((lt == BOOL) == (rt == BOOL))
91 return true;
92
93 if (op == FARG && rn->tn_sys)
94 return false;
95
96 if ((ln->tn_sys || rn->tn_sys) &&
97 (is_int_constant_zero(ln, lt) || is_int_constant_zero(rn, rt)))
98 return true;
99
100 if (is_assignment_bool_or_other(op))
101 return lt != BOOL && (ln->tn_sys || rn->tn_sys);
102
103 return !is_symmetric_bool_or_other(op);
104 }
105
106 /*
107 * Some operators require that either both operands are bool or both are
108 * scalar.
109 *
110 * Code that passes this check can be compiled in a pre-C99 environment that
111 * doesn't implement the special rule C99 6.3.1.2, without silent change in
112 * behavior.
113 */
114 static bool
115 typeok_strict_bool_binary_compatible(op_t op, int arg,
116 const tnode_t *ln, tspec_t lt,
117 const tnode_t *rn, tspec_t rt)
118 {
119 if (is_typeok_strict_bool_binary(op, ln, lt, rn, rt))
120 return true;
121
122 if (op == FARG) {
123 /* argument #%d expects '%s', gets passed '%s' */
124 error(334, arg, tspec_name(lt), tspec_name(rt));
125 } else if (op == RETURN) {
126 /* return value type mismatch (%s) and (%s) */
127 error(211, tspec_name(lt), tspec_name(rt));
128 } else {
129 /* operands of '%s' have incompatible types (%s != %s) */
130 error(107, op_name(op), tspec_name(lt), tspec_name(rt));
131 }
132
133 return false;
134 }
135
136 /*
137 * In strict bool mode, check whether the types of the operands match the
138 * operator.
139 */
140 bool
141 typeok_scalar_strict_bool(op_t op, const mod_t *mp, int arg,
142 const tnode_t *ln,
143 const tnode_t *rn)
144
145 {
146 tspec_t lt, rt;
147
148 ln = before_conversion(ln);
149 lt = ln->tn_type->t_tspec;
150
151 if (rn != NULL) {
152 rn = before_conversion(rn);
153 rt = rn->tn_type->t_tspec;
154 } else {
155 rt = NOTSPEC;
156 }
157
158 if (rn != NULL &&
159 !typeok_strict_bool_binary_compatible(op, arg, ln, lt, rn, rt))
160 return false;
161
162 if (mp->m_requires_bool) {
163 bool binary = mp->m_binary;
164 bool lbool = is_typeok_bool_operand(ln);
165 bool ok = true;
166
167 if (!binary && !lbool) {
168 /* operand of '%s' must be bool, not '%s' */
169 error(330, op_name(op), tspec_name(lt));
170 ok = false;
171 }
172 if (binary && !lbool) {
173 /* left operand of '%s' must be bool, not '%s' */
174 error(331, op_name(op), tspec_name(lt));
175 ok = false;
176 }
177 if (binary && op != QUEST && !is_typeok_bool_operand(rn)) {
178 /* right operand of '%s' must be bool, not '%s' */
179 error(332, op_name(op), tspec_name(rt));
180 ok = false;
181 }
182 return ok;
183 }
184
185 if (!mp->m_takes_bool) {
186 bool binary = mp->m_binary;
187 bool lbool = lt == BOOL;
188 bool ok = true;
189
190 if (!binary && lbool) {
191 /* operand of '%s' must not be bool */
192 error(335, op_name(op));
193 ok = false;
194 }
195 if (binary && lbool) {
196 /* left operand of '%s' must not be bool */
197 error(336, op_name(op));
198 ok = false;
199 }
200 if (binary && rt == BOOL) {
201 /* right operand of '%s' must not be bool */
202 error(337, op_name(op));
203 ok = false;
204 }
205 return ok;
206 }
207
208 return true;
209 }
210
211 /*
212 * See if the node is valid as operand of an operator that compares its
213 * argument with 0.
214 */
215 bool
216 is_typeok_bool_operand(const tnode_t *tn)
217 {
218 tspec_t t;
219
220 lint_assert(Tflag);
221
222 while (tn->tn_op == COMMA)
223 tn = tn->tn_right;
224 tn = before_conversion(tn);
225 t = tn->tn_type->t_tspec;
226
227 if (t == BOOL)
228 return true;
229
230 if (tn->tn_sys && is_scalar(t))
231 return true;
232
233 /* For enums that are used as bit sets, allow "flags & FLAG". */
234 if (tn->tn_op == BITAND &&
235 tn->tn_left->tn_op == CVT &&
236 tn->tn_left->tn_type->t_is_enum &&
237 tn->tn_right->tn_type->t_is_enum)
238 return true;
239
240 return false;
241 }
242
243 bool
244 fallback_symbol_strict_bool(sym_t *sym)
245 {
246 if (Tflag && strcmp(sym->s_name, "__lint_false") == 0) {
247 sym->s_scl = BOOL_CONST;
248 sym->s_type = gettyp(BOOL);
249 sym->u.s_bool_constant = false;
250 return true;
251 }
252
253 if (Tflag && strcmp(sym->s_name, "__lint_true") == 0) {
254 sym->s_scl = BOOL_CONST;
255 sym->s_type = gettyp(BOOL);
256 sym->u.s_bool_constant = true;
257 return true;
258 }
259
260 return false;
261 }
262