On Wed, 31 Mar 2010, Flavien wrote:
> Hello,
>
>
> I have about 800 occurences of constructs like this one :
> if (ptr) {
> MEMO_FREE(ptr);
> ptr = NULL;
> }
>
> I would like to simplify it to a new API :
> ng_free(&ptr);
>
> It works well with the following rule :
> // <smpl>
> @@
> identifier I;
> @@
> - if (I) {
> - MEMO_FREE(I);
> - I = NULL;
> - }
> + ng_free(&I);
> // </smpl>
>
>
> However, the code also has constructs :
> * without the enclosing test on "ptr".
> * "if (ptr != NULL)" instead of "if (ptr)"
> * without the "ptr = NULL" assignment
> * with the assignment outside of the test
>
> I'm having a hard time writing the semantic patch corresponding to
> this rework without explicitely specifying all combinations.
I haven't actually tried out your code, but it looks reasonable. One idea
would be to put if (ptr != NULL) rather than if (ptr). Then an
isomorphism will transform it to either if(ptr) or if(NULL != ptr) or if
(ptr != NULL).
You could also consider whether ptr could be an arbitrary expression,
rather than just an identifier. An identifier in Coccinelle is just a
string of characters. It won't match x->y.
julia
> Below is the example I'm playing with as well as the rule file I
> working on... Any help appreciated ! :o)
>
> Flavien.
> ---
> #include <stdlib.h>
>
> #define MEMO_FREE(ptr) free(ptr)
> void new_free(void **ptr)
> {
> free(*ptr);
> *ptr = NULL;
> }
>
> void f1(void)
> {
> char *ptr = malloc(12);
>
> if (ptr) {
> MEMO_FREE(ptr);
> }
> }
>
> void f2(void)
> {
> char *pt = malloc(12);;
> if (ptr) {
> MEMO_FREE(ptr);
> ptr = NULL;
> }
> }
>
> void f3(void)
> {
> char *ptr = malloc(12);
> if (ptr != NULL) {
> MEMO_FREE(ptr);
> }
> ptr = NULL;
> }
> ---
> // <smpl>
> @@
> identifier I;
> @@
> - if (I) {
> - MEMO_FREE(I);
> - I = NULL;
> - }
> + new_free(&I);
>
>
> @@
> identifier I;
> @@
> - if (I) {
> - MEMO_FREE(I);
> - }
> + new_free(&I);
>
>
> @@
> identifier I;
> @@
> - MEMO_FREE(I);
> - I = NULL;
> + new_free(&I);
>
>
> @@
> identifier I;
> @@
> - MEMO_FREE(I);
> + new_free(&I);
> // </smpl>
> _______________________________________________
> Cocci mailing list
> [email protected]
> http://lists.diku.dk/mailman/listinfo/cocci
> (Web access from inside DIKUs LAN only)
>
_______________________________________________
Cocci mailing list
[email protected]
http://lists.diku.dk/mailman/listinfo/cocci
(Web access from inside DIKUs LAN only)