[Clang] Incorrect overflow check and evaluation for MS integer literal suffixes (i8/i16/i32/i64)
Inspired by #212504
I discovered other issues while trying to fix this assertion.
For code : static_assert(-255i8, ""); Surprisingly, the current mainline Clang doesn't produce any errors or warnings at all.
The current Clang mainline produces neither errors nor warnings for this, yet the value is actually truncated; consequently, it fails the assertion static_assert(-255i8 == -255, "-255 != -255 ?").
https://godbolt.org/z/K88b1sxz9
This drew my attention to the fact that the logic handling Microsoft-style integer literal extensions appears quite fragile. I plan to submit a patch later, though some edge cases may still require further discussion.
For example, how should -128i8 be handled?
@llvm/issue-subscribers-clang-frontend
Author: Shengxin Pei (TPPPP72)
I discovered other issues while trying to fix this assertion.
For code : static_assert(-255i8, ""); Surprisingly, the current mainline Clang doesn't produce any errors or warnings at all.
The current Clang mainline produces neither errors nor warnings for this, yet the value is actually truncated; consequently, it fails the assertion static_assert(-255i8 == -255, "-255 != -255 ?").
https://godbolt.org/z/K88b1sxz9
This drew my attention to the fact that the logic handling Microsoft-style integer literal extensions appears quite fragile. I plan to submit a patch later, though some edge cases may still require further discussion.
For example, how should -128i8 be handled?
Clang behaves same as MSVC for these. Godbolt link.
Although it's highly confusing to accept 128i8..255i8. Maybe we should also suggest MSVC to reject these.
Also, the i8 suffix produces char values and the signedness of char can be affected by Clang's -funsigned-char and MSVC's /J.
If char is set to unsigned, then 128i8..255i8 work as expected, but the i8 suffix becomes confusing instead.
In my view, when char is unsigned, the i8 literal does not serve its intended purpose and therefore should generate a warning. Additionally, it could be suggested that MSVC do the same.
@AaronBallman CC
Alternatively, when char is unsigned, the type of the i8 literal could be changed to signed char, but this would need to be coordinated with MSVC.
If we are mirroring MSVC behavior in -fms-extensions then that seems like it is working as intended and someone would have to open a ticket with them to see if this is intended behavior or not.
If we are mirroring MSVC behavior in
-fms-extensionsthen that seems like it is working as intended and someone would have to open a ticket with them to see if this is intended behavior or not.
As it stands, the assertion failure definitely needs to be fixed. Whether a warning or even an error should be issued depends on Microsoft's feedback.
If we are mirroring MSVC behavior in
-fms-extensionsthen that seems like it is working as intended and someone would have to open a ticket with them to see if this is intended behavior or not.
@shafik I've opened https://developercommunity.visualstudio.com/t/11127972 for this. In any case, we should fix the related crash first (see #212743).