For the following source code:

(uintptr_t)b->ptr;

when b->ptr has an counted_by annotation, the IR for this pointer reference
is changed to a call to .ACCESS_WITH_SIZE as:

(uintptr_t).ACCESS_WITH_SIZE (b->ptr, &b->len, 0B, 1);

As a result, the following code in the routine "build_c_cast" is invoked:

 7455       if (TREE_CODE (value) == CALL_EXPR
 7456           && TREE_CODE (type) != TREE_CODE (otype))
 7457         warning_at (loc, OPT_Wbad_function_cast,
 7458                     "cast from function call of type %qT "
 7459                     "to non-matching type %qT", otype, type);
 7460

It's obviously that C FE should exclude the call to .ACCESS_WITH_SIZE from
issuing such warning.

Bootstrapped and regression tested on both X86 and aarch64.

Okay for committing?

thanks.

Qing

        PR c/123500

gcc/c/ChangeLog:

        * c-typeck.cc (build_c_cast): Exclude call to .ACCESS_WITH_SIZE from
        -Wbad-function-cast warnings.

gcc/testsuite/ChangeLog:

        * gcc.dg/pointer-counted-by-pr123500.c: New test.
---
 gcc/c/c-typeck.cc                                  |  3 ++-
 gcc/testsuite/gcc.dg/pointer-counted-by-pr123500.c | 13 +++++++++++++
 2 files changed, 15 insertions(+), 1 deletion(-)
 create mode 100644 gcc/testsuite/gcc.dg/pointer-counted-by-pr123500.c

diff --git a/gcc/c/c-typeck.cc b/gcc/c/c-typeck.cc
index cab21e29004..d6106dbc945 100644
--- a/gcc/c/c-typeck.cc
+++ b/gcc/c/c-typeck.cc
@@ -7452,7 +7452,8 @@ build_c_cast (location_t loc, tree type, tree expr)
        warning_at (loc, OPT_Wpointer_to_int_cast,
                    "cast from pointer to integer of different size");
 
-      if (TREE_CODE (value) == CALL_EXPR
+      if ((TREE_CODE (value) == CALL_EXPR
+          && !is_access_with_size_p (value))
          && TREE_CODE (type) != TREE_CODE (otype))
        warning_at (loc, OPT_Wbad_function_cast,
                    "cast from function call of type %qT "
diff --git a/gcc/testsuite/gcc.dg/pointer-counted-by-pr123500.c 
b/gcc/testsuite/gcc.dg/pointer-counted-by-pr123500.c
new file mode 100644
index 00000000000..6f57ae5dd66
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/pointer-counted-by-pr123500.c
@@ -0,0 +1,13 @@
+/* PR c/123500 */
+/* { dg-do compile } */
+/* { dg-options "-Wbad-function-cast" } */
+
+#include <stdint.h>
+struct buffer {
+  uint8_t * ptr __attribute__((counted_by(len)));
+  int len;
+};
+
+uintptr_t foo(struct buffer * b) {
+  return (uintptr_t)b->ptr;
+}
-- 
2.31.1

Reply via email to