There is a bug in the evaluation of the conditional operator when it is used as the right hand side argument of an == or != test in a #if. I can't see any logic in its behaviour, see code examples….
Code examples: #if ( ( 1 ? 1 : 0 ) == 10 ) #pragma message_test1NOK #else #pragma message_test1OK #endif
#if (10 == ( 1 ? 1 : 0 ) ) #pragma message_test2NOK #else #pragma message_test2OK #endif
#if (10 == ( 1 ? 10 : 0 ) ) #pragma message_test3OK #else #pragma message_test3NOK #endif
Output: pragma 'message_test1OK' pragma 'message_test2NOK' pragma 'message_test3NOK'
Just in case anyone else finds this via a search engine, the bug is a typo in the eval_signed function in mcpp_eval.c. The line:
v1 = (--*valpp)->val ? v1 : v2;
should read
valp--; v1 = (valp->val) ? v1 : v2;
because later in the code the value of valpp is updated from valp. The eval_unsigned function has this right already.
Log in to post a comment.
There is a bug in the evaluation of the conditional operator when it is used as the right hand side argument of an == or != test in a #if.
I can't see any logic in its behaviour, see code examples….
Code examples:
#if ( ( 1 ? 1 : 0 ) == 10 )
#pragma message_test1NOK
#else
#pragma message_test1OK
#endif
#if (10 == ( 1 ? 1 : 0 ) )
#pragma message_test2NOK
#else
#pragma message_test2OK
#endif
#if (10 == ( 1 ? 10 : 0 ) )
#pragma message_test3OK
#else
#pragma message_test3NOK
#endif
Output:
pragma 'message_test1OK'
pragma 'message_test2NOK'
pragma 'message_test3NOK'
Just in case anyone else finds this via a search engine, the bug is a typo in the eval_signed function in mcpp_eval.c. The line:
should read
because later in the code the value of valpp is updated from valp. The eval_unsigned function has this right already.