Hi!

In the following testcases, we ICE on all 4 function calls.
The problem is using TYPE_PRECISION on vector types (but guess it
would be similarly problematic on structures/unions/arrays).
The test only differentiates between suggestion what to do, whether
to supply explicit size because sizeof (*p) for
{,{,un}signed }char *p is not very likely what the user want, or
dereferencing the pointer, so I think limiting that suggestion
to integral types is ok.

Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk?

2024-10-22  Jakub Jelinek  <ja...@redhat.com>

        PR c/117230
        * c-warn.cc (sizeof_pointer_memaccess_warning): Only compare
        TYPE_PRECISION of TREE_TYPE (type) to precision of char if
        TREE_TYPE (type) is integral type.

        * c-c++-common/Wsizeof-pointer-memaccess5.c: New test.

--- gcc/c-family/c-warn.cc.jj   2024-09-06 13:43:37.340307542 +0200
+++ gcc/c-family/c-warn.cc      2024-10-22 12:10:37.793621714 +0200
@@ -944,8 +944,9 @@ sizeof_pointer_memaccess_warning (locati
                        "argument to %<sizeof%> in %qD call is the same "
                        "expression as the destination; did you mean to "
                        "remove the addressof?", callee);
-         else if ((TYPE_PRECISION (TREE_TYPE (type))
-                   == TYPE_PRECISION (char_type_node))
+         else if ((INTEGRAL_TYPE_P (TREE_TYPE (type))
+                   && (TYPE_PRECISION (TREE_TYPE (type))
+                       == TYPE_PRECISION (char_type_node)))
                   || strop)
            warning_at (loc, OPT_Wsizeof_pointer_memaccess,
                        "argument to %<sizeof%> in %qD call is the same "
@@ -984,8 +985,9 @@ sizeof_pointer_memaccess_warning (locati
                        "argument to %<sizeof%> in %qD call is the same "
                        "expression as the source; did you mean to "
                        "remove the addressof?", callee);
-         else if ((TYPE_PRECISION (TREE_TYPE (type))
-                   == TYPE_PRECISION (char_type_node))
+         else if ((INTEGRAL_TYPE_P (TREE_TYPE (type))
+                   && (TYPE_PRECISION (TREE_TYPE (type))
+                       == TYPE_PRECISION (char_type_node)))
                   || strop)
            warning_at (loc, OPT_Wsizeof_pointer_memaccess,
                        "argument to %<sizeof%> in %qD call is the same "
@@ -1024,8 +1026,9 @@ sizeof_pointer_memaccess_warning (locati
                        "argument to %<sizeof%> in %qD call is the same "
                        "expression as the first source; did you mean to "
                        "remove the addressof?", callee);
-         else if ((TYPE_PRECISION (TREE_TYPE (type))
-                   == TYPE_PRECISION (char_type_node))
+         else if ((INTEGRAL_TYPE_P (TREE_TYPE (type))
+                   && (TYPE_PRECISION (TREE_TYPE (type))
+                       == TYPE_PRECISION (char_type_node)))
                   || strop)
            warning_at (loc, OPT_Wsizeof_pointer_memaccess,
                        "argument to %<sizeof%> in %qD call is the same "
@@ -1064,8 +1067,9 @@ sizeof_pointer_memaccess_warning (locati
                        "argument to %<sizeof%> in %qD call is the same "
                        "expression as the second source; did you mean to "
                        "remove the addressof?", callee);
-         else if ((TYPE_PRECISION (TREE_TYPE (type))
-                   == TYPE_PRECISION (char_type_node))
+         else if ((INTEGRAL_TYPE_P (TREE_TYPE (type))
+                   && (TYPE_PRECISION (TREE_TYPE (type))
+                       == TYPE_PRECISION (char_type_node)))
                   || strop)
            warning_at (loc, OPT_Wsizeof_pointer_memaccess,
                        "argument to %<sizeof%> in %qD call is the same "
--- gcc/testsuite/c-c++-common/Wsizeof-pointer-memaccess5.c.jj  2024-10-22 
12:28:17.611586053 +0200
+++ gcc/testsuite/c-c++-common/Wsizeof-pointer-memaccess5.c     2024-10-22 
12:35:38.849318963 +0200
@@ -0,0 +1,29 @@
+/* PR c/117230 */
+/* { dg-do compile } */
+/* { dg-options "-Wsizeof-pointer-memaccess" } */
+
+typedef int V __attribute__((vector_size (sizeof (int))));
+
+void
+foo (V *a, char *b)
+{
+  __builtin_memcpy (b, a, sizeof (a));         /* { dg-warning "argument to 
'sizeof' in '\[^\n\r]*__builtin_memcpy\[^\n\r]*' call is the same expression as 
the source; did you mean to dereference it\\\?" } */
+}
+
+void
+bar (V *a, char *b)
+{
+  __builtin_memcpy (a, b, sizeof (a));         /* { dg-warning "argument to 
'sizeof' in '\[^\n\r]*__builtin_memcpy\[^\n\r]*' call is the same expression as 
the destination; did you mean to dereference it\\\?" } */
+}
+
+int
+baz (V *a, char *b)
+{
+  return __builtin_memcmp (a, b, sizeof (a));  /* { dg-warning "argument to 
'sizeof' in '\[^\n\r]*__builtin_memcmp\[^\n\r]*' call is the same expression as 
the first source; did you mean to dereference it\\\?" } */
+}
+
+int
+qux (V *a, char *b)
+{
+  return __builtin_memcmp (b, a, sizeof (a));  /* { dg-warning "argument to 
'sizeof' in '\[^\n\r]*__builtin_memcmp\[^\n\r]*' call is the same expression as 
the second source; did you mean to dereference it\\\?" } */
+}

        Jakub

Reply via email to