public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc r13-7967] c-family: char8_t and aliasing in C vs C++ [PR111884]
@ 2023-10-20 19:40 Marek Polacek
  0 siblings, 0 replies; only message in thread
From: Marek Polacek @ 2023-10-20 19:40 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:4f93c6027fb09c94ca8dc7fe13f92a3b9205b68a

commit r13-7967-g4f93c6027fb09c94ca8dc7fe13f92a3b9205b68a
Author: Marek Polacek <polacek@redhat.com>
Date:   Thu Oct 19 16:32:10 2023 -0400

    c-family: char8_t and aliasing in C vs C++ [PR111884]
    
    In the PR, Joseph says that in C char8_t is not a distinct type.  So
    we should behave as if it can alias anything, like ordinary char.
    In C, unsigned_char_type_node == char8_type_node, so with this patch
    we return 0 instead of -1.  And the following comment says:
    
      /* The C standard guarantees that any object may be accessed via an
         lvalue that has narrow character type (except char8_t).  */
      if (t == char_type_node
          || t == signed_char_type_node
          || t == unsigned_char_type_node)
        return 0;
    
    Which appears to be wrong, so I'm adjusting that as well.
    
            PR c/111884
    
    gcc/c-family/ChangeLog:
    
            * c-common.cc (c_common_get_alias_set): Return -1 for char8_t only
            in C++.
    
    gcc/testsuite/ChangeLog:
    
            * c-c++-common/alias-1.c: New test.
    
    (cherry picked from commit 281699fbff6262766674ab13087d37db751cd40a)

Diff:
---
 gcc/c-family/c-common.cc             |  7 ++++---
 gcc/testsuite/c-c++-common/alias-1.c | 23 +++++++++++++++++++++++
 2 files changed, 27 insertions(+), 3 deletions(-)

diff --git a/gcc/c-family/c-common.cc b/gcc/c-family/c-common.cc
index 2b4c82facf7f..aae397ed755c 100644
--- a/gcc/c-family/c-common.cc
+++ b/gcc/c-family/c-common.cc
@@ -3806,12 +3806,13 @@ c_common_get_alias_set (tree t)
   if (!TYPE_P (t))
     return -1;
 
-  /* Unlike char, char8_t doesn't alias. */
-  if (flag_char8_t && t == char8_type_node)
+  /* Unlike char, char8_t doesn't alias in C++.  (In C, char8_t is not
+     a distinct type.)  */
+  if (flag_char8_t && t == char8_type_node && c_dialect_cxx ())
     return -1;
 
   /* The C standard guarantees that any object may be accessed via an
-     lvalue that has narrow character type (except char8_t).  */
+     lvalue that has narrow character type.  */
   if (t == char_type_node
       || t == signed_char_type_node
       || t == unsigned_char_type_node)
diff --git a/gcc/testsuite/c-c++-common/alias-1.c b/gcc/testsuite/c-c++-common/alias-1.c
new file mode 100644
index 000000000000..d72fec47f760
--- /dev/null
+++ b/gcc/testsuite/c-c++-common/alias-1.c
@@ -0,0 +1,23 @@
+/* PR c/111884 */
+/* { dg-do compile } */
+/* { dg-options "-O2 -Wall" } */
+/* { dg-additional-options "-std=c++20" { target c++ } } */
+/* { dg-additional-options "-std=c2x" { target c } } */
+
+int f(int i)
+{
+    int f = 1;
+    return i[(unsigned char *)&f];
+}
+
+int g(int i)
+{
+    int f = 1;
+    return i[(signed char *)&f];
+}
+
+int h(int i)
+{
+    int f = 1;
+    return i[(char *)&f];
+}

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

only message in thread, other threads:[~2023-10-20 19:40 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-10-20 19:40 [gcc r13-7967] c-family: char8_t and aliasing in C vs C++ [PR111884] Marek Polacek

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).