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