[gcc r15-1210] pretty_printer: convert chunk_info into a class

David Malcolm dmalcolm@gcc.gnu.org
Wed Jun 12 13:29:23 GMT 2024


https://gcc.gnu.org/g:1cae1a5ce088c1fa351b5752d43de52f1f116a75

commit r15-1210-g1cae1a5ce088c1fa351b5752d43de52f1f116a75
Author: David Malcolm <dmalcolm@redhat.com>
Date:   Wed Jun 12 09:15:09 2024 -0400

    pretty_printer: convert chunk_info into a class
    
    No functional change intended.
    
    gcc/cp/ChangeLog:
            * error.cc (append_formatted_chunk): Move part of body into
            chunk_info::append_formatted_chunk.
    
    gcc/ChangeLog:
            * dumpfile.cc (dump_pretty_printer::emit_items): Update for
            changes to chunk_info.
            * pretty-print.cc (chunk_info::append_formatted_chunk): New, based
            on code in cp/error.cc's append_formatted_chunk.
            (chunk_info::pop_from_output_buffer): New, based on code in
            pp_output_formatted_text and dump_pretty_printer::emit_items.
            (on_begin_quote): Convert to...
            (chunk_info::on_begin_quote): ...this.
            (on_end_quote): Convert to...
            (chunk_info::on_end_quote): ...this.
            (pretty_printer::format): Update for chunk_info becoming a class
            and its fields gaining "m_" prefixes.  Update for on_begin_quote
            and on_end_quote moving to chunk_info.
            (quoting_info::handle_phase_3): Update for changes to chunk_info.
            (pp_output_formatted_text): Likewise.  Move cleanup code to
            chunk_info::pop_from_output_buffer.
            * pretty-print.h (class output_buffer): New forward decl.
            (class urlifier): New forward decl.
            (struct chunk_info): Convert to...
            (class chunk_info): ...this.  Add friend class pretty_printer.
            (chunk_info::get_args): New accessor.
            (chunk_info::get_quoting_info): New accessor.
            (chunk_info::append_formatted_chunk): New decl.
            (chunk_info::pop_from_output_buffer): New decl.
            (chunk_info::on_begin_quote): New decl.
            (chunk_info::on_end_quote): New decl.
            (chunk_info::prev): Rename to...
            (chunk_info::m_prev): ...this.
            (chunk_info::args): Rename to...
            (chunk_info::m_args): ...this.
            (output_buffer::cur_chunk_array): Drop "struct" from decl.
    
    Signed-off-by: David Malcolm <dmalcolm@redhat.com>

Diff:
---
 gcc/cp/error.cc     | 10 ++----
 gcc/dumpfile.cc     |  9 ++---
 gcc/pretty-print.cc | 96 ++++++++++++++++++++++++++++++++---------------------
 gcc/pretty-print.h  | 30 ++++++++++++++---
 4 files changed, 90 insertions(+), 55 deletions(-)

diff --git a/gcc/cp/error.cc b/gcc/cp/error.cc
index 01ad794df8e3..171a352c85fd 100644
--- a/gcc/cp/error.cc
+++ b/gcc/cp/error.cc
@@ -4307,14 +4307,8 @@ static void
 append_formatted_chunk (pretty_printer *pp, const char *content)
 {
   output_buffer *buffer = pp_buffer (pp);
-  struct chunk_info *chunk_array = buffer->cur_chunk_array;
-  const char **args = chunk_array->args;
-
-  unsigned int chunk_idx;
-  for (chunk_idx = 0; args[chunk_idx]; chunk_idx++)
-    ;
-  args[chunk_idx++] = content;
-  args[chunk_idx] = NULL;
+  chunk_info *chunk_array = buffer->cur_chunk_array;
+  chunk_array->append_formatted_chunk (content);
 }
 
 /* Create a copy of CONTENT, with quotes added, and,
diff --git a/gcc/dumpfile.cc b/gcc/dumpfile.cc
index 097f9bcfff21..82bd8b06bebf 100644
--- a/gcc/dumpfile.cc
+++ b/gcc/dumpfile.cc
@@ -819,8 +819,8 @@ void
 dump_pretty_printer::emit_items (optinfo *dest)
 {
   output_buffer *buffer = pp_buffer (this);
-  struct chunk_info *chunk_array = buffer->cur_chunk_array;
-  const char **args = chunk_array->args;
+  chunk_info *chunk_array = buffer->cur_chunk_array;
+  const char * const *args = chunk_array->get_args ();
 
   gcc_assert (buffer->obstack == &buffer->formatted_obstack);
   gcc_assert (buffer->line_length == 0);
@@ -847,10 +847,7 @@ dump_pretty_printer::emit_items (optinfo *dest)
   /* Ensure that we consumed all of stashed_items.  */
   gcc_assert (stashed_item_idx == m_stashed_items.length ());
 
-  /* Deallocate the chunk structure and everything after it (i.e. the
-     associated series of formatted strings).  */
-  buffer->cur_chunk_array = chunk_array->prev;
-  obstack_free (&buffer->chunk_obstack, chunk_array);
+  chunk_array->pop_from_output_buffer (*buffer);
 }
 
 /* Subroutine of dump_pretty_printer::emit_items
diff --git a/gcc/pretty-print.cc b/gcc/pretty-print.cc
index 271cd650c4d1..639e2b881586 100644
--- a/gcc/pretty-print.cc
+++ b/gcc/pretty-print.cc
@@ -1239,29 +1239,53 @@ private:
   std::vector<run> m_phase_3_quotes;
 };
 
-static void
-on_begin_quote (const output_buffer &buf,
-		unsigned chunk_idx,
-		const urlifier *urlifier)
+/* Adds a chunk to the end of formatted output, so that it
+   will be printed by pp_output_formatted_text.  */
+
+void
+chunk_info::append_formatted_chunk (const char *content)
+{
+  unsigned int chunk_idx;
+  for (chunk_idx = 0; m_args[chunk_idx]; chunk_idx++)
+    ;
+  m_args[chunk_idx++] = content;
+  m_args[chunk_idx] = nullptr;
+}
+
+/* Deallocate the current chunk structure and everything after it (i.e. the
+   associated series of formatted strings).  */
+
+void
+chunk_info::pop_from_output_buffer (output_buffer &buf)
+{
+  delete m_quotes;
+  buf.cur_chunk_array = m_prev;
+  obstack_free (&buf.chunk_obstack, this);
+}
+
+void
+chunk_info::on_begin_quote (const output_buffer &buf,
+			    unsigned chunk_idx,
+			    const urlifier *urlifier)
 {
   if (!urlifier)
     return;
-  if (!buf.cur_chunk_array->m_quotes)
-    buf.cur_chunk_array->m_quotes = new quoting_info ();
-  buf.cur_chunk_array->m_quotes->on_begin_quote (buf, chunk_idx);
+  if (!m_quotes)
+    m_quotes = new quoting_info ();
+  m_quotes->on_begin_quote (buf, chunk_idx);
 }
 
-static void
-on_end_quote (pretty_printer *pp,
-	      output_buffer &buf,
-	      unsigned chunk_idx,
-	      const urlifier *urlifier)
+void
+chunk_info::on_end_quote (pretty_printer *pp,
+			  output_buffer &buf,
+			  unsigned chunk_idx,
+			  const urlifier *urlifier)
 {
   if (!urlifier)
     return;
-  if (!buf.cur_chunk_array->m_quotes)
-    buf.cur_chunk_array->m_quotes = new quoting_info ();
-  buf.cur_chunk_array->m_quotes->on_end_quote (pp, buf, chunk_idx, *urlifier);
+  if (!m_quotes)
+    m_quotes = new quoting_info ();
+  m_quotes->on_end_quote (pp, buf, chunk_idx, *urlifier);
 }
 
 /* The following format specifiers are recognized as being client independent:
@@ -1333,13 +1357,12 @@ pretty_printer::format (text_info *text,
   const char **formatters[PP_NL_ARGMAX];
 
   /* Allocate a new chunk structure.  */
-  struct chunk_info *new_chunk_array
-    = XOBNEW (&buffer->chunk_obstack, struct chunk_info);
+  chunk_info *new_chunk_array = XOBNEW (&buffer->chunk_obstack, chunk_info);
 
-  new_chunk_array->prev = buffer->cur_chunk_array;
+  new_chunk_array->m_prev = buffer->cur_chunk_array;
   new_chunk_array->m_quotes = nullptr;
   buffer->cur_chunk_array = new_chunk_array;
-  const char **args = new_chunk_array->args;
+  const char **args = new_chunk_array->m_args;
 
   /* Formatting phase 1: split up TEXT->format_spec into chunks in
      pp_buffer (PP)->args[].  Even-numbered chunks are to be output
@@ -1380,13 +1403,13 @@ pretty_printer::format (text_info *text,
 	    obstack_grow (&buffer->chunk_obstack, colorstr, strlen (colorstr));
 	    p++;
 
-	    on_begin_quote (*buffer, chunk, urlifier);
+	    buffer->cur_chunk_array->on_begin_quote (*buffer, chunk, urlifier);
 	    continue;
 	  }
 
 	case '>':
 	  {
-	    on_end_quote (this, *buffer, chunk, urlifier);
+	    buffer->cur_chunk_array->on_end_quote (this, *buffer, chunk, urlifier);
 
 	    const char *colorstr = colorize_stop (m_show_color);
 	    obstack_grow (&buffer->chunk_obstack, colorstr, strlen (colorstr));
@@ -1584,7 +1607,7 @@ pretty_printer::format (text_info *text,
       if (quote)
 	{
 	  pp_begin_quote (this, m_show_color);
-	  on_begin_quote (*buffer, chunk, urlifier);
+	  buffer->cur_chunk_array->on_begin_quote (*buffer, chunk, urlifier);
 	}
 
       switch (*p)
@@ -1756,7 +1779,8 @@ pretty_printer::format (text_info *text,
 
       if (quote)
 	{
-	  on_end_quote (this, *buffer, chunk, urlifier);
+	  buffer->cur_chunk_array->on_end_quote (this, *buffer,
+						 chunk, urlifier);
 	  pp_end_quote (this, m_show_color);
 	}
 
@@ -1840,8 +1864,9 @@ quoting_info::handle_phase_3 (pretty_printer *pp,
 {
   unsigned int chunk;
   output_buffer * const buffer = pp_buffer (pp);
-  struct chunk_info *chunk_array = buffer->cur_chunk_array;
-  const char **args = chunk_array->args;
+  chunk_info *chunk_array = buffer->cur_chunk_array;
+  const char * const *args = chunk_array->get_args ();
+  quoting_info *quoting = chunk_array->get_quoting_info ();
 
   /* We need to construct the string into an intermediate buffer
      for this case, since using pp_string can introduce prefixes
@@ -1856,9 +1881,9 @@ quoting_info::handle_phase_3 (pretty_printer *pp,
      correspond to.  */
   size_t start_of_run_byte_offset = 0;
   std::vector<quoting_info::run>::const_iterator iter_run
-    = buffer->cur_chunk_array->m_quotes->m_phase_3_quotes.begin ();
+    = quoting->m_phase_3_quotes.begin ();
   std::vector<quoting_info::run>::const_iterator end_runs
-    = buffer->cur_chunk_array->m_quotes->m_phase_3_quotes.end ();
+    = quoting->m_phase_3_quotes.end ();
   for (chunk = 0; args[chunk]; chunk++)
     {
       size_t start_of_chunk_idx = combined_buf.object_size ();
@@ -1913,8 +1938,9 @@ pp_output_formatted_text (pretty_printer *pp,
 {
   unsigned int chunk;
   output_buffer * const buffer = pp_buffer (pp);
-  struct chunk_info *chunk_array = buffer->cur_chunk_array;
-  const char **args = chunk_array->args;
+  chunk_info *chunk_array = buffer->cur_chunk_array;
+  const char * const *args = chunk_array->get_args ();
+  quoting_info *quoting = chunk_array->get_quoting_info ();
 
   gcc_assert (buffer->obstack == &buffer->formatted_obstack);
 
@@ -1924,18 +1950,14 @@ pp_output_formatted_text (pretty_printer *pp,
   /* If we have any deferred urlification, handle it now.  */
   if (urlifier
       && pp->supports_urls_p ()
-      && buffer->cur_chunk_array->m_quotes
-      && buffer->cur_chunk_array->m_quotes->has_phase_3_quotes_p ())
-    buffer->cur_chunk_array->m_quotes->handle_phase_3 (pp, *urlifier);
+      && quoting
+      && quoting->has_phase_3_quotes_p ())
+    quoting->handle_phase_3 (pp, *urlifier);
   else
     for (chunk = 0; args[chunk]; chunk++)
       pp_string (pp, args[chunk]);
 
-  /* Deallocate the chunk structure and everything after it (i.e. the
-     associated series of formatted strings).  */
-  delete buffer->cur_chunk_array->m_quotes;
-  buffer->cur_chunk_array = chunk_array->prev;
-  obstack_free (&buffer->chunk_obstack, chunk_array);
+  chunk_array->pop_from_output_buffer (*buffer);
 }
 
 /* Helper subroutine of output_verbatim and verbatim. Do the appropriate
diff --git a/gcc/pretty-print.h b/gcc/pretty-print.h
index 99e55dc6a3c0..b41d3ce31d22 100644
--- a/gcc/pretty-print.h
+++ b/gcc/pretty-print.h
@@ -70,16 +70,38 @@ enum diagnostic_prefixing_rule_t
 };
 
 class quoting_info;
+class output_buffer;
+class urlifier;
 
 /* The chunk_info data structure forms a stack of the results from the
    first phase of formatting (pp_format) which have not yet been
    output (pp_output_formatted_text).  A stack is necessary because
    the diagnostic starter may decide to generate its own output by way
    of the formatter.  */
-struct chunk_info
+class chunk_info
 {
+  friend class pretty_printer;
+
+public:
+  const char * const *get_args () const { return m_args; }
+  quoting_info *get_quoting_info () const { return m_quotes; }
+
+  void append_formatted_chunk (const char *content);
+
+  void pop_from_output_buffer (output_buffer &buf);
+
+private:
+  void on_begin_quote (const output_buffer &buf,
+		       unsigned chunk_idx,
+		       const urlifier *urlifier);
+
+  void on_end_quote (pretty_printer *pp,
+		     output_buffer &buf,
+		     unsigned chunk_idx,
+		     const urlifier *urlifier);
+
   /* Pointer to previous chunk on the stack.  */
-  struct chunk_info *prev;
+  chunk_info *m_prev;
 
   /* Array of chunks to output.  Each chunk is a NUL-terminated string.
      In the first phase of formatting, even-numbered chunks are
@@ -87,7 +109,7 @@ struct chunk_info
      The second phase replaces all odd-numbered chunks with formatted
      text, and the third phase simply emits all the chunks in sequence
      with appropriate line-wrapping.  */
-  const char *args[PP_NL_ARGMAX * 2];
+  const char *m_args[PP_NL_ARGMAX * 2];
 
   /* If non-null, information on quoted text runs within the chunks
      for use by a urlifier.  */
@@ -114,7 +136,7 @@ public:
   struct obstack *obstack;
 
   /* Stack of chunk arrays.  These come from the chunk_obstack.  */
-  struct chunk_info *cur_chunk_array;
+  chunk_info *cur_chunk_array;
 
   /* Where to output formatted text.  */
   FILE *stream;


More information about the Gcc-cvs mailing list