From d1299c8ce2c409d76c270d7ce9d83f1ba9ebe126 Mon Sep 17 00:00:00 2001 From: rillig Date: Tue, 3 Aug 2021 21:09:26 +0000 Subject: [PATCH] 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. --- tests/usr.bin/xlint/lint1/gcc_cast_union.c | 8 +++-- tests/usr.bin/xlint/lint1/gcc_cast_union.exp | 6 ++-- usr.bin/xlint/lint1/tree.c | 37 +++----------------- 3 files changed, 12 insertions(+), 39 deletions(-) diff --git a/tests/usr.bin/xlint/lint1/gcc_cast_union.c b/tests/usr.bin/xlint/lint1/gcc_cast_union.c index 2ad0fc6c1f36..4fe301f7a1a9 100644 --- a/tests/usr.bin/xlint/lint1/gcc_cast_union.c +++ b/tests/usr.bin/xlint/lint1/gcc_cast_union.c @@ -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] */ diff --git a/tests/usr.bin/xlint/lint1/gcc_cast_union.exp b/tests/usr.bin/xlint/lint1/gcc_cast_union.exp index c26149ffb9ad..34addb9d1cfa 100644 --- a/tests/usr.bin/xlint/lint1/gcc_cast_union.exp +++ b/tests/usr.bin/xlint/lint1/gcc_cast_union.exp @@ -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] diff --git a/usr.bin/xlint/lint1/tree.c b/usr.bin/xlint/lint1/tree.c index 016eaed3ca77..f01c9a22f9d6 100644 --- a/usr.bin/xlint/lint1/tree.c +++ b/usr.bin/xlint/lint1/tree.c @@ -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 #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 @@ -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;