]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
Simplify cp_print_class_member
authorTom Tromey <tom@tromey.com>
Sun, 23 Apr 2023 15:58:14 +0000 (09:58 -0600)
committerTom Tromey <tom@tromey.com>
Fri, 23 Jan 2026 21:18:50 +0000 (14:18 -0700)
I noticed that cp_print_class_member's calling convention can be
simplified.  In particular it seems cleaner for it to simply take a
value and a stream; and the "prefix" argument is only ever set to one
value.

This version also renames the function, to indicate a bit more clearly
what it actually does.

Regression tested on x86-64 Fedora 40.

Reviewed-By: Keith Seitz <keiths@redhat.com>
gdb/c-lang.h
gdb/cp-valprint.c
gdb/valprint.c

index 9796de437bcbea93cd0b3e22df937b74b106a022..7d9eaf8571bd991b2f21433f7efffc30bff03d06 100644 (file)
@@ -103,8 +103,7 @@ extern void c_type_print_base (struct type *, struct ui_file *,
 
 /* These are in cp-valprint.c */
 
-extern void cp_print_class_member (const gdb_byte *, struct type *,
-                                  struct ui_file *, const char *);
+extern void cp_print_class_memberptr (struct value *, struct ui_file *);
 
 extern void cp_print_value_fields (struct value *,
                                   struct ui_file *, int,
index 0ea8c52407951d648ff7f25a27d27260d1c58275..54b992b3b21e9473981b32d6a4184f2234a79960 100644 (file)
@@ -682,21 +682,17 @@ cp_find_class_member (struct type **self_p, int *fieldno,
 }
 
 void
-cp_print_class_member (const gdb_byte *valaddr, struct type *type,
-                      struct ui_file *stream, const char *prefix)
+cp_print_class_memberptr (struct value *val, struct ui_file *stream)
 {
+  struct type *type = check_typedef (val->type ());
   enum bfd_endian byte_order = type_byte_order (type);
 
-  /* VAL is a byte offset into the structure type SELF_TYPE.
-     Find the name of the field for that offset and
-     print it.  */
   struct type *self_type = TYPE_SELF_TYPE (type);
-  LONGEST val;
   int fieldno;
 
-  val = extract_signed_integer (valaddr,
-                               type->length (),
-                               byte_order);
+  const gdb_byte *valaddr = val->contents_for_printing ().data ();
+  LONGEST pos = extract_signed_integer (valaddr, type->length (),
+                                       byte_order);
 
   /* Pointers to data members are usually byte offsets into an object.
      Because a data member can have offset zero, and a NULL pointer to
@@ -708,19 +704,19 @@ cp_print_class_member (const gdb_byte *valaddr, struct type *type,
      value.  GDB only supports that last form; to add support for
      another form, make this into a cp-abi hook.  */
 
-  if (val == -1)
+  if (pos == -1)
     {
       gdb_printf (stream, "NULL");
       return;
     }
 
-  cp_find_class_member (&self_type, &fieldno, val << 3);
+  cp_find_class_member (&self_type, &fieldno, 8 * pos);
 
   if (self_type != NULL)
     {
       const char *name;
 
-      gdb_puts (prefix, stream);
+      gdb_puts ("&", stream);
       name = self_type->name ();
       if (name)
        gdb_puts (name, stream);
@@ -731,7 +727,7 @@ cp_print_class_member (const gdb_byte *valaddr, struct type *type,
                    variable_name_style.style (), stream);
     }
   else
-    gdb_printf (stream, "%ld", (long) val);
+    gdb_printf (stream, "%s", plongest (pos));
 }
 
 #if GDB_SELF_TEST
index b37b494dec67d1c30a1712f984166690b4bd45f4..cd50f3c31c1451baec891feaca1eb64b255dac54 100644 (file)
@@ -965,9 +965,7 @@ generic_value_print_memberptr
     {
       /* Member pointers are essentially specific to C++, and so if we
         encounter one, we should print it according to C++ rules.  */
-      struct type *type = check_typedef (val->type ());
-      const gdb_byte *valaddr = val->contents_for_printing ().data ();
-      cp_print_class_member (valaddr, type, stream, "&");
+      cp_print_class_memberptr (val, stream);
     }
   else
     value_print_scalar_formatted (val, options, 0, stream);