GCC Middle and Back End API Reference
selftest-paths.h
Go to the documentation of this file.
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
5This file is part of GCC.
6
7GCC is free software; you can redistribute it and/or modify it under
8the terms of the GNU General Public License as published by the Free
9Software Foundation; either version 3, or (at your option) any later
10version.
11
12GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13WARRANTY; without even the implied warranty of MERCHANTABILITY or
14FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15for more details.
16
17You should have received a copy of the GNU General Public License
18along 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"
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
32namespace diagnostics {
33namespace paths {
34namespace 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
43class 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 location_t get_location () const final override { return m_loc; }
57 int get_stack_depth () const final override { return m_depth; }
58 void print_desc (pretty_printer &pp) const final override
59 {
60 pp_string (&pp, m_desc);
61 }
62 logical_location get_logical_location () const final override
63 {
64 return m_logical_loc;
65 }
66 meaning get_meaning () const final override
67 {
68 return meaning ();
69 }
70 bool connect_to_next_event_p () const final override
71 {
72 return m_connected_to_next_event;
73 }
74 thread_id_t get_thread_id () const final override
75 {
76 return m_thread_id;
77 }
78
79 void connect_to_next_event ()
80 {
81 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
95class test_thread : public thread
96{
97public:
98 test_thread (const char *name) : m_name (name) {}
99 label_text get_name (bool) const final override
100 {
101 return label_text::borrow (m_name);
102 }
103
104private:
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
114class 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, ...)
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, ...)
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 */
static struct path_prefix cpath path
Definition collect2.cc:514
#define ATTRIBUTE_GCC_DIAG(m, n)
Definition diagnostic-core.h:71
void final(rtx_insn *first, FILE *file, int optimize_p)
Definition final.cc:2009
static location_t get_location(const gimple *stmt)
Definition gimple-ssa-warn-access.cc:79
Definition event-id.h:64
int thread_id_t
Definition event-id.h:71
Definition coretypes.h:167
void pp_string(pretty_printer *pp, const char *str)
Definition pretty-print.cc:2638
const char * get_name(tree t)
Definition tree.cc:11982