d: Fix struct literals that have non-deterministic hash values (PR96153)
authorIain Buclaw <ibuclaw@gdcproject.org>
Wed, 22 Jul 2020 07:50:38 +0000 (09:50 +0200)
committerIain Buclaw <ibuclaw@gdcproject.org>
Tue, 4 Aug 2020 15:02:56 +0000 (17:02 +0200)
commit2ac51bdf63b0e17d1b9974f30303fb24e3cbc83d
tree78a0b25b1cb01fd3bf17716064ac421dd5f04adf
parent19fad46721af338b24aff48127ae654673bffa7b
d: Fix struct literals that have non-deterministic hash values (PR96153)

Adds code generation for generating a temporary for, and pre-filling
struct and array literals with zeroes before assigning, so that
alignment holes don't cause objects to produce a non-deterministic hash
value.  A new field has been added to the expression visitor to track
whether the result is being generated for another literal, so that
memset() is only called once on the top-level literal expression, and
not for nesting struct or arrays.

gcc/d/ChangeLog:

PR d/96153
* d-tree.h (build_expr): Add literalp argument.
* expr.cc (ExprVisitor): Add literalp_ field.
(ExprVisitor::ExprVisitor): Initialize literalp_.
(ExprVisitor::visit (AssignExp *)): Call memset() on blits where RHS
is a struct literal.  Elide assignment if initializer is all zeroes.
(ExprVisitor::visit (CastExp *)): Forward literalp_ to generation of
subexpression.
(ExprVisitor::visit (AddrExp *)): Likewise.
(ExprVisitor::visit (ArrayLiteralExp *)): Use memset() to pre-fill
object with zeroes.  Set literalp in subexpressions.
(ExprVisitor::visit (StructLiteralExp *)): Likewise.
(ExprVisitor::visit (TupleExp *)): Set literalp in subexpressions.
(ExprVisitor::visit (VectorExp *)): Likewise.
(ExprVisitor::visit (VectorArrayExp *)): Likewise.
(build_expr): Forward literal_p to ExprVisitor.

gcc/testsuite/ChangeLog:

PR d/96153
* gdc.dg/pr96153.d: New test.
gcc/d/d-tree.h
gcc/d/expr.cc
gcc/testsuite/gdc.dg/pr96153.d [new file with mode: 0644]