On 5 January 2012 14:56, Robert Gomulka <[email protected]> wrote:
> is it possible to combine positive and negative regular expressions?
> As I said above, I'd rather drop them altogether, however I'm just
> curious whether it's possible to search for functions ending with
> malloc, but not "bad_malloc" and "another_malloc" (because they have
> separate API)?
Yes. Create a rule to just match the malloc functions you want to treat
different and bind the match with a position. Then in the rule where
you do the transformation use a position variable that you bind to
be everything but the first position. E.g.
(hlovdal) localhost:/download/2012/07_jul/cocci>more malloc_fix.c*
::::::::::::::
malloc_fix.c
::::::::::::::
void test(void)
{
char *p1, *p2;
malloc(p1);
different_malloc(p1);
}
::::::::::::::
malloc_fix.cocci
::::::::::::::
@rule1@
position p1;
@@
different_malloc@p1(...)
@rule2@
identifier malloc_function =~ "malloc$";
position p2 != rule1.p1;
type T;
T *x;
@@
malloc_function@p2
(
- x
+ *(x)
)
(hlovdal) localhost:/download/2012/07_jul/cocci>spatch -sp_file
malloc_fix.cocci malloc_fix.c
init_defs_builtins: /usr/share/coccinelle/standard.h
HANDLING: malloc_fix.c
diff =
--- malloc_fix.c
+++ /tmp/cocci-output-21515-8dc9a9-malloc_fix.c
@@ -2,7 +2,7 @@
void test(void)
{
char *p1, *p2;
- malloc(p1);
+ malloc(*(p1));
different_malloc(p1);
}
(hlovdal) localhost:/download/2012/07_jul/cocci>
BR Håkon Løvdal
_______________________________________________
Cocci mailing list
[email protected]
http://lists.diku.dk/mailman/listinfo/cocci
(Web access from inside DIKUs LAN only)