Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > linux.kernel > #1175585

Re: RFC: kernel coding style: prefer array to &array[0] ?

Path csiph.com!aioe.org!bofh.it!news.nic.it!robomod
From Dmitry Torokhov <dmitry.torokhov@gmail.com>
Newsgroups linux.kernel
Subject Re: RFC: kernel coding style: prefer array to &array[0] ?
Date Thu, 02 Jul 2015 00:40:02 +0200
Message-ID <pHyPE-2MU-13@gated-at.bofh.it> (permalink)
References <pH7FM-ic-17@gated-at.bofh.it> <pHoQj-4Hd-23@gated-at.bofh.it> <pHp9E-53k-5@gated-at.bofh.it> <pHpjk-56P-19@gated-at.bofh.it> <pHrEt-6sY-9@gated-at.bofh.it>
X-Original-To Joe Perches <joe@perches.com>
Dkim-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=date:from:to:cc:subject:message-id:references:mime-version :content-type:content-disposition:in-reply-to:user-agent; bh=8vi329Uj+umLa3P+rXKbxdpOEkxuTTkycs/svb6YMNU=; b=Iwt3PDGCL738IZmSbz38Q2NHwB+jfapqWcpYElsXVSurcRHpueZwmrIY13e162D312 5LVHV+VM0SGbKhyrAiR9VKbBgp4i+APxnXB1l4ANI+5A5AcdCBvaOunPPmHWU8RRuP+b jJXrcWGD7ZmppiHKXr9WTtdDGg0+syCF6SXfqlKV6BNcXXnF7WPsQIhdK02ixvpHEy6g RrX2uFMPCYLK+/i2mz2JtmRJYMOSJ+K0z3NyzS02S1ffyBbxQAxsE0WKQRRUnJ2zl6Us NRsE6+Vtt09Ld0NCjrR3Bp16o36hm/k48A8OSZ817YmQGHHwp/+5mOF+3eOd+cYQjxSS YSDg==
X-Received by 10.50.110.3 with SMTP id hw3mr9252958igb.20.1435789995378; Wed, 01 Jul 2015 15:33:15 -0700 (PDT)
MIME-Version 1.0
Content-Type text/plain; charset=us-ascii
Content-Disposition inline
User-Agent Mutt/1.5.21 (2010-09-15)
Sender robomod@news.nic.it
List-ID <linux-kernel.vger.kernel.org>
X-Mailing-List linux-kernel@vger.kernel.org
Approved robomod@news.nic.it
Lines 79
Organization linux.* mail to news gateway
X-Original-Cc Julia Lawall <julia.lawall@lip6.fr>, Dan Carpenter <dan.carpenter@oracle.com>, Clemens Ladisch <clemens@ladisch.de>, LKML <linux-kernel@vger.kernel.org>, Andrew Morton <akpm@linux-foundation.org>, Liam Girdwood <lgirdwood@gmail.com>, Mark Brown <broonie@kernel.org>
X-Original-Date Wed, 1 Jul 2015 15:33:11 -0700
X-Original-Message-ID <20150701223311.GE36579@dtor-ws>
X-Original-References <1435685746.12101.18.camel@perches.com> <5593D4F5.1090106@ladisch.de> <20150701121524.GA30834@mwanda> <alpine.DEB.2.10.1507011426010.2374@hadrien> <1435762424.12101.95.camel@perches.com>
X-Original-Sender linux-kernel-owner@vger.kernel.org
Xref aioe.org linux.kernel:1175585

Show key headers only | View raw


On Wed, Jul 01, 2015 at 07:53:44AM -0700, Joe Perches wrote:
> On Wed, 2015-07-01 at 14:26 +0200, Julia Lawall wrote:
> > On Wed, 1 Jul 2015, Dan Carpenter wrote:
> > > On Wed, Jul 01, 2015 at 01:54:29PM +0200, Clemens Ladisch wrote:
> > > > Joe Perches wrote:
> > > > > It seems most in-kernel uses are 'array' rather than '&array[0]'
> > > > >
> > > > > Most of the time, using array is simpler to read than &array[0].
> > > > >
> > > > > Exceptions exists when addresses for consecutive members are
> > > > > used like func(&array[0], &array[1]);
> > > >
> > > > I use '&array[0]' when I want to get a pointer to a single object that
> > > > happens to be the first one in an array.
> > >
> > > Yeah.  Of course, you're right.  Otherwise it ends up confusing static
> > > checkers if you want the first element or the whole array.
> 
> Right.
> 
> > > > > Should this preference be put into checkpatch and/or CodingStyle?
> 
> And checkpatch will have no idea what the prototype
> for any function is, so this transform is better left
> for smarter tools like coccinelle.
> 
> The proper answer here is no.
> 
> > > > How about the following low-hanging fruit?
> > > >
> > > >   foo(..., &array[0], ARRAY_SIZE(array), ...)
> > >
> > > Yes, to this also.  I doubt checkpatch.pl will find a meaningful number
> > > of these but doing that is annoying thing.
> > 
> > Atcually, I find 236 of them, in 48 files.
> 
> The uses I found:
> 
> drivers/input/touchscreen nas a few

I got curious so I ran the proposed patch over drivers/input/touchscreen
and it produced the following gems:

CHECK: Using addressof array 'data' index [0] may be simpler as 'data'
#49: FILE: drivers/input/touchscreen/dynapro.c:49:
+#define DYNAPRO_GET_TOUCHED(data) (DYNAPRO_FORMAT_TOUCH_BIT & data[0])


CHECK: Using addressof array 'mtouch->data' index [0] may be simpler as
'mtouch->data'
#97: FILE: drivers/input/touchscreen/mtouch.c:97:
+       if (MTOUCH_FORMAT_TABLET_STATUS_BIT & mtouch->data[0])

... etc.


While below can be written as just "msg" in many cases when you parse
several fields in the structure the original is actually cleaner:

CHECK: Using addressof array 'msg' index [0] may be simpler as 'msg'
#38: FILE: drivers/input/touchscreen/ipaq-micro-ts.c:38:
+                                be16_to_cpup((__be16 *) &msg[0]));

I'd be OK with changing cases like:

CHECK: Using addressof array 'buf' index [0] may be simpler as 'buf'
#232: FILE: drivers/input/touchscreen/zforce_ts.c:232:
+       return zforce_send_wait(ts, &buf[0], ARRAY_SIZE(buf));

Thanks.

-- 
Dmitry
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/

Back to linux.kernel | Previous | Next — Next in thread | Find similar | Unroll thread


Thread

Re: RFC: kernel coding style: prefer array to &array[0] ? Dmitry Torokhov <dmitry.torokhov@gmail.com> - 2015-07-02 00:40 +0200
  Re: RFC: kernel coding style: prefer array to &array[0] ? Joe Perches <joe@perches.com> - 2015-07-02 01:10 +0200

csiph-web