gigi.h (record_builtin_type): Adjust comment.
[platform/upstream/gcc.git] / gcc / tree-diagnostic.c
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.
5
6 This file is part of GCC.
7
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.
12
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.
17
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/>.  */
21
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"
36
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)
42 {
43   diagnostic_report_current_module (context, diagnostic_location (diagnostic));
44   lang_hooks.print_error_function (context, LOCATION_FILE (input_location),
45                                    diagnostic);
46 }
47
48 static void
49 default_tree_diagnostic_starter (diagnostic_context *context,
50                                  diagnostic_info *diagnostic)
51 {
52   diagnostic_report_current_function (context, diagnostic);
53   pp_set_prefix (context->printer, diagnostic_build_prefix (context,
54                                                             diagnostic));
55 }
56
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
61 {
62   const line_map_macro *map;
63   source_location where;
64 } loc_map_pair;
65
66
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:
73
74     $ cat -n test.c
75       1    #define OPERATE(OPRD1, OPRT, OPRD2) \
76       2      OPRD1 OPRT OPRD2;
77       3
78       4    #define SHIFTL(A,B) \
79       5      OPERATE (A,<<,B)
80       6
81       7    #define MULT(A) \
82       8      SHIFTL (A,1)
83       9
84      10    void
85      11    g ()
86      12    {
87      13      MULT (1.0);// 1.0 << 1; <-- so this is an error.
88      14    }
89
90    Here is the diagnostic that we want the compiler to generate:
91
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'
97
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.  */
102
103 static void
104 maybe_unwind_expanded_macro_loc (diagnostic_context *context,
105                                  const diagnostic_info *diagnostic,
106                                  source_location where)
107 {
108   const struct line_map *map;
109   vec<loc_map_pair> loc_vec = vNULL;
110   unsigned ix;
111   loc_map_pair loc, *iter;
112
113   map = linemap_lookup (line_table, where);
114   if (!linemap_macro_expansion_map_p (map))
115     return;
116
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.  */
124
125   do
126     {
127       loc.where = where;
128       loc.map = linemap_check_macro (map);
129
130       loc_vec.safe_push (loc);
131
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));
140
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);
144
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;
150
151   if (!LINEMAP_SYSP (ord_map))
152     FOR_EACH_VEC_ELT (loc_vec, ix, iter)
153       {
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:
159
160                test.c:2:9: note: in definition of macro 'OPERATE'.
161
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):
165
166                test.c:5:14: error: invalid operands to binary << (have ‘double’ and ‘int’)
167
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.
172
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.  */
177
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.  */
182
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);
188
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;
197         
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)
204           {
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;
212           }
213
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);
221
222         diagnostic_append_note (context, resolved_exp_loc, 
223                                 "in expansion of macro %qs",
224                                 linemap_map_get_macro_name (iter->map));
225       }
226
227   loc_vec.release ();
228 }
229
230 /*  This is a diagnostic finalizer implementation that is aware of
231     virtual locations produced by libcpp.
232
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.
236
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)
246 {
247   maybe_unwind_expanded_macro_loc (context, diagnostic,
248                                    diagnostic_location (diagnostic));
249 }
250
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)
255 {
256   tree t;
257
258   /* FUTURE: %+x should set the locus.  */
259   if (precision != 0 || wide || hash)
260     return false;
261
262   switch (*spec)
263     {
264     case 'E':
265       t = va_arg (*text->args_ptr, tree);
266       if (TREE_CODE (t) == IDENTIFIER_NODE)
267         {
268           pp_identifier (pp, IDENTIFIER_POINTER (t));
269           return true;
270         }
271       break;
272
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;
278
279     case 'F':
280     case 'T':
281       t = va_arg (*text->args_ptr, tree);
282       break;
283
284     case 'K':
285       percent_K_format (text);
286       return true;
287
288     default:
289       return false;
290     }
291
292   if (set_locus)
293     text->set_location (0, DECL_SOURCE_LOCATION (t));
294
295   if (DECL_P (t))
296     {
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);
301     }
302   else
303     dump_generic_node (pp, t, 0, TDF_DIAGNOSTIC, 0);
304
305   return true;
306 }
307
308 /* Sets CONTEXT to use language independent diagnostics.  */
309 void
310 tree_diagnostics_defaults (diagnostic_context *context)
311 {
312   diagnostic_starter (context) = default_tree_diagnostic_starter;
313   diagnostic_finalizer (context) = default_diagnostic_finalizer;
314   diagnostic_format_decoder (context) = default_tree_printer;
315 }