From: Joseph Myers Date: Thu, 5 Mar 2020 23:48:06 +0000 (+0000) Subject: c: ignore initializers for elements of variable-size types [PR93577] X-Git-Tag: upstream/12.2.0~17981 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=c9d70946b3c9f10cfd560407aac8b0b4515f10b6;p=platform%2Fupstream%2Fgcc.git c: ignore initializers for elements of variable-size types [PR93577] 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. PR c/93577 gcc/c: * 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: * 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/ChangeLog b/gcc/c/ChangeLog index 199b655..5b8236d 100644 --- a/gcc/c/ChangeLog +++ b/gcc/c/ChangeLog @@ -1,3 +1,11 @@ +2020-03-05 Joseph Myers + + 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. + 2020-03-01 Martin Sebor PR middle-end/93926 diff --git a/gcc/c/c-typeck.c b/gcc/c/c-typeck.c index 308fcff..d8025de 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/ChangeLog b/gcc/testsuite/ChangeLog index 36c87d6..1ff727f 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,11 @@ +2020-03-05 Joseph Myers + + 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. + 2020-03-05 Jeff Law PR tree-optimization/91890 diff --git a/gcc/testsuite/gcc.dg/pr93577-1.c b/gcc/testsuite/gcc.dg/pr93577-1.c new file mode 100644 index 0000000..31023d7 --- /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 0000000..c61589e --- /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 0000000..278146b --- /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 0000000..0ac117c --- /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 0000000..68dfc1f --- /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 0000000..5ec668f --- /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 38e9b01..3125b70 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 } */ }