public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
* sign-compare warning for short and char
@ 2018-02-20 14:44 Christophe de Dinechin
  2018-02-20 15:15 ` Jonathan Wakely
  0 siblings, 1 reply; 4+ messages in thread
From: Christophe de Dinechin @ 2018-02-20 14:44 UTC (permalink / raw)
  To: gcc

I wonder why -Wsign-compare only warns when there is no int promotion?

No warning for this, where the result is “surprisingly” false because of int promotion:
   signed char i = (signed char) -3;
   unsigned char j = (unsigned char) -3;
   printf("i=%x j=%x i==j=%d\n", i, j, i==j);
gcc  -Wsign-compare ~/test.c -o /tmp/glop && /tmp/glop
i=fffffffd j=fd i==j=0

But a warning for this where the result is true (which I believe represents a lower risk than the above case):
    signed int i = (signed int) -3;
    unsigned int j = (unsigned int) -3;
    printf("i=%x j=%x i==j=%d\n", i, j, i==j);

% gcc  -Wsign-compare ~/test.c -o /tmp/glop && /tmp/glop
i=fffffffd j=fd i==j=0
/home/ddd/test.c:7:42: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     printf("i=%x j=%x i==j=%d\n", i, j, i==j);
                                          ^~
i=fffffffd j=fffffffd i==j=1

I did not find a rationale in the documentation, nor a good alternative flag that would focus on comparisons with promotion.

Tested with several semi-old variants of GCC, e.g. 4.8.5, and more recent ones e.g. 7.2.1, with consistent results.
gcc --version
gcc (GCC) 7.2.1 20170915 (Red Hat 7.2.1-2)


Thanks,
Christophe



^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: sign-compare warning for short and char
  2018-02-20 14:44 sign-compare warning for short and char Christophe de Dinechin
@ 2018-02-20 15:15 ` Jonathan Wakely
  2018-02-20 15:43   ` Christophe de Dinechin
  0 siblings, 1 reply; 4+ messages in thread
From: Jonathan Wakely @ 2018-02-20 15:15 UTC (permalink / raw)
  To: Christophe de Dinechin; +Cc: gcc

This question belongs on the gcc-help list really.


On 20 February 2018 at 14:44, Christophe de Dinechin
<dinechin@redhat.com> wrote:
> I wonder why -Wsign-compare only warns when there is no int promotion?

I suspect the correct-but-not-helpful answer is that after integer
promotion the operands have the same type, and so there's no
comparison between signed and unsigned types.

But the compiler could make the warning apply *before* doing integer
promotion, to diagnose this case.

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: sign-compare warning for short and char
  2018-02-20 15:15 ` Jonathan Wakely
@ 2018-02-20 15:43   ` Christophe de Dinechin
  2018-02-20 19:49     ` Jonathan Wakely
  0 siblings, 1 reply; 4+ messages in thread
From: Christophe de Dinechin @ 2018-02-20 15:43 UTC (permalink / raw)
  To: Jonathan Wakely; +Cc: gcc



> On 20 Feb 2018, at 16:15, Jonathan Wakely <jwakely.gcc@gmail.com> wrote:
> 
> This question belongs on the gcc-help list really.

I posted here because I saw it as a possible diagnostic bug / limitation. Do such things go to gcc-help? Or is it that you thought I was asking for the correct option?

> On 20 February 2018 at 14:44, Christophe de Dinechin
> <dinechin@redhat.com> wrote:
>> I wonder why -Wsign-compare only warns when there is no int promotion?
> 
> I suspect the correct-but-not-helpful answer is that after integer
> promotion the operands have the same type, and so there's no
> comparison between signed and unsigned types.

That’s an implementation explanation, not a rationale, right?

> But the compiler could make the warning apply *before* doing integer
> promotion, to diagnose this case.

That is what I am thinking, but I was wondering if there was not some rationale making this a bad idea.

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: sign-compare warning for short and char
  2018-02-20 15:43   ` Christophe de Dinechin
@ 2018-02-20 19:49     ` Jonathan Wakely
  0 siblings, 0 replies; 4+ messages in thread
From: Jonathan Wakely @ 2018-02-20 19:49 UTC (permalink / raw)
  To: Christophe de Dinechin; +Cc: gcc

On 20 February 2018 at 15:43, Christophe de Dinechin wrote:
> I posted here because I saw it as a possible diagnostic bug / limitation. Do such things go to gcc-help? Or is it that you thought I was asking for the correct option?

Bug reports and enhancement requests go to bugzilla not here, and
questions about using GCC (including warnings and "why doesn't this
work?" stuff) belongs on gcc-help.

Although arguably this question is somewhere halfway between gcc@ and gcc-help@

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2018-02-20 19:49 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-02-20 14:44 sign-compare warning for short and char Christophe de Dinechin
2018-02-20 15:15 ` Jonathan Wakely
2018-02-20 15:43   ` Christophe de Dinechin
2018-02-20 19:49     ` Jonathan Wakely

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).