Bug 39089

Summary: -Wconversion is buggy with bitwise operators
Product: gcc Reporter: Oleg Smolsky <oleg.smolsky>
Component: c++Assignee: Not yet assigned to anyone <unassigned>
Status: RESOLVED DUPLICATE    
Severity: normal CC: gcc-bugs, gcc, oleg.smolsky
Priority: P3    
Version: 4.3.3   
Target Milestone: ---   
Host: Target:
Build: Known to work:
Known to fail: Last reconfirmed:
Attachments: The test case
The fix from gcc4.4

Description Oleg Smolsky 2009-02-03 17:57:15 UTC
g++ 4.3.3 emits warnings for every statement in the following function when the code is built this way:

..../gcc43/bin/g++ -c -Wall -Wconversion test.cpp

void func(unsigned char a, unsigned char b, unsigned char *out)
{
    *out = a | b;
    *out = out[0] & 0x20;
    out[0] &= 0x20;
}

I'd expect bitwise operators to see/preserve argument widths...
Comment 1 Oleg Smolsky 2009-02-03 17:58:10 UTC
Created attachment 17238 [details]
The test case
Comment 2 Andrew Gaul 2009-02-03 18:40:37 UTC
Original description is not quite accurate, the or operator does not cause a spurious warning while the and operator does.  Here is a more minimal test case:

void func(char a, char b, char c)
{
    c = a | b;
    c = a & b;
    c = a | 0x20;
    c = a & 0x20;   // warn
}

I do not get the spurious warning in SVN from 20090109.
Comment 3 Andrew Gaul 2009-02-03 18:57:25 UTC
After talking with Oleg, there are differences between gcc and g++ compiling the code in comment #2:

$ g++ --version
g++ (GCC) 4.3.0
Copyright (C) 2008 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

$ g++ -Wconversion and_or_conversion.c -c
and_or_conversion.c: In function ‘void func(char, char, char)’:
and_or_conversion.c:3: warning: conversion to ‘char’ from ‘int’ may alter its va
lue
and_or_conversion.c:4: warning: conversion to ‘char’ from ‘int’ may alter its va
lue
and_or_conversion.c:6: warning: conversion to ‘char’ from ‘int’ may alter its va
lue

$ gcc -Wconversion and_or_conversion.c -c
and_or_conversion.c: In function ‘func’:
and_or_conversion.c:6: warning: conversion to ‘char’ from ‘int’ may alter its va
lue

I get the same results with GCC 4.3.2 as packaged by Fedora 10:

$ g++ --version
g++ (GCC) 4.3.2 20081105 (Red Hat 4.3.2-7)
Copyright (C) 2008 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Comment 4 Andrew Pinski 2009-02-03 19:46:41 UTC
I think this is a duplicate of bug 38522 which is fixed for 4.4.0.
Comment 5 Oleg Smolsky 2009-02-03 21:13:35 UTC
I've just built gcc 4.4 and it emits no warnings for the test case above. 

Is there any chance the fix could be back-ported into 4.3?
Comment 6 Oleg Smolsky 2009-02-03 22:23:30 UTC
Created attachment 17239 [details]
The fix from gcc4.4

This is the naive fix merged from the trunk. Fixes two out of three warnings in C++ mode.
Comment 7 Andrew Pinski 2009-02-03 22:24:14 UTC

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