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