From 4164e58bfd5d57e7399f91c7c27ae6ccebc7dcb0 Mon Sep 17 00:00:00 2001 From: Jakub Jelinek Date: Wed, 9 Dec 2020 09:34:51 +0100 Subject: [PATCH] fold-const: Fix up native_encode_initializer missing field handling [PR98193] When native_encode_initializer is called with non-NULL mask (i.e. ATM bit_cast only), it checks if the current index in the CONSTRUCTOR (if any) is the next initializable FIELD_DECL, and if not, decrements cnt and performs the iteration with that FIELD_DECL as field and val of zero (so that it computes mask properly). As the testcase shows, I forgot to set pos to the byte position of the field though (like it is done for e.g. index referenced FIELD_DECLs in the constructor. 2020-12-09 Jakub Jelinek PR c++/98193 * fold-const.c (native_encode_initializer): Set pos to field's byte position if iterating over a field with missing initializer. * g++.dg/cpp2a/bit-cast7.C: New test. --- gcc/fold-const.c | 1 + gcc/testsuite/g++.dg/cpp2a/bit-cast7.C | 39 ++++++++++++++++++++++++++ 2 files changed, 40 insertions(+) create mode 100644 gcc/testsuite/g++.dg/cpp2a/bit-cast7.C diff --git a/gcc/fold-const.c b/gcc/fold-const.c index 1241b1395d4..81467f19fdb 100644 --- a/gcc/fold-const.c +++ b/gcc/fold-const.c @@ -8256,6 +8256,7 @@ native_encode_initializer (tree init, unsigned char *ptr, int len, { cnt--; field = fld; + pos = int_byte_position (field); val = build_zero_cst (TREE_TYPE (fld)); if (TREE_CODE (val) == CONSTRUCTOR) to_free = val; diff --git a/gcc/testsuite/g++.dg/cpp2a/bit-cast7.C b/gcc/testsuite/g++.dg/cpp2a/bit-cast7.C new file mode 100644 index 00000000000..4a3c6820070 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp2a/bit-cast7.C @@ -0,0 +1,39 @@ +// PR c++/98193 +// { dg-do compile { target c++20 } } + +template +constexpr To +bit_cast (const From &from) +{ + return __builtin_bit_cast (To, from); +} + +struct J +{ + long int a, b : 11, h; +}; + +struct K +{ + long int a, b : 11, c; + constexpr bool operator == (const K &x) + { + return a == x.a && b == x.b && c == x.c; + } +}; + +struct L +{ + long long int a, b : 11, h; +}; +struct M +{ + long long int a, b : 11, c; + constexpr bool operator == (const M &x) + { + return a == x.a && b == x.b && c == x.c; + } +}; + +static_assert (bit_cast (J{}) == K{}, ""); +static_assert (bit_cast (L{0x0feedbacdeadbeefLL}) == M{0x0feedbacdeadbeefLL}, ""); -- 2.30.2