Question: I have a function:

```
void srunner_free(SRunner *sr){
.......
}

```

I am looking to prepend some C code before the original function body, so it becomes:

```
void srunner_free(SRunner *sr){
//Some other random code, not necessary printfs, printf is just an example
printf("hello world");

.......
}

```

How do I go about doing this? can I do it with tools such as grep?

THanks

Answer: 
You should be able to use [coccinelle](http://coccinelle.lip6.fr/) for this. This is a tool specifically made to modify source code like that.

Playing very quickly, with the following test input

```
$ cat test.c

int foo(int i)
{
        return i+1;
}

void srunner_free(SRunner *sr)
{
        int i;
        foo(i);
        return;
}

```

and the following coccinelle script

```
$ cat test.cocci 
@@
typedef SRunner;
statement S;
identifier sr;
@@
void srunner_free(SRunner *sr)
{
        ...
+       printf("hello world");
        S
        ...
}

```

the result is 

```
$ spatch -sp_file test.cocci test.c
init_defs_builtins: /usr/share/coccinelle/standard.h
HANDLING: test.c
diff = 
--- test.c      2014-03-08 01:34:13.000000000 +0100
+++ /tmp/cocci-output-24589-dfa2db-test.c       2014-03-08 01:34:57.000000000 +0100
@@ -7,7 +7,9 @@ int foo(int i)
 void srunner_free(SRunner *sr)
 {
        int i;
+       printf("hello world");
        foo(i);
+       printf("hello world");
        return;
 }

$

```

The printf statement here is put in front of every single statement which is not exactly what you want, but the result is not that far off. I am not sure how to restrict this right of the top of my head, but I think I have seen examples of this before, so it should be possible.

---

**Update:** I continued looking into this today, and it was relatively easy to modify what
I initially suggested to be exactly the solution wanted. The script now consists of two rules.

The first rule is just a matching rule, performing no modifications. It matches two adjacent statements and binds a position to the last one of those. So this rule will bind a 
position to all statements inside the function except the very first statement.

The second rule then inserts the printf statement (or whatever) in front of the first statement by excluding any of the statements bound by a position in rule1.

```
@rule1@
typedef SRunner;
statement S1, S2;
identifier sr;
position p;
@@
void srunner_free(SRunner *sr)
{
        ...
        S1
        S2@p
        ...
}

// typedefs are actually global, so no need to repeat
// (and doing so would in fact generate an error)
@rule2@
statement S;
identifier sr;
position p != rule1.p;
@@
void srunner_free(SRunner *sr)
{
        ...
+       printf("hello world");
        S@p
        ...
}

```
If you want to do this properly on every function, or a large number of functions, and want to do it properly, you will either need to alter the compiler, or build a lexical analyser (using, say `lex`) that uses the same rules as C, and outputs the extra lines.

If you want to bodge it up for a few functions, and are prepared for criteria like 'no macros, normal formatting etc', use a scripting language like `perl`, and regular expressions. This is, however, always going to be a bodge.