Commit 6c80b1b5 by Joseph Myers Committed by Joseph Myers

Make more bad uses of fallthrough attribute into pedwarns.

Various bad uses of the [[fallthrough]] attribute are constraint
violations in C2x, so need pedwarns rather than warnings.

This patch duly turns the relevant warnings into pedwarns.  The
relevant code is not specific to C, and does not know which form the
attribute was given in ([[fallthrough]] or [[gnu::fallthrough]] or
__attribute__((fallthrough))), but as I understand it these usages are
also erroneous for C++ and it seems reasonable to give a pedwarn here
even when a form other than [[fallthrough]] is being used.

The precise meaning of the standard wording about "The next statement
that would be executed" seems a but unclear in some corner cases; the
tests added keep to cases where it is clear whether or not the next
statement executed is of the required form.

Bootstrapped with no regressions for x86_64-pc-linux-gnu.

gcc:
	* gimplify.c (expand_FALLTHROUGH_r, expand_FALLTHROUGH): Use
	pedwarn instead of warning_at for fallthrough not preceding a case
	or default label.

gcc/c-family:
	* c-attribs.c (handle_fallthrough_attribute): Use pedwarn instead
	of warning.

gcc/testsuite:
	* gcc.dg/c2x-attr-fallthrough-6.c: New test.  Split out from
	c2x-attr-fallthrough-3.c.
	* gcc.dg/c2x-attr-fallthrough-1.c: Add more tests.
	* gcc.dg/c2x-attr-fallthrough-2.c: Update expected diagnostics.
	* gcc.dg/c2x-attr-fallthrough-3.c: Split inside-switch part of
	test out to c2x-attr-fallthrough-6.c.

From-SVN: r278599
parent b30e83f8
2019-11-21 Joseph Myers <joseph@codesourcery.com>
* gimplify.c (expand_FALLTHROUGH_r, expand_FALLTHROUGH): Use
pedwarn instead of warning_at for fallthrough not preceding a case
or default label.
2019-11-22 Prathamesh Kulkarni <prathamesh.kulkarni@linaro.org> 2019-11-22 Prathamesh Kulkarni <prathamesh.kulkarni@linaro.org>
PR tree-optimization/92608 PR tree-optimization/92608
2019-11-21 Joseph Myers <joseph@codesourcery.com>
* c-attribs.c (handle_fallthrough_attribute): Use pedwarn instead
of warning.
2019-11-19 Joseph Myers <joseph@codesourcery.com> 2019-11-19 Joseph Myers <joseph@codesourcery.com>
* c-common.c (attribute_fallthrough_p): In C, use pedwarn not * c-common.c (attribute_fallthrough_p): In C, use pedwarn not
......
...@@ -4117,7 +4117,7 @@ tree ...@@ -4117,7 +4117,7 @@ tree
handle_fallthrough_attribute (tree *, tree name, tree, int, handle_fallthrough_attribute (tree *, tree name, tree, int,
bool *no_add_attrs) bool *no_add_attrs)
{ {
warning (OPT_Wattributes, "%qE attribute ignored", name); pedwarn (input_location, OPT_Wattributes, "%qE attribute ignored", name);
*no_add_attrs = true; *no_add_attrs = true;
return NULL_TREE; return NULL_TREE;
} }
......
...@@ -2405,8 +2405,8 @@ expand_FALLTHROUGH_r (gimple_stmt_iterator *gsi_p, bool *handled_ops_p, ...@@ -2405,8 +2405,8 @@ expand_FALLTHROUGH_r (gimple_stmt_iterator *gsi_p, bool *handled_ops_p,
gsi_next (&gsi2); gsi_next (&gsi2);
} }
if (!found) if (!found)
warning_at (loc, 0, "attribute %<fallthrough%> not preceding " pedwarn (loc, 0, "attribute %<fallthrough%> not preceding "
"a case label or default label"); "a case label or default label");
} }
break; break;
default: default:
...@@ -2428,8 +2428,8 @@ expand_FALLTHROUGH (gimple_seq *seq_p) ...@@ -2428,8 +2428,8 @@ expand_FALLTHROUGH (gimple_seq *seq_p)
if (wi.callback_result == integer_zero_node) if (wi.callback_result == integer_zero_node)
/* We've found [[fallthrough]]; at the end of a switch, which the C++ /* We've found [[fallthrough]]; at the end of a switch, which the C++
standard says is ill-formed; see [dcl.attr.fallthrough]. */ standard says is ill-formed; see [dcl.attr.fallthrough]. */
warning_at (loc, 0, "attribute %<fallthrough%> not preceding " pedwarn (loc, 0, "attribute %<fallthrough%> not preceding "
"a case label or default label"); "a case label or default label");
} }
......
2019-11-21 Joseph Myers <joseph@codesourcery.com>
* gcc.dg/c2x-attr-fallthrough-6.c: New test. Split out from
c2x-attr-fallthrough-3.c.
* gcc.dg/c2x-attr-fallthrough-1.c: Add more tests.
* gcc.dg/c2x-attr-fallthrough-2.c: Update expected diagnostics.
* gcc.dg/c2x-attr-fallthrough-3.c: Split inside-switch part of
test out to c2x-attr-fallthrough-6.c.
2019-11-22 Prathamesh Kulkarni <prathamesh.kulkarni@linaro.org> 2019-11-22 Prathamesh Kulkarni <prathamesh.kulkarni@linaro.org>
PR tree-optimization/92608 PR tree-optimization/92608
......
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
/* { dg-options "-std=c2x -pedantic-errors -Wextra" } */ /* { dg-options "-std=c2x -pedantic-errors -Wextra" } */
int int
f (int a) f (int a, int c)
{ {
int b = 2; int b = 2;
switch (a) switch (a)
...@@ -22,6 +22,21 @@ f (int a) ...@@ -22,6 +22,21 @@ f (int a)
case 5: case 5:
b += 1; b += 1;
break; break;
case 6:
if (c == 2)
{
[[fallthrough]];
}
else
{
[[fallthrough]];
}
case 7:
b += 3;
[[fallthrough]];
default:
b += 8;
break;
} }
return b; return b;
} }
...@@ -15,7 +15,8 @@ int z = sizeof (int [[fallthrough]]); /* { dg-error "ignored" } */ ...@@ -15,7 +15,8 @@ int z = sizeof (int [[fallthrough]]); /* { dg-error "ignored" } */
int int
f (int a) f (int a)
{ {
[[fallthrough]] int b = 2; /* { dg-warning "not followed by|ignored" } */ [[fallthrough]] int b = 2; /* { dg-warning "not followed by" } */
/* { dg-error "ignored" "ignored" { target *-*-* } .-1 } */
switch (a) switch (a)
{ {
case 1: case 1:
......
/* Test C2x attribute syntax. Invalid use of fallthrough attribute /* Test C2x attribute syntax. Invalid use of fallthrough attribute
outside switch or in bad context inside switch. */ outside switch. */
/* { dg-do compile } */ /* { dg-do compile } */
/* { dg-options "-std=c2x -pedantic-errors -Wextra" } */ /* { dg-options "-std=c2x -pedantic-errors -Wextra" } */
...@@ -7,12 +7,5 @@ int ...@@ -7,12 +7,5 @@ int
f (int a) f (int a)
{ {
[[fallthrough]]; /* { dg-error "invalid use of attribute 'fallthrough'" } */ [[fallthrough]]; /* { dg-error "invalid use of attribute 'fallthrough'" } */
switch (a)
{
case 1:
a++;
[[fallthrough]]; /* { dg-warning "attribute 'fallthrough' not preceding a case label or default label" } */
a++;
}
return a; return a;
} }
/* Test C2x attribute syntax. Invalid use of fallthrough attribute in
bad context inside switch. */
/* { dg-do compile } */
/* { dg-options "-std=c2x -pedantic-errors -Wextra" } */
int
f (int a)
{
switch (a)
{
case 1:
a++;
[[fallthrough]]; /* { dg-error "attribute 'fallthrough' not preceding a case label or default label" } */
a++;
[[fallthrough]]; /* { dg-error "attribute 'fallthrough' not preceding a case label or default label" } */
}
return a;
}
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