diff options
author | David Malcolm <dmalcolm@redhat.com> | 2024-09-30 11:48:29 -0400 |
---|---|---|
committer | David Malcolm <dmalcolm@redhat.com> | 2024-09-30 11:48:29 -0400 |
commit | be02253af810348be689cfa6f235af96c8cc5802 (patch) | |
tree | d4126b815e7d07d288026eb12e0d6433c6e070ab /gcc/fortran/error.cc | |
parent | cce52867d1892c08386f780107288473ec0033b7 (diff) | |
download | gcc-be02253af810348be689cfa6f235af96c8cc5802.zip gcc-be02253af810348be689cfa6f235af96c8cc5802.tar.gz gcc-be02253af810348be689cfa6f235af96c8cc5802.tar.bz2 |
diagnostics: isolate diagnostic_context with interface classes [PR116613]
As work towards supporting multiple diagnostic outputs (where each
output has its own pretty_printer), avoid passing around
diagnostic_context to the various printing routines, so that we
can be more explicit about which pretty_printer is in use.
Introduce a set of "policy" classes that capture the parts of
diagnostic_context that are needed, and use these rather than
diagnostic_context *. Pass around pretty_printer & rather than
taking value from context. Split out the pretty_printer-using code
from class layout into a new class layout_printer, separating the
responsibilities of determining layout when quoting source versus
actually printing the source.
No functional change intended.
gcc/analyzer/ChangeLog:
PR other/116613
* program-point.cc (function_point::print_source_line): Replace
call to diagnostic_show_locus with a call to
diagnostic_source_print_policy::print.
gcc/ChangeLog:
PR other/116613
* diagnostic-format-json.cc (json_from_expanded_location): Replace
call to diagnostic_context::converted_column with call to
diagnostic_column_policy::converted_column.
* diagnostic-format-sarif.cc
(sarif_builder::make_location_object): Replace call to
diagnostic_show_locus with call to
diagnostic_source_print_policy::print.
* diagnostic-format-text.cc (get_location_text): Replace call to
diagnostic_context::get_location_text with call to
diagnostic_column_policy::get_location_text.
(diagnostic_text_output_format::report_current_module): Replace call
to diagnostic_context::converted_column with call to
diagnostic_column_policy::converted_column.
* diagnostic-format-text.h
(diagnostic_text_output_format::diagnostic_output_format):
Initialize m_column_policy.
(diagnostic_text_output_format::get_column_policy): New.
(diagnostic_text_output_format::m_column_policy): New.
* diagnostic-path.cc (class path_print_policy): New.
(event_range::maybe_add_event): Replace diagnostic_context param
with path_print_policy.
(event_range::print): Convert "pp" from * to &. Convert first
param of start_span callback from diagnostic_context to
diagnostic_location_print_policy.
(path_summary::path_summary): Convert first param from
diagnostic_text_output_format to path_print_policy. Add
colorize param. Update for changes to
event_range::maybe_add_event.
(thread_event_printer::print_swimlane_for_event_range): Assert
that pp is non-null. Update for change to event_range::print.
(diagnostic_text_output_format::print_path): Pass
path_print_policy to path_summary's ctor.
(selftest::test_empty_path): Likewise.
(selftest::test_intraprocedural_path): Likewise.
(selftest::test_interprocedural_path_1): Likewise.
(selftest::test_interprocedural_path_2): Likewise.
(selftest::test_recursion): Likewise.
(selftest::test_control_flow_1): Likewise.
(selftest::test_control_flow_2): Likewise.
(selftest::test_control_flow_3): Likewise.
(selftest::assert_cfg_edge_path_streq): Likewise.
(selftest::test_control_flow_5): Likewise.
(selftest::test_control_flow_6): Likewise.
* diagnostic-show-locus.cc (colorizer::set_range): Update for
change to m_pp.
(colorizer::m_pp): Convert from * to &.
(class layout): Add friend class layout_printer and move various
decls to it.
(layout::m_pp): Drop field.
(layout::m_policy): Rename to...
(layout::m_char_policy): ...this.
(layout::m_colorizer): Move field to class layout_printer.
(layout::m_diagnostic_path_p): Drop field.
(class layout_printer): New class, by refactoring class layout.
(colorizer::colorizer): Convert "pp" param from * to &.
(colorizer::set_named_color): Update for above change.
(colorizer::begin_state): Likewise.
(colorizer::finish_state): Likewise.
(make_policy): Rename to...
(make_char_policy): ...this, and update param from
diagnostic_context to diagnostic_source_print_policy.
(layout::layout): Update param from diagnostic_context to
diagnostic_source_print_policy. Drop params "diagnostic_kind" and
"pp", moving these and other material to class layout_printer.
(layout::maybe_add_location_range): Update for renamed field.
(layout::print_gap_in_line_numbering): Convert to...
(layout_printer::print_gap_in_line_numbering): ...this.
(layout::calculate_x_offset_display): Update for renamed field.
(layout::print_source_line): Convert to...
(layout_printer::print_source_line): ...this.
(layout::print_leftmost_column): Convert to...
(layout_printer::print_leftmost_column): ...this.
(layout::start_annotation_line): Convert to...
(layout_printer::start_annotation_line): ...this.
(layout::print_annotation_line): Convert to...
(layout_printer::print_annotation_line): ...this.
(layout::print_any_labels): Convert to...
(layout_printer::print_any_labels): ...this.
(layout::print_leading_fixits): Convert to...
(layout_printer::print_leading_fixits): ...this.
(layout::print_trailing_fixits): Convert to...
(layout_printer::print_trailing_fixits): ...this.
(layout::print_newline): Convert to...
(layout_printer::print_newline): ...this.
(layout::get_state_at_point): Make const.
(layout::get_x_bound_for_row): Make const.
(layout::move_to_column): Convert to...
(layout_printer::move_to_column): ...this.
(layout::show_ruler): Convert to...
(layout_printer::show_ruler): ...this.
(layout::print_line): Convert to...
(layout_printer::print_line): ...this.
(layout::print_any_right_to_left_edge_lines): Convert to...
(layout_printer::print_any_right_to_left_edge_lines): ...this.
(layout::print_any_right_to_left_edge_lines): Likewise.
(layout_printer::layout_printer): New.
(layout::update_any_effects): Delete, moving logic to
layout_printer::print.
(gcc_rich_location::add_location_if_nearby): Update param from
diagnostic_context to diagnostic_source_print_policy. Add
overload taking a diagnostic_context.
(diagnostic_context::maybe_show_locus): Move handling of null
pretty_printer here, from layout ctor. Convert call to
diagnostic_context::show_locus to
diagnostic_source_print_policy::print.
(diagnostic_source_print_policy::diagnostic_source_print_policy):
New.
(diagnostic_context::show_locus): Convert to...
(diagnostic_source_print_policy::print): ...this. Convert pp
from * to &.
(layout_printer::print): New, based on material in
diagnostic_context::show_locus.
(selftest::make_char_policy): New.
(selftest::test_display_widths): Update for above changes.
(selftest::test_offset_impl): Likewise.
(selftest::test_layout_x_offset_display_utf8): Likewise.
(selftest::test_layout_x_offset_display_tab): Likewise.
(selftest::test_diagnostic_show_locus_unknown_location): Use
test_diagnostic_context::test_show_locus rather than
diagnostic_show_locus.
(selftest::test_one_liner_no_column): Likewise.
(selftest::test_one_liner_caret_and_range): Likewise.
(selftest::test_one_liner_multiple_carets_and_ranges): Likewise.
(selftest::test_one_liner_fixit_insert_before): Likewise.
(selftest::test_one_liner_fixit_insert_after): Likewise.
(selftest::test_one_liner_fixit_remove): Likewise.
(selftest::test_one_liner_fixit_replace): Likewise.
(selftest::test_one_liner_fixit_replace_non_equal_range):
Likewise.
(selftest::test_one_liner_fixit_replace_equal_secondary_range):
Likewise.
(selftest::test_one_liner_fixit_validation_adhoc_locations):
Likewise.
(selftest::test_one_liner_many_fixits_1): Likewise.
(selftest::test_one_liner_many_fixits_2): Likewise.
(selftest::test_one_liner_labels): Likewise.
(selftest::test_one_liner_simple_caret_utf8): Likewise.
(selftest::test_one_liner_caret_and_range_utf8): Likewise.
(selftest::test_one_liner_multiple_carets_and_ranges_utf8):
Likewise.
(selftest::test_one_liner_fixit_insert_before_utf8): Likewise.
(selftest::test_one_liner_fixit_insert_after_utf8): Likewise.
(selftest::test_one_liner_fixit_remove_utf8): Likewise.
(selftest::test_one_liner_fixit_replace_utf8): Likewise.
(selftest::test_one_liner_fixit_replace_non_equal_range_utf8):
Likewise.
(selftest::test_one_liner_fixit_replace_equal_secondary_range_utf8):
Likewise.
(selftest::test_one_liner_fixit_validation_adhoc_locations_utf8):
Likewise.
(selftest::test_one_liner_many_fixits_1_utf8): Likewise.
(selftest::test_one_liner_many_fixits_2_utf8): Likewise.
(selftest::test_one_liner_labels_utf8): Likewise.
(selftest::test_one_liner_colorized_utf8): Likewise.
(selftest::test_add_location_if_nearby): Likewise.
(selftest::test_diagnostic_show_locus_fixit_lines): Likewise.
(selftest::test_overlapped_fixit_printing): Likewise.
(selftest::test_overlapped_fixit_printing_utf8): Likewise.
(selftest::test_overlapped_fixit_printing_2): Likewise.
(selftest::test_fixit_insert_containing_newline): Likewise.
(selftest::test_fixit_insert_containing_newline_2): Likewise.
(selftest::test_fixit_replace_containing_newline): Likewise.
(selftest::test_fixit_deletion_affecting_newline): Likewise.
(selftest::test_tab_expansion): Likewise.
(selftest::test_escaping_bytes_1): Likewise.
(selftest::test_escaping_bytes_2): Likewise.
(selftest::test_line_numbers_multiline_range): Likewise.
* diagnostic.cc
(diagnostic_column_policy::diagnostic_column_policy): New.
(diagnostic_context::converted_column): Convert to...
(diagnostic_column_policy::converted_column): ...this.
(diagnostic_context::get_location_text): Convert to...
(diagnostic_column_policy::get_location_text): ...this, adding
"show_column" param.
(diagnostic_location_print_policy::diagnostic_location_print_policy):
New ctors.
(default_diagnostic_start_span_fn): Convert param from
diagnostic_context * to const diagnostic_location_print_policy &.
Add "pp" param.
(selftest::assert_location_text): Update for above changes.
(selftest::test_diagnostic_get_location_text): Rename to...
(selftest::test_get_location_text): ...this.
(selftest::c_diagnostic_cc_tests): Update for renaming.
* diagnostic.h (class diagnostic_location_print_policy): New
forward decl.
(class diagnostic_source_print_policy): New forward decl.
(diagnostic_start_span_fn): Convert first param from
diagnostic_context * to const diagnostic_location_print_policy &
and add pretty_printer * param.
(class diagnostic_column_policy): New.
(class diagnostic_location_print_policy): New.
(class diagnostic_source_print_policy): New.
(class diagnostic_context): Add friend class
diagnostic_source_print_policy.
(diagnostic_context::converted_column): Drop decl in favor of
diagnostic_column_policy::converted_column.
(diagnostic_context::get_location_text): Drop decl in favor of
diagnostic_column_policy::get_location_text.
(diagnostic_context::show_locus): Drop decl in favor of
diagnostic_source_print_policy::print.
(default_diagnostic_start_span_fn): Update for change to
diagnostic_start_span_fn.
* gcc-rich-location.h (class diagnostic_source_print_policy): New
forward decl.
(gcc_rich_location::add_location_if_nearby): Convert first param
from diagnostic_context to diagnostic_source_print_policy. Add
overload taking diagnostic_context.
* selftest-diagnostic.cc
(selftest::test_diagnostic_context::test_diagnostic_context): Turn
off colorization.
(selftest::test_diagnostic_context::start_span_cb): Update for
change to callback type.
(test_diagnostic_context::test_show_locus): New.
* selftest-diagnostic.h
(selftest::test_diagnostic_context::start_span_cb): Update for
change to callback type.
(test_diagnostic_context::test_show_locus): New decl.
gcc/fortran/ChangeLog:
PR other/116613
* error.cc (gfc_diagnostic_build_locus_prefix): Convert first
param from diagnostic_context * to
const diagnostic_location_print_policy &. Add colorize param.
Likewise for the "two expanded_locations" overload.
(gfc_diagnostic_text_starter): Update for above changes.
(gfc_diagnostic_start_span): Update for change to callback type.
gcc/testsuite/ChangeLog:
PR other/116613
* gcc.dg/plugin/diagnostic_group_plugin.c
(test_diagnostic_start_span_fn): Update for change to callback
type.
Signed-off-by: David Malcolm <dmalcolm@redhat.com>
Diffstat (limited to 'gcc/fortran/error.cc')
-rw-r--r-- | gcc/fortran/error.cc | 41 |
1 files changed, 22 insertions, 19 deletions
diff --git a/gcc/fortran/error.cc b/gcc/fortran/error.cc index cad0e69..2c29537 100644 --- a/gcc/fortran/error.cc +++ b/gcc/fortran/error.cc @@ -1211,17 +1211,17 @@ gfc_diagnostic_build_kind_prefix (diagnostic_context *context, /* Return a malloc'd string describing a location. The caller is responsible for freeing the memory. */ static char * -gfc_diagnostic_build_locus_prefix (diagnostic_context *context, - expanded_location s) +gfc_diagnostic_build_locus_prefix (const diagnostic_location_print_policy &loc_policy, + expanded_location s, + bool colorize) { - pretty_printer *const pp = context->m_printer; - const char *locus_cs = colorize_start (pp_show_color (pp), "locus"); - const char *locus_ce = colorize_stop (pp_show_color (pp)); + const char *locus_cs = colorize_start (colorize, "locus"); + const char *locus_ce = colorize_stop (colorize); return (s.file == NULL ? build_message_string ("%s%s:%s", locus_cs, progname, locus_ce ) : !strcmp (s.file, special_fname_builtin ()) ? build_message_string ("%s%s:%s", locus_cs, s.file, locus_ce) - : context->m_show_column + : loc_policy.show_column_p () ? build_message_string ("%s%s:%d:%d:%s", locus_cs, s.file, s.line, s.column, locus_ce) : build_message_string ("%s%s:%d:%s", locus_cs, s.file, s.line, locus_ce)); @@ -1230,18 +1230,18 @@ gfc_diagnostic_build_locus_prefix (diagnostic_context *context, /* Return a malloc'd string describing two locations. The caller is responsible for freeing the memory. */ static char * -gfc_diagnostic_build_locus_prefix (diagnostic_context *context, - expanded_location s, expanded_location s2) +gfc_diagnostic_build_locus_prefix (const diagnostic_location_print_policy &loc_policy, + expanded_location s, expanded_location s2, + bool colorize) { - pretty_printer *const pp = context->m_printer; - const char *locus_cs = colorize_start (pp_show_color (pp), "locus"); - const char *locus_ce = colorize_stop (pp_show_color (pp)); + const char *locus_cs = colorize_start (colorize, "locus"); + const char *locus_ce = colorize_stop (colorize); return (s.file == NULL ? build_message_string ("%s%s:%s", locus_cs, progname, locus_ce ) : !strcmp (s.file, special_fname_builtin ()) ? build_message_string ("%s%s:%s", locus_cs, s.file, locus_ce) - : context->m_show_column + : loc_policy.show_column_p () ? build_message_string ("%s%s:%d:%d-%d:%s", locus_cs, s.file, s.line, MIN (s.column, s2.column), MAX (s.column, s2.column), locus_ce) @@ -1285,9 +1285,11 @@ gfc_diagnostic_text_starter (diagnostic_text_output_format &text_output, same_locus = diagnostic_same_line (context, s1, s2); } + diagnostic_location_print_policy loc_policy (text_output); + const bool colorize = pp_show_color (pp); char * locus_prefix = (one_locus || !same_locus) - ? gfc_diagnostic_build_locus_prefix (context, s1) - : gfc_diagnostic_build_locus_prefix (context, s1, s2); + ? gfc_diagnostic_build_locus_prefix (loc_policy, s1, colorize) + : gfc_diagnostic_build_locus_prefix (loc_policy, s1, s2, colorize); if (!context->m_source_printing.enabled || diagnostic_location (diagnostic, 0) <= BUILTINS_LOCATION @@ -1309,7 +1311,7 @@ gfc_diagnostic_text_starter (diagnostic_text_output_format &text_output, and we flush with a new line before setting the new prefix. */ pp_string (pp, "(1)"); pp_newline (pp); - locus_prefix = gfc_diagnostic_build_locus_prefix (context, s2); + locus_prefix = gfc_diagnostic_build_locus_prefix (loc_policy, s2, colorize); pp_set_prefix (pp, concat (locus_prefix, " ", kind_prefix, NULL)); free (kind_prefix); @@ -1332,12 +1334,13 @@ gfc_diagnostic_text_starter (diagnostic_text_output_format &text_output, } static void -gfc_diagnostic_start_span (diagnostic_context *context, +gfc_diagnostic_start_span (const diagnostic_location_print_policy &loc_policy, + pretty_printer *pp, expanded_location exploc) { - char *locus_prefix; - locus_prefix = gfc_diagnostic_build_locus_prefix (context, exploc); - pretty_printer * const pp = context->m_printer; + const bool colorize = pp_show_color (pp); + char *locus_prefix + = gfc_diagnostic_build_locus_prefix (loc_policy, exploc, colorize); pp_verbatim (pp, "%s", locus_prefix); free (locus_prefix); pp_newline (pp); |