From cf6a95c296ee95296508e1b8492874f39b6b9d54 Mon Sep 17 00:00:00 2001
From: David Malcolm <dmalcolm@redhat.com>
Date: Tue, 12 Mar 2019 19:02:04 -0400
Subject: [PATCH 100/161] FIXME: logging tweaks

---
 gcc/analyzer/analyzer-logging.cc |  2 +-
 gcc/analyzer/analyzer-logging.h  |  6 ++++
 gcc/analyzer/engine.cc           | 66 ++++++++++++++++------------------------
 gcc/c-family/c-pretty-print.c    |  7 +++++
 gcc/c-family/c-pretty-print.h    |  1 +
 gcc/cp/cxx-pretty-print.c        |  8 +++++
 gcc/cp/cxx-pretty-print.h        |  2 ++
 gcc/pretty-print.c               |  8 +++++
 gcc/pretty-print.h               |  3 +-
 9 files changed, 61 insertions(+), 42 deletions(-)

diff --git a/gcc/analyzer/analyzer-logging.cc b/gcc/analyzer/analyzer-logging.cc
index 2c73a670..c616ad4 100644
--- a/gcc/analyzer/analyzer-logging.cc
+++ b/gcc/analyzer/analyzer-logging.cc
@@ -39,7 +39,7 @@ logger::logger (FILE *f_out,
   m_f_out (f_out),
   m_indent_level (0),
   m_log_refcount_changes (false),
-  m_pp (new pretty_printer (reference_pp))
+  m_pp (reference_pp.clone ())
 {
   pp_show_color (m_pp) = 0;
   pp_buffer (m_pp)->stream = f_out;
diff --git a/gcc/analyzer/analyzer-logging.h b/gcc/analyzer/analyzer-logging.h
index ae594da..e09ed8e8 100644
--- a/gcc/analyzer/analyzer-logging.h
+++ b/gcc/analyzer/analyzer-logging.h
@@ -219,5 +219,11 @@ log_user::exit_scope (const char *scope_name)
 #define JIT_LOG_FUNC_2(LOGGER, FMT, A0, A1)		\
   log_scope s (LOGGER, __func__, FMT, A0, A1)
 
+#define JIT_LOG_FUNC_3(LOGGER, FMT, A0, A1, A2)	\
+  log_scope s (LOGGER, __func__, FMT, A0, A1, A2)
+
+#define JIT_LOG_FUNC_4(LOGGER, FMT, A0, A1, A2, A3) \
+  log_scope s (LOGGER, __func__, FMT, A0, A1, A2, A3)
+
 
 #endif /* JIT_LOGGING_H */
diff --git a/gcc/analyzer/engine.cc b/gcc/analyzer/engine.cc
index 8d5362c..2c0f7e7 100644
--- a/gcc/analyzer/engine.cc
+++ b/gcc/analyzer/engine.cc
@@ -42,11 +42,6 @@
 #include "tristate.h"
 #include "selftest.h"
 
-static const bool DEBUG = false;
-//static const bool DEBUG = true;
-
-static int verbosity = 2;
-
 ////////////////////////////////////////////////////////////////////////////
 
 class sm_instance;
@@ -642,6 +637,16 @@ public:
     return result + "]";
   }
 
+  void maybe_log (logger *logger, const char *desc) const
+  {
+    if (!logger)
+      return;
+    logger->log ("%s: %s", desc, to_str ().c_str ());
+    for (int i = 0; i < m_events.length (); i++)
+      logger->log ("%s[%i]: %s",
+		   desc, i, m_events[i]->to_str ().c_str ());
+  }
+
   unsigned num_events () const FINAL OVERRIDE
   {
     return m_events.length ();
@@ -2068,7 +2073,9 @@ engine::follow_call (sm_instance sm_inst, backtrace b, path_state ps,
     checker_event *event
       = new call_summary (caller_node, edge,
 			  0,/*int depth,*/
-			  "FIXME: call summary (using cache)");
+			  print ("call summary: call to %qD from %qD",
+				 edge->callee->get_fun ()->decl,
+				 caller_node->m_fun->decl));
     new_b.m_path.add_event (event);
 
     sm_instance restored_sm
@@ -2292,20 +2299,18 @@ engine::expand_call_summary (const call_summary *summary_event,
 			     tree var, state_machine::state_t state)
 {
   function *callee_fun = summary_event->m_edge->callee->get_fun ();
-  JIT_LOG_FUNC_1 (get_logger (), "%qD", callee_fun->decl);
-  if (get_logger ())
-    {
-      log ("var: %s", expr_to_string (var).c_str ());
-      log ("state: %s", m_sm.get_state_name (state));
-    }
-
-  const int caller_stack_depth = summary_event->m_depth;
-
   const supernode *callee_node
     = m_sg.get_node_for_function_entry (callee_fun);
   const supernode *caller_node
     = summary_event->m_caller_node;
 
+  JIT_LOG_FUNC_4 (get_logger (), "%qD called from %qD: var: %qE state: %qs",
+		  callee_fun->decl, caller_node->m_fun->decl,
+		  var, m_sm.get_state_name (state));
+
+  const int caller_stack_depth = summary_event->m_depth;
+
+
   checker_path *result = new checker_path ();
 
   // TODO:
@@ -2433,6 +2438,9 @@ engine::expand_call_summary (const call_summary *summary_event,
 				     print ("returning to %qE from %qE",
 					    caller_node->m_fun->decl,
 					    callee_node->m_fun->decl)));
+
+  result->maybe_log (get_logger (), "result");
+
   return result;
 }
 
@@ -2483,14 +2491,7 @@ void
 engine::prune_path (checker_path *path, tree var, state_machine::state_t state)
 {
   JIT_LOG_FUNC (get_logger ());
-  if (get_logger ())
-    {
-      log ("path: %s", path->to_str ().c_str ());
-      for (int i = 0; i < path->m_events.length (); i++)
-	{
-	  log ("path[%i]: %s", i, path->m_events[i]->to_str ().c_str ());
-	}
-    }
+  path->maybe_log (get_logger (), "path");
 
   // TODO
   /* TODO: go backwards, skipping state change events that aren't var
@@ -2615,14 +2616,7 @@ void
 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 ());
-      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 ());
-	}
-    }
+  cd.m_path.maybe_log (get_logger (), "path");
 
   gcc_rich_location rich_loc (cd.m_stmt->location);
 #if 0
@@ -2654,15 +2648,7 @@ engine::maybe_emit (const candidate_diagnostic &cd)
   // FIXME: ideally would show the full message here, too?
 
 
-  if (get_logger ())
-    {
-      log ("path for emission: %s", expanded_path->to_str ().c_str ());
-      for (int i = 0; i < expanded_path->m_events.length (); i++)
-	{
-	  log ("path[%i]: %s", i,
-	       expanded_path->m_events[i]->to_str ().c_str ());
-	}
-    }
+  expanded_path->maybe_log (get_logger (), "path for emission");
 
   rich_loc.set_path (expanded_path);
 
diff --git a/gcc/c-family/c-pretty-print.c b/gcc/c-family/c-pretty-print.c
index 46db469..909562d 100644
--- a/gcc/c-family/c-pretty-print.c
+++ b/gcc/c-family/c-pretty-print.c
@@ -2358,6 +2358,13 @@ c_pretty_printer::c_pretty_printer ()
   parameter_list            = pp_c_parameter_type_list;
 }
 
+/* FIXME.  */
+
+pretty_printer *
+c_pretty_printer::clone () const
+{
+  return new c_pretty_printer (*this);
+}
 
 /* Print the tree T in full, on file FILE.  */
 
diff --git a/gcc/c-family/c-pretty-print.h b/gcc/c-family/c-pretty-print.h
index 6084dcf..1100b76 100644
--- a/gcc/c-family/c-pretty-print.h
+++ b/gcc/c-family/c-pretty-print.h
@@ -50,6 +50,7 @@ typedef void (*c_pretty_print_fn) (c_pretty_printer *, tree);
 struct c_pretty_printer : pretty_printer
 {
   c_pretty_printer ();
+  pretty_printer *clone () const OVERRIDE;
 
   // Format string, possibly translated.
   void translate_string (const char *);
diff --git a/gcc/cp/cxx-pretty-print.c b/gcc/cp/cxx-pretty-print.c
index 47eebd1..26fa9e1 100644
--- a/gcc/cp/cxx-pretty-print.c
+++ b/gcc/cp/cxx-pretty-print.c
@@ -3030,3 +3030,11 @@ cxx_pretty_printer::cxx_pretty_printer ()
   type_specifier_seq = (pp_fun) pp_cxx_type_specifier_seq;
   parameter_list = (pp_fun) pp_cxx_parameter_declaration_clause;
 }
+
+/* FIXME.  */
+
+pretty_printer *
+cxx_pretty_printer::clone () const
+{
+  return new cxx_pretty_printer (*this);
+}
diff --git a/gcc/cp/cxx-pretty-print.h b/gcc/cp/cxx-pretty-print.h
index aba3e84..742ac4a 100644
--- a/gcc/cp/cxx-pretty-print.h
+++ b/gcc/cp/cxx-pretty-print.h
@@ -33,6 +33,8 @@ struct cxx_pretty_printer : c_pretty_printer
 {
   cxx_pretty_printer ();
 
+  pretty_printer *clone () const OVERRIDE;
+
   void constant (tree);
   void id_expression (tree);
   void primary_expression (tree);
diff --git a/gcc/pretty-print.c b/gcc/pretty-print.c
index ee43ca0..2436b8a 100644
--- a/gcc/pretty-print.c
+++ b/gcc/pretty-print.c
@@ -1618,6 +1618,14 @@ pretty_printer::~pretty_printer ()
   free (prefix);
 }
 
+/* FIXME.  */
+
+pretty_printer *
+pretty_printer::clone () const
+{
+  return new pretty_printer (*this);
+}
+
 /* Append a string delimited by START and END to the output area of
    PRETTY-PRINTER.  No line wrapping is done.  However, if beginning a
    new line then emit PRETTY-PRINTER's prefix and skip any leading
diff --git a/gcc/pretty-print.h b/gcc/pretty-print.h
index 133886c..9a8b81a 100644
--- a/gcc/pretty-print.h
+++ b/gcc/pretty-print.h
@@ -220,11 +220,12 @@ struct pretty_printer
   /* Default construct a pretty printer with specified
      maximum line length cut off limit.  */
   explicit pretty_printer (int = 0);
-
   explicit pretty_printer (const pretty_printer &other);
 
   virtual ~pretty_printer ();
 
+  virtual pretty_printer *clone () const;
+
   /* Where we print external representation of ENTITY.  */
   output_buffer *buffer;
 
-- 
1.8.5.3

