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]

Re: [rfa] Add -Wswitch-default


> Anyway, ok to commit?
> 
>> > The patch is OK, with a suitable testcase, but GCC is presently
>> > feature-frozen before a release, see
>> > <http://gcc.gnu.org/develop.html>.  It'll unfreeze after 15 Feb.
> 
>> 
>> 
>> M'kay, add entry to diary.  Is the OK also for doco?

I've checked in the attached.  I also merged more recent changes to 
Wswitch.c into the new Wswitch-default.c test case.

Andrew


2002-03-23  Andrew Cagney  <ac131313@redhat.com>

	* doc/invoke.texi (Option Summary): Mention -Wswitch-default.
	(Warning Options): Document -Wswitch-default.
	* toplev.c (W_options): Add -Wswitch-default.  Update comment on
	-Wswitch.
	(warn_switch_default): Define variable.
	(warn_switch): Update comment.
	* flags.h (warn_switch_default): Declare variable.
	(warn_switch): Update comment.
	* stmt.c (expand_end_case): Check for and, when
	warn_switch_no_default, warn of a missing default case.
	
Index: f/ChangeLog
Sat Mar 23 11:18:17 2002  Andrew Cagney  <ac131313@redhat.com>

	* invoke.texi (Warning Options): Mention -Wswitch-default.

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

	* gcc.dg/Wswitch-default.c: New test.

Index: flags.h
===================================================================
RCS file: /cvs/gcc/gcc/gcc/flags.h,v
retrieving revision 1.81
diff -u -r1.81 flags.h
--- flags.h	2002/03/21 23:15:28	1.81
+++ flags.h	2002/03/23 16:26:43
@@ -126,9 +126,14 @@
 
 extern int warn_shadow;
 
-/* Warn if a switch on an enum fails to have a case for every enum value.  */
+/* Warn if a switch on an enum, that does not have a default case,
+   fails to have a case for every enum value.  */
 
 extern int warn_switch;
+
+/* Warn if a switch does not have a default case.  */
+
+extern int warn_switch_default;
 
 /* 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.249
diff -u -r1.249 stmt.c
--- stmt.c	2002/03/21 18:39:14	1.249
+++ stmt.c	2002/03/23 16:27:17
@@ -5284,6 +5284,9 @@
 	  && TREE_CODE (index_expr) != INTEGER_CST)
 	check_for_full_enumeration_handling (orig_type);
 
+      if (warn_switch_default && !thiscase->data.case_stmt.default_label)
+	warning ("switch missing default case");
+
       /* If we don't have a default-label, create one here,
 	 after the body of the switch.  */
       if (thiscase->data.case_stmt.default_label == 0)
Index: toplev.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/toplev.c,v
retrieving revision 1.601
diff -u -r1.601 toplev.c
--- toplev.c	2002/03/22 15:31:53	1.601
+++ toplev.c	2002/03/23 16:27:34
@@ -1395,10 +1395,15 @@
 
 int warn_shadow;
 
-/* Warn if a switch on an enum fails to have a case for every enum value.  */
+/* Warn if a switch on an enum, that does not have a default case,
+   fails to have a case for every enum value.  */
 
 int warn_switch;
 
+/* Warn if a switch does not have a default case.  */
+
+int warn_switch_default;
+
 /* 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.  */
 
@@ -1468,7 +1473,9 @@
   {"shadow", &warn_shadow, 1,
    N_("Warn when one local variable shadows another") },
   {"switch", &warn_switch, 1,
-   N_("Warn about enumerated switches missing a specific case") },
+   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") },
   {"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.132
diff -u -r1.132 invoke.texi
--- invoke.texi	2002/03/22 21:59:04	1.132
+++ invoke.texi	2002/03/23 16:28:33
@@ -228,7 +228,7 @@
 -Wno-import  -Wpacked  -Wpadded @gol
 -Wparentheses  -Wpointer-arith  -Wredundant-decls @gol
 -Wreturn-type  -Wsequence-point  -Wshadow @gol
--Wsign-compare  -Wswitch  -Wsystem-headers @gol
+-Wsign-compare  -Wswitch  -Wswitch-default  -Wsystem-headers @gol
 -Wtrigraphs  -Wundef  -Wuninitialized @gol
 -Wunknown-pragmas  -Wunreachable-code @gol
 -Wunused  -Wunused-function  -Wunused-label  -Wunused-parameter @gol
@@ -2033,6 +2033,11 @@
 enumeration.  (The presence of a @code{default} label prevents this
 warning.)  @code{case} labels outside the enumeration range also
 provoke warnings when this option is used.
+
+@item -Wswitch-default
+@opindex Wswitch-switch
+Warn whenever a @code{switch} statement does not have a @code{default}
+case.
 
 @item -Wtrigraphs
 @opindex Wtrigraphs
Index: f/invoke.texi
===================================================================
RCS file: /cvs/gcc/gcc/gcc/f/invoke.texi,v
retrieving revision 1.9
diff -u -r1.9 invoke.texi
--- invoke.texi	2002/02/09 09:27:11	1.9
+++ invoke.texi	2002/03/23 16:28:39
@@ -1356,6 +1356,9 @@
 @cindex -Wswitch option
 @cindex options, -Wswitch
 @item -Wswitch
+@cindex -Wswitch-default option
+@cindex options, -Wswitch-default
+@item -Wswitch-default
 @cindex -Wtraditional option
 @cindex options, -Wtraditional
 @item -Wtraditional
Index: testsuite/gcc.dg/Wswitch-default.c
===================================================================
RCS file: Wswitch-default.c
diff -N Wswitch-default.c
--- /dev/null	Tue May  5 13:32:27 1998
+++ Wswitch-default.c	Sat Mar 23 08:29:09 2002
@@ -0,0 +1,62 @@
+/* { dg-do compile } */
+/* { dg-options "-Wswitch-default" } */
+
+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;
+    } /* { dg-warning "switch missing default case" } */
+  switch (j)
+    {
+    case 3: return 4;
+    case 4: return 3;
+    default: break;
+    }
+  switch (ei)
+    { /* { dg-warning "switch missing default case" } */
+    }
+  switch (ej)
+    {
+    default: break;
+    }
+  switch (ek)
+    {
+    case e1: return 1;
+    } /* { dg-warning "switch missing default case" } */
+  switch (el)
+    {
+    case e1: return 1;
+    default: break;
+    }
+  switch (em)
+    {
+    case e1: return 1;
+    case e2: return 2;
+    } /* { dg-warning "switch missing default case" } */
+  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 "switch missing default case" } */
+  switch (ep)
+    {
+    case e1: return 1;
+    case e2: return 2;
+    case 3: return 3;
+    default: break;
+    }
+  return 0;
+}

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