tree-diagnostic.c 11 KB

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