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