From 856c52efe2250264dec11f0ae6d7c81a34d4383c Mon Sep 17 00:00:00 2001
From: David Malcolm <dmalcolm@redhat.com>
Date: Tue, 12 Mar 2019 18:54:44 -0400
Subject: [PATCH 099/169] FIXME: use pretty_print within logging

---
 gcc/analyzer/analyzer-logging.cc | 31 +++++++++++++++++++++++--------
 gcc/analyzer/analyzer-logging.h  | 35 +++++++++++++++--------------------
 gcc/analyzer/engine.cc           |  8 +++++---
 gcc/cp/error.c                   |  5 +++++
 gcc/pretty-print.c               | 21 +++++++++++++++++++++
 gcc/pretty-print.h               |  3 +++
 6 files changed, 72 insertions(+), 31 deletions(-)

diff --git a/gcc/analyzer/analyzer-logging.cc b/gcc/analyzer/analyzer-logging.cc
index 15cce36..2c73a670 100644
--- a/gcc/analyzer/analyzer-logging.cc
+++ b/gcc/analyzer/analyzer-logging.cc
@@ -22,6 +22,7 @@ along with GCC; see the file COPYING3.  If not see
 #include "system.h"
 #include "coretypes.h"
 #include "toplev.h" /* for print_version */
+#include "pretty-print.h" /* for print_version */
 
 #include "analyzer-logging.h"
 
@@ -32,14 +33,20 @@ along with GCC; see the file COPYING3.  If not see
 
 logger::logger (FILE *f_out,
 		int, /* flags */
-		int /* verbosity */) :
+		int /* verbosity */,
+		const pretty_printer &reference_pp) :
   m_refcount (0),
   m_f_out (f_out),
   m_indent_level (0),
-  m_log_refcount_changes (false)
+  m_log_refcount_changes (false),
+  m_pp (new pretty_printer (reference_pp))
 {
+  pp_show_color (m_pp) = 0;
+  pp_buffer (m_pp)->stream = f_out;
+
   /* Begin the log by writing the GCC version.  */
   print_version (f_out, "", false);
+
 }
 
 /* The destructor for gcc::jit::logger, invoked via
@@ -51,6 +58,7 @@ logger::~logger ()
   /* This should be the last message emitted.  */
   log ("%s", __PRETTY_FUNCTION__);
   gcc_assert (m_refcount == 0);
+  delete m_pp;
 }
 
 /* Increment the reference count of the gcc::jit::logger.  */
@@ -86,7 +94,7 @@ logger::log (const char *fmt, ...)
 {
   va_list ap;
   va_start (ap, fmt);
-  log_va (fmt, ap);
+  log_va (fmt, &ap);
   va_end (ap);
 }
 
@@ -98,7 +106,7 @@ logger::log (const char *fmt, ...)
    buffer.  */
 
 void
-logger::log_va (const char *fmt, va_list ap)
+logger::log_va (const char *fmt, va_list *ap)
 {
   start_log_line ();
   log_va_partial (fmt, ap);
@@ -117,14 +125,21 @@ logger::log_partial (const char *fmt, ...)
 {
   va_list ap;
   va_start (ap, fmt);
-  log_va_partial (fmt, ap);
+  log_va_partial (fmt, &ap);
   va_end (ap);
 }
 
 void
-logger::log_va_partial (const char *fmt, va_list ap)
+logger::log_va_partial (const char *fmt, va_list *ap)
 {
-  vfprintf (m_f_out, fmt, ap);
+  text_info text;
+  text.format_spec = fmt;
+  text.args_ptr = ap;
+  text.err_no = 0;
+  pp_format (m_pp, &text);
+  pp_output_formatted_text (m_pp);
+  pp_flush (m_pp);
+  pp_clear_output_area (m_pp);
 }
 
 void
@@ -145,7 +160,7 @@ logger::enter_scope (const char *scope_name)
 }
 
 void
-logger::enter_scope (const char *scope_name, const char *fmt, va_list ap)
+logger::enter_scope (const char *scope_name, const char *fmt, va_list *ap)
 {
   start_log_line ();
   log_partial ("entering: %s: ", scope_name);
diff --git a/gcc/analyzer/analyzer-logging.h b/gcc/analyzer/analyzer-logging.h
index c073475..ae594da 100644
--- a/gcc/analyzer/analyzer-logging.h
+++ b/gcc/analyzer/analyzer-logging.h
@@ -23,13 +23,7 @@ along with GCC; see the file COPYING3.  If not see
 #ifndef JIT_LOGGING_H
 #define JIT_LOGGING_H
 
-#ifdef GCC_VERSION
-#if GCC_VERSION >= 4001
-#define GNU_PRINTF(M, N) __attribute__ ((format (gnu_printf, (M), (N))))
-#else
-#define GNU_PRINTF(M, N)
-#endif
-#endif
+#include "diagnostic-core.h"
 
 // FIXME:
 
@@ -39,26 +33,26 @@ along with GCC; see the file COPYING3.  If not see
 class logger
 {
  public:
-  logger (FILE *f_out, int flags, int verbosity);
+  logger (FILE *f_out, int flags, int verbosity, const pretty_printer &reference_pp);
   ~logger ();
 
   void incref (const char *reason);
   void decref (const char *reason);
 
   void log (const char *fmt, ...)
-    GNU_PRINTF(2, 3);
-  void log_va (const char *fmt, va_list ap)
-    GNU_PRINTF(2, 0);
+    ATTRIBUTE_GCC_DIAG(2, 3);
+  void log_va (const char *fmt, va_list *ap)
+    ATTRIBUTE_GCC_DIAG(2, 0);
   void start_log_line ();
   void log_partial (const char *fmt, ...)
-    GNU_PRINTF(2, 3);
-  void log_va_partial (const char *fmt, va_list ap)
-    GNU_PRINTF(2, 0);
+    ATTRIBUTE_GCC_DIAG(2, 3);
+  void log_va_partial (const char *fmt, va_list *ap)
+    ATTRIBUTE_GCC_DIAG(2, 0);
   void end_log_line ();
 
   void enter_scope (const char *scope_name);
-  void enter_scope (const char *scope_name, const char *fmt, va_list ap)
-    GNU_PRINTF(3, 0);
+  void enter_scope (const char *scope_name, const char *fmt, va_list *ap)
+    ATTRIBUTE_GCC_DIAG(3, 0);
   void exit_scope (const char *scope_name);
 
 private:
@@ -66,6 +60,7 @@ private:
   FILE *m_f_out;
   int m_indent_level;
   bool m_log_refcount_changes;
+  pretty_printer *m_pp;
 };
 
 /* The class gcc::jit::log_scope is an RAII-style class intended to make
@@ -77,7 +72,7 @@ class log_scope
 public:
   log_scope (logger *logger, const char *name);
   log_scope (logger *logger, const char *name, const char *fmt, ...)
-    GNU_PRINTF(4, 5);
+    ATTRIBUTE_GCC_DIAG(4, 5);
   ~log_scope ();
 
  private:
@@ -116,7 +111,7 @@ log_scope::log_scope (logger *logger, const char *name, const char *fmt, ...):
       m_logger->incref ("log_scope ctor");
       va_list ap;
       va_start (ap, fmt);
-      m_logger->enter_scope (m_name, fmt, ap);
+      m_logger->enter_scope (m_name, fmt, &ap);
       va_end (ap);
     }
 }
@@ -160,7 +155,7 @@ class log_user
   void set_logger (logger * logger);
 
   void log (const char *fmt, ...) const
-    GNU_PRINTF(2, 3);
+    ATTRIBUTE_GCC_DIAG(2, 3);
 
   void enter_scope (const char *scope_name);
   void exit_scope (const char *scope_name);
@@ -179,7 +174,7 @@ log_user::log (const char *fmt, ...) const
     {
       va_list ap;
       va_start (ap, fmt);
-      m_logger->log_va (fmt, ap);
+      m_logger->log_va (fmt, &ap);
       va_end (ap);
     }
 }
diff --git a/gcc/analyzer/engine.cc b/gcc/analyzer/engine.cc
index dae1465..8d5362c 100644
--- a/gcc/analyzer/engine.cc
+++ b/gcc/analyzer/engine.cc
@@ -2291,7 +2291,8 @@ checker_path *
 engine::expand_call_summary (const call_summary *summary_event,
 			     tree var, state_machine::state_t state)
 {
-  JIT_LOG_FUNC (get_logger ());
+  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 ());
@@ -2301,7 +2302,7 @@ engine::expand_call_summary (const call_summary *summary_event,
   const int caller_stack_depth = summary_event->m_depth;
 
   const supernode *callee_node
-    = m_sg.get_node_for_function_entry (summary_event->m_edge->callee->get_fun ());
+    = m_sg.get_node_for_function_entry (callee_fun);
   const supernode *caller_node
     = summary_event->m_caller_node;
 
@@ -2966,7 +2967,8 @@ run_checkers ()
     if (flag_dump_analyzer)
       {
 	FILE *dump_file = /* 0 ? dump : */stderr; // FIXME
-	the_logger.set_logger (new logger (dump_file, 0, 0));
+	the_logger.set_logger (new logger (dump_file, 0, 0,
+					   *global_dc->printer));
       }
     JIT_LOG_SCOPE (the_logger.get_logger ());
 
diff --git a/gcc/cp/error.c b/gcc/cp/error.c
index 3677213..3c9df67 100644
--- a/gcc/cp/error.c
+++ b/gcc/cp/error.c
@@ -141,6 +141,11 @@ class cxx_format_postprocessor : public format_postprocessor
   : m_type_a (), m_type_b ()
   {}
 
+  format_postprocessor *clone() const FINAL OVERRIDE
+  {
+    return new cxx_format_postprocessor ();
+  }
+
   void handle (pretty_printer *pp) FINAL OVERRIDE;
 
   deferred_printed_type m_type_a;
diff --git a/gcc/pretty-print.c b/gcc/pretty-print.c
index a2d6f04..ee43ca0 100644
--- a/gcc/pretty-print.c
+++ b/gcc/pretty-print.c
@@ -1588,6 +1588,27 @@ pretty_printer::pretty_printer (int maximum_length)
   pp_set_prefix (this, NULL);
 }
 
+pretty_printer::pretty_printer (const pretty_printer &other)
+: buffer (new (XCNEW (output_buffer)) output_buffer ()),
+  prefix (),
+  padding (other.padding),
+  maximum_length (other.maximum_length),
+  indent_skip (other.indent_skip),
+  wrapping (other.wrapping),
+  format_decoder (other.format_decoder),
+  m_format_postprocessor (other.m_format_postprocessor), // FIXME: deep copy
+  emitted_prefix (other.emitted_prefix),
+  need_newline (other.need_newline),
+  translate_identifiers (other.translate_identifiers),
+  show_color (other.show_color),
+  show_urls (other.show_urls)
+{
+  pp_line_cutoff (this) = maximum_length;
+  /* By default, we emit prefixes once per message.  */
+  pp_prefixing_rule (this) = pp_prefixing_rule (&other);
+  pp_set_prefix (this, NULL);
+}
+
 pretty_printer::~pretty_printer ()
 {
   if (m_format_postprocessor)
diff --git a/gcc/pretty-print.h b/gcc/pretty-print.h
index 07cd391..133886c 100644
--- a/gcc/pretty-print.h
+++ b/gcc/pretty-print.h
@@ -191,6 +191,7 @@ class format_postprocessor
 {
  public:
   virtual ~format_postprocessor () {}
+  virtual format_postprocessor *clone() const = 0;
   virtual void handle (pretty_printer *) = 0;
 };
 
@@ -220,6 +221,8 @@ struct pretty_printer
      maximum line length cut off limit.  */
   explicit pretty_printer (int = 0);
 
+  explicit pretty_printer (const pretty_printer &other);
+
   virtual ~pretty_printer ();
 
   /* Where we print external representation of ENTITY.  */
-- 
1.8.5.3

