public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* Quieten -Wconversion warnings for bit-fields
@ 2008-03-03 11:35 Christian Iseli
  2008-03-23 19:16 ` Manuel López-Ibáñez
  0 siblings, 1 reply; 3+ messages in thread
From: Christian Iseli @ 2008-03-03 11:35 UTC (permalink / raw)
  To: gcc-help

Dear all,

Given:
---- test.c ----
typedef struct _my_struct_t {
  unsigned int small:1;
  unsigned int big:31;
} my_struct_t, *my_struct_p_t;

void
my_func(unsigned int sm, unsigned int bi, my_struct_p_t msp)
{
  msp->small = sm;
  msp->big = bi;
}
----

and:
gcc -v
Using built-in specs.
Target: x86_64-redhat-linux
Configured with: ../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-bootstrap --enable-shared --enable-threads=posix --enable-checking=release --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-languages=c,c++,objc,obj-c++,java,fortran,ada --enable-java-awt=gtk --disable-dssi --enable-plugin --with-java-home=/usr/lib/jvm/java-1.5.0-gcj-1.5.0.0/jre --enable-libgcj-multifile --enable-java-maintainer-mode --with-ecj-jar=/usr/share/java/eclipse-ecj.jar --disable-libjava-multilib --with-cpu=generic --build=x86_64-redhat-linux
Thread model: posix
gcc version 4.3.0 20080229 (Red Hat 4.3.0-0.13) (GCC)

and:
gcc -O2 -std=gnu99 -W -Wall -Wconversion -pedantic -c test.c

I get:
test.c: In function ‘my_func’:
test.c:9: warning: conversion to ‘unsigned char:1’ from ‘unsigned int’ may alter its value
test.c:10: warning: conversion to ‘unsigned int:31’ from ‘unsigned int’ may alter its value

I can't seem to find a typecast incantation that would tell GCC that I
did indeed take proper precautions and that the assignment is fine.

I also tried "msp->small = sm & 1U;" but it doesn't help

Any hints ?

Thanks,
					Christian

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

* Re: Quieten -Wconversion warnings for bit-fields
  2008-03-03 11:35 Quieten -Wconversion warnings for bit-fields Christian Iseli
@ 2008-03-23 19:16 ` Manuel López-Ibáñez
  2008-03-25 23:30   ` Christian Iseli
  0 siblings, 1 reply; 3+ messages in thread
From: Manuel López-Ibáñez @ 2008-03-23 19:16 UTC (permalink / raw)
  To: Christian Iseli; +Cc: gcc-help

I believe sm & 1U could and should silence the warning. Please, open a 
bug report http://gcc.gnu.org/bugs.html and add manu@gcc.gnu.org to the 
CC list. We probably have a similar issue with
unsigned short x = sm & 1U; or does that work?

Nevertheless, it seems casting types like (unsigned int:1)sm would be a 
useful feature of C. Is it contemplated for some future version of the 
standard?

Thanks,

	Manuel.

Christian Iseli wrote:
> Dear all,
> 
> Given:
> ---- test.c ----
> typedef struct _my_struct_t {
>   unsigned int small:1;
>   unsigned int big:31;
> } my_struct_t, *my_struct_p_t;
> 
> void
> my_func(unsigned int sm, unsigned int bi, my_struct_p_t msp)
> {
>   msp->small = sm;
>   msp->big = bi;
> }
> ----
> 
> and:
> gcc -v
> Using built-in specs.
> Target: x86_64-redhat-linux
> Configured with: ../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-bootstrap --enable-shared --enable-threads=posix --enable-checking=release --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-languages=c,c++,objc,obj-c++,java,fortran,ada --enable-java-awt=gtk --disable-dssi --enable-plugin --with-java-home=/usr/lib/jvm/java-1.5.0-gcj-1.5.0.0/jre --enable-libgcj-multifile --enable-java-maintainer-mode --with-ecj-jar=/usr/share/java/eclipse-ecj.jar --disable-libjava-multilib --with-cpu=generic --build=x86_64-redhat-linux
> Thread model: posix
> gcc version 4.3.0 20080229 (Red Hat 4.3.0-0.13) (GCC)
> 
> and:
> gcc -O2 -std=gnu99 -W -Wall -Wconversion -pedantic -c test.c
> 
> I get:
> test.c: In function ‘my_func’:
> test.c:9: warning: conversion to ‘unsigned char:1’ from ‘unsigned int’ may alter its value
> test.c:10: warning: conversion to ‘unsigned int:31’ from ‘unsigned int’ may alter its value
> 
> I can't seem to find a typecast incantation that would tell GCC that I
> did indeed take proper precautions and that the assignment is fine.
> 
> I also tried "msp->small = sm & 1U;" but it doesn't help
> 
> Any hints ?
> 
> Thanks,
> 					Christian
> 

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

* Re: Quieten -Wconversion warnings for bit-fields
  2008-03-23 19:16 ` Manuel López-Ibáñez
@ 2008-03-25 23:30   ` Christian Iseli
  0 siblings, 0 replies; 3+ messages in thread
From: Christian Iseli @ 2008-03-25 23:30 UTC (permalink / raw)
  To: manu; +Cc: manuellopezibanez, gcc-help

Dear Manuel,

On Sun, 23 Mar 2008 20:15:18 +0100, Manuel López-Ibáñez wrote:
> I believe sm & 1U could and should silence the warning. Please, open
> a bug report http://gcc.gnu.org/bugs.html and add manu@gcc.gnu.org to
> the CC list.

Done.  It is bug 35701 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=35701
I hope I did it properly...

> We probably have a similar issue with unsigned short x = sm & 1U; or
> does that work?

Yes, there is the same issue.  I also included it in the test case.

Kind regards,
					Christian

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

end of thread, other threads:[~2008-03-25 23:30 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-03-03 11:35 Quieten -Wconversion warnings for bit-fields Christian Iseli
2008-03-23 19:16 ` Manuel López-Ibáñez
2008-03-25 23:30   ` Christian Iseli

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