Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/usr.bin/xlint/lint1 lint: don't warn about cast between unio...



details:   https://anonhg.NetBSD.org/src/rev/21084adcf724
branches:  trunk
changeset: 374431:21084adcf724
user:      rillig <rillig%NetBSD.org@localhost>
date:      Sat Apr 22 17:42:29 2023 +0000

description:
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.

diffstat:

 tests/usr.bin/xlint/lint1/msg_247.c |   9 +++------
 usr.bin/xlint/lint1/tree.c          |  15 +++++++++++++--
 2 files changed, 16 insertions(+), 8 deletions(-)

diffs (68 lines):

diff -r 95c135b979cb -r 21084adcf724 tests/usr.bin/xlint/lint1/msg_247.c
--- a/tests/usr.bin/xlint/lint1/msg_247.c       Sat Apr 22 17:30:35 2023 +0000
+++ b/tests/usr.bin/xlint/lint1/msg_247.c       Sat Apr 22 17:42:29 2023 +0000
@@ -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 -r 95c135b979cb -r 21084adcf724 usr.bin/xlint/lint1/tree.c
--- a/usr.bin/xlint/lint1/tree.c        Sat Apr 22 17:30:35 2023 +0000
+++ b/usr.bin/xlint/lint1/tree.c        Sat Apr 22 17:42:29 2023 +0000
@@ -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 <sys/cdefs.h>
 #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 <float.h>
@@ -3491,6 +3491,17 @@ should_warn_about_pointer_cast(const typ
                        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;
 



Home | Main Index | Thread Index | Old Index