From patchwork Tue May 17 23:55:37 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marek Polacek X-Patchwork-Id: 54116 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id CA9FA3857407 for ; Tue, 17 May 2022 23:56:12 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org CA9FA3857407 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1652831772; bh=TC5wI7TInm3QPSW98uwhvWPb2mKrLbkSA+NICbpts9w=; h=Date:To:Subject:References:In-Reply-To:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc: From; b=ssyx3dwM5KJzPc191IxgoPbXZmqRqAay2e/pWfOlQX4cojVNerFtsdKWaNXbHNCrx 7xCeqZoLjIdUWzZZlfwikqxUxDvUNYZN1DOoTCdJu1wspPg8ZJfobPYWAJRSmKIGs4 YmclNL4mXM7Le6OZwsN3pwkuB/fvVBp+VZCA5v5w= 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 C5C263858406 for ; Tue, 17 May 2022 23:55:42 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org C5C263858406 Received: from mail-qt1-f200.google.com (mail-qt1-f200.google.com [209.85.160.200]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-277-cRoIllmGN2OmmGZsRbgDcA-1; Tue, 17 May 2022 19:55:41 -0400 X-MC-Unique: cRoIllmGN2OmmGZsRbgDcA-1 Received: by mail-qt1-f200.google.com with SMTP id h5-20020ac81385000000b002f3f91834ffso522121qtj.10 for ; Tue, 17 May 2022 16:55:41 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to:user-agent; bh=TC5wI7TInm3QPSW98uwhvWPb2mKrLbkSA+NICbpts9w=; b=PrY/aRq3OfLDrwbJsON77WXuPFwVeAskseFuaZSI7b5oqdkwE++7yxHLddNxPA2Cp/ hIfhe0AwDv86QjHAJMs/1fyIaAAoIEiecLa4OdNiyoT+RVXaZwN6d2bKO2ObyYrcKzgo 33kV7yBNBVT+RBeBR0dVQfm0Y+HMhwimK01hZDd4GgCGRumbHhr3xbCYNKUp3Ykxz517 x5RBT2Ka6zJz7BzEgwiZJOY+sEtfgXRX4oA9z4fmw3WOmiZVUzHNTihZ4OMEG8GUtwgs l88UZnS28r1CF8plQgsMJmiM0qvT3IdJzKdOXATkdCUF+qujQ3yzbb2qzwuLMQEXxtje Pypg== X-Gm-Message-State: AOAM530wmn3XeSI1OTsBjoFHwTwXsoOikelaWzAPT7M7BvP/Wk9kzwtL pngRPVKS6ijMp3i5ILQY/9fyJZBhke+VyjWiTKAHHzG3HI78WPDEcQ9Fis9zI1RClSCgdNIk+ja OxNHGQYCS3Msw87snag== X-Received: by 2002:ae9:ef8f:0:b0:6a3:1288:43d1 with SMTP id d137-20020ae9ef8f000000b006a3128843d1mr3023191qkg.623.1652831740304; Tue, 17 May 2022 16:55:40 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwDp1tSP7ymF3ThyVe9j/A6d+Z7kQ5f+JS+IigeNonyoYR9WInbvH8kS/y7Vc0ZSDkdiC6FTw== X-Received: by 2002:ae9:ef8f:0:b0:6a3:1288:43d1 with SMTP id d137-20020ae9ef8f000000b006a3128843d1mr3023181qkg.623.1652831739967; Tue, 17 May 2022 16:55:39 -0700 (PDT) Received: from redhat.com ([2601:184:4780:4310::e811]) by smtp.gmail.com with ESMTPSA id z26-20020ac83e1a000000b002f39b99f6bbsm210597qtf.85.2022.05.17.16.55.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 17 May 2022 16:55:39 -0700 (PDT) Date: Tue, 17 May 2022 19:55:37 -0400 To: Jason Merrill Subject: [PATCH v3] c, c++: -Wswitch warning on [[maybe_unused]] enumerator [PR105497] Message-ID: References: <20220507221456.552767-1-polacek@redhat.com> <580b12c2-fa83-d268-373d-64bee758bf85@redhat.com> MIME-Version: 1.0 In-Reply-To: User-Agent: Mutt/2.2.1 (2022-02-19) X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Disposition: inline X-Spam-Status: No, score=-12.6 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, 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-Patchwork-Original-From: Marek Polacek via Gcc-patches From: Marek Polacek Reply-To: Marek Polacek Cc: GCC Patches , Joseph Myers Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" 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? -- >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 base-commit: 1bfb823e2a7346ef55bd53a5354770599f7a550b 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; + } +}