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: allow cast from a type to a union ...



details:   https://anonhg.NetBSD.org/src/rev/3b795ecb5349
branches:  trunk
changeset: 374439:3b795ecb5349
user:      rillig <rillig%NetBSD.org@localhost>
date:      Sat Apr 22 20:54:28 2023 +0000

description:
lint: allow cast from a type to a union containing that type

diffstat:

 tests/usr.bin/xlint/lint1/msg_135.c |   4 +---
 usr.bin/xlint/lint1/tree.c          |  26 +++++++++++++++++---------
 2 files changed, 18 insertions(+), 12 deletions(-)

diffs (79 lines):

diff -r 659b3f2023d2 -r 3b795ecb5349 tests/usr.bin/xlint/lint1/msg_135.c
--- a/tests/usr.bin/xlint/lint1/msg_135.c       Sat Apr 22 20:27:09 2023 +0000
+++ b/tests/usr.bin/xlint/lint1/msg_135.c       Sat Apr 22 20:54:28 2023 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: msg_135.c,v 1.13 2023/04/22 19:45:04 rillig Exp $      */
+/*     $NetBSD: msg_135.c,v 1.14 2023/04/22 20:54:28 rillig Exp $      */
 # 3 "msg_135.c"
 
 // Test for message: converting '%s' to '%s' increases alignment from %u to %u [135]
@@ -95,8 +95,6 @@ cast_to_union(void)
                double p_align_8;
        } *both;
 
-       /* TODO: don't warn here. */
-       /* expect+1: warning: converting 'pointer to int' to 'pointer to union both' increases alignment from 4 to 8 [135] */
        both = (union both *)&align_4;
        both = (union both *)&align_8;
        return both->p_align_8;
diff -r 659b3f2023d2 -r 3b795ecb5349 usr.bin/xlint/lint1/tree.c
--- a/usr.bin/xlint/lint1/tree.c        Sat Apr 22 20:27:09 2023 +0000
+++ b/usr.bin/xlint/lint1/tree.c        Sat Apr 22 20:54:28 2023 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: tree.c,v 1.518 2023/04/22 20:17:19 rillig Exp $        */
+/*     $NetBSD: tree.c,v 1.519 2023/04/22 20:54:28 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.518 2023/04/22 20:17:19 rillig Exp $");
+__RCSID("$NetBSD: tree.c,v 1.519 2023/04/22 20:54:28 rillig Exp $");
 #endif
 
 #include <float.h>
@@ -3451,6 +3451,17 @@ is_byte_array(const type_t *tp)
 }
 
 static bool
+union_contains(const type_t *utp, const type_t *mtp)
+{
+       for (const sym_t *mem = utp->t_sou->sou_first_member;
+            mem != NULL; mem = mem->s_next) {
+               if (types_compatible(mem->s_type, mtp, true, false, NULL))
+                       return true;
+       }
+       return false;
+}
+
+static bool
 should_warn_about_pointer_cast(const type_t *nstp, tspec_t nst,
                               const type_t *ostp, tspec_t ost)
 {
@@ -3494,12 +3505,8 @@ should_warn_about_pointer_cast(const typ
        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_sou->sou_first_member;
-                    mem != NULL; mem = mem->s_next) {
-                       if (types_compatible(mem->s_type, other_tp,
-                           true, false, NULL))
-                               return false;
-               }
+               if (union_contains(union_tp, other_tp))
+                       return false;
        }
 
        if (is_struct_or_union(nst) && nstp->t_sou != ostp->t_sou)
@@ -3539,7 +3546,8 @@ convert_pointer_from_pointer(type_t *ntp
 
        if (hflag && alignment_in_bits(nstp) > alignment_in_bits(ostp) &&
            ost != CHAR && ost != UCHAR &&
-           !is_incomplete(ostp)) {
+           !is_incomplete(ostp) &&
+           !(nst == UNION && union_contains(nstp, ostp))) {
                /* converting '%s' to '%s' increases alignment ... */
                warning(135, type_name(otp), type_name(ntp),
                    alignment_in_bits(ostp) / CHAR_SIZE,



Home | Main Index | Thread Index | Old Index