Skip to content

Commit

Permalink
analyzer,timevar: avoid naked "new" in JSON-handling
Browse files Browse the repository at this point in the history
Now that <memory> is always included, use std::unique_ptr in a few more
places to avoid naked "new".

No functional change intended.

gcc/analyzer/ChangeLog:
	* engine.cc (strongly_connected_components::to_json): Avoid naked
	"new".
	* infinite-loop.cc (infinite_loop::to_json): Convert return type
	to unique_ptr.  Avoid naked "new".
	* sm-signal.cc (signal_delivery_edge_info_t::to_json): Delete
	unused function.
	* supergraph.cc (supernode::to_json): Avoid naked "new".

gcc/ChangeLog:
	* timevar.cc: Include "make-unique.h".
	(timer::named_items::make_json): Convert return type to unique_ptr.
	Avoid naked "new".
	(make_json_for_timevar_time_def): Likewise.
	(timer::timevar_def::make_json): Likewise.
	(timer::make_json): Likewise.
	* timevar.h (timer::make_json): Likewise.
	(timer::timevar_def::make_json): Likewise.
	* tree-diagnostic-client-data-hooks.cc: Update for above changes.

Signed-off-by: David Malcolm <[email protected]>
  • Loading branch information
davidmalcolm committed Nov 28, 2024
1 parent 5341eb6 commit 066f309
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 34 deletions.
2 changes: 1 addition & 1 deletion gcc/analyzer/engine.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2422,7 +2422,7 @@ strongly_connected_components::to_json () const
{
auto scc_arr = ::make_unique<json::array> ();
for (int i = 0; i < m_sg.num_nodes (); i++)
scc_arr->append (new json::integer_number (get_scc_id (i)));
scc_arr->append (::make_unique<json::integer_number> (get_scc_id (i)));
return scc_arr;
}

Expand Down
8 changes: 4 additions & 4 deletions gcc/analyzer/infinite-loop.cc
Original file line number Diff line number Diff line change
Expand Up @@ -105,15 +105,15 @@ struct infinite_loop
&& m_loc == other.m_loc);
}

json::object *
std::unique_ptr<json::object>
to_json () const
{
json::object *loop_obj = new json::object ();
auto loop_obj = ::make_unique<json::object> ();
loop_obj->set_integer ("enode", m_enode.m_index);
json::array *edge_arr = new json::array ();
auto edge_arr = ::make_unique<json::array> ();
for (auto eedge : m_eedge_vec)
edge_arr->append (eedge->to_json ());
loop_obj->set ("eedges", edge_arr);
loop_obj->set ("eedges", std::move (edge_arr));
return loop_obj;
}

Expand Down
6 changes: 0 additions & 6 deletions gcc/analyzer/sm-signal.cc
Original file line number Diff line number Diff line change
Expand Up @@ -220,12 +220,6 @@ class signal_delivery_edge_info_t : public custom_edge_info
pp_string (pp, "signal delivered");
}

json::object *to_json () const
{
json::object *custom_obj = new json::object ();
return custom_obj;
}

bool update_model (region_model *model,
const exploded_edge *eedge,
region_model_context *) const final override
Expand Down
8 changes: 4 additions & 4 deletions gcc/analyzer/supergraph.cc
Original file line number Diff line number Diff line change
Expand Up @@ -737,7 +737,7 @@ supernode::to_json () const

/* Phi nodes. */
{
json::array *phi_arr = new json::array ();
auto phi_arr = ::make_unique<json::array> ();
for (gphi_iterator gpi = const_cast<supernode *> (this)->start_phis ();
!gsi_end_p (gpi); gsi_next (&gpi))
{
Expand All @@ -747,12 +747,12 @@ supernode::to_json () const
pp_gimple_stmt_1 (&pp, stmt, 0, (dump_flags_t)0);
phi_arr->append_string (pp_formatted_text (&pp));
}
snode_obj->set ("phis", phi_arr);
snode_obj->set ("phis", std::move (phi_arr));
}

/* Statements. */
{
json::array *stmt_arr = new json::array ();
auto stmt_arr = ::make_unique<json::array> ();
int i;
gimple *stmt;
FOR_EACH_VEC_ELT (m_stmts, i, stmt)
Expand All @@ -762,7 +762,7 @@ supernode::to_json () const
pp_gimple_stmt_1 (&pp, stmt, 0, (dump_flags_t)0);
stmt_arr->append_string (pp_formatted_text (&pp));
}
snode_obj->set ("stmts", stmt_arr);
snode_obj->set ("stmts", std::move (stmt_arr));
}

return snode_obj;
Expand Down
31 changes: 16 additions & 15 deletions gcc/timevar.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ along with GCC; see the file COPYING3. If not see
#include "timevar.h"
#include "options.h"
#include "json.h"
#include "make-unique.h"

/* Non-NULL if timevars should be used. In GCC, this happens with
the -ftime-report flag. */
Expand Down Expand Up @@ -59,7 +60,7 @@ class timer::named_items
void pop ();
void print (FILE *fp, const timevar_time_def *total);

json::value *make_json () const;
std::unique_ptr<json::value> make_json () const;

private:
/* Which timer instance does this relate to? */
Expand Down Expand Up @@ -135,10 +136,10 @@ timer::named_items::print (FILE *fp, const timevar_time_def *total)
/* Create a json value representing this object, suitable for use
in SARIF output. */

json::value *
std::unique_ptr<json::value>
timer::named_items::make_json () const
{
json::array *arr = new json::array ();
auto arr = ::make_unique<json::array> ();
for (const char *item_name : m_names)
{
hash_map_t &mut_map = const_cast <hash_map_t &> (m_hash_map);
Expand Down Expand Up @@ -695,10 +696,10 @@ timer::print (FILE *fp)
/* Create a json value representing this object, suitable for use
in SARIF output. */

json::object *
std::unique_ptr<json::object>
make_json_for_timevar_time_def (const timevar_time_def &ttd)
{
json::object *obj = new json::object ();
auto obj = ::make_unique<json::object> ();
obj->set_float ("wall", nanosec_to_floating_sec (ttd.wall));
obj->set_integer ("ggc_mem", ttd.ggc_mem);
return obj;
Expand All @@ -709,10 +710,10 @@ make_json_for_timevar_time_def (const timevar_time_def &ttd)
/* Create a json value representing this object, suitable for use
in SARIF output. */

json::value *
std::unique_ptr<json::value>
timer::timevar_def::make_json () const
{
json::object *timevar_obj = new json::object ();
auto timevar_obj = ::make_unique<json::object> ();
timevar_obj->set_string ("name", name);
timevar_obj->set ("elapsed", make_json_for_timevar_time_def (elapsed));

Expand All @@ -727,20 +728,20 @@ timer::timevar_def::make_json () const
}
if (any_children_with_time)
{
json::array *children_arr = new json::array ();
timevar_obj->set ("children", children_arr);
auto children_arr = ::make_unique<json::array> ();
for (auto i : *children)
{
/* Don't emit timing variables if we're going to get a row of
zeroes. */
if (all_zero (i.second))
continue;
json::object *child_obj = new json::object;
children_arr->append (child_obj);
auto child_obj = ::make_unique<json::object> ();
child_obj->set_string ("name", i.first->name);
child_obj->set ("elapsed",
make_json_for_timevar_time_def (i.second));
children_arr->append (std::move (child_obj));
}
timevar_obj->set ("children", std::move (children_arr));
}
}

Expand All @@ -750,11 +751,11 @@ timer::timevar_def::make_json () const
/* Create a json value representing this object, suitable for use
in SARIF output. */

json::value *
std::unique_ptr<json::value>
timer::make_json () const
{
#if defined (HAVE_WALL_TIME)
json::object *report_obj = new json::object ();
auto report_obj = ::make_unique<json::object> ();
json::array *json_arr = new json::array ();
report_obj->set ("timevars", json_arr);

Expand Down Expand Up @@ -798,10 +799,10 @@ timer::make_json () const
get_time (&total_now);
timevar_diff (&total_elapsed, m_timevars[TV_TOTAL].start_time, total_now);

json::object *total_obj = new json::object();
json_arr->append (total_obj);
auto total_obj = ::make_unique<json::object> ();
total_obj->set_string ("name", "TOTAL");
total_obj->set ("elapsed", make_json_for_timevar_time_def (total_elapsed));
json_arr->append (std::move (total_obj));
}

if (m_jit_client_items)
Expand Down
4 changes: 2 additions & 2 deletions gcc/timevar.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ class timer
void pop_client_item ();

void print (FILE *fp);
json::value *make_json () const;
std::unique_ptr<json::value> make_json () const;

const char *get_topmost_item_name () const;

Expand All @@ -134,7 +134,7 @@ class timer
/* Private type: a timing variable. */
struct timevar_def
{
json::value *make_json () const;
std::unique_ptr<json::value> make_json () const;

/* Elapsed time for this variable. */
struct timevar_time_def elapsed;
Expand Down
4 changes: 2 additions & 2 deletions gcc/tree-diagnostic-client-data-hooks.cc
Original file line number Diff line number Diff line change
Expand Up @@ -143,11 +143,11 @@ class compiler_data_hooks : public diagnostic_client_data_hooks
const final override
{
if (g_timer)
if (json::value *timereport_val = g_timer->make_json ())
if (auto timereport_val = g_timer->make_json ())
{
sarif_property_bag &bag_obj
= invocation_obj.get_or_create_properties ();
bag_obj.set ("gcc/timeReport", timereport_val);
bag_obj.set ("gcc/timeReport", std::move (timereport_val));

/* If the user requested SARIF output, then assume they want the
time report data in the SARIF output, and *not* later emitted on
Expand Down

0 comments on commit 066f309

Please sign in to comment.