ckbool.c revision 1.20 1 /* $NetBSD: ckbool.c,v 1.20 2023/01/21 20:07:01 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.20 2023/01/21 20:07:01 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 /* function has return type '%s' but returns '%s' */
127 error(211, tspec_name(lt), tspec_name(rt));
128 } else {
129 /* operands of '%s' have incompatible types '%s' and '%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 tspec_t lt, rt;
146
147 ln = before_conversion(ln);
148 lt = ln->tn_type->t_tspec;
149
150 if (rn != NULL) {
151 rn = before_conversion(rn);
152 rt = rn->tn_type->t_tspec;
153 } else {
154 rt = NOTSPEC;
155 }
156
157 if (rn != NULL &&
158 !typeok_strict_bool_binary_compatible(op, arg, ln, lt, rn, rt))
159 return false;
160
161 if (mp->m_compares_with_zero) {
162 bool binary = mp->m_binary;
163 bool lbool = is_typeok_bool_compares_with_zero(ln);
164 bool ok = true;
165
166 if (!binary && !lbool) {
167 /* operand of '%s' must be bool, not '%s' */
168 error(330, op_name(op), tspec_name(lt));
169 ok = false;
170 }
171 if (binary && !lbool) {
172 /* left operand of '%s' must be bool, not '%s' */
173 error(331, op_name(op), tspec_name(lt));
174 ok = false;
175 }
176 if (binary && op != QUEST &&
177 !is_typeok_bool_compares_with_zero(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_compares_with_zero(const tnode_t *tn)
217 {
218 tspec_t t;
219
220 while (tn->tn_op == COMMA)
221 tn = tn->tn_right;
222 tn = before_conversion(tn);
223 t = tn->tn_type->t_tspec;
224
225 if (t == BOOL)
226 return true;
227
228 if (tn->tn_sys && is_scalar(t))
229 return true;
230
231 /* For enums that are used as bit sets, allow "flags & FLAG". */
232 if (tn->tn_op == BITAND &&
233 tn->tn_left->tn_op == CVT &&
234 tn->tn_left->tn_type->t_is_enum &&
235 tn->tn_right->tn_type->t_is_enum)
236 return true;
237
238 return false;
239 }
240
241 bool
242 fallback_symbol_strict_bool(sym_t *sym)
243 {
244 if (strcmp(sym->s_name, "__lint_false") == 0) {
245 sym->s_scl = BOOL_CONST;
246 sym->s_type = gettyp(BOOL);
247 sym->u.s_bool_constant = false;
248 return true;
249 }
250
251 if (strcmp(sym->s_name, "__lint_true") == 0) {
252 sym->s_scl = BOOL_CONST;
253 sym->s_type = gettyp(BOOL);
254 sym->u.s_bool_constant = true;
255 return true;
256 }
257
258 return false;
259 }
260