From 6dc51e4683604cea768ac2a10f57ba6b67bf9b71 Mon Sep 17 00:00:00 2001
From: David Malcolm <dmalcolm@redhat.com>
Date: Wed, 27 Jan 2021 16:23:15 -0500
Subject: [PATCH 14/23] FIXME: add feasibility_debugger

---
 gcc/analyzer/diagnostic-manager.cc | 57 +++++++++++++++++++++++++++++-
 gcc/analyzer/engine.cc             |  7 +++-
 gcc/analyzer/exploded-graph.h      | 15 ++++++--
 3 files changed, 75 insertions(+), 4 deletions(-)

diff --git a/gcc/analyzer/diagnostic-manager.cc b/gcc/analyzer/diagnostic-manager.cc
index cbb76d89e0d..3ed198898ce 100644
--- a/gcc/analyzer/diagnostic-manager.cc
+++ b/gcc/analyzer/diagnostic-manager.cc
@@ -416,6 +416,59 @@ public:
   static const bool empty_zero_p = true;
 };
 
+/* FIXME: a way to debug the states within exploded_path::feasible_p.  */
+
+class FIXME_feasibility_debugger : public feasibility_debugger
+{
+public:
+  FIXME_feasibility_debugger ()
+  : m_event_pp (global_dc->printer->clone ()),
+    m_diag_path (m_event_pp)
+  {
+  }
+
+  ~FIXME_feasibility_debugger ()
+  {
+    delete m_event_pp;
+  }
+
+  void on_stmt (const gimple *stmt, const region_model &model) FINAL OVERRIDE
+  {
+    {
+      pretty_printer pp;
+      pp_format_decoder (&pp) = default_tree_printer;
+      model.dump_to_pp (&pp, true, false);
+      m_diag_path.add_event (stmt->location, NULL, 0,
+			     "model: %s", pp_formatted_text (&pp));
+    }
+    {
+      pretty_printer pp;
+      pp_format_decoder (&pp) = default_tree_printer;
+      pp_gimple_stmt_1 (&pp, stmt, 0, (dump_flags_t)0);
+      m_diag_path.add_event (stmt->location, NULL, 0,
+			     "stmt: %s", pp_formatted_text (&pp));
+    }
+  }
+
+  void dump (const feasibility_problem *p)
+  {
+    if (p)
+      {
+	pretty_printer pp;
+	pp_format_decoder (&pp) = default_tree_printer;
+	p->dump_to_pp (&pp);
+	m_diag_path.add_event (p->m_last_stmt->location, NULL, 0,
+			       "problem: %s", pp_formatted_text (&pp));
+      }
+    gcc_rich_location richloc (UNKNOWN_LOCATION); // FIXME
+    richloc.set_path (&m_diag_path);
+    inform (&richloc, "feasibility");
+  }
+
+  pretty_printer *m_event_pp;
+  simple_diagnostic_path m_diag_path;
+};
+
 /* A class for deduplicating diagnostics and finding (and emitting) the
    best diagnostic within each partition.  */
 
@@ -464,7 +517,8 @@ public:
 		   sd->m_snode->m_index);
 
     feasibility_problem *p = NULL;
-    if (dc->get_path ().feasible_p (logger, &p, m_engine, eg))
+    FIXME_feasibility_debugger dbg;
+    if (dc->get_path ().feasible_p (logger, &p, m_engine, eg, &dbg))
       {
 	if (logger)
 	  logger->log ("accepting %qs at EN: %i, SN: %i with feasible path",
@@ -481,6 +535,7 @@ public:
 			   " due to infeasible path",
 			   sd->m_d->get_kind (), sd->m_enode->m_index,
 			   sd->m_snode->m_index);
+	    dbg.dump (p);
 	    sd->set_infeasible (p);
 	    delete dc;
 	    return;
diff --git a/gcc/analyzer/engine.cc b/gcc/analyzer/engine.cc
index d3752a1a6d0..2ca9268f4fc 100644
--- a/gcc/analyzer/engine.cc
+++ b/gcc/analyzer/engine.cc
@@ -3443,10 +3443,12 @@ exploded_path::get_final_enode () const
 /* Check state along this path, returning true if it is feasible.
    If OUT is non-NULL, and the path is infeasible, write a new
    feasibility_problem to *OUT.  */
+// FIXME
 
 bool
 exploded_path::feasible_p (logger *logger, feasibility_problem **out,
-			    engine *eng, const exploded_graph *eg) const
+			   engine *eng, const exploded_graph *eg,
+			   feasibility_debugger *debugger) const
 {
   LOG_SCOPE (logger);
 
@@ -3483,6 +3485,9 @@ exploded_path::feasible_p (logger *logger, feasibility_problem **out,
 	  auto_cfun sentinel (src_point.get_function ());
 	  input_location = stmt->location;
 
+	  if (debugger)
+	    debugger->on_stmt (stmt, model);
+
 	  if (const gassign *assign = dyn_cast <const gassign *> (stmt))
 	    model.on_assignment (assign, NULL);
 	  else if (const greturn *return_ = dyn_cast <const greturn *> (stmt))
diff --git a/gcc/analyzer/exploded-graph.h b/gcc/analyzer/exploded-graph.h
index 894529269fc..94a09b6806a 100644
--- a/gcc/analyzer/exploded-graph.h
+++ b/gcc/analyzer/exploded-graph.h
@@ -860,6 +860,16 @@ private:
   hash_set<function *> m_functions_with_enodes;
 };
 
+/* FIXME.  */
+
+class feasibility_debugger
+{
+public:
+  virtual ~feasibility_debugger () {}
+
+  virtual void on_stmt (const gimple *stmt, const region_model &model) = 0;
+};
+
 /* A path within an exploded_graph: a sequence of edges.  */
 
 class exploded_path
@@ -880,9 +890,10 @@ public:
   void dump (FILE *fp) const;
   void dump () const;
 
-  bool feasible_p (logger *logger, feasibility_problem **out) const;
+  //bool feasible_p (logger *logger, feasibility_problem **out) const;
   bool feasible_p (logger *logger, feasibility_problem **out,
-		    engine *eng, const exploded_graph *eg) const;
+		   engine *eng, const exploded_graph *eg,
+		   feasibility_debugger *debugger) const;
 
   auto_vec<const exploded_edge *> m_edges;
 };
-- 
2.26.2

