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
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
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/>. */
24 #include "coretypes.h"
28 #include "diagnostic.h"
29 #include "tree-pretty-print.h"
30 #include "tree-diagnostic.h"
31 #include "dumpfile.h" /* TDF_DIAGNOSTIC */
32 #include "langhooks.h"
33 #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 (diagnostic
));
43 lang_hooks
.print_error_function (context
, LOCATION_FILE (input_location
),
48 default_tree_diagnostic_starter (diagnostic_context
*context
,
49 diagnostic_info
*diagnostic
)
51 diagnostic_report_current_function (context
, diagnostic
);
52 pp_set_prefix (context
->printer
, diagnostic_build_prefix (context
,
56 /* This is a pair made of a location and the line map it originated
57 from. It's used in the maybe_unwind_expanded_macro_loc function
61 const line_map_macro
*map
;
62 source_location where
;
66 /* Unwind the different macro expansions that lead to the token which
67 location is WHERE and emit diagnostics showing the resulting
68 unwound macro expansion trace. Let's look at an example to see how
69 the trace looks like. Suppose we have this piece of code,
70 artificially annotated with the line numbers to increase
74 1 #define OPERATE(OPRD1, OPRT, OPRD2) \
77 4 #define SHIFTL(A,B) \
86 13 MULT (1.0);// 1.0 << 1; <-- so this is an error.
89 Here is the diagnostic that we want the compiler to generate:
91 test.c: In function ‘g’:
92 test.c:5:14: error: invalid operands to binary << (have ‘double’ and ‘int’)
93 test.c:2:9: note: in definition of macro 'OPERATE'
94 test.c:8:3: note: in expansion of macro 'SHIFTL'
95 test.c:13:3: note: in expansion of macro 'MULT'
97 The part that goes from the third to the fifth line of this
98 diagnostic (the lines containing the 'note:' string) is called the
99 unwound macro expansion trace. That's the part generated by this
103 maybe_unwind_expanded_macro_loc (diagnostic_context
*context
,
104 const diagnostic_info
*diagnostic
,
105 source_location where
)
107 const struct line_map
*map
;
108 vec
<loc_map_pair
> loc_vec
= vNULL
;
110 loc_map_pair loc
, *iter
;
112 map
= linemap_lookup (line_table
, where
);
113 if (!linemap_macro_expansion_map_p (map
))
116 /* Let's unwind the macros that got expanded and led to the token
117 which location is WHERE. We are going to store these macros into
118 LOC_VEC, so that we can later walk it at our convenience to
119 display a somewhat meaningful trace of the macro expansion
120 history to the user. Note that the first macro of the trace
121 (which is OPERATE in the example above) is going to be stored at
122 the beginning of LOC_VEC. */
127 loc
.map
= linemap_check_macro (map
);
129 loc_vec
.safe_push (loc
);
131 /* WHERE is the location of a token inside the expansion of a
132 macro. MAP is the map holding the locations of that macro
133 expansion. Let's get the location of the token inside the
134 context that triggered the expansion of this macro.
135 This is basically how we go "down" in the trace of macro
136 expansions that led to WHERE. */
137 where
= linemap_unwind_toward_expansion (line_table
, where
, &map
);
138 } while (linemap_macro_expansion_map_p (map
));
140 /* Now map is set to the map of the location in the source that
141 first triggered the macro expansion. This must be an ordinary map. */
142 const line_map_ordinary
*ord_map
= linemap_check_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 int saved_location_line
=
148 expand_location_to_spelling_point (diagnostic_location (diagnostic
)).line
;
150 if (!LINEMAP_SYSP (ord_map
))
151 FOR_EACH_VEC_ELT (loc_vec
, ix
, iter
)
153 /* Sometimes, in the unwound macro expansion trace, we want to
154 print a part of the context that shows where, in the
155 definition of the relevant macro, is the token (we are
156 looking at) used. That is the case in the introductory
157 comment of this function, where we print:
159 test.c:2:9: note: in definition of macro 'OPERATE'.
161 We print that "macro definition context" because the
162 diagnostic line (emitted by the call to
163 pp_ouput_formatted_text in diagnostic_report_diagnostic):
165 test.c:5:14: error: invalid operands to binary << (have ‘double’ and ‘int’)
167 does not point into the definition of the macro where the
168 token '<<' (that is an argument to the function-like macro
169 OPERATE) is used. So we must "display" the line of that
170 macro definition context to the user somehow.
172 A contrario, when the first interesting diagnostic line
173 points into the definition of the macro, we don't need to
174 display any line for that macro definition in the trace
175 anymore, otherwise it'd be redundant. */
177 /* Okay, now here is what we want. For each token resulting
178 from macro expansion we want to show: 1/ where in the
179 definition of the macro the token comes from; 2/ where the
180 macro got expanded. */
182 /* Resolve the location iter->where into the locus 1/ of the
184 source_location resolved_def_loc
=
185 linemap_resolve_location (line_table
, iter
->where
,
186 LRK_MACRO_DEFINITION_LOCATION
, NULL
);
188 /* Don't print trace for locations that are reserved or from
189 within a system header. */
190 const line_map_ordinary
*m
= NULL
;
192 linemap_resolve_location (line_table
, resolved_def_loc
,
193 LRK_SPELLING_LOCATION
, &m
);
194 if (l
< RESERVED_LOCATION_COUNT
|| LINEMAP_SYSP (m
))
197 /* We need to print the context of the macro definition only
198 when the locus of the first displayed diagnostic (displayed
199 before this trace) was inside the definition of the
201 int resolved_def_loc_line
= SOURCE_LINE (m
, l
);
202 if (ix
== 0 && saved_location_line
!= resolved_def_loc_line
)
204 diagnostic_append_note (context
, resolved_def_loc
,
205 "in definition of macro %qs",
206 linemap_map_get_macro_name (iter
->map
));
207 /* At this step, as we've printed the context of the macro
208 definition, we don't want to print the context of its
209 expansion, otherwise, it'd be redundant. */
213 /* Resolve the location of the expansion point of the macro
214 which expansion gave the token represented by def_loc.
215 This is the locus 2/ of the earlier comment. */
216 source_location resolved_exp_loc
=
217 linemap_resolve_location (line_table
,
218 MACRO_MAP_EXPANSION_POINT_LOCATION (iter
->map
),
219 LRK_MACRO_DEFINITION_LOCATION
, NULL
);
221 diagnostic_append_note (context
, resolved_exp_loc
,
222 "in expansion of macro %qs",
223 linemap_map_get_macro_name (iter
->map
));
229 /* This is a diagnostic finalizer implementation that is aware of
230 virtual locations produced by libcpp.
232 It has to be called by the diagnostic finalizer of front ends that
233 uses libcpp and wish to get diagnostics involving tokens resulting
234 from macro expansion.
236 For a given location, if said location belongs to a token
237 resulting from a macro expansion, this starter prints the context
238 of the token. E.g, for multiply nested macro expansion, it
239 unwinds the nested macro expansions and prints them in a manner
240 that is similar to what is done for function call stacks, or
241 template instantiation contexts. */
243 virt_loc_aware_diagnostic_finalizer (diagnostic_context
*context
,
244 diagnostic_info
*diagnostic
)
246 maybe_unwind_expanded_macro_loc (context
, diagnostic
,
247 diagnostic_location (diagnostic
));
250 /* Default tree printer. Handles declarations only. */
252 default_tree_printer (pretty_printer
*pp
, text_info
*text
, const char *spec
,
253 int precision
, bool wide
, bool set_locus
, bool hash
)
257 /* FUTURE: %+x should set the locus. */
258 if (precision
!= 0 || wide
|| hash
)
264 t
= va_arg (*text
->args_ptr
, tree
);
265 if (TREE_CODE (t
) == IDENTIFIER_NODE
)
267 pp_identifier (pp
, IDENTIFIER_POINTER (t
));
273 t
= va_arg (*text
->args_ptr
, tree
);
274 if (TREE_CODE (t
) == VAR_DECL
&& DECL_HAS_DEBUG_EXPR_P (t
))
275 t
= DECL_DEBUG_EXPR (t
);
280 t
= va_arg (*text
->args_ptr
, tree
);
284 percent_K_format (text
);
292 text
->set_location (0, DECL_SOURCE_LOCATION (t
));
296 const char *n
= DECL_NAME (t
)
297 ? identifier_to_locale (lang_hooks
.decl_printable_name (t
, 2))
302 dump_generic_node (pp
, t
, 0, TDF_DIAGNOSTIC
, 0);
307 /* Sets CONTEXT to use language independent diagnostics. */
309 tree_diagnostics_defaults (diagnostic_context
*context
)
311 diagnostic_starter (context
) = default_tree_diagnostic_starter
;
312 diagnostic_finalizer (context
) = default_diagnostic_finalizer
;
313 diagnostic_format_decoder (context
) = default_tree_printer
;