2015-05-22 Bob Duff <duff@adacore.com>
[official-gcc.git] / gcc / tree-diagnostic.c
blob7461a94d6ba841567c8e6a1eb4aa3fbbec11f1cd
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-2015 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 "hash-set.h"
26 #include "machmode.h"
27 #include "vec.h"
28 #include "double-int.h"
29 #include "input.h"
30 #include "alias.h"
31 #include "symtab.h"
32 #include "options.h"
33 #include "wide-int.h"
34 #include "inchash.h"
35 #include "tree.h"
36 #include "diagnostic.h"
37 #include "tree-pretty-print.h"
38 #include "tree-diagnostic.h"
39 #include "dumpfile.h" /* TDF_DIAGNOSTIC */
40 #include "langhooks.h"
41 #include "langhooks-def.h"
42 #include "vec.h"
43 #include "intl.h"
45 /* Prints out, if necessary, the name of the current function
46 that caused an error. Called from all error and warning functions. */
47 void
48 diagnostic_report_current_function (diagnostic_context *context,
49 diagnostic_info *diagnostic)
51 diagnostic_report_current_module (context, diagnostic_location (diagnostic));
52 lang_hooks.print_error_function (context, LOCATION_FILE (input_location),
53 diagnostic);
56 static void
57 default_tree_diagnostic_starter (diagnostic_context *context,
58 diagnostic_info *diagnostic)
60 diagnostic_report_current_function (context, diagnostic);
61 pp_set_prefix (context->printer, diagnostic_build_prefix (context,
62 diagnostic));
65 /* This is a pair made of a location and the line map it originated
66 from. It's used in the maybe_unwind_expanded_macro_loc function
67 below. */
68 typedef struct
70 const line_map_macro *map;
71 source_location where;
72 } loc_map_pair;
75 /* Unwind the different macro expansions that lead to the token which
76 location is WHERE and emit diagnostics showing the resulting
77 unwound macro expansion trace. Let's look at an example to see how
78 the trace looks like. Suppose we have this piece of code,
79 artificially annotated with the line numbers to increase
80 legibility:
82 $ cat -n test.c
83 1 #define OPERATE(OPRD1, OPRT, OPRD2) \
84 2 OPRD1 OPRT OPRD2;
86 4 #define SHIFTL(A,B) \
87 5 OPERATE (A,<<,B)
89 7 #define MULT(A) \
90 8 SHIFTL (A,1)
92 10 void
93 11 g ()
94 12 {
95 13 MULT (1.0);// 1.0 << 1; <-- so this is an error.
96 14 }
98 Here is the diagnostic that we want the compiler to generate:
100 test.c: In function ‘g’:
101 test.c:5:14: error: invalid operands to binary << (have ‘double’ and ‘int’)
102 test.c:2:9: note: in definition of macro 'OPERATE'
103 test.c:8:3: note: in expansion of macro 'SHIFTL'
104 test.c:13:3: note: in expansion of macro 'MULT'
106 The part that goes from the third to the fifth line of this
107 diagnostic (the lines containing the 'note:' string) is called the
108 unwound macro expansion trace. That's the part generated by this
109 function. */
111 static void
112 maybe_unwind_expanded_macro_loc (diagnostic_context *context,
113 const diagnostic_info *diagnostic,
114 source_location where)
116 const struct line_map *map;
117 vec<loc_map_pair> loc_vec = vNULL;
118 unsigned ix;
119 loc_map_pair loc, *iter;
121 map = linemap_lookup (line_table, where);
122 if (!linemap_macro_expansion_map_p (map))
123 return;
125 /* Let's unwind the macros that got expanded and led to the token
126 which location is WHERE. We are going to store these macros into
127 LOC_VEC, so that we can later walk it at our convenience to
128 display a somewhat meaningful trace of the macro expansion
129 history to the user. Note that the first macro of the trace
130 (which is OPERATE in the example above) is going to be stored at
131 the beginning of LOC_VEC. */
135 loc.where = where;
136 loc.map = linemap_check_macro (map);
138 loc_vec.safe_push (loc);
140 /* WHERE is the location of a token inside the expansion of a
141 macro. MAP is the map holding the locations of that macro
142 expansion. Let's get the location of the token inside the
143 context that triggered the expansion of this macro.
144 This is basically how we go "down" in the trace of macro
145 expansions that led to WHERE. */
146 where = linemap_unwind_toward_expansion (line_table, where, &map);
147 } while (linemap_macro_expansion_map_p (map));
149 /* Now map is set to the map of the location in the source that
150 first triggered the macro expansion. This must be an ordinary map. */
151 const line_map_ordinary *ord_map = linemap_check_ordinary (map);
153 /* Walk LOC_VEC and print the macro expansion trace, unless the
154 first macro which expansion triggered this trace was expanded
155 inside a system header. */
156 int saved_location_line =
157 expand_location_to_spelling_point (diagnostic_location (diagnostic)).line;
159 if (!LINEMAP_SYSP (ord_map))
160 FOR_EACH_VEC_ELT (loc_vec, ix, iter)
162 /* Sometimes, in the unwound macro expansion trace, we want to
163 print a part of the context that shows where, in the
164 definition of the relevant macro, is the token (we are
165 looking at) used. That is the case in the introductory
166 comment of this function, where we print:
168 test.c:2:9: note: in definition of macro 'OPERATE'.
170 We print that "macro definition context" because the
171 diagnostic line (emitted by the call to
172 pp_ouput_formatted_text in diagnostic_report_diagnostic):
174 test.c:5:14: error: invalid operands to binary << (have ‘double’ and ‘int’)
176 does not point into the definition of the macro where the
177 token '<<' (that is an argument to the function-like macro
178 OPERATE) is used. So we must "display" the line of that
179 macro definition context to the user somehow.
181 A contrario, when the first interesting diagnostic line
182 points into the definition of the macro, we don't need to
183 display any line for that macro definition in the trace
184 anymore, otherwise it'd be redundant. */
186 /* Okay, now here is what we want. For each token resulting
187 from macro expansion we want to show: 1/ where in the
188 definition of the macro the token comes from; 2/ where the
189 macro got expanded. */
191 /* Resolve the location iter->where into the locus 1/ of the
192 comment above. */
193 source_location resolved_def_loc =
194 linemap_resolve_location (line_table, iter->where,
195 LRK_MACRO_DEFINITION_LOCATION, NULL);
197 /* Don't print trace for locations that are reserved or from
198 within a system header. */
199 const line_map_ordinary *m = NULL;
200 source_location l =
201 linemap_resolve_location (line_table, resolved_def_loc,
202 LRK_SPELLING_LOCATION, &m);
203 if (l < RESERVED_LOCATION_COUNT || LINEMAP_SYSP (m))
204 continue;
206 /* We need to print the context of the macro definition only
207 when the locus of the first displayed diagnostic (displayed
208 before this trace) was inside the definition of the
209 macro. */
210 int resolved_def_loc_line = SOURCE_LINE (m, l);
211 if (ix == 0 && saved_location_line != resolved_def_loc_line)
213 diagnostic_append_note (context, resolved_def_loc,
214 "in definition of macro %qs",
215 linemap_map_get_macro_name (iter->map));
216 /* At this step, as we've printed the context of the macro
217 definition, we don't want to print the context of its
218 expansion, otherwise, it'd be redundant. */
219 continue;
222 /* Resolve the location of the expansion point of the macro
223 which expansion gave the token represented by def_loc.
224 This is the locus 2/ of the earlier comment. */
225 source_location resolved_exp_loc =
226 linemap_resolve_location (line_table,
227 MACRO_MAP_EXPANSION_POINT_LOCATION (iter->map),
228 LRK_MACRO_DEFINITION_LOCATION, NULL);
230 diagnostic_append_note (context, resolved_exp_loc,
231 "in expansion of macro %qs",
232 linemap_map_get_macro_name (iter->map));
235 loc_vec.release ();
238 /* This is a diagnostic finalizer implementation that is aware of
239 virtual locations produced by libcpp.
241 It has to be called by the diagnostic finalizer of front ends that
242 uses libcpp and wish to get diagnostics involving tokens resulting
243 from macro expansion.
245 For a given location, if said location belongs to a token
246 resulting from a macro expansion, this starter prints the context
247 of the token. E.g, for multiply nested macro expansion, it
248 unwinds the nested macro expansions and prints them in a manner
249 that is similar to what is done for function call stacks, or
250 template instantiation contexts. */
251 void
252 virt_loc_aware_diagnostic_finalizer (diagnostic_context *context,
253 diagnostic_info *diagnostic)
255 maybe_unwind_expanded_macro_loc (context, diagnostic,
256 diagnostic_location (diagnostic));
259 /* Default tree printer. Handles declarations only. */
260 static bool
261 default_tree_printer (pretty_printer *pp, text_info *text, const char *spec,
262 int precision, bool wide, bool set_locus, bool hash)
264 tree t;
266 /* FUTURE: %+x should set the locus. */
267 if (precision != 0 || wide || hash)
268 return false;
270 switch (*spec)
272 case 'E':
273 t = va_arg (*text->args_ptr, tree);
274 if (TREE_CODE (t) == IDENTIFIER_NODE)
276 pp_identifier (pp, IDENTIFIER_POINTER (t));
277 return true;
279 break;
281 case 'D':
282 t = va_arg (*text->args_ptr, tree);
283 if (TREE_CODE (t) == VAR_DECL && DECL_HAS_DEBUG_EXPR_P (t))
284 t = DECL_DEBUG_EXPR (t);
285 break;
287 case 'F':
288 case 'T':
289 t = va_arg (*text->args_ptr, tree);
290 break;
292 case 'K':
293 percent_K_format (text);
294 return true;
296 default:
297 return false;
300 if (set_locus)
301 text->set_location (0, DECL_SOURCE_LOCATION (t));
303 if (DECL_P (t))
305 const char *n = DECL_NAME (t)
306 ? identifier_to_locale (lang_hooks.decl_printable_name (t, 2))
307 : _("<anonymous>");
308 pp_string (pp, n);
310 else
311 dump_generic_node (pp, t, 0, TDF_DIAGNOSTIC, 0);
313 return true;
316 /* Sets CONTEXT to use language independent diagnostics. */
317 void
318 tree_diagnostics_defaults (diagnostic_context *context)
320 diagnostic_starter (context) = default_tree_diagnostic_starter;
321 diagnostic_finalizer (context) = default_diagnostic_finalizer;
322 diagnostic_format_decoder (context) = default_tree_printer;