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