https://bugs.llvm.org/show_bug.cgi?id=50038
Bug ID: 50038
Summary: Miscompilation of for-loop with a declaration
condition initialized with ternary operator
Product: clang
Version: trunk
Hardware: PC
OS: Windows NT
Status: NEW
Severity: enhancement
Priority: P
Component: -New Bugs
Assignee: unassignedclangb...@nondot.org
Reporter: egor.suvo...@gmail.com
CC: htmldevelo...@gmail.com, llvm-bugs@lists.llvm.org,
neeil...@live.com, richard-l...@metafoo.co.uk
Consider the following code:
#include <iostream>
int main()
{
for (int i = 0; int x = i < 2 ? 1 : 0; i++) {
std::cout << x;
}
}
Its expected output is 11: there are two iterations with i=0 (x=1) and i=1
(x=1), and then the final iterations with i=2 (x=0) stops the loop.
However, clang miscompiles it to produce an infinite series of 000000...
It also gives following warnings:
<source>:5:20: warning: variable 'x' is uninitialized when used here
[-Wuninitialized]
std::cout << x;
^
<source>:4:19: note: variable 'x' is declared here
for (int i = 0; int x = i < 2 ? 1 : 0; i++) {
The behavior traces back as far as clang 8.0.0, see Godbolt:
https://godbolt.org/z/jMb37G7nq
If I replace `i < 2 ? 1 : 0` with `2 - i`, the output is `21` as expected.
--
You are receiving this mail because:
You are on the CC list for the bug.
_______________________________________________
llvm-bugs mailing list
llvm-bugs@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs