diff mbox series

[06/16] diagnostics: output formats: use references for non-null pointers

Message ID 20240724221824.585054-7-dmalcolm@redhat.com
State New
Headers show
Series Revamp of JSON/SARIF output | expand

Commit Message

David Malcolm July 24, 2024, 10:18 p.m. UTC
No functional change intended.

gcc/ChangeLog:
	* diagnostic-format-json.cc (json_from_expanded_location): Make
	"static". Pass param "context" by reference, as it cannot be null.
	(json_from_location_range): Likewise for param "context".
	(json_from_fixit_hint): Likewise.
	(make_json_for_path): Likewise.
	(json_output_format::on_end_diagnostic): Update for above changes.
	(diagnostic_output_format_init_json::diagnostic_output_format_init_json):
	Pass param "context" by reference, as it cannot be null.
	(diagnostic_output_format_init_json_stderr): Likewise.
	(diagnostic_output_format_init_json_file): Likewise.
	(selftest::test_unknown_location): Update for above changes.
	(selftest::test_bad_endpoints): Likewise.
	* diagnostic-format-sarif.cc (sarif_builder::m_context): Convert
	from pointer to reference.
	(sarif_invocation::add_notification_for_ice): Convert both params
	from pointers to references.
	(sarif_invocation::prepare_to_flush): Likewise for "context".
	(sarif_result::on_nested_diagnostic): Likewise for "context" and
	"builder".
	(sarif_result::on_diagram): Likewise.
	(sarif_ice_notification::sarif_ice_notification): Likewise.
	(sarif_builder::sarif_builder): Likewise for "context".
	(sarif_builder::end_diagnostic): Likewise.
	(sarif_builder::emit_diagram): Likewise.
	(sarif_builder::make_result_object): Likewise.
	(make_reporting_descriptor_object_for_warning): Likewise.
	(sarif_builder::make_locations_arr): Update for change to m_context.
	(sarif_builder::get_sarif_column): Likewise.
	(sarif_builder::make_message_object_for_diagram): Convert "context"
	from pointer to reference.
	(sarif_builder::make_tool_object): Likewise for "m_context".
	(sarif_builder::make_driver_tool_component_object): Likewise.
	(sarif_builder::get_or_create_artifact): Likewise.
	(sarif_builder::maybe_make_artifact_content_object): Likewise.
	(sarif_builder::get_source_lines): Likewise.
	(sarif_output_format::on_end_diagnostic): Update for above changes.
	(sarif_output_format::on_diagram): Likewise.
	(sarif_output_format::sarif_output_format): Likewise.
	(diagnostic_output_format_init_sarif): Convert param "context"
	from pointer to reference.
	(diagnostic_output_format_init_sarif_stderr): Likewise.
	(diagnostic_output_format_init_sarif_file): Likewise.
	(diagnostic_output_format_init_sarif_stream): Likewise.
	* diagnostic.cc (diagnostic_output_format_init): Likewise.
	* diagnostic.h (diagnostic_output_format_init): Likewise.
	(diagnostic_output_format_init_json_stderr): Likewise.
	(diagnostic_output_format_init_json_file): Likewise.
	(diagnostic_output_format_init_sarif_stderr): Likewise.
	(diagnostic_output_format_init_sarif_file): Likewise.
	(diagnostic_output_format_init_sarif_stream): Likewise.
	(json_from_expanded_location): Delete decl.
	* gcc.cc (driver_handle_option): Update for change to
	diagnostic_output_format_init.
	* opts.cc (common_handle_option): Likewise.

Signed-off-by: David Malcolm <dmalcolm@redhat.com>
---
 gcc/diagnostic-format-json.cc  |  56 +++++------
 gcc/diagnostic-format-sarif.cc | 166 ++++++++++++++++-----------------
 gcc/diagnostic.cc              |   2 +-
 gcc/diagnostic.h               |  15 ++-
 gcc/gcc.cc                     |   3 +-
 gcc/opts.cc                    |   3 +-
 6 files changed, 122 insertions(+), 123 deletions(-)
diff mbox series

Patch

diff --git a/gcc/diagnostic-format-json.cc b/gcc/diagnostic-format-json.cc
index 4dc0f264fc70..1bf8da663cc2 100644
--- a/gcc/diagnostic-format-json.cc
+++ b/gcc/diagnostic-format-json.cc
@@ -94,8 +94,8 @@  private:
 
 /* Generate a JSON object for LOC.  */
 
-json::value *
-json_from_expanded_location (diagnostic_context *context, location_t loc)
+static json::value *
+json_from_expanded_location (diagnostic_context &context, location_t loc)
 {
   expanded_location exploc = expand_location (loc);
   json::object *result = new json::object ();
@@ -103,7 +103,7 @@  json_from_expanded_location (diagnostic_context *context, location_t loc)
     result->set_string ("file", exploc.file);
   result->set_integer ("line", exploc.line);
 
-  const enum diagnostics_column_unit orig_unit = context->m_column_unit;
+  const enum diagnostics_column_unit orig_unit = context.m_column_unit;
   struct
   {
     const char *name;
@@ -115,22 +115,22 @@  json_from_expanded_location (diagnostic_context *context, location_t loc)
   int the_column = INT_MIN;
   for (int i = 0; i != ARRAY_SIZE (column_fields); ++i)
     {
-      context->m_column_unit = column_fields[i].unit;
-      const int col = context->converted_column (exploc);
+      context.m_column_unit = column_fields[i].unit;
+      const int col = context.converted_column (exploc);
       result->set_integer (column_fields[i].name, col);
       if (column_fields[i].unit == orig_unit)
 	the_column = col;
     }
   gcc_assert (the_column != INT_MIN);
   result->set_integer ("column", the_column);
-  context->m_column_unit = orig_unit;
+  context.m_column_unit = orig_unit;
   return result;
 }
 
 /* Generate a JSON object for LOC_RANGE.  */
 
 static json::object *
-json_from_location_range (diagnostic_context *context,
+json_from_location_range (diagnostic_context &context,
 			  const location_range *loc_range, unsigned range_idx)
 {
   location_t caret_loc = get_pure_location (loc_range->m_loc);
@@ -163,7 +163,7 @@  json_from_location_range (diagnostic_context *context,
 /* Generate a JSON object for HINT.  */
 
 static json::object *
-json_from_fixit_hint (diagnostic_context *context, const fixit_hint *hint)
+json_from_fixit_hint (diagnostic_context &context, const fixit_hint *hint)
 {
   json::object *fixit_obj = new json::object ();
 
@@ -192,7 +192,7 @@  json_from_metadata (const diagnostic_metadata *metadata)
 /* Make a JSON value for PATH.  */
 
 static json::value *
-make_json_for_path (diagnostic_context *context,
+make_json_for_path (diagnostic_context &context,
 		    const diagnostic_path *path)
 {
   json::array *path_array = new json::array ();
@@ -288,7 +288,7 @@  json_output_format::on_end_diagnostic (const diagnostic_info &diagnostic,
     {
       const location_range *loc_range = richloc->get_range (i);
       json::object *loc_obj
-	= json_from_location_range (&m_context, loc_range, i);
+	= json_from_location_range (m_context, loc_range, i);
       if (loc_obj)
 	loc_array->append (loc_obj);
     }
@@ -300,7 +300,7 @@  json_output_format::on_end_diagnostic (const diagnostic_info &diagnostic,
       for (unsigned int i = 0; i < richloc->get_num_fixit_hints (); i++)
 	{
 	  const fixit_hint *hint = richloc->get_fixit_hint (i);
-	  json::object *fixit_obj = json_from_fixit_hint (&m_context, hint);
+	  json::object *fixit_obj = json_from_fixit_hint (m_context, hint);
 	  fixit_array->append (fixit_obj);
 	}
     }
@@ -319,7 +319,7 @@  json_output_format::on_end_diagnostic (const diagnostic_info &diagnostic,
   const diagnostic_path *path = richloc->get_path ();
   if (path)
     {
-      json::value *path_value = make_json_for_path (&m_context, path);
+      json::value *path_value = make_json_for_path (m_context, path);
       diag_obj->set ("path", path_value);
     }
 
@@ -387,46 +387,46 @@  private:
    to a file).  */
 
 static void
-diagnostic_output_format_init_json (diagnostic_context *context)
+diagnostic_output_format_init_json (diagnostic_context &context)
 {
   /* Suppress normal textual path output.  */
-  context->set_path_format (DPF_NONE);
+  context.set_path_format (DPF_NONE);
 
   /* The metadata is handled in JSON format, rather than as text.  */
-  context->set_show_cwe (false);
-  context->set_show_rules (false);
+  context.set_show_cwe (false);
+  context.set_show_rules (false);
 
   /* The option is handled in JSON format, rather than as text.  */
-  context->set_show_option_requested (false);
+  context.set_show_option_requested (false);
 
   /* Don't colorize the text.  */
-  pp_show_color (context->printer) = false;
-  context->set_show_highlight_colors (false);
+  pp_show_color (context.printer) = false;
+  context.set_show_highlight_colors (false);
 }
 
 /* Populate CONTEXT in preparation for JSON output to stderr.  */
 
 void
-diagnostic_output_format_init_json_stderr (diagnostic_context *context,
+diagnostic_output_format_init_json_stderr (diagnostic_context &context,
 					   bool formatted)
 {
   diagnostic_output_format_init_json (context);
-  context->set_output_format (new json_stderr_output_format (*context,
-							     formatted));
+  context.set_output_format (new json_stderr_output_format (context,
+							    formatted));
 }
 
 /* Populate CONTEXT in preparation for JSON output to a file named
    BASE_FILE_NAME.gcc.json.  */
 
 void
-diagnostic_output_format_init_json_file (diagnostic_context *context,
+diagnostic_output_format_init_json_file (diagnostic_context &context,
 					 bool formatted,
 					 const char *base_file_name)
 {
   diagnostic_output_format_init_json (context);
-  context->set_output_format (new json_file_output_format (*context,
-							   formatted,
-							   base_file_name));
+  context.set_output_format (new json_file_output_format (context,
+							  formatted,
+							  base_file_name));
 }
 
 #if CHECKING_P
@@ -440,7 +440,7 @@  static void
 test_unknown_location ()
 {
   test_diagnostic_context dc;
-  delete json_from_expanded_location (&dc, UNKNOWN_LOCATION);
+  delete json_from_expanded_location (dc, UNKNOWN_LOCATION);
 }
 
 /* Verify that we gracefully handle attempts to serialize bad
@@ -459,7 +459,7 @@  test_bad_endpoints ()
   loc_range.m_label = NULL;
 
   test_diagnostic_context dc;
-  json::object *obj = json_from_location_range (&dc, &loc_range, 0);
+  json::object *obj = json_from_location_range (dc, &loc_range, 0);
   /* We should have a "caret" value, but no "start" or "finish" values.  */
   ASSERT_TRUE (obj != NULL);
   ASSERT_TRUE (obj->get ("caret") != NULL);
diff --git a/gcc/diagnostic-format-sarif.cc b/gcc/diagnostic-format-sarif.cc
index c35d8788d6de..d6de5806f5ac 100644
--- a/gcc/diagnostic-format-sarif.cc
+++ b/gcc/diagnostic-format-sarif.cc
@@ -118,10 +118,10 @@  class sarif_invocation : public sarif_object
 public:
   sarif_invocation ();
 
-  void add_notification_for_ice (diagnostic_context *context,
+  void add_notification_for_ice (diagnostic_context &context,
 				 const diagnostic_info &diagnostic,
-				 sarif_builder *builder);
-  void prepare_to_flush (diagnostic_context *context);
+				 sarif_builder &builder);
+  void prepare_to_flush (diagnostic_context &context);
 
 private:
   json::array *m_notifications_arr;
@@ -182,13 +182,13 @@  public:
   sarif_result () : m_related_locations_arr (NULL) {}
 
   void
-  on_nested_diagnostic (diagnostic_context *context,
+  on_nested_diagnostic (diagnostic_context &context,
 			const diagnostic_info &diagnostic,
 			diagnostic_t orig_diag_kind,
-			sarif_builder *builder);
-  void on_diagram (diagnostic_context *context,
+			sarif_builder &builder);
+  void on_diagram (diagnostic_context &context,
 		   const diagnostic_diagram &diagram,
-		   sarif_builder *builder);
+		   sarif_builder &builder);
 
 private:
   void
@@ -275,9 +275,9 @@  class sarif_replacement : public sarif_object {};
 class sarif_ice_notification : public sarif_object
 {
 public:
-  sarif_ice_notification (diagnostic_context *context,
+  sarif_ice_notification (diagnostic_context &context,
 			  const diagnostic_info &diagnostic,
-			  sarif_builder *builder);
+			  sarif_builder &builder);
 };
 
 /* A class for managing SARIF output (for -fdiagnostics-format=sarif-stderr
@@ -326,14 +326,14 @@  public:
 class sarif_builder
 {
 public:
-  sarif_builder (diagnostic_context *context,
+  sarif_builder (diagnostic_context &context,
 		 const char *main_input_filename_,
 		 bool formatted);
 
-  void end_diagnostic (diagnostic_context *context,
+  void end_diagnostic (diagnostic_context &context,
 		       const diagnostic_info &diagnostic,
 		       diagnostic_t orig_diag_kind);
-  void emit_diagram (diagnostic_context *context,
+  void emit_diagram (diagnostic_context &context,
 		     const diagnostic_diagram &diagram);
   void end_group ();
 
@@ -348,14 +348,14 @@  public:
   sarif_message *
   make_message_object (const char *msg) const;
   sarif_message *
-  make_message_object_for_diagram (diagnostic_context *context,
+  make_message_object_for_diagram (diagnostic_context &context,
 				   const diagnostic_diagram &diagram);
   sarif_artifact_content *
   maybe_make_artifact_content_object (const char *filename) const;
 
 private:
   sarif_result *
-  make_result_object (diagnostic_context *context,
+  make_result_object (diagnostic_context &context,
 		      const diagnostic_info &diagnostic,
 		      diagnostic_t orig_diag_kind);
   void
@@ -406,7 +406,7 @@  private:
   sarif_tool_component_reference *
   make_tool_component_reference_object_for_cwe () const;
   sarif_reporting_descriptor *
-  make_reporting_descriptor_object_for_warning (diagnostic_context *context,
+  make_reporting_descriptor_object_for_warning (diagnostic_context &context,
 						const diagnostic_info &diagnostic,
 						diagnostic_t orig_diag_kind,
 						const char *option_text);
@@ -436,7 +436,7 @@  private:
   make_artifact_content_object (const char *text) const;
   int get_sarif_column (expanded_location exploc) const;
 
-  diagnostic_context *m_context;
+  diagnostic_context &m_context;
 
   /* The JSON object for the invocation object.  */
   sarif_invocation *m_invocation_obj;
@@ -491,9 +491,9 @@  sarif_invocation::sarif_invocation ()
    Add an object representing the ICE to the notifications array.  */
 
 void
-sarif_invocation::add_notification_for_ice (diagnostic_context *context,
+sarif_invocation::add_notification_for_ice (diagnostic_context &context,
 					    const diagnostic_info &diagnostic,
-					    sarif_builder *builder)
+					    sarif_builder &builder)
 {
   m_success = false;
 
@@ -503,7 +503,7 @@  sarif_invocation::add_notification_for_ice (diagnostic_context *context,
 }
 
 void
-sarif_invocation::prepare_to_flush (diagnostic_context *context)
+sarif_invocation::prepare_to_flush (diagnostic_context &context)
 {
   /* "executionSuccessful" property (SARIF v2.1.0 section 3.20.14).  */
   set_bool ("executionSuccessful", m_success);
@@ -513,7 +513,7 @@  sarif_invocation::prepare_to_flush (diagnostic_context *context)
 
   /* Call client hook, allowing it to create a custom property bag for
      this object (SARIF v2.1.0 section 3.8) e.g. for recording time vars.  */
-  if (auto client_data_hooks = context->get_client_data_hooks ())
+  if (auto client_data_hooks = context.get_client_data_hooks ())
     client_data_hooks->add_sarif_invocation_properties (*this);
 }
 
@@ -606,20 +606,20 @@  sarif_artifact::populate_roles ()
    secondary diagnostics occur (such as notes to a warning).  */
 
 void
-sarif_result::on_nested_diagnostic (diagnostic_context *context,
+sarif_result::on_nested_diagnostic (diagnostic_context &context,
 				    const diagnostic_info &diagnostic,
 				    diagnostic_t /*orig_diag_kind*/,
-				    sarif_builder *builder)
+				    sarif_builder &builder)
 {
   /* We don't yet generate meaningful logical locations for notes;
      sometimes these will related to current_function_decl, but
      often they won't.  */
   sarif_location *location_obj
-    = builder->make_location_object (*diagnostic.richloc, NULL,
-				     diagnostic_artifact_role::result_file);
+    = builder.make_location_object (*diagnostic.richloc, NULL,
+				    diagnostic_artifact_role::result_file);
   sarif_message *message_obj
-    = builder->make_message_object (pp_formatted_text (context->printer));
-  pp_clear_output_area (context->printer);
+    = builder.make_message_object (pp_formatted_text (context.printer));
+  pp_clear_output_area (context.printer);
   location_obj->set ("message", message_obj);
 
   add_related_location (location_obj);
@@ -632,13 +632,13 @@  sarif_result::on_nested_diagnostic (diagnostic_context *context,
    (SARIF v2.1.0 section 3.28.5).  */
 
 void
-sarif_result::on_diagram (diagnostic_context *context,
+sarif_result::on_diagram (diagnostic_context &context,
 			  const diagnostic_diagram &diagram,
-			  sarif_builder *builder)
+			  sarif_builder &builder)
 {
   sarif_location *location_obj = new sarif_location ();
   sarif_message *message_obj
-    = builder->make_message_object_for_diagram (context, diagram);
+    = builder.make_message_object_for_diagram (context, diagram);
   location_obj->set ("message", message_obj);
 
   add_related_location (location_obj);
@@ -663,20 +663,20 @@  sarif_result::add_related_location (sarif_location *location_obj)
 /* sarif_ice_notification's ctor.
    DIAGNOSTIC is an internal compiler error.  */
 
-sarif_ice_notification::sarif_ice_notification (diagnostic_context *context,
+sarif_ice_notification::sarif_ice_notification (diagnostic_context &context,
 						const diagnostic_info &diagnostic,
-						sarif_builder *builder)
+						sarif_builder &builder)
 {
   /* "locations" property (SARIF v2.1.0 section 3.58.4).  */
   json::array *locations_arr
-    = builder->make_locations_arr (diagnostic,
-				   diagnostic_artifact_role::result_file);
+    = builder.make_locations_arr (diagnostic,
+				  diagnostic_artifact_role::result_file);
   set ("locations", locations_arr);
 
   /* "message" property (SARIF v2.1.0 section 3.85.5).  */
   sarif_message *message_obj
-    = builder->make_message_object (pp_formatted_text (context->printer));
-  pp_clear_output_area (context->printer);
+    = builder.make_message_object (pp_formatted_text (context.printer));
+  pp_clear_output_area (context.printer);
   set ("message", message_obj);
 
   /* "level" property (SARIF v2.1.0 section 3.58.6).  */
@@ -707,7 +707,7 @@  add_location (sarif_thread_flow_location *thread_flow_loc_obj)
 
 /* sarif_builder's ctor.  */
 
-sarif_builder::sarif_builder (diagnostic_context *context,
+sarif_builder::sarif_builder (diagnostic_context &context,
 			      const char *main_input_filename_,
 			      bool formatted)
 : m_context (context),
@@ -717,7 +717,7 @@  sarif_builder::sarif_builder (diagnostic_context *context,
   m_seen_any_relative_paths (false),
   m_rule_id_set (),
   m_rules_arr (new json::array ()),
-  m_tabstop (context->m_tabstop),
+  m_tabstop (context.m_tabstop),
   m_formatted (formatted)
 {
   /* Mark MAIN_INPUT_FILENAME_ as the artifact that the tool was
@@ -734,13 +734,13 @@  sarif_builder::sarif_builder (diagnostic_context *context,
 /* Implementation of "end_diagnostic" for SARIF output.  */
 
 void
-sarif_builder::end_diagnostic (diagnostic_context *context,
+sarif_builder::end_diagnostic (diagnostic_context &context,
 			       const diagnostic_info &diagnostic,
 			       diagnostic_t orig_diag_kind)
 {
   if (diagnostic.kind == DK_ICE || diagnostic.kind == DK_ICE_NOBT)
     {
-      m_invocation_obj->add_notification_for_ice (context, diagnostic, this);
+      m_invocation_obj->add_notification_for_ice (context, diagnostic, *this);
       return;
     }
 
@@ -749,7 +749,7 @@  sarif_builder::end_diagnostic (diagnostic_context *context,
     m_cur_group_result->on_nested_diagnostic (context,
 					      diagnostic,
 					      orig_diag_kind,
-					      this);
+					      *this);
   else
     {
       /* Top-level diagnostic.  */
@@ -764,12 +764,12 @@  sarif_builder::end_diagnostic (diagnostic_context *context,
    for SARIF output.  */
 
 void
-sarif_builder::emit_diagram (diagnostic_context *context,
+sarif_builder::emit_diagram (diagnostic_context &context,
 			     const diagnostic_diagram &diagram)
 {
   /* We must be within the emission of a top-level diagnostic.  */
   gcc_assert (m_cur_group_result);
-  m_cur_group_result->on_diagram (context, diagram, this);
+  m_cur_group_result->on_diagram (context, diagram, *this);
 }
 
 /* Implementation of "end_group_cb" for SARIF output.  */
@@ -840,7 +840,7 @@  make_rule_id_for_diagnostic_kind (diagnostic_t diag_kind)
 /* Make a "result" object (SARIF v2.1.0 section 3.27) for DIAGNOSTIC.  */
 
 sarif_result *
-sarif_builder::make_result_object (diagnostic_context *context,
+sarif_builder::make_result_object (diagnostic_context &context,
 				   const diagnostic_info &diagnostic,
 				   diagnostic_t orig_diag_kind)
 {
@@ -849,8 +849,8 @@  sarif_builder::make_result_object (diagnostic_context *context,
   /* "ruleId" property (SARIF v2.1.0 section 3.27.5).  */
   /* Ideally we'd have an option_name for these.  */
   if (char *option_text
-	= context->make_option_name (diagnostic.option_index,
-				     orig_diag_kind, diagnostic.kind))
+	= context.make_option_name (diagnostic.option_index,
+				    orig_diag_kind, diagnostic.kind))
     {
       /* Lazily create reportingDescriptor objects for and add to m_rules_arr.
 	 Set ruleId referencing them.  */
@@ -903,8 +903,8 @@  sarif_builder::make_result_object (diagnostic_context *context,
 
   /* "message" property (SARIF v2.1.0 section 3.27.11).  */
   sarif_message *message_obj
-    = make_message_object (pp_formatted_text (context->printer));
-  pp_clear_output_area (context->printer);
+    = make_message_object (pp_formatted_text (context.printer));
+  pp_clear_output_area (context.printer);
   result_obj->set ("message", message_obj);
 
   /* "locations" property (SARIF v2.1.0 section 3.27.12).  */
@@ -944,7 +944,7 @@  sarif_builder::make_result_object (diagnostic_context *context,
 
 sarif_reporting_descriptor *
 sarif_builder::
-make_reporting_descriptor_object_for_warning (diagnostic_context *context,
+make_reporting_descriptor_object_for_warning (diagnostic_context &context,
 					      const diagnostic_info &diagnostic,
 					      diagnostic_t /*orig_diag_kind*/,
 					      const char *option_text)
@@ -959,7 +959,7 @@  make_reporting_descriptor_object_for_warning (diagnostic_context *context,
      it seems redundant compared to "id".  */
 
   /* "helpUri" property (SARIF v2.1.0 section 3.49.12).  */
-  if (char *option_url = context->make_option_url (diagnostic.option_index))
+  if (char *option_url = context.make_option_url (diagnostic.option_index))
     {
       reporting_desc->set_string ("helpUri", option_url);
       free (option_url);
@@ -1050,7 +1050,7 @@  sarif_builder::make_locations_arr (const diagnostic_info &diagnostic,
 {
   json::array *locations_arr = new json::array ();
   const logical_location *logical_loc = NULL;
-  if (auto client_data_hooks = m_context->get_client_data_hooks ())
+  if (auto client_data_hooks = m_context.get_client_data_hooks ())
     logical_loc = client_data_hooks->get_current_logical_location ();
 
   sarif_location *location_obj
@@ -1257,7 +1257,7 @@  int
 sarif_builder::get_sarif_column (expanded_location exploc) const
 {
   cpp_char_column_policy policy (m_tabstop, cpp_wcwidth);
-  return location_compute_display_column (m_context->get_file_cache (),
+  return location_compute_display_column (m_context.get_file_cache (),
 					  exploc, policy);
 }
 
@@ -1594,7 +1594,7 @@  sarif_builder::make_message_object (const char *msg) const
    of the message.  */
 
 sarif_message *
-sarif_builder::make_message_object_for_diagram (diagnostic_context *context,
+sarif_builder::make_message_object_for_diagram (diagnostic_context &context,
 						const diagnostic_diagram &diagram)
 {
   sarif_message *message_obj = new sarif_message ();
@@ -1602,19 +1602,19 @@  sarif_builder::make_message_object_for_diagram (diagnostic_context *context,
   /* "text" property (SARIF v2.1.0 section 3.11.8).  */
   message_obj->set_string ("text", diagram.get_alt_text ());
 
-  char *saved_prefix = pp_take_prefix (context->printer);
-  pp_set_prefix (context->printer, NULL);
+  char *saved_prefix = pp_take_prefix (context.printer);
+  pp_set_prefix (context.printer, NULL);
 
   /* "To produce a code block in Markdown, simply indent every line of
      the block by at least 4 spaces or 1 tab."
      Here we use 4 spaces.  */
-  diagram.get_canvas ().print_to_pp (context->printer, "    ");
-  pp_set_prefix (context->printer, saved_prefix);
+  diagram.get_canvas ().print_to_pp (context.printer, "    ");
+  pp_set_prefix (context.printer, saved_prefix);
 
   /* "markdown" property (SARIF v2.1.0 section 3.11.9).  */
-  message_obj->set_string ("markdown", pp_formatted_text (context->printer));
+  message_obj->set_string ("markdown", pp_formatted_text (context.printer));
 
-  pp_clear_output_area (context->printer);
+  pp_clear_output_area (context.printer);
 
   return message_obj;
 }
@@ -1726,7 +1726,7 @@  sarif_builder::make_tool_object () const
 
   /* Report plugins via the "extensions" property
      (SARIF v2.1.0 section 3.18.3).  */
-  if (auto client_data_hooks = m_context->get_client_data_hooks ())
+  if (auto client_data_hooks = m_context.get_client_data_hooks ())
     if (const client_version_info *vinfo
 	  = client_data_hooks->get_any_version_info ())
       {
@@ -1779,7 +1779,7 @@  sarif_builder::make_driver_tool_component_object () const
 {
   sarif_tool_component *driver_obj = new sarif_tool_component ();
 
-  if (auto client_data_hooks = m_context->get_client_data_hooks ())
+  if (auto client_data_hooks = m_context.get_client_data_hooks ())
     if (const client_version_info *vinfo
 	  = client_data_hooks->get_any_version_info ())
       {
@@ -1908,7 +1908,7 @@  sarif_builder::get_or_create_artifact (const char *filename,
     case diagnostic_artifact_role::result_file:
     case diagnostic_artifact_role::traced_file:
       /* Assume that these are in the source language.  */
-      if (auto client_data_hooks = m_context->get_client_data_hooks ())
+      if (auto client_data_hooks = m_context.get_client_data_hooks ())
 	if (const char *source_lang
 	    = client_data_hooks->maybe_get_sarif_source_language (filename))
 	  artifact_obj->set_string ("sourceLanguage", source_lang);
@@ -1930,7 +1930,7 @@  sarif_builder::maybe_make_artifact_content_object (const char *filename) const
 {
   /* Let input.cc handle any charset conversion.  */
   char_span utf8_content
-    = m_context->get_file_cache ().get_source_file_content (filename);
+    = m_context.get_file_cache ().get_source_file_content (filename);
   if (!utf8_content)
     return NULL;
 
@@ -1958,7 +1958,7 @@  sarif_builder::get_source_lines (const char *filename,
   for (int line = start_line; line <= end_line; line++)
     {
       char_span line_content
-	= m_context->get_file_cache ().get_source_line (filename, line);
+	= m_context.get_file_cache ().get_source_line (filename, line);
       if (!line_content.get_buffer ())
 	return NULL;
       result.reserve (line_content.length () + 1);
@@ -2110,11 +2110,11 @@  public:
   on_end_diagnostic (const diagnostic_info &diagnostic,
 		     diagnostic_t orig_diag_kind) final override
   {
-    m_builder.end_diagnostic (&m_context, diagnostic, orig_diag_kind);
+    m_builder.end_diagnostic (m_context, diagnostic, orig_diag_kind);
   }
   void on_diagram (const diagnostic_diagram &diagram) final override
   {
-    m_builder.emit_diagram (&m_context, diagram);
+    m_builder.emit_diagram (m_context, diagram);
   }
 
 protected:
@@ -2122,7 +2122,7 @@  protected:
 		       const char *main_input_filename_,
 		       bool formatted)
   : diagnostic_output_format (context),
-    m_builder (&context, main_input_filename_, formatted)
+    m_builder (context, main_input_filename_, formatted)
   {}
 
   sarif_builder m_builder;
@@ -2193,36 +2193,36 @@  private:
    to a file).  */
 
 static void
-diagnostic_output_format_init_sarif (diagnostic_context *context)
+diagnostic_output_format_init_sarif (diagnostic_context &context)
 {
   /* Suppress normal textual path output.  */
-  context->set_path_format (DPF_NONE);
+  context.set_path_format (DPF_NONE);
 
   /* Override callbacks.  */
-  context->set_ice_handler_callback (sarif_ice_handler);
+  context.set_ice_handler_callback (sarif_ice_handler);
 
   /* The metadata is handled in SARIF format, rather than as text.  */
-  context->set_show_cwe (false);
-  context->set_show_rules (false);
+  context.set_show_cwe (false);
+  context.set_show_rules (false);
 
   /* The option is handled in SARIF format, rather than as text.  */
-  context->set_show_option_requested (false);
+  context.set_show_option_requested (false);
 
   /* Don't colorize the text.  */
-  pp_show_color (context->printer) = false;
-  context->set_show_highlight_colors (false);
+  pp_show_color (context.printer) = false;
+  context.set_show_highlight_colors (false);
 }
 
 /* Populate CONTEXT in preparation for SARIF output to stderr.  */
 
 void
-diagnostic_output_format_init_sarif_stderr (diagnostic_context *context,
+diagnostic_output_format_init_sarif_stderr (diagnostic_context &context,
 					    const char *main_input_filename_,
 					    bool formatted)
 {
   diagnostic_output_format_init_sarif (context);
-  context->set_output_format
-    (new sarif_stream_output_format (*context,
+  context.set_output_format
+    (new sarif_stream_output_format (context,
 				     main_input_filename_,
 				     formatted,
 				     stderr));
@@ -2232,14 +2232,14 @@  diagnostic_output_format_init_sarif_stderr (diagnostic_context *context,
    BASE_FILE_NAME.sarif.  */
 
 void
-diagnostic_output_format_init_sarif_file (diagnostic_context *context,
+diagnostic_output_format_init_sarif_file (diagnostic_context &context,
 					  const char *main_input_filename_,
 					  bool formatted,
 					  const char *base_file_name)
 {
   diagnostic_output_format_init_sarif (context);
-  context->set_output_format
-    (new sarif_file_output_format (*context,
+  context.set_output_format
+    (new sarif_file_output_format (context,
 				   main_input_filename_,
 				   formatted,
 				   base_file_name));
@@ -2248,14 +2248,14 @@  diagnostic_output_format_init_sarif_file (diagnostic_context *context,
 /* Populate CONTEXT in preparation for SARIF output to STREAM.  */
 
 void
-diagnostic_output_format_init_sarif_stream (diagnostic_context *context,
+diagnostic_output_format_init_sarif_stream (diagnostic_context &context,
 					    const char *main_input_filename_,
 					    bool formatted,
 					    FILE *stream)
 {
   diagnostic_output_format_init_sarif (context);
-  context->set_output_format
-    (new sarif_stream_output_format (*context,
+  context.set_output_format
+    (new sarif_stream_output_format (context,
 				     main_input_filename_,
 				     formatted,
 				     stream));
diff --git a/gcc/diagnostic.cc b/gcc/diagnostic.cc
index 196f34d9a2f4..aa8afd521fa2 100644
--- a/gcc/diagnostic.cc
+++ b/gcc/diagnostic.cc
@@ -1800,7 +1800,7 @@  diagnostic_text_output_format::on_diagram (const diagnostic_diagram &diagram)
    file-based output formats.  */
 
 void
-diagnostic_output_format_init (diagnostic_context *context,
+diagnostic_output_format_init (diagnostic_context &context,
 			       const char *main_input_filename_,
 			       const char *base_file_name,
 			       enum diagnostics_output_format format,
diff --git a/gcc/diagnostic.h b/gcc/diagnostic.h
index cb58bc63c2c3..84044b90dfcd 100644
--- a/gcc/diagnostic.h
+++ b/gcc/diagnostic.h
@@ -1091,24 +1091,24 @@  extern char *file_name_as_prefix (diagnostic_context *, const char *);
 
 extern char *build_message_string (const char *, ...) ATTRIBUTE_PRINTF_1;
 
-extern void diagnostic_output_format_init (diagnostic_context *,
+extern void diagnostic_output_format_init (diagnostic_context &,
 					   const char *main_input_filename_,
 					   const char *base_file_name,
 					   enum diagnostics_output_format,
 					   bool json_formatting);
-extern void diagnostic_output_format_init_json_stderr (diagnostic_context *context,
+extern void diagnostic_output_format_init_json_stderr (diagnostic_context &context,
 						       bool formatted);
-extern void diagnostic_output_format_init_json_file (diagnostic_context *context,
+extern void diagnostic_output_format_init_json_file (diagnostic_context &context,
 						     bool formatted,
 						     const char *base_file_name);
-extern void diagnostic_output_format_init_sarif_stderr (diagnostic_context *context,
+extern void diagnostic_output_format_init_sarif_stderr (diagnostic_context &context,
 							const char *main_input_filename_,
 							bool formatted);
-extern void diagnostic_output_format_init_sarif_file (diagnostic_context *context,
+extern void diagnostic_output_format_init_sarif_file (diagnostic_context &context,
 						      const char *main_input_filename_,
 						      bool formatted,
 						      const char *base_file_name);
-extern void diagnostic_output_format_init_sarif_stream (diagnostic_context *context,
+extern void diagnostic_output_format_init_sarif_stream (diagnostic_context &context,
 							const char *main_input_filename_,
 							bool formatted,
 							FILE *stream);
@@ -1116,9 +1116,6 @@  extern void diagnostic_output_format_init_sarif_stream (diagnostic_context *cont
 /* Compute the number of digits in the decimal representation of an integer.  */
 extern int num_digits (int);
 
-extern json::value *json_from_expanded_location (diagnostic_context *context,
-						 location_t loc);
-
 inline bool
 warning_enabled_at (location_t loc, int opt)
 {
diff --git a/gcc/gcc.cc b/gcc/gcc.cc
index 00f93d00f969..abdb40bfe6e0 100644
--- a/gcc/gcc.cc
+++ b/gcc/gcc.cc
@@ -4358,7 +4358,8 @@  driver_handle_option (struct gcc_options *opts,
 	{
 	  const char *basename = (opts->x_dump_base_name ? opts->x_dump_base_name
 				  : opts->x_main_input_basename);
-	  diagnostic_output_format_init (dc,
+	  gcc_assert (dc);
+	  diagnostic_output_format_init (*dc,
 					 opts->x_main_input_filename, basename,
 					 (enum diagnostics_output_format)value,
 					 opts->x_flag_diagnostics_json_formatting);
diff --git a/gcc/opts.cc b/gcc/opts.cc
index be90a632338f..0b7b137c3763 100644
--- a/gcc/opts.cc
+++ b/gcc/opts.cc
@@ -2963,7 +2963,8 @@  common_handle_option (struct gcc_options *opts,
 	{
 	  const char *basename = (opts->x_dump_base_name ? opts->x_dump_base_name
 				  : opts->x_main_input_basename);
-	  diagnostic_output_format_init (dc,
+	  gcc_assert (dc);
+	  diagnostic_output_format_init (*dc,
 					 opts->x_main_input_filename, basename,
 					 (enum diagnostics_output_format)value,
 					 opts->x_flag_diagnostics_json_formatting);