From patchwork Tue Mar 19 18:03:50 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Malcolm X-Patchwork-Id: 1913748 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=ZwX11SXo; 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 4TzfkV5ldvz1yWs for ; Wed, 20 Mar 2024 05:04:33 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 12B3E3858C98 for ; Tue, 19 Mar 2024 18:04:26 +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 ESMTPS id 5A95B3858CDB for ; Tue, 19 Mar 2024 18:03:58 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 5A95B3858CDB 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 5A95B3858CDB 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=1710871440; cv=none; b=g5KA2PPbClrktOYMfcg1Oq6a8iGA31lO74u2bz0Dc6T8fT+Y5EkmyNQD0lAbZXn/LPhd79k6vnPZlPnWP16A/B+lkLpXuc2P3v/R8DbJZDo6xEiDYRdcZPmwCH8VFvWj4YhI4KFuKRHoubuoc+K1bXWwdUxedKqy9O3+Adzp1yo= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1710871440; c=relaxed/simple; bh=aGRSfXMRkyJYYwkEwMHqpYjq/HJH6l0pk1TA5cVV+mk=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=APbe52qx267tambwD5kbq/IIjz7KkFsRp75Er6gffPUwOWCnwXUmwKZMF/lSoHDkZkSTDIgfwhocT6pLImDY2iB+o4yqTHtJF1jKKdHliGY0LXd9ndKyj5L6LcA3Peb4ml8Y0Bc3gvpMcoD20YiQtCQE/mgbVlhO9Fbebow/R9k= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1710871437; 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=6DELL6t45/wxrj1z9Cj6ivrJlTvJ/C/XR0RDGFP6Svg=; b=ZwX11SXohOpOjo1gBhzDXcPUQLXTE7vEwrWYK9jVeqR4q/PCgEgd8ufn5Hpts/8C1EES91 TPj3qlSnOn0rK1fM3L8JinqWYYvLcIdkk4vH7HS+CQvK6MyDPkL73tbtRDy7FsKK9R9LLY wKYD776LDKvHFt9e71iCCuY91FTrA/k= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-307-Jr_7HeQPMmytqJa3MGrCdQ-1; Tue, 19 Mar 2024 14:03:56 -0400 X-MC-Unique: Jr_7HeQPMmytqJa3MGrCdQ-1 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.rdu2.redhat.com [10.11.54.6]) (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 mimecast-mx02.redhat.com (Postfix) with ESMTPS id 06DCD85CDE2 for ; Tue, 19 Mar 2024 18:03:56 +0000 (UTC) Received: from t14s.localdomain.com (unknown [10.22.32.20]) by smtp.corp.redhat.com (Postfix) with ESMTP id CEB5E2166AE1; Tue, 19 Mar 2024 18:03:55 +0000 (UTC) From: David Malcolm To: gcc-patches@gcc.gnu.org Cc: David Malcolm Subject: [pushed] diagnostics: fix corrupt json/SARIF on stderr [PR114348] Date: Tue, 19 Mar 2024 14:03:50 -0400 Message-Id: <20240319180350.721466-1-dmalcolm@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.6 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_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP, T_FILL_THIS_FORM_SHORT, 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 Various values of -fdiagnostics-format= request machine-readable output on stderr, using JSON, but in various places we use fnotice to write free-form text to stderr, such as "compilation terminated", leading to corrupt JSON. Fix by having fnotice skip the output for such cases. Successfully bootstrapped & regrtested on x86_64-pc-linux-gnu. Lightly tested manually. Pushed to trunk as r14-9554-g0bf99b1b7eda2f. gcc/ChangeLog: PR middle-end/114348 * diagnostic-format-json.cc (json_stderr_output_format::machine_readable_stderr_p): New. (json_file_output_format::machine_readable_stderr_p): New. * diagnostic-format-sarif.cc (sarif_stream_output_format::machine_readable_stderr_p): New. (sarif_file_output_format::machine_readable_stderr_p): New. * diagnostic.cc (diagnostic_context::action_after_output): Move "fnotice" to before "finish" call, so that we still have the diagnostic_context. (fnotice): Bail out if the user requested one of the machine-readable diagnostic output formats on stderr. * diagnostic.h (diagnostic_output_format::machine_readable_stderr_p): New pure virtual function. (diagnostic_text_output_format::machine_readable_stderr_p): New. (diagnostic_context::get_output_format): New accessor. Signed-off-by: David Malcolm --- gcc/diagnostic-format-json.cc | 8 ++++++++ gcc/diagnostic-format-sarif.cc | 8 ++++++++ gcc/diagnostic.cc | 12 +++++++++++- gcc/diagnostic.h | 10 ++++++++++ 4 files changed, 37 insertions(+), 1 deletion(-) diff --git a/gcc/diagnostic-format-json.cc b/gcc/diagnostic-format-json.cc index 51e016b6463..0782ae831eb 100644 --- a/gcc/diagnostic-format-json.cc +++ b/gcc/diagnostic-format-json.cc @@ -314,6 +314,10 @@ public: { flush_to_file (stderr); } + bool machine_readable_stderr_p () const final override + { + return true; + } }; class json_file_output_format : public json_output_format @@ -345,6 +349,10 @@ public: fclose (outf); free (filename); } + bool machine_readable_stderr_p () const final override + { + return false; + } private: char *m_base_file_name; diff --git a/gcc/diagnostic-format-sarif.cc b/gcc/diagnostic-format-sarif.cc index 307b2f56c28..97c5943cd33 100644 --- a/gcc/diagnostic-format-sarif.cc +++ b/gcc/diagnostic-format-sarif.cc @@ -1750,6 +1750,10 @@ public: { m_builder.flush_to_file (m_stream); } + bool machine_readable_stderr_p () const final override + { + return m_stream == stderr; + } private: FILE *m_stream; }; @@ -1782,6 +1786,10 @@ public: fclose (outf); free (filename); } + bool machine_readable_stderr_p () const final override + { + return false; + } private: char *m_base_file_name; diff --git a/gcc/diagnostic.cc b/gcc/diagnostic.cc index 1d143fa7498..8e4621f8031 100644 --- a/gcc/diagnostic.cc +++ b/gcc/diagnostic.cc @@ -802,8 +802,8 @@ diagnostic_context::action_after_output (diagnostic_t diag_kind) case DK_FATAL: if (m_abort_on_error) real_abort (); - finish (); fnotice (stderr, "compilation terminated.\n"); + finish (); exit (FATAL_EXIT_CODE); default: @@ -2264,6 +2264,16 @@ diagnostic_context::emit_diagram (const diagnostic_diagram &diagram) void fnotice (FILE *file, const char *cmsgid, ...) { + /* If the user requested one of the machine-readable diagnostic output + formats on stderr (e.g. -fdiagnostics-format=sarif-stderr), then + emitting free-form text on stderr will lead to corrupt output. + Skip the message for such cases. */ + if (file == stderr && global_dc) + if (const diagnostic_output_format *output_format + = global_dc->get_output_format ()) + if (output_format->machine_readable_stderr_p ()) + return; + va_list ap; va_start (ap, cmsgid); diff --git a/gcc/diagnostic.h b/gcc/diagnostic.h index 0a7c7e02b37..065ac784e25 100644 --- a/gcc/diagnostic.h +++ b/gcc/diagnostic.h @@ -210,6 +210,7 @@ public: virtual void on_end_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; protected: diagnostic_output_format (diagnostic_context &context) @@ -238,6 +239,10 @@ public: void on_end_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; + } }; /* A stack of sets of classifications: each entry in the stack is @@ -432,6 +437,11 @@ public: void emit_diagram (const diagnostic_diagram &diagram); + const diagnostic_output_format *get_output_format () const + { + return m_output_format; + } + /* Various setters for use by option-handling logic. */ void set_output_format (diagnostic_output_format *output_format); void set_text_art_charset (enum diagnostic_text_art_charset charset);