[edk2-devel] [PATCH v1 18/19] ArmPlatformPkg: Fix comparison of constants warning
Leif Lindholm
leif.lindholm at linaro.org
Thu Nov 21 12:53:15 UTC 2019
On Thu, Nov 21, 2019 at 13:39:55 +0100, Ard Biesheuvel wrote:
> On Thu, 21 Nov 2019 at 13:35, Leif Lindholm <leif.lindholm at linaro.org> wrote:
> >
> > On Fri, Aug 23, 2019 at 11:55:38 +0100, Sami Mujawar wrote:
> > > The VS2017 compiler reports 'warning C6326: potential
> > > comparison of a constant with another constant' when
> > > a fixed PCD value is compared with a constant value.
> > >
> > > The faulting code is as marked by '-->' below:
> > >
> > > --> if (FixedPcdGet32 (PL011UartInteger) != 0) {
> > > Integer = FixedPcdGet32 (PL011UartInteger);
> > > Fractional = FixedPcdGet32 (PL011UartFractional);
> > > } else {
> > > ...
> > >
> > > The macro FixedPcdGet32 (PL011UartInteger) evaluates
> > > to a macro _PCD_VALUE_PL011UartInteger that is defined
> > > by the build system to represent the UART Integer
> > > value. Therefore, the VS2017 compiler reports the above
> > > warning.
> > >
> > > Fix this warning by enclosing the code in appropriate
> > > #if .. #else .. #endif directives.
> > >
> > > Signed-off-by: Sami Mujawar <sami.mujawar at arm.com>
> >
> > Reviewed-by: Leif Lindholm <leif.lindholm at linaro.org>
> >
>
> I don't like this at all.
>
> If two expressions happen to evaluate to the same constant, we should
> be able to compare them using C code.
>
> I think we should disable the warning instead.
I agree with that as a general rule, and agree that disabling the
warning may be preferable in other contexts.
But I somewhat disagree with referring to *this instance* as two
expressions evaluating to the same constant. This is entirely a
preprocessor event, somewhat obscured by the Pcd syntax.
The test is "has the default baudrate been requested or has a specific
one been requested?".
Now, that might be more cleanly described as a macro than inline like
this - but I think the compiler has usefully warned us about weirdly
written code. So I would really not want to disable the warning on a
global level, and would prefer not to do so here.
/
Leif
>
> > > ---
> > > ArmPlatformPkg/Library/PL011UartLib/PL011UartLib.c | 6 +++---
> > > 1 file changed, 3 insertions(+), 3 deletions(-)
> > >
> > > diff --git a/ArmPlatformPkg/Library/PL011UartLib/PL011UartLib.c b/ArmPlatformPkg/Library/PL011UartLib/PL011UartLib.c
> > > index 2d3c279cce49304959953ec4a34b50e09a7d0045..dabf099b9bc82e1fb1bd5a2eae3fa4b5878a9e07 100644
> > > --- a/ArmPlatformPkg/Library/PL011UartLib/PL011UartLib.c
> > > +++ b/ArmPlatformPkg/Library/PL011UartLib/PL011UartLib.c
> > > @@ -174,10 +174,10 @@ PL011UartInitializePort (
> > > //
> > >
> > > // If PL011 Integer value has been defined then always ignore the BAUD rate
> > > - if (FixedPcdGet32 (PL011UartInteger) != 0) {
> > > +#if (FixedPcdGet32 (PL011UartInteger) != 0)
> > > Integer = FixedPcdGet32 (PL011UartInteger);
> > > Fractional = FixedPcdGet32 (PL011UartFractional);
> > > - } else {
> > > +#else
> > > // If BAUD rate is zero then replace it with the system default value
> > > if (*BaudRate == 0) {
> > > *BaudRate = FixedPcdGet32 (PcdSerialBaudRate);
> > > @@ -197,7 +197,7 @@ PL011UartInitializePort (
> > > Divisor = (UINT32)DivisorValue;
> > > Integer = Divisor >> FRACTION_PART_SIZE_IN_BITS;
> > > Fractional = Divisor & FRACTION_PART_MASK;
> > > - }
> > > +#endif
> > >
> > > //
> > > // If PL011 is already initialized, check the current settings
> > > --
> > > 'Guid(CE165669-3EF3-493F-B85D-6190EE5B9759)'
> > >
> > >
-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.
View/Reply Online (#51058): https://edk2.groups.io/g/devel/message/51058
Mute This Topic: https://groups.io/mt/32999801/1813853
Group Owner: devel+owner at edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/unsub [edk2-devel-archive at redhat.com]
-=-=-=-=-=-=-=-=-=-=-=-
More information about the edk2-devel-archive
mailing list