From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: 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 220E938485BD for ; Wed, 18 May 2022 14:24:09 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 220E938485BD Received: from mail-qv1-f69.google.com (mail-qv1-f69.google.com [209.85.219.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-341-R-f_CRQlNNefq9z0CkK71A-1; Wed, 18 May 2022 10:24:07 -0400 X-MC-Unique: R-f_CRQlNNefq9z0CkK71A-1 Received: by mail-qv1-f69.google.com with SMTP id j2-20020a0cfd42000000b0045ad9cba5deso1729934qvs.5 for ; Wed, 18 May 2022 07:24:07 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:message-id:date:mime-version:user-agent:subject :content-language:to:cc:references:from:in-reply-to :content-transfer-encoding; bh=6TrADfOZRNwB8wnyROcxLhyHGNxncE0Du341LeFhwJg=; b=sBaOl7MdpjCJEZi7JZKK46jNCgEMKZFPfgTD3KNA9Ld1M+1yZznXNV79NFEGive8sC CvyQwx3Wc1Z4VNFLepthZs7j44G2m1gf3IhEjJDo/x/SDkDWyQhJivqVo+MH1PA7VwH9 JHvbodnj8Hkozm5AMThQK7TA3xCKVDH+ql9O4vNU3HbzF1EqgYoCKTweawlq2E5ZPzp9 cIN2PEAvh7FPZpW8i4x+zkJJ932V05I44pdDjVnPzGKIpGq4XZi3sENZ43GISp+O6ymO ybrH4lRIxVtCdJMy5atRa7IWQJ1PEBP7Hm2aGIfoZ5Y0ses7+PzzeRGrCzRUP3N3KLkv Vq4g== X-Gm-Message-State: AOAM530lr/D0Y7ulFRJtAamy2JC8yprajpnXRA7AdVuLnyeIG+B7Zj5T sSEd9PksorAiOHuZafEhk9daBQnx/TSzqBO0Oq9g+UWYtME64d6E9W9cPdTpbNdeU0VObfSrnDb NPoHFmm34Xp5bDB62BA== X-Received: by 2002:ac8:580f:0:b0:2f3:d23b:dcab with SMTP id g15-20020ac8580f000000b002f3d23bdcabmr24237199qtg.591.1652883846459; Wed, 18 May 2022 07:24:06 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzM++qRdVYitoji7YAFprwVNDNJLB74u1qEb26Pzbzm6L1fu9av7/2x7c8m7Tw5luyxpaEsuA== X-Received: by 2002:ac8:580f:0:b0:2f3:d23b:dcab with SMTP id g15-20020ac8580f000000b002f3d23bdcabmr24237168qtg.591.1652883846034; Wed, 18 May 2022 07:24:06 -0700 (PDT) Received: from [192.168.1.100] (130-44-159-43.s15913.c3-0.arl-cbr1.sbo-arl.ma.cable.rcncustomer.com. [130.44.159.43]) by smtp.gmail.com with ESMTPSA id 141-20020a370593000000b0069fc13ce202sm1502774qkf.51.2022.05.18.07.24.04 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 18 May 2022 07:24:04 -0700 (PDT) Message-ID: <419e9663-8193-5f39-5623-c16ecffc38a7@redhat.com> Date: Wed, 18 May 2022 10:24:03 -0400 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.9.0 Subject: Re: [PATCH v3] c, c++: -Wswitch warning on [[maybe_unused]] enumerator [PR105497] To: Marek Polacek Cc: GCC Patches , Joseph Myers References: <20220507221456.552767-1-polacek@redhat.com> <580b12c2-fa83-d268-373d-64bee758bf85@redhat.com> From: Jason Merrill In-Reply-To: X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Language: en-US Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-13.8 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, NICE_REPLY_A, RCVD_IN_DNSWL_LOW, 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.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 18 May 2022 14:24:10 -0000 On 5/17/22 19:55, Marek Polacek wrote: > On Tue, May 10, 2022 at 09:54:12AM -0400, Marek Polacek wrote: >> On Tue, May 10, 2022 at 08:58:46AM -0400, Jason Merrill wrote: >>> On 5/7/22 18:26, Marek Polacek wrote: >>>> Corrected version that avoids an uninitialized warning: >>>> >>>> This PR complains that we emit the "enumeration value not handled in >>>> switch" warning even though the enumerator was marked with the >>>> [[maybe_unused]] attribute. >>>> >>>> The first snag was that I couldn't just check TREE_USED, because >>>> the enumerator could have been used earlier in the function, which >>>> doesn't play well with the c_do_switch_warnings warning. Instead, >>>> I had to check the attributes on the CONST_DECL directly, which led >>>> to the second, and worse, snag: in C we don't have direct access to >>>> the CONST_DECL for the enumerator. >>> >>> I wonder if you want to change that instead of working around it? >> >> I wouldn't mind looking into that; I've hit this discrepancy numerous >> times throughout the years and it'd be good to unify it so that the >> c-common code doesn't need to hack around it. >> >> Let's see how far I'll get... > > Now done (r13-575), which makes this patch a piece of cake. > > Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk? OK. > -- >8 -- > This PR complains that we emit the "enumeration value not handled in > switch" warning even though the enumerator was marked with the > [[maybe_unused]] attribute. > > I couldn't just check TREE_USED, because the enumerator could have been > used earlier in the function, which doesn't play well with the > c_do_switch_warnings warning. Instead, I had to check the attributes on > the CONST_DECL. This is easy since the TYPE_VALUES of an enum type are > now consistent between C and C++, both of which store the CONST_DECL in > its TREE_VALUE. > > PR c++/105497 > > gcc/c-family/ChangeLog: > > * c-warn.cc (c_do_switch_warnings): Don't warn about unhandled > enumerator when it was marked with attribute unused. > > gcc/testsuite/ChangeLog: > > * c-c++-common/Wswitch-1.c: New test. > * g++.dg/warn/Wswitch-4.C: New test. > --- > gcc/c-family/c-warn.cc | 11 +++++- > gcc/testsuite/c-c++-common/Wswitch-1.c | 29 ++++++++++++++ > gcc/testsuite/g++.dg/warn/Wswitch-4.C | 52 ++++++++++++++++++++++++++ > 3 files changed, 90 insertions(+), 2 deletions(-) > create mode 100644 gcc/testsuite/c-c++-common/Wswitch-1.c > create mode 100644 gcc/testsuite/g++.dg/warn/Wswitch-4.C > > diff --git a/gcc/c-family/c-warn.cc b/gcc/c-family/c-warn.cc > index cae89294aea..ea7335f3edf 100644 > --- a/gcc/c-family/c-warn.cc > +++ b/gcc/c-family/c-warn.cc > @@ -1738,8 +1738,8 @@ c_do_switch_warnings (splay_tree cases, location_t switch_location, > for (chain = TYPE_VALUES (type); chain; chain = TREE_CHAIN (chain)) > { > tree value = TREE_VALUE (chain); > - if (TREE_CODE (value) == CONST_DECL) > - value = DECL_INITIAL (value); > + tree attrs = DECL_ATTRIBUTES (value); > + value = DECL_INITIAL (value); > node = splay_tree_lookup (cases, (splay_tree_key) value); > if (node) > { > @@ -1769,6 +1769,13 @@ c_do_switch_warnings (splay_tree cases, location_t switch_location, > /* We've now determined that this enumerated literal isn't > handled by the case labels of the switch statement. */ > > + /* Don't warn if the enumerator was marked as unused. We can't use > + TREE_USED here: it could have been set on the enumerator if the > + enumerator was used earlier. */ > + if (lookup_attribute ("unused", attrs) > + || lookup_attribute ("maybe_unused", attrs)) > + continue; > + > /* If the switch expression is a constant, we only really care > about whether that constant is handled by the switch. */ > if (cond && tree_int_cst_compare (cond, value)) > diff --git a/gcc/testsuite/c-c++-common/Wswitch-1.c b/gcc/testsuite/c-c++-common/Wswitch-1.c > new file mode 100644 > index 00000000000..de9ee03b0a3 > --- /dev/null > +++ b/gcc/testsuite/c-c++-common/Wswitch-1.c > @@ -0,0 +1,29 @@ > +/* PR c++/105497 */ > +/* { dg-options "-Wswitch" } */ > + > +enum E { > + A, > + B, > + C __attribute((unused)), > + D > +}; > + > +void > +g (enum E e) > +{ > + switch (e) > + { > + case A: > + case B: > + case D: > + break; > + } > + > + switch (e) // { dg-warning "not handled in switch" } > + { > + case A: > + case B: > + case C: > + break; > + } > +} > diff --git a/gcc/testsuite/g++.dg/warn/Wswitch-4.C b/gcc/testsuite/g++.dg/warn/Wswitch-4.C > new file mode 100644 > index 00000000000..553a57d777b > --- /dev/null > +++ b/gcc/testsuite/g++.dg/warn/Wswitch-4.C > @@ -0,0 +1,52 @@ > +// PR c++/105497 > +// { dg-do compile { target c++11 } } > +// { dg-options "-Wswitch" } > + > +enum class Button > +{ > + Left, > + Right, > + Middle, > + NumberOfButtons [[maybe_unused]] > +}; > + > +enum class Sound > +{ > + Bark, > + Meow, > + Hiss, > + Moo __attribute((unused)) > +}; > + > +enum class Chordata > +{ > + Urochordata, > + Cephalochordata, > + Vertebrata > +}; > + > +int main() > +{ > + Button b = Button::Left; > + switch (b) { // { dg-bogus "not handled" } > + case Button::Left: > + case Button::Right: > + case Button::Middle: > + break; > + } > + > + Sound s = Sound::Bark; > + switch (s) { // { dg-bogus "not handled" } > + case Sound::Bark: > + case Sound::Meow: > + case Sound::Hiss: > + break; > + } > + > + Chordata c = Chordata::Vertebrata; > + switch (c) { // { dg-warning "not handled" } > + case Chordata::Cephalochordata: > + case Chordata::Vertebrata: > + break; > + } > +} > > base-commit: 1bfb823e2a7346ef55bd53a5354770599f7a550b