All of lore.kernel.org
 help / color / mirror / Atom feed
From: Russell King - ARM Linux <linux-lFZ/pmaqli7XmaaqVzeoHQ@public.gmane.org>
To: Shawn Guo <shawn.guo-QSEj5FYQhm4dnm+yROfE0A@public.gmane.org>
Cc: Wolfram Sang <w.sang-bIcnvbaLZ9MEGnE8C9+IrQ@public.gmane.org>,
	Marek Vasut <marex-ynQEQJNshbs@public.gmane.org>,
	Wolfgang Denk <wd-ynQEQJNshbs@public.gmane.org>,
	Detlev Zundel <dzu-ynQEQJNshbs@public.gmane.org>,
	Linux I2C <linux-i2c-u79uwXL29TY76Z2rM5mHXA@public.gmane.org>,
	Fabio Estevam <festevam-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>,
	Stefano Babic <sbabic-ynQEQJNshbs@public.gmane.org>,
	linux-arm-kernel-IAPFreCvJWM7uuMidbF8XUB+6BGkLq7r@public.gmane.org
Subject: Re: [PATCH 2/2] I2C: Implement DMA support into mxs-i2c
Date: Tue, 1 May 2012 15:37:13 +0100	[thread overview]
Message-ID: <20120501143712.GB3548@n2100.arm.linux.org.uk> (raw)
In-Reply-To: <20120501135800.GL2194-rvtDTF3kK1ictlrPMvKcciBecyulp+rMXqFh9Ls21Oc@public.gmane.org>

On Tue, May 01, 2012 at 09:58:03PM +0800, Shawn Guo wrote:
> On Mon, Apr 30, 2012 at 11:01:08PM +0200, Wolfram Sang wrote:
> > 
> > > > > It complains that data might be undefined, do you want it in a separate
> > > > > patch then ?
> > > > 
> > > > Yes, but only if you can prove that the compiler is right.
> > > 
> > > It's not right, because that variable is always initialized, but this at least 
> > > squashes the compile warning.
> > 
> > The compiler needs to be fixed, not the kernel.
> > 
> Heh, this is coming up again.  I'm not convincing you to take the
> change, but just curious what if this is not a compile warning but
> an error for the same cause that compiler is not right.

It's a compiler warning.

Please look at the wording of the warning.  The compiler uses "may" not
"is".  That means it can't come to a conclusion about it.

But we, as humans with our biological inference engines, can see that
the code is correct, and we _can_ choose to ignore the warning -
just like I do with:

drivers/video/omap2/displays/panel-taal.c: In function 'taal_num_errors_show':
drivers/video/omap2/displays/panel-taal.c:605: warning: 'errors' may be used uninitialized in this function

static int taal_dcs_read_1(struct taal_data *td, u8 dcs_cmd, u8 *data)
{
        int r;
        u8 buf[1];
        r = dsi_vc_dcs_read(td->dssdev, td->channel, dcs_cmd, buf, 1);
        if (r < 0)
                return r;
        *data = buf[0];  
        return 0;
}

        u8 errors;

        mutex_lock(&td->lock);
        if (td->enabled) {
                dsi_bus_lock(dssdev);
                r = taal_wake_up(dssdev);
                if (!r) 
                        r = taal_dcs_read_1(td, DCS_READ_NUM_ERRORS, &errors);
                dsi_bus_unlock(dssdev);
        } else {
                r = -ENODEV;
        }
        mutex_unlock(&td->lock);

        if (r)
                return r;

        return snprintf(buf, PAGE_SIZE, "%d\n", errors);

See?  We can infer from the above that that 'errors' will always be set
if r is zero - but the compiler is saying it's not clever enough to do
that automatically.

This doesn't mean the code _has_ to be changed - we can see that the
above warning is false.  Though, if there is a way to rewrite it to make
it easier for us humans to read and that makes the warning go away, it
_might_ be a good idea to make the change - but only for a maintainability
reason.

The reverse is also true - if trying to fix a warning like this makes
the code more difficult to understand, then the warning _should_ stay.

That's actually the point of programming languages - the ability to
express our instructions to a computer in a way that both we and the
compiler can readily understand.  The most important part of that is
that _we_ understand what's been written.

WARNING: multiple messages have this Message-ID (diff)
From: linux@arm.linux.org.uk (Russell King - ARM Linux)
To: linux-arm-kernel@lists.infradead.org
Subject: [PATCH 2/2] I2C: Implement DMA support into mxs-i2c
Date: Tue, 1 May 2012 15:37:13 +0100	[thread overview]
Message-ID: <20120501143712.GB3548@n2100.arm.linux.org.uk> (raw)
In-Reply-To: <20120501135800.GL2194@S2101-09.ap.freescale.net>

On Tue, May 01, 2012 at 09:58:03PM +0800, Shawn Guo wrote:
> On Mon, Apr 30, 2012 at 11:01:08PM +0200, Wolfram Sang wrote:
> > 
> > > > > It complains that data might be undefined, do you want it in a separate
> > > > > patch then ?
> > > > 
> > > > Yes, but only if you can prove that the compiler is right.
> > > 
> > > It's not right, because that variable is always initialized, but this at least 
> > > squashes the compile warning.
> > 
> > The compiler needs to be fixed, not the kernel.
> > 
> Heh, this is coming up again.  I'm not convincing you to take the
> change, but just curious what if this is not a compile warning but
> an error for the same cause that compiler is not right.

It's a compiler warning.

Please look at the wording of the warning.  The compiler uses "may" not
"is".  That means it can't come to a conclusion about it.

But we, as humans with our biological inference engines, can see that
the code is correct, and we _can_ choose to ignore the warning -
just like I do with:

drivers/video/omap2/displays/panel-taal.c: In function 'taal_num_errors_show':
drivers/video/omap2/displays/panel-taal.c:605: warning: 'errors' may be used uninitialized in this function

static int taal_dcs_read_1(struct taal_data *td, u8 dcs_cmd, u8 *data)
{
        int r;
        u8 buf[1];
        r = dsi_vc_dcs_read(td->dssdev, td->channel, dcs_cmd, buf, 1);
        if (r < 0)
                return r;
        *data = buf[0];  
        return 0;
}

        u8 errors;

        mutex_lock(&td->lock);
        if (td->enabled) {
                dsi_bus_lock(dssdev);
                r = taal_wake_up(dssdev);
                if (!r) 
                        r = taal_dcs_read_1(td, DCS_READ_NUM_ERRORS, &errors);
                dsi_bus_unlock(dssdev);
        } else {
                r = -ENODEV;
        }
        mutex_unlock(&td->lock);

        if (r)
                return r;

        return snprintf(buf, PAGE_SIZE, "%d\n", errors);

See?  We can infer from the above that that 'errors' will always be set
if r is zero - but the compiler is saying it's not clever enough to do
that automatically.

This doesn't mean the code _has_ to be changed - we can see that the
above warning is false.  Though, if there is a way to rewrite it to make
it easier for us humans to read and that makes the warning go away, it
_might_ be a good idea to make the change - but only for a maintainability
reason.

The reverse is also true - if trying to fix a warning like this makes
the code more difficult to understand, then the warning _should_ stay.

That's actually the point of programming languages - the ability to
express our instructions to a computer in a way that both we and the
compiler can readily understand.  The most important part of that is
that _we_ understand what's been written.

  parent reply	other threads:[~2012-05-01 14:37 UTC|newest]

Thread overview: 44+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-04-29 22:36 [PATCH 1/2] I2C: Set I2C timing registers for mxs-i2c Marek Vasut
2012-04-29 22:36 ` Marek Vasut
     [not found] ` <1335738969-27445-1-git-send-email-marex-ynQEQJNshbs@public.gmane.org>
2012-04-29 22:36   ` [PATCH 2/2] I2C: Implement DMA support into mxs-i2c Marek Vasut
2012-04-29 22:36     ` Marek Vasut
     [not found]     ` <1335738969-27445-2-git-send-email-marex-ynQEQJNshbs@public.gmane.org>
2012-04-30  6:05       ` Wolfram Sang
2012-04-30  6:05         ` Wolfram Sang
     [not found]         ` <20120430060554.GB7926-bIcnvbaLZ9MEGnE8C9+IrQ@public.gmane.org>
2012-04-30 12:10           ` Marek Vasut
2012-04-30 12:10             ` Marek Vasut
     [not found]             ` <201204301410.14393.marex-ynQEQJNshbs@public.gmane.org>
2012-04-30 19:52               ` Wolfram Sang
2012-04-30 19:52                 ` Wolfram Sang
     [not found]                 ` <20120430195221.GB28226-bIcnvbaLZ9MEGnE8C9+IrQ@public.gmane.org>
2012-04-30 20:07                   ` Marek Vasut
2012-04-30 20:07                     ` Marek Vasut
     [not found]                     ` <201204302207.39112.marex-ynQEQJNshbs@public.gmane.org>
2012-04-30 20:30                       ` Wolfram Sang
2012-04-30 20:30                         ` Wolfram Sang
     [not found]                         ` <20120430203016.GD28226-bIcnvbaLZ9MEGnE8C9+IrQ@public.gmane.org>
2012-04-30 20:45                           ` Marek Vasut
2012-04-30 20:45                             ` Marek Vasut
     [not found]                             ` <201204302245.57958.marex-ynQEQJNshbs@public.gmane.org>
2012-04-30 21:01                               ` Wolfram Sang
2012-04-30 21:01                                 ` Wolfram Sang
     [not found]                                 ` <20120430210108.GE28226-bIcnvbaLZ9MEGnE8C9+IrQ@public.gmane.org>
2012-04-30 21:19                                   ` Marek Vasut
2012-04-30 21:19                                     ` Marek Vasut
2012-05-01 13:58                                   ` Shawn Guo
2012-05-01 13:58                                     ` Shawn Guo
     [not found]                                     ` <20120501135800.GL2194-rvtDTF3kK1ictlrPMvKcciBecyulp+rMXqFh9Ls21Oc@public.gmane.org>
2012-05-01 14:02                                       ` Marek Vasut
2012-05-01 14:02                                         ` Marek Vasut
     [not found]                                         ` <201205011602.56563.marex-ynQEQJNshbs@public.gmane.org>
2012-05-01 14:09                                           ` Shawn Guo
2012-05-01 14:09                                             ` Shawn Guo
     [not found]                                             ` <20120501140910.GM2194-rvtDTF3kK1ictlrPMvKcciBecyulp+rMXqFh9Ls21Oc@public.gmane.org>
2012-05-01 14:14                                               ` Marek Vasut
2012-05-01 14:14                                                 ` Marek Vasut
2012-05-01 14:37                                       ` Russell King - ARM Linux [this message]
2012-05-01 14:37                                         ` Russell King - ARM Linux
2012-04-30  5:58   ` [PATCH 1/2] I2C: Set I2C timing registers for mxs-i2c Wolfram Sang
2012-04-30  5:58     ` Wolfram Sang
     [not found]     ` <20120430055825.GA7926-bIcnvbaLZ9MEGnE8C9+IrQ@public.gmane.org>
2012-04-30 12:05       ` Marek Vasut
2012-04-30 12:05         ` Marek Vasut
     [not found]         ` <201204301405.42621.marex-ynQEQJNshbs@public.gmane.org>
2012-04-30 19:43           ` Wolfram Sang
2012-04-30 19:43             ` Wolfram Sang
     [not found]             ` <20120430194313.GA28226-bIcnvbaLZ9MEGnE8C9+IrQ@public.gmane.org>
2012-04-30 20:09               ` Marek Vasut
2012-04-30 20:09                 ` Marek Vasut
     [not found]                 ` <201204302209.12482.marex-ynQEQJNshbs@public.gmane.org>
2012-04-30 20:27                   ` Wolfram Sang
2012-04-30 20:27                     ` Wolfram Sang
     [not found]                     ` <20120430202742.GC28226-bIcnvbaLZ9MEGnE8C9+IrQ@public.gmane.org>
2012-04-30 20:47                       ` Marek Vasut
2012-04-30 20:47                         ` Marek Vasut
     [not found]                         ` <201204302247.17242.marex-ynQEQJNshbs@public.gmane.org>
2012-04-30 21:02                           ` Wolfram Sang
2012-04-30 21:02                             ` Wolfram Sang

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20120501143712.GB3548@n2100.arm.linux.org.uk \
    --to=linux-lfz/pmaqli7xmaaqvzeohq@public.gmane.org \
    --cc=dzu-ynQEQJNshbs@public.gmane.org \
    --cc=festevam-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org \
    --cc=linux-arm-kernel-IAPFreCvJWM7uuMidbF8XUB+6BGkLq7r@public.gmane.org \
    --cc=linux-i2c-u79uwXL29TY76Z2rM5mHXA@public.gmane.org \
    --cc=marex-ynQEQJNshbs@public.gmane.org \
    --cc=sbabic-ynQEQJNshbs@public.gmane.org \
    --cc=shawn.guo-QSEj5FYQhm4dnm+yROfE0A@public.gmane.org \
    --cc=w.sang-bIcnvbaLZ9MEGnE8C9+IrQ@public.gmane.org \
    --cc=wd-ynQEQJNshbs@public.gmane.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.