lint: merge almost duplicate code from 'sametype' into 'eqtype'

In 'sametype', the branch for comparing array types was unreachable
since it requires both tspecs to be the same, but t2 underwent the
array-to-pointer conversion.

Previously, lint warned about enum type mismatches, even without -e for
strict enum mode.  Instead, it got the case for 'char *' wrong, which is
now fixed.  Now lint behaves like GCC 10.3.0 in this regard.  The
warning about enum mismatch is useful though, so it may be re-added in a
future commit.
This commit is contained in:
rillig 2021-08-03 21:09:26 +00:00
parent bfc9dbf5d0
commit d1299c8ce2
3 changed files with 12 additions and 39 deletions

View File

@ -1,4 +1,4 @@
/* $NetBSD: gcc_cast_union.c,v 1.1 2021/08/03 20:34:23 rillig Exp $ */
/* $NetBSD: gcc_cast_union.c,v 1.2 2021/08/03 21:09:26 rillig Exp $ */
# 3 "gcc_cast_union.c"
/*
@ -11,6 +11,8 @@
* https://gcc.gnu.org/onlinedocs/gcc/Cast-to-Union.html
*/
/* lint1-extra-flags: -e */
union anything {
_Bool m_bool;
char m_char;
@ -79,9 +81,9 @@ test(void)
any = (union anything)E1;
any = (union anything)E2;
/* GCC allows enum mismatch even with -Wenum-conversion */
/* expect+1: error: type 'enum other_enum' is not a member of 'union anything' [329] */
/* XXX: Lint should warn about enum type mismatch */
any = (union anything)OTHER;
/* GCC strictly complains that 'char *' is not in the union. */
/* expect+1: error: type 'pointer to char' is not a member of 'union anything' [329] */
any = (union anything)"char *";
any = (union anything)(const char *)"char *";
/* expect+1: error: type 'pointer to double' is not a member of 'union anything' [329] */

View File

@ -1,4 +1,4 @@
gcc_cast_union.c(83): error: type 'enum other_enum' is not a member of 'union anything' [329]
gcc_cast_union.c(88): error: type 'pointer to double' is not a member of 'union anything' [329]
gcc_cast_union.c(87): error: type 'pointer to char' is not a member of 'union anything' [329]
gcc_cast_union.c(90): error: type 'pointer to double' is not a member of 'union anything' [329]
gcc_cast_union.c(92): error: type 'pointer to int' is not a member of 'union anything' [329]
gcc_cast_union.c(92): error: type 'pointer to double' is not a member of 'union anything' [329]
gcc_cast_union.c(94): error: type 'pointer to int' is not a member of 'union anything' [329]

View File

@ -1,4 +1,4 @@
/* $NetBSD: tree.c,v 1.329 2021/08/03 20:57:06 rillig Exp $ */
/* $NetBSD: tree.c,v 1.330 2021/08/03 21:09:26 rillig Exp $ */
/*
* Copyright (c) 1994, 1995 Jochen Pohl
@ -37,7 +37,7 @@
#include <sys/cdefs.h>
#if defined(__RCSID) && !defined(lint)
__RCSID("$NetBSD: tree.c,v 1.329 2021/08/03 20:57:06 rillig Exp $");
__RCSID("$NetBSD: tree.c,v 1.330 2021/08/03 21:09:26 rillig Exp $");
#endif
#include <float.h>
@ -102,36 +102,6 @@ op_name(op_t op)
return modtab[op].m_name;
}
static bool
sametype(const type_t *t1, const type_t *t2)
{
/* Maybe this can be merged into 'eqtype'. */
if (t1->t_tspec != t2->t_tspec)
return false;
/* Ignore const/volatile */
switch (t1->t_tspec) {
case ARRAY:
if (t1->t_dim != t2->t_dim)
return false;
/*FALLTHROUGH*/
case PTR:
return sametype(t1->t_subt, t2->t_subt);
case ENUM:
return strcmp(t1->t_enum->en_tag->s_name,
t2->t_enum->en_tag->s_name) == 0;
case STRUCT:
case UNION:
return strcmp(t1->t_str->sou_tag->s_name,
t2->t_str->sou_tag->s_name) == 0;
default:
return true;
}
}
/* Build 'pointer to tp', 'array of tp' or 'function returning tp'. */
type_t *
derive_type(type_t *tp, tspec_t t)
@ -3401,7 +3371,8 @@ cast(tnode_t *tn, type_t *tp)
return NULL;
}
for (m = str->sou_first_member; m != NULL; m = m->s_next) {
if (sametype(m->s_type, tn->tn_type)) {
if (eqtype(m->s_type, tn->tn_type,
false, false, NULL)) {
tn = expr_zalloc_tnode();
tn->tn_op = CVT;
tn->tn_type = tp;