Skip to content

Commit d424245

Browse files
committed
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.
1 parent caf804b commit d424245

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

gcc/cp/cp-tree.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7322,6 +7322,29 @@ extern void cp_check_const_attributes (tree);
73227322
extern void maybe_propagate_warmth_attributes (tree, tree);
73237323

73247324
/* in error.cc */
7325+
/* A class for pretty-printing to -flang-dump-XXX files. Used like
7326+
7327+
if (cxx_dump_pretty_printer pp {foo_dump_id})
7328+
{
7329+
pp_printf (&pp, ...);
7330+
}
7331+
7332+
If the dump is enabled, the pretty printer will open the dump file and
7333+
attach to it, and flush and close the file on destruction. */
7334+
7335+
class cxx_dump_pretty_printer: public pretty_printer
7336+
{
7337+
int phase;
7338+
FILE *outf;
7339+
dump_flags_t flags;
7340+
7341+
public:
7342+
cxx_dump_pretty_printer (int phase);
7343+
operator bool() { return outf != nullptr; }
7344+
bool has_flag (dump_flags_t f) { return (flags & f); }
7345+
~cxx_dump_pretty_printer ();
7346+
};
7347+
73257348
extern const char *type_as_string (tree, int);
73267349
extern const char *type_as_string_translate (tree, int);
73277350
extern const char *decl_as_string (tree, int);

gcc/cp/error.cc

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,33 @@ class cxx_format_postprocessor : public format_postprocessor
193193
deferred_printed_type m_type_b;
194194
};
195195

196+
/* Constructor and destructor for cxx_dump_pretty_printer, defined here to
197+
avoid needing to move cxx_format_postprocessor into the header as well. */
198+
199+
cxx_dump_pretty_printer::
200+
cxx_dump_pretty_printer (int phase)
201+
: phase (phase)
202+
{
203+
outf = dump_begin (phase, &flags);
204+
if (outf)
205+
{
206+
pp_format_decoder (this) = cp_printer;
207+
/* This gets deleted in ~pretty_printer. */
208+
pp_format_postprocessor (this) = new cxx_format_postprocessor ();
209+
set_output_stream (outf);
210+
}
211+
}
212+
213+
cxx_dump_pretty_printer::
214+
~cxx_dump_pretty_printer ()
215+
{
216+
if (outf)
217+
{
218+
pp_flush (this);
219+
dump_end (phase, outf);
220+
}
221+
}
222+
196223
/* Return the in-scope template that's currently being parsed, or
197224
NULL_TREE otherwise. */
198225

0 commit comments

Comments
 (0)