https://gcc.gnu.org/bugzilla/show_bug.cgi?id=116613
--- Comment #8 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:3d3d20ccd8365970f34002bfe0a632f2868bc95b commit r15-3974-g3d3d20ccd8365970f34002bfe0a632f2868bc95b Author: David Malcolm <dmalc...@redhat.com> Date: Mon Sep 30 11:48:29 2024 -0400 diagnostics: use "%e" to avoid intermediate strings [PR116613] Various diagnostics build an intermediate string, potentially with colorization, and then use this in a diagnostic message. This won't work if we have multiple diagnostic sinks, where some might be colorized and some not. This patch reworks such places using "%e" and pp_element subclasses, so that any colorization happens within report_diagnostic's call to pp_format. gcc/analyzer/ChangeLog: PR other/116613 * kf-analyzer.cc: Include "pretty-print-markup.h". (kf_analyzer_dump_escaped::impl_call_pre): Defer colorization choices by eliminating the construction of a intermediate string, replacing it with a new pp_element subclass via "%e". gcc/ChangeLog: PR other/116613 * attribs.cc: Include "pretty-print-markup.h". (decls_mismatched_attributes): Defer colorization choices by replacing printing to a pretty_printer * param with appending to a vec of strings. (maybe_diag_alias_attributes): As above, replacing pretty_printer with usage of pp_markup::comma_separated_quoted_strings and "%e" in two places. * attribs.h (decls_mismatched_attributes): Update decl. * gimple-ssa-warn-access.cc: Include "pretty-print-markup.h". (pass_waccess::maybe_warn_memmodel): Defer colorization choices by replacing printing to a pretty_printer * param with use of pp_markup::comma_separated_quoted_strings and "%e". (pass_waccess::maybe_warn_memmodel): Likewise, replacing printing to a temporary buffer. * pretty-print-markup.h (class pp_markup::comma_separated_quoted_strings): New. * pretty-print.cc (pp_markup::comma_separated_quoted_strings::add_to_phase_2): New. (selftest::test_pp_printf_within_pp_element): New. (selftest::test_comma_separated_quoted_strings): New. (selftest::pretty_print_cc_tests): Call the new tests. gcc/cp/ChangeLog: PR other/116613 * pt.cc: Include "pretty-print-markup.h". (warn_spec_missing_attributes): Defer colorization choices by replacing printing to a pretty_printer * param with appending to a vec of strings. Replace pretty_printer with usage of pp_markup::comma_separated_quoted_strings and "%e". gcc/testsuite/ChangeLog: PR other/116613 * c-c++-common/analyzer/escaping-1.c: Update expected results to remove type information from C++ results. Previously we were using %qD with default_tree_printer, which used lang_hooks.decl_printable_name, whereas now we're using %qD with a clone of the cxx_pretty_printer. Signed-off-by: David Malcolm <dmalc...@redhat.com> --- Comment #9 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:cce52867d1892c08386f780107288473ec0033b7 commit r15-3975-gcce52867d1892c08386f780107288473ec0033b7 Author: David Malcolm <dmalc...@redhat.com> Date: Mon Sep 30 11:48:29 2024 -0400 diagnostics: avoid using diagnostic_context's m_printer [PR116613] As work towards supporting multiple diagnostic outputs (where each output has its own pretty_printer), avoid using diagnostic_context's m_printer field. Instead, use the output format's printer. Currently this *is* the dc's printer, but eventually it might not be. No functional change intended. gcc/ChangeLog: PR other/116613 * diagnostic-format-json.cc (diagnostic_output_format_init_json): Pass in the format. Use the format's printer when disabling colorization. Move the call to set_output_format into here. (diagnostic_output_format_init_json_stderr): Update for above change. (diagnostic_output_format_init_json_file): Likewise. * diagnostic-format-sarif.cc (diagnostic_output_format_init_sarif): Use the format's printer when disabling colorization. * diagnostic-path.cc (selftest::test_empty_path): Use the text_output's printer. (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. gcc/testsuite/ChangeLog: PR other/116613 * gcc.dg/plugin/diagnostic_group_plugin.c (test_output_format::on_begin_group): Use get_printer () rather than accessing m_context.m_printer. (test_output_format::on_end_group): Likewise. * gcc.dg/plugin/diagnostic_plugin_xhtml_format.c (xhtml_builder::m_printer): New field. (xhtml_builder::xhtml_builder): Add "pp" param and use it to initialize m_printer. (xhtml_builder::on_report_diagnostic): Drop "context" param. (xhtml_builder::make_element_for_diagnostic): Likewise. Use this->m_printer rather than the context's m_printer. Pass m_printer to call to diagnostic_show_locus. (xhtml_builder::emit_diagram): Drop "context" param. (xhtml_output_format::on_report_diagnostic): Drop context param from call to m_builder. (xhtml_output_format::on_diagram): Likewise. (xhtml_output_format::xhtml_output_format): Pass result of get_printer as printer for builder. (diagnostic_output_format_init_xhtml): Use the fmt's printer rather than the context's. Signed-off-by: David Malcolm <dmalc...@redhat.com>