[cfe-dev] int-to-bool conversion issues

Jared Grubb via cfe-dev cfe-dev at lists.llvm.org
Tue Oct 1 17:00:30 PDT 2019


FWIW, can you file a bug on this? I think at very least clang should have a warning for this. 

Clang-trunk doesn't seem to flag it but GCC 9.2 does. (https://godbolt.org/z/3WLQVd <https://godbolt.org/z/3WLQVd>)

Jared

> El sep. 13, 2019, a las 13:38, Alexandre Isoard via cfe-dev <cfe-dev at lists.llvm.org> escribió:
> 
> Hello,
> 
> I encountered a really annoying issue, I am not sure it is Clang or C++ language issue.
> 
> But basically, the following code:
> 
> bool foo(bool a) {
> return ~a;
> }
> 
> Collapse into:
> 
> bool foo(bool a) {
> return true;
> }
> 
> The explanation is simple:
> - a is implicitly converted to int using zext
> - int operator~(int) has the unexpected behavior described below
> - then icmp ne 0 is used to convert to bool and is always true
> 
> Here is the truth table of bool-to-int + operator~(int) + int-to-bool:
> false → 0b0000 → 0b0000 → true
> true → 0b0001 → 0b1110 → true
> 
> Note that we can solve the issue by using sext for bool-to-int conversions.
> Should we? Is this a known problem?
> 
> -- 
> Alexandre Isoard
> _______________________________________________
> cfe-dev mailing list
> cfe-dev at lists.llvm.org
> https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20191001/5cfe2ceb/attachment.html>


More information about the cfe-dev mailing list