GCC Middle and Back End API Reference
selftest-diagnostic-path.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_SELFTEST_DIAGNOSTIC_PATH_H
22#define GCC_SELFTEST_DIAGNOSTIC_PATH_H
23
24#include "diagnostic-path.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 selftest {
33
34/* Concrete subclasses of the abstract base classes
35 declared in diagnostic-path.h for use in selftests.
36
37 This code should have no dependency on "tree". */
38
39/* An implementation of diagnostic_event. */
40
41class test_diagnostic_event : public diagnostic_event
42{
43 public:
44 test_diagnostic_event (location_t loc,
45 logical_location logical_loc,
46 int depth,
47 const char *desc,
48 diagnostic_thread_id_t thread_id = 0);
49 ~test_diagnostic_event ();
50
51 location_t get_location () const final override { return m_loc; }
52 int get_stack_depth () const final override { return m_depth; }
53 void print_desc (pretty_printer &pp) const final override
54 {
55 pp_string (&pp, m_desc);
56 }
57 logical_location get_logical_location () const final override
58 {
59 return m_logical_loc;
60 }
61 meaning get_meaning () const final override
62 {
63 return meaning ();
64 }
65 bool connect_to_next_event_p () const final override
66 {
67 return m_connected_to_next_event;
68 }
69 diagnostic_thread_id_t get_thread_id () const final override
70 {
71 return m_thread_id;
72 }
73
74 void connect_to_next_event ()
75 {
76 m_connected_to_next_event = true;
77 }
78
79 private:
80 location_t m_loc;
81 logical_location m_logical_loc;
82 int m_depth;
83 char *m_desc; // has been formatted; doesn't get i18n-ed
84 bool m_connected_to_next_event;
85 diagnostic_thread_id_t m_thread_id;
86};
87
88/* A simple implementation of diagnostic_thread. */
89
90class test_diagnostic_thread : public diagnostic_thread
91{
92public:
93 test_diagnostic_thread (const char *name) : m_name (name) {}
94 label_text get_name (bool) const final override
95 {
96 return label_text::borrow (m_name);
97 }
98
99private:
100 const char *m_name; // has been i18n-ed and formatted
101};
102
103/* A concrete subclass of diagnostic_path for implementing selftests
104 - a vector of test_diagnostic_event instances
105 - adds member functions for adding test event
106 - does no translation of its events
107 - has no dependency on "tree". */
108
109class test_diagnostic_path : public diagnostic_path
110{
111 public:
112 test_diagnostic_path (pretty_printer *event_pp);
113
114 unsigned num_events () const final override;
115 const diagnostic_event & get_event (int idx) const final override;
116 unsigned num_threads () const final override;
117 const diagnostic_thread &
118 get_thread (diagnostic_thread_id_t) const final override;
119 bool
120 same_function_p (int event_idx_a,
121 int event_idx_b) const final override;
122
123 diagnostic_thread_id_t add_thread (const char *name);
124
125 diagnostic_event_id_t add_event (location_t loc, const char *funcname, int depth,
126 const char *fmt, ...)
128 diagnostic_event_id_t
129 add_thread_event (diagnostic_thread_id_t thread_id,
130 location_t loc, const char *funcname, int depth,
131 const char *fmt, ...)
133
134 void connect_to_next_event ();
135
136 void add_entry (const char *callee_name, int stack_depth,
137 diagnostic_thread_id_t thread_id = 0);
138 void add_return (const char *caller_name, int stack_depth,
139 diagnostic_thread_id_t thread_id = 0);
140 void add_call (const char *caller_name,
141 int caller_stack_depth,
142 const char *callee_name,
143 diagnostic_thread_id_t thread_id = 0);
144
145 private:
147 logical_location_from_funcname (const char *funcname);
148
149 test_logical_location_manager m_test_logical_loc_mgr;
150 auto_delete_vec<test_diagnostic_thread> m_threads;
151 auto_delete_vec<test_diagnostic_event> m_events;
152
153 /* (for use by add_event). */
154 pretty_printer *m_event_pp;
155};
156
157} // namespace selftest
158
159#endif /* #if CHECKING_P */
160
161#endif /* ! GCC_SELFTEST_DIAGNOSTIC_PATH_H */
#define ATTRIBUTE_GCC_DIAG(m, n)
Definition diagnostic-core.h:103
int diagnostic_thread_id_t
Definition diagnostic-event-id.h:70
void final(rtx_insn *first, FILE *file, int optimize_p)
Definition final.cc:2008
static location_t get_location(const gimple *stmt)
Definition gimple-ssa-warn-access.cc:78
logical_location_manager::key logical_location
Definition logical-location.h:173
Definition dump-context.h:31
void pp_string(pretty_printer *pp, const char *str)
Definition pretty-print.cc:2650
const char * get_name(tree t)
Definition tree.cc:11897