Issue 208163
Summary [Clang] Non-fixed _BitInt typed enum constant not extended on overflow
Labels
Assignees
Reporter fuhsnn
    https://c.godbolt.org/z/h7d5fKa4K
```C
#include <stdio.h>

int main() {
    enum {
    A = (_BitInt(33))0xFFFF'FFFF,
 B,
    X = _Generic(A, _BitInt(33): 1, default: 0),
    Y = _Generic(B, _BitInt(33): 1, default: 0)
    };

    printf("val A: %lld\n", (long long)A); // 4294967295
    printf("val B: %lld\n", (long long)B); // GCC: 4294967296, Clang: -4294967296
    printf("A is _BitInt(33): %d\n", (int)X); // 1
    printf("B is _BitInt(33): %d\n", (int)Y); // GCC: 0, Clang: 1
}
```

A has the `_BitInt(33)` type because the `0xFFFF'FFFF` value doesn't fit in `int` (for x86). 

B increments A to `0x1'0000'0000` which overflows `_BitInt(33)`, at this point Clang only issues a warning, while GCC extends to a larger type like non-_BitInt types would.

I think relevant text in standard is in 6.7.3.3-12 (N3220):
> (the type of enum constant shall be) ... 

> the type of the integer constant _expression_, if given explicitly with = and if the value of the
integer constant _expression_ is not representable by int; or,

>  type of the value from the previous enumeration constant with one added to it. If such
an integer constant _expression_ would overflow or wraparound the value of the previous
enumeration constant from the addition of one, the type takes on either:

> ... (suitably sized int type)
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to