I ran into a spacing problem with return and its expression, which in
some cases can lead to spatch generating invalid code.

The semantic patch I wrote, intending to remove unnecessary parentheses
around return expressions:

@@
expression E;
@@
 return
-(
 E
-)
 ;

A test case:

int f(int e)
{
    /* Correct: */
    return(e);
    return(f(5));
    return(e?1:2);
    return(sizeof("hello"));

    /* Spacing problem: */
    return(1);
    return(-1);
    return(1+e);
    return(1?1:2);
    return(!!1);
    return(!!e);
    return(~1);
    return('h');
    return("hello");
    return((int)"hello");
}

The results:

--- /tmp/t.c    2010-06-23 13:54:21.000000000 -0700
+++ /tmp/cocci-output-6528-9b002c-t.c   2010-06-23 14:00:26.000000000 -0700
@@ -1,20 +1,20 @@
 int f(int e)
 {
     /* Correct: */
-    return(e);
-    return(f(5));
-    return(e?1:2);
-    return(sizeof("hello"));
+    return e;
+    return f(5);
+    return e?1:2;
+    return sizeof("hello");
 
     /* Spacing problem: */
-    return(1);
-    return(-1);
-    return(1+e);
-    return(1?1:2);
-    return(!!1);
-    return(!!e);
-    return(~1);
-    return('h');
-    return("hello");
-    return((int)"hello");
+    return1;
+    return-1;
+    return1+e;
+    return1?1:2;
+    return!!1;
+    return!!e;
+    return~1;
+    return'h';
+    return"hello";
+    return(int)"hello";
 }


Note that in some cases the generated code will no longer parse
correctly, such as "return1" instead of "return 1".


Also, regarding this semantic patch, I noticed that if I have a return
with multiple parentheses, such as "return ((e));", spatch will only
remove one set of parentheses.  Does any means exist to tell spatch to
apply a particular patch hunk repeatedly until no further matches exist,
or do I need to do that manually by re-running spatch?

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

Reply via email to