https://gcc.gnu.org/bugzilla/show_bug.cgi?id=88664

--- Comment #5 from H.J. Lu <hjl.tools at gmail dot com> ---
(In reply to Jakub Jelinek from comment #3)
> I'd use just
> struct S { short s; void *p; } __attribute__ ((__packed__));
> 
> int *
> foo (struct S *x)
> {
>   return (int *) (x->p);
> }
> for both languages.
> 
> Despite quite many tests added in the -Waddress-of-packed-member commit, I
> really don't see any testsuite coverage of the
>               warning_at (location, OPT_Waddress_of_packed_member,
>                           "converting a packed %qT pointer (alignment %d) "
>                           "to %qT (alignment %d) may may result in an "
>                           "unaligned pointer value",
>                           rhstype, rhs_align, type, type_align);
> warning (and note the "may may" bug in the wording there), is that covered
> by anything?

This is covered by gcc.dg/pr51628-20.c, gcc.dg/pr51628-21.c and
gcc.dg/pr51628-25.c.

> As for the other path, it doesn't care whether address is taken or not:
>   if (INDIRECT_REF_P (rhs))
>     rhs = TREE_OPERAND (rhs, 0);
>                       
>   if (TREE_CODE (rhs) == ADDR_EXPR)
>     rhs = TREE_OPERAND (rhs, 0);
> while it is significant.  We shouldn't warn if we are reading content from
> the packed structure, but should warn if we are taking address (and for that
> needs to take into account the array to pointer conversions, whether they
> happen before or after this warning is reported).

I am testing:

diff --git a/gcc/c-family/c-warn.c b/gcc/c-family/c-warn.c
index 79b2d8ad449..c937c016889 100644
--- a/gcc/c-family/c-warn.c
+++ b/gcc/c-family/c-warn.c
@@ -2752,9 +2752,15 @@ check_and_warn_address_of_packed_member (tree type, tree
rhs)
 {
   if (TREE_CODE (rhs) != COND_EXPR)
     {
+      if (TREE_CODE (rhs) == NOP_EXPR)
+  rhs = TREE_OPERAND (rhs, 0);
+
       while (TREE_CODE (rhs) == COMPOUND_EXPR)
   rhs = TREE_OPERAND (rhs, 1);

+      if (TREE_CODE (rhs) != ADDR_EXPR)
+  return;
+
       tree context = check_address_of_packed_member (type, rhs);
       if (context)
   {
(

Reply via email to