Add -Wswitch-enum. Document.
authorAndrew Cagney <ac131313@redhat.com>
Tue, 26 Mar 2002 15:36:37 +0000 (15:36 +0000)
committerAndrew Cagney <cagney@gcc.gnu.org>
Tue, 26 Mar 2002 15:36:37 +0000 (15:36 +0000)
Fix PR c/5044.

From-SVN: r51386

gcc/ChangeLog
gcc/doc/invoke.texi
gcc/f/ChangeLog
gcc/f/invoke.texi
gcc/flags.h
gcc/stmt.c
gcc/testsuite/ChangeLog
gcc/testsuite/gcc.dg/Wswitch-enum.c [new file with mode: 0644]
gcc/toplev.c

index 9a64e93a28daa5ff537452d036741b06312b8518..3af3e3b3a64cc433ea1f10491b0dda6c8e90c032 100644 (file)
@@ -1,3 +1,15 @@
+2002-03-26  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.
+       
 2002-03-26  Richard Earnshaw  <rearnsha@arm.com>
 
        * arm.md (reload_mulsi3, reload_mulsi_compare0, reload_muladdsi)
index 321cda2d239e0b688f5e1de4433c48ce5bc11156..8f51f2959826081406180b9515f3327269d31749 100644 (file)
@@ -228,8 +228,8 @@ in the following sections.
 -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}
@@ -2039,6 +2039,13 @@ provoke warnings when this option is used.
 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
 Warn if any trigraphs are encountered that might change the meaning of
index da22c002ca559c0a9a7d9d9b38c9ecd18f87e3e9..68c8ade99eed0d5f679d70630c4928dbf05d0381 100644 (file)
@@ -1,3 +1,8 @@
+Tue Mar 26 10:30:05 2002  Andrew Cagney  <ac131313@redhat.com>
+
+       * invoke.texi (Warning Options): Mention -Wswitch-enum.
+       Fix PR c/5044.
+
 Tue Mar 26 07:30:51 2002  Neil Booth  <neil@daikokuya.demon.co.uk>
 
        * com.c (LANG_HOOKS_MARK_TREE): Redefine.
index 025fb9ed7d1a2bc12c12637a573222f42757c7d5..479ff2f2e897f26d6b384dbc7e1b65b9a1cb7100 100644 (file)
@@ -1359,6 +1359,9 @@ Some of these have no effect when compiling programs written in Fortran:
 @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 ee9753a943b4ed6286d00701bbb5ff1eab4a6daf..b9fca23d29cc62603f0be88648bdf64a8cc6d91d 100644 (file)
@@ -135,6 +135,11 @@ extern int warn_switch;
 
 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 5823f8b0497a7c65266f6f0dac80b5f2c05a7100..fcda52d53249d4664c4e23726567f254e182f0ce 100644 (file)
@@ -5280,10 +5280,10 @@ expand_end_case_type (orig_index, orig_type)
     {
       /* 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 7299c6de16f116295aa9b5216bc09c75157a6c64..684ee9d8ec0d3247bf4fea41076aceca906fceb7 100644 (file)
@@ -1,3 +1,8 @@
+2002-03-26  Andrew Cagney  <ac131313@redhat.com>
+
+       * gcc.dg/Wswitch-enum.c: New test.
+       Fix PR c/5044.
+
 2002-03-26  Richard Henderson  <rth@redhat.com>
 
        * gcc.c-torture/execute/20020307-2.c (main): Pass a variable sized
diff --git a/gcc/testsuite/gcc.dg/Wswitch-enum.c b/gcc/testsuite/gcc.dg/Wswitch-enum.c
new file mode 100644 (file)
index 0000000..00e4b55
--- /dev/null
@@ -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 8c7517e2fea45a16c7036a1c6612b67b0a5b7028..7268b5aa787480c54bd43ac5199d44a308c3c3b9 100644 (file)
@@ -1401,6 +1401,11 @@ int warn_switch;
 
 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.  */
 
@@ -1473,6 +1478,8 @@ static const lang_independent_options W_options[] =
    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,