]>
Commit | Line | Data |
---|---|---|
cf835838 JM |
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. | |
7adcbafe | 4 | Copyright (C) 1999-2022 Free Software Foundation, Inc. |
cf835838 JM |
5 | |
6 | This file is part of GCC. | |
7 | ||
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. | |
12 | ||
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. | |
17 | ||
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/>. */ | |
21 | ||
22 | #include "config.h" | |
23 | #include "system.h" | |
24 | #include "coretypes.h" | |
957060b5 | 25 | #include "tree.h" |
cf835838 | 26 | #include "diagnostic.h" |
21e41715 | 27 | #include "tree-pretty-print.h" |
da67acb9 | 28 | #include "gimple-pretty-print.h" |
cf835838 | 29 | #include "tree-diagnostic.h" |
6cf276dd | 30 | #include "diagnostic-client-data-hooks.h" |
cf835838 | 31 | #include "langhooks.h" |
21e41715 | 32 | #include "intl.h" |
cf835838 JM |
33 | |
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) | |
39 | { | |
329769b7 TS |
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); | |
cf835838 JM |
43 | } |
44 | ||
21e41715 | 45 | static void |
cf835838 JM |
46 | default_tree_diagnostic_starter (diagnostic_context *context, |
47 | diagnostic_info *diagnostic) | |
48 | { | |
49 | diagnostic_report_current_function (context, diagnostic); | |
243fbddd JM |
50 | pp_set_prefix (context->printer, diagnostic_build_prefix (context, |
51 | diagnostic)); | |
cf835838 | 52 | } |
07a0b324 TT |
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. */ | |
a79683d5 | 57 | struct loc_map_pair |
07a0b324 | 58 | { |
0e50b624 | 59 | const line_map_macro *map; |
620e594b | 60 | location_t where; |
a79683d5 | 61 | }; |
07a0b324 | 62 | |
07a0b324 TT |
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 | ||
165ca58d DS |
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 | |
07a0b324 TT |
96 | diagnostic (the lines containing the 'note:' string) is called the |
97 | unwound macro expansion trace. That's the part generated by this | |
1d72e96f | 98 | function. */ |
07a0b324 | 99 | |
4bc1899b | 100 | void |
07a0b324 | 101 | maybe_unwind_expanded_macro_loc (diagnostic_context *context, |
620e594b | 102 | location_t where) |
07a0b324 TT |
103 | { |
104 | const struct line_map *map; | |
8c681247 | 105 | auto_vec<loc_map_pair> loc_vec; |
07a0b324 | 106 | unsigned ix; |
8f5c9d66 | 107 | loc_map_pair loc, *iter; |
07a0b324 | 108 | |
4bc1899b DM |
109 | const location_t original_loc = where; |
110 | ||
07a0b324 TT |
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; | |
0e50b624 | 126 | loc.map = linemap_check_macro (map); |
07a0b324 | 127 | |
9771b263 | 128 | loc_vec.safe_push (loc); |
07a0b324 TT |
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, where, &map); | |
137 | } while (linemap_macro_expansion_map_p (map)); | |
138 | ||
1d72e96f MLI |
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. */ | |
0e50b624 | 141 | const line_map_ordinary *ord_map = linemap_check_ordinary (map); |
07a0b324 TT |
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. */ | |
dfa32261 | 146 | int saved_location_line = |
4bc1899b | 147 | expand_location_to_spelling_point (original_loc).line; |
dfa32261 | 148 | |
0e50b624 | 149 | if (!LINEMAP_SYSP (ord_map)) |
9771b263 | 150 | FOR_EACH_VEC_ELT (loc_vec, ix, iter) |
07a0b324 | 151 | { |
165ca58d DS |
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 | |
dfa32261 | 174 | anymore, otherwise it'd be redundant. */ |
07a0b324 TT |
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. */ | |
620e594b | 183 | location_t resolved_def_loc = |
07a0b324 TT |
184 | linemap_resolve_location (line_table, iter->where, |
185 | LRK_MACRO_DEFINITION_LOCATION, NULL); | |
186 | ||
c4ca1a09 DS |
187 | /* Don't print trace for locations that are reserved or from |
188 | within a system header. */ | |
0e50b624 | 189 | const line_map_ordinary *m = NULL; |
620e594b | 190 | location_t l = |
dfa32261 MLI |
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; | |
195 | ||
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) | |
202 | { | |
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; | |
210 | } | |
c4ca1a09 | 211 | |
07a0b324 TT |
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. */ | |
620e594b | 215 | location_t resolved_exp_loc = |
07a0b324 TT |
216 | linemap_resolve_location (line_table, |
217 | MACRO_MAP_EXPANSION_POINT_LOCATION (iter->map), | |
218 | LRK_MACRO_DEFINITION_LOCATION, NULL); | |
219 | ||
dfa32261 MLI |
220 | diagnostic_append_note (context, resolved_exp_loc, |
221 | "in expansion of macro %qs", | |
222 | linemap_map_get_macro_name (iter->map)); | |
07a0b324 | 223 | } |
07a0b324 TT |
224 | } |
225 | ||
226 | /* This is a diagnostic finalizer implementation that is aware of | |
227 | virtual locations produced by libcpp. | |
228 | ||
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. | |
232 | ||
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) | |
242 | { | |
4bc1899b | 243 | maybe_unwind_expanded_macro_loc (context, diagnostic_location (diagnostic)); |
07a0b324 | 244 | } |
21e41715 MLI |
245 | |
246 | /* Default tree printer. Handles declarations only. */ | |
c05c2380 | 247 | bool |
21e41715 | 248 | default_tree_printer (pretty_printer *pp, text_info *text, const char *spec, |
f012c8ef | 249 | int precision, bool wide, bool set_locus, bool hash, |
ce95abc4 | 250 | bool *, const char **) |
21e41715 MLI |
251 | { |
252 | tree t; | |
253 | ||
254 | /* FUTURE: %+x should set the locus. */ | |
255 | if (precision != 0 || wide || hash) | |
256 | return false; | |
257 | ||
258 | switch (*spec) | |
259 | { | |
260 | case 'E': | |
261 | t = va_arg (*text->args_ptr, tree); | |
262 | if (TREE_CODE (t) == IDENTIFIER_NODE) | |
263 | { | |
264 | pp_identifier (pp, IDENTIFIER_POINTER (t)); | |
265 | return true; | |
266 | } | |
267 | break; | |
268 | ||
269 | case 'D': | |
270 | t = va_arg (*text->args_ptr, tree); | |
8813a647 | 271 | if (VAR_P (t) && DECL_HAS_DEBUG_EXPR_P (t)) |
21e41715 MLI |
272 | t = DECL_DEBUG_EXPR (t); |
273 | break; | |
274 | ||
275 | case 'F': | |
276 | case 'T': | |
277 | t = va_arg (*text->args_ptr, tree); | |
278 | break; | |
279 | ||
21e41715 MLI |
280 | default: |
281 | return false; | |
282 | } | |
283 | ||
2a2703a2 | 284 | if (set_locus) |
85204e23 | 285 | text->set_location (0, DECL_SOURCE_LOCATION (t), SHOW_RANGE_WITH_CARET); |
21e41715 MLI |
286 | |
287 | if (DECL_P (t)) | |
288 | { | |
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); | |
293 | } | |
294 | else | |
9d9573d5 | 295 | dump_generic_node (pp, t, 0, TDF_SLIM, 0); |
21e41715 MLI |
296 | |
297 | return true; | |
298 | } | |
299 | ||
6feb628a MS |
300 | /* Set the locations of call sites along the inlining stack corresponding |
301 | to the DIAGNOSTIC location. */ | |
302 | ||
303 | static void | |
304 | set_inlining_locations (diagnostic_context *, | |
305 | diagnostic_info *diagnostic) | |
306 | { | |
307 | location_t loc = diagnostic_location (diagnostic); | |
308 | tree block = LOCATION_BLOCK (loc); | |
309 | ||
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; | |
315 | ||
316 | /* Use a reference to the vector of locations for convenience. */ | |
317 | auto &ilocs = diagnostic->m_iinfo.m_ilocs; | |
318 | ||
319 | while (block && TREE_CODE (block) == BLOCK | |
320 | && BLOCK_ABSTRACT_ORIGIN (block)) | |
321 | { | |
322 | tree ao = BLOCK_ABSTRACT_ORIGIN (block); | |
323 | if (TREE_CODE (ao) == FUNCTION_DECL) | |
324 | { | |
325 | if (!diagnostic->m_iinfo.m_ao) | |
326 | diagnostic->m_iinfo.m_ao = block; | |
327 | ||
328 | location_t bsloc = BLOCK_SOURCE_LOCATION (block); | |
329 | ilocs.safe_push (bsloc); | |
330 | if (in_system_header_at (bsloc)) | |
331 | ++nsyslocs; | |
332 | } | |
333 | else if (TREE_CODE (ao) != BLOCK) | |
334 | break; | |
335 | ||
336 | block = BLOCK_SUPERCONTEXT (block); | |
337 | } | |
338 | ||
339 | if (ilocs.length ()) | |
340 | { | |
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) | |
346 | { | |
347 | loc = sysloc; | |
348 | ++nsyslocs; | |
349 | } | |
350 | } | |
351 | else | |
352 | { | |
353 | /* When there's no inlining context use the original location | |
354 | and set NSYSLOCS accordingly. */ | |
355 | nsyslocs = in_system_header_at (loc) != 0; | |
356 | } | |
357 | ||
358 | ilocs.safe_push (loc); | |
359 | ||
360 | /* Set if all locations are in a system header. */ | |
361 | diagnostic->m_iinfo.m_allsyslocs = nsyslocs == ilocs.length (); | |
362 | ||
363 | if (tree *ao = pp_ti_abstract_origin (&diagnostic->message)) | |
364 | *ao = (tree)diagnostic->m_iinfo.m_ao; | |
365 | } | |
366 | ||
21e41715 MLI |
367 | /* Sets CONTEXT to use language independent diagnostics. */ |
368 | void | |
369 | tree_diagnostics_defaults (diagnostic_context *context) | |
370 | { | |
371 | diagnostic_starter (context) = default_tree_diagnostic_starter; | |
372 | diagnostic_finalizer (context) = default_diagnostic_finalizer; | |
373 | diagnostic_format_decoder (context) = default_tree_printer; | |
4bc1899b DM |
374 | context->print_path = default_tree_diagnostic_path_printer; |
375 | context->make_json_for_path = default_tree_make_json_for_path; | |
6feb628a | 376 | context->set_locations_cb = set_inlining_locations; |
6cf276dd | 377 | context->m_client_data_hooks = make_compiler_data_hooks (); |
21e41715 | 378 | } |