From patchwork Mon Aug 26 16:36:15 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Malcolm X-Patchwork-Id: 1976918 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=CyyRPCif; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=server2.sourceware.org; envelope-from=gcc-patches-bounces~incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4WsxG92sdWz1yYl for ; Tue, 27 Aug 2024 02:39:13 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 40C3E385C6CA for ; Mon, 26 Aug 2024 16:39:11 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTP id 2D87C385E027 for ; Mon, 26 Aug 2024 16:36:21 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 2D87C385E027 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 2D87C385E027 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1724690184; cv=none; b=kt3NmvKA5MmLBy23sKCl6Rb+9ICUqBP5d+xPeWrIEAdW1wrjRprYDi40sH73WsEiFP7r+bJ99Ydox2/cERV0mSbk4/nL+kSJLB7MBgTOB1KzBH4Gl+mSSzfu1KCWvhn+ux5M4YHsSLjRR+p+wmocXs98dDHTa6pC66SVkv9UiGY= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1724690184; c=relaxed/simple; bh=5FcWTiObOt0hXMnpDqXa8O2l6K/wEJBBafVzUlD4vYE=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=R31nAPQ6srcguXkULIKtUYjS/7ekK60s743v58LCActgVGMK1QjGvXebCbvECOPPFrxDNdFk6huBTq88TSrUNjhhi1CU0PjCj+FROKk7TyDUAGGIGMvim6F8GAq9l65jfr/IiT59l1gXrB+hd5Fbg5slc9jg1indrmuRjOakwOg= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1724690180; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=9tBbUlRibZKTaHL694qhK8gaYShLk4v9MzhH4sy/cMo=; b=CyyRPCifG2Qdg3WFW19DbZ9mRycEDCrmdxbdZGj0iVux9fjdfI0Pgi+28gebY1w11DtaJI WdWTDxdcNs+zOx0kOR76Kw+UPSD6Y2Ltz7n/Y2QiT2MOa3OHZc64Bhd68tBrTllPmLWyym jVJ5Caj84r+qTT7TtfLfHWnGdoQx2KE= Received: from mx-prod-mc-03.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-433-mYrtk1HWM4uCzAZXL6VLtQ-1; Mon, 26 Aug 2024 12:36:19 -0400 X-MC-Unique: mYrtk1HWM4uCzAZXL6VLtQ-1 Received: from mx-prod-int-04.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-04.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.40]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id D2BB61955D48 for ; Mon, 26 Aug 2024 16:36:18 +0000 (UTC) Received: from t14s.localdomain.com (unknown [10.22.16.43]) by mx-prod-int-04.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 9F69219560A3; Mon, 26 Aug 2024 16:36:17 +0000 (UTC) From: David Malcolm To: gcc-patches@gcc.gnu.org Cc: David Malcolm Subject: [pushed] diagnostics: consolidate on_{begin, end}_diagnostic into on_report_diagnostic Date: Mon, 26 Aug 2024 12:36:15 -0400 Message-Id: <20240826163615.2393695-1-dmalcolm@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.0 on 10.30.177.40 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-11.6 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces~incoming=patchwork.ozlabs.org@gcc.gnu.org Previously diagnostic_context::report_diagnostic had, after the call to pp_format (phases 1 and 2 of formatting the message): m_output_format->on_begin_diagnostic (*diagnostic); pp_output_formatted_text (this->printer, m_urlifier); if (m_show_cwe) print_any_cwe (*diagnostic); if (m_show_rules) print_any_rules (*diagnostic); if (m_show_option_requested) print_option_information (*diagnostic, orig_diag_kind); m_output_format->on_end_diagnostic (*diagnostic, orig_diag_kind); This patch replaces all of the above with a single call to m_output_format->on_report_diagnostic (*diagnostic, orig_diag_kind); moving responsibility for phase 3 of formatting and printing the result from diagnostic_context to the output format. This simplifies diagnostic_context::report_diagnostic and allows us to move the code that prints CWEs, rules, and option information in textual form from diagnostic_context to diagnostic_text_output_format, where it belongs. No functional change intended. Successfully bootstrapped & regrtested on x86_64-pc-linux-gnu. Pushed to trunk as r15-3200-gac707d30ce449f. gcc/ChangeLog: * diagnostic-format-json.cc (json_output_format::on_begin_diagnostic): Delete. (json_output_format::on_end_diagnostic): Rename to... (json_output_format::on_report_diagnostic): ...this and add call to pp_output_formatted_text. (diagnostic_output_format_init_json): Drop unnecessary calls to disable textual printing of CWEs, rules, and options. * diagnostic-format-sarif.cc (sarif_builder::end_diagnostic): Rename to... (sarif_builder::on_report_diagnostic): ...this and add call to pp_output_formatted_text. (sarif_output_format::on_begin_diagnostic): Delete. (sarif_output_format::on_end_diagnostic): Rename to... (sarif_output_format::on_report_diagnostic): ...this and update call to m_builder accordingly. (diagnostic_output_format_init_sarif): Drop unnecessary calls to disable textual printing of CWEs, rules, and options. * diagnostic.cc (diagnostic_context::print_any_cwe): Convert to... (diagnostic_text_output_format::print_any_cwe): ...this. (diagnostic_context::print_any_rules): Convert to... (diagnostic_text_output_format::print_any_rules): ...this. (diagnostic_context::print_option_information): Convert to... (diagnostic_text_output_format::print_option_information): ...this. (diagnostic_context::report_diagnostic): Replace calls to the output format's on_begin_diagnostic, to pp_output_formatted_text, printing CWE, rules, option info, and the call to the format's on_end_diagnostic with a call to the format's on_report_diagnostic. (diagnostic_text_output_format::on_begin_diagnostic): Delete. (diagnostic_text_output_format::on_end_diagnostic): Delete. (diagnostic_text_output_format::on_report_diagnostic): New vfunc, which effectively does the on_begin_diagnostic, the call to pp_output_formatted_text, the calls for printing CWE, rules, option info, and the call to the diagnostic_finalizer. * diagnostic.h (diagnostic_output_format::on_begin_diagnostic): Delete. (diagnostic_output_format::on_end_diagnostic): Delete. (diagnostic_output_format::on_report_diagnostic): New. (diagnostic_text_output_format::on_begin_diagnostic): Delete. (diagnostic_text_output_format::on_end_diagnostic): Delete. (diagnostic_text_output_format::on_report_diagnostic): New. (class diagnostic_context): Add friend class diagnostic_text_output_format. (diagnostic_context::get_urlifier): New accessor. (diagnostic_context::print_any_cwe): Move decl... (diagnostic_text_output_format::print_any_cwe): ...to here. (diagnostic_context::print_any_rules): Move decl... (diagnostic_text_output_format::print_any_rules): ...to here. (diagnostic_context::print_option_information): Move decl... (diagnostic_text_output_format::print_option_information): ...to here. Signed-off-by: David Malcolm --- gcc/diagnostic-format-json.cc | 24 +-- gcc/diagnostic-format-sarif.cc | 34 ++--- gcc/diagnostic.cc | 261 +++++++++++++++++---------------- gcc/diagnostic.h | 28 ++-- 4 files changed, 171 insertions(+), 176 deletions(-) diff --git a/gcc/diagnostic-format-json.cc b/gcc/diagnostic-format-json.cc index b78cb92cfd7a..f2e9d0d79e51 100644 --- a/gcc/diagnostic-format-json.cc +++ b/gcc/diagnostic-format-json.cc @@ -47,13 +47,8 @@ public: m_cur_children_array = nullptr; } void - on_begin_diagnostic (const diagnostic_info &) final override - { - /* No-op. */ - } - void - on_end_diagnostic (const diagnostic_info &diagnostic, - diagnostic_t orig_diag_kind) final override; + on_report_diagnostic (const diagnostic_info &diagnostic, + diagnostic_t orig_diag_kind) final override; void on_diagram (const diagnostic_diagram &) final override { /* No-op. */ @@ -225,14 +220,16 @@ make_json_for_path (diagnostic_context &context, } -/* Implementation of "on_end_diagnostic" vfunc for JSON output. +/* Implementation of "on_report_diagnostic" vfunc for JSON output. Generate a JSON object for DIAGNOSTIC, and store for output within current diagnostic group. */ void -json_output_format::on_end_diagnostic (const diagnostic_info &diagnostic, - diagnostic_t orig_diag_kind) +json_output_format::on_report_diagnostic (const diagnostic_info &diagnostic, + diagnostic_t orig_diag_kind) { + pp_output_formatted_text (m_context.printer, m_context.get_urlifier ()); + json::object *diag_obj = new json::object (); /* Get "kind" of diagnostic. */ @@ -395,13 +392,6 @@ diagnostic_output_format_init_json (diagnostic_context &context) /* Suppress normal textual path output. */ context.set_path_format (DPF_NONE); - /* The metadata is handled in JSON format, rather than as text. */ - context.set_show_cwe (false); - context.set_show_rules (false); - - /* The option is handled in JSON format, rather than as text. */ - context.set_show_option_requested (false); - /* Don't colorize the text. */ pp_show_color (context.printer) = false; context.set_show_highlight_colors (false); diff --git a/gcc/diagnostic-format-sarif.cc b/gcc/diagnostic-format-sarif.cc index 1d99c904ff0c..554bf3cb2d5c 100644 --- a/gcc/diagnostic-format-sarif.cc +++ b/gcc/diagnostic-format-sarif.cc @@ -592,9 +592,9 @@ public: const char *main_input_filename_, bool formatted); - void end_diagnostic (diagnostic_context &context, - const diagnostic_info &diagnostic, - diagnostic_t orig_diag_kind); + void on_report_diagnostic (diagnostic_context &context, + const diagnostic_info &diagnostic, + diagnostic_t orig_diag_kind); void emit_diagram (diagnostic_context &context, const diagnostic_diagram &diagram); void end_group (); @@ -1350,13 +1350,15 @@ sarif_builder::sarif_builder (diagnostic_context &context, false); } -/* Implementation of "end_diagnostic" for SARIF output. */ +/* Implementation of "on_report_diagnostic" for SARIF output. */ void -sarif_builder::end_diagnostic (diagnostic_context &context, - const diagnostic_info &diagnostic, - diagnostic_t orig_diag_kind) +sarif_builder::on_report_diagnostic (diagnostic_context &context, + const diagnostic_info &diagnostic, + diagnostic_t orig_diag_kind) { + pp_output_formatted_text (context.printer, context.get_urlifier ()); + if (diagnostic.kind == DK_ICE || diagnostic.kind == DK_ICE_NOBT) { m_invocation_obj->add_notification_for_ice (context, diagnostic, *this); @@ -2920,15 +2922,10 @@ public: m_builder.end_group (); } void - on_begin_diagnostic (const diagnostic_info &) final override + on_report_diagnostic (const diagnostic_info &diagnostic, + diagnostic_t orig_diag_kind) final override { - /* No-op, */ - } - void - on_end_diagnostic (const diagnostic_info &diagnostic, - diagnostic_t orig_diag_kind) final override - { - m_builder.end_diagnostic (m_context, diagnostic, orig_diag_kind); + m_builder.on_report_diagnostic (m_context, diagnostic, orig_diag_kind); } void on_diagram (const diagnostic_diagram &diagram) final override { @@ -3022,13 +3019,6 @@ diagnostic_output_format_init_sarif (diagnostic_context &context) /* Override callbacks. */ context.set_ice_handler_callback (sarif_ice_handler); - /* The metadata is handled in SARIF format, rather than as text. */ - context.set_show_cwe (false); - context.set_show_rules (false); - - /* The option is handled in SARIF format, rather than as text. */ - context.set_show_option_requested (false); - /* Don't colorize the text. */ pp_show_color (context.printer) = false; context.set_show_highlight_colors (false); diff --git a/gcc/diagnostic.cc b/gcc/diagnostic.cc index 92bd4f808453..497bbe79705c 100644 --- a/gcc/diagnostic.cc +++ b/gcc/diagnostic.cc @@ -1232,117 +1232,6 @@ get_cwe_url (int cwe) return xasprintf ("https://cwe.mitre.org/data/definitions/%i.html", cwe); } -/* If DIAGNOSTIC has a CWE identifier, print it. - - For example, if the diagnostic metadata associates it with CWE-119, - " [CWE-119]" will be printed, suitably colorized, and with a URL of a - description of the security issue. */ - -void -diagnostic_context::print_any_cwe (const diagnostic_info &diagnostic) -{ - if (diagnostic.metadata == NULL) - return; - - int cwe = diagnostic.metadata->get_cwe (); - if (cwe) - { - pretty_printer * const pp = this->printer; - char *saved_prefix = pp_take_prefix (pp); - pp_string (pp, " ["); - pp_string (pp, colorize_start (pp_show_color (pp), - diagnostic_kind_color[diagnostic.kind])); - if (pp->supports_urls_p ()) - { - char *cwe_url = get_cwe_url (cwe); - pp_begin_url (pp, cwe_url); - free (cwe_url); - } - pp_printf (pp, "CWE-%i", cwe); - pp_set_prefix (pp, saved_prefix); - if (pp->supports_urls_p ()) - pp_end_url (pp); - pp_string (pp, colorize_stop (pp_show_color (pp))); - pp_character (pp, ']'); - } -} - -/* If DIAGNOSTIC has any rules associated with it, print them. - - For example, if the diagnostic metadata associates it with a rule - named "STR34-C", then " [STR34-C]" will be printed, suitably colorized, - with any URL provided by the rule. */ - -void -diagnostic_context::print_any_rules (const diagnostic_info &diagnostic) -{ - if (diagnostic.metadata == NULL) - return; - - for (unsigned idx = 0; idx < diagnostic.metadata->get_num_rules (); idx++) - { - const diagnostic_metadata::rule &rule - = diagnostic.metadata->get_rule (idx); - if (char *desc = rule.make_description ()) - { - pretty_printer * const pp = this->printer; - char *saved_prefix = pp_take_prefix (pp); - pp_string (pp, " ["); - pp_string (pp, - colorize_start (pp_show_color (pp), - diagnostic_kind_color[diagnostic.kind])); - char *url = NULL; - if (pp->supports_urls_p ()) - { - url = rule.make_url (); - if (url) - pp_begin_url (pp, url); - } - pp_string (pp, desc); - pp_set_prefix (pp, saved_prefix); - if (pp->supports_urls_p ()) - if (url) - pp_end_url (pp); - free (url); - pp_string (pp, colorize_stop (pp_show_color (pp))); - pp_character (pp, ']'); - free (desc); - } - } -} - -/* Print any metadata about the option used to control DIAGNOSTIC to CONTEXT's - printer, e.g. " [-Werror=uninitialized]". - Subroutine of diagnostic_context::report_diagnostic. */ - -void -diagnostic_context::print_option_information (const diagnostic_info &diagnostic, - diagnostic_t orig_diag_kind) -{ - if (char *option_text = make_option_name (diagnostic.option_index, - orig_diag_kind, diagnostic.kind)) - { - char *option_url = nullptr; - if (this->printer->supports_urls_p ()) - option_url = make_option_url (diagnostic.option_index); - pretty_printer * const pp = this->printer; - pp_string (pp, " ["); - pp_string (pp, colorize_start (pp_show_color (pp), - diagnostic_kind_color[diagnostic.kind])); - if (option_url) - pp_begin_url (pp, option_url); - pp_string (pp, option_text); - if (option_url) - { - pp_end_url (pp); - free (option_url); - } - pp_string (pp, colorize_stop (pp_show_color (pp))); - pp_character (pp, ']'); - free (option_text); - } -} - /* Returns whether a DIAGNOSTIC should be printed, and adjusts diagnostic->kind as appropriate for #pragma GCC diagnostic and -Werror=foo. */ @@ -1517,15 +1406,10 @@ diagnostic_context::report_diagnostic (diagnostic_info *diagnostic) m_diagnostic_groups.m_emission_count++; pp_format (this->printer, &diagnostic->message, m_urlifier); - m_output_format->on_begin_diagnostic (*diagnostic); - pp_output_formatted_text (this->printer, m_urlifier); - if (m_show_cwe) - print_any_cwe (*diagnostic); - if (m_show_rules) - print_any_rules (*diagnostic); - if (m_show_option_requested) - print_option_information (*diagnostic, orig_diag_kind); - m_output_format->on_end_diagnostic (*diagnostic, orig_diag_kind); + /* Call vfunc in the output format. This is responsible for + phase 3 of formatting, and for printing the result. */ + m_output_format->on_report_diagnostic (*diagnostic, orig_diag_kind); + switch (m_extra_output_kind) { default: @@ -1815,16 +1699,27 @@ diagnostic_text_output_format::~diagnostic_text_output_format () } } +/* Implementation of diagnostic_output_format::on_report_diagnostic vfunc + for GCC's standard textual output. */ + void -diagnostic_text_output_format::on_begin_diagnostic (const diagnostic_info &diagnostic) +diagnostic_text_output_format:: +on_report_diagnostic (const diagnostic_info &diagnostic, + diagnostic_t orig_diag_kind) { (*diagnostic_starter (&m_context)) (&m_context, &diagnostic); -} -void -diagnostic_text_output_format::on_end_diagnostic (const diagnostic_info &diagnostic, - diagnostic_t orig_diag_kind) -{ + pp_output_formatted_text (m_context.printer, m_context.get_urlifier ()); + + if (m_context.m_show_cwe) + print_any_cwe (diagnostic); + + if (m_context.m_show_rules) + print_any_rules (diagnostic); + + if (m_context.m_show_option_requested) + print_option_information (diagnostic, orig_diag_kind); + (*diagnostic_finalizer (&m_context)) (&m_context, &diagnostic, orig_diag_kind); } @@ -1843,6 +1738,120 @@ diagnostic_text_output_format::on_diagram (const diagnostic_diagram &diagram) pp_flush (m_context.printer); } +/* If DIAGNOSTIC has a CWE identifier, print it. + + For example, if the diagnostic metadata associates it with CWE-119, + " [CWE-119]" will be printed, suitably colorized, and with a URL of a + description of the security issue. */ + +void +diagnostic_text_output_format::print_any_cwe (const diagnostic_info &diagnostic) +{ + if (diagnostic.metadata == NULL) + return; + + int cwe = diagnostic.metadata->get_cwe (); + if (cwe) + { + pretty_printer * const pp = m_context.printer; + char *saved_prefix = pp_take_prefix (pp); + pp_string (pp, " ["); + pp_string (pp, colorize_start (pp_show_color (pp), + diagnostic_kind_color[diagnostic.kind])); + if (pp->supports_urls_p ()) + { + char *cwe_url = get_cwe_url (cwe); + pp_begin_url (pp, cwe_url); + free (cwe_url); + } + pp_printf (pp, "CWE-%i", cwe); + pp_set_prefix (pp, saved_prefix); + if (pp->supports_urls_p ()) + pp_end_url (pp); + pp_string (pp, colorize_stop (pp_show_color (pp))); + pp_character (pp, ']'); + } +} + +/* If DIAGNOSTIC has any rules associated with it, print them. + + For example, if the diagnostic metadata associates it with a rule + named "STR34-C", then " [STR34-C]" will be printed, suitably colorized, + with any URL provided by the rule. */ + +void +diagnostic_text_output_format:: +print_any_rules (const diagnostic_info &diagnostic) +{ + if (diagnostic.metadata == NULL) + return; + + for (unsigned idx = 0; idx < diagnostic.metadata->get_num_rules (); idx++) + { + const diagnostic_metadata::rule &rule + = diagnostic.metadata->get_rule (idx); + if (char *desc = rule.make_description ()) + { + pretty_printer * const pp = m_context.printer; + char *saved_prefix = pp_take_prefix (pp); + pp_string (pp, " ["); + pp_string (pp, + colorize_start (pp_show_color (pp), + diagnostic_kind_color[diagnostic.kind])); + char *url = NULL; + if (pp->supports_urls_p ()) + { + url = rule.make_url (); + if (url) + pp_begin_url (pp, url); + } + pp_string (pp, desc); + pp_set_prefix (pp, saved_prefix); + if (pp->supports_urls_p ()) + if (url) + pp_end_url (pp); + free (url); + pp_string (pp, colorize_stop (pp_show_color (pp))); + pp_character (pp, ']'); + free (desc); + } + } +} + +/* Print any metadata about the option used to control DIAGNOSTIC to CONTEXT's + printer, e.g. " [-Werror=uninitialized]". + Subroutine of diagnostic_context::report_diagnostic. */ + +void +diagnostic_text_output_format:: +print_option_information (const diagnostic_info &diagnostic, + diagnostic_t orig_diag_kind) +{ + if (char *option_text + = m_context.make_option_name (diagnostic.option_index, + orig_diag_kind, diagnostic.kind)) + { + char *option_url = nullptr; + pretty_printer * const pp = m_context.printer; + if (pp->supports_urls_p ()) + option_url = m_context.make_option_url (diagnostic.option_index); + pp_string (pp, " ["); + pp_string (pp, colorize_start (pp_show_color (pp), + diagnostic_kind_color[diagnostic.kind])); + if (option_url) + pp_begin_url (pp, option_url); + pp_string (pp, option_text); + if (option_url) + { + pp_end_url (pp); + free (option_url); + } + pp_string (pp, colorize_stop (pp_show_color (pp))); + pp_character (pp, ']'); + free (option_text); + } +} + /* Set the output format for CONTEXT to FORMAT, using BASE_FILE_NAME for file-based output formats. */ diff --git a/gcc/diagnostic.h b/gcc/diagnostic.h index 2a9f2751dca2..0a496e4bfab9 100644 --- a/gcc/diagnostic.h +++ b/gcc/diagnostic.h @@ -208,9 +208,12 @@ public: virtual void on_begin_group () = 0; virtual void on_end_group () = 0; - virtual void on_begin_diagnostic (const diagnostic_info &) = 0; - virtual void on_end_diagnostic (const diagnostic_info &, - diagnostic_t orig_diag_kind) = 0; + + /* Vfunc with responsibility for phase 3 of formatting the message + and "printing" the result. */ + virtual void on_report_diagnostic (const diagnostic_info &, + diagnostic_t orig_diag_kind) = 0; + virtual void on_diagram (const diagnostic_diagram &diagram) = 0; virtual bool machine_readable_stderr_p () const = 0; @@ -237,14 +240,19 @@ public: ~diagnostic_text_output_format (); void on_begin_group () override {} void on_end_group () override {} - void on_begin_diagnostic (const diagnostic_info &) override; - void on_end_diagnostic (const diagnostic_info &, - diagnostic_t orig_diag_kind) override; + void on_report_diagnostic (const diagnostic_info &, + diagnostic_t orig_diag_kind) override; void on_diagram (const diagnostic_diagram &diagram) override; bool machine_readable_stderr_p () const final override { return false; } + +private: + void print_any_cwe (const diagnostic_info &diagnostic); + void print_any_rules (const diagnostic_info &diagnostic); + void print_option_information (const diagnostic_info &diagnostic, + diagnostic_t orig_diag_kind); }; /* A stack of sets of classifications: each entry in the stack is @@ -382,6 +390,8 @@ public: friend diagnostic_finalizer_fn & diagnostic_finalizer (diagnostic_context *context); + friend class diagnostic_text_output_format; + typedef void (*ice_handler_callback_t) (diagnostic_context *); typedef void (*set_locations_callback_t) (diagnostic_context *, diagnostic_info *); @@ -522,6 +532,7 @@ public: { return m_client_data_hooks; } + urlifier *get_urlifier () const { return m_urlifier; } text_art::theme *get_diagram_theme () const { return m_diagrams.m_theme; } int converted_column (expanded_location s) const; @@ -586,11 +597,6 @@ public: private: bool includes_seen_p (const line_map_ordinary *map); - void print_any_cwe (const diagnostic_info &diagnostic); - void print_any_rules (const diagnostic_info &diagnostic); - void print_option_information (const diagnostic_info &diagnostic, - diagnostic_t orig_diag_kind); - void show_any_path (const diagnostic_info &diagnostic); void error_recursion () ATTRIBUTE_NORETURN;