Bug 78736 - enum warnings in GCC (request for -Wenum-conversion to be added)
Summary: enum warnings in GCC (request for -Wenum-conversion to be added)
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c (show other bugs)
Version: unknown
: P3 normal
Target Milestone: ---
Assignee: prathamesh3492
URL:
Keywords: diagnostic, patch
Depends on:
Blocks: new-warning, new_warning
  Show dependency treegraph
 
Reported: 2016-12-08 12:28 UTC by ErikaMolnar
Modified: 2023-07-02 08:55 UTC (History)
4 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2017-08-01 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description ErikaMolnar 2016-12-08 12:28:40 UTC
Hi,

This is not a bug but when compiling with gcc-6.2 the following code:

*******
#include <stdio.h>

typedef enum {brandon, jon, mitch} name_t;
typedef enum {fred, dog, cat} name2_t;
name2_t name = brandon;
name_t hik = 3;


int hal_entry(void)
{

    if (hik < name)

        return(0);
    return 1;
}

int main ()
{
	printf ("%d\n", hal_entry());
	return 0;
}
*******

There is no warning about the enum(which is correct in C) however when I compile the same code with a different/proprietary compiler I get the following warning:

W0511180:The evaluation period has expired.
../src/cc.c(33):W0520188:Enumerated type mixed with another type
../src/cc.c(34):W0520188:Enumerated type mixed with another type


Would you consider implementing such a warning in GCC?

__
Thank you,
Erika Molnar
Comment 1 Marek Polacek 2016-12-08 12:32:25 UTC
We already have -Wenum-compare if that's what you want.
Comment 2 Jonathan Wakely 2016-12-08 12:53:42 UTC
And it's included in -Wall
Comment 3 Richard Biener 2016-12-09 09:33:18 UTC
.
Comment 4 ErikaMolnar 2016-12-12 11:51:27 UTC
Thank you for your fast reply! I knew about the options. I would like some warnings for the two assignments, please.
Comment 5 prathamesh3492 2016-12-12 14:09:52 UTC
clang emits a similar diagnostic with -Wenum-conversion.
I suppose we could also add a similar option that warns when
an enum is implicitly converted from one enum type to other ?

With the following untested patch:

diff --git a/gcc/c/c-typeck.c b/gcc/c/c-typeck.c
index f0917ed..2a6e656 100644
--- a/gcc/c/c-typeck.c
+++ b/gcc/c/c-typeck.c
@@ -6255,6 +6255,19 @@ convert_for_assignment (location_t location, location_t expr_loc, tree type,
        }
     }
 
+  {
+    tree checktype = origtype != NULL_TREE ? origtype : rhstype;
+    if (checktype != error_mark_node
+       && TREE_CODE (checktype) == ENUMERAL_TYPE
+       && TREE_CODE (type) == ENUMERAL_TYPE
+       && TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (type))
+      {
+        gcc_rich_location loc (location);
+       warning_at_rich_loc (&loc, 0, "implicit conversion from"
+                   " enum type %qT to %qT", checktype, type);
+      }
+  }
+
   if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
     return rhs;

gcc emits the diagnostic:
78736.c:5:16: warning: implicit conversion from enum
type ‘enum <anonymous>’ to ‘name2_t {aka enum <anonymous>}’
 name2_t name = brandon;
                ^~~~~~~

Is this patch in the right direction ?
Working on a fair patch.

Thanks,
Prathamesh
Comment 6 ErikaMolnar 2016-12-12 14:44:18 UTC
Thank you for your help! Looking forward to this new feature.
Comment 7 Eric Gallager 2017-08-01 15:32:19 UTC
Prathamesh has submitted a patch to the gcc-patches mailing list that still needs to be reviewed for this bug:
https://gcc.gnu.org/ml/gcc-patches/2017-07/msg00514.html
Comment 8 Eric Gallager 2019-03-31 18:31:08 UTC
(In reply to Eric Gallager from comment #7)
> Prathamesh has submitted a patch to the gcc-patches mailing list that still
> needs to be reviewed for this bug:
> https://gcc.gnu.org/ml/gcc-patches/2017-07/msg00514.html

Retitling to clarify which enum warning in particular this bug is about adding
Comment 9 Eric Gallager 2019-07-02 16:19:20 UTC
This came up again on gcc-help here: https://gcc.gnu.org/ml/gcc-help/2019-07/msg00014.html
Comment 10 Jonny Grant 2019-07-02 21:36:15 UTC
Thank you for adding me to the ticket.
I'll add a bounty of $150 for this feature.

Could Prathamesh's change be incorporated?
Thank you, Jonny
Comment 11 jsm-csl@polyomino.org.uk 2019-08-02 17:15:34 UTC
Discussions of the change continued into Aug / Sep 2017.  In particular, 
there were issues with warnings this introduced in the GCC build (of 
target libraries), and questions about whether fixes posted for those 
warnings were correct or not.  Did all the warnings this introduced in the 
GCC build get resolved?  If not, those warnings would need dealing with 
first.
Comment 12 Jonny Grant 2019-08-02 17:58:10 UTC
If -Wenum-conversion is not enabled automatically, do those GCC build warnings need to be cleaned up before it is added? It's a bit chicken and egg.
Comment 13 Indan 2019-08-28 12:45:50 UTC
Please implement -Wenum-conversion already, there were working patches for it in 2017 and now it's 2019!

Just don't enable it by default if that complicates anything.
Comment 14 prathamesh3492 2019-09-04 16:25:53 UTC
Author: prathamesh3492
Date: Wed Sep  4 16:25:21 2019
New Revision: 275376

URL: https://gcc.gnu.org/viewcvs?rev=275376&root=gcc&view=rev
Log:
Add warning Wenum-conversion for C and ObjC.

The patch enables warning with Wextra due to PR91593 and warnings with
allmodconfig kernel build. Once these issues are resolved, we could
consider promoting it to Wall.

2019-09-04  Prathamesh Kulkarni  <prathamesh.kulkarni@linaro.org>

	PR c/78736
	* doc/invoke.texi: Document -Wenum-conversion.

c-family
	* c.opt (Wenum-conversion): New option.

c/
	* c-typeck.c (convert_for_assignment): Handle Wenum-conversion.

testsuite/
	* gcc.dg/Wenum-conversion.c: New test-case.

Added:
    trunk/gcc/testsuite/gcc.dg/Wenum-conversion.c
Modified:
    trunk/gcc/ChangeLog
    trunk/gcc/c-family/ChangeLog
    trunk/gcc/c-family/c.opt
    trunk/gcc/c/ChangeLog
    trunk/gcc/c/c-typeck.c
    trunk/gcc/doc/invoke.texi
    trunk/gcc/testsuite/ChangeLog
Comment 15 Jonny Grant 2019-09-04 20:00:25 UTC
Great this is added for C and ObjC
Could it also be added for C++?
Comment 16 jsm-csl@polyomino.org.uk 2019-09-04 23:11:40 UTC
Could you give the testcase you expect to be diagnosed with this option 
with C++ that isn't diagnosed by default?
Comment 17 Jonny Grant 2019-09-05 20:50:55 UTC
Hello Joseph

This was the test case I created. There isn't any warning output when 'a_t' is converted to 'int'. Or even if it was converted to an 'unsigned int'

https://gcc.gnu.org/ml/gcc-help/2019-07/msg00014.html


//-O2 -Wall -Wextra -Wconversion -Werror

#include <cstddef>
typedef enum
{
    a = -1
} a_t;

a_t f()
{
    return a;
}

int main()
{
    int b = f();
    return b;
}
Comment 18 Eric Gallager 2019-10-19 05:23:02 UTC
(In reply to Jonny Grant from comment #17)
> Hello Joseph
> 
> This was the test case I created. There isn't any warning output when 'a_t'
> is converted to 'int'. Or even if it was converted to an 'unsigned int'
> 
> https://gcc.gnu.org/ml/gcc-help/2019-07/msg00014.html
> 
> 
> //-O2 -Wall -Wextra -Wconversion -Werror
> 
> #include <cstddef>
> typedef enum
> {
>     a = -1
> } a_t;
> 
> a_t f()
> {
>     return a;
> }
> 
> int main()
> {
>     int b = f();
>     return b;
> }

While it's true that g++ prints no warnings for that testcase, I think that's material for a separate bug. The original bug here was just to add -Wenum-conversion for the C front-end, which has now been done, so I'm closing this. Feel free to open new bugs for any missed cases.
Comment 19 Jonny Grant 2019-10-19 14:28:01 UTC
(In reply to Eric Gallager from comment #18)
> (In reply to Jonny Grant from comment #17)
> > Hello Joseph
> > 
> > This was the test case I created. There isn't any warning output when 'a_t'
> > is converted to 'int'. Or even if it was converted to an 'unsigned int'
> > 
> > https://gcc.gnu.org/ml/gcc-help/2019-07/msg00014.html
> > 
> > 
> > //-O2 -Wall -Wextra -Wconversion -Werror
> > 
> > #include <cstddef>
> > typedef enum
> > {
> >     a = -1
> > } a_t;
> > 
> > a_t f()
> > {
> >     return a;
> > }
> > 
> > int main()
> > {
> >     int b = f();
> >     return b;
> > }
> 
> While it's true that g++ prints no warnings for that testcase, I think
> that's material for a separate bug. The original bug here was just to add
> -Wenum-conversion for the C front-end, which has now been done, so I'm
> closing this. Feel free to open new bugs for any missed cases.

ok, I added 
Bug 92159 to add -Wenum-conversion for C++

Bug 92158 my enum test case for C and C++
Comment 20 Boris 2023-07-02 08:55:00 UTC
(In reply to prathamesh3492 from comment #14)
> The patch enables warning with Wextra due to PR91593 and warnings with
> allmodconfig kernel build. Once these issues are resolved, we could
> consider promoting it to Wall.

Yes, you should. I just built 6.4 allmodconfig and there are no warnings anymore likely because clang enables this warning by default and since we build the kernel with both compilers, people have fixed them all in the meantime.

And clang did catch a legitimate error with this warning here while gcc didn't using the default warning flags but it warned with -Wextra.

So I think we'll enable that warning by default in the kernel build and it does make sense to do that in gcc too, I'd say.

Thx.