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
We already have -Wenum-compare if that's what you want.
And it's included in -Wall
.
Thank you for your fast reply! I knew about the options. I would like some warnings for the two assignments, please.
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
Thank you for your help! Looking forward to this new feature.
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
(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
This came up again on gcc-help here: https://gcc.gnu.org/ml/gcc-help/2019-07/msg00014.html
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
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.
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.
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.
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
Great this is added for C and ObjC Could it also be added for C++?
Could you give the testcase you expect to be diagnosed with this option with C++ that isn't diagnosed by default?
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; }
(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.
(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++
(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.