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