Skip to content

Commit d5cebf7

Browse files
committed
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.
1 parent cf544af commit d5cebf7

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

gcc/c/c-decl.cc

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6514,9 +6514,14 @@ build_compound_literal (location_t loc, tree type, tree init, bool non_const,
65146514
{
65156515
int failure = complete_array_type (&TREE_TYPE (decl),
65166516
DECL_INITIAL (decl), true);
6517-
/* If complete_array_type returns 3, it means that the
6518-
initial value of the compound literal is empty. Allow it. */
6517+
/* If complete_array_type returns 3, it means that the initial value of
6518+
the compound literal is empty. Allow it with a pedwarn; in pre-C23
6519+
modes, the empty initializer itself has been diagnosed if pedantic so
6520+
does not need to be diagnosed again here. */
65196521
gcc_assert (failure == 0 || failure == 3);
6522+
if (failure == 3 && flag_isoc23)
6523+
pedwarn (loc, OPT_Wpedantic,
6524+
"array of unknown size with empty initializer");
65206525

65216526
type = TREE_TYPE (decl);
65226527
TREE_TYPE (DECL_INITIAL (decl)) = type;
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/* Test C23 support for empty initializers: invalid for empty arrays in
2+
compound literals (bug 114266). */
3+
/* { dg-do compile } */
4+
/* { dg-options "-std=c23 -pedantic-errors" } */
5+
6+
void
7+
f ()
8+
{
9+
(int []) { }; /* { dg-error "array of unknown size with empty initializer" } */
10+
}

0 commit comments

Comments
 (0)