The following extends the equality compare dangling pointer diagnostics
suppression for uses following free or realloc to also cover those
following invalidation of auto variables via CLOBBERs.  That avoids
diagnosing idioms like

  return std::find(std::begin(candidates), std::end(candidates), s)
           != std::end(candidates);

for auto candidates which are prone to forwarding of the final
comparison across the storage invalidation as then seen by the
late run access warning pass.

Bootstrapped and tested on x86_64-unknown-linux-gnu.

OK for trunk?

Thanks,
Richard.

2022-04-25  Richard Biener  <rguent...@suse.de>

        PR middle-end/104492
        * gimple-ssa-warn-access.cc
        (pass_waccess::warn_invalid_pointer): Exclude equality compare
        diagnostics for all kind of invalidations.

        * c-c++-common/Wdangling-pointer.c: Adjust for changed
        suppression.
        * c-c++-common/Wdangling-pointer-2.c: Likewise.
---
 gcc/gimple-ssa-warn-access.cc                    | 14 +++++---------
 gcc/testsuite/c-c++-common/Wdangling-pointer-2.c |  4 ++--
 gcc/testsuite/c-c++-common/Wdangling-pointer.c   |  4 ++--
 3 files changed, 9 insertions(+), 13 deletions(-)

diff --git a/gcc/gimple-ssa-warn-access.cc b/gcc/gimple-ssa-warn-access.cc
index 879dbcc1e52..6c404f18db7 100644
--- a/gcc/gimple-ssa-warn-access.cc
+++ b/gcc/gimple-ssa-warn-access.cc
@@ -3896,13 +3896,13 @@ pass_waccess::warn_invalid_pointer (tree ref, gimple 
*use_stmt,
        return;
     }
 
+  if ((equality && warn_use_after_free < 3)
+      || (maybe && warn_use_after_free < 2)
+      || warning_suppressed_p (use_stmt, OPT_Wuse_after_free))
+    return;
+
   if (is_gimple_call (inval_stmt))
     {
-      if ((equality && warn_use_after_free < 3)
-         || (maybe && warn_use_after_free < 2)
-         || warning_suppressed_p (use_stmt, OPT_Wuse_after_free))
-       return;
-
       const tree inval_decl = gimple_call_fndecl (inval_stmt);
 
       if ((ref && warning_at (use_loc, OPT_Wuse_after_free,
@@ -3923,10 +3923,6 @@ pass_waccess::warn_invalid_pointer (tree ref, gimple 
*use_stmt,
       return;
     }
 
-  if ((maybe && warn_dangling_pointer < 2)
-      || warning_suppressed_p (use_stmt, OPT_Wdangling_pointer_))
-    return;
-
   if (DECL_NAME (var))
     {
       if ((ref
diff --git a/gcc/testsuite/c-c++-common/Wdangling-pointer-2.c 
b/gcc/testsuite/c-c++-common/Wdangling-pointer-2.c
index 20f11b227d6..11c939cb086 100644
--- a/gcc/testsuite/c-c++-common/Wdangling-pointer-2.c
+++ b/gcc/testsuite/c-c++-common/Wdangling-pointer-2.c
@@ -356,7 +356,7 @@ NOIPA void warn_cond_if_else (int i)
     }
   else
    {
-     int b[] = { 3, 4 };      // { dg-message "'b' declared" "pr??????" { 
xfail *-*-* } }
+     int b[] = { 3, 4 };      // { dg-message "'b' declared" }
      sink (b);
      p = b;
    }
@@ -365,7 +365,7 @@ NOIPA void warn_cond_if_else (int i)
      because after the first diagnostic the code suppresses subsequent
      ones for the same use.  This needs to be fixed.  */
   sink (p);                   // { dg-warning "dangling pointer 'p' to 'a' may 
be used" }
-                              // { dg-warning "dangling pointer 'p' to 'b' may 
be used" "pr??????" { xfail *-*-* } .-1 }
+                              // { dg-warning "dangling pointer 'p' to 'b' may 
be used" "" { target *-*-* } .-1 }
 }
 
 
diff --git a/gcc/testsuite/c-c++-common/Wdangling-pointer.c 
b/gcc/testsuite/c-c++-common/Wdangling-pointer.c
index 0a18c3c8249..09e4036a4dd 100644
--- a/gcc/testsuite/c-c++-common/Wdangling-pointer.c
+++ b/gcc/testsuite/c-c++-common/Wdangling-pointer.c
@@ -370,7 +370,7 @@ void warn_cond_if_else (int i)
     }
   else
    {
-     int b[] = { 3, 4 };      // { dg-message "'b' declared" "note" { xfail 
*-*-* } }
+     int b[] = { 3, 4 };      // { dg-message "'b' declared" "note" }
      sink (b);
      p = b;
    }
@@ -379,7 +379,7 @@ void warn_cond_if_else (int i)
      because after the first diagnostic the code suppresses subsequent
      ones for the same use.  This needs to be fixed.  */
   sink (p);                   // { dg-warning "dangling pointer 'p' to 'a' may 
be used" }
-                              // { dg-warning "dangling pointer 'p' to 'b' may 
be used" "pr??????" { xfail *-*-* } .-1 }
+                              // { dg-warning "dangling pointer 'p' to 'b' may 
be used" "" { target *-*-* } .-1 }
 }
 
 
-- 
2.34.1

Reply via email to