Debian Codegraph @

gcc/simple-diagnostic-path.cc

raw
1 /* Concrete classes for implementing diagnostic paths.
2 Copyright (C) 2019-2025 Free Software Foundation, Inc.
3 Contributed by David Malcolm <[email protected]>
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
22 #include "config.h"
23 #define INCLUDE_VECTOR
24 #include "system.h"
25 #include "coretypes.h"
26 #include "tree.h"
27 #include "version.h"
28 #include "demangle.h"
29 #include "intl.h"
30 #include "backtrace.h"
31 #include "diagnostic.h"
32 #include "simple-diagnostic-path.h"
33 #include "selftest.h"
34
35 /* class simple_diagnostic_path : public diagnostic_path. */
36
37 simple_diagnostic_path::simple_diagnostic_path (pretty_printer *event_pp)
38 : m_event_pp (event_pp),
39 m_localize_events (true)
40 {
41 add_thread ("main");
42 }
43
44 /* Implementation of diagnostic_path::get_event vfunc for
45 simple_diagnostic_path: simply return the event in the vec. */
46
47 const diagnostic_event &
48 simple_diagnostic_path::get_event (int idx) const
49 {
50 return *m_events[idx];
51 }
52
53 const diagnostic_thread &
54 simple_diagnostic_path::get_thread (diagnostic_thread_id_t idx) const
55 {
56 return *m_threads[idx];
57 }
58
59 bool
60 simple_diagnostic_path::same_function_p (int event_idx_a,
61 int event_idx_b) const
62 {
63 return (m_events[event_idx_a]->get_fndecl ()
64 == m_events[event_idx_b]->get_fndecl ());
65 }
66
67 diagnostic_thread_id_t
68 simple_diagnostic_path::add_thread (const char *name)
69 {
70 m_threads.safe_push (new simple_diagnostic_thread (name));
71 return m_threads.length () - 1;
72 }
73
74 /* Add an event to this path at LOC within function FNDECL at
75 stack depth DEPTH.
76
77 Use m_context's printer to format FMT, as the text of the new
78 event. Localize FMT iff m_localize_events is set.
79
80 Return the id of the new event. */
81
82 diagnostic_event_id_t
83 simple_diagnostic_path::add_event (location_t loc, tree fndecl, int depth,
84 const char *fmt, ...)
85 {
86 pretty_printer *pp = m_event_pp;
87 pp_clear_output_area (pp);
88
89 rich_location rich_loc (line_table, UNKNOWN_LOCATION);
90
91 va_list ap;
92
93 va_start (ap, fmt);
94
95 text_info ti (m_localize_events ? _(fmt) : fmt,
96 &ap, 0, nullptr, &rich_loc);
97 pp_format (pp, &ti);
98 pp_output_formatted_text (pp);
99
100 va_end (ap);
101
102 simple_diagnostic_event *new_event
103 = new simple_diagnostic_event (loc, fndecl, depth, pp_formatted_text (pp));
104 m_events.safe_push (new_event);
105
106 pp_clear_output_area (pp);
107
108 return diagnostic_event_id_t (m_events.length () - 1);
109 }
110
111 diagnostic_event_id_t
112 simple_diagnostic_path::add_thread_event (diagnostic_thread_id_t thread_id,
113 location_t loc,
114 tree fndecl,
115 int depth,
116 const char *fmt, ...)
117 {
118 pretty_printer *pp = m_event_pp;
119 pp_clear_output_area (pp);
120
121 rich_location rich_loc (line_table, UNKNOWN_LOCATION);
122
123 va_list ap;
124
125 va_start (ap, fmt);
126
127 text_info ti (_(fmt), &ap, 0, nullptr, &rich_loc);
128
129 pp_format (pp, &ti);
130 pp_output_formatted_text (pp);
131
132 va_end (ap);
133
134 simple_diagnostic_event *new_event
135 = new simple_diagnostic_event (loc, fndecl, depth, pp_formatted_text (pp),
136 thread_id);
137 m_events.safe_push (new_event);
138
139 pp_clear_output_area (pp);
140
141 return diagnostic_event_id_t (m_events.length () - 1);
142 }
143
144 /* Mark the most recent event on this path (which must exist) as being
145 connected to the next one to be added. */
146
147 void
148 simple_diagnostic_path::connect_to_next_event ()
149 {
150 gcc_assert (m_events.length () > 0);
151 m_events[m_events.length () - 1]->connect_to_next_event ();
152 }
153
154 /* struct simple_diagnostic_event. */
155
156 /* simple_diagnostic_event's ctor. */
157
158 simple_diagnostic_event::
159 simple_diagnostic_event (location_t loc,
160 tree fndecl,
161 int depth,
162 const char *desc,
163 diagnostic_thread_id_t thread_id)
164 : m_loc (loc), m_fndecl (fndecl), m_logical_loc (fndecl),
165 m_depth (depth), m_desc (xstrdup (desc)),
166 m_connected_to_next_event (false),
167 m_thread_id (thread_id)
168 {
169 }
170
171 /* simple_diagnostic_event's dtor. */
172
173 simple_diagnostic_event::~simple_diagnostic_event ()
174 {
175 free (m_desc);
176 }
177
178 void
179 simple_diagnostic_event::print_desc (pretty_printer &pp) const
180 {
181 pp_string (&pp, m_desc);
182 }
183
184 #if CHECKING_P
185
186 namespace selftest {
187
188 static void
189 test_intraprocedural_path (pretty_printer *event_pp)
190 {
191 tree fntype_void_void
192 = build_function_type_array (void_type_node, 0, NULL);
193 tree fndecl_foo = build_fn_decl ("foo", fntype_void_void);
194
195 simple_diagnostic_path path (event_pp);
196 path.add_event (UNKNOWN_LOCATION, fndecl_foo, 0, "first %qs", "free");
197 path.add_event (UNKNOWN_LOCATION, fndecl_foo, 0, "double %qs", "free");
198
199 ASSERT_EQ (path.num_events (), 2);
200 ASSERT_EQ (path.num_threads (), 1);
201 ASSERT_FALSE (path.interprocedural_p ());
202 ASSERT_STREQ (path.get_event (0).get_desc (*event_pp).get (),
203 "first `free'");
204 ASSERT_STREQ (path.get_event (1).get_desc (*event_pp).get (),
205 "double `free'");
206 }
207
208 /* Run all of the selftests within this file. */
209
210 void
211 simple_diagnostic_path_cc_tests ()
212 {
213 /* In a few places we use the global dc's printer to determine
214 colorization so ensure this off during the tests. */
215 pretty_printer *global_pp = global_dc->get_reference_printer ();
216 const bool saved_show_color = pp_show_color (global_pp);
217 pp_show_color (global_pp) = false;
218
219 auto_fix_quotes fix_quotes;
220 std::unique_ptr<pretty_printer> event_pp
221 = std::unique_ptr<pretty_printer> (global_pp->clone ());
222
223 test_intraprocedural_path (event_pp.get ());
224
225 pp_show_color (global_pp) = saved_show_color;
226 }
227
228 } // namespace selftest
229
230 #endif /* #if CHECKING_P */