Fix crash when creating index from index
[binutils-gdb.git] / gdb / selftest-arch.c
1 /* GDB self-test for each gdbarch.
2 Copyright (C) 2017-2022 Free Software Foundation, Inc.
3
4 This file is part of GDB.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>. */
18
19 #include "defs.h"
20 #include <functional>
21
22 #if GDB_SELF_TEST
23 #include "gdbsupport/selftest.h"
24 #include "selftest-arch.h"
25 #include "arch-utils.h"
26
27 namespace selftests {
28
29 static bool skip_arch (const char *arch)
30 {
31 if (strcmp ("fr300", arch) == 0)
32 {
33 /* PR 20946 */
34 return true;
35 }
36
37 if (strcmp ("powerpc:EC603e", arch) == 0
38 || strcmp ("powerpc:e500mc", arch) == 0
39 || strcmp ("powerpc:e500mc64", arch) == 0
40 || strcmp ("powerpc:titan", arch) == 0
41 || strcmp ("powerpc:vle", arch) == 0
42 || strcmp ("powerpc:e5500", arch) == 0
43 || strcmp ("powerpc:e6500", arch) == 0)
44 {
45 /* PR 19797 */
46 return true;
47 }
48
49 return false;
50 }
51
52 /* Generate a selftest for each gdbarch known to GDB. */
53
54 static std::vector<selftest>
55 foreach_arch_test_generator (const std::string &name,
56 self_test_foreach_arch_function *function)
57 {
58 std::vector<selftest> tests;
59 std::vector<const char *> arches = gdbarch_printable_names ();
60 tests.reserve (arches.size ());
61 for (const char *arch : arches)
62 {
63 if (skip_arch (arch))
64 continue;
65
66 auto test_fn
67 = ([=] ()
68 {
69 struct gdbarch_info info;
70 info.bfd_arch_info = bfd_scan_arch (arch);
71 struct gdbarch *gdbarch = gdbarch_find_by_info (info);
72 SELF_CHECK (gdbarch != NULL);
73 function (gdbarch);
74 reset ();
75 });
76
77 tests.emplace_back (string_printf ("%s::%s", name.c_str (), arch),
78 test_fn);
79 }
80 return tests;
81 }
82
83 /* See selftest-arch.h. */
84
85 void
86 register_test_foreach_arch (const std::string &name,
87 self_test_foreach_arch_function *function)
88 {
89 add_lazy_generator ([=] ()
90 {
91 return foreach_arch_test_generator (name, function);
92 });
93 }
94
95 void
96 reset ()
97 {
98 /* Clear GDB internal state. */
99 registers_changed ();
100 reinit_frame_cache ();
101 }
102 } // namespace selftests
103 #endif /* GDB_SELF_TEST */