2018-11-11 Richard Biener <rguenther@suse.de>
[official-gcc.git] / gcc / dump-context.h
blobace139cdc09c10ce523356813abeac11f072521e
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)
10 any later version.
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
25 #include "dumpfile.h"
26 #include "pretty-print.h"
27 #include "selftest.h"
29 namespace selftest { class temp_dump_context; }
31 /* A class for handling the various dump_* calls.
33 In particular, this class has responsibility for consolidating
34 the "dump_*" calls into optinfo instances (delimited by "dump_*_loc"
35 calls), and emitting them.
37 Putting this in a class (rather than as global state) allows
38 for selftesting of this code. */
40 class dump_context
42 friend class selftest::temp_dump_context;
44 public:
45 static dump_context &get () { return *s_current; }
47 ~dump_context ();
49 void refresh_dumps_are_enabled ();
51 void dump_loc (dump_flags_t dump_kind, const dump_location_t &loc);
52 void dump_loc_immediate (dump_flags_t dump_kind, const dump_location_t &loc);
54 void dump_gimple_stmt (dump_flags_t dump_kind, dump_flags_t extra_dump_flags,
55 gimple *gs, int spc);
57 void dump_gimple_stmt_loc (dump_flags_t dump_kind,
58 const dump_location_t &loc,
59 dump_flags_t extra_dump_flags,
60 gimple *gs, int spc);
62 void dump_gimple_expr (dump_flags_t dump_kind,
63 dump_flags_t extra_dump_flags,
64 gimple *gs, int spc);
66 void dump_gimple_expr_loc (dump_flags_t dump_kind,
67 const dump_location_t &loc,
68 dump_flags_t extra_dump_flags,
69 gimple *gs,
70 int spc);
72 void dump_generic_expr (dump_flags_t dump_kind,
73 dump_flags_t extra_dump_flags,
74 tree t);
76 void dump_generic_expr_loc (dump_flags_t dump_kind,
77 const dump_location_t &loc,
78 dump_flags_t extra_dump_flags,
79 tree t);
81 void dump_printf_va (dump_flags_t dump_kind, const char *format,
82 va_list *ap) ATTRIBUTE_GCC_DUMP_PRINTF (3, 0);
84 void dump_printf_loc_va (dump_flags_t dump_kind, const dump_location_t &loc,
85 const char *format, va_list *ap)
86 ATTRIBUTE_GCC_DUMP_PRINTF (4, 0);
88 template<unsigned int N, typename C>
89 void dump_dec (dump_flags_t dump_kind, const poly_int<N, C> &value);
91 void dump_symtab_node (dump_flags_t dump_kind, symtab_node *node);
93 /* Managing nested scopes. */
94 unsigned int get_scope_depth () const;
95 void begin_scope (const char *name, const dump_location_t &loc);
96 void end_scope ();
98 /* For use in selftests; if true then optinfo_enabled_p is true. */
99 bool forcibly_enable_optinfo_p () const
101 return m_forcibly_enable_optinfo;
104 void end_any_optinfo ();
106 void emit_item (optinfo_item *item, dump_flags_t dump_kind);
108 bool apply_dump_filter_p (dump_flags_t dump_kind, dump_flags_t filter) const;
110 private:
111 optinfo &ensure_pending_optinfo ();
112 optinfo &begin_next_optinfo (const dump_location_t &loc);
114 /* For use in selftests; if true then optinfo_enabled_p is true. */
115 bool m_forcibly_enable_optinfo;
117 /* The current nesting depth of dump scopes, for showing nesting
118 via indentation). */
119 unsigned int m_scope_depth;
121 /* The optinfo currently being accumulated since the last dump_*_loc call,
122 if any. */
123 optinfo *m_pending;
125 /* For use in selftests: if non-NULL, then items are to be printed
126 to this, using the given flags. */
127 pretty_printer *m_test_pp;
128 dump_flags_t m_test_pp_flags;
130 /* The currently active dump_context, for use by the dump_* API calls. */
131 static dump_context *s_current;
133 /* The default active context. */
134 static dump_context s_default;
137 /* A subclass of pretty_printer for implementing dump_context::dump_printf_va.
138 In particular, the formatted chunks are captured as optinfo_item instances,
139 thus retaining metadata about the entities being dumped (e.g. source
140 locations), rather than just as plain text. */
142 class dump_pretty_printer : public pretty_printer
144 public:
145 dump_pretty_printer (dump_context *context, dump_flags_t dump_kind);
147 void emit_items (optinfo *dest);
149 private:
150 /* Information on an optinfo_item that was generated during phase 2 of
151 formatting. */
152 struct stashed_item
154 stashed_item (const char **buffer_ptr_, optinfo_item *item_)
155 : buffer_ptr (buffer_ptr_), item (item_) {}
156 const char **buffer_ptr;
157 optinfo_item *item;
160 static bool format_decoder_cb (pretty_printer *pp, text_info *text,
161 const char *spec, int /*precision*/,
162 bool /*wide*/, bool /*set_locus*/,
163 bool /*verbose*/, bool */*quoted*/,
164 const char **buffer_ptr);
166 bool decode_format (text_info *text, const char *spec,
167 const char **buffer_ptr);
169 void stash_item (const char **buffer_ptr, optinfo_item *item);
171 void emit_any_pending_textual_chunks (optinfo *dest);
173 void emit_item (optinfo_item *item, optinfo *dest);
175 dump_context *m_context;
176 dump_flags_t m_dump_kind;
177 auto_vec<stashed_item> m_stashed_items;
180 #if CHECKING_P
182 namespace selftest {
184 /* An RAII-style class for use in selftests for temporarily using a different
185 dump_context. */
187 class temp_dump_context
189 public:
190 temp_dump_context (bool forcibly_enable_optinfo,
191 bool forcibly_enable_dumping,
192 dump_flags_t test_pp_flags);
193 ~temp_dump_context ();
195 /* Support for selftests. */
196 optinfo *get_pending_optinfo () const { return m_context.m_pending; }
197 const char *get_dumped_text ();
199 private:
200 pretty_printer m_pp;
201 dump_context m_context;
202 dump_context *m_saved;
205 /* Implementation detail of ASSERT_DUMPED_TEXT_EQ. */
207 extern void verify_dumped_text (const location &loc,
208 temp_dump_context *context,
209 const char *expected_text);
211 /* Verify that the text dumped so far in CONTEXT equals
212 EXPECTED_TEXT.
213 As a side-effect, the internal buffer is 0-terminated. */
215 #define ASSERT_DUMPED_TEXT_EQ(CONTEXT, EXPECTED_TEXT) \
216 SELFTEST_BEGIN_STMT \
217 verify_dumped_text (SELFTEST_LOCATION, &(CONTEXT), (EXPECTED_TEXT)); \
218 SELFTEST_END_STMT
221 /* Verify that ITEM has the expected values. */
223 void
224 verify_item (const location &loc,
225 const optinfo_item *item,
226 enum optinfo_item_kind expected_kind,
227 location_t expected_location,
228 const char *expected_text);
230 /* Verify that ITEM is a text item, with EXPECTED_TEXT. */
232 #define ASSERT_IS_TEXT(ITEM, EXPECTED_TEXT) \
233 SELFTEST_BEGIN_STMT \
234 verify_item (SELFTEST_LOCATION, (ITEM), OPTINFO_ITEM_KIND_TEXT, \
235 UNKNOWN_LOCATION, (EXPECTED_TEXT)); \
236 SELFTEST_END_STMT
238 /* Verify that ITEM is a tree item, with the expected values. */
240 #define ASSERT_IS_TREE(ITEM, EXPECTED_LOCATION, EXPECTED_TEXT) \
241 SELFTEST_BEGIN_STMT \
242 verify_item (SELFTEST_LOCATION, (ITEM), OPTINFO_ITEM_KIND_TREE, \
243 (EXPECTED_LOCATION), (EXPECTED_TEXT)); \
244 SELFTEST_END_STMT
246 /* Verify that ITEM is a gimple item, with the expected values. */
248 #define ASSERT_IS_GIMPLE(ITEM, EXPECTED_LOCATION, EXPECTED_TEXT) \
249 SELFTEST_BEGIN_STMT \
250 verify_item (SELFTEST_LOCATION, (ITEM), OPTINFO_ITEM_KIND_GIMPLE, \
251 (EXPECTED_LOCATION), (EXPECTED_TEXT)); \
252 SELFTEST_END_STMT
254 /* Verify that ITEM is a symtab node, with the expected values. */
256 #define ASSERT_IS_SYMTAB_NODE(ITEM, EXPECTED_LOCATION, EXPECTED_TEXT) \
257 SELFTEST_BEGIN_STMT \
258 verify_item (SELFTEST_LOCATION, (ITEM), OPTINFO_ITEM_KIND_SYMTAB_NODE, \
259 (EXPECTED_LOCATION), (EXPECTED_TEXT)); \
260 SELFTEST_END_STMT
262 } // namespace selftest
264 #endif /* CHECKING_P */
266 #endif /* GCC_DUMP_CONTEXT_H */