2015-06-25 Zhouyi Zhou <yizhouzhou@ict.ac.cn>
[official-gcc.git] / gcc / tree-diagnostic.c
blob09ffe0c4221c3378eba072caa7cd06bddb100252
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 "alias.h"
26 #include "symtab.h"
27 #include "options.h"
28 #include "tree.h"
29 #include "diagnostic.h"
30 #include "tree-pretty-print.h"
31 #include "tree-diagnostic.h"
32 #include "dumpfile.h" /* TDF_DIAGNOSTIC */
33 #include "langhooks.h"
34 #include "langhooks-def.h"
35 #include "intl.h"
37 /* Prints out, if necessary, the name of the current function
38 that caused an error. Called from all error and warning functions. */
39 void
40 diagnostic_report_current_function (diagnostic_context *context,
41 diagnostic_info *diagnostic)
43 diagnostic_report_current_module (context, diagnostic_location (diagnostic));
44 lang_hooks.print_error_function (context, LOCATION_FILE (input_location),
45 diagnostic);
48 static void
49 default_tree_diagnostic_starter (diagnostic_context *context,
50 diagnostic_info *diagnostic)
52 diagnostic_report_current_function (context, diagnostic);
53 pp_set_prefix (context->printer, diagnostic_build_prefix (context,
54 diagnostic));
57 /* This is a pair made of a location and the line map it originated
58 from. It's used in the maybe_unwind_expanded_macro_loc function
59 below. */
60 typedef struct
62 const line_map_macro *map;
63 source_location where;
64 } loc_map_pair;
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 const diagnostic_info *diagnostic,
106 source_location where)
108 const struct line_map *map;
109 vec<loc_map_pair> loc_vec = vNULL;
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 = linemap_check_macro (map);
130 loc_vec.safe_push (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. */
143 const line_map_ordinary *ord_map = linemap_check_ordinary (map);
145 /* Walk LOC_VEC and print the macro expansion trace, unless the
146 first macro which expansion triggered this trace was expanded
147 inside a system header. */
148 int saved_location_line =
149 expand_location_to_spelling_point (diagnostic_location (diagnostic)).line;
151 if (!LINEMAP_SYSP (ord_map))
152 FOR_EACH_VEC_ELT (loc_vec, ix, iter)
154 /* Sometimes, in the unwound macro expansion trace, we want to
155 print a part of the context that shows where, in the
156 definition of the relevant macro, is the token (we are
157 looking at) used. That is the case in the introductory
158 comment of this function, where we print:
160 test.c:2:9: note: in definition of macro 'OPERATE'.
162 We print that "macro definition context" because the
163 diagnostic line (emitted by the call to
164 pp_ouput_formatted_text in diagnostic_report_diagnostic):
166 test.c:5:14: error: invalid operands to binary << (have ‘double’ and ‘int’)
168 does not point into the definition of the macro where the
169 token '<<' (that is an argument to the function-like macro
170 OPERATE) is used. So we must "display" the line of that
171 macro definition context to the user somehow.
173 A contrario, when the first interesting diagnostic line
174 points into the definition of the macro, we don't need to
175 display any line for that macro definition in the trace
176 anymore, otherwise it'd be redundant. */
178 /* Okay, now here is what we want. For each token resulting
179 from macro expansion we want to show: 1/ where in the
180 definition of the macro the token comes from; 2/ where the
181 macro got expanded. */
183 /* Resolve the location iter->where into the locus 1/ of the
184 comment above. */
185 source_location resolved_def_loc =
186 linemap_resolve_location (line_table, iter->where,
187 LRK_MACRO_DEFINITION_LOCATION, NULL);
189 /* Don't print trace for locations that are reserved or from
190 within a system header. */
191 const line_map_ordinary *m = NULL;
192 source_location l =
193 linemap_resolve_location (line_table, resolved_def_loc,
194 LRK_SPELLING_LOCATION, &m);
195 if (l < RESERVED_LOCATION_COUNT || LINEMAP_SYSP (m))
196 continue;
198 /* We need to print the context of the macro definition only
199 when the locus of the first displayed diagnostic (displayed
200 before this trace) was inside the definition of the
201 macro. */
202 int resolved_def_loc_line = SOURCE_LINE (m, l);
203 if (ix == 0 && saved_location_line != resolved_def_loc_line)
205 diagnostic_append_note (context, resolved_def_loc,
206 "in definition of macro %qs",
207 linemap_map_get_macro_name (iter->map));
208 /* At this step, as we've printed the context of the macro
209 definition, we don't want to print the context of its
210 expansion, otherwise, it'd be redundant. */
211 continue;
214 /* Resolve the location of the expansion point of the macro
215 which expansion gave the token represented by def_loc.
216 This is the locus 2/ of the earlier comment. */
217 source_location resolved_exp_loc =
218 linemap_resolve_location (line_table,
219 MACRO_MAP_EXPANSION_POINT_LOCATION (iter->map),
220 LRK_MACRO_DEFINITION_LOCATION, NULL);
222 diagnostic_append_note (context, resolved_exp_loc,
223 "in expansion of macro %qs",
224 linemap_map_get_macro_name (iter->map));
227 loc_vec.release ();
230 /* This is a diagnostic finalizer implementation that is aware of
231 virtual locations produced by libcpp.
233 It has to be called by the diagnostic finalizer of front ends that
234 uses libcpp and wish to get diagnostics involving tokens resulting
235 from macro expansion.
237 For a given location, if said location belongs to a token
238 resulting from a macro expansion, this starter prints the context
239 of the token. E.g, for multiply nested macro expansion, it
240 unwinds the nested macro expansions and prints them in a manner
241 that is similar to what is done for function call stacks, or
242 template instantiation contexts. */
243 void
244 virt_loc_aware_diagnostic_finalizer (diagnostic_context *context,
245 diagnostic_info *diagnostic)
247 maybe_unwind_expanded_macro_loc (context, diagnostic,
248 diagnostic_location (diagnostic));
251 /* Default tree printer. Handles declarations only. */
252 static bool
253 default_tree_printer (pretty_printer *pp, text_info *text, const char *spec,
254 int precision, bool wide, bool set_locus, bool hash)
256 tree t;
258 /* FUTURE: %+x should set the locus. */
259 if (precision != 0 || wide || hash)
260 return false;
262 switch (*spec)
264 case 'E':
265 t = va_arg (*text->args_ptr, tree);
266 if (TREE_CODE (t) == IDENTIFIER_NODE)
268 pp_identifier (pp, IDENTIFIER_POINTER (t));
269 return true;
271 break;
273 case 'D':
274 t = va_arg (*text->args_ptr, tree);
275 if (TREE_CODE (t) == VAR_DECL && DECL_HAS_DEBUG_EXPR_P (t))
276 t = DECL_DEBUG_EXPR (t);
277 break;
279 case 'F':
280 case 'T':
281 t = va_arg (*text->args_ptr, tree);
282 break;
284 case 'K':
285 percent_K_format (text);
286 return true;
288 default:
289 return false;
292 if (set_locus)
293 text->set_location (0, DECL_SOURCE_LOCATION (t));
295 if (DECL_P (t))
297 const char *n = DECL_NAME (t)
298 ? identifier_to_locale (lang_hooks.decl_printable_name (t, 2))
299 : _("<anonymous>");
300 pp_string (pp, n);
302 else
303 dump_generic_node (pp, t, 0, TDF_DIAGNOSTIC, 0);
305 return true;
308 /* Sets CONTEXT to use language independent diagnostics. */
309 void
310 tree_diagnostics_defaults (diagnostic_context *context)
312 diagnostic_starter (context) = default_tree_diagnostic_starter;
313 diagnostic_finalizer (context) = default_diagnostic_finalizer;
314 diagnostic_format_decoder (context) = default_tree_printer;