From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1756098AbcCNUny (ORCPT ); Mon, 14 Mar 2016 16:43:54 -0400 Received: from mail3-relais-sop.national.inria.fr ([192.134.164.104]:5647 "EHLO mail3-relais-sop.national.inria.fr" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1750741AbcCNUnw (ORCPT ); Mon, 14 Mar 2016 16:43:52 -0400 X-IronPort-AV: E=Sophos;i="5.24,337,1454972400"; d="scan'208";a="168625837" Date: Mon, 14 Mar 2016 21:43:48 +0100 (CET) From: Julia Lawall X-X-Sender: jll@localhost6.localdomain6 To: Joe Perches cc: cocci , LKML , Dan Carpenter , kernel-janitors Subject: Re: coccinelle: generalized removal of unnecessary pointer casts? In-Reply-To: <1457981670.11972.114.camel@perches.com> Message-ID: References: <1457981670.11972.114.camel@perches.com> User-Agent: Alpine 2.02 (DEB 1266 2009-07-14) MIME-Version: 1.0 Content-Type: MULTIPART/MIXED; BOUNDARY="8323328-1368920477-1457988229=:2040" Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org This message is in MIME format. The first part should be readable text, while the remaining parts are likely unreadable without MIME-aware tools. --8323328-1368920477-1457988229=:2040 Content-Type: TEXT/PLAIN; charset=ISO-8859-1 Content-Transfer-Encoding: 8BIT 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 --8323328-1368920477-1457988229=:2040--