From 7158c2ae70b6fe7d6602ed6b905cf64ccab335c1 Mon Sep 17 00:00:00 2001
From: David Malcolm <dmalcolm@redhat.com>
Date: Mon, 14 May 2018 15:25:14 -0400
Subject: [PATCH 35/83] FIXME: support building up a pending_info over several
 statements; implement optinfo_printf

---
 gcc/optinfo.cc | 30 ++++++++++++------------------
 gcc/optinfo.h  | 38 ++++++++++++++++++++++++++++----------
 2 files changed, 40 insertions(+), 28 deletions(-)

diff --git a/gcc/optinfo.cc b/gcc/optinfo.cc
index 535f682..d0b51ff 100644
--- a/gcc/optinfo.cc
+++ b/gcc/optinfo.cc
@@ -76,6 +76,9 @@ optinfo_location::optinfo_location (struct loop *loop)
 
 optinfo::~optinfo ()
 {
+  /* Flush the pending information.  */
+  emit ();
+
   /* Cleanup.  */
   unsigned i;
   optinfo_item *item;
@@ -135,19 +138,8 @@ optinfo::emit () const
 pending_optinfo::pending_optinfo (const location &impl_location,
 				  enum opt_record_kind kind,
 				  optinfo_location loc)
+  : m_optinfo (new optinfo (impl_location, kind, loc))
 {
-  m_optinfo = new optinfo (impl_location, kind, loc);
-}
-
-/* pending_optinfo's dtor.  Emit the pending information, and clean it up.  */
-
-pending_optinfo::~pending_optinfo ()
-{
-  /* Flush the pending information.  */
-  m_optinfo->emit ();
-
-  /* Cleanup.  */
-  delete m_optinfo;
 }
 
 /* FIXME.  */
@@ -155,7 +147,9 @@ pending_optinfo::~pending_optinfo ()
 pending_optinfo&
 pending_optinfo::operator<< (const char *msg)
 {
-  m_optinfo->m_items.safe_push (new optinfo_item_text (msg));
+  optinfo_item *item
+    = new optinfo_item_text (const_cast <char *> (msg), false);
+  m_optinfo->m_items.safe_push (item);
   return *this;
 }
 
@@ -180,11 +174,11 @@ pending_optinfo::operator<< (gimple *stmt)
 /* FIXME.  */
 
 pending_optinfo&
-pending_optinfo::operator<< (optinfo_printf item)
+pending_optinfo::operator<< (const optinfo_printf &p)
 {
-  // FIXME
-#if 0
-  m_optinfo->m_items.safe_push (new optinfo_item_text (stmt));
-#endif
+  // FIXME: some double-copies going on here
+  optinfo_item *item
+    = new optinfo_item_text (xstrdup (p.get_formatted_text ()), true);
+  m_optinfo->m_items.safe_push (item);
   return *this;
 }
diff --git a/gcc/optinfo.h b/gcc/optinfo.h
index ccaa47e..6d1a218 100644
--- a/gcc/optinfo.h
+++ b/gcc/optinfo.h
@@ -21,6 +21,7 @@ along with GCC; see the file COPYING3.  If not see
 #ifndef GCC_OPTINFO_H
 #define GCC_OPTINFO_H
 
+#include "unique-ptr.h"
 #include "optimization-records.h"
 
 /* FIXME: this is flexible, but probably inefficient.  */
@@ -50,7 +51,15 @@ class optinfo_item
 class optinfo_item_text : public optinfo_item
 {
  public:
-  optinfo_item_text (const char *text) : m_text (text) {}
+  optinfo_item_text (char *text, bool owned)
+  : m_text (text), m_owned (owned)
+  {}
+  ~optinfo_item_text ()
+  {
+    if (m_owned)
+      free (m_text);
+  }
+
   enum optinfo_item_kind get_kind () const FINAL OVERRIDE
   {
     return OPTINFO_ITEM_KIND_TEXT;
@@ -59,8 +68,8 @@ class optinfo_item_text : public optinfo_item
   const char *get_text () const { return m_text; }
 
  private:
-  const char *m_text;
-  // FIXME: currently this is assumed to be a string literal i.e. outlive
+  char *m_text;
+  bool m_owned;
 };
 
 /* Packaging up a tree with dump flags.  */
@@ -226,11 +235,21 @@ class optinfo
 class optinfo_printf
 {
  public:
-  optinfo_printf (const char */*fmt*/, ...)
+  optinfo_printf (const char *fmt, ...)
+   ATTRIBUTE_PRINTF_2
   {
-    // FIXME: should generate a malloced-text
-    //
+    va_list ap;
+    va_start (ap, fmt);
+    m_formatted_text = xvasprintf (fmt, ap);
+    va_end (ap);
   }
+
+  ~optinfo_printf () { free (m_formatted_text); } // FIXME: really?
+
+  const char *get_formatted_text () const { return m_formatted_text; }
+
+ private:
+  char *m_formatted_text;
 };
 
 /* Emit the info when this goes out of scope.  */
@@ -241,12 +260,11 @@ class pending_optinfo
   pending_optinfo (const location &impl_location,
 		   enum opt_record_kind kind,
 		   optinfo_location loc);
-  ~pending_optinfo ();
 
   pending_optinfo& operator<< (const char *);
   pending_optinfo& operator<< (optinfo_tree);
   pending_optinfo& operator<< (gimple *);
-  pending_optinfo& operator<< (optinfo_printf);
+  pending_optinfo& operator<< (const optinfo_printf &);
 
 #if 0
   /* Compare with dump_dec (MSG_NOTE, ).  */
@@ -258,9 +276,9 @@ class pending_optinfo
   }
 #endif
 
-
  private:
-  optinfo *m_optinfo;
+  // FIXME: are we allowed this yet?  or do we need a gnu::shared_ptr
+  std::shared_ptr <optinfo> m_optinfo;
 };
 
 #endif /* #ifndef GCC_OPTINFO_H */
-- 
1.8.5.3

