diff options
author | David Malcolm <dmalcolm@redhat.com> | 2015-11-06 19:50:50 +0000 |
---|---|---|
committer | David Malcolm <dmalcolm@gcc.gnu.org> | 2015-11-06 19:50:50 +0000 |
commit | 8a64515099e64564542cbd09be7c9a21c2f580f3 (patch) | |
tree | de618a78e7240cd1dec5c9b663ed1464c2775915 /gcc/diagnostic.c | |
parent | 277ec793cbb5053626a75bcfdb82af03fc735b3e (diff) | |
download | gcc-8a64515099e64564542cbd09be7c9a21c2f580f3.zip gcc-8a64515099e64564542cbd09be7c9a21c2f580f3.tar.gz gcc-8a64515099e64564542cbd09be7c9a21c2f580f3.tar.bz2 |
Reimplement diagnostic_show_locus, introducing rich_location classes
gcc/ChangeLog:
* diagnostic-color.c (color_dict): Eliminate "caret"; add "range1"
and "range2".
(parse_gcc_colors): Update comment to describe default GCC_COLORS.
* diagnostic-core.h (warning_at_rich_loc): New declaration.
(error_at_rich_loc): New declaration.
(permerror_at_rich_loc): New declaration.
(inform_at_rich_loc): New declaration.
* diagnostic-show-locus.c (adjust_line): Delete.
(struct point_state): New struct.
(class colorizer): New class.
(class layout_point): New class.
(class layout_range): New class.
(struct line_bounds): New.
(class layout): New class.
(colorizer::colorizer): New ctor.
(colorizer::~colorizer): New dtor.
(layout::layout): New ctor.
(layout::print_source_line): New method.
(layout::print_annotation_line): New method.
(layout::get_state_at_point): New method.
(layout::get_x_bound_for_row): New method.
(diagnostic_show_locus): Reimplement in terms of class layout.
(diagnostic_print_caret_line): Delete.
* diagnostic.c (diagnostic_initialize): Replace
MAX_LOCATIONS_PER_MESSAGE with rich_location::MAX_RANGES.
(diagnostic_set_info_translated): Convert param from location_t
to rich_location *. Eliminate calls to set_location on the
message in favor of storing the rich_location ptr there.
(diagnostic_set_info): Convert param from location_t to
rich_location *.
(diagnostic_build_prefix): Break out array into...
(diagnostic_kind_color): New variable.
(diagnostic_get_color_for_kind): New function.
(diagnostic_report_diagnostic): Colorize the option_text
using the color for the severity.
(diagnostic_append_note): Update for change in signature of
diagnostic_set_info.
(diagnostic_append_note_at_rich_loc): New function.
(emit_diagnostic): Update for change in signature of
diagnostic_set_info.
(inform): Likewise.
(inform_at_rich_loc): New function.
(inform_n): Update for change in signature of diagnostic_set_info.
(warning): Likewise.
(warning_at): Likewise.
(warning_at_rich_loc): New function.
(warning_n): Update for change in signature of diagnostic_set_info.
(pedwarn): Likewise.
(permerror): Likewise.
(permerror_at_rich_loc): New function.
(error): Update for change in signature of diagnostic_set_info.
(error_n): Likewise.
(error_at): Likewise.
(error_at_rich_loc): New function.
(sorry): Update for change in signature of diagnostic_set_info.
(fatal_error): Likewise.
(internal_error): Likewise.
(internal_error_no_backtrace): Likewise.
(source_range::debug): New function.
* diagnostic.h (struct diagnostic_info): Eliminate field
"override_column". Add field "richloc".
(struct diagnostic_context): Add field "colorize_source_p".
(diagnostic_override_column): Delete.
(diagnostic_set_info): Convert param from location_t to
rich_location *.
(diagnostic_set_info_translated): Likewise.
(diagnostic_append_note_at_rich_loc): New function.
(diagnostic_num_locations): New function.
(diagnostic_expand_location): Get the location from the
rich_location.
(diagnostic_print_caret_line): Delete.
(diagnostic_get_color_for_kind): New declaration.
* genmatch.c (linemap_client_expand_location_to_spelling_point): New.
(error_cb): Update for change in signature of "error" callback.
(fatal_at): Likewise.
(warning_at): Likewise.
* input.c (linemap_client_expand_location_to_spelling_point): New.
* pretty-print.c (text_info::set_range): New method.
(text_info::get_location): New method.
* pretty-print.h (MAX_LOCATIONS_PER_MESSAGE): Eliminate this macro.
(struct text_info): Eliminate "locations" array in favor of
"m_richloc", a rich_location *.
(textinfo::set_location): Add a "caret_p" param, and reimplement
in terms of a call to set_range.
(textinfo::get_location): Eliminate inline implementation in favor of
an out-of-line reimplementation.
(textinfo::set_range): New method.
* rtl-error.c (diagnostic_for_asm): Update for change in signature
of diagnostic_set_info.
* tree-diagnostic.c (default_tree_printer): Update for new
"caret_p" param for textinfo::set_location.
* tree-pretty-print.c (percent_K_format): Likewise.
gcc/c-family/ChangeLog:
* c-common.c (c_cpp_error): Convert parameter from location_t to
rich_location *. Eliminate the "column_override" parameter and
the call to diagnostic_override_column.
Update the "done_lexing" clause to set range 0
on the rich_location, rather than overwriting a location_t.
* c-common.h (c_cpp_error): Convert parameter from location_t to
rich_location *. Eliminate the "column_override" parameter.
gcc/c/ChangeLog:
* c-decl.c (warn_defaults_to): Update for change in signature
of diagnostic_set_info.
* c-errors.c (pedwarn_c99): Likewise.
(pedwarn_c90): Likewise.
* c-objc-common.c (c_tree_printer): Update for new "caret_p" param
for textinfo::set_location.
gcc/cp/ChangeLog:
* error.c (cp_printer): Update for new "caret_p" param for
textinfo::set_location.
(pedwarn_cxx98): Update for change in signature of
diagnostic_set_info.
gcc/fortran/ChangeLog:
* cpp.c (cb_cpp_error): Convert parameter from location_t to
rich_location *. Eliminate the "column_override" parameter.
* error.c (gfc_warning): Update for change in signature of
diagnostic_set_info.
(gfc_format_decoder): Update handling of %C/%L for changes
to struct text_info.
(gfc_diagnostic_starter): Use richloc when determining whether to
print one locus or two. When handling a location that will
involve a call to diagnostic_show_locus, only attempt to print the
locus for the primary location, and don't call into
diagnostic_print_caret_line.
(gfc_warning_now_at): Update for change in signature of
diagnostic_set_info.
(gfc_warning_now): Likewise.
(gfc_error_now): Likewise.
(gfc_fatal_error): Likewise.
(gfc_error): Likewise.
(gfc_internal_error): Likewise.
gcc/testsuite/ChangeLog:
* gcc.dg/plugin/diagnostic-test-show-locus-bw.c: New file.
* gcc.dg/plugin/diagnostic-test-show-locus-color.c: New file.
* gcc.dg/plugin/diagnostic_plugin_test_show_locus.c: New file.
* gcc.dg/plugin/plugin.exp (plugin_test_list): Add the above.
* lib/gcc-dg.exp: Load multiline.exp.
libcpp/ChangeLog:
* errors.c (cpp_diagnostic): Update for change in signature
of "error" callback.
(cpp_diagnostic_with_line): Likewise, calling override_column
on the rich_location.
* include/cpplib.h (struct cpp_callbacks): Within "error"
callback, convert param from source_location to rich_location *,
and drop column_override param.
* include/line-map.h (struct source_range): New struct.
(struct location_range): New struct.
(class rich_location): New class.
(linemap_client_expand_location_to_spelling_point): New declaration.
* line-map.c (rich_location::rich_location): New ctors.
(rich_location::lazily_expand_location): New method.
(rich_location::override_column): New method.
(rich_location::add_range): New methods.
(rich_location::set_range): New method.
From-SVN: r229884
Diffstat (limited to 'gcc/diagnostic.c')
-rw-r--r-- | gcc/diagnostic.c | 202 |
1 files changed, 170 insertions, 32 deletions
diff --git a/gcc/diagnostic.c b/gcc/diagnostic.c index 11c369d..ee034e7 100644 --- a/gcc/diagnostic.c +++ b/gcc/diagnostic.c @@ -144,7 +144,7 @@ diagnostic_initialize (diagnostic_context *context, int n_opts) context->classify_diagnostic[i] = DK_UNSPECIFIED; context->show_caret = false; diagnostic_set_caret_max_width (context, pp_line_cutoff (context->printer)); - for (i = 0; i < MAX_LOCATIONS_PER_MESSAGE; i++) + for (i = 0; i < rich_location::MAX_RANGES; i++) context->caret_chars[i] = '^'; context->show_option_requested = false; context->abort_on_error = false; @@ -234,16 +234,15 @@ diagnostic_finish (diagnostic_context *context) translated. */ void diagnostic_set_info_translated (diagnostic_info *diagnostic, const char *msg, - va_list *args, location_t location, + va_list *args, rich_location *richloc, diagnostic_t kind) { + gcc_assert (richloc); diagnostic->message.err_no = errno; diagnostic->message.args_ptr = args; diagnostic->message.format_spec = msg; - diagnostic->message.set_location (0, location); - for (int i = 1; i < MAX_LOCATIONS_PER_MESSAGE; i++) - diagnostic->message.set_location (i, UNKNOWN_LOCATION); - diagnostic->override_column = 0; + diagnostic->message.m_richloc = richloc; + diagnostic->richloc = richloc; diagnostic->kind = kind; diagnostic->option_index = 0; } @@ -252,10 +251,27 @@ diagnostic_set_info_translated (diagnostic_info *diagnostic, const char *msg, translated. */ void diagnostic_set_info (diagnostic_info *diagnostic, const char *gmsgid, - va_list *args, location_t location, + va_list *args, rich_location *richloc, diagnostic_t kind) { - diagnostic_set_info_translated (diagnostic, _(gmsgid), args, location, kind); + gcc_assert (richloc); + diagnostic_set_info_translated (diagnostic, _(gmsgid), args, richloc, kind); +} + +static const char *const diagnostic_kind_color[] = { +#define DEFINE_DIAGNOSTIC_KIND(K, T, C) (C), +#include "diagnostic.def" +#undef DEFINE_DIAGNOSTIC_KIND + NULL +}; + +/* Get a color name for diagnostics of type KIND + Result could be NULL. */ + +const char * +diagnostic_get_color_for_kind (diagnostic_t kind) +{ + return diagnostic_kind_color[kind]; } /* Return a malloc'd string describing a location. The caller is @@ -270,12 +286,6 @@ diagnostic_build_prefix (diagnostic_context *context, #undef DEFINE_DIAGNOSTIC_KIND "must-not-happen" }; - static const char *const diagnostic_kind_color[] = { -#define DEFINE_DIAGNOSTIC_KIND(K, T, C) (C), -#include "diagnostic.def" -#undef DEFINE_DIAGNOSTIC_KIND - NULL - }; gcc_assert (diagnostic->kind < DK_LAST_DIAGNOSTIC_KIND); const char *text = _(diagnostic_kind_text[diagnostic->kind]); @@ -770,10 +780,14 @@ diagnostic_report_diagnostic (diagnostic_context *context, if (option_text) { + const char *cs + = colorize_start (pp_show_color (context->printer), + diagnostic_kind_color[diagnostic->kind]); + const char *ce = colorize_stop (pp_show_color (context->printer)); diagnostic->message.format_spec = ACONCAT ((diagnostic->message.format_spec, " ", - "[", option_text, "]", + "[", cs, option_text, ce, "]", NULL)); free (option_text); } @@ -853,9 +867,40 @@ diagnostic_append_note (diagnostic_context *context, diagnostic_info diagnostic; va_list ap; const char *saved_prefix; + rich_location richloc (location); va_start (ap, gmsgid); - diagnostic_set_info (&diagnostic, gmsgid, &ap, location, DK_NOTE); + diagnostic_set_info (&diagnostic, gmsgid, &ap, &richloc, DK_NOTE); + if (context->inhibit_notes_p) + { + va_end (ap); + return; + } + saved_prefix = pp_get_prefix (context->printer); + pp_set_prefix (context->printer, + diagnostic_build_prefix (context, &diagnostic)); + pp_newline (context->printer); + pp_format (context->printer, &diagnostic.message); + pp_output_formatted_text (context->printer); + pp_destroy_prefix (context->printer); + pp_set_prefix (context->printer, saved_prefix); + diagnostic_show_locus (context, &diagnostic); + va_end (ap); +} + +/* Same as diagnostic_append_note, but at RICHLOC. */ + +void +diagnostic_append_note_at_rich_loc (diagnostic_context *context, + rich_location *richloc, + const char * gmsgid, ...) +{ + diagnostic_info diagnostic; + va_list ap; + const char *saved_prefix; + + va_start (ap, gmsgid); + diagnostic_set_info (&diagnostic, gmsgid, &ap, richloc, DK_NOTE); if (context->inhibit_notes_p) { va_end (ap); @@ -880,16 +925,17 @@ emit_diagnostic (diagnostic_t kind, location_t location, int opt, diagnostic_info diagnostic; va_list ap; bool ret; + rich_location richloc (location); va_start (ap, gmsgid); if (kind == DK_PERMERROR) { - diagnostic_set_info (&diagnostic, gmsgid, &ap, location, + diagnostic_set_info (&diagnostic, gmsgid, &ap, &richloc, permissive_error_kind (global_dc)); diagnostic.option_index = permissive_error_option (global_dc); } else { - diagnostic_set_info (&diagnostic, gmsgid, &ap, location, kind); + diagnostic_set_info (&diagnostic, gmsgid, &ap, &richloc, kind); if (kind == DK_WARNING || kind == DK_PEDWARN) diagnostic.option_index = opt; } @@ -906,9 +952,23 @@ inform (location_t location, const char *gmsgid, ...) { diagnostic_info diagnostic; va_list ap; + rich_location richloc (location); + + va_start (ap, gmsgid); + diagnostic_set_info (&diagnostic, gmsgid, &ap, &richloc, DK_NOTE); + report_diagnostic (&diagnostic); + va_end (ap); +} + +/* Same as "inform", but at RICHLOC. */ +void +inform_at_rich_loc (rich_location *richloc, const char *gmsgid, ...) +{ + diagnostic_info diagnostic; + va_list ap; va_start (ap, gmsgid); - diagnostic_set_info (&diagnostic, gmsgid, &ap, location, DK_NOTE); + diagnostic_set_info (&diagnostic, gmsgid, &ap, richloc, DK_NOTE); report_diagnostic (&diagnostic); va_end (ap); } @@ -921,11 +981,12 @@ inform_n (location_t location, int n, const char *singular_gmsgid, { diagnostic_info diagnostic; va_list ap; + rich_location richloc (location); va_start (ap, plural_gmsgid); diagnostic_set_info_translated (&diagnostic, ngettext (singular_gmsgid, plural_gmsgid, n), - &ap, location, DK_NOTE); + &ap, &richloc, DK_NOTE); report_diagnostic (&diagnostic); va_end (ap); } @@ -939,9 +1000,10 @@ warning (int opt, const char *gmsgid, ...) diagnostic_info diagnostic; va_list ap; bool ret; + rich_location richloc (input_location); va_start (ap, gmsgid); - diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_WARNING); + diagnostic_set_info (&diagnostic, gmsgid, &ap, &richloc, DK_WARNING); diagnostic.option_index = opt; ret = report_diagnostic (&diagnostic); @@ -959,9 +1021,27 @@ warning_at (location_t location, int opt, const char *gmsgid, ...) diagnostic_info diagnostic; va_list ap; bool ret; + rich_location richloc (location); + + va_start (ap, gmsgid); + diagnostic_set_info (&diagnostic, gmsgid, &ap, &richloc, DK_WARNING); + diagnostic.option_index = opt; + ret = report_diagnostic (&diagnostic); + va_end (ap); + return ret; +} + +/* Same as warning at, but using RICHLOC. */ + +bool +warning_at_rich_loc (rich_location *richloc, int opt, const char *gmsgid, ...) +{ + diagnostic_info diagnostic; + va_list ap; + bool ret; va_start (ap, gmsgid); - diagnostic_set_info (&diagnostic, gmsgid, &ap, location, DK_WARNING); + diagnostic_set_info (&diagnostic, gmsgid, &ap, richloc, DK_WARNING); diagnostic.option_index = opt; ret = report_diagnostic (&diagnostic); va_end (ap); @@ -979,11 +1059,13 @@ warning_n (location_t location, int opt, int n, const char *singular_gmsgid, diagnostic_info diagnostic; va_list ap; bool ret; + rich_location richloc (location); va_start (ap, plural_gmsgid); diagnostic_set_info_translated (&diagnostic, ngettext (singular_gmsgid, plural_gmsgid, n), - &ap, location, DK_WARNING); + &ap, &richloc, DK_WARNING +); diagnostic.option_index = opt; ret = report_diagnostic (&diagnostic); va_end (ap); @@ -1009,9 +1091,10 @@ pedwarn (location_t location, int opt, const char *gmsgid, ...) diagnostic_info diagnostic; va_list ap; bool ret; + rich_location richloc (location); va_start (ap, gmsgid); - diagnostic_set_info (&diagnostic, gmsgid, &ap, location, DK_PEDWARN); + diagnostic_set_info (&diagnostic, gmsgid, &ap, &richloc, DK_PEDWARN); diagnostic.option_index = opt; ret = report_diagnostic (&diagnostic); va_end (ap); @@ -1031,9 +1114,28 @@ permerror (location_t location, const char *gmsgid, ...) diagnostic_info diagnostic; va_list ap; bool ret; + rich_location richloc (location); va_start (ap, gmsgid); - diagnostic_set_info (&diagnostic, gmsgid, &ap, location, + diagnostic_set_info (&diagnostic, gmsgid, &ap, &richloc, + permissive_error_kind (global_dc)); + diagnostic.option_index = permissive_error_option (global_dc); + ret = report_diagnostic (&diagnostic); + va_end (ap); + return ret; +} + +/* Same as "permerror", but at RICHLOC. */ + +bool +permerror_at_rich_loc (rich_location *richloc, const char *gmsgid, ...) +{ + diagnostic_info diagnostic; + va_list ap; + bool ret; + + va_start (ap, gmsgid); + diagnostic_set_info (&diagnostic, gmsgid, &ap, richloc, permissive_error_kind (global_dc)); diagnostic.option_index = permissive_error_option (global_dc); ret = report_diagnostic (&diagnostic); @@ -1048,9 +1150,10 @@ error (const char *gmsgid, ...) { diagnostic_info diagnostic; va_list ap; + rich_location richloc (input_location); va_start (ap, gmsgid); - diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_ERROR); + diagnostic_set_info (&diagnostic, gmsgid, &ap, &richloc, DK_ERROR); report_diagnostic (&diagnostic); va_end (ap); } @@ -1063,11 +1166,12 @@ error_n (location_t location, int n, const char *singular_gmsgid, { diagnostic_info diagnostic; va_list ap; + rich_location richloc (location); va_start (ap, plural_gmsgid); diagnostic_set_info_translated (&diagnostic, ngettext (singular_gmsgid, plural_gmsgid, n), - &ap, location, DK_ERROR); + &ap, &richloc, DK_ERROR); report_diagnostic (&diagnostic); va_end (ap); } @@ -1078,9 +1182,25 @@ error_at (location_t loc, const char *gmsgid, ...) { diagnostic_info diagnostic; va_list ap; + rich_location richloc (loc); va_start (ap, gmsgid); - diagnostic_set_info (&diagnostic, gmsgid, &ap, loc, DK_ERROR); + diagnostic_set_info (&diagnostic, gmsgid, &ap, &richloc, DK_ERROR); + report_diagnostic (&diagnostic); + va_end (ap); +} + +/* Same as above, but use RICH_LOC. */ + +void +error_at_rich_loc (rich_location *rich_loc, const char *gmsgid, ...) +{ + diagnostic_info diagnostic; + va_list ap; + + va_start (ap, gmsgid); + diagnostic_set_info (&diagnostic, gmsgid, &ap, rich_loc, + DK_ERROR); report_diagnostic (&diagnostic); va_end (ap); } @@ -1093,9 +1213,10 @@ sorry (const char *gmsgid, ...) { diagnostic_info diagnostic; va_list ap; + rich_location richloc (input_location); va_start (ap, gmsgid); - diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_SORRY); + diagnostic_set_info (&diagnostic, gmsgid, &ap, &richloc, DK_SORRY); report_diagnostic (&diagnostic); va_end (ap); } @@ -1116,9 +1237,10 @@ fatal_error (location_t loc, const char *gmsgid, ...) { diagnostic_info diagnostic; va_list ap; + rich_location richloc (loc); va_start (ap, gmsgid); - diagnostic_set_info (&diagnostic, gmsgid, &ap, loc, DK_FATAL); + diagnostic_set_info (&diagnostic, gmsgid, &ap, &richloc, DK_FATAL); report_diagnostic (&diagnostic); va_end (ap); @@ -1134,9 +1256,10 @@ internal_error (const char *gmsgid, ...) { diagnostic_info diagnostic; va_list ap; + rich_location richloc (input_location); va_start (ap, gmsgid); - diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_ICE); + diagnostic_set_info (&diagnostic, gmsgid, &ap, &richloc, DK_ICE); report_diagnostic (&diagnostic); va_end (ap); @@ -1151,9 +1274,10 @@ internal_error_no_backtrace (const char *gmsgid, ...) { diagnostic_info diagnostic; va_list ap; + rich_location richloc (input_location); va_start (ap, gmsgid); - diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_ICE_NOBT); + diagnostic_set_info (&diagnostic, gmsgid, &ap, &richloc, DK_ICE_NOBT); report_diagnostic (&diagnostic); va_end (ap); @@ -1217,3 +1341,17 @@ real_abort (void) { abort (); } + +/* Display the given source_range instance, with MSG as a descriptive + comment. This issues a "note" diagnostic at the range. + + This is declared within libcpp, but implemented here, since it + makes use of the diagnostic-printing machinery. */ + +DEBUG_FUNCTION void +source_range::debug (const char *msg) const +{ + rich_location richloc (m_start); + richloc.add_range (m_start, m_finish, false); + inform_at_rich_loc (&richloc, "%s", msg); +} |