[cfe-dev] Enum constants of different type in same enum

Aleksei Sidorin via cfe-dev cfe-dev at lists.llvm.org
Tue Apr 3 07:03:05 PDT 2018


Hello,

Consider an enum: enum SignedAndUnsignedImplicit { SUI_Signed = 2, 
SUI_Unsigned = 4294967295 };

If this code is compiled as C, its first enum constant has type 'int' 
but the second has type 'unsigned int'. Is this intentional?

|-EnumDecl 0x56500d8615c8 <line:92:1, line:93:60> line:92:6 
SignedAndUnsignedImplicit
| |-EnumConstantDecl 0x56500d8616a0 <col:34, col:47> col:34 SUI_Signed 'int'
| | `-IntegerLiteral 0x56500d861680 <col:47> 'int' 2
| `-EnumConstantDecl 0x56500d861710 <line:93:34, col:49> col:34 
SUI_Unsigned 'unsigned int'
|   `-ImplicitCastExpr 0x56500d861760 <col:49> 'unsigned int' <IntegralCast>
|     `-IntegerLiteral 0x56500d8616f0 <col:49> 'long' 4294967295

I know that the second constant is malformed in C because it cannot be 
represented as 'int'. However, there are some more test cases:

// NOTE: Both enum elements are 'unsigned int' actually due to an 
implicit cast.
enum SignedAndUnsignedExplicitBig { SUEB_Signed = 4294967295LL, 
SUEB_Unsigned = 4294967295ULL };

// NOTE: Both enum elements are 'int' actually due to an implicit cast.
enum SignedAndUnsignedExplicitSmall { SUES_Signed = 2L, SUES_Unsigned = 
2ULL };

// NOTE: Both enum elements are 'unsigned long' actually due to an 
implicit cast.
enum SignedAndUnsignedExplicitLL { SUEL_Signed = 5294967295LL, 
SUEL_Unsigned = 5294967295ULL };

And the issue is not seen in these cases: all enum values have same type.

Thank you in advance.


-- 
Best regards,
Aleksei Sidorin,
SRR, Samsung Electronics



More information about the cfe-dev mailing list