From 416ee7db600d3986a29cba155fb2a692a0e56453 Mon Sep 17 00:00:00 2001 From: jason Date: Thu, 15 Dec 2011 21:55:31 +0000 Subject: [PATCH] PR c++/51458 * decl.c (has_designator_problem): New. (reshape_init_r): Check for improper use of designated initializers. git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@182391 138bc75d-0d04-0410-961f-82ee72b054a4 --- gcc/cp/ChangeLog | 7 +++++++ gcc/cp/decl.c | 26 ++++++++++++++++++++++++++ gcc/testsuite/ChangeLog | 5 +++++ gcc/testsuite/g++.dg/ext/desig4.C | 10 ++++++++++ 4 files changed, 48 insertions(+) create mode 100644 gcc/testsuite/g++.dg/ext/desig4.C diff --git a/gcc/cp/ChangeLog b/gcc/cp/ChangeLog index c9ada9a..5953866 100644 --- a/gcc/cp/ChangeLog +++ b/gcc/cp/ChangeLog @@ -1,3 +1,10 @@ +2011-12-15 Jason Merrill + + PR c++/51458 + * decl.c (has_designator_problem): New. + (reshape_init_r): Check for improper use of + designated initializers. + 2011-12-15 Jakub Jelinek PR c++/51463 diff --git a/gcc/cp/decl.c b/gcc/cp/decl.c index 1239535..919e235 100644 --- a/gcc/cp/decl.c +++ b/gcc/cp/decl.c @@ -5128,6 +5128,24 @@ reshape_init_class (tree type, reshape_iter *d, bool first_initializer_p, return new_init; } +/* Subroutine of reshape_init_r. We're in a context where C99 initializer + designators are not valid; either complain or return true to indicate + that reshape_init_r should return error_mark_node. */ + +static bool +has_designator_problem (reshape_iter *d, tsubst_flags_t complain) +{ + if (d->cur->index) + { + if (complain & tf_error) + error ("C99 designator %qE outside aggregate initializer", + d->cur->index); + else + return true; + } + return false; +} + /* Subroutine of reshape_init, which processes a single initializer (part of a CONSTRUCTOR). TYPE is the type of the variable being initialized, D is the iterator within the CONSTRUCTOR which points to the initializer to process. @@ -5143,6 +5161,10 @@ reshape_init_r (tree type, reshape_iter *d, bool first_initializer_p, if (error_operand_p (init)) return error_mark_node; + if (first_initializer_p && !CP_AGGREGATE_TYPE_P (type) + && has_designator_problem (d, complain)) + return error_mark_node; + if (TREE_CODE (type) == COMPLEX_TYPE) { /* A complex type can be initialized from one or two initializers, @@ -5163,6 +5185,8 @@ reshape_init_r (tree type, reshape_iter *d, bool first_initializer_p, VEC(constructor_elt, gc) *v = 0; CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, init); CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, d->cur->value); + if (has_designator_problem (d, complain)) + return error_mark_node; d->cur++; init = build_constructor (init_list_type_node, v); } @@ -5242,6 +5266,8 @@ reshape_init_r (tree type, reshape_iter *d, bool first_initializer_p, array types (one value per array element). */ if (TREE_CODE (str_init) == STRING_CST) { + if (has_designator_problem (d, complain)) + return error_mark_node; d->cur++; return str_init; } diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index 8786f42..c38491b 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,8 @@ +2011-12-15 Jason Merrill + + PR c++/51458 + * g++.dg/ext/desig4.C: New. + 2011-12-15 Paul Thomas * gfortran.dg/class_array_3.f03: Remove explicit indexing of diff --git a/gcc/testsuite/g++.dg/ext/desig4.C b/gcc/testsuite/g++.dg/ext/desig4.C new file mode 100644 index 0000000..48d629a --- /dev/null +++ b/gcc/testsuite/g++.dg/ext/desig4.C @@ -0,0 +1,10 @@ +// PR c++/51458 +// { dg-options "" } + +char g[] = { [7] = "abcd" }; // { dg-error "designator" } +int a = { .foo = 6 }; // { dg-error "designator" } +int b = { [0] = 1 }; // { dg-error "designator" } +_Complex float c = { .foo = 0, 1 }; // { dg-error "designator" } +_Complex float d = { [0] = 0, 1 }; // { dg-error "designator" } +_Complex float e = { 0, .foo = 1 }; // { dg-error "designator" } +_Complex float f = { 0, [0] = 1 }; // { dg-error "designator" } -- 2.7.4