From 6ce1dc38671a4360758cd0bcc0ad741b88445920 Mon Sep 17 00:00:00 2001
From: David Malcolm <dmalcolm@redhat.com>
Date: Thu, 28 Mar 2019 13:53:25 -0400
Subject: [PATCH 161/169] FIXME: introduce pending_diagnostic::formatted_print

---
 gcc/analyzer/hardcoded-state-machine-malloc.cc | 27 +++++---------------------
 gcc/analyzer/hardcoded-state-machine.cc        | 25 ++++++++++++++++++++++++
 gcc/analyzer/hardcoded-state-machine.h         |  3 +++
 3 files changed, 33 insertions(+), 22 deletions(-)

diff --git a/gcc/analyzer/hardcoded-state-machine-malloc.cc b/gcc/analyzer/hardcoded-state-machine-malloc.cc
index 1fa4681..092fbce 100644
--- a/gcc/analyzer/hardcoded-state-machine-malloc.cc
+++ b/gcc/analyzer/hardcoded-state-machine-malloc.cc
@@ -91,11 +91,8 @@ public:
   {
     if (new_state == m_sm.m_free)
       {
-	pretty_printer pp;
-	pp_printf (&pp, "first %qs here", "free");
 	m_first_free_event = event_id;
-	return label_text::take (xstrdup (pp_formatted_text (&pp)));
-	// TODO: using appropriate colorization etc
+	return formatted_print ("first %qs here", "free");
       }
     return label_text ();
   }
@@ -106,10 +103,7 @@ public:
      tree expr ATTRIBUTE_UNUSED,
      state_machine::state_t state ATTRIBUTE_UNUSED)
   {
-    pretty_printer pp;
-    pp_printf (&pp, "second %qs here", "free");
-    return label_text::take (xstrdup (pp_formatted_text (&pp)));
-    // TODO: using appropriate colorization etc
+    return formatted_print ("second %qs here", "free");
   }
 
   /* FIXME: maybe also something like this.  */
@@ -157,10 +151,7 @@ public:
      tree expr,
      state_machine::state_t state ATTRIBUTE_UNUSED)
   {
-    pretty_printer *pp = global_dc->printer;
-    pp_printf (pp, "%qE could be NULL", expr);
-    return label_text::take (xstrdup (pp_formatted_text (pp)));
-    // FIXME: warning due to use of 'E' format code; move to a support function?
+    return formatted_print ("%qE could be NULL", expr);
   }
 
 };
@@ -237,12 +228,7 @@ public:
      tree expr ATTRIBUTE_UNUSED,
      state_machine::state_t state ATTRIBUTE_UNUSED)
   {
-    pretty_printer *pp = global_dc->printer->clone ();
-    pp_printf (pp, "%qE leaks here", expr);
-    label_text result = label_text::take (xstrdup (pp_formatted_text (pp)));
-    // FIXME: warning due to use of 'E' format code; move to a support function?
-    delete pp;
-    return result;
+    return formatted_print ("%qE leaks here", expr);
   }
 
 };
@@ -280,10 +266,7 @@ public:
      tree expr ATTRIBUTE_UNUSED,
      state_machine::state_t state ATTRIBUTE_UNUSED)
   {
-    pretty_printer pp;
-    pp_printf (&pp, "call to %qs here", "free");
-    return label_text::take (xstrdup (pp_formatted_text (&pp)));
-    // TODO: using appropriate colorization etc
+    return formatted_print ("call to %qs here", "free");
   }
 };
 
diff --git a/gcc/analyzer/hardcoded-state-machine.cc b/gcc/analyzer/hardcoded-state-machine.cc
index fcfb6d7..fa69b6c 100644
--- a/gcc/analyzer/hardcoded-state-machine.cc
+++ b/gcc/analyzer/hardcoded-state-machine.cc
@@ -110,6 +110,31 @@ state_machine::get_state_name (state_t s) const
 
 ////////////////////////////////////////////////////////////////////////////
 
+label_text
+pending_diagnostic::formatted_print (const char *fmt, ...)
+{
+  pretty_printer *pp = global_dc->printer->clone ();
+
+  text_info ti;
+  rich_location rich_loc (line_table, UNKNOWN_LOCATION);
+  va_list ap;
+  va_start (ap, fmt);
+  ti.format_spec = _(fmt);
+  ti.args_ptr = &ap;
+  ti.err_no = 0;
+  ti.x_data = NULL;
+  ti.m_richloc = &rich_loc;
+  pp_format (pp, &ti);
+  pp_output_formatted_text (pp);
+  va_end (ap);
+
+  label_text result = label_text::take (xstrdup (pp_formatted_text (pp)));
+  delete pp;
+  return result;
+}
+
+////////////////////////////////////////////////////////////////////////////
+
 void
 make_checkers (auto_delete_vec <state_machine> &out, logger *logger)
 {
diff --git a/gcc/analyzer/hardcoded-state-machine.h b/gcc/analyzer/hardcoded-state-machine.h
index f9b9c9d..bdc79e0 100644
--- a/gcc/analyzer/hardcoded-state-machine.h
+++ b/gcc/analyzer/hardcoded-state-machine.h
@@ -163,6 +163,9 @@ class pending_diagnostic
     /* Default no-op implementation.  */
     return label_text ();
   }
+
+  static label_text formatted_print (const char *fmt, ...)
+    ATTRIBUTE_GCC_DIAG(1,2);
 };
 
 #if 0
-- 
1.8.5.3

