Home | History | Annotate | Line # | Download | only in lint1
ckbool.c revision 1.29
      1 /* $NetBSD: ckbool.c,v 1.29 2024/02/03 12:57:12 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.29 2024/02/03 12:57:12 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 detailed 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 	return false;
    127 }
    128 
    129 /*
    130  * In strict bool mode, check whether the types of the operands match the
    131  * operator.
    132  */
    133 bool
    134 typeok_scalar_strict_bool(op_t op, const mod_t *mp, int arg,
    135 			  const tnode_t *ln,
    136 			  const tnode_t *rn)
    137 {
    138 	ln = before_conversion(ln);
    139 	tspec_t lt = ln->tn_type->t_tspec;
    140 	tspec_t rt = NO_TSPEC;
    141 	if (rn != NULL) {
    142 		rn = before_conversion(rn);
    143 		rt = rn->tn_type->t_tspec;
    144 	}
    145 
    146 	if (rn != NULL &&
    147 	    !typeok_strict_bool_binary_compatible(op, arg, ln, lt, rn, rt))
    148 		return false;
    149 
    150 	if (mp->m_compares_with_zero) {
    151 		bool binary = mp->m_binary;
    152 		bool lbool = is_typeok_bool_compares_with_zero(ln);
    153 		bool ok = true;
    154 
    155 		if (!binary && !lbool) {
    156 			/* operand of '%s' must be bool, not '%s' */
    157 			error(330, op_name(op), tspec_name(lt));
    158 			ok = false;
    159 		}
    160 		if (binary && !lbool) {
    161 			/* left operand of '%s' must be bool, not '%s' */
    162 			error(331, op_name(op), tspec_name(lt));
    163 			ok = false;
    164 		}
    165 		if (binary && op != QUEST &&
    166 		    !is_typeok_bool_compares_with_zero(rn)) {
    167 			/* right operand of '%s' must be bool, not '%s' */
    168 			error(332, op_name(op), tspec_name(rt));
    169 			ok = false;
    170 		}
    171 		return ok;
    172 	}
    173 
    174 	if (!mp->m_takes_bool) {
    175 		bool binary = mp->m_binary;
    176 		bool lbool = lt == BOOL;
    177 		bool ok = true;
    178 
    179 		if (!binary && lbool) {
    180 			/* operand of '%s' must not be bool */
    181 			error(335, op_name(op));
    182 			ok = false;
    183 		}
    184 		if (binary && lbool) {
    185 			/* left operand of '%s' must not be bool */
    186 			error(336, op_name(op));
    187 			ok = false;
    188 		}
    189 		if (binary && rt == BOOL) {
    190 			/* right operand of '%s' must not be bool */
    191 			error(337, op_name(op));
    192 			ok = false;
    193 		}
    194 		return ok;
    195 	}
    196 
    197 	return true;
    198 }
    199 
    200 bool
    201 is_typeok_bool_compares_with_zero(const tnode_t *tn)
    202 {
    203 	while (tn->tn_op == COMMA)
    204 		tn = tn->tn_right;
    205 	tn = before_conversion(tn);
    206 
    207 	return tn->tn_type->t_tspec == BOOL
    208 	    || tn->tn_op == BITAND
    209 	    || (tn->tn_sys && is_scalar(tn->tn_type->t_tspec));
    210 }
    211 
    212 bool
    213 fallback_symbol_strict_bool(sym_t *sym)
    214 {
    215 	if (strcmp(sym->s_name, "__lint_false") == 0) {
    216 		sym->s_scl = BOOL_CONST;
    217 		sym->s_type = gettyp(BOOL);
    218 		sym->u.s_bool_constant = false;
    219 		return true;
    220 	}
    221 
    222 	if (strcmp(sym->s_name, "__lint_true") == 0) {
    223 		sym->s_scl = BOOL_CONST;
    224 		sym->s_type = gettyp(BOOL);
    225 		sym->u.s_bool_constant = true;
    226 		return true;
    227 	}
    228 
    229 	return false;
    230 }
    231