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