From e54b62687aca823aebb15000d33ca59947204881 Mon Sep 17 00:00:00 2001 From: Eric Botcazou Date: Sat, 26 Jan 2019 15:59:34 +0000 Subject: [PATCH] trans.c (gnat_to_gnu): Use DECL_SIZE_UNIT instead of TYPE_SIZE_UNIT for the size to be assigned by a... * gcc-interface/trans.c (gnat_to_gnu) : Use DECL_SIZE_UNIT instead of TYPE_SIZE_UNIT for the size to be assigned by a call to memset if the LHS is a DECL. From-SVN: r268296 --- gcc/ada/ChangeLog | 6 ++++++ gcc/ada/gcc-interface/trans.c | 17 +++++++++++------ gcc/testsuite/ChangeLog | 4 ++++ gcc/testsuite/gnat.dg/array34.adb | 22 ++++++++++++++++++++++ 4 files changed, 43 insertions(+), 6 deletions(-) create mode 100644 gcc/testsuite/gnat.dg/array34.adb diff --git a/gcc/ada/ChangeLog b/gcc/ada/ChangeLog index 628f318cffb..55507d343bc 100644 --- a/gcc/ada/ChangeLog +++ b/gcc/ada/ChangeLog @@ -1,3 +1,9 @@ +2019-01-26 Eric Botcazou + + * gcc-interface/trans.c (gnat_to_gnu) : Use + DECL_SIZE_UNIT instead of TYPE_SIZE_UNIT for the size to be assigned + by a call to memset if the LHS is a DECL. + 2019-01-26 Eric Botcazou * gcc-interface/trans.c (struct loop_info_d): Remove artificial field. diff --git a/gcc/ada/gcc-interface/trans.c b/gcc/ada/gcc-interface/trans.c index be5ce1ca577..db8e4c2d58f 100644 --- a/gcc/ada/gcc-interface/trans.c +++ b/gcc/ada/gcc-interface/trans.c @@ -7742,12 +7742,17 @@ gnat_to_gnu (Node_Id gnat_node) = real_zerop (gnu_rhs) ? integer_zero_node : fold_convert (integer_type_node, gnu_rhs); - tree to = gnu_lhs; - tree type = TREE_TYPE (to); - tree size - = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TYPE_SIZE_UNIT (type), to); - tree to_ptr = build_fold_addr_expr (to); + tree dest = build_fold_addr_expr (gnu_lhs); tree t = builtin_decl_explicit (BUILT_IN_MEMSET); + /* Be extra careful not to write too much data. */ + tree size; + if (TREE_CODE (gnu_lhs) == COMPONENT_REF) + size = DECL_SIZE_UNIT (TREE_OPERAND (gnu_lhs, 1)); + else if (DECL_P (gnu_lhs)) + size = DECL_SIZE_UNIT (gnu_lhs); + else + size = TYPE_SIZE_UNIT (TREE_TYPE (gnu_lhs)); + size = SUBSTITUTE_PLACEHOLDER_IN_EXPR (size, gnu_lhs); if (TREE_CODE (value) == INTEGER_CST && !integer_zerop (value)) { tree mask @@ -7755,7 +7760,7 @@ gnat_to_gnu (Node_Id gnat_node) ((HOST_WIDE_INT) 1 << BITS_PER_UNIT) - 1); value = int_const_binop (BIT_AND_EXPR, value, mask); } - gnu_result = build_call_expr (t, 3, to_ptr, value, size); + gnu_result = build_call_expr (t, 3, dest, value, size); } /* Otherwise build a regular assignment. */ diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index a1805f85442..4d55dee26b7 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,7 @@ +2019-01-26 Eric Botcazou + + * gnat.dg/array34.adb: New test. + 2019-01-26 Jakub Jelinek PR preprocessor/88974 diff --git a/gcc/testsuite/gnat.dg/array34.adb b/gcc/testsuite/gnat.dg/array34.adb new file mode 100644 index 00000000000..62809cd0269 --- /dev/null +++ b/gcc/testsuite/gnat.dg/array34.adb @@ -0,0 +1,22 @@ +-- { dg-do run } + +procedure Array34 is + + type Arr is array (1 .. 6) of Short_Short_Integer; + for Arr'Alignment use 4; + + type Rec is record + A : Arr; + B: Short_Integer; + end record; + pragma Pack (Rec); + + R : Rec; + +begin + R.B := 31415; + R.A := (others => 0); + if R.B /= 31415 then + raise Program_Error; + end if; +end; -- 2.30.2