]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
c: Diagnose compound literal for empty array [PR114266]
authorJoseph Myers <josmyers@redhat.com>
Wed, 20 Nov 2024 21:29:48 +0000 (21:29 +0000)
committerJoseph Myers <josmyers@redhat.com>
Wed, 20 Nov 2024 21:29:48 +0000 (21:29 +0000)
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.

gcc/c/c-decl.cc
gcc/testsuite/gcc.dg/c23-empty-init-4.c [new file with mode: 0644]

index 96bfe9290fd9b501886e8936e64063e42078c6c8..c58ff4ab2488c0f37daf4b3521e243bd157b29f8 100644 (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;
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 (file)
index 0000000..491343c
--- /dev/null
@@ -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" } */
+}