From patchwork Fri Jan 26 01:38:17 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marek Polacek X-Patchwork-Id: 1891082 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=OQK8T//+; 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 4TLgMq4xyGz23fD for ; Fri, 26 Jan 2024 12:39:03 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id AE6C3385841D for ; Fri, 26 Jan 2024 01:39:01 +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 436E23858D37 for ; Fri, 26 Jan 2024 01:38:27 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 436E23858D37 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 436E23858D37 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=1706233109; cv=none; b=XBkQnsYOkSypud0twHhHWkYlN8w2c0HeLyyiuk7mW01hBvyEntE2Nf0nYrnvXBDcW3oPSnkFMvykQm6A/w3a96/0nQRvi+SZBsTYST5/O00jRVDR/gX35xMSa+vwKRlPkOGioo+KWbQ82ltL48ZUmPf6kmwNglXtgbw2KYPC1U8= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1706233109; c=relaxed/simple; bh=5A7Zm75vcsVhwK0xqdsWotaLTe2IlM6dZX1WI7h8Ji8=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=NwuD2/k/aeDx+sKXKAE4MgO1evHeavNIPMKbH3ikv5LL5KVqjtuKzPRaVkibC17rtPvEpQ843mdhpwKwgZOJkDSRD3vuZFC8iBjzVKwR+3JbnJdwH8mBnlN7Rik4o/Mp2CpYg8r3r9H5EhBhv2OuLM3DSM+O2+C/wznQ2VyUII8= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1706233106; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=4MHopF4xwZHqjOQzR5TiiYRzvAd9Yhmwg6DgQ90b9MU=; b=OQK8T//+oIjcp/3RwAFR3PFDr6uZf8WNnJ2f7oYgMNSuqRK7JBpi8fc7n1ZDIwWAj2Li4G MwjTMz4KoaNNfWdWoAKOWfnFVM4WJU+VSBAYLq/R4nigB75zD2S5In0o05DPC0Awz8XlKy G0llRbX8GADf/OXkM+yjsLbvXC+gBh0= 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-357-Pls42aWjOoyXh0WRde0HeA-1; Thu, 25 Jan 2024 20:38:23 -0500 X-MC-Unique: Pls42aWjOoyXh0WRde0HeA-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 918CF845DC1 for ; Fri, 26 Jan 2024 01:38:23 +0000 (UTC) Received: from pdp-11.redhat.com (unknown [10.22.16.209]) by smtp.corp.redhat.com (Postfix) with ESMTP id 1A6E92166B32; Fri, 26 Jan 2024 01:38:23 +0000 (UTC) From: Marek Polacek To: GCC Patches , Jason Merrill Subject: [PATCH] c++: #pragma doesn't disable -Wunused-label [PR113582] Date: Thu, 25 Jan 2024 20:38:17 -0500 Message-ID: <20240126013817.70203-1-polacek@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=-12.8 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_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 Low prio and not a regression. Feel free to ignore till GCC 15. Bootstrapped/regtested on x86_64-pc-linux-gnu. -- >8 -- The PR complains that void do_something(){ #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wunused-label" start:; #pragma GCC diagnostic pop } #1 doesn't work. That's because we warn_for_unused_label only while we're in finish_function, meaning we're at #1 where we're outside the #pragma region. We can use suppress_warning + warning_suppressed_p to fix this. Note that I'm not using TREE_USED. Propagating it in tsubst_stmt/LABEL_EXPR from decl to label would mean that we don't warn in do_something2, but I think we want the warning there: we're in a template and the goto is a discarded statement. PR c++/113582 gcc/c-family/ChangeLog: * c-warn.cc (warn_for_unused_label): Don't warn if -Wunused-label has been suppressed for the label. gcc/cp/ChangeLog: * parser.cc (cp_parser_label_for_labeled_statement): suppress_warning if it's not enabled at input_location. * pt.cc (tsubst_stmt): Call copy_warning. gcc/testsuite/ChangeLog: * g++.dg/warn/Wunused-label-4.C: New test. --- gcc/c-family/c-warn.cc | 4 ++- gcc/cp/parser.cc | 6 ++++- gcc/cp/pt.cc | 9 ++++--- gcc/testsuite/g++.dg/warn/Wunused-label-4.C | 29 +++++++++++++++++++++ 4 files changed, 42 insertions(+), 6 deletions(-) create mode 100644 gcc/testsuite/g++.dg/warn/Wunused-label-4.C base-commit: f22a7ae8a96f7e5e330b12bd5045424619aa4926 diff --git a/gcc/c-family/c-warn.cc b/gcc/c-family/c-warn.cc index 8168696fa45..fdb5338f6f6 100644 --- a/gcc/c-family/c-warn.cc +++ b/gcc/c-family/c-warn.cc @@ -2186,7 +2186,9 @@ warn_for_unused_label (tree label) { if (!TREE_USED (label)) { - if (DECL_INITIAL (label)) + if (warning_suppressed_p (label, OPT_Wunused_label)) + /* Don't warn. */; + else if (DECL_INITIAL (label)) warning (OPT_Wunused_label, "label %q+D defined but not used", label); else warning (OPT_Wunused_label, "label %q+D declared but not defined", label); diff --git a/gcc/cp/parser.cc b/gcc/cp/parser.cc index 3748ccd49ff..224d47f2f90 100644 --- a/gcc/cp/parser.cc +++ b/gcc/cp/parser.cc @@ -13093,7 +13093,11 @@ cp_parser_label_for_labeled_statement (cp_parser* parser, tree attributes) /* Anything else must be an ordinary label. */ label = finish_label_stmt (cp_parser_identifier (parser)); if (label && TREE_CODE (label) == LABEL_DECL) - FALLTHROUGH_LABEL_P (label) = fallthrough_p; + { + FALLTHROUGH_LABEL_P (label) = fallthrough_p; + if (!warning_enabled_at (input_location, OPT_Wunused_label)) + suppress_warning (label, OPT_Wunused_label); + } break; } diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc index 74013533b0f..af9fd8f6f03 100644 --- a/gcc/cp/pt.cc +++ b/gcc/cp/pt.cc @@ -18796,11 +18796,12 @@ tsubst_stmt (tree t, tree args, tsubst_flags_t complain, tree in_decl) case LABEL_EXPR: { tree decl = LABEL_EXPR_LABEL (t); - tree label; - - label = finish_label_stmt (DECL_NAME (decl)); + tree label = finish_label_stmt (DECL_NAME (decl)); if (TREE_CODE (label) == LABEL_DECL) - FALLTHROUGH_LABEL_P (label) = FALLTHROUGH_LABEL_P (decl); + { + FALLTHROUGH_LABEL_P (label) = FALLTHROUGH_LABEL_P (decl); + copy_warning (label, decl); + } if (DECL_ATTRIBUTES (decl) != NULL_TREE) cplus_decl_attributes (&label, DECL_ATTRIBUTES (decl), 0); } diff --git a/gcc/testsuite/g++.dg/warn/Wunused-label-4.C b/gcc/testsuite/g++.dg/warn/Wunused-label-4.C new file mode 100644 index 00000000000..d194f043d21 --- /dev/null +++ b/gcc/testsuite/g++.dg/warn/Wunused-label-4.C @@ -0,0 +1,29 @@ +// PR c++/113582 +// { dg-do compile { target c++17 } } +// { dg-options "-Wunused-label" } + +template void +do_something () +{ +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wunused-label" +start: + if constexpr(B) + goto start; +#pragma GCC diagnostic pop +} + +template void +do_something2 () +{ +start: // { dg-warning "defined but not used" } + if constexpr(B) + goto start; +} + +void +g () +{ + do_something<0>(); + do_something2<0>(); +}