]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/unittests/lookup_name_info-selftests.c
747728158d8e18ebf2e6542f178fa618cd1e425b
[thirdparty/binutils-gdb.git] / gdb / unittests / lookup_name_info-selftests.c
1 /* Self tests for lookup_name_info for GDB, the GNU debugger.
2
3 Copyright (C) 2017-2019 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19
20 #include "defs.h"
21 #include "gdbsupport/selftest.h"
22 #include "symtab.h"
23
24 namespace selftests {
25 namespace lookup_name {
26
27 /* Check that removing parameter info out of NAME produces EXPECTED.
28 COMPLETION_MODE indicates whether we're testing normal and
29 completion mode. FILE and LINE are used to provide better test
30 location information in case the check fails. */
31
32 static void
33 check_make_paramless (const char *file, int line,
34 enum language lang,
35 const char *name, const char *expected,
36 bool completion_mode)
37 {
38 lookup_name_info lookup_name (name, symbol_name_match_type::FULL,
39 completion_mode, true /* ignore_parameters */);
40 const std::string &result = lookup_name.language_lookup_name (lang);
41
42 if (result != expected)
43 {
44 error (_("%s:%d: make-paramless self-test failed: (completion=%d, lang=%d) "
45 "\"%s\" -> \"%s\", expected \"%s\""),
46 file, line, completion_mode, lang, name,
47 result.c_str (), expected);
48 }
49 }
50
51 static void
52 run_tests ()
53 {
54 /* Helper for CHECK and CHECK_INCOMPL. */
55 #define CHECK_1(INCOMPLETE, LANG, NAME, EXPECTED) \
56 do \
57 { \
58 check_make_paramless (__FILE__, __LINE__, \
59 LANG, NAME, \
60 (INCOMPLETE) ? "" : (EXPECTED), false); \
61 check_make_paramless (__FILE__, __LINE__, \
62 LANG, NAME, EXPECTED, true); \
63 } \
64 while (0)
65
66 /* Check that removing parameter info out of NAME produces EXPECTED.
67 Checks both normal and completion modes. */
68 #define CHECK(LANG, NAME, EXPECTED) \
69 CHECK_1(false, LANG, NAME, EXPECTED)
70
71 /* Similar, but used when NAME is incomplete -- i.e., NAME has
72 unbalanced parentheses. In this case, looking for the exact name
73 should fail / return empty. */
74 #define CHECK_INCOMPL(LANG, NAME, EXPECTED) \
75 CHECK_1 (true, LANG, NAME, EXPECTED)
76
77 /* None of these languages support function overloading like C++
78 does, so building a nameless lookup name ends up being just the
79 same as any other lookup name. Mainly this serves as smoke test
80 that C++-specific code doesn't mess up with other languages that
81 use some other scoping character ('.' vs '::'). */
82 CHECK (language_ada, "pck.ada_hello", "pck__ada_hello");
83 CHECK (language_go, "pck.go_hello", "pck.go_hello");
84 CHECK (language_fortran, "mod::func", "mod::func");
85
86 /* D does support function overloading similar to C++, but we're
87 missing support for stripping parameters. At least make sure the
88 input name is preserved unmodified. */
89 CHECK (language_d, "pck.d_hello", "pck.d_hello");
90
91 /* Just a few basic tests to make sure
92 lookup_name_info::make_paramless is well integrated with
93 cp_remove_params_if_any. gdb/cp-support.c has comprehensive
94 testing of C++ specifics. */
95 CHECK (language_cplus, "function()", "function");
96 CHECK (language_cplus, "function() const", "function");
97 CHECK (language_cplus, "A::B::C()", "A::B::C");
98 CHECK (language_cplus, "A::B::C", "A::B::C");
99
100 #undef CHECK
101 #undef CHECK_INCOMPL
102 }
103
104 }} // namespace selftests::lookup_name
105
106 void
107 _initialize_lookup_name_info_selftests ()
108 {
109 selftests::register_test ("lookup_name_info",
110 selftests::lookup_name::run_tests);
111 }