public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
* [PATCH] gdbsupport: add support for references to checked_static_cast
@ 2023-05-18 20:57 Simon Marchi
  2023-05-19 21:31 ` Kevin Buettner
                   ` (2 more replies)
  0 siblings, 3 replies; 11+ messages in thread
From: Simon Marchi @ 2023-05-18 20:57 UTC (permalink / raw)
  To: gdb-patches; +Cc: Simon Marchi

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<const amd_dbgapi_register_type_integer &> (type);
+	  = gdb::checked_static_cast<const amd_dbgapi_register_type_integer &>
+	      (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<const amd_dbgapi_register_type_vector &> (type);
+	  = gdb::checked_static_cast<const amd_dbgapi_register_type_vector &>
+	      (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<const amd_dbgapi_register_type_flags &> (type);
+	  = gdb::checked_static_cast<const amd_dbgapi_register_type_flags &>
+	      (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<const amd_dbgapi_register_type_enum &> (type);
+	  = gdb::checked_static_cast<const amd_dbgapi_register_type_enum &>
+	      (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<const amd_dbgapi_register_type_flags &> (type);
+    const auto &f
+      = gdb::checked_static_cast<const amd_dbgapi_register_type_flags &> (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<const amd_dbgapi_register_type_enum &> (*field.type);
+	    = gdb::checked_static_cast<const amd_dbgapi_register_type_enum &>
+	      (*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<const amd_dbgapi_register_type_integer &> (*f[22].type);
+      = gdb::checked_static_cast<const amd_dbgapi_register_type_integer &>
+	  (*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<const amd_dbgapi_register_type_vector &> (type);
+    const auto &v
+      = gdb::checked_static_cast<const amd_dbgapi_register_type_vector &>
+	  (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<const amd_dbgapi_register_type_integer &> (et);
+    const auto &i
+      = gdb::checked_static_cast<const amd_dbgapi_register_type_integer &> (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<typename T, typename V>
+T
+checked_static_cast (V &v)
+{
+  static_assert (std::is_reference<T>::value, "target must be a reference type");
+
+  using T_no_R = typename std::remove_reference<T>::type;
+  using T_P = typename std::add_pointer<T_no_R>::type;
+
+  using V_no_R = typename std::remove_reference<V>::type;
+
+  return *checked_static_cast<T_P, V_no_R> (&v);
+}
+
 }
 
 #endif /* COMMON_GDB_CHECKED_DYNAMIC_CAST_H */

base-commit: c96452ad168cf42ad42f0d57214dddb38d5fae88
-- 
2.40.1


^ permalink raw reply	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2023-05-24 18:54 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-05-18 20:57 [PATCH] gdbsupport: add support for references to checked_static_cast Simon Marchi
2023-05-19 21:31 ` Kevin Buettner
2023-05-23 12:03   ` Simon Marchi
2023-05-24 13:07 ` Andrew Burgess
2023-05-24 14:51   ` Simon Marchi
2023-05-24 18:04     ` Andrew Burgess
2023-05-24 18:54       ` Simon Marchi
2023-05-24 13:22 ` Lancelot SIX
2023-05-24 14:51   ` Simon Marchi
2023-05-24 15:33     ` Lancelot SIX
2023-05-24 15:44       ` Simon Marchi

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).