public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/30260]  New: Enumeration types and enumeration constants erroneously given unsigned types
@ 2006-12-19 20:51 rda at lemma-one dot com
  2006-12-19 21:06 ` [Bug c/30260] " pinskia at gcc dot gnu dot org
                   ` (8 more replies)
  0 siblings, 9 replies; 10+ messages in thread
From: rda at lemma-one dot com @ 2006-12-19 20:51 UTC (permalink / raw)
  To: gcc-bugs

The comments at the top of the following source file describes the bug.
This happens on gcc 4.1.0 too. for what it is worth, uname -a and
gcc -v output for the Linux box I am currently using are at the end.
The actual code is only 14 lines and only includes stdio.h.

/* source file: enum_bug.c
 *
 * An anomaly in gcc C: according to C99 6.7.2.2, enumeration constants
 * must have signed int type and, if some enumeration constant has
 * a negative value, then the integer type compatible with the
 * enumeration type must be a signed type. It seems to be possible
 * to fool gcc into misapplying these rules by making the type of
 * the integer constant expression giving the value of an enumeration
 * constant an unsigned integer type. This was first observed in a case
 * where the expression involved sizeof, but just a cast to unsigned or using
 * an unsigned integer constant has the same effect as seen below:
 *
 * This has been observed on various Linux PCs and on Mac OS X with
 * several versions of gcc (3.4.1 up to 4.1.0).
 *
 * Compiling and running with
 *
gcc -o enum_bug enum_bug.c ; ./enum_bug
 *
 * produces no diagnostics and code that prints out the following:
 *
(a < 0) = 1, (A2 < 0) = 1, A{1,2} = +0, -1
(b < 0) = 0, (B2 < 0) = 0, B{1,2} = +0, -1
 *
 * Compiling and running with:
 *
gcc -ansi -pedantic -o enum_bug enum_bug.c; ./enum_bug
 *
 * results in an an incorrect warning
 *
enum_bug.c:38: warning: ISO C restricts enumerator values to range of `int'
 *
 * but with code that correctly prints out:
 *
(a < 0) = 1, (A2 < 0) = 1, A{1,2} = +0, -1
(b < 0) = 1, (B2 < 0) = 1, B{1,2} = +0, -1
 *
 * Rob Arthan (rda at lemma-one dot com) 19th December 2006
 */

#include <stdio.h>
enum A {A1 = 0, A2 = A1 - 1};
enum B {B1 = 0u, B2 = B1 - 1};
int main(void)
{
    enum A a = -1;
    enum B b = -1;
    printf("(a < 0) = %d, (A2 < 0) = %d, A{1,2} = %+d, %+d\n",
        (int)(a < 0), (int)(A2 < 0), (int)A1, (int)A2);
    printf("(b < 0) = %d, (B2 < 0) = %d, B{1,2} = %+d, %+d\n",
        (int)(b < 0), (int)(B2 < 0), (int)B1, (int)B2);
    return 0;
}
/*
 * uname -a:
Linux localhost 2.6.8.1-12mdk #1 Fri Oct 1 12:53:41 CEST 2004 i686 Pentium III
(Coppermine) unknown GNU/Linux

 * gcc -v:
Reading specs from /usr/lib/gcc/i586-mandrake-linux-gnu/3.4.1/specs
Configured with: ../configure --prefix=/usr --libdir=/usr/lib
--with-slibdir=/lib --mandir=/usr/share/man --infodir=/usr/share/info
--enable-shared --enable-threads=posix --disable-checking --enable-long-long
--enable-__cxa_atexit --enable-clocale=gnu --disable-libunwind-exceptions
--enable-languages=c,c++,ada,f77,objc,java --host=i586-mandrake-linux-gnu
--with-system-zlib
Thread model: posix
gcc version 3.4.1 (Mandrakelinux 10.1 3.4.1-4mdk)
 */


-- 
           Summary: Enumeration types and enumeration constants erroneously
                    given unsigned types
           Product: gcc
           Version: 3.4.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: rda at lemma-one dot com


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


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

* [Bug c/30260] Enumeration types and enumeration constants erroneously given unsigned types
  2006-12-19 20:51 [Bug c/30260] New: Enumeration types and enumeration constants erroneously given unsigned types rda at lemma-one dot com
@ 2006-12-19 21:06 ` pinskia at gcc dot gnu dot org
  2006-12-19 21:22 ` joseph at codesourcery dot com
                   ` (7 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: pinskia at gcc dot gnu dot org @ 2006-12-19 21:06 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #1 from pinskia at gcc dot gnu dot org  2006-12-19 21:05 -------
C99 6.7.2.2 does not say that.

Read 6.7.2.2 which says:
> The expression that defines the value of an enumeration constant shall be an
> integer constant expression that has a value representable as an int.

So the error with -pedantic is correct as 0U - 1 is not representable as an
int.

Also:
Read 6.7.2.2/4 which says:
> Each enumerated type shall be compatible with char, a signed integer type, or an
> unsigned integer type. The choice of type is implementation-defined,108) but 
> shall be capable of representing the values of all the members of the
> enumeration. The enumerated type is incomplete until after the } that
> terminates the list of enumerator declarations.

http://gcc.gnu.org/onlinedocs/gcc-4.1.1/gcc/Structures-unions-enumerations-and-bit_002dfields-implementation.html
Specifies our implementation-defined behavior.


Now we do have different code with -pedantic and without -pedantic and that is
a bug but that is PR 15236.


-- 

pinskia at gcc dot gnu dot org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |RESOLVED
         Resolution|                            |INVALID


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


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

* [Bug c/30260] Enumeration types and enumeration constants erroneously given unsigned types
  2006-12-19 20:51 [Bug c/30260] New: Enumeration types and enumeration constants erroneously given unsigned types rda at lemma-one dot com
  2006-12-19 21:06 ` [Bug c/30260] " pinskia at gcc dot gnu dot org
@ 2006-12-19 21:22 ` joseph at codesourcery dot com
  2006-12-19 21:23 ` jsm28 at gcc dot gnu dot org
                   ` (6 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: joseph at codesourcery dot com @ 2006-12-19 21:22 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #2 from joseph at codesourcery dot com  2006-12-19 21:22 -------
Subject: Re:  Enumeration types and enumeration constants
 erroneously given unsigned types

On Tue, 19 Dec 2006, pinskia at gcc dot gnu dot org wrote:

> So the error with -pedantic is correct as 0U - 1 is not representable as an
> int.

Read the testcase in the bug.  It doesn't use 0U - 1, it uses B1 - 1.  B1 
is an enumeration constant and so should have type int, regardless of the 
type of the expression used to set its value.  As such, the code is 
conforming and there is a GCC bug here.

There should be no diagnostic here, whether or not -pedantic is used, and 
there should be no dependence on -ansi or -pedantic for this testcase.


-- 


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


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

* [Bug c/30260] Enumeration types and enumeration constants erroneously given unsigned types
  2006-12-19 20:51 [Bug c/30260] New: Enumeration types and enumeration constants erroneously given unsigned types rda at lemma-one dot com
  2006-12-19 21:06 ` [Bug c/30260] " pinskia at gcc dot gnu dot org
  2006-12-19 21:22 ` joseph at codesourcery dot com
@ 2006-12-19 21:23 ` jsm28 at gcc dot gnu dot org
  2008-02-12 14:26 ` manu at gcc dot gnu dot org
                   ` (5 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: jsm28 at gcc dot gnu dot org @ 2006-12-19 21:23 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #3 from jsm28 at gcc dot gnu dot org  2006-12-19 21:23 -------
Reopening.


-- 

jsm28 at gcc dot gnu dot org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
OtherBugsDependingO|                            |16620, 16989
              nThis|                            |
             Status|RESOLVED                    |UNCONFIRMED
         Resolution|INVALID                     |


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


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

* [Bug c/30260] Enumeration types and enumeration constants erroneously given unsigned types
  2006-12-19 20:51 [Bug c/30260] New: Enumeration types and enumeration constants erroneously given unsigned types rda at lemma-one dot com
                   ` (2 preceding siblings ...)
  2006-12-19 21:23 ` jsm28 at gcc dot gnu dot org
@ 2008-02-12 14:26 ` manu at gcc dot gnu dot org
  2008-02-15 11:26 ` manu at gcc dot gnu dot org
                   ` (4 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: manu at gcc dot gnu dot org @ 2008-02-12 14:26 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #4 from manu at gcc dot gnu dot org  2008-02-12 14:25 -------
This bug is confirmed.


-- 

manu at gcc dot gnu dot org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |manu at gcc dot gnu dot org
             Status|UNCONFIRMED                 |NEW
     Ever Confirmed|0                           |1
   Last reconfirmed|0000-00-00 00:00:00         |2008-02-12 14:25:48
               date|                            |


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


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

* [Bug c/30260] Enumeration types and enumeration constants erroneously given unsigned types
  2006-12-19 20:51 [Bug c/30260] New: Enumeration types and enumeration constants erroneously given unsigned types rda at lemma-one dot com
                   ` (3 preceding siblings ...)
  2008-02-12 14:26 ` manu at gcc dot gnu dot org
@ 2008-02-15 11:26 ` manu at gcc dot gnu dot org
  2008-08-23 18:21 ` manu at gcc dot gnu dot org
                   ` (3 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: manu at gcc dot gnu dot org @ 2008-02-15 11:26 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #5 from manu at gcc dot gnu dot org  2008-02-15 11:25 -------
I am not sure how we want to fix this bug. 

A possible fix is to have 2 behaviours, one with -std=cX and another with
-std=gnuX. So, for -std=cX we always convert enum values to integer, while for
-std=gnuX we don't. Currently , this is what is implemented for -pedantic (see
PR15236). This is wrong to do for -pedantic, but may be ok for the -std=
switch.

Otherwise, we could just unconditionally convert anything that fits into a
signed integer into signed integer. That is, 

enum A {A1 = 0, A2 = A1 - 1};
enum B {B1 = 0u, B2 = B1 - 1};

will produce the same result. While

enum A {A1 = (int)0x80001000, A2 = A1 - 1};
enum B {B1 = 0x80001000, B2 = B1 - 1};

won't.

Joseph, what do you think?
Any more ideas?


-- 

manu at gcc dot gnu dot org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Last reconfirmed|2008-02-12 14:25:48         |2008-02-15 11:25:52
               date|                            |


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


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

* [Bug c/30260] Enumeration types and enumeration constants erroneously given unsigned types
  2006-12-19 20:51 [Bug c/30260] New: Enumeration types and enumeration constants erroneously given unsigned types rda at lemma-one dot com
                   ` (4 preceding siblings ...)
  2008-02-15 11:26 ` manu at gcc dot gnu dot org
@ 2008-08-23 18:21 ` manu at gcc dot gnu dot org
  2008-08-24 14:03 ` rda at lemma-one dot com
                   ` (2 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: manu at gcc dot gnu dot org @ 2008-08-23 18:21 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #6 from manu at gcc dot gnu dot org  2008-08-23 18:20 -------
In GCC 4.4 we produce the same output independently of -pedantic:

(a < 0) = 1, (A2 < 0) = 1, A{1,2} = +0, -1
(b < 0) = 0, (B2 < 0) = 0, B{1,2} = +0, -1


-- 


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


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

* [Bug c/30260] Enumeration types and enumeration constants erroneously given unsigned types
  2006-12-19 20:51 [Bug c/30260] New: Enumeration types and enumeration constants erroneously given unsigned types rda at lemma-one dot com
                   ` (5 preceding siblings ...)
  2008-08-23 18:21 ` manu at gcc dot gnu dot org
@ 2008-08-24 14:03 ` rda at lemma-one dot com
  2008-10-19 13:54 ` manu at gcc dot gnu dot org
  2008-10-19 13:56 ` manu at gcc dot gnu dot org
  8 siblings, 0 replies; 10+ messages in thread
From: rda at lemma-one dot com @ 2008-08-24 14:03 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #7 from rda at lemma-one dot com  2008-08-24 14:02 -------
The gcc 4.4 semantics as described by Manuel do not conform to the standard in
either case. B2 is the integer -1 which is less than 0, so B2 < 0 should be 1
not 0.


-- 


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


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

* [Bug c/30260] Enumeration types and enumeration constants erroneously given unsigned types
  2006-12-19 20:51 [Bug c/30260] New: Enumeration types and enumeration constants erroneously given unsigned types rda at lemma-one dot com
                   ` (6 preceding siblings ...)
  2008-08-24 14:03 ` rda at lemma-one dot com
@ 2008-10-19 13:54 ` manu at gcc dot gnu dot org
  2008-10-19 13:56 ` manu at gcc dot gnu dot org
  8 siblings, 0 replies; 10+ messages in thread
From: manu at gcc dot gnu dot org @ 2008-10-19 13:54 UTC (permalink / raw)
  To: gcc-bugs

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain, Size: 737 bytes --]



------- Comment #8 from manu at gcc dot gnu dot org  2008-10-19 13:53 -------
Subject: Bug 30260

Author: manu
Date: Sun Oct 19 13:52:10 2008
New Revision: 141224

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=141224
Log:
2008-10-19  Manuel López-Ibáñez  <manu@gcc.gnu.org>

        PR c/30260
        * c-decl.c (finish_enum): Convert non-integer enumerators to enum
        type.
        (build_enumerator): Convert enumerators that fit in integer to
        integer type.
testsuite/
        * gcc.dg/pr30260.c: New.

Added:
    trunk/gcc/testsuite/gcc.dg/pr30260.c
Modified:
    trunk/gcc/ChangeLog
    trunk/gcc/c-decl.c
    trunk/gcc/testsuite/ChangeLog


-- 


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


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

* [Bug c/30260] Enumeration types and enumeration constants erroneously given unsigned types
  2006-12-19 20:51 [Bug c/30260] New: Enumeration types and enumeration constants erroneously given unsigned types rda at lemma-one dot com
                   ` (7 preceding siblings ...)
  2008-10-19 13:54 ` manu at gcc dot gnu dot org
@ 2008-10-19 13:56 ` manu at gcc dot gnu dot org
  8 siblings, 0 replies; 10+ messages in thread
From: manu at gcc dot gnu dot org @ 2008-10-19 13:56 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #9 from manu at gcc dot gnu dot org  2008-10-19 13:55 -------
Fixed for GCC 4.4


-- 

manu at gcc dot gnu dot org changed:

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


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


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

end of thread, other threads:[~2008-10-19 13:56 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2006-12-19 20:51 [Bug c/30260] New: Enumeration types and enumeration constants erroneously given unsigned types rda at lemma-one dot com
2006-12-19 21:06 ` [Bug c/30260] " pinskia at gcc dot gnu dot org
2006-12-19 21:22 ` joseph at codesourcery dot com
2006-12-19 21:23 ` jsm28 at gcc dot gnu dot org
2008-02-12 14:26 ` manu at gcc dot gnu dot org
2008-02-15 11:26 ` manu at gcc dot gnu dot org
2008-08-23 18:21 ` manu at gcc dot gnu dot org
2008-08-24 14:03 ` rda at lemma-one dot com
2008-10-19 13:54 ` manu at gcc dot gnu dot org
2008-10-19 13:56 ` manu at gcc dot gnu dot org

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