From c1caf19a415d09696aa083f5882dbc584a36f78d Mon Sep 17 00:00:00 2001
From: David Malcolm <dmalcolm@redhat.com>
Date: Fri, 19 Feb 2021 17:07:56 -0500
Subject: [PATCH 20/23] FIXME: fix trailing eedges

---
 gcc/analyzer/diagnostic-manager.cc | 14 ++++++++------
 gcc/analyzer/diagnostic-manager.h  | 22 ++++++++++++++--------
 gcc/analyzer/engine.cc             |  8 --------
 3 files changed, 22 insertions(+), 22 deletions(-)

diff --git a/gcc/analyzer/diagnostic-manager.cc b/gcc/analyzer/diagnostic-manager.cc
index f7230b8d3e0..7bd1df64901 100644
--- a/gcc/analyzer/diagnostic-manager.cc
+++ b/gcc/analyzer/diagnostic-manager.cc
@@ -264,18 +264,16 @@ dedupe_set::~dedupe_set ()
   delete m_best_epath;
 }
 
-/* FIXME.  */
+/* Create and take ownership of a new saved_diagnostic instance.
+   Return it.  */
 
-void
+saved_diagnostic *
 dedupe_set::add_diagnostic (const exploded_node *enode,
 			    stmt_finder *finder,
 			    const svalue *sval,
 			    pending_diagnostic *d,
 			    logger *logger)
 {
-  /* FIXME: we take owndership of D here, I think
-     The first one in the dedupe_set is also borrowed for the key.  */
-
   if (logger)
     logger->log ("adding saved diagnostic %i"
 		 " at EN %i to dedupe_set %i (SN %i, %qs)",
@@ -285,6 +283,7 @@ dedupe_set::add_diagnostic (const exploded_node *enode,
 
   saved_diagnostic *sd = new saved_diagnostic (&m_key, enode, finder, sval, d);
   m_saved_diagnostics.safe_push (sd);
+  return sd;
 }
 
 /* Determine the simplest diagnostic within this set, and store it in
@@ -517,7 +516,9 @@ diagnostic_manager::add_diagnostic (const state_machine *sm,
 
   dedupe_set *set
     = get_or_create_dedupe_set (sm, snode, stmt, var, state, d);
-  set->add_diagnostic (enode, finder, sval, d, get_logger ());
+  saved_diagnostic *sd
+    = set->add_diagnostic (enode, finder, sval, d, get_logger ());
+  m_saved_diagnostics.safe_push (sd);
 }
 
 /* FIXME.  */
@@ -841,6 +842,7 @@ diagnostic_manager::emit_saved_diagnostics (const exploded_graph &eg)
   LOG_SCOPE (get_logger ());
   auto_timevar tv (TV_ANALYZER_DIAGNOSTICS);
 
+  // FIXME
 #if 0
   log ("# saved diagnostics: %i", m_saved_diagnostics.length ());
   if (get_logger ())
diff --git a/gcc/analyzer/diagnostic-manager.h b/gcc/analyzer/diagnostic-manager.h
index cc147119b51..202bc2c76eb 100644
--- a/gcc/analyzer/diagnostic-manager.h
+++ b/gcc/analyzer/diagnostic-manager.h
@@ -116,7 +116,7 @@ public:
   unsigned get_epath_length () const { return m_epath_length; }
 
   //private:
-  const dedupe_key *m_dedupe_key; // or simply pass it around as extrinsic state?
+  const dedupe_key *m_dedupe_key;
   const exploded_node *m_enode;
   stmt_finder *m_stmt_finder;
   const svalue *m_sval;
@@ -141,11 +141,12 @@ public:
   dedupe_set (const dedupe_key &key, unsigned index);
   ~dedupe_set ();
 
-  void add_diagnostic (const exploded_node *enode,
-		       stmt_finder *finder,
-		       const svalue *sval,
-		       pending_diagnostic *d,
-		       logger *logger);
+  saved_diagnostic *
+  add_diagnostic (const exploded_node *enode,
+		  stmt_finder *finder,
+		  const svalue *sval,
+		  pending_diagnostic *d,
+		  logger *logger);
   const dedupe_key *get_key () const { return &m_key; }
   unsigned get_index () const { return m_index; }
   const supernode *get_snode () const { return m_key.m_snode; }
@@ -263,7 +264,7 @@ public:
 
   // FIXME: add dump methods
 
-#if 0
+  /* Access to saved_diagnostic instances in insertion order.  */
   unsigned get_num_diagnostics () const
   {
     return m_saved_diagnostics.length ();
@@ -276,7 +277,6 @@ public:
   {
     return m_saved_diagnostics[idx];
   }
-#endif
 
 private:
   dedupe_set *
@@ -330,6 +330,12 @@ private:
      ordering when iterating through them.  */
   auto_delete_vec<dedupe_set> m_dedupe_set_vec;
 
+  /* Borrowed pointers, keeping track of all saved diagnostics,
+     in insertion order.
+     This is needed so that we can record the trailing eedge on all
+     that were created when adding an eedge.  */
+  auto_vec<saved_diagnostic *> m_saved_diagnostics;
+
   const int m_verbosity;
 };
 
diff --git a/gcc/analyzer/engine.cc b/gcc/analyzer/engine.cc
index ce32b17711f..f30ae8b5fb0 100644
--- a/gcc/analyzer/engine.cc
+++ b/gcc/analyzer/engine.cc
@@ -968,7 +968,6 @@ exploded_node::dump_dot (graphviz_out *gv, const dump_args_t &args) const
 	}
     }
 
-#if 0
   /* Dump any saved_diagnostics at this enode.  */
   {
     const diagnostic_manager &dm = args.m_eg.get_diagnostic_manager ();
@@ -983,7 +982,6 @@ exploded_node::dump_dot (graphviz_out *gv, const dump_args_t &args) const
       }
   }
   // FIXME: maybe have the ECs as dot nodes, and have the diagnostics link to them???
-#endif
 
   pp_write_text_as_dot_label_to_stream (pp, /*for_record=*/true);
 
@@ -1437,9 +1435,7 @@ exploded_node::on_longjmp (exploded_graph &eg,
      any that this adds to show where the longjmp is rewinding to.  */
 
   diagnostic_manager *dm = &eg.get_diagnostic_manager ();
-#if 0
   unsigned prev_num_diagnostics = dm->get_num_diagnostics ();
-#endif
 
   new_region_model->on_longjmp (longjmp_call, setjmp_call,
 				setjmp_point.get_stack_depth (), ctxt);
@@ -1489,14 +1485,12 @@ exploded_node::on_longjmp (exploded_graph &eg,
 
 	 Do this by setting m_trailing_eedge on any diagnostics that were
 	 just saved.  */
-#if 0
       unsigned num_diagnostics = dm->get_num_diagnostics ();
       for (unsigned i = prev_num_diagnostics; i < num_diagnostics; i++)
 	{
 	  saved_diagnostic *sd = dm->get_saved_diagnostic (i);
 	  sd->m_trailing_eedge = eedge;
 	}
-#endif
     }
 }
 
@@ -4620,7 +4614,6 @@ private:
 	break;
       }
     gv->end_tdtr ();
-#if 0
     /* Dump any saved_diagnostics at this enode.  */
     {
       const diagnostic_manager &dm = m_eg.get_diagnostic_manager ();
@@ -4631,7 +4624,6 @@ private:
 	    print_saved_diagnostic (gv, sd);
 	}
     }
-#endif
     pp_printf (pp, "</TABLE>");
     pp_printf (pp, "</TD>");
   }
-- 
2.26.2

