[gcc r16-879] c++: add cxx_dump_pretty_printer
Jason Merrill
jason@gcc.gnu.org
Mon May 26 13:56:33 GMT 2025
https://gcc.gnu.org/g:d424245c7abb2871b977ddc5c1e73b827a78ad07
commit r16-879-gd424245c7abb2871b977ddc5c1e73b827a78ad07
Author: Jason Merrill <jason@redhat.com>
Date: Thu Apr 17 16:29:49 2025 -0400
c++: add cxx_dump_pretty_printer
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.
Diff:
---
gcc/cp/cp-tree.h | 23 +++++++++++++++++++++++
gcc/cp/error.cc | 27 +++++++++++++++++++++++++++
2 files changed, 50 insertions(+)
diff --git a/gcc/cp/cp-tree.h b/gcc/cp/cp-tree.h
index 175ab2874903..7433b8962199 100644
--- a/gcc/cp/cp-tree.h
+++ b/gcc/cp/cp-tree.h
@@ -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);
diff --git a/gcc/cp/error.cc b/gcc/cp/error.cc
index 305064d476c4..d52dad3db293 100644
--- a/gcc/cp/error.cc
+++ b/gcc/cp/error.cc
@@ -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. */
More information about the Gcc-cvs
mailing list