diagnostic.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  1. /* Various declarations for language-independent diagnostics subroutines.
  2. Copyright (C) 2000-2020 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. /* Enum for overriding the standard output format. */
  21. enum diagnostics_output_format
  22. {
  23. /* The default: textual output. */
  24. DIAGNOSTICS_OUTPUT_FORMAT_TEXT,
  25. /* JSON-based output. */
  26. DIAGNOSTICS_OUTPUT_FORMAT_JSON
  27. };
  28. /* An enum for controlling how diagnostic_paths should be printed. */
  29. enum diagnostic_path_format
  30. {
  31. /* Don't print diagnostic_paths. */
  32. DPF_NONE,
  33. /* Print diagnostic_paths by emitting a separate "note" for every event
  34. in the path. */
  35. DPF_SEPARATE_EVENTS,
  36. /* Print diagnostic_paths by consolidating events together where they
  37. are close enough, and printing such runs of events with multiple
  38. calls to diagnostic_show_locus, showing the individual events in
  39. each run via labels in the source. */
  40. DPF_INLINE_EVENTS
  41. };
  42. /* A diagnostic is described by the MESSAGE to send, the FILE and LINE of
  43. its context and its KIND (ice, error, warning, note, ...) See complete
  44. list in diagnostic.def. */
  45. struct diagnostic_info
  46. {
  47. /* Text to be formatted. */
  48. text_info message;
  49. /* The location at which the diagnostic is to be reported. */
  50. rich_location *richloc;
  51. /* An optional bundle of metadata associated with the diagnostic
  52. (or NULL). */
  53. const diagnostic_metadata *metadata;
  54. /* Auxiliary data for client. */
  55. void *x_data;
  56. /* The kind of diagnostic it is about. */
  57. diagnostic_t kind;
  58. /* Which OPT_* directly controls this diagnostic. */
  59. int option_index;
  60. };
  61. /* Each time a diagnostic's classification is changed with a pragma,
  62. we record the change and the location of the change in an array of
  63. these structs. */
  64. struct diagnostic_classification_change_t
  65. {
  66. location_t location;
  67. int option;
  68. diagnostic_t kind;
  69. };
  70. /* Forward declarations. */
  71. typedef void (*diagnostic_starter_fn) (diagnostic_context *,
  72. diagnostic_info *);
  73. typedef void (*diagnostic_start_span_fn) (diagnostic_context *,
  74. expanded_location);
  75. typedef void (*diagnostic_finalizer_fn) (diagnostic_context *,
  76. diagnostic_info *,
  77. diagnostic_t);
  78. class edit_context;
  79. namespace json { class value; }
  80. /* This data structure bundles altogether any information relevant to
  81. the context of a diagnostic message. */
  82. struct diagnostic_context
  83. {
  84. /* Where most of the diagnostic formatting work is done. */
  85. pretty_printer *printer;
  86. /* The number of times we have issued diagnostics. */
  87. int diagnostic_count[DK_LAST_DIAGNOSTIC_KIND];
  88. /* True if it has been requested that warnings be treated as errors. */
  89. bool warning_as_error_requested;
  90. /* The number of option indexes that can be passed to warning() et
  91. al. */
  92. int n_opts;
  93. /* For each option index that can be passed to warning() et al
  94. (OPT_* from options.h when using this code with the core GCC
  95. options), this array may contain a new kind that the diagnostic
  96. should be changed to before reporting, or DK_UNSPECIFIED to leave
  97. it as the reported kind, or DK_IGNORED to not report it at
  98. all. */
  99. diagnostic_t *classify_diagnostic;
  100. /* History of all changes to the classifications above. This list
  101. is stored in location-order, so we can search it, either
  102. binary-wise or end-to-front, to find the most recent
  103. classification for a given diagnostic, given the location of the
  104. diagnostic. */
  105. diagnostic_classification_change_t *classification_history;
  106. /* The size of the above array. */
  107. int n_classification_history;
  108. /* For pragma push/pop. */
  109. int *push_list;
  110. int n_push;
  111. /* True if we should print the source line with a caret indicating
  112. the location. */
  113. bool show_caret;
  114. /* Maximum width of the source line printed. */
  115. int caret_max_width;
  116. /* Character used for caret diagnostics. */
  117. char caret_chars[rich_location::STATICALLY_ALLOCATED_RANGES];
  118. /* True if we should print any CWE identifiers associated with
  119. diagnostics. */
  120. bool show_cwe;
  121. /* How should diagnostic_path objects be printed. */
  122. enum diagnostic_path_format path_format;
  123. /* True if we should print stack depths when printing diagnostic paths. */
  124. bool show_path_depths;
  125. /* True if we should print the command line option which controls
  126. each diagnostic, if known. */
  127. bool show_option_requested;
  128. /* True if we should raise a SIGABRT on errors. */
  129. bool abort_on_error;
  130. /* True if we should show the column number on diagnostics. */
  131. bool show_column;
  132. /* True if pedwarns are errors. */
  133. bool pedantic_errors;
  134. /* True if permerrors are warnings. */
  135. bool permissive;
  136. /* The index of the option to associate with turning permerrors into
  137. warnings. */
  138. int opt_permissive;
  139. /* True if errors are fatal. */
  140. bool fatal_errors;
  141. /* True if all warnings should be disabled. */
  142. bool dc_inhibit_warnings;
  143. /* True if warnings should be given in system headers. */
  144. bool dc_warn_system_headers;
  145. /* Maximum number of errors to report. */
  146. int max_errors;
  147. /* This function is called before any message is printed out. It is
  148. responsible for preparing message prefix and such. For example, it
  149. might say:
  150. In file included from "/usr/local/include/curses.h:5:
  151. from "/home/gdr/src/nifty_printer.h:56:
  152. ...
  153. */
  154. diagnostic_starter_fn begin_diagnostic;
  155. /* This function is called by diagnostic_show_locus in between
  156. disjoint spans of source code, so that the context can print
  157. something to indicate that a new span of source code has begun. */
  158. diagnostic_start_span_fn start_span;
  159. /* This function is called after the diagnostic message is printed. */
  160. diagnostic_finalizer_fn end_diagnostic;
  161. /* Client hook to report an internal error. */
  162. void (*internal_error) (diagnostic_context *, const char *, va_list *);
  163. /* Client hook to say whether the option controlling a diagnostic is
  164. enabled. Returns nonzero if enabled, zero if disabled. */
  165. int (*option_enabled) (int, unsigned, void *);
  166. /* Client information to pass as second argument to
  167. option_enabled. */
  168. void *option_state;
  169. /* Client hook to return the name of an option that controls a
  170. diagnostic. Returns malloced memory. The first diagnostic_t
  171. argument is the kind of diagnostic before any reclassification
  172. (of warnings as errors, etc.); the second is the kind after any
  173. reclassification. May return NULL if no name is to be printed.
  174. May be passed 0 as well as the index of a particular option. */
  175. char *(*option_name) (diagnostic_context *, int, diagnostic_t, diagnostic_t);
  176. /* Client hook to return a URL describing the option that controls
  177. a diagnostic. Returns malloced memory. May return NULL if no URL
  178. is available. May be passed 0 as well as the index of a
  179. particular option. */
  180. char *(*get_option_url) (diagnostic_context *, int);
  181. void (*print_path) (diagnostic_context *, const diagnostic_path *);
  182. json::value *(*make_json_for_path) (diagnostic_context *, const diagnostic_path *);
  183. /* Auxiliary data for client. */
  184. void *x_data;
  185. /* Used to detect that the last caret was printed at the same location. */
  186. location_t last_location;
  187. /* Used to detect when the input file stack has changed since last
  188. described. */
  189. const line_map_ordinary *last_module;
  190. int lock;
  191. /* A copy of lang_hooks.option_lang_mask (). */
  192. unsigned lang_mask;
  193. bool inhibit_notes_p;
  194. /* When printing source code, should the characters at carets and ranges
  195. be colorized? (assuming colorization is on at all).
  196. This should be true for frontends that generate range information
  197. (so that the ranges of code are colorized),
  198. and false for frontends that merely specify points within the
  199. source code (to avoid e.g. colorizing just the first character in
  200. a token, which would look strange). */
  201. bool colorize_source_p;
  202. /* When printing source code, should labelled ranges be printed? */
  203. bool show_labels_p;
  204. /* When printing source code, should there be a left-hand margin
  205. showing line numbers? */
  206. bool show_line_numbers_p;
  207. /* If printing source code, what should the minimum width of the margin
  208. be? Line numbers will be right-aligned, and padded to this width. */
  209. int min_margin_width;
  210. /* Usable by plugins; if true, print a debugging ruler above the
  211. source output. */
  212. bool show_ruler_p;
  213. /* If true, print fixits in machine-parseable form after the
  214. rest of the diagnostic. */
  215. bool parseable_fixits_p;
  216. /* If non-NULL, an edit_context to which fix-it hints should be
  217. applied, for generating patches. */
  218. edit_context *edit_context_ptr;
  219. /* How many diagnostic_group instances are currently alive. */
  220. int diagnostic_group_nesting_depth;
  221. /* How many diagnostics have been emitted since the bottommost
  222. diagnostic_group was pushed. */
  223. int diagnostic_group_emission_count;
  224. /* Optional callbacks for handling diagnostic groups. */
  225. /* If non-NULL, this will be called immediately before the first
  226. time a diagnostic is emitted within a stack of groups. */
  227. void (*begin_group_cb) (diagnostic_context * context);
  228. /* If non-NULL, this will be called when a stack of groups is
  229. popped if any diagnostics were emitted within that group. */
  230. void (*end_group_cb) (diagnostic_context * context);
  231. /* Callback for final cleanup. */
  232. void (*final_cb) (diagnostic_context *context);
  233. };
  234. static inline void
  235. diagnostic_inhibit_notes (diagnostic_context * context)
  236. {
  237. context->inhibit_notes_p = true;
  238. }
  239. /* Client supplied function to announce a diagnostic. */
  240. #define diagnostic_starter(DC) (DC)->begin_diagnostic
  241. /* Client supplied function called after a diagnostic message is
  242. displayed. */
  243. #define diagnostic_finalizer(DC) (DC)->end_diagnostic
  244. /* Extension hooks for client. */
  245. #define diagnostic_context_auxiliary_data(DC) (DC)->x_data
  246. #define diagnostic_info_auxiliary_data(DI) (DI)->x_data
  247. /* Same as pp_format_decoder. Works on 'diagnostic_context *'. */
  248. #define diagnostic_format_decoder(DC) ((DC)->printer->format_decoder)
  249. /* Same as output_prefixing_rule. Works on 'diagnostic_context *'. */
  250. #define diagnostic_prefixing_rule(DC) ((DC)->printer->wrapping.rule)
  251. /* Raise SIGABRT on any diagnostic of severity DK_ERROR or higher. */
  252. #define diagnostic_abort_on_error(DC) \
  253. (DC)->abort_on_error = true
  254. /* This diagnostic_context is used by front-ends that directly output
  255. diagnostic messages without going through `error', `warning',
  256. and similar functions. */
  257. extern diagnostic_context *global_dc;
  258. /* Returns whether the diagnostic framework has been intialized already and is
  259. ready for use. */
  260. #define diagnostic_ready_p() (global_dc->printer != NULL)
  261. /* The total count of a KIND of diagnostics emitted so far. */
  262. #define diagnostic_kind_count(DC, DK) (DC)->diagnostic_count[(int) (DK)]
  263. /* The number of errors that have been issued so far. Ideally, these
  264. would take a diagnostic_context as an argument. */
  265. #define errorcount diagnostic_kind_count (global_dc, DK_ERROR)
  266. /* Similarly, but for warnings. */
  267. #define warningcount diagnostic_kind_count (global_dc, DK_WARNING)
  268. /* Similarly, but for warnings promoted to errors. */
  269. #define werrorcount diagnostic_kind_count (global_dc, DK_WERROR)
  270. /* Similarly, but for sorrys. */
  271. #define sorrycount diagnostic_kind_count (global_dc, DK_SORRY)
  272. /* Returns nonzero if warnings should be emitted. */
  273. #define diagnostic_report_warnings_p(DC, LOC) \
  274. (!(DC)->dc_inhibit_warnings \
  275. && !(in_system_header_at (LOC) && !(DC)->dc_warn_system_headers))
  276. /* Override the option index to be used for reporting a
  277. diagnostic. */
  278. static inline void
  279. diagnostic_override_option_index (diagnostic_info *info, int optidx)
  280. {
  281. info->option_index = optidx;
  282. }
  283. /* Diagnostic related functions. */
  284. extern void diagnostic_initialize (diagnostic_context *, int);
  285. extern void diagnostic_color_init (diagnostic_context *, int value = -1);
  286. extern void diagnostic_urls_init (diagnostic_context *, int value = -1);
  287. extern void diagnostic_finish (diagnostic_context *);
  288. extern void diagnostic_report_current_module (diagnostic_context *, location_t);
  289. extern void diagnostic_show_locus (diagnostic_context *,
  290. rich_location *richloc,
  291. diagnostic_t diagnostic_kind);
  292. extern void diagnostic_show_any_path (diagnostic_context *, diagnostic_info *);
  293. /* Force diagnostics controlled by OPTIDX to be kind KIND. */
  294. extern diagnostic_t diagnostic_classify_diagnostic (diagnostic_context *,
  295. int /* optidx */,
  296. diagnostic_t /* kind */,
  297. location_t);
  298. extern void diagnostic_push_diagnostics (diagnostic_context *, location_t);
  299. extern void diagnostic_pop_diagnostics (diagnostic_context *, location_t);
  300. extern bool diagnostic_report_diagnostic (diagnostic_context *,
  301. diagnostic_info *);
  302. #ifdef ATTRIBUTE_GCC_DIAG
  303. extern void diagnostic_set_info (diagnostic_info *, const char *, va_list *,
  304. rich_location *, diagnostic_t) ATTRIBUTE_GCC_DIAG(2,0);
  305. extern void diagnostic_set_info_translated (diagnostic_info *, const char *,
  306. va_list *, rich_location *,
  307. diagnostic_t)
  308. ATTRIBUTE_GCC_DIAG(2,0);
  309. extern void diagnostic_append_note (diagnostic_context *, location_t,
  310. const char *, ...) ATTRIBUTE_GCC_DIAG(3,4);
  311. #endif
  312. extern char *diagnostic_build_prefix (diagnostic_context *, const diagnostic_info *);
  313. void default_diagnostic_starter (diagnostic_context *, diagnostic_info *);
  314. void default_diagnostic_start_span_fn (diagnostic_context *,
  315. expanded_location);
  316. void default_diagnostic_finalizer (diagnostic_context *, diagnostic_info *,
  317. diagnostic_t);
  318. void diagnostic_set_caret_max_width (diagnostic_context *context, int value);
  319. void diagnostic_action_after_output (diagnostic_context *, diagnostic_t);
  320. void diagnostic_check_max_errors (diagnostic_context *, bool flush = false);
  321. void diagnostic_file_cache_fini (void);
  322. int get_terminal_width (void);
  323. /* Return the location associated to this diagnostic. Parameter WHICH
  324. specifies which location. By default, expand the first one. */
  325. static inline location_t
  326. diagnostic_location (const diagnostic_info * diagnostic, int which = 0)
  327. {
  328. return diagnostic->message.get_location (which);
  329. }
  330. /* Return the number of locations to be printed in DIAGNOSTIC. */
  331. static inline unsigned int
  332. diagnostic_num_locations (const diagnostic_info * diagnostic)
  333. {
  334. return diagnostic->message.m_richloc->get_num_locations ();
  335. }
  336. /* Expand the location of this diagnostic. Use this function for
  337. consistency. Parameter WHICH specifies which location. By default,
  338. expand the first one. */
  339. static inline expanded_location
  340. diagnostic_expand_location (const diagnostic_info * diagnostic, int which = 0)
  341. {
  342. return diagnostic->richloc->get_expanded_location (which);
  343. }
  344. /* This is somehow the right-side margin of a caret line, that is, we
  345. print at least these many characters after the position pointed at
  346. by the caret. */
  347. const int CARET_LINE_MARGIN = 10;
  348. /* Return true if the two locations can be represented within the same
  349. caret line. This is used to build a prefix and also to determine
  350. whether to print one or two caret lines. */
  351. static inline bool
  352. diagnostic_same_line (const diagnostic_context *context,
  353. expanded_location s1, expanded_location s2)
  354. {
  355. return s2.column && s1.line == s2.line
  356. && context->caret_max_width - CARET_LINE_MARGIN > abs (s1.column - s2.column);
  357. }
  358. extern const char *diagnostic_get_color_for_kind (diagnostic_t kind);
  359. /* Pure text formatting support functions. */
  360. extern char *file_name_as_prefix (diagnostic_context *, const char *);
  361. extern char *build_message_string (const char *, ...) ATTRIBUTE_PRINTF_1;
  362. extern void diagnostic_output_format_init (diagnostic_context *,
  363. enum diagnostics_output_format);
  364. /* Compute the number of digits in the decimal representation of an integer. */
  365. extern int num_digits (int);
  366. extern json::value *json_from_expanded_location (location_t loc);
  367. #endif /* ! GCC_DIAGNOSTIC_H */