Debian Codegraph @

gcc/dumpfile.cc

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