Coccinelle is a program matching and transformation engine which provides the language SmPL (Semantic Patch Language) for specifying desired matches and transformations in C code.
## install see http://coccinelle.lip6.fr/download.php
$ sudo apt-get install coccinelle | sudo yum install coccinelle (from fedora rawhide)
## usage
spatch -sp_file <SP> <files> [-o <outfile> ] [-iso_file <iso> ] [ options ]
## examples
$ cat test.cocci
// Replaces calls to alloca by malloc and checks return value
@@
expression E;
identifier ptr;
@@
-ptr = alloca(E);
+ptr = malloc(E);
+if (ptr == NULL)
+ return 1;
$ cat test.c
#include <alloca.h>
int main(int argc, char *argv[]) {
unsigned int bytes = 1024 * 1024;
char *buf;
/* allocate memory */
buf = alloca(bytes);
return 0;
}
$ spatch -sp_file test.cocci test.c
--- test.c
+++ /tmp/cocci-output-29896-40280c-test.c
@@ -3,6 +3,8 @@ int main(int argc, char *argv[]) {
unsigned int bytes = 1024 * 1024;
char *buf;
/* allocate memory */
- buf = alloca(bytes);
+ buf = malloc(bytes);
+ if (buf == NULL)
+ return 1;
return 0;
}
from coccinelle, coccinelle@lwn, coccinelle for the newbie and coccinelle patch examples