c, c++: -Wswitch warning on [[maybe_unused]] enumerator [PR105497]
authorMarek Polacek <polacek@redhat.com>
Sat, 7 May 2022 20:15:49 +0000 (16:15 -0400)
committerMarek Polacek <polacek@redhat.com>
Wed, 18 May 2022 14:34:30 +0000 (10:34 -0400)
commit60fdce11dc9e5ddf671b07a3fc6ed70476860b22
treec8f625cb6000e14bf77d49ce48b2683660526034
parentdfe38b8d5dbfe3dd5209aece4ce2f7a6b303a2f9
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.
gcc/c-family/c-warn.cc
gcc/testsuite/c-c++-common/Wswitch-1.c [new file with mode: 0644]
gcc/testsuite/g++.dg/warn/Wswitch-4.C [new file with mode: 0644]