From b8471952104263b5570e151e7e1e3d5a135d4fca Mon Sep 17 00:00:00 2001 From: rillig Date: Sat, 22 Apr 2023 17:42:29 +0000 Subject: [PATCH] lint: don't warn about cast between union and one of its member types Seen in src/sbin/newfs_udf/udf_core.c for context.anchors. --- tests/usr.bin/xlint/lint1/msg_247.c | 9 +++------ usr.bin/xlint/lint1/tree.c | 15 +++++++++++++-- 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/tests/usr.bin/xlint/lint1/msg_247.c b/tests/usr.bin/xlint/lint1/msg_247.c index 3988e36b4bb9..f5cc9086ea08 100644 --- a/tests/usr.bin/xlint/lint1/msg_247.c +++ b/tests/usr.bin/xlint/lint1/msg_247.c @@ -1,4 +1,4 @@ -/* $NetBSD: msg_247.c,v 1.28 2023/04/22 17:30:35 rillig Exp $ */ +/* $NetBSD: msg_247.c,v 1.29 2023/04/22 17:42:29 rillig Exp $ */ # 3 "msg_247.c" // Test for message: pointer cast from '%s' to '%s' may be troublesome [247] @@ -367,20 +367,17 @@ conversions_from_and_to_union(void) /* expect+1: warning: illegal combination of 'pointer to union typedef anything' and 'pointer to double', op '=' [124] */ p_anything = p_double; - /* FIXME: OK, since the union 'anything' has a 'double' member. */ - /* expect+1: warning: pointer cast from 'pointer to double' to 'pointer to union typedef anything' may be troublesome [247] */ + /* OK, since the union 'anything' has a 'double' member. */ p_anything = (anything *)p_double; /* expect+1: warning: illegal combination of 'pointer to double' and 'pointer to union typedef anything', op '=' [124] */ p_double = p_anything; - /* FIXME: OK, since the union 'anything' has a 'double' member. */ - /* expect+1: warning: pointer cast from 'pointer to union typedef anything' to 'pointer to double' may be troublesome [247] */ + /* OK, since the union 'anything' has a 'double' member. */ p_double = (double *)p_anything; /* * Casting to an intermediate union does not make casting between two * incompatible types better. */ - /* expect+2: warning: pointer cast from 'pointer to int' to 'pointer to union typedef anything' may be troublesome [247] */ /* expect+1: warning: illegal combination of 'pointer to function(void) returning void' and 'pointer to union typedef anything', op '=' [124] */ p_function = (anything *)p_int; diff --git a/usr.bin/xlint/lint1/tree.c b/usr.bin/xlint/lint1/tree.c index 4b3124870307..597ee4f25f60 100644 --- a/usr.bin/xlint/lint1/tree.c +++ b/usr.bin/xlint/lint1/tree.c @@ -1,4 +1,4 @@ -/* $NetBSD: tree.c,v 1.515 2023/04/22 15:14:37 rillig Exp $ */ +/* $NetBSD: tree.c,v 1.516 2023/04/22 17:42:29 rillig Exp $ */ /* * Copyright (c) 1994, 1995 Jochen Pohl @@ -37,7 +37,7 @@ #include #if defined(__RCSID) -__RCSID("$NetBSD: tree.c,v 1.515 2023/04/22 15:14:37 rillig Exp $"); +__RCSID("$NetBSD: tree.c,v 1.516 2023/04/22 17:42:29 rillig Exp $"); #endif #include @@ -3491,6 +3491,17 @@ should_warn_about_pointer_cast(const type_t *nstp, tspec_t nst, return false; } + if (nst == UNION || ost == UNION) { + const type_t *union_tp = nst == UNION ? nstp : ostp; + const type_t *other_tp = nst == UNION ? ostp : nstp; + for (const sym_t *mem = union_tp->t_str->sou_first_member; + mem != NULL; mem = mem->s_next) { + if (types_compatible(mem->s_type, other_tp, + true, false, NULL)) + return false; + } + } + if (is_struct_or_union(nst) && nstp->t_str != ostp->t_str) return true;