diff options
author | David Malcolm <dmalcolm@redhat.com> | 2023-10-02 12:16:55 -0400 |
---|---|---|
committer | David Malcolm <dmalcolm@redhat.com> | 2023-10-02 12:16:55 -0400 |
commit | 140820265d96b0d115ade21235f8b8017fde9a5a (patch) | |
tree | 45e50166cafdee536b20ea2dec572a501554da26 /gcc/testsuite/gcc.dg/plugin | |
parent | c5c565eff6277ac176d6c5c94f55859d0eb28938 (diff) | |
download | gcc-140820265d96b0d115ade21235f8b8017fde9a5a.zip gcc-140820265d96b0d115ade21235f8b8017fde9a5a.tar.gz gcc-140820265d96b0d115ade21235f8b8017fde9a5a.tar.bz2 |
diagnostics: add diagnostic_output_format class
Eliminate various global variables in the json/sarif output code by
bundling together callbacks and state into a new diagnostic_output_format
class, with per-output-format subclasses.
No functional change intended.
gcc/ChangeLog:
* diagnostic-format-json.cc (toplevel_array): Remove global in
favor of json_output_format::m_top_level_array.
(cur_group): Likewise, for json_output_format::m_cur_group.
(cur_children_array): Likewise, for
json_output_format::m_cur_children_array.
(class json_output_format): New.
(json_begin_diagnostic): Remove, in favor of
json_output_format::on_begin_diagnostic.
(json_end_diagnostic): Convert to...
(json_output_format::on_end_diagnostic): ...this.
(json_begin_group): Remove, in favor of
json_output_format::on_begin_group.
(json_end_group): Remove, in favor of
json_output_format::on_end_group.
(json_flush_to_file): Remove, in favor of
json_output_format::flush_to_file.
(json_stderr_final_cb): Remove, in favor of json_output_format
dtor.
(json_output_base_file_name): Remove global.
(class json_stderr_output_format): New.
(json_file_final_cb): Remove.
(class json_file_output_format): New.
(json_emit_diagram): Remove.
(diagnostic_output_format_init_json): Update.
(diagnostic_output_format_init_json_file): Update.
* diagnostic-format-sarif.cc (the_builder): Remove this global,
moving to a field of the sarif_output_format.
(sarif_builder::maybe_make_artifact_content_object): Use the
context's m_file_cache.
(get_source_lines): Convert to...
(sarif_builder::get_source_lines): ...this, using context's
m_file_cache.
(sarif_begin_diagnostic): Remove, in favor of
sarif_output_format::on_begin_diagnostic.
(sarif_end_diagnostic): Remove, in favor of
sarif_output_format::on_end_diagnostic.
(sarif_begin_group): Remove, in favor of
sarif_output_format::on_begin_group.
(sarif_end_group): Remove, in favor of
sarif_output_format::on_end_group.
(sarif_flush_to_file): Delete.
(sarif_stderr_final_cb): Delete.
(sarif_output_base_file_name): Delete.
(sarif_file_final_cb): Delete.
(class sarif_output_format): New.
(sarif_emit_diagram): Delete.
(class sarif_stream_output_format): New.
(class sarif_file_output_format): New.
(diagnostic_output_format_init_sarif): Update.
(diagnostic_output_format_init_sarif_stderr): Update.
(diagnostic_output_format_init_sarif_file): Update.
(diagnostic_output_format_init_sarif_stream): Update.
* diagnostic-show-locus.cc (diagnostic_show_locus): Update.
* diagnostic.cc (default_diagnostic_final_cb): Delete, moving to
diagnostic_text_output_format's dtor.
(diagnostic_initialize): Update, making a new instance of
diagnostic_text_output_format.
(diagnostic_finish): Delete m_output_format, rather than calling
final_cb.
(diagnostic_report_diagnostic): Assert that m_output_format is
non-NULL. Replace call to begin_group_cb with call to
m_output_format->on_begin_group. Replace call to
diagnostic_starter with call to
m_output_format->on_begin_diagnostic. Replace call to
diagnostic_finalizer with call to
m_output_format->on_end_diagnostic.
(diagnostic_emit_diagram): Replace both optional call to
m_diagrams.m_emission_cb and default implementation with call to
m_output_format->on_diagram. Move default implementation to
diagnostic_text_output_format::on_diagram.
(auto_diagnostic_group::~auto_diagnostic_group): Replace call to
end_group_cb with call to m_output_format->on_end_group.
(diagnostic_text_output_format::~diagnostic_text_output_format):
New, based on default_diagnostic_final_cb.
(diagnostic_text_output_format::on_begin_diagnostic): New, based
on code from diagnostic_report_diagnostic.
(diagnostic_text_output_format::on_end_diagnostic): Likewise.
(diagnostic_text_output_format::on_diagram): New, based on code
from diagnostic_emit_diagram.
* diagnostic.h (class diagnostic_output_format): New.
(class diagnostic_text_output_format): New.
(diagnostic_context::begin_diagnostic): Move to...
(diagnostic_context::m_text_callbacks::begin_diagnostic): ...here.
(diagnostic_context::start_span): Move to...
(diagnostic_context::m_text_callbacks::start_span): ...here.
(diagnostic_context::end_diagnostic): Move to...
(diagnostic_context::m_text_callbacks::end_diagnostic): ...here.
(diagnostic_context::begin_group_cb): Remove, in favor of
m_output_format->on_begin_group.
(diagnostic_context::end_group_cb): Remove, in favor of
m_output_format->on_end_group.
(diagnostic_context::final_cb): Remove, in favor of
m_output_format's dtor.
(diagnostic_context::m_output_format): New field.
(diagnostic_context::m_diagrams.m_emission_cb): Remove, in favor
of m_output_format->on_diagram.
(diagnostic_starter): Update.
(diagnostic_finalizer): Update.
(diagnostic_output_format_init_sarif_stream): New.
* input.cc (location_get_source_line): Move implementation apart from
call to diagnostic_file_cache_init to...
(file_cache::get_source_line): ...this new function...
(location_get_source_line): ...and reintroduce, rewritten in terms of
file_cache::get_source_line.
(get_source_file_content): Likewise, refactor into...
(file_cache::get_source_file_content): ...this new function.
* input.h (file_cache::get_source_line): New decl.
(file_cache::get_source_file_content): New decl.
* selftest-diagnostic.cc
(test_diagnostic_context::test_diagnostic_context): Update.
* tree-diagnostic-path.cc (event_range::print): Update for
change to diagnostic_context's start_span callback.
gcc/fortran/ChangeLog:
* error.cc (gfc_diagnostics_init): Update for change to start_span.
gcc/jit/ChangeLog:
* dummy-frontend.cc (jit_langhook_init): Update for change to
diagnostic_context callbacks.
gcc/testsuite/ChangeLog:
* gcc.dg/plugin/diagnostic_group_plugin.c
(test_begin_group_cb, test_end_group_cb): Replace with...
(class test_output_format): ...this new subclass.
(plugin_init): Update.
Signed-off-by: David Malcolm <dmalcolm@redhat.com>
Diffstat (limited to 'gcc/testsuite/gcc.dg/plugin')
-rw-r--r-- | gcc/testsuite/gcc.dg/plugin/diagnostic_group_plugin.c | 45 |
1 files changed, 24 insertions, 21 deletions
diff --git a/gcc/testsuite/gcc.dg/plugin/diagnostic_group_plugin.c b/gcc/testsuite/gcc.dg/plugin/diagnostic_group_plugin.c index 3396b38..0a4f25e 100644 --- a/gcc/testsuite/gcc.dg/plugin/diagnostic_group_plugin.c +++ b/gcc/testsuite/gcc.dg/plugin/diagnostic_group_plugin.c @@ -181,27 +181,31 @@ test_diagnostic_start_span_fn (diagnostic_context *context, pp_newline (context->printer); } -/* Custom diagnostic callback: loudly announce a new diagnostic group. */ +/* Custom output format subclass. */ -static void -test_begin_group_cb (diagnostic_context * context) +class test_output_format : public diagnostic_text_output_format { - pp_string (context->printer, - "================================= BEGIN GROUP =============================="); - pp_newline (context->printer); -} - -/* Custom diagnostic callback: loudly announce the end of a - diagnostic group. */ + public: + test_output_format (diagnostic_context &context) + : diagnostic_text_output_format (context) + {} -static void -test_end_group_cb (diagnostic_context * context) -{ - pp_set_prefix (context->printer, NULL); - pp_string (context->printer, - "---------------------------------- END GROUP -------------------------------"); - pp_newline_and_flush (context->printer); -} + void on_begin_group () final override + { + /* Loudly announce a new diagnostic group. */ + pp_string (m_context.printer, + "================================= BEGIN GROUP =============================="); + pp_newline (m_context.printer); + } + void on_end_group () final override + { + /* Loudly announce the end of a diagnostic group. */ + pp_set_prefix (m_context.printer, NULL); + pp_string (m_context.printer, + "---------------------------------- END GROUP -------------------------------"); + pp_newline_and_flush (m_context.printer); + } +}; /* Entrypoint for the plugin. Install custom callbacks into the global_dc. @@ -220,9 +224,8 @@ plugin_init (struct plugin_name_args *plugin_info, return 1; diagnostic_starter (global_dc) = test_diagnostic_starter; - global_dc->start_span = test_diagnostic_start_span_fn; - global_dc->begin_group_cb = test_begin_group_cb; - global_dc->end_group_cb = test_end_group_cb; + global_dc->m_text_callbacks.start_span = test_diagnostic_start_span_fn; + global_dc->m_output_format = new test_output_format (*global_dc); pass_info.pass = new pass_test_groups (g); pass_info.reference_pass_name = "*warn_function_noreturn"; |