From patchwork Thu May 16 01:29:57 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Malcolm X-Patchwork-Id: 1935780 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=FWvrBGuz; 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 4Vfswr6CzDz1ymw for ; Thu, 16 May 2024 11:30:36 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 446EB384CBA1 for ; Thu, 16 May 2024 01:30:34 +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.133.124]) by sourceware.org (Postfix) with ESMTPS id 73588384CB97 for ; Thu, 16 May 2024 01:30:07 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 73588384CB97 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 73588384CB97 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1715823010; cv=none; b=PKSb/M/12ULcCoCfwp3uL54FrINH8AT/VaG7KgMkk3zipg3eIIXeHfB7IYs+uLENTvWKj9J8screchRyobwa2QMwz4ISPh8xDwAtXfrIdY5pjhn/UzJgdv4RvL/c/SP99NwbNK7G/ue1WtnuEUMZD0dbvRsvfIye4wGkXwj/ekU= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1715823010; c=relaxed/simple; bh=rfjT0kSiqu1DgZebOfXCmQqYaBakHbi8P4eFfZlCidk=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=SGsiCV1RTEMCs0E7QyExAnMLcyjGU5p4/oUK6XiIdn3QlZED8Q0lpk+jdbmSm4KrfAW0B471UZVHUf2ZlJ+FoZ1hoz5KD6Znolu2IGCagqBiBXftJstx5HFkxmWtZrd3VZmFuWMMr8CqlzRIbVSomGk2nqKcPF75ZgqMT06d5Qs= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1715823007; 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=tfffvICfmCy6nM3B7Vn6sJgn8PpqIIGTATJ295ywd1w=; b=FWvrBGuzNZjoPfH+iqyPJd4+rVdndqsPcnYPnTF7e0GMmUYbxtlCyFVCpW16n2h3ezqAcm rvpZxATA09omIup5GC4Bo/gIjoWKq4nkOnVYuhGSWysa+SZn6OWvvc8OSkijZF7vmjsyX4 a1TlVHTX9zx3L+b/hlpdXcgJSRw8vKU= Received: from mimecast-mx02.redhat.com (mx-ext.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-256-P15IpftyMrem5wY_2r6Pqw-1; Wed, 15 May 2024 21:30:04 -0400 X-MC-Unique: P15IpftyMrem5wY_2r6Pqw-1 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.rdu2.redhat.com [10.11.54.7]) (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 17CB929AA3AE for ; Thu, 16 May 2024 01:30:04 +0000 (UTC) Received: from t14s.localdomain.com (unknown [10.22.16.76]) by smtp.corp.redhat.com (Postfix) with ESMTP id AD4EC1C00A8F; Thu, 16 May 2024 01:30:03 +0000 (UTC) From: David Malcolm To: gcc-patches@gcc.gnu.org Cc: David Malcolm Subject: [pushed] diagnostics: handle SGR codes in line_label::m_display_width Date: Wed, 15 May 2024 21:29:57 -0400 Message-Id: <20240516013000.2967650-1-dmalcolm@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.7 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-12.1 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP 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 Successfully bootstrapped & regrtested on x86_64-pc-linux-gnu. Successful run of analyzer integration tests on x86_64-pc-linux-gnu. Pushed to trunk as r15-532-ga7be993806a90a. gcc/ChangeLog: * diagnostic-show-locus.cc: Define INCLUDE_VECTOR and include "text-art/types.h". (line_label::line_label): Drop "policy" argument. Use styled_string::calc_canvas_width when computing m_display_width, as this skips SGR codes. (layout::print_any_labels): Update for line_label ctor change. (selftest::test_one_liner_labels_utf8): Update expected text to reflect that the labels can fit on one line if we don't get confused by SGR colorization codes. Signed-off-by: David Malcolm --- gcc/diagnostic-show-locus.cc | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/gcc/diagnostic-show-locus.cc b/gcc/diagnostic-show-locus.cc index ceccc0b793d1..f42006cfe2a1 100644 --- a/gcc/diagnostic-show-locus.cc +++ b/gcc/diagnostic-show-locus.cc @@ -19,6 +19,7 @@ along with GCC; see the file COPYING3. If not see . */ #include "config.h" +#define INCLUDE_VECTOR #include "system.h" #include "coretypes.h" #include "version.h" @@ -31,6 +32,7 @@ along with GCC; see the file COPYING3. If not see #include "selftest.h" #include "selftest-diagnostic.h" #include "cpplib.h" +#include "text-art/types.h" #ifdef HAVE_TERMIOS_H # include @@ -1923,14 +1925,18 @@ struct pod_label_text class line_label { public: - line_label (const cpp_char_column_policy &policy, - int state_idx, int column, + line_label (int state_idx, int column, label_text text) : m_state_idx (state_idx), m_column (column), m_text (std::move (text)), m_label_line (0), m_has_vbar (true) { - const int bytes = strlen (m_text.m_buffer); - m_display_width = cpp_display_width (m_text.m_buffer, bytes, policy); + /* Using styled_string rather than cpp_display_width here + lets us skip SGR formatting characters for color and URLs. + It doesn't handle tabs and unicode escaping, but we don't + expect to see either of those in labels. */ + text_art::style_manager sm; + text_art::styled_string str (sm, m_text.m_buffer); + m_display_width = str.calc_canvas_width (); } /* Sorting is primarily by column, then by state index. */ @@ -1990,7 +1996,7 @@ layout::print_any_labels (linenum_type row) if (text.get () == NULL) continue; - labels.safe_push (line_label (m_policy, i, disp_col, std::move (text))); + labels.safe_push (line_label (i, disp_col, std::move (text))); } } @@ -4382,9 +4388,9 @@ test_one_liner_labels_utf8 () ASSERT_STREQ (" _foo = _bar._field;\n" " ^~~~~~~~~~~~~ ~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~\n" " | | |\n" - " | | label 2\xcf\x80\n" - " | label 1\xcf\x80\n" - " label 0\xf0\x9f\x98\x82\n", + " label 0\xf0\x9f\x98\x82" + /* ... */ " label 1\xcf\x80" + /* ...................*/ " label 2\xcf\x80\n", pp_formatted_text (dc.printer)); } { @@ -4395,9 +4401,9 @@ test_one_liner_labels_utf8 () (" <9f><98><82>_foo = <80>_bar.<9f><98><82>_field<80>;\n" " ^~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n" " | | |\n" - " | | label 2\xcf\x80\n" - " | label 1\xcf\x80\n" - " label 0\xf0\x9f\x98\x82\n", + " label 0\xf0\x9f\x98\x82" + /* ... */ " label 1\xcf\x80" + /* ..........................*/ " label 2\xcf\x80\n", pp_formatted_text (dc.printer)); } }