public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* c: ignore initializers for elements of variable-size types [PR93577]
@ 2020-03-05 23:50 Joseph Myers
  2020-03-09 14:58 ` Christophe Lyon
  0 siblings, 1 reply; 11+ messages in thread
From: Joseph Myers @ 2020-03-05 23:50 UTC (permalink / raw)
  To: gcc-patches

Bug 93577, apparently a regression (although it isn't very clear to me
exactly when it was introduced; tests I made with various past
compilers produced inconclusive results, including e.g. ICEs appearing
with 64-bit-host compilers for some versions but not 32-bit-host
compilers for the same versions) is an C front-end tree-checking ICE
processing initializers for structs using the VLA-in-struct extension.
There is an error for such initializers, but other processing that
still takes place for them results in the ICE.

This patch ensures that processing of initializers for variable-size
types stops earlier to avoid the code that results in the ICE (and
ensures it stops earlier for error_mark_node to avoid ICEs in the
check for variable-size types), adjusts the conditions for the "empty
scalar initializer" diagnostic to avoid consequent excess errors in
the case of a bad type name, and adds tests for a few variations on
what such initializers might look like, as well as tests for cases
identified from ICEs seen with an earlier version of this patch.

Bootstrapped with no regressions for x86_64-pc-linux-gnu.  Applied to 
mainline.

gcc/c:
2020-03-05  Joseph Myers  <joseph@codesourcery.com>

	PR c/93577
	* c-typeck.c (pop_init_level): Do not diagnose initializers as
	empty when initialized type is error_mark_node.
	(set_designator, process_init_element): Ignore initializers for
	elements of a variable-size type or of error_mark_node.

gcc/testsuite:
2020-03-05  Joseph Myers  <joseph@codesourcery.com>

	PR c/93577
	* gcc.dg/pr93577-1.c, gcc.dg/pr93577-2.c, gcc.dg/pr93577-3.c,
	gcc.dg/pr93577-4.c, gcc.dg/pr93577-5.c, gcc.dg/pr93577-6.c: New
	tests.
	* gcc.dg/vla-init-1.c: Expect fewer errors about VLA initializer.

diff --git a/gcc/c/c-typeck.c b/gcc/c/c-typeck.c
index 308fcffcfb0..d8025de1996 100644
--- a/gcc/c/c-typeck.c
+++ b/gcc/c/c-typeck.c
@@ -8759,7 +8759,7 @@ pop_init_level (location_t loc, int implicit,
 	 the element, after verifying there is just one.  */
       if (vec_safe_is_empty (constructor_elements))
 	{
-	  if (!constructor_erroneous)
+	  if (!constructor_erroneous && constructor_type != error_mark_node)
 	    error_init (loc, "empty scalar initializer");
 	  ret.value = error_mark_node;
 	}
@@ -8836,8 +8836,8 @@ set_designator (location_t loc, bool array,
   enum tree_code subcode;
 
   /* Don't die if an entire brace-pair level is superfluous
-     in the containing level.  */
-  if (constructor_type == NULL_TREE)
+     in the containing level, or for an erroneous type.  */
+  if (constructor_type == NULL_TREE || constructor_type == error_mark_node)
     return true;
 
   /* If there were errors in this designator list already, bail out
@@ -8845,6 +8845,12 @@ set_designator (location_t loc, bool array,
   if (designator_erroneous)
     return true;
 
+  /* Likewise for an initializer for a variable-size type.  Those are
+     diagnosed in digest_init.  */
+  if (COMPLETE_TYPE_P (constructor_type)
+      && TREE_CODE (TYPE_SIZE (constructor_type)) != INTEGER_CST)
+    return true;
+
   if (!designator_depth)
     {
       gcc_assert (!constructor_range_stack);
@@ -9955,8 +9961,14 @@ process_init_element (location_t loc, struct c_expr value, bool implicit,
     }
 
   /* Ignore elements of a brace group if it is entirely superfluous
-     and has already been diagnosed.  */
-  if (constructor_type == NULL_TREE)
+     and has already been diagnosed, or if the type is erroneous.  */
+  if (constructor_type == NULL_TREE || constructor_type == error_mark_node)
+    return;
+
+  /* Ignore elements of an initializer for a variable-size type.
+     Those are diagnosed in digest_init.  */
+  if (COMPLETE_TYPE_P (constructor_type)
+      && TREE_CODE (TYPE_SIZE (constructor_type)) != INTEGER_CST)
     return;
 
   if (!implicit && warn_designated_init && !was_designated
diff --git a/gcc/testsuite/gcc.dg/pr93577-1.c b/gcc/testsuite/gcc.dg/pr93577-1.c
new file mode 100644
index 00000000000..31023d79d99
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/pr93577-1.c
@@ -0,0 +1,16 @@
+/* Test ICE with variable-size struct initializer: bug 93577.  */
+/* { dg-do compile } */
+/* { dg-options "" } */
+
+void
+f (int c)
+{
+  struct s
+  {
+    int x[c];
+    struct
+    {
+      int z;
+    } nest;
+  } v = { 1, 2 }; /* { dg-error "variable-sized object may not be initialized" } */
+}
diff --git a/gcc/testsuite/gcc.dg/pr93577-2.c b/gcc/testsuite/gcc.dg/pr93577-2.c
new file mode 100644
index 00000000000..c61589ea670
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/pr93577-2.c
@@ -0,0 +1,16 @@
+/* Test ICE with variable-size struct initializer: bug 93577.  */
+/* { dg-do compile } */
+/* { dg-options "" } */
+
+void
+f (int c)
+{
+  struct s
+  {
+    int x[c];
+    struct
+    {
+      int a, b;
+    } nest;
+  } v = { .nest.b = 1, .nest.a = 2 }; /* { dg-error "variable-sized object may not be initialized" } */
+}
diff --git a/gcc/testsuite/gcc.dg/pr93577-3.c b/gcc/testsuite/gcc.dg/pr93577-3.c
new file mode 100644
index 00000000000..278146b16bd
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/pr93577-3.c
@@ -0,0 +1,17 @@
+/* Test ICE with variable-size struct initializer: bug 93577.  */
+/* { dg-do compile } */
+/* { dg-options "" } */
+
+void
+f (int c)
+{
+  struct s
+  {
+    int a;
+    int x[c];
+    struct
+    {
+      int a, b;
+    } nest;
+  } v = { .a = 2, .nest.b = 1 }; /* { dg-error "variable-sized object may not be initialized" } */
+}
diff --git a/gcc/testsuite/gcc.dg/pr93577-4.c b/gcc/testsuite/gcc.dg/pr93577-4.c
new file mode 100644
index 00000000000..0ac117c4d77
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/pr93577-4.c
@@ -0,0 +1,17 @@
+/* Test ICE with variable-size struct initializer: bug 93577.  */
+/* { dg-do compile } */
+/* { dg-options "" } */
+
+void
+f (int c)
+{
+  struct s
+  {
+    int a;
+    int x[c];
+    struct
+    {
+      int a, b;
+    } nest;
+  } v[2] = { [1].nest.b = 1 }; /* { dg-error "variable-sized object may not be initialized" } */
+}
diff --git a/gcc/testsuite/gcc.dg/pr93577-5.c b/gcc/testsuite/gcc.dg/pr93577-5.c
new file mode 100644
index 00000000000..68dfc1faaaa
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/pr93577-5.c
@@ -0,0 +1,11 @@
+/* Test ICE with designated initializer in compound literal with bad
+   type name (ICE seen with early version of fix for bug 93577 but not
+   covered in other tests).  */
+/* { dg-do compile } */
+/* { dg-options "" } */
+
+void *
+f (void)
+{
+  return &(const bad_type) { .a = 0 }; /* { dg-error "unknown type name" } */
+}
diff --git a/gcc/testsuite/gcc.dg/pr93577-6.c b/gcc/testsuite/gcc.dg/pr93577-6.c
new file mode 100644
index 00000000000..5ec668fd4df
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/pr93577-6.c
@@ -0,0 +1,11 @@
+/* Test ICE with designated initializer in compound literal with bad
+   type name (ICE seen with early version of fix for bug 93577 but not
+   covered in other tests).  */
+/* { dg-do compile } */
+/* { dg-options "" } */
+
+void *
+f (void)
+{
+  return &(const bad_type) { [0] = 0 }; /* { dg-error "unknown type name" } */
+}
diff --git a/gcc/testsuite/gcc.dg/vla-init-1.c b/gcc/testsuite/gcc.dg/vla-init-1.c
index 38e9b011b9c..3125b703451 100644
--- a/gcc/testsuite/gcc.dg/vla-init-1.c
+++ b/gcc/testsuite/gcc.dg/vla-init-1.c
@@ -10,6 +10,4 @@ void
 foo (void)
 {
   int x[a] = { 1 }; /* { dg-error "variable-sized object may not be initialized" "VLA init" } */
-  /* { dg-warning "excess elements in array initializer" "excess" { target *-*-* } .-1 } */
-  /* { dg-message "near initialization" "near" { target *-*-* } .-2 } */
 }

-- 
Joseph S. Myers
joseph@codesourcery.com

^ permalink raw reply	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2020-03-18 20:55 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-03-05 23:50 c: ignore initializers for elements of variable-size types [PR93577] Joseph Myers
2020-03-09 14:58 ` Christophe Lyon
2020-03-10  0:51   ` Joseph Myers
2020-03-10 16:18     ` Christophe Lyon
2020-03-10 23:37       ` Joseph Myers
2020-03-13 12:32         ` Christophe Lyon
2020-03-13 21:47           ` Joseph Myers
2020-03-16 18:59           ` Richard Sandiford
2020-03-17  9:50             ` Christophe Lyon
2020-03-17 13:27             ` Christophe Lyon
2020-03-18 20:55               ` Jeff Law

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).