]>
Commit | Line | Data |
---|---|---|
38cf91e5 | 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>. | |
4 | ||
5 | This file is part of GCC. | |
6 | ||
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. | |
11 | ||
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. | |
16 | ||
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/>. */ | |
20 | ||
21 | ||
22 | #ifndef GCC_DUMP_CONTEXT_H | |
23 | #define GCC_DUMP_CONTEXT_H 1 | |
24 | ||
bffe1cb4 | 25 | #include "dumpfile.h" |
71e711b7 | 26 | #include "pretty-print.h" |
ed9370cc | 27 | #include "selftest.h" |
28 | ||
29 | namespace selftest { class temp_dump_context; } | |
71e711b7 | 30 | |
38cf91e5 | 31 | /* A class for handling the various dump_* calls. |
32 | ||
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. | |
36 | ||
37 | Putting this in a class (rather than as global state) allows | |
38 | for selftesting of this code. */ | |
39 | ||
40 | class dump_context | |
41 | { | |
ed9370cc | 42 | friend class selftest::temp_dump_context; |
43 | ||
38cf91e5 | 44 | public: |
45 | static dump_context &get () { return *s_current; } | |
46 | ||
47 | ~dump_context (); | |
48 | ||
71e711b7 | 49 | void refresh_dumps_are_enabled (); |
50 | ||
291c13cb | 51 | void dump_loc (dump_flags_t dump_kind, const dump_location_t &loc); |
ed9370cc | 52 | void dump_loc_immediate (dump_flags_t dump_kind, const dump_location_t &loc); |
291c13cb | 53 | |
38cf91e5 | 54 | void dump_gimple_stmt (dump_flags_t dump_kind, dump_flags_t extra_dump_flags, |
55 | gimple *gs, int spc); | |
56 | ||
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); | |
61 | ||
62 | void dump_gimple_expr (dump_flags_t dump_kind, | |
63 | dump_flags_t extra_dump_flags, | |
64 | gimple *gs, int spc); | |
65 | ||
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); | |
71 | ||
72 | void dump_generic_expr (dump_flags_t dump_kind, | |
73 | dump_flags_t extra_dump_flags, | |
74 | tree t); | |
75 | ||
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); | |
80 | ||
81 | void dump_printf_va (dump_flags_t dump_kind, const char *format, | |
bffe1cb4 | 82 | va_list *ap) ATTRIBUTE_GCC_DUMP_PRINTF (3, 0); |
38cf91e5 | 83 | |
84 | void dump_printf_loc_va (dump_flags_t dump_kind, const dump_location_t &loc, | |
bffe1cb4 | 85 | const char *format, va_list *ap) |
86 | ATTRIBUTE_GCC_DUMP_PRINTF (4, 0); | |
38cf91e5 | 87 | |
88 | template<unsigned int N, typename C> | |
89 | void dump_dec (dump_flags_t dump_kind, const poly_int<N, C> &value); | |
90 | ||
91 | void dump_symtab_node (dump_flags_t dump_kind, symtab_node *node); | |
92 | ||
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 (); | |
97 | ||
98 | /* For use in selftests; if true then optinfo_enabled_p is true. */ | |
99 | bool forcibly_enable_optinfo_p () const | |
100 | { | |
101 | return m_forcibly_enable_optinfo; | |
102 | } | |
103 | ||
104 | void end_any_optinfo (); | |
105 | ||
71e711b7 | 106 | void emit_item (optinfo_item *item, dump_flags_t dump_kind); |
107 | ||
9ddd8fa7 | 108 | bool apply_dump_filter_p (dump_flags_t dump_kind, dump_flags_t filter) const; |
109 | ||
38cf91e5 | 110 | private: |
111 | optinfo &ensure_pending_optinfo (); | |
112 | optinfo &begin_next_optinfo (const dump_location_t &loc); | |
113 | ||
114 | /* For use in selftests; if true then optinfo_enabled_p is true. */ | |
115 | bool m_forcibly_enable_optinfo; | |
116 | ||
117 | /* The current nesting depth of dump scopes, for showing nesting | |
118 | via indentation). */ | |
119 | unsigned int m_scope_depth; | |
120 | ||
121 | /* The optinfo currently being accumulated since the last dump_*_loc call, | |
122 | if any. */ | |
123 | optinfo *m_pending; | |
124 | ||
71e711b7 | 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; | |
129 | ||
38cf91e5 | 130 | /* The currently active dump_context, for use by the dump_* API calls. */ |
131 | static dump_context *s_current; | |
132 | ||
133 | /* The default active context. */ | |
134 | static dump_context s_default; | |
135 | }; | |
136 | ||
ed9370cc | 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. */ | |
141 | ||
142 | class dump_pretty_printer : public pretty_printer | |
143 | { | |
144 | public: | |
145 | dump_pretty_printer (dump_context *context, dump_flags_t dump_kind); | |
146 | ||
147 | void emit_items (optinfo *dest); | |
148 | ||
149 | private: | |
150 | /* Information on an optinfo_item that was generated during phase 2 of | |
151 | formatting. */ | |
152 | struct stashed_item | |
153 | { | |
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; | |
158 | }; | |
159 | ||
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); | |
165 | ||
166 | bool decode_format (text_info *text, const char *spec, | |
167 | const char **buffer_ptr); | |
168 | ||
169 | void stash_item (const char **buffer_ptr, optinfo_item *item); | |
170 | ||
171 | void emit_any_pending_textual_chunks (optinfo *dest); | |
172 | ||
173 | void emit_item (optinfo_item *item, optinfo *dest); | |
174 | ||
175 | dump_context *m_context; | |
176 | dump_flags_t m_dump_kind; | |
177 | auto_vec<stashed_item> m_stashed_items; | |
178 | }; | |
179 | ||
38cf91e5 | 180 | #if CHECKING_P |
181 | ||
ed9370cc | 182 | namespace selftest { |
183 | ||
38cf91e5 | 184 | /* An RAII-style class for use in selftests for temporarily using a different |
185 | dump_context. */ | |
186 | ||
187 | class temp_dump_context | |
188 | { | |
189 | public: | |
71e711b7 | 190 | temp_dump_context (bool forcibly_enable_optinfo, |
ed9370cc | 191 | bool forcibly_enable_dumping, |
71e711b7 | 192 | dump_flags_t test_pp_flags); |
38cf91e5 | 193 | ~temp_dump_context (); |
194 | ||
195 | /* Support for selftests. */ | |
196 | optinfo *get_pending_optinfo () const { return m_context.m_pending; } | |
71e711b7 | 197 | const char *get_dumped_text (); |
38cf91e5 | 198 | |
199 | private: | |
71e711b7 | 200 | pretty_printer m_pp; |
38cf91e5 | 201 | dump_context m_context; |
202 | dump_context *m_saved; | |
38cf91e5 | 203 | }; |
204 | ||
ed9370cc | 205 | /* Implementation detail of ASSERT_DUMPED_TEXT_EQ. */ |
206 | ||
207 | extern void verify_dumped_text (const location &loc, | |
208 | temp_dump_context *context, | |
209 | const char *expected_text); | |
210 | ||
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. */ | |
214 | ||
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 | |
219 | ||
220 | ||
221 | /* Verify that ITEM has the expected values. */ | |
222 | ||
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); | |
229 | ||
230 | /* Verify that ITEM is a text item, with EXPECTED_TEXT. */ | |
231 | ||
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 | |
237 | ||
238 | /* Verify that ITEM is a tree item, with the expected values. */ | |
239 | ||
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 | |
245 | ||
246 | /* Verify that ITEM is a gimple item, with the expected values. */ | |
247 | ||
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 | |
253 | ||
254 | } // namespace selftest | |
255 | ||
38cf91e5 | 256 | #endif /* CHECKING_P */ |
257 | ||
258 | #endif /* GCC_DUMP_CONTEXT_H */ |