Commit 9d548dfb by Marek Polacek Committed by Marek Polacek

re PR c/30020 (improve diagnostics for limited range warning for a switch statement)

	PR c/30020
	* c-common.c (check_case_bounds): Add location parameter.
	Use it.
	(c_add_case_label): Pass loc to check_case_bounds.

	* c-c++-common/pr30020.c: New test.

From-SVN: r211212
parent 348d4b0a
2014-06-04 Marek Polacek <polacek@redhat.com>
PR c/30020
* c-common.c (check_case_bounds): Add location parameter.
Use it.
(c_add_case_label): Pass loc to check_case_bounds.
2014-06-03 Marek Polacek <polacek@redhat.com> 2014-06-03 Marek Polacek <polacek@redhat.com>
PR c/60439 PR c/60439
......
...@@ -301,7 +301,7 @@ struct visibility_flags visibility_options; ...@@ -301,7 +301,7 @@ struct visibility_flags visibility_options;
static tree c_fully_fold_internal (tree expr, bool, bool *, bool *); static tree c_fully_fold_internal (tree expr, bool, bool *, bool *);
static tree check_case_value (tree); static tree check_case_value (tree);
static bool check_case_bounds (tree, tree, tree *, tree *); static bool check_case_bounds (location_t, tree, tree, tree *, tree *);
static tree handle_packed_attribute (tree *, tree, tree, int, bool *); static tree handle_packed_attribute (tree *, tree, tree, int, bool *);
static tree handle_nocommon_attribute (tree *, tree, tree, int, bool *); static tree handle_nocommon_attribute (tree *, tree, tree, int, bool *);
...@@ -3355,7 +3355,7 @@ check_case_value (tree value) ...@@ -3355,7 +3355,7 @@ check_case_value (tree value)
untouched) or false if the label is out of range. */ untouched) or false if the label is out of range. */
static bool static bool
check_case_bounds (tree type, tree orig_type, check_case_bounds (location_t loc, tree type, tree orig_type,
tree *case_low_p, tree *case_high_p) tree *case_low_p, tree *case_high_p)
{ {
tree min_value, max_value; tree min_value, max_value;
...@@ -3373,7 +3373,8 @@ check_case_bounds (tree type, tree orig_type, ...@@ -3373,7 +3373,8 @@ check_case_bounds (tree type, tree orig_type,
if (tree_int_cst_compare (case_low, min_value) < 0 if (tree_int_cst_compare (case_low, min_value) < 0
&& tree_int_cst_compare (case_high, min_value) < 0) && tree_int_cst_compare (case_high, min_value) < 0)
{ {
warning (0, "case label value is less than minimum value for type"); warning_at (loc, 0, "case label value is less than minimum value "
"for type");
return false; return false;
} }
...@@ -3381,7 +3382,7 @@ check_case_bounds (tree type, tree orig_type, ...@@ -3381,7 +3382,7 @@ check_case_bounds (tree type, tree orig_type,
if (tree_int_cst_compare (case_low, max_value) > 0 if (tree_int_cst_compare (case_low, max_value) > 0
&& tree_int_cst_compare (case_high, max_value) > 0) && tree_int_cst_compare (case_high, max_value) > 0)
{ {
warning (0, "case label value exceeds maximum value for type"); warning_at (loc, 0, "case label value exceeds maximum value for type");
return false; return false;
} }
...@@ -3389,8 +3390,8 @@ check_case_bounds (tree type, tree orig_type, ...@@ -3389,8 +3390,8 @@ check_case_bounds (tree type, tree orig_type,
if (tree_int_cst_compare (case_high, min_value) >= 0 if (tree_int_cst_compare (case_high, min_value) >= 0
&& tree_int_cst_compare (case_low, min_value) < 0) && tree_int_cst_compare (case_low, min_value) < 0)
{ {
warning (0, "lower value in case label range" warning_at (loc, 0, "lower value in case label range"
" less than minimum value for type"); " less than minimum value for type");
case_low = min_value; case_low = min_value;
} }
...@@ -3398,8 +3399,8 @@ check_case_bounds (tree type, tree orig_type, ...@@ -3398,8 +3399,8 @@ check_case_bounds (tree type, tree orig_type,
if (tree_int_cst_compare (case_low, max_value) <= 0 if (tree_int_cst_compare (case_low, max_value) <= 0
&& tree_int_cst_compare (case_high, max_value) > 0) && tree_int_cst_compare (case_high, max_value) > 0)
{ {
warning (0, "upper value in case label range" warning_at (loc, 0, "upper value in case label range"
" exceeds maximum value for type"); " exceeds maximum value for type");
case_high = max_value; case_high = max_value;
} }
...@@ -6014,7 +6015,7 @@ c_add_case_label (location_t loc, splay_tree cases, tree cond, tree orig_type, ...@@ -6014,7 +6015,7 @@ c_add_case_label (location_t loc, splay_tree cases, tree cond, tree orig_type,
expression. If both low_value and high_value are out of range, expression. If both low_value and high_value are out of range,
don't insert the case label and return NULL_TREE. */ don't insert the case label and return NULL_TREE. */
if (low_value if (low_value
&& !check_case_bounds (type, orig_type, && !check_case_bounds (loc, type, orig_type,
&low_value, high_value ? &high_value : NULL)) &low_value, high_value ? &high_value : NULL))
return NULL_TREE; return NULL_TREE;
......
2014-06-04 Marek Polacek <polacek@redhat.com>
PR c/30020
* c-c++-common/pr30020.c: New test.
2014-06-03 Andrew Pinski <apinski@cavium.com> 2014-06-03 Andrew Pinski <apinski@cavium.com>
* gcc.c-torture/compile/20140528-1.c: New testcase. * gcc.c-torture/compile/20140528-1.c: New testcase.
......
/* PR c/30020 */
/* { dg-do compile } */
int
foo (unsigned char c)
{
switch (c) { case 42: case -1: return -1; }; /* { dg-warning "25:case label value" } */
switch (c) { case 42: case 300: return -1; }; /* { dg-warning "25:case label value" } */
switch (c) { case 42: case -1 ... 2: return -1; }; /* { dg-warning "25:lower value in case" } */
switch (c) { case 42: case 250 ... 300: return -1; }; /* { dg-warning "25:upper value in case" } */
return 0;
}
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