Tested x86_64-pc-linux-gnu, applying to trunk. -- 8< --
A class to simplify implementation of -fdump-lang-foo with support for pp_printf using %D and such. gcc/cp/ChangeLog: * cxx-pretty-print.h (class cxx_dump_pretty_printer): New. * error.cc (cxx_dump_pretty_printer): Ctor/dtor definitions. --- 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 175ab287490..7433b896219 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 305064d476c..d52dad3db29 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. */ base-commit: 545433e9bd32e965726956cb238d53b39844b85c -- 2.49.0