]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
c++: add cxx_dump_pretty_printer
authorJason Merrill <jason@redhat.com>
Thu, 17 Apr 2025 20:29:49 +0000 (16:29 -0400)
committerJason Merrill <jason@redhat.com>
Mon, 26 May 2025 13:56:27 +0000 (09:56 -0400)
A class to simplify implementation of -fdump-lang-foo with support for
pp_printf using %D and such.

gcc/cp/ChangeLog:

* cp-tree.h (class cxx_dump_pretty_printer): New.
* error.cc (cxx_dump_pretty_printer): Ctor/dtor definitions.

gcc/cp/cp-tree.h
gcc/cp/error.cc

index 175ab287490396d92d38fe48da411e99ec9eaa36..7433b8962199e4fb644d3c8955c241919fe19c3b 100644 (file)
@@ -7322,6 +7322,29 @@ extern void cp_check_const_attributes (tree);
 extern void maybe_propagate_warmth_attributes (tree, tree);
 
 /* in error.cc */
+/* A class for pretty-printing to -flang-dump-XXX files.  Used like
+
+   if (cxx_dump_pretty_printer pp {foo_dump_id})
+     {
+       pp_printf (&pp, ...);
+     }
+
+   If the dump is enabled, the pretty printer will open the dump file and
+   attach to it, and flush and close the file on destruction.  */
+
+class cxx_dump_pretty_printer: public pretty_printer
+{
+  int phase;
+  FILE *outf;
+  dump_flags_t flags;
+
+public:
+  cxx_dump_pretty_printer (int phase);
+  operator bool() { return outf != nullptr; }
+  bool has_flag (dump_flags_t f) { return (flags & f); }
+  ~cxx_dump_pretty_printer ();
+};
+
 extern const char *type_as_string              (tree, int);
 extern const char *type_as_string_translate    (tree, int);
 extern const char *decl_as_string              (tree, int);
index 305064d476c45732fcd397dfab256be253afe7bd..d52dad3db29365a4b0d3be970f88ff1d2d101d05 100644 (file)
@@ -193,6 +193,33 @@ class cxx_format_postprocessor : public format_postprocessor
   deferred_printed_type m_type_b;
 };
 
+/* Constructor and destructor for cxx_dump_pretty_printer, defined here to
+   avoid needing to move cxx_format_postprocessor into the header as well.  */
+
+cxx_dump_pretty_printer::
+cxx_dump_pretty_printer (int phase)
+  : phase (phase)
+{
+  outf = dump_begin (phase, &flags);
+  if (outf)
+    {
+      pp_format_decoder (this) = cp_printer;
+      /* This gets deleted in ~pretty_printer.  */
+      pp_format_postprocessor (this) = new cxx_format_postprocessor ();
+      set_output_stream (outf);
+    }
+}
+
+cxx_dump_pretty_printer::
+~cxx_dump_pretty_printer ()
+{
+  if (outf)
+    {
+      pp_flush (this);
+      dump_end (phase, outf);
+    }
+}
+
 /* Return the in-scope template that's currently being parsed, or
    NULL_TREE otherwise.  */