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: 192349 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:e09d:b0:103:945f:af90 with SMTP id gm29csp382924dyb; Thu, 25 Jan 2024 17:39:23 -0800 (PST) X-Google-Smtp-Source: AGHT+IH+P/FOsJfHtJJr4FKJsKBJZSgrsHHRsQGSHPUjLy3mvCVRmN3J7dyj0yoJlhsGYT4F0/E9 X-Received: by 2002:a05:622a:507:b0:42a:2e27:2b8d with SMTP id l7-20020a05622a050700b0042a2e272b8dmr523890qtx.73.1706233163163; Thu, 25 Jan 2024 17:39:23 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706233163; cv=pass; d=google.com; s=arc-20160816; b=aJF77t+ZXY400ZijhdqG6m0YQMWEyr/T8Lwsceo0SoRUZcL++hHgGHK1ogetSfQgkG IJ0LSb55//6CKBpwy1uf4it68eB2Qfg1lK6kCGMEVQ186ZmfTdX5YlA3FKYlxzAqXrUM W7+OBNJOyJ7aMSVrC6UUXpP6XGJD1uP+zeNG6nflTKdCSQlEA+fWoFMfm4BO+bpzRbyk RVah5gUw1KQZ3zntQyiXPOMhlmawkz3nmoqVvtVpd4TsE4pD7EBsrSR2jEZ1CsPHNF5e HN95bQqww73g1Tlud6RoHZaxbx6M6nu2ug3lq4da6sMimfsdeuINRWcph/N0K09FrD6M 6jFQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:message-id:date:subject:to:from:dkim-signature :arc-filter:dmarc-filter:delivered-to; bh=4MHopF4xwZHqjOQzR5TiiYRzvAd9Yhmwg6DgQ90b9MU=; fh=2BFo3VQCimnyWTJsWe6sNdepvNrW+J/2ctoM6N7MZ0Y=; b=XVSg+moETqZIYEA2OIw17rcPObDXmznaUYjPRyDXlpoKC7tA4zbopvJtbAumKZ2Ce0 X2+x39Wozb2Tx34BuLxgjFBEvSSaVNUfx6wnf6H/56KcWb/i7JSuGYuuHb+ki9CfTt0s il6KeBNy8UHE25MZb6/SzweZ32b3bon0n5/NZNjeFAU+0o/gH0xvFCY4AblxwJD8tIWK Dczl5lbBpfGttHXZ86iFXIZzvhIPkSbyGHSxtoPGG6NchQ7p4amhRJbXHOXjiCj0UK6e qcTgSqAiuNectN1jwcf6chJfo5dJdrXg/rJMfZqGiCUgst7sRu9pWcYP1br5ySdX5diu OlQw== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b="OQK8T//+"; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from server2.sourceware.org (server2.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id o13-20020a05622a008d00b0042a266b19dasi147221qtw.299.2024.01.25.17.39.23 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 25 Jan 2024 17:39:23 -0800 (PST) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b="OQK8T//+"; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id DC4183858C35 for ; Fri, 26 Jan 2024 01:39: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.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+ouuuleilei=gmail.com@gcc.gnu.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1789115144923115571 X-GMAIL-MSGID: 1789115144923115571 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>(); +}