From patchwork Sat Jun 12 16:45:27 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Merrill X-Patchwork-Id: 1491310 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=8.43.85.97; helo=sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.a=rsa-sha256 header.s=default header.b=N1PRKH4A; dkim-atps=neutral Received: from sourceware.org (ip-8-43-85-97.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4G2Nrp6tmGz9sWl for ; Sun, 13 Jun 2021 02:46:18 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 9728C386100A for ; Sat, 12 Jun 2021 16:46:15 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 9728C386100A DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1623516375; bh=m65aefdt3BOFy6qGOvdm8VTuFVWRm445accMix/pglA=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:Cc:From; b=N1PRKH4AAJCkJf1zLnlTubWJaM98CtGsTKtAduyYzVY5IV2Dl52a620BKWPoiQH/D 3uHx8i8epBrV8pK5KOtNoNpGzrtm10tiy+i5Po5ZRpxQfP/2g+6IjRu12kophaDOqG +A/k4DHD3qKM1sWZ3E5Sxo0Zu7Q0zdVFC94vCiIQ= 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 ESMTP id ED78B3857C52 for ; Sat, 12 Jun 2021 16:45:32 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org ED78B3857C52 Received: from mail-qt1-f200.google.com (mail-qt1-f200.google.com [209.85.160.200]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-574-l8FadPbrNRyC6ivg-D2Y2w-1; Sat, 12 Jun 2021 12:45:31 -0400 X-MC-Unique: l8FadPbrNRyC6ivg-D2Y2w-1 Received: by mail-qt1-f200.google.com with SMTP id h24-20020ac856980000b0290243c83a3ddcso3967139qta.1 for ; Sat, 12 Jun 2021 09:45:31 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=m65aefdt3BOFy6qGOvdm8VTuFVWRm445accMix/pglA=; b=kysUOQ1lv0fKLS96mv3R6yZE6xPvoabWFzE3c5+2XnYayl82kxTuhML3CGQX27b0XE xj3xNuLe+XmCXWAwGSjfIWrncsoyjiJLrHJZBf/UVPzyKsW/PtH2Otk2R03w0eBK+hyJ qSIGxry3zIiFstJZJdc/SmA+7Prn4uxJDyrvPgG2YEz9f5/OBNMSVzYvOzVS6+UM+MR5 G7YCE8CWGiuEI7jLVyRPHO7vnus/+l36lhDF6hxoAnpI9y46SqrqrNs3irQ/zOre1dZM 1jojKTv3yDnaUGfafSMIt4g04Tg89uJdtA7SN3tnZwKQHRGk51/OXB5bI9ydtTEBztG0 S8Zw== X-Gm-Message-State: AOAM533UPjwjnN5r+XBG0g9t/6Cqn1FxX3/YTnzD+w9kzQuSNCcqqVLw fuwF3EKjc7FsgG/OfHiC6vpyaBIFP6Zkif8pBdeNoi9ZWRYDCHub4Tc17x+5QEI7Br2vjSFX1gS BgYBHvLilsY8H2fihkchNYqtYU4pb8nIpCqIXqwHGuipDWfjHYLU7KNC6/gwH5zO70w== X-Received: by 2002:a05:620a:2118:: with SMTP id l24mr1769371qkl.153.1623516330888; Sat, 12 Jun 2021 09:45:30 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyqfoHr26fucB46/VF3O8YmAX/HAfcBFM4nJQ6b4zioqbm57wVihmA+YXs4i3AP3a8vgRUosQ== X-Received: by 2002:a05:620a:2118:: with SMTP id l24mr1769355qkl.153.1623516330609; Sat, 12 Jun 2021 09:45:30 -0700 (PDT) Received: from barrymore.redhat.com ([130.44.159.43]) by smtp.gmail.com with ESMTPSA id l25sm6427162qtr.32.2021.06.12.09.45.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 12 Jun 2021 09:45:30 -0700 (PDT) To: gcc-patches@gcc.gnu.org Subject: [pushed] c-family: don't warn for [[maybe_unused]] on data member Date: Sat, 12 Jun 2021 12:45:27 -0400 Message-Id: <20210612164527.1616742-1-jason@redhat.com> X-Mailer: git-send-email 2.27.0 MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-13.2 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_LOW, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Jason Merrill via Gcc-patches From: Jason Merrill Reply-To: Jason Merrill Cc: Marek Polacek , Joseph Myers Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org Sender: "Gcc-patches" The C++17 standard (and C2x) says that [[maybe_unused]] may be applied to a non-static data member, so we shouldn't warn about it. And I don't see a reason not to handle a FIELD_DECL the same as any other decl, by setting TREE_USED on it. It doesn't look like anything yet cares about that flag on a FIELD_DECL, but setting it shouldn't hurt. Tested x86_64-pc-linux-gnu, applying to trunk. gcc/c-family/ChangeLog: * c-attribs.c (handle_unused_attribute): Handle FIELD_DECL. gcc/testsuite/ChangeLog: * g++.dg/ext/attrib62.C: No longer warn. * g++.dg/diagnostic/maybe_unused1.C: New test. --- gcc/doc/extend.texi | 6 +++--- gcc/c-family/c-attribs.c | 1 + gcc/testsuite/g++.dg/diagnostic/maybe_unused1.C | 17 +++++++++++++++++ gcc/testsuite/g++.dg/ext/attrib62.C | 2 +- 4 files changed, 22 insertions(+), 4 deletions(-) create mode 100644 gcc/testsuite/g++.dg/diagnostic/maybe_unused1.C base-commit: 08e1ff9d6e5a419d5b4a60c077df549e81601d9b diff --git a/gcc/doc/extend.texi b/gcc/doc/extend.texi index 408979b78af..8fc66d626d8 100644 --- a/gcc/doc/extend.texi +++ b/gcc/doc/extend.texi @@ -7516,9 +7516,9 @@ Not all targets support this attribute. @item unused @cindex @code{unused} variable attribute -This attribute, attached to a variable, means that the variable is meant -to be possibly unused. GCC does not produce a warning for this -variable. +This attribute, attached to a variable or structure field, means that +the variable or field is meant to be possibly unused. GCC does not +produce a warning for this variable or field. @item used @cindex @code{used} variable attribute diff --git a/gcc/c-family/c-attribs.c b/gcc/c-family/c-attribs.c index 42026a811dd..6bf492afcc0 100644 --- a/gcc/c-family/c-attribs.c +++ b/gcc/c-family/c-attribs.c @@ -1568,6 +1568,7 @@ handle_unused_attribute (tree *node, tree name, tree ARG_UNUSED (args), || VAR_OR_FUNCTION_DECL_P (decl) || TREE_CODE (decl) == LABEL_DECL || TREE_CODE (decl) == CONST_DECL + || TREE_CODE (decl) == FIELD_DECL || TREE_CODE (decl) == TYPE_DECL) { TREE_USED (decl) = 1; diff --git a/gcc/testsuite/g++.dg/diagnostic/maybe_unused1.C b/gcc/testsuite/g++.dg/diagnostic/maybe_unused1.C new file mode 100644 index 00000000000..70a8ec937dd --- /dev/null +++ b/gcc/testsuite/g++.dg/diagnostic/maybe_unused1.C @@ -0,0 +1,17 @@ +/* [dcl.attr.unused] The attribute may be applied to the declaration of a + class, a typedef-name, a variable (including a structured binding + declaration), a non-static data member, a function, an enumeration, or an + enumerator. */ +// { dg-do compile { target c++11 } } +// { dg-additional-options "-Wunused -Wextra" } + +class [[maybe_unused]] Test { + [[maybe_unused]] int a_; + void b() {}; +}; + +[[maybe_unused]] typedef Test Test2; + +[[maybe_unused]] int i; +[[maybe_unused]] void f(); +enum [[maybe_unused]] E { e [[maybe_unused]] = 42 }; diff --git a/gcc/testsuite/g++.dg/ext/attrib62.C b/gcc/testsuite/g++.dg/ext/attrib62.C index 116ee829a2d..d34cd2549de 100644 --- a/gcc/testsuite/g++.dg/ext/attrib62.C +++ b/gcc/testsuite/g++.dg/ext/attrib62.C @@ -3,5 +3,5 @@ template struct A { - T a, __attribute((unused)) b; // { dg-warning "attribute ignored" } + T a, __attribute((unused)) b; };