From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753188AbcCNWYF (ORCPT ); Mon, 14 Mar 2016 18:24:05 -0400 Received: from smtprelay0189.hostedemail.com ([216.40.44.189]:57414 "EHLO smtprelay.hostedemail.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1751391AbcCNWYC (ORCPT ); Mon, 14 Mar 2016 18:24:02 -0400 X-Session-Marker: 6A6F6540706572636865732E636F6D X-Spam-Summary: 2,0,0,,d41d8cd98f00b204,joe@perches.com,:::::::::,RULES_HIT:41:355:379:541:599:968:982:988:989:1260:1277:1311:1313:1314:1345:1359:1373:1437:1515:1516:1518:1534:1542:1593:1594:1711:1730:1747:1777:1792:2110:2393:2553:2559:2562:2828:3138:3139:3140:3141:3142:3355:3622:3865:3866:3867:3868:3870:3871:3872:3874:4321:5007:6119:6261:7903:7904:8660:10004:10400:10450:10455:10848:11026:11232:11658:11783:11914:12043:12295:12438:12517:12519:12740:13071:13148:13161:13229:13230:13439:13894:14659:19904:19999:21060:21080:30054:30069:30090:30091,0,RBL:none,CacheIP:none,Bayesian:0.5,0.5,0.5,Netcheck:none,DomainCache:0,MSF:not bulk,SPF:fn,MSBL:0,DNSBL:none,Custom_rules:0:0:0,LFtime:1,LUA_SUMMARY:none X-HE-Tag: men37_22d83e13e9661 X-Filterd-Recvd-Size: 2945 Message-ID: <1457994238.11972.138.camel@perches.com> Subject: Re: coccinelle: generalized removal of unnecessary pointer casts? From: Joe Perches To: Julia Lawall Cc: cocci , LKML , Dan Carpenter , kernel-janitors Date: Mon, 14 Mar 2016 15:23:58 -0700 In-Reply-To: References: <1457981670.11972.114.camel@perches.com> Content-Type: text/plain; charset="ISO-8859-1" X-Mailer: Evolution 3.18.5.1-1ubuntu1 Mime-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, 2016-03-14 at 21:43 +0100, Julia Lawall wrote: > On Mon, 14 Mar 2016, Joe Perches wrote: > > I wrote a little cocci script to remove unnecessary > > casts for memset and memcpy (below) and tested it on > > linux kernel's drivers/staging/ directory. > >  > > For instance, when dst and src are already pointers: > >  > > -     memcpy((u8 *)dst, (u8 *)src, r8712_get_wlan_bssid_ex_sz(src)); > > +     memcpy(dst, src, r8712_get_wlan_bssid_ex_sz(src)); > >  > > It works ok, (it doesn't remove unnecessary parentheses > > around the pointers) but it makes me wonder if there's a > > generalized spatch mechanism to remove casts when an > > arbitrary function takes a void * in any argument > > position and a call to that function uses a cast of a > > pointer to any pointer type for that argument. > >  > > $ cat remove_mem_casts.cocci  > > @@ > > type t; > > t *p; > > type v; > > expression e1; > > expression e2; > > @@ > >  > > -     memset((v*)p, e1, e2) > > +     memset(p, e1, e2) > >  > > @@ > > type t; > > t *p; > > type v; > > expression e1; > > expression e2; > > @@ > >  > > -     memcpy((v*)p, e1, e2) > > +     memcpy(p, e1, e2) > >  > > @@ > > type t; > > t *p; > > type v; > > expression e1; > > expression e2; > > @@ > >  > > -     memcpy(e1, (v*)p, e2) > > +     memcpy(e1, p, e2) > >  > > @@ > > type t1; > > type t2; > > t1 *p1; > > t2 *p2; > > type v1; > > type v2; > > expression e1; > > @@ > >  > > -     memcpy((v1*)p1, (v2*)p2, e1) > > +     memcpy(p1, p2, e1) > > This should do everything: > > @@ > identifier f; > expression *e; > type T; > @@ > > f(..., > - (T *)( >   e > - ) >   ,...) > > @@ > identifier f; > expression *e; > type T; > @@ > > f(..., > - (T *) >   e >   ,...) > > julia Hi Julia, I think your proposed script is not correct. The function must take a void * argument. There's no validation of that here. cheers, Joe