Commit f339eb66 by Marek Polacek Committed by Marek Polacek

PR c++/90805 - detect narrowing in case values.

	* decl.c (case_conversion): Detect narrowing in case values.

	* c-c++-common/pr89888.c: Update expected dg-error.
	* g++.dg/cpp0x/Wnarrowing17.C: New test.
	* g++.dg/cpp0x/enum28.C: Update expected dg-error.

From-SVN: r273976
parent 3a66e68a
2019-08-01 Marek Polacek <polacek@redhat.com>
PR c++/90805 - detect narrowing in case values.
* decl.c (case_conversion): Detect narrowing in case values.
2019-07-31 Paolo Carlini <paolo.carlini@oracle.com> 2019-07-31 Paolo Carlini <paolo.carlini@oracle.com>
* decl2.c (delete_sanity): Improve diagnostic locations, use * decl2.c (delete_sanity): Improve diagnostic locations, use
......
...@@ -3631,16 +3631,23 @@ case_conversion (tree type, tree value) ...@@ -3631,16 +3631,23 @@ case_conversion (tree type, tree value)
value = mark_rvalue_use (value); value = mark_rvalue_use (value);
if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type))
type = type_promotes_to (type);
tree ovalue = value;
/* The constant-expression VALUE shall be a converted constant expression
of the adjusted type of the switch condition, which doesn't allow
narrowing conversions. */
value = build_converted_constant_expr (type, value, tf_warning_or_error);
if (cxx_dialect >= cxx11 if (cxx_dialect >= cxx11
&& (SCOPED_ENUM_P (type) && (SCOPED_ENUM_P (type)
|| !INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (value)))) || !INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (ovalue))))
{ /* Use the converted value. */;
if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type)) else
type = type_promotes_to (type); /* The already integral case. */
value = (perform_implicit_conversion_flags value = ovalue;
(type, value, tf_warning_or_error,
LOOKUP_IMPLICIT | LOOKUP_NO_NON_INTEGRAL));
}
return cxx_constant_value (value); return cxx_constant_value (value);
} }
......
2019-08-01 Marek Polacek <polacek@redhat.com>
PR c++/90805 - detect narrowing in case values.
* c-c++-common/pr89888.c: Update expected dg-error.
* g++.dg/cpp0x/Wnarrowing17.C: New test.
* g++.dg/cpp0x/enum28.C: Update expected dg-error.
2019-08-01 Wilco Dijkstra <wdijkstr@arm.com> 2019-08-01 Wilco Dijkstra <wdijkstr@arm.com>
* gcc/testsuite/g++.dg/lto/pr89330_0.C: Add effective-target shared. * gcc/testsuite/g++.dg/lto/pr89330_0.C: Add effective-target shared.
......
...@@ -11,8 +11,8 @@ foo (unsigned char x) ...@@ -11,8 +11,8 @@ foo (unsigned char x)
{ {
case -1: y = -1; break; /* { dg-message "previously used here" } */ case -1: y = -1; break; /* { dg-message "previously used here" } */
/* { dg-warning "case label value is less than minimum value for type" "" { target *-*-* } .-1 } */ /* { dg-warning "case label value is less than minimum value for type" "" { target *-*-* } .-1 } */
case 0xffffffff: y = 0xffffffff; break; /* { dg-error "duplicate case value" } */ case 0xffffffff: y = 0xffffffff; break; /* { dg-error "duplicate case value|narrowing" } */
case ~0U: y = ~0U; break; /* { dg-error "duplicate case value" } */ case ~0U: y = ~0U; break; /* { dg-error "duplicate case value|narrowing" } */
} }
} }
......
// PR c++/90805 - detect narrowing in case values.
// { dg-do compile { target c++11 } }
void f(int i, char c, unsigned u)
{
switch (i)
{
case 2149056512u:; // { dg-error "narrowing conversion of .2149056512. from .unsigned int. to .int." }
case (long long int) 1e10:; // { dg-error "narrowing conversion of .10000000000. from .long long int. to .int." }
// { dg-warning "overflow in conversion" "overflow" { target *-*-* } .-1 }
}
switch (c)
// No narrowing, the adjusted type is int.
case 300:; // { dg-warning "exceeds maximum value for type" }
switch (u)
case -42:; // { dg-error "narrowing conversion of .-42. from .int. to .unsigned int." }
}
...@@ -7,11 +7,11 @@ void f(int i) ...@@ -7,11 +7,11 @@ void f(int i)
{ {
switch (i) switch (i)
{ {
case 1.0:; // { dg-error "could not convert" } case 1.0:; // { dg-error "could not convert|conversion from" }
} }
switch (i) switch (i)
{ {
case g():; // { dg-error "could not convert" } case g():; // { dg-error "could not convert|conversion from" }
} }
} }
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