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

Douglas Gregor dgregor at apple.com
Wed Feb 3 10:57:00 PST 2010


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.

	- Doug



More information about the cfe-dev mailing list