aboutsummaryrefslogtreecommitdiff
path: root/gcc/text-art/dump.h
blob: e94f308f8ceecca88ab4a0014fa993994fab11e0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/* Templates for dumping objects.
   Copyright (C) 2024 Free Software Foundation, Inc.
   Contributed by David Malcolm <dmalcolm@redhat.com>.

This file is part of GCC.

GCC is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3, or (at your option)
any later version.

GCC is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
General Public License for more details.

You should have received a copy of the GNU General Public License
along with GCC; see the file COPYING3.  If not see
<http://www.gnu.org/licenses/>.  */

#ifndef GCC_TEXT_ART_DUMP_H
#define GCC_TEXT_ART_DUMP_H

#include "tree-diagnostic.h"
#include "text-art/canvas.h"
#include "text-art/widget.h"
#include "text-art/dump-widget-info.h"

/* A family of templates for dumping objects via the text_art::widget
   system.
   Any type T that has a make_dump_widget member function ought to be
   dumpable via these functions.  */

namespace text_art {

/* Dump OBJ to PP, using OBJ's make_dump_widget member function.  */

template <typename T>
void dump_to_pp (const T &obj, text_art::theme *theme, pretty_printer *pp)
{
  if (!theme)
    return;

  style_manager sm;
  style tree_style (get_style_from_color_cap_name ("note"));

  style::id_t tree_style_id (sm.get_or_create_id (tree_style));

  dump_widget_info dwi (sm, *theme, tree_style_id);
  if (std::unique_ptr<widget> w = obj.make_dump_widget (dwi))
    {
      text_art::canvas c (w->to_canvas (dwi.m_sm));
      c.print_to_pp (pp);
    }
}

/* Dump OBJ to OUTF, using OBJ's make_dump_widget member function.  */

template <typename T>
void dump_to_file (const T &obj, FILE *outf)
{
  pretty_printer pp;
  pp_format_decoder (&pp) = default_tree_printer;
  if (outf == stderr)
    pp_show_color (&pp) = pp_show_color (global_dc->printer);
  pp.buffer->stream = outf;

  text_art::theme *theme = global_dc->get_diagram_theme ();
  dump_to_pp (obj, theme, &pp);
  pp_flush (&pp);
}

/* Dump OBJ to stderr, using OBJ's make_dump_widget member function.  */

template <typename T>
void dump (const T &obj)
{
  dump_to_file (obj, stderr);
}

} // namespace text_art

#endif /* GCC_TEXT_ART_DUMP_H */