* coccinelle: generalized removal of unnecessary pointer casts? @ 2016-03-14 18:54 Joe Perches 2016-03-14 20:43 ` Julia Lawall 0 siblings, 1 reply; 4+ messages in thread From: Joe Perches @ 2016-03-14 18:54 UTC (permalink / raw) To: Julia Lawall; +Cc: cocci, LKML, Dan Carpenter, kernel-janitors 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) ^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: coccinelle: generalized removal of unnecessary pointer casts? 2016-03-14 18:54 coccinelle: generalized removal of unnecessary pointer casts? Joe Perches @ 2016-03-14 20:43 ` Julia Lawall 2016-03-14 22:23 ` Joe Perches 0 siblings, 1 reply; 4+ messages in thread From: Julia Lawall @ 2016-03-14 20:43 UTC (permalink / raw) To: Joe Perches; +Cc: cocci, LKML, Dan Carpenter, kernel-janitors [-- Attachment #1: Type: TEXT/PLAIN, Size: 1458 bytes --] 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 ^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: coccinelle: generalized removal of unnecessary pointer casts? 2016-03-14 20:43 ` Julia Lawall @ 2016-03-14 22:23 ` Joe Perches 2016-03-15 5:54 ` Julia Lawall 0 siblings, 1 reply; 4+ messages in thread From: Joe Perches @ 2016-03-14 22:23 UTC (permalink / raw) To: Julia Lawall; +Cc: cocci, LKML, Dan Carpenter, kernel-janitors 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 ^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: coccinelle: generalized removal of unnecessary pointer casts? 2016-03-14 22:23 ` Joe Perches @ 2016-03-15 5:54 ` Julia Lawall 0 siblings, 0 replies; 4+ messages in thread From: Julia Lawall @ 2016-03-15 5:54 UTC (permalink / raw) To: Joe Perches; +Cc: cocci, LKML, Dan Carpenter, kernel-janitors [-- Attachment #1: Type: TEXT/PLAIN, Size: 2315 bytes --] On Mon, 14 Mar 2016, Joe Perches wrote: > 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. OK, that could be added, but I wonder why it is necessary? Isn't one pointer type just as good as any other, since the value will just get casted to the pointer type of the parameter in the end anyway? julia ^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2016-03-15 5:54 UTC | newest] Thread overview: 4+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- 2016-03-14 18:54 coccinelle: generalized removal of unnecessary pointer casts? Joe Perches 2016-03-14 20:43 ` Julia Lawall 2016-03-14 22:23 ` Joe Perches 2016-03-15 5:54 ` Julia Lawall
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox