From patchwork Sat May 7 22:26:32 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marek Polacek X-Patchwork-Id: 1628017 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: bilbo.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=P7m/GHJG; dkim-atps=neutral Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Received: from 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 RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by bilbo.ozlabs.org (Postfix) with ESMTPS id 4Kwhr94PJ1z9sfG for ; Sun, 8 May 2022 08:27:03 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 0DBA13858036 for ; Sat, 7 May 2022 22:26:59 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 0DBA13858036 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1651962419; bh=xpaI8NXpX2651wYSBp+RRbGELVzZZWse1qqkDch0ycY=; h=Date:To:Subject:References:In-Reply-To:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=P7m/GHJGKcOPWleiSMafatBKLFxF34qwfAvPjpctEdqXmow7U8qOPNaW2Igw12Eni DPzxglkKhHGKhVEF184xNlyioWUyonhyb8985/N8aVyjy7Xa7IwuX8wIWGWfQ9tgdC Tv/+bLExuhaPIg9mQPPqCzG/d6jCn0cRup/3zRMg= 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 ESMTPS id 31EC03858D33 for ; Sat, 7 May 2022 22:26:38 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 31EC03858D33 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-302-msYMo2HEPVeJppAl3MCuOw-1; Sat, 07 May 2022 18:26:36 -0400 X-MC-Unique: msYMo2HEPVeJppAl3MCuOw-1 Received: by mail-qv1-f69.google.com with SMTP id s19-20020ad44b33000000b00456107e1120so8726005qvw.0 for ; Sat, 07 May 2022 15:26:36 -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:subject:message-id:references :mime-version:content-disposition:in-reply-to:user-agent; bh=xpaI8NXpX2651wYSBp+RRbGELVzZZWse1qqkDch0ycY=; b=oU30G30S9YD5K9+5uJoPm7uLhrYSNISMLxrtgZXSUKlaxDoBfDoDcRhDf6b4YsV6oC GI2khEm1xITubNkSVV6oCFFdmm5ATbr6ol8AaqBk959QtMpUrK9V2D5lY9ZZNxXvwinP paUEemReufJuV7NZUPXz0FGcC0pPdmis13w3zFoEloYzqCTQo1u1WFW0L8pwA79hs+8x i6Ugx8WoWk+jCCRHE+dlstojBrr3x1GRTsJWVcxluLQdBUM9cX/8OKFSPfdQQXi25RsB Ak7lrwoC5uLiJb8ZuIhoHl1KHLjgJV682GmObcFXRWrFpZrp+a6mdJgOcQk1HEjHGFcF j/cQ== X-Gm-Message-State: AOAM533zTQH8bbNBdLJEkV4GGPoXD/suDH9kWamgmVkx0xF/+pexX8gV +nEJXlWkgr0zgL6Yv6wTDofN8exGGJ22+ngXHzvX4TPziwf9aWV862v5KXmgk+rwHjuQxpcEayk cBUvZ8+YTgrEnGveeap9T6Uu+Z4l3D/+wKORuo9eEHAp/ANW4Sv6ETGT+nl+akJ8QEP82 X-Received: by 2002:a05:622a:3cc:b0:2f3:c6b1:4862 with SMTP id k12-20020a05622a03cc00b002f3c6b14862mr8639282qtx.629.1651962395711; Sat, 07 May 2022 15:26:35 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxJul0poK2n/wRxGk7Tg5aTs5AWrlRvHj+9YC8zO72w6M4xy2/Hp0xsnYMivpM8as51okGNBA== X-Received: by 2002:a05:622a:3cc:b0:2f3:c6b1:4862 with SMTP id k12-20020a05622a03cc00b002f3c6b14862mr8639263qtx.629.1651962395349; Sat, 07 May 2022 15:26:35 -0700 (PDT) Received: from redhat.com ([2601:184:4780:4310::17c5]) by smtp.gmail.com with ESMTPSA id v191-20020a3761c8000000b0069fc13ce1d6sm4546750qkb.7.2022.05.07.15.26.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 07 May 2022 15:26:34 -0700 (PDT) Date: Sat, 7 May 2022 18:26:32 -0400 To: GCC Patches , Jason Merrill , Joseph Myers Subject: [PATCH v2] c, c++: -Wswitch warning on [[maybe_unused]] enumerator [PR105497] Message-ID: References: <20220507221456.552767-1-polacek@redhat.com> MIME-Version: 1.0 In-Reply-To: <20220507221456.552767-1-polacek@redhat.com> User-Agent: Mutt/2.1.5 (2021-12-30) X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Disposition: inline X-Spam-Status: No, score=-23.1 required=5.0 tests=BAYES_00, DKIM_INVALID, DKIM_SIGNED, GIT_PATCH_0, KAM_DMARC_NONE, KAM_DMARC_STATUS, KAM_STOCKGEN, RCVD_IN_DNSWL_LOW, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) 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 Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org Sender: "Gcc-patches" 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 had to handle that by adding a new function that extracts the decl from an identifier's binding. Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk? PR c++/105497 gcc/c-family/ChangeLog: * c-common.h (get_decl_for_identifier): Declare. * c-warn.cc (c_do_switch_warnings): Don't warn about unhandled enumerator when it was marked as unused. gcc/c/ChangeLog: * c-decl.cc (get_decl_for_identifier): New. gcc/cp/ChangeLog: * tree.cc (get_decl_for_identifier): New. gcc/testsuite/ChangeLog: * c-c++-common/Wswitch-1.c: New test. * g++.dg/warn/Wswitch-4.C: New test. --- gcc/c-family/c-common.h | 1 + gcc/c-family/c-warn.cc | 24 ++++++++++-- gcc/c/c-decl.cc | 10 +++++ gcc/cp/tree.cc | 8 ++++ gcc/testsuite/c-c++-common/Wswitch-1.c | 29 ++++++++++++++ gcc/testsuite/g++.dg/warn/Wswitch-4.C | 52 ++++++++++++++++++++++++++ 6 files changed, 121 insertions(+), 3 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: 0c723bb4be2a67657828b692997855afcdc5d286 diff --git a/gcc/c-family/c-common.h b/gcc/c-family/c-common.h index f10be9bd67e..c4221089a18 100644 --- a/gcc/c-family/c-common.h +++ b/gcc/c-family/c-common.h @@ -831,6 +831,7 @@ extern tree (*make_fname_decl) (location_t, tree, int); /* In c-decl.cc and cp/tree.cc. FIXME. */ extern void c_register_addr_space (const char *str, addr_space_t as); +extern tree get_decl_for_identifier (tree); /* In c-common.cc. */ extern bool in_late_binary_op; diff --git a/gcc/c-family/c-warn.cc b/gcc/c-family/c-warn.cc index cae89294aea..03cbc0541b2 100644 --- a/gcc/c-family/c-warn.cc +++ b/gcc/c-family/c-warn.cc @@ -1738,8 +1738,23 @@ 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); + tree id = TREE_PURPOSE (chain); + tree attrs = NULL_TREE; + /* In C++, the TREE_VALUE is a CONST_DECL. */ if (TREE_CODE (value) == CONST_DECL) - value = DECL_INITIAL (value); + { + attrs = DECL_ATTRIBUTES (value); + value = DECL_INITIAL (value); + } + /* In C, the TREE_VALUE is an integer constant. The TREE_PURPOSE is + an identifier from which we must tease out the CONST_DECL. */ + else if (tree decl = get_decl_for_identifier (id)) + attrs = DECL_ATTRIBUTES (decl); + /* Track 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. */ + const bool unused_p = (lookup_attribute ("unused", attrs) + || lookup_attribute ("maybe_unused", attrs)); node = splay_tree_lookup (cases, (splay_tree_key) value); if (node) { @@ -1769,6 +1784,10 @@ 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. */ + if (unused_p) + 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)) @@ -1791,8 +1810,7 @@ c_do_switch_warnings (splay_tree cases, location_t switch_location, (default_node || !warn_switch ? OPT_Wswitch_enum : OPT_Wswitch), - "enumeration value %qE not handled in switch", - TREE_PURPOSE (chain)); + "enumeration value %qE not handled in switch", id); } /* Warn if there are case expressions that don't correspond to diff --git a/gcc/c/c-decl.cc b/gcc/c/c-decl.cc index c701f07befe..c2c813d922a 100644 --- a/gcc/c/c-decl.cc +++ b/gcc/c/c-decl.cc @@ -12466,4 +12466,14 @@ c_check_in_current_scope (tree decl) return b != NULL && B_IN_CURRENT_SCOPE (b); } +/* Returns the symbol associated with an identifier ID. Currently, this + is used only in c_do_switch_warnings so that we can obtain the CONST_DECL + associated with an enumerator identifier. */ + +tree +get_decl_for_identifier (tree id) +{ + return I_SYMBOL_DECL (id); +} + #include "gt-c-c-decl.h" diff --git a/gcc/cp/tree.cc b/gcc/cp/tree.cc index 979728027ed..13852dc6446 100644 --- a/gcc/cp/tree.cc +++ b/gcc/cp/tree.cc @@ -6138,6 +6138,14 @@ maybe_adjust_arg_pos_for_attribute (const_tree fndecl) return n > 0 ? n - 1 : 0; } +/* Stub for c-common. Currently this has no use in the C++ front end. */ + +tree +get_decl_for_identifier (tree) +{ + gcc_unreachable (); +} + /* Release memory we no longer need after parsing. */ void 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; + } +}