From 478dd60ddcf17773ebd1af367c9dcaee2401f797 Mon Sep 17 00:00:00 2001 From: David Malcolm Date: Thu, 15 Nov 2018 14:32:41 +0000 Subject: Machine-readable diagnostic output (PR other/19165) This patch implements a -fdiagnostics-format=json option which converts the diagnostics to be output to stderr in a JSON format; see the documentation in invoke.texi. Logically-related diagnostics are nested at the JSON level, using the auto_diagnostic_group mechanism. gcc/ChangeLog: PR other/19165 * Makefile.in (OBJS): Move json.o to... (OBJS-libcommon): ...here and add diagnostic-format-json.o. * common.opt (fdiagnostics-format=): New option. (diagnostics_output_format): New enum. * diagnostic-format-json.cc: New file. * diagnostic.c (default_diagnostic_final_cb): New function, taken from start of diagnostic_finish. (diagnostic_initialize): Initialize final_cb to default_diagnostic_final_cb. (diagnostic_finish): Move "being treated as errors" messages to default_diagnostic_final_cb. Call any final_cb. (default_diagnostic_finalizer): Add diagnostic_t param. (diagnostic_report_diagnostic): Pass "orig_diag_kind" to diagnostic_finalizer callback. * diagnostic.h (enum diagnostics_output_format): New enum. (diagnostic_finalizer_fn): Reimplement, adding diagnostic_t param. (struct diagnostic_context): Add "final_cb". (default_diagnostic_finalizer): Add diagnostic_t param. (diagnostic_output_format_init): New decl. * doc/invoke.texi (-fdiagnostics-format): New option. * dwarf2out.c (gen_producer_string): Ignore OPT_fdiagnostics_format_. * gcc.c (driver_handle_option): Handle OPT_fdiagnostics_format_. * lto-wrapper.c (append_diag_options): Ignore it. * opts.c (common_handle_option): Handle it. gcc/c-family/ChangeLog: PR other/19165 * c-opts.c (c_diagnostic_finalizer): Add diagnostic_t param. gcc/fortran/ChangeLog: PR other/19165 * error.c (gfc_diagnostic_finalizer): Add diagnostic_t param. gcc/jit/ChangeLog: PR other/19165 * dummy-frontend.c (jit_begin_diagnostic): Add diagnostic_t param. gcc/testsuite/ChangeLog: PR other/19165 * c-c++-common/diagnostic-format-json-1.c: New test. * c-c++-common/diagnostic-format-json-2.c: New test. * c-c++-common/diagnostic-format-json-3.c: New test. * c-c++-common/diagnostic-format-json-4.c: New test. * c-c++-common/diagnostic-format-json-5.c: New test. * gcc.dg/plugin/diagnostic_plugin_test_show_locus.c (custom_diagnostic_finalizer): Add diagnostic_t param. * gcc.dg/plugin/location_overflow_plugin.c (verify_unpacked_ranges): Likewise. (verify_no_columns): Likewise. * gfortran.dg/diagnostic-format-json-1.F90: New test. * gfortran.dg/diagnostic-format-json-2.F90: New test. * gfortran.dg/diagnostic-format-json-3.F90: New test. From-SVN: r266186 --- gcc/diagnostic.c | 45 ++++++++++++++++++++++++++++----------------- 1 file changed, 28 insertions(+), 17 deletions(-) (limited to 'gcc/diagnostic.c') diff --git a/gcc/diagnostic.c b/gcc/diagnostic.c index 3d86a21..27e98fa 100644 --- a/gcc/diagnostic.c +++ b/gcc/diagnostic.c @@ -131,6 +131,28 @@ diagnostic_set_caret_max_width (diagnostic_context *context, int value) context->caret_max_width = value; } +/* Default implementation of final_cb. */ + +static void +default_diagnostic_final_cb (diagnostic_context *context) +{ + /* Some of the errors may actually have been warnings. */ + if (diagnostic_kind_count (context, DK_WERROR)) + { + /* -Werror was given. */ + if (context->warning_as_error_requested) + pp_verbatim (context->printer, + _("%s: all warnings being treated as errors"), + progname); + /* At least one -Werror= was given. */ + else + pp_verbatim (context->printer, + _("%s: some warnings being treated as errors"), + progname); + pp_newline_and_flush (context->printer); + } +} + /* Initialize the diagnostic message outputting machinery. */ void diagnostic_initialize (diagnostic_context *context, int n_opts) @@ -185,6 +207,7 @@ diagnostic_initialize (diagnostic_context *context, int n_opts) context->diagnostic_group_emission_count = 0; context->begin_group_cb = NULL; context->end_group_cb = NULL; + context->final_cb = default_diagnostic_final_cb; } /* Maybe initialize the color support. We require clients to do this @@ -220,21 +243,8 @@ diagnostic_color_init (diagnostic_context *context, int value /*= -1 */) void diagnostic_finish (diagnostic_context *context) { - /* Some of the errors may actually have been warnings. */ - if (diagnostic_kind_count (context, DK_WERROR)) - { - /* -Werror was given. */ - if (context->warning_as_error_requested) - pp_verbatim (context->printer, - _("%s: all warnings being treated as errors"), - progname); - /* At least one -Werror= was given. */ - else - pp_verbatim (context->printer, - _("%s: some warnings being treated as errors"), - progname); - pp_newline_and_flush (context->printer); - } + if (context->final_cb) + context->final_cb (context); diagnostic_file_cache_fini (); @@ -642,7 +652,8 @@ default_diagnostic_start_span_fn (diagnostic_context *context, void default_diagnostic_finalizer (diagnostic_context *context, - diagnostic_info *diagnostic) + diagnostic_info *diagnostic, + diagnostic_t) { diagnostic_show_locus (context, diagnostic->richloc, diagnostic->kind); pp_destroy_prefix (context->printer); @@ -1006,7 +1017,7 @@ diagnostic_report_diagnostic (diagnostic_context *context, pp_output_formatted_text (context->printer); if (context->show_option_requested) print_option_information (context, diagnostic, orig_diag_kind); - (*diagnostic_finalizer (context)) (context, diagnostic); + (*diagnostic_finalizer (context)) (context, diagnostic, orig_diag_kind); if (context->parseable_fixits_p) { print_parseable_fixits (context->printer, diagnostic->richloc); -- cgit v1.1