From 11f78e8d797ec8ebceadbe90c37302a612c5e3e5 Mon Sep 17 00:00:00 2001
From: David Malcolm <dmalcolm@redhat.com>
Date: Fri, 26 Apr 2019 12:33:00 -0400
Subject: [PATCH 201/217] FIXME: impl-4: move diagnostic emission to a
 subroutine

---
 gcc/analyzer/impl-4.cc | 203 ++++++++++++++++++++++++++-----------------------
 1 file changed, 109 insertions(+), 94 deletions(-)

diff --git a/gcc/analyzer/impl-4.cc b/gcc/analyzer/impl-4.cc
index a70824a..22e5769 100644
--- a/gcc/analyzer/impl-4.cc
+++ b/gcc/analyzer/impl-4.cc
@@ -430,6 +430,11 @@ public:
   void add_edge (exploded_node *src, exploded_node *dest,
 		 const superedge *sedge);
 
+  void emit_diagnostic (const exploded_node *enode,
+			const supernode *node, const gimple *stmt,
+			tree var, state_machine::state_t state,
+			pending_diagnostic *d) const;
+
   //private:
   const supergraph &m_sg;
 
@@ -1170,100 +1175,7 @@ public:
     state_machine::state_t current = m_old_smap.get_state (pv);
     if (state == current)
       {
-	/* TODO: generate a path.  */
-	/* TODO: cache the diagnostics, and flush them all at the
-	   end, with one shortest_paths instance, and so finding
-	   the shortest path, rather than just the first path.  */
-	shortest_paths sp (m_eg);
-	auto_vec<const exploded_edge *> exploded_path;
-	sp.get_shortest_path (m_node, &exploded_path);
-	pretty_printer *pp = global_dc->printer->clone ();
-#if 0
-	simple_diagnostic_path emission_path (pp);
-	for (int i = 0; i < exploded_path.length (); i++)
-	  {
-	    if (get_logger ())
-	      log ("EN %i -> EN %i",
-		   exploded_path[i]->m_src->m_index,
-		   exploded_path[i]->m_dest->m_index);
-	    exploded_node *dst_node = exploded_path[i]->m_dest;
-	    if (dst_node->m_ps.m_point.m_kind == PK_BEFORE_STMT)
-	      {
-		emission_path.add_event (dst->m_ps.m_point.get_stmt ()->location,
-					 NULL_TREE, /* FIXME */
-					 0, /* FIXME */
-					 "FIXME");
-	      }
-	  }
-#endif
-
-	checker4_path emission_path;
-	// TODO: build path
-	for (int i = 0; i < exploded_path.length (); i++)
-	  {
-	    const exploded_edge *eedge = exploded_path[i];
-	    if (get_logger ())
-	      log ("EN %i -> EN %i",
-		   eedge->m_src->m_index,
-		   eedge->m_dest->m_index);
-	    const exploded_node *src_node = eedge->m_src;
-	    const program_point &src_point = src_node->m_ps.m_point;
-	    const exploded_node *dst_node = eedge->m_dest;
-	    const program_point &dst_point = dst_node->m_ps.m_point;
-	    switch (dst_point.m_kind)
-	      {
-	      default:
-		break;
-	      case PK_BEFORE_SUPERNODE:
-		if (src_point.m_kind == PK_AFTER_SUPERNODE)
-		  {
-		    if (eedge->m_sedge)
-		      {
-			const gimple *last_stmt
-			  = src_point.m_supernode->get_last_stmt ();
-			emission_path.add_event
-			  (new edge_event (eedge->m_sedge,
-					   (last_stmt
-					    ? last_stmt->location
-					    : UNKNOWN_LOCATION),
-					   src_point.m_supernode->m_fun->decl,
-					   0 /* FIXME: depth */ ));
-			/* TODO: state changes due to edges.  */
-		      }
-		  }
-		// TODO: add function entry events
-		if (dst_point.m_supernode->entry_p ())
-		  {
-		    emission_path.add_event
-		      (new function_entry_event
-		       (dst_point.m_supernode->get_start_location (),
-			dst_point.m_supernode->m_fun->decl,
-			0 /* FIXME: depth */ ));
-		  }
-		break;
-	      case PK_BEFORE_STMT:
-		emission_path.add_event
-		  (new statement_event (dst_point.get_stmt (),
-					dst_point.m_supernode->m_fun->decl,
-					0 /* FIXME: depth */ ));
-		break;
-	      case PK_AFTER_STMT:
-		/* TODO: state changes due to stmts.  */
-		break;
-	      }
-	  }
-	/* TODO: state changes due to stmts.  */
-
-	//m_engine->prune_path (&emission_path, var, state);
-	emission_path.add_final_event (node, stmt, var, state, d);
-	emission_path.prepare_for_emission (d);
-
-	gcc_rich_location rich_loc (stmt->location);
-	rich_loc.set_path (&emission_path);
-
-	auto_cfun sentinel (node->m_fun);
-	d->emit (&rich_loc);
-	delete pp;
+	m_eg.emit_diagnostic (m_node, node, stmt, var, state, d);
       }
     delete d;
   }
@@ -1769,6 +1681,109 @@ exploded_graph::process_node (exploded_node *node)
     }
 }
 
+void
+exploded_graph::emit_diagnostic (const exploded_node *enode,
+				 const supernode *node, const gimple *stmt,
+				 tree var, state_machine::state_t state,
+				 pending_diagnostic *d) const
+{
+  /* TODO: generate a path.  */
+  /* TODO: cache the diagnostics, and flush them all at the
+     end, with one shortest_paths instance, and so finding
+     the shortest path, rather than just the first path.  */
+  shortest_paths sp (*this);
+  auto_vec<const exploded_edge *> exploded_path;
+  sp.get_shortest_path (enode, &exploded_path);
+  pretty_printer *pp = global_dc->printer->clone ();
+#if 0
+  simple_diagnostic_path emission_path (pp);
+  for (int i = 0; i < exploded_path.length (); i++)
+    {
+      if (get_logger ())
+	log ("EN %i -> EN %i",
+	     exploded_path[i]->m_src->m_index,
+	     exploded_path[i]->m_dest->m_index);
+      exploded_node *dst_node = exploded_path[i]->m_dest;
+      if (dst_node->m_ps.m_point.m_kind == PK_BEFORE_STMT)
+	{
+	  emission_path.add_event (dst->m_ps.m_point.get_stmt ()->location,
+				   NULL_TREE, /* FIXME */
+				   0, /* FIXME */
+				   "FIXME");
+	}
+    }
+#endif
+
+  checker4_path emission_path;
+  // TODO: build path
+  for (int i = 0; i < exploded_path.length (); i++)
+    {
+      const exploded_edge *eedge = exploded_path[i];
+      if (get_logger ())
+	log ("EN %i -> EN %i",
+	     eedge->m_src->m_index,
+	     eedge->m_dest->m_index);
+      const exploded_node *src_node = eedge->m_src;
+      const program_point &src_point = src_node->m_ps.m_point;
+      const exploded_node *dst_node = eedge->m_dest;
+      const program_point &dst_point = dst_node->m_ps.m_point;
+      switch (dst_point.m_kind)
+	{
+	default:
+	  break;
+	case PK_BEFORE_SUPERNODE:
+	  if (src_point.m_kind == PK_AFTER_SUPERNODE)
+	    {
+	      if (eedge->m_sedge)
+		{
+		  const gimple *last_stmt
+		    = src_point.m_supernode->get_last_stmt ();
+		  emission_path.add_event
+		    (new edge_event (eedge->m_sedge,
+				     (last_stmt
+				      ? last_stmt->location
+				      : UNKNOWN_LOCATION),
+				     src_point.m_supernode->m_fun->decl,
+				     0 /* FIXME: depth */ ));
+		  /* TODO: state changes due to edges.  */
+		}
+	    }
+	  // TODO: add function entry events
+	  if (dst_point.m_supernode->entry_p ())
+	    {
+	      emission_path.add_event
+		(new function_entry_event
+		 (dst_point.m_supernode->get_start_location (),
+		  dst_point.m_supernode->m_fun->decl,
+		  0 /* FIXME: depth */ ));
+	    }
+	  break;
+	case PK_BEFORE_STMT:
+	  emission_path.add_event
+	    (new statement_event (dst_point.get_stmt (),
+				  dst_point.m_supernode->m_fun->decl,
+				  0 /* FIXME: depth */ ));
+	  break;
+	case PK_AFTER_STMT:
+	  /* TODO: state changes due to stmts.  */
+	  break;
+	}
+    }
+  /* TODO: state changes due to stmts.  */
+
+  //m_engine->prune_path (&emission_path, var, state);
+  emission_path.add_final_event (node, stmt, var, state, d);
+  emission_path.prepare_for_emission (d);
+
+  gcc_rich_location rich_loc (stmt->location);
+  rich_loc.set_path (&emission_path);
+
+  auto_cfun sentinel (node->m_fun);
+  d->emit (&rich_loc);
+  delete pp;
+}
+
+
 ////////////////////////////////////////////////////////////////////////////
 
 /* FIXME.  */
-- 
1.8.5.3

