c++: top level bind when rewriting coroutines [PR106188]
[official-gcc.git] / gcc / tree-diagnostic.cc
blob0d79fe3c3c15dc536a4bc9221d98ff6637d08b96
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-2022 Free Software Foundation, Inc.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
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"
34 /* Prints out, if necessary, the name of the current function
35 that caused an error. Called from all error and warning functions. */
36 void
37 diagnostic_report_current_function (diagnostic_context *context,
38 diagnostic_info *diagnostic)
40 location_t loc = diagnostic_location (diagnostic);
41 diagnostic_report_current_module (context, loc);
42 lang_hooks.print_error_function (context, LOCATION_FILE (loc), diagnostic);
45 static void
46 default_tree_diagnostic_starter (diagnostic_context *context,
47 diagnostic_info *diagnostic)
49 diagnostic_report_current_function (context, diagnostic);
50 pp_set_prefix (context->printer, diagnostic_build_prefix (context,
51 diagnostic));
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. */
57 struct loc_map_pair
59 const line_map_macro *map;
60 location_t where;
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:
71 $ cat -n test.c
72 1 #define OPERATE(OPRD1, OPRT, OPRD2) \
73 2 OPRD1 OPRT OPRD2;
75 4 #define SHIFTL(A,B) \
76 5 OPERATE (A,<<,B)
78 7 #define MULT(A) \
79 8 SHIFTL (A,1)
81 10 void
82 11 g ()
83 12 {
84 13 MULT (1.0);// 1.0 << 1; <-- so this is an error.
85 14 }
87 Here is the diagnostic that we want the compiler to generate:
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'
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. */
100 void
101 maybe_unwind_expanded_macro_loc (diagnostic_context *context,
102 location_t where)
104 const struct line_map *map;
105 auto_vec<loc_map_pair> loc_vec;
106 unsigned ix;
107 loc_map_pair loc, *iter;
109 const location_t original_loc = where;
111 map = linemap_lookup (line_table, where);
112 if (!linemap_macro_expansion_map_p (map))
113 return;
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. */
125 loc.where = where;
126 loc.map = linemap_check_macro (map);
128 loc_vec.safe_push (loc);
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, where, &map);
137 } while (linemap_macro_expansion_map_p (map));
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);
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;
149 if (!LINEMAP_SYSP (ord_map))
150 FOR_EACH_VEC_ELT (loc_vec, ix, iter)
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:
158 test.c:2:9: note: in definition of macro 'OPERATE'.
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):
164 test.c:5:14: error: invalid operands to binary << (have ‘double’ and ‘int’)
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.
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. */
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. */
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, iter->where,
185 LRK_MACRO_DEFINITION_LOCATION, NULL);
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, resolved_def_loc,
192 LRK_SPELLING_LOCATION, &m);
193 if (l < RESERVED_LOCATION_COUNT || LINEMAP_SYSP (m))
194 continue;
196 /* We need to print the context of the macro definition only
197 when the locus of the first displayed diagnostic (displayed
198 before this trace) was inside the definition of the
199 macro. */
200 int resolved_def_loc_line = SOURCE_LINE (m, l);
201 if (ix == 0 && saved_location_line != resolved_def_loc_line)
203 diagnostic_append_note (context, resolved_def_loc,
204 "in definition of macro %qs",
205 linemap_map_get_macro_name (iter->map));
206 /* At this step, as we've printed the context of the macro
207 definition, we don't want to print the context of its
208 expansion, otherwise, it'd be redundant. */
209 continue;
212 /* Resolve the location of the expansion point of the macro
213 which expansion gave the token represented by def_loc.
214 This is the locus 2/ of the earlier comment. */
215 location_t resolved_exp_loc =
216 linemap_resolve_location (line_table,
217 MACRO_MAP_EXPANSION_POINT_LOCATION (iter->map),
218 LRK_MACRO_DEFINITION_LOCATION, NULL);
220 diagnostic_append_note (context, resolved_exp_loc,
221 "in expansion of macro %qs",
222 linemap_map_get_macro_name (iter->map));
226 /* This is a diagnostic finalizer implementation that is aware of
227 virtual locations produced by libcpp.
229 It has to be called by the diagnostic finalizer of front ends that
230 uses libcpp and wish to get diagnostics involving tokens resulting
231 from macro expansion.
233 For a given location, if said location belongs to a token
234 resulting from a macro expansion, this starter prints the context
235 of the token. E.g, for multiply nested macro expansion, it
236 unwinds the nested macro expansions and prints them in a manner
237 that is similar to what is done for function call stacks, or
238 template instantiation contexts. */
239 void
240 virt_loc_aware_diagnostic_finalizer (diagnostic_context *context,
241 diagnostic_info *diagnostic)
243 maybe_unwind_expanded_macro_loc (context, diagnostic_location (diagnostic));
246 /* Default tree printer. Handles declarations only. */
247 bool
248 default_tree_printer (pretty_printer *pp, text_info *text, const char *spec,
249 int precision, bool wide, bool set_locus, bool hash,
250 bool *, const char **)
252 tree t;
254 /* FUTURE: %+x should set the locus. */
255 if (precision != 0 || wide || hash)
256 return false;
258 switch (*spec)
260 case 'E':
261 t = va_arg (*text->args_ptr, tree);
262 if (TREE_CODE (t) == IDENTIFIER_NODE)
264 pp_identifier (pp, IDENTIFIER_POINTER (t));
265 return true;
267 break;
269 case 'D':
270 t = va_arg (*text->args_ptr, tree);
271 if (VAR_P (t) && DECL_HAS_DEBUG_EXPR_P (t))
272 t = DECL_DEBUG_EXPR (t);
273 break;
275 case 'F':
276 case 'T':
277 t = va_arg (*text->args_ptr, tree);
278 break;
280 default:
281 return false;
284 if (set_locus)
285 text->set_location (0, DECL_SOURCE_LOCATION (t), SHOW_RANGE_WITH_CARET);
287 if (DECL_P (t))
289 const char *n = DECL_NAME (t)
290 ? identifier_to_locale (lang_hooks.decl_printable_name (t, 2))
291 : _("<anonymous>");
292 pp_string (pp, n);
294 else
295 dump_generic_node (pp, t, 0, TDF_SLIM, 0);
297 return true;
300 /* Set the locations of call sites along the inlining stack corresponding
301 to the DIAGNOSTIC location. */
303 static void
304 set_inlining_locations (diagnostic_context *,
305 diagnostic_info *diagnostic)
307 location_t loc = diagnostic_location (diagnostic);
308 tree block = LOCATION_BLOCK (loc);
310 /* Count the number of locations in system headers. When all are,
311 warnings are suppressed by -Wno-system-headers. Otherwise, they
312 involve some user code, possibly inlined into a function in a system
313 header, and are not treated as coming from system headers. */
314 unsigned nsyslocs = 0;
316 /* Use a reference to the vector of locations for convenience. */
317 auto &ilocs = diagnostic->m_iinfo.m_ilocs;
319 while (block && TREE_CODE (block) == BLOCK
320 && BLOCK_ABSTRACT_ORIGIN (block))
322 tree ao = BLOCK_ABSTRACT_ORIGIN (block);
323 if (TREE_CODE (ao) == FUNCTION_DECL)
325 if (!diagnostic->m_iinfo.m_ao)
326 diagnostic->m_iinfo.m_ao = block;
328 location_t bsloc = BLOCK_SOURCE_LOCATION (block);
329 ilocs.safe_push (bsloc);
330 if (in_system_header_at (bsloc))
331 ++nsyslocs;
333 else if (TREE_CODE (ao) != BLOCK)
334 break;
336 block = BLOCK_SUPERCONTEXT (block);
339 if (ilocs.length ())
341 /* When there is an inlining context use the macro expansion
342 location for the original location and bump up NSYSLOCS if
343 it's in a system header since it's not counted above. */
344 location_t sysloc = expansion_point_location_if_in_system_header (loc);
345 if (sysloc != loc)
347 loc = sysloc;
348 ++nsyslocs;
351 else
353 /* When there's no inlining context use the original location
354 and set NSYSLOCS accordingly. */
355 nsyslocs = in_system_header_at (loc) != 0;
358 ilocs.safe_push (loc);
360 /* Set if all locations are in a system header. */
361 diagnostic->m_iinfo.m_allsyslocs = nsyslocs == ilocs.length ();
363 if (tree *ao = pp_ti_abstract_origin (&diagnostic->message))
364 *ao = (tree)diagnostic->m_iinfo.m_ao;
367 /* Sets CONTEXT to use language independent diagnostics. */
368 void
369 tree_diagnostics_defaults (diagnostic_context *context)
371 diagnostic_starter (context) = default_tree_diagnostic_starter;
372 diagnostic_finalizer (context) = default_diagnostic_finalizer;
373 diagnostic_format_decoder (context) = default_tree_printer;
374 context->print_path = default_tree_diagnostic_path_printer;
375 context->make_json_for_path = default_tree_make_json_for_path;
376 context->set_locations_cb = set_inlining_locations;
377 context->m_client_data_hooks = make_compiler_data_hooks ();