1 /* Support code for handling the various dump_* calls in dumpfile.h
2 Copyright (C) 2018 Free Software Foundation, Inc.
3 Contributed by David Malcolm <dmalcolm@redhat.com>.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
22 #ifndef GCC_DUMP_CONTEXT_H
23 #define GCC_DUMP_CONTEXT_H 1
26 #include "pretty-print.h"
30 class optrecord_json_writer
;
31 namespace selftest
{ class temp_dump_context
; }
33 /* A class for handling the various dump_* calls.
35 In particular, this class has responsibility for consolidating
36 the "dump_*" calls into optinfo instances (delimited by "dump_*_loc"
37 calls), and emitting them.
39 Putting this in a class (rather than as global state) allows
40 for selftesting of this code. */
44 friend class selftest::temp_dump_context
;
47 static dump_context
&get () { return *s_current
; }
51 void refresh_dumps_are_enabled ();
53 void dump_loc (dump_flags_t dump_kind
, const dump_location_t
&loc
);
54 void dump_loc_immediate (dump_flags_t dump_kind
, const dump_location_t
&loc
);
56 void dump_gimple_stmt (dump_flags_t dump_kind
, dump_flags_t extra_dump_flags
,
59 void dump_gimple_stmt_loc (dump_flags_t dump_kind
,
60 const dump_location_t
&loc
,
61 dump_flags_t extra_dump_flags
,
64 void dump_gimple_expr (dump_flags_t dump_kind
,
65 dump_flags_t extra_dump_flags
,
68 void dump_gimple_expr_loc (dump_flags_t dump_kind
,
69 const dump_location_t
&loc
,
70 dump_flags_t extra_dump_flags
,
74 void dump_generic_expr (dump_flags_t dump_kind
,
75 dump_flags_t extra_dump_flags
,
78 void dump_generic_expr_loc (dump_flags_t dump_kind
,
79 const dump_location_t
&loc
,
80 dump_flags_t extra_dump_flags
,
83 void dump_printf_va (dump_flags_t dump_kind
, const char *format
,
84 va_list *ap
) ATTRIBUTE_GCC_DUMP_PRINTF (3, 0);
86 void dump_printf_loc_va (dump_flags_t dump_kind
, const dump_location_t
&loc
,
87 const char *format
, va_list *ap
)
88 ATTRIBUTE_GCC_DUMP_PRINTF (4, 0);
90 template<unsigned int N
, typename C
>
91 void dump_dec (dump_flags_t dump_kind
, const poly_int
<N
, C
> &value
);
93 void dump_symtab_node (dump_flags_t dump_kind
, symtab_node
*node
);
95 /* Managing nested scopes. */
96 unsigned int get_scope_depth () const;
97 void begin_scope (const char *name
, const dump_location_t
&loc
);
100 /* Should optinfo instances be created?
101 All creation of optinfos should be guarded by this predicate.
102 Return true if any optinfo destinations are active. */
103 bool optinfo_enabled_p () const;
105 bool optimization_records_enabled_p () const
107 return m_json_writer
!= NULL
;
109 void set_json_writer (optrecord_json_writer
*writer
);
110 void finish_any_json_writer ();
112 void end_any_optinfo ();
114 void emit_optinfo (const optinfo
*info
);
115 void emit_item (optinfo_item
*item
, dump_flags_t dump_kind
);
117 bool apply_dump_filter_p (dump_flags_t dump_kind
, dump_flags_t filter
) const;
120 optinfo
&ensure_pending_optinfo ();
121 optinfo
&begin_next_optinfo (const dump_location_t
&loc
);
123 /* The current nesting depth of dump scopes, for showing nesting
125 unsigned int m_scope_depth
;
127 /* The optinfo currently being accumulated since the last dump_*_loc call,
131 /* If -fsave-optimization-record is enabled, the heap-allocated JSON writer
132 instance, otherwise NULL. */
133 optrecord_json_writer
*m_json_writer
;
135 /* For use in selftests: if non-NULL, then items are to be printed
136 to this, using the given flags. */
137 pretty_printer
*m_test_pp
;
138 dump_flags_t m_test_pp_flags
;
140 /* The currently active dump_context, for use by the dump_* API calls. */
141 static dump_context
*s_current
;
143 /* The default active context. */
144 static dump_context s_default
;
147 /* A subclass of pretty_printer for implementing dump_context::dump_printf_va.
148 In particular, the formatted chunks are captured as optinfo_item instances,
149 thus retaining metadata about the entities being dumped (e.g. source
150 locations), rather than just as plain text. */
152 class dump_pretty_printer
: public pretty_printer
155 dump_pretty_printer (dump_context
*context
, dump_flags_t dump_kind
);
157 void emit_items (optinfo
*dest
);
160 /* Information on an optinfo_item that was generated during phase 2 of
164 stashed_item (const char **buffer_ptr_
, optinfo_item
*item_
)
165 : buffer_ptr (buffer_ptr_
), item (item_
) {}
166 const char **buffer_ptr
;
170 static bool format_decoder_cb (pretty_printer
*pp
, text_info
*text
,
171 const char *spec
, int /*precision*/,
172 bool /*wide*/, bool /*set_locus*/,
173 bool /*verbose*/, bool */
*quoted*/
,
174 const char **buffer_ptr
);
176 bool decode_format (text_info
*text
, const char *spec
,
177 const char **buffer_ptr
);
179 void stash_item (const char **buffer_ptr
, optinfo_item
*item
);
181 void emit_any_pending_textual_chunks (optinfo
*dest
);
183 void emit_item (optinfo_item
*item
, optinfo
*dest
);
185 dump_context
*m_context
;
186 dump_flags_t m_dump_kind
;
187 auto_vec
<stashed_item
> m_stashed_items
;
194 /* An RAII-style class for use in selftests for temporarily using a different
197 class temp_dump_context
200 temp_dump_context (bool forcibly_enable_optinfo
,
201 bool forcibly_enable_dumping
,
202 dump_flags_t test_pp_flags
);
203 ~temp_dump_context ();
205 /* Support for selftests. */
206 optinfo
*get_pending_optinfo () const { return m_context
.m_pending
; }
207 const char *get_dumped_text ();
211 dump_context m_context
;
212 dump_context
*m_saved
;
215 /* Implementation detail of ASSERT_DUMPED_TEXT_EQ. */
217 extern void verify_dumped_text (const location
&loc
,
218 temp_dump_context
*context
,
219 const char *expected_text
);
221 /* Verify that the text dumped so far in CONTEXT equals
223 As a side-effect, the internal buffer is 0-terminated. */
225 #define ASSERT_DUMPED_TEXT_EQ(CONTEXT, EXPECTED_TEXT) \
226 SELFTEST_BEGIN_STMT \
227 verify_dumped_text (SELFTEST_LOCATION, &(CONTEXT), (EXPECTED_TEXT)); \
231 /* Verify that ITEM has the expected values. */
234 verify_item (const location
&loc
,
235 const optinfo_item
*item
,
236 enum optinfo_item_kind expected_kind
,
237 location_t expected_location
,
238 const char *expected_text
);
240 /* Verify that ITEM is a text item, with EXPECTED_TEXT. */
242 #define ASSERT_IS_TEXT(ITEM, EXPECTED_TEXT) \
243 SELFTEST_BEGIN_STMT \
244 verify_item (SELFTEST_LOCATION, (ITEM), OPTINFO_ITEM_KIND_TEXT, \
245 UNKNOWN_LOCATION, (EXPECTED_TEXT)); \
248 /* Verify that ITEM is a tree item, with the expected values. */
250 #define ASSERT_IS_TREE(ITEM, EXPECTED_LOCATION, EXPECTED_TEXT) \
251 SELFTEST_BEGIN_STMT \
252 verify_item (SELFTEST_LOCATION, (ITEM), OPTINFO_ITEM_KIND_TREE, \
253 (EXPECTED_LOCATION), (EXPECTED_TEXT)); \
256 /* Verify that ITEM is a gimple item, with the expected values. */
258 #define ASSERT_IS_GIMPLE(ITEM, EXPECTED_LOCATION, EXPECTED_TEXT) \
259 SELFTEST_BEGIN_STMT \
260 verify_item (SELFTEST_LOCATION, (ITEM), OPTINFO_ITEM_KIND_GIMPLE, \
261 (EXPECTED_LOCATION), (EXPECTED_TEXT)); \
264 /* Verify that ITEM is a symtab node, with the expected values. */
266 #define ASSERT_IS_SYMTAB_NODE(ITEM, EXPECTED_LOCATION, EXPECTED_TEXT) \
267 SELFTEST_BEGIN_STMT \
268 verify_item (SELFTEST_LOCATION, (ITEM), OPTINFO_ITEM_KIND_SYMTAB_NODE, \
269 (EXPECTED_LOCATION), (EXPECTED_TEXT)); \
272 } // namespace selftest
274 #endif /* CHECKING_P */
276 #endif /* GCC_DUMP_CONTEXT_H */