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