From patchwork Tue May 28 20:07:39 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Malcolm X-Patchwork-Id: 1940749 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=a/9ebuby; 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 4Vpk956Lh2z20f1 for ; Wed, 29 May 2024 06:08:25 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 1A99D384475D for ; Tue, 28 May 2024 20:08:23 +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 E53933858C50 for ; Tue, 28 May 2024 20:07:49 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org E53933858C50 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 E53933858C50 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=1716926871; cv=none; b=tj+LgqYI+3mgrsjN7LjPUA17LXjV0CMHtQ8RqC680hTjjO08SFuwbmz4l4+sXSmuQ2m+LdCc8qOIZxZhnq8WD165rAr8UdtGgAONwIOldoCYFK/soCrNFNE6pijapgaH4JHQreF+QrkN36w+APQRpTy99YvDzWkraW32W8O7zpI= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1716926871; c=relaxed/simple; bh=aHHnaf4qM7WSnlSeFOxPQPygbLuMKBfle8IEaj689sc=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=vJHF33bdKGkhHU5YQHFA9bqTU4QYBNNR19OHPoUNc/veO+7/Y5ggo2gDo5t5UrVwEPJXQdPj28mO1FeuXDdd8I1cmrbxTrBDvoMB5sXzAAL4o9XD6Ldw8MruSo9dWEDJviBJ1G6+Jw4Va8Hd45b+L1FycLJ4S29NTsL0Et7gbTQ= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1716926869; 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=3mtyV/WL8ZM8gLVquG5qbsg32O3t2e4MBZ2NUXHQLhw=; b=a/9ebubyy8a4+9o5CMA+bUTSSMhTVi04ORlc4fFxKxvz5LepZDhuDZwzdA3SNcCYXBZga9 2JmjoAH1WeXeQ5CzqF2eifIpbFf0C1WehQeDLZP2nmWkEp8JHoGR5SM/k5Y2fJcP8tr/3o bBB9FMYuIhqKNyGmVcfLGXcrW0YtrHE= 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-594-sxrTJ8IJMByM0NKhkyrugA-1; Tue, 28 May 2024 16:07:47 -0400 X-MC-Unique: sxrTJ8IJMByM0NKhkyrugA-1 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) (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 58ADA101A525 for ; Tue, 28 May 2024 20:07:47 +0000 (UTC) Received: from t14s.localdomain.com (unknown [10.22.10.48]) by smtp.corp.redhat.com (Postfix) with ESMTP id 25CCA2026D68; Tue, 28 May 2024 20:07:47 +0000 (UTC) From: David Malcolm To: gcc-patches@gcc.gnu.org Cc: David Malcolm Subject: [pushed 1/3] selftests: split out make_fndecl from selftest.h to its own header Date: Tue, 28 May 2024 16:07:39 -0400 Message-Id: <20240528200741.3791987-1-dmalcolm@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.4 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-11.5 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, 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 Avoid selftest.h requiring the "tree" type. No functional change intended. Successfully bootstrapped & regrtested on x86_64-pc-linux-gnu. Pushed to trunk as r15-873-gfb7a943ead689e. gcc/analyzer/ChangeLog: * region-model.cc: Include "selftest-tree.h". gcc/ChangeLog: * function-tests.cc: Include "selftest-tree.h". * selftest-tree.h: New file. * selftest.h (make_fndecl): Move to selftest-tree.h. Signed-off-by: David Malcolm --- gcc/analyzer/region-model.cc | 1 + gcc/function-tests.cc | 1 + gcc/selftest-tree.h | 41 ++++++++++++++++++++++++++++++++++++ gcc/selftest.h | 7 ------ 4 files changed, 43 insertions(+), 7 deletions(-) create mode 100644 gcc/selftest-tree.h diff --git a/gcc/analyzer/region-model.cc b/gcc/analyzer/region-model.cc index bebe2ed3cd69..0dd5671db1be 100644 --- a/gcc/analyzer/region-model.cc +++ b/gcc/analyzer/region-model.cc @@ -42,6 +42,7 @@ along with GCC; see the file COPYING3. If not see #include "diagnostic-color.h" #include "bitmap.h" #include "selftest.h" +#include "selftest-tree.h" #include "analyzer/analyzer.h" #include "analyzer/analyzer-logging.h" #include "ordered-hash-map.h" diff --git a/gcc/function-tests.cc b/gcc/function-tests.cc index 827734422d88..ea3d722d4b69 100644 --- a/gcc/function-tests.cc +++ b/gcc/function-tests.cc @@ -76,6 +76,7 @@ along with GCC; see the file COPYING3. If not see #include "ipa-ref.h" #include "cgraph.h" #include "selftest.h" +#include "selftest-tree.h" #include "print-rtl.h" #if CHECKING_P diff --git a/gcc/selftest-tree.h b/gcc/selftest-tree.h new file mode 100644 index 000000000000..9922af3340f2 --- /dev/null +++ b/gcc/selftest-tree.h @@ -0,0 +1,41 @@ +/* A self-testing framework, for use by -fself-test. + Copyright (C) 2015-2024 Free Software Foundation, Inc. + +This file is part of GCC. + +GCC is free software; you can redistribute it and/or modify it under +the terms of the GNU General Public License as published by the Free +Software Foundation; either version 3, or (at your option) any later +version. + +GCC is distributed in the hope that it will be useful, but WITHOUT ANY +WARRANTY; without even the implied warranty of MERCHANTABILITY or +FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +for more details. + +You should have received a copy of the GNU General Public License +along with GCC; see the file COPYING3. If not see +. */ + +#ifndef GCC_SELFTEST_TREE_H +#define GCC_SELFTEST_TREE_H + +/* The selftest code should entirely disappear in a production + configuration, hence we guard all of it with #if CHECKING_P. */ + +#if CHECKING_P + +namespace selftest { + +/* Helper function for selftests that need a function decl. */ + +extern tree make_fndecl (tree return_type, + const char *name, + vec ¶m_types, + bool is_variadic = false); + +} /* end of namespace selftest. */ + +#endif /* #if CHECKING_P */ + +#endif /* GCC_SELFTEST_TREE_H */ diff --git a/gcc/selftest.h b/gcc/selftest.h index 3bddaf1c3228..808d432ec480 100644 --- a/gcc/selftest.h +++ b/gcc/selftest.h @@ -178,13 +178,6 @@ class line_table_test ~line_table_test (); }; -/* Helper function for selftests that need a function decl. */ - -extern tree make_fndecl (tree return_type, - const char *name, - vec ¶m_types, - bool is_variadic = false); - /* Run TESTCASE multiple times, once for each case in our test matrix. */ extern void From patchwork Tue May 28 20:07:40 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Malcolm X-Patchwork-Id: 1940748 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=L+8qn0z5; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=8.43.85.97; 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 [8.43.85.97]) (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 4Vpk945Kffz20Q9 for ; Wed, 29 May 2024 06:08:24 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id DC7A438449C8 for ; Tue, 28 May 2024 20:08:22 +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 EB745384AB40 for ; Tue, 28 May 2024 20:07:49 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org EB745384AB40 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 EB745384AB40 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=1716926872; cv=none; b=D5T3lBvAWbof1t7lzSeDXekJZiUIf4WRGx4UsWXgXrR02na7RnYHp+86QmZWxBB3Ov6KlnFg0WD4ABcR1x2YtdG5Mv+we71HvuUmlqG004dSmXdNlAFIRs50VEl7gKRYtEHQ5TmYhFfI/D2oQHlyMIVeF291+MXzBPO/pbeK76A= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1716926872; c=relaxed/simple; bh=R9GTIE0g674GQikAOgH/g4WuRomBInbGzdNTvUCjpx8=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=RDdfzJbWKEayrXnzZ14tgYy4KzIchP75uub5ytZLety6qwjnIu6EyOebwvJC+hqAhsL69BqFUJYvK8Pzskszk80A6eiq/WGEvcV58Thx6hSH1GHxw5PmFwPECtw0si0YNemlkrZFZb1U50YwEtNaKWDTynheqI3mdltQ6DfR0Bs= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1716926869; 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: in-reply-to:in-reply-to:references:references; bh=GGjiwOqghK0yiznJF5nqNt/xBht5QWCQvGB8KUiMGYA=; b=L+8qn0z540ksSTQPgvdO3XPhpqjSXV0MfuoplB2pNb5DypLQmEzeVrA/Hb24fqZizXoTTC l6MFvCBRnjJptS2KnFJAgQ7skio3W7nMGyEzCDk7Wmfd5BZxRmh3rCdlAWYkIYZXGvRWnI Cij5fmGSZ33jyYnrDbuKp5O0D3O1D+0= 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-112-kojZ6c0wOOyNIiM8Ca7skg-1; Tue, 28 May 2024 16:07:48 -0400 X-MC-Unique: kojZ6c0wOOyNIiM8Ca7skg-1 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) (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 DC28A29AA386 for ; Tue, 28 May 2024 20:07:47 +0000 (UTC) Received: from t14s.localdomain.com (unknown [10.22.10.48]) by smtp.corp.redhat.com (Postfix) with ESMTP id A9BCD2026D68; Tue, 28 May 2024 20:07:47 +0000 (UTC) From: David Malcolm To: gcc-patches@gcc.gnu.org Cc: David Malcolm Subject: [pushed 2/3] libcpp: move label_text to its own header Date: Tue, 28 May 2024 16:07:40 -0400 Message-Id: <20240528200741.3791987-2-dmalcolm@redhat.com> In-Reply-To: <20240528200741.3791987-1-dmalcolm@redhat.com> References: <20240528200741.3791987-1-dmalcolm@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.4 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-11.7 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, 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 No functional change intended. Successfully bootstrapped & regrtested on x86_64-pc-linux-gnu. Pushed to trunk as r15-874-g9bda2c4c81b668. libcpp/ChangeLog: * Makefile.in (TAGS_SOURCES): Add include/label-text.h. * include/label-text.h: New file. * include/rich-location.h: Include "label-text.h". (class label_text): Move to label-text.h. Signed-off-by: David Malcolm --- libcpp/Makefile.in | 2 +- libcpp/include/label-text.h | 102 +++++++++++++++++++++++++++++++++ libcpp/include/rich-location.h | 79 +------------------------ 3 files changed, 105 insertions(+), 78 deletions(-) create mode 100644 libcpp/include/label-text.h diff --git a/libcpp/Makefile.in b/libcpp/Makefile.in index ebbca37777fb..7e47153264c0 100644 --- a/libcpp/Makefile.in +++ b/libcpp/Makefile.in @@ -271,7 +271,7 @@ ETAGS = @ETAGS@ TAGS_SOURCES = $(libcpp_a_SOURCES) internal.h system.h ucnid.h \ include/cpplib.h include/line-map.h include/mkdeps.h include/symtab.h \ - include/rich-location.h + include/rich-location.h include/label-text.h TAGS: $(TAGS_SOURCES) diff --git a/libcpp/include/label-text.h b/libcpp/include/label-text.h new file mode 100644 index 000000000000..13562cda41f9 --- /dev/null +++ b/libcpp/include/label-text.h @@ -0,0 +1,102 @@ +/* A very simple string class. + Copyright (C) 2015-2024 Free Software Foundation, Inc. + +This program is free software; you can redistribute it and/or modify it +under the terms of the GNU General Public License as published by the +Free Software Foundation; either version 3, or (at your option) any +later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; see the file COPYING3. If not see +. + + In other words, you are welcome to use, share and improve this program. + You are forbidden to forbid anyone else to use, share and improve + what you give them. Help stamp out software-hoarding! */ + +#ifndef LIBCPP_LABEL_TEXT_H +#define LIBCPP_LABEL_TEXT_H + +/* A struct for the result of range_label::get_text: a NUL-terminated buffer + of localized text, and a flag to determine if the caller should "free" the + buffer. */ + +class label_text +{ +public: + label_text () + : m_buffer (NULL), m_owned (false) + {} + + ~label_text () + { + if (m_owned) + free (m_buffer); + } + + /* Move ctor. */ + label_text (label_text &&other) + : m_buffer (other.m_buffer), m_owned (other.m_owned) + { + other.release (); + } + + /* Move assignment. */ + label_text & operator= (label_text &&other) + { + if (m_owned) + free (m_buffer); + m_buffer = other.m_buffer; + m_owned = other.m_owned; + other.release (); + return *this; + } + + /* Delete the copy ctor and copy-assignment operator. */ + label_text (const label_text &) = delete; + label_text & operator= (const label_text &) = delete; + + /* Create a label_text instance that borrows BUFFER from a + longer-lived owner. */ + static label_text borrow (const char *buffer) + { + return label_text (const_cast (buffer), false); + } + + /* Create a label_text instance that takes ownership of BUFFER. */ + static label_text take (char *buffer) + { + return label_text (buffer, true); + } + + void release () + { + m_buffer = NULL; + m_owned = false; + } + + const char *get () const + { + return m_buffer; + } + + bool is_owner () const + { + return m_owned; + } + +private: + char *m_buffer; + bool m_owned; + + label_text (char *buffer, bool owned) + : m_buffer (buffer), m_owned (owned) + {} +}; + +#endif /* !LIBCPP_LABEL_TEXT_H */ diff --git a/libcpp/include/rich-location.h b/libcpp/include/rich-location.h index a2ece8b033c0..be424cb4b65f 100644 --- a/libcpp/include/rich-location.h +++ b/libcpp/include/rich-location.h @@ -22,6 +22,8 @@ along with this program; see the file COPYING3. If not see #ifndef LIBCPP_RICH_LOCATION_H #define LIBCPP_RICH_LOCATION_H +#include "label-text.h" + class range_label; class label_effects; @@ -541,83 +543,6 @@ protected: const diagnostic_path *m_path; }; -/* A struct for the result of range_label::get_text: a NUL-terminated buffer - of localized text, and a flag to determine if the caller should "free" the - buffer. */ - -class label_text -{ -public: - label_text () - : m_buffer (NULL), m_owned (false) - {} - - ~label_text () - { - if (m_owned) - free (m_buffer); - } - - /* Move ctor. */ - label_text (label_text &&other) - : m_buffer (other.m_buffer), m_owned (other.m_owned) - { - other.release (); - } - - /* Move assignment. */ - label_text & operator= (label_text &&other) - { - if (m_owned) - free (m_buffer); - m_buffer = other.m_buffer; - m_owned = other.m_owned; - other.release (); - return *this; - } - - /* Delete the copy ctor and copy-assignment operator. */ - label_text (const label_text &) = delete; - label_text & operator= (const label_text &) = delete; - - /* Create a label_text instance that borrows BUFFER from a - longer-lived owner. */ - static label_text borrow (const char *buffer) - { - return label_text (const_cast (buffer), false); - } - - /* Create a label_text instance that takes ownership of BUFFER. */ - static label_text take (char *buffer) - { - return label_text (buffer, true); - } - - void release () - { - m_buffer = NULL; - m_owned = false; - } - - const char *get () const - { - return m_buffer; - } - - bool is_owner () const - { - return m_owned; - } - -private: - char *m_buffer; - bool m_owned; - - label_text (char *buffer, bool owned) - : m_buffer (buffer), m_owned (owned) - {} -}; - /* Abstract base class for labelling a range within a rich_location (e.g. for labelling expressions with their type). From patchwork Tue May 28 20:07:41 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Malcolm X-Patchwork-Id: 1940750 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=HE8wtKWU; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=8.43.85.97; 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 [8.43.85.97]) (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 4Vpk956BQQz20Q9 for ; Wed, 29 May 2024 06:08:25 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 14DFA386C5B3 for ; Tue, 28 May 2024 20:08:24 +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 76CA8384AB4C for ; Tue, 28 May 2024 20:07:50 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 76CA8384AB4C 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 76CA8384AB4C 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=1716926873; cv=none; b=efdmxNsctKYKJHpDvxU0ZOo2tYSm84avldtZAtbRAFshjl/+YnorIivmsbLEA9d4OuuL76mwPRfpRTdqlKTMa024xqCA7Nycp3CEFeBTJekENgSM+MXlDTgcHm+iYIAbz56waEIjP4ETxyRK0qK8+gPHbM9yiXNiLxfwKDyYk24= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1716926873; c=relaxed/simple; bh=eFMDDQo689QC/w/sFxEE92/ohlHnWi0BpcrL7AJny8c=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=LDczQyVYjqbrrB/AvONRehs1yzjK6PnmGx2nkvVekZEfWE0+zUKwRaY5QhX80Xi6jLRNrRuGJewJT9QSio5gbBLzQRGztNNG5IKG2jE4qWmvD2qovFb6cPTtH/dRx/vuNJGvQ2gO9tg0Oe2Pht1YiaBJcCO0ToIxVeqsgVz3Wng= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1716926870; 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: in-reply-to:in-reply-to:references:references; bh=gvhbA0As9mx34EA+XvuLLXQV48te2oEwbAV6kZq4zRM=; b=HE8wtKWUj9X/h4U2V8KcWdw9mrM07dk3QzULX82mu/S09OSnjQbcvf50X7q7PWXzH6WAUv weIHfbdO0v3t4Ij7IUGspW9mpHX2mqYZeAi671Ewg5NOwRAyxPB/fq3f9O8rgOvrI27Htq cWBVfjKcB9kP/A5GRFOOMmiOWPBle/o= 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-634-77OFE4S8Oju_NoKFjO_W-g-1; Tue, 28 May 2024 16:07:48 -0400 X-MC-Unique: 77OFE4S8Oju_NoKFjO_W-g-1 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) (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 2814D3C025BE for ; Tue, 28 May 2024 20:07:48 +0000 (UTC) Received: from t14s.localdomain.com (unknown [10.22.10.48]) by smtp.corp.redhat.com (Postfix) with ESMTP id EE4852026D6E; Tue, 28 May 2024 20:07:47 +0000 (UTC) From: David Malcolm To: gcc-patches@gcc.gnu.org Cc: David Malcolm Subject: [pushed 3/3] diagnostics: consolidate global state in diagnostic-color.cc Date: Tue, 28 May 2024 16:07:41 -0400 Message-Id: <20240528200741.3791987-3-dmalcolm@redhat.com> In-Reply-To: <20240528200741.3791987-1-dmalcolm@redhat.com> References: <20240528200741.3791987-1-dmalcolm@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.4 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-11.7 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 Simplify the table of default colors, avoiding the need to manually add the strlen of each entry. Consolidate the global state in diagnostic-color.cc into a g_color_dict, adding selftests for the new class diagnostic_color_dict. No functional change intended. Successfully bootstrapped & regrtested on x86_64-pc-linux-gnu. Tested with "make selftest-valgrind" and manually with various values for GCC_COLORS. Pushed to trunk as r15-875-g21fc89bac61983. gcc/ChangeLog: * diagnostic-color.cc: Define INCLUDE_VECTOR. Include "label-text.h" and "selftest.h". (struct color_cap): Replace with... (struct color_default): ...this, adding "m_" prefixes to fields and dropping "name_len" and "free_val" field. (color_dict): Convert to... (gcc_color_defaults): ...this, making const, dropping the trailing strlen and "false" from each entry. (class diagnostic_color_dict): New. (g_color_dict): New. (colorize_start): Reimplement in terms of g_color_dict. (diagnostic_color_dict::get_entry_by_name): New, based on colorize_start. (diagnostic_color_dict::get_start_by_name): Likewise. (diagnostic_color_dict::diagnostic_color_dict): New. (parse_gcc_colors): Reimplement, moving body... (diagnostic_color_dict::parse_envvar_value): ...here. (colorize_init): Lazily create g_color_dict. (selftest::test_empty_color_dict): New. (selftest::test_default_color_dict): New. (selftest::test_color_dict_envvar_parsing): New. (selftest::diagnostic_color_cc_tests): New. * selftest-run-tests.cc (selftest::run_tests): Call selftest::diagnostic_color_cc_tests. * selftest.h (selftest::diagnostic_color_cc_tests): New decl. Signed-off-by: David Malcolm --- gcc/diagnostic-color.cc | 277 +++++++++++++++++++++++++++++--------- gcc/selftest-run-tests.cc | 1 + gcc/selftest.h | 1 + 3 files changed, 216 insertions(+), 63 deletions(-) diff --git a/gcc/diagnostic-color.cc b/gcc/diagnostic-color.cc index f01a0fc2e377..cbe57ce763f2 100644 --- a/gcc/diagnostic-color.cc +++ b/gcc/diagnostic-color.cc @@ -17,9 +17,11 @@ 02110-1301, USA. */ #include "config.h" +#define INCLUDE_VECTOR #include "system.h" #include "diagnostic-color.h" #include "diagnostic-url.h" +#include "label-text.h" #ifdef __MINGW32__ # define WIN32_LEAN_AND_MEAN @@ -27,6 +29,7 @@ #endif #include "color-macros.h" +#include "selftest.h" /* The context and logic for choosing default --color screen attributes (foreground and background colors, etc.) are the following. @@ -72,56 +75,124 @@ counterparts) and possibly bold blue. */ /* Default colors. The user can overwrite them using environment variable GCC_COLORS. */ -struct color_cap +struct color_default { - const char *name; - const char *val; - unsigned char name_len; - bool free_val; + const char *m_name; + const char *m_val; }; /* For GCC_COLORS. */ -static struct color_cap color_dict[] = +static const color_default gcc_color_defaults[] = { - { "error", SGR_SEQ (COLOR_BOLD COLOR_SEPARATOR COLOR_FG_RED), 5, false }, - { "warning", SGR_SEQ (COLOR_BOLD COLOR_SEPARATOR COLOR_FG_MAGENTA), - 7, false }, - { "note", SGR_SEQ (COLOR_BOLD COLOR_SEPARATOR COLOR_FG_CYAN), 4, false }, - { "range1", SGR_SEQ (COLOR_FG_GREEN), 6, false }, - { "range2", SGR_SEQ (COLOR_FG_BLUE), 6, false }, - { "locus", SGR_SEQ (COLOR_BOLD), 5, false }, - { "quote", SGR_SEQ (COLOR_BOLD), 5, false }, - { "path", SGR_SEQ (COLOR_BOLD COLOR_SEPARATOR COLOR_FG_CYAN), 4, false }, - { "fnname", SGR_SEQ (COLOR_BOLD COLOR_SEPARATOR COLOR_FG_GREEN), 6, false }, - { "targs", SGR_SEQ (COLOR_FG_MAGENTA), 5, false }, - { "fixit-insert", SGR_SEQ (COLOR_FG_GREEN), 12, false }, - { "fixit-delete", SGR_SEQ (COLOR_FG_RED), 12, false }, - { "diff-filename", SGR_SEQ (COLOR_BOLD), 13, false }, - { "diff-hunk", SGR_SEQ (COLOR_FG_CYAN), 9, false }, - { "diff-delete", SGR_SEQ (COLOR_FG_RED), 11, false }, - { "diff-insert", SGR_SEQ (COLOR_FG_GREEN), 11, false }, - { "type-diff", SGR_SEQ (COLOR_BOLD COLOR_SEPARATOR COLOR_FG_GREEN), 9, false }, - { "valid", SGR_SEQ (COLOR_BOLD COLOR_SEPARATOR COLOR_FG_GREEN), 5, false }, - { "invalid", SGR_SEQ (COLOR_BOLD COLOR_SEPARATOR COLOR_FG_RED), 7, false }, - { NULL, NULL, 0, false } + { "error", SGR_SEQ (COLOR_BOLD COLOR_SEPARATOR COLOR_FG_RED) }, + { "warning", SGR_SEQ (COLOR_BOLD COLOR_SEPARATOR COLOR_FG_MAGENTA) }, + { "note", SGR_SEQ (COLOR_BOLD COLOR_SEPARATOR COLOR_FG_CYAN) }, + { "range1", SGR_SEQ (COLOR_FG_GREEN) }, + { "range2", SGR_SEQ (COLOR_FG_BLUE) }, + { "locus", SGR_SEQ (COLOR_BOLD) }, + { "quote", SGR_SEQ (COLOR_BOLD) }, + { "path", SGR_SEQ (COLOR_BOLD COLOR_SEPARATOR COLOR_FG_CYAN) }, + { "fnname", SGR_SEQ (COLOR_BOLD COLOR_SEPARATOR COLOR_FG_GREEN) }, + { "targs", SGR_SEQ (COLOR_FG_MAGENTA) }, + { "fixit-insert", SGR_SEQ (COLOR_FG_GREEN) }, + { "fixit-delete", SGR_SEQ (COLOR_FG_RED) }, + { "diff-filename", SGR_SEQ (COLOR_BOLD) }, + { "diff-hunk", SGR_SEQ (COLOR_FG_CYAN) }, + { "diff-delete", SGR_SEQ (COLOR_FG_RED) }, + { "diff-insert", SGR_SEQ (COLOR_FG_GREEN) }, + { "type-diff", SGR_SEQ (COLOR_BOLD COLOR_SEPARATOR COLOR_FG_GREEN) }, + { "valid", SGR_SEQ (COLOR_BOLD COLOR_SEPARATOR COLOR_FG_GREEN) }, + { "invalid", SGR_SEQ (COLOR_BOLD COLOR_SEPARATOR COLOR_FG_RED) } }; +class diagnostic_color_dict +{ +public: + diagnostic_color_dict (const color_default *default_values, + size_t num_default_values); + + bool parse_envvar_value (const char *const envvar_value); + + const char *get_start_by_name (const char *name, size_t name_len) const; + const char *get_start_by_name (const char *name) const + { + return get_start_by_name (name, strlen (name)); + } + +private: + struct entry + { + entry (const color_default &d) + : m_name (d.m_name), + m_name_len (strlen (d.m_name)), + m_val (label_text::borrow (d.m_val)) + { + } + + const char *m_name; + size_t m_name_len; + label_text m_val; + }; + + const entry *get_entry_by_name (const char *name, size_t name_len) const; + entry *get_entry_by_name (const char *name, size_t name_len); + + std::vector m_entries; +}; + +static diagnostic_color_dict *g_color_dict; + const char * colorize_start (bool show_color, const char *name, size_t name_len) { - struct color_cap const *cap; - if (!show_color) return ""; - for (cap = color_dict; cap->name; cap++) - if (cap->name_len == name_len - && memcmp (cap->name, name, name_len) == 0) - break; - if (cap->name == NULL) + if (!g_color_dict) return ""; - return cap->val; + return g_color_dict->get_start_by_name (name, name_len); +} + +/* Look for an entry named NAME of length NAME_LEN within this + diagnostic_color_dict, or nullptr if there isn't one. */ + +const diagnostic_color_dict::entry * +diagnostic_color_dict::get_entry_by_name (const char *name, + size_t name_len) const +{ + for (auto &iter : m_entries) + if (iter.m_name_len == name_len + && memcmp (iter.m_name, name, name_len) == 0) + return &iter; + return nullptr; +} + +/* Non-const version of the above. */ + +diagnostic_color_dict::entry * +diagnostic_color_dict::get_entry_by_name (const char *name, + size_t name_len) +{ + for (auto &iter : m_entries) + if (iter.m_name_len == name_len + && memcmp (iter.m_name, name, name_len) == 0) + return &iter; + return nullptr; +} + +/* Return the SGR codes to start a color entry named NAME of length + NAME_LEN within this diagnostic_color_dict, or the empty string if + there isn't one. */ + +const char * +diagnostic_color_dict::get_start_by_name (const char *name, + size_t name_len) const +{ + if (const entry *e = get_entry_by_name (name, name_len)) + return e->m_val.get (); + + return ""; } const char * @@ -130,56 +201,58 @@ colorize_stop (bool show_color) return show_color ? SGR_RESET : ""; } -/* Parse GCC_COLORS. The default would look like: - GCC_COLORS='error=01;31:warning=01;35:note=01;36:\ - range1=32:range2=34:locus=01:quote=01:path=01;36:\ - fixit-insert=32:fixit-delete=31:'\ - diff-filename=01:diff-hunk=32:diff-delete=31:diff-insert=32:\ - type-diff=01;32' - No character escaping is needed or supported. */ -static bool -parse_gcc_colors (void) +/* diagnostic_color_dict's ctor. Initialize it from the given array + of color_default values. */ + +diagnostic_color_dict:: +diagnostic_color_dict (const color_default *default_values, + size_t num_default_values) { - const char *p, *q, *name, *val; - char *b; - size_t name_len = 0, val_len = 0; + m_entries.reserve (num_default_values); + for (size_t idx = 0; idx < num_default_values; idx++) + m_entries.push_back (entry (default_values[idx])); +} - p = getenv ("GCC_COLORS"); /* Plural! */ - if (p == NULL) +/* Parse a list of color definitions from an environment variable + value (such as that of GCC_COLORS). + No character escaping is needed or supported. */ + +bool +diagnostic_color_dict::parse_envvar_value (const char *const envvar_value) +{ + /* envvar not set: use the default colors. */ + if (envvar_value == nullptr) return true; - if (*p == '\0') + + /* envvar set to empty string: disable colorization. */ + if (*envvar_value == '\0') return false; - name = q = p; + const char *q, *name, *val; + size_t name_len = 0, val_len = 0; + + name = q = envvar_value; val = NULL; /* From now on, be well-formed or you're gone. */ for (;;) if (*q == ':' || *q == '\0') { - struct color_cap *cap; - if (val) val_len = q - val; else name_len = q - name; /* Empty name without val (empty cap) won't match and will be ignored. */ - for (cap = color_dict; cap->name; cap++) - if (cap->name_len == name_len - && memcmp (cap->name, name, name_len) == 0) - break; + entry *e = get_entry_by_name (name, name_len); /* If name unknown, go on for forward compatibility. */ - if (cap->val && val) + if (e && val) { - if (cap->free_val) - free (CONST_CAST (char *, cap->val)); - b = XNEWVEC (char, val_len + sizeof (SGR_SEQ (""))); + char *b = XNEWVEC (char, val_len + sizeof (SGR_SEQ (""))); memcpy (b, SGR_START, strlen (SGR_START)); memcpy (b + strlen (SGR_START), val, val_len); memcpy (b + strlen (SGR_START) + val_len, SGR_END, sizeof (SGR_END)); - cap->val = (const char *) b; - cap->free_val = true; + e->m_val = label_text::take (b); } if (*q == '\0') return true; @@ -203,6 +276,20 @@ parse_gcc_colors (void) return true; } +/* Parse GCC_COLORS. The default would look like: + GCC_COLORS='error=01;31:warning=01;35:note=01;36:\ + range1=32:range2=34:locus=01:quote=01:path=01;36:\ + fixit-insert=32:fixit-delete=31:'\ + diff-filename=01:diff-hunk=32:diff-delete=31:diff-insert=32:\ + type-diff=01;32'. */ +static bool +parse_gcc_colors () +{ + if (!g_color_dict) + return false; + return g_color_dict->parse_envvar_value (getenv ("GCC_COLORS")); /* Plural! */ +} + /* Return true if we should use color when in auto mode, false otherwise. */ static bool should_colorize (void) @@ -229,6 +316,10 @@ should_colorize (void) bool colorize_init (diagnostic_color_rule_t rule) { + if (!g_color_dict) + g_color_dict = new diagnostic_color_dict (gcc_color_defaults, + ARRAY_SIZE (gcc_color_defaults)); + switch (rule) { case DIAGNOSTICS_COLOR_NO: @@ -351,3 +442,63 @@ determine_url_format (diagnostic_url_rule_t rule) gcc_unreachable (); } } + +#if CHECKING_P + +namespace selftest { + +/* Test of an empty diagnostic_color_dict. */ + +static void +test_empty_color_dict () +{ + diagnostic_color_dict d (nullptr, 0); + ASSERT_STREQ (d.get_start_by_name ("warning"), ""); + ASSERT_STREQ (d.get_start_by_name ("should-not-be-found"), ""); +} + +/* Test of a diagnostic_color_dict with GCC's defaults. */ + +static void +test_default_color_dict () +{ + diagnostic_color_dict d (gcc_color_defaults, + ARRAY_SIZE (gcc_color_defaults)); + ASSERT_STREQ (d.get_start_by_name ("warning"), + SGR_SEQ (COLOR_BOLD COLOR_SEPARATOR COLOR_FG_MAGENTA)); + ASSERT_STREQ (d.get_start_by_name ("should-not-be-found"), ""); +} + +/* Test of a diagnostic_color_dict with GCC's defaults plus overrides from + an environment variable. */ + +static void +test_color_dict_envvar_parsing () +{ + diagnostic_color_dict d (gcc_color_defaults, + ARRAY_SIZE (gcc_color_defaults)); + + d.parse_envvar_value ("error=01;37:warning=01;42:unknown-value=01;36"); + + ASSERT_STREQ (d.get_start_by_name ("error"), + SGR_SEQ ("01;37")); + ASSERT_STREQ (d.get_start_by_name ("warning"), + SGR_SEQ ("01;42")); + ASSERT_STREQ (d.get_start_by_name ("unknown-value"), ""); + ASSERT_STREQ (d.get_start_by_name ("should-not-be-found"), ""); +} + + +/* Run all of the selftests within this file. */ + +void +diagnostic_color_cc_tests () +{ + test_empty_color_dict (); + test_default_color_dict (); + test_color_dict_envvar_parsing (); +} + +} // namespace selftest + +#endif /* #if CHECKING_P */ diff --git a/gcc/selftest-run-tests.cc b/gcc/selftest-run-tests.cc index 1c99de1e6c2a..d8f5e4b34c68 100644 --- a/gcc/selftest-run-tests.cc +++ b/gcc/selftest-run-tests.cc @@ -94,6 +94,7 @@ selftest::run_tests () /* Higher-level tests, or for components that other selftests don't rely on. */ + diagnostic_color_cc_tests (); diagnostic_show_locus_cc_tests (); diagnostic_format_json_cc_tests (); edit_context_cc_tests (); diff --git a/gcc/selftest.h b/gcc/selftest.h index 808d432ec480..9e294ad1e5f9 100644 --- a/gcc/selftest.h +++ b/gcc/selftest.h @@ -220,6 +220,7 @@ extern void attribs_cc_tests (); extern void bitmap_cc_tests (); extern void cgraph_cc_tests (); extern void convert_cc_tests (); +extern void diagnostic_color_cc_tests (); extern void diagnostic_format_json_cc_tests (); extern void diagnostic_show_locus_cc_tests (); extern void digraph_cc_tests ();