[LLVMdev] Is this a bug in clang?

Dimitry Andric dimitry at andric.com
Tue Apr 19 02:07:29 PDT 2011

On 2011-04-19 10:59, Joe Armstrong wrote:
>    I think the correct answer is 41. If my understanding of C is correct
>    (which, or course, it might not be) the incremented value of i++ is
>    first made available at the next sequence point, which is after the
>    ';' at the end of the offending statment statement.
>    j = i++ + 20 + i;

It is not a bug; you are relying on undefined behavior.  Since the +
operator is commutative, the compiler is free to rearrange your
statement to e.g:

j = i + 20 + i++;

or any other possible permutation, all in the name of optimization.
Using different compilers, or even just different architectures or
compile flags, can lead to different results here.

In short, don't do this. :)

More information about the llvm-dev mailing list