aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMartin Sebor <msebor@gcc.gnu.org>2019-10-02 16:00:42 -0600
committerMartin Sebor <msebor@gcc.gnu.org>2019-10-02 16:00:42 -0600
commitd1090a8a805de4e3b33248753e792ab302d3f6db (patch)
tree91ca31bbffaccfb18a83ff5eccbe1563d584818f
parentaa29ed6db6d409b54e552830230205a7b4da0d4d (diff)
downloadgcc-d1090a8a805de4e3b33248753e792ab302d3f6db.zip
gcc-d1090a8a805de4e3b33248753e792ab302d3f6db.tar.gz
gcc-d1090a8a805de4e3b33248753e792ab302d3f6db.tar.bz2
PR tree-optimization/80936 - bcmp, bcopy, and bzero not declared nonnull
gcc/testsuite/ChangeLog: PR tree-optimization/80936 * gcc.dg/Wnonnull-2.c: New test. * gcc.dg/Wnonnull-3.c: New test. * gcc.dg/nonnull-3.c: Expect more warnings. gcc/ChangeLog: PR tree-optimization/80936 * builtins.def (bcmp, bcopy, bzero): Declare nonnull. From-SVN: r276491
-rw-r--r--gcc/builtins.def8
-rw-r--r--gcc/testsuite/gcc.dg/Wnonnull-2.c55
-rw-r--r--gcc/testsuite/gcc.dg/Wnonnull-3.c71
-rw-r--r--gcc/testsuite/gcc.dg/nonnull-3.c10
4 files changed, 134 insertions, 10 deletions
diff --git a/gcc/builtins.def b/gcc/builtins.def
index 8bb7027..5b9b706 100644
--- a/gcc/builtins.def
+++ b/gcc/builtins.def
@@ -687,11 +687,9 @@ DEF_C99_COMPL_BUILTIN (BUILT_IN_CTANHL, "ctanhl", BT_FN_COMPLEX_LONGDOUBL
DEF_C99_COMPL_BUILTIN (BUILT_IN_CTANL, "ctanl", BT_FN_COMPLEX_LONGDOUBLE_COMPLEX_LONGDOUBLE, ATTR_MATHFN_FPROUNDING)
/* Category: string/memory builtins. */
-/* bcmp, bcopy and bzero have traditionally accepted NULL pointers
- when the length parameter is zero, so don't apply attribute "nonnull". */
-DEF_EXT_LIB_BUILTIN (BUILT_IN_BCMP, "bcmp", BT_FN_INT_CONST_PTR_CONST_PTR_SIZE, ATTR_PURE_NOTHROW_LEAF_LIST)
-DEF_EXT_LIB_BUILTIN (BUILT_IN_BCOPY, "bcopy", BT_FN_VOID_CONST_PTR_PTR_SIZE, ATTR_NOTHROW_LEAF_LIST)
-DEF_EXT_LIB_BUILTIN (BUILT_IN_BZERO, "bzero", BT_FN_VOID_PTR_SIZE, ATTR_NOTHROW_LEAF_LIST)
+DEF_EXT_LIB_BUILTIN (BUILT_IN_BCMP, "bcmp", BT_FN_INT_CONST_PTR_CONST_PTR_SIZE, ATTR_PURE_NOTHROW_NONNULL_LEAF)
+DEF_EXT_LIB_BUILTIN (BUILT_IN_BCOPY, "bcopy", BT_FN_VOID_CONST_PTR_PTR_SIZE, ATTR_NOTHROW_NONNULL_LEAF)
+DEF_EXT_LIB_BUILTIN (BUILT_IN_BZERO, "bzero", BT_FN_VOID_PTR_SIZE, ATTR_NOTHROW_NONNULL_LEAF)
DEF_EXT_LIB_BUILTIN (BUILT_IN_INDEX, "index", BT_FN_STRING_CONST_STRING_INT, ATTR_PURE_NOTHROW_NONNULL_LEAF)
DEF_LIB_BUILTIN (BUILT_IN_MEMCHR, "memchr", BT_FN_PTR_CONST_PTR_INT_SIZE, ATTR_PURE_NOTHROW_NONNULL_LEAF)
DEF_LIB_BUILTIN (BUILT_IN_MEMCMP, "memcmp", BT_FN_INT_CONST_PTR_CONST_PTR_SIZE, ATTR_PURE_NOTHROW_NONNULL_LEAF)
diff --git a/gcc/testsuite/gcc.dg/Wnonnull-2.c b/gcc/testsuite/gcc.dg/Wnonnull-2.c
new file mode 100644
index 0000000..d870473
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/Wnonnull-2.c
@@ -0,0 +1,55 @@
+/* PR middle-end/80936 - bcmp, bcopy, and bzero not declared nonnull
+ Verify that -Wnonnull is issued for calls with constant null pointers
+ with no optimization.
+ { dg-do compile }
+ { dg-options "-O0 -Wall" } */
+
+void zero0 (void *p, unsigned n)
+{
+ __builtin_memset (0, 0, n); // { dg-warning "\\\[-Wnonnull]" }
+}
+
+void zero1 (void *p, unsigned n)
+{
+ __builtin_bzero (0, n); // { dg-warning "\\\[-Wnonnull]" }
+}
+
+void copy0 (void *p, const void *q, unsigned n)
+{
+ __builtin_memcpy (0, q, n); // { dg-warning "\\\[-Wnonnull]" }
+}
+
+void copy1 (void *p, const void *q, unsigned n)
+{
+ __builtin_memcpy (0, q, n); // { dg-warning "\\\[-Wnonnull]" }
+}
+
+void copy2 (void *p, const void *q, unsigned n)
+{
+ __builtin_bcopy (q, 0, n); // { dg-warning "\\\[-Wnonnull]" }
+}
+
+void copy3 (void *p, const void *q, unsigned n)
+{
+ __builtin_bcopy (q, 0, n); // { dg-warning "\\\[-Wnonnull]" }
+}
+
+int cmp0 (const void *p, const void *q, unsigned n)
+{
+ return __builtin_memcmp (0, q, n); // { dg-warning "\\\[-Wnonnull]" }
+}
+
+int cmp1 (const void *p, const void *q, unsigned n)
+{
+ return __builtin_memcmp (0, q, n); // { dg-warning "\\\[-Wnonnull]" }
+}
+
+int cmp2 (const void *p, const void *q, unsigned n)
+{
+ return __builtin_bcmp (0, q, n); // { dg-warning "\\\[-Wnonnull]" }
+}
+
+int cmp3 (const void *p, const void *q, unsigned n)
+{
+ return __builtin_bcmp (p, 0, n); // { dg-warning "\\\[-Wnonnull]" }
+}
diff --git a/gcc/testsuite/gcc.dg/Wnonnull-3.c b/gcc/testsuite/gcc.dg/Wnonnull-3.c
new file mode 100644
index 0000000..ad016df
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/Wnonnull-3.c
@@ -0,0 +1,71 @@
+/* PR middle-end/80936 - bcmp, bcopy, and bzero not declared nonnull
+ Verify that with optimization, -Wnonnull is issued for calls with
+ non-constant arguments determined to be null.
+ { dg-do compile }
+ { dg-options "-O2 -Wall" } */
+
+#define NOIPA __attribute__ ((noipa))
+
+NOIPA void zero0 (void *p, unsigned n)
+{
+ if (p == 0)
+ __builtin_memset (p, 0, n); // { dg-warning "\\\[-Wnonnull]" }
+}
+
+NOIPA void zero1 (void *p, unsigned n)
+{
+ if (p == 0)
+ __builtin_bzero (p, n); // { dg-warning "\\\[-Wnonnull]" }
+}
+
+NOIPA void copy0 (void *p, const void *q, unsigned n)
+{
+ if (p == 0)
+ __builtin_memcpy (p, q, n); // { dg-warning "\\\[-Wnonnull]" }
+}
+
+NOIPA void copy1 (void *p, const void *q, unsigned n)
+{
+ if (q == 0)
+ __builtin_memcpy (p, q, n); // { dg-warning "\\\[-Wnonnull]" }
+}
+
+NOIPA void copy2 (void *p, const void *q, unsigned n)
+{
+ if (p == 0)
+ __builtin_bcopy (q, p, n); // { dg-warning "\\\[-Wnonnull]" }
+}
+
+NOIPA void copy3 (void *p, const void *q, unsigned n)
+{
+ if (q == 0)
+ __builtin_bcopy (q, p, n); // { dg-warning "\\\[-Wnonnull]" }
+}
+
+NOIPA int cmp0 (const void *p, const void *q, unsigned n)
+{
+ if (p == 0)
+ return __builtin_memcmp (p, q, n); // { dg-warning "\\\[-Wnonnull]" }
+ return 0;
+}
+
+NOIPA int cmp1 (const void *p, const void *q, unsigned n)
+{
+ if (q == 0)
+ return __builtin_memcmp (p, q, n); // { dg-warning "\\\[-Wnonnull]" }
+ return 0;
+}
+
+NOIPA int cmp2 (const void *p, const void *q, unsigned n)
+{
+ if (p == 0)
+ return __builtin_bcmp (p, q, n); // { dg-warning "\\\[-Wnonnull]" }
+ return 0;
+}
+
+NOIPA int cmp3 (const void *p, const void *q, unsigned n)
+{
+ if (q == 0)
+ return __builtin_bcmp (p, q, n); // { dg-warning "\\\[-Wnonnull]" }
+ return 0;
+}
diff --git a/gcc/testsuite/gcc.dg/nonnull-3.c b/gcc/testsuite/gcc.dg/nonnull-3.c
index 6f7bc4f..c52fe2c 100644
--- a/gcc/testsuite/gcc.dg/nonnull-3.c
+++ b/gcc/testsuite/gcc.dg/nonnull-3.c
@@ -9,11 +9,11 @@
void
foo (void *p, char *s)
{
- __builtin_bzero (NULL, 0);
- __builtin_bcopy (NULL, p, 0);
- __builtin_bcopy (p, NULL, 0);
- __builtin_bcmp (NULL, p, 0);
- __builtin_bcmp (p, NULL, 0);
+ __builtin_bzero (NULL, 0); /* { dg-warning "null" "pr80936" } */
+ __builtin_bcopy (NULL, p, 0); /* { dg-warning "null" "pr80936" } */
+ __builtin_bcopy (p, NULL, 0); /* { dg-warning "null" "pr80936" } */
+ __builtin_bcmp (NULL, p, 0); /* { dg-warning "null" "pr80936" } */
+ __builtin_bcmp (p, NULL, 0); /* { dg-warning "null" "pr80936" } */
__builtin_index (NULL, 16); /* { dg-warning "null" "null pointer check" } */
__builtin_rindex (NULL, 16); /* { dg-warning "null" "null pointer check" } */