From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1734) id 91D383836425; Wed, 18 May 2022 14:34:53 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 91D383836425 MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Marek Polacek To: gcc-cvs@gcc.gnu.org Subject: [gcc r13-622] c, c++: -Wswitch warning on [[maybe_unused]] enumerator [PR105497] X-Act-Checkin: gcc X-Git-Author: Marek Polacek X-Git-Refname: refs/heads/trunk X-Git-Oldrev: dfe38b8d5dbfe3dd5209aece4ce2f7a6b303a2f9 X-Git-Newrev: 60fdce11dc9e5ddf671b07a3fc6ed70476860b22 Message-Id: <20220518143453.91D383836425@sourceware.org> Date: Wed, 18 May 2022 14:34:53 +0000 (GMT) X-BeenThere: gcc-cvs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-cvs mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 18 May 2022 14:34:53 -0000 https://gcc.gnu.org/g:60fdce11dc9e5ddf671b07a3fc6ed70476860b22 commit r13-622-g60fdce11dc9e5ddf671b07a3fc6ed70476860b22 Author: Marek Polacek Date: Sat May 7 16:15:49 2022 -0400 c, c++: -Wswitch warning on [[maybe_unused]] enumerator [PR105497] 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. Diff: --- 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(-) 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; + } +}