Patchwork Clean up pretty printers [12/n]

login
register
mail settings
Submitter Gabriel Dos Reis
Date Aug. 24, 2013, 12:03 a.m.
Message ID <87mwo82bvx.fsf@euclid.axiomatics.org>
Download mbox | patch
Permalink /patch/269581/
State New
Headers show

Comments

Gabriel Dos Reis - Aug. 24, 2013, 12:03 a.m.
The primary function of this patch is to formally make the class
pretty_printer polymorphic (in the OO sense.)  It has always been
conceptually polymorphic -- we just didn't have linguistic support to
say so in C.  As a secondary change, the patch makes a systematic use of
pp_buffer -- making it easier to search for a pretty printer's output
buffer access.

Tested on an x86_64-suse-linux.  Applied to trunk.

-- Gaby

2013-08-23  Gabriel Dos Reis  <gdr@integrable-solutions.net>

	* diagnostic.c (diagnostic_set_caret_max_width): Use pp_buffer.
	* gimple-pretty-print.c (gimple_dump_bb_buff): Likewise.
	* pretty-print.c (pp_formatted_text_data): Likewise.
	(pp_write_text_to_stream): Likewise.
	(pp_write_text_as_dot_label_to_stream): Likewise.
	(pp_append_r): Likewise.
	(pp_format): Likewise.
	(pp_flush): Likewise.
	(pp_clear_output_area): Likewise.
	(pp_append_text): Likewise.
	(pp_formatted_text): Likewise.
	(pp_remaining_character_count_for_line): Likewise.
	(pp_newline): Likewise.
	(pp_character): Likewise.
	(output_buffer::~output_buffer): Define.
	(pretty_printer::~pretty_printer): Destruct output buffer.
	* pretty-print.h (output_buffer::~output_buffer): Declare.
	(pretty_printer::~pretty_printer): Declare virtual.

c/
	* c-objc-common.c (c_tree_printer): Document the nature of the cast.
	(c_initialize_diagnostics): Call a destructor for the early printer.

cp/
	* cp-objcp-common.c (cxx_initialize_diagnostics): Call a
	destructor for the early printer.
	* error.c (type_to_string): Use pp_buffer.

Patch

Index: c/c-objc-common.c
===================================================================
--- c/c-objc-common.c	(revision 201955)
+++ c/c-objc-common.c	(working copy)
@@ -92,6 +92,7 @@ 
 {
   tree t = NULL_TREE;
   tree name;
+  // FIXME: the next cast should be a dynamic_cast, when it is permitted.
   c_pretty_printer *cpp = (c_pretty_printer *) pp;
   pp->padding = pp_none;
 
@@ -192,6 +193,7 @@ 
   context->printer = new (pp) c_pretty_printer ();
 
   /* It is safe to free this object because it was previously XNEW()'d.  */
+  base->~pretty_printer ();
   XDELETE (base);
 }
 
Index: cp/cp-objcp-common.c
===================================================================
--- cp/cp-objcp-common.c	(revision 201955)
+++ cp/cp-objcp-common.c	(working copy)
@@ -140,6 +140,7 @@ 
   context->printer = new (pp) cxx_pretty_printer ();
 
   /* It is safe to free this object because it was previously XNEW()'d.  */
+  base->~pretty_printer ();
   XDELETE (base);
 }
 
Index: cp/error.c
===================================================================
--- cp/error.c	(revision 201955)
+++ cp/error.c	(working copy)
@@ -2881,7 +2881,7 @@ 
       && !uses_template_parms (typ))
     {
       int aka_start; char *p;
-      struct obstack *ob = cxx_pp->buffer->obstack;
+      struct obstack *ob = pp_buffer (cxx_pp)->obstack;
       /* Remember the end of the initial dump.  */
       int len = obstack_object_size (ob);
       tree aka = strip_typedefs (typ);
Index: diagnostic.c
===================================================================
--- diagnostic.c	(revision 201955)
+++ diagnostic.c	(working copy)
@@ -104,7 +104,7 @@ 
 {
   /* One minus to account for the leading empty space.  */
   value = value ? value - 1 
-    : (isatty (fileno (context->printer->buffer->stream))
+    : (isatty (fileno (pp_buffer (context->printer)->stream))
        ? getenv_columns () - 1: INT_MAX);
   
   if (value <= 0) 
Index: gimple-pretty-print.c
===================================================================
--- gimple-pretty-print.c	(revision 201955)
+++ gimple-pretty-print.c	(working copy)
@@ -2249,7 +2249,7 @@ 
       pp_newline_and_flush (buffer);
       gcc_checking_assert (DECL_STRUCT_FUNCTION (current_function_decl));
       dump_histograms_for_stmt (DECL_STRUCT_FUNCTION (current_function_decl),
-				buffer->buffer->stream, stmt);
+				pp_buffer(buffer)->stream, stmt);
     }
 
   dump_implicit_edges (buffer, bb, indent, flags);
Index: pretty-print.c
===================================================================
--- pretty-print.c	(revision 201955)
+++ pretty-print.c	(working copy)
@@ -46,9 +46,17 @@ 
   obstack_init (&chunk_obstack);
 }
 
+// Release resources owned by an output buffer at the end of lifetime.
+
+output_buffer::~output_buffer ()
+{
+  obstack_free (&chunk_obstack, obstack_finish (&chunk_obstack));
+  obstack_free (&formatted_obstack, obstack_finish (&formatted_obstack));
+}
+
 /* A pointer to the formatted diagnostic message.  */
 #define pp_formatted_text_data(PP) \
-   ((const char *) obstack_base ((PP)->buffer->obstack))
+   ((const char *) obstack_base (pp_buffer (PP)->obstack))
 
 /* Format an integer given by va_arg (ARG, type-specifier T) where
    type-specifier is a precision modifier as indicated by PREC.  F is
@@ -112,7 +120,7 @@ 
 pp_write_text_to_stream (pretty_printer *pp)
 {
   const char *text = pp_formatted_text (pp);
-  fputs (text, pp->buffer->stream);
+  fputs (text, pp_buffer (pp)->stream);
   pp_clear_output_area (pp);
 }
 
@@ -130,7 +138,7 @@ 
 {
   const char *text = pp_formatted_text (pp);
   const char *p = text;
-  FILE *fp = pp->buffer->stream;
+  FILE *fp = pp_buffer (pp)->stream;
 
   while (*p)
     {
@@ -216,8 +224,8 @@ 
 static inline void
 pp_append_r (pretty_printer *pp, const char *start, int length)
 {
-  obstack_grow (pp->buffer->obstack, start, length);
-  pp->buffer->line_length += length;
+  obstack_grow (pp_buffer (pp)->obstack, start, length);
+  pp_buffer (pp)->line_length += length;
 }
 
 /* Insert enough spaces into the output area of PRETTY-PRINTER to bring
@@ -268,13 +276,13 @@ 
    A format string can have at most 30 arguments.  */
 
 /* Formatting phases 1 and 2: render TEXT->format_spec plus
-   TEXT->args_ptr into a series of chunks in PP->buffer->args[].
+   TEXT->args_ptr into a series of chunks in pp_buffer (PP)->args[].
    Phase 3 is in pp_format_text.  */
 
 void
 pp_format (pretty_printer *pp, text_info *text)
 {
-  output_buffer *buffer = pp->buffer;
+  output_buffer *buffer = pp_buffer (pp);
   const char *p;
   const char **args;
   struct chunk_info *new_chunk_array;
@@ -291,7 +299,7 @@ 
   args = new_chunk_array->args;
 
   /* Formatting phase 1: split up TEXT->format_spec into chunks in
-     PP->buffer->args[].  Even-numbered chunks are to be output
+     pp_buffer (PP)->args[].  Even-numbered chunks are to be output
      verbatim, odd-numbered chunks are format specifiers.
      %m, %%, %<, %>, and %' are replaced with the appropriate text at
      this point.  */
@@ -677,7 +685,7 @@ 
 {
   pp_write_text_to_stream (pp);
   pp_clear_state (pp);
-  fflush (pp->buffer->stream);
+  fflush (pp_buffer (pp)->stream);
 }
 
 /* Sets the number of maximum characters per line PRETTY-PRINTER can
@@ -694,8 +702,9 @@ 
 void
 pp_clear_output_area (pretty_printer *pp)
 {
-  obstack_free (pp->buffer->obstack, obstack_base (pp->buffer->obstack));
-  pp->buffer->line_length = 0;
+  obstack_free (pp_buffer (pp)->obstack,
+                obstack_base (pp_buffer (pp)->obstack));
+  pp_buffer (pp)->line_length = 0;
 }
 
 /* Set PREFIX for PRETTY-PRINTER.  */
@@ -773,6 +782,12 @@ 
   pp_set_prefix (this, p);
 }
 
+pretty_printer::~pretty_printer ()
+{
+  buffer->~output_buffer ();
+  XDELETE (buffer);
+}
+
 /* Append a string delimited by START and END to the output area of
    PRETTY-PRINTER.  No line wrapping is done.  However, if beginning a
    new line then emit PRETTY-PRINTER's prefix and skip any leading
@@ -782,7 +797,7 @@ 
 pp_append_text (pretty_printer *pp, const char *start, const char *end)
 {
   /* Emit prefix and skip whitespace if we're starting a new line.  */
-  if (pp->buffer->line_length == 0)
+  if (pp_buffer (pp)->line_length == 0)
     {
       pp_emit_prefix (pp);
       if (pp_is_wrapping_line (pp))
@@ -797,7 +812,7 @@ 
 const char *
 pp_formatted_text (pretty_printer *pp)
 {
-  obstack_1grow (pp->buffer->obstack, '\0');
+  obstack_1grow (pp_buffer (pp)->obstack, '\0');
   return pp_formatted_text_data (pp);
 }
 
@@ -807,7 +822,7 @@ 
 pp_last_position_in_text (const pretty_printer *pp)
 {
   const char *p = NULL;
-  struct obstack *text = pp->buffer->obstack;
+  struct obstack *text = pp_buffer (pp)->obstack;
 
   if (obstack_base (text) != obstack_next_free (text))
     p = ((const char *) obstack_next_free (text)) - 1;
@@ -819,7 +834,7 @@ 
 int
 pp_remaining_character_count_for_line (pretty_printer *pp)
 {
-  return pp->maximum_length - pp->buffer->line_length;
+  return pp->maximum_length - pp_buffer (pp)->line_length;
 }
 
 
@@ -863,9 +878,9 @@ 
 void
 pp_newline (pretty_printer *pp)
 {
-  obstack_1grow (pp->buffer->obstack, '\n');
+  obstack_1grow (pp_buffer (pp)->obstack, '\n');
   pp_needs_newline (pp) = false;
-  pp->buffer->line_length = 0;
+  pp_buffer (pp)->line_length = 0;
 }
 
 /* Have PRETTY-PRINTER add a CHARACTER.  */
@@ -879,8 +894,8 @@ 
       if (ISSPACE (c))
         return;
     }
-  obstack_1grow (pp->buffer->obstack, c);
-  ++pp->buffer->line_length;
+  obstack_1grow (pp_buffer (pp)->obstack, c);
+  ++pp_buffer (pp)->line_length;
 }
 
 /* Append a STRING to the output area of PRETTY-PRINTER; the STRING may
Index: pretty-print.h
===================================================================
--- pretty-print.h	(revision 201955)
+++ pretty-print.h	(working copy)
@@ -74,6 +74,7 @@ 
 struct output_buffer
 {
   output_buffer ();
+  ~output_buffer ();
 
   /* Obstack where the text is built up.  */
   struct obstack formatted_obstack;
@@ -165,6 +166,8 @@ 
   // and a maximum line length cut off limit.
   explicit pretty_printer (const char* = NULL, int = 0);
 
+  virtual ~pretty_printer ();
+
   /* Where we print external representation of ENTITY.  */
   output_buffer *buffer;