On Sun, 11 Mar 2012, Ajay Panyala wrote:

Hello,

Suppose we have the code:

int foo (...){...}

Could I write a patch that adds a duplicate function definition for foo.

The resulting code would look like

int foo (...) {...}
int bar (...) {...}

where foo and bar have the exact same parameters and function bodies.

It is possible, but the solution is not very appealing. The problem is that there is no metavariable that matches the body of a function. So we have to wrap the body of the function in extra braces. Then for this nested returns are a problem, so we have to get rid of them. So there are two preprocessing rules, one rule to do the transformation, and then two rules to undo the preprocessing. The resulting spacing is not ideal either. The new function is double indented due to the extra braces, and a space is deleted before the { in the old function, I guess due to a pretty printing bug.

In case it's useful though, the semantic patch is below.

julia

@r@
identifier f;
expression e;
@@

f(...)
{
<...
(
-return e;
+ RETURN(e);
|
-return;
-RETURN;
)
...>
}

@@
identifier r.f;
@@

f(...)
+ {
{
...
}
+}

@@
identifier r.f;
parameter list p;
statement S;
type T;
@@

T f(p) {S}
+T bar(p) {S}

@@
identifier f; /* any function */
@@

f(...) {
-{
...
-}
}

@@
expression e;
@@

(
- RETURN(e);
+ return e;
|
- RETURN;
+ return;
)

_______________________________________________
Cocci mailing list
[email protected]
http://lists.diku.dk/mailman/listinfo/cocci
(Web access from inside DIKUs LAN only)

Reply via email to