This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
Re: [1/2] PR 78736: New warning -Wenum-conversion
- From: Joseph Myers <joseph at codesourcery dot com>
- To: Prathamesh Kulkarni <prathamesh dot kulkarni at linaro dot org>
- Cc: Martin Sebor <msebor at gmail dot com>, gcc Patches <gcc-patches at gcc dot gnu dot org>, Marek Polacek <polacek at redhat dot com>
- Date: Fri, 25 Aug 2017 22:45:00 +0000
- Subject: Re: [1/2] PR 78736: New warning -Wenum-conversion
- Authentication-results: sourceware.org; auth=none
- References: <CAAgBjM=S2EjhqVize+nBjgb_aCd3m+TqdOEp7XBC6hfcm-6tDw@mail.gmail.com> <2b846ba2-2ac0-e387-9928-891bcc8d79af@gmail.com> <CAAgBjMnwWMW378HZyHorO6ohbBQ5c2SGiyOeQ+qHixSL=2aOFg@mail.gmail.com> <CAAgBjMnithBB0WKerdbRFp8zs01bzXS7an8UvPQcjAYBPMo57g@mail.gmail.com> <4bf960f7-440a-1305-e1ac-c744c53a50f9@gmail.com> <CAAgBjMncv6PaEYZ2-vtp1d7HYo-W_wyV1jxoEqwoB+qiZEQjZQ@mail.gmail.com> <alpine.DEB.2.20.1706122015520.17570@digraph.polyomino.org.uk> <CAAgBjMmYSSUX9JChiFjFNF1AOQV=CrOrwT6aatZPTGfeOhTS6w@mail.gmail.com>
On Tue, 11 Jul 2017, Prathamesh Kulkarni wrote:
> On 13 June 2017 at 01:47, Joseph Myers <joseph@codesourcery.com> wrote:
> > This is OK with one fix:
> >
> >> +C ObjC Var(warn_enum_conversion) Init(0) Warning LangEnabledBy(C Objc,Wall)
> >
> > I believe the LangEnabledBy arguments are case-sensitive, so you need to
> > have ObjC not Objc there for it to work correctly. (*.opt parsing isn't
> > very good at detecting typos and giving errors rather than silently
> > ignoring things.)
> Hi,
> Sorry for the late response, I was on a vacation.
> The attached patch is rebased and bootstrap+tested on x86_64-unknown-linux-gnu.
> I have modified it slightly to not warn for enums with different names
> but having same value ranges.
> For eg:
> enum e1 { e1_1, e1_2 };
> enum e2 { e2_1, e2_2 };
>
> enum e1 x = e2_1;
> With this version, there would be no warning for the above assignment
> since both e1 and e2 have
> same value ranges. Is that OK ?
I don't really think that's a good heuristic. I see no particular reason
to think that just because two enums have the same set of values, an
implicit conversion between them is actually deliberate - corresponding
values have the same semantics in some sense - rather than reflecting an
underlying confusion in the code. (You could have different levels of the
warning - and only warn in this case at a higher level - but I don't
really think that makes sense either for this particular warning.)
> The patch has following fallouts in the testsuite:
>
> a) libgomp:
> I initially assume it was a false positive because I thought enum
> gomp_schedule_type
> and enum omp_sched_t have same value-ranges but it looks like omp_sched_t
> has range [1, 4] while gomp_schedule_type has range [0, 4] with one
> extra element.
> Is the warning then correct for this case ?
>
> b) libgfortran:
> i) Implicit conversion from unit_mode to file_mode
> ii) Implicit conversion from unit_sign_s to unit_sign.
> I suppose the warning is OK for these cases since unit_mode, file_mode
> have different value-ranges and similarly for unit_sign_s, unit_sign ?
If it's an implicit conversion between different enum types, the warning
is correct. The more important question for the review is: is it correct
to replace the implicit conversion by an explicit one? That is, for each
value in the source type, what enumerator of the destination type has the
same value, and do the semantics match in whatever way is required for the
code in question?
Also note s/valeu/value/ in the documentation.
--
Joseph S. Myers
joseph@codesourcery.com