Home | History | Annotate | Line # | Download | only in lint1
      1  1.32  rillig /* $NetBSD: ckbool.c,v 1.32 2024/05/12 12:32:39 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.15  rillig #if defined(__RCSID)
     39  1.32  rillig __RCSID("$NetBSD: ckbool.c,v 1.32 2024/05/12 12:32:39 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.29  rillig  * See d_c99_bool_strict.c for the detailed rules and for examples.
     50   1.1  rillig  */
     51   1.1  rillig 
     52  1.10  rillig 
     53   1.1  rillig static bool
     54   1.1  rillig is_int_constant_zero(const tnode_t *tn, tspec_t t)
     55   1.1  rillig {
     56  1.30  rillig 	return t == INT && tn->tn_op == CON && tn->u.value.u.integer == 0;
     57   1.1  rillig }
     58   1.1  rillig 
     59   1.1  rillig static bool
     60   1.5  rillig is_typeok_strict_bool_binary(op_t op,
     61   1.5  rillig 			     const tnode_t *ln, tspec_t lt,
     62   1.5  rillig 			     const tnode_t *rn, tspec_t rt)
     63   1.1  rillig {
     64   1.1  rillig 	if ((lt == BOOL) == (rt == BOOL))
     65   1.1  rillig 		return true;
     66   1.1  rillig 
     67  1.14  rillig 	if (op == FARG && rn->tn_sys)
     68  1.14  rillig 		return false;
     69  1.14  rillig 
     70   1.9  rillig 	if ((ln->tn_sys || rn->tn_sys) &&
     71   1.1  rillig 	    (is_int_constant_zero(ln, lt) || is_int_constant_zero(rn, rt)))
     72   1.1  rillig 		return true;
     73   1.1  rillig 
     74  1.32  rillig 	if (op == ASSIGN || op == ANDASS || op == XORASS || op == ORASS ||
     75  1.32  rillig 	    op == RETURN || op == INIT || op == FARG)
     76   1.9  rillig 		return lt != BOOL && (ln->tn_sys || rn->tn_sys);
     77   1.1  rillig 
     78  1.32  rillig 	if (op == EQ || op == NE ||
     79  1.32  rillig 	    op == BITAND || op == BITXOR || op == BITOR ||
     80  1.32  rillig 	    op == COLON)
     81  1.32  rillig 		return false;
     82  1.32  rillig 
     83  1.32  rillig 	return true;
     84   1.1  rillig }
     85   1.1  rillig 
     86   1.1  rillig /*
     87   1.1  rillig  * Some operators require that either both operands are bool or both are
     88   1.1  rillig  * scalar.
     89   1.1  rillig  *
     90   1.1  rillig  * Code that passes this check can be compiled in a pre-C99 environment that
     91   1.1  rillig  * doesn't implement the special rule C99 6.3.1.2, without silent change in
     92   1.1  rillig  * behavior.
     93   1.1  rillig  */
     94   1.1  rillig static bool
     95   1.5  rillig typeok_strict_bool_binary_compatible(op_t op, int arg,
     96   1.5  rillig 				     const tnode_t *ln, tspec_t lt,
     97   1.5  rillig 				     const tnode_t *rn, tspec_t rt)
     98   1.1  rillig {
     99   1.5  rillig 	if (is_typeok_strict_bool_binary(op, ln, lt, rn, rt))
    100   1.1  rillig 		return true;
    101   1.1  rillig 
    102  1.29  rillig 	if (op == FARG)
    103  1.26  rillig 		/* parameter %d expects '%s', gets passed '%s' */
    104   1.1  rillig 		error(334, arg, tspec_name(lt), tspec_name(rt));
    105  1.29  rillig 	else if (op == RETURN)
    106  1.19  rillig 		/* function has return type '%s' but returns '%s' */
    107   1.1  rillig 		error(211, tspec_name(lt), tspec_name(rt));
    108  1.29  rillig 	else
    109  1.18  rillig 		/* operands of '%s' have incompatible types '%s' and '%s' */
    110   1.1  rillig 		error(107, op_name(op), tspec_name(lt), tspec_name(rt));
    111   1.1  rillig 
    112   1.1  rillig 	return false;
    113   1.1  rillig }
    114   1.1  rillig 
    115   1.1  rillig /*
    116   1.1  rillig  * In strict bool mode, check whether the types of the operands match the
    117   1.1  rillig  * operator.
    118   1.1  rillig  */
    119   1.1  rillig bool
    120   1.1  rillig typeok_scalar_strict_bool(op_t op, const mod_t *mp, int arg,
    121   1.1  rillig 			  const tnode_t *ln,
    122   1.1  rillig 			  const tnode_t *rn)
    123   1.1  rillig {
    124   1.1  rillig 	ln = before_conversion(ln);
    125  1.29  rillig 	tspec_t lt = ln->tn_type->t_tspec;
    126  1.29  rillig 	tspec_t rt = NO_TSPEC;
    127   1.1  rillig 	if (rn != NULL) {
    128   1.1  rillig 		rn = before_conversion(rn);
    129   1.1  rillig 		rt = rn->tn_type->t_tspec;
    130   1.1  rillig 	}
    131   1.1  rillig 
    132   1.5  rillig 	if (rn != NULL &&
    133   1.5  rillig 	    !typeok_strict_bool_binary_compatible(op, arg, ln, lt, rn, rt))
    134   1.1  rillig 		return false;
    135   1.1  rillig 
    136  1.17  rillig 	if (mp->m_compares_with_zero) {
    137   1.1  rillig 		bool binary = mp->m_binary;
    138  1.31  rillig 		bool lbool = is_typeok_bool_compares_with_zero(ln, false);
    139   1.1  rillig 		bool ok = true;
    140   1.1  rillig 
    141   1.1  rillig 		if (!binary && !lbool) {
    142   1.1  rillig 			/* operand of '%s' must be bool, not '%s' */
    143   1.1  rillig 			error(330, op_name(op), tspec_name(lt));
    144   1.1  rillig 			ok = false;
    145   1.1  rillig 		}
    146   1.1  rillig 		if (binary && !lbool) {
    147   1.1  rillig 			/* left operand of '%s' must be bool, not '%s' */
    148   1.1  rillig 			error(331, op_name(op), tspec_name(lt));
    149   1.1  rillig 			ok = false;
    150   1.1  rillig 		}
    151  1.17  rillig 		if (binary && op != QUEST &&
    152  1.31  rillig 		    !is_typeok_bool_compares_with_zero(rn, false)) {
    153   1.1  rillig 			/* right operand of '%s' must be bool, not '%s' */
    154   1.1  rillig 			error(332, op_name(op), tspec_name(rt));
    155   1.1  rillig 			ok = false;
    156   1.1  rillig 		}
    157   1.1  rillig 		return ok;
    158   1.1  rillig 	}
    159   1.1  rillig 
    160   1.1  rillig 	if (!mp->m_takes_bool) {
    161   1.1  rillig 		bool binary = mp->m_binary;
    162   1.8  rillig 		bool lbool = lt == BOOL;
    163   1.1  rillig 		bool ok = true;
    164   1.1  rillig 
    165   1.1  rillig 		if (!binary && lbool) {
    166   1.1  rillig 			/* operand of '%s' must not be bool */
    167   1.1  rillig 			error(335, op_name(op));
    168   1.1  rillig 			ok = false;
    169   1.1  rillig 		}
    170   1.1  rillig 		if (binary && lbool) {
    171   1.1  rillig 			/* left operand of '%s' must not be bool */
    172   1.1  rillig 			error(336, op_name(op));
    173   1.1  rillig 			ok = false;
    174   1.1  rillig 		}
    175   1.8  rillig 		if (binary && rt == BOOL) {
    176   1.1  rillig 			/* right operand of '%s' must not be bool */
    177   1.1  rillig 			error(337, op_name(op));
    178   1.1  rillig 			ok = false;
    179   1.1  rillig 		}
    180   1.1  rillig 		return ok;
    181   1.1  rillig 	}
    182   1.1  rillig 
    183   1.1  rillig 	return true;
    184   1.1  rillig }
    185   1.1  rillig 
    186   1.1  rillig bool
    187  1.31  rillig is_typeok_bool_compares_with_zero(const tnode_t *tn, bool is_do_while)
    188   1.1  rillig {
    189  1.10  rillig 	while (tn->tn_op == COMMA)
    190  1.30  rillig 		tn = tn->u.ops.right;
    191   1.1  rillig 	tn = before_conversion(tn);
    192   1.1  rillig 
    193  1.29  rillig 	return tn->tn_type->t_tspec == BOOL
    194  1.29  rillig 	    || tn->tn_op == BITAND
    195  1.31  rillig 	    || (is_do_while && is_int_constant_zero(tn, tn->tn_type->t_tspec))
    196  1.29  rillig 	    || (tn->tn_sys && is_scalar(tn->tn_type->t_tspec));
    197   1.1  rillig }
    198   1.1  rillig 
    199   1.1  rillig bool
    200   1.1  rillig fallback_symbol_strict_bool(sym_t *sym)
    201   1.1  rillig {
    202  1.20  rillig 	if (strcmp(sym->s_name, "__lint_false") == 0) {
    203  1.11  rillig 		sym->s_scl = BOOL_CONST;
    204   1.1  rillig 		sym->s_type = gettyp(BOOL);
    205  1.12  rillig 		sym->u.s_bool_constant = false;
    206   1.1  rillig 		return true;
    207   1.1  rillig 	}
    208   1.1  rillig 
    209  1.20  rillig 	if (strcmp(sym->s_name, "__lint_true") == 0) {
    210  1.11  rillig 		sym->s_scl = BOOL_CONST;
    211   1.1  rillig 		sym->s_type = gettyp(BOOL);
    212  1.12  rillig 		sym->u.s_bool_constant = true;
    213   1.1  rillig 		return true;
    214   1.1  rillig 	}
    215   1.1  rillig 
    216   1.1  rillig 	return false;
    217   1.1  rillig }
    218