aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJason Merrill <jason@redhat.com>2025-04-17 16:29:49 -0400
committerJason Merrill <jason@redhat.com>2025-05-26 09:56:27 -0400
commitd424245c7abb2871b977ddc5c1e73b827a78ad07 (patch)
treeabbdd93bc08289349d6ee5fcbd582f435edabff3
parentcaf804b1795575d7714c62dd45b649831598055e (diff)
downloadgcc-d424245c7abb2871b977ddc5c1e73b827a78ad07.zip
gcc-d424245c7abb2871b977ddc5c1e73b827a78ad07.tar.gz
gcc-d424245c7abb2871b977ddc5c1e73b827a78ad07.tar.bz2
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.
-rw-r--r--gcc/cp/cp-tree.h23
-rw-r--r--gcc/cp/error.cc27
2 files changed, 50 insertions, 0 deletions
diff --git a/gcc/cp/cp-tree.h b/gcc/cp/cp-tree.h
index 175ab28..7433b89 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 305064d..d52dad3 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. */