From patchwork Wed Jul 24 22:18:17 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Malcolm X-Patchwork-Id: 1964564 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=CkCHhQIH; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=8.43.85.97; helo=server2.sourceware.org; envelope-from=gcc-patches-bounces~incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4WTpQd1dQRz1yY9 for ; Thu, 25 Jul 2024 08:21:45 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 6D80D385C6CB for ; Wed, 24 Jul 2024 22:21:43 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTP id F0932385B532 for ; Wed, 24 Jul 2024 22:18:52 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org F0932385B532 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org F0932385B532 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1721859537; cv=none; b=f+loWuXt1NHM3DYbUoV3/ENsfW0X+HewDvtShMArYzl2EC4NImc0ThaEPMgNB17zt/wSmLF2/1qN3SQBH0YXY4tH7eTc7t9b9C2kXAGrMAjbJy/u15LQZcHweBlsVUblRksEsFGSZSUPGXZV5Fu2YmxtVQn6/3dBJKqo6mr96ec= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1721859537; c=relaxed/simple; bh=w15cZT9tDVxBt5xneCleHsuFDqiGLQvBASdvRNagnhc=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=A8suOokwqbVKntXDB8ETW6oQSFo8up4Ko+dQOzGpquki/fZLJWFg1vt1vO6dkj0x54rc5K8ttS0+9QdrG10pmZINmt/DzHKU65NC8FjB1Rr/QCua+otVvyQVsD5wKFXdqOOqMdJUQYDVTK5mdEoKR8kz37ri7xjRg5MA2QnILfc= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1721859532; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=AKOeJAb5btB0lnqI+Sb+p2wF2qa4OJsOPxxZ/926684=; b=CkCHhQIHbdR+59vh9mWyCTYm9hEy4wH1lKUd91zfQ5v+x85YaEQIsMMTGTulomY4mn5FSM W4SuY6mCn9p62fhqO67fGOXz1rCCNh1/P+/9hzl7Cw7gxiwqrYrhVpe5b4++mqOmgYedWF txfCRXY9oX3ilwmsV5wJLTksYKSNzNY= Received: from mx-prod-mc-04.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-442-IVeD-lFmMquqeKlBjHYIHQ-1; Wed, 24 Jul 2024 18:18:51 -0400 X-MC-Unique: IVeD-lFmMquqeKlBjHYIHQ-1 Received: from mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.17]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-04.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 56D2F1955D58 for ; Wed, 24 Jul 2024 22:18:50 +0000 (UTC) Received: from t14s.localdomain.com (unknown [10.22.33.183]) by mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 426621955F40; Wed, 24 Jul 2024 22:18:48 +0000 (UTC) From: David Malcolm To: gcc-patches@gcc.gnu.org Cc: David Malcolm Subject: [PATCH 09/16] diagnostics: JSON output: use std::unique_ptr throughout Date: Wed, 24 Jul 2024 18:18:17 -0400 Message-Id: <20240724221824.585054-10-dmalcolm@redhat.com> In-Reply-To: <20240724221824.585054-1-dmalcolm@redhat.com> References: <20240724221824.585054-1-dmalcolm@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.0 on 10.30.177.17 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-11.7 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces~incoming=patchwork.ozlabs.org@gcc.gnu.org No functional change intended. gcc/ChangeLog: * diagnostic-format-json.cc: Include "make-unique.h". (json_output_format::m_toplevel_array): Convert to std::unique_ptr. (json_output_format::json_output_format): Update accordingly. (json_output_format::~json_output_format): Remove manual "delete" of field. (json_from_expanded_location): Convert return type to std::unique_ptr. (json_from_location_range): Likewise. Use nullptr rather than NULL. (json_from_fixit_hint): Convert return type to std::unique_ptr. (json_from_metadata): Likewise. (make_json_for_path): Likewise. (json_output_format::on_end_diagnostic): Use std::unique_ptr throughout. (json_file_output_format::~json_file_output_format): Use nullptr. (selftest::test_unknown_location): Update to use std::unique_ptr. (selftest::test_bad_endpoints): Likewise. Replace NULL with nullptr. Signed-off-by: David Malcolm --- gcc/diagnostic-format-json.cc | 122 +++++++++++++++++----------------- 1 file changed, 62 insertions(+), 60 deletions(-) diff --git a/gcc/diagnostic-format-json.cc b/gcc/diagnostic-format-json.cc index 55ba39e0c532..b78cb92cfd7a 100644 --- a/gcc/diagnostic-format-json.cc +++ b/gcc/diagnostic-format-json.cc @@ -30,6 +30,7 @@ along with GCC; see the file COPYING3. If not see #include "json.h" #include "selftest.h" #include "logical-location.h" +#include "make-unique.h" /* Subclass of diagnostic_output_format for JSON output. */ @@ -62,7 +63,7 @@ protected: json_output_format (diagnostic_context &context, bool formatted) : diagnostic_output_format (context), - m_toplevel_array (new json::array ()), + m_toplevel_array (::make_unique ()), m_cur_group (nullptr), m_cur_children_array (nullptr), m_formatted (formatted) @@ -75,31 +76,30 @@ protected: { m_toplevel_array->dump (outf, m_formatted); fprintf (outf, "\n"); - delete m_toplevel_array; m_toplevel_array = nullptr; } private: /* The top-level JSON array of pending diagnostics. */ - json::array *m_toplevel_array; + std::unique_ptr m_toplevel_array; /* The JSON object for the current diagnostic group. */ - json::object *m_cur_group; + json::object *m_cur_group; // borrowed /* The JSON array for the "children" array within the current diagnostic group. */ - json::array *m_cur_children_array; + json::array *m_cur_children_array; // borrowed bool m_formatted; }; /* Generate a JSON object for LOC. */ -static json::value * +static std::unique_ptr json_from_expanded_location (diagnostic_context &context, location_t loc) { expanded_location exploc = expand_location (loc); - json::object *result = new json::object (); + std::unique_ptr result = ::make_unique (); if (exploc.file) result->set_string ("file", exploc.file); result->set_integer ("line", exploc.line); @@ -130,26 +130,29 @@ json_from_expanded_location (diagnostic_context &context, location_t loc) /* Generate a JSON object for LOC_RANGE. */ -static json::object * +static std::unique_ptr 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); if (caret_loc == UNKNOWN_LOCATION) - return NULL; + return nullptr; location_t start_loc = get_start (loc_range->m_loc); location_t finish_loc = get_finish (loc_range->m_loc); - json::object *result = new json::object (); - result->set ("caret", json_from_expanded_location (context, caret_loc)); + std::unique_ptr result = ::make_unique (); + result->set ("caret", + json_from_expanded_location (context, caret_loc)); if (start_loc != caret_loc && start_loc != UNKNOWN_LOCATION) - result->set ("start", json_from_expanded_location (context, start_loc)); + result->set ("start", + json_from_expanded_location (context, start_loc)); if (finish_loc != caret_loc && finish_loc != UNKNOWN_LOCATION) - result->set ("finish", json_from_expanded_location (context, finish_loc)); + result->set ("finish", + json_from_expanded_location (context, finish_loc)); if (loc_range->m_label) { @@ -163,15 +166,17 @@ json_from_location_range (diagnostic_context &context, /* Generate a JSON object for HINT. */ -static json::object * +static std::unique_ptr json_from_fixit_hint (diagnostic_context &context, const fixit_hint *hint) { - json::object *fixit_obj = new json::object (); + std::unique_ptr fixit_obj = ::make_unique (); location_t start_loc = hint->get_start_loc (); - fixit_obj->set ("start", json_from_expanded_location (context, start_loc)); + 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 ("next", + json_from_expanded_location (context, next_loc). release ()); fixit_obj->set_string ("string", hint->get_string ()); return fixit_obj; @@ -179,10 +184,10 @@ json_from_fixit_hint (diagnostic_context &context, const fixit_hint *hint) /* Generate a JSON object for METADATA. */ -static json::object * +static std::unique_ptr json_from_metadata (const diagnostic_metadata *metadata) { - json::object *metadata_obj = new json::object (); + std::unique_ptr metadata_obj = ::make_unique (); if (metadata->get_cwe ()) metadata_obj->set_integer ("cwe", metadata->get_cwe ()); @@ -192,16 +197,16 @@ json_from_metadata (const diagnostic_metadata *metadata) /* Make a JSON value for PATH. */ -static json::value * +static std::unique_ptr make_json_for_path (diagnostic_context &context, const diagnostic_path *path) { - json::array *path_array = new json::array (); + std::unique_ptr path_array = ::make_unique (); for (unsigned i = 0; i < path->num_events (); i++) { const diagnostic_event &event = path->get_event (i); - json::object *event_obj = new json::object (); + std::unique_ptr event_obj = ::make_unique (); if (event.get_location ()) event_obj->set ("location", json_from_expanded_location (context, @@ -214,7 +219,7 @@ make_json_for_path (diagnostic_context &context, event_obj->set_string ("function", name.get ()); } event_obj->set_integer ("depth", event.get_stack_depth ()); - path_array->append (event_obj); + path_array->append (std::move (event_obj)); } return path_array; } @@ -273,37 +278,41 @@ json_output_format::on_end_diagnostic (const diagnostic_info &diagnostic, { /* Otherwise, make diag_obj be the top-level object within the group; add a "children" array and record the column origin. */ - m_toplevel_array->append (diag_obj); m_cur_group = diag_obj; - m_cur_children_array = new json::array (); - diag_obj->set ("children", m_cur_children_array); + std::unique_ptr children_array + = ::make_unique (); + m_cur_children_array = children_array.get (); // borrowed + diag_obj->set ("children", std::move (children_array)); diag_obj->set_integer ("column-origin", m_context.m_column_origin); + m_toplevel_array->append (diag_obj); } - const rich_location *richloc = diagnostic.richloc; + /* diag_obj is now owned by either m_cur_children_array or + m_toplevel_array; further uses of diag_obj are borrowing it. */ - json::array *loc_array = new json::array (); - diag_obj->set ("locations", loc_array); + const rich_location *richloc = diagnostic.richloc; - for (unsigned int i = 0; i < richloc->get_num_locations (); i++) - { - const location_range *loc_range = richloc->get_range (i); - json::object *loc_obj - = json_from_location_range (m_context, loc_range, i); - if (loc_obj) - loc_array->append (loc_obj); - } + { + std::unique_ptr loc_array = ::make_unique (); + for (unsigned int i = 0; i < richloc->get_num_locations (); i++) + { + const location_range *loc_range = richloc->get_range (i); + if (std::unique_ptr loc_obj + = json_from_location_range (m_context, loc_range, i)) + loc_array->append (std::move (loc_obj)); + } + diag_obj->set ("locations", std::move (loc_array)); + } if (richloc->get_num_fixit_hints ()) { - json::array *fixit_array = new json::array (); - diag_obj->set ("fixits", fixit_array); + std::unique_ptr fixit_array = ::make_unique (); 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); - fixit_array->append (fixit_obj); + fixit_array->append (json_from_fixit_hint (m_context, hint)); } + diag_obj->set ("fixits", std::move (fixit_array)); } /* TODO: tree-ish things: @@ -312,20 +321,13 @@ json_output_format::on_end_diagnostic (const diagnostic_info &diagnostic, TODO: macro expansion information. */ if (diagnostic.metadata) - { - json::object *metadata_obj = json_from_metadata (diagnostic.metadata); - diag_obj->set ("metadata", metadata_obj); - } + diag_obj->set ("metadata", json_from_metadata (diagnostic.metadata)); const diagnostic_path *path = richloc->get_path (); if (path) - { - json::value *path_value = make_json_for_path (m_context, path); - diag_obj->set ("path", path_value); - } + diag_obj->set ("path", make_json_for_path (m_context, path)); - diag_obj->set ("escape-source", - new json::literal (richloc->escape_on_output_p ())); + diag_obj->set_bool ("escape-source", richloc->escape_on_output_p ()); } class json_stderr_output_format : public json_output_format @@ -359,7 +361,7 @@ public: ~json_file_output_format () { - char *filename = concat (m_base_file_name, ".gcc.json", NULL); + char *filename = concat (m_base_file_name, ".gcc.json", nullptr); free (m_base_file_name); m_base_file_name = nullptr; FILE *outf = fopen (filename, "w"); @@ -441,7 +443,7 @@ static void test_unknown_location () { test_diagnostic_context dc; - delete json_from_expanded_location (dc, UNKNOWN_LOCATION); + json_from_expanded_location (dc, UNKNOWN_LOCATION); } /* Verify that we gracefully handle attempts to serialize bad @@ -457,16 +459,16 @@ test_bad_endpoints () location_range loc_range; loc_range.m_loc = bad_endpoints; loc_range.m_range_display_kind = SHOW_RANGE_WITH_CARET; - loc_range.m_label = NULL; + loc_range.m_label = nullptr; test_diagnostic_context dc; - json::object *obj = json_from_location_range (dc, &loc_range, 0); + std::unique_ptr 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); - ASSERT_TRUE (obj->get ("start") == NULL); - ASSERT_TRUE (obj->get ("finish") == NULL); - delete obj; + ASSERT_TRUE (obj != nullptr); + ASSERT_TRUE (obj->get ("caret") != nullptr); + ASSERT_TRUE (obj->get ("start") == nullptr); + ASSERT_TRUE (obj->get ("finish") == nullptr); } /* Run all of the selftests within this file. */