From dec60616aa025b4cdf571aba98fb535f944a72b1 Mon Sep 17 00:00:00 2001
From: David Malcolm <dmalcolm@redhat.com>
Date: Tue, 5 Mar 2019 16:30:01 -0500
Subject: [PATCH 056/217] FIXME: capture backtrace in engine_sm_context

---
 gcc/analyzer/engine.cc | 54 +++++++++++++++++++++++++++++---------------------
 1 file changed, 31 insertions(+), 23 deletions(-)

diff --git a/gcc/analyzer/engine.cc b/gcc/analyzer/engine.cc
index 57e795e..81f10dd 100644
--- a/gcc/analyzer/engine.cc
+++ b/gcc/analyzer/engine.cc
@@ -417,14 +417,17 @@ public:
 		const backtrace &b, diagnostic_path *path, path_state *ps);
   void on_assignment (const supernode *node, const gimple *stmt,
 		      sm_instance *sm_inst, const sm_instance *old_sm_inst,
-		      tree lhs, tree rhs, path_state *ps);
+		      tree lhs, tree rhs, const backtrace &b,
+		      path_state *ps);
 
   void relax (backtrace b);
 
   sm_instance refine_for_call (sm_instance sm_inst, cgraph_edge *edge,
-			       const supernode *caller_node);
+			       const supernode *caller_node,
+			       const backtrace &b);
   sm_instance restore_after_call (sm_instance sm_inst, cgraph_edge *edge,
-				  const supernode *rest_of_caller_node);
+				  const supernode *rest_of_caller_node,
+				  const backtrace &b);
   void follow_call (sm_instance sm_inst, backtrace b, path_state ps,
 		    const supernode *caller_node, cgraph_edge *edge);
 
@@ -559,10 +562,11 @@ engine::prune_active_vars_to_cache_misses (sm_instance *sm_inst,
 class engine_sm_context : public sm_context
 {
 public:
-  engine_sm_context (engine *e, sm_instance *sm_inst,
+  engine_sm_context (engine *e, const backtrace &b, sm_instance *sm_inst,
 		     const sm_instance *old_sm_inst, diagnostic_path *path,
 		     path_state *ps)
-    : m_engine (e), m_sm_inst (sm_inst), m_old_sm_inst (old_sm_inst),
+    : m_engine (e), m_backtrace (b),
+      m_sm_inst (sm_inst), m_old_sm_inst (old_sm_inst),
       m_path (path), m_ps (ps) {}
 
   void on_transition (const supernode *node, const gimple *stmt,
@@ -671,6 +675,7 @@ public:
 
 private:
   engine *m_engine;
+  const backtrace &m_backtrace;
   sm_instance *m_sm_inst;
   const sm_instance *m_old_sm_inst;
   diagnostic_path *m_path;
@@ -858,7 +863,7 @@ engine::dfs_traverse (sm_instance sm_inst, backtrace b,
 	      writes into the path each edge has its own copy.  */
 	   backtrace new_b (b);
 	   sm_instance per_edge_new_sm_inst = sm_inst;
-	   engine_sm_context ctxt (this,
+	   engine_sm_context ctxt (this, new_b,
 				   &per_edge_new_sm_inst,
 				   &sm_inst,
 				   &new_b.m_path, &new_ps);
@@ -913,7 +918,7 @@ engine::on_phi (const supernode *node, const gphi *phi,
   size_t index = last_cfg_superedge->m_cfg_edge->dest_idx;
   tree src = gimple_phi_arg_def (const_cast <gphi *> (phi), index); // FIXME const_cast
   tree lhs = gimple_phi_result (phi);
-  on_assignment (node, phi, sm_inst, old_sm_inst, lhs, src, ps);
+  on_assignment (node, phi, sm_inst, old_sm_inst, lhs, src, b, ps);
 }
 
 void
@@ -931,7 +936,7 @@ engine::on_stmt (const supernode *node, const gimple *stmt,
       log ("stmt: %s", pp_formatted_text (&pp));
       log ("sm_inst: %s", sm_inst->to_str (m_sm).c_str ());
     }
-  engine_sm_context ctxt (this, sm_inst, old_sm_inst, path, ps);
+  engine_sm_context ctxt (this, b, sm_inst, old_sm_inst, path, ps);
   m_sm.on_stmt (&ctxt, node, stmt);
   m_on_stmt_calls++;
 
@@ -950,7 +955,8 @@ engine::on_stmt (const supernode *node, const gimple *stmt,
 	case INTEGER_CST:
 	  {
 	    /* Direct assignment of the form LHS = RHS;  */
-	    on_assignment (node, assign, sm_inst, old_sm_inst, lhs, rhs1, ps);
+	    on_assignment (node, assign, sm_inst, old_sm_inst, lhs, rhs1,
+			   b, ps);
 	  }
 	  break;
 
@@ -989,8 +995,8 @@ engine::on_stmt (const supernode *node, const gimple *stmt,
 		if ((*iter).m_var == TREE_OPERAND (rhs1, 0))
 		  {
 		    log ("got match for COMPONENT_REF");
-		    engine_sm_context ctxt (this, sm_inst, old_sm_inst, NULL /* FIXME */,
-					    ps);
+		    engine_sm_context ctxt (this, b, sm_inst, old_sm_inst,
+					    NULL /* FIXME */, ps);
 		    // FIXME: we want to overwrite any old states
 		    // (but respect the gstate, I think):
 		    ctxt.on_transition (node, stmt, lhs, 0/* FIXME */, (*iter).m_state);
@@ -1006,7 +1012,7 @@ engine::on_stmt (const supernode *node, const gimple *stmt,
       tree lhs = DECL_RESULT (callee);
       tree rhs = gimple_return_retval (return_);
       if (rhs)
-	on_assignment (node, return_, sm_inst, old_sm_inst, lhs, rhs, ps);
+	on_assignment (node, return_, sm_inst, old_sm_inst, lhs, rhs, b, ps);
     }
 
   if (const gcall *call = dyn_cast <const gcall *> (stmt))
@@ -1059,7 +1065,7 @@ engine::on_stmt (const supernode *node, const gimple *stmt,
 void
 engine::on_assignment (const supernode *node, const gimple *stmt,
 		       sm_instance *sm_inst, const sm_instance *old_sm_inst,
-		       tree lhs, tree rhs, path_state *ps)
+		       tree lhs, tree rhs, const backtrace &b, path_state *ps)
 {
   JIT_LOG_SCOPE (get_logger ());
   if (get_logger ())
@@ -1085,8 +1091,8 @@ engine::on_assignment (const supernode *node, const gimple *stmt,
       if ((*iter).m_var == rhs)
 	{
 	  log ("got match");
-	  engine_sm_context ctxt (this, sm_inst, old_sm_inst, NULL /* FIXME */,
-				  ps);
+	  engine_sm_context ctxt (this, b, sm_inst, old_sm_inst,
+				  NULL /* FIXME */, ps);
 	  // FIXME: we want to overwrite any old states
 	  // (but respect the gstate, I think):
 	  ctxt.on_transition (node, stmt, lhs, 0/* FIXME */, (*iter).m_state);
@@ -1177,7 +1183,7 @@ engine::relax (backtrace b)
 
 sm_instance
 engine::refine_for_call (sm_instance sm_inst, cgraph_edge *edge,
-			 const supernode *caller_node)
+			 const supernode *caller_node, const backtrace &b)
 {
   JIT_LOG_SCOPE (get_logger ());
   if (get_logger ())
@@ -1210,7 +1216,7 @@ engine::refine_for_call (sm_instance sm_inst, cgraph_edge *edge,
       if (parm_default_ssa)
 	on_assignment (caller_node, call,
 		       &sm_inst_for_call, &sm_inst /* FIXME.  */,
-		       parm_default_ssa, arg, &ps);
+		       parm_default_ssa, arg, b, &ps);
 #if 0
       /* Use: parm, arg, next_sm_inst, to map from args in caller
 	 to parms in callee */
@@ -1236,7 +1242,8 @@ engine::refine_for_call (sm_instance sm_inst, cgraph_edge *edge,
 
 sm_instance
 engine::restore_after_call (sm_instance sm_inst_in_callee, cgraph_edge *edge,
-			    const supernode *rest_of_caller_node)
+			    const supernode *rest_of_caller_node,
+			    const backtrace &b)
 {
   JIT_LOG_SCOPE (get_logger ());
   if (get_logger ())
@@ -1258,7 +1265,7 @@ engine::restore_after_call (sm_instance sm_inst_in_callee, cgraph_edge *edge,
       path_state ps; // FIXME
       on_assignment (rest_of_caller_node, call_stmt,
 		     &sm_inst_in_caller, &sm_inst_in_callee /* FIXME.  */,
-		     lhs, result, &ps);
+		     lhs, result, b, &ps);
     }
 
   // TODO: map formal params back to actual args:
@@ -1294,7 +1301,7 @@ engine::restore_after_call (sm_instance sm_inst_in_callee, cgraph_edge *edge,
 	  if (var == parm)
 	    {
 	      log ("got active var for parm %i", i);
-	      engine_sm_context ctxt (this, &sm_inst_in_caller,
+	      engine_sm_context ctxt (this, b, &sm_inst_in_caller,
 				      &sm_inst_in_callee,
 				      NULL /* FIXME */, &ps);
 	      // FIXME: we want to overwrite any old states
@@ -1326,7 +1333,7 @@ engine::follow_call (sm_instance sm_inst, backtrace b, path_state ps,
 
   /* TODO: refine extension state to the callee's scope.  */
 
-  sm_instance callee_sm_inst = refine_for_call (sm_inst, edge, caller_node);
+  sm_instance callee_sm_inst = refine_for_call (sm_inst, edge, caller_node, b);
 
   const supernode *callee_node
     = m_sg.get_node_for_function_entry (edge->callee->get_fun ());
@@ -1411,7 +1418,8 @@ engine::follow_call (sm_instance sm_inst, backtrace b, path_state ps,
   {
     // TODO: make this a loop:
     sm_instance restored_sm
-      = restore_after_call (callee_summary_sm_inst, edge, rest_of_caller_node);
+      = restore_after_call (callee_summary_sm_inst, edge,
+			    rest_of_caller_node, b);
     log ("rest of call is in SN: %i", rest_of_caller_node->m_index);
     dfs_traverse (restored_sm, b, ps, rest_of_caller_node,
 		  NULL /* FIXME */);
@@ -1591,7 +1599,7 @@ engine::detect_leaks (const supernode *node, const sm_instance &sm_inst,
       // FIXME: various redundant copies here:
       diagnostic_path p (b.m_path);
       sm_instance new_sm_inst (sm_inst);
-      engine_sm_context ctxt (this,
+      engine_sm_context ctxt (this, b,
 			      &new_sm_inst,
 			      &sm_inst,
 			      &p, &new_ps);
-- 
1.8.5.3

