public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/41138] Inconsistent (incorrect?) "overflow in implicit constant conversion" warning
       [not found] <bug-41138-4@http.gcc.gnu.org/bugzilla/>
@ 2011-06-09 23:03 ` msebor at gmail dot com
  2011-06-10 17:45 ` msebor at gmail dot com
  1 sibling, 0 replies; 3+ messages in thread
From: msebor at gmail dot com @ 2011-06-09 23:03 UTC (permalink / raw)
  To: gcc-bugs

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

Martin Sebor <msebor at gmail dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |msebor at gmail dot com

--- Comment #2 from Martin Sebor <msebor at gmail dot com> 2011-06-09 23:03:38 UTC ---
I think this is a bug since there can be no overflow (or slicing) here. Here's
my reasoning.

According to 6.5.16.2 Compound assignment of C99:

  A compound assignment of the form E1 op= E2 differs from the simple
assignment
  expression E1 = E1 op (E2) only in that the lvalue E1 is evaluated only once.

and 6.5.16.1 Simple assignment:

  In simple assignment (=), the value of the right operand is converted to the
  type of the assignment expression and replaces the value stored in the object
  designated by the left operand.

and finally 6.5.16 Assignment operators:

  The type of an assignment expression is the type of the left operand...

So all cases are equivalent to

  foo = foo & 65280;

The result of the expression (foo & 65280) is an int (or unsigned int, long,
unsigned long, depending on the suffix of the constant) which is converted
to char (the type of the assignment expression). In all cases in the first
test case, the value of the result is guaranteed to be representable in
unsigned char. There is no overflow or slicing and the code is strictly
conforming with safe semantics.

That said, a warning stating that foo &= 65280 always evaluates to zero
might be useful in case the intent wasn't to clear the variable (otherwise
the code could be rewritten as foo = 0).


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

* [Bug c/41138] Inconsistent (incorrect?) "overflow in implicit constant conversion" warning
       [not found] <bug-41138-4@http.gcc.gnu.org/bugzilla/>
  2011-06-09 23:03 ` [Bug c/41138] Inconsistent (incorrect?) "overflow in implicit constant conversion" warning msebor at gmail dot com
@ 2011-06-10 17:45 ` msebor at gmail dot com
  1 sibling, 0 replies; 3+ messages in thread
From: msebor at gmail dot com @ 2011-06-10 17:45 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #3 from Martin Sebor <msebor at gmail dot com> 2011-06-10 17:44:47 UTC ---
Here's another test case, one that does involve slicing and where a consistent
but differently phrased warning would, IMO, be useful. Perhaps something like:

warning: slicing high order bits in implicit constant conversion

cat << EOF | gcc -c -xc -
struct S {
    unsigned a:1;
    unsigned b:1;
};

void f (struct S *s, int i) {
    s->a = i & 0x10;   /* line 7: no warning */
    s->b = i & 0x80;   /* line 8: warning  */
}
EOF
<stdin>: In function ‘f’:
<stdin>:8:5: warning: overflow in implicit constant conversion [-Woverflow]


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

* [Bug c/41138] Inconsistent (incorrect?) "overflow in implicit constant conversion" warning
  2009-08-21 12:49 [Bug c/41138] New: " anpaza at mail dot ru
@ 2009-08-21 13:31 ` bugs at nospam dot pz dot podzone dot net
  0 siblings, 0 replies; 3+ messages in thread
From: bugs at nospam dot pz dot podzone dot net @ 2009-08-21 13:31 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #1 from bugs at nospam dot pz dot podzone dot net  2009-08-21 13:31 -------
$ cat foo.c
unsigned char foo;

Also note the inconsistency between x86 gcc and avr-gcc:

void test(void)
{
  foo &= ~0xff;                 /* warning */
  foo &= ~0xfe;                 /* no warning */

  foo &= 65280;                 /* warning */
  foo &= 65280L;                /* warning */
  foo &= 65280U;                /* no warning */
  foo &= 65280LU;               /* no warning */

  foo &= 0xff00;                /* warning only with x86 gcc */
  foo &= 0xff00L;               /* warning */
  foo &= 0xff00U;               /* no warning */
  foo &= 0xff00LU;              /* no warning */

  foo &= 65281;                 /* no warning */
  foo &= 65281L;                /* no warning */
  foo &= 65281U;                /* no warning */
  foo &= 65281LU;               /* no warning */

  foo &= 0xff01;                /* no warning */
  foo &= 0xff01L;               /* no warning */
  foo &= 0xff01U;               /* no warning */
  foo &= 0xff01LU;              /* no warning */
}
$ avr-gcc -c foo.c
foo.c: In function 'test':
foo.c:5: warning: overflow in implicit constant conversion
foo.c:8: warning: overflow in implicit constant conversion
foo.c:9: warning: overflow in implicit constant conversion
foo.c:14: warning: overflow in implicit constant conversion
$ gcc-4 -c foo.c
foo.c: In function 'test':
foo.c:5: warning: overflow in implicit constant conversion
foo.c:8: warning: overflow in implicit constant conversion
foo.c:9: warning: overflow in implicit constant conversion
foo.c:13: warning: overflow in implicit constant conversion
foo.c:14: warning: overflow in implicit constant conversion
$ avr-gcc --version
avr-gcc.exe (WinAVR 20090313) 4.3.2
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.

$ gcc-4 --version
gcc-4 (GCC) 4.3.2 20080827 (beta) 2
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.

$


-- 

bugs at nospam dot pz dot podzone dot net changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |bugs at nospam dot pz dot
                   |                            |podzone dot net


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


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

end of thread, other threads:[~2011-06-10 17:45 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <bug-41138-4@http.gcc.gnu.org/bugzilla/>
2011-06-09 23:03 ` [Bug c/41138] Inconsistent (incorrect?) "overflow in implicit constant conversion" warning msebor at gmail dot com
2011-06-10 17:45 ` msebor at gmail dot com
2009-08-21 12:49 [Bug c/41138] New: " anpaza at mail dot ru
2009-08-21 13:31 ` [Bug c/41138] " bugs at nospam dot pz dot podzone 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).