Hi!

As the testcase shows, handle_warn_if_not_align ICEs on
fields with types with warn_if_not_align attribute in variable length
structures.

Fixed thusly, bootstrapped/regtested on x86_64-linux and i686-linux, ok for
trunk?

2018-01-15  Jakub Jelinek  <ja...@redhat.com>

        PR c/83844
        * stor-layout.c (handle_warn_if_not_align): Use byte_position and
        multiple_of_p instead of unchecked tree_to_uhwi and UHWI check.

        * gcc.dg/pr83844.c: New test.

--- gcc/stor-layout.c.jj        2018-01-14 17:16:55.590836141 +0100
+++ gcc/stor-layout.c   2018-01-15 10:31:42.403874423 +0100
@@ -1150,11 +1150,9 @@ handle_warn_if_not_align (tree field, un
     warning (opt_w, "alignment %u of %qT is less than %u",
             record_align, context, warn_if_not_align);
 
-  unsigned HOST_WIDE_INT off
-    = (tree_to_uhwi (DECL_FIELD_OFFSET (field))
-       + tree_to_uhwi (DECL_FIELD_BIT_OFFSET (field)) / BITS_PER_UNIT);
-  if ((off % warn_if_not_align) != 0)
-    warning (opt_w, "%q+D offset %wu in %qT isn't aligned to %u",
+  tree off = byte_position (field);
+  if (!multiple_of_p (TREE_TYPE (off), off, size_int (warn_if_not_align)))
+    warning (opt_w, "%q+D offset %E in %qT isn't aligned to %u",
             field, off, context, warn_if_not_align);
 }
 
--- gcc/testsuite/gcc.dg/pr83844.c.jj   2018-01-15 10:36:34.986655853 +0100
+++ gcc/testsuite/gcc.dg/pr83844.c      2018-01-15 10:36:02.120669000 +0100
@@ -0,0 +1,28 @@
+/* PR c/83844 */
+/* { dg-do compile } */
+/* { dg-options "-O0 -Wall" } */
+
+typedef unsigned long long __u64 
__attribute__((aligned(4),warn_if_not_aligned(8)));
+void bar (void *, void *);
+
+void
+foo (int n)
+{
+  struct A
+  {
+    int i1;
+    int i2;
+    int i3[n];
+    __u64 x;   /* { dg-warning "in 'struct A' isn't aligned to 8" } */
+  } __attribute__((aligned (8)));
+  struct B
+  {
+    int i1;
+    int i2;
+    long long i3[n];
+    __u64 x;
+  } __attribute__((aligned (8)));
+  struct A a;
+  struct B b;
+  bar (&a, &b);
+}

        Jakub

Reply via email to