From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id C08F33858C54 for ; Wed, 24 May 2023 13:07:20 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org C08F33858C54 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1684933640; 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: in-reply-to:in-reply-to:references:references; bh=f447WVK11wwDRuaXHszQSU8wot1jQTbVZIBzCGpKNLc=; b=GL1PIx2xqrCVDG+rAnbOT9VNVon0nIwKvwM278K83OycQ2b1jL23VP8hExt5S2XHciCoby ski+qnhhsDwpB+uNSR7f4qgyYcRus1eiK+cnPL43kwYz2IJy1synPJxwq/SjUM18ngYWpz H+OxhrrRW6Epmi1XGkTBuyW9tSgKQPQ= Received: from mail-wm1-f72.google.com (mail-wm1-f72.google.com [209.85.128.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-399-CoXlM7EuOMiu80hmbtC09Q-1; Wed, 24 May 2023 09:07:19 -0400 X-MC-Unique: CoXlM7EuOMiu80hmbtC09Q-1 Received: by mail-wm1-f72.google.com with SMTP id 5b1f17b1804b1-3f60d900598so6220705e9.0 for ; Wed, 24 May 2023 06:07:18 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1684933637; x=1687525637; h=mime-version:message-id:date:references:in-reply-to:subject:cc:to :from:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=f447WVK11wwDRuaXHszQSU8wot1jQTbVZIBzCGpKNLc=; b=TVZTLNGQY8b23PhiFpj8vbjWcNrlgyqcZ0yF3DvdbWVKv03AmgH1PLDpK1tOQ9A6pJ ugUGfkKLReTqCuyPjFBy+TubYKzOBEls7u4kwaj8HHHXhMAMwDomYCzFJDG676Y3lNLd 7c6MjbE+4uE9W24x3Tz3m/rx12fzk86cYOOa3JuqUkNvWtyfv75OPi5ShBYHnJpMO8Q9 K4csMNNqVd+rXU483niMOY33ZFrWR6TF6X1WRd7pctjOPuBG/s3sv6UkfCmLmap75Eng xMmlJUvYkd+5Leeg297nr22L5FbwsyU8K7qka0zfMFoQS80dv86ImQwwd2dMMAlSiSSD S70A== X-Gm-Message-State: AC+VfDyQSc0uuOBldrwcd3t7FR9OIl62JH/S3Ex9eDl1gD+hdiKjjDfX V1W2zjTgmXEc2gerlHv7eOXLzH67D6h2hPRG0cisPDTrqobUmbkL7QUzmjv4TvkO+qPF7N+Rzk3 nPVh+lnEYLJsJHOhRCptu+ZD/cazhZnMpKr9euApbKrAHYYS2aBZzdiZfwQDNCjyFCjuQht/PHa Lu2jFRqA== X-Received: by 2002:adf:fa51:0:b0:2fa:26cc:71f0 with SMTP id y17-20020adffa51000000b002fa26cc71f0mr13667677wrr.10.1684933637601; Wed, 24 May 2023 06:07:17 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ6c5+9XavDbYZjYVMNs0F8QJFPG0QwFs2UKyXfnftm8UCUT+lgIiuFWiQCLnBeHmPAzo5+Q6Q== X-Received: by 2002:adf:fa51:0:b0:2fa:26cc:71f0 with SMTP id y17-20020adffa51000000b002fa26cc71f0mr13667660wrr.10.1684933637203; Wed, 24 May 2023 06:07:17 -0700 (PDT) Received: from localhost (11.72.115.87.dyn.plus.net. [87.115.72.11]) by smtp.gmail.com with ESMTPSA id i18-20020a5d5232000000b002ff2c39d072sm14264950wra.104.2023.05.24.06.07.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 24 May 2023 06:07:16 -0700 (PDT) From: Andrew Burgess To: Simon Marchi via Gdb-patches , gdb-patches@sourceware.org Cc: Simon Marchi Subject: Re: [PATCH] gdbsupport: add support for references to checked_static_cast In-Reply-To: <20230518205737.403656-1-simon.marchi@efficios.com> References: <20230518205737.403656-1-simon.marchi@efficios.com> Date: Wed, 24 May 2023 14:07:14 +0100 Message-ID: <878rdd7u9p.fsf@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain 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_H2,SPF_HELO_NONE,SPF_NONE,TXREP,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: Simon Marchi via Gdb-patches writes: > 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. Looks great. Thanks for expanding this feature. Reviewed-By: Andrew Burgess Thanks, Andrew > > 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