* configure: Regenerated.
[official-gcc.git] / gcc / tree-diagnostic.c
blob2756ed21374d254fb4006e6e4261c81f1505c057
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, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
5 2009, 2010 Free Software Foundation, Inc.
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 3, or (at your option) any later
12 version.
14 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 for more details.
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
23 #include "config.h"
24 #include "system.h"
25 #include "coretypes.h"
26 #include "tree.h"
27 #include "diagnostic.h"
28 #include "tree-pretty-print.h"
29 #include "tree-diagnostic.h"
30 #include "dumpfile.h" /* TDF_DIAGNOSTIC */
31 #include "langhooks.h"
32 #include "langhooks-def.h"
33 #include "vec.h"
34 #include "intl.h"
36 /* Prints out, if necessary, the name of the current function
37 that caused an error. Called from all error and warning functions. */
38 void
39 diagnostic_report_current_function (diagnostic_context *context,
40 diagnostic_info *diagnostic)
42 diagnostic_report_current_module (context, diagnostic->location);
43 lang_hooks.print_error_function (context, input_filename, diagnostic);
46 static void
47 default_tree_diagnostic_starter (diagnostic_context *context,
48 diagnostic_info *diagnostic)
50 diagnostic_report_current_function (context, diagnostic);
51 pp_set_prefix (context->printer, diagnostic_build_prefix (context,
52 diagnostic));
55 /* This is a pair made of a location and the line map it originated
56 from. It's used in the maybe_unwind_expanded_macro_loc function
57 below. */
58 typedef struct
60 const struct line_map *map;
61 source_location where;
62 } loc_map_pair;
64 DEF_VEC_O (loc_map_pair);
65 DEF_VEC_ALLOC_O (loc_map_pair, heap);
67 /* Unwind the different macro expansions that lead to the token which
68 location is WHERE and emit diagnostics showing the resulting
69 unwound macro expansion trace. Let's look at an example to see how
70 the trace looks like. Suppose we have this piece of code,
71 artificially annotated with the line numbers to increase
72 legibility:
74 $ cat -n test.c
75 1 #define OPERATE(OPRD1, OPRT, OPRD2) \
76 2 OPRD1 OPRT OPRD2;
78 4 #define SHIFTL(A,B) \
79 5 OPERATE (A,<<,B)
81 7 #define MULT(A) \
82 8 SHIFTL (A,1)
84 10 void
85 11 g ()
86 12 {
87 13 MULT (1.0);// 1.0 << 1; <-- so this is an error.
88 14 }
90 Here is the diagnostic that we want the compiler to generate:
92 test.c: In function ‘g’:
93 test.c:5:14: error: invalid operands to binary << (have ‘double’ and ‘int’)
94 test.c:2:9: note: in definition of macro 'OPERATE'
95 test.c:8:3: note: in expansion of macro 'SHIFTL'
96 test.c:13:3: note: in expansion of macro 'MULT'
98 The part that goes from the third to the fifth line of this
99 diagnostic (the lines containing the 'note:' string) is called the
100 unwound macro expansion trace. That's the part generated by this
101 function. */
103 static void
104 maybe_unwind_expanded_macro_loc (diagnostic_context *context,
105 diagnostic_info *diagnostic,
106 source_location where)
108 const struct line_map *map;
109 VEC(loc_map_pair,heap) *loc_vec = NULL;
110 unsigned ix;
111 loc_map_pair loc, *iter;
113 map = linemap_lookup (line_table, where);
114 if (!linemap_macro_expansion_map_p (map))
115 return;
117 /* Let's unwind the macros that got expanded and led to the token
118 which location is WHERE. We are going to store these macros into
119 LOC_VEC, so that we can later walk it at our convenience to
120 display a somewhat meaningful trace of the macro expansion
121 history to the user. Note that the first macro of the trace
122 (which is OPERATE in the example above) is going to be stored at
123 the beginning of LOC_VEC. */
127 loc.where = where;
128 loc.map = map;
130 VEC_safe_push (loc_map_pair, heap, loc_vec, loc);
132 /* WHERE is the location of a token inside the expansion of a
133 macro. MAP is the map holding the locations of that macro
134 expansion. Let's get the location of the token inside the
135 context that triggered the expansion of this macro.
136 This is basically how we go "down" in the trace of macro
137 expansions that led to WHERE. */
138 where = linemap_unwind_toward_expansion (line_table, where, &map);
139 } while (linemap_macro_expansion_map_p (map));
141 /* Now map is set to the map of the location in the source that
142 first triggered the macro expansion. This must be an ordinary map. */
144 /* Walk LOC_VEC and print the macro expansion trace, unless the
145 first macro which expansion triggered this trace was expanded
146 inside a system header. */
147 if (!LINEMAP_SYSP (map))
148 FOR_EACH_VEC_ELT (loc_map_pair, loc_vec, ix, iter)
150 source_location resolved_def_loc = 0, resolved_exp_loc = 0,
151 saved_location = 0;
152 int resolved_def_loc_line = 0, saved_location_line = 0;
153 diagnostic_t saved_kind;
154 const char *saved_prefix;
155 /* Sometimes, in the unwound macro expansion trace, we want to
156 print a part of the context that shows where, in the
157 definition of the relevant macro, is the token (we are
158 looking at) used. That is the case in the introductory
159 comment of this function, where we print:
161 test.c:2:9: note: in definition of macro 'OPERATE'.
163 We print that "macro definition context" because the
164 diagnostic line (emitted by the call to
165 pp_ouput_formatted_text in diagnostic_report_diagnostic):
167 test.c:5:14: error: invalid operands to binary << (have ‘double’ and ‘int’)
169 does not point into the definition of the macro where the
170 token '<<' (that is an argument to the function-like macro
171 OPERATE) is used. So we must "display" the line of that
172 macro definition context to the user somehow.
174 A contrario, when the first interesting diagnostic line
175 points into the definition of the macro, we don't need to
176 display any line for that macro definition in the trace
177 anymore, otherwise it'd be redundant.
179 This flag is true when we need to display the context of
180 the macro definition. */
181 bool print_definition_context_p = false;
183 /* Okay, now here is what we want. For each token resulting
184 from macro expansion we want to show: 1/ where in the
185 definition of the macro the token comes from; 2/ where the
186 macro got expanded. */
188 /* Resolve the location iter->where into the locus 1/ of the
189 comment above. */
190 resolved_def_loc =
191 linemap_resolve_location (line_table, iter->where,
192 LRK_MACRO_DEFINITION_LOCATION, NULL);
194 /* Don't print trace for locations that are reserved or from
195 within a system header. */
197 const struct line_map *m = NULL;
198 source_location l = linemap_resolve_location (line_table, resolved_def_loc,
199 LRK_SPELLING_LOCATION,
200 &m);
201 if (l < RESERVED_LOCATION_COUNT
202 || LINEMAP_SYSP (m))
203 continue;
205 resolved_def_loc_line = SOURCE_LINE (m, l);
208 /* Resolve the location of the expansion point of the macro
209 which expansion gave the token represented by def_loc.
210 This is the locus 2/ of the earlier comment. */
211 resolved_exp_loc =
212 linemap_resolve_location (line_table,
213 MACRO_MAP_EXPANSION_POINT_LOCATION (iter->map),
214 LRK_MACRO_DEFINITION_LOCATION, NULL);
216 saved_kind = diagnostic->kind;
217 saved_prefix = pp_get_prefix (context->printer);
218 saved_location = diagnostic->location;
219 saved_location_line =
220 expand_location_to_spelling_point (saved_location).line;
222 diagnostic->kind = DK_NOTE;
224 /* We need to print the context of the macro definition only
225 when the locus of the first displayed diagnostic (displayed
226 before this trace) was inside the definition of the
227 macro. */
228 print_definition_context_p =
229 (ix == 0 && (saved_location_line != resolved_def_loc_line));
231 if (print_definition_context_p)
233 diagnostic->location = resolved_def_loc;
234 pp_set_prefix (context->printer,
235 diagnostic_build_prefix (context, diagnostic));
236 pp_newline (context->printer);
237 pp_printf (context->printer, "in definition of macro '%s'",
238 linemap_map_get_macro_name (iter->map));
239 pp_destroy_prefix (context->printer);
240 diagnostic_show_locus (context, diagnostic);
241 /* At this step, as we've printed the context of the macro
242 definition, we don't want to print the context of its
243 expansion, otherwise, it'd be redundant. */
244 continue;
247 diagnostic->location = resolved_exp_loc;
248 pp_set_prefix (context->printer,
249 diagnostic_build_prefix (context, diagnostic));
250 pp_newline (context->printer);
251 pp_printf (context->printer, "in expansion of macro '%s'",
252 linemap_map_get_macro_name (iter->map));
253 pp_destroy_prefix (context->printer);
254 diagnostic_show_locus (context, diagnostic);
256 diagnostic->kind = saved_kind;
257 diagnostic->location = saved_location;
258 pp_set_prefix (context->printer, saved_prefix);
261 VEC_free (loc_map_pair, heap, loc_vec);
264 /* This is a diagnostic finalizer implementation that is aware of
265 virtual locations produced by libcpp.
267 It has to be called by the diagnostic finalizer of front ends that
268 uses libcpp and wish to get diagnostics involving tokens resulting
269 from macro expansion.
271 For a given location, if said location belongs to a token
272 resulting from a macro expansion, this starter prints the context
273 of the token. E.g, for multiply nested macro expansion, it
274 unwinds the nested macro expansions and prints them in a manner
275 that is similar to what is done for function call stacks, or
276 template instantiation contexts. */
277 void
278 virt_loc_aware_diagnostic_finalizer (diagnostic_context *context,
279 diagnostic_info *diagnostic)
281 maybe_unwind_expanded_macro_loc (context, diagnostic,
282 diagnostic->location);
285 /* Default tree printer. Handles declarations only. */
286 static bool
287 default_tree_printer (pretty_printer *pp, text_info *text, const char *spec,
288 int precision, bool wide, bool set_locus, bool hash)
290 tree t;
292 /* FUTURE: %+x should set the locus. */
293 if (precision != 0 || wide || hash)
294 return false;
296 switch (*spec)
298 case 'E':
299 t = va_arg (*text->args_ptr, tree);
300 if (TREE_CODE (t) == IDENTIFIER_NODE)
302 pp_identifier (pp, IDENTIFIER_POINTER (t));
303 return true;
305 break;
307 case 'D':
308 t = va_arg (*text->args_ptr, tree);
309 if (DECL_DEBUG_EXPR_IS_FROM (t) && DECL_DEBUG_EXPR (t))
310 t = DECL_DEBUG_EXPR (t);
311 break;
313 case 'F':
314 case 'T':
315 t = va_arg (*text->args_ptr, tree);
316 break;
318 case 'K':
319 percent_K_format (text);
320 return true;
322 default:
323 return false;
326 if (set_locus && text->locus)
327 *text->locus = DECL_SOURCE_LOCATION (t);
329 if (DECL_P (t))
331 const char *n = DECL_NAME (t)
332 ? identifier_to_locale (lang_hooks.decl_printable_name (t, 2))
333 : _("<anonymous>");
334 pp_string (pp, n);
336 else
337 dump_generic_node (pp, t, 0, TDF_DIAGNOSTIC, 0);
339 return true;
342 /* Sets CONTEXT to use language independent diagnostics. */
343 void
344 tree_diagnostics_defaults (diagnostic_context *context)
346 diagnostic_starter (context) = default_tree_diagnostic_starter;
347 diagnostic_finalizer (context) = default_diagnostic_finalizer;
348 diagnostic_format_decoder (context) = default_tree_printer;