1*38fd1498Szrj /* Language-independent diagnostic subroutines for the GNU Compiler
2*38fd1498Szrj Collection that are only for use in the compilers proper and not
3*38fd1498Szrj the driver or other programs.
4*38fd1498Szrj Copyright (C) 1999-2018 Free Software Foundation, Inc.
5*38fd1498Szrj
6*38fd1498Szrj This file is part of GCC.
7*38fd1498Szrj
8*38fd1498Szrj GCC is free software; you can redistribute it and/or modify it under
9*38fd1498Szrj the terms of the GNU General Public License as published by the Free
10*38fd1498Szrj Software Foundation; either version 3, or (at your option) any later
11*38fd1498Szrj version.
12*38fd1498Szrj
13*38fd1498Szrj GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14*38fd1498Szrj WARRANTY; without even the implied warranty of MERCHANTABILITY or
15*38fd1498Szrj FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16*38fd1498Szrj for more details.
17*38fd1498Szrj
18*38fd1498Szrj You should have received a copy of the GNU General Public License
19*38fd1498Szrj along with GCC; see the file COPYING3. If not see
20*38fd1498Szrj <http://www.gnu.org/licenses/>. */
21*38fd1498Szrj
22*38fd1498Szrj #include "config.h"
23*38fd1498Szrj #include "system.h"
24*38fd1498Szrj #include "coretypes.h"
25*38fd1498Szrj #include "tree.h"
26*38fd1498Szrj #include "diagnostic.h"
27*38fd1498Szrj #include "tree-pretty-print.h"
28*38fd1498Szrj #include "gimple-pretty-print.h"
29*38fd1498Szrj #include "tree-diagnostic.h"
30*38fd1498Szrj #include "langhooks.h"
31*38fd1498Szrj #include "intl.h"
32*38fd1498Szrj
33*38fd1498Szrj /* Prints out, if necessary, the name of the current function
34*38fd1498Szrj that caused an error. Called from all error and warning functions. */
35*38fd1498Szrj void
diagnostic_report_current_function(diagnostic_context * context,diagnostic_info * diagnostic)36*38fd1498Szrj diagnostic_report_current_function (diagnostic_context *context,
37*38fd1498Szrj diagnostic_info *diagnostic)
38*38fd1498Szrj {
39*38fd1498Szrj diagnostic_report_current_module (context, diagnostic_location (diagnostic));
40*38fd1498Szrj lang_hooks.print_error_function (context, LOCATION_FILE (input_location),
41*38fd1498Szrj diagnostic);
42*38fd1498Szrj }
43*38fd1498Szrj
44*38fd1498Szrj static void
default_tree_diagnostic_starter(diagnostic_context * context,diagnostic_info * diagnostic)45*38fd1498Szrj default_tree_diagnostic_starter (diagnostic_context *context,
46*38fd1498Szrj diagnostic_info *diagnostic)
47*38fd1498Szrj {
48*38fd1498Szrj diagnostic_report_current_function (context, diagnostic);
49*38fd1498Szrj pp_set_prefix (context->printer, diagnostic_build_prefix (context,
50*38fd1498Szrj diagnostic));
51*38fd1498Szrj }
52*38fd1498Szrj
53*38fd1498Szrj /* This is a pair made of a location and the line map it originated
54*38fd1498Szrj from. It's used in the maybe_unwind_expanded_macro_loc function
55*38fd1498Szrj below. */
56*38fd1498Szrj struct loc_map_pair
57*38fd1498Szrj {
58*38fd1498Szrj const line_map_macro *map;
59*38fd1498Szrj source_location where;
60*38fd1498Szrj };
61*38fd1498Szrj
62*38fd1498Szrj
63*38fd1498Szrj /* Unwind the different macro expansions that lead to the token which
64*38fd1498Szrj location is WHERE and emit diagnostics showing the resulting
65*38fd1498Szrj unwound macro expansion trace. Let's look at an example to see how
66*38fd1498Szrj the trace looks like. Suppose we have this piece of code,
67*38fd1498Szrj artificially annotated with the line numbers to increase
68*38fd1498Szrj legibility:
69*38fd1498Szrj
70*38fd1498Szrj $ cat -n test.c
71*38fd1498Szrj 1 #define OPERATE(OPRD1, OPRT, OPRD2) \
72*38fd1498Szrj 2 OPRD1 OPRT OPRD2;
73*38fd1498Szrj 3
74*38fd1498Szrj 4 #define SHIFTL(A,B) \
75*38fd1498Szrj 5 OPERATE (A,<<,B)
76*38fd1498Szrj 6
77*38fd1498Szrj 7 #define MULT(A) \
78*38fd1498Szrj 8 SHIFTL (A,1)
79*38fd1498Szrj 9
80*38fd1498Szrj 10 void
81*38fd1498Szrj 11 g ()
82*38fd1498Szrj 12 {
83*38fd1498Szrj 13 MULT (1.0);// 1.0 << 1; <-- so this is an error.
84*38fd1498Szrj 14 }
85*38fd1498Szrj
86*38fd1498Szrj Here is the diagnostic that we want the compiler to generate:
87*38fd1498Szrj
88*38fd1498Szrj test.c: In function ‘g’:
89*38fd1498Szrj test.c:5:14: error: invalid operands to binary << (have ‘double’ and ‘int’)
90*38fd1498Szrj test.c:2:9: note: in definition of macro 'OPERATE'
91*38fd1498Szrj test.c:8:3: note: in expansion of macro 'SHIFTL'
92*38fd1498Szrj test.c:13:3: note: in expansion of macro 'MULT'
93*38fd1498Szrj
94*38fd1498Szrj The part that goes from the third to the fifth line of this
95*38fd1498Szrj diagnostic (the lines containing the 'note:' string) is called the
96*38fd1498Szrj unwound macro expansion trace. That's the part generated by this
97*38fd1498Szrj function. */
98*38fd1498Szrj
99*38fd1498Szrj static void
maybe_unwind_expanded_macro_loc(diagnostic_context * context,const diagnostic_info * diagnostic,source_location where)100*38fd1498Szrj maybe_unwind_expanded_macro_loc (diagnostic_context *context,
101*38fd1498Szrj const diagnostic_info *diagnostic,
102*38fd1498Szrj source_location where)
103*38fd1498Szrj {
104*38fd1498Szrj const struct line_map *map;
105*38fd1498Szrj auto_vec<loc_map_pair> loc_vec;
106*38fd1498Szrj unsigned ix;
107*38fd1498Szrj loc_map_pair loc, *iter;
108*38fd1498Szrj
109*38fd1498Szrj map = linemap_lookup (line_table, where);
110*38fd1498Szrj if (!linemap_macro_expansion_map_p (map))
111*38fd1498Szrj return;
112*38fd1498Szrj
113*38fd1498Szrj /* Let's unwind the macros that got expanded and led to the token
114*38fd1498Szrj which location is WHERE. We are going to store these macros into
115*38fd1498Szrj LOC_VEC, so that we can later walk it at our convenience to
116*38fd1498Szrj display a somewhat meaningful trace of the macro expansion
117*38fd1498Szrj history to the user. Note that the first macro of the trace
118*38fd1498Szrj (which is OPERATE in the example above) is going to be stored at
119*38fd1498Szrj the beginning of LOC_VEC. */
120*38fd1498Szrj
121*38fd1498Szrj do
122*38fd1498Szrj {
123*38fd1498Szrj loc.where = where;
124*38fd1498Szrj loc.map = linemap_check_macro (map);
125*38fd1498Szrj
126*38fd1498Szrj loc_vec.safe_push (loc);
127*38fd1498Szrj
128*38fd1498Szrj /* WHERE is the location of a token inside the expansion of a
129*38fd1498Szrj macro. MAP is the map holding the locations of that macro
130*38fd1498Szrj expansion. Let's get the location of the token inside the
131*38fd1498Szrj context that triggered the expansion of this macro.
132*38fd1498Szrj This is basically how we go "down" in the trace of macro
133*38fd1498Szrj expansions that led to WHERE. */
134*38fd1498Szrj where = linemap_unwind_toward_expansion (line_table, where, &map);
135*38fd1498Szrj } while (linemap_macro_expansion_map_p (map));
136*38fd1498Szrj
137*38fd1498Szrj /* Now map is set to the map of the location in the source that
138*38fd1498Szrj first triggered the macro expansion. This must be an ordinary map. */
139*38fd1498Szrj const line_map_ordinary *ord_map = linemap_check_ordinary (map);
140*38fd1498Szrj
141*38fd1498Szrj /* Walk LOC_VEC and print the macro expansion trace, unless the
142*38fd1498Szrj first macro which expansion triggered this trace was expanded
143*38fd1498Szrj inside a system header. */
144*38fd1498Szrj int saved_location_line =
145*38fd1498Szrj expand_location_to_spelling_point (diagnostic_location (diagnostic)).line;
146*38fd1498Szrj
147*38fd1498Szrj if (!LINEMAP_SYSP (ord_map))
148*38fd1498Szrj FOR_EACH_VEC_ELT (loc_vec, ix, iter)
149*38fd1498Szrj {
150*38fd1498Szrj /* Sometimes, in the unwound macro expansion trace, we want to
151*38fd1498Szrj print a part of the context that shows where, in the
152*38fd1498Szrj definition of the relevant macro, is the token (we are
153*38fd1498Szrj looking at) used. That is the case in the introductory
154*38fd1498Szrj comment of this function, where we print:
155*38fd1498Szrj
156*38fd1498Szrj test.c:2:9: note: in definition of macro 'OPERATE'.
157*38fd1498Szrj
158*38fd1498Szrj We print that "macro definition context" because the
159*38fd1498Szrj diagnostic line (emitted by the call to
160*38fd1498Szrj pp_ouput_formatted_text in diagnostic_report_diagnostic):
161*38fd1498Szrj
162*38fd1498Szrj test.c:5:14: error: invalid operands to binary << (have ‘double’ and ‘int’)
163*38fd1498Szrj
164*38fd1498Szrj does not point into the definition of the macro where the
165*38fd1498Szrj token '<<' (that is an argument to the function-like macro
166*38fd1498Szrj OPERATE) is used. So we must "display" the line of that
167*38fd1498Szrj macro definition context to the user somehow.
168*38fd1498Szrj
169*38fd1498Szrj A contrario, when the first interesting diagnostic line
170*38fd1498Szrj points into the definition of the macro, we don't need to
171*38fd1498Szrj display any line for that macro definition in the trace
172*38fd1498Szrj anymore, otherwise it'd be redundant. */
173*38fd1498Szrj
174*38fd1498Szrj /* Okay, now here is what we want. For each token resulting
175*38fd1498Szrj from macro expansion we want to show: 1/ where in the
176*38fd1498Szrj definition of the macro the token comes from; 2/ where the
177*38fd1498Szrj macro got expanded. */
178*38fd1498Szrj
179*38fd1498Szrj /* Resolve the location iter->where into the locus 1/ of the
180*38fd1498Szrj comment above. */
181*38fd1498Szrj source_location resolved_def_loc =
182*38fd1498Szrj linemap_resolve_location (line_table, iter->where,
183*38fd1498Szrj LRK_MACRO_DEFINITION_LOCATION, NULL);
184*38fd1498Szrj
185*38fd1498Szrj /* Don't print trace for locations that are reserved or from
186*38fd1498Szrj within a system header. */
187*38fd1498Szrj const line_map_ordinary *m = NULL;
188*38fd1498Szrj source_location l =
189*38fd1498Szrj linemap_resolve_location (line_table, resolved_def_loc,
190*38fd1498Szrj LRK_SPELLING_LOCATION, &m);
191*38fd1498Szrj if (l < RESERVED_LOCATION_COUNT || LINEMAP_SYSP (m))
192*38fd1498Szrj continue;
193*38fd1498Szrj
194*38fd1498Szrj /* We need to print the context of the macro definition only
195*38fd1498Szrj when the locus of the first displayed diagnostic (displayed
196*38fd1498Szrj before this trace) was inside the definition of the
197*38fd1498Szrj macro. */
198*38fd1498Szrj int resolved_def_loc_line = SOURCE_LINE (m, l);
199*38fd1498Szrj if (ix == 0 && saved_location_line != resolved_def_loc_line)
200*38fd1498Szrj {
201*38fd1498Szrj diagnostic_append_note (context, resolved_def_loc,
202*38fd1498Szrj "in definition of macro %qs",
203*38fd1498Szrj linemap_map_get_macro_name (iter->map));
204*38fd1498Szrj /* At this step, as we've printed the context of the macro
205*38fd1498Szrj definition, we don't want to print the context of its
206*38fd1498Szrj expansion, otherwise, it'd be redundant. */
207*38fd1498Szrj continue;
208*38fd1498Szrj }
209*38fd1498Szrj
210*38fd1498Szrj /* Resolve the location of the expansion point of the macro
211*38fd1498Szrj which expansion gave the token represented by def_loc.
212*38fd1498Szrj This is the locus 2/ of the earlier comment. */
213*38fd1498Szrj source_location resolved_exp_loc =
214*38fd1498Szrj linemap_resolve_location (line_table,
215*38fd1498Szrj MACRO_MAP_EXPANSION_POINT_LOCATION (iter->map),
216*38fd1498Szrj LRK_MACRO_DEFINITION_LOCATION, NULL);
217*38fd1498Szrj
218*38fd1498Szrj diagnostic_append_note (context, resolved_exp_loc,
219*38fd1498Szrj "in expansion of macro %qs",
220*38fd1498Szrj linemap_map_get_macro_name (iter->map));
221*38fd1498Szrj }
222*38fd1498Szrj }
223*38fd1498Szrj
224*38fd1498Szrj /* This is a diagnostic finalizer implementation that is aware of
225*38fd1498Szrj virtual locations produced by libcpp.
226*38fd1498Szrj
227*38fd1498Szrj It has to be called by the diagnostic finalizer of front ends that
228*38fd1498Szrj uses libcpp and wish to get diagnostics involving tokens resulting
229*38fd1498Szrj from macro expansion.
230*38fd1498Szrj
231*38fd1498Szrj For a given location, if said location belongs to a token
232*38fd1498Szrj resulting from a macro expansion, this starter prints the context
233*38fd1498Szrj of the token. E.g, for multiply nested macro expansion, it
234*38fd1498Szrj unwinds the nested macro expansions and prints them in a manner
235*38fd1498Szrj that is similar to what is done for function call stacks, or
236*38fd1498Szrj template instantiation contexts. */
237*38fd1498Szrj void
virt_loc_aware_diagnostic_finalizer(diagnostic_context * context,diagnostic_info * diagnostic)238*38fd1498Szrj virt_loc_aware_diagnostic_finalizer (diagnostic_context *context,
239*38fd1498Szrj diagnostic_info *diagnostic)
240*38fd1498Szrj {
241*38fd1498Szrj maybe_unwind_expanded_macro_loc (context, diagnostic,
242*38fd1498Szrj diagnostic_location (diagnostic));
243*38fd1498Szrj }
244*38fd1498Szrj
245*38fd1498Szrj /* Default tree printer. Handles declarations only. */
246*38fd1498Szrj bool
default_tree_printer(pretty_printer * pp,text_info * text,const char * spec,int precision,bool wide,bool set_locus,bool hash,bool *,const char **)247*38fd1498Szrj default_tree_printer (pretty_printer *pp, text_info *text, const char *spec,
248*38fd1498Szrj int precision, bool wide, bool set_locus, bool hash,
249*38fd1498Szrj bool *, const char **)
250*38fd1498Szrj {
251*38fd1498Szrj tree t;
252*38fd1498Szrj
253*38fd1498Szrj /* FUTURE: %+x should set the locus. */
254*38fd1498Szrj if (precision != 0 || wide || hash)
255*38fd1498Szrj return false;
256*38fd1498Szrj
257*38fd1498Szrj switch (*spec)
258*38fd1498Szrj {
259*38fd1498Szrj case 'E':
260*38fd1498Szrj t = va_arg (*text->args_ptr, tree);
261*38fd1498Szrj if (TREE_CODE (t) == IDENTIFIER_NODE)
262*38fd1498Szrj {
263*38fd1498Szrj pp_identifier (pp, IDENTIFIER_POINTER (t));
264*38fd1498Szrj return true;
265*38fd1498Szrj }
266*38fd1498Szrj break;
267*38fd1498Szrj
268*38fd1498Szrj case 'D':
269*38fd1498Szrj t = va_arg (*text->args_ptr, tree);
270*38fd1498Szrj if (VAR_P (t) && DECL_HAS_DEBUG_EXPR_P (t))
271*38fd1498Szrj t = DECL_DEBUG_EXPR (t);
272*38fd1498Szrj break;
273*38fd1498Szrj
274*38fd1498Szrj case 'F':
275*38fd1498Szrj case 'T':
276*38fd1498Szrj t = va_arg (*text->args_ptr, tree);
277*38fd1498Szrj break;
278*38fd1498Szrj
279*38fd1498Szrj case 'G':
280*38fd1498Szrj percent_G_format (text);
281*38fd1498Szrj return true;
282*38fd1498Szrj
283*38fd1498Szrj case 'K':
284*38fd1498Szrj t = va_arg (*text->args_ptr, tree);
285*38fd1498Szrj percent_K_format (text, t);
286*38fd1498Szrj return true;
287*38fd1498Szrj
288*38fd1498Szrj default:
289*38fd1498Szrj return false;
290*38fd1498Szrj }
291*38fd1498Szrj
292*38fd1498Szrj if (set_locus)
293*38fd1498Szrj text->set_location (0, DECL_SOURCE_LOCATION (t), true);
294*38fd1498Szrj
295*38fd1498Szrj if (DECL_P (t))
296*38fd1498Szrj {
297*38fd1498Szrj const char *n = DECL_NAME (t)
298*38fd1498Szrj ? identifier_to_locale (lang_hooks.decl_printable_name (t, 2))
299*38fd1498Szrj : _("<anonymous>");
300*38fd1498Szrj pp_string (pp, n);
301*38fd1498Szrj }
302*38fd1498Szrj else
303*38fd1498Szrj dump_generic_node (pp, t, 0, TDF_SLIM, 0);
304*38fd1498Szrj
305*38fd1498Szrj return true;
306*38fd1498Szrj }
307*38fd1498Szrj
308*38fd1498Szrj /* Sets CONTEXT to use language independent diagnostics. */
309*38fd1498Szrj void
tree_diagnostics_defaults(diagnostic_context * context)310*38fd1498Szrj tree_diagnostics_defaults (diagnostic_context *context)
311*38fd1498Szrj {
312*38fd1498Szrj diagnostic_starter (context) = default_tree_diagnostic_starter;
313*38fd1498Szrj diagnostic_finalizer (context) = default_diagnostic_finalizer;
314*38fd1498Szrj diagnostic_format_decoder (context) = default_tree_printer;
315*38fd1498Szrj }
316