From 677611c7f94ad8b5f146d5a5c347f6fc4291bb98 Mon Sep 17 00:00:00 2001
From: David Malcolm <dmalcolm@redhat.com>
Date: Wed, 23 Sep 2020 10:00:31 -0400
Subject: [PATCH 36/49] FIXME: simplify event creation by using improved
 function_point::get_location

---
 gcc/analyzer/checker-path.cc       | 39 +++++++++++------
 gcc/analyzer/checker-path.h        | 51 +++++++++++-----------
 gcc/analyzer/diagnostic-manager.cc | 68 +++++-------------------------
 gcc/analyzer/engine.cc             |  2 +
 4 files changed, 64 insertions(+), 96 deletions(-)

diff --git a/gcc/analyzer/checker-path.cc b/gcc/analyzer/checker-path.cc
index 3e4266a6aae..adc35fbb09c 100644
--- a/gcc/analyzer/checker-path.cc
+++ b/gcc/analyzer/checker-path.cc
@@ -174,9 +174,10 @@ custom_event::get_desc (bool) const
 
 /* statement_event's ctor.  */
 
-statement_event::statement_event (const gimple *stmt, tree fndecl, int depth,
+statement_event::statement_event (const gimple *stmt,
+				  const program_point &point,
 				  const program_state &dst_state)
-: checker_event (EK_STMT, gimple_location (stmt), fndecl, depth),
+: checker_event (EK_STMT, point),
   m_stmt (stmt),
   m_dst_state (dst_state)
 {
@@ -367,8 +368,8 @@ superedge_event::should_filter_p (int verbosity) const
 
 superedge_event::superedge_event (enum event_kind kind,
 				  const exploded_edge &eedge,
-				  location_t loc, tree fndecl, int depth)
-: checker_event (kind, loc, fndecl, depth),
+				  const program_point &point)
+: checker_event (kind, point),
   m_eedge (eedge), m_sedge (eedge.m_sedge),
   m_var (NULL_TREE), m_critical_state (0)
 {
@@ -388,14 +389,21 @@ cfg_edge_event::get_cfg_superedge () const
 
 cfg_edge_event::cfg_edge_event (enum event_kind kind,
 				const exploded_edge &eedge,
-				location_t loc, tree fndecl, int depth)
-: superedge_event (kind, eedge, loc, fndecl, depth)
+				const program_point &point)
+: superedge_event (kind, eedge, point)
 {
   gcc_assert (eedge.m_sedge->m_kind == SUPEREDGE_CFG_EDGE);
 }
 
 /* class start_cfg_edge_event : public cfg_edge_event.  */
 
+/* start_cfg_edge_event's ctor.  */
+
+start_cfg_edge_event::start_cfg_edge_event (const exploded_edge &eedge)
+: cfg_edge_event (EK_START_CFG_EDGE, eedge, eedge.m_src->get_point ())
+{
+}
+
 /* Implementation of diagnostic_event::get_desc vfunc for
    start_cfg_edge_event.
 
@@ -602,13 +610,21 @@ start_cfg_edge_event::should_print_expr_p (tree expr)
   return false;
 }
 
+/* class end_cfg_edge_event : public cfg_edge_event.  */
+
+/* end_cfg_edge_event's ctor.  */
+
+end_cfg_edge_event::end_cfg_edge_event (const exploded_edge &eedge)
+: cfg_edge_event (EK_END_CFG_EDGE, eedge, eedge.m_dest->get_point ())
+{
+}
+
 /* class call_event : public superedge_event.  */
 
 /* call_event's ctor.  */
 
-call_event::call_event (const exploded_edge &eedge,
-			location_t loc, tree fndecl, int depth)
-: superedge_event (EK_CALL_EDGE, eedge, loc, fndecl, depth)
+call_event::call_event (const exploded_edge &eedge)
+: superedge_event (EK_CALL_EDGE, eedge, eedge.m_src->get_point ())
 {
   gcc_assert (eedge.m_sedge->m_kind == SUPEREDGE_CALL);
 }
@@ -659,9 +675,8 @@ call_event::is_call_p () const
 
 /* return_event's ctor.  */
 
-return_event::return_event (const exploded_edge &eedge,
-			    location_t loc, tree fndecl, int depth)
-: superedge_event (EK_RETURN_EDGE, eedge, loc, fndecl, depth)
+return_event::return_event (const exploded_edge &eedge)
+: superedge_event (EK_RETURN_EDGE, eedge, eedge.m_dest->get_point ())
 {
   gcc_assert (eedge.m_sedge->m_kind == SUPEREDGE_RETURN);
 }
diff --git a/gcc/analyzer/checker-path.h b/gcc/analyzer/checker-path.h
index 93b9f20f075..119c4741de7 100644
--- a/gcc/analyzer/checker-path.h
+++ b/gcc/analyzer/checker-path.h
@@ -75,13 +75,6 @@ extern const char *event_kind_to_string (enum event_kind ek);
 class checker_event : public diagnostic_event
 {
 public:
-  checker_event (enum event_kind kind,
-		 location_t loc, tree fndecl, int depth)
-    : m_kind (kind), m_loc (loc), m_fndecl (fndecl), m_depth (depth),
-      m_pending_diagnostic (NULL), m_emission_id ()
-  {
-  }
-
   /* Implementation of diagnostic_event.  */
 
   location_t get_location () const FINAL OVERRIDE { return m_loc; }
@@ -104,6 +97,20 @@ public:
  public:
   const enum event_kind m_kind;
  protected:
+  checker_event (enum event_kind kind,
+		 location_t loc, tree fndecl, int depth)
+  : m_kind (kind), m_loc (loc), m_fndecl (fndecl), m_depth (depth),
+    m_pending_diagnostic (NULL), m_emission_id ()
+  {
+  }
+
+  checker_event (enum event_kind kind, const program_point &point)
+  : m_kind (kind), m_loc (point.get_location ()),
+    m_fndecl (point.get_fndecl ()), m_depth (point.get_stack_depth ()),
+    m_pending_diagnostic (NULL), m_emission_id ()
+  {
+  }
+
   location_t m_loc;
   tree m_fndecl;
   int m_depth;
@@ -163,7 +170,7 @@ private:
 class statement_event : public checker_event
 {
 public:
-  statement_event (const gimple *stmt, tree fndecl, int depth,
+  statement_event (const gimple *stmt, const program_point &point,
 		   const program_state &dst_state);
 
   label_text get_desc (bool) const FINAL OVERRIDE;
@@ -240,7 +247,7 @@ public:
 
  protected:
   superedge_event (enum event_kind kind, const exploded_edge &eedge,
-		   location_t loc, tree fndecl, int depth);
+		   const program_point &point);
 
  public:
   const exploded_edge &m_eedge;
@@ -260,7 +267,7 @@ public:
 
  protected:
   cfg_edge_event (enum event_kind kind, const exploded_edge &eedge,
-		  location_t loc, tree fndecl, int depth);
+		  const program_point &point);
 };
 
 /* A concrete event subclass for the start of a CFG edge
@@ -269,11 +276,7 @@ public:
 class start_cfg_edge_event : public cfg_edge_event
 {
 public:
-  start_cfg_edge_event (const exploded_edge &eedge,
-			location_t loc, tree fndecl, int depth)
-  : cfg_edge_event (EK_START_CFG_EDGE, eedge, loc, fndecl, depth)
-  {
-  }
+  start_cfg_edge_event (const exploded_edge &eedge);
 
   label_text get_desc (bool can_colorize) const FINAL OVERRIDE;
 
@@ -293,11 +296,7 @@ public:
 class end_cfg_edge_event : public cfg_edge_event
 {
 public:
-  end_cfg_edge_event (const exploded_edge &eedge,
-		      location_t loc, tree fndecl, int depth)
-  : cfg_edge_event (EK_END_CFG_EDGE, eedge, loc, fndecl, depth)
-  {
-  }
+  end_cfg_edge_event (const exploded_edge &eedge);
 
   label_text get_desc (bool /*can_colorize*/) const FINAL OVERRIDE
   {
@@ -310,8 +309,7 @@ public:
 class call_event : public superedge_event
 {
 public:
-  call_event (const exploded_edge &eedge,
-	      location_t loc, tree fndecl, int depth);
+  call_event (const exploded_edge &eedge);
 
   label_text get_desc (bool can_colorize) const FINAL OVERRIDE;
 
@@ -323,8 +321,7 @@ public:
 class return_event : public superedge_event
 {
 public:
-  return_event (const exploded_edge &eedge,
-		location_t loc, tree fndecl, int depth);
+  return_event (const exploded_edge &eedge);
 
   label_text get_desc (bool can_colorize) const FINAL OVERRIDE;
 
@@ -336,9 +333,9 @@ public:
 class setjmp_event : public checker_event
 {
 public:
-  setjmp_event (location_t loc, const exploded_node *enode,
-		tree fndecl, int depth, const gcall *setjmp_call)
-  : checker_event (EK_SETJMP, loc, fndecl, depth),
+  setjmp_event (const gcall *setjmp_call, const exploded_node *enode,
+		const program_point &point)
+  : checker_event (EK_SETJMP, point),
     m_enode (enode), m_setjmp_call (setjmp_call)
   {
   }
diff --git a/gcc/analyzer/diagnostic-manager.cc b/gcc/analyzer/diagnostic-manager.cc
index a3cbb97d6dc..3d913c13757 100644
--- a/gcc/analyzer/diagnostic-manager.cc
+++ b/gcc/analyzer/diagnostic-manager.cc
@@ -1021,17 +1021,10 @@ diagnostic_manager::add_events_for_eedge (const path_builder &pb,
 	const gimple *stmt = dst_point.get_stmt ();
 	const gcall *call = dyn_cast <const gcall *> (stmt);
 	if (call && is_setjmp_call_p (call))
-	  emission_path->add_event
-	    (new setjmp_event (stmt->location,
-			       dst_node,
-			       dst_point.get_fndecl (),
-			       dst_stack_depth,
-			       call));
+	  emission_path->add_event (new setjmp_event (call, dst_node, dst_point));
 	else
-	  emission_path->add_event
-	    (new statement_event (stmt,
-				  dst_point.get_fndecl (),
-				  dst_stack_depth, dst_state));
+	  emission_path->add_event (new statement_event (stmt, dst_point,
+							 dst_state));
 
 	/* Create state change events for assignment to NULL.
 	   Iterate through the stmts in dst_enode, adding state change
@@ -1180,53 +1173,26 @@ diagnostic_manager::add_events_for_superedge (const path_builder &pb,
     if (!significant_edge_p (pb, eedge))
       return;
 
-  const exploded_node *src_node = eedge.m_src;
-  const program_point &src_point = src_node->get_point ();
-  const exploded_node *dst_node = eedge.m_dest;
-  const program_point &dst_point = dst_node->get_point ();
-  const int src_stack_depth = src_point.get_stack_depth ();
-  const int dst_stack_depth = dst_point.get_stack_depth ();
-  const gimple *last_stmt = src_point.get_supernode ()->get_last_stmt ();
-
   switch (eedge.m_sedge->m_kind)
     {
     case SUPEREDGE_CFG_EDGE:
-      {
-	emission_path->add_event
-	  (new start_cfg_edge_event (eedge,
-			       (last_stmt
-				? last_stmt->location
-				: UNKNOWN_LOCATION),
-			       src_point.get_fndecl (),
-			       src_stack_depth));
-	emission_path->add_event
-	  (new end_cfg_edge_event (eedge,
-				   dst_point.get_supernode ()->get_start_location (),
-				   dst_point.get_fndecl (),
-				   dst_stack_depth));
-      }
+      emission_path->add_event (new start_cfg_edge_event (eedge));
+      emission_path->add_event (new end_cfg_edge_event (eedge));
       break;
 
     case SUPEREDGE_CALL:
-      {
-	emission_path->add_event
-	  (new call_event (eedge,
-			   (last_stmt
-			    ? last_stmt->location
-			    : UNKNOWN_LOCATION),
-			   src_point.get_fndecl (),
-			   src_stack_depth));
-      }
+      emission_path->add_event (new call_event (eedge));
       break;
 
     case SUPEREDGE_INTRAPROCEDURAL_CALL:
       {
 	/* TODO: add a subclass for this, or generate events for the
 	   summary.  */
+	const exploded_node *src_node = eedge.m_src;
+	const program_point &src_point = src_node->get_point ();
+	const int src_stack_depth = src_point.get_stack_depth ();
 	emission_path->add_event
-	  (new debug_event ((last_stmt
-			     ? last_stmt->location
-			     : UNKNOWN_LOCATION),
+	  (new debug_event (src_point.get_location (),
 			    src_point.get_fndecl (),
 			    src_stack_depth,
 			    "call summary"));
@@ -1234,19 +1200,7 @@ diagnostic_manager::add_events_for_superedge (const path_builder &pb,
       break;
 
     case SUPEREDGE_RETURN:
-      {
-	const return_superedge *return_edge
-	  = as_a <const return_superedge *> (eedge.m_sedge);
-
-	const gcall *call_stmt = return_edge->get_call_stmt ();
-	emission_path->add_event
-	  (new return_event (eedge,
-			     (call_stmt
-			      ? call_stmt->location
-			      : UNKNOWN_LOCATION),
-			     dst_point.get_fndecl (),
-			     dst_stack_depth));
-      }
+      emission_path->add_event (new return_event (eedge));
       break;
     }
 }
diff --git a/gcc/analyzer/engine.cc b/gcc/analyzer/engine.cc
index cd22640d685..0202a99fd49 100644
--- a/gcc/analyzer/engine.cc
+++ b/gcc/analyzer/engine.cc
@@ -1497,6 +1497,8 @@ rewind_info_t::add_events_to_path (checker_path *emission_path,
   const program_point &dst_point = dst_node->get_point ();
   const int dst_stack_depth = dst_point.get_stack_depth ();
 
+  /* This needs to use the location of the longjmp call, rather
+     than that of its enode.  */
   emission_path->add_event
     (new rewind_from_longjmp_event
      (&eedge, get_longjmp_call ()->location,
-- 
2.26.2

