public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/38341]  New: Wrong warning comparison of promoted ~unsigned with unsigned
@ 2008-12-01  8:36 fredrik dot hederstierna at securitas-direct dot com
  2008-12-01 10:02 ` [Bug c/38341] " rguenth at gcc dot gnu dot org
                   ` (8 more replies)
  0 siblings, 9 replies; 13+ messages in thread
From: fredrik dot hederstierna at securitas-direct dot com @ 2008-12-01  8:36 UTC (permalink / raw)
  To: gcc-bugs

Compiler gives wrong warning for "comparison of promoted ~unsigned with
unsigned" when compiling with ARM-ELF.

Submit script for building arm-elf toolchain and testcode.

Compilation using;

arm-elf-gcc -c cast.c -W
cast.c: In function 'test_cast':
cast.c:13: warning: comparison of promoted ~unsigned with unsigned

Best Regards
Fredrik Hederstierna

============
FILE: cast.c
============

typedef unsigned char u8_t;
void test_cast(void) {
  unsigned char c1 = 0;
  unsigned char c2 = 0;  
  u8_t u1 = 0;
  u8_t u2 = 0;  
  if (c1 == (unsigned char)(~c2)) {
  }
  if (u1 == (u8_t)(~u2)) {  // THIS WILL GIVE WARNING
  }
}



========================
FILE: build_toolchain.sh
========================

set -e -x 

TARGET=arm-elf 

BINUTILS_VERSION=2.19
GCC_VERSION=4.3.2
NEWLIB_VERSION=1.16.0
GDB_VERSION=6.8
INSIGHT_VERSION=6.8

DEST="/usr/local/gcc/arm-elf-tools-$GCC_VERSION"

BINUTILS_DIR="binutils-$BINUTILS_VERSION" 
GCC_DIR="gcc-$GCC_VERSION" 
NEWLIB_DIR="newlib-$NEWLIB_VERSION" 
GDB_DIR="gdb-$GDB_VERSION"
INSIGHT_DIR="insight-$INSIGHT_VERSION"

# set rwx access

umask 022 

# unpack tar-balls

rm -fr "$BINUTILS_DIR" "$GCC_DIR" "$NEWLIB_DIR" "$GDB_DIR" "$INSIGHT_DIR"
tar xvjf "binutils-$BINUTILS_VERSION.tar.bz2" 
tar xvjf "gcc-$GCC_VERSION.tar.bz2" 
tar xvzf "newlib-$NEWLIB_VERSION.tar.gz" 
tar xvjf "gdb-$GDB_VERSION.tar.bz2" 
tar xvjf "insight-$INSIGHT_VERSION.tar.bz2" 

cd "$GCC_DIR"
ln -s "../$NEWLIB_DIR/newlib" newlib 
ln -s "../$NEWLIB_DIR/libgloss" libgloss 
cd ..

rm -fr build 
mkdir -p build/binutils build/gcc build/gdb build/insight build/newlib

# Build binutils

cd build/binutils 
"../../$BINUTILS_DIR/configure" --target="$TARGET" --prefix="$DEST"
--disable-nls
make LDFLAGS=-s all install 

# Build GCC

cd ../gcc 
PATH="$DEST/bin:$PATH" 
"../../$GCC_DIR/configure" --enable-languages=c,c++ --target="$TARGET"
--prefix="$DEST" --with-gnu-as --with-gnu-ld --disable-nls --with-newlib
--disable-__cxa_atexit --with-ecos
make LDFLAGS=-s all all-gcc all-target-libstdc++-v3 install install-gcc
install-target-libstdc++-v3

# Build GDB and Insight

cd ../gdb
# Without insight
"../../$GDB_DIR/configure" --target="$TARGET" --prefix="$DEST"
make -w all install

cd ../insight
# With insight
"../../$INSIGHT_DIR/configure" --target="$TARGET" --prefix="$DEST"
make -w all install

# Remove uncompressed sources

cd ../.. 
rm -fr "$BINUTILS_DIR" "$GCC_DIR" "$NEWLIB_DIR" "$GDB_DIR" "$INSIGHT_DIR" build


-- 
           Summary: Wrong warning comparison of promoted ~unsigned with
                    unsigned
           Product: gcc
           Version: 4.3.2
            Status: UNCONFIRMED
          Severity: minor
          Priority: P3
         Component: c
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: fredrik dot hederstierna at securitas-direct dot com
  GCC host triplet: i686-pc-linux-gnu
GCC target triplet: arm-elf-gcc


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=38341


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

* [Bug c/38341] Wrong warning comparison of promoted ~unsigned with unsigned
  2008-12-01  8:36 [Bug c/38341] New: Wrong warning comparison of promoted ~unsigned with unsigned fredrik dot hederstierna at securitas-direct dot com
@ 2008-12-01 10:02 ` rguenth at gcc dot gnu dot org
  2008-12-01 12:41 ` fredrik dot hederstierna at securitas-direct dot com
                   ` (7 subsequent siblings)
  8 siblings, 0 replies; 13+ messages in thread
From: rguenth at gcc dot gnu dot org @ 2008-12-01 10:02 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #1 from rguenth at gcc dot gnu dot org  2008-12-01 10:01 -------
  /* Warn if two unsigned values are being compared in a size larger
     than their original size, and one (and only one) is the result of
     a `~' operator.  This comparison will always fail.

     Also warn if one operand is a constant, and the constant does not
     have all bits set that are set in the ~ operand when it is
     extended.  */

note that integer promotion is done on the operand(!) of ~.  So u1 ==
(u8_t)(~u2)
is equal to

   (int)u1 == (int)(u8_t)(~(int)u2)

that we do not warn for the first case is because it is optimized to
u1 == ~u2 before.

Why do you think the warning is incorrect?


-- 

rguenth at gcc dot gnu dot org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |diagnostic


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=38341


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

* [Bug c/38341] Wrong warning comparison of promoted ~unsigned with unsigned
  2008-12-01  8:36 [Bug c/38341] New: Wrong warning comparison of promoted ~unsigned with unsigned fredrik dot hederstierna at securitas-direct dot com
  2008-12-01 10:02 ` [Bug c/38341] " rguenth at gcc dot gnu dot org
@ 2008-12-01 12:41 ` fredrik dot hederstierna at securitas-direct dot com
  2008-12-01 12:50 ` rguenth at gcc dot gnu dot org
                   ` (6 subsequent siblings)
  8 siblings, 0 replies; 13+ messages in thread
From: fredrik dot hederstierna at securitas-direct dot com @ 2008-12-01 12:41 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #2 from fredrik dot hederstierna at securitas-direct dot com  2008-12-01 12:40 -------
Then why dont we get warning on the first if-statement?
Shouldnt these lines be equal?

  if (c1 == (unsigned char)(~c2)) {
  }
  if (u1 == (u8_t)(~u2)) {  // THIS WILL GIVE WARNING
  }

The first if-statement does not give warnings, should this be evaluated the
same way?

if ((int)c1 == (int)(unsigned char)(~(int)c2)) {

My idea was that either of the if-statements are wrong. Either both or none
should give warnings, or am I wrong? The typedef to "unsigned char" should be
the same as using primitive types regarding this warning, or?


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=38341


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

* [Bug c/38341] Wrong warning comparison of promoted ~unsigned with unsigned
  2008-12-01  8:36 [Bug c/38341] New: Wrong warning comparison of promoted ~unsigned with unsigned fredrik dot hederstierna at securitas-direct dot com
  2008-12-01 10:02 ` [Bug c/38341] " rguenth at gcc dot gnu dot org
  2008-12-01 12:41 ` fredrik dot hederstierna at securitas-direct dot com
@ 2008-12-01 12:50 ` rguenth at gcc dot gnu dot org
  2008-12-01 12:56 ` fredrik dot hederstierna at securitas-direct dot com
                   ` (5 subsequent siblings)
  8 siblings, 0 replies; 13+ messages in thread
From: rguenth at gcc dot gnu dot org @ 2008-12-01 12:50 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #3 from rguenth at gcc dot gnu dot org  2008-12-01 12:49 -------
As I said, for the first case we optimize away the promotions before the
warning
code comes along.


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=38341


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

* [Bug c/38341] Wrong warning comparison of promoted ~unsigned with unsigned
  2008-12-01  8:36 [Bug c/38341] New: Wrong warning comparison of promoted ~unsigned with unsigned fredrik dot hederstierna at securitas-direct dot com
                   ` (2 preceding siblings ...)
  2008-12-01 12:50 ` rguenth at gcc dot gnu dot org
@ 2008-12-01 12:56 ` fredrik dot hederstierna at securitas-direct dot com
  2008-12-01 13:36 ` fredrik dot hederstierna at securitas-direct dot com
                   ` (4 subsequent siblings)
  8 siblings, 0 replies; 13+ messages in thread
From: fredrik dot hederstierna at securitas-direct dot com @ 2008-12-01 12:56 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #4 from fredrik dot hederstierna at securitas-direct dot com  2008-12-01 12:55 -------
Heres another example, then I do not think the warnings are due to
optimization.
I have same warnings with both -O0 and -O3.


#include <stdio.h>

typedef unsigned char u8_t;

void test_cast(unsigned char c1, unsigned char c2, u8_t u1, u8_t u2)
{
  if (c1 == (unsigned char)(~c2)) {
    printf("No warning");
  }
  if (c1 == ~c2) {
    printf("This gives warning");
  }
  if (u1 == (u8_t)(~u2)) {
    printf("This gives warning");
  }
  if ((unsigned char)u1 == (unsigned char)(~u2)) {
    printf("This gives warning");
  }
}


The original code that caused this warnings are the TCP/IP stack lwIP, then I
constructed this minimal example.

Original code from lwIP TCP/IP stack:
-------------------------------------

static u8_t ip_reassbitmap[IP_REASS_BUFSIZE / (8 * 8) + 1];
static const u8_t bitmap_bits[8] = { 0xff, 0x7f, 0x3f, 0x1f, 0x0f, 0x07, 0x03,
0x01 };

/.../
      if (ip_reassbitmap[ip_reasslen / (8 * 8)] !=
        (u8_t) ~ bitmap_bits[ip_reasslen / 8 & 7]) {
/.../


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=38341


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

* [Bug c/38341] Wrong warning comparison of promoted ~unsigned with unsigned
  2008-12-01  8:36 [Bug c/38341] New: Wrong warning comparison of promoted ~unsigned with unsigned fredrik dot hederstierna at securitas-direct dot com
                   ` (3 preceding siblings ...)
  2008-12-01 12:56 ` fredrik dot hederstierna at securitas-direct dot com
@ 2008-12-01 13:36 ` fredrik dot hederstierna at securitas-direct dot com
  2008-12-02 12:15 ` rguenth at gcc dot gnu dot org
                   ` (3 subsequent siblings)
  8 siblings, 0 replies; 13+ messages in thread
From: fredrik dot hederstierna at securitas-direct dot com @ 2008-12-01 13:36 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #5 from fredrik dot hederstierna at securitas-direct dot com  2008-12-01 13:35 -------
On Intel i386-GCC (4.2.3) we just get warning only for the line

  if (c1 == ~c2)

The other lines does not give warnings, so maybe its just the ARM-backend that
catch this warning.

I guess you mean that for ARM target the optimization tree does things that
silence the warning. Is it good that optimizations can silence possible
warnings/errors? And that it differs depending on which backend I'm running?


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=38341


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

* [Bug c/38341] Wrong warning comparison of promoted ~unsigned with unsigned
  2008-12-01  8:36 [Bug c/38341] New: Wrong warning comparison of promoted ~unsigned with unsigned fredrik dot hederstierna at securitas-direct dot com
                   ` (4 preceding siblings ...)
  2008-12-01 13:36 ` fredrik dot hederstierna at securitas-direct dot com
@ 2008-12-02 12:15 ` rguenth at gcc dot gnu dot org
  2009-02-23 23:53 ` john dot carter at tait dot co dot nz
                   ` (2 subsequent siblings)
  8 siblings, 0 replies; 13+ messages in thread
From: rguenth at gcc dot gnu dot org @ 2008-12-02 12:15 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #6 from rguenth at gcc dot gnu dot org  2008-12-02 12:12 -------
*** Bug 38370 has been marked as a duplicate of this bug. ***


-- 

rguenth at gcc dot gnu dot org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |doko at ubuntu dot com


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=38341


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

* [Bug c/38341] Wrong warning comparison of promoted ~unsigned with unsigned
  2008-12-01  8:36 [Bug c/38341] New: Wrong warning comparison of promoted ~unsigned with unsigned fredrik dot hederstierna at securitas-direct dot com
                   ` (5 preceding siblings ...)
  2008-12-02 12:15 ` rguenth at gcc dot gnu dot org
@ 2009-02-23 23:53 ` john dot carter at tait dot co dot nz
  2009-02-24  0:40 ` michael dot malone at tait dot co dot nz
  2009-02-24  0:43 ` michael dot malone at tait dot co dot nz
  8 siblings, 0 replies; 13+ messages in thread
From: john dot carter at tait dot co dot nz @ 2009-02-23 23:53 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #7 from john dot carter at tait dot co dot nz  2009-02-23 23:53 -------
R Guenther said...
>  (int)u1 == (int)(u8_t)(~(int)u2)
>
>  that we do not warn for the first case is because it is optimized to
>    u1 == ~u2 before.
>
>   Why do you think the warning is incorrect?
-----------------------------------------------

I would expect 

  u2 to be promoted to a 4 byte int

  ~ to do the ones complement on a 4 byte int

  (unsigned char)~u2 to truncate to one byte discarding the most significant 3
bytes

  and then the result get promoted to an int to evaluate the ==

ie. The cast cannot get optimized away, it's not a null op, it discards the
most significant bytes. 

ie. (int)(unsigned char)~(int)u2 is not equivalent to
    ~(int)u2

ie. This is a bug


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=38341


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

* [Bug c/38341] Wrong warning comparison of promoted ~unsigned with unsigned
  2008-12-01  8:36 [Bug c/38341] New: Wrong warning comparison of promoted ~unsigned with unsigned fredrik dot hederstierna at securitas-direct dot com
                   ` (6 preceding siblings ...)
  2009-02-23 23:53 ` john dot carter at tait dot co dot nz
@ 2009-02-24  0:40 ` michael dot malone at tait dot co dot nz
  2009-02-24  0:43 ` michael dot malone at tait dot co dot nz
  8 siblings, 0 replies; 13+ messages in thread
From: michael dot malone at tait dot co dot nz @ 2009-02-24  0:40 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #8 from michael dot malone at tait dot co dot nz  2009-02-24 00:40 -------
#ifdef UINT
  #include <stdint.h>
  #define TYPE uint16_t
#else
  #define TYPE unsigned short int
#endif

#define VALUE 0xFF

int main(void);

int main() {
   TYPE variable_a = ~VALUE;
   TYPE variable_b = VALUE;
   TYPE result;

   #ifdef ASSIGN
   TYPE tmp = ~variable_a;
   result = (variable_b == tmp);
   #else
   result = (variable_b == (TYPE) ~variable_a);
   #endif

   return 0;
}

Further to John's input, here is a sample program which shows up why this bug
is interesting.  When one uses a typedef'd type, the promoted comparison
warning is displayed.  When one does not, it isn't!

This is not the case for gcc-4.2.3 -both variants compile without warnings.

The compile commands I used were:
gcc gcc_bug.c -W -Wall -o bug
and
gcc gcc_bug.c -W -Wall -DUINT -o bug


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=38341


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

* [Bug c/38341] Wrong warning comparison of promoted ~unsigned with unsigned
  2008-12-01  8:36 [Bug c/38341] New: Wrong warning comparison of promoted ~unsigned with unsigned fredrik dot hederstierna at securitas-direct dot com
                   ` (7 preceding siblings ...)
  2009-02-24  0:40 ` michael dot malone at tait dot co dot nz
@ 2009-02-24  0:43 ` michael dot malone at tait dot co dot nz
  8 siblings, 0 replies; 13+ messages in thread
From: michael dot malone at tait dot co dot nz @ 2009-02-24  0:43 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #9 from michael dot malone at tait dot co dot nz  2009-02-24 00:43 -------
I forgot to mention, if you assign to an intermediate variable, the warning
also disappears which is the behaviour I would expect from an explicit cast.


-- 

michael dot malone at tait dot co dot nz changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |michael dot malone at tait
                   |                            |dot co dot nz


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=38341


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

* [Bug c/38341] Wrong warning comparison of promoted ~unsigned with unsigned
       [not found] <bug-38341-4@http.gcc.gnu.org/bugzilla/>
  2014-01-29 16:01 ` gjl at gcc dot gnu.org
  2023-05-20  4:26 ` pinskia at gcc dot gnu.org
@ 2023-06-29 14:52 ` wolter.hellmundvega at tevva dot com
  2 siblings, 0 replies; 13+ messages in thread
From: wolter.hellmundvega at tevva dot com @ 2023-06-29 14:52 UTC (permalink / raw)
  To: gcc-bugs

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

wolter.hellmundvega at tevva dot com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |wolter.hellmundvega at tevva dot c
                   |                            |om

--- Comment #14 from wolter.hellmundvega at tevva dot com ---
I'm not sure this is fixed, please correct me if wrong, but

    #include <stdio.h>
    #include <stdint.h>

    int main(void)
    {
        const uint8_t u = 0U;
        const uint8_t y = (uint8_t) ~u;

        return ((uint8_t) u != (uint8_t) ~y);
    }

gives warning

    test.c: In function ‘main’:
    test.c:9:25: warning: comparison of promoted bitwise complement of an
unsigned value with unsigned [-Wsign-compare]
        9 |     return ((uint8_t) u != (uint8_t) ~y);
          |                         ^~

Does this not mean that this issue is still present?

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

* [Bug c/38341] Wrong warning comparison of promoted ~unsigned with unsigned
       [not found] <bug-38341-4@http.gcc.gnu.org/bugzilla/>
  2014-01-29 16:01 ` gjl at gcc dot gnu.org
@ 2023-05-20  4:26 ` pinskia at gcc dot gnu.org
  2023-06-29 14:52 ` wolter.hellmundvega at tevva dot com
  2 siblings, 0 replies; 13+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-05-20  4:26 UTC (permalink / raw)
  To: gcc-bugs

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

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|---                         |DUPLICATE

--- Comment #13 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
So this has been fixed on all of the active branches. Since PR 107465 was the
one recorded in the changelog, closing as a dup of that one.

*** This bug has been marked as a duplicate of bug 107465 ***

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

* [Bug c/38341] Wrong warning comparison of promoted ~unsigned with unsigned
       [not found] <bug-38341-4@http.gcc.gnu.org/bugzilla/>
@ 2014-01-29 16:01 ` gjl at gcc dot gnu.org
  2023-05-20  4:26 ` pinskia at gcc dot gnu.org
  2023-06-29 14:52 ` wolter.hellmundvega at tevva dot com
  2 siblings, 0 replies; 13+ messages in thread
From: gjl at gcc dot gnu.org @ 2014-01-29 16:01 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=38341

Georg-Johann Lay <gjl at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Target|arm-elf-gcc                 |arm-elf-gcc,x86
             Status|UNCONFIRMED                 |NEW
   Last reconfirmed|                            |2014-01-29
                 CC|                            |gjl at gcc dot gnu.org
      Known to work|                            |3.4.5
            Version|4.3.2                       |4.8.2
     Ever confirmed|0                           |1
      Known to fail|                            |4.8.2

--- Comment #10 from Georg-Johann Lay <gjl at gcc dot gnu.org> ---
This is still present for 4.8.2 and current trunk (future 4.9.0).

C++ works fine, i.e. just the warnings that I'd expect.

As this works as expected in good old 3.4.x, shouldn't this be marked as
regression?


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

end of thread, other threads:[~2023-06-29 14:52 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-12-01  8:36 [Bug c/38341] New: Wrong warning comparison of promoted ~unsigned with unsigned fredrik dot hederstierna at securitas-direct dot com
2008-12-01 10:02 ` [Bug c/38341] " rguenth at gcc dot gnu dot org
2008-12-01 12:41 ` fredrik dot hederstierna at securitas-direct dot com
2008-12-01 12:50 ` rguenth at gcc dot gnu dot org
2008-12-01 12:56 ` fredrik dot hederstierna at securitas-direct dot com
2008-12-01 13:36 ` fredrik dot hederstierna at securitas-direct dot com
2008-12-02 12:15 ` rguenth at gcc dot gnu dot org
2009-02-23 23:53 ` john dot carter at tait dot co dot nz
2009-02-24  0:40 ` michael dot malone at tait dot co dot nz
2009-02-24  0:43 ` michael dot malone at tait dot co dot nz
     [not found] <bug-38341-4@http.gcc.gnu.org/bugzilla/>
2014-01-29 16:01 ` gjl at gcc dot gnu.org
2023-05-20  4:26 ` pinskia at gcc dot gnu.org
2023-06-29 14:52 ` wolter.hellmundvega at tevva dot com

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