public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [pushed] json: reduce use of naked new in json-building code
@ 2023-11-14 16:32 David Malcolm
  0 siblings, 0 replies; only message in thread
From: David Malcolm @ 2023-11-14 16:32 UTC (permalink / raw)
  To: gcc-patches; +Cc: David Malcolm

No functional change intended.

Successfully bootstrapped & regrtested on x86_64-pc-linux-gnu.
Pushed to trunk as r14-5451-g070944fdd6394c

gcc/ChangeLog:
	* diagnostic-format-json.cc: Use type-specific "set_*" functions
	of json::object to avoid naked new of json value subclasses.
	* diagnostic-format-sarif.cc: Likewise.
	* gcov.cc: Likewise.
	* json.cc (object::set_string): New.
	(object::set_integer): New.
	(object::set_float): New.
	(object::set_bool): New.
	(selftest::test_writing_objects): Use object::set_string.
	* json.h (object::set_string): New decl.
	(object::set_integer): New decl.
	(object::set_float): New decl.
	(object::set_bool): New decl.
	* optinfo-emit-json.cc: Use type-specific "set_*" functions of
	json::object to avoid naked new of json value subclasses.
	* timevar.cc: Likewise.
	* tree-diagnostic-path.cc: Likewise.
---
 gcc/diagnostic-format-json.cc  |  27 ++++-----
 gcc/diagnostic-format-sarif.cc | 106 ++++++++++++++++-----------------
 gcc/gcov.cc                    |  65 ++++++++------------
 gcc/json.cc                    |  40 ++++++++++++-
 gcc/json.h                     |   7 +++
 gcc/optinfo-emit-json.cc       |  45 +++++++-------
 gcc/timevar.cc                 |  20 +++----
 gcc/tree-diagnostic-path.cc    |   7 +--
 8 files changed, 167 insertions(+), 150 deletions(-)

diff --git a/gcc/diagnostic-format-json.cc b/gcc/diagnostic-format-json.cc
index 8321c488d280..7c5a126786e6 100644
--- a/gcc/diagnostic-format-json.cc
+++ b/gcc/diagnostic-format-json.cc
@@ -94,8 +94,8 @@ json_from_expanded_location (diagnostic_context *context, location_t loc)
   expanded_location exploc = expand_location (loc);
   json::object *result = new json::object ();
   if (exploc.file)
-    result->set ("file", new json::string (exploc.file));
-  result->set ("line", new json::integer_number (exploc.line));
+    result->set_string ("file", exploc.file);
+  result->set_integer ("line", exploc.line);
 
   const enum diagnostics_column_unit orig_unit = context->m_column_unit;
   struct
@@ -111,12 +111,12 @@ json_from_expanded_location (diagnostic_context *context, location_t loc)
     {
       context->m_column_unit = column_fields[i].unit;
       const int col = context->converted_column (exploc);
-      result->set (column_fields[i].name, new json::integer_number (col));
+      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 ("column", new json::integer_number (the_column));
+  result->set_integer ("column", the_column);
   context->m_column_unit = orig_unit;
   return result;
 }
@@ -148,7 +148,7 @@ json_from_location_range (diagnostic_context *context,
     {
       label_text text (loc_range->m_label->get_text (range_idx));
       if (text.get ())
-	result->set ("label", new json::string (text.get ()));
+	result->set_string ("label", text.get ());
     }
 
   return result;
@@ -165,7 +165,7 @@ json_from_fixit_hint (diagnostic_context *context, const fixit_hint *hint)
   fixit_obj->set ("start", json_from_expanded_location (context, start_loc));
   location_t next_loc = hint->get_next_loc ();
   fixit_obj->set ("next", json_from_expanded_location (context, next_loc));
-  fixit_obj->set ("string", new json::string (hint->get_string ()));
+  fixit_obj->set_string ("string", hint->get_string ());
 
   return fixit_obj;
 }
@@ -178,8 +178,7 @@ json_from_metadata (const diagnostic_metadata *metadata)
   json::object *metadata_obj = new json::object ();
 
   if (metadata->get_cwe ())
-    metadata_obj->set ("cwe",
-		       new json::integer_number (metadata->get_cwe ()));
+    metadata_obj->set_integer ("cwe", metadata->get_cwe ());
 
   return metadata_obj;
 }
@@ -210,13 +209,12 @@ json_output_format::on_end_diagnostic (diagnostic_info *diagnostic,
     gcc_assert (kind_text[len - 1] == ' ');
     char *rstrip = xstrdup (kind_text);
     rstrip[len - 2] = '\0';
-    diag_obj->set ("kind", new json::string (rstrip));
+    diag_obj->set_string ("kind", rstrip);
     free (rstrip);
   }
 
   // FIXME: encoding of the message (json::string requires UTF-8)
-  diag_obj->set ("message",
-		 new json::string (pp_formatted_text (m_context.printer)));
+  diag_obj->set_string ("message", pp_formatted_text (m_context.printer));
   pp_clear_output_area (m_context.printer);
 
   char *option_text;
@@ -224,7 +222,7 @@ json_output_format::on_end_diagnostic (diagnostic_info *diagnostic,
 					 orig_diag_kind, diagnostic->kind);
   if (option_text)
     {
-      diag_obj->set ("option", new json::string (option_text));
+      diag_obj->set_string ("option", option_text);
       free (option_text);
     }
 
@@ -234,7 +232,7 @@ json_output_format::on_end_diagnostic (diagnostic_info *diagnostic,
 						     diagnostic->option_index);
       if (option_url)
 	{
-	  diag_obj->set ("option_url", new json::string (option_url));
+	  diag_obj->set_string ("option_url", option_url);
 	  free (option_url);
 	}
     }
@@ -254,8 +252,7 @@ json_output_format::on_end_diagnostic (diagnostic_info *diagnostic,
       m_cur_group = diag_obj;
       m_cur_children_array = new json::array ();
       diag_obj->set ("children", m_cur_children_array);
-      diag_obj->set ("column-origin",
-		     new json::integer_number (m_context.m_column_origin));
+      diag_obj->set_integer ("column-origin", m_context.m_column_origin);
     }
 
   const rich_location *richloc = diagnostic->richloc;
diff --git a/gcc/diagnostic-format-sarif.cc b/gcc/diagnostic-format-sarif.cc
index 25f623927240..fa3ca2e9ae9a 100644
--- a/gcc/diagnostic-format-sarif.cc
+++ b/gcc/diagnostic-format-sarif.cc
@@ -289,7 +289,7 @@ void
 sarif_invocation::prepare_to_flush (diagnostic_context *context)
 {
   /* "executionSuccessful" property (SARIF v2.1.0 section 3.20.14).  */
-  set ("executionSuccessful", new json::literal (m_success));
+  set_bool ("executionSuccessful", m_success);
 
   /* "toolExecutionNotifications" property (SARIF v2.1.0 section 3.20.21).  */
   set ("toolExecutionNotifications", m_notifications_arr);
@@ -380,7 +380,7 @@ sarif_ice_notification::sarif_ice_notification (diagnostic_context *context,
   set ("message", message_obj);
 
   /* "level" property (SARIF v2.1.0 section 3.58.6).  */
-  set ("level", new json::string ("error"));
+  set_string ("level", "error");
 }
 
 /* class sarif_thread_flow : public sarif_object.  */
@@ -389,7 +389,7 @@ sarif_thread_flow::sarif_thread_flow (const diagnostic_thread &thread)
 {
   /* "id" property (SARIF v2.1.0 section 3.37.2).  */
   label_text name (thread.get_name (false));
-  set ("id", new json::string (name.get ()));
+  set_string ("id", name.get ());
 
   /* "locations" property (SARIF v2.1.0 section 3.37.6).  */
   m_locations_arr = new json::array ();
@@ -541,7 +541,7 @@ sarif_builder::make_result_object (diagnostic_context *context,
     {
       /* Lazily create reportingDescriptor objects for and add to m_rules_arr.
 	 Set ruleId referencing them.  */
-      result_obj->set ("ruleId", new json::string (option_text));
+      result_obj->set_string ("ruleId", option_text);
       if (m_rule_id_set.contains (option_text))
 	free (option_text);
       else
@@ -565,7 +565,7 @@ sarif_builder::make_result_object (diagnostic_context *context,
 	 has a ruleId.
 	 We don't bother creating reportingDescriptor objects for these.  */
       char *rule_id = make_rule_id_for_diagnostic_kind (orig_diag_kind);
-      result_obj->set ("ruleId", new json::string (rule_id));
+      result_obj->set_string ("ruleId", rule_id);
       free (rule_id);
     }
 
@@ -582,7 +582,7 @@ sarif_builder::make_result_object (diagnostic_context *context,
 
   /* "level" property (SARIF v2.1.0 section 3.27.10).  */
   if (const char *sarif_level = maybe_get_sarif_level (diagnostic->kind))
-    result_obj->set ("level", new json::string (sarif_level));
+    result_obj->set_string ("level", sarif_level);
 
   /* "message" property (SARIF v2.1.0 section 3.27.11).  */
   json::object *message_obj
@@ -633,7 +633,7 @@ make_reporting_descriptor_object_for_warning (diagnostic_context *context,
   json::object *reporting_desc = new json::object ();
 
   /* "id" property (SARIF v2.1.0 section 3.49.3).  */
-  reporting_desc->set ("id", new json::string (option_text));
+  reporting_desc->set_string ("id", option_text);
 
   /* We don't implement "name" property (SARIF v2.1.0 section 3.49.7), since
      it seems redundant compared to "id".  */
@@ -645,7 +645,7 @@ make_reporting_descriptor_object_for_warning (diagnostic_context *context,
 	= context->m_get_option_url (context, diagnostic->option_index);
       if (option_url)
 	{
-	  reporting_desc->set ("helpUri", new json::string (option_url));
+	  reporting_desc->set_string ("helpUri", option_url);
 	  free (option_url);
 	}
     }
@@ -665,13 +665,13 @@ sarif_builder::make_reporting_descriptor_object_for_cwe_id (int cwe_id) const
   {
     pretty_printer pp;
     pp_printf (&pp, "%i", cwe_id);
-    reporting_desc->set ("id", new json::string (pp_formatted_text (&pp)));
+    reporting_desc->set_string ("id", pp_formatted_text (&pp));
   }
 
   /* "helpUri" property (SARIF v2.1.0 section 3.49.12).  */
   {
     char *url = get_cwe_url (cwe_id);
-    reporting_desc->set ("helpUri", new json::string (url));
+    reporting_desc->set_string ("helpUri", url);
     free (url);
   }
 
@@ -692,7 +692,7 @@ make_reporting_descriptor_reference_object_for_cwe_id (int cwe_id)
   {
     pretty_printer pp;
     pp_printf (&pp, "%i", cwe_id);
-    desc_ref_obj->set ("id", new json::string (pp_formatted_text (&pp)));
+    desc_ref_obj->set_string ("id", pp_formatted_text (&pp));
   }
 
   /* "toolComponent" property (SARIF v2.1.0 section 3.52.7).  */
@@ -716,7 +716,7 @@ make_tool_component_reference_object_for_cwe () const
   json::object *comp_ref_obj = new json::object ();
 
   /* "name" property  (SARIF v2.1.0 section 3.54.3).  */
-  comp_ref_obj->set ("name", new json::string ("cwe"));
+  comp_ref_obj->set_string ("name", "cwe");
 
   return comp_ref_obj;
 }
@@ -858,13 +858,13 @@ sarif_builder::make_artifact_location_object (const char *filename)
   json::object *artifact_loc_obj = new json::object ();
 
   /* "uri" property (SARIF v2.1.0 section 3.4.3).  */
-  artifact_loc_obj->set ("uri", new json::string (filename));
+  artifact_loc_obj->set_string ("uri", filename);
 
   if (filename[0] != '/')
     {
       /* If we have a relative path, set the "uriBaseId" property
 	 (SARIF v2.1.0 section 3.4.4).  */
-      artifact_loc_obj->set ("uriBaseId", new json::string (PWD_PROPERTY_NAME));
+      artifact_loc_obj->set_string ("uriBaseId", PWD_PROPERTY_NAME);
       m_seen_any_relative_paths = true;
     }
 
@@ -908,7 +908,7 @@ sarif_builder::make_artifact_location_object_for_pwd () const
     {
       gcc_assert (strlen (pwd) > 0);
       gcc_assert (pwd[strlen (pwd) - 1] == '/');
-      artifact_loc_obj->set ("uri", new json::string (pwd));
+      artifact_loc_obj->set_string ("uri", pwd);
       free (pwd);
     }
 
@@ -950,21 +950,20 @@ sarif_builder::maybe_make_region_object (location_t loc) const
   json::object *region_obj = new json::object ();
 
   /* "startLine" property (SARIF v2.1.0 section 3.30.5) */
-  region_obj->set ("startLine", new json::integer_number (exploc_start.line));
+  region_obj->set_integer ("startLine", exploc_start.line);
 
   /* "startColumn" property (SARIF v2.1.0 section 3.30.6) */
-  region_obj->set ("startColumn",
-		   new json::integer_number (get_sarif_column (exploc_start)));
+  region_obj->set_integer ("startColumn", get_sarif_column (exploc_start));
 
   /* "endLine" property (SARIF v2.1.0 section 3.30.7) */
   if (exploc_finish.line != exploc_start.line)
-    region_obj->set ("endLine", new json::integer_number (exploc_finish.line));
+    region_obj->set_integer ("endLine", exploc_finish.line);
 
   /* "endColumn" property (SARIF v2.1.0 section 3.30.8).
      This expresses the column immediately beyond the range.  */
   {
     int next_column = sarif_builder::get_sarif_column (exploc_finish) + 1;
-    region_obj->set ("endColumn", new json::integer_number (next_column));
+    region_obj->set_integer ("endColumn", next_column);
   }
 
   return region_obj;
@@ -1001,11 +1000,11 @@ sarif_builder::maybe_make_region_object_for_context (location_t loc) const
   json::object *region_obj = new json::object ();
 
   /* "startLine" property (SARIF v2.1.0 section 3.30.5) */
-  region_obj->set ("startLine", new json::integer_number (exploc_start.line));
+  region_obj->set_integer ("startLine", exploc_start.line);
 
   /* "endLine" property (SARIF v2.1.0 section 3.30.7) */
   if (exploc_finish.line != exploc_start.line)
-    region_obj->set ("endLine", new json::integer_number (exploc_finish.line));
+    region_obj->set_integer ("endLine", exploc_finish.line);
 
   /* "snippet" property (SARIF v2.1.0 section 3.30.13).  */
   if (json::object *artifact_content_obj
@@ -1032,21 +1031,20 @@ sarif_builder::make_region_object_for_hint (const fixit_hint &hint) const
   json::object *region_obj = new json::object ();
 
   /* "startLine" property (SARIF v2.1.0 section 3.30.5) */
-  region_obj->set ("startLine", new json::integer_number (exploc_start.line));
+  region_obj->set_integer ("startLine", exploc_start.line);
 
   /* "startColumn" property (SARIF v2.1.0 section 3.30.6) */
   int start_col = get_sarif_column (exploc_start);
-  region_obj->set ("startColumn",
-		   new json::integer_number (start_col));
+  region_obj->set_integer ("startColumn", start_col);
 
   /* "endLine" property (SARIF v2.1.0 section 3.30.7) */
   if (exploc_next.line != exploc_start.line)
-    region_obj->set ("endLine", new json::integer_number (exploc_next.line));
+    region_obj->set_integer ("endLine", exploc_next.line);
 
   /* "endColumn" property (SARIF v2.1.0 section 3.30.8).
      This expresses the column immediately beyond the range.  */
   int next_col =  get_sarif_column (exploc_next);
-  region_obj->set ("endColumn", new json::integer_number (next_col));
+  region_obj->set_integer ("endColumn", next_col);
 
   return region_obj;
 }
@@ -1095,21 +1093,20 @@ make_logical_location_object (const logical_location &logical_loc) const
 
   /* "name" property (SARIF v2.1.0 section 3.33.4).  */
   if (const char *short_name = logical_loc.get_short_name ())
-    logical_loc_obj->set ("name", new json::string (short_name));
+    logical_loc_obj->set_string ("name", short_name);
 
   /* "fullyQualifiedName" property (SARIF v2.1.0 section 3.33.5).  */
   if (const char *name_with_scope = logical_loc.get_name_with_scope ())
-    logical_loc_obj->set ("fullyQualifiedName",
-			  new json::string (name_with_scope));
+    logical_loc_obj->set_string ("fullyQualifiedName", name_with_scope);
 
   /* "decoratedName" property (SARIF v2.1.0 section 3.33.6).  */
   if (const char *internal_name = logical_loc.get_internal_name ())
-    logical_loc_obj->set ("decoratedName", new json::string (internal_name));
+    logical_loc_obj->set_string ("decoratedName", internal_name);
 
   /* "kind" property (SARIF v2.1.0 section 3.33.7).  */
   enum logical_location_kind kind = logical_loc.get_kind ();
   if (const char *sarif_kind_str = maybe_get_sarif_kind (kind))
-    logical_loc_obj->set ("kind", new json::string (sarif_kind_str));
+    logical_loc_obj->set_string ("kind", sarif_kind_str);
 
   return logical_loc_obj;
 }
@@ -1172,13 +1169,11 @@ sarif_builder::make_thread_flow_location_object (const diagnostic_event &ev,
     thread_flow_loc_obj->set ("kinds", kinds_arr);
 
   /* "nestingLevel" property (SARIF v2.1.0 section 3.38.10).  */
-  thread_flow_loc_obj->set ("nestingLevel",
-			    new json::integer_number (ev.get_stack_depth ()));
+  thread_flow_loc_obj->set_integer ("nestingLevel", ev.get_stack_depth ());
 
   /* "executionOrder" property (SARIF v2.1.0 3.38.11).
      Offset by 1 to match the human-readable values emitted by %@.  */
-  thread_flow_loc_obj->set ("executionOrder",
-			    new json::integer_number (path_event_idx + 1));
+  thread_flow_loc_obj->set_integer ("executionOrder", path_event_idx + 1);
 
   /* It might be nice to eventually implement the following for -fanalyzer:
      - the "stack" property (SARIF v2.1.0 section 3.38.5)
@@ -1222,7 +1217,7 @@ sarif_builder::make_message_object (const char *msg) const
   json::object *message_obj = new json::object ();
 
   /* "text" property (SARIF v2.1.0 section 3.11.8).  */
-  message_obj->set ("text", new json::string (msg));
+  message_obj->set_string ("text", msg);
 
   return message_obj;
 }
@@ -1238,7 +1233,7 @@ sarif_builder::make_message_object_for_diagram (diagnostic_context *context,
   json::object *message_obj = new json::object ();
 
   /* "text" property (SARIF v2.1.0 section 3.11.8).  */
-  message_obj->set ("text", new json::string (diagram.get_alt_text ()));
+  message_obj->set_string ("text", diagram.get_alt_text ());
 
   char *saved_prefix = pp_take_prefix (context->printer);
   pp_set_prefix (context->printer, NULL);
@@ -1250,8 +1245,7 @@ sarif_builder::make_message_object_for_diagram (diagnostic_context *context,
   pp_set_prefix (context->printer, saved_prefix);
 
   /* "markdown" property (SARIF v2.1.0 section 3.11.9).  */
-  message_obj->set ("markdown",
-		    new json::string (pp_formatted_text (context->printer)));
+  message_obj->set_string ("markdown", pp_formatted_text (context->printer));
 
   pp_clear_output_area (context->printer);
 
@@ -1267,7 +1261,7 @@ sarif_builder::make_multiformat_message_string (const char *msg) const
   json::object *message_obj = new json::object ();
 
   /* "text" property (SARIF v2.1.0 section 3.12.3).  */
-  message_obj->set ("text", new json::string (msg));
+  message_obj->set_string ("text", msg);
 
   return message_obj;
 }
@@ -1285,10 +1279,10 @@ sarif_builder::make_top_level_object (sarif_invocation *invocation_obj,
   json::object *log_obj = new json::object ();
 
   /* "$schema" property (SARIF v2.1.0 section 3.13.3) .  */
-  log_obj->set ("$schema", new json::string (SARIF_SCHEMA));
+  log_obj->set_string ("$schema", SARIF_SCHEMA);
 
   /* "version" property (SARIF v2.1.0 section 3.13.2).  */
-  log_obj->set ("version", new json::string (SARIF_VERSION));
+  log_obj->set_string ("version", SARIF_VERSION);
 
   /* "runs" property (SARIF v2.1.0 section 3.13.4).  */
   json::array *run_arr = new json::array ();
@@ -1376,15 +1370,15 @@ sarif_builder::make_tool_object () const
 
 	    /* "name" property (SARIF v2.1.0 section 3.19.8).  */
 	    if (const char *short_name = p.get_short_name ())
-	      plugin_obj->set ("name", new json::string (short_name));
+	      plugin_obj->set_string ("name", short_name);
 
 	    /* "fullName" property (SARIF v2.1.0 section 3.19.9).  */
 	    if (const char *full_name = p.get_full_name ())
-	      plugin_obj->set ("fullName", new json::string (full_name));
+	      plugin_obj->set_string ("fullName", full_name);
 
 	    /* "version" property (SARIF v2.1.0 section 3.19.13).  */
 	    if (const char *version = p.get_version ())
-	      plugin_obj->set ("version", new json::string (version));
+	      plugin_obj->set_string ("version", version);
 	  }
 	  auto_vec <json::object *> m_plugin_objs;
 	};
@@ -1419,23 +1413,23 @@ sarif_builder::make_driver_tool_component_object () const
       {
 	/* "name" property (SARIF v2.1.0 section 3.19.8).  */
 	if (const char *name = vinfo->get_tool_name ())
-	  driver_obj->set ("name", new json::string (name));
+	  driver_obj->set_string ("name", name);
 
 	/* "fullName" property (SARIF v2.1.0 section 3.19.9).  */
 	if (char *full_name = vinfo->maybe_make_full_name ())
 	  {
-	    driver_obj->set ("fullName", new json::string (full_name));
+	    driver_obj->set_string ("fullName", full_name);
 	    free (full_name);
 	  }
 
 	/* "version" property (SARIF v2.1.0 section 3.19.13).  */
 	if (const char *version = vinfo->get_version_string ())
-	  driver_obj->set ("version", new json::string (version));
+	  driver_obj->set_string ("version", version);
 
 	/* "informationUri" property (SARIF v2.1.0 section 3.19.17).  */
 	if (char *version_url =  vinfo->maybe_make_version_url ())
 	  {
-	    driver_obj->set ("informationUri", new json::string (version_url));
+	    driver_obj->set_string ("informationUri", version_url);
 	    free (version_url);
 	  }
       }
@@ -1480,13 +1474,13 @@ sarif_builder::maybe_make_cwe_taxonomy_object () const
   json::object *taxonomy_obj = new json::object ();
 
   /* "name" property (SARIF v2.1.0 section 3.19.8).  */
-  taxonomy_obj->set ("name", new json::string ("CWE"));
+  taxonomy_obj->set_string ("name", "CWE");
 
   /* "version" property (SARIF v2.1.0 section 3.19.13).  */
-  taxonomy_obj->set ("version", new json::string ("4.7"));
+  taxonomy_obj->set_string ("version", "4.7");
 
   /* "organization" property (SARIF v2.1.0 section 3.19.18).  */
-  taxonomy_obj->set ("organization", new json::string ("MITRE"));
+  taxonomy_obj->set_string ("organization", "MITRE");
 
   /* "shortDescription" property (SARIF v2.1.0 section 3.19.19).  */
   json::object *short_desc
@@ -1527,7 +1521,7 @@ sarif_builder::make_artifact_object (const char *filename)
   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 ("sourceLanguage", new json::string (source_lang));
+      artifact_obj->set_string ("sourceLanguage", source_lang);
 
   return artifact_obj;
 }
@@ -1602,7 +1596,7 @@ sarif_builder::maybe_make_artifact_content_object (const char *filename,
     }
 
   json::object *artifact_content_obj = new json::object ();
-  artifact_content_obj->set ("text", new json::string (text_utf8));
+  artifact_content_obj->set_string ("text", text_utf8);
   free (text_utf8);
 
   return artifact_content_obj;
@@ -1676,7 +1670,7 @@ sarif_builder::make_artifact_content_object (const char *text) const
   json::object *content_obj = new json::object ();
 
   /* "text" property (SARIF v2.1.0 section 3.3.2).  */
-  content_obj->set ("text", new json::string (text));
+  content_obj->set_string ("text", text);
 
   return content_obj;
 }
diff --git a/gcc/gcov.cc b/gcc/gcov.cc
index 2fad6aa7edec..8b4748d3a1a8 100644
--- a/gcc/gcov.cc
+++ b/gcc/gcov.cc
@@ -1109,12 +1109,11 @@ output_intermediate_json_line (json::array *object,
     return;
 
   json::object *lineo = new json::object ();
-  lineo->set ("line_number", new json::integer_number (line_num));
+  lineo->set_integer ("line_number", line_num);
   if (function_name != NULL)
-    lineo->set ("function_name", new json::string (function_name));
-  lineo->set ("count", new json::integer_number (line->count));
-  lineo->set ("unexecuted_block",
-	      new json::literal (line->has_unexecuted_block));
+    lineo->set_string ("function_name", function_name);
+  lineo->set_integer ("count", line->count);
+  lineo->set_bool ("unexecuted_block", line->has_unexecuted_block);
 
   json::array *bb_ids = new json::array ();
   for (const block_info *block : line->blocks)
@@ -1135,25 +1134,20 @@ output_intermediate_json_line (json::array *object,
 	if (!(*it)->is_unconditional && !(*it)->is_call_non_return)
 	  {
 	    json::object *branch = new json::object ();
-	    branch->set ("count", new json::integer_number ((*it)->count));
-	    branch->set ("throw", new json::literal ((*it)->is_throw));
-	    branch->set ("fallthrough",
-			 new json::literal ((*it)->fall_through));
-	    branch->set ("source_block_id",
-			 new json::integer_number ((*it)->src->id));
-	    branch->set ("destination_block_id",
-			 new json::integer_number ((*it)->dst->id));
+	    branch->set_integer ("count", (*it)->count);
+	    branch->set_bool ("throw", (*it)->is_throw);
+	    branch->set_bool ("fallthrough", (*it)->fall_through);
+	    branch->set_integer ("source_block_id", (*it)->src->id);
+	    branch->set_integer ("destination_block_id", (*it)->dst->id);
 	    branches->append (branch);
 	  }
 	else if ((*it)->is_call_non_return)
 	  {
 	    json::object *call = new json::object ();
 	    gcov_type returns = (*it)->src->count - (*it)->count;
-	    call->set ("source_block_id",
-		       new json::integer_number ((*it)->src->id));
-	    call->set ("destination_block_id",
-		       new json::integer_number ((*it)->dst->id));
-	    call->set ("returned", new json::integer_number (returns));
+	    call->set_integer ("source_block_id", (*it)->src->id);
+	    call->set_integer ("destination_block_id", (*it)->dst->id);
+	    call->set_integer ("returned", returns);
 	    calls->append (call);
 	  }
       }
@@ -1236,7 +1230,7 @@ output_json_intermediate_file (json::array *json_files, source_info *src)
   json::object *root = new json::object ();
   json_files->append (root);
 
-  root->set ("file", new json::string (src->name));
+  root->set_string ("file", src->name);
 
   json::array *functions = new json::array ();
   root->set ("functions", functions);
@@ -1247,22 +1241,15 @@ output_json_intermediate_file (json::array *json_files, source_info *src)
        it != src->functions.end (); it++)
     {
       json::object *function = new json::object ();
-      function->set ("name", new json::string ((*it)->m_name));
-      function->set ("demangled_name",
-		     new json::string ((*it)->get_demangled_name ()));
-      function->set ("start_line",
-		     new json::integer_number ((*it)->start_line));
-      function->set ("start_column",
-		     new json::integer_number ((*it)->start_column));
-      function->set ("end_line", new json::integer_number ((*it)->end_line));
-      function->set ("end_column",
-		     new json::integer_number ((*it)->end_column));
-      function->set ("blocks",
-		     new json::integer_number ((*it)->get_block_count ()));
-      function->set ("blocks_executed",
-		     new json::integer_number ((*it)->blocks_executed));
-      function->set ("execution_count",
-		     new json::integer_number ((*it)->blocks[0].count));
+      function->set_string ("name", (*it)->m_name);
+      function->set_string ("demangled_name", (*it)->get_demangled_name ());
+      function->set_integer ("start_line", (*it)->start_line);
+      function->set_integer ("start_column", (*it)->start_column);
+      function->set_integer ("end_line", (*it)->end_line);
+      function->set_integer ("end_column", (*it)->end_column);
+      function->set_integer ("blocks", (*it)->get_block_count ());
+      function->set_integer ("blocks_executed", (*it)->blocks_executed);
+      function->set_integer ("execution_count", (*it)->blocks[0].count);
 
       functions->append (function);
     }
@@ -1549,12 +1536,12 @@ generate_results (const char *file_name)
   gcov_intermediate_filename = get_gcov_intermediate_filename (file_name);
 
   json::object *root = new json::object ();
-  root->set ("format_version", new json::string (GCOV_JSON_FORMAT_VERSION));
-  root->set ("gcc_version", new json::string (version_string));
+  root->set_string ("format_version", GCOV_JSON_FORMAT_VERSION);
+  root->set_string ("gcc_version", version_string);
 
   if (bbg_cwd != NULL)
-    root->set ("current_working_directory", new json::string (bbg_cwd));
-  root->set ("data_file", new json::string (file_name));
+    root->set_string ("current_working_directory", bbg_cwd);
+  root->set_string ("data_file", file_name);
 
   json::array *json_files = new json::array ();
   root->set ("files", json_files);
diff --git a/gcc/json.cc b/gcc/json.cc
index 741e97b20e5e..f5398ece066a 100644
--- a/gcc/json.cc
+++ b/gcc/json.cc
@@ -128,6 +128,42 @@ object::get (const char *key) const
     return NULL;
 }
 
+/* Set value of KEY within this object to a JSON
+   string value based on UTF8_VALUE.  */
+
+void
+object::set_string (const char *key, const char *utf8_value)
+{
+  set (key, new json::string (utf8_value));
+}
+
+/* Set value of KEY within this object to a JSON
+   integer value based on V.  */
+
+void
+object::set_integer (const char *key, long v)
+{
+  set (key, new json::integer_number (v));
+}
+
+/* Set value of KEY within this object to a JSON
+   floating point value based on V.  */
+
+void
+object::set_float (const char *key, double v)
+{
+  set (key, new json::float_number (v));
+}
+
+/* Set value of KEY within this object to the JSON
+   literal true or false, based on V.  */
+
+void
+object::set_bool (const char *key, bool v)
+{
+  set (key, new json::literal (v));
+}
+
 /* class json::array, a subclass of json::value, representing
    an ordered collection of values.  */
 
@@ -311,8 +347,8 @@ static void
 test_writing_objects ()
 {
   object obj;
-  obj.set ("foo", new json::string ("bar"));
-  obj.set ("baz", new json::string ("quux"));
+  obj.set_string ("foo", "bar");
+  obj.set_string ("baz", "quux");
   /* This test relies on json::object writing out key/value pairs
      in key-insertion order.  */
   assert_print_eq (obj, "{\"foo\": \"bar\", \"baz\": \"quux\"}");
diff --git a/gcc/json.h b/gcc/json.h
index 6cc58c8ae0c2..6fadd119ba57 100644
--- a/gcc/json.h
+++ b/gcc/json.h
@@ -102,6 +102,13 @@ class object : public value
   void set (const char *key, value *v);
   value *get (const char *key) const;
 
+  void set_string (const char *key, const char *utf8_value);
+  void set_integer (const char *key, long v);
+  void set_float (const char *key, double v);
+
+  /* Set to literal true/false.  */
+  void set_bool (const char *key, bool v);
+
  private:
   typedef hash_map <char *, value *,
     simple_hashmap_traits<nofree_string_hash, value *> > map_t;
diff --git a/gcc/optinfo-emit-json.cc b/gcc/optinfo-emit-json.cc
index a4e8faa83732..11cad42a4330 100644
--- a/gcc/optinfo-emit-json.cc
+++ b/gcc/optinfo-emit-json.cc
@@ -58,14 +58,14 @@ optrecord_json_writer::optrecord_json_writer ()
   /* Populate with metadata; compare with toplev.cc: print_version.  */
   json::object *metadata = new json::object ();
   m_root_tuple->append (metadata);
-  metadata->set ("format", new json::string ("1"));
+  metadata->set_string ("format", "1");
   json::object *generator = new json::object ();
   metadata->set ("generator", generator);
-  generator->set ("name", new json::string (lang_hooks.name));
-  generator->set ("pkgversion", new json::string (pkgversion_string));
-  generator->set ("version", new json::string (version_string));
+  generator->set_string ("name", lang_hooks.name);
+  generator->set_string ("pkgversion", pkgversion_string);
+  generator->set_string ("version", version_string);
   /* TARGET_NAME is passed in by the Makefile.  */
-  generator->set ("target", new json::string (TARGET_NAME));
+  generator->set_string ("target", TARGET_NAME);
 
   /* TODO: capture command-line?
      see gen_producer_string in dwarf2out.cc (currently static).  */
@@ -180,10 +180,10 @@ json::object *
 optrecord_json_writer::impl_location_to_json (dump_impl_location_t loc)
 {
   json::object *obj = new json::object ();
-  obj->set ("file", new json::string (loc.m_file));
-  obj->set ("line", new json::integer_number (loc.m_line));
+  obj->set_string ("file", loc.m_file);
+  obj->set_integer ("line", loc.m_line);
   if (loc.m_function)
-    obj->set ("function", new json::string (loc.m_function));
+    obj->set_string ("function", loc.m_function);
   return obj;
 }
 
@@ -195,9 +195,9 @@ optrecord_json_writer::location_to_json (location_t loc)
   gcc_assert (LOCATION_LOCUS (loc) != UNKNOWN_LOCATION);
   expanded_location exploc = expand_location (loc);
   json::object *obj = new json::object ();
-  obj->set ("file", new json::string (exploc.file));
-  obj->set ("line", new json::integer_number (exploc.line));
-  obj->set ("column", new json::integer_number (exploc.column));
+  obj->set_string ("file", exploc.file);
+  obj->set_integer ("line", exploc.line);
+  obj->set_integer ("column", exploc.column);
   return obj;
 }
 
@@ -207,9 +207,8 @@ json::object *
 optrecord_json_writer::profile_count_to_json (profile_count count)
 {
   json::object *obj = new json::object ();
-  obj->set ("value", new json::integer_number (count.to_gcov_type ()));
-  obj->set ("quality",
-	    new json::string (profile_quality_as_string (count.quality ())));
+  obj->set_integer ("value", count.to_gcov_type ());
+  obj->set_string ("quality", profile_quality_as_string (count.quality ()));
   return obj;
 }
 
@@ -250,8 +249,8 @@ optrecord_json_writer::pass_to_json (opt_pass *pass)
       break;
     }
   obj->set ("id", get_id_value_for_pass (pass));
-  obj->set ("type", new json::string (type));
-  obj->set ("name", new json::string (pass->name));
+  obj->set_string ("type", type);
+  obj->set_string ("name", pass->name);
   /* Represent the optgroup flags as an array.  */
   {
     json::array *optgroups = new json::array ();
@@ -262,7 +261,7 @@ optrecord_json_writer::pass_to_json (opt_pass *pass)
 	  && (pass->optinfo_flags & optgroup->value))
 	optgroups->append (new json::string (optgroup->name));
   }
-  obj->set ("num", new json::integer_number (pass->static_pass_number));
+  obj->set_integer ("num", pass->static_pass_number);
   return obj;
 }
 
@@ -315,7 +314,7 @@ optrecord_json_writer::inlining_chain_to_json (location_t loc)
 	  json::object *obj = new json::object ();
 	  const char *printable_name
 	    = lang_hooks.decl_printable_name (fndecl, 2);
-	  obj->set ("fndecl", new json::string (printable_name));
+	  obj->set_string ("fndecl", printable_name);
 	  if (LOCATION_LOCUS (*locus) != UNKNOWN_LOCATION)
 	    obj->set ("site", location_to_json (*locus));
 	  array->append (obj);
@@ -336,7 +335,7 @@ optrecord_json_writer::optinfo_to_json (const optinfo *optinfo)
 	    impl_location_to_json (optinfo->get_impl_location ()));
 
   const char *kind_str = optinfo_kind_to_string (optinfo->get_kind ());
-  obj->set ("kind", new json::string (kind_str));
+  obj->set_string ("kind", kind_str);
   json::array *message = new json::array ();
   obj->set ("message", message);
   for (unsigned i = 0; i < optinfo->num_items (); i++)
@@ -354,7 +353,7 @@ optrecord_json_writer::optinfo_to_json (const optinfo *optinfo)
 	case OPTINFO_ITEM_KIND_TREE:
 	  {
 	    json::object *json_item = new json::object ();
-	    json_item->set ("expr", new json::string (item->get_text ()));
+	    json_item->set_string ("expr", item->get_text ());
 
 	    /* Capture any location for the node.  */
 	    if (LOCATION_LOCUS (item->get_location ()) != UNKNOWN_LOCATION)
@@ -367,7 +366,7 @@ optrecord_json_writer::optinfo_to_json (const optinfo *optinfo)
 	case OPTINFO_ITEM_KIND_GIMPLE:
 	  {
 	    json::object *json_item = new json::object ();
-	    json_item->set ("stmt", new json::string (item->get_text ()));
+	    json_item->set_string ("stmt", item->get_text ());
 
 	    /* Capture any location for the stmt.  */
 	    if (LOCATION_LOCUS (item->get_location ()) != UNKNOWN_LOCATION)
@@ -380,7 +379,7 @@ optrecord_json_writer::optinfo_to_json (const optinfo *optinfo)
 	case OPTINFO_ITEM_KIND_SYMTAB_NODE:
 	  {
 	    json::object *json_item = new json::object ();
-	    json_item->set ("symtab_node", new json::string (item->get_text ()));
+	    json_item->set_string ("symtab_node", item->get_text ());
 
 	    /* Capture any location for the node.  */
 	    if (LOCATION_LOCUS (item->get_location ()) != UNKNOWN_LOCATION)
@@ -413,7 +412,7 @@ optrecord_json_writer::optinfo_to_json (const optinfo *optinfo)
     {
       const char *fnname
 	= IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (current_function_decl));
-      obj->set ("function", new json::string (fnname));
+      obj->set_string ("function", fnname);
     }
 
   if (loc != UNKNOWN_LOCATION)
diff --git a/gcc/timevar.cc b/gcc/timevar.cc
index a63e5c6334e6..f89e61055e00 100644
--- a/gcc/timevar.cc
+++ b/gcc/timevar.cc
@@ -841,12 +841,10 @@ json::object *
 make_json_for_timevar_time_def (const timevar_time_def &ttd)
 {
   json::object *obj = new json::object ();
-  obj->set ("user",
-	    new json::float_number (nanosec_to_floating_sec (ttd.user)));
-  obj->set ("sys", new json::float_number (nanosec_to_floating_sec (ttd.sys)));
-  obj->set ("wall",
-	    new json::float_number (nanosec_to_floating_sec (ttd.wall)));
-  obj->set ("ggc_mem", new json::integer_number (ttd.ggc_mem));
+  obj->set_float ("user", nanosec_to_floating_sec (ttd.user));
+  obj->set_float ("sys", nanosec_to_floating_sec (ttd.sys));
+  obj->set_float ("wall", nanosec_to_floating_sec (ttd.wall));
+  obj->set_integer ("ggc_mem", ttd.ggc_mem);
   return obj;
 }
 #undef nanosec_to_floating_sec
@@ -859,7 +857,7 @@ json::value *
 timer::timevar_def::make_json () const
 {
   json::object *timevar_obj = new json::object ();
-  timevar_obj->set ("name", new json::string (name));
+  timevar_obj->set_string ("name", name);
   timevar_obj->set ("elapsed", make_json_for_timevar_time_def (elapsed));
 
   if (children)
@@ -883,7 +881,7 @@ timer::timevar_def::make_json () const
 		continue;
 	      json::object *child_obj = new json::object;
 	      children_arr->append (child_obj);
-	      child_obj->set ("name", new json::string (i.first->name));
+	      child_obj->set_string ("name", i.first->name);
 	      child_obj->set ("elapsed",
 			      make_json_for_timevar_time_def (i.second));
 	    }
@@ -947,15 +945,15 @@ timer::make_json () const
 
     json::object *total_obj = new json::object();
     json_arr->append (total_obj);
-    total_obj->set ("name", new json::string ("TOTAL"));
+    total_obj->set_string ("name", "TOTAL");
     total_obj->set ("elapsed", make_json_for_timevar_time_def (total_elapsed));
   }
 
   if (m_jit_client_items)
     report_obj->set ("client_items", m_jit_client_items->make_json ());
 
-  report_obj->set ("CHECKING_P", new json::literal ((bool)CHECKING_P));
-  report_obj->set ("flag_checking", new json::literal ((bool)flag_checking));
+  report_obj->set_bool ("CHECKING_P", CHECKING_P);
+  report_obj->set_bool ("flag_checking", flag_checking);
 
   return report_obj;
 
diff --git a/gcc/tree-diagnostic-path.cc b/gcc/tree-diagnostic-path.cc
index ee957cce2cf5..d2345c8dae74 100644
--- a/gcc/tree-diagnostic-path.cc
+++ b/gcc/tree-diagnostic-path.cc
@@ -673,15 +673,14 @@ default_tree_make_json_for_path (diagnostic_context *context,
 			json_from_expanded_location (context,
 						     event.get_location ()));
       label_text event_text (event.get_desc (false));
-      event_obj->set ("description", new json::string (event_text.get ()));
+      event_obj->set_string ("description", event_text.get ());
       if (tree fndecl = event.get_fndecl ())
 	{
 	  const char *function
 	    = identifier_to_locale (lang_hooks.decl_printable_name (fndecl, 2));
-	  event_obj->set ("function", new json::string (function));
+	  event_obj->set_string ("function", function);
 	}
-      event_obj->set ("depth",
-		      new json::integer_number (event.get_stack_depth ()));
+      event_obj->set_integer ("depth", event.get_stack_depth ());
       path_array->append (event_obj);
     }
   return path_array;
-- 
2.26.3


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2023-11-14 16:32 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-11-14 16:32 [pushed] json: reduce use of naked new in json-building code David Malcolm

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).