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