[cfe-dev] warning with conditional operator and printf, is this a bug ?

Pasi Parviainen pasi.parviainen at iki.fi
Fri Nov 4 15:59:12 PDT 2011


On 4.11.2011 21:10, Abramo Bagnara wrote:
> Il 04/11/2011 08:20, John McCall ha scritto:
>> It's a clear false positive in our format checker.  Promoting an unsigned value to a wider signed type performs a zero-extend, so the distinction between signed and unsigned formats is irrelevant and either %d or %ud should be acceptable.  (The reverse would not be true:  we should still warn about using unsigned formats with promoted signed types).
>
> It is not clear to me how you intend to do that: conditional operator
> operands are promoted *before* to evaluate the operator. Unless you use
> some complex way to track the original type despite the promotions I
> don't see any way to avoid this warning.
>
> Also I don't believe it is so clear that it is a false positive: indeed
> we are passing an integer argument to an unsigned short format.

Hi,

Here's a reason why I think it is clearly a false positive.

<quote>

ISO/IEC 9899:TC3 Commettee Draft - Septemper 7, 2007 W14/N1256

6.5.15 Conditional operator

First semantics paragraph:

"The first operand is evaluated; there is a sequence point after
its evaluation." ... (description about when the second or third
operand are evaluated) ... "the result is the value of the
second or third operand (whichever is evaluated), converted to
the type described below."

Second sematics paragraph:

"If both the second and third operands have arithmetic type, the
result type that would be determined by the usual arithmetic
conversions, were they applied to those two operands, is the
type of the result."

</quote>

Based on this, which I *assume* hasn't changed from the original
ISO/IEC 9899:1999 standard, I can conclude following:

1) There is no a reason to do any arithmetic conversions to the
second or third operand of the conditional operator in this case.
So the result of both operands should be original type, which is
unit16_t for both operands.

2) As what comes for the first operand, I can see that the "Usual
Arithmetic Conversions" rules do apply to it. But since it is
only a sequence point, it is irrelevant in this case (or in any
case, if that matters).

So, according those points, which I concluded, the warning is a
false positive indeed.

Pasi.




More information about the cfe-dev mailing list