From patchwork Thu Apr 6 15:06:36 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tom Tromey X-Patchwork-Id: 67468 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 85267385783F for ; Thu, 6 Apr 2023 15:07:47 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 85267385783F DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1680793667; bh=875Hr5MGacd6L91dKRTBv5ApK4XArobD5lGWaT9gvDk=; h=To:Cc:Subject:Date:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=lsRSJzy+WGYZDK6LHNQFMf1/bOUPD/JWJkLDDBV9zHwLIJW5YiyrYsGpK+u0zqp8m OPlAp/h/ave/T6rcNEZYhLNyb+htX+SBQAtL+8rmLMXS01q/HleSUgotTumLaGaTvL XowJPLClkcBIIcjtLC0entFFMIVuO1JTYuwU0xPs= X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from mail-io1-xd34.google.com (mail-io1-xd34.google.com [IPv6:2607:f8b0:4864:20::d34]) by sourceware.org (Postfix) with ESMTPS id 016D53857C44 for ; Thu, 6 Apr 2023 15:06:48 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 016D53857C44 Received: by mail-io1-xd34.google.com with SMTP id o12so17501040iow.6 for ; Thu, 06 Apr 2023 08:06:48 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680793606; x=1683385606; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=875Hr5MGacd6L91dKRTBv5ApK4XArobD5lGWaT9gvDk=; b=lsw0JQXMejI6y4fju/0uW4DbYdZx9w+9WBBCjRiU6ZY8BauxDmMp3aP03EdU2/lSrz t2ucnrzNuZYg9LyrDzEhMtSMe4bMyazQrfhTDxFEdXmkqXV31VKbvTMVC0auXDB5RQaE ORW7IdL3owbPnzLwBmPhW3qXci3+Q2vDTV7J/qaDretELwiQYKrZc7Ur1Ss1srZe9e0d MkHQr0Z9uGyLE5oHMpfNUvMO8IJhWOlfBKloerajuemUMAMzqZYoa6qMSoJbE20u6QIK BehTz8ca1XVYfl5T0HZcBkK8xkQqk7ac+ZEOkkJUy7n5qthGXsaBrYOTZ5vl8OG3IrYj vEFw== X-Gm-Message-State: AAQBX9epu8K8T/5CGvqepy50PgLVjk4C7CIe6/ly9njNKVAhQmzk/hzv lcmfLICLHJoO4IEy5LrJag027eEH+iOWh2k+dPXjPw== X-Google-Smtp-Source: AKy350bDkusRWDK9smFPFtG9zbKoXXQ10UnBLCWneJcgJPiu0/+1bIJfEMi4zQiV2WS9RDw6fbK2gw== X-Received: by 2002:a5e:dc0a:0:b0:746:1b8:8687 with SMTP id b10-20020a5edc0a000000b0074601b88687mr7291904iok.14.1680793606034; Thu, 06 Apr 2023 08:06:46 -0700 (PDT) Received: from localhost.localdomain (75-166-159-36.hlrn.qwest.net. [75.166.159.36]) by smtp.gmail.com with ESMTPSA id f34-20020a05663832a200b00408b3bc8061sm449774jav.43.2023.04.06.08.06.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 06 Apr 2023 08:06:45 -0700 (PDT) To: gdb-patches@sourceware.org Cc: Tom Tromey Subject: [PATCH] Use unique_xmalloc_ptr in apply_ext_lang_type_printers Date: Thu, 6 Apr 2023 09:06:36 -0600 Message-Id: <20230406150636.2676355-1-tromey@adacore.com> X-Mailer: git-send-email 2.39.1 MIME-Version: 1.0 X-Spam-Status: No, score=-12.2 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, 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: gdb-patches@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Tom Tromey via Gdb-patches From: Tom Tromey Reply-To: Tom Tromey Errors-To: gdb-patches-bounces+patchwork=sourceware.org@sourceware.org Sender: "Gdb-patches" This changes apply_ext_lang_type_printers to use unique_xmalloc_ptr, removing some manual memory management. Regression tested on x86-64 Fedora 36. --- gdb/extension-priv.h | 5 +++-- gdb/extension.c | 6 +++--- gdb/extension.h | 4 ++-- gdb/python/python.c | 10 ++++++---- gdb/typeprint.c | 13 +++++-------- 5 files changed, 19 insertions(+), 19 deletions(-) diff --git a/gdb/extension-priv.h b/gdb/extension-priv.h index 23a9f646d12..3442302a0be 100644 --- a/gdb/extension-priv.h +++ b/gdb/extension-priv.h @@ -137,7 +137,7 @@ struct extension_language_ops struct ext_lang_type_printers *); /* Try to pretty-print TYPE. If successful the pretty-printed type is - stored in *PRETTIED_TYPE, and the caller must free it. + stored in *PRETTIED_TYPE. Returns EXT_LANG_RC_OK upon success, EXT_LANG_RC_NOP if the type is not recognized, and EXT_LANG_RC_ERROR if an error was encountered. This function has a bit of a funny name, since it actually applies @@ -146,7 +146,8 @@ struct extension_language_ops enum ext_lang_rc (*apply_type_printers) (const struct extension_language_defn *, const struct ext_lang_type_printers *, - struct type *, char **prettied_type); + struct type *, + gdb::unique_xmalloc_ptr *prettied_type); /* Called after a type has been printed to give the type pretty-printer mechanism an opportunity to clean up. */ diff --git a/gdb/extension.c b/gdb/extension.c index 4ac6e0b6732..65f3bab32a7 100644 --- a/gdb/extension.c +++ b/gdb/extension.c @@ -418,13 +418,13 @@ ext_lang_type_printers::ext_lang_type_printers () returning the result of the first one that succeeds. If there was an error, or if no printer succeeds, then NULL is returned. */ -char * +gdb::unique_xmalloc_ptr apply_ext_lang_type_printers (struct ext_lang_type_printers *printers, struct type *type) { for (const struct extension_language_defn *extlang : extension_languages) { - char *result = NULL; + gdb::unique_xmalloc_ptr result; enum ext_lang_rc rc; if (extlang->ops == nullptr @@ -435,7 +435,7 @@ apply_ext_lang_type_printers (struct ext_lang_type_printers *printers, switch (rc) { case EXT_LANG_RC_OK: - gdb_assert (result != NULL); + gdb_assert (result != nullptr); return result; case EXT_LANG_RC_ERROR: return NULL; diff --git a/gdb/extension.h b/gdb/extension.h index ab83f9c6a28..2b0445133d3 100644 --- a/gdb/extension.h +++ b/gdb/extension.h @@ -282,8 +282,8 @@ extern void eval_ext_lang_from_control_command (struct command_line *cmd); extern void auto_load_ext_lang_scripts_for_objfile (struct objfile *); -extern char *apply_ext_lang_type_printers (struct ext_lang_type_printers *, - struct type *); +extern gdb::unique_xmalloc_ptr apply_ext_lang_type_printers + (struct ext_lang_type_printers *, struct type *); extern int apply_ext_lang_val_pretty_printer (struct value *value, struct ui_file *stream, int recurse, diff --git a/gdb/python/python.c b/gdb/python/python.c index b295ff88743..ea51766ec3e 100644 --- a/gdb/python/python.c +++ b/gdb/python/python.c @@ -112,7 +112,8 @@ static void gdbpy_start_type_printers (const struct extension_language_defn *, struct ext_lang_type_printers *); static enum ext_lang_rc gdbpy_apply_type_printers (const struct extension_language_defn *, - const struct ext_lang_type_printers *, struct type *, char **); + const struct ext_lang_type_printers *, struct type *, + gdb::unique_xmalloc_ptr *); static void gdbpy_free_type_printers (const struct extension_language_defn *, struct ext_lang_type_printers *); static void gdbpy_set_quit_flag (const struct extension_language_defn *); @@ -1700,7 +1701,7 @@ gdbpy_start_type_printers (const struct extension_language_defn *extlang, /* If TYPE is recognized by some type printer, store in *PRETTIED_TYPE a newly allocated string holding the type's replacement name, and return - EXT_LANG_RC_OK. The caller is responsible for freeing the string. + EXT_LANG_RC_OK. If there's a Python error return EXT_LANG_RC_ERROR. Otherwise, return EXT_LANG_RC_NOP. This is the extension_language_ops.apply_type_printers "method". */ @@ -1708,7 +1709,8 @@ gdbpy_start_type_printers (const struct extension_language_defn *extlang, static enum ext_lang_rc gdbpy_apply_type_printers (const struct extension_language_defn *extlang, const struct ext_lang_type_printers *ext_printers, - struct type *type, char **prettied_type) + struct type *type, + gdb::unique_xmalloc_ptr *prettied_type) { PyObject *printers_obj = (PyObject *) ext_printers->py_type_printers; gdb::unique_xmalloc_ptr result; @@ -1763,7 +1765,7 @@ gdbpy_apply_type_printers (const struct extension_language_defn *extlang, return EXT_LANG_RC_ERROR; } - *prettied_type = result.release (); + *prettied_type = std::move (result); return EXT_LANG_RC_OK; } diff --git a/gdb/typeprint.c b/gdb/typeprint.c index f59a7c4f107..06ecd2c4776 100644 --- a/gdb/typeprint.c +++ b/gdb/typeprint.c @@ -309,7 +309,6 @@ const char * typedef_hash_table::find_global_typedef (const struct type_print_options *flags, struct type *t) { - char *applied; void **slot; struct decl_field tf, *new_tf; @@ -334,14 +333,12 @@ typedef_hash_table::find_global_typedef (const struct type_print_options *flags, *slot = new_tf; - applied = apply_ext_lang_type_printers (flags->global_printers, t); + gdb::unique_xmalloc_ptr applied + = apply_ext_lang_type_printers (flags->global_printers, t); - if (applied != NULL) - { - new_tf->name = obstack_strdup (&flags->global_typedefs->m_storage, - applied); - xfree (applied); - } + if (applied != nullptr) + new_tf->name = obstack_strdup (&flags->global_typedefs->m_storage, + applied.get ()); return new_tf->name; }