public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc r13-8626] fold-const: Handle NON_LVALUE_EXPR in native_encode_initializer [PR114537]
@ 2024-04-21  4:08 Jakub Jelinek
  0 siblings, 0 replies; only message in thread
From: Jakub Jelinek @ 2024-04-21  4:08 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:a297f9bbb9611414fe48f6d61a8829bf5808bd2c

commit r13-8626-ga297f9bbb9611414fe48f6d61a8829bf5808bd2c
Author: Jakub Jelinek <jakub@redhat.com>
Date:   Thu Apr 4 10:47:52 2024 +0200

    fold-const: Handle NON_LVALUE_EXPR in native_encode_initializer [PR114537]
    
    The following testcase is incorrectly rejected.  The problem is that
    for bit-fields native_encode_initializer expects the corresponding
    CONSTRUCTOR elt value must be INTEGER_CST, but that isn't the case
    here, it is wrapped into NON_LVALUE_EXPR by maybe_wrap_with_location.
    We could STRIP_ANY_LOCATION_WRAPPER as well, but as all we are looking for
    is INTEGER_CST inside, just looking through NON_LVALUE_EXPR seems easier.
    
    2024-04-04  Jakub Jelinek  <jakub@redhat.com>
    
            PR c++/114537
            * fold-const.cc (native_encode_initializer): Look through
            NON_LVALUE_EXPR if val is INTEGER_CST.
    
            * g++.dg/cpp2a/bit-cast16.C: New test.
    
    (cherry picked from commit 1baec8deb014b8a7da58879a407a4c00cdeb5a09)

Diff:
---
 gcc/fold-const.cc                       |  2 ++
 gcc/testsuite/g++.dg/cpp2a/bit-cast16.C | 16 ++++++++++++++++
 2 files changed, 18 insertions(+)

diff --git a/gcc/fold-const.cc b/gcc/fold-const.cc
index 31ae6cebbe6..25dd7c1094e 100644
--- a/gcc/fold-const.cc
+++ b/gcc/fold-const.cc
@@ -8485,6 +8485,8 @@ native_encode_initializer (tree init, unsigned char *ptr, int len,
 		  if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN)
 		    return 0;
 
+		  if (TREE_CODE (val) == NON_LVALUE_EXPR)
+		    val = TREE_OPERAND (val, 0);
 		  if (TREE_CODE (val) != INTEGER_CST)
 		    return 0;
 
diff --git a/gcc/testsuite/g++.dg/cpp2a/bit-cast16.C b/gcc/testsuite/g++.dg/cpp2a/bit-cast16.C
new file mode 100644
index 00000000000..d298af67ef2
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp2a/bit-cast16.C
@@ -0,0 +1,16 @@
+// PR c++/114537
+// { dg-do compile { target c++20 } }
+
+namespace std {
+template<typename T, typename F>
+constexpr T
+bit_cast (const F& f) noexcept
+{
+  return __builtin_bit_cast (T, f);
+}
+}
+
+struct A { signed char b : 1 = 0; signed char c : 7 = 0; };
+struct D { unsigned char e; };
+constexpr unsigned char f = std::bit_cast<D> (A{}).e;
+static_assert (f == 0);

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2024-04-21  4:08 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-04-21  4:08 [gcc r13-8626] fold-const: Handle NON_LVALUE_EXPR in native_encode_initializer [PR114537] Jakub Jelinek

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).