This patch formalizes the observation that certain pretty printing
functions return formatted texts that have lifetime logically outlasting
the local instances of pretty printers.  This has not been a problem so
far because we were using static storage, but once we switch local
pretty printers, it becomes a problem.

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

-- Gaby

2013-08-20  Gabriel Dos Reis  <g...@integrable-solutions.net>

        * error.c (pp_ggc_formatted_text): New.
        (type_as_string): Use it in lieu of pp_formatted_text.
        (type_as_string_translate): Likewise.
        (expr_as_string): Likewise.
        (decl_as_string): Likewise.
        (decl_as_string_translate): Likewise.
        (lang_decl_name): Likewise.
        (decl_to_string): Likewise.
        (expr_to_string): Likewise.
        (fndecl_to_string): Likewise.
        (parm_to_string): Likewise.
        (type_to_string): Likewise.
        (args_to_string): Likewise.
        (subst_to_string): Likewise.

Index: cp/error.c
===================================================================
--- cp/error.c  (revision 201871)
+++ cp/error.c  (working copy)
@@ -2632,7 +2632,15 @@
   cxx_pp->enclosing_scope = current_function_decl;
 }
 
+/* Same as pp_formatted_text, except the return string is a separate
+   copy and has a GGC storage duration, e.g. an indefinite lifetime.  */
 
+inline const char *
+pp_ggc_formatted_text (pretty_printer *pp)
+{
+  return ggc_strdup (pp_formatted_text (pp));
+}
+
 /* Exported interface to stringifying types, exprs and decls under TFF_*
    control.  */
 
@@ -2642,7 +2650,7 @@
   reinit_cxx_pp ();
   pp_translate_identifiers (cxx_pp) = false;
   dump_type (cxx_pp, typ, flags);
-  return pp_formatted_text (cxx_pp);
+  return pp_ggc_formatted_text (cxx_pp);
 }
 
 const char *
@@ -2650,7 +2658,7 @@
 {
   reinit_cxx_pp ();
   dump_type (cxx_pp, typ, flags);
-  return pp_formatted_text (cxx_pp);
+  return pp_ggc_formatted_text (cxx_pp);
 }
 
 const char *
@@ -2659,7 +2667,7 @@
   reinit_cxx_pp ();
   pp_translate_identifiers (cxx_pp) = false;
   dump_expr (cxx_pp, decl, flags);
-  return pp_formatted_text (cxx_pp);
+  return pp_ggc_formatted_text (cxx_pp);
 }
 
 /* Wrap decl_as_string with options appropriate for dwarf.  */
@@ -2683,7 +2691,7 @@
   reinit_cxx_pp ();
   pp_translate_identifiers (cxx_pp) = false;
   dump_decl (cxx_pp, decl, flags);
-  return pp_formatted_text (cxx_pp);
+  return pp_ggc_formatted_text (cxx_pp);
 }
 
 const char *
@@ -2691,7 +2699,7 @@
 {
   reinit_cxx_pp ();
   dump_decl (cxx_pp, decl, flags);
-  return pp_formatted_text (cxx_pp);
+  return pp_ggc_formatted_text (cxx_pp);
 }
 
 /* Wrap lang_decl_name with options appropriate for dwarf.  */
@@ -2738,7 +2746,7 @@
   else
     dump_decl (cxx_pp, DECL_NAME (decl), TFF_PLAIN_IDENTIFIER);
 
-  return pp_formatted_text (cxx_pp);
+  return pp_ggc_formatted_text (cxx_pp);
 }
 
 /* Return the location of a tree passed to %+ formats.  */
@@ -2782,7 +2790,7 @@
 
   reinit_cxx_pp ();
   dump_decl (cxx_pp, decl, flags);
-  return pp_formatted_text (cxx_pp);
+  return pp_ggc_formatted_text (cxx_pp);
 }
 
 static const char *
@@ -2790,7 +2798,7 @@
 {
   reinit_cxx_pp ();
   dump_expr (cxx_pp, decl, 0);
-  return pp_formatted_text (cxx_pp);
+  return pp_ggc_formatted_text (cxx_pp);
 }
 
 static const char *
@@ -2804,7 +2812,7 @@
     flags |= TFF_FUNCTION_DEFAULT_ARGUMENTS;
   reinit_cxx_pp ();
   dump_decl (cxx_pp, fndecl, flags);
-  return pp_formatted_text (cxx_pp);
+  return pp_ggc_formatted_text (cxx_pp);
 }
 
 
@@ -2844,7 +2852,7 @@
     pp_string (cxx_pp, "'this'");
   else
     pp_decimal_int (cxx_pp, p + 1);
-  return pp_formatted_text (cxx_pp);
+  return pp_ggc_formatted_text (cxx_pp);
 }
 
 static const char *
@@ -2887,7 +2895,7 @@
       if (memcmp (p, p+aka_start, len) == 0)
        p[len] = '\0';
     }
-  return pp_formatted_text (cxx_pp);
+  return pp_ggc_formatted_text (cxx_pp);
 }
 
 static const char *
@@ -2920,7 +2928,7 @@
       if (TREE_CHAIN (p))
        pp_separate_with_comma (cxx_pp);
     }
-  return pp_formatted_text (cxx_pp);
+  return pp_ggc_formatted_text (cxx_pp);
 }
 
 /* Pretty-print a deduction substitution (from deduction_tsubst_fntype).  P
@@ -2947,7 +2955,7 @@
   pp_cxx_whitespace (cxx_pp);
   dump_template_bindings (cxx_pp, tparms, targs, NULL);
   pp_cxx_right_bracket (cxx_pp);
-  return pp_formatted_text (cxx_pp);
+  return pp_ggc_formatted_text (cxx_pp);
 }
 
 static const char *
@@ -2956,7 +2964,7 @@
   reinit_cxx_pp ();
   cxx_pp->padding = v ? pp_before : pp_none;
   pp_cxx_cv_qualifier_seq (cxx_pp, p);
-  return pp_formatted_text (cxx_pp);
+  return pp_ggc_formatted_text (cxx_pp);
 }
 
 /* Langhook for print_error_function.  */

Reply via email to