diagnostic.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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 it has been requested that warnings be treated as errors. */
  57. bool warning_as_error_requested;
  58. /* The number of option indexes that can be passed to warning() et
  59. al. */
  60. int n_opts;
  61. /* For each option index that can be passed to warning() et al
  62. (OPT_* from options.h when using this code with the core GCC
  63. options), this array may contain a new kind that the diagnostic
  64. should be changed to before reporting, or DK_UNSPECIFIED to leave
  65. it as the reported kind, or DK_IGNORED to not report it at
  66. all. */
  67. diagnostic_t *classify_diagnostic;
  68. /* History of all changes to the classifications above. This list
  69. is stored in location-order, so we can search it, either
  70. binary-wise or end-to-front, to find the most recent
  71. classification for a given diagnostic, given the location of the
  72. diagnostic. */
  73. diagnostic_classification_change_t *classification_history;
  74. /* The size of the above array. */
  75. int n_classification_history;
  76. /* For pragma push/pop. */
  77. int *push_list;
  78. int n_push;
  79. /* True if we should print the source line with a caret indicating
  80. the location. */
  81. bool show_caret;
  82. /* Maximum width of the source line printed. */
  83. int caret_max_width;
  84. /* Character used for caret diagnostics. */
  85. char caret_char;
  86. /* True if we should print the command line option which controls
  87. each diagnostic, if known. */
  88. bool show_option_requested;
  89. /* True if we should raise a SIGABRT on errors. */
  90. bool abort_on_error;
  91. /* True if we should show the column number on diagnostics. */
  92. bool show_column;
  93. /* True if pedwarns are errors. */
  94. bool pedantic_errors;
  95. /* True if permerrors are warnings. */
  96. bool permissive;
  97. /* The index of the option to associate with turning permerrors into
  98. warnings. */
  99. int opt_permissive;
  100. /* True if errors are fatal. */
  101. bool fatal_errors;
  102. /* True if all warnings should be disabled. */
  103. bool dc_inhibit_warnings;
  104. /* True if warnings should be given in system headers. */
  105. bool dc_warn_system_headers;
  106. /* Maximum number of errors to report. */
  107. unsigned int max_errors;
  108. /* This function is called before any message is printed out. It is
  109. responsible for preparing message prefix and such. For example, it
  110. might say:
  111. In file included from "/usr/local/include/curses.h:5:
  112. from "/home/gdr/src/nifty_printer.h:56:
  113. ...
  114. */
  115. diagnostic_starter_fn begin_diagnostic;
  116. /* This function is called after the diagnostic message is printed. */
  117. diagnostic_finalizer_fn end_diagnostic;
  118. /* Client hook to report an internal error. */
  119. void (*internal_error) (diagnostic_context *, const char *, va_list *);
  120. /* Client hook to say whether the option controlling a diagnostic is
  121. enabled. Returns nonzero if enabled, zero if disabled. */
  122. int (*option_enabled) (int, void *);
  123. /* Client information to pass as second argument to
  124. option_enabled. */
  125. void *option_state;
  126. /* Client hook to return the name of an option that controls a
  127. diagnostic. Returns malloced memory. The first diagnostic_t
  128. argument is the kind of diagnostic before any reclassification
  129. (of warnings as errors, etc.); the second is the kind after any
  130. reclassification. May return NULL if no name is to be printed.
  131. May be passed 0 as well as the index of a particular option. */
  132. char *(*option_name) (diagnostic_context *, int, diagnostic_t, diagnostic_t);
  133. /* Auxiliary data for client. */
  134. void *x_data;
  135. /* Used to detect that the last caret was printed at the same location. */
  136. location_t last_location;
  137. /* Used to detect when the input file stack has changed since last
  138. described. */
  139. const struct line_map *last_module;
  140. int lock;
  141. bool inhibit_notes_p;
  142. };
  143. static inline void
  144. diagnostic_inhibit_notes (diagnostic_context * context)
  145. {
  146. context->inhibit_notes_p = true;
  147. }
  148. /* Client supplied function to announce a diagnostic. */
  149. #define diagnostic_starter(DC) (DC)->begin_diagnostic
  150. /* Client supplied function called after a diagnostic message is
  151. displayed. */
  152. #define diagnostic_finalizer(DC) (DC)->end_diagnostic
  153. /* Extension hooks for client. */
  154. #define diagnostic_context_auxiliary_data(DC) (DC)->x_data
  155. #define diagnostic_info_auxiliary_data(DI) (DI)->x_data
  156. /* Same as pp_format_decoder. Works on 'diagnostic_context *'. */
  157. #define diagnostic_format_decoder(DC) ((DC)->printer->format_decoder)
  158. /* Same as output_prefixing_rule. Works on 'diagnostic_context *'. */
  159. #define diagnostic_prefixing_rule(DC) ((DC)->printer->wrapping.rule)
  160. /* Maximum characters per line in automatic line wrapping mode.
  161. Zero means don't wrap lines. */
  162. #define diagnostic_line_cutoff(DC) ((DC)->printer->wrapping.line_cutoff)
  163. #define diagnostic_flush_buffer(DC) pp_flush ((DC)->printer)
  164. /* True if the last module or file in which a diagnostic was reported is
  165. different from the current one. */
  166. #define diagnostic_last_module_changed(DC, MAP) \
  167. ((DC)->last_module != MAP)
  168. /* Remember the current module or file as being the last one in which we
  169. report a diagnostic. */
  170. #define diagnostic_set_last_module(DC, MAP) \
  171. (DC)->last_module = MAP
  172. /* Raise SIGABRT on any diagnostic of severity DK_ERROR or higher. */
  173. #define diagnostic_abort_on_error(DC) \
  174. (DC)->abort_on_error = true
  175. /* This diagnostic_context is used by front-ends that directly output
  176. diagnostic messages without going through `error', `warning',
  177. and similar functions. */
  178. extern diagnostic_context *global_dc;
  179. /* The total count of a KIND of diagnostics emitted so far. */
  180. #define diagnostic_kind_count(DC, DK) (DC)->diagnostic_count[(int) (DK)]
  181. /* The number of errors that have been issued so far. Ideally, these
  182. would take a diagnostic_context as an argument. */
  183. #define errorcount diagnostic_kind_count (global_dc, DK_ERROR)
  184. /* Similarly, but for warnings. */
  185. #define warningcount diagnostic_kind_count (global_dc, DK_WARNING)
  186. /* Similarly, but for warnings promoted to errors. */
  187. #define werrorcount diagnostic_kind_count (global_dc, DK_WERROR)
  188. /* Similarly, but for sorrys. */
  189. #define sorrycount diagnostic_kind_count (global_dc, DK_SORRY)
  190. /* Returns nonzero if warnings should be emitted. */
  191. #define diagnostic_report_warnings_p(DC, LOC) \
  192. (!(DC)->dc_inhibit_warnings \
  193. && !(in_system_header_at (LOC) && !(DC)->dc_warn_system_headers))
  194. #define report_diagnostic(D) diagnostic_report_diagnostic (global_dc, D)
  195. /* Override the column number to be used for reporting a
  196. diagnostic. */
  197. #define diagnostic_override_column(DI, COL) (DI)->override_column = (COL)
  198. /* Override the option index to be used for reporting a
  199. diagnostic. */
  200. #define diagnostic_override_option_index(DI, OPTIDX) \
  201. ((DI)->option_index = (OPTIDX))
  202. /* Diagnostic related functions. */
  203. extern void diagnostic_initialize (diagnostic_context *, int);
  204. extern void diagnostic_color_init (diagnostic_context *, int value = -1);
  205. extern void diagnostic_finish (diagnostic_context *);
  206. extern void diagnostic_report_current_module (diagnostic_context *, location_t);
  207. extern void diagnostic_show_locus (diagnostic_context *, const diagnostic_info *);
  208. /* Force diagnostics controlled by OPTIDX to be kind KIND. */
  209. extern diagnostic_t diagnostic_classify_diagnostic (diagnostic_context *,
  210. int /* optidx */,
  211. diagnostic_t /* kind */,
  212. location_t);
  213. extern void diagnostic_push_diagnostics (diagnostic_context *, location_t);
  214. extern void diagnostic_pop_diagnostics (diagnostic_context *, location_t);
  215. extern bool diagnostic_report_diagnostic (diagnostic_context *,
  216. diagnostic_info *);
  217. #ifdef ATTRIBUTE_GCC_DIAG
  218. extern void diagnostic_set_info (diagnostic_info *, const char *, va_list *,
  219. location_t, diagnostic_t) ATTRIBUTE_GCC_DIAG(2,0);
  220. extern void diagnostic_set_info_translated (diagnostic_info *, const char *,
  221. va_list *, location_t,
  222. diagnostic_t)
  223. ATTRIBUTE_GCC_DIAG(2,0);
  224. extern void diagnostic_append_note (diagnostic_context *, location_t,
  225. const char *, ...) ATTRIBUTE_GCC_DIAG(3,4);
  226. #endif
  227. extern char *diagnostic_build_prefix (diagnostic_context *, const diagnostic_info *);
  228. void default_diagnostic_starter (diagnostic_context *, diagnostic_info *);
  229. void default_diagnostic_finalizer (diagnostic_context *, diagnostic_info *);
  230. void diagnostic_set_caret_max_width (diagnostic_context *context, int value);
  231. void diagnostic_action_after_output (diagnostic_context *, diagnostic_t);
  232. void diagnostic_file_cache_fini (void);
  233. int get_terminal_width (void);
  234. /* Expand the location of this diagnostic. Use this function for consistency. */
  235. static inline expanded_location
  236. diagnostic_expand_location (const diagnostic_info * diagnostic)
  237. {
  238. expanded_location s
  239. = expand_location_to_spelling_point (diagnostic->location);
  240. if (diagnostic->override_column)
  241. s.column = diagnostic->override_column;
  242. return s;
  243. }
  244. /* Pure text formatting support functions. */
  245. extern char *file_name_as_prefix (diagnostic_context *, const char *);
  246. extern char *build_message_string (const char *, ...) ATTRIBUTE_PRINTF_1;
  247. #endif /* ! GCC_DIAGNOSTIC_H */