public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/112463] New: ternary operator / -Wsign-compare inconsistency
@ 2023-11-09 16:38 vincent-gcc at vinc17 dot net
  2023-11-10  3:01 ` [Bug c/112463] " egallager at gcc dot gnu.org
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: vincent-gcc at vinc17 dot net @ 2023-11-09 16:38 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=112463

            Bug ID: 112463
           Summary: ternary operator / -Wsign-compare inconsistency
           Product: gcc
           Version: 13.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: vincent-gcc at vinc17 dot net
  Target Milestone: ---

-Wsign-compare is described in the man page as follows:

  -Wsign-compare
    Warn when a comparison between signed and unsigned  values  could
    produce an incorrect result when the signed value is converted to
    unsigned.   In C++, this warning is also enabled by -Wall.  In C,
    it is also enabled by -Wextra.

But it can emit a warning even in the absence of comparisons between signed and
unsigned values. For instance, it can appear due to the 2nd and 3rd operands of
the ternary operator (these operands are not compared, just selected from the
value of the first operand). This affects the warning output by -Wextra.

Consider the following C code:

#include <stdio.h>
int main (void)
{
  for (int c = -1; c <= 1; c++)
    {
      long long
        i = c == 0 ? 0LL : (c >= 0 ? 1U : -1),
        j = c >= 0 ? (c == 0 ? 0LL : 1U) : -1;
      printf ("i = %lld\nj = %lld\n", i, j);
    }
  return 0;
}

(which shows that the ternary operator is not associative due to type
conversions). With -Wextra, I get:

ternary-op.c: In function ‘main’:
ternary-op.c:7:43: warning: operand of ‘?:’ changes signedness from ‘int’ to
‘unsigned int’ due to unsignedness of other operand [-Wsign-compare]
    7 |         i = c == 0 ? 0LL : (c >= 0 ? 1U : -1),
      |                                           ^~

But the "-Wsign-compare" is incorrect as there are no comparisons between
signed and unsigned values. Only -Wsign-conversion should trigger a warning.

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

* [Bug c/112463] ternary operator / -Wsign-compare inconsistency
  2023-11-09 16:38 [Bug c/112463] New: ternary operator / -Wsign-compare inconsistency vincent-gcc at vinc17 dot net
@ 2023-11-10  3:01 ` egallager at gcc dot gnu.org
  2023-11-10  3:22 ` pinskia at gcc dot gnu.org
  2023-11-10  9:02 ` vincent-gcc at vinc17 dot net
  2 siblings, 0 replies; 4+ messages in thread
From: egallager at gcc dot gnu.org @ 2023-11-10  3:01 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=112463

Eric Gallager <egallager at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |egallager at gcc dot gnu.org
           Keywords|                            |diagnostic

--- Comment #1 from Eric Gallager <egallager at gcc dot gnu.org> ---
I think I've seen this bug before, but I can't seem to find the one I was
thinking it might be a dup of...

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

* [Bug c/112463] ternary operator / -Wsign-compare inconsistency
  2023-11-09 16:38 [Bug c/112463] New: ternary operator / -Wsign-compare inconsistency vincent-gcc at vinc17 dot net
  2023-11-10  3:01 ` [Bug c/112463] " egallager at gcc dot gnu.org
@ 2023-11-10  3:22 ` pinskia at gcc dot gnu.org
  2023-11-10  9:02 ` vincent-gcc at vinc17 dot net
  2 siblings, 0 replies; 4+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-11-10  3:22 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=112463

--- Comment #2 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
So digging through the history and I find this:
https://gcc.gnu.org/pipermail/gcc-patches/1999-December/024572.html

Which added the original place for the warning.
There has been improvements to it later on about enum types and constants but
the option has been that way since then.

One problem with -Wsign-conversion is that it is not enabled with -Wextra/-Wall
.

Maybe there should be a 3rd option added for this that gets enabled for -Wextra
(C)/-Wall(C++) .

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

* [Bug c/112463] ternary operator / -Wsign-compare inconsistency
  2023-11-09 16:38 [Bug c/112463] New: ternary operator / -Wsign-compare inconsistency vincent-gcc at vinc17 dot net
  2023-11-10  3:01 ` [Bug c/112463] " egallager at gcc dot gnu.org
  2023-11-10  3:22 ` pinskia at gcc dot gnu.org
@ 2023-11-10  9:02 ` vincent-gcc at vinc17 dot net
  2 siblings, 0 replies; 4+ messages in thread
From: vincent-gcc at vinc17 dot net @ 2023-11-10  9:02 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=112463

--- Comment #3 from Vincent Lefèvre <vincent-gcc at vinc17 dot net> ---
(In reply to Andrew Pinski from comment #2)
> One problem with -Wsign-conversion is that it is not enabled with
> -Wextra/-Wall .

However, I don't understand why -Wsign-compare is enabled by -Wextra but not
-Wsign-conversion, while these are similar warnings.

Note that when compiling GNU MPFR, *both* give lots of false positives (which
could be avoided, at least for most of them, if PR38470 were fixed).

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

end of thread, other threads:[~2023-11-10  9:02 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-11-09 16:38 [Bug c/112463] New: ternary operator / -Wsign-compare inconsistency vincent-gcc at vinc17 dot net
2023-11-10  3:01 ` [Bug c/112463] " egallager at gcc dot gnu.org
2023-11-10  3:22 ` pinskia at gcc dot gnu.org
2023-11-10  9:02 ` vincent-gcc at vinc17 dot net

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).