https://gcc.gnu.org/bugzilla/show_bug.cgi?id=116613
--- Comment #10 from GCC Commits <cvs-commit at gcc dot gnu.org> --- The master branch has been updated by David Malcolm <dmalc...@gcc.gnu.org>: https://gcc.gnu.org/g:be02253af810348be689cfa6f235af96c8cc5802 commit r15-3976-gbe02253af810348be689cfa6f235af96c8cc5802 Author: David Malcolm <dmalc...@redhat.com> Date: Mon Sep 30 11:48:29 2024 -0400 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 <dmalc...@redhat.com>