As reported in bug 114266, GCC fails to pedwarn for a compound
literal, whose type is an array of unknown size, initialized with an
empty initializer. This case is disallowed by C23 (which doesn't have
zero-size objects); the case of a named object is diagnosed as
expected, but not that for compound literals. (Before C23, the
pedwarn for empty initializers sufficed.) Add a check for this
specific case with a pedwarn.
Bootstrapped with no regressions for x86_64-pc-linux-gnu.
PR c/114266
gcc/c/
* c-decl.cc (build_compound_literal): Diagnose array of unknown
size with empty initializer for C23.
gcc/testsuite/
* gcc.dg/c23-empty-init-4.c: New test.
diff --git a/gcc/c/c-decl.cc b/gcc/c/c-decl.cc
index 96bfe9290fd9..c58ff4ab2488 100644
--- a/gcc/c/c-decl.cc
+++ b/gcc/c/c-decl.cc
@@ -6514,9 +6514,14 @@ build_compound_literal (location_t loc, tree type, tree
init, bool non_const,
{
int failure = complete_array_type (&TREE_TYPE (decl),
DECL_INITIAL (decl), true);
- /* If complete_array_type returns 3, it means that the
- initial value of the compound literal is empty. Allow it. */
+ /* If complete_array_type returns 3, it means that the initial value of
+ the compound literal is empty. Allow it with a pedwarn; in pre-C23
+ modes, the empty initializer itself has been diagnosed if pedantic so
+ does not need to be diagnosed again here. */
gcc_assert (failure == 0 || failure == 3);
+ if (failure == 3 && flag_isoc23)
+ pedwarn (loc, OPT_Wpedantic,
+ "array of unknown size with empty initializer");
type = TREE_TYPE (decl);
TREE_TYPE (DECL_INITIAL (decl)) = type;
diff --git a/gcc/testsuite/gcc.dg/c23-empty-init-4.c
b/gcc/testsuite/gcc.dg/c23-empty-init-4.c
new file mode 100644
index 000000000000..491343c053d9
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/c23-empty-init-4.c
@@ -0,0 +1,10 @@
+/* Test C23 support for empty initializers: invalid for empty arrays in
+ compound literals (bug 114266). */
+/* { dg-do compile } */
+/* { dg-options "-std=c23 -pedantic-errors" } */
+
+void
+f ()
+{
+ (int []) { }; /* { dg-error "array of unknown size with empty initializer" }
*/
+}
--
Joseph S. Myers
[email protected]