]> git.ipfire.org Git - thirdparty/gcc.git/blobdiff - gcc/dumpfile.c
c++: Handle multiple aggregate overloads [PR95319].
[thirdparty/gcc.git] / gcc / dumpfile.c
index a1ab20531db9cc1774679980679d8d03f15b0f25..5d61946fc49a902cde3aff9ad035f99849ed96c7 100644 (file)
@@ -1,5 +1,5 @@
 /* Dump infrastructure for optimizations and intermediate representation.
-   Copyright (C) 2012-2018 Free Software Foundation, Inc.
+   Copyright (C) 2012-2020 Free Software Foundation, Inc.
 
 This file is part of GCC.
 
@@ -39,6 +39,7 @@ along with GCC; see the file COPYING3.  If not see
 #include "tree-pass.h" /* for "current_pass".  */
 #include "optinfo-emit-json.h"
 #include "stringpool.h" /* for get_identifier.  */
+#include "spellcheck.h"
 
 /* If non-NULL, return one past-the-end of the matching SUBPART of
    the WHOLE string.  */
@@ -47,7 +48,7 @@ along with GCC; see the file COPYING3.  If not see
 
 static dump_flags_t pflags;                  /* current dump_flags */
 
-static void dump_loc (dump_flags_t, FILE *, source_location);
+static void dump_loc (dump_flags_t, FILE *, location_t);
 
 /* Current -fopt-info output stream, if any, and flags.  */
 static FILE *alt_dump_file = NULL;
@@ -115,6 +116,7 @@ static struct dump_file_info dump_files[TDI_end] =
    in dumpfile.h and opt_info_options below. */
 static const kv_pair<dump_flags_t> dump_options[] =
 {
+  {"none", TDF_NONE},
   {"address", TDF_ADDRESS},
   {"asmname", TDF_ASMNAME},
   {"slim", TDF_SLIM},
@@ -466,7 +468,7 @@ kind_as_string (dump_flags_t dump_kind)
 /* Print source location on DFILE if enabled.  */
 
 static void
-dump_loc (dump_flags_t dump_kind, FILE *dfile, source_location loc)
+dump_loc (dump_flags_t dump_kind, FILE *dfile, location_t loc)
 {
   if (dump_kind)
     {
@@ -487,7 +489,7 @@ dump_loc (dump_flags_t dump_kind, FILE *dfile, source_location loc)
 /* Print source location to PP if enabled.  */
 
 static void
-dump_loc (dump_flags_t dump_kind, pretty_printer *pp, source_location loc)
+dump_loc (dump_flags_t dump_kind, pretty_printer *pp, location_t loc)
 {
   if (dump_kind)
     {
@@ -515,6 +517,28 @@ dump_context::~dump_context ()
   delete m_pending;
 }
 
+void
+dump_context::set_json_writer (optrecord_json_writer *writer)
+{
+  delete m_json_writer;
+  m_json_writer = writer;
+}
+
+/* Perform cleanup activity for -fsave-optimization-record.
+   Currently, the file is written out here in one go, before cleaning
+   up.  */
+
+void
+dump_context::finish_any_json_writer ()
+{
+  if (!m_json_writer)
+    return;
+
+  m_json_writer->write ();
+  delete m_json_writer;
+  m_json_writer = NULL;
+}
+
 /* Update the "dumps_are_enabled" global; to be called whenever dump_file
    or alt_dump_file change, or when changing dump_context in selftests.  */
 
@@ -558,24 +582,22 @@ dump_context::apply_dump_filter_p (dump_flags_t dump_kind,
    If optinfos are enabled, begin a new optinfo.  */
 
 void
-dump_context::dump_loc (dump_flags_t dump_kind, const dump_location_t &loc)
+dump_context::dump_loc (const dump_metadata_t &metadata,
+                       const dump_user_location_t &loc)
 {
   end_any_optinfo ();
 
-  dump_loc_immediate (dump_kind, loc);
+  dump_loc_immediate (metadata.get_dump_flags (), loc);
 
   if (optinfo_enabled_p ())
-    {
-      optinfo &info = begin_next_optinfo (loc);
-      info.handle_dump_file_kind (dump_kind);
-    }
+    begin_next_optinfo (metadata, loc);
 }
 
 /* As dump_loc above, but without starting a new optinfo. */
 
 void
 dump_context::dump_loc_immediate (dump_flags_t dump_kind,
-                                 const dump_location_t &loc)
+                                 const dump_user_location_t &loc)
 {
   location_t srcloc = loc.get_location_t ();
 
@@ -610,18 +632,17 @@ make_item_for_dump_gimple_stmt (gimple *stmt, int spc, dump_flags_t dump_flags)
    EXTRA_DUMP_FLAGS on the dump streams if DUMP_KIND is enabled.  */
 
 void
-dump_context::dump_gimple_stmt (dump_flags_t dump_kind,
+dump_context::dump_gimple_stmt (const dump_metadata_t &metadata,
                                dump_flags_t extra_dump_flags,
                                gimple *gs, int spc)
 {
   optinfo_item *item
     = make_item_for_dump_gimple_stmt (gs, spc, dump_flags | extra_dump_flags);
-  emit_item (item, dump_kind);
+  emit_item (item, metadata.get_dump_flags ());
 
   if (optinfo_enabled_p ())
     {
-      optinfo &info = ensure_pending_optinfo ();
-      info.handle_dump_file_kind (dump_kind);
+      optinfo &info = ensure_pending_optinfo (metadata);
       info.add_item (item);
     }
   else
@@ -631,13 +652,13 @@ dump_context::dump_gimple_stmt (dump_flags_t dump_kind,
 /* Similar to dump_gimple_stmt, except additionally print source location.  */
 
 void
-dump_context::dump_gimple_stmt_loc (dump_flags_t dump_kind,
-                                   const dump_location_t &loc,
+dump_context::dump_gimple_stmt_loc (const dump_metadata_t &metadata,
+                                   const dump_user_location_t &loc,
                                    dump_flags_t extra_dump_flags,
                                    gimple *gs, int spc)
 {
-  dump_loc (dump_kind, loc);
-  dump_gimple_stmt (dump_kind, extra_dump_flags, gs, spc);
+  dump_loc (metadata, loc);
+  dump_gimple_stmt (metadata, extra_dump_flags, gs, spc);
 }
 
 /* Make an item for the given dump call, equivalent to print_gimple_expr.  */
@@ -661,18 +682,17 @@ make_item_for_dump_gimple_expr (gimple *stmt, int spc, dump_flags_t dump_flags)
    Do not terminate with a newline or semicolon.  */
 
 void
-dump_context::dump_gimple_expr (dump_flags_t dump_kind,
+dump_context::dump_gimple_expr (const dump_metadata_t &metadata,
                                dump_flags_t extra_dump_flags,
                                gimple *gs, int spc)
 {
   optinfo_item *item
     = make_item_for_dump_gimple_expr (gs, spc, dump_flags | extra_dump_flags);
-  emit_item (item, dump_kind);
+  emit_item (item, metadata.get_dump_flags ());
 
   if (optinfo_enabled_p ())
     {
-      optinfo &info = ensure_pending_optinfo ();
-      info.handle_dump_file_kind (dump_kind);
+      optinfo &info = ensure_pending_optinfo (metadata);
       info.add_item (item);
     }
   else
@@ -682,14 +702,14 @@ dump_context::dump_gimple_expr (dump_flags_t dump_kind,
 /* Similar to dump_gimple_expr, except additionally print source location.  */
 
 void
-dump_context::dump_gimple_expr_loc (dump_flags_t dump_kind,
-                                   const dump_location_t &loc,
+dump_context::dump_gimple_expr_loc (const dump_metadata_t &metadata,
+                                   const dump_user_location_t &loc,
                                    dump_flags_t extra_dump_flags,
                                    gimple *gs,
                                    int spc)
 {
-  dump_loc (dump_kind, loc);
-  dump_gimple_expr (dump_kind, extra_dump_flags, gs, spc);
+  dump_loc (metadata, loc);
+  dump_gimple_expr (metadata, extra_dump_flags, gs, spc);
 }
 
 /* Make an item for the given dump call, equivalent to print_generic_expr.  */
@@ -716,18 +736,17 @@ make_item_for_dump_generic_expr (tree node, dump_flags_t dump_flags)
    DUMP_KIND is enabled.  */
 
 void
-dump_context::dump_generic_expr (dump_flags_t dump_kind,
+dump_context::dump_generic_expr (const dump_metadata_t &metadata,
                                 dump_flags_t extra_dump_flags,
                                 tree t)
 {
   optinfo_item *item
     = make_item_for_dump_generic_expr (t, dump_flags | extra_dump_flags);
-  emit_item (item, dump_kind);
+  emit_item (item, metadata.get_dump_flags ());
 
   if (optinfo_enabled_p ())
     {
-      optinfo &info = ensure_pending_optinfo ();
-      info.handle_dump_file_kind (dump_kind);
+      optinfo &info = ensure_pending_optinfo (metadata);
       info.add_item (item);
     }
   else
@@ -739,13 +758,13 @@ dump_context::dump_generic_expr (dump_flags_t dump_kind,
    location.  */
 
 void
-dump_context::dump_generic_expr_loc (dump_flags_t dump_kind,
-                                    const dump_location_t &loc,
+dump_context::dump_generic_expr_loc (const dump_metadata_t &metadata,
+                                    const dump_user_location_t &loc,
                                     dump_flags_t extra_dump_flags,
                                     tree t)
 {
-  dump_loc (dump_kind, loc);
-  dump_generic_expr (dump_kind, extra_dump_flags, t);
+  dump_loc (metadata, loc);
+  dump_generic_expr (metadata, extra_dump_flags, t);
 }
 
 /* Make an item for the given dump call.  */
@@ -965,10 +984,10 @@ dump_pretty_printer::decode_format (text_info *text, const char *spec,
 /* Output a formatted message using FORMAT on appropriate dump streams.  */
 
 void
-dump_context::dump_printf_va (dump_flags_t dump_kind, const char *format,
+dump_context::dump_printf_va (const dump_metadata_t &metadata, const char *format,
                              va_list *ap)
 {
-  dump_pretty_printer pp (this, dump_kind);
+  dump_pretty_printer pp (this, metadata.get_dump_flags ());
 
   text_info text;
   text.err_no = errno;
@@ -981,8 +1000,7 @@ dump_context::dump_printf_va (dump_flags_t dump_kind, const char *format,
   /* Phase 3.  */
   if (optinfo_enabled_p ())
     {
-      optinfo &info = ensure_pending_optinfo ();
-      info.handle_dump_file_kind (dump_kind);
+      optinfo &info = ensure_pending_optinfo (metadata);
       pp.emit_items (&info);
     }
   else
@@ -993,12 +1011,12 @@ dump_context::dump_printf_va (dump_flags_t dump_kind, const char *format,
    dump location captured.  */
 
 void
-dump_context::dump_printf_loc_va (dump_flags_t dump_kind,
-                                 const dump_location_t &loc,
+dump_context::dump_printf_loc_va (const dump_metadata_t &metadata,
+                                 const dump_user_location_t &loc,
                                  const char *format, va_list *ap)
 {
-  dump_loc (dump_kind, loc);
-  dump_printf_va (dump_kind, format, ap);
+  dump_loc (metadata, loc);
+  dump_printf_va (metadata, format, ap);
 }
 
 /* Make an item for the given dump call, equivalent to print_dec.  */
@@ -1034,15 +1052,14 @@ make_item_for_dump_dec (const poly_int<N, C> &value)
 
 template<unsigned int N, typename C>
 void
-dump_context::dump_dec (dump_flags_t dump_kind, const poly_int<N, C> &value)
+dump_context::dump_dec (const dump_metadata_t &metadata, const poly_int<N, C> &value)
 {
   optinfo_item *item = make_item_for_dump_dec (value);
-  emit_item (item, dump_kind);
+  emit_item (item, metadata.get_dump_flags ());
 
   if (optinfo_enabled_p ())
     {
-      optinfo &info = ensure_pending_optinfo ();
-      info.handle_dump_file_kind (dump_kind);
+      optinfo &info = ensure_pending_optinfo (metadata);
       info.add_item (item);
     }
   else
@@ -1052,15 +1069,14 @@ dump_context::dump_dec (dump_flags_t dump_kind, const poly_int<N, C> &value)
 /* Output the name of NODE on appropriate dump streams.  */
 
 void
-dump_context::dump_symtab_node (dump_flags_t dump_kind, symtab_node *node)
+dump_context::dump_symtab_node (const dump_metadata_t &metadata, symtab_node *node)
 {
   optinfo_item *item = make_item_for_dump_symtab_node (node);
-  emit_item (item, dump_kind);
+  emit_item (item, metadata.get_dump_flags ());
 
   if (optinfo_enabled_p ())
     {
-      optinfo &info = ensure_pending_optinfo ();
-      info.handle_dump_file_kind (dump_kind);
+      optinfo &info = ensure_pending_optinfo (metadata);
       info.add_item (item);
     }
   else
@@ -1083,19 +1099,23 @@ dump_context::get_scope_depth () const
    Emit a "scope" optinfo if optinfos are enabled.  */
 
 void
-dump_context::begin_scope (const char *name, const dump_location_t &loc)
+dump_context::begin_scope (const char *name,
+                          const dump_user_location_t &user_location,
+                          const dump_impl_location_t &impl_location)
 {
   m_scope_depth++;
 
+  location_t src_loc = user_location.get_location_t ();
+
   if (dump_file && apply_dump_filter_p (MSG_NOTE, pflags))
-    ::dump_loc (MSG_NOTE, dump_file, loc.get_location_t ());
+    ::dump_loc (MSG_NOTE, dump_file, src_loc);
 
   if (alt_dump_file && apply_dump_filter_p (MSG_NOTE, alt_flags))
-    ::dump_loc (MSG_NOTE, alt_dump_file, loc.get_location_t ());
+    ::dump_loc (MSG_NOTE, alt_dump_file, src_loc);
 
   /* Support for temp_dump_context in selftests.  */
   if (m_test_pp && apply_dump_filter_p (MSG_NOTE, m_test_pp_flags))
-    ::dump_loc (MSG_NOTE, m_test_pp, loc.get_location_t ());
+    ::dump_loc (MSG_NOTE, m_test_pp, src_loc);
 
   pretty_printer pp;
   pp_printf (&pp, "=== %s ===\n", name);
@@ -1106,9 +1126,12 @@ dump_context::begin_scope (const char *name, const dump_location_t &loc)
 
   if (optinfo_enabled_p ())
     {
-      optinfo &info = begin_next_optinfo (loc);
+      optinfo &info
+       = begin_next_optinfo (dump_metadata_t (MSG_NOTE, impl_location),
+                             user_location);
       info.m_kind = OPTINFO_KIND_SCOPE;
       info.add_item (item);
+      end_any_optinfo ();
     }
   else
     delete item;
@@ -1121,17 +1144,29 @@ dump_context::end_scope ()
 {
   end_any_optinfo ();
   m_scope_depth--;
-  optimization_records_maybe_pop_dump_scope ();
+
+  if (m_json_writer)
+    m_json_writer->pop_scope ();
+}
+
+/* Should optinfo instances be created?
+   All creation of optinfos should be guarded by this predicate.
+   Return true if any optinfo destinations are active.  */
+
+bool
+dump_context::optinfo_enabled_p () const
+{
+  return (optimization_records_enabled_p ());
 }
 
 /* Return the optinfo currently being accumulated, creating one if
    necessary.  */
 
 optinfo &
-dump_context::ensure_pending_optinfo ()
+dump_context::ensure_pending_optinfo (const dump_metadata_t &metadata)
 {
   if (!m_pending)
-    return begin_next_optinfo (dump_location_t (dump_user_location_t ()));
+    return begin_next_optinfo (metadata, dump_user_location_t ());
   return *m_pending;
 }
 
@@ -1139,11 +1174,14 @@ dump_context::ensure_pending_optinfo ()
    accumulated.  */
 
 optinfo &
-dump_context::begin_next_optinfo (const dump_location_t &loc)
+dump_context::begin_next_optinfo (const dump_metadata_t &metadata,
+                                 const dump_user_location_t &user_loc)
 {
   end_any_optinfo ();
   gcc_assert (m_pending == NULL);
+  dump_location_t loc (user_loc, metadata.get_impl_location ());
   m_pending = new optinfo (loc, OPTINFO_KIND_NOTE, current_pass);
+  m_pending->handle_dump_file_kind (metadata.get_dump_flags ());
   return *m_pending;
 }
 
@@ -1154,11 +1192,23 @@ void
 dump_context::end_any_optinfo ()
 {
   if (m_pending)
-    m_pending->emit ();
+    emit_optinfo (m_pending);
   delete m_pending;
   m_pending = NULL;
 }
 
+/* Emit the optinfo to all of the "non-immediate" destinations
+   (emission to "immediate" destinations is done by
+   dump_context::emit_item).  */
+
+void
+dump_context::emit_optinfo (const optinfo *info)
+{
+  /* -fsave-optimization-record.  */
+  if (m_json_writer)
+    m_json_writer->add_record (info);
+}
+
 /* Emit ITEM to all item destinations (those that don't require
    consolidation into optinfo instances).  */
 
@@ -1201,21 +1251,22 @@ dump_context dump_context::s_default;
    EXTRA_DUMP_FLAGS on the dump streams if DUMP_KIND is enabled.  */
 
 void
-dump_gimple_stmt (dump_flags_t dump_kind, dump_flags_t extra_dump_flags,
+dump_gimple_stmt (const dump_metadata_t &metadata, dump_flags_t extra_dump_flags,
                  gimple *gs, int spc)
 {
   VERIFY_DUMP_ENABLED_P;
-  dump_context::get ().dump_gimple_stmt (dump_kind, extra_dump_flags, gs, spc);
+  dump_context::get ().dump_gimple_stmt (metadata, extra_dump_flags, gs, spc);
 }
 
 /* Similar to dump_gimple_stmt, except additionally print source location.  */
 
 void
-dump_gimple_stmt_loc (dump_flags_t dump_kind, const dump_location_t &loc,
+dump_gimple_stmt_loc (const dump_metadata_t &metadata,
+                     const dump_user_location_t &loc,
                      dump_flags_t extra_dump_flags, gimple *gs, int spc)
 {
   VERIFY_DUMP_ENABLED_P;
-  dump_context::get ().dump_gimple_stmt_loc (dump_kind, loc, extra_dump_flags,
+  dump_context::get ().dump_gimple_stmt_loc (metadata, loc, extra_dump_flags,
                                             gs, spc);
 }
 
@@ -1224,21 +1275,23 @@ dump_gimple_stmt_loc (dump_flags_t dump_kind, const dump_location_t &loc,
    Do not terminate with a newline or semicolon.  */
 
 void
-dump_gimple_expr (dump_flags_t dump_kind, dump_flags_t extra_dump_flags,
+dump_gimple_expr (const dump_metadata_t &metadata,
+                 dump_flags_t extra_dump_flags,
                  gimple *gs, int spc)
 {
   VERIFY_DUMP_ENABLED_P;
-  dump_context::get ().dump_gimple_expr (dump_kind, extra_dump_flags, gs, spc);
+  dump_context::get ().dump_gimple_expr (metadata, extra_dump_flags, gs, spc);
 }
 
 /* Similar to dump_gimple_expr, except additionally print source location.  */
 
 void
-dump_gimple_expr_loc (dump_flags_t dump_kind, const dump_location_t &loc,
+dump_gimple_expr_loc (const dump_metadata_t &metadata,
+                     const dump_user_location_t &loc,
                      dump_flags_t extra_dump_flags, gimple *gs, int spc)
 {
   VERIFY_DUMP_ENABLED_P;
-  dump_context::get ().dump_gimple_expr_loc (dump_kind, loc, extra_dump_flags,
+  dump_context::get ().dump_gimple_expr_loc (metadata, loc, extra_dump_flags,
                                             gs, spc);
 }
 
@@ -1246,34 +1299,35 @@ dump_gimple_expr_loc (dump_flags_t dump_kind, const dump_location_t &loc,
    DUMP_KIND is enabled.  */
 
 void
-dump_generic_expr (dump_flags_t dump_kind, dump_flags_t extra_dump_flags,
+dump_generic_expr (const dump_metadata_t &metadata, dump_flags_t extra_dump_flags,
                   tree t)
 {
   VERIFY_DUMP_ENABLED_P;
-  dump_context::get ().dump_generic_expr (dump_kind, extra_dump_flags, t);
+  dump_context::get ().dump_generic_expr (metadata, extra_dump_flags, t);
 }
 
 /* Similar to dump_generic_expr, except additionally print the source
    location.  */
 
 void
-dump_generic_expr_loc (dump_flags_t dump_kind, const dump_location_t &loc,
+dump_generic_expr_loc (const dump_metadata_t &metadata,
+                      const dump_user_location_t &loc,
                       dump_flags_t extra_dump_flags, tree t)
 {
   VERIFY_DUMP_ENABLED_P;
-  dump_context::get ().dump_generic_expr_loc (dump_kind, loc, extra_dump_flags,
+  dump_context::get ().dump_generic_expr_loc (metadata, loc, extra_dump_flags,
                                              t);
 }
 
 /* Output a formatted message using FORMAT on appropriate dump streams.  */
 
 void
-dump_printf (dump_flags_t dump_kind, const char *format, ...)
+dump_printf (const dump_metadata_t &metadata, const char *format, ...)
 {
   VERIFY_DUMP_ENABLED_P;
   va_list ap;
   va_start (ap, format);
-  dump_context::get ().dump_printf_va (dump_kind, format, &ap);
+  dump_context::get ().dump_printf_va (metadata, format, &ap);
   va_end (ap);
 }
 
@@ -1281,13 +1335,14 @@ dump_printf (dump_flags_t dump_kind, const char *format, ...)
    dump location captured.  */
 
 void
-dump_printf_loc (dump_flags_t dump_kind, const dump_location_t &loc,
+dump_printf_loc (const dump_metadata_t &metadata,
+                const dump_user_location_t &loc,
                 const char *format, ...)
 {
   VERIFY_DUMP_ENABLED_P;
   va_list ap;
   va_start (ap, format);
-  dump_context::get ().dump_printf_loc_va (dump_kind, loc, format, &ap);
+  dump_context::get ().dump_printf_loc_va (metadata, loc, format, &ap);
   va_end (ap);
 }
 
@@ -1295,17 +1350,17 @@ dump_printf_loc (dump_flags_t dump_kind, const dump_location_t &loc,
 
 template<unsigned int N, typename C>
 void
-dump_dec (dump_flags_t dump_kind, const poly_int<N, C> &value)
+dump_dec (const dump_metadata_t &metadata, const poly_int<N, C> &value)
 {
   VERIFY_DUMP_ENABLED_P;
-  dump_context::get ().dump_dec (dump_kind, value);
+  dump_context::get ().dump_dec (metadata, value);
 }
 
-template void dump_dec (dump_flags_t, const poly_uint16 &);
-template void dump_dec (dump_flags_t, const poly_int64 &);
-template void dump_dec (dump_flags_t, const poly_uint64 &);
-template void dump_dec (dump_flags_t, const poly_offset_int &);
-template void dump_dec (dump_flags_t, const poly_widest_int &);
+template void dump_dec (const dump_metadata_t &metadata, const poly_uint16 &);
+template void dump_dec (const dump_metadata_t &metadata, const poly_int64 &);
+template void dump_dec (const dump_metadata_t &metadata, const poly_uint64 &);
+template void dump_dec (const dump_metadata_t &metadata, const poly_offset_int &);
+template void dump_dec (const dump_metadata_t &metadata, const poly_widest_int &);
 
 void
 dump_dec (dump_flags_t dump_kind, const poly_wide_int &value, signop sgn)
@@ -1347,10 +1402,10 @@ dumpfile_ensure_any_optinfo_are_flushed ()
 /* Output the name of NODE on appropriate dump streams.  */
 
 void
-dump_symtab_node (dump_flags_t dump_kind, symtab_node *node)
+dump_symtab_node (const dump_metadata_t &metadata, symtab_node *node)
 {
   VERIFY_DUMP_ENABLED_P;
-  dump_context::get ().dump_symtab_node (dump_kind, node);
+  dump_context::get ().dump_symtab_node (metadata, node);
 }
 
 /* Get the current dump scope-nesting depth.
@@ -1369,9 +1424,11 @@ get_dump_scope_depth ()
    Increment the scope depth.  */
 
 void
-dump_begin_scope (const char *name, const dump_location_t &loc)
+dump_begin_scope (const char *name,
+                 const dump_user_location_t &user_location,
+                 const dump_impl_location_t &impl_location)
 {
-  dump_context::get ().begin_scope (name, loc);
+  dump_context::get ().begin_scope (name, user_location, impl_location);
 }
 
 /* Pop a nested dump scope.  */
@@ -1476,21 +1533,17 @@ FILE *
 gcc::dump_manager::
 dump_begin (int phase, dump_flags_t *flag_ptr, int part)
 {
-  char *name;
-  struct dump_file_info *dfi;
-  FILE *stream;
-
   if (phase == TDI_none || !dump_phase_enabled_p (phase))
     return NULL;
 
-  name = get_dump_file_name (phase, part);
+  char *name = get_dump_file_name (phase, part);
   if (!name)
     return NULL;
-  dfi = get_dump_file_info (phase);
+  struct dump_file_info *dfi = get_dump_file_info (phase);
 
   /* We do not support re-opening of dump files with parts.  This would require
      tracking pstate per part of the dump file.  */
-  stream = dump_open (name, part != -1 || dfi->pstate < 0);
+  FILE *stream = dump_open (name, part != -1 || dfi->pstate < 0);
   if (stream)
     dfi->pstate = 1;
   free (name);
@@ -1715,28 +1768,19 @@ gcc::dump_manager::update_dfi_for_opt_info (dump_file_info *dfi) const
   return true;
 }
 
-/* Parse ARG as a dump switch. Return nonzero if it is, and store the
-   relevant details in the dump_files array.  */
+/* Helper routine to parse -<dump format>[=filename]
+   and return the corresponding dump flag.  If POS_P is non-NULL,
+   assign start of filename into *POS_P.  */
 
-int
-gcc::dump_manager::
-dump_switch_p_1 (const char *arg, struct dump_file_info *dfi, bool doglob)
+dump_flags_t
+parse_dump_option (const char *option_value, const char **pos_p)
 {
-  const char *option_value;
   const char *ptr;
   dump_flags_t flags;
 
-  if (doglob && !dfi->glob)
-    return 0;
-
-  option_value = skip_leading_substring (arg, doglob ? dfi->glob : dfi->swtch);
-  if (!option_value)
-    return 0;
-
-  if (*option_value && *option_value != '-' && *option_value != '=')
-    return 0;
-
   ptr = option_value;
+  if (pos_p)
+    *pos_p = NULL;
 
   /* Retain "user-facing" and "internals" messages, but filter out
      those from an opt_problem being re-emitted at the top level
@@ -1750,14 +1794,13 @@ dump_switch_p_1 (const char *arg, struct dump_file_info *dfi, bool doglob)
       const char *end_ptr;
       const char *eq_ptr;
       unsigned length;
-
       while (*ptr == '-')
        ptr++;
       end_ptr = strchr (ptr, '-');
       eq_ptr = strchr (ptr, '=');
 
       if (eq_ptr && !end_ptr)
-        end_ptr = eq_ptr;
+       end_ptr = eq_ptr;
 
       if (!end_ptr)
        end_ptr = ptr + strlen (ptr);
@@ -1766,25 +1809,59 @@ dump_switch_p_1 (const char *arg, struct dump_file_info *dfi, bool doglob)
       for (option_ptr = dump_options; option_ptr->name; option_ptr++)
        if (strlen (option_ptr->name) == length
            && !memcmp (option_ptr->name, ptr, length))
-          {
-            flags |= option_ptr->value;
+         {
+           flags |= option_ptr->value;
            goto found;
-          }
+         }
 
       if (*ptr == '=')
-        {
+       {
           /* Interpret rest of the argument as a dump filename.  This
              filename overrides other command line filenames.  */
-          if (dfi->pfilename)
-            free (CONST_CAST (char *, dfi->pfilename));
-          dfi->pfilename = xstrdup (ptr + 1);
-          break;
-        }
+         if (pos_p)
+           *pos_p = ptr + 1;
+         break;
+       }
       else
-        warning (0, "ignoring unknown option %q.*s in %<-fdump-%s%>",
-                 length, ptr, dfi->swtch);
-    found:;
+      {
+       warning (0, "ignoring unknown option %q.*s",
+                length, ptr);
+       flags = TDF_ERROR;
+      }
+    found:
       ptr = end_ptr;
+  }
+
+  return flags;
+}
+
+/* Parse ARG as a dump switch.  Return nonzero if it is, and store the
+   relevant details in the dump_files array.  */
+
+int
+gcc::dump_manager::
+dump_switch_p_1 (const char *arg, struct dump_file_info *dfi, bool doglob)
+{
+  const char *option_value;
+  dump_flags_t flags = TDF_NONE;
+
+  if (doglob && !dfi->glob)
+    return 0;
+
+  option_value = skip_leading_substring (arg, doglob ? dfi->glob : dfi->swtch);
+  if (!option_value)
+    return 0;
+
+  if (*option_value && *option_value != '-' && *option_value != '=')
+    return 0;
+
+  const char *filename;
+  flags = parse_dump_option (option_value, &filename);
+  if (filename)
+    {
+      if (dfi->pfilename)
+  free (CONST_CAST (char *, dfi->pfilename));
+      dfi->pfilename = xstrdup (filename);
     }
 
   dfi->pstate = -1;
@@ -1798,7 +1875,7 @@ dump_switch_p_1 (const char *arg, struct dump_file_info *dfi, bool doglob)
   return 1;
 }
 
-int
+void
 gcc::dump_manager::
 dump_switch_p (const char *arg)
 {
@@ -1820,8 +1897,20 @@ dump_switch_p (const char *arg)
     for (i = 0; i < m_extra_dump_files_in_use; i++)
       any |= dump_switch_p_1 (arg, &m_extra_dump_files[i], true);
 
-
-  return any;
+  if (!any)
+    {
+      auto_vec<const char *> candidates;
+      for (size_t i = TDI_none + 1; i != TDI_end; i++)
+       candidates.safe_push (dump_files[i].swtch);
+      for (size_t i = 0; i < m_extra_dump_files_in_use; i++)
+       candidates.safe_push (m_extra_dump_files[i].swtch);
+      const char *hint = find_closest_string (arg, &candidates);
+      if (hint)
+       error ("unrecognized command-line option %<-fdump-%s%>; "
+              "did you mean %<-fdump-%s%>?", arg, hint);
+      else
+       error ("unrecognized command-line option %<-fdump-%s%>", arg);
+    }
 }
 
 /* Parse ARG as a -fopt-info switch and store flags, optgroup_flags
@@ -1860,10 +1949,9 @@ opt_info_switch_p_1 (const char *arg, dump_flags_t *flags,
       end_ptr = strchr (ptr, '-');
       eq_ptr = strchr (ptr, '=');
 
-      if (eq_ptr && !end_ptr)
+      if (eq_ptr && (!end_ptr || eq_ptr < end_ptr))
         end_ptr = eq_ptr;
-
-      if (!end_ptr)
+      else if (!end_ptr)
        end_ptr = ptr + strlen (ptr);
       length = end_ptr - ptr;
 
@@ -1990,6 +2078,34 @@ enable_rtl_dump_file (void)
   return num_enabled > 0;
 }
 
+/* debug_dump_context's ctor.  Temporarily override the dump_context
+   (to forcibly enable output to stderr).  */
+
+debug_dump_context::debug_dump_context ()
+: m_context (),
+  m_saved (&dump_context::get ()),
+  m_saved_flags (dump_flags),
+  m_saved_pflags (pflags),
+  m_saved_file (dump_file)
+{
+  set_dump_file (stderr);
+  dump_context::s_current = &m_context;
+  pflags = dump_flags = MSG_ALL_KINDS | MSG_ALL_PRIORITIES;
+  dump_context::get ().refresh_dumps_are_enabled ();
+}
+
+/* debug_dump_context's dtor.  Restore the saved dump_context.  */
+
+debug_dump_context::~debug_dump_context ()
+{
+  set_dump_file (m_saved_file);
+  dump_context::s_current = m_saved;
+  dump_flags = m_saved_flags;
+  pflags = m_saved_pflags;
+  dump_context::get ().refresh_dumps_are_enabled ();
+}
+
+
 #if CHECKING_P
 
 namespace selftest {
@@ -2001,10 +2117,11 @@ temp_dump_context::temp_dump_context (bool forcibly_enable_optinfo,
                                      bool forcibly_enable_dumping,
                                      dump_flags_t test_pp_flags)
 : m_context (),
-  m_saved (&dump_context ().get ())
+  m_saved (&dump_context::get ())
 {
   dump_context::s_current = &m_context;
-  m_context.m_forcibly_enable_optinfo = forcibly_enable_optinfo;
+  if (forcibly_enable_optinfo)
+    m_context.set_json_writer (new optrecord_json_writer ());
   /* Conditionally enable the test dump, so that we can verify both the
      dump_enabled_p and the !dump_enabled_p cases in selftests.  */
   if (forcibly_enable_dumping)
@@ -2020,6 +2137,8 @@ temp_dump_context::temp_dump_context (bool forcibly_enable_optinfo,
 
 temp_dump_context::~temp_dump_context ()
 {
+  m_context.set_json_writer (NULL);
+
   dump_context::s_current = m_saved;
 
   dump_context::get ().refresh_dumps_are_enabled ();
@@ -2033,6 +2152,36 @@ temp_dump_context::get_dumped_text ()
   return pp_formatted_text (&m_pp);
 }
 
+/* Verify that IMPL_LOC is within EXPECTED_FILE at EXPECTED_LINE,
+   from EXPECTED_FUNCTION, using LOC for the location of any failure,
+   provided that the build compiler is sufficiently recent.  */
+
+static void
+assert_impl_location_eq (const location &loc ATTRIBUTE_UNUSED,
+                        const dump_impl_location_t &impl_loc ATTRIBUTE_UNUSED,
+                        const char *expected_file ATTRIBUTE_UNUSED,
+                        int expected_line ATTRIBUTE_UNUSED,
+                        const char *expected_function ATTRIBUTE_UNUSED)
+{
+#if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8)
+  ASSERT_STR_CONTAINS_AT (loc, impl_loc.m_file, expected_file);
+  ASSERT_EQ_AT (loc, impl_loc.m_line, expected_line);
+  ASSERT_STR_CONTAINS_AT (loc, impl_loc.m_function, expected_function);
+#endif
+}
+
+/* Verify that IMPL_LOC is within EXPECTED_FILE at EXPECTED_LINE,
+   from EXPECTED_FUNCTION, provided that the build compiler is
+   sufficiently recent.  */
+
+#define ASSERT_IMPL_LOCATION_EQ(IMPL_LOC, EXPECTED_FILE, EXPECTED_LINE, \
+                               EXPECTED_FUNCTION)                      \
+  SELFTEST_BEGIN_STMT                                                  \
+    assert_impl_location_eq (SELFTEST_LOCATION, IMPL_LOC,              \
+                            EXPECTED_FILE, EXPECTED_LINE,              \
+                            EXPECTED_FUNCTION);                        \
+  SELFTEST_END_STMT
+
 /* Verify that the dump_location_t constructors capture the source location
    at which they were called (provided that the build compiler is sufficiently
    recent).  */
@@ -2040,31 +2189,29 @@ temp_dump_context::get_dumped_text ()
 static void
 test_impl_location ()
 {
-#if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8)
   /* Default ctor.  */
   {
     dump_location_t loc;
     const int expected_line = __LINE__ - 1;
-    ASSERT_STR_CONTAINS (loc.get_impl_location ().m_file, "dumpfile.c");
-    ASSERT_EQ (loc.get_impl_location ().m_line, expected_line);
+    ASSERT_IMPL_LOCATION_EQ (loc.get_impl_location (),
+                            "dumpfile.c", expected_line, "test_impl_location");
   }
 
   /* Constructing from a gimple.  */
   {
     dump_location_t loc ((gimple *)NULL);
     const int expected_line = __LINE__ - 1;
-    ASSERT_STR_CONTAINS (loc.get_impl_location ().m_file, "dumpfile.c");
-    ASSERT_EQ (loc.get_impl_location ().m_line, expected_line);
+    ASSERT_IMPL_LOCATION_EQ (loc.get_impl_location (),
+                            "dumpfile.c", expected_line, "test_impl_location");
   }
 
   /* Constructing from an rtx_insn.  */
   {
     dump_location_t loc ((rtx_insn *)NULL);
     const int expected_line = __LINE__ - 1;
-    ASSERT_STR_CONTAINS (loc.get_impl_location ().m_file, "dumpfile.c");
-    ASSERT_EQ (loc.get_impl_location ().m_line, expected_line);
+    ASSERT_IMPL_LOCATION_EQ (loc.get_impl_location (),
+                            "dumpfile.c", expected_line, "test_impl_location");
   }
-#endif
 }
 
 /* Verify that the text dumped so far in CONTEXT equals
@@ -2111,7 +2258,7 @@ test_capture_of_dump_calls (const line_table_case &case_)
   if (stmt_loc > LINE_MAP_MAX_LOCATION_WITH_COLS)
     return;
 
-  dump_location_t loc = dump_location_t::from_location_t (stmt_loc);
+  dump_user_location_t loc = dump_user_location_t::from_location_t (stmt_loc);
 
   gimple *stmt = gimple_build_return (NULL);
   gimple_set_location (stmt, stmt_loc);
@@ -2138,6 +2285,7 @@ test_capture_of_dump_calls (const line_table_case &case_)
        temp_dump_context tmp (with_optinfo, true,
                               MSG_ALL_KINDS | MSG_PRIORITY_USER_FACING);
        dump_printf (MSG_NOTE, "int: %i str: %s", 42, "foo");
+       const int expected_impl_line = __LINE__ - 1;
 
        ASSERT_DUMPED_TEXT_EQ (tmp, "int: 42 str: foo");
        if (with_optinfo)
@@ -2147,6 +2295,9 @@ test_capture_of_dump_calls (const line_table_case &case_)
            ASSERT_EQ (info->get_kind (), OPTINFO_KIND_NOTE);
            ASSERT_EQ (info->num_items (), 1);
            ASSERT_IS_TEXT (info->get_item (0), "int: 42 str: foo");
+           ASSERT_IMPL_LOCATION_EQ (info->get_impl_location (),
+                                    "dumpfile.c", expected_impl_line,
+                                    "test_capture_of_dump_calls");
          }
       }
 
@@ -2155,6 +2306,7 @@ test_capture_of_dump_calls (const line_table_case &case_)
        temp_dump_context tmp (with_optinfo, true,
                               MSG_ALL_KINDS | MSG_PRIORITY_USER_FACING);
        dump_printf (MSG_NOTE, "tree: %T", integer_zero_node);
+       const int expected_impl_line = __LINE__ - 1;
 
        ASSERT_DUMPED_TEXT_EQ (tmp, "tree: 0");
        if (with_optinfo)
@@ -2165,6 +2317,9 @@ test_capture_of_dump_calls (const line_table_case &case_)
            ASSERT_EQ (info->num_items (), 2);
            ASSERT_IS_TEXT (info->get_item (0), "tree: ");
            ASSERT_IS_TREE (info->get_item (1), UNKNOWN_LOCATION, "0");
+           ASSERT_IMPL_LOCATION_EQ (info->get_impl_location (),
+                                    "dumpfile.c", expected_impl_line,
+                                    "test_capture_of_dump_calls");
          }
       }
 
@@ -2173,6 +2328,7 @@ test_capture_of_dump_calls (const line_table_case &case_)
        temp_dump_context tmp (with_optinfo, true,
                               MSG_ALL_KINDS | MSG_PRIORITY_USER_FACING);
        dump_printf (MSG_NOTE, "gimple: %E", stmt);
+       const int expected_impl_line = __LINE__ - 1;
 
        ASSERT_DUMPED_TEXT_EQ (tmp, "gimple: return;");
        if (with_optinfo)
@@ -2183,6 +2339,9 @@ test_capture_of_dump_calls (const line_table_case &case_)
            ASSERT_EQ (info->num_items (), 2);
            ASSERT_IS_TEXT (info->get_item (0), "gimple: ");
            ASSERT_IS_GIMPLE (info->get_item (1), stmt_loc, "return;");
+           ASSERT_IMPL_LOCATION_EQ (info->get_impl_location (),
+                                    "dumpfile.c", expected_impl_line,
+                                    "test_capture_of_dump_calls");
          }
       }
 
@@ -2191,6 +2350,7 @@ test_capture_of_dump_calls (const line_table_case &case_)
        temp_dump_context tmp (with_optinfo, true,
                               MSG_ALL_KINDS | MSG_PRIORITY_USER_FACING);
        dump_printf (MSG_NOTE, "gimple: %G", stmt);
+       const int expected_impl_line = __LINE__ - 1;
 
        ASSERT_DUMPED_TEXT_EQ (tmp, "gimple: return;\n");
        if (with_optinfo)
@@ -2201,6 +2361,9 @@ test_capture_of_dump_calls (const line_table_case &case_)
            ASSERT_EQ (info->num_items (), 2);
            ASSERT_IS_TEXT (info->get_item (0), "gimple: ");
            ASSERT_IS_GIMPLE (info->get_item (1), stmt_loc, "return;\n");
+           ASSERT_IMPL_LOCATION_EQ (info->get_impl_location (),
+                                    "dumpfile.c", expected_impl_line,
+                                    "test_capture_of_dump_calls");
          }
       }
 
@@ -2209,6 +2372,7 @@ test_capture_of_dump_calls (const line_table_case &case_)
        temp_dump_context tmp (with_optinfo, true,
                               MSG_ALL_KINDS | MSG_PRIORITY_USER_FACING);
        dump_printf (MSG_NOTE, "node: %C", node);
+       const int expected_impl_line = __LINE__ - 1;
 
        ASSERT_DUMPED_TEXT_EQ (tmp, "node: test_decl/0");
        if (with_optinfo)
@@ -2219,6 +2383,9 @@ test_capture_of_dump_calls (const line_table_case &case_)
            ASSERT_EQ (info->num_items (), 2);
            ASSERT_IS_TEXT (info->get_item (0), "node: ");
            ASSERT_IS_SYMTAB_NODE (info->get_item (1), decl_loc, "test_decl/0");
+           ASSERT_IMPL_LOCATION_EQ (info->get_impl_location (),
+                                    "dumpfile.c", expected_impl_line,
+                                    "test_capture_of_dump_calls");
          }
       }
 
@@ -2252,6 +2419,9 @@ test_capture_of_dump_calls (const line_table_case &case_)
            ASSERT_IS_GIMPLE (info->get_item (5), stmt_loc, "return;");
            ASSERT_IS_GIMPLE (info->get_item (6), stmt_loc, "return;");
            ASSERT_IS_TEXT (info->get_item (7), " after\n");
+           /* We don't ASSERT_IMPL_LOCATION_EQ here, to avoid having to
+              enforce at which exact line the multiline dump_printf_loc
+              occurred.  */
          }
       }
 
@@ -2260,6 +2430,7 @@ test_capture_of_dump_calls (const line_table_case &case_)
        temp_dump_context tmp (with_optinfo, true,
                               MSG_ALL_KINDS | MSG_PRIORITY_USER_FACING);
        dump_printf_loc (MSG_NOTE, loc, "test of tree: ");
+       const int expected_impl_line = __LINE__ - 1;
        dump_generic_expr (MSG_NOTE, TDF_SLIM, integer_zero_node);
 
        ASSERT_DUMPED_TEXT_EQ (tmp, "test.txt:5:10: note: test of tree: 0");
@@ -2272,6 +2443,9 @@ test_capture_of_dump_calls (const line_table_case &case_)
            ASSERT_EQ (info->num_items (), 2);
            ASSERT_IS_TEXT (info->get_item (0), "test of tree: ");
            ASSERT_IS_TREE (info->get_item (1), UNKNOWN_LOCATION, "0");
+           ASSERT_IMPL_LOCATION_EQ (info->get_impl_location (),
+                                    "dumpfile.c", expected_impl_line,
+                                    "test_capture_of_dump_calls");
          }
       }
 
@@ -2280,6 +2454,7 @@ test_capture_of_dump_calls (const line_table_case &case_)
        temp_dump_context tmp (with_optinfo, true,
                               MSG_ALL_KINDS | MSG_PRIORITY_USER_FACING);
        dump_generic_expr_loc (MSG_NOTE, loc, TDF_SLIM, integer_one_node);
+       const int expected_impl_line = __LINE__ - 1;
 
        ASSERT_DUMPED_TEXT_EQ (tmp, "test.txt:5:10: note: 1");
        if (with_optinfo)
@@ -2290,6 +2465,9 @@ test_capture_of_dump_calls (const line_table_case &case_)
            ASSERT_EQ (info->get_kind (), OPTINFO_KIND_NOTE);
            ASSERT_EQ (info->num_items (), 1);
            ASSERT_IS_TREE (info->get_item (0), UNKNOWN_LOCATION, "1");
+           ASSERT_IMPL_LOCATION_EQ (info->get_impl_location (),
+                                    "dumpfile.c", expected_impl_line,
+                                    "test_capture_of_dump_calls");
          }
       }
 
@@ -2300,6 +2478,7 @@ test_capture_of_dump_calls (const line_table_case &case_)
          temp_dump_context tmp (with_optinfo, true,
                                 MSG_ALL_KINDS | MSG_PRIORITY_USER_FACING);
          dump_gimple_stmt_loc (MSG_NOTE, loc, TDF_SLIM, stmt, 2);
+         const int expected_impl_line = __LINE__ - 1;
 
          ASSERT_DUMPED_TEXT_EQ (tmp, "test.txt:5:10: note: return;\n");
          if (with_optinfo)
@@ -2308,6 +2487,9 @@ test_capture_of_dump_calls (const line_table_case &case_)
              ASSERT_TRUE (info != NULL);
              ASSERT_EQ (info->num_items (), 1);
              ASSERT_IS_GIMPLE (info->get_item (0), stmt_loc, "return;\n");
+             ASSERT_IMPL_LOCATION_EQ (info->get_impl_location (),
+                                      "dumpfile.c", expected_impl_line,
+                                      "test_capture_of_dump_calls");
            }
        }
 
@@ -2316,6 +2498,7 @@ test_capture_of_dump_calls (const line_table_case &case_)
          temp_dump_context tmp (with_optinfo, true,
                                 MSG_ALL_KINDS | MSG_PRIORITY_USER_FACING);
          dump_gimple_stmt (MSG_NOTE, TDF_SLIM, stmt, 2);
+         const int expected_impl_line = __LINE__ - 1;
 
          ASSERT_DUMPED_TEXT_EQ (tmp, "return;\n");
          if (with_optinfo)
@@ -2324,6 +2507,9 @@ test_capture_of_dump_calls (const line_table_case &case_)
              ASSERT_TRUE (info != NULL);
              ASSERT_EQ (info->num_items (), 1);
              ASSERT_IS_GIMPLE (info->get_item (0), stmt_loc, "return;\n");
+             ASSERT_IMPL_LOCATION_EQ (info->get_impl_location (),
+                                      "dumpfile.c", expected_impl_line,
+                                      "test_capture_of_dump_calls");
            }
        }
 
@@ -2332,6 +2518,7 @@ test_capture_of_dump_calls (const line_table_case &case_)
          temp_dump_context tmp (with_optinfo, true,
                                 MSG_ALL_KINDS | MSG_PRIORITY_USER_FACING);
          dump_gimple_expr_loc (MSG_NOTE, loc, TDF_SLIM, stmt, 2);
+         const int expected_impl_line = __LINE__ - 1;
 
          ASSERT_DUMPED_TEXT_EQ (tmp, "test.txt:5:10: note: return;");
          if (with_optinfo)
@@ -2340,6 +2527,9 @@ test_capture_of_dump_calls (const line_table_case &case_)
              ASSERT_TRUE (info != NULL);
              ASSERT_EQ (info->num_items (), 1);
              ASSERT_IS_GIMPLE (info->get_item (0), stmt_loc, "return;");
+             ASSERT_IMPL_LOCATION_EQ (info->get_impl_location (),
+                                      "dumpfile.c", expected_impl_line,
+                                      "test_capture_of_dump_calls");
            }
        }
 
@@ -2348,6 +2538,7 @@ test_capture_of_dump_calls (const line_table_case &case_)
          temp_dump_context tmp (with_optinfo, true,
                                 MSG_ALL_KINDS | MSG_PRIORITY_USER_FACING);
          dump_gimple_expr (MSG_NOTE, TDF_SLIM, stmt, 2);
+         const int expected_impl_line = __LINE__ - 1;
 
          ASSERT_DUMPED_TEXT_EQ (tmp, "return;");
          if (with_optinfo)
@@ -2356,6 +2547,9 @@ test_capture_of_dump_calls (const line_table_case &case_)
              ASSERT_TRUE (info != NULL);
              ASSERT_EQ (info->num_items (), 1);
              ASSERT_IS_GIMPLE (info->get_item (0), stmt_loc, "return;");
+             ASSERT_IMPL_LOCATION_EQ (info->get_impl_location (),
+                                      "dumpfile.c", expected_impl_line,
+                                      "test_capture_of_dump_calls");
            }
        }
       }
@@ -2365,6 +2559,7 @@ test_capture_of_dump_calls (const line_table_case &case_)
        temp_dump_context tmp (with_optinfo, true,
                               MSG_ALL_KINDS | MSG_PRIORITY_USER_FACING);
        dump_symtab_node (MSG_NOTE, node);
+       const int expected_impl_line = __LINE__ - 1;
 
        ASSERT_DUMPED_TEXT_EQ (tmp, "test_decl/0");
        if (with_optinfo)
@@ -2374,6 +2569,9 @@ test_capture_of_dump_calls (const line_table_case &case_)
            ASSERT_EQ (info->get_kind (), OPTINFO_KIND_NOTE);
            ASSERT_EQ (info->num_items (), 1);
            ASSERT_IS_SYMTAB_NODE (info->get_item (0), decl_loc, "test_decl/0");
+           ASSERT_IMPL_LOCATION_EQ (info->get_impl_location (),
+                                    "dumpfile.c", expected_impl_line,
+                                    "test_capture_of_dump_calls");
          }
       }
 
@@ -2382,6 +2580,7 @@ test_capture_of_dump_calls (const line_table_case &case_)
        temp_dump_context tmp (with_optinfo, true,
                               MSG_ALL_KINDS | MSG_PRIORITY_USER_FACING);
        dump_dec (MSG_NOTE, poly_int64 (42));
+       const int expected_impl_line = __LINE__ - 1;
 
        ASSERT_DUMPED_TEXT_EQ (tmp, "42");
        if (with_optinfo)
@@ -2390,6 +2589,9 @@ test_capture_of_dump_calls (const line_table_case &case_)
            ASSERT_TRUE (info != NULL);
            ASSERT_EQ (info->num_items (), 1);
            ASSERT_IS_TEXT (info->get_item (0), "42");
+           ASSERT_IMPL_LOCATION_EQ (info->get_impl_location (),
+                                    "dumpfile.c", expected_impl_line,
+                                    "test_capture_of_dump_calls");
          }
       }
 
@@ -2426,6 +2628,7 @@ test_capture_of_dump_calls (const line_table_case &case_)
            dump_printf_loc (MSG_NOTE, stmt, "msg 6\n");
          }
          dump_printf_loc (MSG_NOTE, stmt, "msg 7\n");
+         const int expected_impl_line = __LINE__ - 1;
 
          switch (dump_filter & MSG_ALL_PRIORITIES)
            {
@@ -2477,6 +2680,9 @@ test_capture_of_dump_calls (const line_table_case &case_)
              ASSERT_TRUE (info != NULL);
              ASSERT_EQ (info->num_items (), 1);
              ASSERT_IS_TEXT (info->get_item (0), "msg 7\n");
+             ASSERT_IMPL_LOCATION_EQ (info->get_impl_location (),
+                                      "dumpfile.c", expected_impl_line,
+                                      "test_capture_of_dump_calls");
            }
        }
     }
@@ -2526,6 +2732,20 @@ test_capture_of_dump_calls (const line_table_case &case_)
   }
 }
 
+static void
+test_pr87025 ()
+{
+  dump_user_location_t loc
+    = dump_user_location_t::from_location_t (UNKNOWN_LOCATION);
+
+  temp_dump_context tmp (true, true,
+                        MSG_ALL_KINDS | MSG_PRIORITY_USER_FACING);
+  {
+    AUTO_DUMP_SCOPE ("outer scope", loc);
+    dump_printf (MSG_NOTE, "msg1\n");
+  }
+}
+
 /* Run all of the selftests within this file.  */
 
 void
@@ -2533,6 +2753,7 @@ dumpfile_c_tests ()
 {
   test_impl_location ();
   for_each_line_table_case (test_capture_of_dump_calls);
+  test_pr87025 ();
 }
 
 } // namespace selftest