diagnostic.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. /* Various declarations for language-independent diagnostics subroutines.
  2. Copyright (C) 2000-2018 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 to be formatted. */
  26. text_info message;
  27. /* The location at which the diagnostic is to be reported. */
  28. rich_location *richloc;
  29. /* Auxiliary data for client. */
  30. void *x_data;
  31. /* The kind of diagnostic it is about. */
  32. diagnostic_t kind;
  33. /* Which OPT_* directly controls this diagnostic. */
  34. int option_index;
  35. };
  36. /* Each time a diagnostic's classification is changed with a pragma,
  37. we record the change and the location of the change in an array of
  38. these structs. */
  39. struct diagnostic_classification_change_t
  40. {
  41. location_t location;
  42. int option;
  43. diagnostic_t kind;
  44. };
  45. /* Forward declarations. */
  46. typedef void (*diagnostic_starter_fn) (diagnostic_context *,
  47. diagnostic_info *);
  48. typedef void (*diagnostic_start_span_fn) (diagnostic_context *,
  49. expanded_location);
  50. typedef diagnostic_starter_fn diagnostic_finalizer_fn;
  51. class edit_context;
  52. /* This data structure bundles altogether any information relevant to
  53. the context of a diagnostic message. */
  54. struct diagnostic_context
  55. {
  56. /* Where most of the diagnostic formatting work is done. */
  57. pretty_printer *printer;
  58. /* The number of times we have issued diagnostics. */
  59. int diagnostic_count[DK_LAST_DIAGNOSTIC_KIND];
  60. /* True if it has been requested that warnings be treated as errors. */
  61. bool warning_as_error_requested;
  62. /* The number of option indexes that can be passed to warning() et
  63. al. */
  64. int n_opts;
  65. /* For each option index that can be passed to warning() et al
  66. (OPT_* from options.h when using this code with the core GCC
  67. options), this array may contain a new kind that the diagnostic
  68. should be changed to before reporting, or DK_UNSPECIFIED to leave
  69. it as the reported kind, or DK_IGNORED to not report it at
  70. all. */
  71. diagnostic_t *classify_diagnostic;
  72. /* History of all changes to the classifications above. This list
  73. is stored in location-order, so we can search it, either
  74. binary-wise or end-to-front, to find the most recent
  75. classification for a given diagnostic, given the location of the
  76. diagnostic. */
  77. diagnostic_classification_change_t *classification_history;
  78. /* The size of the above array. */
  79. int n_classification_history;
  80. /* For pragma push/pop. */
  81. int *push_list;
  82. int n_push;
  83. /* True if we should print the source line with a caret indicating
  84. the location. */
  85. bool show_caret;
  86. /* Maximum width of the source line printed. */
  87. int caret_max_width;
  88. /* Character used for caret diagnostics. */
  89. char caret_chars[rich_location::STATICALLY_ALLOCATED_RANGES];
  90. /* True if we should print the command line option which controls
  91. each diagnostic, if known. */
  92. bool show_option_requested;
  93. /* True if we should raise a SIGABRT on errors. */
  94. bool abort_on_error;
  95. /* True if we should show the column number on diagnostics. */
  96. bool show_column;
  97. /* True if pedwarns are errors. */
  98. bool pedantic_errors;
  99. /* True if permerrors are warnings. */
  100. bool permissive;
  101. /* The index of the option to associate with turning permerrors into
  102. warnings. */
  103. int opt_permissive;
  104. /* True if errors are fatal. */
  105. bool fatal_errors;
  106. /* True if all warnings should be disabled. */
  107. bool dc_inhibit_warnings;
  108. /* True if warnings should be given in system headers. */
  109. bool dc_warn_system_headers;
  110. /* Maximum number of errors to report. */
  111. int max_errors;
  112. /* This function is called before any message is printed out. It is
  113. responsible for preparing message prefix and such. For example, it
  114. might say:
  115. In file included from "/usr/local/include/curses.h:5:
  116. from "/home/gdr/src/nifty_printer.h:56:
  117. ...
  118. */
  119. diagnostic_starter_fn begin_diagnostic;
  120. /* This function is called by diagnostic_show_locus in between
  121. disjoint spans of source code, so that the context can print
  122. something to indicate that a new span of source code has begun. */
  123. diagnostic_start_span_fn start_span;
  124. /* This function is called after the diagnostic message is printed. */
  125. diagnostic_finalizer_fn end_diagnostic;
  126. /* Client hook to report an internal error. */
  127. void (*internal_error) (diagnostic_context *, const char *, va_list *);
  128. /* Client hook to say whether the option controlling a diagnostic is
  129. enabled. Returns nonzero if enabled, zero if disabled. */
  130. int (*option_enabled) (int, void *);
  131. /* Client information to pass as second argument to
  132. option_enabled. */
  133. void *option_state;
  134. /* Client hook to return the name of an option that controls a
  135. diagnostic. Returns malloced memory. The first diagnostic_t
  136. argument is the kind of diagnostic before any reclassification
  137. (of warnings as errors, etc.); the second is the kind after any
  138. reclassification. May return NULL if no name is to be printed.
  139. May be passed 0 as well as the index of a particular option. */
  140. char *(*option_name) (diagnostic_context *, int, diagnostic_t, diagnostic_t);
  141. /* Auxiliary data for client. */
  142. void *x_data;
  143. /* Used to detect that the last caret was printed at the same location. */
  144. location_t last_location;
  145. /* Used to detect when the input file stack has changed since last
  146. described. */
  147. const line_map_ordinary *last_module;
  148. int lock;
  149. bool inhibit_notes_p;
  150. /* When printing source code, should the characters at carets and ranges
  151. be colorized? (assuming colorization is on at all).
  152. This should be true for frontends that generate range information
  153. (so that the ranges of code are colorized),
  154. and false for frontends that merely specify points within the
  155. source code (to avoid e.g. colorizing just the first character in
  156. a token, which would look strange). */
  157. bool colorize_source_p;
  158. /* Usable by plugins; if true, print a debugging ruler above the
  159. source output. */
  160. bool show_ruler_p;
  161. /* If true, print fixits in machine-parseable form after the
  162. rest of the diagnostic. */
  163. bool parseable_fixits_p;
  164. /* If non-NULL, an edit_context to which fix-it hints should be
  165. applied, for generating patches. */
  166. edit_context *edit_context_ptr;
  167. };
  168. static inline void
  169. diagnostic_inhibit_notes (diagnostic_context * context)
  170. {
  171. context->inhibit_notes_p = true;
  172. }
  173. /* Client supplied function to announce a diagnostic. */
  174. #define diagnostic_starter(DC) (DC)->begin_diagnostic
  175. /* Client supplied function called after a diagnostic message is
  176. displayed. */
  177. #define diagnostic_finalizer(DC) (DC)->end_diagnostic
  178. /* Extension hooks for client. */
  179. #define diagnostic_context_auxiliary_data(DC) (DC)->x_data
  180. #define diagnostic_info_auxiliary_data(DI) (DI)->x_data
  181. /* Same as pp_format_decoder. Works on 'diagnostic_context *'. */
  182. #define diagnostic_format_decoder(DC) ((DC)->printer->format_decoder)
  183. /* Same as output_prefixing_rule. Works on 'diagnostic_context *'. */
  184. #define diagnostic_prefixing_rule(DC) ((DC)->printer->wrapping.rule)
  185. /* Raise SIGABRT on any diagnostic of severity DK_ERROR or higher. */
  186. #define diagnostic_abort_on_error(DC) \
  187. (DC)->abort_on_error = true
  188. /* This diagnostic_context is used by front-ends that directly output
  189. diagnostic messages without going through `error', `warning',
  190. and similar functions. */
  191. extern diagnostic_context *global_dc;
  192. /* The total count of a KIND of diagnostics emitted so far. */
  193. #define diagnostic_kind_count(DC, DK) (DC)->diagnostic_count[(int) (DK)]
  194. /* The number of errors that have been issued so far. Ideally, these
  195. would take a diagnostic_context as an argument. */
  196. #define errorcount diagnostic_kind_count (global_dc, DK_ERROR)
  197. /* Similarly, but for warnings. */
  198. #define warningcount diagnostic_kind_count (global_dc, DK_WARNING)
  199. /* Similarly, but for warnings promoted to errors. */
  200. #define werrorcount diagnostic_kind_count (global_dc, DK_WERROR)
  201. /* Similarly, but for sorrys. */
  202. #define sorrycount diagnostic_kind_count (global_dc, DK_SORRY)
  203. /* Returns nonzero if warnings should be emitted. */
  204. #define diagnostic_report_warnings_p(DC, LOC) \
  205. (!(DC)->dc_inhibit_warnings \
  206. && !(in_system_header_at (LOC) && !(DC)->dc_warn_system_headers))
  207. /* Override the option index to be used for reporting a
  208. diagnostic. */
  209. static inline void
  210. diagnostic_override_option_index (diagnostic_info *info, int optidx)
  211. {
  212. info->option_index = optidx;
  213. }
  214. /* Diagnostic related functions. */
  215. extern void diagnostic_initialize (diagnostic_context *, int);
  216. extern void diagnostic_color_init (diagnostic_context *, int value = -1);
  217. extern void diagnostic_finish (diagnostic_context *);
  218. extern void diagnostic_report_current_module (diagnostic_context *, location_t);
  219. extern void diagnostic_show_locus (diagnostic_context *,
  220. rich_location *richloc,
  221. diagnostic_t diagnostic_kind);
  222. /* Force diagnostics controlled by OPTIDX to be kind KIND. */
  223. extern diagnostic_t diagnostic_classify_diagnostic (diagnostic_context *,
  224. int /* optidx */,
  225. diagnostic_t /* kind */,
  226. location_t);
  227. extern void diagnostic_push_diagnostics (diagnostic_context *, location_t);
  228. extern void diagnostic_pop_diagnostics (diagnostic_context *, location_t);
  229. extern bool diagnostic_report_diagnostic (diagnostic_context *,
  230. diagnostic_info *);
  231. #ifdef ATTRIBUTE_GCC_DIAG
  232. extern void diagnostic_set_info (diagnostic_info *, const char *, va_list *,
  233. rich_location *, diagnostic_t) ATTRIBUTE_GCC_DIAG(2,0);
  234. extern void diagnostic_set_info_translated (diagnostic_info *, const char *,
  235. va_list *, rich_location *,
  236. diagnostic_t)
  237. ATTRIBUTE_GCC_DIAG(2,0);
  238. extern void diagnostic_append_note (diagnostic_context *, location_t,
  239. const char *, ...) ATTRIBUTE_GCC_DIAG(3,4);
  240. #endif
  241. extern char *diagnostic_build_prefix (diagnostic_context *, const diagnostic_info *);
  242. void default_diagnostic_starter (diagnostic_context *, diagnostic_info *);
  243. void default_diagnostic_start_span_fn (diagnostic_context *,
  244. expanded_location);
  245. void default_diagnostic_finalizer (diagnostic_context *, diagnostic_info *);
  246. void diagnostic_set_caret_max_width (diagnostic_context *context, int value);
  247. void diagnostic_action_after_output (diagnostic_context *, diagnostic_t);
  248. void diagnostic_check_max_errors (diagnostic_context *, bool flush = false);
  249. void diagnostic_file_cache_fini (void);
  250. int get_terminal_width (void);
  251. /* Return the location associated to this diagnostic. Parameter WHICH
  252. specifies which location. By default, expand the first one. */
  253. static inline location_t
  254. diagnostic_location (const diagnostic_info * diagnostic, int which = 0)
  255. {
  256. return diagnostic->message.get_location (which);
  257. }
  258. /* Return the number of locations to be printed in DIAGNOSTIC. */
  259. static inline unsigned int
  260. diagnostic_num_locations (const diagnostic_info * diagnostic)
  261. {
  262. return diagnostic->message.m_richloc->get_num_locations ();
  263. }
  264. /* Expand the location of this diagnostic. Use this function for
  265. consistency. Parameter WHICH specifies which location. By default,
  266. expand the first one. */
  267. static inline expanded_location
  268. diagnostic_expand_location (const diagnostic_info * diagnostic, int which = 0)
  269. {
  270. return diagnostic->richloc->get_expanded_location (which);
  271. }
  272. /* This is somehow the right-side margin of a caret line, that is, we
  273. print at least these many characters after the position pointed at
  274. by the caret. */
  275. const int CARET_LINE_MARGIN = 10;
  276. /* Return true if the two locations can be represented within the same
  277. caret line. This is used to build a prefix and also to determine
  278. whether to print one or two caret lines. */
  279. static inline bool
  280. diagnostic_same_line (const diagnostic_context *context,
  281. expanded_location s1, expanded_location s2)
  282. {
  283. return s2.column && s1.line == s2.line
  284. && context->caret_max_width - CARET_LINE_MARGIN > abs (s1.column - s2.column);
  285. }
  286. extern const char *diagnostic_get_color_for_kind (diagnostic_t kind);
  287. /* Pure text formatting support functions. */
  288. extern char *file_name_as_prefix (diagnostic_context *, const char *);
  289. extern char *build_message_string (const char *, ...) ATTRIBUTE_PRINTF_1;
  290. #endif /* ! GCC_DIAGNOSTIC_H */