Branch data Line data Source code
1 : : /* Concrete classes for selftests involving diagnostic paths.
2 : : Copyright (C) 2019-2025 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 : : #ifndef GCC_DIAGNOSTICS_SELFTEST_PATHS_H
22 : : #define GCC_DIAGNOSTICS_SELFTEST_PATHS_H
23 : :
24 : : #include "diagnostics/paths.h"
25 : : #include "diagnostics/selftest-logical-locations.h"
26 : :
27 : : /* The selftest code should entirely disappear in a production
28 : : configuration, hence we guard all of it with #if CHECKING_P. */
29 : :
30 : : #if CHECKING_P
31 : :
32 : : namespace diagnostics {
33 : : namespace paths {
34 : : namespace selftest {
35 : :
36 : : /* Concrete subclasses of the abstract base classes
37 : : declared in diagnostic-path.h for use in selftests.
38 : :
39 : : This code should have no dependency on "tree". */
40 : :
41 : : /* An implementation of diagnostics::paths::event. */
42 : :
43 : : class test_event : public event
44 : : {
45 : : public:
46 : : using logical_location = logical_locations::key;
47 : : using thread_id_t = paths::thread_id_t;
48 : :
49 : : test_event (location_t loc,
50 : : logical_location logical_loc,
51 : : int depth,
52 : : const char *desc,
53 : : thread_id_t thread_id = 0);
54 : : ~test_event ();
55 : :
56 : 19188 : location_t get_location () const final override { return m_loc; }
57 : 10744 : int get_stack_depth () const final override { return m_depth; }
58 : 3812 : void print_desc (pretty_printer &pp) const final override
59 : : {
60 : 3812 : pp_string (&pp, m_desc);
61 : 3812 : }
62 : 14596 : logical_location get_logical_location () const final override
63 : : {
64 : 8332 : return m_logical_loc;
65 : : }
66 : 3404 : meaning get_meaning () const final override
67 : : {
68 : 3404 : return meaning ();
69 : : }
70 : 12964 : bool connect_to_next_event_p () const final override
71 : : {
72 : 12964 : return m_connected_to_next_event;
73 : : }
74 : 10892 : thread_id_t get_thread_id () const final override
75 : : {
76 : 10892 : return m_thread_id;
77 : : }
78 : :
79 : 2016 : void connect_to_next_event ()
80 : : {
81 : 2016 : m_connected_to_next_event = true;
82 : : }
83 : :
84 : : private:
85 : : location_t m_loc;
86 : : logical_location m_logical_loc;
87 : : int m_depth;
88 : : char *m_desc; // has been formatted; doesn't get i18n-ed
89 : : bool m_connected_to_next_event;
90 : : thread_id_t m_thread_id;
91 : : };
92 : :
93 : : /* A simple implementation of diagnostics::paths::thread. */
94 : :
95 : : class test_thread : public thread
96 : : {
97 : : public:
98 : 1660 : test_thread (const char *name) : m_name (name) {}
99 : 1284 : label_text get_name (bool) const final override
100 : : {
101 : 1284 : return label_text::borrow (m_name);
102 : : }
103 : :
104 : : private:
105 : : const char *m_name; // has been i18n-ed and formatted
106 : : };
107 : :
108 : : /* A concrete subclass of diagnostics::paths::path for implementing selftests
109 : : - a vector of test_event instances
110 : : - adds member functions for adding test event
111 : : - does no translation of its events
112 : : - has no dependency on "tree". */
113 : :
114 : : class test_path : public path
115 : : {
116 : : public:
117 : : test_path (logical_locations::selftest::test_manager &logical_loc_mgr,
118 : : pretty_printer *event_pp);
119 : :
120 : : unsigned num_events () const final override;
121 : : const event & get_event (int idx) const final override;
122 : : unsigned num_threads () const final override;
123 : : const thread &
124 : : get_thread (thread_id_t) const final override;
125 : : bool
126 : : same_function_p (int event_idx_a,
127 : : int event_idx_b) const final override;
128 : :
129 : : thread_id_t add_thread (const char *name);
130 : :
131 : : event_id_t add_event (location_t loc, const char *funcname, int depth,
132 : : const char *fmt, ...)
133 : : ATTRIBUTE_GCC_DIAG(5,6);
134 : : event_id_t
135 : : add_thread_event (thread_id_t thread_id,
136 : : location_t loc, const char *funcname, int depth,
137 : : const char *fmt, ...)
138 : : ATTRIBUTE_GCC_DIAG(6,7);
139 : :
140 : : void connect_to_next_event ();
141 : :
142 : : void add_entry (const char *callee_name, int stack_depth,
143 : : thread_id_t thread_id = 0);
144 : : void add_return (const char *caller_name, int stack_depth,
145 : : thread_id_t thread_id = 0);
146 : : void add_call (const char *caller_name,
147 : : int caller_stack_depth,
148 : : const char *callee_name,
149 : : thread_id_t thread_id = 0);
150 : :
151 : : private:
152 : : logical_locations::key
153 : : logical_location_from_funcname (const char *funcname);
154 : :
155 : : logical_locations::selftest::test_manager &m_test_logical_loc_mgr;
156 : : auto_delete_vec<test_thread> m_threads;
157 : : auto_delete_vec<test_event> m_events;
158 : :
159 : : /* (for use by add_event). */
160 : : pretty_printer *m_event_pp;
161 : : };
162 : :
163 : : } // namespace diagnostics::paths::selftest
164 : : } // namespace diagnostics::paths
165 : : } // namespace diagnostics
166 : :
167 : : #endif /* #if CHECKING_P */
168 : :
169 : : #endif /* ! GCC_DIAGNOSTICS_SELFTEST_PATHS_H */
|