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 822383858D33 for ; Sat, 7 May 2022 22:15:07 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 822383858D33 Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-665-scrxypKqNpGnppdii9bwrw-1; Sat, 07 May 2022 18:15:05 -0400 X-MC-Unique: scrxypKqNpGnppdii9bwrw-1 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.rdu2.redhat.com [10.11.54.7]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 8A78D294EDCE; Sat, 7 May 2022 22:15:05 +0000 (UTC) Received: from pdp-11.redhat.com (unknown [10.22.8.70]) by smtp.corp.redhat.com (Postfix) with ESMTP id 31A8D1544F2A; Sat, 7 May 2022 22:15:05 +0000 (UTC) From: Marek Polacek To: GCC Patches , Jason Merrill , Joseph Myers Subject: [PATCH] c, c++: -Wswitch warning on [[maybe_unused]] enumerator [PR105497] Date: Sat, 7 May 2022 18:14:56 -0400 Message-Id: <20220507221456.552767-1-polacek@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.85 on 10.11.54.7 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset="US-ASCII"; x-default=true X-Spam-Status: No, score=-22.6 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-List-Received-Date: Sat, 07 May 2022 22:15:09 -0000 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 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..01765624eb5 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; + /* 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; + } +} base-commit: 0c723bb4be2a67657828b692997855afcdc5d286 -- 2.35.1