diagnostic.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. /* Various declarations for language-independent diagnostics subroutines.
  2. Copyright (C) 2000-2015 Free Software Foundation, Inc.
  3. Contributed by Gabriel Dos Reis <gdr@codesourcery.com>
  4. This file is part of GCC.
  5. GCC is free software; you can redistribute it and/or modify it under
  6. the terms of the GNU General Public License as published by the Free
  7. Software Foundation; either version 3, or (at your option) any later
  8. version.
  9. GCC is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  12. for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with GCC; see the file COPYING3. If not see
  15. <http://www.gnu.org/licenses/>. */
  16. #ifndef GCC_DIAGNOSTIC_H
  17. #define GCC_DIAGNOSTIC_H
  18. #include "pretty-print.h"
  19. #include "diagnostic-core.h"
  20. /* A diagnostic is described by the MESSAGE to send, the FILE and LINE of
  21. its context and its KIND (ice, error, warning, note, ...) See complete
  22. list in diagnostic.def. */
  23. struct diagnostic_info
  24. {
  25. text_info message;
  26. location_t location;
  27. unsigned int override_column;
  28. /* Auxiliary data for client. */
  29. void *x_data;
  30. /* The kind of diagnostic it is about. */
  31. diagnostic_t kind;
  32. /* Which OPT_* directly controls this diagnostic. */
  33. int option_index;
  34. };
  35. /* Each time a diagnostic's classification is changed with a pragma,
  36. we record the change and the location of the change in an array of
  37. these structs. */
  38. struct diagnostic_classification_change_t
  39. {
  40. location_t location;
  41. int option;
  42. diagnostic_t kind;
  43. };
  44. /* Forward declarations. */
  45. typedef void (*diagnostic_starter_fn) (diagnostic_context *,
  46. diagnostic_info *);
  47. typedef diagnostic_starter_fn diagnostic_finalizer_fn;
  48. /* This data structure bundles altogether any information relevant to
  49. the context of a diagnostic message. */
  50. struct diagnostic_context
  51. {
  52. /* Where most of the diagnostic formatting work is done. */
  53. pretty_printer *printer;
  54. /* The number of times we have issued diagnostics. */
  55. int diagnostic_count[DK_LAST_DIAGNOSTIC_KIND];
  56. /* True if we should display the "warnings are being tread as error"
  57. message, usually displayed once per compiler run. */
  58. bool some_warnings_are_errors;
  59. /* True if it has been requested that warnings be treated as errors. */
  60. bool warning_as_error_requested;
  61. /* The number of option indexes that can be passed to warning() et
  62. al. */
  63. int n_opts;
  64. /* For each option index that can be passed to warning() et al
  65. (OPT_* from options.h when using this code with the core GCC
  66. options), this array may contain a new kind that the diagnostic
  67. should be changed to before reporting, or DK_UNSPECIFIED to leave
  68. it as the reported kind, or DK_IGNORED to not report it at
  69. all. */
  70. diagnostic_t *classify_diagnostic;
  71. /* History of all changes to the classifications above. This list
  72. is stored in location-order, so we can search it, either
  73. binary-wise or end-to-front, to find the most recent
  74. classification for a given diagnostic, given the location of the
  75. diagnostic. */
  76. diagnostic_classification_change_t *classification_history;
  77. /* The size of the above array. */
  78. int n_classification_history;
  79. /* For pragma push/pop. */
  80. int *push_list;
  81. int n_push;
  82. /* True if we should print the source line with a caret indicating
  83. the location. */
  84. bool show_caret;
  85. /* Maximum width of the source line printed. */
  86. int caret_max_width;
  87. /* Character used for caret diagnostics. */
  88. char caret_char;
  89. /* True if we should print the command line option which controls
  90. each diagnostic, if known. */
  91. bool show_option_requested;
  92. /* True if we should raise a SIGABRT on errors. */
  93. bool abort_on_error;
  94. /* True if we should show the column number on diagnostics. */
  95. bool show_column;
  96. /* True if pedwarns are errors. */
  97. bool pedantic_errors;
  98. /* True if permerrors are warnings. */
  99. bool permissive;
  100. /* The index of the option to associate with turning permerrors into
  101. warnings. */
  102. int opt_permissive;
  103. /* True if errors are fatal. */
  104. bool fatal_errors;
  105. /* True if all warnings should be disabled. */
  106. bool dc_inhibit_warnings;
  107. /* True if warnings should be given in system headers. */
  108. bool dc_warn_system_headers;
  109. /* Maximum number of errors to report. */
  110. unsigned int max_errors;
  111. /* This function is called before any message is printed out. It is
  112. responsible for preparing message prefix and such. For example, it
  113. might say:
  114. In file included from "/usr/local/include/curses.h:5:
  115. from "/home/gdr/src/nifty_printer.h:56:
  116. ...
  117. */
  118. diagnostic_starter_fn begin_diagnostic;
  119. /* This function is called after the diagnostic message is printed. */
  120. diagnostic_finalizer_fn end_diagnostic;
  121. /* Client hook to report an internal error. */
  122. void (*internal_error) (diagnostic_context *, const char *, va_list *);
  123. /* Client hook to say whether the option controlling a diagnostic is
  124. enabled. Returns nonzero if enabled, zero if disabled. */
  125. int (*option_enabled) (int, void *);
  126. /* Client information to pass as second argument to
  127. option_enabled. */
  128. void *option_state;
  129. /* Client hook to return the name of an option that controls a
  130. diagnostic. Returns malloced memory. The first diagnostic_t
  131. argument is the kind of diagnostic before any reclassification
  132. (of warnings as errors, etc.); the second is the kind after any
  133. reclassification. May return NULL if no name is to be printed.
  134. May be passed 0 as well as the index of a particular option. */
  135. char *(*option_name) (diagnostic_context *, int, diagnostic_t, diagnostic_t);
  136. /* Auxiliary data for client. */
  137. void *x_data;
  138. /* Used to detect that the last caret was printed at the same location. */
  139. location_t last_location;
  140. /* Used to detect when the input file stack has changed since last
  141. described. */
  142. const struct line_map *last_module;
  143. int lock;
  144. bool inhibit_notes_p;
  145. };
  146. static inline void
  147. diagnostic_inhibit_notes (diagnostic_context * context)
  148. {
  149. context->inhibit_notes_p = true;
  150. }
  151. /* Client supplied function to announce a diagnostic. */
  152. #define diagnostic_starter(DC) (DC)->begin_diagnostic
  153. /* Client supplied function called after a diagnostic message is
  154. displayed. */
  155. #define diagnostic_finalizer(DC) (DC)->end_diagnostic
  156. /* Extension hooks for client. */
  157. #define diagnostic_context_auxiliary_data(DC) (DC)->x_data
  158. #define diagnostic_info_auxiliary_data(DI) (DI)->x_data
  159. /* Same as pp_format_decoder. Works on 'diagnostic_context *'. */
  160. #define diagnostic_format_decoder(DC) ((DC)->printer->format_decoder)
  161. /* Same as output_prefixing_rule. Works on 'diagnostic_context *'. */
  162. #define diagnostic_prefixing_rule(DC) ((DC)->printer->wrapping.rule)
  163. /* Maximum characters per line in automatic line wrapping mode.
  164. Zero means don't wrap lines. */
  165. #define diagnostic_line_cutoff(DC) ((DC)->printer->wrapping.line_cutoff)
  166. #define diagnostic_flush_buffer(DC) pp_flush ((DC)->printer)
  167. /* True if the last module or file in which a diagnostic was reported is
  168. different from the current one. */
  169. #define diagnostic_last_module_changed(DC, MAP) \
  170. ((DC)->last_module != MAP)
  171. /* Remember the current module or file as being the last one in which we
  172. report a diagnostic. */
  173. #define diagnostic_set_last_module(DC, MAP) \
  174. (DC)->last_module = MAP
  175. /* Raise SIGABRT on any diagnostic of severity DK_ERROR or higher. */
  176. #define diagnostic_abort_on_error(DC) \
  177. (DC)->abort_on_error = true
  178. /* This diagnostic_context is used by front-ends that directly output
  179. diagnostic messages without going through `error', `warning',
  180. and similar functions. */
  181. extern diagnostic_context *global_dc;
  182. /* The total count of a KIND of diagnostics emitted so far. */
  183. #define diagnostic_kind_count(DC, DK) (DC)->diagnostic_count[(int) (DK)]
  184. /* The number of errors that have been issued so far. Ideally, these
  185. would take a diagnostic_context as an argument. */
  186. #define errorcount diagnostic_kind_count (global_dc, DK_ERROR)
  187. /* Similarly, but for warnings. */
  188. #define warningcount diagnostic_kind_count (global_dc, DK_WARNING)
  189. /* Similarly, but for warnings promoted to errors. */
  190. #define werrorcount diagnostic_kind_count (global_dc, DK_WERROR)
  191. /* Similarly, but for sorrys. */
  192. #define sorrycount diagnostic_kind_count (global_dc, DK_SORRY)
  193. /* Returns nonzero if warnings should be emitted. */
  194. #define diagnostic_report_warnings_p(DC, LOC) \
  195. (!(DC)->dc_inhibit_warnings \
  196. && !(in_system_header_at (LOC) && !(DC)->dc_warn_system_headers))
  197. #define report_diagnostic(D) diagnostic_report_diagnostic (global_dc, D)
  198. /* Override the column number to be used for reporting a
  199. diagnostic. */
  200. #define diagnostic_override_column(DI, COL) (DI)->override_column = (COL)
  201. /* Override the option index to be used for reporting a
  202. diagnostic. */
  203. #define diagnostic_override_option_index(DI, OPTIDX) \
  204. ((DI)->option_index = (OPTIDX))
  205. /* Diagnostic related functions. */
  206. extern void diagnostic_initialize (diagnostic_context *, int);
  207. extern void diagnostic_color_init (diagnostic_context *, int value = -1);
  208. extern void diagnostic_finish (diagnostic_context *);
  209. extern void diagnostic_report_current_module (diagnostic_context *, location_t);
  210. extern void diagnostic_show_locus (diagnostic_context *, const diagnostic_info *);
  211. /* Force diagnostics controlled by OPTIDX to be kind KIND. */
  212. extern diagnostic_t diagnostic_classify_diagnostic (diagnostic_context *,
  213. int /* optidx */,
  214. diagnostic_t /* kind */,
  215. location_t);
  216. extern void diagnostic_push_diagnostics (diagnostic_context *, location_t);
  217. extern void diagnostic_pop_diagnostics (diagnostic_context *, location_t);
  218. extern bool diagnostic_report_diagnostic (diagnostic_context *,
  219. diagnostic_info *);
  220. #ifdef ATTRIBUTE_GCC_DIAG
  221. extern void diagnostic_set_info (diagnostic_info *, const char *, va_list *,
  222. location_t, diagnostic_t) ATTRIBUTE_GCC_DIAG(2,0);
  223. extern void diagnostic_set_info_translated (diagnostic_info *, const char *,
  224. va_list *, location_t,
  225. diagnostic_t)
  226. ATTRIBUTE_GCC_DIAG(2,0);
  227. extern void diagnostic_append_note (diagnostic_context *, location_t,
  228. const char *, ...) ATTRIBUTE_GCC_DIAG(3,4);
  229. #endif
  230. extern char *diagnostic_build_prefix (diagnostic_context *, const diagnostic_info *);
  231. void default_diagnostic_starter (diagnostic_context *, diagnostic_info *);
  232. void default_diagnostic_finalizer (diagnostic_context *, diagnostic_info *);
  233. void diagnostic_set_caret_max_width (diagnostic_context *context, int value);
  234. void diagnostic_action_after_output (diagnostic_context *, diagnostic_t);
  235. void diagnostic_file_cache_fini (void);
  236. int get_terminal_width (void);
  237. /* Expand the location of this diagnostic. Use this function for consistency. */
  238. static inline expanded_location
  239. diagnostic_expand_location (const diagnostic_info * diagnostic)
  240. {
  241. expanded_location s
  242. = expand_location_to_spelling_point (diagnostic->location);
  243. if (diagnostic->override_column)
  244. s.column = diagnostic->override_column;
  245. return s;
  246. }
  247. /* Pure text formatting support functions. */
  248. extern char *file_name_as_prefix (diagnostic_context *, const char *);
  249. extern char *build_message_string (const char *, ...) ATTRIBUTE_PRINTF_1;
  250. #endif /* ! GCC_DIAGNOSTIC_H */