Line data Source code
1 : /* Concrete subclass of logical_locations::manager for use in selftests.
2 : Copyright (C) 2024-2026 Free Software Foundation, Inc.
3 : Contributed by David Malcolm <dmalcolm@redhat.com>.
4 :
5 : This file is part of GCC.
6 :
7 : GCC is free software; you can redistribute it and/or modify it under
8 : the terms of the GNU General Public License as published by the Free
9 : Software Foundation; either version 3, or (at your option) any later
10 : version.
11 :
12 : GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 : WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 : FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 : for more details.
16 :
17 : You should have received a copy of the GNU General Public License
18 : along with GCC; see the file COPYING3. If not see
19 : <http://www.gnu.org/licenses/>. */
20 :
21 : #include "config.h"
22 : #include "system.h"
23 : #include "coretypes.h"
24 : #include "selftest.h"
25 : #include "diagnostics/selftest-logical-locations.h"
26 : #include "diagnostics/dumping.h"
27 :
28 : #if CHECKING_P
29 :
30 : namespace diagnostics {
31 : namespace logical_locations {
32 : namespace selftest {
33 :
34 : /* class test_manager : public manager. */
35 :
36 1664 : test_manager::~test_manager ()
37 : {
38 1760 : for (auto iter : m_name_to_item_map)
39 48 : delete iter.second;
40 1664 : }
41 :
42 : void
43 0 : test_manager::dump (FILE *outfile, int indent) const
44 : {
45 0 : dumping::emit_heading (outfile, indent, "test_manager");
46 0 : }
47 :
48 : label_text
49 0 : test_manager::get_short_name (key k) const
50 : {
51 0 : auto item = item_from_key (k);
52 0 : if (!item)
53 0 : return label_text ();
54 0 : return label_text::borrow (item->m_name);
55 : }
56 :
57 : label_text
58 0 : test_manager::get_name_with_scope (key k) const
59 : {
60 0 : auto item = item_from_key (k);
61 0 : return label_text::borrow (item->m_name);
62 : }
63 :
64 : label_text
65 0 : test_manager::get_internal_name (key k) const
66 : {
67 0 : auto item = item_from_key (k);
68 0 : return label_text::borrow (item->m_name);
69 : }
70 :
71 : enum diagnostics::logical_locations::kind
72 20 : test_manager::get_kind (key k) const
73 : {
74 20 : auto item = item_from_key (k);
75 20 : return item->m_kind;
76 : }
77 :
78 : label_text
79 156 : test_manager::get_name_for_path_output (key k) const
80 : {
81 156 : auto item = item_from_key (k);
82 156 : return label_text::borrow (item->m_name);
83 : }
84 :
85 : diagnostics::logical_locations::key
86 4668 : test_manager::
87 : logical_location_from_funcname (const char *funcname)
88 : {
89 4668 : const item *i = item_from_funcname (funcname);
90 4668 : return key::from_ptr (i);
91 : }
92 :
93 : const test_manager::item *
94 4668 : test_manager::item_from_funcname (const char *funcname)
95 : {
96 4668 : if (!funcname)
97 : return nullptr;
98 :
99 156 : if (item **slot = m_name_to_item_map.get (funcname))
100 108 : return *slot;
101 :
102 48 : item *i = new item (kind::function, funcname);
103 48 : m_name_to_item_map.put (funcname, i);
104 48 : return i;
105 : }
106 :
107 : /* Run all of the selftests within this file. */
108 :
109 : void
110 0 : selftest_logical_locations_cc_tests ()
111 : {
112 0 : test_manager mgr;
113 :
114 0 : ASSERT_FALSE (mgr.logical_location_from_funcname (nullptr));
115 :
116 0 : key loc_foo = mgr.logical_location_from_funcname ("foo");
117 0 : key loc_bar = mgr.logical_location_from_funcname ("bar");
118 :
119 0 : ASSERT_NE (loc_foo, loc_bar);
120 :
121 0 : ASSERT_STREQ (mgr.get_short_name (loc_foo).get (), "foo");
122 0 : ASSERT_STREQ (mgr.get_short_name (loc_bar).get (), "bar");
123 0 : }
124 :
125 : } // namespace diagnostics::logical_locations::selftest
126 : } // namespace diagnostics::logical_locations
127 : } // namespace diagnostics
128 :
129 : #endif /* #if CHECKING_P */
|