gdb: remove the silent parameter from exit_inferior_1 and cleanup
[binutils-gdb.git] / gdb / gdbarch-selftests.c
index 68d340f81ead01b9a89d409d265640f1a51c2dd9..cc9604b9bf7e1b0b39c4753e11c999945163967a 100644 (file)
@@ -1,6 +1,6 @@
 /* Self tests for gdbarch for GDB, the GNU debugger.
 
-   Copyright (C) 2017-2022 Free Software Foundation, Inc.
+   Copyright (C) 2017-2023 Free Software Foundation, Inc.
 
    This file is part of GDB.
 
@@ -27,6 +27,8 @@
 #include "gdbarch.h"
 #include "scoped-mock-context.h"
 
+#include <map>
+
 namespace selftests {
 
 /* Test gdbarch methods register_to_value and value_to_register.  */
@@ -71,7 +73,7 @@ register_to_value_test (struct gdbarch *gdbarch)
 
   scoped_mock_context<test_target_ops> mockctx (gdbarch);
 
-  struct frame_info *frame = get_current_frame ();
+  frame_info_ptr frame = get_current_frame ();
   const int num_regs = gdbarch_num_cooked_regs (gdbarch);
 
   /* Test gdbarch methods register_to_value and value_to_register with
@@ -122,6 +124,47 @@ register_to_value_test (struct gdbarch *gdbarch)
     }
 }
 
+/* Test function gdbarch_register_name.  */
+
+static void
+register_name_test (struct gdbarch *gdbarch)
+{
+  scoped_mock_context<test_target_ops> mockctx (gdbarch);
+
+  /* Track the number of times each register name appears.  */
+  std::map<const std::string, int> name_counts;
+
+  const int num_regs = gdbarch_num_cooked_regs (gdbarch);
+  for (auto regnum = 0; regnum < num_regs; regnum++)
+    {
+      /* If a register is to be hidden from the user then we should get
+        back an empty string, not nullptr.  Every other register should
+        return a non-empty string.  */
+      const char *name = gdbarch_register_name (gdbarch, regnum);
+
+      if (run_verbose() && name == nullptr)
+       debug_printf ("arch: %s, register: %d returned nullptr\n",
+                     gdbarch_bfd_arch_info (gdbarch)->printable_name,
+                     regnum);
+      SELF_CHECK (name != nullptr);
+
+      /* Every register name, that is not the empty string, should be
+        unique.  If this is not the case then the user will see duplicate
+        copies of the register in e.g. 'info registers' output, but will
+        only be able to interact with one of the copies.  */
+      if (*name != '\0')
+       {
+         std::string s (name);
+         name_counts[s]++;
+         if (run_verbose() && name_counts[s] > 1)
+           debug_printf ("arch: %s, register: %d (%s) is a duplicate\n",
+                         gdbarch_bfd_arch_info (gdbarch)->printable_name,
+                         regnum, name);
+         SELF_CHECK (name_counts[s] == 1);
+       }
+    }
+}
+
 } // namespace selftests
 
 void _initialize_gdbarch_selftests ();
@@ -130,4 +173,7 @@ _initialize_gdbarch_selftests ()
 {
   selftests::register_test_foreach_arch ("register_to_value",
                                         selftests::register_to_value_test);
+
+  selftests::register_test_foreach_arch ("register_name",
+                                        selftests::register_name_test);
 }