This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[rfa] Add -Wswitch-enum


Hello,

The attached adds a -Wswitch-enum to warn when there are too few or too 
many enum's in an enum switch.  Unlike -Wswitch, this warns of problems 
even when there is a `default:' case.

ok to commit?

Andrew
Index: ChangeLog
2002-03-24  Andrew Cagney  <ac131313@redhat.com>

	* doc/invoke.texi (Option Summary): Mention -Wswitch-enum.
	(Warning Options): Document -Wswitch-enum.
	* toplev.c (W_options): Add -Wswitch-enum.  Update comment on
	-Wswitch.
	(warn_switch_enum): Define variables.
	* flags.h (warn_switch_enum): Declare variables.
	* stmt.c (expand_end_case_type): When warn_switch_enum /
	-Wswitch-enum, perform switch checks.
	Fix PR c/5044.
	
Index: f/ChangeLog
Sun Mar 24 15:07:10 2002  Andrew Cagney  <ac131313@redhat.com>

	* invoke.texi (Warning Options): Mention -Wswitch-enum.
	Fix PR c/5044.

Index: testsuite/ChangeLog
2002-03-24  Andrew Cagney  <ac131313@redhat.com>

	* gcc.dg/Wswitch-enum.c: New test.
	Fix PR c/5044.

Index: flags.h
===================================================================
RCS file: /cvs/gcc/gcc/gcc/flags.h,v
retrieving revision 1.82
diff -u -r1.82 flags.h
--- flags.h	2002/03/23 16:33:41	1.82
+++ flags.h	2002/03/24 23:15:24
@@ -135,6 +135,11 @@
 
 extern int warn_switch_default;
 
+/* Warn if a switch on an enum fails to have a case for every enum
+   value (regardless of the presence or otherwise of a default case).  */
+
+extern int warn_switch_enum;
+
 /* Nonzero means warn about function definitions that default the return type
    or that use a null return and have a return-type other than void.  */
 
Index: stmt.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/stmt.c,v
retrieving revision 1.251
diff -u -r1.251 stmt.c
--- stmt.c	2002/03/24 19:53:49	1.251
+++ stmt.c	2002/03/24 23:15:44
@@ -5280,10 +5280,10 @@
     {
       /* If the switch expression was an enumerated type, check that
 	 exactly all enumeration literals are covered by the cases.
-	 The check is made -Wswitch was specified and there is no
-	 default case.  */
-
-      if ((warn_switch && !thiscase->data.case_stmt.default_label)
+	 The check is made when -Wswitch was specified and there is no
+	 default case, or when -Wswitch-enum was specified.  */
+      if (((warn_switch && !thiscase->data.case_stmt.default_label)
+	   || warn_switch_enum)
 	  && TREE_CODE (orig_type) == ENUMERAL_TYPE
 	  && TREE_CODE (index_expr) != INTEGER_CST)
 	check_for_full_enumeration_handling (orig_type);
Index: toplev.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/toplev.c,v
retrieving revision 1.602
diff -u -r1.602 toplev.c
--- toplev.c	2002/03/23 16:33:41	1.602
+++ toplev.c	2002/03/24 23:16:06
@@ -1404,6 +1404,11 @@
 
 int warn_switch_default;
 
+/* Warn if a switch on an enum fails to have a case for every enum
+   value (regardless of the presence or otherwise of a default case).  */
+
+int warn_switch_enum;
+
 /* Nonzero means warn about function definitions that default the return type
    or that use a null return and have a return-type other than void.  */
 
@@ -1476,6 +1481,8 @@
    N_("Warn about enumerated switches, with no default, missing a case") },
   {"switch-default", &warn_switch_default, 1,
    N_("Warn about enumerated switches missing a default case") },
+  {"switch-enum", &warn_switch_enum, 1,
+   N_("Warn about all enumerated switches missing a specific case") },
   {"aggregate-return", &warn_aggregate_return, 1,
    N_("Warn about returning structures, unions or arrays") },
   {"cast-align", &warn_cast_align, 1,
Index: doc/invoke.texi
===================================================================
RCS file: /cvs/gcc/gcc/gcc/doc/invoke.texi,v
retrieving revision 1.133
diff -u -r1.133 invoke.texi
--- invoke.texi	2002/03/23 16:33:42	1.133
+++ invoke.texi	2002/03/24 23:17:31
@@ -228,8 +228,8 @@
 -Wno-import  -Wpacked  -Wpadded @gol
 -Wparentheses  -Wpointer-arith  -Wredundant-decls @gol
 -Wreturn-type  -Wsequence-point  -Wshadow @gol
--Wsign-compare  -Wswitch  -Wswitch-default  -Wsystem-headers @gol
--Wtrigraphs  -Wundef  -Wuninitialized @gol
+-Wsign-compare  -Wswitch  -Wswitch-default -Wswitch-enum @gol
+-Wsystem-headers -Wtrigraphs  -Wundef  -Wuninitialized @gol
 -Wunknown-pragmas  -Wunreachable-code @gol
 -Wunused  -Wunused-function  -Wunused-label  -Wunused-parameter @gol
 -Wunused-value  -Wunused-variable  -Wwrite-strings}
@@ -2038,6 +2038,13 @@
 @opindex Wswitch-switch
 Warn whenever a @code{switch} statement does not have a @code{default}
 case.
+
+@item -Wswitch-enum
+@opindex Wswitch-enum
+Warn whenever a @code{switch} statement has an index of enumeral type
+and lacks a @code{case} for one or more of the named codes of that
+enumeration.  @code{case} labels outside the enumeration range also
+provoke warnings when this option is used.
 
 @item -Wtrigraphs
 @opindex Wtrigraphs
Index: f/invoke.texi
===================================================================
RCS file: /cvs/gcc/gcc/gcc/f/invoke.texi,v
retrieving revision 1.10
diff -u -r1.10 invoke.texi
--- invoke.texi	2002/03/23 16:33:43	1.10
+++ invoke.texi	2002/03/24 23:18:01
@@ -1359,6 +1359,9 @@
 @cindex -Wswitch-default option
 @cindex options, -Wswitch-default
 @item -Wswitch-default
+@cindex -Wswitch-enum option
+@cindex options, -Wswitch-enum
+@item -Wswitch-enum
 @cindex -Wtraditional option
 @cindex options, -Wtraditional
 @item -Wtraditional
Index: testsuite/gcc.dg/Wswitch-enum.c
===================================================================
RCS file: Wswitch-enum.c
diff -N Wswitch-enum.c
--- /dev/null	Tue May  5 13:32:27 1998
+++ Wswitch-enum.c	Sun Mar 24 15:18:50 2002
@@ -0,0 +1,63 @@
+/* PR c/5044 */
+/* { dg-do compile } */
+/* { dg-options "-Wswitch-enum" } */
+
+enum e { e1, e2 };
+
+int
+foo (int i, int j, enum e ei, enum e ej, enum e ek, enum e el,
+     enum e em, enum e en, enum e eo, enum e ep)
+{
+  switch (i)
+    {
+    case 1: return 1;
+    case 2: return 2;
+    }
+  switch (j)
+    {
+    case 3: return 4;
+    case 4: return 3;
+    default: break;
+    }
+  switch (ei)
+    { /* { dg-warning "enumeration value `e1' not handled in switch" "enum e1" } */
+    } /* { dg-warning "enumeration value `e2' not handled in switch" "enum e2" { target *-*-* } 23 } */
+  switch (ej)
+    { /* { dg-warning "enumeration value `e1' not handled in switch" "enum e1" { target *-*-* } 28 } */
+    default: break;
+    } /* { dg-warning "enumeration value `e2' not handled in switch" "enum e2" } */
+  switch (ek)
+    {
+    case e1: return 1;
+    } /* { dg-warning "enumeration value `e2' not handled in switch" "enum e2" } */
+  switch (el)
+    {
+    case e1: return 1;
+    default: break;
+    }  /* { dg-warning "enumeration value `e2' not handled in switch" "enum e2" } */
+  switch (em)
+    {
+    case e1: return 1;
+    case e2: return 2;
+    }
+  switch (en)
+    {
+    case e1: return 1;
+    case e2: return 2;
+    default: break;
+    }
+  switch (eo)
+    {
+    case e1: return 1;
+    case e2: return 2;
+    case 3: return 3;
+    } /* { dg-warning "case value `3' not in enumerated type `e'" "excess 3" } */
+  switch (ep)
+    {
+    case e1: return 1;
+    case e2: return 2;
+    case 3: return 3;
+    default: break;
+    } /* { dg-warning "case value `3' not in enumerated type `e'" "excess 3" } */
+  return 0;
+}

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]