From ea336e5c35623171815c74a6d3a5b339066f081a Mon Sep 17 00:00:00 2001
From: David Malcolm <dmalcolm@redhat.com>
Date: Thu, 7 May 2020 10:41:15 -0400
Subject: [PATCH 108/179] FIXME: more WIP on leak detection

---
 gcc/analyzer/engine.cc        |  5 ++++-
 gcc/analyzer/program-state.cc | 23 ++++++++++++++++-------
 gcc/analyzer/program-state.h  |  3 ++-
 gcc/analyzer/region-model2.cc |  3 ++-
 4 files changed, 24 insertions(+), 10 deletions(-)

diff --git a/gcc/analyzer/engine.cc b/gcc/analyzer/engine.cc
index be7a3411557..a23120c4c9f 100644
--- a/gcc/analyzer/engine.cc
+++ b/gcc/analyzer/engine.cc
@@ -3052,8 +3052,11 @@ exploded_graph::process_node (exploded_node *node)
 	    if (flags.m_terminate_path)
 	      return;
 
+	    impl_region_model2_context ctxt (*this, node,
+					     &old_state, &next_state, &change,
+					     stmt);
 	    program_state::detect_leaks2 (old_state, next_state,
-					  get_ext_state ());
+					  get_ext_state (), &ctxt);
 
 	    unsigned next_idx = stmt_idx + 1;
 	    program_point next_point
diff --git a/gcc/analyzer/program-state.cc b/gcc/analyzer/program-state.cc
index 2cee8a16c79..15014b73757 100644
--- a/gcc/analyzer/program-state.cc
+++ b/gcc/analyzer/program-state.cc
@@ -1618,6 +1618,7 @@ program_state::prune_for_point2 (exploded_graph &eg,
   const state_purge_map *pm = eg.get_purge_map ();
   if (pm)
     {
+      unsigned num_ssas_purged = 0;
       auto_vec<const decl_region2 *> ssa_name_regs;
       new_state.m_region_model2->get_ssa_name_regions_for_current_frame
 	(&ssa_name_regs);
@@ -1632,6 +1633,7 @@ program_state::prune_for_point2 (exploded_graph &eg,
 	    {
 	      // TODO: see logic below on sm-state
 	      new_state.m_region_model2->purge_region (reg);
+	      num_ssas_purged++;
 	      // TODO: update stats
 	      if (logger)
 		logger->log ("purging region: TODO");
@@ -1711,14 +1713,20 @@ program_state::prune_for_point2 (exploded_graph &eg,
 					       &stats,
 					       eg.get_logger ());
 #endif
+      /* FIXME.  Do this here rather than at add_edge, to cover the
+	 fine-grained cases.  */
+      if (num_ssas_purged > 0)
+	{
+	  // TODO: which enode to use, if any?
+	  impl_region_model2_context ctxt (eg, NULL,
+					   this,
+					   &new_state,
+					   change,
+					   point.get_stmt ());
+	  detect_leaks2 (*this, new_state, eg.get_ext_state (), &ctxt);
+	}
     }
 
-  /* FIXME.  Do this here rather than at add_edge, to cover the
-     fine-grained cases.  */
-  // FIXME: maybe only do this if anything was pruned
-#if 1
-  detect_leaks2 (*this, new_state, eg.get_ext_state ());
-#endif
 
   /* Purge unused svalues.  */
   /* FIXME: this makes no sense in the revamped model.
@@ -1914,7 +1922,8 @@ log_set_of_svalue2s (logger *logger, const char *name,
 void
 program_state::detect_leaks2 (const program_state &src_state,
 			      const program_state &dest_state,
-			      const extrinsic_state &ext_state)
+			      const extrinsic_state &ext_state,
+			      region_model2_context *ctxt)
 {
   logger *logger = ext_state.get_logger ();
   LOG_SCOPE (logger);
diff --git a/gcc/analyzer/program-state.h b/gcc/analyzer/program-state.h
index a8e1562d419..22bbd9f075f 100644
--- a/gcc/analyzer/program-state.h
+++ b/gcc/analyzer/program-state.h
@@ -413,7 +413,8 @@ public:
 
   static void detect_leaks2 (const program_state &src_state,
 			     const program_state &dest_state,
-			     const extrinsic_state &ext_state);
+			     const extrinsic_state &ext_state,
+			     region_model2_context *ctxt);
 
   revamp_state *m_revamp_state;
   region_model2 *m_region_model2;
diff --git a/gcc/analyzer/region-model2.cc b/gcc/analyzer/region-model2.cc
index 8431c4a09be..91efadb15e3 100644
--- a/gcc/analyzer/region-model2.cc
+++ b/gcc/analyzer/region-model2.cc
@@ -4230,7 +4230,8 @@ region_model2::on_call_post (const gcall *call,
 
 /* Helper class for region_model2::handle_unrecognized_call, for keeping
    track of all region2s that are reachable, and, of those, which are
-   mutable.  */
+   mutable.
+   FIXME: also for detecting leaks.  */
 
 class reachable_region2s
 {
-- 
2.21.0

