From 44d13fc2d187f29efafdab4da22fd8031dea48e7 Mon Sep 17 00:00:00 2001
From: David Malcolm <dmalcolm@redhat.com>
Date: Wed, 31 May 2017 16:13:16 -0400
Subject: [PATCH 36/38] FIXME: capture the json within class checker

---
 gcc/checkers.c | 154 ++++++++++++++++++++++++++++++---------------------------
 1 file changed, 81 insertions(+), 73 deletions(-)

diff --git a/gcc/checkers.c b/gcc/checkers.c
index b2e8ce3..d5ed5f51 100644
--- a/gcc/checkers.c
+++ b/gcc/checkers.c
@@ -149,73 +149,6 @@ handle_failure (const firehose::analysis &analysis,
 		 failure.m_message);
 }
 
-/* Given UTF8_BUFFER, a non-NULL UTF-8-encoded buffer of JSON output
-   from a tool, in Firehose JSON format, emit the results through GCC's
-   diagnostic subsystem.
-   This must be run on the main thread (firehose::analysis::from_json can
-   touch the global line_table, and error_at and the other diagnostic
-   emission requires the main thread).   */
-
-static void
-handle_json (const char *utf8_buffer)
-{
-  /* Attempt to parse the buffer as UTF-8-encoded JSON.  */
-  char *err = NULL;
-  json::value *jv = json::parse_utf8_string (utf8_buffer, &err);
-  if (!jv)
-    {
-      gcc_assert (err);
-      error_at (UNKNOWN_LOCATION,
-		"unable to parse tool output as UTF-8 JSON: %s", err);
-      free (err);
-      return;
-    }
-
-  gcc_assert (err == NULL);
-
-  if (0)
-    {
-      jv->dump(stderr);
-      fprintf (stderr, "\n");
-    }
-
-  /* Attempt to parse the JSON values into Firehose objects.  */
-  firehose::analysis analysis;
-  analysis.from_json (jv, err);
-  if (err)
-    {
-      error_at (UNKNOWN_LOCATION, "error parsing JSON output: %qs", err);
-      free (err);
-      delete jv;
-      return;
-    }
-
-  gcc_assert (err == NULL);
-
-  /* Emit the results as GCC diagnostics.  */
-  int i;
-  firehose::result *result;
-  FOR_EACH_VEC_ELT (analysis.m_results, i, result)
-    {
-      switch (result->get_kind ())
-	{
-	case firehose::result::FIREHOSE_ISSUE:
-	  handle_issue (analysis, *static_cast <firehose::issue *> (result));
-	  break;
-	case firehose::result::FIREHOSE_INFO:
-	  handle_info (analysis, *static_cast <firehose::info *> (result));
-	  break;
-	case firehose::result::FIREHOSE_FAILURE:
-	  handle_failure (analysis, *static_cast <firehose::failure *> (result));
-	  break;
-	default:
-	  gcc_unreachable ();
-	}
-    }
-
-  delete jv;
-}
-
 /* FIXME: taken from jit-playback.c.  */
 
 /* A subclass of auto_vec <char *> that frees all of its elements on
@@ -269,14 +202,21 @@ class checker
 
   const char *get_executable () const { return m_executable; }
 
+  const char *get_output () const { return m_utf8_buffer; }
+  const json::value *get_json_output () const { return m_jv; }
+
  private:
   static void *run_checker_thread (void *ptr);
   thread_result *run_in_thread ();
   char *capture_stdout (char *&out_err);
   void make_args (auto_argvec &out) const;
+  void handle_json ();
 
   char *m_executable;
   pthread_t m_tid;
+
+  char *m_utf8_buffer;
+  json::value *m_jv;
 };
 
 /* A policy, listing which checkers to run.  */
@@ -293,7 +233,7 @@ class policy
 
 /* FIXME.  */
 
-checker::checker () : m_executable (NULL)
+checker::checker () : m_executable (NULL), m_utf8_buffer (NULL), m_jv (NULL)
 {
 }
 
@@ -302,6 +242,8 @@ checker::checker () : m_executable (NULL)
 checker::~checker ()
 {
   free (m_executable);
+  free (m_utf8_buffer);
+  delete m_jv;
 }
 
 /* FIXME.  */
@@ -368,7 +310,10 @@ checker::finish ()
       delete result;
       return;
     }
-  handle_json (result->m_utf8_buffer);
+
+  m_utf8_buffer = result->m_utf8_buffer;
+  result->m_utf8_buffer = NULL;
+  handle_json ();
   delete result;
 }
 
@@ -394,16 +339,15 @@ void
 checker::run_single_threaded ()
 {
   char *err = NULL;
-  char *utf8_buffer = capture_stdout (err);
-  if (!utf8_buffer)
+  m_utf8_buffer = capture_stdout (err);
+  if (!m_utf8_buffer)
     {
       error_at (UNKNOWN_LOCATION, "error invoking checker %qs: %qs",
 		m_executable, err);
       free (err);
       return;
     }
-  handle_json (utf8_buffer);
-  free (utf8_buffer);
+  handle_json ();
 }
 
 /* Run the checker, capturing its stdout.
@@ -496,6 +440,70 @@ checker::make_args (auto_argvec &out) const
 #undef ADD_ARG
 }
 
+/* Given UTF8_BUFFER, a non-NULL UTF-8-encoded buffer of JSON output
+   from a tool, in Firehose JSON format, emit the results through GCC's
+   diagnostic subsystem.
+   This must be run on the main thread (firehose::analysis::from_json can
+   touch the global line_table, and error_at and the other diagnostic
+   emission requires the main thread).   */
+
+void
+checker::handle_json ()
+{
+  /* Attempt to parse the buffer as UTF-8-encoded JSON.  */
+  char *err = NULL;
+  m_jv = json::parse_utf8_string (m_utf8_buffer, &err);
+  if (!m_jv)
+    {
+      gcc_assert (err);
+      error_at (UNKNOWN_LOCATION,
+		"unable to parse tool output as UTF-8 JSON: %s", err);
+      free (err);
+      return;
+    }
+
+  gcc_assert (err == NULL);
+
+  if (0)
+    {
+      m_jv->dump(stderr);
+      fprintf (stderr, "\n");
+    }
+
+  /* Attempt to parse the JSON values into Firehose objects.  */
+  firehose::analysis analysis;
+  analysis.from_json (m_jv, err);
+  if (err)
+    {
+      error_at (UNKNOWN_LOCATION, "error parsing JSON output: %qs", err);
+      free (err);
+      return;
+    }
+
+  gcc_assert (err == NULL);
+
+  /* Emit the results as GCC diagnostics.  */
+  int i;
+  firehose::result *result;
+  FOR_EACH_VEC_ELT (analysis.m_results, i, result)
+    {
+      switch (result->get_kind ())
+	{
+	case firehose::result::FIREHOSE_ISSUE:
+	  handle_issue (analysis, *static_cast <firehose::issue *> (result));
+	  break;
+	case firehose::result::FIREHOSE_INFO:
+	  handle_info (analysis, *static_cast <firehose::info *> (result));
+	  break;
+	case firehose::result::FIREHOSE_FAILURE:
+	  handle_failure (analysis, *static_cast <firehose::failure *> (result));
+	  break;
+	default:
+	  gcc_unreachable ();
+	}
+    }
+}
+
 /* FIXME.  */
 
 policy::~policy ()
-- 
1.8.5.3

