GCC Middle and Back End API Reference
dump-context.h
Go to the documentation of this file.
1/* Support code for handling the various dump_* calls in dumpfile.h
2 Copyright (C) 2018-2024 Free Software Foundation, Inc.
3 Contributed by David Malcolm <dmalcolm@redhat.com>.
4
5This file is part of GCC.
6
7GCC is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
9the Free Software Foundation; either version 3, or (at your option)
10any later version.
11
12GCC is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with GCC; see the file COPYING3. If not see
19<http://www.gnu.org/licenses/>. */
20
21
22#ifndef GCC_DUMP_CONTEXT_H
23#define GCC_DUMP_CONTEXT_H 1
24
25#include "dumpfile.h"
26#include "pretty-print.h"
27#include "selftest.h"
28#include "optinfo.h"
29
31namespace selftest { class temp_dump_context; }
33
34/* A class for handling the various dump_* calls.
35
36 In particular, this class has responsibility for consolidating
37 the "dump_*" calls into optinfo instances (delimited by "dump_*_loc"
38 calls), and emitting them.
39
40 Putting this in a class (rather than as global state) allows
41 for selftesting of this code. */
42
44{
46 friend class debug_dump_context;
47
48 public:
49 static dump_context &get () { return *s_current; }
50
52
54
55 void dump_loc (const dump_metadata_t &metadata,
56 const dump_user_location_t &loc);
58 const dump_user_location_t &loc);
59
60 void dump_gimple_stmt (const dump_metadata_t &metadata,
61 dump_flags_t extra_dump_flags,
62 gimple *gs, int spc);
63
64 void dump_gimple_stmt_loc (const dump_metadata_t &metadata,
65 const dump_user_location_t &loc,
66 dump_flags_t extra_dump_flags,
67 gimple *gs, int spc);
68
69 void dump_gimple_expr (const dump_metadata_t &metadata,
70 dump_flags_t extra_dump_flags,
71 gimple *gs, int spc);
72
73 void dump_gimple_expr_loc (const dump_metadata_t &metadata,
74 const dump_user_location_t &loc,
75 dump_flags_t extra_dump_flags,
76 gimple *gs,
77 int spc);
78
79 void dump_generic_expr (const dump_metadata_t &metadata,
80 dump_flags_t extra_dump_flags,
81 tree t);
82
83 void dump_generic_expr_loc (const dump_metadata_t &metadata,
84 const dump_user_location_t &loc,
85 dump_flags_t extra_dump_flags,
86 tree t);
87
88 void dump_printf_va (const dump_metadata_t &metadata, const char *format,
89 va_list *ap) ATTRIBUTE_GCC_DUMP_PRINTF (3, 0);
90
91 void dump_printf_loc_va (const dump_metadata_t &metadata,
92 const dump_user_location_t &loc,
93 const char *format, va_list *ap)
95
96 template<unsigned int N, typename C>
97 void dump_dec (const dump_metadata_t &metadata, const poly_int<N, C> &value);
98
99 void dump_symtab_node (const dump_metadata_t &metadata, symtab_node *node);
100
101 /* Managing nested scopes. */
102 unsigned int get_scope_depth () const;
103 void begin_scope (const char *name,
104 const dump_user_location_t &user_location,
105 const dump_impl_location_t &impl_location);
106 void end_scope ();
107
108 /* Should optinfo instances be created?
109 All creation of optinfos should be guarded by this predicate.
110 Return true if any optinfo destinations are active. */
111 bool optinfo_enabled_p () const;
112
114 {
115 return m_json_writer != NULL;
116 }
119
120 void end_any_optinfo ();
121
122 void emit_optinfo (const optinfo *info);
123 void emit_item (const optinfo_item &item, dump_flags_t dump_kind);
124
126
127 private:
130 const dump_user_location_t &loc);
131
132 /* The current nesting depth of dump scopes, for showing nesting
133 via indentation). */
134 unsigned int m_scope_depth;
135
136 /* The optinfo currently being accumulated since the last dump_*_loc call,
137 if any. */
139
140 /* If -fsave-optimization-record is enabled, the heap-allocated JSON writer
141 instance, otherwise NULL. */
143
144 /* For use in selftests: if non-NULL, then items are to be printed
145 to this, using the given flags. */
148
149 /* The currently active dump_context, for use by the dump_* API calls. */
151
152 /* The default active context. */
154};
155
156/* A subclass of pretty_printer for implementing dump_context::dump_printf_va.
157 In particular, the formatted chunks are captured as optinfo_item instances
158 as pp_token_custom_data, thus retaining metadata about the entities being
159 dumped (e.g. source locations), rather than just as plain text.
160 These custom items are retained through to the end of stage 3 of formatted
161 printing; the printer uses a custom token_printer subclass to emit them to
162 the active optinfo (if any). */
163
165{
166public:
168
170
171private:
173 {
175 : m_dump_pp (dump_pp),
176 m_optinfo (nullptr)
177 {}
179 const pp_token_list &tokens) final override;
181
184 };
185
186 static bool format_decoder_cb (pretty_printer *pp, text_info *text,
187 const char *spec, int /*precision*/,
188 bool /*wide*/, bool /*set_locus*/,
189 bool /*verbose*/, bool */*quoted*/,
190 pp_token_list &formatted_tok_list);
191
192 bool decode_format (text_info *text, const char *spec,
193 pp_token_list &formatted_tok_list);
194
195 void stash_item (pp_token_list &formatted_tok_list,
196 std::unique_ptr<optinfo_item> item);
197
198 void emit_item (std::unique_ptr<optinfo_item> item, optinfo *dest);
199
203};
204
205/* An RAII-style class for use in debug dumpers for temporarily using a
206 different dump_context. It enables full details and outputs to
207 stderr instead of the currently active dump_file. */
208
222
223
224#if CHECKING_P
225
226namespace selftest {
227
228/* An RAII-style class for use in selftests for temporarily using a different
229 dump_context. */
230
231class temp_dump_context
232{
233 public:
234 temp_dump_context (bool forcibly_enable_optinfo,
235 bool forcibly_enable_dumping,
236 dump_flags_t test_pp_flags);
237 ~temp_dump_context ();
238
239 /* Support for selftests. */
240 optinfo *get_pending_optinfo () const { return m_context.m_pending; }
241 const char *get_dumped_text ();
242
243 private:
244 pretty_printer m_pp;
245 dump_context m_context;
246 dump_context *m_saved;
247};
248
249/* Implementation detail of ASSERT_DUMPED_TEXT_EQ. */
250
251extern void verify_dumped_text (const location &loc,
252 temp_dump_context *context,
253 const char *expected_text);
254
255/* Verify that the text dumped so far in CONTEXT equals
256 EXPECTED_TEXT.
257 As a side-effect, the internal buffer is 0-terminated. */
258
259#define ASSERT_DUMPED_TEXT_EQ(CONTEXT, EXPECTED_TEXT) \
260 SELFTEST_BEGIN_STMT \
261 verify_dumped_text (SELFTEST_LOCATION, &(CONTEXT), (EXPECTED_TEXT)); \
262 SELFTEST_END_STMT
263
264
265/* Verify that ITEM has the expected values. */
266
267void
268verify_item (const location &loc,
269 const optinfo_item *item,
270 enum optinfo_item_kind expected_kind,
271 location_t expected_location,
272 const char *expected_text);
273
274/* Verify that ITEM is a text item, with EXPECTED_TEXT. */
275
276#define ASSERT_IS_TEXT(ITEM, EXPECTED_TEXT) \
277 SELFTEST_BEGIN_STMT \
278 verify_item (SELFTEST_LOCATION, (ITEM), OPTINFO_ITEM_KIND_TEXT, \
279 UNKNOWN_LOCATION, (EXPECTED_TEXT)); \
280 SELFTEST_END_STMT
281
282/* Verify that ITEM is a tree item, with the expected values. */
283
284#define ASSERT_IS_TREE(ITEM, EXPECTED_LOCATION, EXPECTED_TEXT) \
285 SELFTEST_BEGIN_STMT \
286 verify_item (SELFTEST_LOCATION, (ITEM), OPTINFO_ITEM_KIND_TREE, \
287 (EXPECTED_LOCATION), (EXPECTED_TEXT)); \
288 SELFTEST_END_STMT
289
290/* Verify that ITEM is a gimple item, with the expected values. */
291
292#define ASSERT_IS_GIMPLE(ITEM, EXPECTED_LOCATION, EXPECTED_TEXT) \
293 SELFTEST_BEGIN_STMT \
294 verify_item (SELFTEST_LOCATION, (ITEM), OPTINFO_ITEM_KIND_GIMPLE, \
295 (EXPECTED_LOCATION), (EXPECTED_TEXT)); \
296 SELFTEST_END_STMT
297
298/* Verify that ITEM is a symtab node, with the expected values. */
299
300#define ASSERT_IS_SYMTAB_NODE(ITEM, EXPECTED_LOCATION, EXPECTED_TEXT) \
301 SELFTEST_BEGIN_STMT \
302 verify_item (SELFTEST_LOCATION, (ITEM), OPTINFO_ITEM_KIND_SYMTAB_NODE, \
303 (EXPECTED_LOCATION), (EXPECTED_TEXT)); \
304 SELFTEST_END_STMT
305
306} // namespace selftest
307
308#endif /* CHECKING_P */
309
310#endif /* GCC_DUMP_CONTEXT_H */
Definition dump-context.h:210
dump_flags_t m_saved_pflags
Definition dump-context.h:219
debug_dump_context(FILE *f=stderr)
Definition dumpfile.cc:2117
dump_context m_context
Definition dump-context.h:216
~debug_dump_context()
Definition dumpfile.cc:2132
dump_flags_t m_saved_flags
Definition dump-context.h:218
dump_context * m_saved
Definition dump-context.h:217
FILE * m_saved_file
Definition dump-context.h:220
Definition dump-context.h:44
optinfo & ensure_pending_optinfo(const dump_metadata_t &metadata)
Definition dumpfile.cc:1208
void emit_optinfo(const optinfo *info)
Definition dumpfile.cc:1247
void dump_loc_immediate(dump_flags_t dump_kind, const dump_user_location_t &loc)
Definition dumpfile.cc:614
void begin_scope(const char *name, const dump_user_location_t &user_location, const dump_impl_location_t &impl_location)
Definition dumpfile.cc:1142
void dump_gimple_stmt_loc(const dump_metadata_t &metadata, const dump_user_location_t &loc, dump_flags_t extra_dump_flags, gimple *gs, int spc)
Definition dumpfile.cc:669
optinfo & begin_next_optinfo(const dump_metadata_t &metadata, const dump_user_location_t &loc)
Definition dumpfile.cc:1219
void dump_gimple_stmt(const dump_metadata_t &metadata, dump_flags_t extra_dump_flags, gimple *gs, int spc)
Definition dumpfile.cc:651
bool optinfo_enabled_p() const
Definition dumpfile.cc:1199
static dump_context s_default
Definition dump-context.h:153
void set_json_writer(optrecord_json_writer *writer)
Definition dumpfile.cc:536
friend class selftest::temp_dump_context
Definition dump-context.h:45
void refresh_dumps_are_enabled()
Definition dumpfile.cc:561
pretty_printer * m_test_pp
Definition dump-context.h:146
void dump_printf_va(const dump_metadata_t &metadata, const char *format, va_list *ap) ATTRIBUTE_GCC_DUMP_PRINTF(3
Definition dumpfile.cc:1029
optinfo * m_pending
Definition dump-context.h:138
void dump_gimple_expr_loc(const dump_metadata_t &metadata, const dump_user_location_t &loc, dump_flags_t extra_dump_flags, gimple *gs, int spc)
Definition dumpfile.cc:718
void dump_loc(const dump_metadata_t &metadata, const dump_user_location_t &loc)
Definition dumpfile.cc:600
bool optimization_records_enabled_p() const
Definition dump-context.h:113
void void dump_printf_loc_va(const dump_metadata_t &metadata, const dump_user_location_t &loc, const char *format, va_list *ap) ATTRIBUTE_GCC_DUMP_PRINTF(4
Definition dumpfile.cc:1056
~dump_context()
Definition dumpfile.cc:530
void dump_generic_expr_loc(const dump_metadata_t &metadata, const dump_user_location_t &loc, dump_flags_t extra_dump_flags, tree t)
Definition dumpfile.cc:772
static dump_context * s_current
Definition dump-context.h:150
unsigned int get_scope_depth() const
Definition dumpfile.cc:1130
void emit_item(const optinfo_item &item, dump_flags_t dump_kind)
Definition dumpfile.cc:1258
void end_any_optinfo()
Definition dumpfile.cc:1234
bool apply_dump_filter_p(dump_flags_t dump_kind, dump_flags_t filter) const
Definition dumpfile.cc:574
optrecord_json_writer * m_json_writer
Definition dump-context.h:142
void dump_gimple_expr(const dump_metadata_t &metadata, dump_flags_t extra_dump_flags, gimple *gs, int spc)
Definition dumpfile.cc:700
void end_scope()
Definition dumpfile.cc:1185
unsigned int m_scope_depth
Definition dump-context.h:134
static dump_context & get()
Definition dump-context.h:49
void void void dump_dec(const dump_metadata_t &metadata, const poly_int< N, C > &value)
void finish_any_json_writer()
Definition dumpfile.cc:547
void dump_symtab_node(const dump_metadata_t &metadata, symtab_node *node)
Definition dumpfile.cc:1113
void dump_generic_expr(const dump_metadata_t &metadata, dump_flags_t extra_dump_flags, tree t)
Definition dumpfile.cc:752
dump_flags_t m_test_pp_flags
Definition dump-context.h:147
Definition dumpfile.h:381
Definition dumpfile.h:414
Definition dump-context.h:165
dump_context * m_context
Definition dump-context.h:200
dump_flags_t m_dump_kind
Definition dump-context.h:201
void stash_item(pp_token_list &formatted_tok_list, std::unique_ptr< optinfo_item > item)
Definition dumpfile.cc:844
void emit_item(std::unique_ptr< optinfo_item > item, optinfo *dest)
Definition dumpfile.cc:832
static bool format_decoder_cb(pretty_printer *pp, text_info *text, const char *spec, int, bool, bool, bool, bool @endverbatim *, pp_token_list &formatted_tok_list)
Definition dumpfile.cc:860
custom_token_printer m_token_printer
Definition dump-context.h:202
bool decode_format(text_info *text, const char *spec, pp_token_list &formatted_tok_list)
Definition dumpfile.cc:893
void set_optinfo(optinfo *info)
Definition dump-context.h:169
dump_pretty_printer(dump_context *context, dump_flags_t dump_kind)
Definition dumpfile.cc:817
Definition dumpfile.h:340
Definition optinfo.h:151
Definition optinfo.h:94
Definition optinfo-emit-json.h:31
Definition poly-int.h:378
Definition pretty-print-format-impl.h:300
Definition pretty-print.h:241
Definition pretty-print.h:208
union tree_node * tree
Definition coretypes.h:97
dump_kind
Definition dumpfile.h:64
#define ATTRIBUTE_GCC_DUMP_PRINTF(m, n)
Definition dumpfile.h:36
enum dump_flag dump_flags_t
Definition dumpfile.h:209
Definition dump-context.h:31
optinfo_item_kind
Definition optinfo.h:141
static void const char va_list ap
Definition read-md.cc:205
Definition dump-context.h:173
dump_pretty_printer & m_dump_pp
Definition dump-context.h:182
optinfo * m_optinfo
Definition dump-context.h:183
custom_token_printer(dump_pretty_printer &dump_pp)
Definition dump-context.h:174
void emit_any_pending_textual_chunks()
Definition dumpfile.cc:1004
void print_tokens(pretty_printer *pp, const pp_token_list &tokens) final override
Definition dumpfile.cc:947
Definition gimple.h:221
Definition cgraph.h:106
Definition pretty-print.h:34
#define NULL
Definition system.h:50