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