This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
[PATCH 4.4] fix PR17843 - warn about superfluous default label for switch(boolean_type)
- From: Bernhard Fischer <rep dot dot dot nop at gmail dot com>
- To: gcc-patches at gcc dot gnu dot org
- Cc: Bernhard Fischer <rep dot dot dot nop at gmail dot com>
- Date: Fri, 4 Jan 2008 21:29:02 +0100
- Subject: [PATCH 4.4] fix PR17843 - warn about superfluous default label for switch(boolean_type)
:ADDPATCH middle-end:
(not yet up again, i guess.)
Hi,
The attached patchlet would warn about a superfluous default_label in a
switch_statement that uses a boolean as condition.
Would something like this be appropriate, provided testing passes, or
should it be more generic somehow?
gcc/ChangeLog
2008-01-04 Bernhard Fischer <>
* c-common.c (record_surplus_node): New function.
(c_do_switch_warnings): Warn for surplus default label in switch
stmt.
Index: gcc-4.3/gcc/c-common.c
===================================================================
--- gcc-4.3/gcc/c-common.c (revision 131117)
+++ gcc-4.3/gcc/c-common.c (working copy)
@@ -4365,6 +4365,30 @@ match_case_to_enum (splay_tree_node node
return 0;
}
+/* Helper for c_do_switch_warnings to warn about a
+ surplus default label in a switch stmt. */
+static int
+record_surplus_node (splay_tree_node stn,
+ void *data)
+{
+ tree elt = (tree) stn->key;
+ unsigned *seen = (unsigned*) data;
+
+ if (!elt)
+ {
+ /* Mark the default_label. */
+ *seen = 1;
+ }
+ else
+ {
+ /* Record the value of the label. Warnings about duplicate labels
+ or labels that are too big for the underlying type are emitted
+ elsewhere. */
+ *seen |= ((int) elt->type.values + 1) << 1;
+ }
+ return 0;
+}
+
/* Handle -Wswitch*. Called from the front end after parsing the
switch construct. */
/* ??? Should probably be somewhere generic, since other languages
@@ -4388,6 +4412,15 @@ c_do_switch_warnings (splay_tree cases,
warning (OPT_Wswitch_default, "%Hswitch missing default case",
&switch_location);
+ if (type && TREE_CODE (type) == BOOLEAN_TYPE)
+ {
+ unsigned nodes_seen = 0;
+ splay_tree_foreach (cases, record_surplus_node, &nodes_seen);
+ /* Seen true, false *and* default? */
+ if (nodes_seen == 7)
+ warning (0, "%Hsurplus default case in switch", &switch_location);
+ }
+
/* From here on, we only care about about enumerated types. */
if (!type || TREE_CODE (type) != ENUMERAL_TYPE)
return;