public inbox for gdb-cvs@sourceware.org
help / color / mirror / Atom feed
* [binutils-gdb] Use checked_static_cast in more places
@ 2022-09-12 20:26 Tom Tromey
  0 siblings, 0 replies; 2+ messages in thread
From: Tom Tromey @ 2022-09-12 20:26 UTC (permalink / raw)
  To: gdb-cvs

https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=5f48d886a9ce0b7d109a26c860adf66330ca65cf

commit 5f48d886a9ce0b7d109a26c860adf66330ca65cf
Author: Tom Tromey <tromey@adacore.com>
Date:   Wed Aug 17 11:47:17 2022 -0600

    Use checked_static_cast in more places
    
    I went through all the uses of dynamic_cast<> in gdb, looking for ones
    that could be replaced with checked_static_cast.  This patch is the
    result.  Regression tested on x86-64 Fedora 34.

Diff:
---
 gdb/ada-exp.y    | 4 +---
 gdb/ax-gdb.c     | 6 ++----
 gdb/disasm.c     | 3 +--
 gdb/location.c   | 4 ++--
 gdb/mi/mi-out.c  | 7 ++-----
 gdb/tracepoint.c | 8 ++++----
 gdb/tui/tui-io.c | 4 ++--
 7 files changed, 14 insertions(+), 22 deletions(-)

diff --git a/gdb/ada-exp.y b/gdb/ada-exp.y
index 8660205809a..78aec4e920b 100644
--- a/gdb/ada-exp.y
+++ b/gdb/ada-exp.y
@@ -342,9 +342,7 @@ static ada_choices_component *
 choice_component ()
 {
   ada_component *last = components.back ().get ();
-  ada_choices_component *result = dynamic_cast<ada_choices_component *> (last);
-  gdb_assert (result != nullptr);
-  return result;
+  return gdb::checked_static_cast<ada_choices_component *> (last);
 }
 
 /* Pop the most recent component from the global stack, and return
diff --git a/gdb/ax-gdb.c b/gdb/ax-gdb.c
index 1fccfde559e..0d2566f1518 100644
--- a/gdb/ax-gdb.c
+++ b/gdb/ax-gdb.c
@@ -1920,8 +1920,7 @@ assign_operation::do_generate_ax (struct expression *exp,
     error (_("May only assign to trace state variables"));
 
   internalvar_operation *ivarop
-    = dynamic_cast<internalvar_operation *> (subop);
-  gdb_assert (ivarop != nullptr);
+    = gdb::checked_static_cast<internalvar_operation *> (subop);
 
   const char *name = internalvar_name (ivarop->get_internalvar ());
   struct trace_state_variable *tsv;
@@ -1950,8 +1949,7 @@ assign_modify_operation::do_generate_ax (struct expression *exp,
     error (_("May only assign to trace state variables"));
 
   internalvar_operation *ivarop
-    = dynamic_cast<internalvar_operation *> (subop);
-  gdb_assert (ivarop != nullptr);
+    = gdb::checked_static_cast<internalvar_operation *> (subop);
 
   const char *name = internalvar_name (ivarop->get_internalvar ());
   struct trace_state_variable *tsv;
diff --git a/gdb/disasm.c b/gdb/disasm.c
index fe4eed2d524..989120e05b1 100644
--- a/gdb/disasm.c
+++ b/gdb/disasm.c
@@ -246,8 +246,7 @@ gdb_printing_disassembler::stream_from_gdb_disassemble_info (void *dis_info)
 {
   gdb_disassemble_info *di = (gdb_disassemble_info *) dis_info;
   gdb_printing_disassembler *dis
-    = dynamic_cast<gdb_printing_disassembler *> (di);
-  gdb_assert (dis != nullptr);
+    = gdb::checked_static_cast<gdb_printing_disassembler *> (di);
   ui_file *stream = dis->stream ();
   gdb_assert (stream != nullptr);
   return stream;
diff --git a/gdb/location.c b/gdb/location.c
index 2b31baa4c85..e4863fdefce 100644
--- a/gdb/location.c
+++ b/gdb/location.c
@@ -18,6 +18,7 @@
 
 #include "defs.h"
 #include "gdbsupport/gdb_assert.h"
+#include "gdbsupport/gdb-checked-static-cast.h"
 #include "location.h"
 #include "symtab.h"
 #include "language.h"
@@ -848,8 +849,7 @@ string_to_location_spec (const char **stringp,
 	 linespec parsing below and discard the explicit location
 	 spec.  */
       explicit_location_spec *xloc
-	= dynamic_cast<explicit_location_spec *> (locspec.get ());
-      gdb_assert (xloc != nullptr);
+	= gdb::checked_static_cast<explicit_location_spec *> (locspec.get ());
       match_type = xloc->func_name_match_type;
     }
 
diff --git a/gdb/mi/mi-out.c b/gdb/mi/mi-out.c
index eedc5f70549..96a847eb6b6 100644
--- a/gdb/mi/mi-out.c
+++ b/gdb/mi/mi-out.c
@@ -27,6 +27,7 @@
 #include "interps.h"
 #include "ui-out.h"
 #include "utils.h"
+#include "gdbsupport/gdb-checked-static-cast.h"
 
 /* Mark beginning of a table.  */
 
@@ -327,11 +328,7 @@ mi_out_new (const char *mi_version)
 static mi_ui_out *
 as_mi_ui_out (ui_out *uiout)
 {
-  mi_ui_out *mi_uiout = dynamic_cast<mi_ui_out *> (uiout);
-
-  gdb_assert (mi_uiout != NULL);
-
-  return mi_uiout;
+  return gdb::checked_static_cast<mi_ui_out *> (uiout);
 }
 
 int
diff --git a/gdb/tracepoint.c b/gdb/tracepoint.c
index 75ac0cef3b0..dfb62dcf62c 100644
--- a/gdb/tracepoint.c
+++ b/gdb/tracepoint.c
@@ -687,7 +687,7 @@ validate_actionline (const char *line, struct breakpoint *b)
 		{
 		  symbol *sym;
 		  expr::var_value_operation *vvop
-		    = (dynamic_cast<expr::var_value_operation *>
+		    = (gdb::checked_static_cast<expr::var_value_operation *>
 		       (exp->op.get ()));
 		  sym = vvop->get_symbol ();
 
@@ -1357,7 +1357,7 @@ encode_actions_1 (struct command_line *action,
 		    case OP_REGISTER:
 		      {
 			expr::register_operation *regop
-			  = (dynamic_cast<expr::register_operation *>
+			  = (gdb::checked_static_cast<expr::register_operation *>
 			     (exp->op.get ()));
 			const char *name = regop->get_name ();
 
@@ -1380,7 +1380,7 @@ encode_actions_1 (struct command_line *action,
 			tempval = evaluate_expression (exp.get ());
 			addr = value_address (tempval);
 			expr::unop_memval_operation *memop
-			  = (dynamic_cast<expr::unop_memval_operation *>
+			  = (gdb::checked_static_cast<expr::unop_memval_operation *>
 			     (exp->op.get ()));
 			struct type *type = memop->get_type ();
 			/* Initialize the TYPE_LENGTH if it is a typedef.  */
@@ -1397,7 +1397,7 @@ encode_actions_1 (struct command_line *action,
 		    case OP_VAR_VALUE:
 		      {
 			expr::var_value_operation *vvo
-			  = (dynamic_cast<expr::var_value_operation *>
+			  = (gdb::checked_static_cast<expr::var_value_operation *>
 			     (exp->op.get ()));
 			struct symbol *sym = vvo->get_symbol ();
 			const char *name = sym->natural_name ();
diff --git a/gdb/tui/tui-io.c b/gdb/tui/tui-io.c
index 0efaf69335c..a30000ef626 100644
--- a/gdb/tui/tui-io.c
+++ b/gdb/tui/tui-io.c
@@ -45,6 +45,7 @@
 #include "gdb_curses.h"
 #include <map>
 #include "pager.h"
+#include "gdbsupport/gdb-checked-static-cast.h"
 
 /* This redefines CTRL if it is not already defined, so it must come
    after terminal state releated include files like <term.h> and
@@ -832,8 +833,7 @@ tui_setup_io (int mode)
       tui_old_stdout = gdb_stdout;
       tui_old_stderr = gdb_stderr;
       tui_old_stdlog = gdb_stdlog;
-      tui_old_uiout = dynamic_cast<cli_ui_out *> (current_uiout);
-      gdb_assert (tui_old_uiout != nullptr);
+      tui_old_uiout = gdb::checked_static_cast<cli_ui_out *> (current_uiout);
 
       /* Reconfigure gdb output.  */
       gdb_stdout = tui_stdout;

^ permalink raw reply	[flat|nested] 2+ messages in thread
* [binutils-gdb] Use checked_static_cast in more places
@ 2022-10-16 18:17 Tom Tromey
  0 siblings, 0 replies; 2+ messages in thread
From: Tom Tromey @ 2022-10-16 18:17 UTC (permalink / raw)
  To: gdb-cvs

https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=98ed24fb35d89eb20179edf6c12f599c7a9e228e

commit 98ed24fb35d89eb20179edf6c12f599c7a9e228e
Author: Tom Tromey <tom@tromey.com>
Date:   Sat Sep 24 13:34:09 2022 -0600

    Use checked_static_cast in more places
    
    I looked through all the uses of static_cast<... *> in gdb and
    converted many of them to checked_static_cast.
    
    I couldn't test a few of these changes.

Diff:
---
 gdb/aix-thread.c             |  2 +-
 gdb/breakpoint.c             |  9 +++++----
 gdb/darwin-nat.h             |  4 ++--
 gdb/dwarf2/read.c            | 14 +++++++-------
 gdb/linux-thread-db.c        |  2 +-
 gdb/location.c               | 10 +++++-----
 gdb/nto-tdep.h               |  3 ++-
 gdb/process-stratum-target.h |  3 ++-
 gdb/remote.c                 |  4 ++--
 9 files changed, 27 insertions(+), 24 deletions(-)

diff --git a/gdb/aix-thread.c b/gdb/aix-thread.c
index 57e5756e144..e556c153576 100644
--- a/gdb/aix-thread.c
+++ b/gdb/aix-thread.c
@@ -90,7 +90,7 @@ struct aix_thread_info : public private_thread_info
 static aix_thread_info *
 get_aix_thread_info (thread_info *thread)
 {
-  return static_cast<aix_thread_info *> (thread->priv.get ());
+  return gdb::checked_static_cast<aix_thread_info *> (thread->priv.get ());
 }
 
 /* Information about a thread of which libpthdebug is aware.  */
diff --git a/gdb/breakpoint.c b/gdb/breakpoint.c
index f6591d43871..91e13a03360 100644
--- a/gdb/breakpoint.c
+++ b/gdb/breakpoint.c
@@ -997,7 +997,7 @@ set_breakpoint_condition (struct breakpoint *b, const char *exp,
       b->cond_string.reset ();
 
       if (is_watchpoint (b))
-	static_cast<watchpoint *> (b)->cond_exp.reset ();
+	gdb::checked_static_cast<watchpoint *> (b)->cond_exp.reset ();
       else
 	{
 	  int loc_num = 1;
@@ -1029,7 +1029,7 @@ set_breakpoint_condition (struct breakpoint *b, const char *exp,
 	  expression_up new_exp = parse_exp_1 (&arg, 0, 0, 0, &tracker);
 	  if (*arg != 0)
 	    error (_("Junk at end of expression"));
-	  watchpoint *w = static_cast<watchpoint *> (b);
+	  watchpoint *w = gdb::checked_static_cast<watchpoint *> (b);
 	  w->cond_exp = std::move (new_exp);
 	  w->cond_exp_valid_block = tracker.block ();
 	}
@@ -8312,7 +8312,7 @@ code_breakpoint::code_breakpoint (struct gdbarch *gdbarch_,
   if (type == bp_static_tracepoint
       || type == bp_static_marker_tracepoint)
     {
-      auto *t = static_cast<struct tracepoint *> (this);
+      auto *t = gdb::checked_static_cast<struct tracepoint *> (this);
       struct static_tracepoint_marker marker;
 
       if (strace_marker_p (this))
@@ -13502,7 +13502,8 @@ insert_single_step_breakpoint (struct gdbarch *gdbarch,
   sal.explicit_pc = 1;
 
   auto *ss_bp
-    = static_cast<momentary_breakpoint *> (tp->control.single_step_breakpoints);
+    = (gdb::checked_static_cast<momentary_breakpoint *>
+       (tp->control.single_step_breakpoints));
   ss_bp->add_location (sal);
 
   update_global_location_list (UGLL_INSERT);
diff --git a/gdb/darwin-nat.h b/gdb/darwin-nat.h
index e3dcd963706..f8a14981aee 100644
--- a/gdb/darwin-nat.h
+++ b/gdb/darwin-nat.h
@@ -154,7 +154,7 @@ struct darwin_exception_info
 static inline darwin_thread_info *
 get_darwin_thread_info (class thread_info *thread)
 {
-  return static_cast<darwin_thread_info *> (thread->priv.get ());
+  return gdb::checked_static_cast<darwin_thread_info *> (thread->priv.get ());
 }
 
 /* Describe an inferior.  */
@@ -188,7 +188,7 @@ struct darwin_inferior : public private_inferior
 static inline darwin_inferior *
 get_darwin_inferior (inferior *inf)
 {
-  return static_cast<darwin_inferior *> (inf->priv.get ());
+  return gdb::checked_static_cast<darwin_inferior *> (inf->priv.get ());
 }
 
 /* Exception port.  */
diff --git a/gdb/dwarf2/read.c b/gdb/dwarf2/read.c
index 78f4cc1f60d..3f71ebd1d66 100644
--- a/gdb/dwarf2/read.c
+++ b/gdb/dwarf2/read.c
@@ -3102,7 +3102,7 @@ dwarf2_gdb_index::dump (struct objfile *objfile)
 {
   dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
 
-  mapped_index *index = (static_cast<mapped_index *>
+  mapped_index *index = (gdb::checked_static_cast<mapped_index *>
 			 (per_objfile->per_bfd->index_table.get ()));
   gdb_printf (".gdb_index: version %d\n", index->version);
   gdb_printf ("\n");
@@ -18576,11 +18576,11 @@ dwarf2_per_cu_data *
 cooked_index_functions::find_per_cu (dwarf2_per_bfd *per_bfd,
 				     CORE_ADDR adjusted_pc)
 {
+  if (per_bfd->index_table == nullptr)
+    return nullptr;
   cooked_index_vector *table
-    = (static_cast<cooked_index_vector *>
+    = (gdb::checked_static_cast<cooked_index_vector *>
        (per_bfd->index_table.get ()));
-  if (table == nullptr)
-    return nullptr;
   return table->lookup (adjusted_pc);
 }
 
@@ -18597,7 +18597,7 @@ cooked_index_functions::find_compunit_symtab_by_address
 
   CORE_ADDR baseaddr = objfile->data_section_offset ();
   cooked_index_vector *table
-    = (static_cast<cooked_index_vector *>
+    = (gdb::checked_static_cast<cooked_index_vector *>
        (per_objfile->per_bfd->index_table.get ()));
   dwarf2_per_cu_data *per_cu = table->lookup (address - baseaddr);
   if (per_cu == nullptr)
@@ -18625,7 +18625,7 @@ cooked_index_functions::expand_matching_symbols
     = lang->get_symbol_name_matcher (lookup_name);
 
   cooked_index_vector *table
-    = (static_cast<cooked_index_vector *>
+    = (gdb::checked_static_cast<cooked_index_vector *>
        (per_objfile->per_bfd->index_table.get ()));
   for (const cooked_index_entry *entry : table->all_entries ())
     {
@@ -18657,7 +18657,7 @@ cooked_index_functions::expand_symtabs_matching
     return true;
 
   cooked_index_vector *table
-    = (static_cast<cooked_index_vector *>
+    = (gdb::checked_static_cast<cooked_index_vector *>
        (per_objfile->per_bfd->index_table.get ()));
   table->wait ();
 
diff --git a/gdb/linux-thread-db.c b/gdb/linux-thread-db.c
index 11da82a1abc..839271c01c5 100644
--- a/gdb/linux-thread-db.c
+++ b/gdb/linux-thread-db.c
@@ -317,7 +317,7 @@ struct thread_db_thread_info : public private_thread_info
 static thread_db_thread_info *
 get_thread_db_thread_info (thread_info *thread)
 {
-  return static_cast<thread_db_thread_info *> (thread->priv.get ());
+  return gdb::checked_static_cast<thread_db_thread_info *> (thread->priv.get ());
 }
 
 static const char *
diff --git a/gdb/location.c b/gdb/location.c
index e4863fdefce..ba86b7c48f7 100644
--- a/gdb/location.c
+++ b/gdb/location.c
@@ -221,7 +221,7 @@ const linespec_location_spec *
 as_linespec_location_spec (const location_spec *locspec)
 {
   gdb_assert (locspec->type () == LINESPEC_LOCATION_SPEC);
-  return static_cast<const linespec_location_spec *> (locspec);
+  return gdb::checked_static_cast<const linespec_location_spec *> (locspec);
 }
 
 /* See description in location.h.  */
@@ -240,7 +240,7 @@ const address_location_spec *
 as_address_location_spec (const location_spec *locspec)
 {
   gdb_assert (locspec->type () == ADDRESS_LOCATION_SPEC);
-  return static_cast<const address_location_spec *> (locspec);
+  return gdb::checked_static_cast<const address_location_spec *> (locspec);
 }
 
 /* See description in location.h.  */
@@ -257,7 +257,7 @@ const probe_location_spec *
 as_probe_location_spec (const location_spec *locspec)
 {
   gdb_assert (locspec->type () == PROBE_LOCATION_SPEC);
-  return static_cast<const probe_location_spec *> (locspec);
+  return gdb::checked_static_cast<const probe_location_spec *> (locspec);
 }
 
 /* See description in location.h.  */
@@ -266,7 +266,7 @@ const explicit_location_spec *
 as_explicit_location_spec (const location_spec *locspec)
 {
   gdb_assert (locspec->type () == EXPLICIT_LOCATION_SPEC);
-  return static_cast<const explicit_location_spec *> (locspec);
+  return gdb::checked_static_cast<const explicit_location_spec *> (locspec);
 }
 
 /* See description in location.h.  */
@@ -275,7 +275,7 @@ explicit_location_spec *
 as_explicit_location_spec (location_spec *locspec)
 {
   gdb_assert (locspec->type () == EXPLICIT_LOCATION_SPEC);
-  return static_cast<explicit_location_spec *> (locspec);
+  return gdb::checked_static_cast<explicit_location_spec *> (locspec);
 }
 
 /* Return a string representation of the explicit location spec in
diff --git a/gdb/nto-tdep.h b/gdb/nto-tdep.h
index f176b70df27..e97eee0708b 100644
--- a/gdb/nto-tdep.h
+++ b/gdb/nto-tdep.h
@@ -26,6 +26,7 @@
 #include "osabi.h"
 #include "regset.h"
 #include "gdbthread.h"
+#include "gdbsupport/gdb-checked-static-cast.h"
 
 /* Target operations defined for Neutrino targets (<target>-nto-tdep.c).  */
 
@@ -145,7 +146,7 @@ struct nto_thread_info : public private_thread_info
 static inline nto_thread_info *
 get_nto_thread_info (thread_info *thread)
 {
-  return static_cast<nto_thread_info *> (thread->priv.get ());
+  return gdb::checked_static_cast<nto_thread_info *> (thread->priv.get ());
 }
 
 /* Per-inferior data, common for both procfs and remote.  */
diff --git a/gdb/process-stratum-target.h b/gdb/process-stratum-target.h
index bb8e39f53a3..62f56166f5e 100644
--- a/gdb/process-stratum-target.h
+++ b/gdb/process-stratum-target.h
@@ -23,6 +23,7 @@
 #include "target.h"
 #include <set>
 #include "gdbsupport/intrusive_list.h"
+#include "gdbsupport/gdb-checked-static-cast.h"
 #include "gdbthread.h"
 
 /* Abstract base class inherited by all process_stratum targets.  */
@@ -160,7 +161,7 @@ static inline process_stratum_target *
 as_process_stratum_target (target_ops *target)
 {
   gdb_assert (target->stratum () == process_stratum);
-  return static_cast<process_stratum_target *> (target);
+  return gdb::checked_static_cast<process_stratum_target *> (target);
 }
 
 /* Return a collection of targets that have non-exited inferiors.  */
diff --git a/gdb/remote.c b/gdb/remote.c
index 2f6cb2d01ee..17c2d17c8fe 100644
--- a/gdb/remote.c
+++ b/gdb/remote.c
@@ -2694,7 +2694,7 @@ get_remote_thread_info (thread_info *thread)
   if (thread->priv == NULL)
     thread->priv.reset (new remote_thread_info);
 
-  return static_cast<remote_thread_info *> (thread->priv.get ());
+  return gdb::checked_static_cast<remote_thread_info *> (thread->priv.get ());
 }
 
 /* Return PTID's private thread data, creating it if necessary.  */
@@ -6611,7 +6611,7 @@ get_remote_inferior (inferior *inf)
   if (inf->priv == NULL)
     inf->priv.reset (new remote_inferior);
 
-  return static_cast<remote_inferior *> (inf->priv.get ());
+  return gdb::checked_static_cast<remote_inferior *> (inf->priv.get ());
 }
 
 /* Class used to track the construction of a vCont packet in the

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

end of thread, other threads:[~2022-10-16 18:17 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-09-12 20:26 [binutils-gdb] Use checked_static_cast in more places Tom Tromey
2022-10-16 18:17 Tom Tromey

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).