[cfe-dev] SwitchStmt cond type and EnumConstantDecl type problems

Douglas Gregor dgregor at apple.com
Wed Feb 3 11:35:59 PST 2010


On Feb 3, 2010, at 10:57 AM, Douglas Gregor wrote:

> 
> On Jan 30, 2010, at 6:57 AM, Michal wrote:
> 
>> On Tue, Jan 12, 2010 at 1:26 AM, Douglas Gregor <dgregor at apple.com> wrote:
>>> 
>>> On Jan 8, 2010, at 4:29 PM, Michal wrote:
>>> 
>>>> Hi,
>>>> when clang parses such code :
>>>> 
>>>> enum X {
>>>> A,
>>>> B,
>>>> C };
>>>> 
>>>> void foo(enum X)
>>>> {
>>>> switch(X) {
>>>>  case A:
>>>>    break;
>>>> }
>>>> }
>>>> 
>>>> value of switch condition expr and CaseStmt->getLHS->evaluateAsInt()
>>>> for each case stmt is ASPInt holding an unsigned value, but type of
>>>> EnumConstantDecl->getInitVal() representing A,B,C is ASPInt holding a
>>>> signed value.
>>>> 
>>>> Is this intentional?
>>> 
>>> Yes. The enum constants have type 'int', which is signed, while the case
>>> expressions will be converted to the type of the enum (whose promoted type
>>> may not be 'int').
>> 
>> For such enum :
>> enum unsignedEnum {
>>       A,
>>       Z = 4294967286
>> };
>> A is signed and Z is unsigned. Is it a bug or I misunderstood something?
> 
> In C, we're outside of the C standard because 4294967286 is not representable in an int, so "technically" we are allowed to do what we're doing.
> 
> 
>> Anyway gcc casts Z to signed integer giving Z == -10.
> 
> 
> ... but this is what's actually important, and we're not following GCC's extensions in this area.
> 
> So, yes, there's a bug here. I know GCC has fixed some bugs in this area in the 4.4/4.5 time-frame, so it would be interesting to see what they do now with this.


Actually, I take that back: there may not be a bug here :)

GCC 4.2 on my platform has Z=4294967286, unless you compile with "-pedantic", in which case it complains and then has Z=-10. 

Clang always has Z=4294967286, but it complains if you compile with "-pedantic".

I'd still like to know what recent GCC's do, but at this point it doesn't look like a Clang bug.

	- Doug



More information about the cfe-dev mailing list