PR 12242 g++ should warn about out-of-range int->enum conversions
Manuel López-Ibáñez
lopezibanez@gmail.com
Thu Aug 7 08:18:00 GMT 2008
I added the testcases from the duplicates to the patch. They also pass.
The message should not mention "static_cast" since the warning may be
triggered by a c_style cast. So I would go for something like:
"the result of the cast is unspecified because %qE is outside the range
[%d,%d] of type %qT"
Updated testcase follows:
// PR 12242: should warn about out-of-range int->enum conversions
// { dg-do compile }
// { dg-options "-Wconversion" }
enum X { A };
enum Y { B, C, D };
void foo () {
X a = static_cast<X> (10); // { dg-warning "warning:\[^\n\]*unspecified" }
X b = static_cast<X> (0);
X c = static_cast<X> (1);
X d = static_cast<X> (2); // { dg-warning "warning:\[^\n\]*unspecified" }
X f = static_cast<X> ((int)A);
X g = static_cast<X> (B);
X h = static_cast<X> (C);
X e = static_cast<X> (D); // { dg-warning "warning\[^\n\]*unspecified" }
}
enum QEvent { x = 42 };
int bar()
{
QEvent x = ( QEvent ) 42000; // { dg-warning "warning\[^\n\]*unspecified" }
return ( int ) x;
}
enum W {a,b,c};
enum Z {d,e,f,g};
void bazz (int, int, int, int);
void baz() {
int three = 3;
int four = 4;
bazz (
W(three),
W(3),
Z(four),
Z(4) // { dg-warning "warning\[^\n\]*unspecified" }
);
}
2008/8/7 Manuel López-Ibáñez <lopezibanez@gmail.com>:
> This patch fixes PR 12242 by warning for unspecified behaviour.
>
> Bootstrapped and regression tested on x86_64-pc-linux-gnu wit
> --enable-languages=all.
>
> OK for trunk?
>
> 2008-08-07 Manuel Lopez-Ibanez <manu@gcc.gnu.org>
>
> PR 12242
> cp/
> * typeck.c (buld_static_cast_1): Warn for out-of-range conversions to enum.
> testsuite/
> * g++.dg/warn/pr12242.C: New.
>
More information about the Gcc-patches
mailing list