From 6d9001072d3ed5a82b8277426e6dd41a1c605990 Mon Sep 17 00:00:00 2001 From: Bernd Edlinger Date: Thu, 6 Sep 2018 15:39:48 +0000 Subject: [PATCH] varasm.c (output_constructor_regular_field): Check TYPE_SIZE_UNIT of the init value. * varasm.c (output_constructor_regular_field): Check TYPE_SIZE_UNIT of the init value. * c-common.c (complete_flexible_array_elts): New helper function. * c-common.h (complete_flexible_array_elts): Declare. * c-decl.c (finish_decl): Call complete_flexible_array_elts. * decl.c (check_initializer): Call cp_complete_array_type. From-SVN: r264147 --- gcc/ChangeLog | 5 +++++ gcc/c-family/ChangeLog | 5 +++++ gcc/c-family/c-common.c | 22 ++++++++++++++++++++++ gcc/c-family/c-common.h | 1 + gcc/c/ChangeLog | 4 ++++ gcc/c/c-decl.c | 2 ++ gcc/cp/ChangeLog | 4 ++++ gcc/cp/decl.c | 10 ++++++++++ gcc/varasm.c | 2 ++ 9 files changed, 55 insertions(+) diff --git a/gcc/ChangeLog b/gcc/ChangeLog index 19800f5930b..f8fedba6171 100644 --- a/gcc/ChangeLog +++ b/gcc/ChangeLog @@ -1,3 +1,8 @@ +2018-09-06 Bernd Edlinger + + * varasm.c (output_constructor_regular_field): Check TYPE_SIZE_UNIT of + the init value. + 2018-09-06 Will Schmidt * config/rs6000/rs6000.c (rs6000_gimple_fold_builtin): Add support for diff --git a/gcc/c-family/ChangeLog b/gcc/c-family/ChangeLog index 4129f244cd5..75064a2b684 100644 --- a/gcc/c-family/ChangeLog +++ b/gcc/c-family/ChangeLog @@ -1,3 +1,8 @@ +2018-09-06 Bernd Edlinger + + * c-common.c (complete_flexible_array_elts): New helper function. + * c-common.h (complete_flexible_array_elts): Declare. + 2018-09-02 Bernd Edlinger * c-common.c (braced_list_to_string): Remove eval parameter. diff --git a/gcc/c-family/c-common.c b/gcc/c-family/c-common.c index 13ed65cb012..4bfb14585e2 100644 --- a/gcc/c-family/c-common.c +++ b/gcc/c-family/c-common.c @@ -6425,6 +6425,28 @@ complete_array_type (tree *ptype, tree initial_value, bool do_default) return failure; } +/* INIT is an constructor of a structure with a flexible array member. + Complete the flexible array member with a domain based on it's value. */ +void +complete_flexible_array_elts (tree init) +{ + tree elt, type; + + if (init == NULL_TREE || TREE_CODE (init) != CONSTRUCTOR) + return; + + if (vec_safe_is_empty (CONSTRUCTOR_ELTS (init))) + return; + + elt = CONSTRUCTOR_ELTS (init)->last ().value; + type = TREE_TYPE (elt); + if (TREE_CODE (type) == ARRAY_TYPE + && TYPE_SIZE (type) == NULL_TREE) + complete_array_type (&TREE_TYPE (elt), elt, false); + else + complete_flexible_array_elts (elt); +} + /* Like c_mark_addressable but don't check register qualifier. */ void c_common_mark_addressable_vec (tree t) diff --git a/gcc/c-family/c-common.h b/gcc/c-family/c-common.h index cc168e22c6b..9e868768d01 100644 --- a/gcc/c-family/c-common.h +++ b/gcc/c-family/c-common.h @@ -1038,6 +1038,7 @@ extern tree fold_offsetof (tree, tree = size_type_node, tree_code ctx = ERROR_MARK); extern int complete_array_type (tree *, tree, bool); +extern void complete_flexible_array_elts (tree); extern tree builtin_type_for_size (int, bool); diff --git a/gcc/c/ChangeLog b/gcc/c/ChangeLog index 5c18f6307f8..74d34b1ddb0 100644 --- a/gcc/c/ChangeLog +++ b/gcc/c/ChangeLog @@ -1,3 +1,7 @@ +2018-09-06 Bernd Edlinger + + * c-decl.c (finish_decl): Call complete_flexible_array_elts. + 2018-09-02 Bernd Edlinger * c-decl.c (finish_decl): Call braced_list_to_string here ... diff --git a/gcc/c/c-decl.c b/gcc/c/c-decl.c index fd08d72b11f..fdcfbde2d99 100644 --- a/gcc/c/c-decl.c +++ b/gcc/c/c-decl.c @@ -5042,6 +5042,8 @@ finish_decl (tree decl, location_t init_loc, tree init, if (init && TREE_CODE (init) == CONSTRUCTOR) add_flexible_array_elts_to_size (decl, init); + complete_flexible_array_elts (DECL_INITIAL (decl)); + if (DECL_SIZE (decl) == NULL_TREE && TREE_TYPE (decl) != error_mark_node && COMPLETE_TYPE_P (TREE_TYPE (decl))) layout_decl (decl, 0); diff --git a/gcc/cp/ChangeLog b/gcc/cp/ChangeLog index d5879f8cd0e..42e70cdbe25 100644 --- a/gcc/cp/ChangeLog +++ b/gcc/cp/ChangeLog @@ -1,3 +1,7 @@ +2018-09-06 Bernd Edlinger + + * decl.c (check_initializer): Call cp_complete_array_type. + 2018-09-05 Marek Polacek PR c++/87109, wrong overload with ref-qualifiers. diff --git a/gcc/cp/decl.c b/gcc/cp/decl.c index 5962c19fbe4..50b60e89df5 100644 --- a/gcc/cp/decl.c +++ b/gcc/cp/decl.c @@ -6478,6 +6478,16 @@ check_initializer (tree decl, tree init, int flags, vec **cleanups) init_code = store_init_value (decl, init, cleanups, flags); + if (DECL_INITIAL (decl) + && TREE_CODE (DECL_INITIAL (decl)) == CONSTRUCTOR + && !vec_safe_is_empty (CONSTRUCTOR_ELTS (DECL_INITIAL (decl)))) + { + tree elt = CONSTRUCTOR_ELTS (DECL_INITIAL (decl))->last ().value; + if (TREE_CODE (TREE_TYPE (elt)) == ARRAY_TYPE + && TYPE_SIZE (TREE_TYPE (elt)) == NULL_TREE) + cp_complete_array_type (&TREE_TYPE (elt), elt, false); + } + if (pedantic && TREE_CODE (type) == ARRAY_TYPE && DECL_INITIAL (decl) && TREE_CODE (DECL_INITIAL (decl)) == STRING_CST diff --git a/gcc/varasm.c b/gcc/varasm.c index 2180da48895..27d878f218a 100644 --- a/gcc/varasm.c +++ b/gcc/varasm.c @@ -5160,6 +5160,8 @@ output_constructor_regular_field (oc_local_state *local) on the chain is a TYPE_DECL of the enclosing struct. */ const_tree next = DECL_CHAIN (local->field); gcc_assert (!fieldsize || !next || TREE_CODE (next) != FIELD_DECL); + tree size = TYPE_SIZE_UNIT (TREE_TYPE (local->val)); + gcc_checking_assert (compare_tree_int (size, fieldsize) == 0); } else fieldsize = tree_to_uhwi (DECL_SIZE_UNIT (local->field)); -- 2.30.2