PageRenderTime 191ms CodeModel.GetById 36ms RepoModel.GetById 1ms app.codeStats 0ms

/gcc/tree-diagnostic.cc

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