Tighten 'dg-warning' alternatives in 'c-c++-common/Wfree-nonheap-object{,-2,-3}.c' (was: [PATCH] correct -Wmismatched-new-delete (PR 98160, 98166))

Message ID 87wn0f71uq.fsf@euler.schwinge.homeip.net
State Committed
Headers
Series Tighten 'dg-warning' alternatives in 'c-c++-common/Wfree-nonheap-object{,-2,-3}.c' (was: [PATCH] correct -Wmismatched-new-delete (PR 98160, 98166)) |

Checks

Context Check Description
linaro-tcwg-bot/tcwg_gcc_build--master-arm fail Patch failed to apply
linaro-tcwg-bot/tcwg_gcc_check--master-arm fail Patch failed to apply
linaro-tcwg-bot/tcwg_gcc_build--master-aarch64 fail Patch failed to apply

Commit Message

Thomas Schwinge June 7, 2023, 3:01 p.m. UTC
  Hi!

On 2020-12-08T13:46:32-0700, Martin Sebor via Gcc-patches <gcc-patches@gcc.gnu.org> wrote:
> The attached changes [...]

... eventually became commit fe7f75cf16783589eedbab597e6d0b8d35d7e470
"Correct/improve maybe_emit_free_warning (PR middle-end/98166, PR c++/57111, PR middle-end/98160)".

>       * c-c++-common/Wfree-nonheap-object-2.c: New test.
>       * c-c++-common/Wfree-nonheap-object-3.c: New test.
>       * c-c++-common/Wfree-nonheap-object.c: New test.

OK to push the attached
"Tighten 'dg-warning' alternatives in 'c-c++-common/Wfree-nonheap-object{,-2,-3}.c'"?


Grüße
 Thomas


> diff --git a/gcc/testsuite/c-c++-common/Wfree-nonheap-object-2.c b/gcc/testsuite/c-c++-common/Wfree-nonheap-object-2.c
> new file mode 100644
> index 00000000000..0aedf1babbc
> --- /dev/null
> +++ b/gcc/testsuite/c-c++-common/Wfree-nonheap-object-2.c
> @@ -0,0 +1,52 @@
> +/* PR middle-end/98166: bogus -Wmismatched-dealloc on user-defined allocator
> +   and inlining
> +   Verify that the allocator can be declared inline without a warning when
> +   it's associated with a standard deallocator.  Associating an inline
> +   deallocator with an allocator would cause false positives when the former
> +   calls a deallocation function the allocator isn't associated with, so
> +   that triggers a warning on declaration.
> +   { dg-do compile }
> +   { dg-options "-O2 -Wall" } */
> +
> +__attribute__ ((malloc (__builtin_free)))
> +inline int*
> +alloc_int (int n)
> +{
> +  return (int*)__builtin_malloc (n + sizeof (int));
> +}
> +
> +void test_nowarn_int (int n)
> +{
> +  {
> +    int *p = alloc_int (n);
> +    __builtin_free (p);
> +  }
> +
> +  {
> +    int *p = alloc_int (n);
> +    __builtin_free (p + 1);   // { dg-warning "\\\[-Wfree-nonheap-object" }
> +  }
> +}
> +
> +
> +inline void
> +dealloc_long (long *p)
> +{
> +  __builtin_free (p);         // { dg-warning "'__builtin_free|void __builtin_free\\(void\\*\\)' called on pointer 'p|<unknown>' with nonzero offset" }
> +}
> +
> +__attribute__ ((malloc (dealloc_long)))
> +long* alloc_long (int);       // { dg-warning "'malloc \\\(dealloc_long\\\)' attribute ignored with deallocation functions declared 'inline'" }
> +
> +void test_nowarn_long (int n)
> +{
> +  {
> +    long *p = alloc_long (n);
> +    dealloc_long (p);
> +  }
> +
> +  {
> +    long *p = alloc_long (n);
> +    dealloc_long (p + 1);
> +  }
> +}
> diff --git a/gcc/testsuite/c-c++-common/Wfree-nonheap-object-3.c b/gcc/testsuite/c-c++-common/Wfree-nonheap-object-3.c
> new file mode 100644
> index 00000000000..41a5b50362e
> --- /dev/null
> +++ b/gcc/testsuite/c-c++-common/Wfree-nonheap-object-3.c
> @@ -0,0 +1,70 @@
> +/* PR middle-end/98166: bogus -Wmismatched-dealloc on user-defined allocator
> +   and inlining
> +   Verify that without inlining, both the allocator and the deallocator
> +   can be declared inline without a warning and that mismatched calls are
> +   detected, but that declaring them always_inline does trigger a warning.
> +   { dg-do compile }
> +   { dg-options "-Wall" } */
> +
> +__attribute__ ((malloc (__builtin_free)))
> +inline int*
> +alloc_int (int n)
> +{
> +  return (int*)__builtin_malloc (n + sizeof (int));
> +}
> +
> +void test_nowarn_int (int n)
> +{
> +  {
> +    int *p = alloc_int (n);
> +    __builtin_free (p);
> +  }
> +
> +  {
> +    int *p = alloc_int (n);
> +    __builtin_free (p + 1);   // { dg-warning "'__builtin_free|void __builtin_free\\(void\\*\\)' called on pointer 'p|<unknown>' with nonzero offset" }
> +  }
> +}
> +
> +
> +inline void
> +dealloc_long (long *p) { __builtin_free (p); }
> +
> +__attribute__ ((malloc (dealloc_long)))
> +long* alloc_long (int);
> +
> +void test_nowarn_long (int n)
> +{
> +  {
> +    long *p = alloc_long (n);
> +    dealloc_long (p);
> +  }
> +
> +  {
> +    long *p = alloc_long (n);
> +    dealloc_long (p + 1);     // { dg-warning "'dealloc_long' called on pointer 'p|<unknown>' with nonzero offset" }
> +  }
> +}
> +
> +
> +inline __attribute__ ((always_inline)) void
> +dealloc_float (float *p)      // { dg-message "deallocation function declared here" }
> +{
> +  __builtin_free (p);         // { dg-warning "'__builtin_free|void __builtin_free\\(void\\*\\)' called on pointer 'p|<unknown>' with nonzero offset" }
> +}
> +
> +__attribute__ ((malloc (dealloc_float)))
> +float* alloc_float (int);     // { dg-warning "'malloc \\(dealloc_float\\)' attribute ignored with deallocation functions declared 'inline'" }
> +
> +void test_nowarn_float (int n)
> +{
> +  {
> +    float *p = alloc_float (n);
> +    dealloc_float (p);
> +  }
> +
> +  {
> +    float *p = alloc_float (n);
> +    dealloc_float (p + 2);
> +  }
> +}
> diff --git a/gcc/testsuite/c-c++-common/Wfree-nonheap-object.c b/gcc/testsuite/c-c++-common/Wfree-nonheap-object.c
> new file mode 100644
> index 00000000000..dfbb296e9a7
> --- /dev/null
> +++ b/gcc/testsuite/c-c++-common/Wfree-nonheap-object.c
> @@ -0,0 +1,50 @@
> +/* Verify that built-in forms of functions can be used interchangeably
> +   with their ordinary (library) forms in attribute malloc.
> +   { dg-do compile }
> +   { dg-options "-Wall" } */
> +
> +char* f (void) __attribute__ ((malloc (__builtin_free)));
> +
> +#if __cplusplus
> +extern "C" {
> +#endif
> +
> +void free (void*);
> +
> +#if __cplusplus
> +}
> +#endif
> +
> +char* g (void) __attribute__ ((malloc (free)));
> +
> +
> +void test_nowarm (void)
> +{
> +  char *p = f ();
> +  free (p);
> +
> +  p = g ();
> +  free (p);
> +
> +  p = f ();
> +  __builtin_free (p);
> +
> +  p = g ();
> +  __builtin_free (p);
> +}
> +
> +
> +void test_warn (void)
> +{
> +  char *p = f ();
> +  free (p + 1);               // { dg-warning "'free|void free\\(void\\*\\)' called on pointer 'p|<unknown>' with nonzero offset" }
> +
> +  p = g ();
> +  free (p + 2);               // { dg-warning "'free|void free\\(void\\*\\)' called on pointer 'p|<unknown>' with nonzero offset" }
> +
> +  p = f ();
> +  __builtin_free (p + 3);     // { dg-warning "'__builtin_free|void __builtin_free\\(void\\*\\)' called on pointer 'p|<unknown>' with nonzero offset" }
> +
> +  p = g ();
> +  __builtin_free (p + 4);     // { dg-warning "'__builtin_free|void __builtin_free\\(void\\*\\)' called on pointer 'p|<unknown>' with nonzero offset" }
> +}


-----------------
Siemens Electronic Design Automation GmbH; Anschrift: Arnulfstraße 201, 80634 München; Gesellschaft mit beschränkter Haftung; Geschäftsführer: Thomas Heurung, Frank Thürauf; Sitz der Gesellschaft: München; Registergericht München, HRB 106955
  

Comments

Mike Stump June 7, 2023, 5:13 p.m. UTC | #1
On Jun 7, 2023, at 8:01 AM, Thomas Schwinge <thomas@codesourcery.com> wrote:
> On 2020-12-08T13:46:32-0700, Martin Sebor via Gcc-patches <gcc-patches@gcc.gnu.org> wrote:
>> The attached changes [...]
> 
> ... eventually became commit fe7f75cf16783589eedbab597e6d0b8d35d7e470
> "Correct/improve maybe_emit_free_warning (PR middle-end/98166, PR c++/57111, PR middle-end/98160)".
> 
>>      * c-c++-common/Wfree-nonheap-object-2.c: New test.
>>      * c-c++-common/Wfree-nonheap-object-3.c: New test.
>>      * c-c++-common/Wfree-nonheap-object.c: New test.
> 
> OK to push the attached
> "Tighten 'dg-warning' alternatives in 'c-c++-common/Wfree-nonheap-object{,-2,-3}.c'"?

Ok.
  
Thomas Schwinge June 15, 2023, 7:23 a.m. UTC | #2
Hi!

On 2023-06-15T08:50:59+0800, "haochen.jiang via Gcc-patches" <gcc-patches@gcc.gnu.org> wrote:
> On Linux/x86_64,

Actually: generally...

> 9c03391ba447ff86038d6a34c90ae737c3915b5f is the first bad commit
> commit 9c03391ba447ff86038d6a34c90ae737c3915b5f
> Author: Thomas Schwinge <thomas@codesourcery.com>
> Date:   Wed Jun 7 16:24:26 2023 +0200
>
>     Tighten 'dg-warning' alternatives in 'c-c++-common/Wfree-nonheap-object{,-2,-3}.c'
>
> caused
>
> FAIL: c-c++-common/Wfree-nonheap-object-3.c  -std=gnu++14 (test for excess errors)
> FAIL: c-c++-common/Wfree-nonheap-object-3.c  -std=gnu++14  (test for warnings, line 45)
> FAIL: c-c++-common/Wfree-nonheap-object-3.c  -std=gnu++17 (test for excess errors)
> FAIL: c-c++-common/Wfree-nonheap-object-3.c  -std=gnu++17  (test for warnings, line 45)
> FAIL: c-c++-common/Wfree-nonheap-object-3.c  -std=gnu++20 (test for excess errors)
> FAIL: c-c++-common/Wfree-nonheap-object-3.c  -std=gnu++20  (test for warnings, line 45)
> FAIL: c-c++-common/Wfree-nonheap-object-3.c  -std=gnu++98 (test for excess errors)
> FAIL: c-c++-common/Wfree-nonheap-object-3.c  -std=gnu++98  (test for warnings, line 45)

Indeed.  Sorry -- not sure how that escaped my testing.  I already did
have the fix in a different Git commit (but not in my testing build).
Pushed to master branch commit df071fbd467f0cb3711119ef41d74792fc5e6c8c
"Fix 'dg-warning' in 'c-c++-common/Wfree-nonheap-object-3.c' for C++",
see attached.


Grüße
 Thomas


> with GCC configured with
>
> ../../gcc/configure --prefix=/export/users/haochenj/src/gcc-bisect/master/master/r14-1805/usr --enable-clocale=gnu --with-system-zlib --with-demangler-in-ld --with-fpmath=sse --enable-languages=c,c++,fortran --enable-cet --without-isl --enable-libmpx x86_64-linux --disable-bootstrap
>
> To reproduce:
>
> $ cd {build_dir}/gcc && make check RUNTESTFLAGS="dg.exp=c-c++-common/Wfree-nonheap-object-3.c --target_board='unix{-m32}'"
> $ cd {build_dir}/gcc && make check RUNTESTFLAGS="dg.exp=c-c++-common/Wfree-nonheap-object-3.c --target_board='unix{-m32\ -march=cascadelake}'"
> $ cd {build_dir}/gcc && make check RUNTESTFLAGS="dg.exp=c-c++-common/Wfree-nonheap-object-3.c --target_board='unix{-m64}'"
> $ cd {build_dir}/gcc && make check RUNTESTFLAGS="dg.exp=c-c++-common/Wfree-nonheap-object-3.c --target_board='unix{-m64\ -march=cascadelake}'"
>
> (Please do not reply to this email, for question about this report, contact me at haochen dot jiang at intel.com)


-----------------
Siemens Electronic Design Automation GmbH; Anschrift: Arnulfstraße 201, 80634 München; Gesellschaft mit beschränkter Haftung; Geschäftsführer: Thomas Heurung, Frank Thürauf; Sitz der Gesellschaft: München; Registergericht München, HRB 106955
  

Patch

From d9039d3d5b410075e2afa3a5a635f7399f072edd Mon Sep 17 00:00:00 2001
From: Thomas Schwinge <thomas@codesourcery.com>
Date: Wed, 7 Jun 2023 16:24:26 +0200
Subject: [PATCH] Tighten 'dg-warning' alternatives in
 'c-c++-common/Wfree-nonheap-object{,-2,-3}.c'

..., added in commit fe7f75cf16783589eedbab597e6d0b8d35d7e470
"Correct/improve maybe_emit_free_warning (PR middle-end/98166, PR c++/57111, PR middle-end/98160)".

These use alternatives like, for example, "AB|CDE|FG", but what really must've
been meant is "A(B|C)D(E|F)G".  The former variant also does "work": it matches
any of "AB", or "CDE", or "FG", which are components of the latter variant.
(That means, the former variant matches too loosely.)

	gcc/testsuite/
	* c-c++-common/Wfree-nonheap-object-2.c: Tighten 'dg-warning'
	alternatives.
	* c-c++-common/Wfree-nonheap-object-3.c: Likewise.
	* c-c++-common/Wfree-nonheap-object.c: Likewise.
---
 gcc/testsuite/c-c++-common/Wfree-nonheap-object-2.c | 2 +-
 gcc/testsuite/c-c++-common/Wfree-nonheap-object-3.c | 6 +++---
 gcc/testsuite/c-c++-common/Wfree-nonheap-object.c   | 8 ++++----
 3 files changed, 8 insertions(+), 8 deletions(-)

diff --git a/gcc/testsuite/c-c++-common/Wfree-nonheap-object-2.c b/gcc/testsuite/c-c++-common/Wfree-nonheap-object-2.c
index 0aedf1babbc..a2dbd181e33 100644
--- a/gcc/testsuite/c-c++-common/Wfree-nonheap-object-2.c
+++ b/gcc/testsuite/c-c++-common/Wfree-nonheap-object-2.c
@@ -32,7 +32,7 @@  void test_nowarn_int (int n)
 inline void
 dealloc_long (long *p)
 {
-  __builtin_free (p);         // { dg-warning "'__builtin_free|void __builtin_free\\(void\\*\\)' called on pointer 'p|<unknown>' with nonzero offset" }
+  __builtin_free (p);         // { dg-warning "'(__builtin_free|void __builtin_free\\(void\\*\\))' called on pointer '(p|<unknown>)' with nonzero offset" }
 }
 
 __attribute__ ((malloc (dealloc_long)))
diff --git a/gcc/testsuite/c-c++-common/Wfree-nonheap-object-3.c b/gcc/testsuite/c-c++-common/Wfree-nonheap-object-3.c
index 41a5b50362e..8f20de8455f 100644
--- a/gcc/testsuite/c-c++-common/Wfree-nonheap-object-3.c
+++ b/gcc/testsuite/c-c++-common/Wfree-nonheap-object-3.c
@@ -22,7 +22,7 @@  void test_nowarn_int (int n)
 
   {
     int *p = alloc_int (n);
-    __builtin_free (p + 1);   // { dg-warning "'__builtin_free|void __builtin_free\\(void\\*\\)' called on pointer 'p|<unknown>' with nonzero offset" }
+    __builtin_free (p + 1);   // { dg-warning "'(__builtin_free|void __builtin_free\\(void\\*\\))' called on pointer '(p|<unknown>)' with nonzero offset" }
   }
 }
 
@@ -42,7 +42,7 @@  void test_nowarn_long (int n)
 
   {
     long *p = alloc_long (n);
-    dealloc_long (p + 1);     // { dg-warning "'dealloc_long' called on pointer 'p|<unknown>' with nonzero offset" }
+    dealloc_long (p + 1);     // { dg-warning "'dealloc_long' called on pointer '(p|<unknown>)' with nonzero offset" }
   }
 }
 
@@ -50,7 +50,7 @@  void test_nowarn_long (int n)
 inline __attribute__ ((always_inline)) void
 dealloc_float (float *p)      // { dg-message "deallocation function declared here" }
 {
-  __builtin_free (p);         // { dg-warning "'__builtin_free|void __builtin_free\\(void\\*\\)' called on pointer 'p|<unknown>' with nonzero offset" }
+  __builtin_free (p);         // { dg-warning "'(__builtin_free|void __builtin_free\\(void\\*\\))' called on pointer '(p|<unknown>)' with nonzero offset" }
 }
 
 __attribute__ ((malloc (dealloc_float)))
diff --git a/gcc/testsuite/c-c++-common/Wfree-nonheap-object.c b/gcc/testsuite/c-c++-common/Wfree-nonheap-object.c
index dfbb296e9a7..5f1e3fb28f3 100644
--- a/gcc/testsuite/c-c++-common/Wfree-nonheap-object.c
+++ b/gcc/testsuite/c-c++-common/Wfree-nonheap-object.c
@@ -37,14 +37,14 @@  void test_nowarm (void)
 void test_warn (void)
 {
   char *p = f ();
-  free (p + 1);               // { dg-warning "'free|void free\\(void\\*\\)' called on pointer 'p|<unknown>' with nonzero offset" }
+  free (p + 1);               // { dg-warning "'(free|void free\\(void\\*\\))' called on pointer '(p|<unknown>)' with nonzero offset" }
 
   p = g ();
-  free (p + 2);               // { dg-warning "'free|void free\\(void\\*\\)' called on pointer 'p|<unknown>' with nonzero offset" }
+  free (p + 2);               // { dg-warning "'(free|void free\\(void\\*\\))' called on pointer '(p|<unknown>)' with nonzero offset" }
 
   p = f ();
-  __builtin_free (p + 3);     // { dg-warning "'__builtin_free|void __builtin_free\\(void\\*\\)' called on pointer 'p|<unknown>' with nonzero offset" }
+  __builtin_free (p + 3);     // { dg-warning "'(__builtin_free|void __builtin_free\\(void\\*\\))' called on pointer '(p|<unknown>)' with nonzero offset" }
 
   p = g ();
-  __builtin_free (p + 4);     // { dg-warning "'__builtin_free|void __builtin_free\\(void\\*\\)' called on pointer 'p|<unknown>' with nonzero offset" }
+  __builtin_free (p + 4);     // { dg-warning "'(__builtin_free|void __builtin_free\\(void\\*\\))' called on pointer '(p|<unknown>)' with nonzero offset" }
 }
-- 
2.34.1