xref: /netbsd-src/external/gpl3/gcc.old/dist/gcc/tree-diagnostic.c (revision 8feb0f0b7eaff0608f8350bbfa3098827b4bb91b)
11debfc3dSmrg /* Language-independent diagnostic subroutines for the GNU Compiler
21debfc3dSmrg    Collection that are only for use in the compilers proper and not
31debfc3dSmrg    the driver or other programs.
4*8feb0f0bSmrg    Copyright (C) 1999-2020 Free Software Foundation, Inc.
51debfc3dSmrg 
61debfc3dSmrg This file is part of GCC.
71debfc3dSmrg 
81debfc3dSmrg GCC is free software; you can redistribute it and/or modify it under
91debfc3dSmrg the terms of the GNU General Public License as published by the Free
101debfc3dSmrg Software Foundation; either version 3, or (at your option) any later
111debfc3dSmrg version.
121debfc3dSmrg 
131debfc3dSmrg GCC is distributed in the hope that it will be useful, but WITHOUT ANY
141debfc3dSmrg WARRANTY; without even the implied warranty of MERCHANTABILITY or
151debfc3dSmrg FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
161debfc3dSmrg for more details.
171debfc3dSmrg 
181debfc3dSmrg You should have received a copy of the GNU General Public License
191debfc3dSmrg along with GCC; see the file COPYING3.  If not see
201debfc3dSmrg <http://www.gnu.org/licenses/>.  */
211debfc3dSmrg 
221debfc3dSmrg #include "config.h"
231debfc3dSmrg #include "system.h"
241debfc3dSmrg #include "coretypes.h"
251debfc3dSmrg #include "tree.h"
261debfc3dSmrg #include "diagnostic.h"
271debfc3dSmrg #include "tree-pretty-print.h"
28a2dc1f3fSmrg #include "gimple-pretty-print.h"
291debfc3dSmrg #include "tree-diagnostic.h"
301debfc3dSmrg #include "langhooks.h"
311debfc3dSmrg #include "intl.h"
321debfc3dSmrg 
331debfc3dSmrg /* Prints out, if necessary, the name of the current function
341debfc3dSmrg    that caused an error.  Called from all error and warning functions.  */
351debfc3dSmrg void
diagnostic_report_current_function(diagnostic_context * context,diagnostic_info * diagnostic)361debfc3dSmrg diagnostic_report_current_function (diagnostic_context *context,
371debfc3dSmrg 				    diagnostic_info *diagnostic)
381debfc3dSmrg {
391debfc3dSmrg   diagnostic_report_current_module (context, diagnostic_location (diagnostic));
401debfc3dSmrg   lang_hooks.print_error_function (context, LOCATION_FILE (input_location),
411debfc3dSmrg 				   diagnostic);
421debfc3dSmrg }
431debfc3dSmrg 
441debfc3dSmrg static void
default_tree_diagnostic_starter(diagnostic_context * context,diagnostic_info * diagnostic)451debfc3dSmrg default_tree_diagnostic_starter (diagnostic_context *context,
461debfc3dSmrg 				 diagnostic_info *diagnostic)
471debfc3dSmrg {
481debfc3dSmrg   diagnostic_report_current_function (context, diagnostic);
491debfc3dSmrg   pp_set_prefix (context->printer, diagnostic_build_prefix (context,
501debfc3dSmrg 							    diagnostic));
511debfc3dSmrg }
521debfc3dSmrg 
531debfc3dSmrg /* This is a pair made of a location and the line map it originated
541debfc3dSmrg    from.  It's used in the maybe_unwind_expanded_macro_loc function
551debfc3dSmrg    below.  */
561debfc3dSmrg struct loc_map_pair
571debfc3dSmrg {
581debfc3dSmrg   const line_map_macro *map;
59c0a68be4Smrg   location_t where;
601debfc3dSmrg };
611debfc3dSmrg 
621debfc3dSmrg 
631debfc3dSmrg /* Unwind the different macro expansions that lead to the token which
641debfc3dSmrg    location is WHERE and emit diagnostics showing the resulting
651debfc3dSmrg    unwound macro expansion trace.  Let's look at an example to see how
661debfc3dSmrg    the trace looks like.  Suppose we have this piece of code,
671debfc3dSmrg    artificially annotated with the line numbers to increase
681debfc3dSmrg    legibility:
691debfc3dSmrg 
701debfc3dSmrg     $ cat -n test.c
711debfc3dSmrg       1    #define OPERATE(OPRD1, OPRT, OPRD2) \
721debfc3dSmrg       2      OPRD1 OPRT OPRD2;
731debfc3dSmrg       3
741debfc3dSmrg       4    #define SHIFTL(A,B) \
751debfc3dSmrg       5      OPERATE (A,<<,B)
761debfc3dSmrg       6
771debfc3dSmrg       7    #define MULT(A) \
781debfc3dSmrg       8      SHIFTL (A,1)
791debfc3dSmrg       9
801debfc3dSmrg      10    void
811debfc3dSmrg      11    g ()
821debfc3dSmrg      12    {
831debfc3dSmrg      13      MULT (1.0);// 1.0 << 1; <-- so this is an error.
841debfc3dSmrg      14    }
851debfc3dSmrg 
861debfc3dSmrg    Here is the diagnostic that we want the compiler to generate:
871debfc3dSmrg 
881debfc3dSmrg     test.c: In function ‘g’:
891debfc3dSmrg     test.c:5:14: error: invalid operands to binary << (have ‘double’ and ‘int’)
901debfc3dSmrg     test.c:2:9: note: in definition of macro 'OPERATE'
911debfc3dSmrg     test.c:8:3: note: in expansion of macro 'SHIFTL'
921debfc3dSmrg     test.c:13:3: note: in expansion of macro 'MULT'
931debfc3dSmrg 
941debfc3dSmrg    The part that goes from the third to the fifth line of this
951debfc3dSmrg    diagnostic (the lines containing the 'note:' string) is called the
961debfc3dSmrg    unwound macro expansion trace.  That's the part generated by this
971debfc3dSmrg    function.  */
981debfc3dSmrg 
99*8feb0f0bSmrg void
maybe_unwind_expanded_macro_loc(diagnostic_context * context,location_t where)1001debfc3dSmrg maybe_unwind_expanded_macro_loc (diagnostic_context *context,
101c0a68be4Smrg                                  location_t where)
1021debfc3dSmrg {
1031debfc3dSmrg   const struct line_map *map;
1041debfc3dSmrg   auto_vec<loc_map_pair> loc_vec;
1051debfc3dSmrg   unsigned ix;
1061debfc3dSmrg   loc_map_pair loc, *iter;
1071debfc3dSmrg 
108*8feb0f0bSmrg   const location_t original_loc = where;
109*8feb0f0bSmrg 
1101debfc3dSmrg   map = linemap_lookup (line_table, where);
1111debfc3dSmrg   if (!linemap_macro_expansion_map_p (map))
1121debfc3dSmrg     return;
1131debfc3dSmrg 
1141debfc3dSmrg   /* Let's unwind the macros that got expanded and led to the token
1151debfc3dSmrg      which location is WHERE.  We are going to store these macros into
1161debfc3dSmrg      LOC_VEC, so that we can later walk it at our convenience to
1171debfc3dSmrg      display a somewhat meaningful trace of the macro expansion
1181debfc3dSmrg      history to the user.  Note that the first macro of the trace
1191debfc3dSmrg      (which is OPERATE in the example above) is going to be stored at
1201debfc3dSmrg      the beginning of LOC_VEC.  */
1211debfc3dSmrg 
1221debfc3dSmrg   do
1231debfc3dSmrg     {
1241debfc3dSmrg       loc.where = where;
1251debfc3dSmrg       loc.map = linemap_check_macro (map);
1261debfc3dSmrg 
1271debfc3dSmrg       loc_vec.safe_push (loc);
1281debfc3dSmrg 
1291debfc3dSmrg       /* WHERE is the location of a token inside the expansion of a
1301debfc3dSmrg          macro.  MAP is the map holding the locations of that macro
1311debfc3dSmrg          expansion.  Let's get the location of the token inside the
1321debfc3dSmrg          context that triggered the expansion of this macro.
1331debfc3dSmrg          This is basically how we go "down" in the trace of macro
1341debfc3dSmrg          expansions that led to WHERE.  */
1351debfc3dSmrg       where = linemap_unwind_toward_expansion (line_table, where, &map);
1361debfc3dSmrg     } while (linemap_macro_expansion_map_p (map));
1371debfc3dSmrg 
1381debfc3dSmrg   /* Now map is set to the map of the location in the source that
1391debfc3dSmrg      first triggered the macro expansion.  This must be an ordinary map.  */
1401debfc3dSmrg   const line_map_ordinary *ord_map = linemap_check_ordinary (map);
1411debfc3dSmrg 
1421debfc3dSmrg   /* Walk LOC_VEC and print the macro expansion trace, unless the
1431debfc3dSmrg      first macro which expansion triggered this trace was expanded
1441debfc3dSmrg      inside a system header.  */
1451debfc3dSmrg   int saved_location_line =
146*8feb0f0bSmrg     expand_location_to_spelling_point (original_loc).line;
1471debfc3dSmrg 
1481debfc3dSmrg   if (!LINEMAP_SYSP (ord_map))
1491debfc3dSmrg     FOR_EACH_VEC_ELT (loc_vec, ix, iter)
1501debfc3dSmrg       {
1511debfc3dSmrg 	/* Sometimes, in the unwound macro expansion trace, we want to
1521debfc3dSmrg 	   print a part of the context that shows where, in the
1531debfc3dSmrg 	   definition of the relevant macro, is the token (we are
1541debfc3dSmrg 	   looking at) used.  That is the case in the introductory
1551debfc3dSmrg 	   comment of this function, where we print:
1561debfc3dSmrg 
1571debfc3dSmrg 	       test.c:2:9: note: in definition of macro 'OPERATE'.
1581debfc3dSmrg 
1591debfc3dSmrg 	   We print that "macro definition context" because the
1601debfc3dSmrg 	   diagnostic line (emitted by the call to
1611debfc3dSmrg 	   pp_ouput_formatted_text in diagnostic_report_diagnostic):
1621debfc3dSmrg 
1631debfc3dSmrg 	       test.c:5:14: error: invalid operands to binary << (have ‘double’ and ‘int’)
1641debfc3dSmrg 
1651debfc3dSmrg 	   does not point into the definition of the macro where the
1661debfc3dSmrg 	   token '<<' (that is an argument to the function-like macro
1671debfc3dSmrg 	   OPERATE) is used.  So we must "display" the line of that
1681debfc3dSmrg 	   macro definition context to the user somehow.
1691debfc3dSmrg 
1701debfc3dSmrg 	   A contrario, when the first interesting diagnostic line
1711debfc3dSmrg 	   points into the definition of the macro, we don't need to
1721debfc3dSmrg 	   display any line for that macro definition in the trace
1731debfc3dSmrg 	   anymore, otherwise it'd be redundant.  */
1741debfc3dSmrg 
1751debfc3dSmrg         /* Okay, now here is what we want.  For each token resulting
1761debfc3dSmrg            from macro expansion we want to show: 1/ where in the
1771debfc3dSmrg            definition of the macro the token comes from; 2/ where the
1781debfc3dSmrg            macro got expanded.  */
1791debfc3dSmrg 
1801debfc3dSmrg         /* Resolve the location iter->where into the locus 1/ of the
1811debfc3dSmrg            comment above.  */
182c0a68be4Smrg         location_t resolved_def_loc =
1831debfc3dSmrg           linemap_resolve_location (line_table, iter->where,
1841debfc3dSmrg                                     LRK_MACRO_DEFINITION_LOCATION, NULL);
1851debfc3dSmrg 
1861debfc3dSmrg 	/* Don't print trace for locations that are reserved or from
1871debfc3dSmrg 	   within a system header.  */
1881debfc3dSmrg         const line_map_ordinary *m = NULL;
189c0a68be4Smrg         location_t l =
1901debfc3dSmrg           linemap_resolve_location (line_table, resolved_def_loc,
1911debfc3dSmrg                                     LRK_SPELLING_LOCATION,  &m);
1921debfc3dSmrg         if (l < RESERVED_LOCATION_COUNT || LINEMAP_SYSP (m))
1931debfc3dSmrg           continue;
1941debfc3dSmrg 
1951debfc3dSmrg 	/* We need to print the context of the macro definition only
1961debfc3dSmrg 	   when the locus of the first displayed diagnostic (displayed
1971debfc3dSmrg 	   before this trace) was inside the definition of the
1981debfc3dSmrg 	   macro.  */
1991debfc3dSmrg         int resolved_def_loc_line = SOURCE_LINE (m, l);
2001debfc3dSmrg         if (ix == 0 && saved_location_line != resolved_def_loc_line)
2011debfc3dSmrg           {
2021debfc3dSmrg             diagnostic_append_note (context, resolved_def_loc,
2031debfc3dSmrg                                     "in definition of macro %qs",
2041debfc3dSmrg                                     linemap_map_get_macro_name (iter->map));
2051debfc3dSmrg             /* At this step, as we've printed the context of the macro
2061debfc3dSmrg                definition, we don't want to print the context of its
2071debfc3dSmrg                expansion, otherwise, it'd be redundant.  */
2081debfc3dSmrg             continue;
2091debfc3dSmrg           }
2101debfc3dSmrg 
2111debfc3dSmrg         /* Resolve the location of the expansion point of the macro
2121debfc3dSmrg            which expansion gave the token represented by def_loc.
2131debfc3dSmrg            This is the locus 2/ of the earlier comment.  */
214c0a68be4Smrg         location_t resolved_exp_loc =
2151debfc3dSmrg           linemap_resolve_location (line_table,
2161debfc3dSmrg                                     MACRO_MAP_EXPANSION_POINT_LOCATION (iter->map),
2171debfc3dSmrg                                     LRK_MACRO_DEFINITION_LOCATION, NULL);
2181debfc3dSmrg 
2191debfc3dSmrg         diagnostic_append_note (context, resolved_exp_loc,
2201debfc3dSmrg                                 "in expansion of macro %qs",
2211debfc3dSmrg                                 linemap_map_get_macro_name (iter->map));
2221debfc3dSmrg       }
2231debfc3dSmrg }
2241debfc3dSmrg 
2251debfc3dSmrg /*  This is a diagnostic finalizer implementation that is aware of
2261debfc3dSmrg     virtual locations produced by libcpp.
2271debfc3dSmrg 
2281debfc3dSmrg     It has to be called by the diagnostic finalizer of front ends that
2291debfc3dSmrg     uses libcpp and wish to get diagnostics involving tokens resulting
2301debfc3dSmrg     from macro expansion.
2311debfc3dSmrg 
2321debfc3dSmrg     For a given location, if said location belongs to a token
2331debfc3dSmrg     resulting from a macro expansion, this starter prints the context
2341debfc3dSmrg     of the token.  E.g, for multiply nested macro expansion, it
2351debfc3dSmrg     unwinds the nested macro expansions and prints them in a manner
2361debfc3dSmrg     that is similar to what is done for function call stacks, or
2371debfc3dSmrg     template instantiation contexts.  */
2381debfc3dSmrg void
virt_loc_aware_diagnostic_finalizer(diagnostic_context * context,diagnostic_info * diagnostic)2391debfc3dSmrg virt_loc_aware_diagnostic_finalizer (diagnostic_context *context,
2401debfc3dSmrg 				     diagnostic_info *diagnostic)
2411debfc3dSmrg {
242*8feb0f0bSmrg   maybe_unwind_expanded_macro_loc (context, diagnostic_location (diagnostic));
2431debfc3dSmrg }
2441debfc3dSmrg 
2451debfc3dSmrg /* Default tree printer.   Handles declarations only.  */
2461debfc3dSmrg 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 **)2471debfc3dSmrg default_tree_printer (pretty_printer *pp, text_info *text, const char *spec,
248a2dc1f3fSmrg 		      int precision, bool wide, bool set_locus, bool hash,
249a2dc1f3fSmrg 		      bool *, const char **)
2501debfc3dSmrg {
2511debfc3dSmrg   tree t;
2521debfc3dSmrg 
2531debfc3dSmrg   /* FUTURE: %+x should set the locus.  */
2541debfc3dSmrg   if (precision != 0 || wide || hash)
2551debfc3dSmrg     return false;
2561debfc3dSmrg 
2571debfc3dSmrg   switch (*spec)
2581debfc3dSmrg     {
2591debfc3dSmrg     case 'E':
2601debfc3dSmrg       t = va_arg (*text->args_ptr, tree);
2611debfc3dSmrg       if (TREE_CODE (t) == IDENTIFIER_NODE)
2621debfc3dSmrg 	{
2631debfc3dSmrg 	  pp_identifier (pp, IDENTIFIER_POINTER (t));
2641debfc3dSmrg 	  return true;
2651debfc3dSmrg 	}
2661debfc3dSmrg       break;
2671debfc3dSmrg 
2681debfc3dSmrg     case 'D':
2691debfc3dSmrg       t = va_arg (*text->args_ptr, tree);
2701debfc3dSmrg       if (VAR_P (t) && DECL_HAS_DEBUG_EXPR_P (t))
2711debfc3dSmrg 	t = DECL_DEBUG_EXPR (t);
2721debfc3dSmrg       break;
2731debfc3dSmrg 
2741debfc3dSmrg     case 'F':
2751debfc3dSmrg     case 'T':
2761debfc3dSmrg       t = va_arg (*text->args_ptr, tree);
2771debfc3dSmrg       break;
2781debfc3dSmrg 
279a2dc1f3fSmrg     case 'G':
280a2dc1f3fSmrg       percent_G_format (text);
281a2dc1f3fSmrg       return true;
282a2dc1f3fSmrg 
2831debfc3dSmrg     case 'K':
284a2dc1f3fSmrg       t = va_arg (*text->args_ptr, tree);
285c0a68be4Smrg       percent_K_format (text, EXPR_LOCATION (t), TREE_BLOCK (t));
2861debfc3dSmrg       return true;
2871debfc3dSmrg 
2881debfc3dSmrg     default:
2891debfc3dSmrg       return false;
2901debfc3dSmrg     }
2911debfc3dSmrg 
2921debfc3dSmrg   if (set_locus)
293c0a68be4Smrg     text->set_location (0, DECL_SOURCE_LOCATION (t), SHOW_RANGE_WITH_CARET);
2941debfc3dSmrg 
2951debfc3dSmrg   if (DECL_P (t))
2961debfc3dSmrg     {
2971debfc3dSmrg       const char *n = DECL_NAME (t)
2981debfc3dSmrg         ? identifier_to_locale (lang_hooks.decl_printable_name (t, 2))
2991debfc3dSmrg         : _("<anonymous>");
3001debfc3dSmrg       pp_string (pp, n);
3011debfc3dSmrg     }
3021debfc3dSmrg   else
303a2dc1f3fSmrg     dump_generic_node (pp, t, 0, TDF_SLIM, 0);
3041debfc3dSmrg 
3051debfc3dSmrg   return true;
3061debfc3dSmrg }
3071debfc3dSmrg 
3081debfc3dSmrg /* Sets CONTEXT to use language independent diagnostics.  */
3091debfc3dSmrg void
tree_diagnostics_defaults(diagnostic_context * context)3101debfc3dSmrg tree_diagnostics_defaults (diagnostic_context *context)
3111debfc3dSmrg {
3121debfc3dSmrg   diagnostic_starter (context) = default_tree_diagnostic_starter;
3131debfc3dSmrg   diagnostic_finalizer (context) = default_diagnostic_finalizer;
3141debfc3dSmrg   diagnostic_format_decoder (context) = default_tree_printer;
315*8feb0f0bSmrg   context->print_path = default_tree_diagnostic_path_printer;
316*8feb0f0bSmrg   context->make_json_for_path = default_tree_make_json_for_path;
3171debfc3dSmrg }
318