Re: [PATCH 4.19 60/87] usb: charger: assign specific number for enum value

From: Greg Kroah-Hartman
Date: Wed Mar 04 2020 - 12:39:24 EST


On Wed, Mar 04, 2020 at 06:27:36PM +0100, Pavel Machek wrote:
> On Tue 2020-03-03 18:43:51, Greg Kroah-Hartman wrote:
> > From: Peter Chen <peter.chen@xxxxxxx>
> >
> > commit ca4b43c14cd88d28cfc6467d2fa075aad6818f1d upstream.
> >
> > To work properly on every architectures and compilers, the enum value
> > needs to be specific numbers.
>
> All compilers are expected to handle this in the same way, as this is
> in C standard. This patch is not neccessary, and should not be in mainline,
> either.
>
> http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1124.pdf
>
> 6.7.2.2 Enumeration specifiers
> Syntax
> ...
> 3 The identifiers in an enumerator list are declared as constants that have type int and
> may appear wherever such are permitted.107) An enumerator with = defines its
> enumeration constant as the value of the constant expression. If the first enumerator has
> no =, the value of its enumeration constant is 0. Each subsequent enumerator with no =
> defines its enumeration constant as the value of the constant expression obtained by
> adding 1 to the value of the previous enumeration constant. (The use of enumerators with
> = may produce enumeration constants with values that duplicate other values in the same
> enumeration.) The enumerators of an enumeration are also known as its members.
>
> Best regards,
> Pavel
>
> > enum usb_charger_type {
> > - UNKNOWN_TYPE,
> > - SDP_TYPE,
> > - DCP_TYPE,
> > - CDP_TYPE,
> > - ACA_TYPE,
> > + UNKNOWN_TYPE = 0,
> > + SDP_TYPE = 1,
> > + DCP_TYPE = 2,
> > + CDP_TYPE = 3,
> > + ACA_TYPE = 4,
> > };

It specified that we need to do this by the in-kernel documentation
about how to write a solid api (which I can't find at the moment to
point you at, sorry...) Also, you pointed at a draft C standard, is
that really implemented by older compilers?

thanks,

greg k-h