This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
[rfa] Add -Wswitch-default
- From: Andrew Cagney <ac131313 at cygnus dot com>
- To: gcc-patches at gcc dot gnu dot org
- Date: Sat, 02 Feb 2002 16:32:27 -0500
- Subject: [rfa] Add -Wswitch-default
Hello,
The attached patch tweeks the GCC trunk so that it supports a
-Wswitch-default flag.
This flag reports a warning when a switch statement does not have a
``default:'' case.
I'm open to other suggestions for a flag name: -Wswitch-no-default
(-Wno-switch-no-default ..) -Wswitch-defaultless, ...
As for a test case, I couldn't find anywhere to add one. Guess I should
file a bug report.
Anyway, ok to commit?
Andrew
Index: ChangeLog
2002-02-02 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 Feb 2 11:58:12 2002 Andrew Cagney <ac131313@redhat.com>
* invoke.texi (Warning Options): Mention -Wswitch-default.
Index: flags.h
===================================================================
RCS file: /cvs/gcc/gcc/gcc/flags.h,v
retrieving revision 1.76
diff -p -r1.76 flags.h
*** flags.h 2002/01/10 18:51:14 1.76
--- flags.h 2002/02/02 17:03:22
*************** extern int warn_unknown_pragmas;
*** 126,134 ****
extern int warn_shadow;
! /* Warn if a switch on an enum fails to have a case for every enum value. */
extern int warn_switch;
/* 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. */
--- 126,139 ----
extern int warn_shadow;
! /* 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.244
diff -p -r1.244 stmt.c
*** stmt.c 2002/01/31 06:21:19 1.244
--- stmt.c 2002/02/02 17:03:46
*************** expand_end_case (orig_index)
*** 5265,5270 ****
--- 5265,5273 ----
&& TREE_CODE (index_expr) != INTEGER_CST)
check_for_full_enumeration_handling (TREE_TYPE (orig_index));
+ 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.573
diff -p -r1.573 toplev.c
*** toplev.c 2002/01/28 18:37:58 1.573
--- toplev.c 2002/02/02 17:04:50
*************** int warn_uninitialized;
*** 1413,1422 ****
int warn_shadow;
! /* Warn if a switch on an enum fails to have a case for every enum value. */
int warn_switch;
/* 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. */
--- 1413,1427 ----
int warn_shadow;
! /* 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. */
*************** static const lang_independent_options W_
*** 1486,1492 ****
{"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") },
{"aggregate-return", &warn_aggregate_return, 1,
N_("Warn about returning structures, unions or arrays") },
{"cast-align", &warn_cast_align, 1,
--- 1491,1499 ----
{"shadow", &warn_shadow, 1,
N_("Warn when one local variable shadows another") },
{"switch", &warn_switch, 1,
! 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.111
diff -p -r1.111 invoke.texi
*** invoke.texi 2002/01/27 17:44:54 1.111
--- invoke.texi 2002/02/02 17:07:06
*************** in the following sections.
*** 228,234 ****
-Wno-import -Wpacked -Wpadded @gol
-Wparentheses -Wpointer-arith -Wredundant-decls @gol
-Wreturn-type -Wsequence-point -Wshadow @gol
! -Wsign-compare -Wswitch -Wsystem-headers @gol
-Wtrigraphs -Wundef -Wuninitialized @gol
-Wunknown-pragmas -Wunreachable-code @gol
-Wunused -Wunused-function -Wunused-label -Wunused-parameter @gol
--- 228,234 ----
-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
-Wunknown-pragmas -Wunreachable-code @gol
-Wunused -Wunused-function -Wunused-label -Wunused-parameter @gol
*************** and lacks a @code{case} for one or more
*** 2067,2072 ****
--- 2067,2077 ----
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.8
diff -p -r1.8 invoke.texi
*** invoke.texi 2002/01/08 09:28:24 1.8
--- invoke.texi 2002/02/02 17:07:32
*************** Some of these have no effect when compil
*** 1356,1361 ****
--- 1356,1364 ----
@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