From da9e9b571483537a617d08ce7f0da84435497053 Mon Sep 17 00:00:00 2001 From: Matthew Beliveau Date: Fri, 21 Jun 2019 17:50:29 +0000 Subject: [PATCH] PR c++/90875 - added -Wswitch-outside-range option * doc/invoke.texi (Wswitch-outside-range): Document. * c.opt (Wswitch-outside-range): Added new option. * c-warn.c (c_do_switch_warnings): Use OPT_Wswitch-outside-range. * c-c++-common/Wswitch-outside-range-1.c: New test. * c-c++-common/Wswitch-outside-range-2.c: New test. * c-c++-common/Wswitch-outside-range-3.c: New test. * c-c++-common/Wswitch-outside-range-4.c: New test. From-SVN: r272559 --- gcc/ChangeLog | 5 +++++ gcc/c-family/ChangeLog | 6 ++++++ gcc/c-family/c-warn.c | 15 ++++++++------- gcc/c-family/c.opt | 4 ++++ gcc/doc/invoke.texi | 7 +++++++ gcc/testsuite/ChangeLog | 8 ++++++++ 6 files changed, 38 insertions(+), 7 deletions(-) diff --git a/gcc/ChangeLog b/gcc/ChangeLog index fa45d9d2185..14fb81a6de3 100644 --- a/gcc/ChangeLog +++ b/gcc/ChangeLog @@ -1,3 +1,8 @@ +2019-06-21 Matthew Beliveau + + PR c++/90875 - added -Wswitch-outside-range option + * doc/invoke.texi (Wswitch-outside-range): Document. + 2019-06-21 Jeff Law PR tree-optimization/90949 diff --git a/gcc/c-family/ChangeLog b/gcc/c-family/ChangeLog index c1522b249b5..fec706ae94c 100644 --- a/gcc/c-family/ChangeLog +++ b/gcc/c-family/ChangeLog @@ -1,3 +1,9 @@ +2019-06-21 Matthew Beliveau + + PR c++/90875 - added -Wswitch-outside-range option + * c.opt (Wswitch-outside-range): Added new option. + * c-warn.c (c_do_switch_warnings): Use OPT_Wswitch-outside-range. + 2019-06-21 Marek Polacek PR c++/90953 - ICE with -Wmissing-format-attribute. diff --git a/gcc/c-family/c-warn.c b/gcc/c-family/c-warn.c index 5941c10cddb..b5d09e761d7 100644 --- a/gcc/c-family/c-warn.c +++ b/gcc/c-family/c-warn.c @@ -1460,8 +1460,9 @@ c_do_switch_warnings (splay_tree cases, location_t switch_location, min_value) >= 0) { location_t loc = EXPR_LOCATION ((tree) node->value); - warning_at (loc, 0, "lower value in case label range" - " less than minimum value for type"); + warning_at (loc, OPT_Wswitch_outside_range, + "lower value in case label range less than minimum" + " value for type"); CASE_LOW ((tree) node->value) = convert (TREE_TYPE (cond), min_value); node->key = (splay_tree_key) CASE_LOW ((tree) node->value); @@ -1474,8 +1475,8 @@ c_do_switch_warnings (splay_tree cases, location_t switch_location, if (node == NULL || !node->key) break; location_t loc = EXPR_LOCATION ((tree) node->value); - warning_at (loc, 0, "case label value is less than minimum " - "value for type"); + warning_at (loc, OPT_Wswitch_outside_range, "case label value is" + " less than minimum value for type"); splay_tree_remove (cases, node->key); } while (1); @@ -1491,8 +1492,8 @@ c_do_switch_warnings (splay_tree cases, location_t switch_location, max_value) > 0) { location_t loc = EXPR_LOCATION ((tree) node->value); - warning_at (loc, 0, "upper value in case label range" - " exceeds maximum value for type"); + warning_at (loc, OPT_Wswitch_outside_range, "upper value in case" + " label range exceeds maximum value for type"); CASE_HIGH ((tree) node->value) = convert (TREE_TYPE (cond), max_value); outside_range_p = true; @@ -1503,7 +1504,7 @@ c_do_switch_warnings (splay_tree cases, location_t switch_location, != NULL) { location_t loc = EXPR_LOCATION ((tree) node->value); - warning_at (loc, 0, + warning_at (loc, OPT_Wswitch_outside_range, "case label value exceeds maximum value for type"); splay_tree_remove (cases, node->key); outside_range_p = true; diff --git a/gcc/c-family/c.opt b/gcc/c-family/c.opt index 572cf186262..a4cf3bd623d 100644 --- a/gcc/c-family/c.opt +++ b/gcc/c-family/c.opt @@ -819,6 +819,10 @@ Wswitch-bool C ObjC C++ ObjC++ Var(warn_switch_bool) Warning Init(1) Warn about switches with boolean controlling expression. +Wswitch-outside-range +C ObjC C++ ObjC++ Var(warn_switch_outside_range) Warning Init(1) +Warn about switch values that are outside of the switch's type range. + Wtemplates C++ ObjC++ Var(warn_templates) Warning Warn on primary template declaration. diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi index eaef4cd63d2..4f93c7e0de7 100644 --- a/gcc/doc/invoke.texi +++ b/gcc/doc/invoke.texi @@ -5390,6 +5390,13 @@ switch ((int) (a == 4)) @end smallexample This warning is enabled by default for C and C++ programs. +@item -Wswitch-outside-range +@opindex Wswitch-outside-range +@opindex Wno-switch-outside-range +Warn whenever a @code{switch} case has a value that is outside of its +respective type range. This warning is enabled by default for +C and C++ programs. + @item -Wswitch-unreachable @opindex Wswitch-unreachable @opindex Wno-switch-unreachable diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index 9826e3de002..2c80ac4bdd6 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,11 @@ +2019-06-21 Matthew Beliveau + + PR c++/90875 - added -Wswitch-outside-range option + * c-c++-common/Wswitch-outside-range-1.c: New test. + * c-c++-common/Wswitch-outside-range-2.c: New test. + * c-c++-common/Wswitch-outside-range-3.c: New test. + * c-c++-common/Wswitch-outside-range-4.c: New test. + 2019-06-21 Steven G. Kargl PR fortran/51991 -- 2.30.2