diff --git a/gcc/c/ChangeLog b/gcc/c/ChangeLog index 199b655270c..5b8236ddb09 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 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/ChangeLog b/gcc/testsuite/ChangeLog index 36c87d68e56..1ff727f9e09 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 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 } */ }