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


Groups > linux.kernel > #1357619

Re: coccinelle: generalized removal of unnecessary pointer casts?

From Julia Lawall <julia.lawall@lip6.fr>
Newsgroups linux.kernel
Subject Re: coccinelle: generalized removal of unnecessary pointer casts?
Date 2016-03-14 21:50 +0100
Message-ID <rcHB8-pu-25@gated-at.bofh.it> (permalink)
References <rcFSG-7FX-15@gated-at.bofh.it>
Organization linux.* mail to news gateway

Show all headers | View raw


[Multipart message — attachments visible in raw view] - view raw

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

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


Thread

coccinelle: generalized removal of unnecessary pointer casts? Joe Perches <joe@perches.com> - 2016-03-14 20:00 +0100
  Re: coccinelle: generalized removal of unnecessary pointer casts? Julia Lawall <julia.lawall@lip6.fr> - 2016-03-14 21:50 +0100
    Re: coccinelle: generalized removal of unnecessary pointer casts? Joe Perches <joe@perches.com> - 2016-03-14 23:30 +0100
      Re: coccinelle: generalized removal of unnecessary pointer casts? Julia Lawall <julia.lawall@lip6.fr> - 2016-03-15 07:00 +0100

csiph-web