1/* Language-independent diagnostic subroutines for the GNU Compiler
2 Collection that are only for use in the compilers proper and not
3 the driver or other programs.
4 Copyright (C) 1999-2023 Free Software Foundation, Inc.
5
6This file is part of GCC.
7
8GCC is free software; you can redistribute it and/or modify it under
9the terms of the GNU General Public License as published by the Free
10Software Foundation; either version 3, or (at your option) any later
11version.
12
13GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14WARRANTY; without even the implied warranty of MERCHANTABILITY or
15FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16for more details.
17
18You should have received a copy of the GNU General Public License
19along with GCC; see the file COPYING3. If not see
20<http://www.gnu.org/licenses/>. */
21
22#include "config.h"
23#include "system.h"
24#include "coretypes.h"
25#include "tree.h"
26#include "diagnostic.h"
27#include "tree-pretty-print.h"
28#include "gimple-pretty-print.h"
29#include "tree-diagnostic.h"
30#include "diagnostic-client-data-hooks.h"
31#include "langhooks.h"
32#include "intl.h"
33
34/* Prints out, if necessary, the name of the current function
35 that caused an error. Called from all error and warning functions. */
36void
37diagnostic_report_current_function (diagnostic_context *context,
38 diagnostic_info *diagnostic)
39{
40 location_t loc = diagnostic_location (diagnostic);
41 diagnostic_report_current_module (context, where: loc);
42 lang_hooks.print_error_function (context, LOCATION_FILE (loc), diagnostic);
43}
44
45static void
46default_tree_diagnostic_starter (diagnostic_context *context,
47 diagnostic_info *diagnostic)
48{
49 diagnostic_report_current_function (context, diagnostic);
50 pp_set_prefix (context->printer, diagnostic_build_prefix (context,
51 diagnostic));
52}
53
54/* This is a pair made of a location and the line map it originated
55 from. It's used in the maybe_unwind_expanded_macro_loc function
56 below. */
57struct loc_map_pair
58{
59 const line_map_macro *map;
60 location_t where;
61};
62
63
64/* Unwind the different macro expansions that lead to the token which
65 location is WHERE and emit diagnostics showing the resulting
66 unwound macro expansion trace. Let's look at an example to see how
67 the trace looks like. Suppose we have this piece of code,
68 artificially annotated with the line numbers to increase
69 legibility:
70
71 $ cat -n test.c
72 1 #define OPERATE(OPRD1, OPRT, OPRD2) \
73 2 OPRD1 OPRT OPRD2;
74 3
75 4 #define SHIFTL(A,B) \
76 5 OPERATE (A,<<,B)
77 6
78 7 #define MULT(A) \
79 8 SHIFTL (A,1)
80 9
81 10 void
82 11 g ()
83 12 {
84 13 MULT (1.0);// 1.0 << 1; <-- so this is an error.
85 14 }
86
87 Here is the diagnostic that we want the compiler to generate:
88
89 test.c: In function ‘g’:
90 test.c:5:14: error: invalid operands to binary << (have ‘double’ and ‘int’)
91 test.c:2:9: note: in definition of macro 'OPERATE'
92 test.c:8:3: note: in expansion of macro 'SHIFTL'
93 test.c:13:3: note: in expansion of macro 'MULT'
94
95 The part that goes from the third to the fifth line of this
96 diagnostic (the lines containing the 'note:' string) is called the
97 unwound macro expansion trace. That's the part generated by this
98 function. */
99
100void
101maybe_unwind_expanded_macro_loc (diagnostic_context *context,
102 location_t where)
103{
104 const struct line_map *map;
105 auto_vec<loc_map_pair> loc_vec;
106 unsigned ix;
107 loc_map_pair loc, *iter;
108
109 const location_t original_loc = where;
110
111 map = linemap_lookup (line_table, where);
112 if (!linemap_macro_expansion_map_p (map))
113 return;
114
115 /* Let's unwind the macros that got expanded and led to the token
116 which location is WHERE. We are going to store these macros into
117 LOC_VEC, so that we can later walk it at our convenience to
118 display a somewhat meaningful trace of the macro expansion
119 history to the user. Note that the first macro of the trace
120 (which is OPERATE in the example above) is going to be stored at
121 the beginning of LOC_VEC. */
122
123 do
124 {
125 loc.where = where;
126 loc.map = linemap_check_macro (map);
127
128 loc_vec.safe_push (obj: loc);
129
130 /* WHERE is the location of a token inside the expansion of a
131 macro. MAP is the map holding the locations of that macro
132 expansion. Let's get the location of the token inside the
133 context that triggered the expansion of this macro.
134 This is basically how we go "down" in the trace of macro
135 expansions that led to WHERE. */
136 where = linemap_unwind_toward_expansion (line_table, loc: where, loc_map: &map);
137 } while (linemap_macro_expansion_map_p (map));
138
139 /* Now map is set to the map of the location in the source that
140 first triggered the macro expansion. This must be an ordinary map. */
141 const line_map_ordinary *ord_map = linemap_check_ordinary (map);
142
143 /* Walk LOC_VEC and print the macro expansion trace, unless the
144 first macro which expansion triggered this trace was expanded
145 inside a system header. */
146 int saved_location_line =
147 expand_location_to_spelling_point (original_loc).line;
148
149 if (!LINEMAP_SYSP (ord_map))
150 FOR_EACH_VEC_ELT (loc_vec, ix, iter)
151 {
152 /* Sometimes, in the unwound macro expansion trace, we want to
153 print a part of the context that shows where, in the
154 definition of the relevant macro, is the token (we are
155 looking at) used. That is the case in the introductory
156 comment of this function, where we print:
157
158 test.c:2:9: note: in definition of macro 'OPERATE'.
159
160 We print that "macro definition context" because the
161 diagnostic line (emitted by the call to
162 pp_ouput_formatted_text in diagnostic_report_diagnostic):
163
164 test.c:5:14: error: invalid operands to binary << (have ‘double’ and ‘int’)
165
166 does not point into the definition of the macro where the
167 token '<<' (that is an argument to the function-like macro
168 OPERATE) is used. So we must "display" the line of that
169 macro definition context to the user somehow.
170
171 A contrario, when the first interesting diagnostic line
172 points into the definition of the macro, we don't need to
173 display any line for that macro definition in the trace
174 anymore, otherwise it'd be redundant. */
175
176 /* Okay, now here is what we want. For each token resulting
177 from macro expansion we want to show: 1/ where in the
178 definition of the macro the token comes from; 2/ where the
179 macro got expanded. */
180
181 /* Resolve the location iter->where into the locus 1/ of the
182 comment above. */
183 location_t resolved_def_loc =
184 linemap_resolve_location (line_table, loc: iter->where,
185 lrk: LRK_MACRO_DEFINITION_LOCATION, NULL);
186
187 /* Don't print trace for locations that are reserved or from
188 within a system header. */
189 const line_map_ordinary *m = NULL;
190 location_t l =
191 linemap_resolve_location (line_table, loc: resolved_def_loc,
192 lrk: LRK_SPELLING_LOCATION, loc_map: &m);
193 location_t l0 = l;
194 if (IS_ADHOC_LOC (loc: l0))
195 l0 = get_location_from_adhoc_loc (line_table, l0);
196 if (l0 < RESERVED_LOCATION_COUNT || LINEMAP_SYSP (ord_map: m))
197 continue;
198
199 /* We need to print the context of the macro definition only
200 when the locus of the first displayed diagnostic (displayed
201 before this trace) was inside the definition of the
202 macro. */
203 const int resolved_def_loc_line = SOURCE_LINE (ord_map: m, loc: l0);
204 if (ix == 0 && saved_location_line != resolved_def_loc_line)
205 {
206 diagnostic_append_note (context, resolved_def_loc,
207 "in definition of macro %qs",
208 linemap_map_get_macro_name (iter->map));
209 /* At this step, as we've printed the context of the macro
210 definition, we don't want to print the context of its
211 expansion, otherwise, it'd be redundant. */
212 continue;
213 }
214
215 /* Resolve the location of the expansion point of the macro
216 which expansion gave the token represented by def_loc.
217 This is the locus 2/ of the earlier comment. */
218 location_t resolved_exp_loc =
219 linemap_resolve_location (line_table,
220 loc: iter->map->get_expansion_point_location (),
221 lrk: LRK_MACRO_DEFINITION_LOCATION, NULL);
222
223 diagnostic_append_note (context, resolved_exp_loc,
224 "in expansion of macro %qs",
225 linemap_map_get_macro_name (iter->map));
226 }
227}
228
229/* This is a diagnostic finalizer implementation that is aware of
230 virtual locations produced by libcpp.
231
232 It has to be called by the diagnostic finalizer of front ends that
233 uses libcpp and wish to get diagnostics involving tokens resulting
234 from macro expansion.
235
236 For a given location, if said location belongs to a token
237 resulting from a macro expansion, this starter prints the context
238 of the token. E.g, for multiply nested macro expansion, it
239 unwinds the nested macro expansions and prints them in a manner
240 that is similar to what is done for function call stacks, or
241 template instantiation contexts. */
242void
243virt_loc_aware_diagnostic_finalizer (diagnostic_context *context,
244 diagnostic_info *diagnostic)
245{
246 maybe_unwind_expanded_macro_loc (context, where: diagnostic_location (diagnostic));
247}
248
249/* Default tree printer. Handles declarations only. */
250bool
251default_tree_printer (pretty_printer *pp, text_info *text, const char *spec,
252 int precision, bool wide, bool set_locus, bool hash,
253 bool *, const char **)
254{
255 tree t;
256
257 /* FUTURE: %+x should set the locus. */
258 if (precision != 0 || wide || hash)
259 return false;
260
261 switch (*spec)
262 {
263 case 'E':
264 t = va_arg (*text->m_args_ptr, tree);
265 if (TREE_CODE (t) == IDENTIFIER_NODE)
266 {
267 pp_identifier (pp, IDENTIFIER_POINTER (t));
268 return true;
269 }
270 break;
271
272 case 'D':
273 t = va_arg (*text->m_args_ptr, tree);
274 if (VAR_P (t) && DECL_HAS_DEBUG_EXPR_P (t))
275 t = DECL_DEBUG_EXPR (t);
276 break;
277
278 case 'F':
279 case 'T':
280 t = va_arg (*text->m_args_ptr, tree);
281 break;
282
283 default:
284 return false;
285 }
286
287 if (set_locus)
288 text->set_location (idx: 0, DECL_SOURCE_LOCATION (t), range_display_kind: SHOW_RANGE_WITH_CARET);
289
290 if (DECL_P (t))
291 {
292 const char *n = DECL_NAME (t)
293 ? identifier_to_locale (lang_hooks.decl_printable_name (t, 2))
294 : _("<anonymous>");
295 pp_string (pp, n);
296 }
297 else
298 dump_generic_node (pp, t, 0, TDF_SLIM, 0);
299
300 return true;
301}
302
303/* Set the locations of call sites along the inlining stack corresponding
304 to the DIAGNOSTIC location. */
305
306static void
307set_inlining_locations (diagnostic_context *,
308 diagnostic_info *diagnostic)
309{
310 location_t loc = diagnostic_location (diagnostic);
311 tree block = LOCATION_BLOCK (loc);
312
313 /* Count the number of locations in system headers. When all are,
314 warnings are suppressed by -Wno-system-headers. Otherwise, they
315 involve some user code, possibly inlined into a function in a system
316 header, and are not treated as coming from system headers. */
317 unsigned nsyslocs = 0;
318
319 /* Use a reference to the vector of locations for convenience. */
320 auto &ilocs = diagnostic->m_iinfo.m_ilocs;
321
322 while (block && TREE_CODE (block) == BLOCK
323 && BLOCK_ABSTRACT_ORIGIN (block))
324 {
325 tree ao = BLOCK_ABSTRACT_ORIGIN (block);
326 if (TREE_CODE (ao) == FUNCTION_DECL)
327 {
328 if (!diagnostic->m_iinfo.m_ao)
329 diagnostic->m_iinfo.m_ao = block;
330
331 location_t bsloc = BLOCK_SOURCE_LOCATION (block);
332 ilocs.safe_push (obj: bsloc);
333 if (in_system_header_at (loc: bsloc))
334 ++nsyslocs;
335 }
336 else if (TREE_CODE (ao) != BLOCK)
337 break;
338
339 block = BLOCK_SUPERCONTEXT (block);
340 }
341
342 if (ilocs.length ())
343 {
344 /* When there is an inlining context use the macro expansion
345 location for the original location and bump up NSYSLOCS if
346 it's in a system header since it's not counted above. */
347 location_t sysloc = expansion_point_location_if_in_system_header (loc);
348 if (sysloc != loc)
349 {
350 loc = sysloc;
351 ++nsyslocs;
352 }
353 }
354 else
355 {
356 /* When there's no inlining context use the original location
357 and set NSYSLOCS accordingly. */
358 nsyslocs = in_system_header_at (loc) != 0;
359 }
360
361 ilocs.safe_push (obj: loc);
362
363 /* Set if all locations are in a system header. */
364 diagnostic->m_iinfo.m_allsyslocs = nsyslocs == ilocs.length ();
365
366 if (tree *ao = pp_ti_abstract_origin (&diagnostic->message))
367 *ao = (tree)diagnostic->m_iinfo.m_ao;
368}
369
370/* Sets CONTEXT to use language independent diagnostics. */
371void
372tree_diagnostics_defaults (diagnostic_context *context)
373{
374 diagnostic_starter (context) = default_tree_diagnostic_starter;
375 diagnostic_finalizer (context) = default_diagnostic_finalizer;
376 diagnostic_format_decoder (context) = default_tree_printer;
377 context->m_print_path = default_tree_diagnostic_path_printer;
378 context->m_make_json_for_path = default_tree_make_json_for_path;
379 context->set_set_locations_callback (set_inlining_locations);
380 context->set_client_data_hooks (make_compiler_data_hooks ());
381}
382

source code of gcc/tree-diagnostic.cc