From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id jEquLWaRZmQ8+A0AWB0awg (envelope-from ) for ; Thu, 18 May 2023 16:58:14 -0400 Received: by simark.ca (Postfix, from userid 112) id A4B0A1E11E; Thu, 18 May 2023 16:58:14 -0400 (EDT) Authentication-Results: simark.ca; dkim=pass (1024-bit key; secure) header.d=sourceware.org header.i=@sourceware.org header.a=rsa-sha256 header.s=default header.b=tJCPrZ6L; dkim-atps=neutral X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on simark.ca X-Spam-Level: X-Spam-Status: No, score=-8.0 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI, URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.6 Received: from 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 RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by simark.ca (Postfix) with ESMTPS id 22AE81E0D4 for ; Thu, 18 May 2023 16:58:14 -0400 (EDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 25F833857009 for ; Thu, 18 May 2023 20:58:13 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 25F833857009 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1684443493; bh=0RPm+NAQMAQlFuuIQN3gUmzJ5M7JutG5JbTpYIPwYVk=; h=To:Cc:Subject:Date:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=tJCPrZ6Ld3X1TLP1ucPhVpslyjQ3EVHP6vtqp2shd+J2cnVosckvhZKxFmAoFXuYZ C2mBaI9pvbbWV5pQpGroYEBMGJMnXpXe+UuBv0VskW8A/C36gi3JzQKQmWlMzX1pNw oHmNPQJCYN9oWKClFdSh66siw1Ng4aO9LbMMnpkg= Received: from simark.ca (simark.ca [158.69.221.121]) by sourceware.org (Postfix) with ESMTPS id 0340E3857016 for ; Thu, 18 May 2023 20:57:38 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 0340E3857016 Received: from localhost.localdomain (unknown [167.248.160.41]) (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 simark.ca (Postfix) with ESMTPSA id A08A91E0D4; Thu, 18 May 2023 16:57:38 -0400 (EDT) To: gdb-patches@sourceware.org Cc: Simon Marchi Subject: [PATCH] gdbsupport: add support for references to checked_static_cast Date: Thu, 18 May 2023 16:57:37 -0400 Message-Id: <20230518205737.403656-1-simon.marchi@efficios.com> X-Mailer: git-send-email 2.40.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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: , From: Simon Marchi via Gdb-patches Reply-To: Simon Marchi Errors-To: gdb-patches-bounces+public-inbox=simark.ca@sourceware.org Sender: "Gdb-patches" Add a checked_static_cast overload that works with references. A bad dynamic cast with references throws std::bad_cast, it would be possible to implement the new overload based on that, but it seemed simpler to just piggy back off the existing function. I found some potential uses of this new overload in amd-dbgapi-target.c, update them to illustrate the use of the new overload. To build amd-dbgapi-target.c, on needs the amd-dbgapi library, which I don't expect many people to have. But I have it, and it builds fine here. I did test the new overload by making a purposely bad cast and it did catch it. Change-Id: Id6b6a7db09fe3b4aa43cddb60575ff5f46761e96 --- gdb/amdgpu-tdep.c | 28 +++++++++++++++++++--------- gdbsupport/gdb-checked-static-cast.h | 16 ++++++++++++++++ 2 files changed, 35 insertions(+), 9 deletions(-) diff --git a/gdb/amdgpu-tdep.c b/gdb/amdgpu-tdep.c index 1077fab5c65e..21a7a3ae1b79 100644 --- a/gdb/amdgpu-tdep.c +++ b/gdb/amdgpu-tdep.c @@ -674,7 +674,8 @@ amd_dbgapi_register_type_to_gdb_type (const amd_dbgapi_register_type &type, case amd_dbgapi_register_type::kind::INTEGER: { const auto &integer_type - = static_cast (type); + = gdb::checked_static_cast + (type); switch (integer_type.bit_size ()) { case 32: @@ -697,7 +698,8 @@ amd_dbgapi_register_type_to_gdb_type (const amd_dbgapi_register_type &type, case amd_dbgapi_register_type::kind::VECTOR: { const auto &vector_type - = static_cast (type); + = gdb::checked_static_cast + (type); struct type *element_type = amd_dbgapi_register_type_to_gdb_type (vector_type.element_type (), gdbarch); @@ -716,7 +718,8 @@ amd_dbgapi_register_type_to_gdb_type (const amd_dbgapi_register_type &type, case amd_dbgapi_register_type::kind::FLAGS: { const auto &flags_type - = static_cast (type); + = gdb::checked_static_cast + (type); struct type *gdb_type = arch_flags_type (gdbarch, flags_type.name ().c_str (), flags_type.bit_size ()); @@ -747,7 +750,8 @@ amd_dbgapi_register_type_to_gdb_type (const amd_dbgapi_register_type &type, case amd_dbgapi_register_type::kind::ENUM: { const auto &enum_type - = static_cast (type); + = gdb::checked_static_cast + (type); struct type *gdb_type = (type_allocator (gdbarch) .new_type (TYPE_CODE_ENUM, enum_type.bit_size (), @@ -1310,7 +1314,8 @@ amdgpu_register_type_parse_test () gdb_assert (type.kind () == amd_dbgapi_register_type::kind::FLAGS); - const auto &f = static_cast (type); + const auto &f + = gdb::checked_static_cast (type); gdb_assert (f.size () == 23); /* Check the two "FP_ROUND" fields. */ @@ -1322,7 +1327,8 @@ amdgpu_register_type_parse_test () == amd_dbgapi_register_type::kind::ENUM); const auto &e - = static_cast (*field.type); + = gdb::checked_static_cast + (*field.type); gdb_assert (e.size () == 4); gdb_assert (e[0].name == "NEAREST_EVEN"); gdb_assert (e[0].value == 0); @@ -1338,7 +1344,8 @@ amdgpu_register_type_parse_test () gdb_assert (f[22].type->kind () == amd_dbgapi_register_type::kind::INTEGER); const auto &i - = static_cast (*f[22].type); + = gdb::checked_static_cast + (*f[22].type); gdb_assert (i.bit_size () == 32); gdb_assert (i.is_unsigned ()); } @@ -1352,13 +1359,16 @@ amdgpu_register_type_parse_test () gdb_assert (type.kind () == amd_dbgapi_register_type::kind::VECTOR); - const auto &v = static_cast (type); + const auto &v + = gdb::checked_static_cast + (type); gdb_assert (v.count () == 64); const auto &et = v.element_type (); gdb_assert (et.kind () == amd_dbgapi_register_type::kind::INTEGER); - const auto &i = static_cast (et); + const auto &i + = gdb::checked_static_cast (et); gdb_assert (i.bit_size () == 32); gdb_assert (!i.is_unsigned ()); } diff --git a/gdbsupport/gdb-checked-static-cast.h b/gdbsupport/gdb-checked-static-cast.h index bc75244bddd0..7e5a69a6474d 100644 --- a/gdbsupport/gdb-checked-static-cast.h +++ b/gdbsupport/gdb-checked-static-cast.h @@ -66,6 +66,22 @@ checked_static_cast (V *v) return result; } +/* Same as the above, but to cast from a reference type to another. */ + +template +T +checked_static_cast (V &v) +{ + static_assert (std::is_reference::value, "target must be a reference type"); + + using T_no_R = typename std::remove_reference::type; + using T_P = typename std::add_pointer::type; + + using V_no_R = typename std::remove_reference::type; + + return *checked_static_cast (&v); +} + } #endif /* COMMON_GDB_CHECKED_DYNAMIC_CAST_H */ base-commit: c96452ad168cf42ad42f0d57214dddb38d5fae88 -- 2.40.1