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
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
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/>. */
25 #include "coretypes.h"
27 #include "diagnostic.h"
28 #include "tree-pretty-print.h"
29 #include "tree-diagnostic.h"
30 #include "tree-pass.h" /* TDF_DIAGNOSTIC */
31 #include "langhooks.h"
32 #include "langhooks-def.h"
36 /* Prints out, if necessary, the name of the current function
37 that caused an error. Called from all error and warning functions. */
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
);
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
,
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
60 const struct line_map
*map
;
61 source_location where
;
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
75 1 #define OPERATE(OPRD1, OPRT, OPRD2) \
78 4 #define SHIFTL(A,B) \
87 13 MULT (1.0);// 1.0 << 1; <-- so this is an error.
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 expansion of macro 'OPERATE'
95 test.c:5:3: note: expanded from here
96 test.c:5:14: note: in expansion of macro 'SHIFTL'
97 test.c:8:3: note: expanded from here
98 test.c:8:3: note: in expansion of macro 'MULT'
99 test.c:13:3: note: expanded from here
101 The part that goes from the third to the eighth line of this
102 diagnostic (the lines containing the 'note:' string) is called the
103 unwound macro expansion trace. That's the part generated by this
107 maybe_unwind_expanded_macro_loc (diagnostic_context
*context
,
108 diagnostic_info
*diagnostic
,
109 source_location where
)
111 const struct line_map
*map
;
112 VEC(loc_map_pair
,heap
) *loc_vec
= NULL
;
114 loc_map_pair loc
, *iter
;
116 map
= linemap_lookup (line_table
, where
);
117 if (!linemap_macro_expansion_map_p (map
))
120 /* Let's unwind the macros that got expanded and led to the token
121 which location is WHERE. We are going to store these macros into
122 LOC_VEC, so that we can later walk it at our convenience to
123 display a somewhat meaningful trace of the macro expansion
124 history to the user. Note that the first macro of the trace
125 (which is OPERATE in the example above) is going to be stored at
126 the beginning of LOC_VEC. */
133 VEC_safe_push (loc_map_pair
, heap
, loc_vec
, &loc
);
135 /* WHERE is the location of a token inside the expansion of a
136 macro. MAP is the map holding the locations of that macro
137 expansion. Let's get the location of the token inside the
138 context that triggered the expansion of this macro.
139 This is basically how we go "down" in the trace of macro
140 expansions that led to WHERE. */
141 where
= linemap_unwind_toward_expansion (line_table
, where
, &map
);
142 } while (linemap_macro_expansion_map_p (map
));
144 /* Now map is set to the map of the location in the source that
145 first triggered the macro expansion. This must be an ordinary map. */
147 /* Walk LOC_VEC and print the macro expansion trace, unless the
148 first macro which expansion triggered this trace was expanded
149 inside a system header. */
150 if (!LINEMAP_SYSP (map
))
151 FOR_EACH_VEC_ELT (loc_map_pair
, loc_vec
, ix
, iter
)
153 source_location resolved_def_loc
= 0, resolved_exp_loc
= 0;
154 diagnostic_t saved_kind
;
155 const char *saved_prefix
;
156 source_location saved_location
;
158 /* Okay, now here is what we want. For each token resulting
159 from macro expansion we want to show: 1/ where in the
160 definition of the macro the token comes from; 2/ where the
161 macro got expanded. */
163 /* Resolve the location iter->where into the locus 1/ of the
166 linemap_resolve_location (line_table
, iter
->where
,
167 LRK_MACRO_DEFINITION_LOCATION
, NULL
);
169 /* Don't print trace for locations that are reserved or from
170 within a system header. */
172 const struct line_map
*m
= NULL
;
173 source_location l
= linemap_resolve_location (line_table
, resolved_def_loc
,
174 LRK_SPELLING_LOCATION
,
176 if (l
< RESERVED_LOCATION_COUNT
181 /* Resolve the location of the expansion point of the macro
182 which expansion gave the token represented by def_loc.
183 This is the locus 2/ of the earlier comment. */
185 linemap_resolve_location (line_table
,
186 MACRO_MAP_EXPANSION_POINT_LOCATION (iter
->map
),
187 LRK_MACRO_DEFINITION_LOCATION
, NULL
);
189 saved_kind
= diagnostic
->kind
;
190 saved_prefix
= pp_get_prefix (context
->printer
);
191 saved_location
= diagnostic
->location
;
193 diagnostic
->kind
= DK_NOTE
;
194 diagnostic
->location
= resolved_def_loc
;
195 pp_set_prefix (context
->printer
,
196 diagnostic_build_prefix (context
, diagnostic
));
197 pp_newline (context
->printer
);
198 pp_printf (context
->printer
, "in expansion of macro '%s'",
199 linemap_map_get_macro_name (iter
->map
));
200 pp_destroy_prefix (context
->printer
);
201 diagnostic_show_locus (context
, diagnostic
);
203 diagnostic
->location
= resolved_exp_loc
;
204 pp_set_prefix (context
->printer
,
205 diagnostic_build_prefix (context
, diagnostic
));
206 pp_newline (context
->printer
);
207 pp_string (context
->printer
, "expanded from here");
208 pp_destroy_prefix (context
->printer
);
209 diagnostic_show_locus (context
, diagnostic
);
211 diagnostic
->kind
= saved_kind
;
212 diagnostic
->location
= saved_location
;
213 pp_set_prefix (context
->printer
, saved_prefix
);
216 VEC_free (loc_map_pair
, heap
, loc_vec
);
219 /* This is a diagnostic finalizer implementation that is aware of
220 virtual locations produced by libcpp.
222 It has to be called by the diagnostic finalizer of front ends that
223 uses libcpp and wish to get diagnostics involving tokens resulting
224 from macro expansion.
226 For a given location, if said location belongs to a token
227 resulting from a macro expansion, this starter prints the context
228 of the token. E.g, for multiply nested macro expansion, it
229 unwinds the nested macro expansions and prints them in a manner
230 that is similar to what is done for function call stacks, or
231 template instantiation contexts. */
233 virt_loc_aware_diagnostic_finalizer (diagnostic_context
*context
,
234 diagnostic_info
*diagnostic
)
236 maybe_unwind_expanded_macro_loc (context
, diagnostic
,
237 diagnostic
->location
);
240 /* Default tree printer. Handles declarations only. */
242 default_tree_printer (pretty_printer
*pp
, text_info
*text
, const char *spec
,
243 int precision
, bool wide
, bool set_locus
, bool hash
)
247 /* FUTURE: %+x should set the locus. */
248 if (precision
!= 0 || wide
|| hash
)
254 t
= va_arg (*text
->args_ptr
, tree
);
255 if (TREE_CODE (t
) == IDENTIFIER_NODE
)
257 pp_identifier (pp
, IDENTIFIER_POINTER (t
));
263 t
= va_arg (*text
->args_ptr
, tree
);
264 if (DECL_DEBUG_EXPR_IS_FROM (t
) && DECL_DEBUG_EXPR (t
))
265 t
= DECL_DEBUG_EXPR (t
);
270 t
= va_arg (*text
->args_ptr
, tree
);
274 percent_K_format (text
);
281 if (set_locus
&& text
->locus
)
282 *text
->locus
= DECL_SOURCE_LOCATION (t
);
286 const char *n
= DECL_NAME (t
)
287 ? identifier_to_locale (lang_hooks
.decl_printable_name (t
, 2))
292 dump_generic_node (pp
, t
, 0, TDF_DIAGNOSTIC
, 0);
297 /* Sets CONTEXT to use language independent diagnostics. */
299 tree_diagnostics_defaults (diagnostic_context
*context
)
301 diagnostic_starter (context
) = default_tree_diagnostic_starter
;
302 diagnostic_finalizer (context
) = default_diagnostic_finalizer
;
303 diagnostic_format_decoder (context
) = default_tree_printer
;