public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
* No warning about duplicate values in enum
@ 2023-03-11 12:54 Basile Starynkevitch
  2023-03-11 16:48 ` Jonathan Wakely
  0 siblings, 1 reply; 6+ messages in thread
From: Basile Starynkevitch @ 2023-03-11 12:54 UTC (permalink / raw)
  To: Andrea Monaco; +Cc: gcc

Hello all,


Andrea observed that:

In gcc 8.3.0, compiling


enum
test
   {
     FIRST = 1,
     SECOND = 1,
     THIRD = 2
   };

int
main (void)
{
   return 0;
}


generates no warning even with -Wextra.

I believe that the C standard (which I don't have here, but see also https://port70.net/~nsz/c/c11/n1570.html or buy it from ISO) explicitly allow duplicate values in enum.

For readability of some weird code, or in the case of C code generated by other tools.

By the way, gcc version 12.2.0 (Debian 12.2.0-14) don't emit any warnings neither when used on your example as gcc -Wall -Wextra -fanalyzer /tmp/andrea.c -o /tmp/andrea

If you really want to get warnings, consider writing your GCC plugin (perhaps starting with https://github.com/bstarynk/bismon/ ....) or using static analysis tools like https://frama-c.com/

Regards.

NB my pet open source project is the RefPerSys open source inference engine (work in progress) on http://refpersys.org/

-- 
Basile Starynkevitch <basile@starynkevitch.net>
92340 Bourg-la-Reine, France
http://starynkevitch.net/Basile/ and http://refpersys.org/


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

* Re: No warning about duplicate values in enum
  2023-03-11 12:54 No warning about duplicate values in enum Basile Starynkevitch
@ 2023-03-11 16:48 ` Jonathan Wakely
  2023-03-13 13:29   ` Marek Polacek
  0 siblings, 1 reply; 6+ messages in thread
From: Jonathan Wakely @ 2023-03-11 16:48 UTC (permalink / raw)
  To: Basile Starynkevitch; +Cc: Andrea Monaco, gcc

[-- Attachment #1: Type: text/plain, Size: 686 bytes --]

On Sat, 11 Mar 2023, 12:53 Basile Starynkevitch, <basile@starynkevitch.net>
wrote:

> Hello all,
>
>
> Andrea observed that:
>
> In gcc 8.3.0, compiling
>
>
> enum
> test
>    {
>      FIRST = 1,
>      SECOND = 1,
>      THIRD = 2
>    };
>
> int
> main (void)
> {
>    return 0;
> }
>
>
> generates no warning even with -Wextra.
>
> I believe that the C standard (which I don't have here, but see also
> https://port70.net/~nsz/c/c11/n1570.html or buy it from ISO) explicitly
> allow duplicate values in enum.
>


Of course it does, it's perfectly valid. Nobody has said it should be
rejected. The request is for a warning, because for *some* uses of enums
duplicates are not wanted.

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

* Re: No warning about duplicate values in enum
  2023-03-11 16:48 ` Jonathan Wakely
@ 2023-03-13 13:29   ` Marek Polacek
  2023-03-13 13:38     ` Basile Starynkevitch
  0 siblings, 1 reply; 6+ messages in thread
From: Marek Polacek @ 2023-03-13 13:29 UTC (permalink / raw)
  To: Jonathan Wakely; +Cc: Basile Starynkevitch, Andrea Monaco, gcc

On Sat, Mar 11, 2023 at 04:48:14PM +0000, Jonathan Wakely via Gcc wrote:
> On Sat, 11 Mar 2023, 12:53 Basile Starynkevitch, <basile@starynkevitch.net>
> wrote:
> 
> > Hello all,
> >
> >
> > Andrea observed that:
> >
> > In gcc 8.3.0, compiling
> >
> >
> > enum
> > test
> >    {
> >      FIRST = 1,
> >      SECOND = 1,
> >      THIRD = 2
> >    };
> >
> > int
> > main (void)
> > {
> >    return 0;
> > }
> >
> >
> > generates no warning even with -Wextra.
> >
> > I believe that the C standard (which I don't have here, but see also
> > https://port70.net/~nsz/c/c11/n1570.html or buy it from ISO) explicitly
> > allow duplicate values in enum.
> >
> 
> 
> Of course it does, it's perfectly valid. Nobody has said it should be
> rejected. The request is for a warning, because for *some* uses of enums
> duplicates are not wanted.

And as I said in the other thread about the very same issue, it's
<https://gcc.gnu.org/PR16186> which is assigned to me and I hope to
implement it for GC 14.

Marek


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

* Re: No warning about duplicate values in enum
  2023-03-13 13:29   ` Marek Polacek
@ 2023-03-13 13:38     ` Basile Starynkevitch
  0 siblings, 0 replies; 6+ messages in thread
From: Basile Starynkevitch @ 2023-03-13 13:38 UTC (permalink / raw)
  To: Marek Polacek; +Cc: gcc


On 3/13/23 14:29, Marek Polacek wrote:
> On Sat, Mar 11, 2023 at 04:48:14PM +0000, Jonathan Wakely via Gcc wrote:
>> On Sat, 11 Mar 2023, 12:53 Basile Starynkevitch, <basile@starynkevitch.net>
>> wrote:
>>
>>> Hello all,
>>>
>>>
>>> Andrea observed that:
>>>
>>> In gcc 8.3.0, compiling
>>>
>>>
>>> enum
>>> test
>>>     {
>>>       FIRST = 1,
>>>       SECOND = 1,
>>>       THIRD = 2
>>>     };
>>>
>>> int
>>> main (void)
>>> {
>>>     return 0;
>>> }
>>>
>>>
>>> generates no warning even with -Wextra.
>>>
>>> I believe that the C standard (which I don't have here, but see also
>>> https://port70.net/~nsz/c/c11/n1570.html or buy it from ISO) explicitly
>>> allow duplicate values in enum.
>>>
>>
>> Of course it does, it's perfectly valid. Nobody has said it should be
>> rejected. The request is for a warning, because for *some* uses of enums
>> duplicates are not wanted.
> And as I said in the other thread about the very same issue, it's
> <https://gcc.gnu.org/PR16186> which is assigned to me and I hope to
> implement it for GCC 14.
>
> Marek


Then consider perhaps:

prototyping that warning in your own GCC plugin (and test it on a bunch 
of existing open source software).

adding some explicit new warning option to enable that new warning or 
adding another option to disable that warning in a compilation unit.

adding some explicit #pragma to disable that new warning (with the hope 
that tools generating C++ code would later -in 2024- emit the pragma 
appropriately)

Since there are some valid, probably weird, cases (notably generated C++ 
code, or low level driver code where two different enum names need to 
share some common values, ....) where having duplicate values in enum 
may make sense.

Regards

-- 
Basile Starynkevitch                  <basile@starynkevitch.net>
(only mine opinions / les opinions sont miennes uniquement)
92340 Bourg-la-Reine, France
web page: starynkevitch.net/Basile/ & refpersys.org


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

* Re: No warning about duplicate values in enum
  2023-03-10 12:57 Andrea Monaco
@ 2023-03-10 13:05 ` Marek Polacek
  0 siblings, 0 replies; 6+ messages in thread
From: Marek Polacek @ 2023-03-10 13:05 UTC (permalink / raw)
  To: Andrea Monaco; +Cc: gcc

On Fri, Mar 10, 2023 at 01:57:06PM +0100, Andrea Monaco via Gcc wrote:
> 
> In gcc 8.3.0, compiling
> 
> 
> enum
> test
>   {
>     FIRST = 1,
>     SECOND = 1,
>     THIRD = 2
>   };
> 
> int
> main (void)
> {
>   return 0;
> }
> 
> 
> generates no warning even with -Wextra.  That hit me today, because I
> had a large enum with many explicitly assigned constants and I
> accidentally used the same value twice, which is an obvious source of
> problems.

This is https://gcc.gnu.org/PR16186.

Marek


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

* No warning about duplicate values in enum
@ 2023-03-10 12:57 Andrea Monaco
  2023-03-10 13:05 ` Marek Polacek
  0 siblings, 1 reply; 6+ messages in thread
From: Andrea Monaco @ 2023-03-10 12:57 UTC (permalink / raw)
  To: gcc


In gcc 8.3.0, compiling


enum
test
  {
    FIRST = 1,
    SECOND = 1,
    THIRD = 2
  };

int
main (void)
{
  return 0;
}


generates no warning even with -Wextra.  That hit me today, because I
had a large enum with many explicitly assigned constants and I
accidentally used the same value twice, which is an obvious source of
problems.



Andrea Monaco


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

end of thread, other threads:[~2023-03-13 13:38 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-03-11 12:54 No warning about duplicate values in enum Basile Starynkevitch
2023-03-11 16:48 ` Jonathan Wakely
2023-03-13 13:29   ` Marek Polacek
2023-03-13 13:38     ` Basile Starynkevitch
  -- strict thread matches above, loose matches on Subject: below --
2023-03-10 12:57 Andrea Monaco
2023-03-10 13:05 ` Marek Polacek

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