Add `set print array-indexes' tests for C/C++ arrays
[binutils-gdb.git] / gdb / selftest-arch.c
index 38cd6248d047f31585a3fd90b9e48ea75d6fad55..1375838b0c26a6aba0917078802b975644f91c6f 100644 (file)
@@ -1,5 +1,5 @@
 /* GDB self-test for each gdbarch.
-   Copyright (C) 2017-2019 Free Software Foundation, Inc.
+   Copyright (C) 2017-2022 Free Software Foundation, Inc.
 
    This file is part of GDB.
 
    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
 
 #include "defs.h"
+#include <functional>
 
 #if GDB_SELF_TEST
-#include "common/selftest.h"
+#include "gdbsupport/selftest.h"
 #include "selftest-arch.h"
 #include "arch-utils.h"
 
 namespace selftests {
 
-/* A kind of selftest that calls the test function once for each gdbarch known
-   to GDB.  */
-
-struct gdbarch_selftest : public selftest
+static bool skip_arch (const char *arch)
 {
-  gdbarch_selftest (self_test_foreach_arch_function *function_)
-  : function (function_)
-  {}
-
-  void operator() () const override
-  {
-    const char **arches = gdbarch_printable_names ();
-    bool pass = true;
-
-    for (int i = 0; arches[i] != NULL; i++)
-      {
-       if (strcmp ("fr300", arches[i]) == 0)
-         {
-           /* PR 20946 */
-           continue;
-         }
-       else if (strcmp ("powerpc:EC603e", arches[i]) == 0
-                || strcmp ("powerpc:e500mc", arches[i]) == 0
-                || strcmp ("powerpc:e500mc64", arches[i]) == 0
-                || strcmp ("powerpc:titan", arches[i]) == 0
-                || strcmp ("powerpc:vle", arches[i]) == 0
-                || strcmp ("powerpc:e5500", arches[i]) == 0
-                || strcmp ("powerpc:e6500", arches[i]) == 0)
-         {
-           /* PR 19797 */
-           continue;
-         }
-
-       QUIT;
-
-       try
-         {
-           struct gdbarch_info info;
-
-           gdbarch_info_init (&info);
-           info.bfd_arch_info = bfd_scan_arch (arches[i]);
-
-           struct gdbarch *gdbarch = gdbarch_find_by_info (info);
-           SELF_CHECK (gdbarch != NULL);
-
-           function (gdbarch);
-         }
-       catch (const gdb_exception_error &ex)
-         {
-           pass = false;
-           exception_fprintf (gdb_stderr, ex,
-                              _("Self test failed: arch %s: "), arches[i]);
-         }
-
-       reset ();
-      }
-
-    SELF_CHECK (pass);
-  }
+  if (strcmp ("fr300", arch) == 0)
+    {
+      /* PR 20946 */
+      return true;
+    }
+
+  if (strcmp ("powerpc:EC603e", arch) == 0
+      || strcmp ("powerpc:e500mc", arch) == 0
+      || strcmp ("powerpc:e500mc64", arch) == 0
+      || strcmp ("powerpc:titan", arch) == 0
+      || strcmp ("powerpc:vle", arch) == 0
+      || strcmp ("powerpc:e5500", arch) == 0
+      || strcmp ("powerpc:e6500", arch) == 0)
+    {
+      /* PR 19797 */
+      return true;
+    }
+
+  return false;
+}
 
-  self_test_foreach_arch_function *function;
-};
+/* Register a kind of selftest that calls the test function once for each
+   gdbarch known to GDB.  */
 
 void
 register_test_foreach_arch (const std::string &name,
                            self_test_foreach_arch_function *function)
 {
-  register_test (name, new gdbarch_selftest (function));
+  std::vector<const char *> arches = gdbarch_printable_names ();
+  for (const char *arch : arches)
+    {
+      if (skip_arch (arch))
+       continue;
+
+      auto test_fn
+       = ([=] ()
+          {
+            struct gdbarch_info info;
+            info.bfd_arch_info = bfd_scan_arch (arch);
+            struct gdbarch *gdbarch = gdbarch_find_by_info (info);
+            SELF_CHECK (gdbarch != NULL);
+            function (gdbarch);
+            reset ();
+          });
+
+      std::string test_name
+       = name + std::string ("::") + std::string (arch);
+      register_test (test_name, test_fn);
+    }
 }
 
 void