[Bug c/92857] -Wsign-conversion flag issues false positives for expression using typedef'ed unsigned types

2019-12-09 Thread joshua.a.saxby at gmail dot com
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=92857

Joshua Saxby  changed:

   What|Removed |Added

 Status|UNCONFIRMED |RESOLVED
 Resolution|--- |INVALID

--- Comment #5 from Joshua Saxby  ---
Ah, I have some reading to do, then. Thank you for pointing that out and
apologies for wasting your time everyone.

[Bug c/92857] -Wsign-conversion flag issues false positives for expression using typedef'ed unsigned types

2019-12-09 Thread joshua.a.saxby at gmail dot com
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=92857

--- Comment #3 from Joshua Saxby  ---
I can confirm that if `MyUnsigned` is changed to `unsigned char`, the issue
persists, however if it is changed to `unsigned int`, the issue is not present.

[Bug c/92857] -Wsign-conversion flag issues false positives for expression using typedef'ed unsigned types

2019-12-09 Thread joshua.a.saxby at gmail dot com
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=92857

--- Comment #2 from Joshua Saxby  ---
(In reply to Richard Biener from comment #1)
> I think the warning is about foo - bar being carried out in type 'int' due to
> integer promotion and that converted to size_t which may turn the negative
> result into a positive.

That could be the issue, although I would expect the integer promotion to
promote to an unsigned integer type rather than a signed one. Am I mistaken in
this assumption?

I don't know if it makes a difference, but if `MyUnsigned` is replaced with
`unsigned`, then the warning diagnostic is not issued.

Then again, to demonstrate if it is an issue with the integer promotion or not,
probably it should be tested with `unsigned char` as well.

[Bug c/92857] New: -Wsign-conversion flag issues false positives for expression using typedef'ed unsigned types

2019-12-08 Thread joshua.a.saxby at gmail dot com
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=92857

Bug ID: 92857
   Summary: -Wsign-conversion flag issues false positives for
expression using typedef'ed unsigned types
   Product: gcc
   Version: 5.4.0
Status: UNCONFIRMED
  Severity: normal
  Priority: P3
 Component: c
  Assignee: unassigned at gcc dot gnu.org
  Reporter: joshua.a.saxby at gmail dot com
  Target Milestone: ---

Created attachment 47439
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=47439=edit
Full pre-processed source of the minimal code sample required to produce the
erroneous behaviour

When using typedef'ed unsigned types in an arithmetic expression and compiling
with GCC's -Wsign-conversion flag, GCC produces a false positive warning about
sign conversion as a result of the expression.

Here is a minimal code sample (gcc_sign_conversion_warning_bug.c):

#include 
#include 

typedef uint8_t MyUnsigned;

int main(void) {
MyUnsigned foo = 3U;
MyUnsigned bar = 2U;
// conversion to ‘unsigned int’ from ‘int’ may change the sign of the
result
size_t baz = foo - bar + 1U;
// ^
return (int)baz;
}

(the full preprocessed source is also attached as a file to this bug report)

Essential information:

- GCC version 5.4.0 20160609

- Running on Ubuntu 16.04

- GCC build-time options: ../src/configure -v --with-pkgversion='Ubuntu
5.4.0-6ubuntu1~16.04.12' --with-bugurl=file:///usr/share/doc/gcc-5/README.Bugs
--enable-languages=c,ada,c++,java,go,d,fortran,objc,obj-c++ --prefix=/usr
--program-suffix=-5 --enable-shared --enable-linker-build-id
--libexecdir=/usr/lib --without-included-gettext --enable-threads=posix
--libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu
--enable-libstdcxx-debug --enable-libstdcxx-time=yes
--with-default-libstdcxx-abi=new --enable-gnu-unique-object
--disable-vtable-verify --enable-libmpx --enable-plugin --with-system-zlib
--disable-browser-plugin --enable-java-awt=gtk --enable-gtk-cairo
--with-java-home=/usr/lib/jvm/java-1.5.0-gcj-5-amd64/jre --enable-java-home
--with-jvm-root-dir=/usr/lib/jvm/java-1.5.0-gcj-5-amd64
--with-jvm-jar-dir=/usr/lib/jvm-exports/java-1.5.0-gcj-5-amd64
--with-arch-directory=amd64 --with-ecj-jar=/usr/share/java/eclipse-ecj.jar
--enable-objc-gc --enable-multiarch --disable-werror --with-arch-32=i686
--with-abi=m64 --with-multilib-list=m32,m64,mx32 --enable-multilib
--with-tune=generic --enable-checking=release --build=x86_64-linux-gnu
--host=x86_64-linux-gnu --target=x86_64-linux-gnu

- Command line that triggers the bug: gcc -v -save-temps -std=gnu99
-Wsign-conversion gcc_sign_conversion_warning_bug.c

- The erroneous warning message produced is: 
gcc_sign_conversion_warning_bug.c:16:28: warning: conversion to ‘unsigned int’
from ‘int’ may change the sign of the result [-Wsign-conversion]
 size_t baz = foo - bar + 1U;
^
Additional information:

I have used the online Compiler Explorer at godbolt.org to verify that this
issue is also present on all GCC versions from 5.4 through 9.2 inclusive. If it
is useful to anyone, there is a link to this online sample here:
https://godbolt.org/z/-2jSYx

I have also tested this on the Clang compiler on the same system used to verify
the GCC sample and Clang does not issue a warning (same warning flag was used).