]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
c: For array element type drop qualifiers but keep other properties of the element...
authorJakub Jelinek <jakub@redhat.com>
Tue, 28 Jan 2025 08:31:27 +0000 (09:31 +0100)
committerJakub Jelinek <jakub@gcc.gnu.org>
Tue, 28 Jan 2025 08:31:27 +0000 (09:31 +0100)
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 c_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.

2025-01-28  Jakub Jelinek  <jakub@redhat.com>

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

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

gcc/c/c-decl.cc
gcc/testsuite/gcc.dg/pr116357.c [new file with mode: 0644]

index 68d331b2250325f1298c53536109dd2910da2b40..0dcbae9b26f573630621cdfa54044dcda581179f 100644 (file)
@@ -7058,7 +7058,7 @@ grokdeclarator (const struct c_declarator *declarator,
       && TYPE_QUALS (element_type))
     {
       orig_qual_type = type;
-      type = TYPE_MAIN_VARIANT (type);
+      type = c_build_qualified_type (type, TYPE_UNQUALIFIED);
     }
   type_quals = ((constp ? TYPE_QUAL_CONST : 0)
                | (restrictp ? TYPE_QUAL_RESTRICT : 0)
diff --git a/gcc/testsuite/gcc.dg/pr116357.c b/gcc/testsuite/gcc.dg/pr116357.c
new file mode 100644 (file)
index 0000000..07effa1
--- /dev/null
@@ -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" } */