From efd21533974e260c6864b8f546cfa5abc55ceefa Mon Sep 17 00:00:00 2001 From: Jakub Jelinek Date: Tue, 28 Jan 2025 09:31:27 +0100 Subject: [PATCH] c: For array element type drop qualifiers but keep other properties of the element type [PR116357] 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 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 | 2 +- gcc/testsuite/gcc.dg/pr116357.c | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) create mode 100644 gcc/testsuite/gcc.dg/pr116357.c diff --git a/gcc/c/c-decl.cc b/gcc/c/c-decl.cc index 68d331b2250..0dcbae9b26f 100644 --- a/gcc/c/c-decl.cc +++ b/gcc/c/c-decl.cc @@ -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 index 00000000000..07effa13254 --- /dev/null +++ b/gcc/testsuite/gcc.dg/pr116357.c @@ -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" } */ -- 2.47.2