Commit 173028e5 by Andrew Cagney Committed by Andrew Cagney

Add -Wswitch-enum. Document.

Fix PR c/5044.

From-SVN: r51386
parent e14365a7
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> 2002-03-26 Richard Earnshaw <rearnsha@arm.com>
* arm.md (reload_mulsi3, reload_mulsi_compare0, reload_muladdsi) * arm.md (reload_mulsi3, reload_mulsi_compare0, reload_muladdsi)
......
...@@ -228,8 +228,8 @@ in the following sections. ...@@ -228,8 +228,8 @@ in the following sections.
-Wno-import -Wpacked -Wpadded @gol -Wno-import -Wpacked -Wpadded @gol
-Wparentheses -Wpointer-arith -Wredundant-decls @gol -Wparentheses -Wpointer-arith -Wredundant-decls @gol
-Wreturn-type -Wsequence-point -Wshadow @gol -Wreturn-type -Wsequence-point -Wshadow @gol
-Wsign-compare -Wswitch -Wswitch-default -Wsystem-headers @gol -Wsign-compare -Wswitch -Wswitch-default -Wswitch-enum @gol
-Wtrigraphs -Wundef -Wuninitialized @gol -Wsystem-headers -Wtrigraphs -Wundef -Wuninitialized @gol
-Wunknown-pragmas -Wunreachable-code @gol -Wunknown-pragmas -Wunreachable-code @gol
-Wunused -Wunused-function -Wunused-label -Wunused-parameter @gol -Wunused -Wunused-function -Wunused-label -Wunused-parameter @gol
-Wunused-value -Wunused-variable -Wwrite-strings} -Wunused-value -Wunused-variable -Wwrite-strings}
...@@ -2039,6 +2039,13 @@ provoke warnings when this option is used. ...@@ -2039,6 +2039,13 @@ provoke warnings when this option is used.
Warn whenever a @code{switch} statement does not have a @code{default} Warn whenever a @code{switch} statement does not have a @code{default}
case. 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 @item -Wtrigraphs
@opindex Wtrigraphs @opindex Wtrigraphs
Warn if any trigraphs are encountered that might change the meaning of Warn if any trigraphs are encountered that might change the meaning of
......
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> Tue Mar 26 07:30:51 2002 Neil Booth <neil@daikokuya.demon.co.uk>
* com.c (LANG_HOOKS_MARK_TREE): Redefine. * com.c (LANG_HOOKS_MARK_TREE): Redefine.
......
...@@ -1359,6 +1359,9 @@ Some of these have no effect when compiling programs written in Fortran: ...@@ -1359,6 +1359,9 @@ Some of these have no effect when compiling programs written in Fortran:
@cindex -Wswitch-default option @cindex -Wswitch-default option
@cindex options, -Wswitch-default @cindex options, -Wswitch-default
@item -Wswitch-default @item -Wswitch-default
@cindex -Wswitch-enum option
@cindex options, -Wswitch-enum
@item -Wswitch-enum
@cindex -Wtraditional option @cindex -Wtraditional option
@cindex options, -Wtraditional @cindex options, -Wtraditional
@item -Wtraditional @item -Wtraditional
......
...@@ -135,6 +135,11 @@ extern int warn_switch; ...@@ -135,6 +135,11 @@ extern int warn_switch;
extern int warn_switch_default; 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 /* 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. */ or that use a null return and have a return-type other than void. */
......
...@@ -5280,10 +5280,10 @@ expand_end_case_type (orig_index, orig_type) ...@@ -5280,10 +5280,10 @@ expand_end_case_type (orig_index, orig_type)
{ {
/* If the switch expression was an enumerated type, check that /* If the switch expression was an enumerated type, check that
exactly all enumeration literals are covered by the cases. exactly all enumeration literals are covered by the cases.
The check is made -Wswitch was specified and there is no The check is made when -Wswitch was specified and there is no
default case. */ default case, or when -Wswitch-enum was specified. */
if (((warn_switch && !thiscase->data.case_stmt.default_label)
if ((warn_switch && !thiscase->data.case_stmt.default_label) || warn_switch_enum)
&& TREE_CODE (orig_type) == ENUMERAL_TYPE && TREE_CODE (orig_type) == ENUMERAL_TYPE
&& TREE_CODE (index_expr) != INTEGER_CST) && TREE_CODE (index_expr) != INTEGER_CST)
check_for_full_enumeration_handling (orig_type); check_for_full_enumeration_handling (orig_type);
......
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> 2002-03-26 Richard Henderson <rth@redhat.com>
* gcc.c-torture/execute/20020307-2.c (main): Pass a variable sized * gcc.c-torture/execute/20020307-2.c (main): Pass a variable sized
......
/* 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;
}
...@@ -1401,6 +1401,11 @@ int warn_switch; ...@@ -1401,6 +1401,11 @@ int warn_switch;
int warn_switch_default; 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 /* 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. */ 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[] = ...@@ -1473,6 +1478,8 @@ static const lang_independent_options W_options[] =
N_("Warn about enumerated switches, with no default, missing a case") }, N_("Warn about enumerated switches, with no default, missing a case") },
{"switch-default", &warn_switch_default, 1, {"switch-default", &warn_switch_default, 1,
N_("Warn about enumerated switches missing a default case") }, 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, {"aggregate-return", &warn_aggregate_return, 1,
N_("Warn about returning structures, unions or arrays") }, N_("Warn about returning structures, unions or arrays") },
{"cast-align", &warn_cast_align, 1, {"cast-align", &warn_cast_align, 1,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment