Hi!

In the following testcase we error on the first case because it is
trying to construct an array from overaligned type, but if there are
qualifiers, we accept it silently (unlike in C++ which diagnoses all 3).

The problem is that grokdeclarator if TYPE_QUALS (element_type) is
non-zero just uses TYPE_MAIN_VARIANT; that loses not just the qualifiers
but also attributes, alignment etc.

The following patch uses build_qualified_type with TYPE_UNQUALIFIED instead,
which will be in the common case the same as TYPE_MAIN_VARIANT if the
checks are satisfied for it, but if not, will look up different unqualified
type or even create it if there is none.

Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk?

2025-01-25  Jakub Jelinek  <ja...@redhat.com>

        PR c/116357
        * c-decl.cc (grokdeclarator): Use build_qualified_type with
        TYPE_UNQUALIFIED instead of TYPE_MAIN_VARIANT.

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

--- gcc/c/c-decl.cc.jj  2025-01-21 09:13:54.523510241 +0100
+++ gcc/c/c-decl.cc     2025-01-24 15:31:46.698725165 +0100
@@ -7058,7 +7058,7 @@ grokdeclarator (const struct c_declarato
       && TYPE_QUALS (element_type))
     {
       orig_qual_type = type;
-      type = TYPE_MAIN_VARIANT (type);
+      type = build_qualified_type (type, TYPE_UNQUALIFIED);
     }
   type_quals = ((constp ? TYPE_QUAL_CONST : 0)
                | (restrictp ? TYPE_QUAL_RESTRICT : 0)
--- gcc/testsuite/gcc.dg/pr116357.c.jj  2025-01-24 16:20:48.011687335 +0100
+++ gcc/testsuite/gcc.dg/pr116357.c     2025-01-24 16:25:49.490532761 +0100
@@ -0,0 +1,10 @@
+/* PR c/116357 */
+/* { dg-do compile } */
+/* { dg-options "" } */
+
+typedef int A __attribute__((aligned (2 * alignof (int))));
+A a[4];        /* { dg-error "alignment of array elements is greater than 
element size" } */
+typedef volatile int B __attribute__((aligned (2 * alignof (int))));
+B b[4];        /* { dg-error "alignment of array elements is greater than 
element size" } */
+typedef const int C __attribute__((aligned (2 * alignof (int))));
+C c[4];        /* { dg-error "alignment of array elements is greater than 
element size" } */

        Jakub

Reply via email to