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)
{
/* 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))
--- /dev/null
+/* 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;
+ }
+}
--- /dev/null
+// 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;
+ }
+}