C-family, Objective-C [1/3] : Implement Wobjc-root-class [PR77404].
[gcc.git] / gcc / dumpfile.c
1 /* Dump infrastructure for optimizations and intermediate representation.
2 Copyright (C) 2012-2020 Free Software Foundation, Inc.
3
4 This file is part of GCC.
5
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
10
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
19
20 #include "config.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include "options.h"
24 #include "tree.h"
25 #include "gimple-pretty-print.h"
26 #include "diagnostic-core.h"
27 #include "dumpfile.h"
28 #include "context.h"
29 #include "profile-count.h"
30 #include "tree-cfg.h"
31 #include "langhooks.h"
32 #include "backend.h" /* for gimple.h. */
33 #include "gimple.h" /* for dump_user_location_t ctor. */
34 #include "rtl.h" /* for dump_user_location_t ctor. */
35 #include "selftest.h"
36 #include "optinfo.h"
37 #include "dump-context.h"
38 #include "cgraph.h"
39 #include "tree-pass.h" /* for "current_pass". */
40 #include "optinfo-emit-json.h"
41 #include "stringpool.h" /* for get_identifier. */
42 #include "spellcheck.h"
43
44 /* If non-NULL, return one past-the-end of the matching SUBPART of
45 the WHOLE string. */
46 #define skip_leading_substring(whole, part) \
47 (strncmp (whole, part, strlen (part)) ? NULL : whole + strlen (part))
48
49 static dump_flags_t pflags; /* current dump_flags */
50
51 static void dump_loc (dump_flags_t, FILE *, location_t);
52
53 /* Current -fopt-info output stream, if any, and flags. */
54 static FILE *alt_dump_file = NULL;
55 static dump_flags_t alt_flags;
56
57 static FILE *dump_open_alternate_stream (struct dump_file_info *);
58
59 /* These are currently used for communicating between passes.
60 However, instead of accessing them directly, the passes can use
61 dump_printf () for dumps. */
62 FILE *dump_file = NULL;
63 const char *dump_file_name;
64 dump_flags_t dump_flags;
65 bool dumps_are_enabled = false;
66
67
68 /* Set global "dump_file" to NEW_DUMP_FILE, refreshing the "dumps_are_enabled"
69 global. */
70
71 void
72 set_dump_file (FILE *new_dump_file)
73 {
74 dumpfile_ensure_any_optinfo_are_flushed ();
75 dump_file = new_dump_file;
76 dump_context::get ().refresh_dumps_are_enabled ();
77 }
78
79 /* Set "alt_dump_file" to NEW_ALT_DUMP_FILE, refreshing the "dumps_are_enabled"
80 global. */
81
82 static void
83 set_alt_dump_file (FILE *new_alt_dump_file)
84 {
85 dumpfile_ensure_any_optinfo_are_flushed ();
86 alt_dump_file = new_alt_dump_file;
87 dump_context::get ().refresh_dumps_are_enabled ();
88 }
89
90 #define DUMP_FILE_INFO(suffix, swtch, dkind, num) \
91 {suffix, swtch, NULL, NULL, NULL, NULL, NULL, dkind, TDF_NONE, TDF_NONE, \
92 OPTGROUP_NONE, 0, 0, num, false, false}
93
94 /* Table of tree dump switches. This must be consistent with the
95 TREE_DUMP_INDEX enumeration in dumpfile.h. */
96 static struct dump_file_info dump_files[TDI_end] =
97 {
98 DUMP_FILE_INFO (NULL, NULL, DK_none, 0),
99 DUMP_FILE_INFO (".cgraph", "ipa-cgraph", DK_ipa, 0),
100 DUMP_FILE_INFO (".type-inheritance", "ipa-type-inheritance", DK_ipa, 0),
101 DUMP_FILE_INFO (".ipa-clones", "ipa-clones", DK_ipa, 0),
102 DUMP_FILE_INFO (".original", "tree-original", DK_tree, 0),
103 DUMP_FILE_INFO (".gimple", "tree-gimple", DK_tree, 0),
104 DUMP_FILE_INFO (".nested", "tree-nested", DK_tree, 0),
105 DUMP_FILE_INFO (".lto-stream-out", "ipa-lto-stream-out", DK_ipa, 0),
106 DUMP_FILE_INFO (".profile-report", "profile-report", DK_ipa, 0),
107 #define FIRST_AUTO_NUMBERED_DUMP 1
108 #define FIRST_ME_AUTO_NUMBERED_DUMP 5
109
110 DUMP_FILE_INFO (NULL, "lang-all", DK_lang, 0),
111 DUMP_FILE_INFO (NULL, "tree-all", DK_tree, 0),
112 DUMP_FILE_INFO (NULL, "rtl-all", DK_rtl, 0),
113 DUMP_FILE_INFO (NULL, "ipa-all", DK_ipa, 0),
114 };
115
116 /* Table of dump options. This must be consistent with the TDF_* flags
117 in dumpfile.h and opt_info_options below. */
118 static const kv_pair<dump_flags_t> dump_options[] =
119 {
120 {"none", TDF_NONE},
121 {"address", TDF_ADDRESS},
122 {"asmname", TDF_ASMNAME},
123 {"slim", TDF_SLIM},
124 {"raw", TDF_RAW},
125 {"graph", TDF_GRAPH},
126 {"details", (TDF_DETAILS | MSG_OPTIMIZED_LOCATIONS
127 | MSG_MISSED_OPTIMIZATION
128 | MSG_NOTE)},
129 {"cselib", TDF_CSELIB},
130 {"stats", TDF_STATS},
131 {"blocks", TDF_BLOCKS},
132 {"vops", TDF_VOPS},
133 {"lineno", TDF_LINENO},
134 {"uid", TDF_UID},
135 {"stmtaddr", TDF_STMTADDR},
136 {"memsyms", TDF_MEMSYMS},
137 {"eh", TDF_EH},
138 {"alias", TDF_ALIAS},
139 {"nouid", TDF_NOUID},
140 {"enumerate_locals", TDF_ENUMERATE_LOCALS},
141 {"scev", TDF_SCEV},
142 {"gimple", TDF_GIMPLE},
143 {"folding", TDF_FOLDING},
144 {"optimized", MSG_OPTIMIZED_LOCATIONS},
145 {"missed", MSG_MISSED_OPTIMIZATION},
146 {"note", MSG_NOTE},
147 {"optall", MSG_ALL_KINDS},
148 {"all", dump_flags_t (TDF_ALL_VALUES
149 & ~(TDF_RAW | TDF_SLIM | TDF_LINENO | TDF_GRAPH
150 | TDF_STMTADDR | TDF_RHS_ONLY | TDF_NOUID
151 | TDF_ENUMERATE_LOCALS | TDF_SCEV | TDF_GIMPLE))},
152 {NULL, TDF_NONE}
153 };
154
155 /* A subset of the dump_options table which is used for -fopt-info
156 types. This must be consistent with the MSG_* flags in dumpfile.h.
157 */
158 static const kv_pair<dump_flags_t> optinfo_verbosity_options[] =
159 {
160 {"optimized", MSG_OPTIMIZED_LOCATIONS},
161 {"missed", MSG_MISSED_OPTIMIZATION},
162 {"note", MSG_NOTE},
163 {"all", MSG_ALL_KINDS},
164 {"internals", MSG_PRIORITY_INTERNALS},
165 {NULL, TDF_NONE}
166 };
167
168 /* Flags used for -fopt-info groups. */
169 const kv_pair<optgroup_flags_t> optgroup_options[] =
170 {
171 {"ipa", OPTGROUP_IPA},
172 {"loop", OPTGROUP_LOOP},
173 {"inline", OPTGROUP_INLINE},
174 {"omp", OPTGROUP_OMP},
175 {"vec", OPTGROUP_VEC},
176 {"optall", OPTGROUP_ALL},
177 {NULL, OPTGROUP_NONE}
178 };
179
180 gcc::dump_manager::dump_manager ():
181 m_next_dump (FIRST_AUTO_NUMBERED_DUMP),
182 m_extra_dump_files (NULL),
183 m_extra_dump_files_in_use (0),
184 m_extra_dump_files_alloced (0),
185 m_optgroup_flags (OPTGROUP_NONE),
186 m_optinfo_flags (TDF_NONE),
187 m_optinfo_filename (NULL)
188 {
189 }
190
191 gcc::dump_manager::~dump_manager ()
192 {
193 free (m_optinfo_filename);
194 for (size_t i = 0; i < m_extra_dump_files_in_use; i++)
195 {
196 dump_file_info *dfi = &m_extra_dump_files[i];
197 /* suffix, swtch, glob are statically allocated for the entries
198 in dump_files, and for statistics, but are dynamically allocated
199 for those for passes. */
200 if (dfi->owns_strings)
201 {
202 XDELETEVEC (const_cast <char *> (dfi->suffix));
203 XDELETEVEC (const_cast <char *> (dfi->swtch));
204 XDELETEVEC (const_cast <char *> (dfi->glob));
205 }
206 /* These, if non-NULL, are always dynamically allocated. */
207 XDELETEVEC (const_cast <char *> (dfi->pfilename));
208 XDELETEVEC (const_cast <char *> (dfi->alt_filename));
209 }
210 XDELETEVEC (m_extra_dump_files);
211 }
212
213 unsigned int
214 gcc::dump_manager::
215 dump_register (const char *suffix, const char *swtch, const char *glob,
216 dump_kind dkind, optgroup_flags_t optgroup_flags,
217 bool take_ownership)
218 {
219 int num = m_next_dump++;
220
221 size_t count = m_extra_dump_files_in_use++;
222
223 if (count >= m_extra_dump_files_alloced)
224 {
225 if (m_extra_dump_files_alloced == 0)
226 m_extra_dump_files_alloced = 512;
227 else
228 m_extra_dump_files_alloced *= 2;
229 m_extra_dump_files = XRESIZEVEC (struct dump_file_info,
230 m_extra_dump_files,
231 m_extra_dump_files_alloced);
232
233 /* Construct a new object in the space allocated above. */
234 new (m_extra_dump_files + count) dump_file_info ();
235 }
236 else
237 {
238 /* Zero out the already constructed object. */
239 m_extra_dump_files[count] = dump_file_info ();
240 }
241
242 m_extra_dump_files[count].suffix = suffix;
243 m_extra_dump_files[count].swtch = swtch;
244 m_extra_dump_files[count].glob = glob;
245 m_extra_dump_files[count].dkind = dkind;
246 m_extra_dump_files[count].optgroup_flags = optgroup_flags;
247 m_extra_dump_files[count].num = num;
248 m_extra_dump_files[count].owns_strings = take_ownership;
249
250 return count + TDI_end;
251 }
252
253
254 /* Allow languages and middle-end to register their dumps before the
255 optimization passes. */
256
257 void
258 gcc::dump_manager::
259 register_dumps ()
260 {
261 lang_hooks.register_dumps (this);
262 /* If this assert fails, some FE registered more than
263 FIRST_ME_AUTO_NUMBERED_DUMP - FIRST_AUTO_NUMBERED_DUMP
264 dump files. Bump FIRST_ME_AUTO_NUMBERED_DUMP accordingly. */
265 gcc_assert (m_next_dump <= FIRST_ME_AUTO_NUMBERED_DUMP);
266 m_next_dump = FIRST_ME_AUTO_NUMBERED_DUMP;
267 dump_files[TDI_original].num = m_next_dump++;
268 dump_files[TDI_gimple].num = m_next_dump++;
269 dump_files[TDI_nested].num = m_next_dump++;
270 }
271
272
273 /* Return the dump_file_info for the given phase. */
274
275 struct dump_file_info *
276 gcc::dump_manager::
277 get_dump_file_info (int phase) const
278 {
279 if (phase < TDI_end)
280 return &dump_files[phase];
281 else if ((size_t) (phase - TDI_end) >= m_extra_dump_files_in_use)
282 return NULL;
283 else
284 return m_extra_dump_files + (phase - TDI_end);
285 }
286
287 /* Locate the dump_file_info with swtch equal to SWTCH,
288 or return NULL if no such dump_file_info exists. */
289
290 struct dump_file_info *
291 gcc::dump_manager::
292 get_dump_file_info_by_switch (const char *swtch) const
293 {
294 for (unsigned i = 0; i < m_extra_dump_files_in_use; i++)
295 if (strcmp (m_extra_dump_files[i].swtch, swtch) == 0)
296 return &m_extra_dump_files[i];
297
298 /* Not found. */
299 return NULL;
300 }
301
302
303 /* Return the name of the dump file for the given phase.
304 The caller is responsible for calling free on the returned
305 buffer.
306 If the dump is not enabled, returns NULL. */
307
308 char *
309 gcc::dump_manager::
310 get_dump_file_name (int phase, int part) const
311 {
312 struct dump_file_info *dfi;
313
314 if (phase == TDI_none)
315 return NULL;
316
317 dfi = get_dump_file_info (phase);
318
319 return get_dump_file_name (dfi, part);
320 }
321
322 /* Return the name of the dump file for the given dump_file_info.
323 The caller is responsible for calling free on the returned
324 buffer.
325 If the dump is not enabled, returns NULL. */
326
327 char *
328 gcc::dump_manager::
329 get_dump_file_name (struct dump_file_info *dfi, int part) const
330 {
331 char dump_id[10];
332
333 gcc_assert (dfi);
334
335 if (dfi->pstate == 0)
336 return NULL;
337
338 /* If available, use the command line dump filename. */
339 if (dfi->pfilename)
340 return xstrdup (dfi->pfilename);
341
342 if (dfi->num < 0)
343 dump_id[0] = '\0';
344 else
345 {
346 /* (null), LANG, TREE, RTL, IPA. */
347 char suffix = " ltri"[dfi->dkind];
348
349 if (snprintf (dump_id, sizeof (dump_id), ".%03d%c", dfi->num, suffix) < 0)
350 dump_id[0] = '\0';
351 }
352
353 if (part != -1)
354 {
355 char part_id[8];
356 snprintf (part_id, sizeof (part_id), ".%i", part);
357 return concat (dump_base_name, dump_id, part_id, dfi->suffix, NULL);
358 }
359 else
360 return concat (dump_base_name, dump_id, dfi->suffix, NULL);
361 }
362
363 /* Open a dump file called FILENAME. Some filenames are special and
364 refer to the standard streams. TRUNC indicates whether this is the
365 first open (so the file should be truncated, rather than appended).
366 An error message is emitted in the event of failure. */
367
368 static FILE *
369 dump_open (const char *filename, bool trunc)
370 {
371 if (strcmp ("stderr", filename) == 0)
372 return stderr;
373
374 if (strcmp ("stdout", filename) == 0
375 || strcmp ("-", filename) == 0)
376 return stdout;
377
378 FILE *stream = fopen (filename, trunc ? "w" : "a");
379
380 if (!stream)
381 error ("could not open dump file %qs: %m", filename);
382 return stream;
383 }
384
385 /* For a given DFI, open an alternate dump filename (which could also
386 be a standard stream such as stdout/stderr). If the alternate dump
387 file cannot be opened, return NULL. */
388
389 static FILE *
390 dump_open_alternate_stream (struct dump_file_info *dfi)
391 {
392 if (!dfi->alt_filename)
393 return NULL;
394
395 if (dfi->alt_stream)
396 return dfi->alt_stream;
397
398 FILE *stream = dump_open (dfi->alt_filename, dfi->alt_state < 0);
399
400 if (stream)
401 dfi->alt_state = 1;
402
403 return stream;
404 }
405
406 /* Construct a dump_user_location_t from STMT (using its location and
407 hotness). */
408
409 dump_user_location_t::dump_user_location_t (const gimple *stmt)
410 : m_count (), m_loc (UNKNOWN_LOCATION)
411 {
412 if (stmt)
413 {
414 if (stmt->bb)
415 m_count = stmt->bb->count;
416 m_loc = gimple_location (stmt);
417 }
418 }
419
420 /* Construct a dump_user_location_t from an RTL instruction (using its
421 location and hotness). */
422
423 dump_user_location_t::dump_user_location_t (const rtx_insn *insn)
424 : m_count (), m_loc (UNKNOWN_LOCATION)
425 {
426 if (insn)
427 {
428 basic_block bb = BLOCK_FOR_INSN (insn);
429 if (bb)
430 m_count = bb->count;
431 m_loc = INSN_LOCATION (insn);
432 }
433 }
434
435 /* Construct from a function declaration. This one requires spelling out
436 to avoid accidentally constructing from other kinds of tree. */
437
438 dump_user_location_t
439 dump_user_location_t::from_function_decl (tree fndecl)
440 {
441 gcc_assert (fndecl);
442
443 // FIXME: profile count for function?
444 return dump_user_location_t (profile_count (),
445 DECL_SOURCE_LOCATION (fndecl));
446 }
447
448 /* Extract the MSG_* component from DUMP_KIND and return a string for use
449 as a prefix to dump messages.
450 These match the strings in optinfo_verbosity_options and thus the
451 "OPTIONS" within "-fopt-info-OPTIONS". */
452
453 static const char *
454 kind_as_string (dump_flags_t dump_kind)
455 {
456 switch (dump_kind & MSG_ALL_KINDS)
457 {
458 default:
459 gcc_unreachable ();
460 case MSG_OPTIMIZED_LOCATIONS:
461 return "optimized";
462 case MSG_MISSED_OPTIMIZATION:
463 return "missed";
464 case MSG_NOTE:
465 return "note";
466 }
467 }
468
469 /* Print source location on DFILE if enabled. */
470
471 static void
472 dump_loc (dump_flags_t dump_kind, FILE *dfile, location_t loc)
473 {
474 if (dump_kind)
475 {
476 if (LOCATION_LOCUS (loc) > BUILTINS_LOCATION)
477 fprintf (dfile, "%s:%d:%d: ", LOCATION_FILE (loc),
478 LOCATION_LINE (loc), LOCATION_COLUMN (loc));
479 else if (current_function_decl)
480 fprintf (dfile, "%s:%d:%d: ",
481 DECL_SOURCE_FILE (current_function_decl),
482 DECL_SOURCE_LINE (current_function_decl),
483 DECL_SOURCE_COLUMN (current_function_decl));
484 fprintf (dfile, "%s: ", kind_as_string (dump_kind));
485 /* Indentation based on scope depth. */
486 fprintf (dfile, "%*s", get_dump_scope_depth (), "");
487 }
488 }
489
490 /* Print source location to PP if enabled. */
491
492 static void
493 dump_loc (dump_flags_t dump_kind, pretty_printer *pp, location_t loc)
494 {
495 if (dump_kind)
496 {
497 if (LOCATION_LOCUS (loc) > BUILTINS_LOCATION)
498 pp_printf (pp, "%s:%d:%d: ", LOCATION_FILE (loc),
499 LOCATION_LINE (loc), LOCATION_COLUMN (loc));
500 else if (current_function_decl)
501 pp_printf (pp, "%s:%d:%d: ",
502 DECL_SOURCE_FILE (current_function_decl),
503 DECL_SOURCE_LINE (current_function_decl),
504 DECL_SOURCE_COLUMN (current_function_decl));
505 pp_printf (pp, "%s: ", kind_as_string (dump_kind));
506 /* Indentation based on scope depth. */
507 for (unsigned i = 0; i < get_dump_scope_depth (); i++)
508 pp_character (pp, ' ');
509 }
510 }
511
512 /* Implementation of dump_context member functions. */
513
514 /* dump_context's dtor. */
515
516 dump_context::~dump_context ()
517 {
518 delete m_pending;
519 }
520
521 void
522 dump_context::set_json_writer (optrecord_json_writer *writer)
523 {
524 delete m_json_writer;
525 m_json_writer = writer;
526 }
527
528 /* Perform cleanup activity for -fsave-optimization-record.
529 Currently, the file is written out here in one go, before cleaning
530 up. */
531
532 void
533 dump_context::finish_any_json_writer ()
534 {
535 if (!m_json_writer)
536 return;
537
538 m_json_writer->write ();
539 delete m_json_writer;
540 m_json_writer = NULL;
541 }
542
543 /* Update the "dumps_are_enabled" global; to be called whenever dump_file
544 or alt_dump_file change, or when changing dump_context in selftests. */
545
546 void
547 dump_context::refresh_dumps_are_enabled ()
548 {
549 dumps_are_enabled = (dump_file || alt_dump_file || optinfo_enabled_p ()
550 || m_test_pp);
551 }
552
553 /* Determine if a message of kind DUMP_KIND and at the current scope depth
554 should be printed.
555
556 Only show messages that match FILTER both on their kind *and*
557 their priority. */
558
559 bool
560 dump_context::apply_dump_filter_p (dump_flags_t dump_kind,
561 dump_flags_t filter) const
562 {
563 /* Few messages, if any, have an explicit MSG_PRIORITY.
564 If DUMP_KIND does, we'll use it.
565 Otherwise, generate an implicit priority value for the message based
566 on the current scope depth.
567 Messages at the top-level scope are MSG_PRIORITY_USER_FACING,
568 whereas those in nested scopes are MSG_PRIORITY_INTERNALS. */
569 if (!(dump_kind & MSG_ALL_PRIORITIES))
570 {
571 dump_flags_t implicit_priority
572 = (m_scope_depth > 0
573 ? MSG_PRIORITY_INTERNALS
574 : MSG_PRIORITY_USER_FACING);
575 dump_kind |= implicit_priority;
576 }
577
578 return (dump_kind & (filter & MSG_ALL_KINDS)
579 && dump_kind & (filter & MSG_ALL_PRIORITIES));
580 }
581
582 /* Print LOC to the appropriate dump destinations, given DUMP_KIND.
583 If optinfos are enabled, begin a new optinfo. */
584
585 void
586 dump_context::dump_loc (const dump_metadata_t &metadata,
587 const dump_user_location_t &loc)
588 {
589 end_any_optinfo ();
590
591 dump_loc_immediate (metadata.get_dump_flags (), loc);
592
593 if (optinfo_enabled_p ())
594 begin_next_optinfo (metadata, loc);
595 }
596
597 /* As dump_loc above, but without starting a new optinfo. */
598
599 void
600 dump_context::dump_loc_immediate (dump_flags_t dump_kind,
601 const dump_user_location_t &loc)
602 {
603 location_t srcloc = loc.get_location_t ();
604
605 if (dump_file && apply_dump_filter_p (dump_kind, pflags))
606 ::dump_loc (dump_kind, dump_file, srcloc);
607
608 if (alt_dump_file && apply_dump_filter_p (dump_kind, alt_flags))
609 ::dump_loc (dump_kind, alt_dump_file, srcloc);
610
611 /* Support for temp_dump_context in selftests. */
612 if (m_test_pp && apply_dump_filter_p (dump_kind, m_test_pp_flags))
613 ::dump_loc (dump_kind, m_test_pp, srcloc);
614 }
615
616 /* Make an item for the given dump call, equivalent to print_gimple_stmt. */
617
618 static optinfo_item *
619 make_item_for_dump_gimple_stmt (gimple *stmt, int spc, dump_flags_t dump_flags)
620 {
621 pretty_printer pp;
622 pp_needs_newline (&pp) = true;
623 pp_gimple_stmt_1 (&pp, stmt, spc, dump_flags);
624 pp_newline (&pp);
625
626 optinfo_item *item
627 = new optinfo_item (OPTINFO_ITEM_KIND_GIMPLE, gimple_location (stmt),
628 xstrdup (pp_formatted_text (&pp)));
629 return item;
630 }
631
632 /* Dump gimple statement GS with SPC indentation spaces and
633 EXTRA_DUMP_FLAGS on the dump streams if DUMP_KIND is enabled. */
634
635 void
636 dump_context::dump_gimple_stmt (const dump_metadata_t &metadata,
637 dump_flags_t extra_dump_flags,
638 gimple *gs, int spc)
639 {
640 optinfo_item *item
641 = make_item_for_dump_gimple_stmt (gs, spc, dump_flags | extra_dump_flags);
642 emit_item (item, metadata.get_dump_flags ());
643
644 if (optinfo_enabled_p ())
645 {
646 optinfo &info = ensure_pending_optinfo (metadata);
647 info.add_item (item);
648 }
649 else
650 delete item;
651 }
652
653 /* Similar to dump_gimple_stmt, except additionally print source location. */
654
655 void
656 dump_context::dump_gimple_stmt_loc (const dump_metadata_t &metadata,
657 const dump_user_location_t &loc,
658 dump_flags_t extra_dump_flags,
659 gimple *gs, int spc)
660 {
661 dump_loc (metadata, loc);
662 dump_gimple_stmt (metadata, extra_dump_flags, gs, spc);
663 }
664
665 /* Make an item for the given dump call, equivalent to print_gimple_expr. */
666
667 static optinfo_item *
668 make_item_for_dump_gimple_expr (gimple *stmt, int spc, dump_flags_t dump_flags)
669 {
670 dump_flags |= TDF_RHS_ONLY;
671 pretty_printer pp;
672 pp_needs_newline (&pp) = true;
673 pp_gimple_stmt_1 (&pp, stmt, spc, dump_flags);
674
675 optinfo_item *item
676 = new optinfo_item (OPTINFO_ITEM_KIND_GIMPLE, gimple_location (stmt),
677 xstrdup (pp_formatted_text (&pp)));
678 return item;
679 }
680
681 /* Dump gimple statement GS with SPC indentation spaces and
682 EXTRA_DUMP_FLAGS on the dump streams if DUMP_KIND is enabled.
683 Do not terminate with a newline or semicolon. */
684
685 void
686 dump_context::dump_gimple_expr (const dump_metadata_t &metadata,
687 dump_flags_t extra_dump_flags,
688 gimple *gs, int spc)
689 {
690 optinfo_item *item
691 = make_item_for_dump_gimple_expr (gs, spc, dump_flags | extra_dump_flags);
692 emit_item (item, metadata.get_dump_flags ());
693
694 if (optinfo_enabled_p ())
695 {
696 optinfo &info = ensure_pending_optinfo (metadata);
697 info.add_item (item);
698 }
699 else
700 delete item;
701 }
702
703 /* Similar to dump_gimple_expr, except additionally print source location. */
704
705 void
706 dump_context::dump_gimple_expr_loc (const dump_metadata_t &metadata,
707 const dump_user_location_t &loc,
708 dump_flags_t extra_dump_flags,
709 gimple *gs,
710 int spc)
711 {
712 dump_loc (metadata, loc);
713 dump_gimple_expr (metadata, extra_dump_flags, gs, spc);
714 }
715
716 /* Make an item for the given dump call, equivalent to print_generic_expr. */
717
718 static optinfo_item *
719 make_item_for_dump_generic_expr (tree node, dump_flags_t dump_flags)
720 {
721 pretty_printer pp;
722 pp_needs_newline (&pp) = true;
723 pp_translate_identifiers (&pp) = false;
724 dump_generic_node (&pp, node, 0, dump_flags, false);
725
726 location_t loc = UNKNOWN_LOCATION;
727 if (EXPR_HAS_LOCATION (node))
728 loc = EXPR_LOCATION (node);
729
730 optinfo_item *item
731 = new optinfo_item (OPTINFO_ITEM_KIND_TREE, loc,
732 xstrdup (pp_formatted_text (&pp)));
733 return item;
734 }
735
736 /* Dump expression tree T using EXTRA_DUMP_FLAGS on dump streams if
737 DUMP_KIND is enabled. */
738
739 void
740 dump_context::dump_generic_expr (const dump_metadata_t &metadata,
741 dump_flags_t extra_dump_flags,
742 tree t)
743 {
744 optinfo_item *item
745 = make_item_for_dump_generic_expr (t, dump_flags | extra_dump_flags);
746 emit_item (item, metadata.get_dump_flags ());
747
748 if (optinfo_enabled_p ())
749 {
750 optinfo &info = ensure_pending_optinfo (metadata);
751 info.add_item (item);
752 }
753 else
754 delete item;
755 }
756
757
758 /* Similar to dump_generic_expr, except additionally print the source
759 location. */
760
761 void
762 dump_context::dump_generic_expr_loc (const dump_metadata_t &metadata,
763 const dump_user_location_t &loc,
764 dump_flags_t extra_dump_flags,
765 tree t)
766 {
767 dump_loc (metadata, loc);
768 dump_generic_expr (metadata, extra_dump_flags, t);
769 }
770
771 /* Make an item for the given dump call. */
772
773 static optinfo_item *
774 make_item_for_dump_symtab_node (symtab_node *node)
775 {
776 location_t loc = DECL_SOURCE_LOCATION (node->decl);
777 optinfo_item *item
778 = new optinfo_item (OPTINFO_ITEM_KIND_SYMTAB_NODE, loc,
779 xstrdup (node->dump_name ()));
780 return item;
781 }
782
783 /* dump_pretty_printer's ctor. */
784
785 dump_pretty_printer::dump_pretty_printer (dump_context *context,
786 dump_flags_t dump_kind)
787 : pretty_printer (), m_context (context), m_dump_kind (dump_kind),
788 m_stashed_items ()
789 {
790 pp_format_decoder (this) = format_decoder_cb;
791 }
792
793 /* Phase 3 of formatting; compare with pp_output_formatted_text.
794
795 Emit optinfo_item instances for the various formatted chunks from phases
796 1 and 2 (i.e. pp_format).
797
798 Some chunks may already have had their items built (during decode_format).
799 These chunks have been stashed into m_stashed_items; we emit them here.
800
801 For all other purely textual chunks, they are printed into
802 buffer->formatted_obstack, and then emitted as a textual optinfo_item.
803 This consolidates multiple adjacent text chunks into a single text
804 optinfo_item. */
805
806 void
807 dump_pretty_printer::emit_items (optinfo *dest)
808 {
809 output_buffer *buffer = pp_buffer (this);
810 struct chunk_info *chunk_array = buffer->cur_chunk_array;
811 const char **args = chunk_array->args;
812
813 gcc_assert (buffer->obstack == &buffer->formatted_obstack);
814 gcc_assert (buffer->line_length == 0);
815
816 unsigned stashed_item_idx = 0;
817 for (unsigned chunk = 0; args[chunk]; chunk++)
818 {
819 if (stashed_item_idx < m_stashed_items.length ()
820 && args[chunk] == *m_stashed_items[stashed_item_idx].buffer_ptr)
821 {
822 emit_any_pending_textual_chunks (dest);
823 /* This chunk has a stashed item: use it. */
824 emit_item (m_stashed_items[stashed_item_idx++].item, dest);
825 }
826 else
827 /* This chunk is purely textual. Print it (to
828 buffer->formatted_obstack), so that we can consolidate adjacent
829 chunks into one textual optinfo_item. */
830 pp_string (this, args[chunk]);
831 }
832
833 emit_any_pending_textual_chunks (dest);
834
835 /* Ensure that we consumed all of stashed_items. */
836 gcc_assert (stashed_item_idx == m_stashed_items.length ());
837
838 /* Deallocate the chunk structure and everything after it (i.e. the
839 associated series of formatted strings). */
840 buffer->cur_chunk_array = chunk_array->prev;
841 obstack_free (&buffer->chunk_obstack, chunk_array);
842 }
843
844 /* Subroutine of dump_pretty_printer::emit_items
845 for consolidating multiple adjacent pure-text chunks into single
846 optinfo_items (in phase 3). */
847
848 void
849 dump_pretty_printer::emit_any_pending_textual_chunks (optinfo *dest)
850 {
851 gcc_assert (buffer->obstack == &buffer->formatted_obstack);
852
853 /* Don't emit an item if the pending text is empty. */
854 if (output_buffer_last_position_in_text (buffer) == NULL)
855 return;
856
857 char *formatted_text = xstrdup (pp_formatted_text (this));
858 optinfo_item *item
859 = new optinfo_item (OPTINFO_ITEM_KIND_TEXT, UNKNOWN_LOCATION,
860 formatted_text);
861 emit_item (item, dest);
862
863 /* Clear the pending text by unwinding formatted_text back to the start
864 of the buffer (without deallocating). */
865 obstack_free (&buffer->formatted_obstack,
866 buffer->formatted_obstack.object_base);
867 }
868
869 /* Emit ITEM and take ownership of it. If DEST is non-NULL, add ITEM
870 to DEST; otherwise delete ITEM. */
871
872 void
873 dump_pretty_printer::emit_item (optinfo_item *item, optinfo *dest)
874 {
875 m_context->emit_item (item, m_dump_kind);
876 if (dest)
877 dest->add_item (item);
878 else
879 delete item;
880 }
881
882 /* Record that ITEM (generated in phase 2 of formatting) is to be used for
883 the chunk at BUFFER_PTR in phase 3 (by emit_items). */
884
885 void
886 dump_pretty_printer::stash_item (const char **buffer_ptr, optinfo_item *item)
887 {
888 gcc_assert (buffer_ptr);
889 gcc_assert (item);
890
891 m_stashed_items.safe_push (stashed_item (buffer_ptr, item));
892 }
893
894 /* pp_format_decoder callback for dump_pretty_printer, and thus for
895 dump_printf and dump_printf_loc.
896
897 A wrapper around decode_format, for type-safety. */
898
899 bool
900 dump_pretty_printer::format_decoder_cb (pretty_printer *pp, text_info *text,
901 const char *spec, int /*precision*/,
902 bool /*wide*/, bool /*set_locus*/,
903 bool /*verbose*/, bool */*quoted*/,
904 const char **buffer_ptr)
905 {
906 dump_pretty_printer *opp = static_cast <dump_pretty_printer *> (pp);
907 return opp->decode_format (text, spec, buffer_ptr);
908 }
909
910 /* Format decoder for dump_pretty_printer, and thus for dump_printf and
911 dump_printf_loc.
912
913 Supported format codes (in addition to the standard pretty_printer ones)
914 are:
915
916 %C: cgraph_node *:
917 Equivalent to: dump_symtab_node (MSG_*, node)
918 %E: gimple *:
919 Equivalent to: dump_gimple_expr (MSG_*, TDF_SLIM, stmt, 0)
920 %G: gimple *:
921 Equivalent to: dump_gimple_stmt (MSG_*, TDF_SLIM, stmt, 0)
922 %T: tree:
923 Equivalent to: dump_generic_expr (MSG_*, arg, TDF_SLIM).
924
925 TODO: add a format code that can handle (symtab_node*) *and* both
926 subclasses (presumably means teaching -Wformat about non-virtual
927 subclasses).
928
929 These format codes build optinfo_item instances, thus capturing metadata
930 about the arguments being dumped, as well as the textual output. */
931
932 bool
933 dump_pretty_printer::decode_format (text_info *text, const char *spec,
934 const char **buffer_ptr)
935 {
936 /* Various format codes that imply making an optinfo_item and stashed it
937 for later use (to capture metadata, rather than plain text). */
938 switch (*spec)
939 {
940 case 'C':
941 {
942 cgraph_node *node = va_arg (*text->args_ptr, cgraph_node *);
943
944 /* Make an item for the node, and stash it. */
945 optinfo_item *item = make_item_for_dump_symtab_node (node);
946 stash_item (buffer_ptr, item);
947 return true;
948 }
949
950 case 'E':
951 {
952 gimple *stmt = va_arg (*text->args_ptr, gimple *);
953
954 /* Make an item for the stmt, and stash it. */
955 optinfo_item *item = make_item_for_dump_gimple_expr (stmt, 0, TDF_SLIM);
956 stash_item (buffer_ptr, item);
957 return true;
958 }
959
960 case 'G':
961 {
962 gimple *stmt = va_arg (*text->args_ptr, gimple *);
963
964 /* Make an item for the stmt, and stash it. */
965 optinfo_item *item = make_item_for_dump_gimple_stmt (stmt, 0, TDF_SLIM);
966 stash_item (buffer_ptr, item);
967 return true;
968 }
969
970 case 'T':
971 {
972 tree t = va_arg (*text->args_ptr, tree);
973
974 /* Make an item for the tree, and stash it. */
975 optinfo_item *item = make_item_for_dump_generic_expr (t, TDF_SLIM);
976 stash_item (buffer_ptr, item);
977 return true;
978 }
979
980 default:
981 return false;
982 }
983 }
984
985 /* Output a formatted message using FORMAT on appropriate dump streams. */
986
987 void
988 dump_context::dump_printf_va (const dump_metadata_t &metadata, const char *format,
989 va_list *ap)
990 {
991 dump_pretty_printer pp (this, metadata.get_dump_flags ());
992
993 text_info text;
994 text.err_no = errno;
995 text.args_ptr = ap;
996 text.format_spec = format;
997
998 /* Phases 1 and 2, using pp_format. */
999 pp_format (&pp, &text);
1000
1001 /* Phase 3. */
1002 if (optinfo_enabled_p ())
1003 {
1004 optinfo &info = ensure_pending_optinfo (metadata);
1005 pp.emit_items (&info);
1006 }
1007 else
1008 pp.emit_items (NULL);
1009 }
1010
1011 /* Similar to dump_printf, except source location is also printed, and
1012 dump location captured. */
1013
1014 void
1015 dump_context::dump_printf_loc_va (const dump_metadata_t &metadata,
1016 const dump_user_location_t &loc,
1017 const char *format, va_list *ap)
1018 {
1019 dump_loc (metadata, loc);
1020 dump_printf_va (metadata, format, ap);
1021 }
1022
1023 /* Make an item for the given dump call, equivalent to print_dec. */
1024
1025 template<unsigned int N, typename C>
1026 static optinfo_item *
1027 make_item_for_dump_dec (const poly_int<N, C> &value)
1028 {
1029 STATIC_ASSERT (poly_coeff_traits<C>::signedness >= 0);
1030 signop sgn = poly_coeff_traits<C>::signedness ? SIGNED : UNSIGNED;
1031
1032 pretty_printer pp;
1033
1034 if (value.is_constant ())
1035 pp_wide_int (&pp, value.coeffs[0], sgn);
1036 else
1037 {
1038 pp_character (&pp, '[');
1039 for (unsigned int i = 0; i < N; ++i)
1040 {
1041 pp_wide_int (&pp, value.coeffs[i], sgn);
1042 pp_character (&pp, i == N - 1 ? ']' : ',');
1043 }
1044 }
1045
1046 optinfo_item *item
1047 = new optinfo_item (OPTINFO_ITEM_KIND_TEXT, UNKNOWN_LOCATION,
1048 xstrdup (pp_formatted_text (&pp)));
1049 return item;
1050 }
1051
1052 /* Output VALUE in decimal to appropriate dump streams. */
1053
1054 template<unsigned int N, typename C>
1055 void
1056 dump_context::dump_dec (const dump_metadata_t &metadata, const poly_int<N, C> &value)
1057 {
1058 optinfo_item *item = make_item_for_dump_dec (value);
1059 emit_item (item, metadata.get_dump_flags ());
1060
1061 if (optinfo_enabled_p ())
1062 {
1063 optinfo &info = ensure_pending_optinfo (metadata);
1064 info.add_item (item);
1065 }
1066 else
1067 delete item;
1068 }
1069
1070 /* Output the name of NODE on appropriate dump streams. */
1071
1072 void
1073 dump_context::dump_symtab_node (const dump_metadata_t &metadata, symtab_node *node)
1074 {
1075 optinfo_item *item = make_item_for_dump_symtab_node (node);
1076 emit_item (item, metadata.get_dump_flags ());
1077
1078 if (optinfo_enabled_p ())
1079 {
1080 optinfo &info = ensure_pending_optinfo (metadata);
1081 info.add_item (item);
1082 }
1083 else
1084 delete item;
1085 }
1086
1087 /* Get the current dump scope-nesting depth.
1088 For use by -fopt-info (for showing nesting via indentation). */
1089
1090 unsigned int
1091 dump_context::get_scope_depth () const
1092 {
1093 return m_scope_depth;
1094 }
1095
1096 /* Push a nested dump scope.
1097 Increment the scope depth.
1098 Print "=== NAME ===\n" to the dumpfile, if any, and to the -fopt-info
1099 destination, if any.
1100 Emit a "scope" optinfo if optinfos are enabled. */
1101
1102 void
1103 dump_context::begin_scope (const char *name,
1104 const dump_user_location_t &user_location,
1105 const dump_impl_location_t &impl_location)
1106 {
1107 m_scope_depth++;
1108
1109 location_t src_loc = user_location.get_location_t ();
1110
1111 if (dump_file && apply_dump_filter_p (MSG_NOTE, pflags))
1112 ::dump_loc (MSG_NOTE, dump_file, src_loc);
1113
1114 if (alt_dump_file && apply_dump_filter_p (MSG_NOTE, alt_flags))
1115 ::dump_loc (MSG_NOTE, alt_dump_file, src_loc);
1116
1117 /* Support for temp_dump_context in selftests. */
1118 if (m_test_pp && apply_dump_filter_p (MSG_NOTE, m_test_pp_flags))
1119 ::dump_loc (MSG_NOTE, m_test_pp, src_loc);
1120
1121 pretty_printer pp;
1122 pp_printf (&pp, "=== %s ===\n", name);
1123 optinfo_item *item
1124 = new optinfo_item (OPTINFO_ITEM_KIND_TEXT, UNKNOWN_LOCATION,
1125 xstrdup (pp_formatted_text (&pp)));
1126 emit_item (item, MSG_NOTE);
1127
1128 if (optinfo_enabled_p ())
1129 {
1130 optinfo &info
1131 = begin_next_optinfo (dump_metadata_t (MSG_NOTE, impl_location),
1132 user_location);
1133 info.m_kind = OPTINFO_KIND_SCOPE;
1134 info.add_item (item);
1135 end_any_optinfo ();
1136 }
1137 else
1138 delete item;
1139 }
1140
1141 /* Pop a nested dump scope. */
1142
1143 void
1144 dump_context::end_scope ()
1145 {
1146 end_any_optinfo ();
1147 m_scope_depth--;
1148
1149 if (m_json_writer)
1150 m_json_writer->pop_scope ();
1151 }
1152
1153 /* Should optinfo instances be created?
1154 All creation of optinfos should be guarded by this predicate.
1155 Return true if any optinfo destinations are active. */
1156
1157 bool
1158 dump_context::optinfo_enabled_p () const
1159 {
1160 return (optimization_records_enabled_p ());
1161 }
1162
1163 /* Return the optinfo currently being accumulated, creating one if
1164 necessary. */
1165
1166 optinfo &
1167 dump_context::ensure_pending_optinfo (const dump_metadata_t &metadata)
1168 {
1169 if (!m_pending)
1170 return begin_next_optinfo (metadata, dump_user_location_t ());
1171 return *m_pending;
1172 }
1173
1174 /* Start a new optinfo and return it, ending any optinfo that was already
1175 accumulated. */
1176
1177 optinfo &
1178 dump_context::begin_next_optinfo (const dump_metadata_t &metadata,
1179 const dump_user_location_t &user_loc)
1180 {
1181 end_any_optinfo ();
1182 gcc_assert (m_pending == NULL);
1183 dump_location_t loc (user_loc, metadata.get_impl_location ());
1184 m_pending = new optinfo (loc, OPTINFO_KIND_NOTE, current_pass);
1185 m_pending->handle_dump_file_kind (metadata.get_dump_flags ());
1186 return *m_pending;
1187 }
1188
1189 /* End any optinfo that has been accumulated within this context; emitting
1190 it to any destinations as appropriate, such as optimization records. */
1191
1192 void
1193 dump_context::end_any_optinfo ()
1194 {
1195 if (m_pending)
1196 emit_optinfo (m_pending);
1197 delete m_pending;
1198 m_pending = NULL;
1199 }
1200
1201 /* Emit the optinfo to all of the "non-immediate" destinations
1202 (emission to "immediate" destinations is done by
1203 dump_context::emit_item). */
1204
1205 void
1206 dump_context::emit_optinfo (const optinfo *info)
1207 {
1208 /* -fsave-optimization-record. */
1209 if (m_json_writer)
1210 m_json_writer->add_record (info);
1211 }
1212
1213 /* Emit ITEM to all item destinations (those that don't require
1214 consolidation into optinfo instances). */
1215
1216 void
1217 dump_context::emit_item (optinfo_item *item, dump_flags_t dump_kind)
1218 {
1219 if (dump_file && apply_dump_filter_p (dump_kind, pflags))
1220 fprintf (dump_file, "%s", item->get_text ());
1221
1222 if (alt_dump_file && apply_dump_filter_p (dump_kind, alt_flags))
1223 fprintf (alt_dump_file, "%s", item->get_text ());
1224
1225 /* Support for temp_dump_context in selftests. */
1226 if (m_test_pp && apply_dump_filter_p (dump_kind, m_test_pp_flags))
1227 pp_string (m_test_pp, item->get_text ());
1228 }
1229
1230 /* The current singleton dump_context, and its default. */
1231
1232 dump_context *dump_context::s_current = &dump_context::s_default;
1233 dump_context dump_context::s_default;
1234
1235 /* Implementation of dump_* API calls, calling into dump_context
1236 member functions. */
1237
1238 /* Calls to the dump_* functions do non-trivial work, so they ought
1239 to be guarded by:
1240 if (dump_enabled_p ())
1241 Assert that they are guarded, and, if assertions are disabled,
1242 bail out if the calls weren't properly guarded. */
1243
1244 #define VERIFY_DUMP_ENABLED_P \
1245 do { \
1246 gcc_assert (dump_enabled_p ()); \
1247 if (!dump_enabled_p ()) \
1248 return; \
1249 } while (0)
1250
1251 /* Dump gimple statement GS with SPC indentation spaces and
1252 EXTRA_DUMP_FLAGS on the dump streams if DUMP_KIND is enabled. */
1253
1254 void
1255 dump_gimple_stmt (const dump_metadata_t &metadata, dump_flags_t extra_dump_flags,
1256 gimple *gs, int spc)
1257 {
1258 VERIFY_DUMP_ENABLED_P;
1259 dump_context::get ().dump_gimple_stmt (metadata, extra_dump_flags, gs, spc);
1260 }
1261
1262 /* Similar to dump_gimple_stmt, except additionally print source location. */
1263
1264 void
1265 dump_gimple_stmt_loc (const dump_metadata_t &metadata,
1266 const dump_user_location_t &loc,
1267 dump_flags_t extra_dump_flags, gimple *gs, int spc)
1268 {
1269 VERIFY_DUMP_ENABLED_P;
1270 dump_context::get ().dump_gimple_stmt_loc (metadata, loc, extra_dump_flags,
1271 gs, spc);
1272 }
1273
1274 /* Dump gimple statement GS with SPC indentation spaces and
1275 EXTRA_DUMP_FLAGS on the dump streams if DUMP_KIND is enabled.
1276 Do not terminate with a newline or semicolon. */
1277
1278 void
1279 dump_gimple_expr (const dump_metadata_t &metadata,
1280 dump_flags_t extra_dump_flags,
1281 gimple *gs, int spc)
1282 {
1283 VERIFY_DUMP_ENABLED_P;
1284 dump_context::get ().dump_gimple_expr (metadata, extra_dump_flags, gs, spc);
1285 }
1286
1287 /* Similar to dump_gimple_expr, except additionally print source location. */
1288
1289 void
1290 dump_gimple_expr_loc (const dump_metadata_t &metadata,
1291 const dump_user_location_t &loc,
1292 dump_flags_t extra_dump_flags, gimple *gs, int spc)
1293 {
1294 VERIFY_DUMP_ENABLED_P;
1295 dump_context::get ().dump_gimple_expr_loc (metadata, loc, extra_dump_flags,
1296 gs, spc);
1297 }
1298
1299 /* Dump expression tree T using EXTRA_DUMP_FLAGS on dump streams if
1300 DUMP_KIND is enabled. */
1301
1302 void
1303 dump_generic_expr (const dump_metadata_t &metadata, dump_flags_t extra_dump_flags,
1304 tree t)
1305 {
1306 VERIFY_DUMP_ENABLED_P;
1307 dump_context::get ().dump_generic_expr (metadata, extra_dump_flags, t);
1308 }
1309
1310 /* Similar to dump_generic_expr, except additionally print the source
1311 location. */
1312
1313 void
1314 dump_generic_expr_loc (const dump_metadata_t &metadata,
1315 const dump_user_location_t &loc,
1316 dump_flags_t extra_dump_flags, tree t)
1317 {
1318 VERIFY_DUMP_ENABLED_P;
1319 dump_context::get ().dump_generic_expr_loc (metadata, loc, extra_dump_flags,
1320 t);
1321 }
1322
1323 /* Output a formatted message using FORMAT on appropriate dump streams. */
1324
1325 void
1326 dump_printf (const dump_metadata_t &metadata, const char *format, ...)
1327 {
1328 VERIFY_DUMP_ENABLED_P;
1329 va_list ap;
1330 va_start (ap, format);
1331 dump_context::get ().dump_printf_va (metadata, format, &ap);
1332 va_end (ap);
1333 }
1334
1335 /* Similar to dump_printf, except source location is also printed, and
1336 dump location captured. */
1337
1338 void
1339 dump_printf_loc (const dump_metadata_t &metadata,
1340 const dump_user_location_t &loc,
1341 const char *format, ...)
1342 {
1343 VERIFY_DUMP_ENABLED_P;
1344 va_list ap;
1345 va_start (ap, format);
1346 dump_context::get ().dump_printf_loc_va (metadata, loc, format, &ap);
1347 va_end (ap);
1348 }
1349
1350 /* Output VALUE in decimal to appropriate dump streams. */
1351
1352 template<unsigned int N, typename C>
1353 void
1354 dump_dec (const dump_metadata_t &metadata, const poly_int<N, C> &value)
1355 {
1356 VERIFY_DUMP_ENABLED_P;
1357 dump_context::get ().dump_dec (metadata, value);
1358 }
1359
1360 template void dump_dec (const dump_metadata_t &metadata, const poly_uint16 &);
1361 template void dump_dec (const dump_metadata_t &metadata, const poly_int64 &);
1362 template void dump_dec (const dump_metadata_t &metadata, const poly_uint64 &);
1363 template void dump_dec (const dump_metadata_t &metadata, const poly_offset_int &);
1364 template void dump_dec (const dump_metadata_t &metadata, const poly_widest_int &);
1365
1366 void
1367 dump_dec (dump_flags_t dump_kind, const poly_wide_int &value, signop sgn)
1368 {
1369 VERIFY_DUMP_ENABLED_P;
1370 if (dump_file
1371 && dump_context::get ().apply_dump_filter_p (dump_kind, pflags))
1372 print_dec (value, dump_file, sgn);
1373
1374 if (alt_dump_file
1375 && dump_context::get ().apply_dump_filter_p (dump_kind, alt_flags))
1376 print_dec (value, alt_dump_file, sgn);
1377 }
1378
1379 /* Output VALUE in hexadecimal to appropriate dump streams. */
1380
1381 void
1382 dump_hex (dump_flags_t dump_kind, const poly_wide_int &value)
1383 {
1384 VERIFY_DUMP_ENABLED_P;
1385 if (dump_file
1386 && dump_context::get ().apply_dump_filter_p (dump_kind, pflags))
1387 print_hex (value, dump_file);
1388
1389 if (alt_dump_file
1390 && dump_context::get ().apply_dump_filter_p (dump_kind, alt_flags))
1391 print_hex (value, alt_dump_file);
1392 }
1393
1394 /* Emit and delete the currently pending optinfo, if there is one,
1395 without the caller needing to know about class dump_context. */
1396
1397 void
1398 dumpfile_ensure_any_optinfo_are_flushed ()
1399 {
1400 dump_context::get().end_any_optinfo ();
1401 }
1402
1403 /* Output the name of NODE on appropriate dump streams. */
1404
1405 void
1406 dump_symtab_node (const dump_metadata_t &metadata, symtab_node *node)
1407 {
1408 VERIFY_DUMP_ENABLED_P;
1409 dump_context::get ().dump_symtab_node (metadata, node);
1410 }
1411
1412 /* Get the current dump scope-nesting depth.
1413 For use by -fopt-info (for showing nesting via indentation). */
1414
1415 unsigned int
1416 get_dump_scope_depth ()
1417 {
1418 return dump_context::get ().get_scope_depth ();
1419 }
1420
1421 /* Push a nested dump scope.
1422 Print "=== NAME ===\n" to the dumpfile, if any, and to the -fopt-info
1423 destination, if any.
1424 Emit a "scope" opinfo if optinfos are enabled.
1425 Increment the scope depth. */
1426
1427 void
1428 dump_begin_scope (const char *name,
1429 const dump_user_location_t &user_location,
1430 const dump_impl_location_t &impl_location)
1431 {
1432 dump_context::get ().begin_scope (name, user_location, impl_location);
1433 }
1434
1435 /* Pop a nested dump scope. */
1436
1437 void
1438 dump_end_scope ()
1439 {
1440 dump_context::get ().end_scope ();
1441 }
1442
1443 /* Start a dump for PHASE. Store user-supplied dump flags in
1444 *FLAG_PTR. Return the number of streams opened. Set globals
1445 DUMP_FILE, and ALT_DUMP_FILE to point to the opened streams, and
1446 set dump_flags appropriately for both pass dump stream and
1447 -fopt-info stream. */
1448
1449 int
1450 gcc::dump_manager::
1451 dump_start (int phase, dump_flags_t *flag_ptr)
1452 {
1453 int count = 0;
1454 char *name;
1455 struct dump_file_info *dfi;
1456 FILE *stream;
1457 if (phase == TDI_none || !dump_phase_enabled_p (phase))
1458 return 0;
1459
1460 dfi = get_dump_file_info (phase);
1461 name = get_dump_file_name (phase);
1462 if (name)
1463 {
1464 stream = dump_open (name, dfi->pstate < 0);
1465 if (stream)
1466 {
1467 dfi->pstate = 1;
1468 count++;
1469 }
1470 free (name);
1471 dfi->pstream = stream;
1472 set_dump_file (dfi->pstream);
1473 /* Initialize current dump flags. */
1474 pflags = dfi->pflags;
1475 }
1476
1477 stream = dump_open_alternate_stream (dfi);
1478 if (stream)
1479 {
1480 dfi->alt_stream = stream;
1481 count++;
1482 set_alt_dump_file (dfi->alt_stream);
1483 /* Initialize current -fopt-info flags. */
1484 alt_flags = dfi->alt_flags;
1485 }
1486
1487 if (flag_ptr)
1488 *flag_ptr = dfi->pflags;
1489
1490 return count;
1491 }
1492
1493 /* Finish a tree dump for PHASE and close associated dump streams. Also
1494 reset the globals DUMP_FILE, ALT_DUMP_FILE, and DUMP_FLAGS. */
1495
1496 void
1497 gcc::dump_manager::
1498 dump_finish (int phase)
1499 {
1500 struct dump_file_info *dfi;
1501
1502 if (phase < 0)
1503 return;
1504 dfi = get_dump_file_info (phase);
1505 if (dfi->pstream && dfi->pstream != stdout && dfi->pstream != stderr)
1506 fclose (dfi->pstream);
1507
1508 if (dfi->alt_stream && dfi->alt_stream != stdout && dfi->alt_stream != stderr)
1509 fclose (dfi->alt_stream);
1510
1511 dfi->alt_stream = NULL;
1512 dfi->pstream = NULL;
1513 set_dump_file (NULL);
1514 set_alt_dump_file (NULL);
1515 dump_flags = TDF_NONE;
1516 alt_flags = TDF_NONE;
1517 pflags = TDF_NONE;
1518 }
1519
1520 /* Begin a tree dump for PHASE. Stores any user supplied flag in
1521 *FLAG_PTR and returns a stream to write to. If the dump is not
1522 enabled, returns NULL.
1523 PART can be used for dump files which should be split to multiple
1524 parts. PART == -1 indicates dump file with no parts.
1525 If PART is -1, multiple calls will reopen and append to the dump file. */
1526
1527 FILE *
1528 dump_begin (int phase, dump_flags_t *flag_ptr, int part)
1529 {
1530 return g->get_dumps ()->dump_begin (phase, flag_ptr, part);
1531 }
1532
1533 FILE *
1534 gcc::dump_manager::
1535 dump_begin (int phase, dump_flags_t *flag_ptr, int part)
1536 {
1537 if (phase == TDI_none || !dump_phase_enabled_p (phase))
1538 return NULL;
1539
1540 char *name = get_dump_file_name (phase, part);
1541 if (!name)
1542 return NULL;
1543 struct dump_file_info *dfi = get_dump_file_info (phase);
1544
1545 /* We do not support re-opening of dump files with parts. This would require
1546 tracking pstate per part of the dump file. */
1547 FILE *stream = dump_open (name, part != -1 || dfi->pstate < 0);
1548 if (stream)
1549 dfi->pstate = 1;
1550 free (name);
1551
1552 if (flag_ptr)
1553 *flag_ptr = dfi->pflags;
1554
1555 /* Initialize current flags */
1556 pflags = dfi->pflags;
1557 return stream;
1558 }
1559
1560 /* Returns nonzero if dump PHASE is enabled for at least one stream.
1561 If PHASE is TDI_tree_all, return nonzero if any dump is enabled for
1562 any phase. */
1563
1564 int
1565 gcc::dump_manager::
1566 dump_phase_enabled_p (int phase) const
1567 {
1568 if (phase == TDI_tree_all)
1569 {
1570 size_t i;
1571 for (i = TDI_none + 1; i < (size_t) TDI_end; i++)
1572 if (dump_files[i].pstate || dump_files[i].alt_state)
1573 return 1;
1574 for (i = 0; i < m_extra_dump_files_in_use; i++)
1575 if (m_extra_dump_files[i].pstate || m_extra_dump_files[i].alt_state)
1576 return 1;
1577 return 0;
1578 }
1579 else
1580 {
1581 struct dump_file_info *dfi = get_dump_file_info (phase);
1582 return dfi->pstate || dfi->alt_state;
1583 }
1584 }
1585
1586 /* Returns nonzero if tree dump PHASE has been initialized. */
1587
1588 int
1589 gcc::dump_manager::
1590 dump_initialized_p (int phase) const
1591 {
1592 struct dump_file_info *dfi = get_dump_file_info (phase);
1593 return dfi->pstate > 0 || dfi->alt_state > 0;
1594 }
1595
1596 /* Returns the switch name of PHASE. */
1597
1598 const char *
1599 dump_flag_name (int phase)
1600 {
1601 return g->get_dumps ()->dump_flag_name (phase);
1602 }
1603
1604 const char *
1605 gcc::dump_manager::
1606 dump_flag_name (int phase) const
1607 {
1608 struct dump_file_info *dfi = get_dump_file_info (phase);
1609 return dfi->swtch;
1610 }
1611
1612 /* Handle -fdump-* and -fopt-info for a pass added after
1613 command-line options are parsed (those from plugins and
1614 those from backends).
1615
1616 Because the registration of plugin/backend passes happens after the
1617 command-line options are parsed, the options that specify single
1618 pass dumping (e.g. -fdump-tree-PASSNAME) cannot be used for new
1619 passes. Therefore we currently can only enable dumping of
1620 new passes when the 'dump-all' flags (e.g. -fdump-tree-all)
1621 are specified. This is done here.
1622
1623 Similarly, the saved -fopt-info options are wired up to the new pass. */
1624
1625 void
1626 gcc::dump_manager::register_pass (opt_pass *pass)
1627 {
1628 gcc_assert (pass);
1629
1630 register_one_dump_file (pass);
1631
1632 dump_file_info *pass_dfi = get_dump_file_info (pass->static_pass_number);
1633 gcc_assert (pass_dfi);
1634
1635 enum tree_dump_index tdi;
1636 if (pass->type == SIMPLE_IPA_PASS
1637 || pass->type == IPA_PASS)
1638 tdi = TDI_ipa_all;
1639 else if (pass->type == GIMPLE_PASS)
1640 tdi = TDI_tree_all;
1641 else
1642 tdi = TDI_rtl_all;
1643 const dump_file_info *tdi_dfi = get_dump_file_info (tdi);
1644 gcc_assert (tdi_dfi);
1645
1646 /* Check if dump-all flag is specified. */
1647 if (tdi_dfi->pstate)
1648 {
1649 pass_dfi->pstate = tdi_dfi->pstate;
1650 pass_dfi->pflags = tdi_dfi->pflags;
1651 }
1652
1653 update_dfi_for_opt_info (pass_dfi);
1654 }
1655
1656 /* Finish a tree dump for PHASE. STREAM is the stream created by
1657 dump_begin. */
1658
1659 void
1660 dump_end (int phase ATTRIBUTE_UNUSED, FILE *stream)
1661 {
1662 if (stream != stderr && stream != stdout)
1663 fclose (stream);
1664 }
1665
1666 /* Enable all tree dumps with FLAGS on FILENAME. Return number of
1667 enabled tree dumps. */
1668
1669 int
1670 gcc::dump_manager::
1671 dump_enable_all (dump_kind dkind, dump_flags_t flags, const char *filename)
1672 {
1673 int n = 0;
1674 size_t i;
1675
1676 for (i = TDI_none + 1; i < (size_t) TDI_end; i++)
1677 {
1678 if (dump_files[i].dkind == dkind)
1679 {
1680 const char *old_filename = dump_files[i].pfilename;
1681 dump_files[i].pstate = -1;
1682 dump_files[i].pflags |= flags;
1683 n++;
1684 /* Override the existing filename. */
1685 if (filename)
1686 {
1687 dump_files[i].pfilename = xstrdup (filename);
1688 /* Since it is a command-line provided file, which is
1689 common to all the phases, use it in append mode. */
1690 dump_files[i].pstate = 1;
1691 }
1692 if (old_filename && filename != old_filename)
1693 free (CONST_CAST (char *, old_filename));
1694 }
1695 }
1696
1697 for (i = 0; i < m_extra_dump_files_in_use; i++)
1698 {
1699 if (m_extra_dump_files[i].dkind == dkind)
1700 {
1701 const char *old_filename = m_extra_dump_files[i].pfilename;
1702 m_extra_dump_files[i].pstate = -1;
1703 m_extra_dump_files[i].pflags |= flags;
1704 n++;
1705 /* Override the existing filename. */
1706 if (filename)
1707 {
1708 m_extra_dump_files[i].pfilename = xstrdup (filename);
1709 /* Since it is a command-line provided file, which is
1710 common to all the phases, use it in append mode. */
1711 m_extra_dump_files[i].pstate = 1;
1712 }
1713 if (old_filename && filename != old_filename)
1714 free (CONST_CAST (char *, old_filename));
1715 }
1716 }
1717
1718 return n;
1719 }
1720
1721 /* Enable -fopt-info dumps on all dump files matching OPTGROUP_FLAGS.
1722 Enable dumps with FLAGS on FILENAME. Return the number of enabled
1723 dumps. */
1724
1725 int
1726 gcc::dump_manager::
1727 opt_info_enable_passes (optgroup_flags_t optgroup_flags, dump_flags_t flags,
1728 const char *filename)
1729 {
1730 int n = 0;
1731
1732 m_optgroup_flags = optgroup_flags;
1733 m_optinfo_flags = flags;
1734 m_optinfo_filename = xstrdup (filename);
1735
1736 for (size_t i = TDI_none + 1; i < (size_t) TDI_end; i++)
1737 if (update_dfi_for_opt_info (&dump_files[i]))
1738 n++;
1739
1740 for (size_t i = 0; i < m_extra_dump_files_in_use; i++)
1741 if (update_dfi_for_opt_info (&m_extra_dump_files[i]))
1742 n++;
1743
1744 return n;
1745 }
1746
1747 /* Use the saved -fopt-info options to update DFI.
1748 Return true if the dump is enabled. */
1749
1750 bool
1751 gcc::dump_manager::update_dfi_for_opt_info (dump_file_info *dfi) const
1752 {
1753 gcc_assert (dfi);
1754
1755 if (!(dfi->optgroup_flags & m_optgroup_flags))
1756 return false;
1757
1758 const char *old_filename = dfi->alt_filename;
1759 /* Since this file is shared among different passes, it
1760 should be opened in append mode. */
1761 dfi->alt_state = 1;
1762 dfi->alt_flags |= m_optinfo_flags;
1763 /* Override the existing filename. */
1764 if (m_optinfo_filename)
1765 dfi->alt_filename = xstrdup (m_optinfo_filename);
1766 if (old_filename && m_optinfo_filename != old_filename)
1767 free (CONST_CAST (char *, old_filename));
1768
1769 return true;
1770 }
1771
1772 /* Helper routine to parse -<dump format>[=filename]
1773 and return the corresponding dump flag. If POS_P is non-NULL,
1774 assign start of filename into *POS_P. */
1775
1776 dump_flags_t
1777 parse_dump_option (const char *option_value, const char **pos_p)
1778 {
1779 const char *ptr;
1780 dump_flags_t flags;
1781
1782 ptr = option_value;
1783 if (pos_p)
1784 *pos_p = NULL;
1785
1786 /* Retain "user-facing" and "internals" messages, but filter out
1787 those from an opt_problem being re-emitted at the top level
1788 (MSG_PRIORITY_REEMITTED), so as to avoid duplicate messages
1789 messing up scan-tree-dump-times" in DejaGnu tests. */
1790 flags = MSG_PRIORITY_USER_FACING | MSG_PRIORITY_INTERNALS;
1791
1792 while (*ptr)
1793 {
1794 const struct kv_pair<dump_flags_t> *option_ptr;
1795 const char *end_ptr;
1796 const char *eq_ptr;
1797 unsigned length;
1798 while (*ptr == '-')
1799 ptr++;
1800 end_ptr = strchr (ptr, '-');
1801 eq_ptr = strchr (ptr, '=');
1802
1803 if (eq_ptr && (!end_ptr || end_ptr > eq_ptr))
1804 end_ptr = eq_ptr;
1805
1806 if (!end_ptr)
1807 end_ptr = ptr + strlen (ptr);
1808 length = end_ptr - ptr;
1809
1810 for (option_ptr = dump_options; option_ptr->name; option_ptr++)
1811 if (strlen (option_ptr->name) == length
1812 && !memcmp (option_ptr->name, ptr, length))
1813 {
1814 flags |= option_ptr->value;
1815 goto found;
1816 }
1817
1818 if (*ptr == '=')
1819 {
1820 /* Interpret rest of the argument as a dump filename. This
1821 filename overrides other command line filenames. */
1822 if (pos_p)
1823 *pos_p = ptr + 1;
1824 break;
1825 }
1826 else
1827 {
1828 warning (0, "ignoring unknown option %q.*s",
1829 length, ptr);
1830 flags = TDF_ERROR;
1831 }
1832 found:
1833 ptr = end_ptr;
1834 }
1835
1836 return flags;
1837 }
1838
1839 /* Parse ARG as a dump switch. Return nonzero if it is, and store the
1840 relevant details in the dump_files array. */
1841
1842 int
1843 gcc::dump_manager::
1844 dump_switch_p_1 (const char *arg, struct dump_file_info *dfi, bool doglob)
1845 {
1846 const char *option_value;
1847 dump_flags_t flags = TDF_NONE;
1848
1849 if (doglob && !dfi->glob)
1850 return 0;
1851
1852 option_value = skip_leading_substring (arg, doglob ? dfi->glob : dfi->swtch);
1853 if (!option_value)
1854 return 0;
1855
1856 if (*option_value && *option_value != '-' && *option_value != '=')
1857 return 0;
1858
1859 const char *filename;
1860 flags = parse_dump_option (option_value, &filename);
1861 if (filename)
1862 {
1863 if (dfi->pfilename)
1864 free (CONST_CAST (char *, dfi->pfilename));
1865 dfi->pfilename = xstrdup (filename);
1866 }
1867
1868 dfi->pstate = -1;
1869 dfi->pflags |= flags;
1870
1871 /* Process -fdump-tree-all and -fdump-rtl-all, by enabling all the
1872 known dumps. */
1873 if (dfi->suffix == NULL)
1874 dump_enable_all (dfi->dkind, dfi->pflags, dfi->pfilename);
1875
1876 return 1;
1877 }
1878
1879 void
1880 gcc::dump_manager::
1881 dump_switch_p (const char *arg)
1882 {
1883 size_t i;
1884 int any = 0;
1885
1886 for (i = TDI_none + 1; i != TDI_end; i++)
1887 any |= dump_switch_p_1 (arg, &dump_files[i], false);
1888
1889 /* Don't glob if we got a hit already */
1890 if (!any)
1891 for (i = TDI_none + 1; i != TDI_end; i++)
1892 any |= dump_switch_p_1 (arg, &dump_files[i], true);
1893
1894 for (i = 0; i < m_extra_dump_files_in_use; i++)
1895 any |= dump_switch_p_1 (arg, &m_extra_dump_files[i], false);
1896
1897 if (!any)
1898 for (i = 0; i < m_extra_dump_files_in_use; i++)
1899 any |= dump_switch_p_1 (arg, &m_extra_dump_files[i], true);
1900
1901 if (!any)
1902 {
1903 auto_vec<const char *> candidates;
1904 for (size_t i = TDI_none + 1; i != TDI_end; i++)
1905 candidates.safe_push (dump_files[i].swtch);
1906 for (size_t i = 0; i < m_extra_dump_files_in_use; i++)
1907 candidates.safe_push (m_extra_dump_files[i].swtch);
1908 const char *hint = find_closest_string (arg, &candidates);
1909 if (hint)
1910 error ("unrecognized command-line option %<-fdump-%s%>; "
1911 "did you mean %<-fdump-%s%>?", arg, hint);
1912 else
1913 error ("unrecognized command-line option %<-fdump-%s%>", arg);
1914 }
1915 }
1916
1917 /* Parse ARG as a -fopt-info switch and store flags, optgroup_flags
1918 and filename. Return non-zero if it is a recognized switch. */
1919
1920 static int
1921 opt_info_switch_p_1 (const char *arg, dump_flags_t *flags,
1922 optgroup_flags_t *optgroup_flags, char **filename)
1923 {
1924 const char *option_value;
1925 const char *ptr;
1926
1927 option_value = arg;
1928 ptr = option_value;
1929
1930 *filename = NULL;
1931
1932 /* Default to filtering out "internals" messages, and retaining
1933 "user-facing" messages, and those from an opt_problem being
1934 re-emitted at the top level. */
1935 *flags = MSG_PRIORITY_USER_FACING | MSG_PRIORITY_REEMITTED;
1936
1937 *optgroup_flags = OPTGROUP_NONE;
1938
1939 if (!ptr)
1940 return 1; /* Handle '-fopt-info' without any additional options. */
1941
1942 while (*ptr)
1943 {
1944 const char *end_ptr;
1945 const char *eq_ptr;
1946 unsigned length;
1947
1948 while (*ptr == '-')
1949 ptr++;
1950 end_ptr = strchr (ptr, '-');
1951 eq_ptr = strchr (ptr, '=');
1952
1953 if (eq_ptr && (!end_ptr || eq_ptr < end_ptr))
1954 end_ptr = eq_ptr;
1955 else if (!end_ptr)
1956 end_ptr = ptr + strlen (ptr);
1957 length = end_ptr - ptr;
1958
1959 for (const kv_pair<dump_flags_t> *option_ptr = optinfo_verbosity_options;
1960 option_ptr->name; option_ptr++)
1961 if (strlen (option_ptr->name) == length
1962 && !memcmp (option_ptr->name, ptr, length))
1963 {
1964 *flags |= option_ptr->value;
1965 goto found;
1966 }
1967
1968 for (const kv_pair<optgroup_flags_t> *option_ptr = optgroup_options;
1969 option_ptr->name; option_ptr++)
1970 if (strlen (option_ptr->name) == length
1971 && !memcmp (option_ptr->name, ptr, length))
1972 {
1973 *optgroup_flags |= option_ptr->value;
1974 goto found;
1975 }
1976
1977 if (*ptr == '=')
1978 {
1979 /* Interpret rest of the argument as a dump filename. This
1980 filename overrides other command line filenames. */
1981 *filename = xstrdup (ptr + 1);
1982 break;
1983 }
1984 else
1985 {
1986 warning (0, "unknown option %q.*s in %<-fopt-info-%s%>",
1987 length, ptr, arg);
1988 return 0;
1989 }
1990 found:;
1991 ptr = end_ptr;
1992 }
1993
1994 return 1;
1995 }
1996
1997 /* Return non-zero if ARG is a recognized switch for
1998 -fopt-info. Return zero otherwise. */
1999
2000 int
2001 opt_info_switch_p (const char *arg)
2002 {
2003 dump_flags_t flags;
2004 optgroup_flags_t optgroup_flags;
2005 char *filename;
2006 static char *file_seen = NULL;
2007 gcc::dump_manager *dumps = g->get_dumps ();
2008
2009 if (!opt_info_switch_p_1 (arg, &flags, &optgroup_flags, &filename))
2010 return 0;
2011
2012 if (!filename)
2013 filename = xstrdup ("stderr");
2014
2015 /* Bail out if a different filename has been specified. */
2016 if (file_seen && strcmp (file_seen, filename))
2017 {
2018 warning (0, "ignoring possibly conflicting option %<-fopt-info-%s%>",
2019 arg);
2020 return 1;
2021 }
2022
2023 file_seen = xstrdup (filename);
2024 if (!(flags & MSG_ALL_KINDS))
2025 flags |= MSG_OPTIMIZED_LOCATIONS;
2026 if (!optgroup_flags)
2027 optgroup_flags = OPTGROUP_ALL;
2028
2029 return dumps->opt_info_enable_passes (optgroup_flags, flags, filename);
2030 }
2031
2032 /* Print basic block on the dump streams. */
2033
2034 void
2035 dump_basic_block (dump_flags_t dump_kind, basic_block bb, int indent)
2036 {
2037 if (dump_file
2038 && dump_context::get ().apply_dump_filter_p (dump_kind, pflags))
2039 dump_bb (dump_file, bb, indent, TDF_DETAILS);
2040 if (alt_dump_file
2041 && dump_context::get ().apply_dump_filter_p (dump_kind, alt_flags))
2042 dump_bb (alt_dump_file, bb, indent, TDF_DETAILS);
2043 }
2044
2045 /* Dump FUNCTION_DECL FN as tree dump PHASE. */
2046
2047 void
2048 dump_function (int phase, tree fn)
2049 {
2050 FILE *stream;
2051 dump_flags_t flags;
2052
2053 stream = dump_begin (phase, &flags);
2054 if (stream)
2055 {
2056 dump_function_to_file (fn, stream, flags);
2057 dump_end (phase, stream);
2058 }
2059 }
2060
2061 /* Print information from the combine pass on dump_file. */
2062
2063 void
2064 print_combine_total_stats (void)
2065 {
2066 if (dump_file)
2067 dump_combine_total_stats (dump_file);
2068 }
2069
2070 /* Enable RTL dump for all the RTL passes. */
2071
2072 bool
2073 enable_rtl_dump_file (void)
2074 {
2075 gcc::dump_manager *dumps = g->get_dumps ();
2076 int num_enabled =
2077 dumps->dump_enable_all (DK_rtl, dump_flags_t (TDF_DETAILS) | TDF_BLOCKS,
2078 NULL);
2079 return num_enabled > 0;
2080 }
2081
2082 /* debug_dump_context's ctor. Temporarily override the dump_context
2083 (to forcibly enable output to stderr). */
2084
2085 debug_dump_context::debug_dump_context ()
2086 : m_context (),
2087 m_saved (&dump_context::get ()),
2088 m_saved_flags (dump_flags),
2089 m_saved_pflags (pflags),
2090 m_saved_file (dump_file)
2091 {
2092 set_dump_file (stderr);
2093 dump_context::s_current = &m_context;
2094 pflags = dump_flags = MSG_ALL_KINDS | MSG_ALL_PRIORITIES;
2095 dump_context::get ().refresh_dumps_are_enabled ();
2096 }
2097
2098 /* debug_dump_context's dtor. Restore the saved dump_context. */
2099
2100 debug_dump_context::~debug_dump_context ()
2101 {
2102 set_dump_file (m_saved_file);
2103 dump_context::s_current = m_saved;
2104 dump_flags = m_saved_flags;
2105 pflags = m_saved_pflags;
2106 dump_context::get ().refresh_dumps_are_enabled ();
2107 }
2108
2109
2110 #if CHECKING_P
2111
2112 namespace selftest {
2113
2114 /* temp_dump_context's ctor. Temporarily override the dump_context
2115 (to forcibly enable optinfo-generation). */
2116
2117 temp_dump_context::temp_dump_context (bool forcibly_enable_optinfo,
2118 bool forcibly_enable_dumping,
2119 dump_flags_t test_pp_flags)
2120 : m_context (),
2121 m_saved (&dump_context::get ())
2122 {
2123 dump_context::s_current = &m_context;
2124 if (forcibly_enable_optinfo)
2125 m_context.set_json_writer (new optrecord_json_writer ());
2126 /* Conditionally enable the test dump, so that we can verify both the
2127 dump_enabled_p and the !dump_enabled_p cases in selftests. */
2128 if (forcibly_enable_dumping)
2129 {
2130 m_context.m_test_pp = &m_pp;
2131 m_context.m_test_pp_flags = test_pp_flags;
2132 }
2133
2134 dump_context::get ().refresh_dumps_are_enabled ();
2135 }
2136
2137 /* temp_dump_context's dtor. Restore the saved dump_context. */
2138
2139 temp_dump_context::~temp_dump_context ()
2140 {
2141 m_context.set_json_writer (NULL);
2142
2143 dump_context::s_current = m_saved;
2144
2145 dump_context::get ().refresh_dumps_are_enabled ();
2146 }
2147
2148 /* 0-terminate the text dumped so far, and return it. */
2149
2150 const char *
2151 temp_dump_context::get_dumped_text ()
2152 {
2153 return pp_formatted_text (&m_pp);
2154 }
2155
2156 /* Verify that IMPL_LOC is within EXPECTED_FILE at EXPECTED_LINE,
2157 from EXPECTED_FUNCTION, using LOC for the location of any failure,
2158 provided that the build compiler is sufficiently recent. */
2159
2160 static void
2161 assert_impl_location_eq (const location &loc ATTRIBUTE_UNUSED,
2162 const dump_impl_location_t &impl_loc ATTRIBUTE_UNUSED,
2163 const char *expected_file ATTRIBUTE_UNUSED,
2164 int expected_line ATTRIBUTE_UNUSED,
2165 const char *expected_function ATTRIBUTE_UNUSED)
2166 {
2167 #if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8)
2168 ASSERT_STR_CONTAINS_AT (loc, impl_loc.m_file, expected_file);
2169 ASSERT_EQ_AT (loc, impl_loc.m_line, expected_line);
2170 ASSERT_STR_CONTAINS_AT (loc, impl_loc.m_function, expected_function);
2171 #endif
2172 }
2173
2174 /* Verify that IMPL_LOC is within EXPECTED_FILE at EXPECTED_LINE,
2175 from EXPECTED_FUNCTION, provided that the build compiler is
2176 sufficiently recent. */
2177
2178 #define ASSERT_IMPL_LOCATION_EQ(IMPL_LOC, EXPECTED_FILE, EXPECTED_LINE, \
2179 EXPECTED_FUNCTION) \
2180 SELFTEST_BEGIN_STMT \
2181 assert_impl_location_eq (SELFTEST_LOCATION, IMPL_LOC, \
2182 EXPECTED_FILE, EXPECTED_LINE, \
2183 EXPECTED_FUNCTION); \
2184 SELFTEST_END_STMT
2185
2186 /* Verify that the dump_location_t constructors capture the source location
2187 at which they were called (provided that the build compiler is sufficiently
2188 recent). */
2189
2190 static void
2191 test_impl_location ()
2192 {
2193 /* Default ctor. */
2194 {
2195 dump_location_t loc;
2196 const int expected_line = __LINE__ - 1;
2197 ASSERT_IMPL_LOCATION_EQ (loc.get_impl_location (),
2198 "dumpfile.c", expected_line, "test_impl_location");
2199 }
2200
2201 /* Constructing from a gimple. */
2202 {
2203 dump_location_t loc ((gimple *)NULL);
2204 const int expected_line = __LINE__ - 1;
2205 ASSERT_IMPL_LOCATION_EQ (loc.get_impl_location (),
2206 "dumpfile.c", expected_line, "test_impl_location");
2207 }
2208
2209 /* Constructing from an rtx_insn. */
2210 {
2211 dump_location_t loc ((rtx_insn *)NULL);
2212 const int expected_line = __LINE__ - 1;
2213 ASSERT_IMPL_LOCATION_EQ (loc.get_impl_location (),
2214 "dumpfile.c", expected_line, "test_impl_location");
2215 }
2216 }
2217
2218 /* Verify that the text dumped so far in CONTEXT equals
2219 EXPECTED_TEXT, using LOC for the location of any failure.
2220 As a side-effect, the internal buffer is 0-terminated. */
2221
2222 void
2223 verify_dumped_text (const location &loc,
2224 temp_dump_context *context,
2225 const char *expected_text)
2226 {
2227 gcc_assert (context);
2228 ASSERT_STREQ_AT (loc, context->get_dumped_text (),
2229 expected_text);
2230 }
2231
2232 /* Verify that ITEM has the expected values. */
2233
2234 void
2235 verify_item (const location &loc,
2236 const optinfo_item *item,
2237 enum optinfo_item_kind expected_kind,
2238 location_t expected_location,
2239 const char *expected_text)
2240 {
2241 ASSERT_EQ_AT (loc, item->get_kind (), expected_kind);
2242 ASSERT_EQ_AT (loc, item->get_location (), expected_location);
2243 ASSERT_STREQ_AT (loc, item->get_text (), expected_text);
2244 }
2245
2246 /* Verify that calls to the dump_* API are captured and consolidated into
2247 optimization records. */
2248
2249 static void
2250 test_capture_of_dump_calls (const line_table_case &case_)
2251 {
2252 /* Generate a location_t for testing. */
2253 line_table_test ltt (case_);
2254 linemap_add (line_table, LC_ENTER, false, "test.txt", 0);
2255 linemap_line_start (line_table, 5, 100);
2256 linemap_add (line_table, LC_LEAVE, false, NULL, 0);
2257 location_t decl_loc = linemap_position_for_column (line_table, 8);
2258 location_t stmt_loc = linemap_position_for_column (line_table, 10);
2259 if (stmt_loc > LINE_MAP_MAX_LOCATION_WITH_COLS)
2260 return;
2261
2262 dump_user_location_t loc = dump_user_location_t::from_location_t (stmt_loc);
2263
2264 gimple *stmt = gimple_build_return (NULL);
2265 gimple_set_location (stmt, stmt_loc);
2266
2267 tree test_decl = build_decl (decl_loc, FUNCTION_DECL,
2268 get_identifier ("test_decl"),
2269 build_function_type_list (void_type_node,
2270 NULL_TREE));
2271
2272 symbol_table_test tmp_symtab;
2273
2274 cgraph_node *node = cgraph_node::get_create (test_decl);
2275 gcc_assert (node);
2276
2277 /* Run all tests twice, with and then without optinfo enabled, to ensure
2278 that immediate destinations vs optinfo-based destinations both
2279 work, independently of each other, with no leaks. */
2280 for (int i = 0 ; i < 2; i++)
2281 {
2282 bool with_optinfo = (i == 0);
2283
2284 /* Test of dump_printf. */
2285 {
2286 temp_dump_context tmp (with_optinfo, true,
2287 MSG_ALL_KINDS | MSG_PRIORITY_USER_FACING);
2288 dump_printf (MSG_NOTE, "int: %i str: %s", 42, "foo");
2289 const int expected_impl_line = __LINE__ - 1;
2290
2291 ASSERT_DUMPED_TEXT_EQ (tmp, "int: 42 str: foo");
2292 if (with_optinfo)
2293 {
2294 optinfo *info = tmp.get_pending_optinfo ();
2295 ASSERT_TRUE (info != NULL);
2296 ASSERT_EQ (info->get_kind (), OPTINFO_KIND_NOTE);
2297 ASSERT_EQ (info->num_items (), 1);
2298 ASSERT_IS_TEXT (info->get_item (0), "int: 42 str: foo");
2299 ASSERT_IMPL_LOCATION_EQ (info->get_impl_location (),
2300 "dumpfile.c", expected_impl_line,
2301 "test_capture_of_dump_calls");
2302 }
2303 }
2304
2305 /* Test of dump_printf with %T. */
2306 {
2307 temp_dump_context tmp (with_optinfo, true,
2308 MSG_ALL_KINDS | MSG_PRIORITY_USER_FACING);
2309 dump_printf (MSG_NOTE, "tree: %T", integer_zero_node);
2310 const int expected_impl_line = __LINE__ - 1;
2311
2312 ASSERT_DUMPED_TEXT_EQ (tmp, "tree: 0");
2313 if (with_optinfo)
2314 {
2315 optinfo *info = tmp.get_pending_optinfo ();
2316 ASSERT_TRUE (info != NULL);
2317 ASSERT_EQ (info->get_kind (), OPTINFO_KIND_NOTE);
2318 ASSERT_EQ (info->num_items (), 2);
2319 ASSERT_IS_TEXT (info->get_item (0), "tree: ");
2320 ASSERT_IS_TREE (info->get_item (1), UNKNOWN_LOCATION, "0");
2321 ASSERT_IMPL_LOCATION_EQ (info->get_impl_location (),
2322 "dumpfile.c", expected_impl_line,
2323 "test_capture_of_dump_calls");
2324 }
2325 }
2326
2327 /* Test of dump_printf with %E. */
2328 {
2329 temp_dump_context tmp (with_optinfo, true,
2330 MSG_ALL_KINDS | MSG_PRIORITY_USER_FACING);
2331 dump_printf (MSG_NOTE, "gimple: %E", stmt);
2332 const int expected_impl_line = __LINE__ - 1;
2333
2334 ASSERT_DUMPED_TEXT_EQ (tmp, "gimple: return;");
2335 if (with_optinfo)
2336 {
2337 optinfo *info = tmp.get_pending_optinfo ();
2338 ASSERT_TRUE (info != NULL);
2339 ASSERT_EQ (info->get_kind (), OPTINFO_KIND_NOTE);
2340 ASSERT_EQ (info->num_items (), 2);
2341 ASSERT_IS_TEXT (info->get_item (0), "gimple: ");
2342 ASSERT_IS_GIMPLE (info->get_item (1), stmt_loc, "return;");
2343 ASSERT_IMPL_LOCATION_EQ (info->get_impl_location (),
2344 "dumpfile.c", expected_impl_line,
2345 "test_capture_of_dump_calls");
2346 }
2347 }
2348
2349 /* Test of dump_printf with %G. */
2350 {
2351 temp_dump_context tmp (with_optinfo, true,
2352 MSG_ALL_KINDS | MSG_PRIORITY_USER_FACING);
2353 dump_printf (MSG_NOTE, "gimple: %G", stmt);
2354 const int expected_impl_line = __LINE__ - 1;
2355
2356 ASSERT_DUMPED_TEXT_EQ (tmp, "gimple: return;\n");
2357 if (with_optinfo)
2358 {
2359 optinfo *info = tmp.get_pending_optinfo ();
2360 ASSERT_TRUE (info != NULL);
2361 ASSERT_EQ (info->get_kind (), OPTINFO_KIND_NOTE);
2362 ASSERT_EQ (info->num_items (), 2);
2363 ASSERT_IS_TEXT (info->get_item (0), "gimple: ");
2364 ASSERT_IS_GIMPLE (info->get_item (1), stmt_loc, "return;\n");
2365 ASSERT_IMPL_LOCATION_EQ (info->get_impl_location (),
2366 "dumpfile.c", expected_impl_line,
2367 "test_capture_of_dump_calls");
2368 }
2369 }
2370
2371 /* Test of dump_printf with %C. */
2372 {
2373 temp_dump_context tmp (with_optinfo, true,
2374 MSG_ALL_KINDS | MSG_PRIORITY_USER_FACING);
2375 dump_printf (MSG_NOTE, "node: %C", node);
2376 const int expected_impl_line = __LINE__ - 1;
2377
2378 ASSERT_DUMPED_TEXT_EQ (tmp, "node: test_decl/0");
2379 if (with_optinfo)
2380 {
2381 optinfo *info = tmp.get_pending_optinfo ();
2382 ASSERT_TRUE (info != NULL);
2383 ASSERT_EQ (info->get_kind (), OPTINFO_KIND_NOTE);
2384 ASSERT_EQ (info->num_items (), 2);
2385 ASSERT_IS_TEXT (info->get_item (0), "node: ");
2386 ASSERT_IS_SYMTAB_NODE (info->get_item (1), decl_loc, "test_decl/0");
2387 ASSERT_IMPL_LOCATION_EQ (info->get_impl_location (),
2388 "dumpfile.c", expected_impl_line,
2389 "test_capture_of_dump_calls");
2390 }
2391 }
2392
2393 /* dump_print_loc with multiple format codes. This tests various
2394 things:
2395 - intermingling of text, format codes handled by the base
2396 pretty_printer, and dump-specific format codes
2397 - multiple dump-specific format codes: some consecutive, others
2398 separated by text, trailing text after the final one. */
2399 {
2400 temp_dump_context tmp (with_optinfo, true,
2401 MSG_ALL_KINDS | MSG_PRIORITY_USER_FACING);
2402 dump_printf_loc (MSG_NOTE, loc, "before %T and %T"
2403 " %i consecutive %E%E after\n",
2404 integer_zero_node, test_decl, 42, stmt, stmt);
2405
2406 ASSERT_DUMPED_TEXT_EQ (tmp,
2407 "test.txt:5:10: note: before 0 and test_decl"
2408 " 42 consecutive return;return; after\n");
2409 if (with_optinfo)
2410 {
2411 optinfo *info = tmp.get_pending_optinfo ();
2412 ASSERT_TRUE (info != NULL);
2413 ASSERT_EQ (info->get_kind (), OPTINFO_KIND_NOTE);
2414 ASSERT_EQ (info->num_items (), 8);
2415 ASSERT_IS_TEXT (info->get_item (0), "before ");
2416 ASSERT_IS_TREE (info->get_item (1), UNKNOWN_LOCATION, "0");
2417 ASSERT_IS_TEXT (info->get_item (2), " and ");
2418 ASSERT_IS_TREE (info->get_item (3), UNKNOWN_LOCATION, "test_decl");
2419 ASSERT_IS_TEXT (info->get_item (4), " 42 consecutive ");
2420 ASSERT_IS_GIMPLE (info->get_item (5), stmt_loc, "return;");
2421 ASSERT_IS_GIMPLE (info->get_item (6), stmt_loc, "return;");
2422 ASSERT_IS_TEXT (info->get_item (7), " after\n");
2423 /* We don't ASSERT_IMPL_LOCATION_EQ here, to avoid having to
2424 enforce at which exact line the multiline dump_printf_loc
2425 occurred. */
2426 }
2427 }
2428
2429 /* Tree, via dump_generic_expr. */
2430 {
2431 temp_dump_context tmp (with_optinfo, true,
2432 MSG_ALL_KINDS | MSG_PRIORITY_USER_FACING);
2433 dump_printf_loc (MSG_NOTE, loc, "test of tree: ");
2434 const int expected_impl_line = __LINE__ - 1;
2435 dump_generic_expr (MSG_NOTE, TDF_SLIM, integer_zero_node);
2436
2437 ASSERT_DUMPED_TEXT_EQ (tmp, "test.txt:5:10: note: test of tree: 0");
2438 if (with_optinfo)
2439 {
2440 optinfo *info = tmp.get_pending_optinfo ();
2441 ASSERT_TRUE (info != NULL);
2442 ASSERT_EQ (info->get_location_t (), stmt_loc);
2443 ASSERT_EQ (info->get_kind (), OPTINFO_KIND_NOTE);
2444 ASSERT_EQ (info->num_items (), 2);
2445 ASSERT_IS_TEXT (info->get_item (0), "test of tree: ");
2446 ASSERT_IS_TREE (info->get_item (1), UNKNOWN_LOCATION, "0");
2447 ASSERT_IMPL_LOCATION_EQ (info->get_impl_location (),
2448 "dumpfile.c", expected_impl_line,
2449 "test_capture_of_dump_calls");
2450 }
2451 }
2452
2453 /* Tree, via dump_generic_expr_loc. */
2454 {
2455 temp_dump_context tmp (with_optinfo, true,
2456 MSG_ALL_KINDS | MSG_PRIORITY_USER_FACING);
2457 dump_generic_expr_loc (MSG_NOTE, loc, TDF_SLIM, integer_one_node);
2458 const int expected_impl_line = __LINE__ - 1;
2459
2460 ASSERT_DUMPED_TEXT_EQ (tmp, "test.txt:5:10: note: 1");
2461 if (with_optinfo)
2462 {
2463 optinfo *info = tmp.get_pending_optinfo ();
2464 ASSERT_TRUE (info != NULL);
2465 ASSERT_EQ (info->get_location_t (), stmt_loc);
2466 ASSERT_EQ (info->get_kind (), OPTINFO_KIND_NOTE);
2467 ASSERT_EQ (info->num_items (), 1);
2468 ASSERT_IS_TREE (info->get_item (0), UNKNOWN_LOCATION, "1");
2469 ASSERT_IMPL_LOCATION_EQ (info->get_impl_location (),
2470 "dumpfile.c", expected_impl_line,
2471 "test_capture_of_dump_calls");
2472 }
2473 }
2474
2475 /* Gimple. */
2476 {
2477 /* dump_gimple_stmt_loc. */
2478 {
2479 temp_dump_context tmp (with_optinfo, true,
2480 MSG_ALL_KINDS | MSG_PRIORITY_USER_FACING);
2481 dump_gimple_stmt_loc (MSG_NOTE, loc, TDF_SLIM, stmt, 2);
2482 const int expected_impl_line = __LINE__ - 1;
2483
2484 ASSERT_DUMPED_TEXT_EQ (tmp, "test.txt:5:10: note: return;\n");
2485 if (with_optinfo)
2486 {
2487 optinfo *info = tmp.get_pending_optinfo ();
2488 ASSERT_TRUE (info != NULL);
2489 ASSERT_EQ (info->num_items (), 1);
2490 ASSERT_IS_GIMPLE (info->get_item (0), stmt_loc, "return;\n");
2491 ASSERT_IMPL_LOCATION_EQ (info->get_impl_location (),
2492 "dumpfile.c", expected_impl_line,
2493 "test_capture_of_dump_calls");
2494 }
2495 }
2496
2497 /* dump_gimple_stmt. */
2498 {
2499 temp_dump_context tmp (with_optinfo, true,
2500 MSG_ALL_KINDS | MSG_PRIORITY_USER_FACING);
2501 dump_gimple_stmt (MSG_NOTE, TDF_SLIM, stmt, 2);
2502 const int expected_impl_line = __LINE__ - 1;
2503
2504 ASSERT_DUMPED_TEXT_EQ (tmp, "return;\n");
2505 if (with_optinfo)
2506 {
2507 optinfo *info = tmp.get_pending_optinfo ();
2508 ASSERT_TRUE (info != NULL);
2509 ASSERT_EQ (info->num_items (), 1);
2510 ASSERT_IS_GIMPLE (info->get_item (0), stmt_loc, "return;\n");
2511 ASSERT_IMPL_LOCATION_EQ (info->get_impl_location (),
2512 "dumpfile.c", expected_impl_line,
2513 "test_capture_of_dump_calls");
2514 }
2515 }
2516
2517 /* dump_gimple_expr_loc. */
2518 {
2519 temp_dump_context tmp (with_optinfo, true,
2520 MSG_ALL_KINDS | MSG_PRIORITY_USER_FACING);
2521 dump_gimple_expr_loc (MSG_NOTE, loc, TDF_SLIM, stmt, 2);
2522 const int expected_impl_line = __LINE__ - 1;
2523
2524 ASSERT_DUMPED_TEXT_EQ (tmp, "test.txt:5:10: note: return;");
2525 if (with_optinfo)
2526 {
2527 optinfo *info = tmp.get_pending_optinfo ();
2528 ASSERT_TRUE (info != NULL);
2529 ASSERT_EQ (info->num_items (), 1);
2530 ASSERT_IS_GIMPLE (info->get_item (0), stmt_loc, "return;");
2531 ASSERT_IMPL_LOCATION_EQ (info->get_impl_location (),
2532 "dumpfile.c", expected_impl_line,
2533 "test_capture_of_dump_calls");
2534 }
2535 }
2536
2537 /* dump_gimple_expr. */
2538 {
2539 temp_dump_context tmp (with_optinfo, true,
2540 MSG_ALL_KINDS | MSG_PRIORITY_USER_FACING);
2541 dump_gimple_expr (MSG_NOTE, TDF_SLIM, stmt, 2);
2542 const int expected_impl_line = __LINE__ - 1;
2543
2544 ASSERT_DUMPED_TEXT_EQ (tmp, "return;");
2545 if (with_optinfo)
2546 {
2547 optinfo *info = tmp.get_pending_optinfo ();
2548 ASSERT_TRUE (info != NULL);
2549 ASSERT_EQ (info->num_items (), 1);
2550 ASSERT_IS_GIMPLE (info->get_item (0), stmt_loc, "return;");
2551 ASSERT_IMPL_LOCATION_EQ (info->get_impl_location (),
2552 "dumpfile.c", expected_impl_line,
2553 "test_capture_of_dump_calls");
2554 }
2555 }
2556 }
2557
2558 /* symtab_node. */
2559 {
2560 temp_dump_context tmp (with_optinfo, true,
2561 MSG_ALL_KINDS | MSG_PRIORITY_USER_FACING);
2562 dump_symtab_node (MSG_NOTE, node);
2563 const int expected_impl_line = __LINE__ - 1;
2564
2565 ASSERT_DUMPED_TEXT_EQ (tmp, "test_decl/0");
2566 if (with_optinfo)
2567 {
2568 optinfo *info = tmp.get_pending_optinfo ();
2569 ASSERT_TRUE (info != NULL);
2570 ASSERT_EQ (info->get_kind (), OPTINFO_KIND_NOTE);
2571 ASSERT_EQ (info->num_items (), 1);
2572 ASSERT_IS_SYMTAB_NODE (info->get_item (0), decl_loc, "test_decl/0");
2573 ASSERT_IMPL_LOCATION_EQ (info->get_impl_location (),
2574 "dumpfile.c", expected_impl_line,
2575 "test_capture_of_dump_calls");
2576 }
2577 }
2578
2579 /* poly_int. */
2580 {
2581 temp_dump_context tmp (with_optinfo, true,
2582 MSG_ALL_KINDS | MSG_PRIORITY_USER_FACING);
2583 dump_dec (MSG_NOTE, poly_int64 (42));
2584 const int expected_impl_line = __LINE__ - 1;
2585
2586 ASSERT_DUMPED_TEXT_EQ (tmp, "42");
2587 if (with_optinfo)
2588 {
2589 optinfo *info = tmp.get_pending_optinfo ();
2590 ASSERT_TRUE (info != NULL);
2591 ASSERT_EQ (info->num_items (), 1);
2592 ASSERT_IS_TEXT (info->get_item (0), "42");
2593 ASSERT_IMPL_LOCATION_EQ (info->get_impl_location (),
2594 "dumpfile.c", expected_impl_line,
2595 "test_capture_of_dump_calls");
2596 }
2597 }
2598
2599 /* Scopes. Test with all 4 combinations of
2600 filtering by MSG_PRIORITY_USER_FACING
2601 and/or filtering by MSG_PRIORITY_INTERNALS. */
2602 for (int j = 0; j < 3; j++)
2603 {
2604 dump_flags_t dump_filter = MSG_ALL_KINDS;
2605 if (j % 2)
2606 dump_filter |= MSG_PRIORITY_USER_FACING;
2607 if (j / 2)
2608 dump_filter |= MSG_PRIORITY_INTERNALS;
2609
2610 temp_dump_context tmp (with_optinfo, true, dump_filter);
2611 /* Emit various messages, mostly with implicit priority. */
2612 dump_printf_loc (MSG_NOTE, stmt, "msg 1\n");
2613 dump_printf_loc (MSG_NOTE | MSG_PRIORITY_INTERNALS, stmt,
2614 "explicitly internal msg\n");
2615 {
2616 AUTO_DUMP_SCOPE ("outer scope", stmt);
2617 dump_printf_loc (MSG_NOTE, stmt, "msg 2\n");
2618 {
2619 AUTO_DUMP_SCOPE ("middle scope", stmt);
2620 dump_printf_loc (MSG_NOTE, stmt, "msg 3\n");
2621 {
2622 AUTO_DUMP_SCOPE ("inner scope", stmt);
2623 dump_printf_loc (MSG_NOTE, stmt, "msg 4\n");
2624 dump_printf_loc (MSG_NOTE | MSG_PRIORITY_USER_FACING, stmt,
2625 "explicitly user-facing msg\n");
2626 }
2627 dump_printf_loc (MSG_NOTE, stmt, "msg 5\n");
2628 }
2629 dump_printf_loc (MSG_NOTE, stmt, "msg 6\n");
2630 }
2631 dump_printf_loc (MSG_NOTE, stmt, "msg 7\n");
2632 const int expected_impl_line = __LINE__ - 1;
2633
2634 switch (dump_filter & MSG_ALL_PRIORITIES)
2635 {
2636 default:
2637 gcc_unreachable ();
2638 case 0:
2639 ASSERT_DUMPED_TEXT_EQ (tmp, "");
2640 break;
2641 case MSG_PRIORITY_USER_FACING:
2642 ASSERT_DUMPED_TEXT_EQ
2643 (tmp,
2644 "test.txt:5:10: note: msg 1\n"
2645 "test.txt:5:10: note: explicitly user-facing msg\n"
2646 "test.txt:5:10: note: msg 7\n");
2647 break;
2648 case MSG_PRIORITY_INTERNALS:
2649 ASSERT_DUMPED_TEXT_EQ
2650 (tmp,
2651 "test.txt:5:10: note: explicitly internal msg\n"
2652 "test.txt:5:10: note: === outer scope ===\n"
2653 "test.txt:5:10: note: msg 2\n"
2654 "test.txt:5:10: note: === middle scope ===\n"
2655 "test.txt:5:10: note: msg 3\n"
2656 "test.txt:5:10: note: === inner scope ===\n"
2657 "test.txt:5:10: note: msg 4\n"
2658 "test.txt:5:10: note: msg 5\n"
2659 "test.txt:5:10: note: msg 6\n");
2660 break;
2661 case MSG_ALL_PRIORITIES:
2662 ASSERT_DUMPED_TEXT_EQ
2663 (tmp,
2664 "test.txt:5:10: note: msg 1\n"
2665 "test.txt:5:10: note: explicitly internal msg\n"
2666 "test.txt:5:10: note: === outer scope ===\n"
2667 "test.txt:5:10: note: msg 2\n"
2668 "test.txt:5:10: note: === middle scope ===\n"
2669 "test.txt:5:10: note: msg 3\n"
2670 "test.txt:5:10: note: === inner scope ===\n"
2671 "test.txt:5:10: note: msg 4\n"
2672 "test.txt:5:10: note: explicitly user-facing msg\n"
2673 "test.txt:5:10: note: msg 5\n"
2674 "test.txt:5:10: note: msg 6\n"
2675 "test.txt:5:10: note: msg 7\n");
2676 break;
2677 }
2678 if (with_optinfo)
2679 {
2680 optinfo *info = tmp.get_pending_optinfo ();
2681 ASSERT_TRUE (info != NULL);
2682 ASSERT_EQ (info->num_items (), 1);
2683 ASSERT_IS_TEXT (info->get_item (0), "msg 7\n");
2684 ASSERT_IMPL_LOCATION_EQ (info->get_impl_location (),
2685 "dumpfile.c", expected_impl_line,
2686 "test_capture_of_dump_calls");
2687 }
2688 }
2689 }
2690
2691 /* Verify that MSG_* affects optinfo->get_kind (); we tested MSG_NOTE
2692 above. */
2693 {
2694 /* MSG_OPTIMIZED_LOCATIONS. */
2695 {
2696 temp_dump_context tmp (true, true, MSG_ALL_KINDS);
2697 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, loc, "test");
2698 ASSERT_EQ (tmp.get_pending_optinfo ()->get_kind (),
2699 OPTINFO_KIND_SUCCESS);
2700 }
2701
2702 /* MSG_MISSED_OPTIMIZATION. */
2703 {
2704 temp_dump_context tmp (true, true, MSG_ALL_KINDS);
2705 dump_printf_loc (MSG_MISSED_OPTIMIZATION, loc, "test");
2706 ASSERT_EQ (tmp.get_pending_optinfo ()->get_kind (),
2707 OPTINFO_KIND_FAILURE);
2708 }
2709 }
2710
2711 /* Verify that MSG_* affect AUTO_DUMP_SCOPE and the dump calls. */
2712 {
2713 temp_dump_context tmp (false, true,
2714 MSG_OPTIMIZED_LOCATIONS | MSG_ALL_PRIORITIES);
2715 dump_printf_loc (MSG_NOTE, stmt, "msg 1\n");
2716 {
2717 AUTO_DUMP_SCOPE ("outer scope", stmt);
2718 dump_printf_loc (MSG_NOTE, stmt, "msg 2\n");
2719 {
2720 AUTO_DUMP_SCOPE ("middle scope", stmt);
2721 dump_printf_loc (MSG_NOTE, stmt, "msg 3\n");
2722 {
2723 AUTO_DUMP_SCOPE ("inner scope", stmt);
2724 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, stmt, "msg 4\n");
2725 }
2726 dump_printf_loc (MSG_NOTE, stmt, "msg 5\n");
2727 }
2728 dump_printf_loc (MSG_NOTE, stmt, "msg 6\n");
2729 }
2730 dump_printf_loc (MSG_NOTE, stmt, "msg 7\n");
2731
2732 ASSERT_DUMPED_TEXT_EQ (tmp, "test.txt:5:10: optimized: msg 4\n");
2733 }
2734 }
2735
2736 static void
2737 test_pr87025 ()
2738 {
2739 dump_user_location_t loc
2740 = dump_user_location_t::from_location_t (UNKNOWN_LOCATION);
2741
2742 temp_dump_context tmp (true, true,
2743 MSG_ALL_KINDS | MSG_PRIORITY_USER_FACING);
2744 {
2745 AUTO_DUMP_SCOPE ("outer scope", loc);
2746 dump_printf (MSG_NOTE, "msg1\n");
2747 }
2748 }
2749
2750 /* Run all of the selftests within this file. */
2751
2752 void
2753 dumpfile_c_tests ()
2754 {
2755 test_impl_location ();
2756 for_each_line_table_case (test_capture_of_dump_calls);
2757 test_pr87025 ();
2758 }
2759
2760 } // namespace selftest
2761
2762 #endif /* CHECKING_P */