analyzer: workaround for nested pp_printf
authorDavid Malcolm <dmalcolm@redhat.com>
Wed, 5 Feb 2020 20:26:44 +0000 (15:26 -0500)
committerDavid Malcolm <dmalcolm@redhat.com>
Tue, 11 Feb 2020 18:28:25 +0000 (13:28 -0500)
The dumps from the analyzer sometimes contain garbled output.

The root cause is due to nesting of calls to pp_printf: I'm using
pp_printf with %qT to print types with a PP using default_tree_printer.

default_tree_printer handles 'T' (and various other codes) via
  dump_generic_node (pp, t, 0, TDF_SLIM, 0);
and dump_generic_node can call pp_printf in various ways, leading
to a pp_printf within a pp_printf, and garbled output.

I don't think it's feasible to fix pp_printf to be reentrant, in
stage 4, at least, so for the moment this patch works around it
in the analyzer.

gcc/analyzer/ChangeLog:
* region-model.cc (print_quoted_type): New function.
(svalue::print): Use it to replace %qT.
(region::dump_to_pp): Likewise.
(region::dump_child_label): Likewise.
(region::print_fields): Likewise.

gcc/analyzer/ChangeLog
gcc/analyzer/region-model.cc

index 4dace2d..57fee4c 100644 (file)
@@ -1,3 +1,11 @@
+2020-02-11  David Malcolm  <dmalcolm@redhat.com>
+
+       * region-model.cc (print_quoted_type): New function.
+       (svalue::print): Use it to replace %qT.
+       (region::dump_to_pp): Likewise.
+       (region::dump_child_label): Likewise.
+       (region::print_fields): Likewise.
+
 2020-02-10  David Malcolm  <dmalcolm@redhat.com>
 
        PR analyzer/93659
index 8515560..b9d52f6 100644 (file)
@@ -73,6 +73,25 @@ dump_tree (pretty_printer *pp, tree t)
   dump_generic_node (pp, t, 0, TDF_SLIM, 0);
 }
 
+/* Equivalent to pp_printf (pp, "%qT", t), to avoid nesting pp_printf
+   calls within other pp_printf calls.
+
+   default_tree_printer handles 'T' and some other codes by calling
+     dump_generic_node (pp, t, 0, TDF_SLIM, 0);
+   dump_generic_node calls pp_printf in various places, leading to
+   garbled output.
+
+   Ideally pp_printf could be made to be reentrant, but in the meantime
+   this function provides a workaround.  */
+
+static void
+print_quoted_type (pretty_printer *pp, tree t)
+{
+  pp_begin_quote (pp, pp_show_color (pp));
+  dump_generic_node (pp, t, 0, TDF_SLIM, 0);
+  pp_end_quote (pp, pp_show_color (pp));
+}
+
 /* Dump this path_var to PP (which must support %E for trees).
 
    Express the stack depth using an "@DEPTH" suffix, so e.g. given
@@ -319,7 +338,9 @@ svalue::print (const region_model &model,
   if (m_type)
     {
       gcc_assert (TYPE_P (m_type));
-      pp_printf (pp, "type: %qT, ", m_type);
+      pp_string (pp, "type: ");
+      print_quoted_type (pp, m_type);
+      pp_string (pp, ", ");
     }
 
   /* vfunc.  */
@@ -1282,7 +1303,8 @@ region::dump_to_pp (const region_model &model,
     }
   if (m_type)
     {
-      pp_printf (pp, "%s type: %qT", field_prefix, m_type);
+      pp_printf (pp, "%s type: ", field_prefix);
+      print_quoted_type (pp, m_type);
       pp_newline (pp);
     }
 
@@ -1332,7 +1354,9 @@ region::dump_child_label (const region_model &model,
        pp_string (pp, "active ");
       else
        pp_string (pp, "inactive ");
-      pp_printf (pp, "view as %qT: ", child->get_type ());
+      pp_string (pp, "view as ");
+      print_quoted_type (pp, child->get_type ());
+      pp_string (pp, ": ");
     }
 }
 
@@ -1463,7 +1487,10 @@ region::print_fields (const region_model &model ATTRIBUTE_UNUSED,
   m_sval_id.print (pp);
 
   if (m_type)
-    pp_printf (pp, ", type: %qT", m_type);
+    {
+      pp_printf (pp, ", type: ");
+      print_quoted_type (pp, m_type);
+    }
 }
 
 /* Determine if a pointer to this region must be non-NULL.