From 7113eca94ad4de3b32d5e12836d5d717d2e66911 Mon Sep 17 00:00:00 2001
From: David Malcolm <dmalcolm@redhat.com>
Date: Thu, 7 Mar 2019 11:16:01 -0500
Subject: [PATCH 077/169] FIXME: fix stack depths; add call edge_event

---
 gcc/analyzer/engine.cc | 60 ++++++++++++++++++++++++++++++++++++++++----------
 gcc/diagnostic.c       |  2 +-
 2 files changed, 49 insertions(+), 13 deletions(-)

diff --git a/gcc/analyzer/engine.cc b/gcc/analyzer/engine.cc
index 8916295..3632552 100644
--- a/gcc/analyzer/engine.cc
+++ b/gcc/analyzer/engine.cc
@@ -403,6 +403,15 @@ public:
 
   virtual checker_event *clone () const = 0;
 
+  std::string to_str () const
+  {
+    std::string result ("\"");
+    result += m_desc;
+    result += "\" (depth ";
+    result += std::to_string (m_depth) + ")";
+    return result;
+  }
+
   enum event_kind m_kind;
 };
 
@@ -427,13 +436,14 @@ class state_change_event : public checker_event
 {
 public:
   state_change_event (const supernode *node, const gimple *stmt,
+		      int stack_depth,
 		      const state_machine &sm,
 		      tree var,
 		      state_machine::state_t from,
 		      state_machine::state_t to)
   : checker_event (EK_STATE_CHANGE,
 		   stmt->location, node->m_fun->decl,
-		   0, /*state.get_stack_depth ()*/
+		   stack_depth,
 		   make_desc (var, sm, from, to)),
     m_node (node), m_stmt (stmt), m_sm (sm),
     m_var (var), m_from (from), m_to (to)
@@ -452,7 +462,8 @@ public:
 
   checker_event *clone () const FINAL OVERRIDE
   {
-    return new state_change_event (m_node, m_stmt, m_sm, m_var, m_from, m_to);
+    return new state_change_event (m_node, m_stmt, m_depth,
+				   m_sm, m_var, m_from, m_to);
   }
 
   const supernode *m_node;
@@ -521,7 +532,7 @@ public:
 class checker_path : public diagnostic_path
 {
 public:
-  checker_path () : diagnostic_path () {}
+  checker_path () : diagnostic_path (), m_stack_depth (0) {}
   checker_path (const checker_path &other);
   checker_path& operator= (const checker_path &other);
 
@@ -571,10 +582,12 @@ public:
   }
 
   auto_delete_vec<checker_event> m_events;
+  int m_stack_depth;
 };
 
 checker_path::checker_path (const checker_path &other)
-: m_events ()//other.m_events.length ())
+: m_events (), //other.m_events.length ()),
+  m_stack_depth (other.m_stack_depth)
 {
   checker_event *e;
   int i;
@@ -589,6 +602,8 @@ checker_path::checker_path (const checker_path &other)
 checker_path&
 checker_path::operator= (const checker_path &other)
 {
+  m_stack_depth = other.m_stack_depth;
+
   checker_event *e;
   int i;
   FOR_EACH_VEC_ELT (other.m_events, i, e)
@@ -964,7 +979,8 @@ public:
 	if (m_path)
 	  {
 	    checker_event *event
-	      = new state_change_event (node, stmt, m_engine->m_sm,
+	      = new state_change_event (node, stmt, m_path->m_stack_depth,
+					m_engine->m_sm,
 					var, from, to);
 	    m_path->add_event (event);
 	  }
@@ -1124,9 +1140,11 @@ engine::dfs_traverse (sm_instance sm_inst, backtrace b,
       log ("path_state: %s", ps.to_str ().c_str ());
     }
 
-  b.m_path.add_event (new debug_event (node->get_start_location (),
-				       node->m_fun->decl, 0,/*state.get_stack_depth ()*/
-				       print ("SN: %i", node->m_index)));
+  if (m_verbosity > 0)
+    b.m_path.add_event (new debug_event (node->get_start_location (),
+					 node->m_fun->decl,
+					 b.m_path.m_stack_depth,
+					 print ("SN: %i", node->m_index)));
 
   /* Look up to see if we've visited this node in these states before.  */
   prune_active_vars_to_cache_misses (&sm_inst, node);
@@ -1227,7 +1245,8 @@ engine::dfs_traverse (sm_instance sm_inst, backtrace b,
 	     = new edge_event ((last_stmt
 				? last_stmt->location
 				: UNKNOWN_LOCATION),
-			       node->m_fun->decl, 0,/*state.get_stack_depth ()*/
+			       node->m_fun->decl,
+			       new_b.m_path.m_stack_depth,
 			       // TODO: possibly a higher-level description?
 			       print ("taking edge SN:%i -> SN:%i",
 				      node->m_index, succ->m_dest->m_index));
@@ -1316,7 +1335,7 @@ engine::on_stmt (const supernode *node, const gimple *stmt,
       checker_event *event
 	= new debug_event (stmt->location,
 			   node->m_fun->decl,
-			   0,/*state.get_stack_depth ()*/
+			   path->m_stack_depth,
 			   pp_formatted_text (&pp));
       path->add_event (event);
     }
@@ -1729,6 +1748,17 @@ engine::follow_call (sm_instance sm_inst, backtrace b, path_state ps,
   /* Traverse into the callee.  */
   backtrace b_in_callee;
   b_in_callee.m_path = b.m_path;
+
+  if (m_verbosity > 0)
+    b_in_callee.m_path.add_event
+      (new edge_event (edge->call_stmt->location,
+		       caller_node->m_fun->decl,
+		       b_in_callee.m_path.m_stack_depth,
+		       print ("calling %qE from %qE",
+			      callee_node->m_fun->decl,
+			      caller_node->m_fun->decl)));
+  b_in_callee.m_path.m_stack_depth++;
+
   dfs_traverse (callee_sm_inst, b_in_callee, ps, callee_node,
 		NULL /* FIXME */);
 
@@ -2152,7 +2182,13 @@ engine::maybe_emit (const candidate_diagnostic &cd)
 {
   JIT_LOG_FUNC (get_logger ());
   if (get_logger ())
-    log ("path: %s", cd.m_path.to_str ().c_str ());
+    {
+      log ("path: %s", cd.m_path.to_str ().c_str ());
+      for (int i = 0; i < cd.m_path.m_events.length (); i++)
+	{
+	  log ("path[%i]: %s", i, cd.m_path.m_events[i]->to_str ().c_str ());
+	}
+    }
 
   gcc_rich_location rich_loc (cd.m_stmt->location);
 #if 0
@@ -2178,7 +2214,7 @@ engine::maybe_emit (const candidate_diagnostic &cd)
 
   checker_event *end_of_path
    = new warning_event (cd.m_stmt->location,
-			cd.m_node->m_fun->decl, 0,/*state.get_stack_depth ()*/
+			cd.m_node->m_fun->decl, path.m_stack_depth,
 			msg);
   path.add_event (end_of_path);
   // FIXME: ideally would show the full message here, too?
diff --git a/gcc/diagnostic.c b/gcc/diagnostic.c
index e1de579..12cb3f1 100644
--- a/gcc/diagnostic.c
+++ b/gcc/diagnostic.c
@@ -1830,7 +1830,7 @@ simple_diagnostic_path::add_event (location_t loc, tree fndecl, int depth, const
   diagnostic_event *new_event
     = new diagnostic_event (loc, fndecl, depth, pp_formatted_text (pp));
   m_events.safe_push (new_event);
-#if 0  
+#if 0
   fprintf (stderr, "DBG: added to path %p: %s\n", this, new_event->m_desc);
 #endif
 
-- 
1.8.5.3

