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

Douglas Gregor dgregor at apple.com
Mon Jan 11 16:26:14 PST 2010


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').

> I am trying to implement gcc's -Wswitch warning and this type mismatch
> forbids comparisons between case stmts and enum values. I don't know
> clang/llvm enough to know how to convert safely between ASPInts (how
> should I, if at all?).


You can construct an APSInt from an APInt + sign bit, then use  
APSInt::extend to extend the APSInt to the appropriate bit width. Once  
you've done so, you can compare them.

	- Doug



More information about the cfe-dev mailing list