c: Diagnose compound literal for empty array [PR114266]

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.
This commit is contained in:
Joseph Myers 2024-11-20 21:29:48 +00:00
parent cf544af03a
commit d5cebf7e44
2 changed files with 17 additions and 2 deletions

View file

@ -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;

View file

@ -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" } */
}